blob: 034f8cd2037f3f155827ba9272c42c1ad6227f94 [file] [log] [blame]
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +00001/*
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 */
kwiberg91d97562016-02-14 01:10:03 -080010
11#include <memory>
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000012#include <string>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#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"
Karl Wiberge40468b2017-11-22 10:42:26 +010018#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "test/gtest.h"
20#include "test/testsupport/fileutils.h"
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000021
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000022namespace webrtc {
23
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000024using test::AudioLoop;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000025using ::testing::TestWithParam;
26using ::testing::Values;
27using ::testing::Combine;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000028
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000029// Maximum number of bytes in output bitstream.
pbos@webrtc.org3004c792013-05-07 12:36:21 +000030const size_t kMaxBytes = 1000;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000031// Sample rate of Opus.
Peter Kastingdce40cf2015-08-24 14:52:23 -070032const size_t kOpusRateKhz = 48;
minyue@webrtc.orgf563e852014-07-18 21:11:27 +000033// Number of samples-per-channel in a 20 ms frame, sampled at 48 kHz.
Peter Kastingdce40cf2015-08-24 14:52:23 -070034const size_t kOpus20msFrameSamples = kOpusRateKhz * 20;
minyue@webrtc.orgf563e852014-07-18 21:11:27 +000035// Number of samples-per-channel in a 10 ms frame, sampled at 48 kHz.
Peter Kastingdce40cf2015-08-24 14:52:23 -070036const size_t kOpus10msFrameSamples = kOpusRateKhz * 10;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000037
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000038class OpusTest : public TestWithParam<::testing::tuple<int, int>> {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000039 protected:
40 OpusTest();
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000041
minyue3cea2562015-11-10 03:49:26 -080042 void TestDtxEffect(bool dtx, int block_length_ms);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000043
soren28dc2852017-04-06 05:48:36 -070044 void TestCbrEffect(bool dtx, int block_length_ms);
45
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000046 // Prepare |speech_data_| for encoding, read from a hard-coded file.
47 // After preparation, |speech_data_.GetNextBlock()| returns a pointer to a
48 // block of |block_length_ms| milliseconds. The data is looped every
49 // |loop_length_ms| milliseconds.
Peter Kasting69558702016-01-12 16:26:35 -080050 void PrepareSpeechData(size_t channel,
51 int block_length_ms,
52 int loop_length_ms);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000053
54 int EncodeDecode(WebRtcOpusEncInst* encoder,
kwiberg288886b2015-11-06 01:21:35 -080055 rtc::ArrayView<const int16_t> input_audio,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000056 WebRtcOpusDecInst* decoder,
57 int16_t* output_audio,
58 int16_t* audio_type);
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +000059
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000060 void SetMaxPlaybackRate(WebRtcOpusEncInst* encoder,
Yves Gerey665174f2018-06-19 15:03:05 +020061 opus_int32 expect,
62 int32_t set);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000063
Yves Gerey665174f2018-06-19 15:03:05 +020064 void CheckAudioBounded(const int16_t* audio,
65 size_t samples,
66 size_t channels,
minyue3cea2562015-11-10 03:49:26 -080067 uint16_t bound) const;
68
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000069 WebRtcOpusEncInst* opus_encoder_;
70 WebRtcOpusDecInst* opus_decoder_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000071
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000072 AudioLoop speech_data_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000073 uint8_t bitstream_[kMaxBytes];
Peter Kastingdce40cf2015-08-24 14:52:23 -070074 size_t encoded_bytes_;
Peter Kasting69558702016-01-12 16:26:35 -080075 size_t channels_;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000076 int application_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000077};
78
79OpusTest::OpusTest()
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000080 : opus_encoder_(NULL),
81 opus_decoder_(NULL),
82 encoded_bytes_(0),
Peter Kasting69558702016-01-12 16:26:35 -080083 channels_(static_cast<size_t>(::testing::get<0>(GetParam()))),
Yves Gerey665174f2018-06-19 15:03:05 +020084 application_(::testing::get<1>(GetParam())) {}
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000085
Yves Gerey665174f2018-06-19 15:03:05 +020086void OpusTest::PrepareSpeechData(size_t channel,
87 int block_length_ms,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000088 int loop_length_ms) {
Yves Gerey665174f2018-06-19 15:03:05 +020089 const std::string file_name = webrtc::test::ResourcePath(
90 (channel == 1) ? "audio_coding/testfile32kHz"
91 : "audio_coding/teststereo32kHz",
92 "pcm");
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000093 if (loop_length_ms < block_length_ms) {
94 loop_length_ms = block_length_ms;
95 }
96 EXPECT_TRUE(speech_data_.Init(file_name,
97 loop_length_ms * kOpusRateKhz * channel,
98 block_length_ms * kOpusRateKhz * channel));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000099}
100
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000101void OpusTest::SetMaxPlaybackRate(WebRtcOpusEncInst* encoder,
102 opus_int32 expect,
103 int32_t set) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000104 opus_int32 bandwidth;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000105 EXPECT_EQ(0, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, set));
Yves Gerey665174f2018-06-19 15:03:05 +0200106 opus_encoder_ctl(opus_encoder_->encoder, OPUS_GET_MAX_BANDWIDTH(&bandwidth));
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000107 EXPECT_EQ(expect, bandwidth);
108}
109
Yves Gerey665174f2018-06-19 15:03:05 +0200110void OpusTest::CheckAudioBounded(const int16_t* audio,
111 size_t samples,
112 size_t channels,
113 uint16_t bound) const {
minyue3cea2562015-11-10 03:49:26 -0800114 for (size_t i = 0; i < samples; ++i) {
Peter Kasting69558702016-01-12 16:26:35 -0800115 for (size_t c = 0; c < channels; ++c) {
minyue3cea2562015-11-10 03:49:26 -0800116 ASSERT_GE(audio[i * channels + c], -bound);
117 ASSERT_LE(audio[i * channels + c], bound);
118 }
119 }
120}
121
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000122int OpusTest::EncodeDecode(WebRtcOpusEncInst* encoder,
kwiberg288886b2015-11-06 01:21:35 -0800123 rtc::ArrayView<const int16_t> input_audio,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000124 WebRtcOpusDecInst* decoder,
125 int16_t* output_audio,
126 int16_t* audio_type) {
Yves Gerey665174f2018-06-19 15:03:05 +0200127 int encoded_bytes_int =
128 WebRtcOpus_Encode(encoder, input_audio.data(),
129 rtc::CheckedDivExact(input_audio.size(), channels_),
130 kMaxBytes, bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700131 EXPECT_GE(encoded_bytes_int, 0);
132 encoded_bytes_ = static_cast<size_t>(encoded_bytes_int);
minyuel6d92bf52015-09-23 15:20:39 +0200133 int est_len = WebRtcOpus_DurationEst(decoder, bitstream_, encoded_bytes_);
Yves Gerey665174f2018-06-19 15:03:05 +0200134 int act_len = WebRtcOpus_Decode(decoder, bitstream_, encoded_bytes_,
135 output_audio, audio_type);
minyuel6d92bf52015-09-23 15:20:39 +0200136 EXPECT_EQ(est_len, act_len);
137 return act_len;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000138}
139
140// Test if encoder/decoder can enter DTX mode properly and do not enter DTX when
141// they should not. This test is signal dependent.
minyue3cea2562015-11-10 03:49:26 -0800142void OpusTest::TestDtxEffect(bool dtx, int block_length_ms) {
143 PrepareSpeechData(channels_, block_length_ms, 2000);
144 const size_t samples = kOpusRateKhz * block_length_ms;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000145
146 // Create encoder memory.
Yves Gerey665174f2018-06-19 15:03:05 +0200147 EXPECT_EQ(0,
148 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000149 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000150
151 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200152 EXPECT_EQ(
153 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000154
155 // Set input audio as silence.
minyue3cea2562015-11-10 03:49:26 -0800156 std::vector<int16_t> silence(samples * channels_, 0);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000157
158 // Setting DTX.
Yves Gerey665174f2018-06-19 15:03:05 +0200159 EXPECT_EQ(0, dtx ? WebRtcOpus_EnableDtx(opus_encoder_)
160 : WebRtcOpus_DisableDtx(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000161
162 int16_t audio_type;
minyue3cea2562015-11-10 03:49:26 -0800163 int16_t* output_data_decode = new int16_t[samples * channels_];
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000164
165 for (int i = 0; i < 100; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200166 EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode(
167 opus_encoder_, speech_data_.GetNextBlock(),
168 opus_decoder_, output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000169 // If not DTX, it should never enter DTX mode. If DTX, we do not care since
170 // whether it enters DTX depends on the signal type.
171 if (!dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700172 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000173 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
174 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000175 EXPECT_EQ(0, audio_type); // Speech.
176 }
177 }
178
179 // We input some silent segments. In DTX mode, the encoder will stop sending.
180 // However, DTX may happen after a while.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000181 for (int i = 0; i < 30; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200182 EXPECT_EQ(samples, static_cast<size_t>(
183 EncodeDecode(opus_encoder_, silence, opus_decoder_,
184 output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000185 if (!dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700186 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000187 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
188 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000189 EXPECT_EQ(0, audio_type); // Speech.
Peter Kasting728d9032015-06-11 14:31:38 -0700190 } else if (encoded_bytes_ == 1) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000191 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
192 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000193 EXPECT_EQ(2, audio_type); // Comfort noise.
194 break;
195 }
196 }
197
Minyue Li092041c2015-05-11 12:19:35 +0200198 // When Opus is in DTX, it wakes up in a regular basis. It sends two packets,
199 // one with an arbitrary size and the other of 1-byte, then stops sending for
minyue3cea2562015-11-10 03:49:26 -0800200 // a certain number of frames.
201
202 // |max_dtx_frames| is the maximum number of frames Opus can stay in DTX.
203 const int max_dtx_frames = 400 / block_length_ms + 1;
204
205 // We run |kRunTimeMs| milliseconds of pure silence.
minyue58e08cb2016-02-24 03:49:19 -0800206 const int kRunTimeMs = 4500;
minyue3cea2562015-11-10 03:49:26 -0800207
208 // We check that, after a |kCheckTimeMs| milliseconds (given that the CNG in
209 // Opus needs time to adapt), the absolute values of DTX decoded signal are
210 // bounded by |kOutputValueBound|.
minyue58e08cb2016-02-24 03:49:19 -0800211 const int kCheckTimeMs = 4000;
minyue3cea2562015-11-10 03:49:26 -0800212
213#if defined(OPUS_FIXED_POINT)
minyuel7e937e92016-02-29 10:24:15 +0100214 // Fixed-point Opus generates a random (comfort) noise, which has a less
215 // predictable value bound than its floating-point Opus. This value depends on
216 // input signal, and the time window for checking the output values (between
217 // |kCheckTimeMs| and |kRunTimeMs|).
218 const uint16_t kOutputValueBound = 30;
219
minyue3cea2562015-11-10 03:49:26 -0800220#else
minyue58e08cb2016-02-24 03:49:19 -0800221 const uint16_t kOutputValueBound = 2;
minyue3cea2562015-11-10 03:49:26 -0800222#endif
223
224 int time = 0;
225 while (time < kRunTimeMs) {
226 // DTX mode is maintained for maximum |max_dtx_frames| frames.
227 int i = 0;
228 for (; i < max_dtx_frames; ++i) {
229 time += block_length_ms;
Yves Gerey665174f2018-06-19 15:03:05 +0200230 EXPECT_EQ(samples, static_cast<size_t>(
231 EncodeDecode(opus_encoder_, silence, opus_decoder_,
232 output_data_decode, &audio_type)));
Minyue Li092041c2015-05-11 12:19:35 +0200233 if (dtx) {
minyue3cea2562015-11-10 03:49:26 -0800234 if (encoded_bytes_ > 1)
235 break;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700236 EXPECT_EQ(0U, encoded_bytes_) // Send 0 byte.
Minyue Li092041c2015-05-11 12:19:35 +0200237 << "Opus should have entered DTX mode.";
238 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
239 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
240 EXPECT_EQ(2, audio_type); // Comfort noise.
minyue3cea2562015-11-10 03:49:26 -0800241 if (time >= kCheckTimeMs) {
242 CheckAudioBounded(output_data_decode, samples, channels_,
243 kOutputValueBound);
244 }
Minyue Li092041c2015-05-11 12:19:35 +0200245 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700246 EXPECT_GT(encoded_bytes_, 1U);
Minyue Li092041c2015-05-11 12:19:35 +0200247 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
248 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
249 EXPECT_EQ(0, audio_type); // Speech.
250 }
251 }
252
minyue3cea2562015-11-10 03:49:26 -0800253 if (dtx) {
254 // With DTX, Opus must stop transmission for some time.
255 EXPECT_GT(i, 1);
256 }
Minyue Li092041c2015-05-11 12:19:35 +0200257
minyue3cea2562015-11-10 03:49:26 -0800258 // We expect a normal payload.
Minyue Li092041c2015-05-11 12:19:35 +0200259 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
260 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
261 EXPECT_EQ(0, audio_type); // Speech.
262
263 // Enters DTX again immediately.
minyue3cea2562015-11-10 03:49:26 -0800264 time += block_length_ms;
Yves Gerey665174f2018-06-19 15:03:05 +0200265 EXPECT_EQ(samples, static_cast<size_t>(
266 EncodeDecode(opus_encoder_, silence, opus_decoder_,
267 output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000268 if (dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700269 EXPECT_EQ(1U, encoded_bytes_); // Send 1 byte.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000270 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
271 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000272 EXPECT_EQ(2, audio_type); // Comfort noise.
minyue3cea2562015-11-10 03:49:26 -0800273 if (time >= kCheckTimeMs) {
274 CheckAudioBounded(output_data_decode, samples, channels_,
275 kOutputValueBound);
276 }
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000277 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700278 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000279 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
280 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000281 EXPECT_EQ(0, audio_type); // Speech.
282 }
283 }
284
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000285 silence[0] = 10000;
286 if (dtx) {
287 // Verify that encoder/decoder can jump out from DTX mode.
Yves Gerey665174f2018-06-19 15:03:05 +0200288 EXPECT_EQ(samples, static_cast<size_t>(
289 EncodeDecode(opus_encoder_, silence, opus_decoder_,
290 output_data_decode, &audio_type)));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700291 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000292 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
293 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000294 EXPECT_EQ(0, audio_type); // Speech.
295 }
296
297 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000298 delete[] output_data_decode;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000299 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
300 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000301}
302
soren28dc2852017-04-06 05:48:36 -0700303// Test if CBR does what we expect.
304void OpusTest::TestCbrEffect(bool cbr, int block_length_ms) {
305 PrepareSpeechData(channels_, block_length_ms, 2000);
306 const size_t samples = kOpusRateKhz * block_length_ms;
307
308 int32_t max_pkt_size_diff = 0;
309 int32_t prev_pkt_size = 0;
310
311 // Create encoder memory.
312 EXPECT_EQ(0,
313 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
314 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
315
316 // Set bitrate.
317 EXPECT_EQ(
318 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
319
320 // Setting CBR.
321 EXPECT_EQ(0, cbr ? WebRtcOpus_EnableCbr(opus_encoder_)
322 : WebRtcOpus_DisableCbr(opus_encoder_));
323
324 int16_t audio_type;
325 std::vector<int16_t> audio_out(samples * channels_);
326 for (int i = 0; i < 100; ++i) {
327 EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode(
328 opus_encoder_, speech_data_.GetNextBlock(),
329 opus_decoder_, audio_out.data(), &audio_type)));
330
331 if (prev_pkt_size > 0) {
332 int32_t diff = std::abs((int32_t)encoded_bytes_ - prev_pkt_size);
333 max_pkt_size_diff = std::max(max_pkt_size_diff, diff);
334 }
Mirko Bonadei737e0732017-10-19 09:00:17 +0200335 prev_pkt_size = rtc::checked_cast<int32_t>(encoded_bytes_);
soren28dc2852017-04-06 05:48:36 -0700336 }
337
338 if (cbr) {
339 EXPECT_EQ(max_pkt_size_diff, 0);
340 } else {
341 EXPECT_GT(max_pkt_size_diff, 0);
342 }
343
344 // Free memory.
345 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
346 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
347}
348
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000349// Test failing Create.
henrika1d34fe92015-06-16 10:04:20 +0200350TEST(OpusTest, OpusCreateFail) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000351 WebRtcOpusEncInst* opus_encoder;
352 WebRtcOpusDecInst* opus_decoder;
353
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000354 // Test to see that an invalid pointer is caught.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000355 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(NULL, 1, 0));
356 // Invalid channel number.
357 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 3, 0));
358 // Invalid applciation mode.
359 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 1, 2));
360
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000361 EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(NULL, 1));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000362 // Invalid channel number.
363 EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(&opus_decoder, 3));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000364}
365
366// Test failing Free.
henrika1d34fe92015-06-16 10:04:20 +0200367TEST(OpusTest, OpusFreeFail) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000368 // Test to see that an invalid pointer is caught.
369 EXPECT_EQ(-1, WebRtcOpus_EncoderFree(NULL));
370 EXPECT_EQ(-1, WebRtcOpus_DecoderFree(NULL));
371}
372
373// Test normal Create and Free.
henrika1d34fe92015-06-16 10:04:20 +0200374TEST_P(OpusTest, OpusCreateFree) {
Yves Gerey665174f2018-06-19 15:03:05 +0200375 EXPECT_EQ(0,
376 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000377 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
378 EXPECT_TRUE(opus_encoder_ != NULL);
379 EXPECT_TRUE(opus_decoder_ != NULL);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000380 // Free encoder and decoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000381 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
382 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000383}
384
henrika1d34fe92015-06-16 10:04:20 +0200385TEST_P(OpusTest, OpusEncodeDecode) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000386 PrepareSpeechData(channels_, 20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000387
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000388 // Create encoder memory.
Yves Gerey665174f2018-06-19 15:03:05 +0200389 EXPECT_EQ(0,
390 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
391 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000392
393 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200394 EXPECT_EQ(
395 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000396
397 // Check number of channels for decoder.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000398 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
399
400 // Check application mode.
401 opus_int32 app;
Yves Gerey665174f2018-06-19 15:03:05 +0200402 opus_encoder_ctl(opus_encoder_->encoder, OPUS_GET_APPLICATION(&app));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000403 EXPECT_EQ(application_ == 0 ? OPUS_APPLICATION_VOIP : OPUS_APPLICATION_AUDIO,
404 app);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000405
406 // Encode & decode.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000407 int16_t audio_type;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000408 int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_];
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000409 EXPECT_EQ(kOpus20msFrameSamples,
kwiberg288886b2015-11-06 01:21:35 -0800410 static_cast<size_t>(
411 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
412 opus_decoder_, output_data_decode, &audio_type)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000413
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000414 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000415 delete[] output_data_decode;
416 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
417 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000418}
419
henrika1d34fe92015-06-16 10:04:20 +0200420TEST_P(OpusTest, OpusSetBitRate) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000421 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000422 EXPECT_EQ(-1, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000423
424 // Create encoder memory, try with different bitrates.
Yves Gerey665174f2018-06-19 15:03:05 +0200425 EXPECT_EQ(0,
426 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000427 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 30000));
428 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
429 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 300000));
430 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 600000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000431
432 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000433 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000434}
435
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000436TEST_P(OpusTest, OpusSetComplexity) {
minyue@webrtc.org04546882014-03-07 08:55:48 +0000437 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000438 EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 9));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000439
440 // Create encoder memory, try with different complexities.
Yves Gerey665174f2018-06-19 15:03:05 +0200441 EXPECT_EQ(0,
442 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000443
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000444 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 0));
445 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 10));
446 EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 11));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000447
448 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000449 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000450}
451
Alex Luebseeb27652017-11-20 11:13:56 -0800452TEST_P(OpusTest, OpusSetBandwidth) {
453 PrepareSpeechData(channels_, 20, 20);
454
455 int16_t audio_type;
456 std::unique_ptr<int16_t[]> output_data_decode(
457 new int16_t[kOpus20msFrameSamples * channels_]());
458
459 // Test without creating encoder memory.
460 EXPECT_EQ(-1,
461 WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
462 EXPECT_EQ(-1, WebRtcOpus_GetBandwidth(opus_encoder_));
463
464 // Create encoder memory, try with different bandwidths.
465 EXPECT_EQ(0,
466 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
467 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
468
469 EXPECT_EQ(-1, WebRtcOpus_SetBandwidth(opus_encoder_,
470 OPUS_BANDWIDTH_NARROWBAND - 1));
471 EXPECT_EQ(0,
472 WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
473 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
474 output_data_decode.get(), &audio_type);
475 EXPECT_EQ(OPUS_BANDWIDTH_NARROWBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
476 EXPECT_EQ(0, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND));
477 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
478 output_data_decode.get(), &audio_type);
479 EXPECT_EQ(OPUS_BANDWIDTH_FULLBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
480 EXPECT_EQ(
481 -1, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND + 1));
482 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
483 output_data_decode.get(), &audio_type);
484 EXPECT_EQ(OPUS_BANDWIDTH_FULLBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
485
486 // Free memory.
487 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
488 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
489}
490
minyuec8299f92016-09-27 02:08:47 -0700491TEST_P(OpusTest, OpusForceChannels) {
492 // Test without creating encoder memory.
493 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
494
495 ASSERT_EQ(0,
496 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
497
498 if (channels_ == 2) {
499 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 3));
500 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
501 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
502 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
503 } else {
504 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
505 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
506 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
507 }
508
509 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
510}
511
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000512// Encode and decode one frame, initialize the decoder and
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000513// decode once more.
henrika1d34fe92015-06-16 10:04:20 +0200514TEST_P(OpusTest, OpusDecodeInit) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000515 PrepareSpeechData(channels_, 20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000516
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000517 // Create encoder memory.
Yves Gerey665174f2018-06-19 15:03:05 +0200518 EXPECT_EQ(0,
519 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000520 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000521
522 // Encode & decode.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000523 int16_t audio_type;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000524 int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_];
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000525 EXPECT_EQ(kOpus20msFrameSamples,
kwiberg288886b2015-11-06 01:21:35 -0800526 static_cast<size_t>(
527 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
528 opus_decoder_, output_data_decode, &audio_type)));
minyue@webrtc.org52bc4f42014-12-04 11:00:50 +0000529
Karl Wiberg43766482015-08-27 15:22:11 +0200530 WebRtcOpus_DecoderInit(opus_decoder_);
minyue@webrtc.org52bc4f42014-12-04 11:00:50 +0000531
532 EXPECT_EQ(kOpus20msFrameSamples,
Yves Gerey665174f2018-06-19 15:03:05 +0200533 static_cast<size_t>(
534 WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_,
535 output_data_decode, &audio_type)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000536
537 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000538 delete[] output_data_decode;
539 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
540 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000541}
542
henrika1d34fe92015-06-16 10:04:20 +0200543TEST_P(OpusTest, OpusEnableDisableFec) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000544 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000545 EXPECT_EQ(-1, WebRtcOpus_EnableFec(opus_encoder_));
546 EXPECT_EQ(-1, WebRtcOpus_DisableFec(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000547
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000548 // Create encoder memory.
Yves Gerey665174f2018-06-19 15:03:05 +0200549 EXPECT_EQ(0,
550 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000551
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000552 EXPECT_EQ(0, WebRtcOpus_EnableFec(opus_encoder_));
553 EXPECT_EQ(0, WebRtcOpus_DisableFec(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000554
555 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000556 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000557}
558
henrika1d34fe92015-06-16 10:04:20 +0200559TEST_P(OpusTest, OpusEnableDisableDtx) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000560 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000561 EXPECT_EQ(-1, WebRtcOpus_EnableDtx(opus_encoder_));
562 EXPECT_EQ(-1, WebRtcOpus_DisableDtx(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000563
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000564 // Create encoder memory.
Yves Gerey665174f2018-06-19 15:03:05 +0200565 EXPECT_EQ(0,
566 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000567
568 opus_int32 dtx;
569
570 // DTX is off by default.
Yves Gerey665174f2018-06-19 15:03:05 +0200571 opus_encoder_ctl(opus_encoder_->encoder, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000572 EXPECT_EQ(0, dtx);
573
574 // Test to enable DTX.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000575 EXPECT_EQ(0, WebRtcOpus_EnableDtx(opus_encoder_));
Yves Gerey665174f2018-06-19 15:03:05 +0200576 opus_encoder_ctl(opus_encoder_->encoder, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000577 EXPECT_EQ(1, dtx);
578
579 // Test to disable DTX.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000580 EXPECT_EQ(0, WebRtcOpus_DisableDtx(opus_encoder_));
Yves Gerey665174f2018-06-19 15:03:05 +0200581 opus_encoder_ctl(opus_encoder_->encoder, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000582 EXPECT_EQ(0, dtx);
583
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000584 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000585 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000586}
587
henrika1d34fe92015-06-16 10:04:20 +0200588TEST_P(OpusTest, OpusDtxOff) {
minyue3cea2562015-11-10 03:49:26 -0800589 TestDtxEffect(false, 10);
590 TestDtxEffect(false, 20);
591 TestDtxEffect(false, 40);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000592}
593
henrika1d34fe92015-06-16 10:04:20 +0200594TEST_P(OpusTest, OpusDtxOn) {
minyue3cea2562015-11-10 03:49:26 -0800595 TestDtxEffect(true, 10);
596 TestDtxEffect(true, 20);
597 TestDtxEffect(true, 40);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000598}
599
soren28dc2852017-04-06 05:48:36 -0700600TEST_P(OpusTest, OpusCbrOff) {
601 TestCbrEffect(false, 10);
602 TestCbrEffect(false, 20);
603 TestCbrEffect(false, 40);
604}
605
606TEST_P(OpusTest, OpusCbrOn) {
607 TestCbrEffect(true, 10);
608 TestCbrEffect(true, 20);
609 TestCbrEffect(true, 40);
610}
611
henrika1d34fe92015-06-16 10:04:20 +0200612TEST_P(OpusTest, OpusSetPacketLossRate) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000613 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000614 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000615
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000616 // Create encoder memory.
Yves Gerey665174f2018-06-19 15:03:05 +0200617 EXPECT_EQ(0,
618 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000619
620 EXPECT_EQ(0, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
621 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, -1));
622 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 101));
623
624 // Free memory.
625 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
626}
627
henrika1d34fe92015-06-16 10:04:20 +0200628TEST_P(OpusTest, OpusSetMaxPlaybackRate) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000629 // Test without creating encoder memory.
630 EXPECT_EQ(-1, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, 20000));
631
632 // Create encoder memory.
Yves Gerey665174f2018-06-19 15:03:05 +0200633 EXPECT_EQ(0,
634 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000635
636 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 48000);
637 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 24001);
638 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 24000);
639 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 16001);
640 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 16000);
641 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 12001);
642 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 12000);
643 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 8001);
644 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 8000);
645 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 4000);
646
647 // Free memory.
648 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
649}
650
651// Test PLC.
henrika1d34fe92015-06-16 10:04:20 +0200652TEST_P(OpusTest, OpusDecodePlc) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000653 PrepareSpeechData(channels_, 20, 20);
654
655 // Create encoder memory.
Yves Gerey665174f2018-06-19 15:03:05 +0200656 EXPECT_EQ(0,
657 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000658 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000659
660 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200661 EXPECT_EQ(
662 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000663
664 // Check number of channels for decoder.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000665 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000666
667 // Encode & decode.
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000668 int16_t audio_type;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000669 int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_];
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000670 EXPECT_EQ(kOpus20msFrameSamples,
kwiberg288886b2015-11-06 01:21:35 -0800671 static_cast<size_t>(
672 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
673 opus_decoder_, output_data_decode, &audio_type)));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000674
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000675 // Call decoder PLC.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000676 int16_t* plc_buffer = new int16_t[kOpus20msFrameSamples * channels_];
Yves Gerey665174f2018-06-19 15:03:05 +0200677 EXPECT_EQ(kOpus20msFrameSamples, static_cast<size_t>(WebRtcOpus_DecodePlc(
678 opus_decoder_, plc_buffer, 1)));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000679
680 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000681 delete[] plc_buffer;
682 delete[] output_data_decode;
683 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
684 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000685}
686
687// Duration estimation.
henrika1d34fe92015-06-16 10:04:20 +0200688TEST_P(OpusTest, OpusDurationEstimation) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000689 PrepareSpeechData(channels_, 20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000690
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000691 // Create.
Yves Gerey665174f2018-06-19 15:03:05 +0200692 EXPECT_EQ(0,
693 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000694 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000695
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000696 // 10 ms. We use only first 10 ms of a 20 ms block.
kwiberg288886b2015-11-06 01:21:35 -0800697 auto speech_block = speech_data_.GetNextBlock();
698 int encoded_bytes_int = WebRtcOpus_Encode(
699 opus_encoder_, speech_block.data(),
Yves Gerey665174f2018-06-19 15:03:05 +0200700 rtc::CheckedDivExact(speech_block.size(), 2 * channels_), kMaxBytes,
701 bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700702 EXPECT_GE(encoded_bytes_int, 0);
Yves Gerey665174f2018-06-19 15:03:05 +0200703 EXPECT_EQ(
704 kOpus10msFrameSamples,
705 static_cast<size_t>(WebRtcOpus_DurationEst(
706 opus_decoder_, bitstream_, static_cast<size_t>(encoded_bytes_int))));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000707
708 // 20 ms
kwiberg288886b2015-11-06 01:21:35 -0800709 speech_block = speech_data_.GetNextBlock();
Yves Gerey665174f2018-06-19 15:03:05 +0200710 encoded_bytes_int =
711 WebRtcOpus_Encode(opus_encoder_, speech_block.data(),
712 rtc::CheckedDivExact(speech_block.size(), channels_),
713 kMaxBytes, bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700714 EXPECT_GE(encoded_bytes_int, 0);
Yves Gerey665174f2018-06-19 15:03:05 +0200715 EXPECT_EQ(
716 kOpus20msFrameSamples,
717 static_cast<size_t>(WebRtcOpus_DurationEst(
718 opus_decoder_, bitstream_, static_cast<size_t>(encoded_bytes_int))));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000719
720 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000721 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
722 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000723}
724
henrika1d34fe92015-06-16 10:04:20 +0200725TEST_P(OpusTest, OpusDecodeRepacketized) {
minyuea613eb62017-03-14 14:33:30 -0700726 constexpr size_t kPackets = 6;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000727
728 PrepareSpeechData(channels_, 20, 20 * kPackets);
729
730 // Create encoder memory.
Yves Gerey665174f2018-06-19 15:03:05 +0200731 ASSERT_EQ(0,
732 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
733 ASSERT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000734
735 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200736 EXPECT_EQ(
737 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000738
739 // Check number of channels for decoder.
740 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
741
742 // Encode & decode.
743 int16_t audio_type;
kwiberg91d97562016-02-14 01:10:03 -0800744 std::unique_ptr<int16_t[]> output_data_decode(
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000745 new int16_t[kPackets * kOpus20msFrameSamples * channels_]);
746 OpusRepacketizer* rp = opus_repacketizer_create();
747
minyuea613eb62017-03-14 14:33:30 -0700748 size_t num_packets = 0;
749 constexpr size_t kMaxCycles = 100;
750 for (size_t idx = 0; idx < kMaxCycles; ++idx) {
kwiberg288886b2015-11-06 01:21:35 -0800751 auto speech_block = speech_data_.GetNextBlock();
752 encoded_bytes_ =
753 WebRtcOpus_Encode(opus_encoder_, speech_block.data(),
Peter Kasting69558702016-01-12 16:26:35 -0800754 rtc::CheckedDivExact(speech_block.size(), channels_),
kwiberg288886b2015-11-06 01:21:35 -0800755 kMaxBytes, bitstream_);
Yves Gerey665174f2018-06-19 15:03:05 +0200756 if (opus_repacketizer_cat(rp, bitstream_,
757 rtc::checked_cast<opus_int32>(encoded_bytes_)) ==
758 OPUS_OK) {
minyuea613eb62017-03-14 14:33:30 -0700759 ++num_packets;
760 if (num_packets == kPackets) {
761 break;
762 }
763 } else {
764 // Opus repacketizer cannot guarantee a success. We try again if it fails.
765 opus_repacketizer_init(rp);
766 num_packets = 0;
767 }
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000768 }
minyuea613eb62017-03-14 14:33:30 -0700769 EXPECT_EQ(kPackets, num_packets);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000770
771 encoded_bytes_ = opus_repacketizer_out(rp, bitstream_, kMaxBytes);
772
773 EXPECT_EQ(kOpus20msFrameSamples * kPackets,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700774 static_cast<size_t>(WebRtcOpus_DurationEst(
775 opus_decoder_, bitstream_, encoded_bytes_)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000776
777 EXPECT_EQ(kOpus20msFrameSamples * kPackets,
Yves Gerey665174f2018-06-19 15:03:05 +0200778 static_cast<size_t>(
779 WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_,
780 output_data_decode.get(), &audio_type)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000781
782 // Free memory.
783 opus_repacketizer_destroy(rp);
784 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
785 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
786}
787
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000788INSTANTIATE_TEST_CASE_P(VariousMode,
789 OpusTest,
790 Combine(Values(1, 2), Values(0, 1)));
791
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000792} // namespace webrtc