diff --git a/src/Record/HlsMediaSource.cpp b/src/Record/HlsMediaSource.cpp index 7888eddf..99f2022e 100644 --- a/src/Record/HlsMediaSource.cpp +++ b/src/Record/HlsMediaSource.cpp @@ -23,9 +23,7 @@ void HlsCookieData::addReaderCount(){ if(!*_added){ auto src = dynamic_pointer_cast(MediaSource::find(HLS_SCHEMA,_info._vhost,_info._app,_info._streamid)); if(src){ - src->modifyReaderCount(true); *_added = true; - _src = src; _ring_reader = src->getRing()->attach(EventPollerPool::Instance().getPoller()); auto added = _added; _ring_reader->setDetachCB([added](){ @@ -38,10 +36,6 @@ void HlsCookieData::addReaderCount(){ HlsCookieData::~HlsCookieData() { if (*_added) { - auto src = _src.lock(); - if (src) { - src->modifyReaderCount(false); - } uint64_t duration = (_ticker.createdTime() - _ticker.elapsedTime()) / 1000; WarnL << _sock_info->getIdentifier() << "(" << _sock_info->get_peer_ip() << ":" << _sock_info->get_peer_port() << ") " << "HLS播放器(" << _info._vhost << "/" << _info._app << "/" << _info._streamid diff --git a/src/Record/HlsMediaSource.h b/src/Record/HlsMediaSource.h index ddf26721..b5b8357f 100644 --- a/src/Record/HlsMediaSource.h +++ b/src/Record/HlsMediaSource.h @@ -21,11 +21,7 @@ public: friend class HlsCookieData; typedef RingBuffer RingType; typedef std::shared_ptr Ptr; - 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(); - } - + HlsMediaSource(const string &vhost, const string &app, const string &stream_id) : MediaSource(HLS_SCHEMA, vhost, app, stream_id){} ~HlsMediaSource() override = default; /** @@ -37,10 +33,9 @@ public: /** * 获取播放器个数 - * @return */ int readerCount() override { - return _reader_count.load(); + return _ring ? _ring->readerCount() : 0; } /** @@ -50,6 +45,15 @@ public: void registHls(bool file_created){ if (!_is_regist) { _is_regist = true; + weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); + auto lam = [weakSelf](int size) { + auto strongSelf = weakSelf.lock(); + if (!strongSelf) { + return; + } + strongSelf->onReaderChanged(size); + }; + _ring = std::make_shared(0, std::move(lam)); onReaderChanged(0); regist(); } @@ -69,29 +73,14 @@ public: }); } - void waitForFile(function cb){ + void waitForFile(function cb) { //等待生成m3u8文件 lock_guard lck(_mtx_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: bool _is_regist = false; - atomic_int _reader_count; RingType::Ptr _ring; mutex _mtx_cb; List > _list_cb; @@ -111,7 +100,6 @@ private: atomic _bytes {0}; MediaInfo _info; std::shared_ptr _added; - weak_ptr _src; Ticker _ticker; std::shared_ptr _sock_info; HlsMediaSource::RingType::RingReader::Ptr _ring_reader;