From 73d348ea96b037d7b673c316aa2549ccf0e79e93 Mon Sep 17 00:00:00 2001 From: mtdxc Date: Tue, 30 Sep 2025 11:05:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dtest=5Fplayer=E6=B2=A1?= =?UTF-8?q?=E5=A3=B0=E9=9F=B3=E5=92=8Cwindows=E6=8E=A7=E5=88=B6=E5=8F=B0?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=B8=8D=E8=BE=93=E5=87=BAbug=20(#4484)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- player/SDLAudioDevice.cpp | 19 +++++++++++++------ player/SDLAudioDevice.h | 1 + player/test_player.cpp | 18 ++++++++++-------- src/Codec/Transcode.cpp | 9 +++++++++ src/Codec/Transcode.h | 1 + 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/player/SDLAudioDevice.cpp b/player/SDLAudioDevice.cpp index 14f74add..709c95e4 100644 --- a/player/SDLAudioDevice.cpp +++ b/player/SDLAudioDevice.cpp @@ -18,7 +18,10 @@ using namespace toolkit; INSTANCE_IMP(SDLAudioDevice); SDLAudioDevice::~SDLAudioDevice() { - SDL_CloseAudio(); + if (_device) { + SDL_CloseAudioDevice(_device); + _device = 0; + } } SDLAudioDevice::SDLAudioDevice() { @@ -33,9 +36,13 @@ SDLAudioDevice::SDLAudioDevice() { SDLAudioDevice *_this = (SDLAudioDevice *) userdata; _this->onReqPCM((char *) stream, len); }; - if (SDL_OpenAudioDevice(NULL, 0, &wanted_spec, &_audio_config, SDL_AUDIO_ALLOW_ANY_CHANGE) < 0) { - throw std::runtime_error("SDL_OpenAudioDevice failed"); - } + + _device = SDL_OpenAudioDevice(NULL, 0, &wanted_spec, &_audio_config, 0); + if (_device <= 0) + _device = SDL_OpenAudioDevice(NULL, 0, &wanted_spec, &_audio_config, SDL_AUDIO_ALLOW_ANY_CHANGE); + if (_device <= 0) { + throw std::runtime_error("SDL_OpenAudioDevice failed"); + } InfoL << "actual audioSpec, " << "freq:" << _audio_config.freq << ", format:" << hex << _audio_config.format << dec @@ -51,7 +58,7 @@ SDLAudioDevice::SDLAudioDevice() { void SDLAudioDevice::addChannel(AudioSRC *chn) { lock_guard lck(_channel_mtx); if (_channels.empty()) { - SDL_PauseAudio(0); + SDL_PauseAudioDevice(_device, false); } chn->setOutputAudioConfig(_audio_config); _channels.emplace(chn); @@ -61,7 +68,7 @@ void SDLAudioDevice::delChannel(AudioSRC *chn) { lock_guard lck(_channel_mtx); _channels.erase(chn); if (_channels.empty()) { - SDL_PauseAudio(true); + SDL_PauseAudioDevice(_device, true); } } diff --git a/player/SDLAudioDevice.h b/player/SDLAudioDevice.h index bddea9cb..69320995 100644 --- a/player/SDLAudioDevice.h +++ b/player/SDLAudioDevice.h @@ -40,6 +40,7 @@ private: void onReqPCM(char *stream, int len); private: + SDL_AudioDeviceID _device; std::shared_ptr _play_buf; SDL_AudioSpec _audio_config; std::recursive_mutex _channel_mtx; diff --git a/player/test_player.cpp b/player/test_player.cpp index bfeb5a0d..9904dfd5 100644 --- a/player/test_player.cpp +++ b/player/test_player.cpp @@ -41,6 +41,10 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstanc, LPSTR lpCmdLine, freopen_s(&stream, "CON", "r", stdin);//重定向输入流 freopen_s(&stream, "CON", "w", stdout);//重定向输入流 + // 清除流缓冲区, 在win11上还是无法输出文字,需要在加入如下代码 + std::cin.clear(); + std::cout.clear(); + //3. 如果我们需要用到控制台窗口句柄,可以调用FindWindow取得: HWND _consoleHwnd; SetConsoleTitleA("test_player");//设置窗口名 @@ -56,8 +60,8 @@ int main(int argc, char *argv[]) { Logger::Instance().add(std::make_shared()); Logger::Instance().setWriter(std::make_shared()); - if (argc < 3) { - ErrorL << "\r\n测试方法:./test_player rtxp_url rtp_type\r\n" + if (argc < 2) { + ErrorL << "\r\n测试方法:./test_player rtxp_url [rtp_type] [play_track]\r\n" << "例如:./test_player rtsp://admin:123456@127.0.0.1/live/0 0\r\n"; return 0; } @@ -96,18 +100,15 @@ int main(int argc, char *argv[]) { FFmpegSwr::Ptr swr; decoder->setOnDecode([audio_player, swr](const FFmpegFrame::Ptr &frame) mutable { - int chs = 0; if (!swr) { # if LIBAVCODEC_VERSION_INT >= FF_CODEC_VER_7_1 swr = std::make_shared(AV_SAMPLE_FMT_S16, &(frame->get()->ch_layout), frame->get()->sample_rate); - chs = (&frame->get()->ch_layout)->nb_channels; #else swr = std::make_shared(AV_SAMPLE_FMT_S16, frame->get()->channels, frame->get()->channel_layout, frame->get()->sample_rate); - chs = frame->get()->channels; #endif } auto pcm = swr->inputFrame(frame); - auto len = pcm->get()->nb_samples * chs * av_get_bytes_per_sample((enum AVSampleFormat)pcm->get()->format); + auto len = pcm->get()->nb_samples * pcm->getChannels() * av_get_bytes_per_sample((enum AVSampleFormat)pcm->get()->format); audio_player->playPCM((const char *)(pcm->get()->data[0]), MIN(len, frame->get()->linesize[0])); }); audioTrack->addDelegate([decoder](const Frame::Ptr &frame) { return decoder->inputFrame(frame, false, true); }); @@ -115,10 +116,11 @@ int main(int argc, char *argv[]) { }); player->setOnShutdown([](const SockException &ex) { WarnL << "play shutdown: " << ex.what(); }); - - (*player)[Client::kRtpType] = atoi(argv[2]); // 不等待track ready再回调播放成功事件,这样可以加快秒开速度 (*player)[Client::kWaitTrackReady] = false; + if (argc > 2) { + (*player)[Client::kRtpType] = atoi(argv[2]); + } if (argc > 3) { (*player)[Client::kPlayTrack] = atoi(argv[3]); } diff --git a/src/Codec/Transcode.cpp b/src/Codec/Transcode.cpp index 0851b783..5e143599 100644 --- a/src/Codec/Transcode.cpp +++ b/src/Codec/Transcode.cpp @@ -247,6 +247,15 @@ void FFmpegFrame::fillPicture(AVPixelFormat target_format, int target_width, int av_image_fill_arrays(_frame->data, _frame->linesize, (uint8_t *) _data, target_format, target_width, target_height, 32); } +int FFmpegFrame::getChannels() const { + if (!_frame) return 0; +#if LIBAVCODEC_VERSION_INT >= FF_CODEC_VER_7_1 + return _frame->ch_layout.nb_channels; +#else + return _frame->channels; +#endif +} + /////////////////////////////////////////////////////////////////////////// template diff --git a/src/Codec/Transcode.h b/src/Codec/Transcode.h index 61f1a6a5..6b7b212c 100644 --- a/src/Codec/Transcode.h +++ b/src/Codec/Transcode.h @@ -44,6 +44,7 @@ public: AVFrame *get() const; void fillPicture(AVPixelFormat target_format, int target_width, int target_height); + int getChannels() const; private: char *_data = nullptr;