完善PlayerProxy关闭机制,重试次数超限后自动关闭
This commit is contained in:
parent
ad2cd11eec
commit
92f879d703
|
|
@ -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()操作将无效
|
* 如果你不调用mk_proxy_player_release函数,那么MediaSource.close()操作将无效
|
||||||
* @param user_data 用户数据指针,通过mk_proxy_player_set_on_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()事件
|
* 监听MediaSource.close()事件
|
||||||
|
|
|
||||||
|
|
@ -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);
|
PlayerProxy::Ptr &obj = *((PlayerProxy::Ptr *) ctx);
|
||||||
obj->getPoller()->async([obj,cb,user_data](){
|
obj->getPoller()->async([obj,cb,user_data](){
|
||||||
//切换线程再操作
|
//切换线程再操作
|
||||||
obj->setOnClose([cb,user_data](){
|
obj->setOnClose([cb,user_data](const SockException &ex){
|
||||||
if(cb){
|
if(cb){
|
||||||
cb(user_data);
|
cb(user_data, ex.getErrCode(), ex.what(), ex.getCustomCode());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -634,7 +634,7 @@ void installWebApi() {
|
||||||
});
|
});
|
||||||
|
|
||||||
//被主动关闭拉流
|
//被主动关闭拉流
|
||||||
player->setOnClose([key](){
|
player->setOnClose([key](const SockException &ex){
|
||||||
lock_guard<recursive_mutex> lck(s_proxyMapMtx);
|
lock_guard<recursive_mutex> lck(s_proxyMapMtx);
|
||||||
s_proxyMap.erase(key);
|
s_proxyMap.erase(key);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -55,14 +55,15 @@ PlayerProxy::PlayerProxy(const string &vhost, const string &app, const string &s
|
||||||
_enable_hls = enable_hls;
|
_enable_hls = enable_hls;
|
||||||
_enable_mp4 = enable_mp4;
|
_enable_mp4 = enable_mp4;
|
||||||
_retry_count = retry_count;
|
_retry_count = retry_count;
|
||||||
|
_on_close = [](const SockException &) {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerProxy::setPlayCallbackOnce(const function<void(const SockException &ex)> &cb) {
|
void PlayerProxy::setPlayCallbackOnce(const function<void(const SockException &ex)> &cb) {
|
||||||
_on_play = cb;
|
_on_play = cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerProxy::setOnClose(const function<void()> &cb){
|
void PlayerProxy::setOnClose(const function<void(const SockException &ex)> &cb) {
|
||||||
_on_close = cb;
|
_on_close = cb ? cb : [](const SockException &) {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerProxy::play(const string &strUrlTmp) {
|
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) {
|
} else if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) {
|
||||||
// 播放失败,延时重试播放
|
// 播放失败,延时重试播放
|
||||||
strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++);
|
strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++);
|
||||||
|
} else {
|
||||||
|
//达到了最大重试次数,回调关闭
|
||||||
|
strongSelf->_on_close(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
setOnShutdown([weakSelf, strUrlTmp, piFailedCnt](const SockException &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) {
|
if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) {
|
||||||
strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++);
|
strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++);
|
||||||
|
} else {
|
||||||
|
//达到了最大重试次数,回调关闭
|
||||||
|
strongSelf->_on_close(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
MediaPlayer::play(strUrlTmp);
|
MediaPlayer::play(strUrlTmp);
|
||||||
|
|
@ -174,9 +181,7 @@ bool PlayerProxy::close(MediaSource &sender,bool force) {
|
||||||
strongSelf->setMediaSource(nullptr);
|
strongSelf->setMediaSource(nullptr);
|
||||||
strongSelf->teardown();
|
strongSelf->teardown();
|
||||||
});
|
});
|
||||||
if (_on_close) {
|
_on_close(SockException(Err_shutdown, "closed by user"));
|
||||||
_on_close();
|
|
||||||
}
|
|
||||||
WarnL << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
|
WarnL << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,15 +34,15 @@ public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置play结果回调,只触发一次;在play执行之前有效
|
* 设置play结果回调,只触发一次;在play执行之前有效
|
||||||
* @param cb
|
* @param cb 回调对象
|
||||||
*/
|
*/
|
||||||
void setPlayCallbackOnce(const function<void(const SockException &ex)> &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 _stream_id;
|
||||||
string _pull_url;
|
string _pull_url;
|
||||||
Timer::Ptr _timer;
|
Timer::Ptr _timer;
|
||||||
function<void()> _on_close;
|
function<void(const SockException &ex)> _on_close;
|
||||||
function<void(const SockException &ex)> _on_play;
|
function<void(const SockException &ex)> _on_play;
|
||||||
MultiMediaSourceMuxer::Ptr _muxer;
|
MultiMediaSourceMuxer::Ptr _muxer;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue