mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-13 11:41:44 +08:00
优化播放器接口
This commit is contained in:
@@ -18,21 +18,16 @@ using namespace ZL::Thread;
|
||||
namespace ZL {
|
||||
namespace DEV {
|
||||
|
||||
const char PlayerProxy::kAliveSecond[] = "alive_second";
|
||||
|
||||
PlayerProxy::PlayerProxy(const char *strApp,const char *strSrc){
|
||||
m_strApp = strApp;
|
||||
m_strSrc = strSrc;
|
||||
}
|
||||
|
||||
void PlayerProxy::play(const char* strUrl, const char *strUser,
|
||||
const char *strPwd, PlayerBase::eRtpType eType, uint32_t iSecond) {
|
||||
m_aliveSecond = iSecond;
|
||||
string strUrlTmp(strUrl);
|
||||
string strUserTmp(strUser);
|
||||
string strPwdTmp(strPwd);
|
||||
|
||||
m_pPlayer.reset(new MediaPlayer());
|
||||
void PlayerProxy::play(const char* strUrl) {
|
||||
m_aliveSecond = (*this)[kAliveSecond];
|
||||
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
|
||||
m_pPlayer->setOnVideoCB( [weakSelf,strUrlTmp](const H264Frame &data ) {
|
||||
setOnVideoCB( [weakSelf](const H264Frame &data ) {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if(!strongSelf){
|
||||
return;
|
||||
@@ -44,7 +39,7 @@ void PlayerProxy::play(const char* strUrl, const char *strUser,
|
||||
}
|
||||
strongSelf->checkExpired();
|
||||
});
|
||||
m_pPlayer->setOnAudioCB( [weakSelf,strUrlTmp](const AdtsFrame &data ) {
|
||||
setOnAudioCB( [weakSelf](const AdtsFrame &data ) {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if(!strongSelf){
|
||||
return;
|
||||
@@ -58,7 +53,8 @@ void PlayerProxy::play(const char* strUrl, const char *strUser,
|
||||
});
|
||||
|
||||
std::shared_ptr<uint64_t> piFailedCnt(new uint64_t(0)); //连续播放失败次数
|
||||
m_pPlayer->setOnPlayResult([weakSelf,strUrlTmp,strUserTmp,strPwdTmp,eType,piFailedCnt](const SockException &err) {
|
||||
string strUrlTmp(strUrl);
|
||||
setOnPlayResult([weakSelf,strUrlTmp,piFailedCnt](const SockException &err) {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if(!strongSelf) {
|
||||
return;
|
||||
@@ -69,13 +65,12 @@ void PlayerProxy::play(const char* strUrl, const char *strUser,
|
||||
*piFailedCnt = 0;//连续播放失败次数清0
|
||||
}else if(*piFailedCnt < replayCnt) {
|
||||
// 播放失败,延时重试播放
|
||||
strongSelf->rePlay(strUrlTmp, strUserTmp, strPwdTmp, eType,(*piFailedCnt)++);
|
||||
strongSelf->rePlay(strUrlTmp,(*piFailedCnt)++);
|
||||
}else{
|
||||
strongSelf->expired();
|
||||
}
|
||||
});
|
||||
weak_ptr<MediaPlayer> weakPtr= m_pPlayer;
|
||||
m_pPlayer->setOnShutdown([weakSelf,weakPtr,strUrlTmp,strUserTmp,strPwdTmp,eType,piFailedCnt](const SockException &err) {
|
||||
setOnShutdown([weakSelf,strUrlTmp,piFailedCnt](const SockException &err) {
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if(!strongSelf) {
|
||||
return;
|
||||
@@ -86,52 +81,52 @@ void PlayerProxy::play(const char* strUrl, const char *strUser,
|
||||
//播放异常中断,延时重试播放
|
||||
static uint64_t replayCnt = mINI::Instance()[Config::Proxy::kReplayCount].as<uint64_t>();
|
||||
if(*piFailedCnt < replayCnt) {
|
||||
strongSelf->rePlay(strUrlTmp, strUserTmp, strPwdTmp, eType,(*piFailedCnt)++);
|
||||
strongSelf->rePlay(strUrlTmp,(*piFailedCnt)++);
|
||||
}else{
|
||||
strongSelf->expired();
|
||||
}
|
||||
});
|
||||
m_pPlayer->play(strUrl, strUser, strPwd, eType);
|
||||
MediaPlayer::play(strUrl);
|
||||
}
|
||||
|
||||
PlayerProxy::~PlayerProxy() {
|
||||
auto iTaskId = reinterpret_cast<uint64_t>(this);
|
||||
AsyncTaskThread::Instance().CancelTask(iTaskId);
|
||||
}
|
||||
void PlayerProxy::rePlay(const string &strUrl, const string &strUser, const string &strPwd, PlayerBase::eRtpType eType, uint64_t iFailedCnt){
|
||||
void PlayerProxy::rePlay(const string &strUrl,uint64_t iFailedCnt){
|
||||
checkExpired();
|
||||
auto iTaskId = reinterpret_cast<uint64_t>(this);
|
||||
auto iDelay = MAX((uint64_t)2 * 1000, MIN(iFailedCnt * 3000,(uint64_t)60*1000));
|
||||
weak_ptr<MediaPlayer> weakPtr = m_pPlayer;
|
||||
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
|
||||
AsyncTaskThread::Instance().CancelTask(iTaskId);
|
||||
AsyncTaskThread::Instance().DoTaskDelay(iTaskId, iDelay, [weakPtr,strUrl,strUser,strPwd,eType,iFailedCnt]() {
|
||||
AsyncTaskThread::Instance().DoTaskDelay(iTaskId, iDelay, [weakSelf,strUrl,iFailedCnt]() {
|
||||
//播放失败次数越多,则延时越长
|
||||
auto strongPlayer = weakPtr.lock();
|
||||
auto strongPlayer = weakSelf.lock();
|
||||
if(!strongPlayer) {
|
||||
return false;
|
||||
}
|
||||
WarnL << "重试播放[" << iFailedCnt << "]:" << strUrl;
|
||||
strongPlayer->play(strUrl.data(), strUser.data(), strPwd.data(), eType);
|
||||
strongPlayer->MediaPlayer::play(strUrl.data());
|
||||
return false;
|
||||
});
|
||||
}
|
||||
void PlayerProxy::initMedia() {
|
||||
if (!m_pPlayer->isInited()) {
|
||||
if (!isInited()) {
|
||||
return;
|
||||
}
|
||||
m_pChn.reset(new DevChannel(m_strApp.data(),m_strSrc.data(),m_pPlayer->getDuration()));
|
||||
if (m_pPlayer->containVideo()) {
|
||||
m_pChn.reset(new DevChannel(m_strApp.data(),m_strSrc.data(),getDuration()));
|
||||
if (containVideo()) {
|
||||
VideoInfo info;
|
||||
info.iFrameRate = m_pPlayer->getVideoFps();
|
||||
info.iWidth = m_pPlayer->getVideoWidth();
|
||||
info.iHeight = m_pPlayer->getVideoHeight();
|
||||
info.iFrameRate = getVideoFps();
|
||||
info.iWidth = getVideoWidth();
|
||||
info.iHeight = getVideoHeight();
|
||||
m_pChn->initVideo(info);
|
||||
}
|
||||
if (m_pPlayer->containAudio()) {
|
||||
if (containAudio()) {
|
||||
AudioInfo info;
|
||||
info.iSampleRate = m_pPlayer->getAudioSampleRate();
|
||||
info.iChannel = m_pPlayer->getAudioChannel();
|
||||
info.iSampleBit = m_pPlayer->getAudioSampleBit();
|
||||
info.iSampleRate = getAudioSampleRate();
|
||||
info.iChannel = getAudioChannel();
|
||||
info.iSampleBit = getAudioSampleBit();
|
||||
m_pChn->initAudio(info);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user