From 1afacdcff85a8a75ac6ccdc9491b783a5e5c6181 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 28 Dec 2019 18:50:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E6=B7=BB=E5=8A=A0HlsMediaSou?= =?UTF-8?q?rce?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/MediaSource.cpp | 31 ++---- src/Common/MediaSource.h | 2 +- src/Common/MultiMediaSourceMuxer.h | 5 +- src/Common/config.h | 1 + src/Http/HttpFileManager.cpp | 2 +- src/Record/HlsMakerImp.cpp | 7 ++ src/Record/HlsMakerImp.h | 10 ++ src/Record/HlsManager.cpp | 101 ------------------ src/Record/HlsMediaSource.cpp | 52 +++++++++ src/Record/{HlsManager.h => HlsMediaSource.h} | 85 ++++++++------- src/Record/HlsRecorder.h | 3 + src/Record/Recorder.cpp | 4 +- 12 files changed, 135 insertions(+), 168 deletions(-) delete mode 100644 src/Record/HlsManager.cpp create mode 100644 src/Record/HlsMediaSource.cpp rename src/Record/{HlsManager.h => HlsMediaSource.h} (59%) diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index f5ae9213..ff0b6094 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -39,9 +39,7 @@ recursive_mutex MediaSource::g_mtxMediaSrc; MediaSource::SchemaVhostAppStreamMap MediaSource::g_mapMediaSrc; MediaSource::MediaSource(const string &strSchema, const string &strVhost, const string &strApp, const string &strId) : - _strSchema(strSchema), - _strApp(strApp), - _strId(strId) { + _strSchema(strSchema), _strApp(strApp), _strId(strId) { if (strVhost.empty()) { _strVhost = DEFAULT_VHOST; } else { @@ -146,12 +144,7 @@ void MediaSource::for_each_media(const function -static bool searchMedia(MAP &map, - const string &schema, - const string &vhost, - const string &app, - const string &id, - FUNC &&func) { +static bool searchMedia(MAP &map, const string &schema, const string &vhost, const string &app, const string &id, FUNC &&func) { auto it0 = map.find(schema); if (it0 == map.end()) { //未找到协议 @@ -188,16 +181,9 @@ static void eraseIfEmpty(MAP &map, IT0 it0, IT1 it1, IT2 it2) { } }; -void findAsync_l(const MediaInfo &info, - const std::shared_ptr &session, - bool retry, - const function &cb){ - - auto src = MediaSource::find(info._schema, - info._vhost, - info._app, - info._streamid, - true); +void findAsync_l(const MediaInfo &info, const std::shared_ptr &session, bool retry, + const function &cb){ + auto src = MediaSource::find(info._schema, info._vhost, info._app, info._streamid, true); if(src || !retry){ cb(src); return; @@ -263,12 +249,7 @@ void MediaSource::findAsync(const MediaInfo &info, const std::shared_ptr{ public: @@ -66,7 +66,8 @@ public: } _get_hls_player = [vhost,strApp,strId](){ - return HlsManager::Instance().hlsPlayerCount(vhost,strApp,strId); + auto src = MediaSource::find(HLS_SCHEMA,vhost,strApp,strId); + return src ? src->readerCount() : 0; }; } virtual ~MultiMediaSourceMuxer(){} diff --git a/src/Common/config.h b/src/Common/config.h index 53910dfc..cf22c3f0 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -62,6 +62,7 @@ bool loadIniConfig(const char *ini_path = nullptr); #define HTTP_SCHEMA "http" #define RTSP_SCHEMA "rtsp" #define RTMP_SCHEMA "rtmp" +#define HLS_SCHEMA "hls" #define DEFAULT_VHOST "__defaultVhost__" ////////////广播名称/////////// diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index c35b0729..5e39823c 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -32,7 +32,7 @@ #include "HttpFileManager.h" #include "Util/File.h" #include "HttpSession.h" -#include "Record/HlsManager.h" +#include "Record/HlsMediaSource.h" namespace mediakit { diff --git a/src/Record/HlsMakerImp.cpp b/src/Record/HlsMakerImp.cpp index cfb42ed6..2c7825bb 100644 --- a/src/Record/HlsMakerImp.cpp +++ b/src/Record/HlsMakerImp.cpp @@ -95,6 +95,9 @@ void HlsMakerImp::onWriteHls(const char *data, int len) { if(hls){ fwrite(data,len,1,hls.get()); hls.reset(); + if(_media_src){ + _media_src->registHls(); + } } else{ WarnL << "create hls file falied," << _path_hls << " " << get_uv_errmsg(); } @@ -115,4 +118,8 @@ std::shared_ptr HlsMakerImp::makeFile(const string &file,bool setbuf) { return ret; } +void HlsMakerImp::setMediaInfo(const string &vhost, const string &app, const string &stream_id) { + _media_src = std::make_shared(vhost, app, stream_id); +} + }//namespace mediakit \ No newline at end of file diff --git a/src/Record/HlsMakerImp.h b/src/Record/HlsMakerImp.h index c9683510..3a267243 100644 --- a/src/Record/HlsMakerImp.h +++ b/src/Record/HlsMakerImp.h @@ -31,6 +31,7 @@ #include #include #include "HlsMaker.h" +#include "HlsMediaSource.h" using namespace std; namespace mediakit { @@ -43,6 +44,14 @@ public: float seg_duration = 5, uint32_t seg_number = 3); virtual ~HlsMakerImp(); + + /** + * 设置媒体信息 + * @param vhost 虚拟主机 + * @param app 应用名 + * @param stream_id 流id + */ + void setMediaInfo(const string &vhost, const string &app, const string &stream_id); protected: string onOpenSegment(int index) override ; void onDelSegment(int index) override; @@ -51,6 +60,7 @@ protected: private: std::shared_ptr makeFile(const string &file,bool setbuf = false); private: + HlsMediaSource::Ptr _media_src; map _segment_file_paths; std::shared_ptr _file; std::shared_ptr _file_buf; diff --git a/src/Record/HlsManager.cpp b/src/Record/HlsManager.cpp deleted file mode 100644 index 72fad860..00000000 --- a/src/Record/HlsManager.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com> - * - * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit). - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "HlsManager.h" -#include "Util/util.h" -using namespace toolkit; - -namespace mediakit{ - -HlsCookieData::HlsCookieData(const MediaInfo &info) { - _info = info; - _manager = HlsManager::Instance().shared_from_this(); - _manager->onAddHlsPlayer(_info); -} - -HlsCookieData::~HlsCookieData() { - _manager->onAddHlsPlayer(_info); -} - -void HlsCookieData::addByteUsage(uint64_t bytes) { - _bytes += bytes; -} - -//////////////////////////////////////////////////////// -HlsManager::HlsManager() {} -HlsManager::~HlsManager() {} -INSTANCE_IMP(HlsManager); - -void HlsManager::onAddHlsPlayer(const MediaInfo &info) { - lock_guard lck(_mtx); - ++_player_counter[info._vhost][info._app][info._streamid]._count; -} - -void HlsManager::onDelHlsPlayer(const MediaInfo &info) { - lock_guard lck(_mtx); - auto it0 = _player_counter.find(info._vhost); - if(it0 == _player_counter.end()){ - return; - } - auto it1 = it0->second.find(info._app); - if(it1 == it0->second.end()){ - return; - } - auto it2 = it1->second.find(info._streamid); - if(it2 == it1->second.end()){ - return; - } - if(--(it2->second._count) == 0){ - it1->second.erase(it2); - if(it1->second.empty()){ - it0->second.erase(it1); - if(it0->second.empty()){ - _player_counter.erase(it0); - } - } - } -} - -int HlsManager::hlsPlayerCount(const string &vhost, const string &app, const string &stream) { - lock_guard lck(_mtx); - auto it0 = _player_counter.find(vhost); - if(it0 == _player_counter.end()){ - return 0; - } - auto it1 = it0->second.find(app); - if(it1 == it0->second.end()){ - return 0; - } - auto it2 = it1->second.find(stream); - if(it2 == it1->second.end()){ - return 0; - } - return it2->second._count; -} - - -}//namespace mediakit - diff --git a/src/Record/HlsMediaSource.cpp b/src/Record/HlsMediaSource.cpp new file mode 100644 index 00000000..4f1933b2 --- /dev/null +++ b/src/Record/HlsMediaSource.cpp @@ -0,0 +1,52 @@ +/* + * MIT License + * + * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com> + * + * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "HlsMediaSource.h" + +namespace mediakit{ + +HlsCookieData::HlsCookieData(const MediaInfo &info) { + _info = info; + auto src = dynamic_pointer_cast(MediaSource::find(HLS_SCHEMA,_info._vhost,_info._app,_info._streamid)); + if(src){ + src->modifyCount(true); + } +} + +HlsCookieData::~HlsCookieData() { + auto src = dynamic_pointer_cast(MediaSource::find(HLS_SCHEMA,_info._vhost,_info._app,_info._streamid)); + if(src){ + src->modifyCount(false); + } +} + +void HlsCookieData::addByteUsage(uint64_t bytes) { + _bytes += bytes; +} + + +}//namespace mediakit + diff --git a/src/Record/HlsManager.h b/src/Record/HlsMediaSource.h similarity index 59% rename from src/Record/HlsManager.h rename to src/Record/HlsMediaSource.h index 059c1777..e7a8eb2a 100644 --- a/src/Record/HlsManager.h +++ b/src/Record/HlsMediaSource.h @@ -1,4 +1,4 @@ -/* +/* * MIT License * * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com> @@ -23,20 +23,13 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +#ifndef ZLMEDIAKIT_HLSMEDIASOURCE_H +#define ZLMEDIAKIT_HLSMEDIASOURCE_H - -#ifndef ZLMEDIAKIT_HLSMANAGER_H -#define ZLMEDIAKIT_HLSMANAGER_H - -#include -#include -#include +#include #include "Common/MediaSource.h" -using namespace std; - namespace mediakit{ -class HlsManager; class HlsCookieData{ public: HlsCookieData(const MediaInfo &info); @@ -45,41 +38,59 @@ public: private: uint64_t _bytes = 0; MediaInfo _info; - std::shared_ptr _manager; }; - -class HlsManager : public std::enable_shared_from_this{ +class HlsMediaSource : public MediaSource { public: friend class HlsCookieData; - ~HlsManager(); - static HlsManager& Instance(); + typedef std::shared_ptr Ptr; + HlsMediaSource(const string &vhost, + const string &app, + const string &stream_id) : + MediaSource(HLS_SCHEMA, vhost, app, stream_id){ + _readerCount = 0; + } + + virtual ~HlsMediaSource() = default; /** - * 获取hls播放器个数 - * @param vhost 虚拟主机 - * @param app 应用名 - * @param stream 流id - * @return 播放器个数 + * 获取播放器个数 + * @return + */ + int readerCount() override { + return _readerCount.load(); + } + + /** + * 注册hls */ - int hlsPlayerCount(const string &vhost,const string &app,const string &stream); -private: - void onAddHlsPlayer(const MediaInfo &info); - void onDelHlsPlayer(const MediaInfo &info); - HlsManager(); -private: - class HlsPlayerCounter{ - private: - friend class HlsManager; - int _count = 0; - }; -private: - recursive_mutex _mtx; - unordered_map > > _player_counter; + void registHls(){ + if(!_registed){ + regist(); + _registed = true; + } + } +private: + /** + * 修改观看者个数 + * @param add 添加海思删除 + */ + void modifyCount(bool add) { + if (add) { + ++_readerCount; + return; + } + if (--_readerCount == 0 && totalReaderCount() == 0) { + onNoneReader(); + } + } +private: + atomic_int _readerCount; + bool _registed = false; }; + }//namespace mediakit -#endif //ZLMEDIAKIT_HLSMANAGER_H +#endif //ZLMEDIAKIT_HLSMEDIASOURCE_H diff --git a/src/Record/HlsRecorder.h b/src/Record/HlsRecorder.h index c0def9bb..c29d27cd 100644 --- a/src/Record/HlsRecorder.h +++ b/src/Record/HlsRecorder.h @@ -43,6 +43,9 @@ public: ~HlsRecorder(){ delete _hls; } + void setMediaInfo(const string &vhost, const string &app, const string &stream_id){ + _hls->setMediaInfo(vhost,app,stream_id); + } protected: void onTs(const void *packet, int bytes,uint32_t timestamp,int flags) override { _hls->inputData((char *)packet,bytes,timestamp); diff --git a/src/Record/Recorder.cpp b/src/Record/Recorder.cpp index 5c7f1397..d2ad95aa 100644 --- a/src/Record/Recorder.cpp +++ b/src/Record/Recorder.cpp @@ -54,7 +54,9 @@ MediaSinkInterface *createHlsRecorder(const string &strVhost_tmp, const string & m3u8FilePath = strApp + "/" + strId + "/hls.m3u8"; } m3u8FilePath = File::absolutePath(m3u8FilePath, hlsPath); - return new HlsRecorder(m3u8FilePath, params); + auto ret = new HlsRecorder(m3u8FilePath, params); + ret->setMediaInfo(strVhost,strApp,strId); + return ret; #else return nullptr; #endif //defined(ENABLE_HLS)