修复test_player没声音和windows控制台日志不输出bug (#4484)

This commit is contained in:
mtdxc
2025-09-30 11:05:01 +08:00
committed by GitHub
parent b9d513f9aa
commit 73d348ea96
5 changed files with 34 additions and 14 deletions

View File

@@ -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<recursive_mutex> 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<recursive_mutex> lck(_channel_mtx);
_channels.erase(chn);
if (_channels.empty()) {
SDL_PauseAudio(true);
SDL_PauseAudioDevice(_device, true);
}
}

View File

@@ -40,6 +40,7 @@ private:
void onReqPCM(char *stream, int len);
private:
SDL_AudioDeviceID _device;
std::shared_ptr<char> _play_buf;
SDL_AudioSpec _audio_config;
std::recursive_mutex _channel_mtx;

View File

@@ -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<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
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<FFmpegSwr>(AV_SAMPLE_FMT_S16, &(frame->get()->ch_layout), frame->get()->sample_rate);
chs = (&frame->get()->ch_layout)->nb_channels;
#else
swr = std::make_shared<FFmpegSwr>(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]);
}

View File

@@ -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<bool decoder = true>

View File

@@ -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;