Files
ZLMediaKit/src/Pusher/PusherProxy.cpp

100 lines
3.3 KiB
C++
Raw Normal View History

2021-06-28 16:02:13 +08:00
/*
2021-06-16 19:40:08 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#include "PusherProxy.h"
using namespace toolkit;
namespace mediakit {
PusherProxy::PusherProxy(const MediaSource::Ptr &src, int retry_count, const EventPoller::Ptr &poller)
: MediaPusher(src, poller){
2021-06-16 19:40:08 +08:00
_retry_count = retry_count;
_on_close = [](const SockException &) {};
_weak_src = src;
2021-06-16 19:40:08 +08:00
}
PusherProxy::~PusherProxy() {
_timer.reset();
}
void PusherProxy::setPushCallbackOnce(const function<void(const SockException &ex)> &cb) {
_on_publish = cb;
}
void PusherProxy::setOnClose(const function<void(const SockException &ex)> &cb) {
_on_close = cb;
}
void PusherProxy::publish(const string &dst_url) {
std::weak_ptr<PusherProxy> weak_self = shared_from_this();
std::shared_ptr<int> failed_cnt(new int(0));
2021-06-16 19:40:08 +08:00
setOnPublished([weak_self, dst_url, failed_cnt](const SockException &err) {
auto strong_self = weak_self.lock();
if (!strong_self) {
return;
}
2021-06-16 19:40:08 +08:00
if (strong_self->_on_publish) {
strong_self->_on_publish(err);
strong_self->_on_publish = nullptr;
}
2021-06-16 19:40:08 +08:00
auto src = strong_self->_weak_src.lock();
2021-06-16 19:40:08 +08:00
if (!err) {
// 推流成功
*failed_cnt = 0;
InfoL << "Publish " << dst_url << " success";
} else if (src && (*failed_cnt < strong_self->_retry_count || strong_self->_retry_count < 0)) {
2021-06-16 19:40:08 +08:00
// 推流失败,延时重试推送
strong_self->rePublish(dst_url, (*failed_cnt)++);
2021-06-16 19:40:08 +08:00
} else {
//如果媒体源已经注销, 或达到了最大重试次数,回调关闭
strong_self->_on_close(err);
2021-06-16 19:40:08 +08:00
}
});
setOnShutdown([weak_self, dst_url, failed_cnt](const SockException &err) {
auto strong_self = weak_self.lock();
if (!strong_self) {
return;
}
2021-06-16 19:40:08 +08:00
auto src = strong_self->_weak_src.lock();
2021-06-16 19:40:08 +08:00
//推流异常中断,延时重试播放
if (src && (*failed_cnt < strong_self->_retry_count || strong_self->_retry_count < 0)) {
strong_self->rePublish(dst_url, (*failed_cnt)++);
2021-06-16 19:40:08 +08:00
} else {
//如果媒体源已经注销, 或达到了最大重试次数,回调关闭
strong_self->_on_close(err);
2021-06-16 19:40:08 +08:00
}
});
MediaPusher::publish(dst_url);
2021-06-16 19:40:08 +08:00
}
void PusherProxy::rePublish(const string &dst_url, int failed_cnt) {
auto delay = MAX(2 * 1000, MIN(failed_cnt * 3000, 60 * 1000));
weak_ptr<PusherProxy> weak_self = shared_from_this();
_timer = std::make_shared<Timer>(delay / 1000.0f, [weak_self, dst_url, failed_cnt]() {
2021-06-16 19:40:08 +08:00
//推流失败次数越多,则延时越长
auto strong_self = weak_self.lock();
if (!strong_self) {
2021-06-16 19:40:08 +08:00
return false;
}
WarnL << "推流重试[" << failed_cnt << "]:" << dst_url;
strong_self->MediaPusher::publish(dst_url);
2021-06-16 19:40:08 +08:00
return false;
}, getPoller());
}
} /* namespace mediakit */