From d8a11734693c2fed2438c1f0a35f6bf834f0c674 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 29 Oct 2018 11:12:37 +0800 Subject: [PATCH] =?UTF-8?q?DevChannle=E5=AF=B9=E8=B1=A1=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E6=88=90MultiMediaSourceMuxer=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MediaFile/MediaReader.cpp | 16 ++++++++-------- src/MediaFile/MediaReader.h | 30 ++++++++++++++---------------- src/Player/PlayerProxy.cpp | 24 ++++++++++++------------ src/Player/PlayerProxy.h | 2 +- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/MediaFile/MediaReader.cpp b/src/MediaFile/MediaReader.cpp index dd3bb74f..2e8ee66d 100644 --- a/src/MediaFile/MediaReader.cpp +++ b/src/MediaFile/MediaReader.cpp @@ -127,15 +127,15 @@ MediaReader::MediaReader(const string &strVhost,const string &strApp, const stri } _iDuration = MAX(_video_ms,_audio_ms); - _pChn.reset(new DevChannel(strVhost.data(),strApp.data(),strId.data(),_iDuration/1000.0,false, false)); + _mediaMuxer.reset(new MultiMediaSourceMuxer(strVhost.data(),strApp.data(),strId.data(),_iDuration/1000.0,false, false)); if (_audio_trId != MP4_INVALID_TRACK_ID) { AACTrack::Ptr track = std::make_shared(_strAacCfg); - _pChn->addTrack(track); + _mediaMuxer->addTrack(track); } if (_video_trId != MP4_INVALID_TRACK_ID) { H264Track::Ptr track = std::make_shared(_strSps,_strPps); - _pChn->addTrack(track); + _mediaMuxer->addTrack(track); } } @@ -155,7 +155,7 @@ void MediaReader::startReadMP4() { AsyncTaskThread::Instance().DoTaskDelay(reinterpret_cast(this), sampleMS, [strongSelf](){ return strongSelf->readSample(); }); - _pChn->setListener(strongSelf); + _mediaMuxer->setListener(strongSelf); } bool MediaReader::seekTo(uint32_t ui32Stamp){ seek(ui32Stamp); @@ -171,7 +171,7 @@ bool MediaReader::readSample(int iTimeInc) { lock_guard lck(_mtx); auto bFlag0 = readVideoSample(iTimeInc);//数据没读完 auto bFlag1 = readAudioSample(iTimeInc);//数据没读完 - auto bFlag2 = _pChn->readerCount() > 0;//读取者大于0 + auto bFlag2 = _mediaMuxer->readerCount() > 0;//读取者大于0 if((bFlag0 || bFlag1) && bFlag2){ _alive.resetTime(); } @@ -236,11 +236,11 @@ inline bool MediaReader::readAudioSample(int iTimeInc) { } inline void MediaReader::writeH264(uint8_t *pucData,int iLen,uint32_t uiStamp) { - _pChn->inputFrame(std::make_shared((char*)pucData,iLen,uiStamp)); + _mediaMuxer->inputFrame(std::make_shared((char*)pucData,iLen,uiStamp)); } inline void MediaReader::writeAAC(uint8_t *pucData,int iLen,uint32_t uiStamp) { - _pChn->inputFrame(std::make_shared((char*)pucData,iLen,uiStamp)); + _mediaMuxer->inputFrame(std::make_shared((char*)pucData,iLen,uiStamp)); } inline MP4SampleId MediaReader::getVideoSampleId(int iTimeInc ) { @@ -287,7 +287,7 @@ void MediaReader::seek(uint32_t iSeekTime,bool bReStart){ setSeekTime(iSeekTime); } } - _pChn->setTimeStamp(_iSeekTime); + _mediaMuxer->setTimeStamp(_iSeekTime); if(bReStart){ AsyncTaskThread::Instance().CancelTask(reinterpret_cast(this)); diff --git a/src/MediaFile/MediaReader.h b/src/MediaFile/MediaReader.h index 65d9406e..40ed34a4 100644 --- a/src/MediaFile/MediaReader.h +++ b/src/MediaFile/MediaReader.h @@ -47,9 +47,21 @@ public: public: bool seekTo(uint32_t ui32Stamp) override; bool close() override; -private: #ifdef ENABLE_MP4V2 +private: + void seek(uint32_t iSeekTime,bool bReStart = true); + inline void setSeekTime(uint32_t iSeekTime); + inline uint32_t getVideoCurrentTime(); + void startReadMP4(); + inline MP4SampleId getVideoSampleId(int iTimeInc = 0); + inline MP4SampleId getAudioSampleId(int iTimeInc = 0); + bool readSample(int iTimeInc = 0); + inline bool readVideoSample(int iTimeInc = 0); + inline bool readAudioSample(int iTimeInc = 0); + inline void writeH264(uint8_t *pucData,int iLen,uint32_t uiStamp); + inline void writeAAC(uint8_t *pucData,int iLen,uint32_t uiStamp); +private: MP4FileHandle _hMP4File = MP4_INVALID_FILE_HANDLE; MP4TrackId _video_trId = MP4_INVALID_TRACK_ID; uint32_t _video_ms = 0; @@ -72,7 +84,7 @@ private: AACFrame _adts; int _iDuration = 0; - DevChannel::Ptr _pChn; + MultiMediaSourceMuxer::Ptr _mediaMuxer; MP4SampleId _video_current = 0; MP4SampleId _audio_current = 0; std::shared_ptr _pcVideoSample; @@ -81,20 +93,6 @@ private: Ticker _ticker; Ticker _alive; recursive_mutex _mtx; - - void seek(uint32_t iSeekTime,bool bReStart = true); - inline void setSeekTime(uint32_t iSeekTime); - inline uint32_t getVideoCurrentTime(); - void startReadMP4(); - inline MP4SampleId getVideoSampleId(int iTimeInc = 0); - inline MP4SampleId getAudioSampleId(int iTimeInc = 0); - bool readSample(int iTimeInc = 0); - inline bool readVideoSample(int iTimeInc = 0); - inline bool readAudioSample(int iTimeInc = 0); - inline void writeH264(uint8_t *pucData,int iLen,uint32_t uiStamp); - inline void writeAAC(uint8_t *pucData,int iLen,uint32_t uiStamp); - - #endif //ENABLE_MP4V2 }; diff --git a/src/Player/PlayerProxy.cpp b/src/Player/PlayerProxy.cpp index 618e47e3..a639bf5f 100644 --- a/src/Player/PlayerProxy.cpp +++ b/src/Player/PlayerProxy.cpp @@ -99,12 +99,12 @@ void PlayerProxy::play(const char* strUrl) { if(!strongSelf) { return; } - if(strongSelf->_pChn) { + if(strongSelf->_mediaMuxer) { auto tracks = strongSelf->getTracks(); for (auto & track : tracks){ - track->delDelegate(strongSelf->_pChn.get()); + track->delDelegate(strongSelf->_mediaMuxer.get()); } - strongSelf->_pChn.reset(); + strongSelf->_mediaMuxer.reset(); } //播放异常中断,延时重试播放 if(*piFailedCnt < strongSelf->_iRetryCount || strongSelf->_iRetryCount < 0) { @@ -142,7 +142,7 @@ bool PlayerProxy::close() { executor->async_first([weakSlef]() { auto stronSelf = weakSlef.lock(); if (stronSelf) { - stronSelf->_pChn.reset(); + stronSelf->_mediaMuxer.reset(); stronSelf->teardown(); } }); @@ -174,32 +174,32 @@ private: }; void PlayerProxy::onPlaySuccess() { - _pChn.reset(new DevChannel(_strVhost.data(),_strApp.data(),_strSrc.data(),getDuration(),_bEnableHls,_bEnableMp4)); - _pChn->setListener(shared_from_this()); + _mediaMuxer.reset(new MultiMediaSourceMuxer(_strVhost.data(),_strApp.data(),_strSrc.data(),getDuration(),_bEnableHls,_bEnableMp4)); + _mediaMuxer->setListener(shared_from_this()); auto videoTrack = getTrack(TrackVideo); if(videoTrack){ //添加视频 - _pChn->addTrack(videoTrack); + _mediaMuxer->addTrack(videoTrack); //视频数据写入_pChn - videoTrack->addDelegate(_pChn); + videoTrack->addDelegate(_mediaMuxer); } auto audioTrack = getTrack(TrackAudio); if(audioTrack){ //添加音频 - _pChn->addTrack(audioTrack); + _mediaMuxer->addTrack(audioTrack); //音频数据写入_pChn - audioTrack->addDelegate(_pChn); + audioTrack->addDelegate(_mediaMuxer); }else if(videoTrack){ //没有音频信息,产生一个静音音频 MuteAudioMaker::Ptr audioMaker = std::make_shared(); //videoTrack把数据写入MuteAudioMaker videoTrack->addDelegate(audioMaker); //添加一个静音Track至_pChn - _pChn->addTrack(std::make_shared()); + _mediaMuxer->addTrack(std::make_shared()); //MuteAudioMaker生成静音音频然后写入_pChn; - audioMaker->addDelegate(_pChn); + audioMaker->addDelegate(_mediaMuxer); } } diff --git a/src/Player/PlayerProxy.h b/src/Player/PlayerProxy.h index bcb583c7..d3f37ea3 100644 --- a/src/Player/PlayerProxy.h +++ b/src/Player/PlayerProxy.h @@ -64,7 +64,7 @@ private: bool _bEnableHls; bool _bEnableMp4; int _iRetryCount; - DevChannel::Ptr _pChn; + MultiMediaSourceMuxer::Ptr _mediaMuxer; string _strVhost; string _strApp; string _strSrc;