blob: d506e60282f95bf7cd7306cdcb62e98f11e43dd8 [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};
Alex Loikoe5b94162019-04-08 17:19:41 +020028constexpr int kQuadTotalStreams = 2;
Alex Loiko50b8c392019-04-03 15:12:01 +020029constexpr int kQuadCoupledStreams = 2;
30
31constexpr unsigned char kStereoChannelMapping[] = {0, 1};
Alex Loikoe5b94162019-04-08 17:19:41 +020032constexpr int kStereoTotalStreams = 1;
Alex Loiko50b8c392019-04-03 15:12:01 +020033constexpr int kStereoCoupledStreams = 1;
34
35constexpr unsigned char kMonoChannelMapping[] = {0};
Alex Loikoe5b94162019-04-08 17:19:41 +020036constexpr int kMonoTotalStreams = 1;
Alex Loiko50b8c392019-04-03 15:12:01 +020037constexpr int kMonoCoupledStreams = 0;
38
39void CreateSingleOrMultiStreamEncoder(WebRtcOpusEncInst** opus_encoder,
40 int channels,
41 int application,
42 bool force_multistream = false) {
43 if (!force_multistream && (channels == 1 || channels == 2)) {
44 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(opus_encoder, channels, application));
45 } else if (force_multistream && channels == 1) {
46 EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
Alex Loikoe5b94162019-04-08 17:19:41 +020047 opus_encoder, channels, application, kMonoTotalStreams,
48 kMonoCoupledStreams, kMonoChannelMapping));
Alex Loiko50b8c392019-04-03 15:12:01 +020049 } else if (force_multistream && channels == 2) {
50 EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
Alex Loikoe5b94162019-04-08 17:19:41 +020051 opus_encoder, channels, application, kStereoTotalStreams,
52 kStereoCoupledStreams, kStereoChannelMapping));
Alex Loiko50b8c392019-04-03 15:12:01 +020053 } else if (channels == 4) {
54 EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
Alex Loikoe5b94162019-04-08 17:19:41 +020055 opus_encoder, channels, application, kQuadTotalStreams,
56 kQuadCoupledStreams, kQuadChannelMapping));
Alex Loiko50b8c392019-04-03 15:12:01 +020057 } else {
58 EXPECT_TRUE(false) << channels;
59 }
60}
61
62void CreateSingleOrMultiStreamDecoder(WebRtcOpusDecInst** opus_decoder,
63 int channels,
64 bool force_multistream = false) {
65 if (!force_multistream && (channels == 1 || channels == 2)) {
66 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(opus_decoder, channels));
67 } else if (channels == 1) {
Alex Loikoe5b94162019-04-08 17:19:41 +020068 EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(
69 opus_decoder, channels, kMonoTotalStreams,
70 kMonoCoupledStreams, kMonoChannelMapping));
Alex Loiko50b8c392019-04-03 15:12:01 +020071 } else if (channels == 2) {
Alex Loikoe5b94162019-04-08 17:19:41 +020072 EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(
73 opus_decoder, channels, kStereoTotalStreams,
74 kStereoCoupledStreams, kStereoChannelMapping));
Alex Loiko50b8c392019-04-03 15:12:01 +020075 } else if (channels == 4) {
Alex Loikoe5b94162019-04-08 17:19:41 +020076 EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(
77 opus_decoder, channels, kQuadTotalStreams,
78 kQuadCoupledStreams, kQuadChannelMapping));
Alex Loiko50b8c392019-04-03 15:12:01 +020079 } else {
80 EXPECT_TRUE(false) << channels;
81 }
82}
83} // namespace
84
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000085using test::AudioLoop;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000086using ::testing::TestWithParam;
87using ::testing::Values;
88using ::testing::Combine;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000089
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000090// Maximum number of bytes in output bitstream.
Alex Loiko7a3e43a2019-01-29 12:27:08 +010091const size_t kMaxBytes = 2000;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000092// Sample rate of Opus.
Peter Kastingdce40cf2015-08-24 14:52:23 -070093const size_t kOpusRateKhz = 48;
minyue@webrtc.orgf563e852014-07-18 21:11:27 +000094// Number of samples-per-channel in a 20 ms frame, sampled at 48 kHz.
Peter Kastingdce40cf2015-08-24 14:52:23 -070095const size_t kOpus20msFrameSamples = kOpusRateKhz * 20;
minyue@webrtc.orgf563e852014-07-18 21:11:27 +000096// Number of samples-per-channel in a 10 ms frame, sampled at 48 kHz.
Peter Kastingdce40cf2015-08-24 14:52:23 -070097const size_t kOpus10msFrameSamples = kOpusRateKhz * 10;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +000098
Alex Loiko50b8c392019-04-03 15:12:01 +020099class OpusTest : public TestWithParam<::testing::tuple<int, int, bool>> {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000100 protected:
101 OpusTest();
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000102
minyue3cea2562015-11-10 03:49:26 -0800103 void TestDtxEffect(bool dtx, int block_length_ms);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000104
soren28dc2852017-04-06 05:48:36 -0700105 void TestCbrEffect(bool dtx, int block_length_ms);
106
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000107 // Prepare |speech_data_| for encoding, read from a hard-coded file.
108 // After preparation, |speech_data_.GetNextBlock()| returns a pointer to a
109 // block of |block_length_ms| milliseconds. The data is looped every
110 // |loop_length_ms| milliseconds.
Peter Kasting69558702016-01-12 16:26:35 -0800111 void PrepareSpeechData(size_t channel,
112 int block_length_ms,
113 int loop_length_ms);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000114
115 int EncodeDecode(WebRtcOpusEncInst* encoder,
kwiberg288886b2015-11-06 01:21:35 -0800116 rtc::ArrayView<const int16_t> input_audio,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000117 WebRtcOpusDecInst* decoder,
118 int16_t* output_audio,
119 int16_t* audio_type);
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000120
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000121 void SetMaxPlaybackRate(WebRtcOpusEncInst* encoder,
Yves Gerey665174f2018-06-19 15:03:05 +0200122 opus_int32 expect,
123 int32_t set);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000124
Yves Gerey665174f2018-06-19 15:03:05 +0200125 void CheckAudioBounded(const int16_t* audio,
126 size_t samples,
127 size_t channels,
minyue3cea2562015-11-10 03:49:26 -0800128 uint16_t bound) const;
129
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000130 WebRtcOpusEncInst* opus_encoder_;
131 WebRtcOpusDecInst* opus_decoder_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000132
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000133 AudioLoop speech_data_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000134 uint8_t bitstream_[kMaxBytes];
Peter Kastingdce40cf2015-08-24 14:52:23 -0700135 size_t encoded_bytes_;
Peter Kasting69558702016-01-12 16:26:35 -0800136 size_t channels_;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000137 int application_;
Alex Loiko50b8c392019-04-03 15:12:01 +0200138 bool force_multistream_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000139};
140
141OpusTest::OpusTest()
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000142 : opus_encoder_(NULL),
143 opus_decoder_(NULL),
144 encoded_bytes_(0),
Peter Kasting69558702016-01-12 16:26:35 -0800145 channels_(static_cast<size_t>(::testing::get<0>(GetParam()))),
Alex Loiko50b8c392019-04-03 15:12:01 +0200146 application_(::testing::get<1>(GetParam())),
147 force_multistream_(::testing::get<2>(GetParam())) {}
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000148
Yves Gerey665174f2018-06-19 15:03:05 +0200149void OpusTest::PrepareSpeechData(size_t channel,
150 int block_length_ms,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000151 int loop_length_ms) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100152 std::map<int, std::string> channel_to_basename = {
153 {1, "audio_coding/testfile32kHz"},
154 {2, "audio_coding/teststereo32kHz"},
155 {4, "audio_coding/speech_4_channels_48k_one_second"}};
156 std::map<int, std::string> channel_to_suffix = {
157 {1, "pcm"}, {2, "pcm"}, {4, "wav"}};
Yves Gerey665174f2018-06-19 15:03:05 +0200158 const std::string file_name = webrtc::test::ResourcePath(
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100159 channel_to_basename[channel], channel_to_suffix[channel]);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000160 if (loop_length_ms < block_length_ms) {
161 loop_length_ms = block_length_ms;
162 }
163 EXPECT_TRUE(speech_data_.Init(file_name,
164 loop_length_ms * kOpusRateKhz * channel,
165 block_length_ms * kOpusRateKhz * channel));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000166}
167
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000168void OpusTest::SetMaxPlaybackRate(WebRtcOpusEncInst* encoder,
169 opus_int32 expect,
170 int32_t set) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000171 opus_int32 bandwidth;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000172 EXPECT_EQ(0, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, set));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100173 EXPECT_EQ(0, WebRtcOpus_GetMaxPlaybackRate(opus_encoder_, &bandwidth));
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000174 EXPECT_EQ(expect, bandwidth);
175}
176
Yves Gerey665174f2018-06-19 15:03:05 +0200177void OpusTest::CheckAudioBounded(const int16_t* audio,
178 size_t samples,
179 size_t channels,
180 uint16_t bound) const {
minyue3cea2562015-11-10 03:49:26 -0800181 for (size_t i = 0; i < samples; ++i) {
Peter Kasting69558702016-01-12 16:26:35 -0800182 for (size_t c = 0; c < channels; ++c) {
minyue3cea2562015-11-10 03:49:26 -0800183 ASSERT_GE(audio[i * channels + c], -bound);
184 ASSERT_LE(audio[i * channels + c], bound);
185 }
186 }
187}
188
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000189int OpusTest::EncodeDecode(WebRtcOpusEncInst* encoder,
kwiberg288886b2015-11-06 01:21:35 -0800190 rtc::ArrayView<const int16_t> input_audio,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000191 WebRtcOpusDecInst* decoder,
192 int16_t* output_audio,
193 int16_t* audio_type) {
Yves Gerey665174f2018-06-19 15:03:05 +0200194 int encoded_bytes_int =
195 WebRtcOpus_Encode(encoder, input_audio.data(),
196 rtc::CheckedDivExact(input_audio.size(), channels_),
197 kMaxBytes, bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700198 EXPECT_GE(encoded_bytes_int, 0);
199 encoded_bytes_ = static_cast<size_t>(encoded_bytes_int);
minyuel6d92bf52015-09-23 15:20:39 +0200200 int est_len = WebRtcOpus_DurationEst(decoder, bitstream_, encoded_bytes_);
Yves Gerey665174f2018-06-19 15:03:05 +0200201 int act_len = WebRtcOpus_Decode(decoder, bitstream_, encoded_bytes_,
202 output_audio, audio_type);
minyuel6d92bf52015-09-23 15:20:39 +0200203 EXPECT_EQ(est_len, act_len);
204 return act_len;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000205}
206
207// Test if encoder/decoder can enter DTX mode properly and do not enter DTX when
208// they should not. This test is signal dependent.
minyue3cea2562015-11-10 03:49:26 -0800209void OpusTest::TestDtxEffect(bool dtx, int block_length_ms) {
210 PrepareSpeechData(channels_, block_length_ms, 2000);
211 const size_t samples = kOpusRateKhz * block_length_ms;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000212
213 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200214 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
215 force_multistream_);
216 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
217 force_multistream_);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000218
219 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200220 EXPECT_EQ(
221 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000222
223 // Set input audio as silence.
minyue3cea2562015-11-10 03:49:26 -0800224 std::vector<int16_t> silence(samples * channels_, 0);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000225
226 // Setting DTX.
Yves Gerey665174f2018-06-19 15:03:05 +0200227 EXPECT_EQ(0, dtx ? WebRtcOpus_EnableDtx(opus_encoder_)
228 : WebRtcOpus_DisableDtx(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000229
230 int16_t audio_type;
minyue3cea2562015-11-10 03:49:26 -0800231 int16_t* output_data_decode = new int16_t[samples * channels_];
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000232
233 for (int i = 0; i < 100; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200234 EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode(
235 opus_encoder_, speech_data_.GetNextBlock(),
236 opus_decoder_, output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000237 // If not DTX, it should never enter DTX mode. If DTX, we do not care since
238 // whether it enters DTX depends on the signal type.
239 if (!dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700240 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000241 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
242 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000243 EXPECT_EQ(0, audio_type); // Speech.
244 }
245 }
246
247 // We input some silent segments. In DTX mode, the encoder will stop sending.
248 // However, DTX may happen after a while.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000249 for (int i = 0; i < 30; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200250 EXPECT_EQ(samples, static_cast<size_t>(
251 EncodeDecode(opus_encoder_, silence, opus_decoder_,
252 output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000253 if (!dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700254 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000255 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
256 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000257 EXPECT_EQ(0, audio_type); // Speech.
Peter Kasting728d9032015-06-11 14:31:38 -0700258 } else if (encoded_bytes_ == 1) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000259 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
260 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000261 EXPECT_EQ(2, audio_type); // Comfort noise.
262 break;
263 }
264 }
265
Minyue Li092041c2015-05-11 12:19:35 +0200266 // When Opus is in DTX, it wakes up in a regular basis. It sends two packets,
267 // one with an arbitrary size and the other of 1-byte, then stops sending for
minyue3cea2562015-11-10 03:49:26 -0800268 // a certain number of frames.
269
270 // |max_dtx_frames| is the maximum number of frames Opus can stay in DTX.
271 const int max_dtx_frames = 400 / block_length_ms + 1;
272
273 // We run |kRunTimeMs| milliseconds of pure silence.
minyue58e08cb2016-02-24 03:49:19 -0800274 const int kRunTimeMs = 4500;
minyue3cea2562015-11-10 03:49:26 -0800275
276 // We check that, after a |kCheckTimeMs| milliseconds (given that the CNG in
277 // Opus needs time to adapt), the absolute values of DTX decoded signal are
278 // bounded by |kOutputValueBound|.
minyue58e08cb2016-02-24 03:49:19 -0800279 const int kCheckTimeMs = 4000;
minyue3cea2562015-11-10 03:49:26 -0800280
281#if defined(OPUS_FIXED_POINT)
minyuel7e937e92016-02-29 10:24:15 +0100282 // Fixed-point Opus generates a random (comfort) noise, which has a less
283 // predictable value bound than its floating-point Opus. This value depends on
284 // input signal, and the time window for checking the output values (between
285 // |kCheckTimeMs| and |kRunTimeMs|).
286 const uint16_t kOutputValueBound = 30;
287
minyue3cea2562015-11-10 03:49:26 -0800288#else
minyue58e08cb2016-02-24 03:49:19 -0800289 const uint16_t kOutputValueBound = 2;
minyue3cea2562015-11-10 03:49:26 -0800290#endif
291
292 int time = 0;
293 while (time < kRunTimeMs) {
294 // DTX mode is maintained for maximum |max_dtx_frames| frames.
295 int i = 0;
296 for (; i < max_dtx_frames; ++i) {
297 time += block_length_ms;
Yves Gerey665174f2018-06-19 15:03:05 +0200298 EXPECT_EQ(samples, static_cast<size_t>(
299 EncodeDecode(opus_encoder_, silence, opus_decoder_,
300 output_data_decode, &audio_type)));
Minyue Li092041c2015-05-11 12:19:35 +0200301 if (dtx) {
minyue3cea2562015-11-10 03:49:26 -0800302 if (encoded_bytes_ > 1)
303 break;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700304 EXPECT_EQ(0U, encoded_bytes_) // Send 0 byte.
Minyue Li092041c2015-05-11 12:19:35 +0200305 << "Opus should have entered DTX mode.";
306 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
307 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
308 EXPECT_EQ(2, audio_type); // Comfort noise.
minyue3cea2562015-11-10 03:49:26 -0800309 if (time >= kCheckTimeMs) {
310 CheckAudioBounded(output_data_decode, samples, channels_,
311 kOutputValueBound);
312 }
Minyue Li092041c2015-05-11 12:19:35 +0200313 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700314 EXPECT_GT(encoded_bytes_, 1U);
Minyue Li092041c2015-05-11 12:19:35 +0200315 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
316 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
317 EXPECT_EQ(0, audio_type); // Speech.
318 }
319 }
320
minyue3cea2562015-11-10 03:49:26 -0800321 if (dtx) {
322 // With DTX, Opus must stop transmission for some time.
323 EXPECT_GT(i, 1);
324 }
Minyue Li092041c2015-05-11 12:19:35 +0200325
minyue3cea2562015-11-10 03:49:26 -0800326 // We expect a normal payload.
Minyue Li092041c2015-05-11 12:19:35 +0200327 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
328 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
329 EXPECT_EQ(0, audio_type); // Speech.
330
331 // Enters DTX again immediately.
minyue3cea2562015-11-10 03:49:26 -0800332 time += block_length_ms;
Yves Gerey665174f2018-06-19 15:03:05 +0200333 EXPECT_EQ(samples, static_cast<size_t>(
334 EncodeDecode(opus_encoder_, silence, opus_decoder_,
335 output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000336 if (dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700337 EXPECT_EQ(1U, encoded_bytes_); // Send 1 byte.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000338 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
339 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000340 EXPECT_EQ(2, audio_type); // Comfort noise.
minyue3cea2562015-11-10 03:49:26 -0800341 if (time >= kCheckTimeMs) {
342 CheckAudioBounded(output_data_decode, samples, channels_,
343 kOutputValueBound);
344 }
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000345 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700346 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000347 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
348 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000349 EXPECT_EQ(0, audio_type); // Speech.
350 }
351 }
352
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000353 silence[0] = 10000;
354 if (dtx) {
355 // Verify that encoder/decoder can jump out from DTX mode.
Yves Gerey665174f2018-06-19 15:03:05 +0200356 EXPECT_EQ(samples, static_cast<size_t>(
357 EncodeDecode(opus_encoder_, silence, opus_decoder_,
358 output_data_decode, &audio_type)));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700359 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000360 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
361 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000362 EXPECT_EQ(0, audio_type); // Speech.
363 }
364
365 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000366 delete[] output_data_decode;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000367 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
368 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000369}
370
soren28dc2852017-04-06 05:48:36 -0700371// Test if CBR does what we expect.
372void OpusTest::TestCbrEffect(bool cbr, int block_length_ms) {
373 PrepareSpeechData(channels_, block_length_ms, 2000);
374 const size_t samples = kOpusRateKhz * block_length_ms;
375
376 int32_t max_pkt_size_diff = 0;
377 int32_t prev_pkt_size = 0;
378
379 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200380 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
381 force_multistream_);
382 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
383 force_multistream_);
soren28dc2852017-04-06 05:48:36 -0700384
385 // Set bitrate.
386 EXPECT_EQ(
387 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
388
389 // Setting CBR.
390 EXPECT_EQ(0, cbr ? WebRtcOpus_EnableCbr(opus_encoder_)
391 : WebRtcOpus_DisableCbr(opus_encoder_));
392
393 int16_t audio_type;
394 std::vector<int16_t> audio_out(samples * channels_);
395 for (int i = 0; i < 100; ++i) {
396 EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode(
397 opus_encoder_, speech_data_.GetNextBlock(),
398 opus_decoder_, audio_out.data(), &audio_type)));
399
400 if (prev_pkt_size > 0) {
401 int32_t diff = std::abs((int32_t)encoded_bytes_ - prev_pkt_size);
402 max_pkt_size_diff = std::max(max_pkt_size_diff, diff);
403 }
Mirko Bonadei737e0732017-10-19 09:00:17 +0200404 prev_pkt_size = rtc::checked_cast<int32_t>(encoded_bytes_);
soren28dc2852017-04-06 05:48:36 -0700405 }
406
407 if (cbr) {
408 EXPECT_EQ(max_pkt_size_diff, 0);
409 } else {
410 EXPECT_GT(max_pkt_size_diff, 0);
411 }
412
413 // Free memory.
414 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
415 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
416}
417
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000418// Test failing Create.
henrika1d34fe92015-06-16 10:04:20 +0200419TEST(OpusTest, OpusCreateFail) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000420 WebRtcOpusEncInst* opus_encoder;
421 WebRtcOpusDecInst* opus_decoder;
422
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000423 // Test to see that an invalid pointer is caught.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000424 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(NULL, 1, 0));
425 // Invalid channel number.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100426 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 257, 0));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000427 // Invalid applciation mode.
428 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 1, 2));
429
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000430 EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(NULL, 1));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000431 // Invalid channel number.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100432 EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(&opus_decoder, 257));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000433}
434
435// Test failing Free.
henrika1d34fe92015-06-16 10:04:20 +0200436TEST(OpusTest, OpusFreeFail) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000437 // Test to see that an invalid pointer is caught.
438 EXPECT_EQ(-1, WebRtcOpus_EncoderFree(NULL));
439 EXPECT_EQ(-1, WebRtcOpus_DecoderFree(NULL));
440}
441
442// Test normal Create and Free.
henrika1d34fe92015-06-16 10:04:20 +0200443TEST_P(OpusTest, OpusCreateFree) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200444 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
445 force_multistream_);
446 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
447 force_multistream_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000448 EXPECT_TRUE(opus_encoder_ != NULL);
449 EXPECT_TRUE(opus_decoder_ != NULL);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000450 // Free encoder and decoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000451 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
452 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000453}
454
Alex Loiko50b8c392019-04-03 15:12:01 +0200455#define ENCODER_CTL(inst, vargs) \
456 inst->encoder \
457 ? opus_encoder_ctl(inst->encoder, vargs) \
458 : opus_multistream_encoder_ctl(inst->multistream_encoder, vargs)
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100459
henrika1d34fe92015-06-16 10:04:20 +0200460TEST_P(OpusTest, OpusEncodeDecode) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000461 PrepareSpeechData(channels_, 20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000462
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000463 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200464 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
465 force_multistream_);
466 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
467 force_multistream_);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000468
469 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200470 EXPECT_EQ(
471 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000472
473 // Check number of channels for decoder.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000474 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
475
476 // Check application mode.
477 opus_int32 app;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100478 ENCODER_CTL(opus_encoder_, OPUS_GET_APPLICATION(&app));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000479 EXPECT_EQ(application_ == 0 ? OPUS_APPLICATION_VOIP : OPUS_APPLICATION_AUDIO,
480 app);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000481
482 // Encode & decode.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000483 int16_t audio_type;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000484 int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_];
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000485 EXPECT_EQ(kOpus20msFrameSamples,
kwiberg288886b2015-11-06 01:21:35 -0800486 static_cast<size_t>(
487 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
488 opus_decoder_, output_data_decode, &audio_type)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000489
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000490 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000491 delete[] output_data_decode;
492 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
493 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000494}
495
henrika1d34fe92015-06-16 10:04:20 +0200496TEST_P(OpusTest, OpusSetBitRate) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000497 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000498 EXPECT_EQ(-1, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000499
500 // Create encoder memory, try with different bitrates.
Alex Loiko50b8c392019-04-03 15:12:01 +0200501 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
502 force_multistream_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000503 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 30000));
504 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
505 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 300000));
506 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 600000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000507
508 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000509 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000510}
511
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000512TEST_P(OpusTest, OpusSetComplexity) {
minyue@webrtc.org04546882014-03-07 08:55:48 +0000513 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000514 EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 9));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000515
516 // Create encoder memory, try with different complexities.
Alex Loiko50b8c392019-04-03 15:12:01 +0200517 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
518 force_multistream_);
minyue@webrtc.org04546882014-03-07 08:55:48 +0000519
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000520 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 0));
521 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 10));
522 EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 11));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000523
524 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000525 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000526}
527
Alex Luebseeb27652017-11-20 11:13:56 -0800528TEST_P(OpusTest, OpusSetBandwidth) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100529 if (channels_ > 2) {
530 // TODO(webrtc:10217): investigate why multi-stream Opus reports
531 // narrowband when it's configured with FULLBAND.
532 return;
533 }
Alex Luebseeb27652017-11-20 11:13:56 -0800534 PrepareSpeechData(channels_, 20, 20);
535
536 int16_t audio_type;
537 std::unique_ptr<int16_t[]> output_data_decode(
538 new int16_t[kOpus20msFrameSamples * channels_]());
539
540 // Test without creating encoder memory.
541 EXPECT_EQ(-1,
542 WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
543 EXPECT_EQ(-1, WebRtcOpus_GetBandwidth(opus_encoder_));
544
545 // Create encoder memory, try with different bandwidths.
Alex Loiko50b8c392019-04-03 15:12:01 +0200546 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
547 force_multistream_);
548 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
549 force_multistream_);
Alex Luebseeb27652017-11-20 11:13:56 -0800550
551 EXPECT_EQ(-1, WebRtcOpus_SetBandwidth(opus_encoder_,
552 OPUS_BANDWIDTH_NARROWBAND - 1));
553 EXPECT_EQ(0,
554 WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
555 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
556 output_data_decode.get(), &audio_type);
557 EXPECT_EQ(OPUS_BANDWIDTH_NARROWBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
558 EXPECT_EQ(0, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND));
559 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
560 output_data_decode.get(), &audio_type);
561 EXPECT_EQ(OPUS_BANDWIDTH_FULLBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
562 EXPECT_EQ(
563 -1, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND + 1));
564 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
565 output_data_decode.get(), &audio_type);
566 EXPECT_EQ(OPUS_BANDWIDTH_FULLBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
567
568 // Free memory.
569 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
570 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
571}
572
minyuec8299f92016-09-27 02:08:47 -0700573TEST_P(OpusTest, OpusForceChannels) {
574 // Test without creating encoder memory.
575 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
576
Alex Loiko50b8c392019-04-03 15:12:01 +0200577 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
578 force_multistream_);
579 ASSERT_NE(nullptr, opus_encoder_);
minyuec8299f92016-09-27 02:08:47 -0700580
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100581 if (channels_ >= 2) {
minyuec8299f92016-09-27 02:08:47 -0700582 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 3));
583 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
584 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
585 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
586 } else {
587 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
588 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
589 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
590 }
591
592 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
593}
594
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000595// Encode and decode one frame, initialize the decoder and
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000596// decode once more.
henrika1d34fe92015-06-16 10:04:20 +0200597TEST_P(OpusTest, OpusDecodeInit) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000598 PrepareSpeechData(channels_, 20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000599
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000600 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200601 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
602 force_multistream_);
603 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
604 force_multistream_);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000605
606 // Encode & decode.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000607 int16_t audio_type;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000608 int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_];
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000609 EXPECT_EQ(kOpus20msFrameSamples,
kwiberg288886b2015-11-06 01:21:35 -0800610 static_cast<size_t>(
611 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
612 opus_decoder_, output_data_decode, &audio_type)));
minyue@webrtc.org52bc4f42014-12-04 11:00:50 +0000613
Karl Wiberg43766482015-08-27 15:22:11 +0200614 WebRtcOpus_DecoderInit(opus_decoder_);
minyue@webrtc.org52bc4f42014-12-04 11:00:50 +0000615
616 EXPECT_EQ(kOpus20msFrameSamples,
Yves Gerey665174f2018-06-19 15:03:05 +0200617 static_cast<size_t>(
618 WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_,
619 output_data_decode, &audio_type)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000620
621 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000622 delete[] output_data_decode;
623 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
624 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000625}
626
henrika1d34fe92015-06-16 10:04:20 +0200627TEST_P(OpusTest, OpusEnableDisableFec) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000628 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000629 EXPECT_EQ(-1, WebRtcOpus_EnableFec(opus_encoder_));
630 EXPECT_EQ(-1, WebRtcOpus_DisableFec(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000631
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000632 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200633 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
634 force_multistream_);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000635
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000636 EXPECT_EQ(0, WebRtcOpus_EnableFec(opus_encoder_));
637 EXPECT_EQ(0, WebRtcOpus_DisableFec(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000638
639 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000640 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000641}
642
henrika1d34fe92015-06-16 10:04:20 +0200643TEST_P(OpusTest, OpusEnableDisableDtx) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000644 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000645 EXPECT_EQ(-1, WebRtcOpus_EnableDtx(opus_encoder_));
646 EXPECT_EQ(-1, WebRtcOpus_DisableDtx(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000647
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000648 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200649 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
650 force_multistream_);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000651
652 opus_int32 dtx;
653
654 // DTX is off by default.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100655 ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000656 EXPECT_EQ(0, dtx);
657
658 // Test to enable DTX.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000659 EXPECT_EQ(0, WebRtcOpus_EnableDtx(opus_encoder_));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100660 ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000661 EXPECT_EQ(1, dtx);
662
663 // Test to disable DTX.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000664 EXPECT_EQ(0, WebRtcOpus_DisableDtx(opus_encoder_));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100665 ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000666 EXPECT_EQ(0, dtx);
667
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000668 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000669 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000670}
671
henrika1d34fe92015-06-16 10:04:20 +0200672TEST_P(OpusTest, OpusDtxOff) {
minyue3cea2562015-11-10 03:49:26 -0800673 TestDtxEffect(false, 10);
674 TestDtxEffect(false, 20);
675 TestDtxEffect(false, 40);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000676}
677
henrika1d34fe92015-06-16 10:04:20 +0200678TEST_P(OpusTest, OpusDtxOn) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100679 if (channels_ > 2) {
680 // TODO(webrtc:10218): adapt the test to the sizes and order of multi-stream
681 // DTX packets.
682 return;
683 }
minyue3cea2562015-11-10 03:49:26 -0800684 TestDtxEffect(true, 10);
685 TestDtxEffect(true, 20);
686 TestDtxEffect(true, 40);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000687}
688
soren28dc2852017-04-06 05:48:36 -0700689TEST_P(OpusTest, OpusCbrOff) {
690 TestCbrEffect(false, 10);
691 TestCbrEffect(false, 20);
692 TestCbrEffect(false, 40);
693}
694
695TEST_P(OpusTest, OpusCbrOn) {
696 TestCbrEffect(true, 10);
697 TestCbrEffect(true, 20);
698 TestCbrEffect(true, 40);
699}
700
henrika1d34fe92015-06-16 10:04:20 +0200701TEST_P(OpusTest, OpusSetPacketLossRate) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000702 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000703 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000704
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000705 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200706 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
707 force_multistream_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000708
709 EXPECT_EQ(0, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
710 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, -1));
711 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 101));
712
713 // Free memory.
714 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
715}
716
henrika1d34fe92015-06-16 10:04:20 +0200717TEST_P(OpusTest, OpusSetMaxPlaybackRate) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000718 // Test without creating encoder memory.
719 EXPECT_EQ(-1, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, 20000));
720
721 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200722 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
723 force_multistream_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000724
725 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 48000);
726 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 24001);
727 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 24000);
728 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 16001);
729 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 16000);
730 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 12001);
731 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 12000);
732 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 8001);
733 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 8000);
734 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 4000);
735
736 // Free memory.
737 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
738}
739
740// Test PLC.
henrika1d34fe92015-06-16 10:04:20 +0200741TEST_P(OpusTest, OpusDecodePlc) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000742 PrepareSpeechData(channels_, 20, 20);
743
744 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200745 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
746 force_multistream_);
747 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
748 force_multistream_);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000749
750 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200751 EXPECT_EQ(
752 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000753
754 // Check number of channels for decoder.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000755 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000756
757 // Encode & decode.
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000758 int16_t audio_type;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000759 int16_t* output_data_decode = new int16_t[kOpus20msFrameSamples * channels_];
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000760 EXPECT_EQ(kOpus20msFrameSamples,
kwiberg288886b2015-11-06 01:21:35 -0800761 static_cast<size_t>(
762 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
763 opus_decoder_, output_data_decode, &audio_type)));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000764
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000765 // Call decoder PLC.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000766 int16_t* plc_buffer = new int16_t[kOpus20msFrameSamples * channels_];
Yves Gerey665174f2018-06-19 15:03:05 +0200767 EXPECT_EQ(kOpus20msFrameSamples, static_cast<size_t>(WebRtcOpus_DecodePlc(
768 opus_decoder_, plc_buffer, 1)));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000769
770 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000771 delete[] plc_buffer;
772 delete[] output_data_decode;
773 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
774 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000775}
776
777// Duration estimation.
henrika1d34fe92015-06-16 10:04:20 +0200778TEST_P(OpusTest, OpusDurationEstimation) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000779 PrepareSpeechData(channels_, 20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000780
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000781 // Create.
Alex Loiko50b8c392019-04-03 15:12:01 +0200782 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
783 force_multistream_);
784 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
785 force_multistream_);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000786
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000787 // 10 ms. We use only first 10 ms of a 20 ms block.
kwiberg288886b2015-11-06 01:21:35 -0800788 auto speech_block = speech_data_.GetNextBlock();
789 int encoded_bytes_int = WebRtcOpus_Encode(
790 opus_encoder_, speech_block.data(),
Yves Gerey665174f2018-06-19 15:03:05 +0200791 rtc::CheckedDivExact(speech_block.size(), 2 * channels_), kMaxBytes,
792 bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700793 EXPECT_GE(encoded_bytes_int, 0);
Yves Gerey665174f2018-06-19 15:03:05 +0200794 EXPECT_EQ(
795 kOpus10msFrameSamples,
796 static_cast<size_t>(WebRtcOpus_DurationEst(
797 opus_decoder_, bitstream_, static_cast<size_t>(encoded_bytes_int))));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000798
799 // 20 ms
kwiberg288886b2015-11-06 01:21:35 -0800800 speech_block = speech_data_.GetNextBlock();
Yves Gerey665174f2018-06-19 15:03:05 +0200801 encoded_bytes_int =
802 WebRtcOpus_Encode(opus_encoder_, speech_block.data(),
803 rtc::CheckedDivExact(speech_block.size(), channels_),
804 kMaxBytes, bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700805 EXPECT_GE(encoded_bytes_int, 0);
Yves Gerey665174f2018-06-19 15:03:05 +0200806 EXPECT_EQ(
807 kOpus20msFrameSamples,
808 static_cast<size_t>(WebRtcOpus_DurationEst(
809 opus_decoder_, bitstream_, static_cast<size_t>(encoded_bytes_int))));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000810
811 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000812 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
813 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000814}
815
henrika1d34fe92015-06-16 10:04:20 +0200816TEST_P(OpusTest, OpusDecodeRepacketized) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100817 if (channels_ > 2) {
818 // As per the Opus documentation
819 // https://mf4.xiph.org/jenkins/view/opus/job/opus/ws/doc/html/group__opus__repacketizer.html#details,
820 // multiple streams are not supported.
821 return;
822 }
minyuea613eb62017-03-14 14:33:30 -0700823 constexpr size_t kPackets = 6;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000824
825 PrepareSpeechData(channels_, 20, 20 * kPackets);
826
827 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200828 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
829 force_multistream_);
830 ASSERT_NE(nullptr, opus_encoder_);
831 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_,
832 force_multistream_);
833 ASSERT_NE(nullptr, opus_decoder_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000834
835 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200836 EXPECT_EQ(
837 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000838
839 // Check number of channels for decoder.
840 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
841
842 // Encode & decode.
843 int16_t audio_type;
kwiberg91d97562016-02-14 01:10:03 -0800844 std::unique_ptr<int16_t[]> output_data_decode(
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000845 new int16_t[kPackets * kOpus20msFrameSamples * channels_]);
846 OpusRepacketizer* rp = opus_repacketizer_create();
847
minyuea613eb62017-03-14 14:33:30 -0700848 size_t num_packets = 0;
849 constexpr size_t kMaxCycles = 100;
850 for (size_t idx = 0; idx < kMaxCycles; ++idx) {
kwiberg288886b2015-11-06 01:21:35 -0800851 auto speech_block = speech_data_.GetNextBlock();
852 encoded_bytes_ =
853 WebRtcOpus_Encode(opus_encoder_, speech_block.data(),
Peter Kasting69558702016-01-12 16:26:35 -0800854 rtc::CheckedDivExact(speech_block.size(), channels_),
kwiberg288886b2015-11-06 01:21:35 -0800855 kMaxBytes, bitstream_);
Yves Gerey665174f2018-06-19 15:03:05 +0200856 if (opus_repacketizer_cat(rp, bitstream_,
857 rtc::checked_cast<opus_int32>(encoded_bytes_)) ==
858 OPUS_OK) {
minyuea613eb62017-03-14 14:33:30 -0700859 ++num_packets;
860 if (num_packets == kPackets) {
861 break;
862 }
863 } else {
864 // Opus repacketizer cannot guarantee a success. We try again if it fails.
865 opus_repacketizer_init(rp);
866 num_packets = 0;
867 }
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000868 }
minyuea613eb62017-03-14 14:33:30 -0700869 EXPECT_EQ(kPackets, num_packets);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000870
871 encoded_bytes_ = opus_repacketizer_out(rp, bitstream_, kMaxBytes);
872
873 EXPECT_EQ(kOpus20msFrameSamples * kPackets,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700874 static_cast<size_t>(WebRtcOpus_DurationEst(
875 opus_decoder_, bitstream_, encoded_bytes_)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000876
877 EXPECT_EQ(kOpus20msFrameSamples * kPackets,
Yves Gerey665174f2018-06-19 15:03:05 +0200878 static_cast<size_t>(
879 WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_,
880 output_data_decode.get(), &audio_type)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000881
882 // Free memory.
883 opus_repacketizer_destroy(rp);
884 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
885 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
886}
887
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100888INSTANTIATE_TEST_SUITE_P(VariousMode,
889 OpusTest,
Alex Loiko50b8c392019-04-03 15:12:01 +0200890 ::testing::ValuesIn({
891 std::make_tuple(1, 0, true),
Alex Loikoe5b94162019-04-08 17:19:41 +0200892 std::make_tuple(2, 1, true),
Alex Loiko50b8c392019-04-03 15:12:01 +0200893 std::make_tuple(2, 0, false),
894 std::make_tuple(4, 0, false),
895 std::make_tuple(1, 1, false),
896 std::make_tuple(4, 1, false),
897 }));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000898
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000899} // namespace webrtc