Skip to content

Commit

Permalink
[media] Fix OpusAudioDecoder::Reset() on PartialAudio tests
Browse files Browse the repository at this point in the history
This PR (#2501) recreating OpusAUdioDecoder in Reset(). Change to reset a previously initialized state using the #OPUS_RESET_STATE CTL as stated in the opus document.

b/327281974
  • Loading branch information
borongc committed Mar 27, 2024
1 parent 12f1046 commit 530907d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
65 changes: 33 additions & 32 deletions starboard/shared/opus/opus_audio_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,31 @@ static const VorbisLayout vorbis_mappings[8] = {

OpusAudioDecoder::OpusAudioDecoder(const AudioStreamInfo& audio_stream_info)
: audio_stream_info_(audio_stream_info) {
InitializeCodec();
int error;
int channels = audio_stream_info_.number_of_channels;
if (channels > 8 || channels < 1) {
SB_LOG(ERROR) << "Can't create decoder with " << channels << " channels";
return;

Check warning on line 54 in starboard/shared/opus/opus_audio_decoder.cc

View check run for this annotation

Codecov / codecov/patch

starboard/shared/opus/opus_audio_decoder.cc#L53-L54

Added lines #L53 - L54 were not covered by tests
}

decoder_ = opus_multistream_decoder_create(
audio_stream_info_.samples_per_second, channels,
vorbis_mappings[channels - 1].nb_streams,
vorbis_mappings[channels - 1].nb_coupled_streams,
vorbis_mappings[channels - 1].mapping, &error);
if (error != OPUS_OK) {
SB_LOG(ERROR) << "Failed to create decoder with error: "
<< opus_strerror(error);
decoder_ = NULL;
return;

Check warning on line 66 in starboard/shared/opus/opus_audio_decoder.cc

View check run for this annotation

Codecov / codecov/patch

starboard/shared/opus/opus_audio_decoder.cc#L63-L66

Added lines #L63 - L66 were not covered by tests
}
SB_DCHECK(decoder_ != NULL);
}

OpusAudioDecoder::~OpusAudioDecoder() {
TeardownCodec();
if (decoder_) {
opus_multistream_decoder_destroy(decoder_);
}
}

void OpusAudioDecoder::Initialize(const OutputCB& output_cb,
Expand Down Expand Up @@ -195,34 +215,6 @@ void OpusAudioDecoder::WriteEndOfStream() {
Schedule(output_cb_);
}

void OpusAudioDecoder::InitializeCodec() {
int error;
int channels = audio_stream_info_.number_of_channels;
if (channels > 8 || channels < 1) {
SB_LOG(ERROR) << "Can't create decoder with " << channels << " channels";
return;
}

decoder_ = opus_multistream_decoder_create(
audio_stream_info_.samples_per_second, channels,
vorbis_mappings[channels - 1].nb_streams,
vorbis_mappings[channels - 1].nb_coupled_streams,
vorbis_mappings[channels - 1].mapping, &error);
if (error != OPUS_OK) {
SB_LOG(ERROR) << "Failed to create decoder with error: "
<< opus_strerror(error);
decoder_ = NULL;
return;
}
SB_DCHECK(decoder_ != NULL);
}

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

scoped_refptr<OpusAudioDecoder::DecodedAudio> OpusAudioDecoder::Read(
int* samples_per_second) {
SB_DCHECK(BelongsToCurrentThread());
Expand All @@ -241,9 +233,18 @@ scoped_refptr<OpusAudioDecoder::DecodedAudio> OpusAudioDecoder::Read(
void OpusAudioDecoder::Reset() {
SB_DCHECK(BelongsToCurrentThread());

TeardownCodec();
InitializeCodec();
if (decoder_) {
int error = opus_multistream_decoder_ctl(decoder_, OPUS_RESET_STATE);
if (error != OPUS_OK) {
SB_LOG(ERROR) << "Failed to reset decoder with error: "
<< opus_strerror(error);
decoder_ = NULL;
return;

Check warning on line 242 in starboard/shared/opus/opus_audio_decoder.cc

View check run for this annotation

Codecov / codecov/patch

starboard/shared/opus/opus_audio_decoder.cc#L239-L242

Added lines #L239 - L242 were not covered by tests
}
SB_DCHECK(decoder_ != NULL);
}

frames_per_au_ = kMaxOpusFramesPerAU;
stream_ended_ = false;
while (!decoded_audios_.empty()) {
decoded_audios_.pop();
Expand Down
2 changes: 0 additions & 2 deletions starboard/shared/opus/opus_audio_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class OpusAudioDecoder
private:
static constexpr int kMinimumBuffersToDecode = 2;

void InitializeCodec();
void TeardownCodec();
void DecodePendingBuffers();
bool DecodeInternal(const scoped_refptr<InputBuffer>& input_buffer);
static const int kMaxOpusFramesPerAU = 9600;
Expand Down

0 comments on commit 530907d

Please sign in to comment.