From e663ddd9a63270b3423e42c37fb43ef283569438 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 24 Dec 2019 14:08:16 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=E4=BA=8B=E4=BB=B6=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/FFmpegSource.cpp | 10 +++++----- server/WebHook.cpp | 8 ++++---- src/Common/MediaSource.cpp | 29 ++++++++--------------------- src/Common/config.h | 4 ++-- src/Record/Recorder.cpp | 20 ++++++++++---------- tests/test_pusher.cpp | 4 ++-- tests/test_server.cpp | 10 +++++----- 7 files changed, 36 insertions(+), 49 deletions(-) diff --git a/server/FFmpegSource.cpp b/server/FFmpegSource.cpp index 81d98bd5..12c8a072 100644 --- a/server/FFmpegSource.cpp +++ b/server/FFmpegSource.cpp @@ -147,11 +147,11 @@ void FFmpegSource::findAsync(int maxWaitMS, const function_media_info._schema || - vhost != strongSelf->_media_info._vhost || - app != strongSelf->_media_info._app || - stream != strongSelf->_media_info._streamid){ + if (!bRegist || + sender.getSchema() != strongSelf->_media_info._schema || + sender.getVhost() != strongSelf->_media_info._vhost || + sender.getApp() != strongSelf->_media_info._app || + sender.getId() != strongSelf->_media_info._streamid) { //不是自己感兴趣的事件,忽略之 return; } diff --git a/server/WebHook.cpp b/server/WebHook.cpp index b6283025..6c911588 100644 --- a/server/WebHook.cpp +++ b/server/WebHook.cpp @@ -341,10 +341,10 @@ void installWebHook(){ } ArgsType body; body["regist"] = bRegist; - body["schema"] = schema; - body["vhost"] = vhost; - body["app"] = app; - body["stream"] = stream; + body["schema"] = sender.getSchema(); + body["vhost"] = sender.getVhost(); + body["app"] = sender.getApp(); + body["stream"] = sender.getId(); //执行hook do_http_hook(hook_stream_chaned,body, nullptr); }); diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 036f541c..d1e8409b 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -86,12 +86,7 @@ void MediaSource::setTrackSource(const std::weak_ptr &track_src) { if (!strongPtr) { return; } - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaResetTracks, - _strSchema, - _strVhost, - _strApp, - _strId, - *this); + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaResetTracks, *this); },false); } @@ -228,7 +223,11 @@ void findAsync_l(const MediaInfo &info, return; } - if(!bRegist || schema != info._schema || vhost != info._vhost || app != info._app ||stream != info._streamid){ + if (!bRegist || + sender.getSchema() != info._schema || + sender.getVhost() != info._vhost || + sender.getApp() != info._app || + sender.getId() != info._streamid) { //不是自己感兴趣的事件,忽略之 return; } @@ -312,13 +311,7 @@ void MediaSource::regist() { if (!strongPtr) { return; } - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged, - true, - _strSchema, - _strVhost, - _strApp, - _strId, - *this); + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged, true, *this); },false); } bool MediaSource::unregist() { @@ -341,13 +334,7 @@ bool MediaSource::unregist() { } void MediaSource::unregisted(){ InfoL << "" << _strSchema << " " << _strVhost << " " << _strApp << " " << _strId; - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged, - false, - _strSchema, - _strVhost, - _strApp, - _strId, - *this); + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged, false, *this); } diff --git a/src/Common/config.h b/src/Common/config.h index 9e9b3c1e..53910dfc 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -69,11 +69,11 @@ namespace Broadcast { //注册或反注册MediaSource事件广播 extern const string kBroadcastMediaChanged; -#define BroadcastMediaChangedArgs const bool &bRegist, const string &schema,const string &vhost,const string &app,const string &stream,MediaSource &sender +#define BroadcastMediaChangedArgs const bool &bRegist, MediaSource &sender //MediaSource重置Track事件 extern const string kBroadcastMediaResetTracks; -#define BroadcastMediaResetTracksArgs const string &schema,const string &vhost,const string &app,const string &stream,MediaSource &sender +#define BroadcastMediaResetTracksArgs MediaSource &sender //录制mp4文件成功后广播 extern const string kBroadcastRecordMP4; diff --git a/src/Record/Recorder.cpp b/src/Record/Recorder.cpp index 43baa0e4..b2062c28 100644 --- a/src/Record/Recorder.cpp +++ b/src/Record/Recorder.cpp @@ -221,13 +221,13 @@ private: _notice_center = NoticeCenter::Instance().shared_from_this(); _notice_center->addListener(this,Broadcast::kBroadcastMediaChanged,[this](BroadcastMediaChangedArgs){ if(bRegist){ - onRegist(schema,vhost,app,stream,sender); + onRegist(sender); }else{ - onUnRegist(schema,vhost,app,stream,sender); + onUnRegist(sender); } }); _notice_center->addListener(this,Broadcast::kBroadcastMediaResetTracks,[this](BroadcastMediaResetTracksArgs){ - onRegist(schema,vhost,app,stream,sender); + onRegist(sender); }); } @@ -236,8 +236,8 @@ private: _notice_center->delListener(this,Broadcast::kBroadcastMediaResetTracks); } - void onRegist(const string &schema,const string &vhost,const string &app,const string &stream,MediaSource &sender){ - auto key = getRecorderKey(vhost,app,stream); + void onRegist(MediaSource &sender){ + auto key = getRecorderKey(sender.getVhost(),sender.getApp(),sender.getId()); lock_guard lck(_recorder_mtx); auto it = _recorder_map.find(key); if(it == _recorder_map.end()){ @@ -245,20 +245,20 @@ private: return; } - if(!it->second->isRecording() || it->second->getSchema() == schema){ + if(!it->second->isRecording() || it->second->getSchema() == sender.getSchema()){ // 绑定的协议一致或者并未正在录制则替换tracks auto tracks = sender.getTracks(true); if (!tracks.empty()) { - it->second->attachTracks(std::move(tracks),schema); + it->second->attachTracks(std::move(tracks),sender.getSchema()); } } } - void onUnRegist(const string &schema,const string &vhost,const string &app,const string &stream,MediaSource &sender){ - auto key = getRecorderKey(vhost,app,stream); + void onUnRegist(MediaSource &sender){ + auto key = getRecorderKey(sender.getVhost(),sender.getApp(),sender.getId()); lock_guard lck(_recorder_mtx); auto it = _recorder_map.find(key); - if(it == _recorder_map.end() || it->second->getSchema() != schema){ + if(it == _recorder_map.end() || it->second->getSchema() != sender.getSchema()){ // 录像记录不存在或绑定的协议不一致 return; } diff --git a/tests/test_pusher.cpp b/tests/test_pusher.cpp index c7a56aa0..d51aeadb 100644 --- a/tests/test_pusher.cpp +++ b/tests/test_pusher.cpp @@ -99,8 +99,8 @@ int domain(const string &playUrl, const string &pushUrl) { NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaChanged, [pushUrl,poller](BroadcastMediaChangedArgs) { //媒体源"app/stream"已经注册,这时方可新建一个RtmpPusher对象并绑定该媒体源 - if(bRegist && pushUrl.find(schema) == 0){ - createPusher(poller,schema,vhost,app, stream, pushUrl); + if(bRegist && pushUrl.find(sender.getSchema()) == 0){ + createPusher(poller,sender.getSchema(),sender.getVhost(),sender.getApp(), sender.getId(), pushUrl); } }); diff --git a/tests/test_server.cpp b/tests/test_server.cpp index 27c9c86c..cb6dc2da 100644 --- a/tests/test_server.cpp +++ b/tests/test_server.cpp @@ -178,23 +178,23 @@ void initEventListener() { //监听rtsp、rtmp源注册或注销事件;此处用于测试rtmp保存为flv录像,保存在http根目录下 NoticeCenter::Instance().addListener(nullptr, Broadcast::kBroadcastMediaChanged, [](BroadcastMediaChangedArgs) { - if (schema == RTMP_SCHEMA && app == "live") { + if (sender.getSchema() == RTMP_SCHEMA && sender.getApp() == "live") { lock_guard lck(s_mtxFlvRecorder); if (bRegist) { - DebugL << "开始录制RTMP:" << schema << " " << vhost << " " << app << " " << stream; + DebugL << "开始录制RTMP:" << sender.getSchema() << " " << sender.getVhost() << " " << sender.getApp() << " " << sender.getId(); GET_CONFIG(string, http_root, Http::kRootPath); auto path = - http_root + "/" + vhost + "/" + app + "/" + stream + "_" + to_string(time(NULL)) + ".flv"; + http_root + "/" + sender.getVhost() + "/" + sender.getApp() + "/" + sender.getId() + "_" + to_string(time(NULL)) + ".flv"; FlvRecorder::Ptr recorder(new FlvRecorder); try { recorder->startRecord(EventPollerPool::Instance().getPoller(), dynamic_pointer_cast(sender.shared_from_this()), path); - s_mapFlvRecorder[vhost + "/" + app + "/" + stream] = recorder; + s_mapFlvRecorder[sender.getVhost() + "/" + sender.getApp() + "/" + sender.getId()] = recorder; } catch (std::exception &ex) { WarnL << ex.what(); } } else { - s_mapFlvRecorder.erase(vhost + "/" + app + "/" + stream); + s_mapFlvRecorder.erase(sender.getVhost() + "/" + sender.getApp() + "/" + sender.getId()); } } });