2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2018-06-21 14:03:43 +08:00
|
|
|
|
#include "Player/Player.h"
|
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"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Thread/AsyncTaskThread.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
using namespace ZL::Util;
|
|
|
|
|
using namespace ZL::Thread;
|
|
|
|
|
|
|
|
|
|
namespace ZL {
|
|
|
|
|
namespace DEV {
|
|
|
|
|
|
2018-02-07 11:16:43 +08:00
|
|
|
|
PlayerProxy::PlayerProxy(const char *strVhost,
|
|
|
|
|
const char *strApp,
|
|
|
|
|
const char *strSrc,
|
|
|
|
|
bool bEnableHls,
|
|
|
|
|
bool bEnableMp4,
|
|
|
|
|
int iRetryCount){
|
2018-02-02 18:06:08 +08:00
|
|
|
|
m_strVhost = strVhost;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
m_strApp = strApp;
|
|
|
|
|
m_strSrc = strSrc;
|
2018-02-07 11:16:43 +08:00
|
|
|
|
m_bEnableHls = bEnableHls;
|
|
|
|
|
m_bEnableMp4 = bEnableMp4;
|
|
|
|
|
m_iRetryCount = iRetryCount;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2017-08-03 13:55:46 +08:00
|
|
|
|
void PlayerProxy::play(const char* strUrl) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
|
2017-08-03 13:55:46 +08:00
|
|
|
|
setOnVideoCB( [weakSelf](const H264Frame &data ) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if(!strongSelf){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(strongSelf->m_pChn){
|
2018-06-21 14:03:43 +08:00
|
|
|
|
strongSelf->m_pChn->inputH264((char *)data.data.data(), data.data.size(), data.timeStamp);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}else{
|
|
|
|
|
strongSelf->initMedia();
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-08-03 13:55:46 +08:00
|
|
|
|
setOnAudioCB( [weakSelf](const AdtsFrame &data ) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if(!strongSelf){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(strongSelf->m_pChn){
|
2018-06-21 14:03:43 +08:00
|
|
|
|
strongSelf->m_pChn->inputAAC((char *)data.data, data.aac_frame_length, data.timeStamp);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}else{
|
|
|
|
|
strongSelf->initMedia();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-02-07 11:16:43 +08:00
|
|
|
|
std::shared_ptr<int> piFailedCnt(new int(0)); //连续播放失败次数
|
2017-08-03 13:55:46 +08:00
|
|
|
|
string strUrlTmp(strUrl);
|
|
|
|
|
setOnPlayResult([weakSelf,strUrlTmp,piFailedCnt](const SockException &err) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if(!strongSelf) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(!err) {
|
|
|
|
|
// 播放成功
|
|
|
|
|
*piFailedCnt = 0;//连续播放失败次数清0
|
2018-02-07 11:16:43 +08:00
|
|
|
|
}else if(*piFailedCnt < strongSelf->m_iRetryCount || strongSelf->m_iRetryCount < 0) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
// 播放失败,延时重试播放
|
2017-08-03 13:55:46 +08:00
|
|
|
|
strongSelf->rePlay(strUrlTmp,(*piFailedCnt)++);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2017-08-03 13:55:46 +08:00
|
|
|
|
setOnShutdown([weakSelf,strUrlTmp,piFailedCnt](const SockException &err) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if(!strongSelf) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(strongSelf->m_pChn) {
|
|
|
|
|
strongSelf->m_pChn.reset();
|
|
|
|
|
}
|
|
|
|
|
//播放异常中断,延时重试播放
|
2018-02-07 11:16:43 +08:00
|
|
|
|
if(*piFailedCnt < strongSelf->m_iRetryCount || strongSelf->m_iRetryCount < 0) {
|
2017-08-03 13:55:46 +08:00
|
|
|
|
strongSelf->rePlay(strUrlTmp,(*piFailedCnt)++);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2017-08-03 13:55:46 +08:00
|
|
|
|
MediaPlayer::play(strUrl);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PlayerProxy::~PlayerProxy() {
|
|
|
|
|
auto iTaskId = reinterpret_cast<uint64_t>(this);
|
|
|
|
|
AsyncTaskThread::Instance().CancelTask(iTaskId);
|
|
|
|
|
}
|
2018-02-07 11:16:43 +08:00
|
|
|
|
void PlayerProxy::rePlay(const string &strUrl,int iFailedCnt){
|
2017-04-01 16:35:56 +08:00
|
|
|
|
auto iTaskId = reinterpret_cast<uint64_t>(this);
|
2018-02-07 11:16:43 +08:00
|
|
|
|
auto iDelay = MAX(2 * 1000, MIN(iFailedCnt * 3000,60*1000));
|
2017-08-03 13:55:46 +08:00
|
|
|
|
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
AsyncTaskThread::Instance().CancelTask(iTaskId);
|
2017-08-03 13:55:46 +08:00
|
|
|
|
AsyncTaskThread::Instance().DoTaskDelay(iTaskId, iDelay, [weakSelf,strUrl,iFailedCnt]() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//播放失败次数越多,则延时越长
|
2017-08-03 13:55:46 +08:00
|
|
|
|
auto strongPlayer = weakSelf.lock();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if(!strongPlayer) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
WarnL << "重试播放[" << iFailedCnt << "]:" << strUrl;
|
2017-08-03 13:55:46 +08:00
|
|
|
|
strongPlayer->MediaPlayer::play(strUrl.data());
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
void PlayerProxy::initMedia() {
|
2017-08-03 13:55:46 +08:00
|
|
|
|
if (!isInited()) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-02-07 11:16:43 +08:00
|
|
|
|
m_pChn.reset(new DevChannel(m_strVhost.data(),m_strApp.data(),m_strSrc.data(),getDuration(),m_bEnableHls,m_bEnableMp4));
|
2018-02-08 17:24:42 +08:00
|
|
|
|
m_pChn->setListener(shared_from_this());
|
2017-08-03 13:55:46 +08:00
|
|
|
|
if (containVideo()) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
VideoInfo info;
|
2017-08-03 13:55:46 +08:00
|
|
|
|
info.iFrameRate = getVideoFps();
|
|
|
|
|
info.iWidth = getVideoWidth();
|
|
|
|
|
info.iHeight = getVideoHeight();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
m_pChn->initVideo(info);
|
|
|
|
|
}
|
2017-08-03 13:55:46 +08:00
|
|
|
|
if (containAudio()) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
AudioInfo info;
|
2017-08-03 13:55:46 +08:00
|
|
|
|
info.iSampleRate = getAudioSampleRate();
|
|
|
|
|
info.iChannel = getAudioChannel();
|
|
|
|
|
info.iSampleBit = getAudioSampleBit();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
m_pChn->initAudio(info);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-08 17:24:42 +08:00
|
|
|
|
bool PlayerProxy::shutDown() {
|
|
|
|
|
//通知其停止推流
|
|
|
|
|
weak_ptr<PlayerProxy> weakSlef = dynamic_pointer_cast<PlayerProxy>(shared_from_this());
|
|
|
|
|
ASYNC_TRACE([weakSlef](){
|
|
|
|
|
auto stronSelf = weakSlef.lock();
|
|
|
|
|
if(stronSelf){
|
|
|
|
|
stronSelf->m_pChn.reset();
|
|
|
|
|
stronSelf->teardown();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} /* namespace Player */
|
|
|
|
|
} /* namespace ZL */
|