Make Opus PLC always output 10ms audio.

BUG: b/143582588
Change-Id: I41ad5f4f91d9af3f595666a8f32b7ab5382605bd
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/158672
Commit-Queue: Minyue Li <minyue@webrtc.org>
Reviewed-by: Jakob Ivarsson <jakobi@webrtc.org>
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29733}
diff --git a/modules/audio_coding/codecs/opus/opus_fec_test.cc b/modules/audio_coding/codecs/opus/opus_fec_test.cc
index 47e40c6..1923647 100644
--- a/modules/audio_coding/codecs/opus/opus_fec_test.cc
+++ b/modules/audio_coding/codecs/opus/opus_fec_test.cc
@@ -154,8 +154,13 @@
           WebRtcOpus_DecodeFec(opus_decoder_, &bit_stream_[0], encoded_bytes_,
                                &out_data_[0], &audio_type);
     } else {
-      value_1 =
-          WebRtcOpus_Decode(opus_decoder_, NULL, 0, &out_data_[0], &audio_type);
+      // Call decoder PLC.
+      while (value_1 < static_cast<int>(block_length_sample_)) {
+        int ret = WebRtcOpus_Decode(opus_decoder_, NULL, 0, &out_data_[value_1],
+                                    &audio_type);
+        EXPECT_EQ(ret, sampling_khz_ * 10);  // Should return 10 ms of samples.
+        value_1 += ret;
+      }
     }
     EXPECT_EQ(static_cast<int>(block_length_sample_), value_1);
   }
diff --git a/modules/audio_coding/codecs/opus/opus_inst.h b/modules/audio_coding/codecs/opus/opus_inst.h
index 9c3acb3..148baa2 100644
--- a/modules/audio_coding/codecs/opus/opus_inst.h
+++ b/modules/audio_coding/codecs/opus/opus_inst.h
@@ -31,6 +31,7 @@
   OpusDecoder* decoder;
   OpusMSDecoder* multistream_decoder;
   int prev_decoded_samples;
+  bool plc_use_prev_decoded_samples;
   size_t channels;
   int in_dtx_mode;
   int sample_rate_hz;
diff --git a/modules/audio_coding/codecs/opus/opus_interface.cc b/modules/audio_coding/codecs/opus/opus_interface.cc
index fc3d3ff..2f475cb 100644
--- a/modules/audio_coding/codecs/opus/opus_interface.cc
+++ b/modules/audio_coding/codecs/opus/opus_interface.cc
@@ -11,6 +11,7 @@
 #include "modules/audio_coding/codecs/opus/opus_interface.h"
 
 #include "rtc_base/checks.h"
+#include "system_wrappers/include/field_trial.h"
 
 enum {
 #if WEBRTC_OPUS_SUPPORT_120MS_PTIME
@@ -25,8 +26,14 @@
    * side, we must allow for packets of that size. NetEq is currently limited
    * to 60 ms on the receive side. */
   kWebRtcOpusMaxDecodeFrameSizeMs = 120,
+
+  // Duration of audio that each call to packet loss concealment covers.
+  kWebRtcOpusPlcFrameSizeMs = 10,
 };
 
