diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index d884908fb8..616815a212 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -7,35 +7,36 @@ * 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 -#include "Util/util.h" -#include "Util/NoticeCenter.h" -#include "Network/sockutil.h" -#include "Network/Session.h" #include "MediaSource.h" -#include "Common/config.h" #include "Common/Parser.h" -#include "Record/MP4Reader.h" +#include "Common/config.h" +#include "Network/Session.h" +#include "Network/sockutil.h" #include "PacketCache.h" +#include "Record/MP4Reader.h" +#include "Util/NoticeCenter.h" +#include "Util/util.h" #include "server/AuthCenter.h" +#include using namespace std; using namespace toolkit; namespace toolkit { - StatisticImp(mediakit::MediaSource); +StatisticImp(mediakit::MediaSource); } namespace mediakit { static recursive_mutex s_media_source_mtx; -using StreamMap = unordered_map >; -using AppStreamMap = unordered_map; -using VhostAppStreamMap = unordered_map; -using SchemaVhostAppStreamMap = unordered_map; +using StreamMap = unordered_map>; +using AppStreamMap = unordered_map; +using VhostAppStreamMap = unordered_map; +using SchemaVhostAppStreamMap = unordered_map; static SchemaVhostAppStreamMap s_media_source_map; -string getOriginTypeString(MediaOriginType type){ -#define SWITCH_CASE(type) case MediaOriginType::type : return #type +string getOriginTypeString(MediaOriginType type) { +#define SWITCH_CASE(type) \ + case MediaOriginType::type: return #type switch (type) { SWITCH_CASE(unknown); SWITCH_CASE(rtmp_push); @@ -47,7 +48,7 @@ string getOriginTypeString(MediaOriginType type){ SWITCH_CASE(device_chn); SWITCH_CASE(rtc_push); SWITCH_CASE(srt_push); - default : return "unknown"; + default: return "unknown"; } } @@ -110,7 +111,8 @@ ProtocolOption::ProtocolOption() { ////////////////////////////////////////////////////////////////////////////////////////////////////////////// struct MediaSourceNull : public MediaSource { - MediaSourceNull() : MediaSource("schema", "vhost", "app", "stream") {}; + MediaSourceNull() + : MediaSource("schema", "vhost", "app", "stream") {}; int readerCount() override { return 0; } }; @@ -119,7 +121,7 @@ MediaSource &MediaSource::NullMediaSource() { return *s_null; } -MediaSource::MediaSource(const string &schema, const string &vhost, const string &app, const string &stream_id){ +MediaSource::MediaSource(const string &schema, const string &vhost, const string &app, const string &stream_id) { GET_CONFIG(bool, enableVhost, General::kEnableVhost); if (!enableVhost) { _vhost = DEFAULT_VHOST; @@ -136,31 +138,31 @@ MediaSource::~MediaSource() { unregist(); } -const string& MediaSource::getSchema() const { +const string &MediaSource::getSchema() const { return _schema; } -const string& MediaSource::getVhost() const { +const string &MediaSource::getVhost() const { return _vhost; } -const string& MediaSource::getApp() const { - //获取该源的id +const string &MediaSource::getApp() const { + // 获取该源的id return _app; } -const string& MediaSource::getId() const { +const string &MediaSource::getId() const { return _stream_id; } std::shared_ptr MediaSource::getOwnership() { if (_owned.test_and_set()) { - //已经被所有 + // 已经被所有 return nullptr; } weak_ptr weak_self = shared_from_this(); - //确保返回的Ownership智能指针不为空,0x01无实际意义 - return std::shared_ptr((void *) 0x01, [weak_self](void *ptr) { + // 确保返回的Ownership智能指针不为空,0x01无实际意义 + return std::shared_ptr((void *)0x01, [weak_self](void *ptr) { auto strong_self = weak_self.lock(); if (strong_self) { strong_self->_owned.clear(); @@ -168,49 +170,49 @@ std::shared_ptr MediaSource::getOwnership() { }); } -int MediaSource::getBytesSpeed(TrackType type){ - if(type == TrackInvalid || type == TrackMax){ +int MediaSource::getBytesSpeed(TrackType type) { + if (type == TrackInvalid || type == TrackMax) { return _speed[TrackVideo].getSpeed() + _speed[TrackAudio].getSpeed(); } return _speed[type].getSpeed(); } uint64_t MediaSource::getAliveSecond() const { - //使用Ticker对象获取存活时间的目的是防止修改系统时间导致回退 + // 使用Ticker对象获取存活时间的目的是防止修改系统时间导致回退 return _ticker.createdTime() / 1000; } vector MediaSource::getTracks(bool ready) const { auto listener = _listener.lock(); - if(!listener){ + if (!listener) { return vector(); } return listener->getMediaTracks(const_cast(*this), ready); } -void MediaSource::setListener(const std::weak_ptr &listener){ +void MediaSource::setListener(const std::weak_ptr &listener) { _listener = listener; } -std::weak_ptr MediaSource::getListener(bool next) const{ +std::weak_ptr MediaSource::getListener(bool next) const { if (!next) { return _listener; } auto listener = dynamic_pointer_cast(_listener.lock()); if (!listener) { - //不是MediaSourceEventInterceptor对象或者对象已经销毁 + // 不是MediaSourceEventInterceptor对象或者对象已经销毁 return _listener; } - //获取被拦截的对象 + // 获取被拦截的对象 auto next_obj = listener->getDelegate(); - //有则返回之 + // 有则返回之 return next_obj ? next_obj : _listener; } -int MediaSource::totalReaderCount(){ +int MediaSource::totalReaderCount() { auto listener = _listener.lock(); - if(!listener){ + if (!listener) { return readerCount(); } return listener->totalReaderCount(*this); @@ -246,7 +248,7 @@ std::shared_ptr MediaSource::getOriginSock() const { bool MediaSource::seekTo(uint32_t stamp) { auto listener = _listener.lock(); - if(!listener){ + if (!listener) { return false; } return listener->seekTo(*this, stamp); @@ -274,7 +276,7 @@ bool MediaSource::close(bool force) { return false; } if (!force && totalReaderCount()) { - //有人观看,不强制关闭 + // 有人观看,不强制关闭 return false; } return listener->close(*this); @@ -319,7 +321,7 @@ void MediaSource::onReaderChanged(int size) { } } -bool MediaSource::setupRecord(Recorder::type type, bool start, const string &custom_path, size_t max_second){ +bool MediaSource::setupRecord(Recorder::type type, bool start, const string &custom_path, size_t max_second) { auto listener = _listener.lock(); if (!listener) { WarnL << "未设置MediaSource的事件监听者,setupRecord失败:" << getUrl(); @@ -328,15 +330,16 @@ bool MediaSource::setupRecord(Recorder::type type, bool start, const string &cus return listener->setupRecord(*this, type, start, custom_path, max_second); } -bool MediaSource::isRecording(Recorder::type type){ +bool MediaSource::isRecording(Recorder::type type) { auto listener = _listener.lock(); - if(!listener){ + if (!listener) { return false; } return listener->isRecording(*this, type); } -void MediaSource::startSendRtp(const MediaSourceEvent::SendRtpArgs &args, const std::function cb) { +void MediaSource::startSendRtp( + const MediaSourceEvent::SendRtpArgs &args, const std::function cb) { auto listener = _listener.lock(); if (!listener) { cb(0, SockException(Err_other, "尚未设置事件监听器")); @@ -353,7 +356,7 @@ bool MediaSource::stopSendRtp(const string &ssrc) { return listener->stopSendRtp(*this, ssrc); } -template +template static void for_each_media_l(const MAP &map, LIST &list, const First &first, const KeyTypes &...keys) { if (first.empty()) { for (auto &pr : map) { @@ -367,7 +370,7 @@ static void for_each_media_l(const MAP &map, LIST &list, const First &first, con } } -template +template static void emplace_back(LIST &list, const Ptr &ptr) { auto src = ptr.lock(); if (src) { @@ -375,7 +378,7 @@ static void emplace_back(LIST &list, const Ptr &ptr) { } } -template +template static void for_each_media_l(const MAP &map, LIST &list, const First &first) { if (first.empty()) { for (auto &pr : map) { @@ -389,11 +392,9 @@ static void for_each_media_l(const MAP &map, LIST &list, const First &first) { } } -void MediaSource::for_each_media(const function &cb, - const string &schema, - const string &vhost, - const string &app, - const string &stream) { +void MediaSource::for_each_media( + const function &cb, const string &schema, const string &vhost, const string &app, + const string &stream) { deque src_list; { lock_guard lock(s_media_source_mtx); @@ -404,31 +405,35 @@ void MediaSource::for_each_media(const function &cb, } } -static MediaSource::Ptr find_l(const string &schema, const string &vhost_in, const string &app, const string &id, bool from_mp4) { +static MediaSource::Ptr +find_l(const string &schema, const string &vhost_in, const string &app, const string &id, bool from_mp4) { string vhost = vhost_in; GET_CONFIG(bool, enableVhost, General::kEnableVhost); - if(vhost.empty() || !enableVhost){ + if (vhost.empty() || !enableVhost) { vhost = DEFAULT_VHOST; } if (app.empty() || id.empty()) { - //如果未指定app与stream id,那么就是遍历而非查找,所以应该返回查找失败 + // 如果未指定app与stream id,那么就是遍历而非查找,所以应该返回查找失败 return nullptr; } MediaSource::Ptr ret; - MediaSource::for_each_media([&](const MediaSource::Ptr &src) { ret = std::move(const_cast(src)); }, schema, vhost, app, id); + MediaSource::for_each_media( + [&](const MediaSource::Ptr &src) { ret = std::move(const_cast(src)); }, schema, vhost, app, + id); - if(!ret && from_mp4 && schema != HLS_SCHEMA){ - //未找到媒体源,则读取mp4创建一个 - //播放hls不触发mp4点播(因为HLS也可以用于录像,不是纯粹的直播) + if (!ret && from_mp4 && schema != HLS_SCHEMA) { + // 未找到媒体源,则读取mp4创建一个 + // 播放hls不触发mp4点播(因为HLS也可以用于录像,不是纯粹的直播) ret = MediaSource::createFromMP4(schema, vhost, app, id); } return ret; } -static void findAsync_l(const MediaInfo &info, const std::shared_ptr &session, bool retry, - const function &cb){ +static void findAsync_l( + const MediaInfo &info, const std::shared_ptr &session, bool retry, + const function &cb) { auto src = find_l(info._schema, info._vhost, info._app, info._streamid, true); if (src || !retry) { cb(src); @@ -438,10 +443,10 @@ static void findAsync_l(const MediaInfo &info, const std::shared_ptr &s GET_CONFIG(int, maxWaitMS, General::kMaxStreamWaitTimeMS); void *listener_tag = session.get(); auto poller = session->getPoller(); - std::shared_ptr invoked(new atomic_flag{false}); + std::shared_ptr invoked(new atomic_flag { false }); auto cb_once = [cb, invoked](const MediaSource::Ptr &src) { if (invoked->test_and_set()) { - //回调已经执行过了 + // 回调已经执行过了 return; } cb(src); @@ -455,53 +460,55 @@ static void findAsync_l(const MediaInfo &info, const std::shared_ptr &s }); auto cancel_all = [on_timeout, listener_tag]() { - //取消延时任务,防止多次回调 + // 取消延时任务,防止多次回调 on_timeout->cancel(); - //取消媒体注册事件监听 + // 取消媒体注册事件监听 NoticeCenter::Instance().delListener(listener_tag, Broadcast::kBroadcastMediaChanged); }; weak_ptr weak_session = session; auto on_register = [weak_session, info, cb_once, cancel_all, poller](BroadcastMediaChangedArgs) { - if (!bRegist || - sender.getSchema() != info._schema || - sender.getVhost() != info._vhost || - sender.getApp() != info._app || - sender.getId() != info._streamid) { - //不是自己感兴趣的事件,忽略之 + if (!bRegist || sender.getSchema() != info._schema || sender.getVhost() != info._vhost + || sender.getApp() != info._app || sender.getId() != info._streamid) { + // 不是自己感兴趣的事件,忽略之 return; } - poller->async([weak_session, cancel_all, info, cb_once]() { - cancel_all(); - if (auto strong_session = weak_session.lock()) { - //播发器请求的流终于注册上了,切换到自己的线程再回复 - DebugL << "收到媒体注册事件,回复播放器:" << info.getUrl(); - //再找一遍媒体源,一般能找到 - findAsync_l(info, strong_session, false, cb_once); - } - }, false); + poller->async( + [weak_session, cancel_all, info, cb_once]() { + cancel_all(); + if (auto strong_session = weak_session.lock()) { + // 播发器请求的流终于注册上了,切换到自己的线程再回复 + DebugL << "收到媒体注册事件,回复播放器:" << info.getUrl(); + // 再找一遍媒体源,一般能找到 + findAsync_l(info, strong_session, false, cb_once); + } + }, + false); }; - //监听媒体注册事件 + // 监听媒体注册事件 NoticeCenter::Instance().addListener(listener_tag, Broadcast::kBroadcastMediaChanged, on_register); function close_player = [cb_once, cancel_all, poller]() { poller->async([cancel_all, cb_once]() { cancel_all(); - //告诉播放器,流不存在,这样会立即断开播放器 + // 告诉播放器,流不存在,这样会立即断开播放器 cb_once(nullptr); }); }; - //广播未找到流,此时可以立即去拉流,这样还来得及 - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastNotFoundStream, info, static_cast(*session), close_player); + // 广播未找到流,此时可以立即去拉流,这样还来得及 + NoticeCenter::Instance().emitEvent( + Broadcast::kBroadcastNotFoundStream, info, static_cast(*session), close_player); } -void MediaSource::findAsync(const MediaInfo &info, const std::shared_ptr &session, const function &cb) { +void MediaSource::findAsync( + const MediaInfo &info, const std::shared_ptr &session, const function &cb) { return findAsync_l(info, session, true, cb); } -MediaSource::Ptr MediaSource::find(const string &schema, const string &vhost, const string &app, const string &id, bool from_mp4) { +MediaSource::Ptr +MediaSource::find(const string &schema, const string &vhost, const string &app, const string &id, bool from_mp4) { return find_l(schema, vhost, app, id, from_mp4); } @@ -517,20 +524,20 @@ MediaSource::Ptr MediaSource::find(const string &vhost, const string &app, const return MediaSource::find(HLS_SCHEMA, vhost, app, stream_id, from_mp4); } -void MediaSource::emitEvent(bool regist){ +void MediaSource::emitEvent(bool regist) { auto listener = _listener.lock(); if (listener) { - //触发回调 + // 触发回调 listener->onRegist(*this, regist); } - //触发广播 + // 触发广播 NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged, regist, *this); InfoL << (regist ? "媒体注册:" : "媒体注销:") << getUrl(); } void MediaSource::regist() { { - //减小互斥锁临界区 + // 减小互斥锁临界区 lock_guard lock(s_media_source_mtx); auto &ref = s_media_source_map[_schema][_vhost][_app][_stream_id]; auto src = ref.lock(); @@ -538,7 +545,7 @@ void MediaSource::regist() { if (src.get() == this) { return; } - //增加判断, 防止当前流已注册时再次注册 + // 增加判断, 防止当前流已注册时再次注册 throw std::invalid_argument("media source already existed:" + getUrl()); } ref = shared_from_this(); @@ -546,8 +553,9 @@ void MediaSource::regist() { emitEvent(true); } -template -static bool erase_media_source(bool &hit, const MediaSource *thiz, MAP &map, const First &first, const KeyTypes &...keys) { +template +static bool +erase_media_source(bool &hit, const MediaSource *thiz, MAP &map, const First &first, const KeyTypes &...keys) { auto it = map.find(first); if (it != map.end() && erase_media_source(hit, thiz, it->second, keys...)) { map.erase(it); @@ -555,13 +563,13 @@ static bool erase_media_source(bool &hit, const MediaSource *thiz, MAP &map, con return map.empty(); } -template +template static bool erase_media_source(bool &hit, const MediaSource *thiz, MAP &map, const First &first) { auto it = map.find(first); if (it != map.end()) { auto src = it->second.lock(); if (!src || src.get() == thiz) { - //对象已经销毁或者对象就是自己,那么移除之 + // 对象已经销毁或者对象就是自己,那么移除之 map.erase(it); hit = true; } @@ -569,11 +577,11 @@ static bool erase_media_source(bool &hit, const MediaSource *thiz, MAP &map, con return map.empty(); } -//反注册该源 +// 反注册该源 bool MediaSource::unregist() { bool ret = false; { - //减小互斥锁临界区 + // 减小互斥锁临界区 lock_guard lock(s_media_source_mtx); erase_media_source(ret, this, s_media_source_map, _schema, _vhost, _app, _stream_id); } @@ -586,7 +594,7 @@ bool MediaSource::unregist() { /////////////////////////////////////MediaInfo////////////////////////////////////// -void MediaInfo::parse(const std::string &url_in){ +void MediaInfo::parse(const std::string &url_in) { _full_url = url_in; auto url = url_in; auto pos = url.find("?"); @@ -605,8 +613,8 @@ void MediaInfo::parse(const std::string &url_in){ if (split_vec.size() > 0) { splitUrl(split_vec[0], _host, _port); _vhost = _host; - if (_vhost == "localhost" || isIP(_vhost.data())) { - //如果访问的是localhost或ip,那么则为默认虚拟主机 + if (_vhost == "localhost" || isIP(_vhost.data())) { + // 如果访问的是localhost或ip,那么则为默认虚拟主机 _vhost = DEFAULT_VHOST; } } @@ -631,12 +639,14 @@ void MediaInfo::parse(const std::string &url_in){ GET_CONFIG(bool, enableVhost, General::kEnableVhost); if (!enableVhost || _vhost.empty()) { - //如果关闭虚拟主机或者虚拟主机为空,则设置虚拟主机为默认 + // 如果关闭虚拟主机或者虚拟主机为空,则设置虚拟主机为默认 _vhost = DEFAULT_VHOST; } } -MediaSource::Ptr MediaSource::createFromMP4(const string &schema, const string &vhost, const string &app, const string &stream, const string &file_path , bool check_app){ +MediaSource::Ptr MediaSource::createFromMP4( + const string &schema, const string &vhost, const string &app, const string &stream, const string &file_path, + bool check_app) { GET_CONFIG(string, appName, Record::kAppName); if (check_app && app != appName) { return nullptr; @@ -653,21 +663,21 @@ MediaSource::Ptr MediaSource::createFromMP4(const string &schema, const string & #else WarnL << "创建MP4点播失败,请编译时打开\"ENABLE_MP4\"选项"; return nullptr; -#endif //ENABLE_MP4 +#endif // ENABLE_MP4 } /////////////////////////////////////MediaSourceEvent////////////////////////////////////// -void MediaSourceEvent::onReaderChanged(MediaSource &sender, int size){ +void MediaSourceEvent::onReaderChanged(MediaSource &sender, int size) { // 说明是第一次播放 或者是无人播放事件 - if(this->_last_played_count.load() == 0 && size){ + if (this->_last_played_count.load() == 0 && totalReaderCount(sender)) { InfoL << "first played, app=" << sender.getApp() << ", stream_id=" << sender.getId(); // 更新播放次数 _last_played_count.store(size); _first_played.store(toolkit::getCurrentMillisecond(true)); AuthCenter::instance().media_changed(sender.getApp(), sender.getId(), size, _first_played.load()); // 这里我们记录从播放到无人播放的情况 - }else if (size == 0 && this->_last_played_count.load() != 0) { + } else if (totalReaderCount(sender) == 0 && this->_last_played_count.load() != 0) { // 说明是无人播放 InfoL << "no one played, app=" << sender.getApp() << ", stream_id=" << sender.getId(); this->_last_played_count.store(size); @@ -675,39 +685,42 @@ void MediaSourceEvent::onReaderChanged(MediaSource &sender, int size){ } if (size || sender.totalReaderCount()) { - //还有人观看该视频,不触发关闭事件 + // 还有人观看该视频,不触发关闭事件 _async_close_timer = nullptr; return; } - //没有任何人观看该视频源,表明该源可以关闭了 + // 没有任何人观看该视频源,表明该源可以关闭了 GET_CONFIG(string, record_app, Record::kAppName); GET_CONFIG(int, stream_none_reader_delay, General::kStreamNoneReaderDelayMS); - //如果mp4点播, 无人观看时我们强制关闭点播 + // 如果mp4点播, 无人观看时我们强制关闭点播 bool is_mp4_vod = sender.getApp() == record_app; weak_ptr weak_sender = sender.shared_from_this(); - _async_close_timer = std::make_shared(stream_none_reader_delay / 1000.0f, [weak_sender, is_mp4_vod]() { - auto strong_sender = weak_sender.lock(); - if (!strong_sender) { - //对象已经销毁 - return false; - } + _async_close_timer = std::make_shared( + stream_none_reader_delay / 1000.0f, + [weak_sender, is_mp4_vod]() { + auto strong_sender = weak_sender.lock(); + if (!strong_sender) { + // 对象已经销毁 + return false; + } - if (strong_sender->totalReaderCount()) { - //还有人观看该视频,不触发关闭事件 - return false; - } + if (strong_sender->totalReaderCount()) { + // 还有人观看该视频,不触发关闭事件 + return false; + } - if (!is_mp4_vod) { - //直播时触发无人观看事件,让开发者自行选择是否关闭 - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastStreamNoneReader, *strong_sender); - } else { - //这个是mp4点播,我们自动关闭 - WarnL << "MP4点播无人观看,自动关闭:" << strong_sender->getUrl(); - strong_sender->close(false); - } - return false; - }, nullptr); + if (!is_mp4_vod) { + // 直播时触发无人观看事件,让开发者自行选择是否关闭 + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastStreamNoneReader, *strong_sender); + } else { + // 这个是mp4点播,我们自动关闭 + WarnL << "MP4点播无人观看,自动关闭:" << strong_sender->getUrl(); + strong_sender->close(false); + } + return false; + }, + nullptr); } string MediaSourceEvent::getOriginUrl(MediaSource &sender) const { @@ -798,12 +811,12 @@ void MediaSourceEventInterceptor::onRegist(MediaSource &sender, bool regist) { } } -float MediaSourceEventInterceptor::getLossRate(MediaSource &sender, TrackType type){ +float MediaSourceEventInterceptor::getLossRate(MediaSource &sender, TrackType type) { auto listener = _listener.lock(); if (listener) { return listener->getLossRate(sender, type); } - return -1; //异常返回-1 + return -1; // 异常返回-1 } toolkit::EventPoller::Ptr MediaSourceEventInterceptor::getOwnerPoller(MediaSource &sender) { @@ -814,7 +827,8 @@ toolkit::EventPoller::Ptr MediaSourceEventInterceptor::getOwnerPoller(MediaSourc throw std::runtime_error(toolkit::demangle(typeid(*this).name()) + "::getOwnerPoller failed"); } -bool MediaSourceEventInterceptor::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second) { +bool MediaSourceEventInterceptor::setupRecord( + MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second) { auto listener = _listener.lock(); if (!listener) { return false; @@ -838,7 +852,9 @@ vector MediaSourceEventInterceptor::getMediaTracks(MediaSource &send return listener->getMediaTracks(sender, trackReady); } -void MediaSourceEventInterceptor::startSendRtp(MediaSource &sender, const MediaSourceEvent::SendRtpArgs &args, const std::function cb) { +void MediaSourceEventInterceptor::startSendRtp( + MediaSource &sender, const MediaSourceEvent::SendRtpArgs &args, + const std::function cb) { auto listener = _listener.lock(); if (listener) { listener->startSendRtp(sender, args, cb); @@ -847,7 +863,7 @@ void MediaSourceEventInterceptor::startSendRtp(MediaSource &sender, const MediaS } } -bool MediaSourceEventInterceptor::stopSendRtp(MediaSource &sender, const string &ssrc){ +bool MediaSourceEventInterceptor::stopSendRtp(MediaSource &sender, const string &ssrc) { auto listener = _listener.lock(); if (listener) { return listener->stopSendRtp(sender, ssrc); @@ -862,7 +878,7 @@ void MediaSourceEventInterceptor::setDelegate(const std::weak_ptr MediaSourceEventInterceptor::getDelegate() const{ +std::shared_ptr MediaSourceEventInterceptor::getDelegate() const { return _listener.lock(); } @@ -870,39 +886,39 @@ std::shared_ptr MediaSourceEventInterceptor::getDelegate() con static bool isFlushAble_default(bool is_video, uint64_t last_stamp, uint64_t new_stamp, size_t cache_size) { if (new_stamp + 500 < last_stamp) { - //时间戳回退比较大(可能seek中),由于rtp中时间戳是pts,是可能存在一定程度的回退的 + // 时间戳回退比较大(可能seek中),由于rtp中时间戳是pts,是可能存在一定程度的回退的 return true; } - //时间戳发送变化或者缓存超过1024个,sendmsg接口一般最多只能发送1024个数据包 + // 时间戳发送变化或者缓存超过1024个,sendmsg接口一般最多只能发送1024个数据包 return last_stamp != new_stamp || cache_size >= 1024; } static bool isFlushAble_merge(bool is_video, uint64_t last_stamp, uint64_t new_stamp, size_t cache_size, int merge_ms) { if (new_stamp + 500 < last_stamp) { - //时间戳回退比较大(可能seek中),由于rtp中时间戳是pts,是可能存在一定程度的回退的 + // 时间戳回退比较大(可能seek中),由于rtp中时间戳是pts,是可能存在一定程度的回退的 return true; } if (new_stamp > last_stamp + merge_ms) { - //时间戳增量超过合并写阈值 + // 时间戳增量超过合并写阈值 return true; } - //缓存数超过1024个,这个逻辑用于避免时间戳异常的流导致的内存暴增问题 - //而且sendmsg接口一般最多只能发送1024个数据包 + // 缓存数超过1024个,这个逻辑用于避免时间戳异常的流导致的内存暴增问题 + // 而且sendmsg接口一般最多只能发送1024个数据包 return cache_size >= 1024; } bool FlushPolicy::isFlushAble(bool is_video, bool is_key, uint64_t new_stamp, size_t cache_size) { bool flush_flag = false; if (is_key && is_video) { - //遇到关键帧flush掉前面的数据,确保关键帧为该组数据的第一帧,确保GOP缓存有效 + // 遇到关键帧flush掉前面的数据,确保关键帧为该组数据的第一帧,确保GOP缓存有效 flush_flag = true; } else { GET_CONFIG(int, mergeWriteMS, General::kMergeWriteMS); if (mergeWriteMS <= 0) { - //关闭了合并写或者合并写阈值小于等于0 + // 关闭了合并写或者合并写阈值小于等于0 flush_flag = isFlushAble_default(is_video, _last_stamp[is_video], new_stamp, cache_size); } else { flush_flag = isFlushAble_merge(is_video, _last_stamp[is_video], new_stamp, cache_size, mergeWriteMS);