Skip to content

Commit

Permalink
Cherry pick PR #2727: [media] Revise OpusAudioDecoder::Reset() via OP…
Browse files Browse the repository at this point in the history
…US_RESET_STATE (#2945)

Refer to the original PR: #2727

This PR (#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

Co-authored-by: Bo-Rong Chen <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and borongc authored Apr 17, 2024
1 parent b6b8811 commit 4336918
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions starboard/shared/opus/opus_audio_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -218,8 +221,9 @@ void OpusAudioDecoder::InitializeCodec() {
}

void OpusAudioDecoder::TeardownCodec() {
if (decoder_) {
if (is_valid()) {
opus_multistream_decoder_destroy(decoder_);
decoder_ = NULL;
}
}

Expand All @@ -241,9 +245,19 @@ scoped_refptr<OpusAudioDecoder::DecodedAudio> 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();
Expand Down

0 comments on commit 4336918

Please sign in to comment.