From f93b32740f14c091a0db32553a9006cb85bad99c Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 28 Dec 2019 16:48:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=9E=E7=8E=B0hls?= =?UTF-8?q?=E6=92=AD=E6=94=BE=E5=99=A8=E4=B8=AA=E6=95=B0=E8=AE=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/HlsManager.cpp | 101 ++++++++++++++++++++++++++++++++++++++ src/Record/HlsManager.h | 85 ++++++++++++++++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 src/Record/HlsManager.cpp create mode 100644 src/Record/HlsManager.h diff --git a/src/Record/HlsManager.cpp b/src/Record/HlsManager.cpp new file mode 100644 index 00000000..72fad860 --- /dev/null +++ b/src/Record/HlsManager.cpp @@ -0,0 +1,101 @@ +/* + * 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/HlsManager.h b/src/Record/HlsManager.h new file mode 100644 index 00000000..059c1777 --- /dev/null +++ b/src/Record/HlsManager.h @@ -0,0 +1,85 @@ +/* + * 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. + */ + + +#ifndef ZLMEDIAKIT_HLSMANAGER_H +#define ZLMEDIAKIT_HLSMANAGER_H + +#include +#include +#include +#include "Common/MediaSource.h" +using namespace std; + +namespace mediakit{ + +class HlsManager; +class HlsCookieData{ +public: + HlsCookieData(const MediaInfo &info); + ~HlsCookieData(); + void addByteUsage(uint64_t bytes); +private: + uint64_t _bytes = 0; + MediaInfo _info; + std::shared_ptr _manager; +}; + + +class HlsManager : public std::enable_shared_from_this{ +public: + friend class HlsCookieData; + ~HlsManager(); + static HlsManager& Instance(); + + /** + * 获取hls播放器个数 + * @param vhost 虚拟主机 + * @param app 应用名 + * @param stream 流id + * @return 播放器个数 + */ + 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; + + +}; + +}//namespace mediakit +#endif //ZLMEDIAKIT_HLSMANAGER_H