添加媒体注册回调事件:#373

This commit is contained in:
xiongziliang 2020-07-02 18:14:39 +08:00
parent 5195475116
commit 3c858a8351
8 changed files with 73 additions and 1 deletions

View File

@ -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 10
*/
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

View File

@ -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;

View File

@ -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());

View File

@ -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;

View File

@ -295,7 +295,7 @@ void MediaSource::regist() {
//注册该源,注册后服务器才能找到该源
{
lock_guard<recursive_mutex> 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;
}

View File

@ -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;

View File

@ -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);
}

View File

@ -137,6 +137,13 @@ public:
*/
void onNoneReader(MediaSource &sender) override;
/**
*
* @param sender
* @param regist
*/
void onRegist(MediaSource &sender, bool regist) override;
/**
*
* @param type