初步实现hls播放器个数计数

This commit is contained in:
xiongziliang 2019-12-28 16:48:42 +08:00
parent 94806b2cd6
commit f93b32740f
2 changed files with 186 additions and 0 deletions

101
src/Record/HlsManager.cpp Normal file
View File

@ -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<decltype(_mtx)> lck(_mtx);
++_player_counter[info._vhost][info._app][info._streamid]._count;
}
void HlsManager::onDelHlsPlayer(const MediaInfo &info) {
lock_guard<decltype(_mtx)> 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<decltype(_mtx)> 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

85
src/Record/HlsManager.h Normal file
View File

@ -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 <memory>
#include <string>
#include <mutex>
#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<HlsManager> _manager;
};
class HlsManager : public std::enable_shared_from_this<HlsManager>{
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<string/*vhost*/,unordered_map<string/*app*/,
unordered_map<string/*stream*/,HlsPlayerCounter> > > _player_counter;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_HLSMANAGER_H