完成PlayerProxy改造

This commit is contained in:
xiongziliang 2018-10-26 17:14:39 +08:00
parent f26076635d
commit 097cf273d5
7 changed files with 89 additions and 81 deletions

View File

@ -30,7 +30,7 @@
#include "RtspMuxer/RtspMediaSourceMuxer.h" #include "RtspMuxer/RtspMediaSourceMuxer.h"
#include "RtmpMuxer/RtmpMediaSourceMuxer.h" #include "RtmpMuxer/RtmpMediaSourceMuxer.h"
class MultiMediaSourceMuxer { class MultiMediaSourceMuxer : public FrameRingWriterInterface{
public: public:
typedef std::shared_ptr<MultiMediaSourceMuxer> Ptr; typedef std::shared_ptr<MultiMediaSourceMuxer> Ptr;
@ -48,16 +48,16 @@ public:
* *
* @param track * @param track
*/ */
void addTrack(const Track::Ptr & track,int mtu = 1400) { void addTrack(const Track::Ptr & track) {
_rtmp->addTrack(track); _rtmp->addTrack(track);
_rtsp->addTrack(track,0,mtu); _rtsp->addTrack(track);
} }
/** /**
* rtmp * rtmp
* @param frame * @param frame
*/ */
void inputFrame(const Frame::Ptr &frame) { void inputFrame(const Frame::Ptr &frame) override {
_rtmp->inputFrame(frame); _rtmp->inputFrame(frame);
_rtsp->inputFrame(frame); _rtsp->inputFrame(frame);
} }

View File

@ -81,35 +81,6 @@ PlayerProxy::PlayerProxy(const char *strVhost,
} }
void PlayerProxy::play(const char* strUrl) { void PlayerProxy::play(const char* strUrl) {
weak_ptr<PlayerProxy> weakSelf = shared_from_this(); weak_ptr<PlayerProxy> weakSelf = shared_from_this();
//todo(xzl) 修复此处
// setOnVideoCB( [weakSelf](const H264Frame &data ) {
// auto strongSelf = weakSelf.lock();
// if(!strongSelf){
// return;
// }
// if(strongSelf->_pChn){
// strongSelf->_pChn->inputH264((char *)data.data(), data.size(), data.timeStamp);
// if(!strongSelf->_haveAudio){
// strongSelf->makeMuteAudio(data.timeStamp);
// }
// }else{
// strongSelf->initMedia();
// }
// });
// setOnAudioCB( [weakSelf](const AACFrame &data ) {
// auto strongSelf = weakSelf.lock();
// if(!strongSelf){
// return;
// }
// if(strongSelf->_pChn){
// strongSelf->_pChn->inputAAC((char *)data.data(), data.size(), data.timeStamp);
// }else{
// strongSelf->initMedia();
// }
// });
std::shared_ptr<int> piFailedCnt(new int(0)); //连续播放失败次数 std::shared_ptr<int> piFailedCnt(new int(0)); //连续播放失败次数
string strUrlTmp(strUrl); string strUrlTmp(strUrl);
setOnPlayResult([weakSelf,strUrlTmp,piFailedCnt](const SockException &err) { setOnPlayResult([weakSelf,strUrlTmp,piFailedCnt](const SockException &err) {
@ -120,6 +91,7 @@ void PlayerProxy::play(const char* strUrl) {
if(!err) { if(!err) {
// 播放成功 // 播放成功
*piFailedCnt = 0;//连续播放失败次数清0 *piFailedCnt = 0;//连续播放失败次数清0
strongSelf->onPlaySuccess();
}else if(*piFailedCnt < strongSelf->_iRetryCount || strongSelf->_iRetryCount < 0) { }else if(*piFailedCnt < strongSelf->_iRetryCount || strongSelf->_iRetryCount < 0) {
// 播放失败,延时重试播放 // 播放失败,延时重试播放
strongSelf->rePlay(strUrlTmp,(*piFailedCnt)++); strongSelf->rePlay(strUrlTmp,(*piFailedCnt)++);
@ -161,38 +133,6 @@ void PlayerProxy::rePlay(const string &strUrl,int iFailedCnt){
return false; return false;
}); });
} }
void PlayerProxy::initMedia() {
if (!isInited()) {
return;
}
_pChn.reset(new DevChannel(_strVhost.data(),_strApp.data(),_strSrc.data(),getDuration(),_bEnableHls,_bEnableMp4));
_pChn->setListener(shared_from_this());
//todo(xzl) 修复此处
// if (containVideo()) {
// VideoInfo info;
// info.iFrameRate = getVideoFps();
// info.iWidth = getVideoWidth();
// info.iHeight = getVideoHeight();
// _pChn->initVideo(info);
// }
//
// _haveAudio = containAudio();
// if (containAudio()) {
// AudioInfo info;
// info.iSampleRate = getAudioSampleRate();
// info.iChannel = getAudioChannel();
// info.iSampleBit = getAudioSampleBit();
// _pChn->initAudio(info);
// }else{
// AudioInfo info;
// info.iSampleRate = MUTE_ADTS_SAMPLE_RATE;
// info.iChannel = MUTE_ADTS_CHN_CNT;
// info.iSampleBit = MUTE_ADTS_SAMPLE_BIT;
// _pChn->initAudio(info);
// }
}
bool PlayerProxy::close() { bool PlayerProxy::close() {
//通知其停止推流 //通知其停止推流
weak_ptr<PlayerProxy> weakSlef = dynamic_pointer_cast<PlayerProxy>(shared_from_this()); weak_ptr<PlayerProxy> weakSlef = dynamic_pointer_cast<PlayerProxy>(shared_from_this());
@ -209,12 +149,56 @@ bool PlayerProxy::close() {
return true; return true;
} }
void PlayerProxy::makeMuteAudio(uint32_t stamp) {
auto iAudioIndex = stamp / MUTE_ADTS_DATA_MS; class MuteAudioMaker : public FrameRingInterfaceDelegate{
if(_iAudioIndex != iAudioIndex){ public:
_iAudioIndex = iAudioIndex; typedef std::shared_ptr<MuteAudioMaker> Ptr;
_pChn->inputFrame(std::make_shared<AACFrameNoCopyAble>((char *)MUTE_ADTS_DATA,MUTE_ADTS_DATA_LEN, _iAudioIndex * MUTE_ADTS_DATA_MS));
//DebugL << _iAudioIndex * MUTE_ADTS_DATA_MS << " " << stamp; MuteAudioMaker(){};
virtual ~MuteAudioMaker(){}
void inputFrame(const Frame::Ptr &frame) override {
if(frame->getTrackType() == TrackVideo){
auto iAudioIndex = frame->stamp() / MUTE_ADTS_DATA_MS;
if(_iAudioIndex != iAudioIndex){
_iAudioIndex = iAudioIndex;
auto aacFrame = std::make_shared<AACFrameNoCopyAble>((char *)MUTE_ADTS_DATA,
MUTE_ADTS_DATA_LEN,
_iAudioIndex * MUTE_ADTS_DATA_MS);
FrameRingInterfaceDelegate::inputFrame(aacFrame);
}
}
}
private:
int _iAudioIndex = 0;
};
void PlayerProxy::onPlaySuccess() {
_pChn.reset(new DevChannel(_strVhost.data(),_strApp.data(),_strSrc.data(),getDuration(),_bEnableHls,_bEnableMp4));
_pChn->setListener(shared_from_this());
auto videoTrack = getTrack(TrackVideo);
if(videoTrack){
//添加视频
_pChn->addTrack(videoTrack->clone());
//视频数据写入_pChn
videoTrack->addDelegate(_pChn);
}
auto audioTrack = getTrack(TrackAudio);
if(audioTrack){
//添加音频
_pChn->addTrack(audioTrack->clone());
//音频数据写入_pChn
audioTrack->addDelegate(_pChn);
}else if(videoTrack){
//没有音频信息,产生一个静音音频
MuteAudioMaker::Ptr audioMaker = std::make_shared<MuteAudioMaker>();
//videoTrack把数据写入MuteAudioMaker
videoTrack->addDelegate(audioMaker);
//添加一个静音Track至_pChn
_pChn->addTrack(std::make_shared<AACTrack>());
//MuteAudioMaker生成静音音频然后写入_pChn
audioMaker->addDelegate(_pChn);
} }
} }

View File

@ -38,7 +38,9 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
class PlayerProxy :public MediaPlayer, public std::enable_shared_from_this<PlayerProxy> , public MediaSourceEvent { class PlayerProxy :public MediaPlayer,
public std::enable_shared_from_this<PlayerProxy> ,
public MediaSourceEvent{
public: public:
typedef std::shared_ptr<PlayerProxy> Ptr; typedef std::shared_ptr<PlayerProxy> Ptr;
@ -58,7 +60,7 @@ public:
private: private:
void initMedia(); void initMedia();
void rePlay(const string &strUrl,int iFailedCnt); void rePlay(const string &strUrl,int iFailedCnt);
void makeMuteAudio(uint32_t stamp); void onPlaySuccess();
private: private:
bool _bEnableHls; bool _bEnableHls;
bool _bEnableMp4; bool _bEnableMp4;
@ -67,8 +69,6 @@ private:
string _strVhost; string _strVhost;
string _strApp; string _strApp;
string _strSrc; string _strSrc;
bool _haveAudio = false;
int _iAudioIndex = 0;
}; };
} /* namespace mediakit */ } /* namespace mediakit */

View File

@ -41,6 +41,7 @@ class Track : public FrameRingInterfaceDelegate , public CodecInfo{
public: public:
typedef std::shared_ptr<Track> Ptr; typedef std::shared_ptr<Track> Ptr;
Track(){} Track(){}
virtual ~Track(){} virtual ~Track(){}
/** /**
@ -48,6 +49,19 @@ public:
* @return * @return
*/ */
virtual bool ready() = 0; virtual bool ready() = 0;
/**
*
* @return
*/
virtual Track::Ptr clone() = 0;
/**
*
*
* @param that
*/
Track(const Track &that){}
}; };
class VideoTrack : public Track { class VideoTrack : public Track {
@ -245,6 +259,9 @@ private:
void parseSps(const string &sps){ void parseSps(const string &sps){
getAVCInfo(sps,_width,_height,_fps); getAVCInfo(sps,_width,_height,_fps);
} }
Track::Ptr clone() override {
return std::make_shared<std::remove_reference<decltype(*this)>::type >(*this);
}
private: private:
string _sps; string _sps;
string _pps; string _pps;
@ -369,6 +386,9 @@ private:
makeAdtsHeader(aac_cfg,aacFrame); makeAdtsHeader(aac_cfg,aacFrame);
getAACInfo(aacFrame,_sampleRate,_channel); getAACInfo(aacFrame,_sampleRate,_channel);
} }
Track::Ptr clone() override {
return std::make_shared<std::remove_reference<decltype(*this)>::type >(*this);
}
private: private:
string _cfg; string _cfg;
int _sampleRate = 0; int _sampleRate = 0;

View File

@ -28,10 +28,11 @@
#define ZLMEDIAKIT_RTMPMUXER_H #define ZLMEDIAKIT_RTMPMUXER_H
#include "RtmpMetedata.h" #include "RtmpMetedata.h"
#include "Player/Frame.h"
namespace mediakit{ namespace mediakit{
class RtmpMuxer{ class RtmpMuxer : public FrameRingWriterInterface{
public: public:
typedef std::shared_ptr<RtmpMuxer> Ptr; typedef std::shared_ptr<RtmpMuxer> Ptr;
@ -64,7 +65,7 @@ public:
* rtmp * rtmp
* @param frame * @param frame
*/ */
void inputFrame(const Frame::Ptr &frame) ; void inputFrame(const Frame::Ptr &frame) override ;
/** /**
* rtmp然后再写入 * rtmp然后再写入

View File

@ -32,7 +32,9 @@ namespace mediakit {
void RtspMuxer::addTrack(const Track::Ptr &track, uint32_t ssrc, int mtu) { void RtspMuxer::addTrack(const Track::Ptr &track, uint32_t ssrc, int mtu) {
auto codec_id = track->getCodecId(); auto codec_id = track->getCodecId();
_track_map[codec_id] = track; _track_map[codec_id] = track;
if(mtu == 0){
mtu = (track->getTrackType() == TrackVideo ? 1400 : 600);
}
auto lam = [this,ssrc,mtu,track](){ auto lam = [this,ssrc,mtu,track](){
//异步生成rtp编码器 //异步生成rtp编码器
//根据track生产sdp //根据track生产sdp

View File

@ -28,12 +28,13 @@
#define ZLMEDIAKIT_RTSPMUXER_H #define ZLMEDIAKIT_RTSPMUXER_H
#include "RtspSdp.h" #include "RtspSdp.h"
#include "Player/Frame.h"
namespace mediakit{ namespace mediakit{
/** /**
* rtsp生成器 * rtsp生成器
*/ */
class RtspMuxer{ class RtspMuxer : public FrameRingWriterInterface{
public: public:
typedef std::shared_ptr<RtspMuxer> Ptr; typedef std::shared_ptr<RtspMuxer> Ptr;
@ -56,7 +57,7 @@ public:
* @param ssrc rtp ssrc * @param ssrc rtp ssrc
* @param mtu rtp mtu * @param mtu rtp mtu
*/ */
void addTrack(const Track::Ptr & track,uint32_t ssrc = 0,int mtu = 1400) ; void addTrack(const Track::Ptr & track,uint32_t ssrc = 0,int mtu = 0) ;
/** /**
* SDP字符串 * SDP字符串
@ -68,7 +69,7 @@ public:
* rtp * rtp
* @param frame * @param frame
*/ */
void inputFrame(const Frame::Ptr &frame) ; void inputFrame(const Frame::Ptr &frame) override ;
/** /**
* rtp然后再写入 * rtp然后再写入