blob: be0530b74b8c8af9b7d43a364e2d9b884f2c81d2 [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"
Mirko Bonadei737e0732017-10-19 09:00:17 +020018#include "rtc_base/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,
61 opus_int32 expect, int32_t set);
62
Peter Kasting69558702016-01-12 16:26:35 -080063 void CheckAudioBounded(const int16_t* audio, size_t samples, size_t channels,
minyue3cea2562015-11-10 03:49:26 -080064 uint16_t bound) const;
65
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000066 WebRtcOpusEncInst* opus_encoder_;
67 WebRtcOpusDecInst* opus_decoder_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000068
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000069 AudioLoop speech_data_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000070 uint8_t bitstream_[kMaxBytes];
Peter Kastingdce40cf2015-08-24 14:52:23 -070071 size_t encoded_bytes_;
Peter Kasting69558702016-01-12 16:26:35 -080072 size_t channels_;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000073 int application_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000074};
75
76OpusTest::OpusTest()
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000077 : opus_encoder_(NULL),
78 opus_decoder_(NULL),
79 encoded_bytes_(0),
Peter Kasting69558702016-01-12 16:26:35 -080080 channels_(static_cast<size_t>(::testing::get<0>(GetParam()))),
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000081 application_(::testing::get<1>(GetParam())) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000082}
83
Peter Kasting69558702016-01-12 16:26:35 -080084void OpusTest::PrepareSpeechData(size_t channel, int block_length_ms,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000085 int loop_length_ms) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000086 const std::string file_name =
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000087 webrtc::test::ResourcePath((channel == 1) ?
88 "audio_coding/testfile32kHz" :
89 "audio_coding/teststereo32kHz", "pcm");
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000090 if (loop_length_ms < block_length_ms) {
91 loop_length_ms = block_length_ms;
92 }
93 EXPECT_TRUE(speech_data_.Init(file_name,
94 loop_length_ms * kOpusRateKhz * channel,
95 block_length_ms * kOpusRateKhz * channel));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000096}
97
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000098void OpusTest::SetMaxPlaybackRate(WebRtcOpusEncInst* encoder,
99 opus_int32 expect,
100 int32_t set) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000101 opus_int32 bandwidth;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000102 EXPECT_EQ(0, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, set));
103 opus_encoder_ctl(opus_encoder_->encoder,
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000104 OPUS_GET_MAX_BANDWIDTH(&bandwidth));
105 EXPECT_EQ(expect, bandwidth);
106}
107
minyue3cea2562015-11-10 03:49:26 -0800108void OpusTest::CheckAudioBounded(const int16_t* audio, size_t samples,
Peter Kasting69558702016-01-12 16:26:35 -0800109 size_t channels, uint16_t bound) const {
minyue3cea2562015-11-10 03:49:26 -0800110 for (size_t i = 0; i < samples; ++i) {
Peter Kasting69558702016-01-12 16:26:35 -0800111 for (size_t c = 0; c < channels; ++c) {
minyue3cea2562015-11-10 03:49:26 -0800112 ASSERT_GE(audio[i * channels + c], -bound);
113 ASSERT_LE(audio[i * channels + c], bound);
114 }
115 }
116}
117
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000118int OpusTest::EncodeDecode(WebRtcOpusEncInst* encoder,
kwiberg288886b2015-11-06 01:21:35 -0800119 rtc::ArrayView<const int16_t> input_audio,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000120 WebRtcOpusDecInst* decoder,
121 int16_t* output_audio,
122 int16_t* audio_type) {
kwiberg288886b2015-11-06 01:21:35 -0800123 int encoded_bytes_int = WebRtcOpus_Encode(
124 encoder, input_audio.data(),
Peter Kasting69558702016-01-12 16:26:35 -0800125 rtc::CheckedDivExact(input_audio.size(), channels_),
kwiberg288886b2015-11-06 01:21:35 -0800126 kMaxBytes, bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700127 EXPECT_GE(encoded_bytes_int, 0);
128 encoded_bytes_ = static_cast<size_t>(encoded_bytes_int);
minyuel6d92bf52015-09-23 15:20:39 +0200129 int est_len = WebRtcOpus_DurationEst(decoder, bitstream_, encoded_bytes_);
130 int act_len = WebRtcOpus_Decode(decoder, bitstream_,
131 encoded_bytes_, output_audio,
132 audio_type);
133 EXPECT_EQ(est_len, act_len);
134 return act_len;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000135}
136
137// Test if encoder/decoder can enter DTX mode properly and do not enter DTX when
138// they should not. This test is signal dependent.
minyue3cea2562015-11-10 03:49:26 -0800139void OpusTest::TestDtxEffect(bool dtx, int block_length_ms) {
140 PrepareSpeechData(channels_, block_length_ms, 2000);
141 const size_t samples = kOpusRateKhz * block_length_ms;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000142
143 // Create encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000144 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
145 channels_,
146 application_));
147 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000148
149 // Set bitrate.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000150 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_,
151 channels_ == 1 ? 32000 : 64000));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000152
153 // Set input audio as silence.
minyue3cea2562015-11-10 03:49:26 -0800154 std::vector<int16_t> silence(samples * channels_, 0);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000155
156 // Setting DTX.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000157 EXPECT_EQ(0, dtx ? WebRtcOpus_EnableDtx(opus_encoder_) :
158 WebRtcOpus_DisableDtx(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000159
160 int16_t audio_type;
minyue3cea2562015-11-10 03:49:26 -0800161 int16_t* output_data_decode = new int16_t[samples * channels_];
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000162
163 for (int i = 0; i < 100; ++i) {
minyue3cea2562015-11-10 03:49:26 -0800164 EXPECT_EQ(samples,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700165 static_cast<size_t>(EncodeDecode(
kwiberg288886b2015-11-06 01:21:35 -0800166 opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
167 output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000168 // If not DTX, it should never enter DTX mode. If DTX, we do not care since
169 // whether it enters DTX depends on the signal type.
170 if (!dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700171 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000172 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
173 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000174 EXPECT_EQ(0, audio_type); // Speech.
175 }
176 }
177
178 // We input some silent segments. In DTX mode, the encoder will stop sending.
179 // However, DTX may happen after a while.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000180 for (int i = 0; i < 30; ++i) {
minyue3cea2562015-11-10 03:49:26 -0800181 EXPECT_EQ(samples,
182 static_cast<size_t>(EncodeDecode(
183 opus_encoder_, silence, opus_decoder_, output_data_decode,
184 &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;
230 EXPECT_EQ(samples,
231 static_cast<size_t>(EncodeDecode(
232 opus_encoder_, silence, opus_decoder_, output_data_decode,
233 &audio_type)));
Minyue Li092041c2015-05-11 12:19:35 +0200234 if (dtx) {
minyue3cea2562015-11-10 03:49:26 -0800235 if (encoded_bytes_ > 1)
236 break;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700237 EXPECT_EQ(0U, encoded_bytes_) // Send 0 byte.
Minyue Li092041c2015-05-11 12:19:35 +0200238 << "Opus should have entered DTX mode.";
239 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
240 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
241 EXPECT_EQ(2, audio_type); // Comfort noise.
minyue3cea2562015-11-10 03:49:26 -0800242 if (time >= kCheckTimeMs) {
243 CheckAudioBounded(output_data_decode, samples, channels_,
244 kOutputValueBound);
245 }
Minyue Li092041c2015-05-11 12:19:35 +0200246 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700247 EXPECT_GT(encoded_bytes_, 1U);
Minyue Li092041c2015-05-11 12:19:35 +0200248 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
249 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
250 EXPECT_EQ(0, audio_type); // Speech.
251 }
252 }
253
minyue3cea2562015-11-10 03:49:26 -0800254 if (dtx) {
255 // With DTX, Opus must stop transmission for some time.
256 EXPECT_GT(i, 1);
257 }
Minyue Li092041c2015-05-11 12:19:35 +0200258
minyue3cea2562015-11-10 03:49:26 -0800259 // We expect a normal payload.
Minyue Li092041c2015-05-11 12:19:35 +0200260 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
261 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
262 EXPECT_EQ(0, audio_type); // Speech.
263
264 // Enters DTX again immediately.
minyue3cea2562015-11-10 03:49:26 -0800265 time += block_length_ms;
266 EXPECT_EQ(samples,
267 static_cast<size_t>(EncodeDecode(
268 opus_encoder_, silence, opus_decoder_, output_data_decode,
269 &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000270 if (dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700271 EXPECT_EQ(1U, encoded_bytes_); // Send 1 byte.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000272 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
273 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000274 EXPECT_EQ(2, audio_type); // Comfort noise.
minyue3cea2562015-11-10 03:49:26 -0800275 if (time >= kCheckTimeMs) {
276 CheckAudioBounded(output_data_decode, samples, channels_,
277 kOutputValueBound);
278 }
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000279 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700280 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000281 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
282 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000283 EXPECT_EQ(0, audio_type); // Speech.
284 }
285 }
286
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000287 silence[0] = 10000;
288 if (dtx) {
289 // Verify that encoder/decoder can jump out from DTX mode.
minyue3cea2562015-11-10 03:49:26 -0800290 EXPECT_EQ(samples,
291 static_cast<size_t>(EncodeDecode(
292 opus_encoder_, silence, opus_decoder_, output_data_decode,
293 &audio_type)));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700294 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000295 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
296 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000297 EXPECT_EQ(0, audio_type); // Speech.
298 }
299
300 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000301 delete[] output_data_decode;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000302 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
303 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000304}
305
soren28dc2852017-04-06 05:48:36 -0700306// Test if CBR does what we expect.
307void OpusTest::TestCbrEffect(bool cbr, int block_length_ms) {
308 PrepareSpeechData(channels_, block_length_ms, 2000);
309 const size_t samples = kOpusRateKhz * block_length_ms;
310
311 int32_t max_pkt_size_diff = 0;
312 int32_t prev_pkt_size = 0;
313
314 // Create encoder memory.
315 EXPECT_EQ(0,
316 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
317 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
318
319 // Set bitrate.
320 EXPECT_EQ(
321 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
322
323 // Setting CBR.
324 EXPECT_EQ(0, cbr ? WebRtcOpus_EnableCbr(opus_encoder_)
325 : WebRtcOpus_DisableCbr(opus_encoder_));
326
327 int16_t audio_type;
328 std::vector<int16_t> audio_out(samples * channels_);
329 for (int i = 0; i < 100; ++i) {
330 EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode(
331 opus_encoder_, speech_data_.GetNextBlock(),
332 opus_decoder_, audio_out.data(), &audio_type)));
333
334 if (prev_pkt_size > 0) {
335 int32_t diff = std::abs((int32_t)encoded_bytes_ - prev_pkt_size);
336 max_pkt_size_diff = std::max(max_pkt_size_diff, diff);
337 }
Mirko Bonadei737e0732017-10-19 09:00:17 +0200338 prev_pkt_size = rtc::checked_cast<int32_t>(encoded_bytes_);
soren28dc2852017-04-06 05:48:36 -0700339 }
340
341 if (cbr) {
342 EXPECT_EQ(max_pkt_size_diff, 0);
343 } else {
344 EXPECT_GT(max_pkt_size_diff, 0);
345 }
346
347 // Free memory.
348 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
349 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
350}
351
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000352// Test failing Create.
henrika1d34fe92015-06-16 10:04:20 +0200353TEST(OpusTest, OpusCreateFail) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000354 WebRtcOpusEncInst* opus_encoder;
355 WebRtcOpusDecInst* opus_decoder;
356
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000357 // Test to see that an invalid pointer is caught.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000358 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(NULL, 1, 0));
359 // Invalid channel number.
360 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 3, 0));
361 // Invalid applciation mode.
362 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 1, 2));
363
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000364 EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(NULL, 1));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000365 // Invalid channel number.
366 EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(&opus_decoder, 3));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000367}
368
369// Test failing Free.
henrika1d34fe92015-06-16 10:04:20 +0200370TEST(OpusTest, OpusFreeFail) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000371 // Test to see that an invalid pointer is caught.
372 EXPECT_EQ(-1, WebRtcOpus_EncoderFree(NULL));
373 EXPECT_EQ(-1, WebRtcOpus_DecoderFree(NULL));
374}
375
376// Test normal Create and Free.
henrika1d34fe92015-06-16 10:04:20 +0200377TEST_P(OpusTest, OpusCreateFree) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000378 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
379 channels_,
380 application_));
381 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
382 EXPECT_TRUE(opus_encoder_ != NULL);
383 EXPECT_TRUE(opus_decoder_ != NULL);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000384 // Free encoder and decoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000385 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
386 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000387}
388
henrika1d34fe92015-06-16 10:04:20 +0200389TEST_P(OpusTest, OpusEncodeDecode) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000390 PrepareSpeechData(channels_, 20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000391
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000392 // Create encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000393 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
394 channels_,
395 application_));
396 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_,
397 channels_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000398
399 // Set bitrate.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000400 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_,
401 channels_ == 1 ? 32000 : 64000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000402
403 // Check number of channels for decoder.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000404 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
405
406 // Check application mode.
407 opus_int32 app;
408 opus_encoder_ctl(opus_encoder_->encoder,
409 OPUS_GET_APPLICATION(&app));
410 EXPECT_EQ(application_ == 0 ? OPUS_APPLICATION_VOIP : OPUS_APPLICATION_AUDIO,
411 app);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000412
413 // Encode & decode.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000414 int16_t audio_type;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000415 int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_];
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000416 EXPECT_EQ(kOpus20msFrameSamples,
kwiberg288886b2015-11-06 01:21:35 -0800417 static_cast<size_t>(
418 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
419 opus_decoder_, output_data_decode, &audio_type)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000420
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000421 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000422 delete[] output_data_decode;
423 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
424 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000425}
426
henrika1d34fe92015-06-16 10:04:20 +0200427TEST_P(OpusTest, OpusSetBitRate) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000428 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000429 EXPECT_EQ(-1, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000430
431 // Create encoder memory, try with different bitrates.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000432 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
433 channels_,
434 application_));
435 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 30000));
436 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
437 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 300000));
438 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 600000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000439
440 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000441 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000442}
443
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000444TEST_P(OpusTest, OpusSetComplexity) {
minyue@webrtc.org04546882014-03-07 08:55:48 +0000445 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000446 EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 9));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000447
448 // Create encoder memory, try with different complexities.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000449 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
450 channels_,
451 application_));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000452
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000453 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 0));
454 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 10));
455 EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 11));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000456
457 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000458 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000459}
460
Alex Luebseeb27652017-11-20 11:13:56 -0800461TEST_P(OpusTest, OpusSetBandwidth) {
462 PrepareSpeechData(channels_, 20, 20);
463
464 int16_t audio_type;
465 std::unique_ptr<int16_t[]> output_data_decode(
466 new int16_t[kOpus20msFrameSamples * channels_]());
467
468 // Test without creating encoder memory.
469 EXPECT_EQ(-1,
470 WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
471 EXPECT_EQ(-1, WebRtcOpus_GetBandwidth(opus_encoder_));
472
473 // Create encoder memory, try with different bandwidths.
474 EXPECT_EQ(0,
475 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
476 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
477
478 EXPECT_EQ(-1, WebRtcOpus_SetBandwidth(opus_encoder_,
479 OPUS_BANDWIDTH_NARROWBAND - 1));
480 EXPECT_EQ(0,
481 WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
482 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
483 output_data_decode.get(), &audio_type);
484 EXPECT_EQ(OPUS_BANDWIDTH_NARROWBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
485 EXPECT_EQ(0, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND));
486 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
487 output_data_decode.get(), &audio_type);
488 EXPECT_EQ(OPUS_BANDWIDTH_FULLBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
489 EXPECT_EQ(
490 -1, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND + 1));
491 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
492 output_data_decode.get(), &audio_type);
493 EXPECT_EQ(OPUS_BANDWIDTH_FULLBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
494
495 // Free memory.
496 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
497 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
498}
499
minyuec8299f92016-09-27 02:08:47 -0700500TEST_P(OpusTest, OpusForceChannels) {
501 // Test without creating encoder memory.
502 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
503
504 ASSERT_EQ(0,
505 WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
506
507 if (channels_ == 2) {
508 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 3));
509 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
510 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
511 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
512 } else {
513 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
514 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
515 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
516 }
517
518 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
519}
520
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000521// Encode and decode one frame, initialize the decoder and
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000522// decode once more.
henrika1d34fe92015-06-16 10:04:20 +0200523TEST_P(OpusTest, OpusDecodeInit) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000524 PrepareSpeechData(channels_, 20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000525
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000526 // Create encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000527 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
528 channels_,
529 application_));
530 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000531
532 // Encode & decode.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000533 int16_t audio_type;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000534 int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_];
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000535 EXPECT_EQ(kOpus20msFrameSamples,
kwiberg288886b2015-11-06 01:21:35 -0800536 static_cast<size_t>(
537 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
538 opus_decoder_, output_data_decode, &audio_type)));
minyue@webrtc.org52bc4f42014-12-04 11:00:50 +0000539
Karl Wiberg43766482015-08-27 15:22:11 +0200540 WebRtcOpus_DecoderInit(opus_decoder_);
minyue@webrtc.org52bc4f42014-12-04 11:00:50 +0000541
542 EXPECT_EQ(kOpus20msFrameSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700543 static_cast<size_t>(WebRtcOpus_Decode(
544 opus_decoder_, bitstream_, encoded_bytes_, output_data_decode,
545 &audio_type)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000546
547 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000548 delete[] output_data_decode;
549 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
550 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000551}
552
henrika1d34fe92015-06-16 10:04:20 +0200553TEST_P(OpusTest, OpusEnableDisableFec) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000554 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000555 EXPECT_EQ(-1, WebRtcOpus_EnableFec(opus_encoder_));
556 EXPECT_EQ(-1, WebRtcOpus_DisableFec(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000557
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000558 // Create encoder memory.
559 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
560 channels_,
561 application_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000562
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000563 EXPECT_EQ(0, WebRtcOpus_EnableFec(opus_encoder_));
564 EXPECT_EQ(0, WebRtcOpus_DisableFec(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000565
566 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000567 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000568}
569
henrika1d34fe92015-06-16 10:04:20 +0200570TEST_P(OpusTest, OpusEnableDisableDtx) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000571 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000572 EXPECT_EQ(-1, WebRtcOpus_EnableDtx(opus_encoder_));
573 EXPECT_EQ(-1, WebRtcOpus_DisableDtx(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000574
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000575 // Create encoder memory.
576 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
577 channels_,
578 application_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000579
580 opus_int32 dtx;
581
582 // DTX is off by default.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000583 opus_encoder_ctl(opus_encoder_->encoder,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000584 OPUS_GET_DTX(&dtx));
585 EXPECT_EQ(0, dtx);
586
587 // Test to enable DTX.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000588 EXPECT_EQ(0, WebRtcOpus_EnableDtx(opus_encoder_));
589 opus_encoder_ctl(opus_encoder_->encoder,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000590 OPUS_GET_DTX(&dtx));
591 EXPECT_EQ(1, dtx);
592
593 // Test to disable DTX.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000594 EXPECT_EQ(0, WebRtcOpus_DisableDtx(opus_encoder_));
595 opus_encoder_ctl(opus_encoder_->encoder,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000596 OPUS_GET_DTX(&dtx));
597 EXPECT_EQ(0, dtx);
598
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000599
600 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000601 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000602}
603
henrika1d34fe92015-06-16 10:04:20 +0200604TEST_P(OpusTest, OpusDtxOff) {
minyue3cea2562015-11-10 03:49:26 -0800605 TestDtxEffect(false, 10);
606 TestDtxEffect(false, 20);
607 TestDtxEffect(false, 40);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000608}
609
henrika1d34fe92015-06-16 10:04:20 +0200610TEST_P(OpusTest, OpusDtxOn) {
minyue3cea2562015-11-10 03:49:26 -0800611 TestDtxEffect(true, 10);
612 TestDtxEffect(true, 20);
613 TestDtxEffect(true, 40);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000614}
615
soren28dc2852017-04-06 05:48:36 -0700616TEST_P(OpusTest, OpusCbrOff) {
617 TestCbrEffect(false, 10);
618 TestCbrEffect(false, 20);
619 TestCbrEffect(false, 40);
620}
621
622TEST_P(OpusTest, OpusCbrOn) {
623 TestCbrEffect(true, 10);
624 TestCbrEffect(true, 20);
625 TestCbrEffect(true, 40);
626}
627
henrika1d34fe92015-06-16 10:04:20 +0200628TEST_P(OpusTest, OpusSetPacketLossRate) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000629 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000630 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000631
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000632 // Create encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000633 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
634 channels_,
635 application_));
636
637 EXPECT_EQ(0, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
638 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, -1));
639 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 101));
640
641 // Free memory.
642 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
643}
644
henrika1d34fe92015-06-16 10:04:20 +0200645TEST_P(OpusTest, OpusSetMaxPlaybackRate) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000646 // Test without creating encoder memory.
647 EXPECT_EQ(-1, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, 20000));
648
649 // Create encoder memory.
650 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
651 channels_,
652 application_));
653
654 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 48000);
655 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 24001);
656 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 24000);
657 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 16001);
658 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 16000);
659 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 12001);
660 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 12000);
661 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 8001);
662 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 8000);
663 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 4000);
664
665 // Free memory.
666 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
667}
668
669// Test PLC.
henrika1d34fe92015-06-16 10:04:20 +0200670TEST_P(OpusTest, OpusDecodePlc) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000671 PrepareSpeechData(channels_, 20, 20);
672
673 // Create encoder memory.
674 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
675 channels_,
676 application_));
677 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000678
679 // Set bitrate.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000680 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_,
681 channels_== 1 ? 32000 : 64000));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000682
683 // Check number of channels for decoder.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000684 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000685
686 // Encode & decode.
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000687 int16_t audio_type;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000688 int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_];
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000689 EXPECT_EQ(kOpus20msFrameSamples,
kwiberg288886b2015-11-06 01:21:35 -0800690 static_cast<size_t>(
691 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
692 opus_decoder_, output_data_decode, &audio_type)));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000693
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000694 // Call decoder PLC.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000695 int16_t* plc_buffer = new int16_t[kOpus20msFrameSamples * channels_];
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000696 EXPECT_EQ(kOpus20msFrameSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700697 static_cast<size_t>(WebRtcOpus_DecodePlc(
698 opus_decoder_, plc_buffer, 1)));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000699
700 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000701 delete[] plc_buffer;
702 delete[] output_data_decode;
703 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
704 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000705}
706
707// Duration estimation.
henrika1d34fe92015-06-16 10:04:20 +0200708TEST_P(OpusTest, OpusDurationEstimation) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000709 PrepareSpeechData(channels_, 20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000710
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000711 // Create.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000712 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
713 channels_,
714 application_));
715 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000716
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000717 // 10 ms. We use only first 10 ms of a 20 ms block.
kwiberg288886b2015-11-06 01:21:35 -0800718 auto speech_block = speech_data_.GetNextBlock();
719 int encoded_bytes_int = WebRtcOpus_Encode(
720 opus_encoder_, speech_block.data(),
Peter Kasting69558702016-01-12 16:26:35 -0800721 rtc::CheckedDivExact(speech_block.size(), 2 * channels_),
kwiberg288886b2015-11-06 01:21:35 -0800722 kMaxBytes, bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700723 EXPECT_GE(encoded_bytes_int, 0);
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000724 EXPECT_EQ(kOpus10msFrameSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700725 static_cast<size_t>(WebRtcOpus_DurationEst(
726 opus_decoder_, bitstream_,
727 static_cast<size_t>(encoded_bytes_int))));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000728
729 // 20 ms
kwiberg288886b2015-11-06 01:21:35 -0800730 speech_block = speech_data_.GetNextBlock();
731 encoded_bytes_int = WebRtcOpus_Encode(
732 opus_encoder_, speech_block.data(),
Peter Kasting69558702016-01-12 16:26:35 -0800733 rtc::CheckedDivExact(speech_block.size(), channels_),
kwiberg288886b2015-11-06 01:21:35 -0800734 kMaxBytes, bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700735 EXPECT_GE(encoded_bytes_int, 0);
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000736 EXPECT_EQ(kOpus20msFrameSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700737 static_cast<size_t>(WebRtcOpus_DurationEst(
738 opus_decoder_, bitstream_,
739 static_cast<size_t>(encoded_bytes_int))));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000740
741 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000742 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
743 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000744}
745
henrika1d34fe92015-06-16 10:04:20 +0200746TEST_P(OpusTest, OpusDecodeRepacketized) {
minyuea613eb62017-03-14 14:33:30 -0700747 constexpr size_t kPackets = 6;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000748
749 PrepareSpeechData(channels_, 20, 20 * kPackets);
750
751 // Create encoder memory.
752 ASSERT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_,
753 channels_,
754 application_));
755 ASSERT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_,
756 channels_));
757
758 // Set bitrate.
759 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_,
760 channels_ == 1 ? 32000 : 64000));
761
762 // Check number of channels for decoder.
763 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
764
765 // Encode & decode.
766 int16_t audio_type;
kwiberg91d97562016-02-14 01:10:03 -0800767 std::unique_ptr<int16_t[]> output_data_decode(
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000768 new int16_t[kPackets * kOpus20msFrameSamples * channels_]);
769 OpusRepacketizer* rp = opus_repacketizer_create();
770
minyuea613eb62017-03-14 14:33:30 -0700771 size_t num_packets = 0;
772 constexpr size_t kMaxCycles = 100;
773 for (size_t idx = 0; idx < kMaxCycles; ++idx) {
kwiberg288886b2015-11-06 01:21:35 -0800774 auto speech_block = speech_data_.GetNextBlock();
775 encoded_bytes_ =
776 WebRtcOpus_Encode(opus_encoder_, speech_block.data(),
Peter Kasting69558702016-01-12 16:26:35 -0800777 rtc::CheckedDivExact(speech_block.size(), channels_),
kwiberg288886b2015-11-06 01:21:35 -0800778 kMaxBytes, bitstream_);
Mirko Bonadei737e0732017-10-19 09:00:17 +0200779 if (opus_repacketizer_cat(
780 rp, bitstream_,
781 rtc::checked_cast<opus_int32>(encoded_bytes_)) == OPUS_OK) {
minyuea613eb62017-03-14 14:33:30 -0700782 ++num_packets;
783 if (num_packets == kPackets) {
784 break;
785 }
786 } else {
787 // Opus repacketizer cannot guarantee a success. We try again if it fails.
788 opus_repacketizer_init(rp);
789 num_packets = 0;
790 }
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000791 }
minyuea613eb62017-03-14 14:33:30 -0700792 EXPECT_EQ(kPackets, num_packets);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000793
794 encoded_bytes_ = opus_repacketizer_out(rp, bitstream_, kMaxBytes);
795
796 EXPECT_EQ(kOpus20msFrameSamples * kPackets,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700797 static_cast<size_t>(WebRtcOpus_DurationEst(
798 opus_decoder_, bitstream_, encoded_bytes_)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000799
800 EXPECT_EQ(kOpus20msFrameSamples * kPackets,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700801 static_cast<size_t>(WebRtcOpus_Decode(
802 opus_decoder_, bitstream_, encoded_bytes_,
803 output_data_decode.get(), &audio_type)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000804
805 // Free memory.
806 opus_repacketizer_destroy(rp);
807 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
808 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
809}
810
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000811INSTANTIATE_TEST_CASE_P(VariousMode,
812 OpusTest,
813 Combine(Values(1, 2), Values(0, 1)));
814
815
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000816} // namespace webrtc