From 5e68ddacd892c19dc10bf60154d515f84a6cbb6c Mon Sep 17 00:00:00 2001 From: KkemChen Date: Thu, 28 Dec 2023 15:46:39 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E7=94=A8=E4=BA=8E=E6=B5=8B=E8=AF=95dec?= =?UTF-8?q?oder=E5=8F=8D=E5=A4=8D=E5=88=9B=E5=BB=BA=E9=94=80=E6=AF=81?= =?UTF-8?q?=E5=BC=95=E5=8F=91=E7=9A=84=E5=86=85=E5=AD=98=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- tests/test_decoder.cpp | 111 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 tests/test_decoder.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 64157b7f..b432d6a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,7 @@ option(ENABLE_API_STATIC_LIB "Enable mk_api static lib" OFF) option(ENABLE_ASAN "Enable Address Sanitize" OFF) option(ENABLE_CXX_API "Enable C++ API SDK" OFF) option(ENABLE_FAAC "Enable FAAC" OFF) -option(ENABLE_FFMPEG "Enable FFmpeg" OFF) +option(ENABLE_FFMPEG "Enable FFmpeg" ON) option(ENABLE_HLS "Enable HLS" ON) option(ENABLE_JEMALLOC_STATIC "Enable static linking to the jemalloc library" OFF) option(ENABLE_JEMALLOC_DUMP "Enable jemalloc to dump malloc statistics" OFF) diff --git a/tests/test_decoder.cpp b/tests/test_decoder.cpp new file mode 100644 index 00000000..0c9fd950 --- /dev/null +++ b/tests/test_decoder.cpp @@ -0,0 +1,111 @@ + +#include "Codec/Transcode.h" +#include "Common/config.h" +#include "Player/MediaPlayer.h" +#include "Rtsp/UDPServer.h" +#include "Util/logger.h" +#include "Util/onceToken.h" +#include +#include +#include +#include + +using namespace std; +using namespace toolkit; +using namespace mediakit; + +std::mutex mx; +std::list g_list; + +class TestPlayer : public std::enable_shared_from_this { +public: + using Ptr = std::shared_ptr; + + TestPlayer(const std::string &url) + : _url(url) {} + + void play() // 遍历,然后调用channel的回调 + { + auto url = _url; + // 创建拉流 解码对象 + auto player = std::make_shared(); + std::weak_ptr weakPlayer = player; + + std::weak_ptr weakSelf = shared_from_this(); + + player->setOnPlayResult([weakPlayer, weakSelf, url](const toolkit::SockException &ex) mutable { + InfoL << "OnPlayResult:" << ex.what(); + auto strongPlayer = weakPlayer.lock(); + if (!strongPlayer) { + return; + } + + auto videoTrack = std::dynamic_pointer_cast(strongPlayer->getTrack(mediakit::TrackVideo, false)); + // auto audioTrack = std::dynamic_pointer_cast(strongPlayer->getTrack(mediakit::TrackAudio, false)); + + if (videoTrack) { + // auto decoder = std::make_shared(videoTrack); + auto decoder = std::make_shared(videoTrack, 0, std::vector { "h264", "hevc" }); + decoder->setOnDecode([weakSelf](const mediakit::FFmpegFrame::Ptr &frame) mutable { + auto strongSelf = weakSelf.lock(); + if (!strongSelf) { + return; + } + + // strongSelf->frame_list.push_back(frame); + std::unique_lock lock(mx); + g_list.push_back(frame); + InfoL << "pts:" << frame->get()->pts << "size: " << g_list.size(); + }); + + videoTrack->addDelegate((std::function)[ weakSelf, decoder ](const mediakit::Frame::Ptr &frame) { + return decoder->inputFrame(frame, false, false); + }); + } + }); + + player->setOnShutdown([weakPlayer, url, weakSelf](const toolkit::SockException &ex) { + WarnL << "play shutdown: " << ex.what(); + auto strongPlayer = weakPlayer.lock(); + if (!strongPlayer) { + return; + } + }); + + (*player)[mediakit::Client::kWaitTrackReady] = false; // 不等待TrackReady + (*player)[mediakit::Client::kRtpType] = mediakit::Rtsp::RTP_TCP; + + player->play(url); + + _player = player; + } + +public: + std::list frame_list; + +private: + std::string _url; + + mediakit::MediaPlayer::Ptr _player; +}; + +int main() { + for (int i = 0; i < 100; i++) { + { + std::this_thread::sleep_for(std::chrono::seconds(3)); + auto player1 = std::make_shared("rtsp://kkem.me:1554/live/test"); + auto player2 = std::make_shared("rtsp://kkem.me:1554/live/test"); + auto player3 = std::make_shared("rtsp://kkem.me:1554/live/test"); + + player1->play(); + player2->play(); + player3->play(); + + std::this_thread::sleep_for(std::chrono::seconds(5)); + } + std::unique_lock lock(mx); + g_list.clear(); + } + getchar(); + return 0; +} \ No newline at end of file