From 0d1d188a89a31c7800cb69e295f65048432f8116 Mon Sep 17 00:00:00 2001 From: lintaicheng Date: Thu, 11 Sep 2025 15:44:41 +0800 Subject: [PATCH] fix hls crash Signed-off-by: lintaicheng --- .../http_source/hls/hls_media_downloader.cpp | 32 ++++++++++++------- .../http_source/hls/hls_media_downloader.h | 2 ++ 2 files changed, 23 insertions(+), 11 deletions(-) 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 efc472f85..6d4fdb61a 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 @@ -204,8 +204,11 @@ void HlsMediaDownloader::PutRequestIntoDownloader(const PlayInfo& playInfo) requestInfo.url = playInfo.rangeUrl_.empty() ? playInfo.url_ : playInfo.rangeUrl_; requestInfo.httpHeader = httpHeader_; bool isRequestWholeFile = playInfo.rangeUrl_.empty() ? true : playInfo.length_ <= 0; - downloadRequest_ = std::make_shared(playInfo.duration_, dataSave_, - realStatusCallback, requestInfo, isRequestWholeFile); + { + AutoLock lock(dlRequestMutex_); + downloadRequest_ = std::make_shared(playInfo.duration_, dataSave_, + realStatusCallback, requestInfo, isRequestWholeFile); + } fragmentDownloadStart[playInfo.url_] = true; int64_t startTimePos = playInfo.startTimePos_; curUrl_ = playInfo.rangeUrl_.empty() ? playInfo.url_ : playInfo.rangeUrl_; @@ -319,12 +322,16 @@ void HlsMediaDownloader::Resume() bool HlsMediaDownloader::CheckReadStatus() { // eos:palylist is empty, request is finished, hls is vod, do not select bitrate + bool isEos = false; FALSE_RETURN_V(playlistDownloader_ != nullptr, false); - FALSE_RETURN_V(downloadRequest_ != nullptr, false); - bool isEos = playList_->Empty() && (downloadRequest_ != nullptr) && - downloadRequest_->IsEos() && playlistDownloader_ != nullptr && - (playlistDownloader_->GetDuration() > 0) && - playlistDownloader_->IsParseAndNotifyFinished(); + { + AutoLock lock(dlRequestMutex_); + FALSE_RETURN_V(downloadRequest_ != nullptr, false); + isEos = playList_->Empty() && (downloadRequest_ != nullptr) && + downloadRequest_->IsEos() && playlistDownloader_ != nullptr && + (playlistDownloader_->GetDuration() > 0) && + playlistDownloader_->IsParseAndNotifyFinished(); + } if (isEos) { MEDIA_LOG_I("HLS download done."); return true; @@ -707,6 +714,7 @@ Status HlsMediaDownloader::Read(unsigned char* buff, ReadDataInfo& readDataInfo) HandleFfmpegReadback(readDataInfo.ffmpegOffset); + AutoLock lock(cacheMutex_); auto ret = ReadDelegate(buff, readDataInfo); uint64_t freeSize = cacheMediaBuffer_->GetFreeSize(); @@ -755,10 +763,12 @@ void HlsMediaDownloader::PrepareToSeek() playList_->Clear(); downloader_->Cancel(); - - cacheMediaBuffer_.reset(); - cacheMediaBuffer_ = std::make_shared(); - cacheMediaBuffer_->Init(totalBufferSize_, CHUNK_SIZE); + { + AutoLock lock(cacheMutex_); + cacheMediaBuffer_.reset(); + cacheMediaBuffer_ = std::make_shared(); + cacheMediaBuffer_->Init(totalBufferSize_, CHUNK_SIZE); + } memorySize_ = totalBufferSize_; canWrite_ = true; diff --git a/services/media_engine/plugins/source/http_source/hls/hls_media_downloader.h b/services/media_engine/plugins/source/http_source/hls/hls_media_downloader.h index 6ce2f8678..4cb3f0553 100644 --- a/services/media_engine/plugins/source/http_source/hls/hls_media_downloader.h +++ b/services/media_engine/plugins/source/http_source/hls/hls_media_downloader.h @@ -292,6 +292,8 @@ private: uint64_t lastCachedSize_ {0}; std::atomic isBufferingStart_ {false}; std::shared_ptr cacheMediaBuffer_; + Mutex cacheMutex_ {}; + Mutex dlRequestMutex_ {}; uint64_t readOffset_ {0}; uint64_t writeOffset_ {0}; std::map> tsStorageInfo_ {}; -- Gitee