AudioDecoder: Replace Init() with Reset()

The Init() method was previously used to initialize and reset
decoders, and returned an error code. The new Reset() method is used
for reset only; the constructor is now responsible for fully
initializing the AudioDecoder.

Reset() doesn't return an error code; it turned out that none of the
functions it ended up calling could actually fail, so this CL removes
their error return codes as well.

R=henrik.lundin@webrtc.org

Review URL: https://codereview.webrtc.org/1319683002 .

Cr-Commit-Position: refs/heads/master@{#9798}
diff --git a/webrtc/modules/audio_coding/neteq/audio_decoder_impl.cc b/webrtc/modules/audio_coding/neteq/audio_decoder_impl.cc
index 769f0b0..592f17b 100644
--- a/webrtc/modules/audio_coding/neteq/audio_decoder_impl.cc
+++ b/webrtc/modules/audio_coding/neteq/audio_decoder_impl.cc
@@ -39,8 +39,7 @@
 
 // PCMu
 
-int AudioDecoderPcmU::Init() {
-  return 0;
+void AudioDecoderPcmU::Reset() {
 }
 size_t AudioDecoderPcmU::Channels() const {
   return 1;
@@ -70,8 +69,7 @@
 
 // PCMa
 
-int AudioDecoderPcmA::Init() {
-  return 0;
+void AudioDecoderPcmA::Reset() {
 }
 size_t AudioDecoderPcmA::Channels() const {
   return 1;
@@ -103,8 +101,7 @@
 #ifdef WEBRTC_CODEC_PCM16
 AudioDecoderPcm16B::AudioDecoderPcm16B() {}
 
-int AudioDecoderPcm16B::Init() {
-  return 0;
+void AudioDecoderPcm16B::Reset() {
 }
 size_t AudioDecoderPcm16B::Channels() const {
   return 1;
@@ -143,6 +140,7 @@
 #ifdef WEBRTC_CODEC_ILBC
 AudioDecoderIlbc::AudioDecoderIlbc() {
   WebRtcIlbcfix_DecoderCreate(&dec_state_);
+  WebRtcIlbcfix_Decoderinit30Ms(dec_state_);
 }
 
 AudioDecoderIlbc::~AudioDecoderIlbc() {
@@ -170,8 +168,8 @@
   return WebRtcIlbcfix_NetEqPlc(dec_state_, decoded, num_frames);
 }
 
-int AudioDecoderIlbc::Init() {
-  return WebRtcIlbcfix_Decoderinit30Ms(dec_state_);
+void AudioDecoderIlbc::Reset() {
+  WebRtcIlbcfix_Decoderinit30Ms(dec_state_);
 }
 
 size_t AudioDecoderIlbc::Channels() const {
@@ -183,6 +181,7 @@
 #ifdef WEBRTC_CODEC_G722
 AudioDecoderG722::AudioDecoderG722() {
   WebRtcG722_CreateDecoder(&dec_state_);
+  WebRtcG722_DecoderInit(dec_state_);
 }
 
 AudioDecoderG722::~AudioDecoderG722() {
@@ -206,8 +205,8 @@
   return static_cast<int>(ret);
 }
 
-int AudioDecoderG722::Init() {
-  return WebRtcG722_DecoderInit(dec_state_);
+void AudioDecoderG722::Reset() {
+  WebRtcG722_DecoderInit(dec_state_);
 }
 
 int AudioDecoderG722::PacketDuration(const uint8_t* encoded,
@@ -223,6 +222,8 @@
 AudioDecoderG722Stereo::AudioDecoderG722Stereo() {
   WebRtcG722_CreateDecoder(&dec_state_left_);
   WebRtcG722_CreateDecoder(&dec_state_right_);
+  WebRtcG722_DecoderInit(dec_state_left_);
+  WebRtcG722_DecoderInit(dec_state_right_);
 }
 
 AudioDecoderG722Stereo::~AudioDecoderG722Stereo() {
@@ -265,11 +266,9 @@
   return 2;
 }
 
-int AudioDecoderG722Stereo::Init() {
-  int r = WebRtcG722_DecoderInit(dec_state_left_);
-  if (r != 0)
-    return r;
-  return WebRtcG722_DecoderInit(dec_state_right_);
+void AudioDecoderG722Stereo::Reset() {
+  WebRtcG722_DecoderInit(dec_state_left_);
+  WebRtcG722_DecoderInit(dec_state_right_);
 }
 
 // Split the stereo packet and place left and right channel after each other
@@ -306,6 +305,7 @@
     : channels_(num_channels) {
   DCHECK(num_channels == 1 || num_channels == 2);
   WebRtcOpus_DecoderCreate(&dec_state_, static_cast<int>(channels_));
+  WebRtcOpus_DecoderInit(dec_state_);
 }
 
 AudioDecoderOpus::~AudioDecoderOpus() {
@@ -348,8 +348,8 @@
   return ret;
 }
 
-int AudioDecoderOpus::Init() {
-  return WebRtcOpus_DecoderInit(dec_state_);
+void AudioDecoderOpus::Reset() {
+  WebRtcOpus_DecoderInit(dec_state_);
 }
 
 int AudioDecoderOpus::PacketDuration(const uint8_t* encoded,
@@ -381,14 +381,15 @@
 
 AudioDecoderCng::AudioDecoderCng() {
   CHECK_EQ(0, WebRtcCng_CreateDec(&dec_state_));
+  WebRtcCng_InitDec(dec_state_);
 }
 
 AudioDecoderCng::~AudioDecoderCng() {
   WebRtcCng_FreeDec(dec_state_);
 }
 
-int AudioDecoderCng::Init() {
-  return WebRtcCng_InitDec(dec_state_);
+void AudioDecoderCng::Reset() {
+  WebRtcCng_InitDec(dec_state_);
 }
 
 int AudioDecoderCng::IncomingPacket(const uint8_t* payload,
diff --git a/webrtc/modules/audio_coding/neteq/audio_decoder_impl.h b/webrtc/modules/audio_coding/neteq/audio_decoder_impl.h
index 427a0a6..f2ca711 100644
--- a/webrtc/modules/audio_coding/neteq/audio_decoder_impl.h
+++ b/webrtc/modules/audio_coding/neteq/audio_decoder_impl.h
@@ -37,7 +37,7 @@
 class AudioDecoderPcmU : public AudioDecoder {
  public:
   AudioDecoderPcmU() {}
-  int Init() override;
+  void Reset() override;
   int PacketDuration(const uint8_t* encoded, size_t encoded_len) const override;
   size_t Channels() const override;
 
@@ -55,7 +55,7 @@
 class AudioDecoderPcmA : public AudioDecoder {
  public:
   AudioDecoderPcmA() {}
-  int Init() override;
+  void Reset() override;
   int PacketDuration(const uint8_t* encoded, size_t encoded_len) const override;
   size_t Channels() const override;
 
@@ -102,7 +102,7 @@
 class AudioDecoderPcm16B : public AudioDecoder {
  public:
   AudioDecoderPcm16B();
-  int Init() override;
+  void Reset() override;
   int PacketDuration(const uint8_t* encoded, size_t encoded_len) const override;
   size_t Channels() const override;
 
@@ -138,7 +138,7 @@
   ~AudioDecoderIlbc() override;
   bool HasDecodePlc() const override;
   size_t DecodePlc(size_t num_frames, int16_t* decoded) override;
-  int Init() override;
+  void Reset() override;
   size_t Channels() const override;
 
  protected:
@@ -160,7 +160,7 @@
   AudioDecoderG722();
   ~AudioDecoderG722() override;
   bool HasDecodePlc() const override;
-  int Init() override;
+  void Reset() override;
   int PacketDuration(const uint8_t* encoded, size_t encoded_len) const override;
   size_t Channels() const override;
 
@@ -180,7 +180,7 @@
  public:
   AudioDecoderG722Stereo();
   ~AudioDecoderG722Stereo() override;
-  int Init() override;
+  void Reset() override;
 
  protected:
   int DecodeInternal(const uint8_t* encoded,
@@ -212,7 +212,7 @@
   explicit AudioDecoderOpus(size_t num_channels);
   ~AudioDecoderOpus() override;
 
-  int Init() override;
+  void Reset() override;
   int PacketDuration(const uint8_t* encoded, size_t encoded_len) const override;
   int PacketDurationRedundant(const uint8_t* encoded,
                               size_t encoded_len) const override;
@@ -248,7 +248,7 @@
  public:
   explicit AudioDecoderCng();
   ~AudioDecoderCng() override;
-  int Init() override;
+  void Reset() override;
   int IncomingPacket(const uint8_t* payload,
                      size_t payload_len,
                      uint16_t rtp_sequence_number,
diff --git a/webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc b/webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc
index a2ef9d1..392e3dc 100644
--- a/webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc
+++ b/webrtc/modules/audio_coding/neteq/audio_decoder_unittest.cc
@@ -171,7 +171,6 @@
     size_t processed_samples = 0u;
     encoded_bytes_ = 0u;
     InitEncoder();
-    EXPECT_EQ(0, decoder_->Init());
     std::vector<int16_t> input;
     std::vector<int16_t> decoded;
     while (processed_samples + frame_size_ <= data_length_) {
@@ -220,7 +219,7 @@
     size_t enc_len = EncodeFrame(input.get(), frame_size_, encoded_);
     size_t dec_len;
     AudioDecoder::SpeechType speech_type1, speech_type2;
-    EXPECT_EQ(0, decoder_->Init());
+    decoder_->Reset();
     rtc::scoped_ptr<int16_t[]> output1(new int16_t[frame_size_ * channels_]);
     dec_len = decoder_->Decode(encoded_, enc_len, codec_input_rate_hz_,
                                frame_size_ * channels_ * sizeof(int16_t),
@@ -228,7 +227,7 @@
     ASSERT_LE(dec_len, frame_size_ * channels_);
     EXPECT_EQ(frame_size_ * channels_, dec_len);
     // Re-init decoder and decode again.
-    EXPECT_EQ(0, decoder_->Init());
+    decoder_->Reset();
     rtc::scoped_ptr<int16_t[]> output2(new int16_t[frame_size_ * channels_]);
     dec_len = decoder_->Decode(encoded_, enc_len, codec_input_rate_hz_,
                                frame_size_ * channels_ * sizeof(int16_t),
@@ -249,7 +248,7 @@
         input_audio_.Read(frame_size_, codec_input_rate_hz_, input.get()));
     size_t enc_len = EncodeFrame(input.get(), frame_size_, encoded_);
     AudioDecoder::SpeechType speech_type;
-    EXPECT_EQ(0, decoder_->Init());
+    decoder_->Reset();
     rtc::scoped_ptr<int16_t[]> output(new int16_t[frame_size_ * channels_]);
     size_t dec_len = decoder_->Decode(encoded_, enc_len, codec_input_rate_hz_,
                                       frame_size_ * channels_ * sizeof(int16_t),
@@ -341,7 +340,7 @@
         input_audio_.Read(frame_size_, codec_input_rate_hz_, input.get()));
     size_t enc_len = EncodeFrame(input.get(), frame_size_, encoded_);
     AudioDecoder::SpeechType speech_type;
-    EXPECT_EQ(0, decoder_->Init());
+    decoder_->Reset();
     rtc::scoped_ptr<int16_t[]> output(new int16_t[frame_size_ * channels_]);
     size_t dec_len = decoder_->Decode(encoded_, enc_len, codec_input_rate_hz_,
                                       frame_size_ * channels_ * sizeof(int16_t),
diff --git a/webrtc/modules/audio_coding/neteq/decoder_database.cc b/webrtc/modules/audio_coding/neteq/decoder_database.cc
index 18eee06..97dc00d 100644
--- a/webrtc/modules/audio_coding/neteq/decoder_database.cc
+++ b/webrtc/modules/audio_coding/neteq/decoder_database.cc
@@ -72,7 +72,6 @@
   if (!decoder) {
     return kInvalidPointer;
   }
-  decoder->Init();
   std::pair<DecoderMap::iterator, bool> ret;
   DecoderInfo info(codec_type, fs_hz, decoder, true);
   ret = decoders_.insert(std::make_pair(rtp_payload_type, info));
@@ -136,7 +135,6 @@
     AudioDecoder* decoder = CreateAudioDecoder(info->codec_type);
     assert(decoder);  // Should not be able to have an unsupported codec here.
     info->decoder = decoder;
-    info->decoder->Init();
   }
   return info->decoder;
 }
diff --git a/webrtc/modules/audio_coding/neteq/mock/mock_audio_decoder.h b/webrtc/modules/audio_coding/neteq/mock/mock_audio_decoder.h
index d26e2a1..90f132b 100644
--- a/webrtc/modules/audio_coding/neteq/mock/mock_audio_decoder.h
+++ b/webrtc/modules/audio_coding/neteq/mock/mock_audio_decoder.h
@@ -27,7 +27,7 @@
       int(const uint8_t*, size_t, int, size_t, int16_t*, SpeechType*));
   MOCK_CONST_METHOD0(HasDecodePlc, bool());
   MOCK_METHOD2(DecodePlc, size_t(size_t, int16_t*));
-  MOCK_METHOD0(Init, int());
+  MOCK_METHOD0(Reset, void());
   MOCK_METHOD5(IncomingPacket, int(const uint8_t*, size_t, uint16_t, uint32_t,
                                    uint32_t));
   MOCK_METHOD0(ErrorCode, int());
diff --git a/webrtc/modules/audio_coding/neteq/mock/mock_external_decoder_pcm16b.h b/webrtc/modules/audio_coding/neteq/mock/mock_external_decoder_pcm16b.h
index f239b4a..fca1c2d 100644
--- a/webrtc/modules/audio_coding/neteq/mock/mock_external_decoder_pcm16b.h
+++ b/webrtc/modules/audio_coding/neteq/mock/mock_external_decoder_pcm16b.h
@@ -28,7 +28,7 @@
 class ExternalPcm16B : public AudioDecoder {
  public:
   ExternalPcm16B() {}
-  virtual int Init() { return 0; }
+  void Reset() override {}
 
  protected:
   int DecodeInternal(const uint8_t* encoded,
@@ -58,8 +58,8 @@
         .WillByDefault(Invoke(&real_, &ExternalPcm16B::HasDecodePlc));
     ON_CALL(*this, DecodePlc(_, _))
         .WillByDefault(Invoke(&real_, &ExternalPcm16B::DecodePlc));
-    ON_CALL(*this, Init())
-        .WillByDefault(Invoke(&real_, &ExternalPcm16B::Init));
+    ON_CALL(*this, Reset())
+        .WillByDefault(Invoke(&real_, &ExternalPcm16B::Reset));
     ON_CALL(*this, IncomingPacket(_, _, _, _, _))
         .WillByDefault(Invoke(&real_, &ExternalPcm16B::IncomingPacket));
     ON_CALL(*this, ErrorCode())
@@ -79,8 +79,7 @@
       bool());
   MOCK_METHOD2(DecodePlc,
       size_t(size_t num_frames, int16_t* decoded));
-  MOCK_METHOD0(Init,
-      int());
+  MOCK_METHOD0(Reset, void());
   MOCK_METHOD5(IncomingPacket,
       int(const uint8_t* payload, size_t payload_len,
           uint16_t rtp_sequence_number, uint32_t rtp_timestamp,
diff --git a/webrtc/modules/audio_coding/neteq/neteq_external_decoder_unittest.cc b/webrtc/modules/audio_coding/neteq/neteq_external_decoder_unittest.cc
index 3c945f9..2a11616 100644
--- a/webrtc/modules/audio_coding/neteq/neteq_external_decoder_unittest.cc
+++ b/webrtc/modules/audio_coding/neteq/neteq_external_decoder_unittest.cc
@@ -40,8 +40,6 @@
         payload_size_bytes_(0),
         last_send_time_(0),
         last_arrival_time_(0) {
-    // Init() will trigger external_decoder_->Init().
-    EXPECT_CALL(*external_decoder_, Init());
     // NetEq is not allowed to delete the external decoder (hence Times(0)).
     EXPECT_CALL(*external_decoder_, Die()).Times(0);
     Init();
diff --git a/webrtc/modules/audio_coding/neteq/neteq_impl.cc b/webrtc/modules/audio_coding/neteq/neteq_impl.cc
index 22e71f7..cf7afbc 100644
--- a/webrtc/modules/audio_coding/neteq/neteq_impl.cc
+++ b/webrtc/modules/audio_coding/neteq/neteq_impl.cc
@@ -1178,15 +1178,14 @@
 
   if (reset_decoder_) {
     // TODO(hlundin): Write test for this.
-    // Reset decoder.
-    if (decoder) {
-      decoder->Init();
-    }
+    if (decoder)
+      decoder->Reset();
+
     // Reset comfort noise decoder.
     AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
-    if (cng_decoder) {
-      cng_decoder->Init();
-    }
+    if (cng_decoder)
+      cng_decoder->Reset();
+
     reset_decoder_ = false;
   }
 
@@ -1896,11 +1895,9 @@
     mute_factor_array_[i] = 16384;  // 1.0 in Q14.
   }
 
-  // Reset comfort noise decoder, if there is one active.
   AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
-  if (cng_decoder) {
-    cng_decoder->Init();
-  }
+  if (cng_decoder)
+    cng_decoder->Reset();
 
   // Reinit post-decode VAD with new sample rate.
   assert(vad_.get());  // Cannot be NULL here.
diff --git a/webrtc/modules/audio_coding/neteq/neteq_impl_unittest.cc b/webrtc/modules/audio_coding/neteq/neteq_impl_unittest.cc
index 006a5ad..ee04a6f 100644
--- a/webrtc/modules/audio_coding/neteq/neteq_impl_unittest.cc
+++ b/webrtc/modules/audio_coding/neteq/neteq_impl_unittest.cc
@@ -444,10 +444,7 @@
       return encoded_len;
     }
 
-    virtual int Init() {
-      next_value_ = 1;
-      return 0;
-    }
+    void Reset() override { next_value_ = 1; }
 
     size_t Channels() const override { return 1; }
 
@@ -524,7 +521,7 @@
 
   // Create a mock decoder object.
   MockAudioDecoder mock_decoder;
-  EXPECT_CALL(mock_decoder, Init()).WillRepeatedly(Return(0));
+  EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
   EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
   EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
       .WillRepeatedly(Return(0));
@@ -690,7 +687,7 @@
 
   // Create a mock decoder object.
   MockAudioDecoder mock_decoder;
-  EXPECT_CALL(mock_decoder, Init()).WillRepeatedly(Return(0));
+  EXPECT_CALL(mock_decoder, Reset()).WillRepeatedly(Return());
   EXPECT_CALL(mock_decoder, Channels()).WillRepeatedly(Return(1));
   EXPECT_CALL(mock_decoder, IncomingPacket(_, kPayloadLengthBytes, _, _, _))
       .WillRepeatedly(Return(0));
@@ -829,9 +826,7 @@
 
   class MockAudioDecoder : public AudioDecoder {
    public:
-    int Init() override {
-      return 0;
-    }
+    void Reset() override {}
     MOCK_CONST_METHOD2(PacketDuration, int(const uint8_t*, size_t));
     MOCK_METHOD5(DecodeInternal, int(const uint8_t*, size_t, int, int16_t*,
                                      SpeechType*));
diff --git a/webrtc/modules/audio_coding/neteq/neteq_network_stats_unittest.cc b/webrtc/modules/audio_coding/neteq/neteq_network_stats_unittest.cc
index e1a0f69..139106b 100644
--- a/webrtc/modules/audio_coding/neteq/neteq_network_stats_unittest.cc
+++ b/webrtc/modules/audio_coding/neteq/neteq_network_stats_unittest.cc
@@ -33,7 +33,7 @@
   virtual ~MockAudioDecoderOpus() { Die(); }
   MOCK_METHOD0(Die, void());
 
-  MOCK_METHOD0(Init, int());
+  MOCK_METHOD0(Reset, void());
 
   int PacketDuration(const uint8_t* encoded,
                      size_t encoded_len) const override {
@@ -271,7 +271,6 @@
 
 TEST(NetEqNetworkStatsTest, OpusDecodeFec) {
   MockAudioDecoderOpus decoder(1);
-  EXPECT_CALL(decoder, Init());
   NetEqNetworkStatsTest test(kDecoderOpus, &decoder);
   test.DecodeFecTest();
   EXPECT_CALL(decoder, Die()).Times(1);
@@ -279,7 +278,6 @@
 
 TEST(NetEqNetworkStatsTest, StereoOpusDecodeFec) {
   MockAudioDecoderOpus decoder(2);
-  EXPECT_CALL(decoder, Init());
   NetEqNetworkStatsTest test(kDecoderOpus, &decoder);
   test.DecodeFecTest();
   EXPECT_CALL(decoder, Die()).Times(1);
@@ -287,7 +285,6 @@
 
 TEST(NetEqNetworkStatsTest, NoiseExpansionTest) {
   MockAudioDecoderOpus decoder(1);
-  EXPECT_CALL(decoder, Init());
   NetEqNetworkStatsTest test(kDecoderOpus, &decoder);
   test.NoiseExpansionTest();
   EXPECT_CALL(decoder, Die()).Times(1);