From 56e0f359552f10ec4367b3dd935a03705753d725 Mon Sep 17 00:00:00 2001 From: zhangqiang Date: Thu, 1 Feb 2024 20:11:50 +0800 Subject: [PATCH] =?UTF-8?q?20240201=20=20=20soundpool=E4=B8=8Eavcodec?= =?UTF-8?q?=E5=9B=9E=E8=B0=83=E5=87=BA=E7=8E=B0=E7=A9=BA=E6=8C=87=E9=92=88?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangqiang --- frameworks/native/soundpool/sound_parser.cpp | 3 +++ frameworks/native/soundpool/sound_parser.h | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/frameworks/native/soundpool/sound_parser.cpp b/frameworks/native/soundpool/sound_parser.cpp index 8133690d2..a298132d6 100644 --- a/frameworks/native/soundpool/sound_parser.cpp +++ b/frameworks/native/soundpool/sound_parser.cpp @@ -62,6 +62,7 @@ int32_t SoundParser::DoParser() { MEDIA_LOGE("SoundParser do parser."); std::unique_lock lock(soundParserLock_); + isParsing_.store(true); DoDemuxer(&trackFormat_); DoDecode(trackFormat_); return MSERR_OK; @@ -167,6 +168,8 @@ int32_t SoundParser::SetCallback(const std::shared_ptr &call int32_t SoundParser::Release() { MEDIA_LOGI("SoundParser Release."); + std::unique_lock lock(soundParserLock_); + isParsing_.store(false); int32_t ret = MSERR_OK; if (soundParserListener_ != nullptr) soundParserListener_.reset(); if (audioDecCb_ != nullptr) { diff --git a/frameworks/native/soundpool/sound_parser.h b/frameworks/native/soundpool/sound_parser.h index 60fda165b..dd945948e 100644 --- a/frameworks/native/soundpool/sound_parser.h +++ b/frameworks/native/soundpool/sound_parser.h @@ -122,17 +122,27 @@ private: void OnSoundDecodeCompleted(const std::deque> &availableAudioBuffers) override { - std::unique_lock lock(soundParserInner_.lock()->soundParserLock_); if (!soundParserInner_.expired()) { + while (!soundParserInner_.lock()->soundParserLock_.try_lock()) { + if (!soundParserInner_.lock()->isParsing_.load()) { + return; + } + } soundData_ = availableAudioBuffers; isSoundParserCompleted_.store(true); + soundParserInner_.lock()->soundParserLock_.unlock(); } } void SetSoundBufferTotalSize(const size_t soundBufferTotalSize) override { - std::unique_lock lock(soundParserInner_.lock()->soundParserLock_); if (!soundParserInner_.expired()) { + while (!soundParserInner_.lock()->soundParserLock_.try_lock()) { + if (!soundParserInner_.lock()->isParsing_.load()) { + return; + } + } soundBufferTotalSize_ = soundBufferTotalSize; + soundParserInner_.lock()->soundParserLock_.unlock(); } } int32_t GetSoundData(std::deque> &soundData) const @@ -171,6 +181,7 @@ private: std::shared_ptr soundParserListener_; std::shared_ptr callback_ = nullptr; bool isRawFile_ = false; + std::atomic isParsing_ = false; MediaAVCodec::Format trackFormat_; -- Gitee