From 8a15dd858a41b141a03e3c541affcc8d68441466 Mon Sep 17 00:00:00 2001 From: Bo-Rong Chen Date: Thu, 11 Apr 2024 16:40:13 -0700 Subject: [PATCH] [media] Revise OpusAudioDecoder::Reset() via OPUS_RESET_STATE (#2727) This PR (https://github.com/youtube/cobalt/pull/2501) recreates OpusAudioDecoder in OpusAudioDecoder::Reset(), as originally Cobalt recreates OpusAudioDecoder on PartialAudio tests. Change to reset a previously initialized state using the #OPUS_RESET_STATE CTL as stated in the opus document. b/327281974 --- starboard/shared/opus/opus_audio_decoder.cc | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/starboard/shared/opus/opus_audio_decoder.cc b/starboard/shared/opus/opus_audio_decoder.cc index 85ba47e6eb1f..fa8e00a69e2f 100644 --- a/starboard/shared/opus/opus_audio_decoder.cc +++ b/starboard/shared/opus/opus_audio_decoder.cc @@ -51,6 +51,9 @@ OpusAudioDecoder::OpusAudioDecoder(const AudioStreamInfo& audio_stream_info) } OpusAudioDecoder::~OpusAudioDecoder() { + if (is_valid()) { + opus_multistream_decoder_ctl(decoder_, OPUS_RESET_STATE); + } TeardownCodec(); } @@ -218,8 +221,9 @@ void OpusAudioDecoder::InitializeCodec() { } void OpusAudioDecoder::TeardownCodec() { - if (decoder_) { + if (is_valid()) { opus_multistream_decoder_destroy(decoder_); + decoder_ = NULL; } } @@ -241,9 +245,19 @@ scoped_refptr OpusAudioDecoder::Read( void OpusAudioDecoder::Reset() { SB_DCHECK(BelongsToCurrentThread()); - TeardownCodec(); - InitializeCodec(); + if (is_valid()) { + int error = opus_multistream_decoder_ctl(decoder_, OPUS_RESET_STATE); + if (error != OPUS_OK) { + SB_LOG(ERROR) << "Failed to reset OpusAudioDecoder with error: " + << opus_strerror(error); + + // If fail to reset opus decoder, re-create it. + TeardownCodec(); + InitializeCodec(); + } + } + frames_per_au_ = kMaxOpusFramesPerAU; stream_ended_ = false; while (!decoded_audios_.empty()) { decoded_audios_.pop();