blob: 3407d7d3cf989080e2ab33bc245accca5258a82e [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,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020042 bool use_multistream,
43 int encoder_sample_rate_hz) {
44 EXPECT_TRUE(channels == 1 || channels == 2 || use_multistream);
45 if (use_multistream) {
46 EXPECT_EQ(encoder_sample_rate_hz, 48000);
47 if (channels == 1) {
48 EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
49 opus_encoder, channels, application, kMonoTotalStreams,
50 kMonoCoupledStreams, kMonoChannelMapping));
51 } else if (channels == 2) {
52 EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
53 opus_encoder, channels, application, kStereoTotalStreams,
54 kStereoCoupledStreams, kStereoChannelMapping));
55 } else if (channels == 4) {
56 EXPECT_EQ(0, WebRtcOpus_MultistreamEncoderCreate(
57 opus_encoder, channels, application, kQuadTotalStreams,
58 kQuadCoupledStreams, kQuadChannelMapping));
59 } else {
60 EXPECT_TRUE(false) << channels;
61 }
Alex Loiko50b8c392019-04-03 15:12:01 +020062 } else {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020063 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(opus_encoder, channels, application,
64 encoder_sample_rate_hz));
Alex Loiko50b8c392019-04-03 15:12:01 +020065 }
66}
67
68void CreateSingleOrMultiStreamDecoder(WebRtcOpusDecInst** opus_decoder,
69 int channels,
Karl Wiberga1d1a1e2019-05-28 14:41:07 +020070 bool use_multistream,
71 int decoder_sample_rate_hz) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020072 EXPECT_TRUE(channels == 1 || channels == 2 || use_multistream);
73 if (use_multistream) {
Karl Wiberga1d1a1e2019-05-28 14:41:07 +020074 EXPECT_EQ(decoder_sample_rate_hz, 48000);
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020075 if (channels == 1) {
76 EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(
77 opus_decoder, channels, kMonoTotalStreams,
78 kMonoCoupledStreams, kMonoChannelMapping));
79 } else if (channels == 2) {
80 EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(
81 opus_decoder, channels, kStereoTotalStreams,
82 kStereoCoupledStreams, kStereoChannelMapping));
83 } else if (channels == 4) {
84 EXPECT_EQ(0, WebRtcOpus_MultistreamDecoderCreate(
85 opus_decoder, channels, kQuadTotalStreams,
86 kQuadCoupledStreams, kQuadChannelMapping));
87 } else {
88 EXPECT_TRUE(false) << channels;
89 }
Alex Loiko50b8c392019-04-03 15:12:01 +020090 } else {
Karl Wiberga1d1a1e2019-05-28 14:41:07 +020091 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(opus_decoder, channels,
92 decoder_sample_rate_hz));
Alex Loiko50b8c392019-04-03 15:12:01 +020093 }
94}
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020095
Karl Wiberga1d1a1e2019-05-28 14:41:07 +020096int SamplesPerChannel(int sample_rate_hz, int duration_ms) {
97 const int samples_per_ms = rtc::CheckedDivExact(sample_rate_hz, 1000);
98 return samples_per_ms * duration_ms;
99}
100
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000101using test::AudioLoop;
Jonas Olssona4d87372019-07-05 19:08:33 +0200102using ::testing::Combine;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000103using ::testing::TestWithParam;
104using ::testing::Values;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000105
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000106// Maximum number of bytes in output bitstream.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100107const size_t kMaxBytes = 2000;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000108
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200109class OpusTest
110 : public TestWithParam<::testing::tuple<size_t, int, bool, int, int>> {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000111 protected:
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200112 OpusTest() = default;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000113
minyue3cea2562015-11-10 03:49:26 -0800114 void TestDtxEffect(bool dtx, int block_length_ms);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000115
soren28dc2852017-04-06 05:48:36 -0700116 void TestCbrEffect(bool dtx, int block_length_ms);
117
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000118 // Prepare |speech_data_| for encoding, read from a hard-coded file.
119 // After preparation, |speech_data_.GetNextBlock()| returns a pointer to a
120 // block of |block_length_ms| milliseconds. The data is looped every
121 // |loop_length_ms| milliseconds.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200122 void PrepareSpeechData(int block_length_ms, int loop_length_ms);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000123
124 int EncodeDecode(WebRtcOpusEncInst* encoder,
kwiberg288886b2015-11-06 01:21:35 -0800125 rtc::ArrayView<const int16_t> input_audio,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000126 WebRtcOpusDecInst* decoder,
127 int16_t* output_audio,
128 int16_t* audio_type);
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000129
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000130 void SetMaxPlaybackRate(WebRtcOpusEncInst* encoder,
Yves Gerey665174f2018-06-19 15:03:05 +0200131 opus_int32 expect,
132 int32_t set);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000133
Yves Gerey665174f2018-06-19 15:03:05 +0200134 void CheckAudioBounded(const int16_t* audio,
135 size_t samples,
136 size_t channels,
minyue3cea2562015-11-10 03:49:26 -0800137 uint16_t bound) const;
138
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200139 WebRtcOpusEncInst* opus_encoder_ = nullptr;
140 WebRtcOpusDecInst* opus_decoder_ = nullptr;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000141 AudioLoop speech_data_;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000142 uint8_t bitstream_[kMaxBytes];
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200143 size_t encoded_bytes_ = 0;
144 const size_t channels_{std::get<0>(GetParam())};
145 const int application_{std::get<1>(GetParam())};
146 const bool use_multistream_{std::get<2>(GetParam())};
147 const int encoder_sample_rate_hz_{std::get<3>(GetParam())};
148 const int decoder_sample_rate_hz_{std::get<4>(GetParam())};
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000149};
150
Yves Gerey3a65f392019-11-11 18:05:42 +0100151} // namespace
152
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200153// Singlestream: Try all combinations.
154INSTANTIATE_TEST_SUITE_P(Singlestream,
155 OpusTest,
156 testing::Combine(testing::Values(1, 2),
157 testing::Values(0, 1),
158 testing::Values(false),
159 testing::Values(16000, 48000),
160 testing::Values(16000, 48000)));
161
162// Multistream: Some representative cases (only 48 kHz for now).
163INSTANTIATE_TEST_SUITE_P(
164 Multistream,
165 OpusTest,
166 testing::Values(std::make_tuple(1, 0, true, 48000, 48000),
167 std::make_tuple(2, 1, true, 48000, 48000),
168 std::make_tuple(4, 0, true, 48000, 48000),
169 std::make_tuple(4, 1, true, 48000, 48000)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000170
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200171void OpusTest::PrepareSpeechData(int block_length_ms, int loop_length_ms) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100172 std::map<int, std::string> channel_to_basename = {
173 {1, "audio_coding/testfile32kHz"},
174 {2, "audio_coding/teststereo32kHz"},
175 {4, "audio_coding/speech_4_channels_48k_one_second"}};
176 std::map<int, std::string> channel_to_suffix = {
177 {1, "pcm"}, {2, "pcm"}, {4, "wav"}};
Yves Gerey665174f2018-06-19 15:03:05 +0200178 const std::string file_name = webrtc::test::ResourcePath(
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200179 channel_to_basename[channels_], channel_to_suffix[channels_]);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000180 if (loop_length_ms < block_length_ms) {
181 loop_length_ms = block_length_ms;
182 }
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200183 const int sample_rate_khz =
184 rtc::CheckedDivExact(encoder_sample_rate_hz_, 1000);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000185 EXPECT_TRUE(speech_data_.Init(file_name,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200186 loop_length_ms * sample_rate_khz * channels_,
187 block_length_ms * sample_rate_khz * channels_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000188}
189
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000190void OpusTest::SetMaxPlaybackRate(WebRtcOpusEncInst* encoder,
191 opus_int32 expect,
192 int32_t set) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000193 opus_int32 bandwidth;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000194 EXPECT_EQ(0, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, set));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100195 EXPECT_EQ(0, WebRtcOpus_GetMaxPlaybackRate(opus_encoder_, &bandwidth));
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000196 EXPECT_EQ(expect, bandwidth);
197}
198
Yves Gerey665174f2018-06-19 15:03:05 +0200199void OpusTest::CheckAudioBounded(const int16_t* audio,
200 size_t samples,
201 size_t channels,
202 uint16_t bound) const {
minyue3cea2562015-11-10 03:49:26 -0800203 for (size_t i = 0; i < samples; ++i) {
Peter Kasting69558702016-01-12 16:26:35 -0800204 for (size_t c = 0; c < channels; ++c) {
minyue3cea2562015-11-10 03:49:26 -0800205 ASSERT_GE(audio[i * channels + c], -bound);
206 ASSERT_LE(audio[i * channels + c], bound);
207 }
208 }
209}
210
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000211int OpusTest::EncodeDecode(WebRtcOpusEncInst* encoder,
kwiberg288886b2015-11-06 01:21:35 -0800212 rtc::ArrayView<const int16_t> input_audio,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000213 WebRtcOpusDecInst* decoder,
214 int16_t* output_audio,
215 int16_t* audio_type) {
Minyue Li8e83c7a2019-11-04 14:47:52 +0100216 const int input_samples_per_channel =
217 rtc::CheckedDivExact(input_audio.size(), channels_);
Yves Gerey665174f2018-06-19 15:03:05 +0200218 int encoded_bytes_int =
Minyue Li8e83c7a2019-11-04 14:47:52 +0100219 WebRtcOpus_Encode(encoder, input_audio.data(), input_samples_per_channel,
Yves Gerey665174f2018-06-19 15:03:05 +0200220 kMaxBytes, bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700221 EXPECT_GE(encoded_bytes_int, 0);
222 encoded_bytes_ = static_cast<size_t>(encoded_bytes_int);
Minyue Li8e83c7a2019-11-04 14:47:52 +0100223 if (encoded_bytes_ != 0) {
224 int est_len = WebRtcOpus_DurationEst(decoder, bitstream_, encoded_bytes_);
225 int act_len = WebRtcOpus_Decode(decoder, bitstream_, encoded_bytes_,
226 output_audio, audio_type);
227 EXPECT_EQ(est_len, act_len);
228 return act_len;
229 } else {
230 int total_dtx_len = 0;
231 const int output_samples_per_channel = input_samples_per_channel *
232 decoder_sample_rate_hz_ /
233 encoder_sample_rate_hz_;
234 while (total_dtx_len < output_samples_per_channel) {
235 int est_len = WebRtcOpus_DurationEst(decoder, NULL, 0);
236 int act_len = WebRtcOpus_Decode(decoder, NULL, 0,
237 &output_audio[total_dtx_len * channels_],
238 audio_type);
239 EXPECT_EQ(est_len, act_len);
240 total_dtx_len += act_len;
241 }
242 return total_dtx_len;
243 }
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000244}
245
246// Test if encoder/decoder can enter DTX mode properly and do not enter DTX when
247// they should not. This test is signal dependent.
minyue3cea2562015-11-10 03:49:26 -0800248void OpusTest::TestDtxEffect(bool dtx, int block_length_ms) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200249 PrepareSpeechData(block_length_ms, 2000);
250 const size_t input_samples =
251 rtc::CheckedDivExact(encoder_sample_rate_hz_, 1000) * block_length_ms;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200252 const size_t output_samples =
253 rtc::CheckedDivExact(decoder_sample_rate_hz_, 1000) * block_length_ms;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000254
255 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200256 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200257 use_multistream_, encoder_sample_rate_hz_);
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200258 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
259 decoder_sample_rate_hz_);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000260
261 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200262 EXPECT_EQ(
263 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000264
265 // Set input audio as silence.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200266 std::vector<int16_t> silence(input_samples * channels_, 0);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000267
268 // Setting DTX.
Yves Gerey665174f2018-06-19 15:03:05 +0200269 EXPECT_EQ(0, dtx ? WebRtcOpus_EnableDtx(opus_encoder_)
270 : WebRtcOpus_DisableDtx(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000271
272 int16_t audio_type;
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200273 int16_t* output_data_decode = new int16_t[output_samples * channels_];
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000274
275 for (int i = 0; i < 100; ++i) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200276 EXPECT_EQ(output_samples,
277 static_cast<size_t>(EncodeDecode(
278 opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
279 output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000280 // If not DTX, it should never enter DTX mode. If DTX, we do not care since
281 // whether it enters DTX depends on the signal type.
282 if (!dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700283 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000284 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
285 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000286 EXPECT_EQ(0, audio_type); // Speech.
287 }
288 }
289
290 // We input some silent segments. In DTX mode, the encoder will stop sending.
291 // However, DTX may happen after a while.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000292 for (int i = 0; i < 30; ++i) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200293 EXPECT_EQ(output_samples, static_cast<size_t>(EncodeDecode(
294 opus_encoder_, silence, opus_decoder_,
295 output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000296 if (!dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700297 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000298 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
299 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000300 EXPECT_EQ(0, audio_type); // Speech.
Peter Kasting728d9032015-06-11 14:31:38 -0700301 } else if (encoded_bytes_ == 1) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000302 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
303 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000304 EXPECT_EQ(2, audio_type); // Comfort noise.
305 break;
306 }
307 }
308
Minyue Li092041c2015-05-11 12:19:35 +0200309 // When Opus is in DTX, it wakes up in a regular basis. It sends two packets,
310 // one with an arbitrary size and the other of 1-byte, then stops sending for
minyue3cea2562015-11-10 03:49:26 -0800311 // a certain number of frames.
312
313 // |max_dtx_frames| is the maximum number of frames Opus can stay in DTX.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200314 // TODO(kwiberg): Why does this number depend on the encoding sample rate?
315 const int max_dtx_frames =
316 (encoder_sample_rate_hz_ == 16000 ? 800 : 400) / block_length_ms + 1;
minyue3cea2562015-11-10 03:49:26 -0800317
318 // We run |kRunTimeMs| milliseconds of pure silence.
minyue58e08cb2016-02-24 03:49:19 -0800319 const int kRunTimeMs = 4500;
minyue3cea2562015-11-10 03:49:26 -0800320
321 // We check that, after a |kCheckTimeMs| milliseconds (given that the CNG in
322 // Opus needs time to adapt), the absolute values of DTX decoded signal are
323 // bounded by |kOutputValueBound|.
minyue58e08cb2016-02-24 03:49:19 -0800324 const int kCheckTimeMs = 4000;
minyue3cea2562015-11-10 03:49:26 -0800325
326#if defined(OPUS_FIXED_POINT)
minyuel7e937e92016-02-29 10:24:15 +0100327 // Fixed-point Opus generates a random (comfort) noise, which has a less
328 // predictable value bound than its floating-point Opus. This value depends on
329 // input signal, and the time window for checking the output values (between
330 // |kCheckTimeMs| and |kRunTimeMs|).
331 const uint16_t kOutputValueBound = 30;
332
minyue3cea2562015-11-10 03:49:26 -0800333#else
minyue58e08cb2016-02-24 03:49:19 -0800334 const uint16_t kOutputValueBound = 2;
minyue3cea2562015-11-10 03:49:26 -0800335#endif
336
337 int time = 0;
338 while (time < kRunTimeMs) {
339 // DTX mode is maintained for maximum |max_dtx_frames| frames.
340 int i = 0;
341 for (; i < max_dtx_frames; ++i) {
342 time += block_length_ms;
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200343 EXPECT_EQ(output_samples, static_cast<size_t>(EncodeDecode(
344 opus_encoder_, silence, opus_decoder_,
345 output_data_decode, &audio_type)));
Minyue Li092041c2015-05-11 12:19:35 +0200346 if (dtx) {
minyue3cea2562015-11-10 03:49:26 -0800347 if (encoded_bytes_ > 1)
348 break;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700349 EXPECT_EQ(0U, encoded_bytes_) // Send 0 byte.
Minyue Li092041c2015-05-11 12:19:35 +0200350 << "Opus should have entered DTX mode.";
351 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
352 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
353 EXPECT_EQ(2, audio_type); // Comfort noise.
minyue3cea2562015-11-10 03:49:26 -0800354 if (time >= kCheckTimeMs) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200355 CheckAudioBounded(output_data_decode, output_samples, channels_,
minyue3cea2562015-11-10 03:49:26 -0800356 kOutputValueBound);
357 }
Minyue Li092041c2015-05-11 12:19:35 +0200358 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700359 EXPECT_GT(encoded_bytes_, 1U);
Minyue Li092041c2015-05-11 12:19:35 +0200360 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
361 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
362 EXPECT_EQ(0, audio_type); // Speech.
363 }
364 }
365
minyue3cea2562015-11-10 03:49:26 -0800366 if (dtx) {
367 // With DTX, Opus must stop transmission for some time.
368 EXPECT_GT(i, 1);
369 }
Minyue Li092041c2015-05-11 12:19:35 +0200370
minyue3cea2562015-11-10 03:49:26 -0800371 // We expect a normal payload.
Minyue Li092041c2015-05-11 12:19:35 +0200372 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
373 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
374 EXPECT_EQ(0, audio_type); // Speech.
375
376 // Enters DTX again immediately.
minyue3cea2562015-11-10 03:49:26 -0800377 time += block_length_ms;
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200378 EXPECT_EQ(output_samples, static_cast<size_t>(EncodeDecode(
379 opus_encoder_, silence, opus_decoder_,
380 output_data_decode, &audio_type)));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000381 if (dtx) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700382 EXPECT_EQ(1U, encoded_bytes_); // Send 1 byte.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000383 EXPECT_EQ(1, opus_encoder_->in_dtx_mode);
384 EXPECT_EQ(1, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000385 EXPECT_EQ(2, audio_type); // Comfort noise.
minyue3cea2562015-11-10 03:49:26 -0800386 if (time >= kCheckTimeMs) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200387 CheckAudioBounded(output_data_decode, output_samples, channels_,
minyue3cea2562015-11-10 03:49:26 -0800388 kOutputValueBound);
389 }
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000390 } else {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700391 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000392 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
393 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000394 EXPECT_EQ(0, audio_type); // Speech.
395 }
396 }
397
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000398 silence[0] = 10000;
399 if (dtx) {
400 // Verify that encoder/decoder can jump out from DTX mode.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200401 EXPECT_EQ(output_samples, static_cast<size_t>(EncodeDecode(
402 opus_encoder_, silence, opus_decoder_,
403 output_data_decode, &audio_type)));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700404 EXPECT_GT(encoded_bytes_, 1U);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000405 EXPECT_EQ(0, opus_encoder_->in_dtx_mode);
406 EXPECT_EQ(0, opus_decoder_->in_dtx_mode);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000407 EXPECT_EQ(0, audio_type); // Speech.
408 }
409
410 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000411 delete[] output_data_decode;
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000412 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
413 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000414}
415
soren28dc2852017-04-06 05:48:36 -0700416// Test if CBR does what we expect.
417void OpusTest::TestCbrEffect(bool cbr, int block_length_ms) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200418 PrepareSpeechData(block_length_ms, 2000);
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200419 const size_t output_samples =
420 rtc::CheckedDivExact(decoder_sample_rate_hz_, 1000) * block_length_ms;
soren28dc2852017-04-06 05:48:36 -0700421
422 int32_t max_pkt_size_diff = 0;
423 int32_t prev_pkt_size = 0;
424
425 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200426 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200427 use_multistream_, encoder_sample_rate_hz_);
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200428 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
429 decoder_sample_rate_hz_);
soren28dc2852017-04-06 05:48:36 -0700430
431 // Set bitrate.
432 EXPECT_EQ(
433 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
434
435 // Setting CBR.
436 EXPECT_EQ(0, cbr ? WebRtcOpus_EnableCbr(opus_encoder_)
437 : WebRtcOpus_DisableCbr(opus_encoder_));
438
439 int16_t audio_type;
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200440 std::vector<int16_t> audio_out(output_samples * channels_);
soren28dc2852017-04-06 05:48:36 -0700441 for (int i = 0; i < 100; ++i) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200442 EXPECT_EQ(output_samples,
443 static_cast<size_t>(
444 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
445 opus_decoder_, audio_out.data(), &audio_type)));
soren28dc2852017-04-06 05:48:36 -0700446
447 if (prev_pkt_size > 0) {
448 int32_t diff = std::abs((int32_t)encoded_bytes_ - prev_pkt_size);
449 max_pkt_size_diff = std::max(max_pkt_size_diff, diff);
450 }
Mirko Bonadei737e0732017-10-19 09:00:17 +0200451 prev_pkt_size = rtc::checked_cast<int32_t>(encoded_bytes_);
soren28dc2852017-04-06 05:48:36 -0700452 }
453
454 if (cbr) {
455 EXPECT_EQ(max_pkt_size_diff, 0);
456 } else {
457 EXPECT_GT(max_pkt_size_diff, 0);
458 }
459
460 // Free memory.
461 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
462 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
463}
464
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000465// Test failing Create.
henrika1d34fe92015-06-16 10:04:20 +0200466TEST(OpusTest, OpusCreateFail) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000467 WebRtcOpusEncInst* opus_encoder;
468 WebRtcOpusDecInst* opus_decoder;
469
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000470 // Test to see that an invalid pointer is caught.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200471 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(NULL, 1, 0, 48000));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000472 // Invalid channel number.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200473 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 257, 0, 48000));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000474 // Invalid applciation mode.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200475 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 1, 2, 48000));
476 // Invalid sample rate.
477 EXPECT_EQ(-1, WebRtcOpus_EncoderCreate(&opus_encoder, 1, 0, 12345));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000478
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200479 EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(NULL, 1, 48000));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000480 // Invalid channel number.
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200481 EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(&opus_decoder, 257, 48000));
482 // Invalid sample rate.
483 EXPECT_EQ(-1, WebRtcOpus_DecoderCreate(&opus_decoder, 1, 12345));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000484}
485
486// Test failing Free.
henrika1d34fe92015-06-16 10:04:20 +0200487TEST(OpusTest, OpusFreeFail) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000488 // Test to see that an invalid pointer is caught.
489 EXPECT_EQ(-1, WebRtcOpus_EncoderFree(NULL));
490 EXPECT_EQ(-1, WebRtcOpus_DecoderFree(NULL));
491}
492
493// Test normal Create and Free.
henrika1d34fe92015-06-16 10:04:20 +0200494TEST_P(OpusTest, OpusCreateFree) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200495 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200496 use_multistream_, encoder_sample_rate_hz_);
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200497 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
498 decoder_sample_rate_hz_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000499 EXPECT_TRUE(opus_encoder_ != NULL);
500 EXPECT_TRUE(opus_decoder_ != NULL);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000501 // Free encoder and decoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000502 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
503 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000504}
505
Alex Loiko50b8c392019-04-03 15:12:01 +0200506#define ENCODER_CTL(inst, vargs) \
507 inst->encoder \
508 ? opus_encoder_ctl(inst->encoder, vargs) \
509 : opus_multistream_encoder_ctl(inst->multistream_encoder, vargs)
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100510
henrika1d34fe92015-06-16 10:04:20 +0200511TEST_P(OpusTest, OpusEncodeDecode) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200512 PrepareSpeechData(20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000513
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000514 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200515 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200516 use_multistream_, encoder_sample_rate_hz_);
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200517 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
518 decoder_sample_rate_hz_);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000519
520 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200521 EXPECT_EQ(
522 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000523
524 // Check number of channels for decoder.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000525 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
526
527 // Check application mode.
528 opus_int32 app;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100529 ENCODER_CTL(opus_encoder_, OPUS_GET_APPLICATION(&app));
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000530 EXPECT_EQ(application_ == 0 ? OPUS_APPLICATION_VOIP : OPUS_APPLICATION_AUDIO,
531 app);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000532
533 // Encode & decode.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000534 int16_t audio_type;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200535 const int decode_samples_per_channel =
536 SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/20);
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200537 int16_t* output_data_decode =
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200538 new int16_t[decode_samples_per_channel * channels_];
539 EXPECT_EQ(decode_samples_per_channel,
540 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
541 opus_decoder_, output_data_decode, &audio_type));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000542
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000543 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000544 delete[] output_data_decode;
545 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
546 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000547}
548
henrika1d34fe92015-06-16 10:04:20 +0200549TEST_P(OpusTest, OpusSetBitRate) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000550 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000551 EXPECT_EQ(-1, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000552
553 // Create encoder memory, try with different bitrates.
Alex Loiko50b8c392019-04-03 15:12:01 +0200554 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200555 use_multistream_, encoder_sample_rate_hz_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000556 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 30000));
557 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 60000));
558 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 300000));
559 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, 600000));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000560
561 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000562 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000563}
564
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000565TEST_P(OpusTest, OpusSetComplexity) {
minyue@webrtc.org04546882014-03-07 08:55:48 +0000566 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000567 EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 9));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000568
569 // Create encoder memory, try with different complexities.
Alex Loiko50b8c392019-04-03 15:12:01 +0200570 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200571 use_multistream_, encoder_sample_rate_hz_);
minyue@webrtc.org04546882014-03-07 08:55:48 +0000572
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000573 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 0));
574 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, 10));
575 EXPECT_EQ(-1, WebRtcOpus_SetComplexity(opus_encoder_, 11));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000576
577 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000578 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000579}
580
Alex Luebseeb27652017-11-20 11:13:56 -0800581TEST_P(OpusTest, OpusSetBandwidth) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100582 if (channels_ > 2) {
583 // TODO(webrtc:10217): investigate why multi-stream Opus reports
584 // narrowband when it's configured with FULLBAND.
585 return;
586 }
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200587 PrepareSpeechData(20, 20);
Alex Luebseeb27652017-11-20 11:13:56 -0800588
589 int16_t audio_type;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200590 const int decode_samples_per_channel =
591 SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/20);
Alex Luebseeb27652017-11-20 11:13:56 -0800592 std::unique_ptr<int16_t[]> output_data_decode(
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200593 new int16_t[decode_samples_per_channel * channels_]());
Alex Luebseeb27652017-11-20 11:13:56 -0800594
595 // Test without creating encoder memory.
596 EXPECT_EQ(-1,
597 WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
598 EXPECT_EQ(-1, WebRtcOpus_GetBandwidth(opus_encoder_));
599
600 // Create encoder memory, try with different bandwidths.
Alex Loiko50b8c392019-04-03 15:12:01 +0200601 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200602 use_multistream_, encoder_sample_rate_hz_);
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200603 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
604 decoder_sample_rate_hz_);
Alex Luebseeb27652017-11-20 11:13:56 -0800605
606 EXPECT_EQ(-1, WebRtcOpus_SetBandwidth(opus_encoder_,
607 OPUS_BANDWIDTH_NARROWBAND - 1));
608 EXPECT_EQ(0,
609 WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND));
610 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
611 output_data_decode.get(), &audio_type);
612 EXPECT_EQ(OPUS_BANDWIDTH_NARROWBAND, WebRtcOpus_GetBandwidth(opus_encoder_));
613 EXPECT_EQ(0, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND));
614 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
615 output_data_decode.get(), &audio_type);
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200616 EXPECT_EQ(encoder_sample_rate_hz_ == 16000 ? OPUS_BANDWIDTH_WIDEBAND
617 : OPUS_BANDWIDTH_FULLBAND,
618 WebRtcOpus_GetBandwidth(opus_encoder_));
Alex Luebseeb27652017-11-20 11:13:56 -0800619 EXPECT_EQ(
620 -1, WebRtcOpus_SetBandwidth(opus_encoder_, OPUS_BANDWIDTH_FULLBAND + 1));
621 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(), opus_decoder_,
622 output_data_decode.get(), &audio_type);
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200623 EXPECT_EQ(encoder_sample_rate_hz_ == 16000 ? OPUS_BANDWIDTH_WIDEBAND
624 : OPUS_BANDWIDTH_FULLBAND,
625 WebRtcOpus_GetBandwidth(opus_encoder_));
Alex Luebseeb27652017-11-20 11:13:56 -0800626
627 // Free memory.
628 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
629 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
630}
631
minyuec8299f92016-09-27 02:08:47 -0700632TEST_P(OpusTest, OpusForceChannels) {
633 // Test without creating encoder memory.
634 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
635
Alex Loiko50b8c392019-04-03 15:12:01 +0200636 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200637 use_multistream_, encoder_sample_rate_hz_);
Alex Loiko50b8c392019-04-03 15:12:01 +0200638 ASSERT_NE(nullptr, opus_encoder_);
minyuec8299f92016-09-27 02:08:47 -0700639
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100640 if (channels_ >= 2) {
minyuec8299f92016-09-27 02:08:47 -0700641 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 3));
642 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
643 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
644 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
645 } else {
646 EXPECT_EQ(-1, WebRtcOpus_SetForceChannels(opus_encoder_, 2));
647 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 1));
648 EXPECT_EQ(0, WebRtcOpus_SetForceChannels(opus_encoder_, 0));
649 }
650
651 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
652}
653
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000654// Encode and decode one frame, initialize the decoder and
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000655// decode once more.
henrika1d34fe92015-06-16 10:04:20 +0200656TEST_P(OpusTest, OpusDecodeInit) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200657 PrepareSpeechData(20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000658
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000659 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200660 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200661 use_multistream_, encoder_sample_rate_hz_);
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200662 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
663 decoder_sample_rate_hz_);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000664
665 // Encode & decode.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000666 int16_t audio_type;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200667 const int decode_samples_per_channel =
668 SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/20);
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200669 int16_t* output_data_decode =
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200670 new int16_t[decode_samples_per_channel * channels_];
671 EXPECT_EQ(decode_samples_per_channel,
672 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
673 opus_decoder_, output_data_decode, &audio_type));
minyue@webrtc.org52bc4f42014-12-04 11:00:50 +0000674
Karl Wiberg43766482015-08-27 15:22:11 +0200675 WebRtcOpus_DecoderInit(opus_decoder_);
minyue@webrtc.org52bc4f42014-12-04 11:00:50 +0000676
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200677 EXPECT_EQ(decode_samples_per_channel,
678 WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_,
679 output_data_decode, &audio_type));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000680
681 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000682 delete[] output_data_decode;
683 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
684 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000685}
686
henrika1d34fe92015-06-16 10:04:20 +0200687TEST_P(OpusTest, OpusEnableDisableFec) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000688 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000689 EXPECT_EQ(-1, WebRtcOpus_EnableFec(opus_encoder_));
690 EXPECT_EQ(-1, WebRtcOpus_DisableFec(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000691
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000692 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200693 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200694 use_multistream_, encoder_sample_rate_hz_);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000695
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000696 EXPECT_EQ(0, WebRtcOpus_EnableFec(opus_encoder_));
697 EXPECT_EQ(0, WebRtcOpus_DisableFec(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000698
699 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000700 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000701}
702
henrika1d34fe92015-06-16 10:04:20 +0200703TEST_P(OpusTest, OpusEnableDisableDtx) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000704 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000705 EXPECT_EQ(-1, WebRtcOpus_EnableDtx(opus_encoder_));
706 EXPECT_EQ(-1, WebRtcOpus_DisableDtx(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000707
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000708 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200709 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200710 use_multistream_, encoder_sample_rate_hz_);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000711
712 opus_int32 dtx;
713
714 // DTX is off by default.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100715 ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000716 EXPECT_EQ(0, dtx);
717
718 // Test to enable DTX.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000719 EXPECT_EQ(0, WebRtcOpus_EnableDtx(opus_encoder_));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100720 ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000721 EXPECT_EQ(1, dtx);
722
723 // Test to disable DTX.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000724 EXPECT_EQ(0, WebRtcOpus_DisableDtx(opus_encoder_));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100725 ENCODER_CTL(opus_encoder_, OPUS_GET_DTX(&dtx));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000726 EXPECT_EQ(0, dtx);
727
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000728 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000729 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000730}
731
henrika1d34fe92015-06-16 10:04:20 +0200732TEST_P(OpusTest, OpusDtxOff) {
minyue3cea2562015-11-10 03:49:26 -0800733 TestDtxEffect(false, 10);
734 TestDtxEffect(false, 20);
735 TestDtxEffect(false, 40);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000736}
737
henrika1d34fe92015-06-16 10:04:20 +0200738TEST_P(OpusTest, OpusDtxOn) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100739 if (channels_ > 2) {
740 // TODO(webrtc:10218): adapt the test to the sizes and order of multi-stream
741 // DTX packets.
742 return;
743 }
minyue3cea2562015-11-10 03:49:26 -0800744 TestDtxEffect(true, 10);
745 TestDtxEffect(true, 20);
746 TestDtxEffect(true, 40);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000747}
748
soren28dc2852017-04-06 05:48:36 -0700749TEST_P(OpusTest, OpusCbrOff) {
750 TestCbrEffect(false, 10);
751 TestCbrEffect(false, 20);
752 TestCbrEffect(false, 40);
753}
754
755TEST_P(OpusTest, OpusCbrOn) {
756 TestCbrEffect(true, 10);
757 TestCbrEffect(true, 20);
758 TestCbrEffect(true, 40);
759}
760
henrika1d34fe92015-06-16 10:04:20 +0200761TEST_P(OpusTest, OpusSetPacketLossRate) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000762 // Test without creating encoder memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000763 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000764
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000765 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200766 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200767 use_multistream_, encoder_sample_rate_hz_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000768
769 EXPECT_EQ(0, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));
770 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, -1));
771 EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 101));
772
773 // Free memory.
774 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
775}
776
henrika1d34fe92015-06-16 10:04:20 +0200777TEST_P(OpusTest, OpusSetMaxPlaybackRate) {
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000778 // Test without creating encoder memory.
779 EXPECT_EQ(-1, WebRtcOpus_SetMaxPlaybackRate(opus_encoder_, 20000));
780
781 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200782 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200783 use_multistream_, encoder_sample_rate_hz_);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000784
785 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 48000);
786 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_FULLBAND, 24001);
787 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 24000);
788 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_SUPERWIDEBAND, 16001);
789 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 16000);
790 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_WIDEBAND, 12001);
791 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 12000);
792 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_MEDIUMBAND, 8001);
793 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 8000);
794 SetMaxPlaybackRate(opus_encoder_, OPUS_BANDWIDTH_NARROWBAND, 4000);
795
796 // Free memory.
797 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
798}
799
800// Test PLC.
henrika1d34fe92015-06-16 10:04:20 +0200801TEST_P(OpusTest, OpusDecodePlc) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200802 PrepareSpeechData(20, 20);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000803
804 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200805 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200806 use_multistream_, encoder_sample_rate_hz_);
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200807 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
808 decoder_sample_rate_hz_);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000809
810 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200811 EXPECT_EQ(
812 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000813
814 // Check number of channels for decoder.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000815 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000816
817 // Encode & decode.
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000818 int16_t audio_type;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200819 const int decode_samples_per_channel =
820 SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/20);
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200821 int16_t* output_data_decode =
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200822 new int16_t[decode_samples_per_channel * channels_];
823 EXPECT_EQ(decode_samples_per_channel,
824 EncodeDecode(opus_encoder_, speech_data_.GetNextBlock(),
825 opus_decoder_, output_data_decode, &audio_type));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000826
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000827 // Call decoder PLC.
Minyue Li8e83c7a2019-11-04 14:47:52 +0100828 constexpr int kPlcDurationMs = 10;
829 const int plc_samples = decoder_sample_rate_hz_ * kPlcDurationMs / 1000;
830 int16_t* plc_buffer = new int16_t[plc_samples * channels_];
831 EXPECT_EQ(plc_samples,
Minyue Lifb075d52019-10-29 21:38:15 +0100832 WebRtcOpus_Decode(opus_decoder_, NULL, 0, plc_buffer, &audio_type));
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000833
834 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000835 delete[] plc_buffer;
836 delete[] output_data_decode;
837 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
838 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000839}
840
841// Duration estimation.
henrika1d34fe92015-06-16 10:04:20 +0200842TEST_P(OpusTest, OpusDurationEstimation) {
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200843 PrepareSpeechData(20, 20);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000844
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000845 // Create.
Alex Loiko50b8c392019-04-03 15:12:01 +0200846 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200847 use_multistream_, encoder_sample_rate_hz_);
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200848 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
849 decoder_sample_rate_hz_);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000850
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000851 // 10 ms. We use only first 10 ms of a 20 ms block.
kwiberg288886b2015-11-06 01:21:35 -0800852 auto speech_block = speech_data_.GetNextBlock();
853 int encoded_bytes_int = WebRtcOpus_Encode(
854 opus_encoder_, speech_block.data(),
Yves Gerey665174f2018-06-19 15:03:05 +0200855 rtc::CheckedDivExact(speech_block.size(), 2 * channels_), kMaxBytes,
856 bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700857 EXPECT_GE(encoded_bytes_int, 0);
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200858 EXPECT_EQ(SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/10),
859 WebRtcOpus_DurationEst(opus_decoder_, bitstream_,
860 static_cast<size_t>(encoded_bytes_int)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000861
862 // 20 ms
kwiberg288886b2015-11-06 01:21:35 -0800863 speech_block = speech_data_.GetNextBlock();
Yves Gerey665174f2018-06-19 15:03:05 +0200864 encoded_bytes_int =
865 WebRtcOpus_Encode(opus_encoder_, speech_block.data(),
866 rtc::CheckedDivExact(speech_block.size(), channels_),
867 kMaxBytes, bitstream_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700868 EXPECT_GE(encoded_bytes_int, 0);
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200869 EXPECT_EQ(SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/20),
870 WebRtcOpus_DurationEst(opus_decoder_, bitstream_,
871 static_cast<size_t>(encoded_bytes_int)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000872
873 // Free memory.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000874 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
875 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000876}
877
henrika1d34fe92015-06-16 10:04:20 +0200878TEST_P(OpusTest, OpusDecodeRepacketized) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100879 if (channels_ > 2) {
880 // As per the Opus documentation
881 // https://mf4.xiph.org/jenkins/view/opus/job/opus/ws/doc/html/group__opus__repacketizer.html#details,
882 // multiple streams are not supported.
883 return;
884 }
minyuea613eb62017-03-14 14:33:30 -0700885 constexpr size_t kPackets = 6;
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000886
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200887 PrepareSpeechData(20, 20 * kPackets);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000888
889 // Create encoder memory.
Alex Loiko50b8c392019-04-03 15:12:01 +0200890 CreateSingleOrMultiStreamEncoder(&opus_encoder_, channels_, application_,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200891 use_multistream_, encoder_sample_rate_hz_);
Alex Loiko50b8c392019-04-03 15:12:01 +0200892 ASSERT_NE(nullptr, opus_encoder_);
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200893 CreateSingleOrMultiStreamDecoder(&opus_decoder_, channels_, use_multistream_,
894 decoder_sample_rate_hz_);
Alex Loiko50b8c392019-04-03 15:12:01 +0200895 ASSERT_NE(nullptr, opus_decoder_);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000896
897 // Set bitrate.
Yves Gerey665174f2018-06-19 15:03:05 +0200898 EXPECT_EQ(
899 0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000900
901 // Check number of channels for decoder.
902 EXPECT_EQ(channels_, WebRtcOpus_DecoderChannels(opus_decoder_));
903
904 // Encode & decode.
905 int16_t audio_type;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200906 const int decode_samples_per_channel =
907 SamplesPerChannel(decoder_sample_rate_hz_, /*ms=*/20);
kwiberg91d97562016-02-14 01:10:03 -0800908 std::unique_ptr<int16_t[]> output_data_decode(
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200909 new int16_t[kPackets * decode_samples_per_channel * channels_]);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000910 OpusRepacketizer* rp = opus_repacketizer_create();
911
minyuea613eb62017-03-14 14:33:30 -0700912 size_t num_packets = 0;
913 constexpr size_t kMaxCycles = 100;
914 for (size_t idx = 0; idx < kMaxCycles; ++idx) {
kwiberg288886b2015-11-06 01:21:35 -0800915 auto speech_block = speech_data_.GetNextBlock();
916 encoded_bytes_ =
917 WebRtcOpus_Encode(opus_encoder_, speech_block.data(),
Peter Kasting69558702016-01-12 16:26:35 -0800918 rtc::CheckedDivExact(speech_block.size(), channels_),
kwiberg288886b2015-11-06 01:21:35 -0800919 kMaxBytes, bitstream_);
Yves Gerey665174f2018-06-19 15:03:05 +0200920 if (opus_repacketizer_cat(rp, bitstream_,
921 rtc::checked_cast<opus_int32>(encoded_bytes_)) ==
922 OPUS_OK) {
minyuea613eb62017-03-14 14:33:30 -0700923 ++num_packets;
924 if (num_packets == kPackets) {
925 break;
926 }
927 } else {
928 // Opus repacketizer cannot guarantee a success. We try again if it fails.
929 opus_repacketizer_init(rp);
930 num_packets = 0;
931 }
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000932 }
minyuea613eb62017-03-14 14:33:30 -0700933 EXPECT_EQ(kPackets, num_packets);
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000934
935 encoded_bytes_ = opus_repacketizer_out(rp, bitstream_, kMaxBytes);
936
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200937 EXPECT_EQ(decode_samples_per_channel * kPackets,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700938 static_cast<size_t>(WebRtcOpus_DurationEst(
939 opus_decoder_, bitstream_, encoded_bytes_)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000940
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200941 EXPECT_EQ(decode_samples_per_channel * kPackets,
Yves Gerey665174f2018-06-19 15:03:05 +0200942 static_cast<size_t>(
943 WebRtcOpus_Decode(opus_decoder_, bitstream_, encoded_bytes_,
944 output_data_decode.get(), &audio_type)));
minyue@webrtc.org7f7d7e32015-03-16 12:30:37 +0000945
946 // Free memory.
947 opus_repacketizer_destroy(rp);
948 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
949 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
950}
951
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000952} // namespace webrtc