• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <memory>
12 #include <string>
13 
14 #include "modules/audio_coding/codecs/opus/opus_inst.h"
15 #include "modules/audio_coding/codecs/opus/opus_interface.h"
16 #include "modules/audio_coding/neteq/tools/audio_loop.h"
17 #include "rtc_base/checks.h"
18 #include "rtc_base/numerics/safe_conversions.h"
19 #include "test/gtest.h"
20 #include "test/testsupport/file_utils.h"
21 
22 namespace webrtc {
23 
24 namespace {
25 // Equivalent to SDP params
26 // {{"channel_mapping", "0,1,2,3"}, {"coupled_streams", "2"}}.
27 constexpr unsigned char kQuadChannelMapping[] = {0, 1, 2, 3};
28 constexpr int kQuadTotalStreams = 2;
29 constexpr int kQuadCoupledStreams = 2;
30 
31 constexpr unsigned char kStereoChannelMapping[] = {0, 1};
32 constexpr int kStereoTotalStreams = 1;
33 constexpr int kStereoCoupledStreams = 1;
34 
35 constexpr unsigned char kMonoChannelMapping[] = {0};
36 constexpr int kMonoTotalStreams = 1;
37 constexpr int kMonoCoupledStreams = 0;
38 
CreateSingleOrMultiStreamEncoder(WebRtcOpusEncInst ** opus_encoder,int channels,int application,bool use_multistream,int encoder_sample_rate_hz)39 void CreateSingleOrMultiStreamEncoder(WebRtcOpusEncInst** opus_encoder,
40                                       int channels,
41                                       int application,
42                                       bool use_multistream,
43                                       int encoder_sample_rate_hz) {
44   EXPECT_TRUE(channels == 1 || channels == 2 || use_multistream);
45   if (use_multistream) {
46     EXPECT_EQ(encoder_sample_rate_hz, 48000);
47     if (channels == 1) {
48       EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
49                        opus_encoder, channels, application, kMonoTotalStreams,
50                        kMonoCoupledStreams, kMonoChannelMapping));
51     } else if (channels == 2) {
52       EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
53                        opus_encoder, channels, application, kStereoTotalStreams,
54                        kStereoCoupledStreams, kStereoChannelMapping));
55     } else if (channels == 4) {
56       EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
57                        opus_encoder, channels, application, kQuadTotalStreams,
58                        kQuadCoupledStreams, kQuadChannelMapping));
59     } else {
60       EXPECT_TRUE(false) << channels;
61     }
62   } else {
63     EXPECT_EQ(0, WebRtcOpus_EncoderCreate(opus_encoder, channels, application,
64                                           encoder_sample_rate_hz));
65   }
66 }
67 
CreateSingleOrMultiStreamDecoder(WebRtcOpusDecInst ** opus_decoder,int channels,bool use_multistream,int decoder_sample_rate_hz)68 void CreateSingleOrMultiStreamDecoder(WebRtcOpusDecInst** opus_decoder,
69                                       int channels,
70                                       bool use_multistream,
71                                       int decoder_sample_rate_hz) {
72   EXPECT_TRUE(channels == 1 || channels == 2 || use_multistream);
73   if (use_multistream) {
74     EXPECT_EQ(decoder_sample_rate_hz, 48000);
75     if (channels == 1) {
76       EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(
77                        opus_decoder, channels, kMonoTotalStreams,
78                        kMonoCoupledStreams, kMonoChannelMapping));
79     } else if (channels == 2) {
80       EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(
81                        opus_decoder, channels, kStereoTotalStreams,
82                        kStereoCoupledStreams, kStereoChannelMapping));
83     } else if (channels == 4) {
84       EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(
85                        opus_decoder, channels, kQuadTotalStreams,
86                        kQuadCoupledStreams, kQuadChannelMapping));
87     } else {
88       EXPECT_TRUE(false) << channels;
89     }
90   } else {
91     EXPECT_EQ(0, WebRtcOpus_DecoderCreate(opus_decoder, channels,
92                                           decoder_sample_rate_hz));
93   }
94 }
95 
SamplesPerChannel(int sample_rate_hz,int duration_ms)96 int SamplesPerChannel(int sample_rate_hz, int duration_ms) {
97   const int samples_per_ms = rtc::CheckedDivExact(sample_rate_hz, 1000);
98   return samples_per_ms * duration_ms;
99 }
100 
101 using test::AudioLoop;
102 using ::testing::Combine;
103 using ::testing::TestWithParam;
104 using ::testing::Values;
105 
106 // Maximum number of bytes in output bitstream.
107 const size_t kMaxBytes = 2000;
108 
109 class OpusTest
110     : public TestWithParam<::testing::tuple<size_t, int, bool, int, int>> {
111  protected:
112   OpusTest() = default;
113 
114   void TestDtxEffect(bool dtx, int block_length_ms);
115 
116   void TestCbrEffect(bool dtx, int block_length_ms);
117 
118   // Prepare `speech_data_` for encoding, read from a hard-coded file.
119   // After preparation, `speech_data_.GetNextBlock()` returns a pointer to a
120   // block of `block_length_ms` milliseconds. The data is looped every
121   // `loop_length_ms` milliseconds.
122   void PrepareSpeechData(int block_length_ms, int loop_length_ms);
123 
124   int EncodeDecode(WebRtcOpusEncInst* encoder,
125                    rtc::ArrayView<const int16_t> input_audio,
126                    WebRtcOpusDecInst* decoder,
127                    int16_t* output_audio,
128                    int16_t* audio_type);
129 
130   void SetMaxPlaybackRate(WebRtcOpusEncInst* encoder,
131                           opus_int32 expect,
132                           int32_t set);
133 
134   void CheckAudioBounded(const int16_t* audio,
135                          size_t samples,
136                          size_t channels,
137                          uint16_t bound) const;
138 
139   WebRtcOpusEncInst* opus_encoder_ = nullptr;
140   WebRtcOpusDecInst* opus_decoder_ = nullptr;
141   AudioLoop speech_data_;
142   uint8_t bitstream_[kMaxBytes];
143   size_t encoded_bytes_ = 0;
144   const size_t channels_{std::get<0>(GetParam())};
145   const int application_{std::get<1>(GetParam())};
146   const bool use_multistream_{std::get<2>(GetParam())};
147   const int encoder_sample_rate_hz_{std::get<3>(GetParam())};
148   const int decoder_sample_rate_hz_{std::get<4>(GetParam())};
149 };
150 
151 }  // namespace
152 
153 // Singlestream: Try all combinations.
154 INSTANTIATE_TEST_SUITE_P(Singlestream,
155                          OpusTest,
156                          testing::Combine(testing::Values(1, 2),
157                                           testing::Values(0, 1),
158                                           testing::Values(false),
159                                           testing::Values(16000, 48000),
160                                           testing::Values(16000, 48000)));
161 
162 // Multistream: Some representative cases (only 48 kHz for now).
163 INSTANTIATE_TEST_SUITE_P(
164     Multistream,
165     OpusTest,
166     testing::Values(std::make_tuple(1, 0, true, 48000, 48000),
167                     std::make_tuple(2, 1, true, 48000, 48000),
168                     std::make_tuple(4, 0, true, 48000, 48000),
169                     std::make_tuple(4, 1, true, 48000, 48000)));
170 
PrepareSpeechData(int block_length_ms,int loop_length_ms)171 void OpusTest::PrepareSpeechData(int block_length_ms, int loop_length_ms) {
172   std::map<int, std::string> channel_to_basename = {
173       {1, "audio_coding/testfile32kHz"},
174       {2, "audio_coding/teststereo32kHz"},
175       {4, "audio_coding/speech_4_channels_48k_one_second"}};
176   std::map<int, std::string> channel_to_suffix = {
177       {1, "pcm"}, {2, "pcm"}, {4, "wav"}};
178   const std::string file_name = webrtc::test::ResourcePath(
179       channel_to_basename[channels_], channel_to_suffix[channels_]);
180   if (loop_length_ms < block_length_ms) {
181     loop_length_ms = block_length_ms;
182   }
183   const int sample_rate_khz =
184       rtc::CheckedDivExact(encoder_sample_rate_hz_, 1000);
185   EXPECT_TRUE(speech_data_.Init(file_name,
186                                 loop_length_ms * sample_rate_khz * channels_,
187                                 block_length_ms * sample_rate_khz * channels_));
188 }
189 
SetMaxPlaybackRate(WebRtcOpusEncInst * encoder,opus_int32 expect,int32_t set)190 void OpusTest::SetMaxPlaybackRate(WebRtcOpusEncInst* encoder,
191                                   opus_int32 expect,
192                                   int32_t set) {
193   opus_int32 bandwidth;
194   EXPECT_EQ(0, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, set));
195   EXPECT_EQ(0, WebRtcOpus_GetMaxPlaybackRate(opus_encoder_, &bandwidth));
196   EXPECT_EQ(expect, bandwidth);
197 }
198 
CheckAudioBounded(const int16_t * audio,size_t samples,size_t channels,uint16_t bound) const199 void OpusTest::CheckAudioBounded(const int16_t* audio,
200                                  size_t samples,
201                                  size_t channels,
202                                  uint16_t bound) const {
203   for (size_t i = 0; i < samples; ++i) {
204     for (size_t c = 0; c < channels; ++c) {
205       ASSERT_GE(audio[i * channels + c], -bound);
206       ASSERT_LE(audio[i * channels + c], bound);
207     }
208   }
209 }
210 
EncodeDecode(WebRtcOpusEncInst * encoder,rtc::ArrayView<const int16_t> input_audio,WebRtcOpusDecInst * decoder,int16_t * output_audio,int16_t * audio_type)211 int OpusTest::EncodeDecode(WebRtcOpusEncInst* encoder,
212                            rtc::ArrayView<const int16_t> input_audio,
213                            WebRtcOpusDecInst* decoder,
214                            int16_t* output_audio,
215                            int16_t* audio_type) {
216   const int input_samples_per_channel =
217       rtc::CheckedDivExact(input_audio.size(), channels_);
218   int encoded_bytes_int =
219       WebRtcOpus_Encode(encoder, input_audio.data(), input_samples_per_channel,
220                         kMaxBytes, bitstream_);
221   EXPECT_GE(encoded_bytes_int, 0);
222   encoded_bytes_ = static_cast<size_t>(encoded_bytes_int);
223   if (encoded_bytes_ != 0) {
224     int est_len = WebRtcOpus_DurationEst(decoder, bitstream_, encoded_bytes_);
225     int act_len = WebRtcOpus_Decode(decoder, bitstream_, encoded_bytes_,
226                                     output_audio, audio_type);
227     EXPECT_EQ(est_len, act_len);
228     return act_len;
229   } else {
230     int total_dtx_len = 0;
231     const int output_samples_per_channel = input_samples_per_channel *
232                                            decoder_sample_rate_hz_ /
233                                            encoder_sample_rate_hz_;
234     while (total_dtx_len < output_samples_per_channel) {
235       int est_len = WebRtcOpus_DurationEst(decoder, NULL, 0);
236       int act_len = WebRtcOpus_Decode(decoder, NULL, 0,
237                                       &output_audio[total_dtx_len * channels_],
238                                       audio_type);
239       EXPECT_EQ(est_len, act_len);
240       total_dtx_len += act_len;
241     }
242     return total_dtx_len;
243   }
244 }
245 
246 // Test if encoder/decoder can enter DTX mode properly and do not enter DTX when
247 // they should not. This test is signal dependent.
TestDtxEffect(bool dtx,int block_length_ms)248 void OpusTest::TestDtxEffect(bool dtx, int block_length_ms) {
249   PrepareSpeechData(block_length_ms, 2000);
250   const size_t input_samples =
251       rtc::CheckedDivExact(encoder_sample_rate_hz_, 1000) * block_length_ms;
252   const size_t output_samples =
253       rtc::CheckedDivExact(decoder_sample_rate_hz_, 1000) * block_length_ms;
254 
255   // Create encoder memory.
256   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
257                                    use_multistream_, encoder_sample_rate_hz_);
258   CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
259                                    decoder_sample_rate_hz_);
260 
261   // Set bitrate.
262   EXPECT_EQ(
263       0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
264 
265   // Set input audio as silence.
266   std::vector<int16_t> silence(input_samples * channels_, 0);
267 
268   // Setting DTX.
269   EXPECT_EQ(0, dtx ? WebRtcOpus_EnableDtx(opus_encoder_)
270                    : WebRtcOpus_DisableDtx(opus_encoder_));
271 
272   int16_t audio_type;
273   int16_t* output_data_decode = new int16_t[output_samples * channels_];
274 
275   for (int i = 0; i < 100; ++i) {
276     EXPECT_EQ(output_samples,
277               static_cast<size_t>(EncodeDecode(
278                   opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
279                   output_data_decode, &audio_type)));
280     // If not DTX, it should never enter DTX mode. If DTX, we do not care since
281     // whether it enters DTX depends on the signal type.
282     if (!dtx) {
283       EXPECT_GT(encoded_bytes_, 1U);
284       EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
285       EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
286       EXPECT_EQ(0, audio_type);  // Speech.
287     }
288   }
289 
290   // We input some silent segments. In DTX mode, the encoder will stop sending.
291   // However, DTX may happen after a while.
292   for (int i = 0; i < 30; ++i) {
293     EXPECT_EQ(output_samples, static_cast<size_t>(EncodeDecode(
294                                   opus_encoder_, silence, opus_decoder_,
295                                   output_data_decode, &audio_type)));
296     if (!dtx) {
297       EXPECT_GT(encoded_bytes_, 1U);
298       EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
299       EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
300       EXPECT_EQ(0, audio_type);  // Speech.
301     } else if (encoded_bytes_ == 1) {
302       EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
303       EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
304       EXPECT_EQ(2, audio_type);  // Comfort noise.
305       break;
306     }
307   }
308 
309   // When Opus is in DTX, it wakes up in a regular basis. It sends two packets,
310   // one with an arbitrary size and the other of 1-byte, then stops sending for
311   // a certain number of frames.
312 
313   // `max_dtx_frames` is the maximum number of frames Opus can stay in DTX.
314   // TODO(kwiberg): Why does this number depend on the encoding sample rate?
315   const int max_dtx_frames =
316       (encoder_sample_rate_hz_ == 16000 ? 800 : 400) / block_length_ms + 1;
317 
318   // We run `kRunTimeMs` milliseconds of pure silence.
319   const int kRunTimeMs = 4500;
320 
321   // We check that, after a `kCheckTimeMs` milliseconds (given that the CNG in
322   // Opus needs time to adapt), the absolute values of DTX decoded signal are
323   // bounded by `kOutputValueBound`.
324   const int kCheckTimeMs = 4000;
325 
326 #if defined(OPUS_FIXED_POINT)
327   // Fixed-point Opus generates a random (comfort) noise, which has a less
328   // predictable value bound than its floating-point Opus. This value depends on
329   // input signal, and the time window for checking the output values (between
330   // `kCheckTimeMs` and `kRunTimeMs`).
331   const uint16_t kOutputValueBound = 30;
332 
333 #else
334   const uint16_t kOutputValueBound = 2;
335 #endif
336 
337   int time = 0;
338   while (time < kRunTimeMs) {
339     // DTX mode is maintained for maximum `max_dtx_frames` frames.
340     int i = 0;
341     for (; i < max_dtx_frames; ++i) {
342       time += block_length_ms;
343       EXPECT_EQ(output_samples, static_cast<size_t>(EncodeDecode(
344                                     opus_encoder_, silence, opus_decoder_,
345                                     output_data_decode, &audio_type)));
346       if (dtx) {
347         if (encoded_bytes_ > 1)
348           break;
349         EXPECT_EQ(0U, encoded_bytes_)  // Send 0 byte.
350             << "Opus should have entered DTX mode.";
351         EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
352         EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
353         EXPECT_EQ(2, audio_type);  // Comfort noise.
354         if (time >= kCheckTimeMs) {
355           CheckAudioBounded(output_data_decode, output_samples, channels_,
356                             kOutputValueBound);
357         }
358       } else {
359         EXPECT_GT(encoded_bytes_, 1U);
360         EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
361         EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
362         EXPECT_EQ(0, audio_type);  // Speech.
363       }
364     }
365 
366     if (dtx) {
367       // With DTX, Opus must stop transmission for some time.
368       EXPECT_GT(i, 1);
369     }
370 
371     // We expect a normal payload.
372     EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
373     EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
374     EXPECT_EQ(0, audio_type);  // Speech.
375 
376     // Enters DTX again immediately.
377     time += block_length_ms;
378     EXPECT_EQ(output_samples, static_cast<size_t>(EncodeDecode(
379                                   opus_encoder_, silence, opus_decoder_,
380                                   output_data_decode, &audio_type)));
381     if (dtx) {
382       EXPECT_EQ(1U, encoded_bytes_);  // Send 1 byte.
383       EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
384       EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
385       EXPECT_EQ(2, audio_type);  // Comfort noise.
386       if (time >= kCheckTimeMs) {
387         CheckAudioBounded(output_data_decode, output_samples, channels_,
388                           kOutputValueBound);
389       }
390     } else {
391       EXPECT_GT(encoded_bytes_, 1U);
392       EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
393       EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
394       EXPECT_EQ(0, audio_type);  // Speech.
395     }
396   }
397 
398   silence[0] = 10000;
399   if (dtx) {
400     // Verify that encoder/decoder can jump out from DTX mode.
401     EXPECT_EQ(output_samples, static_cast<size_t>(EncodeDecode(
402                                   opus_encoder_, silence, opus_decoder_,
403                                   output_data_decode, &audio_type)));
404     EXPECT_GT(encoded_bytes_, 1U);
405     EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
406     EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
407     EXPECT_EQ(0, audio_type);  // Speech.
408   }
409 
410   // Free memory.
411   delete[] output_data_decode;
412   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
413   EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
414 }
415 
416 // Test if CBR does what we expect.
TestCbrEffect(bool cbr,int block_length_ms)417 void OpusTest::TestCbrEffect(bool cbr, int block_length_ms) {
418   PrepareSpeechData(block_length_ms, 2000);
419   const size_t output_samples =
420       rtc::CheckedDivExact(decoder_sample_rate_hz_, 1000) * block_length_ms;
421 
422   int32_t max_pkt_size_diff = 0;
423   int32_t prev_pkt_size = 0;
424 
425   // Create encoder memory.
426   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
427                                    use_multistream_, encoder_sample_rate_hz_);
428   CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
429                                    decoder_sample_rate_hz_);
430 
431   // Set bitrate.
432   EXPECT_EQ(
433       0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
434 
435   // Setting CBR.
436   EXPECT_EQ(0, cbr ? WebRtcOpus_EnableCbr(opus_encoder_)
437                    : WebRtcOpus_DisableCbr(opus_encoder_));
438 
439   int16_t audio_type;
440   std::vector<int16_t> audio_out(output_samples * channels_);
441   for (int i = 0; i < 100; ++i) {
442     EXPECT_EQ(output_samples,
443               static_cast<size_t>(
444                   EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
445                                opus_decoder_, audio_out.data(), &audio_type)));
446 
447     if (prev_pkt_size > 0) {
448       int32_t diff = std::abs((int32_t)encoded_bytes_ - prev_pkt_size);
449       max_pkt_size_diff = std::max(max_pkt_size_diff, diff);
450     }
451     prev_pkt_size = rtc::checked_cast<int32_t>(encoded_bytes_);
452   }
453 
454   if (cbr) {
455     EXPECT_EQ(max_pkt_size_diff, 0);
456   } else {
457     EXPECT_GT(max_pkt_size_diff, 0);
458   }
459 
460   // Free memory.
461   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
462   EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
463 }
464 
465 // Test failing Create.
TEST(OpusTest,OpusCreateFail)466 TEST(OpusTest, OpusCreateFail) {
467   WebRtcOpusEncInst* opus_encoder;
468   WebRtcOpusDecInst* opus_decoder;
469 
470   // Test to see that an invalid pointer is caught.
471   EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(NULL, 1, 0, 48000));
472   // Invalid channel number.
473   EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 257, 0, 48000));
474   // Invalid applciation mode.
475   EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 1, 2, 48000));
476   // Invalid sample rate.
477   EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 1, 0, 12345));
478 
479   EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(NULL, 1, 48000));
480   // Invalid channel number.
481   EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(&opus_decoder, 257, 48000));
482   // Invalid sample rate.
483   EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(&opus_decoder, 1, 12345));
484 }
485 
486 // Test failing Free.
TEST(OpusTest,OpusFreeFail)487 TEST(OpusTest, OpusFreeFail) {
488   // Test to see that an invalid pointer is caught.
489   EXPECT_EQ(-1, WebRtcOpus_EncoderFree(NULL));
490   EXPECT_EQ(-1, WebRtcOpus_DecoderFree(NULL));
491 }
492 
493 // Test normal Create and Free.
TEST_P(OpusTest,OpusCreateFree)494 TEST_P(OpusTest, OpusCreateFree) {
495   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
496                                    use_multistream_, encoder_sample_rate_hz_);
497   CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
498                                    decoder_sample_rate_hz_);
499   EXPECT_TRUE(opus_encoder_ != NULL);
500   EXPECT_TRUE(opus_decoder_ != NULL);
501   // Free encoder and decoder memory.
502   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
503   EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
504 }
505 
506 #define ENCODER_CTL(inst, vargs)               \
507   inst->encoder                                \
508       ? opus_encoder_ctl(inst->encoder, vargs) \
509       : opus_multistream_encoder_ctl(inst->multistream_encoder, vargs)
510 
TEST_P(OpusTest,OpusEncodeDecode)511 TEST_P(OpusTest, OpusEncodeDecode) {
512   PrepareSpeechData(20, 20);
513 
514   // Create encoder memory.
515   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
516                                    use_multistream_, encoder_sample_rate_hz_);
517   CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
518                                    decoder_sample_rate_hz_);
519 
520   // Set bitrate.
521   EXPECT_EQ(
522       0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
523 
524   // Check number of channels for decoder.
525   EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
526 
527   // Check application mode.
528   opus_int32 app;
529   ENCODER_CTL(opus_encoder_, OPUS_GET_APPLICATION(&app));
530   EXPECT_EQ(application_ == 0 ? OPUS_APPLICATION_VOIP : OPUS_APPLICATION_AUDIO,
531             app);
532 
533   // Encode & decode.
534   int16_t audio_type;
535   const int decode_samples_per_channel =
536       SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/20);
537   int16_t* output_data_decode =
538       new int16_t[decode_samples_per_channel * channels_];
539   EXPECT_EQ(decode_samples_per_channel,
540             EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
541                          opus_decoder_, output_data_decode, &audio_type));
542 
543   // Free memory.
544   delete[] output_data_decode;
545   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
546   EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
547 }
548 
TEST_P(OpusTest,OpusSetBitRate)549 TEST_P(OpusTest, OpusSetBitRate) {
550   // Test without creating encoder memory.
551   EXPECT_EQ(-1, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
552 
553   // Create encoder memory, try with different bitrates.
554   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
555                                    use_multistream_, encoder_sample_rate_hz_);
556   EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 30000));
557   EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
558   EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 300000));
559   EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 600000));
560 
561   // Free memory.
562   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
563 }
564 
TEST_P(OpusTest,OpusSetComplexity)565 TEST_P(OpusTest, OpusSetComplexity) {
566   // Test without creating encoder memory.
567   EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 9));
568 
569   // Create encoder memory, try with different complexities.
570   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
571                                    use_multistream_, encoder_sample_rate_hz_);
572 
573   EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 0));
574   EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 10));
575   EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 11));
576 
577   // Free memory.
578   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
579 }
580 
TEST_P(OpusTest,OpusSetBandwidth)581 TEST_P(OpusTest, OpusSetBandwidth) {
582   if (channels_ > 2) {
583     // TODO(webrtc:10217): investigate why multi-stream Opus reports
584     // narrowband when it's configured with FULLBAND.
585     return;
586   }
587   PrepareSpeechData(20, 20);
588 
589   int16_t audio_type;
590   const int decode_samples_per_channel =
591       SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/20);
592   std::unique_ptr<int16_t[]> output_data_decode(
593       new int16_t[decode_samples_per_channel * channels_]());
594 
595   // Test without creating encoder memory.
596   EXPECT_EQ(-1,
597             WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
598   EXPECT_EQ(-1, WebRtcOpus_GetBandwidth(opus_encoder_));
599 
600   // Create encoder memory, try with different bandwidths.
601   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
602                                    use_multistream_, encoder_sample_rate_hz_);
603   CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
604                                    decoder_sample_rate_hz_);
605 
606   EXPECT_EQ(-1, WebRtcOpus_SetBandwidth(opus_encoder_,
607                                         OPUS_BANDWIDTH_NARROWBAND - 1));
608   EXPECT_EQ(0,
609             WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
610   EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
611                output_data_decode.get(), &audio_type);
612   EXPECT_EQ(OPUS_BANDWIDTH_NARROWBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
613   EXPECT_EQ(0, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND));
614   EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
615                output_data_decode.get(), &audio_type);
616   EXPECT_EQ(encoder_sample_rate_hz_ == 16000 ? OPUS_BANDWIDTH_WIDEBAND
617                                              : OPUS_BANDWIDTH_FULLBAND,
618             WebRtcOpus_GetBandwidth(opus_encoder_));
619   EXPECT_EQ(
620       -1, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND + 1));
621   EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
622                output_data_decode.get(), &audio_type);
623   EXPECT_EQ(encoder_sample_rate_hz_ == 16000 ? OPUS_BANDWIDTH_WIDEBAND
624                                              : OPUS_BANDWIDTH_FULLBAND,
625             WebRtcOpus_GetBandwidth(opus_encoder_));
626 
627   // Free memory.
628   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
629   EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
630 }
631 
TEST_P(OpusTest,OpusForceChannels)632 TEST_P(OpusTest, OpusForceChannels) {
633   // Test without creating encoder memory.
634   EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
635 
636   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
637                                    use_multistream_, encoder_sample_rate_hz_);
638   ASSERT_NE(nullptr, opus_encoder_);
639 
640   if (channels_ >= 2) {
641     EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 3));
642     EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
643     EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
644     EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
645   } else {
646     EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
647     EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
648     EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
649   }
650 
651   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
652 }
653 
654 // Encode and decode one frame, initialize the decoder and
655 // decode once more.
TEST_P(OpusTest,OpusDecodeInit)656 TEST_P(OpusTest, OpusDecodeInit) {
657   PrepareSpeechData(20, 20);
658 
659   // Create encoder memory.
660   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
661                                    use_multistream_, encoder_sample_rate_hz_);
662   CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
663                                    decoder_sample_rate_hz_);
664 
665   // Encode & decode.
666   int16_t audio_type;
667   const int decode_samples_per_channel =
668       SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/20);
669   int16_t* output_data_decode =
670       new int16_t[decode_samples_per_channel * channels_];
671   EXPECT_EQ(decode_samples_per_channel,
672             EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
673                          opus_decoder_, output_data_decode, &audio_type));
674 
675   WebRtcOpus_DecoderInit(opus_decoder_);
676 
677   EXPECT_EQ(decode_samples_per_channel,
678             WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_,
679                               output_data_decode, &audio_type));
680 
681   // Free memory.
682   delete[] output_data_decode;
683   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
684   EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
685 }
686 
TEST_P(OpusTest,OpusEnableDisableFec)687 TEST_P(OpusTest, OpusEnableDisableFec) {
688   // Test without creating encoder memory.
689   EXPECT_EQ(-1, WebRtcOpus_EnableFec(opus_encoder_));
690   EXPECT_EQ(-1, WebRtcOpus_DisableFec(opus_encoder_));
691 
692   // Create encoder memory.
693   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
694                                    use_multistream_, encoder_sample_rate_hz_);
695 
696   EXPECT_EQ(0, WebRtcOpus_EnableFec(opus_encoder_));
697   EXPECT_EQ(0, WebRtcOpus_DisableFec(opus_encoder_));
698 
699   // Free memory.
700   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
701 }
702 
TEST_P(OpusTest,OpusEnableDisableDtx)703 TEST_P(OpusTest, OpusEnableDisableDtx) {
704   // Test without creating encoder memory.
705   EXPECT_EQ(-1, WebRtcOpus_EnableDtx(opus_encoder_));
706   EXPECT_EQ(-1, WebRtcOpus_DisableDtx(opus_encoder_));
707 
708   // Create encoder memory.
709   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
710                                    use_multistream_, encoder_sample_rate_hz_);
711 
712   opus_int32 dtx;
713 
714   // DTX is off by default.
715   ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
716   EXPECT_EQ(0, dtx);
717 
718   // Test to enable DTX.
719   EXPECT_EQ(0, WebRtcOpus_EnableDtx(opus_encoder_));
720   ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
721   EXPECT_EQ(1, dtx);
722 
723   // Test to disable DTX.
724   EXPECT_EQ(0, WebRtcOpus_DisableDtx(opus_encoder_));
725   ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
726   EXPECT_EQ(0, dtx);
727 
728   // Free memory.
729   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
730 }
731 
TEST_P(OpusTest,OpusDtxOff)732 TEST_P(OpusTest, OpusDtxOff) {
733   TestDtxEffect(false, 10);
734   TestDtxEffect(false, 20);
735   TestDtxEffect(false, 40);
736 }
737 
TEST_P(OpusTest,OpusDtxOn)738 TEST_P(OpusTest, OpusDtxOn) {
739   if (channels_ > 2) {
740     // TODO(webrtc:10218): adapt the test to the sizes and order of multi-stream
741     // DTX packets.
742     return;
743   }
744   TestDtxEffect(true, 10);
745   TestDtxEffect(true, 20);
746   TestDtxEffect(true, 40);
747 }
748 
TEST_P(OpusTest,OpusCbrOff)749 TEST_P(OpusTest, OpusCbrOff) {
750   TestCbrEffect(false, 10);
751   TestCbrEffect(false, 20);
752   TestCbrEffect(false, 40);
753 }
754 
TEST_P(OpusTest,OpusCbrOn)755 TEST_P(OpusTest, OpusCbrOn) {
756   TestCbrEffect(true, 10);
757   TestCbrEffect(true, 20);
758   TestCbrEffect(true, 40);
759 }
760 
TEST_P(OpusTest,OpusSetPacketLossRate)761 TEST_P(OpusTest, OpusSetPacketLossRate) {
762   // Test without creating encoder memory.
763   EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
764 
765   // Create encoder memory.
766   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
767                                    use_multistream_, encoder_sample_rate_hz_);
768 
769   EXPECT_EQ(0, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
770   EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, -1));
771   EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 101));
772 
773   // Free memory.
774   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
775 }
776 
TEST_P(OpusTest,OpusSetMaxPlaybackRate)777 TEST_P(OpusTest, OpusSetMaxPlaybackRate) {
778   // Test without creating encoder memory.
779   EXPECT_EQ(-1, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, 20000));
780 
781   // Create encoder memory.
782   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
783                                    use_multistream_, encoder_sample_rate_hz_);
784 
785   SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 48000);
786   SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 24001);
787   SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 24000);
788   SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 16001);
789   SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 16000);
790   SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 12001);
791   SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 12000);
792   SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 8001);
793   SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 8000);
794   SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 4000);
795 
796   // Free memory.
797   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
798 }
799 
800 // Test PLC.
TEST_P(OpusTest,OpusDecodePlc)801 TEST_P(OpusTest, OpusDecodePlc) {
802   PrepareSpeechData(20, 20);
803 
804   // Create encoder memory.
805   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
806                                    use_multistream_, encoder_sample_rate_hz_);
807   CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
808                                    decoder_sample_rate_hz_);
809 
810   // Set bitrate.
811   EXPECT_EQ(
812       0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
813 
814   // Check number of channels for decoder.
815   EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
816 
817   // Encode & decode.
818   int16_t audio_type;
819   const int decode_samples_per_channel =
820       SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/20);
821   int16_t* output_data_decode =
822       new int16_t[decode_samples_per_channel * channels_];
823   EXPECT_EQ(decode_samples_per_channel,
824             EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
825                          opus_decoder_, output_data_decode, &audio_type));
826 
827   // Call decoder PLC.
828   constexpr int kPlcDurationMs = 10;
829   const int plc_samples = decoder_sample_rate_hz_ * kPlcDurationMs / 1000;
830   int16_t* plc_buffer = new int16_t[plc_samples * channels_];
831   EXPECT_EQ(plc_samples,
832             WebRtcOpus_Decode(opus_decoder_, NULL, 0, plc_buffer, &audio_type));
833 
834   // Free memory.
835   delete[] plc_buffer;
836   delete[] output_data_decode;
837   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
838   EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
839 }
840 
841 // Duration estimation.
TEST_P(OpusTest,OpusDurationEstimation)842 TEST_P(OpusTest, OpusDurationEstimation) {
843   PrepareSpeechData(20, 20);
844 
845   // Create.
846   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
847                                    use_multistream_, encoder_sample_rate_hz_);
848   CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
849                                    decoder_sample_rate_hz_);
850 
851   // 10 ms. We use only first 10 ms of a 20 ms block.
852   auto speech_block = speech_data_.GetNextBlock();
853   int encoded_bytes_int = WebRtcOpus_Encode(
854       opus_encoder_, speech_block.data(),
855       rtc::CheckedDivExact(speech_block.size(), 2 * channels_), kMaxBytes,
856       bitstream_);
857   EXPECT_GE(encoded_bytes_int, 0);
858   EXPECT_EQ(SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/10),
859             WebRtcOpus_DurationEst(opus_decoder_, bitstream_,
860                                    static_cast<size_t>(encoded_bytes_int)));
861 
862   // 20 ms
863   speech_block = speech_data_.GetNextBlock();
864   encoded_bytes_int =
865       WebRtcOpus_Encode(opus_encoder_, speech_block.data(),
866                         rtc::CheckedDivExact(speech_block.size(), channels_),
867                         kMaxBytes, bitstream_);
868   EXPECT_GE(encoded_bytes_int, 0);
869   EXPECT_EQ(SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/20),
870             WebRtcOpus_DurationEst(opus_decoder_, bitstream_,
871                                    static_cast<size_t>(encoded_bytes_int)));
872 
873   // Free memory.
874   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
875   EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
876 }
877 
TEST_P(OpusTest,OpusDecodeRepacketized)878 TEST_P(OpusTest, OpusDecodeRepacketized) {
879   if (channels_ > 2) {
880     // As per the Opus documentation
881     // https://mf4.xiph.org/jenkins/view/opus/job/opus/ws/doc/html/group__opus__repacketizer.html#details,
882     // multiple streams are not supported.
883     return;
884   }
885   constexpr size_t kPackets = 6;
886 
887   PrepareSpeechData(20, 20 * kPackets);
888 
889   // Create encoder memory.
890   CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
891                                    use_multistream_, encoder_sample_rate_hz_);
892   ASSERT_NE(nullptr, opus_encoder_);
893   CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
894                                    decoder_sample_rate_hz_);
895   ASSERT_NE(nullptr, opus_decoder_);
896 
897   // Set bitrate.
898   EXPECT_EQ(
899       0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
900 
901   // Check number of channels for decoder.
902   EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
903 
904   // Encode & decode.
905   int16_t audio_type;
906   const int decode_samples_per_channel =
907       SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/20);
908   std::unique_ptr<int16_t[]> output_data_decode(
909       new int16_t[kPackets * decode_samples_per_channel * channels_]);
910   OpusRepacketizer* rp = opus_repacketizer_create();
911 
912   size_t num_packets = 0;
913   constexpr size_t kMaxCycles = 100;
914   for (size_t idx = 0; idx < kMaxCycles; ++idx) {
915     auto speech_block = speech_data_.GetNextBlock();
916     encoded_bytes_ =
917         WebRtcOpus_Encode(opus_encoder_, speech_block.data(),
918                           rtc::CheckedDivExact(speech_block.size(), channels_),
919                           kMaxBytes, bitstream_);
920     if (opus_repacketizer_cat(rp, bitstream_,
921                               rtc::checked_cast<opus_int32>(encoded_bytes_)) ==
922         OPUS_OK) {
923       ++num_packets;
924       if (num_packets == kPackets) {
925         break;
926       }
927     } else {
928       // Opus repacketizer cannot guarantee a success. We try again if it fails.
929       opus_repacketizer_init(rp);
930       num_packets = 0;
931     }
932   }
933   EXPECT_EQ(kPackets, num_packets);
934 
935   encoded_bytes_ = opus_repacketizer_out(rp, bitstream_, kMaxBytes);
936 
937   EXPECT_EQ(decode_samples_per_channel * kPackets,
938             static_cast<size_t>(WebRtcOpus_DurationEst(
939                 opus_decoder_, bitstream_, encoded_bytes_)));
940 
941   EXPECT_EQ(decode_samples_per_channel * kPackets,
942             static_cast<size_t>(
943                 WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_,
944                                   output_data_decode.get(), &audio_type)));
945 
946   // Free memory.
947   opus_repacketizer_destroy(rp);
948   EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
949   EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
950 }
951 
TEST(OpusVadTest,CeltUnknownStatus)952 TEST(OpusVadTest, CeltUnknownStatus) {
953   const uint8_t celt[] = {0x80};
954   EXPECT_EQ(WebRtcOpus_PacketHasVoiceActivity(celt, 1), -1);
955 }
956 
TEST(OpusVadTest,Mono20msVadSet)957 TEST(OpusVadTest, Mono20msVadSet) {
958   uint8_t silk20msMonoVad[] = {0x78, 0x80};
959   EXPECT_TRUE(WebRtcOpus_PacketHasVoiceActivity(silk20msMonoVad, 2));
960 }
961 
TEST(OpusVadTest,Mono20MsVadUnset)962 TEST(OpusVadTest, Mono20MsVadUnset) {
963   uint8_t silk20msMonoSilence[] = {0x78, 0x00};
964   EXPECT_FALSE(WebRtcOpus_PacketHasVoiceActivity(silk20msMonoSilence, 2));
965 }
966 
TEST(OpusVadTest,Stereo20MsVadOnSideChannel)967 TEST(OpusVadTest, Stereo20MsVadOnSideChannel) {
968   uint8_t silk20msStereoVadSideChannel[] = {0x78 | 0x04, 0x20};
969   EXPECT_TRUE(
970       WebRtcOpus_PacketHasVoiceActivity(silk20msStereoVadSideChannel, 2));
971 }
972 
TEST(OpusVadTest,TwoOpusMonoFramesVadOnSecond)973 TEST(OpusVadTest, TwoOpusMonoFramesVadOnSecond) {
974   uint8_t twoMonoFrames[] = {0x78 | 0x1, 0x00, 0x80};
975   EXPECT_TRUE(WebRtcOpus_PacketHasVoiceActivity(twoMonoFrames, 3));
976 }
977 
978 }  // namespace webrtc
979