From ac41a1e933523788766cfc3b8f63a1b3cce60b81 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Wed, 10 Nov 2021 15:25:24 +0800 Subject: [PATCH] =?UTF-8?q?RtmpPlay:=20=E5=85=BC=E5=AE=B9=E6=97=A0metadata?= =?UTF-8?q?=E7=9A=84rtmp=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rtmp/RtmpDemuxer.cpp | 17 +++++++++++++++++ src/Rtmp/RtmpDemuxer.h | 2 ++ src/Rtmp/RtmpPlayerImp.h | 9 ++++++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Rtmp/RtmpDemuxer.cpp b/src/Rtmp/RtmpDemuxer.cpp index 357e1d98..08edbdfa 100644 --- a/src/Rtmp/RtmpDemuxer.cpp +++ b/src/Rtmp/RtmpDemuxer.cpp @@ -13,6 +13,23 @@ namespace mediakit { +size_t RtmpDemuxer::trackCount(const AMFValue &metadata) { + size_t ret = 0; + metadata.object_for_each([&](const string &key, const AMFValue &val) { + if (key == "videocodecid") { + //找到视频 + ++ret; + return; + } + if (key == "audiocodecid") { + //找到音频 + ++ret; + return; + } + }); + return ret; +} + bool RtmpDemuxer::loadMetaData(const AMFValue &val){ bool ret = false; try { diff --git a/src/Rtmp/RtmpDemuxer.h b/src/Rtmp/RtmpDemuxer.h index 65f6181e..cce7cbd5 100644 --- a/src/Rtmp/RtmpDemuxer.h +++ b/src/Rtmp/RtmpDemuxer.h @@ -30,6 +30,8 @@ public: RtmpDemuxer() = default; ~RtmpDemuxer() override = default; + static size_t trackCount(const AMFValue &metadata); + bool loadMetaData(const AMFValue &metadata); /** diff --git a/src/Rtmp/RtmpPlayerImp.h b/src/Rtmp/RtmpPlayerImp.h index f976707c..bea7811b 100644 --- a/src/Rtmp/RtmpPlayerImp.h +++ b/src/Rtmp/RtmpPlayerImp.h @@ -63,6 +63,8 @@ public: private: //派生类回调函数 bool onCheckMeta(const AMFValue &val) override { + //无metadata或metadata中无track信息时,需要从数据包中获取track + _wait_track_ready = (*this)[Client::kWaitTrackReady].as() || RtmpDemuxer::trackCount(val) == 0; onCheckMeta_l(val); return true; } @@ -79,7 +81,7 @@ private: } void onPlayResult(const SockException &ex) override { - if (!(*this)[Client::kWaitTrackReady].as() || ex) { + if (!_wait_track_ready || ex) { Super::onPlayResult(ex); return; } @@ -88,7 +90,7 @@ private: bool addTrack(const Track::Ptr &track) override { return true; } void addTrackCompleted() override { - if ((*this)[Client::kWaitTrackReady].as()) { + if (_wait_track_ready) { Super::onPlayResult(SockException(Err_success, "play success")); } } @@ -100,11 +102,12 @@ private: _rtmp_src->setMetaData(val); } _demuxer = std::make_shared(); - _demuxer->setTrackListener(this, (*this)[Client::kWaitTrackReady].as()); + _demuxer->setTrackListener(this, _wait_track_ready); _demuxer->loadMetaData(val); } private: + bool _wait_track_ready = true; RtmpDemuxer::Ptr _demuxer; RtmpMediaSource::Ptr _rtmp_src; };