媒体源注销后不做无谓推流重试

This commit is contained in:
ziyue 2021-06-17 11:32:57 +08:00
parent e4172b4ab1
commit 60a2346819
2 changed files with 40 additions and 33 deletions

View File

@ -18,6 +18,7 @@ PusherProxy::PusherProxy(const MediaSource::Ptr &src, int retry_count, const Eve
: MediaPusher(src, poller){ : MediaPusher(src, poller){
_retry_count = retry_count; _retry_count = retry_count;
_on_close = [](const SockException &) {}; _on_close = [](const SockException &) {};
_weak_src = src;
} }
PusherProxy::~PusherProxy() { PusherProxy::~PusherProxy() {
@ -32,59 +33,65 @@ void PusherProxy::setOnClose(const function<void(const SockException &ex)> &cb)
_on_close = cb; _on_close = cb;
} }
void PusherProxy::publish(const string& dstUrl) { void PusherProxy::publish(const string &dst_url) {
std::weak_ptr<PusherProxy> weakSelf = shared_from_this(); std::weak_ptr<PusherProxy> weak_self = shared_from_this();
std::shared_ptr<int> piFailedCnt(new int(0)); std::shared_ptr<int> failed_cnt(new int(0));
setOnPublished([weakSelf, dstUrl, piFailedCnt](const SockException &err) { setOnPublished([weak_self, dst_url, failed_cnt](const SockException &err) {
auto strongSelf = weakSelf.lock(); auto strong_self = weak_self.lock();
if (!strongSelf) return; if (!strong_self) {
return;
}
if (strongSelf->_on_publish) { if (strong_self->_on_publish) {
strongSelf->_on_publish(err); strong_self->_on_publish(err);
strongSelf->_on_publish = nullptr; strong_self->_on_publish = nullptr;
} }
auto src = strong_self->_weak_src.lock();
if (!err) { if (!err) {
// 推流成功 // 推流成功
*piFailedCnt = 0; *failed_cnt = 0;
InfoL << "Publish " << dstUrl << " success"; InfoL << "Publish " << dst_url << " success";
} else if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) { } else if (src && (*failed_cnt < strong_self->_retry_count || strong_self->_retry_count < 0)) {
// 推流失败,延时重试推送 // 推流失败,延时重试推送
strongSelf->rePublish(dstUrl, (*piFailedCnt)++); strong_self->rePublish(dst_url, (*failed_cnt)++);
} else { } else {
//达到了最大重试次数,回调关闭 //如果媒体源已经注销, 或达到了最大重试次数,回调关闭
strongSelf->_on_close(err); strong_self->_on_close(err);
} }
}); });
setOnShutdown([weakSelf, dstUrl, piFailedCnt](const SockException &err) { setOnShutdown([weak_self, dst_url, failed_cnt](const SockException &err) {
auto strongSelf = weakSelf.lock(); auto strong_self = weak_self.lock();
if (!strongSelf) return; if (!strong_self) {
return;
}
auto src = strong_self->_weak_src.lock();
//推流异常中断,延时重试播放 //推流异常中断,延时重试播放
if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) { if (src && (*failed_cnt < strong_self->_retry_count || strong_self->_retry_count < 0)) {
strongSelf->rePublish(dstUrl, (*piFailedCnt)++); strong_self->rePublish(dst_url, (*failed_cnt)++);
} else { } else {
//达到了最大重试次数,回调关闭 //如果媒体源已经注销, 或达到了最大重试次数,回调关闭
strongSelf->_on_close(err); strong_self->_on_close(err);
} }
}); });
MediaPusher::publish(dstUrl); MediaPusher::publish(dst_url);
} }
void PusherProxy::rePublish(const string &dstUrl, int iFailedCnt) { void PusherProxy::rePublish(const string &dst_url, int failed_cnt) {
auto iDelay = MAX(2 * 1000, MIN(iFailedCnt * 3000, 60 * 1000)); auto delay = MAX(2 * 1000, MIN(failed_cnt * 3000, 60 * 1000));
weak_ptr<PusherProxy> weakSelf = shared_from_this(); weak_ptr<PusherProxy> weak_self = shared_from_this();
_timer = std::make_shared<Timer>(iDelay / 1000.0f, [weakSelf, dstUrl, iFailedCnt]() { _timer = std::make_shared<Timer>(delay / 1000.0f, [weak_self, dst_url, failed_cnt]() {
//推流失败次数越多,则延时越长 //推流失败次数越多,则延时越长
auto strongPusher = weakSelf.lock(); auto strong_self = weak_self.lock();
if (!strongPusher) { if (!strong_self) {
return false; return false;
} }
WarnL << "推流重试[" << iFailedCnt << "]:" << dstUrl; WarnL << "推流重试[" << failed_cnt << "]:" << dst_url;
strongPusher->MediaPusher::publish(dstUrl); strong_self->MediaPusher::publish(dst_url);
return false; return false;
}, getPoller()); }, getPoller());
} }

View File

@ -53,7 +53,7 @@ private:
private: private:
int _retry_count; int _retry_count;
Timer::Ptr _timer; Timer::Ptr _timer;
std::weak_ptr<MediaSource> _weak_src;
function<void(const SockException &ex)> _on_close; function<void(const SockException &ex)> _on_close;
function<void(const SockException &ex)> _on_publish; function<void(const SockException &ex)> _on_publish;
}; };