优化HLS播放个数统计

This commit is contained in:
xiongziliang 2020-09-12 21:27:34 +08:00
parent d26b751273
commit 9b305bd4ae
2 changed files with 12 additions and 30 deletions

View File

@ -23,9 +23,7 @@ void HlsCookieData::addReaderCount(){
if(!*_added){ if(!*_added){
auto src = dynamic_pointer_cast<HlsMediaSource>(MediaSource::find(HLS_SCHEMA,_info._vhost,_info._app,_info._streamid)); auto src = dynamic_pointer_cast<HlsMediaSource>(MediaSource::find(HLS_SCHEMA,_info._vhost,_info._app,_info._streamid));
if(src){ if(src){
src->modifyReaderCount(true);
*_added = true; *_added = true;
_src = src;
_ring_reader = src->getRing()->attach(EventPollerPool::Instance().getPoller()); _ring_reader = src->getRing()->attach(EventPollerPool::Instance().getPoller());
auto added = _added; auto added = _added;
_ring_reader->setDetachCB([added](){ _ring_reader->setDetachCB([added](){
@ -38,10 +36,6 @@ void HlsCookieData::addReaderCount(){
HlsCookieData::~HlsCookieData() { HlsCookieData::~HlsCookieData() {
if (*_added) { if (*_added) {
auto src = _src.lock();
if (src) {
src->modifyReaderCount(false);
}
uint64_t duration = (_ticker.createdTime() - _ticker.elapsedTime()) / 1000; uint64_t duration = (_ticker.createdTime() - _ticker.elapsedTime()) / 1000;
WarnL << _sock_info->getIdentifier() << "(" << _sock_info->get_peer_ip() << ":" << _sock_info->get_peer_port() << ") " WarnL << _sock_info->getIdentifier() << "(" << _sock_info->get_peer_ip() << ":" << _sock_info->get_peer_port() << ") "
<< "HLS播放器(" << _info._vhost << "/" << _info._app << "/" << _info._streamid << "HLS播放器(" << _info._vhost << "/" << _info._app << "/" << _info._streamid

View File

@ -21,11 +21,7 @@ public:
friend class HlsCookieData; friend class HlsCookieData;
typedef RingBuffer<string> RingType; typedef RingBuffer<string> RingType;
typedef std::shared_ptr<HlsMediaSource> Ptr; typedef std::shared_ptr<HlsMediaSource> Ptr;
HlsMediaSource(const string &vhost, const string &app, const string &stream_id) : MediaSource(HLS_SCHEMA, vhost, app, stream_id){ HlsMediaSource(const string &vhost, const string &app, const string &stream_id) : MediaSource(HLS_SCHEMA, vhost, app, stream_id){}
_reader_count = 0;
_ring = std::make_shared<RingType>();
}
~HlsMediaSource() override = default; ~HlsMediaSource() override = default;
/** /**
@ -37,10 +33,9 @@ public:
/** /**
* *
* @return
*/ */
int readerCount() override { int readerCount() override {
return _reader_count.load(); return _ring ? _ring->readerCount() : 0;
} }
/** /**
@ -50,6 +45,15 @@ public:
void registHls(bool file_created){ void registHls(bool file_created){
if (!_is_regist) { if (!_is_regist) {
_is_regist = true; _is_regist = true;
weak_ptr<HlsMediaSource> weakSelf = dynamic_pointer_cast<HlsMediaSource>(shared_from_this());
auto lam = [weakSelf](int size) {
auto strongSelf = weakSelf.lock();
if (!strongSelf) {
return;
}
strongSelf->onReaderChanged(size);
};
_ring = std::make_shared<RingType>(0, std::move(lam));
onReaderChanged(0); onReaderChanged(0);
regist(); regist();
} }
@ -69,29 +73,14 @@ public:
}); });
} }
void waitForFile(function<void()> cb){ void waitForFile(function<void()> cb) {
//等待生成m3u8文件 //等待生成m3u8文件
lock_guard<mutex> lck(_mtx_cb); lock_guard<mutex> lck(_mtx_cb);
_list_cb.emplace_back(std::move(cb)); _list_cb.emplace_back(std::move(cb));
} }
private:
/**
*
* @param add
*/
void modifyReaderCount(bool add) {
if (add) {
++_reader_count;
} else {
--_reader_count;
}
onReaderChanged(_reader_count);
}
private: private:
bool _is_regist = false; bool _is_regist = false;
atomic_int _reader_count;
RingType::Ptr _ring; RingType::Ptr _ring;
mutex _mtx_cb; mutex _mtx_cb;
List<function<void()> > _list_cb; List<function<void()> > _list_cb;
@ -111,7 +100,6 @@ private:
atomic<uint64_t> _bytes {0}; atomic<uint64_t> _bytes {0};
MediaInfo _info; MediaInfo _info;
std::shared_ptr<bool> _added; std::shared_ptr<bool> _added;
weak_ptr<HlsMediaSource> _src;
Ticker _ticker; Ticker _ticker;
std::shared_ptr<SockInfo> _sock_info; std::shared_ptr<SockInfo> _sock_info;
HlsMediaSource::RingType::RingReader::Ptr _ring_reader; HlsMediaSource::RingType::RingReader::Ptr _ring_reader;