修复addFFmpegSource添加的流事件拦截可能失效问题 (#2642 #2629)

此pr主要为了修复 #2629,通过新增getMuxer接口,
可以直接获取到所有协议共享的MultiMediaSourceMuxer对象,
在此对象完成事件拦截,防止某种协议事件丢失。
同时调整了下FFmpegSource.cpp代码格式。
This commit is contained in:
夏楚 2023-07-08 21:35:09 +08:00 committed by GitHub
parent fad8dd74e7
commit e52c1cc510
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 115 additions and 106 deletions

View File

@ -11,6 +11,7 @@
#include "FFmpegSource.h" #include "FFmpegSource.h"
#include "Common/config.h" #include "Common/config.h"
#include "Common/MediaSource.h" #include "Common/MediaSource.h"
#include "Common/MultiMediaSourceMuxer.h"
#include "Util/File.h" #include "Util/File.h"
#include "System.h" #include "System.h"
#include "Thread/WorkThreadPool.h" #include "Thread/WorkThreadPool.h"
@ -154,10 +155,7 @@ void FFmpegSource::play(const string &ffmpeg_cmd_key, const string &src_url,cons
} }
void FFmpegSource::findAsync(int maxWaitMS, const function<void(const MediaSource::Ptr &src)> &cb) { void FFmpegSource::findAsync(int maxWaitMS, const function<void(const MediaSource::Ptr &src)> &cb) {
auto src = MediaSource::find(_media_info.schema, auto src = MediaSource::find(_media_info.schema, _media_info.vhost, _media_info.app, _media_info.stream);
_media_info.vhost,
_media_info.app,
_media_info.stream);
if (src || !maxWaitMS) { if (src || !maxWaitMS) {
cb(src); cb(src);
return; return;
@ -182,8 +180,7 @@ void FFmpegSource::findAsync(int maxWaitMS, const function<void(const MediaSourc
return; return;
} }
if (!bRegist || if (!bRegist || sender.getSchema() != strongSelf->_media_info.schema ||
sender.getSchema() != strongSelf->_media_info.schema ||
!equalMediaTuple(sender.getMediaTuple(), strongSelf->_media_info)) { !equalMediaTuple(sender.getMediaTuple(), strongSelf->_media_info)) {
// 不是自己感兴趣的事件,忽略之 // 不是自己感兴趣的事件,忽略之
return; return;
@ -196,12 +193,10 @@ void FFmpegSource::findAsync(int maxWaitMS, const function<void(const MediaSourc
// 切换到自己的线程再回复 // 切换到自己的线程再回复
strongSelf->_poller->async([weakSelf, cb]() { strongSelf->_poller->async([weakSelf, cb]() {
auto strongSelf = weakSelf.lock(); if (auto strongSelf = weakSelf.lock()) {
if(!strongSelf) {
return;
}
// 再找一遍媒体源,一般能找到 // 再找一遍媒体源,一般能找到
strongSelf->findAsync(0, cb); strongSelf->findAsync(0, cb);
}
}, false); }, false);
}; };
// 监听媒体注册事件 // 监听媒体注册事件
@ -298,16 +293,13 @@ string FFmpegSource::getOriginUrl(MediaSource &sender) const{
return _src_url; return _src_url;
} }
std::shared_ptr<SockInfo> FFmpegSource::getOriginSock(MediaSource &sender) const {
return nullptr;
}
void FFmpegSource::onGetMediaSource(const MediaSource::Ptr &src) { void FFmpegSource::onGetMediaSource(const MediaSource::Ptr &src) {
auto listener = src->getListener(true); auto muxer = src->getMuxer();
if (listener.lock().get() != this) { auto listener = muxer ? muxer->getDelegate() : nullptr;
if (listener && listener.get() != this) {
//防止多次进入onGetMediaSource函数导致无限递归调用的bug //防止多次进入onGetMediaSource函数导致无限递归调用的bug
setDelegate(listener); setDelegate(listener);
src->setListener(shared_from_this()); muxer->setDelegate(shared_from_this());
if (_enable_hls) { if (_enable_hls) {
src->setupRecord(Recorder::type_hls, true, "", 0); src->setupRecord(Recorder::type_hls, true, "", 0);
} }
@ -355,4 +347,3 @@ void FFmpegSnap::makeSnap(const string &play_url, const string &save_path, float
cb(success, (!success && !log_file.empty()) ? File::loadFile(log_file.data()) : ""); cb(success, (!success && !log_file.empty()) ? File::loadFile(log_file.data()) : "");
}); });
} }

View File

@ -79,8 +79,6 @@ private:
mediakit::MediaOriginType getOriginType(mediakit::MediaSource &sender) const override; mediakit::MediaOriginType getOriginType(mediakit::MediaSource &sender) const override;
//获取媒体源url或者文件路径 //获取媒体源url或者文件路径
std::string getOriginUrl(mediakit::MediaSource &sender) const override; std::string getOriginUrl(mediakit::MediaSource &sender) const override;
// 获取媒体源客户端相关信息
std::shared_ptr<toolkit::SockInfo> getOriginSock(mediakit::MediaSource &sender) const override;
private: private:
bool _enable_hls = false; bool _enable_hls = false;

View File

@ -172,22 +172,10 @@ void MediaSource::setListener(const std::weak_ptr<MediaSourceEvent> &listener){
_listener = listener; _listener = listener;
} }
std::weak_ptr<MediaSourceEvent> MediaSource::getListener(bool next) const{ std::weak_ptr<MediaSourceEvent> MediaSource::getListener() const {
if (!next) {
return _listener; return _listener;
} }
auto listener = dynamic_pointer_cast<MediaSourceEventInterceptor>(_listener.lock());
if (!listener) {
//不是MediaSourceEventInterceptor对象或者对象已经销毁
return _listener;
}
//获取被拦截的对象
auto next_obj = listener->getDelegate();
//有则返回之
return next_obj ? next_obj : _listener;
}
int MediaSource::totalReaderCount(){ int MediaSource::totalReaderCount(){
auto listener = _listener.lock(); auto listener = _listener.lock();
if(!listener){ if(!listener){
@ -277,6 +265,11 @@ toolkit::EventPoller::Ptr MediaSource::getOwnerPoller() {
throw std::runtime_error(toolkit::demangle(typeid(*this).name()) + "::getOwnerPoller failed: " + getUrl()); throw std::runtime_error(toolkit::demangle(typeid(*this).name()) + "::getOwnerPoller failed: " + getUrl());
} }
std::shared_ptr<MultiMediaSourceMuxer> MediaSource::getMuxer() {
auto listener = _listener.lock();
return listener ? listener->getMuxer(*this) : nullptr;
}
void MediaSource::onReaderChanged(int size) { void MediaSource::onReaderChanged(int size) {
try { try {
weak_ptr<MediaSource> weak_self = shared_from_this(); weak_ptr<MediaSource> weak_self = shared_from_this();
@ -780,6 +773,11 @@ toolkit::EventPoller::Ptr MediaSourceEventInterceptor::getOwnerPoller(MediaSourc
throw std::runtime_error(toolkit::demangle(typeid(*this).name()) + "::getOwnerPoller failed"); throw std::runtime_error(toolkit::demangle(typeid(*this).name()) + "::getOwnerPoller failed");
} }
std::shared_ptr<MultiMediaSourceMuxer> MediaSourceEventInterceptor::getMuxer(MediaSource &sender) {
auto listener = _listener.lock();
return listener ? listener->getMuxer(sender) : nullptr;
}
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(); auto listener = _listener.lock();
if (!listener) { if (!listener) {

View File

@ -41,6 +41,7 @@ enum class MediaOriginType : uint8_t {
std::string getOriginTypeString(MediaOriginType type); std::string getOriginTypeString(MediaOriginType type);
class MediaSource; class MediaSource;
class MultiMediaSourceMuxer;
class MediaSourceEvent { class MediaSourceEvent {
public: public:
friend class MediaSource; friend class MediaSource;
@ -88,6 +89,8 @@ public:
virtual bool isRecording(MediaSource &sender, Recorder::type type) { return false; } virtual bool isRecording(MediaSource &sender, Recorder::type type) { return false; }
// 获取所有track相关信息 // 获取所有track相关信息
virtual std::vector<Track::Ptr> getMediaTracks(MediaSource &sender, bool trackReady = true) const { return std::vector<Track::Ptr>(); }; virtual std::vector<Track::Ptr> getMediaTracks(MediaSource &sender, bool trackReady = true) const { return std::vector<Track::Ptr>(); };
// 获取MultiMediaSourceMuxer对象
virtual std::shared_ptr<MultiMediaSourceMuxer> getMuxer(MediaSource &sender) { return nullptr; }
class SendRtpArgs { class SendRtpArgs {
public: public:
@ -257,6 +260,7 @@ public:
bool stopSendRtp(MediaSource &sender, const std::string &ssrc) override; bool stopSendRtp(MediaSource &sender, const std::string &ssrc) override;
float getLossRate(MediaSource &sender, TrackType type) override; float getLossRate(MediaSource &sender, TrackType type) override;
toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override; toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override;
std::shared_ptr<MultiMediaSourceMuxer> getMuxer(MediaSource &sender) override;
private: private:
std::weak_ptr<MediaSourceEvent> _listener; std::weak_ptr<MediaSourceEvent> _listener;
@ -330,7 +334,7 @@ public:
// 设置监听者 // 设置监听者
virtual void setListener(const std::weak_ptr<MediaSourceEvent> &listener); virtual void setListener(const std::weak_ptr<MediaSourceEvent> &listener);
// 获取监听者 // 获取监听者
std::weak_ptr<MediaSourceEvent> getListener(bool next = false) const; std::weak_ptr<MediaSourceEvent> getListener() const;
// 本协议获取观看者个数,可能返回本协议的观看人数,也可能返回总人数 // 本协议获取观看者个数,可能返回本协议的观看人数,也可能返回总人数
virtual int readerCount() = 0; virtual int readerCount() = 0;
@ -372,6 +376,8 @@ public:
float getLossRate(mediakit::TrackType type); float getLossRate(mediakit::TrackType type);
// 获取所在线程 // 获取所在线程
toolkit::EventPoller::Ptr getOwnerPoller(); toolkit::EventPoller::Ptr getOwnerPoller();
// 获取MultiMediaSourceMuxer对象
std::shared_ptr<MultiMediaSourceMuxer> getMuxer();
////////////////static方法查找或生成MediaSource//////////////// ////////////////static方法查找或生成MediaSource////////////////

View File

@ -71,6 +71,14 @@ static string getTrackInfoStr(const TrackSource *track_src){
return std::move(codec_info); return std::move(codec_info);
} }
const ProtocolOption &MultiMediaSourceMuxer::getOption() const {
return _option;
}
const MediaTuple &MultiMediaSourceMuxer::getMediaTuple() const {
return _tuple;
}
std::string MultiMediaSourceMuxer::shortUrl() const { std::string MultiMediaSourceMuxer::shortUrl() const {
auto ret = getOriginUrl(MediaSource::NullMediaSource()); auto ret = getOriginUrl(MediaSource::NullMediaSource());
if (!ret.empty()) { if (!ret.empty()) {
@ -361,6 +369,10 @@ EventPoller::Ptr MultiMediaSourceMuxer::getOwnerPoller(MediaSource &sender) {
} }
} }
std::shared_ptr<MultiMediaSourceMuxer> MultiMediaSourceMuxer::getMuxer(MediaSource &sender) {
return shared_from_this();
}
bool MultiMediaSourceMuxer::onTrackReady(const Track::Ptr &track) { bool MultiMediaSourceMuxer::onTrackReady(const Track::Ptr &track) {
bool ret = false; bool ret = false;
if (_rtmp) { if (_rtmp) {

View File

@ -126,9 +126,13 @@ public:
*/ */
toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override; toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override;
const MediaTuple& getMediaTuple() const { /**
return _tuple; *
} */
std::shared_ptr<MultiMediaSourceMuxer> getMuxer(MediaSource &sender) override;
const ProtocolOption &getOption() const;
const MediaTuple &getMediaTuple() const;
std::string shortUrl() const; std::string shortUrl() const;
protected: protected: