blob: aa7eee97d6249b7945acde4eefaca54fdb6fdcd3 [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"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "test/testsupport/file_utils.h"
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000021
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000022namespace webrtc {
23
Alex Loiko50b8c392019-04-03 15:12:01 +020024namespace {
25// Equivalent to SDP params
26// {{"channel_mapping", "0,1,2,3"}, {"coupled_streams", "2"}}.
27constexpr unsigned char kQuadChannelMapping[] = {0, 1, 2, 3};
28constexpr int kQuadCoupledStreams = 2;
29
30constexpr unsigned char kStereoChannelMapping[] = {0, 1};
31constexpr int kStereoCoupledStreams = 1;
32
33constexpr unsigned char kMonoChannelMapping[] = {0};
34constexpr int kMonoCoupledStreams = 0;
35
36void CreateSingleOrMultiStreamEncoder(WebRtcOpusEncInst** opus_encoder,
37 int channels,
38 int application,
39 bool force_multistream = false) {
40 if (!force_multistream && (channels == 1 || channels == 2)) {
41 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(opus_encoder, channels, application));
42 } else if (force_multistream && channels == 1) {
43 EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
44 opus_encoder, channels, application, kMonoCoupledStreams,
45 kMonoChannelMapping));
46 } else if (force_multistream && channels == 2) {
47 EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
48 opus_encoder, channels, application, kStereoCoupledStreams,
49 kStereoChannelMapping));
50 } else if (channels == 4) {
51 EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
52 opus_encoder, channels, application, kQuadCoupledStreams,
53 kQuadChannelMapping));
54 } else {
55 EXPECT_TRUE(false) << channels;
56 }
57}
58
59void CreateSingleOrMultiStreamDecoder(WebRtcOpusDecInst** opus_decoder,
60 int channels,
61 bool force_multistream = false) {
62 if (!force_multistream && (channels == 1 || channels == 2)) {
63 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(opus_decoder, channels));
64 } else if (channels == 1) {
65 EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(opus_decoder, channels,
66 kMonoCoupledStreams,
67 kMonoChannelMapping));
68 } else if (channels == 2) {
69 EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(opus_decoder, channels,
70 kStereoCoupledStreams,
71 kStereoChannelMapping));
72 } else if (channels == 4) {
73 EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(opus_decoder, channels,
74 kQuadCoupledStreams,
75 kQuadChannelMapping));
76 } else {
77 EXPECT_TRUE(false) << channels;
78 }
79}
80} // namespace
81
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000082using test::AudioLoop;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000083using ::testing::TestWithParam;
84using ::testing::Values;
85using ::testing::Combine;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000086
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000087// Maximum number of bytes in output bitstream.
Alex Loiko7a3e43a2019-01-29 12:27:08 +010088const size_t kMaxBytes = 2000;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000089// Sample rate of Opus.
Peter Kastingdce40cf2015-08-24 14:52:23 -070090const size_t kOpusRateKhz = 48;
minyue@webrtc.orgf563e852014-07-18 21:11:27 +000091// Number of samples-per-channel in a 20 ms frame, sampled at 48 kHz.
Peter Kastingdce40cf2015-08-24 14:52:23 -070092const size_t kOpus20msFrameSamples = kOpusRateKhz * 20;
minyue@webrtc.orgf563e852014-07-18 21:11:27 +000093// Number of samples-per-channel in a 10 ms frame, sampled at 48 kHz.
Peter Kastingdce40cf2015-08-24 14:52:23 -070094const size_t kOpus10msFrameSamples = kOpusRateKhz * 10;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000095
Alex Loiko50b8c392019-04-03 15:12:01 +020096class OpusTest : public TestWithParam<::testing::tuple<int, int, bool>> {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000097 protected:
98 OpusTest();
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000099
minyue3cea2562015-11-10 03:49:26 -0800100 void TestDtxEffect(bool dtx, int block_length_ms);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000101
soren28dc2852017-04-06 05:48:36 -0700102 void TestCbrEffect(bool dtx, int block_length_ms);
103
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000104 // Prepare |speech_data_| for encoding, read from a hard-coded file.
105 // After preparation, |speech_data_.GetNextBlock()| returns a pointer to a
106 // block of |block_length_ms| milliseconds. The data is looped every
107 // |loop_length_ms| milliseconds.
Peter Kasting69558702016-01-12 16:26:35 -0800108 void PrepareSpeechData(size_t channel,
109 int block_length_ms,
110 int loop_length_ms);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000111
112 int EncodeDecode(WebRtcOpusEncInst* encoder,
kwiberg288886b2015-11-06 01:21:35 -0800113 rtc::ArrayView<const int16_t> input_audio,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000114 WebRtcOpusDecInst* decoder,
115 int16_t* output_audio,
116 int16_t* audio_type);
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000117
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000118 void SetMaxPlaybackRate(WebRtcOpusEncInst* encoder,
Yves Gerey665174f2018-06-19 15:03:05 +0200119 opus_int32 expect,
120 int32_t set);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000121
Yves Gerey665174f2018-06-19 15:03:05 +0200122 void CheckAudioBounded(const int16_t* audio,
123 size_t samples,
124 size_t channels,
minyue3cea2562015-11-10 03:49:26 -0800125 uint16_t bound) const;
126
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000127 WebRtcOpusEncInst* opus_encoder_;
128 WebRtcOpusDecInst* opus_decoder_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000129
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000130 AudioLoop speech_data_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000131 uint8_t bitstream_[kMaxBytes];
Peter Kastingdce40cf2015-08-24 14:52:23 -0700132 size_t encoded_bytes_;
Peter Kasting69558702016-01-12 16:26:35 -0800133 size_t channels_;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000134 int application_;
Alex Loiko50b8c392019-04-03 15:12:01 +0200135 bool force_multistream_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000136};
137
138OpusTest::OpusTest()
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000139 : opus_encoder_(NULL),
140 opus_decoder_(NULL),
141 encoded_bytes_(0),
Peter Kasting69558702016-01-12 16:26:35 -0800142 channels_(static_cast<size_t>(::testing::get<0>(GetParam()))),
Alex Loiko50b8c392019-04-03 15:12:01 +0200143 application_(::testing::get<1>(GetParam())),
144 force_multistream_(::testing::get<2>(GetParam())) {}
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000145
Yves Gerey665174f2018-06-19 15:03:05 +0200146void OpusTest::PrepareSpeechData(size_t channel,
147 int block_length_ms,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000148 int loop_length_ms) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100149 std::map<int, std::string> channel_to_basename = {
150 {1, "audio_coding/testfile32kHz"},
151 {2, "audio_coding/teststereo32kHz"},
152 {4, "audio_coding/speech_4_channels_48k_one_second"}};
153 std::map<int, std::string> channel_to_suffix = {
154 {1, "pcm"}, {2, "pcm"}, {4, "wav"}};
Yves Gerey665174f2018-06-19 15:03:05 +0200155 const std::string file_name = webrtc::test::ResourcePath(
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100156 channel_to_basename[channel], channel_to_suffix[channel]);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000157 if (loop_length_ms < block_length_ms) {
158 loop_length_ms = block_length_ms;
159 }
160 EXPECT_TRUE(speech_data_.Init(file_name,
161 loop_length_ms * kOpusRateKhz * channel,
162 block_length_ms * kOpusRateKhz * channel));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000163}
164
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000165void OpusTest::SetMaxPlaybackRate(WebRtcOpusEncInst* encoder,
166 opus_int32 expect,
167 int32_t set) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000168 opus_int32 bandwidth;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000169 EXPECT_EQ(0, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, set));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100170 EXPECT_EQ(0, WebRtcOpus_GetMaxPlaybackRate(opus_encoder_, &bandwidth));
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000171 EXPECT_EQ(expect, bandwidth);
172}
173
Yves Gerey665174f2018-06-19 15:03:05 +0200174void OpusTest::CheckAudioBounded(const int16_t* audio,
175 size_t samples,
176 size_t channels,
177 uint16_t bound) const {
minyue3cea2562015-11-10 03:49:26 -0800178 for (size_t i = 0; i < samples; ++i) {
Peter Kasting69558702016-01-12 16:26:35 -0800179 for (size_t c = 0; c < channels; ++c) {
minyue3cea2562015-11-10 03:49:26 -0800180 ASSERT_GE(audio[i * channels + c], -bound);
181 ASSERT_LE(audio[i * channels + c], bound);
182 }
183 }
184}
185
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000186int OpusTest::EncodeDecode(WebRtcOpusEncInst* encoder,
kwiberg288886b2015-11-06 01:21:35 -0800187 rtc::ArrayView<const int16_t> input_audio,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000188 WebRtcOpusDecInst* decoder,
189 int16_t* output_audio,
190 int16_t* audio_type) {
Yves Gerey665174f2018-06-19 15:03:05 +0200191 int encoded_bytes_int =
192 WebRtcOpus_Encode(encoder, input_audio.data(),
193 rtc::CheckedDivExact(input_audio.size(), channels_),
194 kMaxBytes, bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700195 EXPECT_GE(encoded_bytes_int, 0);
196 encoded_bytes_ = static_cast<size_t>(encoded_bytes_int);
minyuel6d92bf52015-09-23 15:20:39 +0200197 int est_len = WebRtcOpus_DurationEst(decoder, bitstream_, encoded_bytes_);
Yves Gerey665174f2018-06-19 15:03:05 +0200198 int act_len = WebRtcOpus_Decode(decoder, bitstream_, encoded_bytes_,
199 output_audio, audio_type);
minyuel6d92bf52015-09-23 15:20:39 +0200200 EXPECT_EQ(est_len, act_len);
201 return act_len;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000202}
203
204// Test if encoder/decoder can enter DTX mode properly and do not enter DTX when
205// they should not. This test is signal dependent.
minyue3cea2562015-11-10 03:49:26 -0800206void OpusTest::TestDtxEffect(bool dtx, int block_length_ms) {
207 PrepareSpeechData(channels_, block_length_ms, 2000);
208 const size_t samples = kOpusRateKhz * block_length_ms;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000209
210 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200211 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
212 force_multistream_);
213 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
214 force_multistream_);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000215
216 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200217 EXPECT_EQ(
218 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000219
220 // Set input audio as silence.
minyue3cea2562015-11-10 03:49:26 -0800221 std::vector<int16_t> silence(samples * channels_, 0);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000222
223 // Setting DTX.
Yves Gerey665174f2018-06-19 15:03:05 +0200224 EXPECT_EQ(0, dtx ? WebRtcOpus_EnableDtx(opus_encoder_)
225 : WebRtcOpus_DisableDtx(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000226
227 int16_t audio_type;
minyue3cea2562015-11-10 03:49:26 -0800228 int16_t* output_data_decode = new int16_t[samples * channels_];
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000229
230 for (int i = 0; i < 100; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200231 EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode(
232 opus_encoder_, speech_data_.GetNextBlock(),
233 opus_decoder_, output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000234 // If not DTX, it should never enter DTX mode. If DTX, we do not care since
235 // whether it enters DTX depends on the signal type.
236 if (!dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700237 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000238 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
239 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000240 EXPECT_EQ(0, audio_type); // Speech.
241 }
242 }
243
244 // We input some silent segments. In DTX mode, the encoder will stop sending.
245 // However, DTX may happen after a while.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000246 for (int i = 0; i < 30; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200247 EXPECT_EQ(samples, static_cast<size_t>(
248 EncodeDecode(opus_encoder_, silence, opus_decoder_,
249 output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000250 if (!dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700251 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000252 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
253 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000254 EXPECT_EQ(0, audio_type); // Speech.
Peter Kasting728d9032015-06-11 14:31:38 -0700255 } else if (encoded_bytes_ == 1) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000256 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
257 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000258 EXPECT_EQ(2, audio_type); // Comfort noise.
259 break;
260 }
261 }
262
Minyue Li092041c2015-05-11 12:19:35 +0200263 // When Opus is in DTX, it wakes up in a regular basis. It sends two packets,
264 // one with an arbitrary size and the other of 1-byte, then stops sending for
minyue3cea2562015-11-10 03:49:26 -0800265 // a certain number of frames.
266
267 // |max_dtx_frames| is the maximum number of frames Opus can stay in DTX.
268 const int max_dtx_frames = 400 / block_length_ms + 1;
269
270 // We run |kRunTimeMs| milliseconds of pure silence.
minyue58e08cb2016-02-24 03:49:19 -0800271 const int kRunTimeMs = 4500;
minyue3cea2562015-11-10 03:49:26 -0800272
273 // We check that, after a |kCheckTimeMs| milliseconds (given that the CNG in
274 // Opus needs time to adapt), the absolute values of DTX decoded signal are
275 // bounded by |kOutputValueBound|.
minyue58e08cb2016-02-24 03:49:19 -0800276 const int kCheckTimeMs = 4000;
minyue3cea2562015-11-10 03:49:26 -0800277
278#if defined(OPUS_FIXED_POINT)
minyuel7e937e92016-02-29 10:24:15 +0100279 // Fixed-point Opus generates a random (comfort) noise, which has a less
280 // predictable value bound than its floating-point Opus. This value depends on
281 // input signal, and the time window for checking the output values (between
282 // |kCheckTimeMs| and |kRunTimeMs|).
283 const uint16_t kOutputValueBound = 30;
284
minyue3cea2562015-11-10 03:49:26 -0800285#else
minyue58e08cb2016-02-24 03:49:19 -0800286 const uint16_t kOutputValueBound = 2;
minyue3cea2562015-11-10 03:49:26 -0800287#endif
288
289 int time = 0;
290 while (time < kRunTimeMs) {
291 // DTX mode is maintained for maximum |max_dtx_frames| frames.
292 int i = 0;
293 for (; i < max_dtx_frames; ++i) {
294 time += block_length_ms;
Yves Gerey665174f2018-06-19 15:03:05 +0200295 EXPECT_EQ(samples, static_cast<size_t>(
296 EncodeDecode(opus_encoder_, silence, opus_decoder_,
297 output_data_decode, &audio_type)));
Minyue Li092041c2015-05-11 12:19:35 +0200298 if (dtx) {
minyue3cea2562015-11-10 03:49:26 -0800299 if (encoded_bytes_ > 1)
300 break;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700301 EXPECT_EQ(0U, encoded_bytes_) // Send 0 byte.
Minyue Li092041c2015-05-11 12:19:35 +0200302 << "Opus should have entered DTX mode.";
303 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
304 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
305 EXPECT_EQ(2, audio_type); // Comfort noise.
minyue3cea2562015-11-10 03:49:26 -0800306 if (time >= kCheckTimeMs) {
307 CheckAudioBounded(output_data_decode, samples, channels_,
308 kOutputValueBound);
309 }
Minyue Li092041c2015-05-11 12:19:35 +0200310 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700311 EXPECT_GT(encoded_bytes_, 1U);
Minyue Li092041c2015-05-11 12:19:35 +0200312 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
313 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
314 EXPECT_EQ(0, audio_type); // Speech.
315 }
316 }
317
minyue3cea2562015-11-10 03:49:26 -0800318 if (dtx) {
319 // With DTX, Opus must stop transmission for some time.
320 EXPECT_GT(i, 1);
321 }
Minyue Li092041c2015-05-11 12:19:35 +0200322
minyue3cea2562015-11-10 03:49:26 -0800323 // We expect a normal payload.
Minyue Li092041c2015-05-11 12:19:35 +0200324 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
325 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
326 EXPECT_EQ(0, audio_type); // Speech.
327
328 // Enters DTX again immediately.
minyue3cea2562015-11-10 03:49:26 -0800329 time += block_length_ms;
Yves Gerey665174f2018-06-19 15:03:05 +0200330 EXPECT_EQ(samples, static_cast<size_t>(
331 EncodeDecode(opus_encoder_, silence, opus_decoder_,
332 output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000333 if (dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700334 EXPECT_EQ(1U, encoded_bytes_); // Send 1 byte.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000335 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
336 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000337 EXPECT_EQ(2, audio_type); // Comfort noise.
minyue3cea2562015-11-10 03:49:26 -0800338 if (time >= kCheckTimeMs) {
339 CheckAudioBounded(output_data_decode, samples, channels_,
340 kOutputValueBound);
341 }
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000342 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700343 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000344 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
345 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000346 EXPECT_EQ(0, audio_type); // Speech.
347 }
348 }
349
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000350 silence[0] = 10000;
351 if (dtx) {
352 // Verify that encoder/decoder can jump out from DTX mode.
Yves Gerey665174f2018-06-19 15:03:05 +0200353 EXPECT_EQ(samples, static_cast<size_t>(
354 EncodeDecode(opus_encoder_, silence, opus_decoder_,
355 output_data_decode, &audio_type)));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700356 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000357 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
358 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000359 EXPECT_EQ(0, audio_type); // Speech.
360 }
361
362 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000363 delete[] output_data_decode;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000364 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
365 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000366}
367
soren28dc2852017-04-06 05:48:36 -0700368// Test if CBR does what we expect.
369void OpusTest::TestCbrEffect(bool cbr, int block_length_ms) {
370 PrepareSpeechData(channels_, block_length_ms, 2000);
371 const size_t samples = kOpusRateKhz * block_length_ms;
372
373 int32_t max_pkt_size_diff = 0;
374 int32_t prev_pkt_size = 0;
375
376 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200377 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
378 force_multistream_);
379 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
380 force_multistream_);
soren28dc2852017-04-06 05:48:36 -0700381
382 // Set bitrate.
383 EXPECT_EQ(
384 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
385
386 // Setting CBR.
387 EXPECT_EQ(0, cbr ? WebRtcOpus_EnableCbr(opus_encoder_)
388 : WebRtcOpus_DisableCbr(opus_encoder_));
389
390 int16_t audio_type;
391 std::vector<int16_t> audio_out(samples * channels_);
392 for (int i = 0; i < 100; ++i) {
393 EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode(
394 opus_encoder_, speech_data_.GetNextBlock(),
395 opus_decoder_, audio_out.data(), &audio_type)));
396
397 if (prev_pkt_size > 0) {
398 int32_t diff = std::abs((int32_t)encoded_bytes_ - prev_pkt_size);
399 max_pkt_size_diff = std::max(max_pkt_size_diff, diff);
400 }
Mirko Bonadei737e0732017-10-19 09:00:17 +0200401 prev_pkt_size = rtc::checked_cast<int32_t>(encoded_bytes_);
soren28dc2852017-04-06 05:48:36 -0700402 }
403
404 if (cbr) {
405 EXPECT_EQ(max_pkt_size_diff, 0);
406 } else {
407 EXPECT_GT(max_pkt_size_diff, 0);
408 }
409
410 // Free memory.
411 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
412 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
413}
414
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000415// Test failing Create.
henrika1d34fe92015-06-16 10:04:20 +0200416TEST(OpusTest, OpusCreateFail) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000417 WebRtcOpusEncInst* opus_encoder;
418 WebRtcOpusDecInst* opus_decoder;
419
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000420 // Test to see that an invalid pointer is caught.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000421 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(NULL, 1, 0));
422 // Invalid channel number.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100423 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 257, 0));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000424 // Invalid applciation mode.
425 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 1, 2));
426
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000427 EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(NULL, 1));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000428 // Invalid channel number.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100429 EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(&opus_decoder, 257));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000430}
431
432// Test failing Free.
henrika1d34fe92015-06-16 10:04:20 +0200433TEST(OpusTest, OpusFreeFail) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000434 // Test to see that an invalid pointer is caught.
435 EXPECT_EQ(-1, WebRtcOpus_EncoderFree(NULL));
436 EXPECT_EQ(-1, WebRtcOpus_DecoderFree(NULL));
437}
438
439// Test normal Create and Free.
henrika1d34fe92015-06-16 10:04:20 +0200440TEST_P(OpusTest, OpusCreateFree) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200441 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
442 force_multistream_);
443 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
444 force_multistream_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000445 EXPECT_TRUE(opus_encoder_ != NULL);
446 EXPECT_TRUE(opus_decoder_ != NULL);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000447 // Free encoder and decoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000448 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
449 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000450}
451
Alex Loiko50b8c392019-04-03 15:12:01 +0200452#define ENCODER_CTL(inst, vargs) \
453 inst->encoder \
454 ? opus_encoder_ctl(inst->encoder, vargs) \
455 : opus_multistream_encoder_ctl(inst->multistream_encoder, vargs)
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100456
henrika1d34fe92015-06-16 10:04:20 +0200457TEST_P(OpusTest, OpusEncodeDecode) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000458 PrepareSpeechData(channels_, 20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000459
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000460 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200461 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
462 force_multistream_);
463 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
464 force_multistream_);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000465
466 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200467 EXPECT_EQ(
468 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000469
470 // Check number of channels for decoder.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000471 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
472
473 // Check application mode.
474 opus_int32 app;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100475 ENCODER_CTL(opus_encoder_, OPUS_GET_APPLICATION(&app));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000476 EXPECT_EQ(application_ == 0 ? OPUS_APPLICATION_VOIP : OPUS_APPLICATION_AUDIO,
477 app);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000478
479 // Encode & decode.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000480 int16_t audio_type;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000481 int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_];
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000482 EXPECT_EQ(kOpus20msFrameSamples,
kwiberg288886b2015-11-06 01:21:35 -0800483 static_cast<size_t>(
484 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
485 opus_decoder_, output_data_decode, &audio_type)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000486
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000487 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000488 delete[] output_data_decode;
489 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
490 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000491}
492
henrika1d34fe92015-06-16 10:04:20 +0200493TEST_P(OpusTest, OpusSetBitRate) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000494 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000495 EXPECT_EQ(-1, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000496
497 // Create encoder memory, try with different bitrates.
Alex Loiko50b8c392019-04-03 15:12:01 +0200498 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
499 force_multistream_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000500 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 30000));
501 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
502 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 300000));
503 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 600000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000504
505 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000506 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000507}
508
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000509TEST_P(OpusTest, OpusSetComplexity) {
minyue@webrtc.org04546882014-03-07 08:55:48 +0000510 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000511 EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 9));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000512
513 // Create encoder memory, try with different complexities.
Alex Loiko50b8c392019-04-03 15:12:01 +0200514 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
515 force_multistream_);
minyue@webrtc.org04546882014-03-07 08:55:48 +0000516
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000517 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 0));
518 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 10));
519 EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 11));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000520
521 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000522 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000523}
524
Alex Luebseeb27652017-11-20 11:13:56 -0800525TEST_P(OpusTest, OpusSetBandwidth) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100526 if (channels_ > 2) {
527 // TODO(webrtc:10217): investigate why multi-stream Opus reports
528 // narrowband when it's configured with FULLBAND.
529 return;
530 }
Alex Luebseeb27652017-11-20 11:13:56 -0800531 PrepareSpeechData(channels_, 20, 20);
532
533 int16_t audio_type;
534 std::unique_ptr<int16_t[]> output_data_decode(
535 new int16_t[kOpus20msFrameSamples * channels_]());
536
537 // Test without creating encoder memory.
538 EXPECT_EQ(-1,
539 WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
540 EXPECT_EQ(-1, WebRtcOpus_GetBandwidth(opus_encoder_));
541
542 // Create encoder memory, try with different bandwidths.
Alex Loiko50b8c392019-04-03 15:12:01 +0200543 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
544 force_multistream_);
545 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
546 force_multistream_);
Alex Luebseeb27652017-11-20 11:13:56 -0800547
548 EXPECT_EQ(-1, WebRtcOpus_SetBandwidth(opus_encoder_,
549 OPUS_BANDWIDTH_NARROWBAND - 1));
550 EXPECT_EQ(0,
551 WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
552 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
553 output_data_decode.get(), &audio_type);
554 EXPECT_EQ(OPUS_BANDWIDTH_NARROWBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
555 EXPECT_EQ(0, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND));
556 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
557 output_data_decode.get(), &audio_type);
558 EXPECT_EQ(OPUS_BANDWIDTH_FULLBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
559 EXPECT_EQ(
560 -1, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND + 1));
561 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
562 output_data_decode.get(), &audio_type);
563 EXPECT_EQ(OPUS_BANDWIDTH_FULLBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
564
565 // Free memory.
566 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
567 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
568}
569
minyuec8299f92016-09-27 02:08:47 -0700570TEST_P(OpusTest, OpusForceChannels) {
571 // Test without creating encoder memory.
572 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
573
Alex Loiko50b8c392019-04-03 15:12:01 +0200574 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
575 force_multistream_);
576 ASSERT_NE(nullptr, opus_encoder_);
minyuec8299f92016-09-27 02:08:47 -0700577
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100578 if (channels_ >= 2) {
minyuec8299f92016-09-27 02:08:47 -0700579 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 3));
580 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
581 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
582 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
583 } else {
584 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
585 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
586 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
587 }
588
589 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
590}
591
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000592// Encode and decode one frame, initialize the decoder and
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000593// decode once more.
henrika1d34fe92015-06-16 10:04:20 +0200594TEST_P(OpusTest, OpusDecodeInit) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000595 PrepareSpeechData(channels_, 20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000596
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000597 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200598 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
599 force_multistream_);
600 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
601 force_multistream_);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000602
603 // Encode & decode.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000604 int16_t audio_type;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000605 int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_];
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000606 EXPECT_EQ(kOpus20msFrameSamples,
kwiberg288886b2015-11-06 01:21:35 -0800607 static_cast<size_t>(
608 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
609 opus_decoder_, output_data_decode, &audio_type)));
minyue@webrtc.org52bc4f42014-12-04 11:00:50 +0000610
Karl Wiberg43766482015-08-27 15:22:11 +0200611 WebRtcOpus_DecoderInit(opus_decoder_);
minyue@webrtc.org52bc4f42014-12-04 11:00:50 +0000612
613 EXPECT_EQ(kOpus20msFrameSamples,
Yves Gerey665174f2018-06-19 15:03:05 +0200614 static_cast<size_t>(
615 WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_,
616 output_data_decode, &audio_type)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000617
618 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000619 delete[] output_data_decode;
620 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
621 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000622}
623
henrika1d34fe92015-06-16 10:04:20 +0200624TEST_P(OpusTest, OpusEnableDisableFec) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000625 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000626 EXPECT_EQ(-1, WebRtcOpus_EnableFec(opus_encoder_));
627 EXPECT_EQ(-1, WebRtcOpus_DisableFec(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000628
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000629 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200630 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
631 force_multistream_);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000632
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000633 EXPECT_EQ(0, WebRtcOpus_EnableFec(opus_encoder_));
634 EXPECT_EQ(0, WebRtcOpus_DisableFec(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000635
636 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000637 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000638}
639
henrika1d34fe92015-06-16 10:04:20 +0200640TEST_P(OpusTest, OpusEnableDisableDtx) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000641 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000642 EXPECT_EQ(-1, WebRtcOpus_EnableDtx(opus_encoder_));
643 EXPECT_EQ(-1, WebRtcOpus_DisableDtx(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000644
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000645 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200646 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
647 force_multistream_);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000648
649 opus_int32 dtx;
650
651 // DTX is off by default.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100652 ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000653 EXPECT_EQ(0, dtx);
654
655 // Test to enable DTX.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000656 EXPECT_EQ(0, WebRtcOpus_EnableDtx(opus_encoder_));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100657 ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000658 EXPECT_EQ(1, dtx);
659
660 // Test to disable DTX.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000661 EXPECT_EQ(0, WebRtcOpus_DisableDtx(opus_encoder_));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100662 ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000663 EXPECT_EQ(0, dtx);
664
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000665 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000666 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000667}
668
henrika1d34fe92015-06-16 10:04:20 +0200669TEST_P(OpusTest, OpusDtxOff) {
minyue3cea2562015-11-10 03:49:26 -0800670 TestDtxEffect(false, 10);
671 TestDtxEffect(false, 20);
672 TestDtxEffect(false, 40);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000673}
674
henrika1d34fe92015-06-16 10:04:20 +0200675TEST_P(OpusTest, OpusDtxOn) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100676 if (channels_ > 2) {
677 // TODO(webrtc:10218): adapt the test to the sizes and order of multi-stream
678 // DTX packets.
679 return;
680 }
minyue3cea2562015-11-10 03:49:26 -0800681 TestDtxEffect(true, 10);
682 TestDtxEffect(true, 20);
683 TestDtxEffect(true, 40);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000684}
685
soren28dc2852017-04-06 05:48:36 -0700686TEST_P(OpusTest, OpusCbrOff) {
687 TestCbrEffect(false, 10);
688 TestCbrEffect(false, 20);
689 TestCbrEffect(false, 40);
690}
691
692TEST_P(OpusTest, OpusCbrOn) {
693 TestCbrEffect(true, 10);
694 TestCbrEffect(true, 20);
695 TestCbrEffect(true, 40);
696}
697
henrika1d34fe92015-06-16 10:04:20 +0200698TEST_P(OpusTest, OpusSetPacketLossRate) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000699 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000700 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000701
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000702 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200703 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
704 force_multistream_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000705
706 EXPECT_EQ(0, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
707 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, -1));
708 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 101));
709
710 // Free memory.
711 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
712}
713
henrika1d34fe92015-06-16 10:04:20 +0200714TEST_P(OpusTest, OpusSetMaxPlaybackRate) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000715 // Test without creating encoder memory.
716 EXPECT_EQ(-1, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, 20000));
717
718 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200719 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
720 force_multistream_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000721
722 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 48000);
723 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 24001);
724 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 24000);
725 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 16001);
726 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 16000);
727 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 12001);
728 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 12000);
729 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 8001);
730 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 8000);
731 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 4000);
732
733 // Free memory.
734 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
735}
736
737// Test PLC.
henrika1d34fe92015-06-16 10:04:20 +0200738TEST_P(OpusTest, OpusDecodePlc) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000739 PrepareSpeechData(channels_, 20, 20);
740
741 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200742 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
743 force_multistream_);
744 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
745 force_multistream_);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000746
747 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200748 EXPECT_EQ(
749 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000750
751 // Check number of channels for decoder.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000752 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000753
754 // Encode & decode.
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000755 int16_t audio_type;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000756 int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_];
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000757 EXPECT_EQ(kOpus20msFrameSamples,
kwiberg288886b2015-11-06 01:21:35 -0800758 static_cast<size_t>(
759 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
760 opus_decoder_, output_data_decode, &audio_type)));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000761
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000762 // Call decoder PLC.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000763 int16_t* plc_buffer = new int16_t[kOpus20msFrameSamples * channels_];
Yves Gerey665174f2018-06-19 15:03:05 +0200764 EXPECT_EQ(kOpus20msFrameSamples, static_cast<size_t>(WebRtcOpus_DecodePlc(
765 opus_decoder_, plc_buffer, 1)));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000766
767 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000768 delete[] plc_buffer;
769 delete[] output_data_decode;
770 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
771 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000772}
773
774// Duration estimation.
henrika1d34fe92015-06-16 10:04:20 +0200775TEST_P(OpusTest, OpusDurationEstimation) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000776 PrepareSpeechData(channels_, 20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000777
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000778 // Create.
Alex Loiko50b8c392019-04-03 15:12:01 +0200779 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
780 force_multistream_);
781 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
782 force_multistream_);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000783
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000784 // 10 ms. We use only first 10 ms of a 20 ms block.
kwiberg288886b2015-11-06 01:21:35 -0800785 auto speech_block = speech_data_.GetNextBlock();
786 int encoded_bytes_int = WebRtcOpus_Encode(
787 opus_encoder_, speech_block.data(),
Yves Gerey665174f2018-06-19 15:03:05 +0200788 rtc::CheckedDivExact(speech_block.size(), 2 * channels_), kMaxBytes,
789 bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700790 EXPECT_GE(encoded_bytes_int, 0);
Yves Gerey665174f2018-06-19 15:03:05 +0200791 EXPECT_EQ(
792 kOpus10msFrameSamples,
793 static_cast<size_t>(WebRtcOpus_DurationEst(
794 opus_decoder_, bitstream_, static_cast<size_t>(encoded_bytes_int))));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000795
796 // 20 ms
kwiberg288886b2015-11-06 01:21:35 -0800797 speech_block = speech_data_.GetNextBlock();
Yves Gerey665174f2018-06-19 15:03:05 +0200798 encoded_bytes_int =
799 WebRtcOpus_Encode(opus_encoder_, speech_block.data(),
800 rtc::CheckedDivExact(speech_block.size(), channels_),
801 kMaxBytes, bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700802 EXPECT_GE(encoded_bytes_int, 0);
Yves Gerey665174f2018-06-19 15:03:05 +0200803 EXPECT_EQ(
804 kOpus20msFrameSamples,
805 static_cast<size_t>(WebRtcOpus_DurationEst(
806 opus_decoder_, bitstream_, static_cast<size_t>(encoded_bytes_int))));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000807
808 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000809 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
810 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000811}
812
henrika1d34fe92015-06-16 10:04:20 +0200813TEST_P(OpusTest, OpusDecodeRepacketized) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100814 if (channels_ > 2) {
815 // As per the Opus documentation
816 // https://mf4.xiph.org/jenkins/view/opus/job/opus/ws/doc/html/group__opus__repacketizer.html#details,
817 // multiple streams are not supported.
818 return;
819 }
minyuea613eb62017-03-14 14:33:30 -0700820 constexpr size_t kPackets = 6;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000821
822 PrepareSpeechData(channels_, 20, 20 * kPackets);
823
824 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200825 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
826 force_multistream_);
827 ASSERT_NE(nullptr, opus_encoder_);
828 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
829 force_multistream_);
830 ASSERT_NE(nullptr, opus_decoder_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000831
832 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200833 EXPECT_EQ(
834 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000835
836 // Check number of channels for decoder.
837 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
838
839 // Encode & decode.
840 int16_t audio_type;
kwiberg91d97562016-02-14 01:10:03 -0800841 std::unique_ptr<int16_t[]> output_data_decode(
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000842 new int16_t[kPackets * kOpus20msFrameSamples * channels_]);
843 OpusRepacketizer* rp = opus_repacketizer_create();
844
minyuea613eb62017-03-14 14:33:30 -0700845 size_t num_packets = 0;
846 constexpr size_t kMaxCycles = 100;
847 for (size_t idx = 0; idx < kMaxCycles; ++idx) {
kwiberg288886b2015-11-06 01:21:35 -0800848 auto speech_block = speech_data_.GetNextBlock();
849 encoded_bytes_ =
850 WebRtcOpus_Encode(opus_encoder_, speech_block.data(),
Peter Kasting69558702016-01-12 16:26:35 -0800851 rtc::CheckedDivExact(speech_block.size(), channels_),
kwiberg288886b2015-11-06 01:21:35 -0800852 kMaxBytes, bitstream_);
Yves Gerey665174f2018-06-19 15:03:05 +0200853 if (opus_repacketizer_cat(rp, bitstream_,
854 rtc::checked_cast<opus_int32>(encoded_bytes_)) ==
855 OPUS_OK) {
minyuea613eb62017-03-14 14:33:30 -0700856 ++num_packets;
857 if (num_packets == kPackets) {
858 break;
859 }
860 } else {
861 // Opus repacketizer cannot guarantee a success. We try again if it fails.
862 opus_repacketizer_init(rp);
863 num_packets = 0;
864 }
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000865 }
minyuea613eb62017-03-14 14:33:30 -0700866 EXPECT_EQ(kPackets, num_packets);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000867
868 encoded_bytes_ = opus_repacketizer_out(rp, bitstream_, kMaxBytes);
869
870 EXPECT_EQ(kOpus20msFrameSamples * kPackets,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700871 static_cast<size_t>(WebRtcOpus_DurationEst(
872 opus_decoder_, bitstream_, encoded_bytes_)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000873
874 EXPECT_EQ(kOpus20msFrameSamples * kPackets,
Yves Gerey665174f2018-06-19 15:03:05 +0200875 static_cast<size_t>(
876 WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_,
877 output_data_decode.get(), &audio_type)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000878
879 // Free memory.
880 opus_repacketizer_destroy(rp);
881 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
882 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
883}
884
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100885INSTANTIATE_TEST_SUITE_P(VariousMode,
886 OpusTest,
Alex Loiko50b8c392019-04-03 15:12:01 +0200887 ::testing::ValuesIn({
888 std::make_tuple(1, 0, true),
889 std::make_tuple(1, 1, true),
890 std::make_tuple(2, 0, false),
891 std::make_tuple(4, 0, false),
892 std::make_tuple(1, 1, false),
893 std::make_tuple(4, 1, false),
894 }));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000895
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000896} // namespace webrtc