From 5ae154642c97795dc559cc025eb5d916a0cb6faa Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 24 Oct 2018 22:29:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=88=A4=E6=96=AD=E8=A7=A3?= =?UTF-8?q?=E5=A4=8D=E7=94=A8=E5=99=A8=E6=98=AF=E5=90=A6=E5=87=86=E5=A4=87?= =?UTF-8?q?=E5=A5=BD=E7=9A=84=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Player/Track.h | 22 ++++++++++++---------- src/RtmpMuxer/RtmpDemuxer.cpp | 34 +++++++++++++++++++--------------- src/RtmpMuxer/RtmpDemuxer.h | 17 ++++++++++++++--- src/RtspMuxer/RtspDemuxer.cpp | 21 +++++++++++---------- 4 files changed, 56 insertions(+), 38 deletions(-) diff --git a/src/Player/Track.h b/src/Player/Track.h index fbe0a1f9..7ab516d3 100644 --- a/src/Player/Track.h +++ b/src/Player/Track.h @@ -20,6 +20,12 @@ public: typedef std::shared_ptr Ptr; Track(){} virtual ~Track(){} + + /** + * 是否准备好 + * @return + */ + virtual bool ready() = 0; }; class VideoTrack : public Track { @@ -152,11 +158,8 @@ public: return _fps; } - TrackType getTrackType() const override { - if(_sps.empty() || _pps.empty()){ - return TrackInvalid; - } - return TrackVideo; + bool ready() override { + return !_sps.empty() && !_pps.empty(); } @@ -295,12 +298,11 @@ public: * 在获取aac_cfg前是无效的Track * @return */ - TrackType getTrackType() const override { - if(_cfg.empty()){ - return TrackInvalid; - } - return TrackAudio; + bool ready() override { + return !_cfg.empty(); } + + /** * 返回音频采样率 * @return diff --git a/src/RtmpMuxer/RtmpDemuxer.cpp b/src/RtmpMuxer/RtmpDemuxer.cpp index 574aa1d0..8817b587 100644 --- a/src/RtmpMuxer/RtmpDemuxer.cpp +++ b/src/RtmpMuxer/RtmpDemuxer.cpp @@ -29,6 +29,7 @@ namespace mediakit { + RtmpDemuxer::RtmpDemuxer(const AMFValue &val) { try { makeVideoTrack(val["videocodecid"]); @@ -44,17 +45,14 @@ RtmpDemuxer::RtmpDemuxer(const AMFValue &val) { } } -RtmpDemuxer::~RtmpDemuxer() { -} - bool RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) { switch (pkt->typeId) { case MSG_VIDEO: { if(_videoRtmpDecoder){ return _videoRtmpDecoder->inputRtmp(pkt, true); } - if(!_tryGetVideoTrack){ - _tryGetVideoTrack = true; + if(!_tryedGetVideoTrack){ + _tryedGetVideoTrack = true; auto codec = AMFValue(pkt->getMediaType()); makeVideoTrack(codec); } @@ -66,8 +64,8 @@ bool RtmpDemuxer::inputRtmp(const RtmpPacket::Ptr &pkt) { _audioRtmpDecoder->inputRtmp(pkt, false); return false; } - if(!_tryGetAudioTrack) { - _tryGetAudioTrack = true; + if(!_tryedGetAudioTrack) { + _tryedGetAudioTrack = true; auto codec = AMFValue(pkt->getMediaType()); makeAudioTrack(codec); } @@ -122,16 +120,22 @@ vector RtmpDemuxer::getTracks() const { } bool RtmpDemuxer::isInited() const { - bool ret = true; - if(ret && _audioTrack){ - //getTrackType() 等于TrackInvalid时说明该Track还未准备好 - ret = _audioTrack->getTrackType() != TrackInvalid; + bool videoReady ,auidoReady; + + if(_videoTrack){ + //getTrackType() != TrackInvalid说明其已经准备好了 + videoReady = _videoTrack->ready(); + }else{ + videoReady = _tryedGetVideoTrack || _tryedGetAudioTrack; } - if(ret && _videoTrack){ - //getTrackType() 等于TrackInvalid时说明该Track还未准备好 - ret = _videoTrack->getTrackType() != TrackInvalid; + + if(_audioTrack){ + auidoReady = _audioTrack->ready(); + }else{ + auidoReady = _tryedGetVideoTrack || _tryedGetAudioTrack; } - return ret; + + return videoReady && auidoReady; } float RtmpDemuxer::getDuration() const { diff --git a/src/RtmpMuxer/RtmpDemuxer.h b/src/RtmpMuxer/RtmpDemuxer.h index 344fb94c..2c610222 100644 --- a/src/RtmpMuxer/RtmpDemuxer.h +++ b/src/RtmpMuxer/RtmpDemuxer.h @@ -43,8 +43,19 @@ namespace mediakit { class RtmpDemuxer : public PlayerBase{ public: typedef std::shared_ptr Ptr; + + /** + * 等效于RtmpDemuxer(AMFValue(AMF_NULL)) + */ + RtmpDemuxer(){} + /** + * 构造rtmp解复用器 + * @param val rtmp的metedata,可以传入null类型, + * 这样就会在inputRtmp时异步探测媒体编码格式 + */ RtmpDemuxer(const AMFValue &val); - virtual ~RtmpDemuxer(); + + virtual ~RtmpDemuxer(){}; /** * 开始解复用 @@ -77,8 +88,8 @@ private: void makeAudioTrack(const AMFValue &val); private: float _fDuration = 0; - bool _tryGetVideoTrack = false; - bool _tryGetAudioTrack = false; + bool _tryedGetVideoTrack = false; + bool _tryedGetAudioTrack = false; AudioTrack::Ptr _audioTrack; VideoTrack::Ptr _videoTrack; RtmpCodec::Ptr _audioRtmpDecoder; diff --git a/src/RtspMuxer/RtspDemuxer.cpp b/src/RtspMuxer/RtspDemuxer.cpp index 60883338..99819075 100644 --- a/src/RtspMuxer/RtspDemuxer.cpp +++ b/src/RtspMuxer/RtspDemuxer.cpp @@ -135,16 +135,17 @@ vector RtspDemuxer::getTracks() const { } bool RtspDemuxer::isInited() const { - bool ret = true; - if(ret && _audioTrack){ - //getTrackType() 等于TrackInvalid时说明该Track还未准备好 - ret = _audioTrack->getTrackType() != TrackInvalid; - } - if(ret && _videoTrack){ - //getTrackType() 等于TrackInvalid时说明该Track还未准备好 - ret = _videoTrack->getTrackType() != TrackInvalid; - } - return ret; + bool videoReady = true ,auidoReady = true; + + if(_videoTrack){ + videoReady = _videoTrack->ready(); + } + + if(_audioTrack){ + auidoReady = _audioTrack->ready(); + } + + return videoReady && auidoReady; } float RtspDemuxer::getDuration() const {