format code style for pusher and player
This commit is contained in:
parent
7c89c1655f
commit
ffed4b3bb2
|
|
@ -8,24 +8,26 @@
|
||||||
* may be found in the AUTHORS file in the root of the source tree.
|
* may be found in the AUTHORS file in the root of the source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Common/config.h"
|
|
||||||
#include "PlayerProxy.h"
|
#include "PlayerProxy.h"
|
||||||
#include "Util/mini.h"
|
#include "Common/config.h"
|
||||||
#include "Util/MD5.h"
|
|
||||||
#include "Util/logger.h"
|
|
||||||
#include "Extension/AAC.h"
|
#include "Extension/AAC.h"
|
||||||
#include "Rtmp/RtmpMediaSource.h"
|
#include "Rtmp/RtmpMediaSource.h"
|
||||||
#include "Rtsp/RtspMediaSource.h"
|
|
||||||
#include "Rtmp/RtmpPlayer.h"
|
#include "Rtmp/RtmpPlayer.h"
|
||||||
|
#include "Rtsp/RtspMediaSource.h"
|
||||||
#include "Rtsp/RtspPlayer.h"
|
#include "Rtsp/RtspPlayer.h"
|
||||||
|
#include "Util/MD5.h"
|
||||||
|
#include "Util/logger.h"
|
||||||
|
#include "Util/mini.h"
|
||||||
|
|
||||||
using namespace toolkit;
|
using namespace toolkit;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
namespace mediakit {
|
namespace mediakit {
|
||||||
|
|
||||||
PlayerProxy::PlayerProxy(const string &vhost, const string &app, const string &stream_id, const ProtocolOption &option,
|
PlayerProxy::PlayerProxy(
|
||||||
int retry_count, const EventPoller::Ptr &poller) : MediaPlayer(poller) , _option(option) {
|
const string &vhost, const string &app, const string &stream_id, const ProtocolOption &option, int retry_count, const EventPoller::Ptr &poller)
|
||||||
|
: MediaPlayer(poller)
|
||||||
|
, _option(option) {
|
||||||
_vhost = vhost;
|
_vhost = vhost;
|
||||||
_app = app;
|
_app = app;
|
||||||
_stream_id = stream_id;
|
_stream_id = stream_id;
|
||||||
|
|
@ -48,7 +50,7 @@ void PlayerProxy::setOnClose(const function<void(const SockException &ex)> &cb)
|
||||||
|
|
||||||
void PlayerProxy::play(const string &strUrlTmp) {
|
void PlayerProxy::play(const string &strUrlTmp) {
|
||||||
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
|
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
|
||||||
std::shared_ptr<int> piFailedCnt(new int(0)); //连续播放失败次数
|
std::shared_ptr<int> piFailedCnt(new int(0)); // 连续播放失败次数
|
||||||
setOnPlayResult([weakSelf, strUrlTmp, piFailedCnt](const SockException &err) {
|
setOnPlayResult([weakSelf, strUrlTmp, piFailedCnt](const SockException &err) {
|
||||||
auto strongSelf = weakSelf.lock();
|
auto strongSelf = weakSelf.lock();
|
||||||
if (!strongSelf) {
|
if (!strongSelf) {
|
||||||
|
|
@ -66,7 +68,7 @@ void PlayerProxy::play(const string &strUrlTmp) {
|
||||||
strongSelf->_live_ticker.resetTime();
|
strongSelf->_live_ticker.resetTime();
|
||||||
strongSelf->_live_status = 0;
|
strongSelf->_live_status = 0;
|
||||||
// 播放成功
|
// 播放成功
|
||||||
*piFailedCnt = 0;//连续播放失败次数清0
|
*piFailedCnt = 0; // 连续播放失败次数清0
|
||||||
strongSelf->onPlaySuccess();
|
strongSelf->onPlaySuccess();
|
||||||
|
|
||||||
InfoL << "play " << strUrlTmp << " success";
|
InfoL << "play " << strUrlTmp << " success";
|
||||||
|
|
@ -74,7 +76,7 @@ void PlayerProxy::play(const string &strUrlTmp) {
|
||||||
// 播放失败,延时重试播放
|
// 播放失败,延时重试播放
|
||||||
strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++);
|
strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++);
|
||||||
} else {
|
} else {
|
||||||
//达到了最大重试次数,回调关闭
|
// 达到了最大重试次数,回调关闭
|
||||||
strongSelf->_on_close(err);
|
strongSelf->_on_close(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -84,7 +86,7 @@ void PlayerProxy::play(const string &strUrlTmp) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//注销直接拉流代理产生的流:#532
|
// 注销直接拉流代理产生的流:#532
|
||||||
strongSelf->setMediaSource(nullptr);
|
strongSelf->setMediaSource(nullptr);
|
||||||
|
|
||||||
if (strongSelf->_muxer) {
|
if (strongSelf->_muxer) {
|
||||||
|
|
@ -101,19 +103,19 @@ void PlayerProxy::play(const string &strUrlTmp) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(*piFailedCnt == 0){
|
if (*piFailedCnt == 0) {
|
||||||
// 第一次重拉更新时长
|
// 第一次重拉更新时长
|
||||||
strongSelf->_live_secs += strongSelf->_live_ticker.elapsedTime()/1000;
|
strongSelf->_live_secs += strongSelf->_live_ticker.elapsedTime() / 1000;
|
||||||
strongSelf->_live_ticker.resetTime();
|
strongSelf->_live_ticker.resetTime();
|
||||||
TraceL<<" live secs "<<strongSelf->_live_secs;
|
TraceL << " live secs " << strongSelf->_live_secs;
|
||||||
}
|
}
|
||||||
|
|
||||||
//播放异常中断,延时重试播放
|
// 播放异常中断,延时重试播放
|
||||||
if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) {
|
if (*piFailedCnt < strongSelf->_retry_count || strongSelf->_retry_count < 0) {
|
||||||
strongSelf->_repull_count++;
|
strongSelf->_repull_count++;
|
||||||
strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++);
|
strongSelf->rePlay(strUrlTmp, (*piFailedCnt)++);
|
||||||
} else {
|
} else {
|
||||||
//达到了最大重试次数,回调关闭
|
// 达到了最大重试次数,回调关闭
|
||||||
strongSelf->_on_close(err);
|
strongSelf->_on_close(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -131,13 +133,13 @@ void PlayerProxy::play(const string &strUrlTmp) {
|
||||||
void PlayerProxy::setDirectProxy() {
|
void PlayerProxy::setDirectProxy() {
|
||||||
MediaSource::Ptr mediaSource;
|
MediaSource::Ptr mediaSource;
|
||||||
if (dynamic_pointer_cast<RtspPlayer>(_delegate)) {
|
if (dynamic_pointer_cast<RtspPlayer>(_delegate)) {
|
||||||
//rtsp拉流
|
// rtsp拉流
|
||||||
GET_CONFIG(bool, directProxy, Rtsp::kDirectProxy);
|
GET_CONFIG(bool, directProxy, Rtsp::kDirectProxy);
|
||||||
if (directProxy) {
|
if (directProxy) {
|
||||||
mediaSource = std::make_shared<RtspMediaSource>(_vhost, _app, _stream_id);
|
mediaSource = std::make_shared<RtspMediaSource>(_vhost, _app, _stream_id);
|
||||||
}
|
}
|
||||||
} else if (dynamic_pointer_cast<RtmpPlayer>(_delegate)) {
|
} else if (dynamic_pointer_cast<RtmpPlayer>(_delegate)) {
|
||||||
//rtmp拉流,rtmp强制直接代理
|
// rtmp拉流,rtmp强制直接代理
|
||||||
mediaSource = std::make_shared<RtmpMediaSource>(_vhost, _app, _stream_id);
|
mediaSource = std::make_shared<RtmpMediaSource>(_vhost, _app, _stream_id);
|
||||||
}
|
}
|
||||||
if (mediaSource) {
|
if (mediaSource) {
|
||||||
|
|
@ -148,7 +150,7 @@ void PlayerProxy::setDirectProxy() {
|
||||||
PlayerProxy::~PlayerProxy() {
|
PlayerProxy::~PlayerProxy() {
|
||||||
_timer.reset();
|
_timer.reset();
|
||||||
// 避免析构时, 忘记回调api请求
|
// 避免析构时, 忘记回调api请求
|
||||||
if(_on_play) {
|
if (_on_play) {
|
||||||
_on_play(SockException(Err_shutdown, "player proxy close"));
|
_on_play(SockException(Err_shutdown, "player proxy close"));
|
||||||
_on_play = nullptr;
|
_on_play = nullptr;
|
||||||
}
|
}
|
||||||
|
|
@ -157,21 +159,24 @@ PlayerProxy::~PlayerProxy() {
|
||||||
void PlayerProxy::rePlay(const string &strUrl, int iFailedCnt) {
|
void PlayerProxy::rePlay(const string &strUrl, int iFailedCnt) {
|
||||||
auto iDelay = MAX(2 * 1000, MIN(iFailedCnt * 3000, 60 * 1000));
|
auto iDelay = MAX(2 * 1000, MIN(iFailedCnt * 3000, 60 * 1000));
|
||||||
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
|
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
|
||||||
_timer = std::make_shared<Timer>(iDelay / 1000.0f, [weakSelf, strUrl, iFailedCnt]() {
|
_timer = std::make_shared<Timer>(
|
||||||
//播放失败次数越多,则延时越长
|
iDelay / 1000.0f,
|
||||||
auto strongPlayer = weakSelf.lock();
|
[weakSelf, strUrl, iFailedCnt]() {
|
||||||
if (!strongPlayer) {
|
// 播放失败次数越多,则延时越长
|
||||||
|
auto strongPlayer = weakSelf.lock();
|
||||||
|
if (!strongPlayer) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
WarnL << "重试播放[" << iFailedCnt << "]:" << strUrl;
|
||||||
|
strongPlayer->MediaPlayer::play(strUrl);
|
||||||
|
strongPlayer->setDirectProxy();
|
||||||
return false;
|
return false;
|
||||||
}
|
},
|
||||||
WarnL << "重试播放[" << iFailedCnt << "]:" << strUrl;
|
getPoller());
|
||||||
strongPlayer->MediaPlayer::play(strUrl);
|
|
||||||
strongPlayer->setDirectProxy();
|
|
||||||
return false;
|
|
||||||
}, getPoller());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PlayerProxy::close(MediaSource &sender) {
|
bool PlayerProxy::close(MediaSource &sender) {
|
||||||
//通知其停止推流
|
// 通知其停止推流
|
||||||
weak_ptr<PlayerProxy> weakSelf = dynamic_pointer_cast<PlayerProxy>(shared_from_this());
|
weak_ptr<PlayerProxy> weakSelf = dynamic_pointer_cast<PlayerProxy>(shared_from_this());
|
||||||
getPoller()->async_first([weakSelf]() {
|
getPoller()->async_first([weakSelf]() {
|
||||||
auto strongSelf = weakSelf.lock();
|
auto strongSelf = weakSelf.lock();
|
||||||
|
|
@ -214,19 +219,19 @@ float PlayerProxy::getLossRate(MediaSource &sender, TrackType type) {
|
||||||
void PlayerProxy::onPlaySuccess() {
|
void PlayerProxy::onPlaySuccess() {
|
||||||
GET_CONFIG(bool, reset_when_replay, General::kResetWhenRePlay);
|
GET_CONFIG(bool, reset_when_replay, General::kResetWhenRePlay);
|
||||||
if (dynamic_pointer_cast<RtspMediaSource>(_media_src)) {
|
if (dynamic_pointer_cast<RtspMediaSource>(_media_src)) {
|
||||||
//rtsp拉流代理
|
// rtsp拉流代理
|
||||||
if (reset_when_replay || !_muxer) {
|
if (reset_when_replay || !_muxer) {
|
||||||
_option.enable_rtsp = false;
|
_option.enable_rtsp = false;
|
||||||
_muxer = std::make_shared<MultiMediaSourceMuxer>(_vhost, _app, _stream_id, getDuration(), _option);
|
_muxer = std::make_shared<MultiMediaSourceMuxer>(_vhost, _app, _stream_id, getDuration(), _option);
|
||||||
}
|
}
|
||||||
} else if (dynamic_pointer_cast<RtmpMediaSource>(_media_src)) {
|
} else if (dynamic_pointer_cast<RtmpMediaSource>(_media_src)) {
|
||||||
//rtmp拉流代理
|
// rtmp拉流代理
|
||||||
if (reset_when_replay || !_muxer) {
|
if (reset_when_replay || !_muxer) {
|
||||||
_option.enable_rtmp = false;
|
_option.enable_rtmp = false;
|
||||||
_muxer = std::make_shared<MultiMediaSourceMuxer>(_vhost, _app, _stream_id, getDuration(), _option);
|
_muxer = std::make_shared<MultiMediaSourceMuxer>(_vhost, _app, _stream_id, getDuration(), _option);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//其他拉流代理
|
// 其他拉流代理
|
||||||
if (reset_when_replay || !_muxer) {
|
if (reset_when_replay || !_muxer) {
|
||||||
_muxer = std::make_shared<MultiMediaSourceMuxer>(_vhost, _app, _stream_id, getDuration(), _option);
|
_muxer = std::make_shared<MultiMediaSourceMuxer>(_vhost, _app, _stream_id, getDuration(), _option);
|
||||||
}
|
}
|
||||||
|
|
@ -235,25 +240,25 @@ void PlayerProxy::onPlaySuccess() {
|
||||||
|
|
||||||
auto videoTrack = getTrack(TrackVideo, false);
|
auto videoTrack = getTrack(TrackVideo, false);
|
||||||
if (videoTrack) {
|
if (videoTrack) {
|
||||||
//添加视频
|
// 添加视频
|
||||||
_muxer->addTrack(videoTrack);
|
_muxer->addTrack(videoTrack);
|
||||||
//视频数据写入_mediaMuxer
|
// 视频数据写入_mediaMuxer
|
||||||
videoTrack->addDelegate(_muxer);
|
videoTrack->addDelegate(_muxer);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto audioTrack = getTrack(TrackAudio, false);
|
auto audioTrack = getTrack(TrackAudio, false);
|
||||||
if (audioTrack) {
|
if (audioTrack) {
|
||||||
//添加音频
|
// 添加音频
|
||||||
_muxer->addTrack(audioTrack);
|
_muxer->addTrack(audioTrack);
|
||||||
//音频数据写入_mediaMuxer
|
// 音频数据写入_mediaMuxer
|
||||||
audioTrack->addDelegate(_muxer);
|
audioTrack->addDelegate(_muxer);
|
||||||
}
|
}
|
||||||
|
|
||||||
//添加完毕所有track,防止单track情况下最大等待3秒
|
// 添加完毕所有track,防止单track情况下最大等待3秒
|
||||||
_muxer->addTrackCompleted();
|
_muxer->addTrackCompleted();
|
||||||
|
|
||||||
if (_media_src) {
|
if (_media_src) {
|
||||||
//让_muxer对象拦截一部分事件(比如说录像相关事件)
|
// 让_muxer对象拦截一部分事件(比如说录像相关事件)
|
||||||
_media_src->setListener(_muxer);
|
_media_src->setListener(_muxer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -262,14 +267,14 @@ int PlayerProxy::getStatus() {
|
||||||
return _live_status.load();
|
return _live_status.load();
|
||||||
}
|
}
|
||||||
uint64_t PlayerProxy::getLiveSecs() {
|
uint64_t PlayerProxy::getLiveSecs() {
|
||||||
if(_live_status == 0){
|
if (_live_status == 0) {
|
||||||
return _live_secs + _live_ticker.elapsedTime()/1000;
|
return _live_secs + _live_ticker.elapsedTime() / 1000;
|
||||||
}else{
|
} else {
|
||||||
return _live_secs;
|
return _live_secs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t PlayerProxy::getRePullCount(){
|
uint64_t PlayerProxy::getRePullCount() {
|
||||||
return _repull_count;
|
return _repull_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,21 +11,25 @@
|
||||||
#ifndef SRC_DEVICE_PLAYERPROXY_H_
|
#ifndef SRC_DEVICE_PLAYERPROXY_H_
|
||||||
#define SRC_DEVICE_PLAYERPROXY_H_
|
#define SRC_DEVICE_PLAYERPROXY_H_
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include "Common/MultiMediaSourceMuxer.h"
|
#include "Common/MultiMediaSourceMuxer.h"
|
||||||
#include "Player/MediaPlayer.h"
|
#include "Player/MediaPlayer.h"
|
||||||
#include "Util/TimeTicker.h"
|
#include "Util/TimeTicker.h"
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace mediakit {
|
namespace mediakit {
|
||||||
|
|
||||||
class PlayerProxy : public MediaPlayer, public MediaSourceEvent, public std::enable_shared_from_this<PlayerProxy> {
|
class PlayerProxy
|
||||||
|
: public MediaPlayer
|
||||||
|
, public MediaSourceEvent
|
||||||
|
, public std::enable_shared_from_this<PlayerProxy> {
|
||||||
public:
|
public:
|
||||||
using Ptr = std::shared_ptr<PlayerProxy>;
|
using Ptr = std::shared_ptr<PlayerProxy>;
|
||||||
|
|
||||||
//如果retry_count<0,则一直重试播放;否则重试retry_count次数
|
// 如果retry_count<0,则一直重试播放;否则重试retry_count次数
|
||||||
//默认一直重试
|
// 默认一直重试
|
||||||
PlayerProxy(const std::string &vhost, const std::string &app, const std::string &stream_id,
|
PlayerProxy(
|
||||||
const ProtocolOption &option, int retry_count = -1, const toolkit::EventPoller::Ptr &poller = nullptr);
|
const std::string &vhost, const std::string &app, const std::string &stream_id, const ProtocolOption &option, int retry_count = -1,
|
||||||
|
const toolkit::EventPoller::Ptr &poller = nullptr);
|
||||||
|
|
||||||
~PlayerProxy() override;
|
~PlayerProxy() override;
|
||||||
|
|
||||||
|
|
@ -50,15 +54,14 @@ public:
|
||||||
/**
|
/**
|
||||||
* 获取观看总人数
|
* 获取观看总人数
|
||||||
*/
|
*/
|
||||||
int totalReaderCount() ;
|
int totalReaderCount();
|
||||||
|
|
||||||
|
|
||||||
int getStatus();
|
int getStatus();
|
||||||
uint64_t getLiveSecs();
|
uint64_t getLiveSecs();
|
||||||
uint64_t getRePullCount();
|
uint64_t getRePullCount();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//MediaSourceEvent override
|
// MediaSourceEvent override
|
||||||
bool close(MediaSource &sender) override;
|
bool close(MediaSource &sender) override;
|
||||||
int totalReaderCount(MediaSource &sender) override;
|
int totalReaderCount(MediaSource &sender) override;
|
||||||
MediaOriginType getOriginType(MediaSource &sender) const override;
|
MediaOriginType getOriginType(MediaSource &sender) const override;
|
||||||
|
|
@ -66,7 +69,7 @@ private:
|
||||||
std::shared_ptr<toolkit::SockInfo> getOriginSock(MediaSource &sender) const override;
|
std::shared_ptr<toolkit::SockInfo> getOriginSock(MediaSource &sender) const override;
|
||||||
float getLossRate(MediaSource &sender, TrackType type) override;
|
float getLossRate(MediaSource &sender, TrackType type) override;
|
||||||
|
|
||||||
void rePlay(const std::string &strUrl,int iFailedCnt);
|
void rePlay(const std::string &strUrl, int iFailedCnt);
|
||||||
void onPlaySuccess();
|
void onPlaySuccess();
|
||||||
void setDirectProxy();
|
void setDirectProxy();
|
||||||
|
|
||||||
|
|
@ -83,7 +86,7 @@ private:
|
||||||
MultiMediaSourceMuxer::Ptr _muxer;
|
MultiMediaSourceMuxer::Ptr _muxer;
|
||||||
|
|
||||||
toolkit::Ticker _live_ticker;
|
toolkit::Ticker _live_ticker;
|
||||||
//0 表示正常 1 表示正在尝试拉流
|
// 0 表示正常 1 表示正在尝试拉流
|
||||||
std::atomic<int> _live_status;
|
std::atomic<int> _live_status;
|
||||||
std::atomic<uint64_t> _live_secs;
|
std::atomic<uint64_t> _live_secs;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ using namespace std;
|
||||||
namespace mediakit {
|
namespace mediakit {
|
||||||
|
|
||||||
PusherProxy::PusherProxy(const MediaSource::Ptr &src, int retry_count, const EventPoller::Ptr &poller)
|
PusherProxy::PusherProxy(const MediaSource::Ptr &src, int retry_count, const EventPoller::Ptr &poller)
|
||||||
: 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;
|
_weak_src = src;
|
||||||
|
|
@ -65,7 +65,7 @@ void PusherProxy::publish(const string &dst_url) {
|
||||||
strong_self->_live_status = 1;
|
strong_self->_live_status = 1;
|
||||||
strong_self->rePublish(dst_url, (*failed_cnt)++);
|
strong_self->rePublish(dst_url, (*failed_cnt)++);
|
||||||
} else {
|
} else {
|
||||||
//如果媒体源已经注销, 或达到了最大重试次数,回调关闭
|
// 如果媒体源已经注销, 或达到了最大重试次数,回调关闭
|
||||||
strong_self->_on_close(err);
|
strong_self->_on_close(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -76,20 +76,20 @@ void PusherProxy::publish(const string &dst_url) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(*failed_cnt == 0){
|
if (*failed_cnt == 0) {
|
||||||
// 第一次重推更新时长
|
// 第一次重推更新时长
|
||||||
strong_self->_live_secs += strong_self->_live_ticker.elapsedTime()/1000;
|
strong_self->_live_secs += strong_self->_live_ticker.elapsedTime() / 1000;
|
||||||
strong_self->_live_ticker.resetTime();
|
strong_self->_live_ticker.resetTime();
|
||||||
TraceL<<" live secs "<<strong_self->_live_secs;
|
TraceL << " live secs " << strong_self->_live_secs;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto src = strong_self->_weak_src.lock();
|
auto src = strong_self->_weak_src.lock();
|
||||||
//推流异常中断,延时重试播放
|
// 推流异常中断,延时重试播放
|
||||||
if (src && (*failed_cnt < strong_self->_retry_count || strong_self->_retry_count < 0)) {
|
if (src && (*failed_cnt < strong_self->_retry_count || strong_self->_retry_count < 0)) {
|
||||||
strong_self->_republish_count++;
|
strong_self->_republish_count++;
|
||||||
strong_self->rePublish(dst_url, (*failed_cnt)++);
|
strong_self->rePublish(dst_url, (*failed_cnt)++);
|
||||||
} else {
|
} else {
|
||||||
//如果媒体源已经注销, 或达到了最大重试次数,回调关闭
|
// 如果媒体源已经注销, 或达到了最大重试次数,回调关闭
|
||||||
strong_self->_on_close(err);
|
strong_self->_on_close(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -100,30 +100,33 @@ void PusherProxy::publish(const string &dst_url) {
|
||||||
void PusherProxy::rePublish(const string &dst_url, int failed_cnt) {
|
void PusherProxy::rePublish(const string &dst_url, int failed_cnt) {
|
||||||
auto delay = MAX(2 * 1000, MIN(failed_cnt * 3000, 60 * 1000));
|
auto delay = MAX(2 * 1000, MIN(failed_cnt * 3000, 60 * 1000));
|
||||||
weak_ptr<PusherProxy> weak_self = shared_from_this();
|
weak_ptr<PusherProxy> weak_self = shared_from_this();
|
||||||
_timer = std::make_shared<Timer>(delay / 1000.0f, [weak_self, dst_url, failed_cnt]() {
|
_timer = std::make_shared<Timer>(
|
||||||
//推流失败次数越多,则延时越长
|
delay / 1000.0f,
|
||||||
auto strong_self = weak_self.lock();
|
[weak_self, dst_url, failed_cnt]() {
|
||||||
if (!strong_self) {
|
// 推流失败次数越多,则延时越长
|
||||||
|
auto strong_self = weak_self.lock();
|
||||||
|
if (!strong_self) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
WarnL << "推流重试[" << failed_cnt << "]:" << dst_url;
|
||||||
|
strong_self->MediaPusher::publish(dst_url);
|
||||||
return false;
|
return false;
|
||||||
}
|
},
|
||||||
WarnL << "推流重试[" << failed_cnt << "]:" << dst_url;
|
getPoller());
|
||||||
strong_self->MediaPusher::publish(dst_url);
|
|
||||||
return false;
|
|
||||||
}, getPoller());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int PusherProxy::getStatus() {
|
int PusherProxy::getStatus() {
|
||||||
return _live_status.load();
|
return _live_status.load();
|
||||||
}
|
}
|
||||||
uint64_t PusherProxy::getLiveSecs() {
|
uint64_t PusherProxy::getLiveSecs() {
|
||||||
if(_live_status == 0){
|
if (_live_status == 0) {
|
||||||
return _live_secs + _live_ticker.elapsedTime()/1000;
|
return _live_secs + _live_ticker.elapsedTime() / 1000;
|
||||||
}else{
|
} else {
|
||||||
return _live_secs;
|
return _live_secs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t PusherProxy::getRePublishCount(){
|
uint64_t PusherProxy::getRePublishCount() {
|
||||||
return _republish_count;
|
return _republish_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,9 @@
|
||||||
|
|
||||||
namespace mediakit {
|
namespace mediakit {
|
||||||
|
|
||||||
class PusherProxy : public MediaPusher, public std::enable_shared_from_this<PusherProxy> {
|
class PusherProxy
|
||||||
|
: public MediaPusher
|
||||||
|
, public std::enable_shared_from_this<PusherProxy> {
|
||||||
public:
|
public:
|
||||||
using Ptr = std::shared_ptr<PusherProxy>;
|
using Ptr = std::shared_ptr<PusherProxy>;
|
||||||
|
|
||||||
|
|
@ -41,7 +43,7 @@ public:
|
||||||
* 开始拉流播放
|
* 开始拉流播放
|
||||||
* @param dstUrl 目标推流地址
|
* @param dstUrl 目标推流地址
|
||||||
*/
|
*/
|
||||||
void publish(const std::string& dstUrl) override;
|
void publish(const std::string &dstUrl) override;
|
||||||
|
|
||||||
int getStatus();
|
int getStatus();
|
||||||
uint64_t getLiveSecs();
|
uint64_t getLiveSecs();
|
||||||
|
|
@ -55,7 +57,7 @@ private:
|
||||||
int _retry_count;
|
int _retry_count;
|
||||||
toolkit::Timer::Ptr _timer;
|
toolkit::Timer::Ptr _timer;
|
||||||
toolkit::Ticker _live_ticker;
|
toolkit::Ticker _live_ticker;
|
||||||
//0 表示正常 1 表示正在尝试推流
|
// 0 表示正常 1 表示正在尝试推流
|
||||||
std::atomic<int> _live_status;
|
std::atomic<int> _live_status;
|
||||||
std::atomic<uint64_t> _live_secs;
|
std::atomic<uint64_t> _live_secs;
|
||||||
std::atomic<uint64_t> _republish_count;
|
std::atomic<uint64_t> _republish_count;
|
||||||
|
|
@ -66,4 +68,4 @@ private:
|
||||||
|
|
||||||
} /* namespace mediakit */
|
} /* namespace mediakit */
|
||||||
|
|
||||||
#endif //SRC_DEVICE_PUSHERPROXY_H
|
#endif // SRC_DEVICE_PUSHERPROXY_H
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue