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

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,
float dur_sec = 0.0,
bool bEanbleHls = true,
bool bEnableMp4 = false){
bool bEnableMp4 = false,
bool bEanbleRtmp = true,
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);
}
virtual ~MultiMediaSourceMuxer(){}
@ -54,8 +59,12 @@ public:
* @param track
*/
void addTrack(const Track::Ptr & track) {
if(_rtmp){
_rtmp->addTrack(track);
}
if(_rtsp){
_rtsp->addTrack(track);
}
_record->addTrack(track);
}
@ -64,8 +73,12 @@ public:
* @param frame
*/
void inputFrame(const Frame::Ptr &frame) override {
if(_rtmp) {
_rtmp->inputFrame(frame);
}
if(_rtsp) {
_rtsp->inputFrame(frame);
}
_record->inputFrame(frame);
}
@ -74,21 +87,27 @@ public:
* @param listener
*/
void setListener(const std::weak_ptr<MediaSourceEvent> &listener){
if(_rtmp) {
_rtmp->setListener(listener);
}
if(_rtsp) {
_rtsp->setListener(listener);
}
}
/**
*
* @return
*/
int readerCount() const{
return _rtsp->readerCount() + _rtmp->readerCount();
return (_rtsp ? _rtsp->readerCount() : 0) + (_rtmp ? _rtmp->readerCount() : 0);
}
void setTimeStamp(uint32_t stamp){
if(_rtsp){
_rtsp->setTimeStamp(stamp);
}
}
private:
RtmpMediaSourceMuxer::Ptr _rtmp;
RtspMediaSourceMuxer::Ptr _rtsp;

View File

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

View File

@ -126,13 +126,25 @@ void PlayerProxy::play(const string &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() {
_timer.reset();
}
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));
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
_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;
}, getPoller());
}
int PlayerProxy::readerCount(){
return (_mediaMuxer ? _mediaMuxer->readerCount() : 0) + (_pMediaSrc ? _pMediaSrc->readerCount() : 0);
}
bool PlayerProxy::close(MediaSource &sender,bool force) {
if(!_mediaMuxer || (!force && _mediaMuxer->readerCount() != 0)){
if(!force && readerCount() != 0){
return false;
}
@ -157,6 +174,7 @@ bool PlayerProxy::close(MediaSource &sender,bool force) {
auto stronSelf = weakSlef.lock();
if (stronSelf) {
stronSelf->_mediaMuxer.reset();
stronSelf->setMediaSouce(nullptr);
stronSelf->teardown();
if(stronSelf->_onClose){
stronSelf->_onClose();
@ -197,7 +215,16 @@ private:
};
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());
auto videoTrack = getTrack(TrackVideo,false);

View File

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