完善PlayerProxy关闭机制,重试次数超限后自动关闭

This commit is contained in:
ziyue 2021-06-09 15:01:45 +08:00
parent ad2cd11eec
commit 92f879d703
5 changed files with 53 additions and 48 deletions

View File

@ -59,7 +59,7 @@ API_EXPORT void API_CALL mk_proxy_player_play(mk_proxy_player ctx, const char *u
* mk_proxy_player_release函数MediaSource.close()
* @param user_data mk_proxy_player_set_on_close函数设置
*/
typedef void(API_CALL *on_mk_proxy_player_close)(void *user_data);
typedef void(API_CALL *on_mk_proxy_player_close)(void *user_data, int err, const char *what, int sys_err);
/**
* MediaSource.close()

View File

@ -51,9 +51,9 @@ API_EXPORT void API_CALL mk_proxy_player_set_on_close(mk_proxy_player ctx, on_mk
PlayerProxy::Ptr &obj = *((PlayerProxy::Ptr *) ctx);
obj->getPoller()->async([obj,cb,user_data](){
//切换线程再操作
obj->setOnClose([cb,user_data](){
obj->setOnClose([cb,user_data](const SockException &ex){
if(cb){
cb(user_data);
cb(user_data, ex.getErrCode(), ex.what(), ex.getCustomCode());
}
});
});

View File

@ -634,7 +634,7 @@ void installWebApi() {
});
//被主动关闭拉流
player->setOnClose([key](){
player->setOnClose([key](const SockException &ex){
lock_guard<recursive_mutex> lck(s_proxyMapMtx);
s_proxyMap.erase(key);
});

View File

@ -55,14 +55,15 @@ PlayerProxy::PlayerProxy(const string &vhost, const string &app, const string &s
_enable_hls = enable_hls;
_enable_mp4 = enable_mp4;
_retry_count = retry_count;
_on_close = [](const SockException &) {};
}
void PlayerProxy::setPlayCallbackOnce(const function<void(const SockException &ex)> &cb) {
_on_play = cb;
}
void PlayerProxy::setOnClose(const function<void()> &cb){
_on_close = cb;
void PlayerProxy::setOnClose(const function<void(const SockException &ex)> &cb) {
_on_close = cb ? cb : [](const SockException &) {};
}
void PlayerProxy::play(const string &strUrlTmp) {
@ -86,6 +87,9 @@ void PlayerProxy::play(const string &strUrlTmp) {
} else if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) {
// 播放失败,延时重试播放
strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++);
} else {
//达到了最大重试次数,回调关闭
strongSelf->_on_close(err);
}
});
setOnShutdown([weakSelf, strUrlTmp, piFailedCnt](const SockException &err) {
@ -113,6 +117,9 @@ void PlayerProxy::play(const string &strUrlTmp) {
//播放异常中断,延时重试播放
if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) {
strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++);
} else {
//达到了最大重试次数,回调关闭
strongSelf->_on_close(err);
}
});
MediaPlayer::play(strUrlTmp);
@ -174,9 +181,7 @@ bool PlayerProxy::close(MediaSource &sender,bool force) {
strongSelf->setMediaSource(nullptr);
strongSelf->teardown();
});
if (_on_close) {
_on_close();
}
_on_close(SockException(Err_shutdown, "closed by user"));
WarnL << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
return true;
}

View File

@ -34,15 +34,15 @@ public:
/**
* play结果回调play执行之前有效
* @param cb
* @param cb
*/
void setPlayCallbackOnce(const function<void(const SockException &ex)> &cb);
/**
*
* @param cb
* @param cb
*/
void setOnClose(const function<void()> &cb);
void setOnClose(const function<void(const SockException &ex)> &cb);
/**
*
@ -76,7 +76,7 @@ private:
string _stream_id;
string _pull_url;
Timer::Ptr _timer;
function<void()> _on_close;
function<void(const SockException &ex)> _on_close;
function<void(const SockException &ex)> _on_play;
MultiMediaSourceMuxer::Ptr _muxer;
};