简化事件参数

This commit is contained in:
xiongziliang 2019-12-24 14:08:16 +08:00
parent 5886a38a9f
commit e663ddd9a6
7 changed files with 36 additions and 49 deletions

View File

@ -148,10 +148,10 @@ void FFmpegSource::findAsync(int maxWaitMS, const function<void(const MediaSourc
}
if (!bRegist ||
schema != strongSelf->_media_info._schema ||
vhost != strongSelf->_media_info._vhost ||
app != strongSelf->_media_info._app ||
stream != strongSelf->_media_info._streamid){
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;
}

View File

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

View File

@ -86,12 +86,7 @@ void MediaSource::setTrackSource(const std::weak_ptr<TrackSource> &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);
}

View File

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

View File

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

View File

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

View File

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