diff --git a/player/test_player.cpp b/player/test_player.cpp index 79102a94..d0cb8afb 100644 --- a/player/test_player.cpp +++ b/player/test_player.cpp @@ -10,6 +10,7 @@ #include #include "Util/logger.h" +#include "Util/util.h" #include #include "Common/config.h" #include "Rtsp/UDPServer.h" @@ -48,80 +49,76 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstanc, LPSTR lpCmdLine, int main(int argc, char *argv[]) { #endif static char *url = argv[1]; - //设置退出信号处理函数 - signal(SIGINT, [](int) { SDLDisplayerHelper::Instance().shutdown(); }); - //设置日志 - Logger::Instance().add(std::make_shared()); - Logger::Instance().setWriter(std::make_shared()); + { + // 设置退出信号处理函数 + signal(SIGINT, [](int) { SDLDisplayerHelper::Instance().shutdown(); }); + // 设置日志 + 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" - << "例如:./test_player rtsp://admin:123456@127.0.0.1/live/0 0\r\n"; - return 0; - } - - auto player = std::make_shared(); - //sdl要求在main线程初始化 - auto displayer = std::make_shared(nullptr, url); - weak_ptr weakPlayer = player; - player->setOnPlayResult([weakPlayer, displayer](const SockException &ex) { - InfoL << "OnPlayResult:" << ex.what(); - auto strongPlayer = weakPlayer.lock(); - if (ex || !strongPlayer) { - return; + if (argc < 3) { + ErrorL << "\r\n测试方法:./test_player rtxp_url rtp_type\r\n" + << "例如:./test_player rtsp://admin:123456@127.0.0.1/live/0 0\r\n"; + return 0; } - auto videoTrack = dynamic_pointer_cast(strongPlayer->getTrack(TrackVideo, false)); - auto audioTrack = dynamic_pointer_cast(strongPlayer->getTrack(TrackAudio,false)); + auto player = std::make_shared(); + // sdl要求在main线程初始化 + auto displayer = std::make_shared(nullptr, url); + weak_ptr weakPlayer = player; + player->setOnPlayResult([weakPlayer, displayer](const SockException &ex) { + InfoL << "OnPlayResult:" << ex.what(); + auto strongPlayer = weakPlayer.lock(); + if (ex || !strongPlayer) { + return; + } - if (videoTrack) { - auto decoder = std::make_shared(videoTrack); - decoder->setOnDecode([displayer](const FFmpegFrame::Ptr &yuv) { - SDLDisplayerHelper::Instance().doTask([yuv, displayer]() { - //sdl要求在main线程渲染 - displayer->displayYUV(yuv->get()); - return true; + auto videoTrack = dynamic_pointer_cast(strongPlayer->getTrack(TrackVideo, false)); + auto audioTrack = dynamic_pointer_cast(strongPlayer->getTrack(TrackAudio, false)); + + if (videoTrack) { + auto decoder = std::make_shared(videoTrack); + decoder->setOnDecode([displayer](const FFmpegFrame::Ptr &yuv) { + SDLDisplayerHelper::Instance().doTask([yuv, displayer]() { + // sdl要求在main线程渲染 + displayer->displayYUV(yuv->get()); + return true; + }); }); - }); - videoTrack->addDelegate([decoder](const Frame::Ptr &frame) { - return decoder->inputFrame(frame, false, true); - }); + videoTrack->addDelegate([decoder](const Frame::Ptr &frame) { return decoder->inputFrame(frame, false, true); }); + } + + if (audioTrack) { + auto decoder = std::make_shared(audioTrack); + auto audio_player = std::make_shared(); + // FFmpeg解码时已经统一转换为16位整型pcm + audio_player->setup(audioTrack->getAudioSampleRate(), audioTrack->getAudioChannel(), AUDIO_S16); + FFmpegSwr::Ptr swr; + + decoder->setOnDecode([audio_player, swr](const FFmpegFrame::Ptr &frame) mutable { + if (!swr) { + swr = std::make_shared(AV_SAMPLE_FMT_S16, frame->get()->channels, frame->get()->channel_layout, frame->get()->sample_rate); + } + auto pcm = swr->inputFrame(frame); + auto len = pcm->get()->nb_samples * pcm->get()->channels * 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); }); + } + }); + + player->setOnShutdown([](const SockException &ex) { WarnL << "play shutdown: " << ex.what(); }); + + (*player)[Client::kRtpType] = atoi(argv[2]); + // 不等待track ready再回调播放成功事件,这样可以加快秒开速度 + (*player)[Client::kWaitTrackReady] = false; + if (argc > 3) { + (*player)[Client::kPlayTrack] = atoi(argv[3]); } - - if (audioTrack) { - auto decoder = std::make_shared(audioTrack); - auto audio_player = std::make_shared(); - //FFmpeg解码时已经统一转换为16位整型pcm - audio_player->setup(audioTrack->getAudioSampleRate(), audioTrack->getAudioChannel(), AUDIO_S16); - FFmpegSwr::Ptr swr; - - decoder->setOnDecode([audio_player, swr](const FFmpegFrame::Ptr &frame) mutable{ - if (!swr) { - swr = std::make_shared(AV_SAMPLE_FMT_S16, frame->get()->channels, - frame->get()->channel_layout, frame->get()->sample_rate); - } - auto pcm = swr->inputFrame(frame); - auto len = pcm->get()->nb_samples * pcm->get()->channels * 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); - }); - } - }); - - player->setOnShutdown([](const SockException &ex){ - WarnL << "play shutdown: " << ex.what(); - }); - - (*player)[Client::kRtpType] = atoi(argv[2]); - //不等待track ready再回调播放成功事件,这样可以加快秒开速度 - (*player)[Client::kWaitTrackReady] = false; - if (argc > 3) { - (*player)[Client::kPlayTrack] = atoi(argv[3]); + player->play(argv[1]); + SDLDisplayerHelper::Instance().runLoop(); } - player->play(argv[1]); - SDLDisplayerHelper::Instance().runLoop(); + sleep(1); return 0; }