From 3c858a835175f6d7f1c66b62a6cc0db2ed511156 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 2 Jul 2020 18:14:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AA=92=E4=BD=93=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E5=9B=9E=E8=B0=83=E4=BA=8B=E4=BB=B6:#373?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/include/mk_media.h | 17 +++++++++++++++++ api/source/mk_media.cpp | 21 +++++++++++++++++++++ server/FFmpegSource.cpp | 7 +++++++ server/FFmpegSource.h | 1 + src/Common/MediaSource.cpp | 12 +++++++++++- src/Common/MediaSource.h | 2 ++ src/Common/MultiMediaSourceMuxer.cpp | 7 +++++++ src/Common/MultiMediaSourceMuxer.h | 7 +++++++ 8 files changed, 73 insertions(+), 1 deletion(-) diff --git a/api/include/mk_media.h b/api/include/mk_media.h index 3337b59f..87b8836d 100755 --- a/api/include/mk_media.h +++ b/api/include/mk_media.h @@ -12,6 +12,7 @@ #define MK_MEDIA_H_ #include "mk_common.h" +#include "mk_events_objects.h" #ifdef __cplusplus extern "C" { @@ -158,6 +159,22 @@ API_EXPORT void API_CALL mk_media_set_on_seek(mk_media ctx, on_mk_media_seek cb, */ API_EXPORT int API_CALL mk_media_total_reader_count(mk_media ctx); +/** + * 生成的MediaSource注册或注销事件 + * @param user_data 设置回调时的用户数据指针 + * @param sender 生成的MediaSource对象 + * @param regist 1为注册事件,0为注销事件 + */ +typedef void(API_CALL *on_mk_media_source_regist)(void *user_data, mk_media_source sender, int regist); + +/** + * 设置MediaSource注册或注销事件回调函数 + * @param ctx 对象指针 + * @param cb 回调指针 + * @param user_data 用户数据指针 + */ +API_EXPORT void API_CALL mk_media_set_on_regist(mk_media ctx, on_mk_media_source_regist cb, void *user_data); + #ifdef __cplusplus } #endif diff --git a/api/source/mk_media.cpp b/api/source/mk_media.cpp index 91761829..bad5bf51 100755 --- a/api/source/mk_media.cpp +++ b/api/source/mk_media.cpp @@ -42,6 +42,12 @@ public: _on_seek = cb; _on_seek_data = user_data; } + + void setOnRegist(on_mk_media_source_regist cb, void *user_data){ + _on_regist = cb; + _on_regist_data = user_data; + } + protected: // 通知其停止推流 bool close(MediaSource &sender,bool force) override{ @@ -70,12 +76,21 @@ protected: int totalReaderCount(MediaSource &sender) override{ return _channel->totalReaderCount(); } + + void onRegist(MediaSource &sender, bool regist) override{ + if (_on_regist) { + _on_regist(_on_regist_data, &sender, regist); + } + } + private: DevChannel::Ptr _channel; on_mk_media_close _on_close = nullptr; on_mk_media_seek _on_seek = nullptr; + on_mk_media_source_regist _on_regist = nullptr; void *_on_seek_data; void *_on_close_data; + void *_on_regist_data; }; API_EXPORT void API_CALL mk_media_set_on_close(mk_media ctx, on_mk_media_close cb, void *user_data){ @@ -90,6 +105,12 @@ API_EXPORT void API_CALL mk_media_set_on_seek(mk_media ctx, on_mk_media_seek cb, (*obj)->setOnSeek(cb, user_data); } +API_EXPORT void API_CALL mk_media_set_on_regist(mk_media ctx, on_mk_media_source_regist cb, void *user_data){ + assert(ctx); + MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; + (*obj)->setOnRegist(cb, user_data); +} + API_EXPORT int API_CALL mk_media_total_reader_count(mk_media ctx){ assert(ctx); MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; diff --git a/server/FFmpegSource.cpp b/server/FFmpegSource.cpp index b6a7ef7e..fdf9b7f2 100644 --- a/server/FFmpegSource.cpp +++ b/server/FFmpegSource.cpp @@ -249,6 +249,13 @@ void FFmpegSource::onNoneReader(MediaSource &sender){ MediaSourceEvent::onNoneReader(sender); } +void FFmpegSource::onRegist(MediaSource &sender, bool regist){ + auto listener = _listener.lock(); + if(listener){ + listener->onRegist(sender, regist); + } +} + void FFmpegSource::onGetMediaSource(const MediaSource::Ptr &src) { _listener = src->getListener(); src->setListener(shared_from_this()); diff --git a/server/FFmpegSource.h b/server/FFmpegSource.h index 06bcf410..00635ac5 100644 --- a/server/FFmpegSource.h +++ b/server/FFmpegSource.h @@ -62,6 +62,7 @@ private: bool close(MediaSource &sender,bool force) override; int totalReaderCount(MediaSource &sender) override; void onNoneReader(MediaSource &sender) override; + void onRegist(MediaSource &sender, bool regist) override; private: Process _process; diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 7d9ba2f8..e7bc6b14 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -295,7 +295,7 @@ void MediaSource::regist() { //注册该源,注册后服务器才能找到该源 { lock_guard lock(g_mtxMediaSrc); - g_mapMediaSrc[_strSchema][_strVhost][_strApp][_strId] = shared_from_this(); + g_mapMediaSrc[_strSchema][_strVhost][_strApp][_strId] = shared_from_this(); } _StrPrinter codec_info; auto tracks = getTracks(true); @@ -326,6 +326,11 @@ void MediaSource::regist() { InfoL << _strSchema << " " << _strVhost << " " << _strApp << " " << _strId << " " << codec_info; NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged, true, *this); + + auto listener = _listener.lock(); + if (listener) { + listener->onRegist(*this, true); + } } //反注册该源 @@ -352,6 +357,11 @@ bool MediaSource::unregist() { if(ret){ InfoL << _strSchema << " " << _strVhost << " " << _strApp << " " << _strId; NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged, false, *this); + + auto listener = _listener.lock(); + if (listener) { + listener->onRegist(*this, false); + } } return ret; } diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 28330166..d4e44d4e 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -55,6 +55,8 @@ public: virtual bool isRecording(MediaSource &sender, Recorder::type type) { return false; }; // 通知无人观看 virtual void onNoneReader(MediaSource &sender); + //流注册或注销事件 + virtual void onRegist(MediaSource &sender, bool regist) {}; private: Timer::Ptr _async_close_timer; diff --git a/src/Common/MultiMediaSourceMuxer.cpp b/src/Common/MultiMediaSourceMuxer.cpp index b5f10da8..aeaa84bc 100644 --- a/src/Common/MultiMediaSourceMuxer.cpp +++ b/src/Common/MultiMediaSourceMuxer.cpp @@ -287,6 +287,13 @@ void MultiMediaSourceMuxer::onNoneReader(MediaSource &sender){ listener->onNoneReader(sender); } +void MultiMediaSourceMuxer::onRegist(MediaSource &sender, bool regist){ + auto listener = _listener.lock(); + if (listener) { + listener->onRegist(sender, regist); + } +} + bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) { return _muxer->setupRecord(sender,type,start,custom_path); } diff --git a/src/Common/MultiMediaSourceMuxer.h b/src/Common/MultiMediaSourceMuxer.h index 679906ba..216ba443 100644 --- a/src/Common/MultiMediaSourceMuxer.h +++ b/src/Common/MultiMediaSourceMuxer.h @@ -137,6 +137,13 @@ public: */ void onNoneReader(MediaSource &sender) override; + /** + * 媒体注册注销事件 + * @param sender 触发者 + * @param regist 是否为注册事件 + */ + void onRegist(MediaSource &sender, bool regist) override; + /** * 设置录制状态 * @param type 录制类型