From 0063571f3a4879218626675aba53549f2d53ab55 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sun, 29 Dec 2019 17:55:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3hls=E4=B8=AD=E6=96=AD?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E6=97=B6=E6=92=AD=E6=94=BE=E5=99=A8=E8=AE=A1?= =?UTF-8?q?=E6=95=B0=E4=B8=8D=E5=87=86=E7=A1=AE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpFileManager.cpp | 14 ++++++------ src/Record/HlsMediaSource.cpp | 13 ++++++++--- src/Record/HlsMediaSource.h | 42 ++++++++++++++++++++++------------- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index 430df39f..7cd2d26f 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -258,13 +258,13 @@ static void canAccessPath(TcpSession &sender, const Parser &parser, const MediaI auto lck = cookie->getLock(); auto accessErr = (*cookie)[kAccessErrKey].get(); auto cookiePath = (*cookie)[kCookiePathKey].get(); - auto is_hls = (*cookie)[kAccessHls].get(); + auto cookie_is_hls = (*cookie)[kAccessHls].get(); if (path.find(cookiePath) == 0) { //上次cookie是限定本目录 if (accessErr.empty()) { //上次鉴权成功 - if(is_hls){ - //如果播放的是hls,那么刷新hls的cookie + if(cookie_is_hls){ + //如果播放的是hls,那么刷新hls的cookie(获取ts文件也会刷新) cookie->updateTime(); cookie_from_header = false; } @@ -284,8 +284,8 @@ static void canAccessPath(TcpSession &sender, const Parser &parser, const MediaI bool is_hls = mediaInfo._schema == HLS_SCHEMA; //该用户从来未获取过cookie,这个时候我们广播是否允许该用户访问该http目录 - HttpSession::HttpAccessPathInvoker accessPathInvoker = [callback, uid, path, is_dir, is_hls, mediaInfo] - (const string &errMsg, const string &cookie_path_in, int cookieLifeSecond) { + HttpSession::HttpAccessPathInvoker accessPathInvoker = [callback, uid, path, is_dir, is_hls, mediaInfo] + (const string &errMsg, const string &cookie_path_in, int cookieLifeSecond) { HttpServerCookie::Ptr cookie; if (cookieLifeSecond) { //本次鉴权设置了有效期,我们把鉴权结果缓存在cookie中 @@ -388,13 +388,13 @@ static void accessFile(TcpSession &sender, const Parser &parser, const MediaInfo return; } - auto response_file = [](const HttpServerCookie::Ptr &cookie, const HttpFileManager::invoker &cb, const string &strFile, const Parser &parser) { + auto response_file = [file_exist](const HttpServerCookie::Ptr &cookie, const HttpFileManager::invoker &cb, const string &strFile, const Parser &parser) { StrCaseMap httpHeader; if (cookie) { httpHeader["Set-Cookie"] = cookie->getCookie((*cookie)[kCookiePathKey].get()); } HttpSession::HttpResponseInvoker invoker = [&](const string &codeOut, const StrCaseMap &headerOut, const HttpBody::Ptr &body) { - if (cookie) { + if (cookie && file_exist) { cookie->getLock(); auto is_hls = (*cookie)[kAccessHls].get(); if (is_hls) { diff --git a/src/Record/HlsMediaSource.cpp b/src/Record/HlsMediaSource.cpp index 9ff3f010..1f48723a 100644 --- a/src/Record/HlsMediaSource.cpp +++ b/src/Record/HlsMediaSource.cpp @@ -30,22 +30,29 @@ namespace mediakit{ HlsCookieData::HlsCookieData(const MediaInfo &info) { _info = info; + _added = std::make_shared(false); addReaderCount(); } void HlsCookieData::addReaderCount(){ - if(!_added){ + if(!*_added){ auto src = dynamic_pointer_cast(MediaSource::find(HLS_SCHEMA,_info._vhost,_info._app,_info._streamid)); if(src){ src->modifyReaderCount(true); - _added = true; + *_added = true; _src = src; + _ring_reader = src->getRing()->attach(EventPollerPool::Instance().getPoller()); + auto added = _added; + _ring_reader->setDetachCB([added](){ + //HlsMediaSource已经销毁 + *added = false; + }); } } } HlsCookieData::~HlsCookieData() { - if (_added) { + if (*_added) { auto src = _src.lock(); if (src) { src->modifyReaderCount(false); diff --git a/src/Record/HlsMediaSource.h b/src/Record/HlsMediaSource.h index 760676e1..7dd83c71 100644 --- a/src/Record/HlsMediaSource.h +++ b/src/Record/HlsMediaSource.h @@ -31,32 +31,25 @@ #include "Common/MediaSource.h" namespace mediakit{ -class HlsMediaSource; -class HlsCookieData{ -public: - HlsCookieData(const MediaInfo &info); - ~HlsCookieData(); - void addByteUsage(uint64_t bytes); -private: - void addReaderCount(); -private: - uint64_t _bytes = 0; - MediaInfo _info; - bool _added = false; - weak_ptr _src; - Ticker _ticker; -}; - class HlsMediaSource : public MediaSource { 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){ _readerCount = 0; + _ring = std::make_shared(); } virtual ~HlsMediaSource() = default; + /** + * 获取媒体源的环形缓冲 + */ + const RingType::Ptr &getRing() const { + return _ring; + } + /** * 获取播放器个数 * @return @@ -93,6 +86,23 @@ private: private: atomic_int _readerCount; bool _registed = false; + RingType::Ptr _ring; +}; + +class HlsCookieData{ +public: + HlsCookieData(const MediaInfo &info); + ~HlsCookieData(); + void addByteUsage(uint64_t bytes); +private: + void addReaderCount(); +private: + uint64_t _bytes = 0; + MediaInfo _info; + std::shared_ptr _added; + weak_ptr _src; + Ticker _ticker; + HlsMediaSource::RingType::RingReader::Ptr _ring_reader; };