ZLMediaKit/src/Player/PlayerProxy.cpp

281 lines
12 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2017-04-01 16:35:56 +08:00
*/
2017-05-02 17:15:12 +08:00
#include "Common/config.h"
2017-04-01 16:35:56 +08:00
#include "PlayerProxy.h"
2017-04-25 11:35:41 +08:00
#include "Util/mini.h"
2017-04-01 16:35:56 +08:00
#include "Util/MD5.h"
#include "Util/logger.h"
2019-06-28 17:25:53 +08:00
#include "Extension/AAC.h"
2017-04-01 16:35:56 +08:00
2018-10-24 17:17:55 +08:00
using namespace toolkit;
2017-04-01 16:35:56 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit {
2017-04-01 16:35:56 +08:00
static uint8_t s_mute_adts[] = {0xff, 0xf1, 0x6c, 0x40, 0x2d, 0x3f, 0xfc, 0x00, 0xe0, 0x34, 0x20, 0xad, 0xf2, 0x3f, 0xb5, 0xdd,
0x73, 0xac, 0xbd, 0xca, 0xd7, 0x7d, 0x4a, 0x13, 0x2d, 0x2e, 0xa2, 0x62, 0x02, 0x70, 0x3c, 0x1c,
0xc5, 0x63, 0x55, 0x69, 0x94, 0xb5, 0x8d, 0x70, 0xd7, 0x24, 0x6a, 0x9e, 0x2e, 0x86, 0x24, 0xea,
0x4f, 0xd4, 0xf8, 0x10, 0x53, 0xa5, 0x4a, 0xb2, 0x9a, 0xf0, 0xa1, 0x4f, 0x2f, 0x66, 0xf9, 0xd3,
0x8c, 0xa6, 0x97, 0xd5, 0x84, 0xac, 0x09, 0x25, 0x98, 0x0b, 0x1d, 0x77, 0x04, 0xb8, 0x55, 0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37, 0x8b, 0xac, 0x37, 0x39, 0x41, 0x27,
0x6a, 0xf0, 0xe9, 0xb4, 0xc2, 0xac, 0xb0, 0x39, 0x73, 0x17, 0x64, 0x95, 0xf4, 0xdc, 0x33, 0xbb,
0x84, 0x94, 0x3e, 0xf8, 0x65, 0x71, 0x60, 0x7b, 0xd4, 0x5f, 0x27, 0x79, 0x95, 0x6a, 0xba, 0x76,
0xa6, 0xa5, 0x9a, 0xec, 0xae, 0x55, 0x3a, 0x27, 0x48, 0x23, 0xcf, 0x5c, 0x4d, 0xbc, 0x0b, 0x35,
0x5c, 0xa7, 0x17, 0xcf, 0x34, 0x57, 0xc9, 0x58, 0xc5, 0x20, 0x09, 0xee, 0xa5, 0xf2, 0x9c, 0x6c,
0x39, 0x1a, 0x77, 0x92, 0x9b, 0xff, 0xc6, 0xae, 0xf8, 0x36, 0xba, 0xa8, 0xaa, 0x6b, 0x1e, 0x8c,
0xc5, 0x97, 0x39, 0x6a, 0xb8, 0xa2, 0x55, 0xa8, 0xf8};
#define MUTE_ADTS_DATA s_mute_adts
#define MUTE_ADTS_DATA_LEN sizeof(s_mute_adts)
2018-07-02 16:00:55 +08:00
#define MUTE_ADTS_DATA_MS 130
2019-03-27 18:41:52 +08:00
PlayerProxy::PlayerProxy(const string &strVhost,
const string &strApp,
const string &strSrc,
2020-03-20 11:51:24 +08:00
bool bEnableRtsp,
bool bEnableRtmp,
bool bEnableHls,
bool bEnableMp4,
int iRetryCount,
2020-03-20 11:51:24 +08:00
const EventPoller::Ptr &poller) : MediaPlayer(poller){
_strVhost = strVhost;
_strApp = strApp;
_strSrc = strSrc;
_bEnableRtsp = bEnableRtsp;
_bEnableRtmp = bEnableRtmp;
2018-10-24 15:43:52 +08:00
_bEnableHls = bEnableHls;
_bEnableMp4 = bEnableMp4;
_iRetryCount = iRetryCount;
2017-04-01 16:35:56 +08:00
}
2019-05-20 18:08:55 +08:00
void PlayerProxy::setPlayCallbackOnce(const function<void(const SockException &ex)> &cb){
_playCB = cb;
}
2019-05-27 14:14:42 +08:00
void PlayerProxy::setOnClose(const function<void()> &cb){
_onClose = cb;
}
2019-03-27 18:41:52 +08:00
void PlayerProxy::play(const string &strUrlTmp) {
2020-03-20 11:51:24 +08:00
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
std::shared_ptr<int> piFailedCnt(new int(0)); //连续播放失败次数
setOnPlayResult([weakSelf,strUrlTmp,piFailedCnt](const SockException &err) {
auto strongSelf = weakSelf.lock();
if(!strongSelf) {
return;
}
2019-05-20 18:08:55 +08:00
2020-03-20 11:51:24 +08:00
if(strongSelf->_playCB) {
2019-05-20 18:08:55 +08:00
strongSelf->_playCB(err);
strongSelf->_playCB = nullptr;
}
2020-03-20 11:51:24 +08:00
if(!err) {
// 播放成功
*piFailedCnt = 0;//连续播放失败次数清0
strongSelf->onPlaySuccess();
}else if(*piFailedCnt < strongSelf->_iRetryCount || strongSelf->_iRetryCount < 0) {
// 播放失败,延时重试播放
strongSelf->rePlay(strUrlTmp,(*piFailedCnt)++);
}
});
setOnShutdown([weakSelf,strUrlTmp,piFailedCnt](const SockException &err) {
auto strongSelf = weakSelf.lock();
if(!strongSelf) {
return;
}
if(strongSelf->_mediaMuxer) {
auto tracks = strongSelf->getTracks(false);
for (auto & track : tracks){
track->delDelegate(strongSelf->_mediaMuxer.get());
}
2020-03-20 11:51:24 +08:00
GET_CONFIG(bool,resetWhenRePlay,General::kResetWhenRePlay);
if (resetWhenRePlay) {
strongSelf->_mediaMuxer.reset();
} else {
strongSelf->_mediaMuxer->resetTracks();
}
}
//播放异常中断,延时重试播放
if(*piFailedCnt < strongSelf->_iRetryCount || strongSelf->_iRetryCount < 0) {
strongSelf->rePlay(strUrlTmp,(*piFailedCnt)++);
}
});
MediaPlayer::play(strUrlTmp);
2020-03-20 11:51:24 +08:00
MediaSource::Ptr mediaSource;
if(dynamic_pointer_cast<RtspPlayer>(_delegate)){
//rtsp拉流
GET_CONFIG(bool,directProxy,Rtsp::kDirectProxy);
if(directProxy && _bEnableRtsp){
mediaSource = std::make_shared<RtspMediaSource>(_strVhost,_strApp,_strSrc);
}
} else if(dynamic_pointer_cast<RtmpPlayer>(_delegate)){
//rtmp拉流
if(_bEnableRtmp){
mediaSource = std::make_shared<RtmpMediaSource>(_strVhost,_strApp,_strSrc);
}
}
if(mediaSource){
setMediaSouce(mediaSource);
mediaSource->setListener(shared_from_this());
}
2017-04-01 16:35:56 +08:00
}
PlayerProxy::~PlayerProxy() {
2020-03-20 11:51:24 +08:00
_timer.reset();
2017-04-01 16:35:56 +08:00
}
void PlayerProxy::rePlay(const string &strUrl,int iFailedCnt){
2020-03-20 11:51:24 +08:00
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]() {
//播放失败次数越多,则延时越长
auto strongPlayer = weakSelf.lock();
if(!strongPlayer) {
return false;
}
WarnL << "重试播放[" << iFailedCnt << "]:" << strUrl;
strongPlayer->MediaPlayer::play(strUrl);
return false;
}, getPoller());
2017-04-01 16:35:56 +08:00
}
2019-05-29 18:08:50 +08:00
bool PlayerProxy::close(MediaSource &sender,bool force) {
2019-12-28 16:48:11 +08:00
if(!force && totalReaderCount()){
2019-05-27 18:39:43 +08:00
return false;
}
2020-03-20 11:51:24 +08:00
//通知其停止推流
weak_ptr<PlayerProxy> weakSlef = dynamic_pointer_cast<PlayerProxy>(shared_from_this());
getPoller()->async_first([weakSlef]() {
auto stronSelf = weakSlef.lock();
if (stronSelf) {
stronSelf->_mediaMuxer.reset();
stronSelf->setMediaSouce(nullptr);
stronSelf->teardown();
if(stronSelf->_onClose){
2019-05-27 14:14:42 +08:00
stronSelf->_onClose();
2020-03-20 11:51:24 +08:00
}
}
});
2019-05-29 18:08:50 +08:00
WarnL << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
return true;
}
2017-04-01 16:35:56 +08:00
2019-12-28 16:48:11 +08:00
int PlayerProxy::totalReaderCount(){
return (_mediaMuxer ? _mediaMuxer->totalReaderCount() : 0) + (_pMediaSrc ? _pMediaSrc->readerCount() : 0);
}
int PlayerProxy::totalReaderCount(MediaSource &sender) {
2020-03-20 11:51:24 +08:00
return totalReaderCount();
2019-12-28 16:48:11 +08:00
}
2019-12-25 20:07:42 +08:00
class MuteAudioMaker : public FrameDispatcher{
2018-10-26 17:14:39 +08:00
public:
2020-03-20 11:51:24 +08:00
typedef std::shared_ptr<MuteAudioMaker> Ptr;
2018-10-26 17:14:39 +08:00
2020-03-20 11:51:24 +08:00
MuteAudioMaker(){};
virtual ~MuteAudioMaker(){}
void inputFrame(const Frame::Ptr &frame) override {
if(frame->getTrackType() == TrackVideo){
auto iAudioIndex = frame->dts() / MUTE_ADTS_DATA_MS;
if(_iAudioIndex != iAudioIndex){
_iAudioIndex = iAudioIndex;
2020-04-03 20:45:58 +08:00
auto aacFrame = std::make_shared<AACFrameCacheAble>((char *)MUTE_ADTS_DATA, MUTE_ADTS_DATA_LEN, _iAudioIndex * MUTE_ADTS_DATA_MS, 0);
2020-03-20 11:51:24 +08:00
FrameDispatcher::inputFrame(aacFrame);
}
}
}
2020-01-08 12:14:27 +08:00
private:
2020-03-20 11:51:24 +08:00
class AACFrameCacheAble : public AACFrameNoCacheAble{
public:
template <typename ... ARGS>
AACFrameCacheAble(ARGS && ...args) : AACFrameNoCacheAble(std::forward<ARGS>(args)...){};
virtual ~AACFrameCacheAble() = default;
2020-01-08 12:14:27 +08:00
2020-03-20 11:51:24 +08:00
bool cacheAble() const override {
return true;
}
};
2018-10-26 17:14:39 +08:00
private:
2020-03-20 11:51:24 +08:00
int _iAudioIndex = 0;
2018-10-26 17:14:39 +08:00
};
void PlayerProxy::onPlaySuccess() {
2020-03-20 11:51:24 +08:00
GET_CONFIG(bool,resetWhenRePlay,General::kResetWhenRePlay);
if (dynamic_pointer_cast<RtspMediaSource>(_pMediaSrc)) {
//rtsp拉流代理
if (resetWhenRePlay || !_mediaMuxer) {
_mediaMuxer.reset(new MultiMediaSourceMuxer(_strVhost, _strApp, _strSrc, getDuration(), false, _bEnableRtmp, _bEnableHls, _bEnableMp4));
}
} else if (dynamic_pointer_cast<RtmpMediaSource>(_pMediaSrc)) {
//rtmp拉流代理
if (resetWhenRePlay || !_mediaMuxer) {
_mediaMuxer.reset(new MultiMediaSourceMuxer(_strVhost, _strApp, _strSrc, getDuration(), _bEnableRtsp, false, _bEnableHls, _bEnableMp4));
}
} else {
//其他拉流代理
if (resetWhenRePlay || !_mediaMuxer) {
_mediaMuxer.reset(new MultiMediaSourceMuxer(_strVhost, _strApp, _strSrc, getDuration(), _bEnableRtsp, _bEnableRtmp, _bEnableHls, _bEnableMp4));
}
}
2020-04-05 09:26:29 +08:00
_mediaMuxer->setMediaListener(shared_from_this());
2018-10-26 17:14:39 +08:00
2020-03-20 11:51:24 +08:00
auto videoTrack = getTrack(TrackVideo,false);
if(videoTrack){
//添加视频
_mediaMuxer->addTrack(videoTrack);
//视频数据写入_mediaMuxer
videoTrack->addDelegate(_mediaMuxer);
}
2018-10-26 17:14:39 +08:00
2020-03-20 11:51:24 +08:00
//是否添加静音音频
GET_CONFIG(bool,addMuteAudio,General::kAddMuteAudio);
2019-09-29 10:33:05 +08:00
2020-03-20 11:51:24 +08:00
auto audioTrack = getTrack(TrackAudio, false);
if(audioTrack){
//添加音频
_mediaMuxer->addTrack(audioTrack);
//音频数据写入_mediaMuxer
audioTrack->addDelegate(_mediaMuxer);
2019-09-29 10:33:05 +08:00
}else if(addMuteAudio && videoTrack){
2020-03-20 11:51:24 +08:00
//没有音频信息,产生一个静音音频
MuteAudioMaker::Ptr audioMaker = std::make_shared<MuteAudioMaker>();
//videoTrack把数据写入MuteAudioMaker
videoTrack->addDelegate(audioMaker);
//添加一个静音Track至_mediaMuxer
_mediaMuxer->addTrack(std::make_shared<AACTrack>());
//MuteAudioMaker生成静音音频然后写入_mediaMuxer
audioMaker->addDelegate(_mediaMuxer);
}
2019-12-17 09:05:34 +08:00
2020-03-20 11:51:24 +08:00
//添加完毕所有track防止单track情况下最大等待3秒
_mediaMuxer->addTrackCompleted();
2019-12-17 09:05:34 +08:00
if(_pMediaSrc){
_pMediaSrc->setTrackSource(_mediaMuxer);
}
}
2017-04-01 16:35:56 +08:00
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */