diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 2b2f5b35..75d6c269 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -253,22 +253,25 @@ MediaSource::Ptr MediaSource::find(const string &schema, const string &vhost_tmp vhost = DEFAULT_VHOST; } - lock_guard lock(g_mtxMediaSrc); MediaSource::Ptr ret; - //查找某一媒体源,找到后返回 - searchMedia(g_mapMediaSrc, schema, vhost, app, id, [&](SchemaVhostAppStreamMap::iterator &it0, - VhostAppStreamMap::iterator &it1, - AppStreamMap::iterator &it2, - StreamMap::iterator &it3) { - ret = it3->second.lock(); - if (!ret) { - //该对象已经销毁 - it2->second.erase(it3); - eraseIfEmpty(g_mapMediaSrc,it0, it1, it2); - return false; - } - return true; - }); + { + lock_guard lock(g_mtxMediaSrc); + //查找某一媒体源,找到后返回 + searchMedia(g_mapMediaSrc, schema, vhost, app, id, [&](SchemaVhostAppStreamMap::iterator &it0, + VhostAppStreamMap::iterator &it1, + AppStreamMap::iterator &it2, + StreamMap::iterator &it3) { + ret = it3->second.lock(); + if (!ret) { + //该对象已经销毁 + it2->second.erase(it3); + eraseIfEmpty(g_mapMediaSrc, it0, it1, it2); + return false; + } + return true; + }); + } + if(!ret && bMake){ //未查找媒体源,则创建一个 ret = MP4Reader::onMakeMediaSource(schema, vhost,app,id); @@ -288,29 +291,34 @@ void MediaSource::regist() { InfoL << _strSchema << " " << _strVhost << " " << _strApp << " " << _strId; NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged, true, *this); } -bool MediaSource::unregist() { - //反注册该源 - lock_guard lock(g_mtxMediaSrc); - return searchMedia(g_mapMediaSrc, _strSchema, _strVhost, _strApp, _strId,[&](SchemaVhostAppStreamMap::iterator &it0, - VhostAppStreamMap::iterator &it1, - AppStreamMap::iterator &it2, - StreamMap::iterator &it3) { - auto strongMedia = it3->second.lock(); - if (strongMedia && this != strongMedia.get()) { - //不是自己,不允许反注册 - return false; - } - it2->second.erase(it3); - eraseIfEmpty(g_mapMediaSrc, it0, it1, it2); - unregisted(); - return true; - }); -} -void MediaSource::unregisted(){ - InfoL << "" << _strSchema << " " << _strVhost << " " << _strApp << " " << _strId; - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged, false, *this); -} +//反注册该源 +bool MediaSource::unregist() { + bool ret; + { + lock_guard lock(g_mtxMediaSrc); + ret = searchMedia(g_mapMediaSrc, _strSchema, _strVhost, _strApp, _strId, + [&](SchemaVhostAppStreamMap::iterator &it0, + VhostAppStreamMap::iterator &it1, + AppStreamMap::iterator &it2, + StreamMap::iterator &it3) { + auto strongMedia = it3->second.lock(); + if (strongMedia && this != strongMedia.get()) { + //不是自己,不允许反注册 + return false; + } + it2->second.erase(it3); + eraseIfEmpty(g_mapMediaSrc, it0, it1, it2); + return true; + }); + } + + if(ret){ + InfoL << "" << _strSchema << " " << _strVhost << " " << _strApp << " " << _strId; + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastMediaChanged, false, *this); + } + return ret; +} /////////////////////////////////////MediaInfo////////////////////////////////////// diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 385d6d8c..e6920050 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -151,7 +151,6 @@ public: protected: void regist() ; bool unregist() ; - void unregisted(); private: string _strSchema; string _strVhost;