From 02dc352332e38f52f2aacb272e65f063995f98db Mon Sep 17 00:00:00 2001 From: Bo-Rong Chen Date: Mon, 1 Apr 2024 14:02:06 -0700 Subject: [PATCH] [media] Fix reset AudioDecoder twice when destroying AudioDecoder (#2788) This PR (https://github.com/youtube/cobalt/pull/2501) changes to recreate or flush AudioDecoder in AudioDecoder::Reset(). However, when destroying AdaptiveAudioDecoder, this causes Cobalt to destroy->create->destroy AudioDecoder, which is unnecessary. This PR fixes the extra reset when destroying AudioDecoder. b/327229953 --- .../filter/adaptive_audio_decoder_internal.cc | 29 ++++++++++++------- .../filter/adaptive_audio_decoder_internal.h | 1 + 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/starboard/shared/starboard/player/filter/adaptive_audio_decoder_internal.cc b/starboard/shared/starboard/player/filter/adaptive_audio_decoder_internal.cc index 0c341ea3e912..40c77b2cf2c8 100644 --- a/starboard/shared/starboard/player/filter/adaptive_audio_decoder_internal.cc +++ b/starboard/shared/starboard/player/filter/adaptive_audio_decoder_internal.cc @@ -44,7 +44,12 @@ AdaptiveAudioDecoder::AdaptiveAudioDecoder( } AdaptiveAudioDecoder::~AdaptiveAudioDecoder() { - Reset(); + SB_DCHECK(BelongsToCurrentThread()); + + if (audio_decoder_) { + TeardownAudioDecoder(); + } + ResetInternal(); } void AdaptiveAudioDecoder::Initialize(const OutputCB& output_cb, @@ -150,15 +155,7 @@ void AdaptiveAudioDecoder::Reset() { resampler_.reset(); channel_mixer_.reset(); } - CancelPendingJobs(); - while (!decoded_audios_.empty()) { - decoded_audios_.pop(); - } - pending_input_buffers_.clear(); - pending_consumed_cb_ = nullptr; - flushing_ = false; - stream_ended_ = false; - first_output_received_ = false; + ResetInternal(); } void AdaptiveAudioDecoder::InitializeAudioDecoder( @@ -188,6 +185,18 @@ void AdaptiveAudioDecoder::TeardownAudioDecoder() { channel_mixer_.reset(); } +void AdaptiveAudioDecoder::ResetInternal() { + CancelPendingJobs(); + while (!decoded_audios_.empty()) { + decoded_audios_.pop(); + } + pending_input_buffers_.clear(); + pending_consumed_cb_ = nullptr; + flushing_ = false; + stream_ended_ = false; + first_output_received_ = false; +} + void AdaptiveAudioDecoder::OnDecoderOutput() { if (!BelongsToCurrentThread()) { Schedule(std::bind(&AdaptiveAudioDecoder::OnDecoderOutput, this)); diff --git a/starboard/shared/starboard/player/filter/adaptive_audio_decoder_internal.h b/starboard/shared/starboard/player/filter/adaptive_audio_decoder_internal.h index 2fc9964f1d50..f128e8826127 100644 --- a/starboard/shared/starboard/player/filter/adaptive_audio_decoder_internal.h +++ b/starboard/shared/starboard/player/filter/adaptive_audio_decoder_internal.h @@ -65,6 +65,7 @@ class AdaptiveAudioDecoder : public AudioDecoder, private JobQueue::JobOwner { void InitializeAudioDecoder(const media::AudioStreamInfo& audio_stream_info); void TeardownAudioDecoder(); void OnDecoderOutput(); + void ResetInternal(); const uint32_t initial_samples_per_second_; const SbDrmSystem drm_system_;