diff --git a/starboard/shared/opus/opus_audio_decoder.cc b/starboard/shared/opus/opus_audio_decoder.cc index 85ba47e6eb1f..f345f70e9cd5 100644 --- a/starboard/shared/opus/opus_audio_decoder.cc +++ b/starboard/shared/opus/opus_audio_decoder.cc @@ -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; + } + + 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); } OpusAudioDecoder::~OpusAudioDecoder() { - TeardownCodec(); + if (decoder_) { + opus_multistream_decoder_destroy(decoder_); + } } void OpusAudioDecoder::Initialize(const OutputCB& output_cb, @@ -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::Read( int* samples_per_second) { SB_DCHECK(BelongsToCurrentThread()); @@ -241,9 +233,18 @@ scoped_refptr 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; + } + SB_DCHECK(decoder_ != NULL); + } + frames_per_au_ = kMaxOpusFramesPerAU; stream_ended_ = false; while (!decoded_audios_.empty()) { decoded_audios_.pop(); diff --git a/starboard/shared/opus/opus_audio_decoder.h b/starboard/shared/opus/opus_audio_decoder.h index f16ba245ff9b..7716cc75e064 100644 --- a/starboard/shared/opus/opus_audio_decoder.h +++ b/starboard/shared/opus/opus_audio_decoder.h @@ -54,8 +54,6 @@ class OpusAudioDecoder private: static constexpr int kMinimumBuffersToDecode = 2; - void InitializeCodec(); - void TeardownCodec(); void DecodePendingBuffers(); bool DecodeInternal(const scoped_refptr& input_buffer); static const int kMaxOpusFramesPerAU = 9600;