diff --git a/frameworks/native/soundpool/sound_parser.cpp b/frameworks/native/soundpool/sound_parser.cpp index 8133690d282205b261085d03ef1d9681e1107524..a298132d6c13ea75f5a318347951f521068da3e7 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 60fda165bf89dc4fa2354d8a01bd41b9316339fc..dd945948ec81a88a36994c70c369cb68f6fdf70c 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_;