blob: b8d8e67b0e95b35edb2e4291af26148db84a49f8 [file] [log] [blame]
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +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 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/test/opus_test.h"
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000012
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000013#include <string>
14
Karl Wiberg5817d3d2018-04-06 10:06:42 +020015#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_coding/codecs/opus/opus_interface.h"
17#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
18#include "modules/audio_coding/test/TestStereo.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.org73222cf2013-03-15 13:29:17 +000021
22namespace webrtc {
23
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000024OpusTest::OpusTest()
Karl Wiberg5817d3d2018-04-06 10:06:42 +020025 : acm_receiver_(AudioCodingModule::Create(
26 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000027 channel_a2b_(NULL),
28 counter_(0),
29 payload_type_(255),
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000030 rtp_timestamp_(0) {}
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000031
32OpusTest::~OpusTest() {
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000033 if (channel_a2b_ != NULL) {
34 delete channel_a2b_;
35 channel_a2b_ = NULL;
36 }
37 if (opus_mono_encoder_ != NULL) {
38 WebRtcOpus_EncoderFree(opus_mono_encoder_);
39 opus_mono_encoder_ = NULL;
40 }
41 if (opus_stereo_encoder_ != NULL) {
42 WebRtcOpus_EncoderFree(opus_stereo_encoder_);
43 opus_stereo_encoder_ = NULL;
44 }
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +000045 if (opus_mono_decoder_ != NULL) {
46 WebRtcOpus_DecoderFree(opus_mono_decoder_);
47 opus_mono_decoder_ = NULL;
48 }
49 if (opus_stereo_decoder_ != NULL) {
50 WebRtcOpus_DecoderFree(opus_stereo_decoder_);
51 opus_stereo_decoder_ = NULL;
52 }
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000053}
54
55void OpusTest::Perform() {
56#ifndef WEBRTC_CODEC_OPUS
57 // Opus isn't defined, exit.
58 return;
59#else
60 uint16_t frequency_hz;
Peter Kasting69558702016-01-12 16:26:35 -080061 size_t audio_channels;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000062 int16_t test_cntr = 0;
63
64 // Open both mono and stereo test files in 32 kHz.
65 const std::string file_name_stereo =
66 webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm");
67 const std::string file_name_mono =
68 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
69 frequency_hz = 32000;
70 in_file_stereo_.Open(file_name_stereo, frequency_hz, "rb");
71 in_file_stereo_.ReadStereo(true);
72 in_file_mono_.Open(file_name_mono, frequency_hz, "rb");
73 in_file_mono_.ReadStereo(false);
74
75 // Create Opus encoders for mono and stereo.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000076 ASSERT_GT(WebRtcOpus_EncoderCreate(&opus_mono_encoder_, 1, 0), -1);
77 ASSERT_GT(WebRtcOpus_EncoderCreate(&opus_stereo_encoder_, 2, 1), -1);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000078
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +000079 // Create Opus decoders for mono and stereo for stand-alone testing of Opus.
80 ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_mono_decoder_, 1), -1);
81 ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_stereo_decoder_, 2), -1);
Karl Wiberg43766482015-08-27 15:22:11 +020082 WebRtcOpus_DecoderInit(opus_mono_decoder_);
83 WebRtcOpus_DecoderInit(opus_stereo_decoder_);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +000084
andrew@webrtc.org89df0922013-09-12 01:27:43 +000085 ASSERT_TRUE(acm_receiver_.get() != NULL);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000086 EXPECT_EQ(0, acm_receiver_->InitializeReceiver());
87
88 // Register Opus stereo as receiving codec.
Fredrik Solenberg657b2962018-12-05 10:30:25 +010089 constexpr int kOpusPayloadType = 120;
90 const SdpAudioFormat kOpusFormatStereo("opus", 48000, 2, {{"stereo", "1"}});
91 payload_type_ = kOpusPayloadType;
92 acm_receiver_->SetReceiveCodecs({{kOpusPayloadType, kOpusFormatStereo}});
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000093
94 // Create and connect the channel.
95 channel_a2b_ = new TestPackStereo;
andrew@webrtc.org89df0922013-09-12 01:27:43 +000096 channel_a2b_->RegisterReceiverACM(acm_receiver_.get());
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000097
98 //
99 // Test Stereo.
100 //
101
102 channel_a2b_->set_codec_mode(kStereo);
103 audio_channels = 2;
104 test_cntr++;
105 OpenOutFile(test_cntr);
106
107 // Run Opus with 2.5 ms frame size.
108 Run(channel_a2b_, audio_channels, 64000, 120);
109
110 // Run Opus with 5 ms frame size.
111 Run(channel_a2b_, audio_channels, 64000, 240);
112
113 // Run Opus with 10 ms frame size.
114 Run(channel_a2b_, audio_channels, 64000, 480);
115
116 // Run Opus with 20 ms frame size.
117 Run(channel_a2b_, audio_channels, 64000, 960);
118
119 // Run Opus with 40 ms frame size.
120 Run(channel_a2b_, audio_channels, 64000, 1920);
121
122 // Run Opus with 60 ms frame size.
123 Run(channel_a2b_, audio_channels, 64000, 2880);
124
125 out_file_.Close();
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000126 out_file_standalone_.Close();
127
128 //
129 // Test Opus stereo with packet-losses.
130 //
131
132 test_cntr++;
133 OpenOutFile(test_cntr);
134
135 // Run Opus with 20 ms frame size, 1% packet loss.
136 Run(channel_a2b_, audio_channels, 64000, 960, 1);
137
138 // Run Opus with 20 ms frame size, 5% packet loss.
139 Run(channel_a2b_, audio_channels, 64000, 960, 5);
140
141 // Run Opus with 20 ms frame size, 10% packet loss.
142 Run(channel_a2b_, audio_channels, 64000, 960, 10);
143
144 out_file_.Close();
145 out_file_standalone_.Close();
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000146
147 //
148 // Test Mono.
149 //
150 channel_a2b_->set_codec_mode(kMono);
151 audio_channels = 1;
152 test_cntr++;
153 OpenOutFile(test_cntr);
154
155 // Register Opus mono as receiving codec.
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100156 const SdpAudioFormat kOpusFormatMono("opus", 48000, 2);
157 acm_receiver_->SetReceiveCodecs({{kOpusPayloadType, kOpusFormatMono}});
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000158
159 // Run Opus with 2.5 ms frame size.
160 Run(channel_a2b_, audio_channels, 32000, 120);
161
162 // Run Opus with 5 ms frame size.
163 Run(channel_a2b_, audio_channels, 32000, 240);
164
165 // Run Opus with 10 ms frame size.
166 Run(channel_a2b_, audio_channels, 32000, 480);
167
168 // Run Opus with 20 ms frame size.
169 Run(channel_a2b_, audio_channels, 32000, 960);
170
171 // Run Opus with 40 ms frame size.
172 Run(channel_a2b_, audio_channels, 32000, 1920);
173
174 // Run Opus with 60 ms frame size.
175 Run(channel_a2b_, audio_channels, 32000, 2880);
176
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000177 out_file_.Close();
178 out_file_standalone_.Close();
179
180 //
181 // Test Opus mono with packet-losses.
182 //
183 test_cntr++;
184 OpenOutFile(test_cntr);
185
186 // Run Opus with 20 ms frame size, 1% packet loss.
187 Run(channel_a2b_, audio_channels, 64000, 960, 1);
188
189 // Run Opus with 20 ms frame size, 5% packet loss.
190 Run(channel_a2b_, audio_channels, 64000, 960, 5);
191
192 // Run Opus with 20 ms frame size, 10% packet loss.
193 Run(channel_a2b_, audio_channels, 64000, 960, 10);
194
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000195 // Close the files.
196 in_file_stereo_.Close();
197 in_file_mono_.Close();
198 out_file_.Close();
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000199 out_file_standalone_.Close();
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000200#endif
201}
202
Yves Gerey665174f2018-06-19 15:03:05 +0200203void OpusTest::Run(TestPackStereo* channel,
204 size_t channels,
205 int bitrate,
206 size_t frame_length,
207 int percent_loss) {
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000208 AudioFrame audio_frame;
209 int32_t out_freq_hz_b = out_file_.SamplingFrequency();
pkasting25702cb2016-01-08 13:50:27 -0800210 const size_t kBufferSizeSamples = 480 * 12 * 2; // 120 ms stereo audio.
henrik.lundin@webrtc.org439a4c42014-04-24 19:05:33 +0000211 int16_t audio[kBufferSizeSamples];
212 int16_t out_audio[kBufferSizeSamples];
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000213 int16_t audio_type;
pkasting25702cb2016-01-08 13:50:27 -0800214 size_t written_samples = 0;
215 size_t read_samples = 0;
216 size_t decoded_samples = 0;
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000217 bool first_packet = true;
218 uint32_t start_time_stamp = 0;
minyue@webrtc.org3e427262013-11-11 22:03:52 +0000219
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000220 channel->reset_payload_size();
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000221 counter_ = 0;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000222
223 // Set encoder rate.
224 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_mono_encoder_, bitrate));
225 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_stereo_encoder_, bitrate));
226
tina.legrand@webrtc.org92c0e292014-03-24 14:38:36 +0000227#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_ARCH_ARM)
228 // If we are on Android, iOS and/or ARM, use a lower complexity setting as
229 // default.
230 const int kOpusComplexity5 = 5;
231 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_mono_encoder_, kOpusComplexity5));
Yves Gerey665174f2018-06-19 15:03:05 +0200232 EXPECT_EQ(0,
233 WebRtcOpus_SetComplexity(opus_stereo_encoder_, kOpusComplexity5));
tina.legrand@webrtc.org92c0e292014-03-24 14:38:36 +0000234#endif
235
Henrik Lundin4d682082015-12-10 16:24:39 +0100236 // Fast-forward 1 second (100 blocks) since the files start with silence.
237 in_file_stereo_.FastForward(100);
238 in_file_mono_.FastForward(100);
239
240 // Limit the runtime to 1000 blocks of 10 ms each.
241 for (size_t audio_length = 0; audio_length < 1000; audio_length += 10) {
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000242 bool lost_packet = false;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000243
244 // Get 10 msec of audio.
245 if (channels == 1) {
246 if (in_file_mono_.EndOfFile()) {
247 break;
248 }
249 in_file_mono_.Read10MsData(audio_frame);
250 } else {
251 if (in_file_stereo_.EndOfFile()) {
252 break;
253 }
254 in_file_stereo_.Read10MsData(audio_frame);
255 }
256
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000257 // If input audio is sampled at 32 kHz, resampling to 48 kHz is required.
Yves Gerey665174f2018-06-19 15:03:05 +0200258 EXPECT_EQ(480, resampler_.Resample10Msec(
259 audio_frame.data(), audio_frame.sample_rate_hz_, 48000,
260 channels, kBufferSizeSamples - written_samples,
261 &audio[written_samples]));
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000262 written_samples += 480 * channels;
263
264 // Sometimes we need to loop over the audio vector to produce the right
265 // number of packets.
Yves Gerey665174f2018-06-19 15:03:05 +0200266 size_t loop_encode =
267 (written_samples - read_samples) / (channels * frame_length);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000268
269 if (loop_encode > 0) {
pkasting25702cb2016-01-08 13:50:27 -0800270 const size_t kMaxBytes = 1000; // Maximum number of bytes for one packet.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700271 size_t bitstream_len_byte;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000272 uint8_t bitstream[kMaxBytes];
pkasting25702cb2016-01-08 13:50:27 -0800273 for (size_t i = 0; i < loop_encode; i++) {
Peter Kastingbba78072015-06-11 19:02:46 -0700274 int bitstream_len_byte_int = WebRtcOpus_Encode(
275 (channels == 1) ? opus_mono_encoder_ : opus_stereo_encoder_,
276 &audio[read_samples], frame_length, kMaxBytes, bitstream);
277 ASSERT_GE(bitstream_len_byte_int, 0);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700278 bitstream_len_byte = static_cast<size_t>(bitstream_len_byte_int);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000279
280 // Simulate packet loss by setting |packet_loss_| to "true" in
281 // |percent_loss| percent of the loops.
282 // TODO(tlegrand): Move handling of loss simulation to TestPackStereo.
283 if (percent_loss > 0) {
284 if (counter_ == floor((100 / percent_loss) + 0.5)) {
285 counter_ = 0;
286 lost_packet = true;
287 channel->set_lost_packet(true);
288 } else {
289 lost_packet = false;
290 channel->set_lost_packet(false);
291 }
292 counter_++;
293 }
294
295 // Run stand-alone Opus decoder, or decode PLC.
296 if (channels == 1) {
297 if (!lost_packet) {
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000298 decoded_samples += WebRtcOpus_Decode(
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000299 opus_mono_decoder_, bitstream, bitstream_len_byte,
300 &out_audio[decoded_samples * channels], &audio_type);
301 } else {
302 decoded_samples += WebRtcOpus_DecodePlc(
303 opus_mono_decoder_, &out_audio[decoded_samples * channels], 1);
304 }
305 } else {
306 if (!lost_packet) {
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000307 decoded_samples += WebRtcOpus_Decode(
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000308 opus_stereo_decoder_, bitstream, bitstream_len_byte,
309 &out_audio[decoded_samples * channels], &audio_type);
310 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200311 decoded_samples +=
312 WebRtcOpus_DecodePlc(opus_stereo_decoder_,
313 &out_audio[decoded_samples * channels], 1);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000314 }
315 }
316
317 // Send data to the channel. "channel" will handle the loss simulation.
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000318 channel->SendData(kAudioFrameSpeech, payload_type_, rtp_timestamp_,
319 bitstream, bitstream_len_byte, NULL);
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000320 if (first_packet) {
321 first_packet = false;
322 start_time_stamp = rtp_timestamp_;
323 }
pkasting25702cb2016-01-08 13:50:27 -0800324 rtp_timestamp_ += static_cast<uint32_t>(frame_length);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000325 read_samples += frame_length * channels;
326 }
327 if (read_samples == written_samples) {
328 read_samples = 0;
329 written_samples = 0;
330 }
331 }
332
333 // Run received side of ACM.
henrik.lundind4ccb002016-05-17 12:21:55 -0700334 bool muted;
335 ASSERT_EQ(
336 0, acm_receiver_->PlayoutData10Ms(out_freq_hz_b, &audio_frame, &muted));
337 ASSERT_FALSE(muted);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000338
339 // Write output speech to file.
340 out_file_.Write10MsData(
yujo36b1a5f2017-06-12 12:45:32 -0700341 audio_frame.data(),
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000342 audio_frame.samples_per_channel_ * audio_frame.num_channels_);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000343
344 // Write stand-alone speech to file.
pkasting25702cb2016-01-08 13:50:27 -0800345 out_file_standalone_.Write10MsData(out_audio, decoded_samples * channels);
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000346
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000347 if (audio_frame.timestamp_ > start_time_stamp) {
348 // Number of channels should be the same for both stand-alone and
349 // ACM-decoding.
350 EXPECT_EQ(audio_frame.num_channels_, channels);
351 }
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000352
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000353 decoded_samples = 0;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000354 }
355
356 if (in_file_mono_.EndOfFile()) {
357 in_file_mono_.Rewind();
358 }
359 if (in_file_stereo_.EndOfFile()) {
360 in_file_stereo_.Rewind();
361 }
362 // Reset in case we ended with a lost packet.
363 channel->set_lost_packet(false);
364}
365
366void OpusTest::OpenOutFile(int test_number) {
367 std::string file_name;
368 std::stringstream file_stream;
Yves Gerey665174f2018-06-19 15:03:05 +0200369 file_stream << webrtc::test::OutputPath() << "opustest_out_" << test_number
370 << ".pcm";
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000371 file_name = file_stream.str();
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000372 out_file_.Open(file_name, 48000, "wb");
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000373 file_stream.str("");
374 file_name = file_stream.str();
375 file_stream << webrtc::test::OutputPath() << "opusstandalone_out_"
Yves Gerey665174f2018-06-19 15:03:05 +0200376 << test_number << ".pcm";
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000377 file_name = file_stream.str();
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000378 out_file_standalone_.Open(file_name, 48000, "wb");
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000379}
380
381} // namespace webrtc