diff --git a/services/media_engine/plugins/source/http_source/dash/dash_media_downloader.cpp b/services/media_engine/plugins/source/http_source/dash/dash_media_downloader.cpp index 78d4d20d73931fc4d5103ec9f18f2793d47ab1a4..ca2e3e459a0dc0fc65946ff5337938f6913acbf3 100644 --- a/services/media_engine/plugins/source/http_source/dash/dash_media_downloader.cpp +++ b/services/media_engine/plugins/source/http_source/dash/dash_media_downloader.cpp @@ -471,12 +471,17 @@ void DashMediaDownloader::OpenInitSegment( if (statusCallback_ != nullptr) { downloader->SetStatusCallback(statusCallback_); } - auto doneCallback = [this] (int streamId) { - UpdateDownloadFinished(streamId); + auto weakDownloader = weak_from_this(); + auto doneCallback = [weakDownloader] (int streamId) { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "doneCb, dash media downloader already destructed."); + shareDownloader->UpdateDownloadFinished(streamId); }; downloader->SetDownloadDoneCallback(doneCallback); - auto bufferingCallback = [this] (int streamId, BufferingInfoType type) { - PostBufferingEvent(streamId, type); + auto bufferingCallback = [weakDownloader] (int streamId, BufferingInfoType type) { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "bufferingCb, dash media downloader already destructed."); + shareDownloader->PostBufferingEvent(streamId, type); }; downloader->SetSegmentBufferingCallback(bufferingCallback); segmentDownloaders_.push_back(downloader); diff --git a/services/media_engine/plugins/source/http_source/dash/dash_media_downloader.h b/services/media_engine/plugins/source/http_source/dash/dash_media_downloader.h index 2a902f70d98073f12c865e570ee79ba01f934954..438d634d1962eda6f8c7546c3579d1074beb3342 100644 --- a/services/media_engine/plugins/source/http_source/dash/dash_media_downloader.h +++ b/services/media_engine/plugins/source/http_source/dash/dash_media_downloader.h @@ -33,7 +33,8 @@ struct DashPreparedAction { DashMpdTrackParam preparedSubtitleParam_; }; -class DashMediaDownloader : public MediaDownloader, public DashMpdCallback { +class DashMediaDownloader : public MediaDownloader, public DashMpdCallback, + public std::enable_shared_from_this { public: explicit DashMediaDownloader(std::shared_ptr sourceLoader = nullptr); ~DashMediaDownloader() override; diff --git a/services/media_engine/plugins/source/http_source/dash/dash_mpd_downloader.cpp b/services/media_engine/plugins/source/http_source/dash/dash_mpd_downloader.cpp index 2f5209e84c85a3a527d2e2dd232d8d6b857292cb..051354d2ffa4f18f35b495f11bdb250c4f2bf668 100644 --- a/services/media_engine/plugins/source/http_source/dash/dash_mpd_downloader.cpp +++ b/services/media_engine/plugins/source/http_source/dash/dash_mpd_downloader.cpp @@ -47,8 +47,11 @@ DashMpdDownloader::DashMpdDownloader(std::shared_ptr("dashMpd"); } - dataSave_ = [this] (uint8_t*&& data, uint32_t&& len, bool&& notBlock) { - return SaveData(std::forward(data), std::forward(len), + auto weakDownloader = weak_from_this(); + dataSave_ = [weakDownloader] (uint8_t*&& data, uint32_t&& len, bool&& notBlock) -> uint32_t { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_V_MSG(shareDownloader != nullptr, 0u, "doneCb, dash mpd downloader already destructed."); + return shareDownloader->SaveData(std::forward(data), std::forward(len), std::forward(notBlock)); }; @@ -786,10 +789,14 @@ void DashMpdDownloader::OpenStream(std::shared_ptr stream void DashMpdDownloader::DoOpen(const std::string& url, int64_t startRange, int64_t endRange) { downloadContent_.clear(); - auto realStatusCallback = [this](DownloadStatus &&status, std::shared_ptr &downloader, + auto weakDownloader = weak_from_this(); + auto realStatusCallback = [weakDownloader](DownloadStatus &&status, std::shared_ptr &downloader, std::shared_ptr &request) { - if (statusCallback_ != nullptr) { - statusCallback_(status, downloader_, std::forward(request)); + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "realStatusCb, dash mpd downloader already destructed."); + if (shareDownloader->statusCallback_ != nullptr) { + shareDownloader->statusCallback_(status, shareDownloader->downloader_, + std::forward(request)); } }; @@ -803,8 +810,10 @@ void DashMpdDownloader::DoOpen(const std::string& url, int64_t startRange, int64 mediaSource.url = url; mediaSource.timeoutMs = MPD_HTTP_TIME_OUT_MS; downloadRequest_ = std::make_shared(dataSave_, realStatusCallback, mediaSource, requestWholeFile); - auto downloadDoneCallback = [this](const std::string &url, const std::string &location) { - UpdateDownloadFinished(url); + auto downloadDoneCallback = [weakDownloader](const std::string &url, const std::string &location) { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "downloadDoneCb, dash mpd downloader already destructed."); + shareDownloader->UpdateDownloadFinished(url); }; downloadRequest_->SetDownloadDoneCb(downloadDoneCallback); downloadRequest_->SetRequestProtocolType(RequestProtocolType::DASH); diff --git a/services/media_engine/plugins/source/http_source/dash/dash_mpd_downloader.h b/services/media_engine/plugins/source/http_source/dash/dash_mpd_downloader.h index 3153b7041982f87d273e64436739e2d35d368742..d9fba2d4a90003cba2be48b5129c8da8a95c3c5f 100644 --- a/services/media_engine/plugins/source/http_source/dash/dash_mpd_downloader.h +++ b/services/media_engine/plugins/source/http_source/dash/dash_mpd_downloader.h @@ -109,7 +109,7 @@ struct DashMpdCallback { virtual void OnDrmInfoChanged(const std::multimap>& drmInfos) = 0; }; -class DashMpdDownloader { +class DashMpdDownloader : public std::enable_shared_from_this { public: explicit DashMpdDownloader(std::shared_ptr sourceLoader = nullptr); virtual ~DashMpdDownloader() noexcept; diff --git a/services/media_engine/plugins/source/http_source/dash/dash_segment_downloader.cpp b/services/media_engine/plugins/source/http_source/dash/dash_segment_downloader.cpp index 13532087e5dc92e652862a32ede4b9fd20095746..7e00f281ecc2428221648ef9ba4e02e67f3d066b 100644 --- a/services/media_engine/plugins/source/http_source/dash/dash_segment_downloader.cpp +++ b/services/media_engine/plugins/source/http_source/dash/dash_segment_downloader.cpp @@ -75,8 +75,11 @@ DashSegmentDownloader::DashSegmentDownloader(Callback *callback, int streamId, M sourceLoader_ = sourceLoader; downloader_ = std::make_shared("dashSegment", sourceLoader_); - dataSave_ = [this] (uint8_t*&& data, uint32_t&& len, bool&& notBlock) { - return SaveData(std::forward(data), std::forward(len), + auto weakDownloader = weak_from_this(); + dataSave_ = [weakDownloader] (uint8_t*&& data, uint32_t&& len, bool&& notBlock) -> uint32_t { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_V_MSG(shareDownloader != nullptr, 0u, "downloadDoneCb, dash seg downloader already destructed."); + return shareDownloader->SaveData(std::forward(data), std::forward(len), std::forward(notBlock)); }; @@ -1027,12 +1030,18 @@ uint32_t DashSegmentDownloader::GetRingBufferCapacity() const void DashSegmentDownloader::PutRequestIntoDownloader(unsigned int duration, int64_t startPos, int64_t endPos, const std::string &url) { - auto realStatusCallback = [this](DownloadStatus &&status, std::shared_ptr &downloader, + auto weakDownloader = weak_from_this(); + auto realStatusCallback = [weakDownloader](DownloadStatus &&status, std::shared_ptr &downloader, std::shared_ptr &request) { - statusCallback_(status, downloader_, std::forward(request)); + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "realStatusCb, dash seg downloader already destructed."); + shareDownloader->statusCallback_(status, shareDownloader->downloader_, + std::forward(request)); }; - auto downloadDoneCallback = [this](const std::string &url, const std::string &location) { - UpdateDownloadFinished(url, location); + auto downloadDoneCallback = [weakDownloader](const std::string &url, const std::string &location) { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "downloadDoneCb, dash seg downloader already destructed."); + shareDownloader->UpdateDownloadFinished(url, location); }; bool requestWholeFile = true; diff --git a/services/media_engine/plugins/source/http_source/dash/dash_segment_downloader.h b/services/media_engine/plugins/source/http_source/dash/dash_segment_downloader.h index 6348d379596c396f53baafe39382024aba7e5065..4612af4380454a5ab7d58b4350658dcd28688a7a 100644 --- a/services/media_engine/plugins/source/http_source/dash/dash_segment_downloader.h +++ b/services/media_engine/plugins/source/http_source/dash/dash_segment_downloader.h @@ -135,7 +135,7 @@ struct DashBufferSegment { using SegmentDownloadDoneCbFunc = std::function; using SegmentBufferingCbFunc = std::function; -class DashSegmentDownloader { +class DashSegmentDownloader : public std::enable_shared_from_this { public: DashSegmentDownloader(Callback *callback, int streamId, MediaAVCodec::MediaType streamType, uint64_t expectDuration, std::shared_ptr sourceLoader); diff --git a/services/media_engine/plugins/source/http_source/hls/hls_media_downloader.cpp b/services/media_engine/plugins/source/http_source/hls/hls_media_downloader.cpp index 4426d4f74dd53615877b0f3e7bf7b020b112038e..b1aa1a3fbca43f8b90ca781b6524c6598bed5e13 100644 --- a/services/media_engine/plugins/source/http_source/hls/hls_media_downloader.cpp +++ b/services/media_engine/plugins/source/http_source/hls/hls_media_downloader.cpp @@ -125,8 +125,11 @@ void HlsMediaDownloader::HlsInit(std::shared_ptr downloader_ = std::make_shared("hlsMedia"); } playList_ = std::make_shared>("PlayList"); - dataSave_ = [this] (uint8_t*&& data, uint32_t&& len, bool&& notBlock) { - return SaveData(std::forward(data), std::forward(len), + auto weakDownloader = weak_from_this(); + dataSave_ = [weakDownloader] (uint8_t*&& data, uint32_t&& len, bool&& notBlock) -> uint32_t { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_V_MSG(shareDownloader != nullptr, 0u, "dataSave_, Hls Media Downloader already destructed."); + return shareDownloader->SaveData(std::forward(data), std::forward(len), std::forward(notBlock)); }; if (sourceLoader != nullptr) { @@ -177,17 +180,18 @@ std::string HlsMediaDownloader::GetContentType() void HlsMediaDownloader::SetDownloaderRequestCb( StatusCallbackFunc& statusCallback, DownloadDoneCbFunc& downloadDoneCallback) { - std::weak_ptr weakThis = weak_from_this(); - statusCallback = [weakThis] (DownloadStatus&& status, std::shared_ptr& downloader, + auto weakDownloader = weak_from_this(); + statusCallback = [weakDownloader] (DownloadStatus&& status, std::shared_ptr& downloader, std::shared_ptr& request) { - auto shareThis = weakThis.lock(); - FALSE_RETURN_MSG(shareThis != nullptr, "hls statusCallback shareThis, is nullptr!"); - shareThis->statusCallback_(status, shareThis->downloader_, std::forward(request)); + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "statusCallback, Hls Media Downloader already destructed."); + shareDownloader->statusCallback_(status, shareDownloader->downloader_, + std::forward(request)); }; - downloadDoneCallback = [weakThis] (const std::string &url, const std::string& location) { - auto shareThis = weakThis.lock(); - FALSE_RETURN_MSG(shareThis != nullptr, "hls downloadDoneCallback, shareThis is nullptr!"); - shareThis->UpdateDownloadFinished(url, location); + downloadDoneCallback = [weakDownloader] (const std::string &url, const std::string& location) { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "downloadDoneCb, Hls Media Downloader already destructed."); + shareDownloader->UpdateDownloadFinished(url, location); }; } diff --git a/services/media_engine/plugins/source/http_source/hls/m3u8.cpp b/services/media_engine/plugins/source/http_source/hls/m3u8.cpp index 2c7d8da93beeee5211eea7cffacf99411d024390..511736a9ea9c571629c957318cebea3a9d13f377 100644 --- a/services/media_engine/plugins/source/http_source/hls/m3u8.cpp +++ b/services/media_engine/plugins/source/http_source/hls/m3u8.cpp @@ -234,21 +234,33 @@ void M3U8::DownloadMap(const std::string& uri, size_t offset, size_t length) return; } downloaderHeader_ = std::make_shared("HlsSourceMap"); - dataSaveHeader_ = [this](uint8_t *&&data, uint32_t &&len, bool &¬Block) { - return SaveMapData(std::forward(data), std::forward(len), notBlock); + auto weakDownloader = weak_from_this(); + dataSaveHeader_ = [weakDownloader](uint8_t *&&data, uint32_t &&len, bool &¬Block) -> uint32_t { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_V_MSG(shareDownloader != nullptr, 0u, "dataSaveHeader, M3U8 map downloader already destructed."); + return shareDownloader->SaveMapData(std::forward(data), std::forward(len), + notBlock); }; - statusCallback_ = [this](DownloadStatus &&status, std::shared_ptr d, + statusCallback_ = [weakDownloader](DownloadStatus &&status, std::shared_ptr d, std::shared_ptr &request) { - OnDownloadStatus(std::forward(status), downloaderHeader_, - std::forward(request)); + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "statusCb, M3U8 map downloader already destructed."); + shareDownloader->OnDownloadStatus(std::forward(status), shareDownloader->downloaderHeader_, + std::forward(request)); }; - auto downloadDoneCallback = [this] (const std::string &url, const std::string& location) { - UpdateDownloadFinished(url, location); + auto downloadDoneCallback = [weakDownloader] (const std::string &url, const std::string& location) { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "downloadDoneCb, M3U8 map downloader already destructed."); + shareDownloader->UpdateDownloadFinished(url, location); }; - auto realStatusCallback = [this](DownloadStatus &&status, std::shared_ptr &downloader, + auto realStatusCallback = [weakDownloader](DownloadStatus &&status, std::shared_ptr &downloader, std::shared_ptr &request) { - monitorStatusCallback_(status, downloaderHeader_, std::forward(request)); - statusCallback_(status, downloaderHeader_, std::forward(request)); + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "realStatusCb, M3U8 map downloader already destructed."); + shareDownloader->monitorStatusCallback_(status, shareDownloader->downloaderHeader_, + std::forward(request)); + shareDownloader->statusCallback_(status, shareDownloader->downloaderHeader_, + std::forward(request)); }; RequestInfo requestInfo; requestInfo.url = uri; @@ -410,18 +422,29 @@ void M3U8::DownloadKey() } downloader_ = std::make_shared("hlsSourceKey"); - dataSave_ = [this](uint8_t *&&data, uint32_t &&len, bool &¬Block) { - return SaveData(std::forward(data), std::forward(len), notBlock); + auto weakDownloader = weak_from_this(); + dataSave_ = [weakDownloader](uint8_t *&&data, uint32_t &&len, bool &¬Block) -> uint32_t { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_V_MSG(shareDownloader != nullptr, 0u, "dataSave, M3U8 key downloader already destructed."); + return shareDownloader->SaveData(std::forward(data), std::forward(len), + notBlock); }; // this is default callback - statusCallback_ = [this](DownloadStatus &&status, std::shared_ptr d, + statusCallback_ = [weakDownloader](DownloadStatus &&status, std::shared_ptr d, std::shared_ptr &request) { - OnDownloadStatus(std::forward(status), downloader_, std::forward(request)); + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "statusCb, M3U8 key downloader already destructed."); + shareDownloader->OnDownloadStatus(std::forward(status), shareDownloader->downloader_, + std::forward(request)); }; - auto realStatusCallback = [this](DownloadStatus &&status, std::shared_ptr &downloader, + auto realStatusCallback = [weakDownloader](DownloadStatus &&status, std::shared_ptr &downloader, std::shared_ptr &request) { - monitorStatusCallback_(status, downloader_, std::forward(request)); - statusCallback_(status, downloader_, std::forward(request)); + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "realStatusCb, M3U8 key downloader already destructed."); + shareDownloader->monitorStatusCallback_(status, shareDownloader->downloader_, + std::forward(request)); + shareDownloader->statusCallback_(status, shareDownloader->downloader_, + std::forward(request)); }; std::string realKeyUrl = UriJoin(uri_, *keyUri_); RequestInfo requestInfo; diff --git a/services/media_engine/plugins/source/http_source/hls/m3u8.h b/services/media_engine/plugins/source/http_source/hls/m3u8.h index 48b94afdf75acc1c656aab9d549be5c3acf0e5f7..98cc84e7333ba1c961b5fb5ee124635d7c0958c3 100644 --- a/services/media_engine/plugins/source/http_source/hls/m3u8.h +++ b/services/media_engine/plugins/source/http_source/hls/m3u8.h @@ -65,7 +65,7 @@ struct M3U8Info { bool bVod {false}; }; -struct M3U8 { +struct M3U8 : public std::enable_shared_from_this { M3U8(std::string uri, std::string name, StatusCallbackFunc statusCallback = [](DownloadStatus, std::shared_ptr&, std::shared_ptr&) {}); diff --git a/services/media_engine/plugins/source/http_source/hls/playlist_downloader.cpp b/services/media_engine/plugins/source/http_source/hls/playlist_downloader.cpp index eadf606bee2dcce03dac02aef7f9bbae5f985169..875e37b843e2da8aa8352a8cf65162f67e9919f4 100644 --- a/services/media_engine/plugins/source/http_source/hls/playlist_downloader.cpp +++ b/services/media_engine/plugins/source/http_source/hls/playlist_downloader.cpp @@ -40,15 +40,20 @@ static bool isNumber(const std::string& str) void PlayListDownloader::PlayListDownloaderInit() { - dataSave_ = [this] (uint8_t*&& data, uint32_t&& len, bool&& notBlock) { - return SaveData(std::forward(data), std::forward(len), + auto weakDownloader = weak_from_this(); + dataSave_ = [weakDownloader] (uint8_t*&& data, uint32_t&& len, bool&& notBlock) -> uint32_t { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_V_MSG(shareDownloader != nullptr, 0u, "dataSave, playlist downloader already destructed."); + return shareDownloader->SaveData(std::forward(data), std::forward(len), std::forward(notBlock)); }; // this is default callback - statusCallback_ = [this] (DownloadStatus&& status, std::shared_ptr d, + statusCallback_ = [weakDownloader] (DownloadStatus&& status, std::shared_ptr d, std::shared_ptr& request) { - OnDownloadStatus(std::forward(status), downloader_, - std::forward(request)); + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "statusCb, playlist downloader already destructed."); + shareDownloader->OnDownloadStatus(std::forward(status), shareDownloader->downloader_, + std::forward(request)); }; updateTask_ = std::make_shared(std::string("OS_FragmentListUpdate"), "", TaskType::SINGLETON); updateTask_->RegisterJob([this] { @@ -88,28 +93,32 @@ void PlayListDownloader::SaveHttpHeader(const std::map void PlayListDownloader::DoOpen(const std::string& url) { - auto realStatusCallback = [this] (DownloadStatus&& status, std::shared_ptr& downloader, - std::shared_ptr& request) { + auto weakDownloader = weak_from_this(); + auto realStatusCallback = [weakDownloader] (DownloadStatus&& status, std::shared_ptr& downloader, + std::shared_ptr& request) { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "realStatusCb, playlist downloader already destructed."); // 目标资源永久删除,需要重头重新请求 if (request->GetServerError() == HTTP_SERVER_ERROR_410) { - ReOpen(); + shareDownloader->ReOpen(); return; } - if (retryStartTime_ == 0) { - retryStartTime_ = request->GetNowTime(); + if (shareDownloader->retryStartTime_ == 0) { + shareDownloader->retryStartTime_ = request->GetNowTime(); } int64_t nowTime = request->GetNowTime(); - bool isNeedReportError = (nowTime - retryStartTime_) >= RETRY_DELTA_TIME_TO_REPORT_ERROR + bool isNeedReportError = (nowTime - shareDownloader->retryStartTime_) >= RETRY_DELTA_TIME_TO_REPORT_ERROR || request->GetRetryTimes() >= RETRY_TIME_TO_REPORT_ERROR; - if (isNeedReportError && eventCallback_ != nullptr) { + if (isNeedReportError && shareDownloader->eventCallback_ != nullptr) { MEDIA_LOG_E("fail to download m3u8."); - eventCallback_->OnEvent({PluginEventType::CLIENT_ERROR, - {NetworkClientErrorCode::ERROR_TIME_OUT}, "download m3u8"}); + shareDownloader->eventCallback_->OnEvent({PluginEventType::CLIENT_ERROR, + {NetworkClientErrorCode::ERROR_TIME_OUT}, "download m3u8"}); return; } - playList_.clear(); - statusCallback_(status, downloader_, std::forward(request)); + shareDownloader->playList_.clear(); + shareDownloader->statusCallback_(status, shareDownloader->downloader_, + std::forward(request)); }; RequestInfo requestInfo; @@ -120,8 +129,10 @@ void PlayListDownloader::DoOpen(const std::string& url) MEDIA_LOG_E("no enough memory downloadRequest_ is nullptr"); return; } - auto downloadDoneCallback = [this] (const std::string& url, const std::string& location) { - UpdateDownloadFinished(url, location); + auto downloadDoneCallback = [weakDownloader] (const std::string& url, const std::string& location) { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "downloadDoneCb, playlist downloader already destructed."); + shareDownloader->UpdateDownloadFinished(url, location); }; downloadRequest_->SetRequestProtocolType(RequestProtocolType::HLS); downloadRequest_->SetDownloadDoneCb(downloadDoneCallback); diff --git a/services/media_engine/plugins/source/http_source/hls/playlist_downloader.h b/services/media_engine/plugins/source/http_source/hls/playlist_downloader.h index 30f69215ac7596754603b6fb17cec7393936a1bf..ed51d91186e40bf5ac08d7b4e2f2ceacfaae039e 100644 --- a/services/media_engine/plugins/source/http_source/hls/playlist_downloader.h +++ b/services/media_engine/plugins/source/http_source/hls/playlist_downloader.h @@ -41,7 +41,7 @@ struct PlayListChangeCallback { virtual void OnSourceKeyChange(const uint8_t* key, size_t keyLen, const uint8_t* iv) = 0; virtual void OnDrmInfoChanged(const std::multimap>& drmInfos) = 0; }; -class PlayListDownloader { +class PlayListDownloader : public std::enable_shared_from_this { public: explicit PlayListDownloader( const std::map& httpHeader = std::map(), diff --git a/services/media_engine/plugins/source/http_source/http/http_media_downloader.cpp b/services/media_engine/plugins/source/http_source/http/http_media_downloader.cpp index 6f482a02293fbabadd333519928ae492f53ffe93..585b62d3f5da397d87d56e035564bed626ef1771 100644 --- a/services/media_engine/plugins/source/http_source/http/http_media_downloader.cpp +++ b/services/media_engine/plugins/source/http_source/http/http_media_downloader.cpp @@ -176,34 +176,38 @@ bool HttpMediaDownloader::Open(const std::string& url, const std::map(data), std::forward(len), + auto weakDownloader = weak_from_this(); + auto saveData = [weakDownloader] (uint8_t*&& data, uint32_t&& len, bool&& notBlock) -> uint32_t { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_V_MSG(shareDownloader != nullptr, 0u, "saveData, Http Media Downloader already destructed."); + return shareDownloader->SaveData(std::forward(data), std::forward(len), std::forward(notBlock)); }; FALSE_RETURN_V(statusCallback_ != nullptr, false); - std::weak_ptr weakThis = weak_from_this(); - auto realStatusCallback = [weakThis] (DownloadStatus&& status, std::shared_ptr& downloader, - std::shared_ptr& request) { - auto shareThis = weakThis.lock(); - FALSE_RETURN_MSG(shareThis != nullptr, "HTTP statusCallback shareThis, is nullptr!"); - shareThis->statusCallback_(status, shareThis->downloader_, std::forward(request)); + auto realStatusCallback = [weakDownloader] (DownloadStatus&& status, std::shared_ptr& downloader, + std::shared_ptr& request) { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "realStatusCb, Http Media Downloader already destructed."); + shareDownloader->statusCallback_(status, shareDownloader->downloader_, + std::forward(request)); }; - auto downloadDoneCallback = [this] (const std::string &url, const std::string& location) { - if (downloadRequest_ != nullptr && downloadRequest_->IsEos() - && (totalBits_ / BYTES_TO_BIT) >= downloadRequest_->GetFileContentLengthNoWait()) { - isDownloadFinish_ = true; + auto downloadDoneCallback = [weakDownloader] (const std::string &url, const std::string& location) { + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "downloadDoneCb, Http Media Downloader already destructed."); + if (shareDownloader->downloadRequest_ != nullptr && shareDownloader->downloadRequest_->IsEos() + && (shareDownloader->totalBits_ / BYTES_TO_BIT) >= + shareDownloader->downloadRequest_->GetFileContentLengthNoWait()) { + shareDownloader->isDownloadFinish_ = true; } - int64_t nowTime = steadyClock_.ElapsedMilliseconds(); - double downloadTime = (static_cast(nowTime) - static_cast(startDownloadTime_)) / - SECOND_TO_MILLISECONDS; + int64_t nowTime = shareDownloader->steadyClock_.ElapsedMilliseconds(); + double downloadTime = (nowTime - shareDownloader->startDownloadTime_) * 1.0 / SECOND_TO_MILLISECONDS; if (downloadTime > ZERO_THRESHOLD) { - avgDownloadSpeed_ = totalBits_ / downloadTime; + shareDownloader->avgDownloadSpeed_ = shareDownloader->totalBits_ / downloadTime; } - MEDIA_LOG_D("HTTP Download done, average download speed: " PUBLIC_LOG_D32 " bit/s", - static_cast(avgDownloadSpeed_)); - MEDIA_LOG_I("HTTP Download done, data usage: " PUBLIC_LOG_U64 " bits in " PUBLIC_LOG_D64 "ms", - totalBits_, static_cast(downloadTime * SECOND_TO_MILLISECONDS)); - HandleBuffering(); + MEDIA_LOG_I("HTTP Download done, data usage: " PUBLIC_LOG_U64 " bits in %{public}.0lf " + "ms, average download speed: %{public}.0lf bits/s", shareDownloader->totalBits_, + downloadTime * SECOND_TO_MILLISECONDS, shareDownloader->avgDownloadSpeed_); + shareDownloader->HandleBuffering(); }; RequestInfo requestInfo; requestInfo.url = defaultStream_ != nullptr ? defaultStream_->url : url; diff --git a/services/media_engine/plugins/source/http_source/monitor/download_monitor.cpp b/services/media_engine/plugins/source/http_source/monitor/download_monitor.cpp index a9815c38a0eef3a9ec5706fe4baa80d62b5cd8c0..7326a6dca4de644bf54501c571750c323fe08059 100644 --- a/services/media_engine/plugins/source/http_source/monitor/download_monitor.cpp +++ b/services/media_engine/plugins/source/http_source/monitor/download_monitor.cpp @@ -58,13 +58,14 @@ namespace { DownloadMonitor::DownloadMonitor(std::shared_ptr downloader) noexcept : downloader_(std::move(downloader)) { - auto statusCallback = [this] (DownloadStatus&& status, std::shared_ptr& downloader, + auto weakDownloader = weak_from_this(); + auto statusCallback = [weakDownloader] (DownloadStatus&& status, std::shared_ptr& downloader, std::shared_ptr& request) { - if (isClosed_) { - MEDIA_LOG_W("Downloader monitor is already closed."); - return; - } - OnDownloadStatus(std::forward(downloader), std::forward(request)); + auto shareDownloader = weakDownloader.lock(); + FALSE_RETURN_MSG(shareDownloader != nullptr, "statusCallback, Downloader monitor already destructed."); + FALSE_RETURN_MSG_W(!shareDownloader->isClosed_, "statusCallback, Downloader monitor is already closed."); + shareDownloader->OnDownloadStatus(std::forward(downloader), + std::forward(request)); }; downloader_->SetStatusCallback(statusCallback); task_ = std::make_shared(std::string("OS_HttpMonitor")); diff --git a/services/media_engine/plugins/source/http_source/monitor/download_monitor.h b/services/media_engine/plugins/source/http_source/monitor/download_monitor.h index 34396edeb32a45eb7e8576c0d2d4fec815d293b1..ae38595ee62b99c4e79ccb198da432fcff183424 100644 --- a/services/media_engine/plugins/source/http_source/monitor/download_monitor.h +++ b/services/media_engine/plugins/source/http_source/monitor/download_monitor.h @@ -40,7 +40,7 @@ struct RetryRequest { std::function function; }; -class DownloadMonitor : public MediaDownloader { +class DownloadMonitor : public MediaDownloader, public std::enable_shared_from_this { public: explicit DownloadMonitor(std::shared_ptr downloader) noexcept; ~DownloadMonitor() override = default;