+constexpr char kPlcUsePrevDecodedSamplesFieldTrial[] =
+    "WebRTC-Audio-OpusPlcUsePrevDecodedSamples";
+
 static int FrameSizePerChannel(int frame_size_ms, int sample_rate_hz) {
   RTC_DCHECK_GT(frame_size_ms, 0);
   RTC_DCHECK_EQ(frame_size_ms % 10, 0);
@@ -381,9 +388,14 @@
     if (error == OPUS_OK && state->decoder) {
       // Creation of memory all ok.
       state->channels = channels;
-      state->prev_decoded_samples = DefaultFrameSizePerChannel(sample_rate_hz);
-      state->in_dtx_mode = 0;
       state->sample_rate_hz = sample_rate_hz;
+      state->plc_use_prev_decoded_samples =
+          webrtc::field_trial::IsEnabled(kPlcUsePrevDecodedSamplesFieldTrial);
+      if (state->plc_use_prev_decoded_samples) {
+        state->prev_decoded_samples =
+            DefaultFrameSizePerChannel(state->sample_rate_hz);
+      }
+      state->in_dtx_mode = 0;
       *inst = state;
       return 0;
     }
@@ -420,9 +432,14 @@
     if (error == OPUS_OK && state->multistream_decoder) {
       // Creation of memory all ok.
       state->channels = channels;
-      state->prev_decoded_samples = DefaultFrameSizePerChannel(48000);
-      state->in_dtx_mode = 0;
       state->sample_rate_hz = 48000;
+      state->plc_use_prev_decoded_samples =
+          webrtc::field_trial::IsEnabled(kPlcUsePrevDecodedSamplesFieldTrial);
+      if (state->plc_use_prev_decoded_samples) {
+        state->prev_decoded_samples =
+            DefaultFrameSizePerChannel(state->sample_rate_hz);
+      }
+      state->in_dtx_mode = 0;
       *inst = state;
       return 0;
     }
@@ -517,17 +534,20 @@
 static int DecodePlc(OpusDecInst* inst, int16_t* decoded) {
   int16_t audio_type = 0;
   int decoded_samples;
-  int plc_samples;
+  int plc_samples =
+      FrameSizePerChannel(kWebRtcOpusPlcFrameSizeMs, inst->sample_rate_hz);
 
-  /* The number of samples we ask for is |number_of_lost_frames| times
-   * |prev_decoded_samples_|. Limit the number of samples to maximum
-   * |MaxFrameSizePerChannel()|. */
-  plc_samples = inst->prev_decoded_samples;
-  const int max_samples_per_channel =
-      MaxFrameSizePerChannel(inst->sample_rate_hz);
-  plc_samples = plc_samples <= max_samples_per_channel
-                    ? plc_samples
-                    : max_samples_per_channel;
+  if (inst->plc_use_prev_decoded_samples) {
+    /* The number of samples we ask for is |number_of_lost_frames| times
+     * |prev_decoded_samples_|. Limit the number of samples to maximum
+     * |MaxFrameSizePerChannel()|. */
+    plc_samples = inst->prev_decoded_samples;
+    const int max_samples_per_channel =
+        MaxFrameSizePerChannel(inst->sample_rate_hz);
+    plc_samples = plc_samples <= max_samples_per_channel
+                      ? plc_samples
+                      : max_samples_per_channel;
+  }
   decoded_samples =
       DecodeNative(inst, NULL, 0, plc_samples, decoded, &audio_type, 0);
   if (decoded_samples < 0) {
@@ -556,8 +576,10 @@
     return -1;
   }
 
-  /* Update decoded sample memory, to be used by the PLC in case of losses. */
-  inst->prev_decoded_samples = decoded_samples;
+  if (inst->plc_use_prev_decoded_samples) {
+    /* Update decoded sample memory, to be used by the PLC in case of losses. */
+    inst->prev_decoded_samples = decoded_samples;
+  }
 
   return decoded_samples;
 }
@@ -612,14 +634,17 @@
 }
 
 int WebRtcOpus_PlcDuration(OpusDecInst* inst) {
-  /* The number of samples we ask for is |number_of_lost_frames| times
-   * |prev_decoded_samples_|. Limit the number of samples to maximum
-   * |MaxFrameSizePerChannel()|. */
-  const int plc_samples = inst->prev_decoded_samples;
-  const int max_samples_per_channel =
-      MaxFrameSizePerChannel(inst->sample_rate_hz);
-  return plc_samples <= max_samples_per_channel ? plc_samples
-                                                : max_samples_per_channel;
+  if (inst->plc_use_prev_decoded_samples) {
+    /* The number of samples we ask for is |number_of_lost_frames| times
+     * |prev_decoded_samples_|. Limit the number of samples to maximum
+     * |MaxFrameSizePerChannel()|. */
+    const int plc_samples = inst->prev_decoded_samples;
+    const int max_samples_per_channel =
+        MaxFrameSizePerChannel(inst->sample_rate_hz);
+    return plc_samples <= max_samples_per_channel ? plc_samples
+                                                  : max_samples_per_channel;
+  }
+  return FrameSizePerChannel(kWebRtcOpusPlcFrameSizeMs, inst->sample_rate_hz);
 }
 
 int WebRtcOpus_FecDurationEst(const uint8_t* payload,
diff --git a/modules/audio_coding/codecs/opus/opus_unittest.cc b/modules/audio_coding/codecs/opus/opus_unittest.cc
index 10897fb..0cc4f25 100644
--- a/modules/audio_coding/codecs/opus/opus_unittest.cc
+++ b/modules/audio_coding/codecs/opus/opus_unittest.cc
@@ -213,17 +213,34 @@
                            WebRtcOpusDecInst* decoder,
                            int16_t* output_audio,
                            int16_t* audio_type) {
+  const int input_samples_per_channel =
+      rtc::CheckedDivExact(input_audio.size(), channels_);
   int encoded_bytes_int =
-      WebRtcOpus_Encode(encoder, input_audio.data(),
-                        rtc::CheckedDivExact(input_audio.size(), channels_),
+      WebRtcOpus_Encode(encoder, input_audio.data(), input_samples_per_channel,
                         kMaxBytes, bitstream_);
   EXPECT_GE(encoded_bytes_int, 0);
   encoded_bytes_ = static_cast<size_t>(encoded_bytes_int);
-  int est_len = WebRtcOpus_DurationEst(decoder, bitstream_, encoded_bytes_);
-  int act_len = WebRtcOpus_Decode(decoder, bitstream_, encoded_bytes_,
-                                  output_audio, audio_type);
-  EXPECT_EQ(est_len, act_len);
-  return act_len;
+  if (encoded_bytes_ != 0) {
+    int est_len = WebRtcOpus_DurationEst(decoder, bitstream_, encoded_bytes_);
+    int act_len = WebRtcOpus_Decode(decoder, bitstream_, encoded_bytes_,
+                                    output_audio, audio_type);
+    EXPECT_EQ(est_len, act_len);
+    return act_len;
+  } else {
+    int total_dtx_len = 0;
+    const int output_samples_per_channel = input_samples_per_channel *
+                                           decoder_sample_rate_hz_ /
+                                           encoder_sample_rate_hz_;
+    while (total_dtx_len < output_samples_per_channel) {
+      int est_len = WebRtcOpus_DurationEst(decoder, NULL, 0);
+      int act_len = WebRtcOpus_Decode(decoder, NULL, 0,
+                                      &output_audio[total_dtx_len * channels_],
+                                      audio_type);
+      EXPECT_EQ(est_len, act_len);
+      total_dtx_len += act_len;
+    }
+    return total_dtx_len;
+  }
 }
 
 // Test if encoder/decoder can enter DTX mode properly and do not enter DTX when
@@ -808,8 +825,10 @@
                          opus_decoder_, output_data_decode, &audio_type));
 
   // Call decoder PLC.
-  int16_t* plc_buffer = new int16_t[decode_samples_per_channel * channels_];
-  EXPECT_EQ(decode_samples_per_channel,
+  constexpr int kPlcDurationMs = 10;
+  const int plc_samples = decoder_sample_rate_hz_ * kPlcDurationMs / 1000;
+  int16_t* plc_buffer = new int16_t[plc_samples * channels_];
+  EXPECT_EQ(plc_samples,
             WebRtcOpus_Decode(opus_decoder_, NULL, 0, plc_buffer, &audio_type));
 
   // Free memory.