本协议拉流代码支持任意编码格式

This commit is contained in:
xiongziliang 2019-07-19 11:30:39 +08:00
parent e095a604ab
commit d71f1dd293
5 changed files with 64 additions and 17 deletions

@ -1 +1 @@
Subproject commit 03d0953f91e0a75fa8e95fce032ff9d3bef04789 Subproject commit ea465afe2f114d0b82f9e0869c728146abf5f05e

View File

@ -40,11 +40,16 @@ public:
const string &strId, const string &strId,
float dur_sec = 0.0, float dur_sec = 0.0,
bool bEanbleHls = true, bool bEanbleHls = true,
bool bEnableMp4 = false){ bool bEnableMp4 = false,
_rtmp = std::make_shared<RtmpMediaSourceMuxer>(vhost,strApp,strId,std::make_shared<TitleMete>(dur_sec)); bool bEanbleRtmp = true,
_rtsp = std::make_shared<RtspMediaSourceMuxer>(vhost,strApp,strId,std::make_shared<TitleSdp>(dur_sec)); bool bEanbleRtsp = true){
if (bEanbleRtmp) {
_rtmp = std::make_shared<RtmpMediaSourceMuxer>(vhost, strApp, strId, std::make_shared<TitleMete>(dur_sec));
}
if (bEanbleRtsp) {
_rtsp = std::make_shared<RtspMediaSourceMuxer>(vhost, strApp, strId, std::make_shared<TitleSdp>(dur_sec));
}
_record = std::make_shared<MediaRecorder>(vhost,strApp,strId,bEanbleHls,bEnableMp4); _record = std::make_shared<MediaRecorder>(vhost,strApp,strId,bEanbleHls,bEnableMp4);
} }
virtual ~MultiMediaSourceMuxer(){} virtual ~MultiMediaSourceMuxer(){}
@ -54,8 +59,12 @@ public:
* @param track * @param track
*/ */
void addTrack(const Track::Ptr & track) { void addTrack(const Track::Ptr & track) {
_rtmp->addTrack(track); if(_rtmp){
_rtsp->addTrack(track); _rtmp->addTrack(track);
}
if(_rtsp){
_rtsp->addTrack(track);
}
_record->addTrack(track); _record->addTrack(track);
} }
@ -64,8 +73,12 @@ public:
* @param frame * @param frame
*/ */
void inputFrame(const Frame::Ptr &frame) override { void inputFrame(const Frame::Ptr &frame) override {
_rtmp->inputFrame(frame); if(_rtmp) {
_rtsp->inputFrame(frame); _rtmp->inputFrame(frame);
}
if(_rtsp) {
_rtsp->inputFrame(frame);
}
_record->inputFrame(frame); _record->inputFrame(frame);
} }
@ -74,8 +87,12 @@ public:
* @param listener * @param listener
*/ */
void setListener(const std::weak_ptr<MediaSourceEvent> &listener){ void setListener(const std::weak_ptr<MediaSourceEvent> &listener){
_rtmp->setListener(listener); if(_rtmp) {
_rtsp->setListener(listener); _rtmp->setListener(listener);
}
if(_rtsp) {
_rtsp->setListener(listener);
}
} }
/** /**
@ -83,11 +100,13 @@ public:
* @return * @return
*/ */
int readerCount() const{ int readerCount() const{
return _rtsp->readerCount() + _rtmp->readerCount(); return (_rtsp ? _rtsp->readerCount() : 0) + (_rtmp ? _rtmp->readerCount() : 0);
} }
void setTimeStamp(uint32_t stamp){ void setTimeStamp(uint32_t stamp){
_rtsp->setTimeStamp(stamp); if(_rtsp){
_rtsp->setTimeStamp(stamp);
}
} }
private: private:
RtmpMediaSourceMuxer::Ptr _rtmp; RtmpMediaSourceMuxer::Ptr _rtmp;

View File

@ -216,7 +216,7 @@ public:
void setMediaSouce(const MediaSource::Ptr & src) override { void setMediaSouce(const MediaSource::Ptr & src) override {
if (_parser) { if (_parser) {
return _parser->setMediaSouce(src); _parser->setMediaSouce(src);
} }
_pMediaSrc = src; _pMediaSrc = src;
} }

View File

@ -126,13 +126,25 @@ void PlayerProxy::play(const string &strUrlTmp) {
} }
}); });
MediaPlayer::play(strUrlTmp); MediaPlayer::play(strUrlTmp);
MediaSource::Ptr mediaSource;
if(dynamic_pointer_cast<RtspPlayer>(_parser)){
//rtsp拉流
mediaSource = std::make_shared<RtspMediaSource>(_strVhost,_strApp,_strSrc);
}else if(dynamic_pointer_cast<RtmpPlayer>(_parser)){
//rtmp拉流
mediaSource = std::make_shared<RtmpMediaSource>(_strVhost,_strApp,_strSrc);
}
if(mediaSource){
setMediaSouce(mediaSource);
mediaSource->setListener(shared_from_this());
}
} }
PlayerProxy::~PlayerProxy() { PlayerProxy::~PlayerProxy() {
_timer.reset(); _timer.reset();
} }
void PlayerProxy::rePlay(const string &strUrl,int iFailedCnt){ void PlayerProxy::rePlay(const string &strUrl,int iFailedCnt){
auto iTaskId = reinterpret_cast<uint64_t>(this);
auto iDelay = MAX(2 * 1000, MIN(iFailedCnt * 3000,60*1000)); auto iDelay = MAX(2 * 1000, MIN(iFailedCnt * 3000,60*1000));
weak_ptr<PlayerProxy> weakSelf = shared_from_this(); weak_ptr<PlayerProxy> weakSelf = shared_from_this();
_timer = std::make_shared<Timer>(iDelay / 1000.0f,[weakSelf,strUrl,iFailedCnt]() { _timer = std::make_shared<Timer>(iDelay / 1000.0f,[weakSelf,strUrl,iFailedCnt]() {
@ -146,8 +158,13 @@ void PlayerProxy::rePlay(const string &strUrl,int iFailedCnt){
return false; return false;
}, getPoller()); }, getPoller());
} }
int PlayerProxy::readerCount(){
return (_mediaMuxer ? _mediaMuxer->readerCount() : 0) + (_pMediaSrc ? _pMediaSrc->readerCount() : 0);
}
bool PlayerProxy::close(MediaSource &sender,bool force) { bool PlayerProxy::close(MediaSource &sender,bool force) {
if(!_mediaMuxer || (!force && _mediaMuxer->readerCount() != 0)){ if(!force && readerCount() != 0){
return false; return false;
} }
@ -157,6 +174,7 @@ bool PlayerProxy::close(MediaSource &sender,bool force) {
auto stronSelf = weakSlef.lock(); auto stronSelf = weakSlef.lock();
if (stronSelf) { if (stronSelf) {
stronSelf->_mediaMuxer.reset(); stronSelf->_mediaMuxer.reset();
stronSelf->setMediaSouce(nullptr);
stronSelf->teardown(); stronSelf->teardown();
if(stronSelf->_onClose){ if(stronSelf->_onClose){
stronSelf->_onClose(); stronSelf->_onClose();
@ -197,7 +215,16 @@ private:
}; };
void PlayerProxy::onPlaySuccess() { void PlayerProxy::onPlaySuccess() {
_mediaMuxer.reset(new MultiMediaSourceMuxer(_strVhost,_strApp,_strSrc,getDuration(),_bEnableHls,_bEnableMp4)); if (dynamic_pointer_cast<RtspMediaSource>(_pMediaSrc)) {
//rtsp拉流代理
_mediaMuxer.reset(new MultiMediaSourceMuxer(_strVhost, _strApp, _strSrc, getDuration(), _bEnableHls, _bEnableMp4 , true, false));
} else if (dynamic_pointer_cast<RtmpMediaSource>(_pMediaSrc)) {
//rtmp拉流代理
_mediaMuxer.reset(new MultiMediaSourceMuxer(_strVhost, _strApp, _strSrc, getDuration(), _bEnableHls, _bEnableMp4 , false, true));
} else {
//其他拉流代理
_mediaMuxer.reset(new MultiMediaSourceMuxer(_strVhost, _strApp, _strSrc, getDuration(), _bEnableHls, _bEnableMp4 , true, true));
}
_mediaMuxer->setListener(shared_from_this()); _mediaMuxer->setListener(shared_from_this());
auto videoTrack = getTrack(TrackVideo,false); auto videoTrack = getTrack(TrackVideo,false);

View File

@ -84,6 +84,7 @@ private:
void onNoneReader(MediaSource &sender) override; void onNoneReader(MediaSource &sender) override;
void rePlay(const string &strUrl,int iFailedCnt); void rePlay(const string &strUrl,int iFailedCnt);
void onPlaySuccess(); void onPlaySuccess();
int readerCount() ;
private: private:
bool _bEnableHls; bool _bEnableHls;
bool _bEnableMp4; bool _bEnableMp4;