blob: 954b1832c19467886555021900d781eb92cede77 [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"
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +000016#include "common_types.h" // NOLINT(build/include)
17#include "modules/audio_coding/codecs/audio_format_conversion.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_coding/codecs/opus/opus_interface.h"
19#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
20#include "modules/audio_coding/test/TestStereo.h"
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +000021#include "modules/audio_coding/test/utility.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "test/gtest.h"
23#include "test/testsupport/fileutils.h"
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000024
25namespace webrtc {
26
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000027OpusTest::OpusTest()
Karl Wiberg5817d3d2018-04-06 10:06:42 +020028 : acm_receiver_(AudioCodingModule::Create(
29 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000030 channel_a2b_(NULL),
31 counter_(0),
32 payload_type_(255),
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000033 rtp_timestamp_(0) {}
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000034
35OpusTest::~OpusTest() {
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000036 if (channel_a2b_ != NULL) {
37 delete channel_a2b_;
38 channel_a2b_ = NULL;
39 }
40 if (opus_mono_encoder_ != NULL) {
41 WebRtcOpus_EncoderFree(opus_mono_encoder_);
42 opus_mono_encoder_ = NULL;
43 }
44 if (opus_stereo_encoder_ != NULL) {
45 WebRtcOpus_EncoderFree(opus_stereo_encoder_);
46 opus_stereo_encoder_ = NULL;
47 }
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +000048 if (opus_mono_decoder_ != NULL) {
49 WebRtcOpus_DecoderFree(opus_mono_decoder_);
50 opus_mono_decoder_ = NULL;
51 }
52 if (opus_stereo_decoder_ != NULL) {
53 WebRtcOpus_DecoderFree(opus_stereo_decoder_);
54 opus_stereo_decoder_ = NULL;
55 }
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000056}
57
58void OpusTest::Perform() {
59#ifndef WEBRTC_CODEC_OPUS
60 // Opus isn't defined, exit.
61 return;
62#else
63 uint16_t frequency_hz;
Peter Kasting69558702016-01-12 16:26:35 -080064 size_t audio_channels;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000065 int16_t test_cntr = 0;
66
67 // Open both mono and stereo test files in 32 kHz.
68 const std::string file_name_stereo =
69 webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm");
70 const std::string file_name_mono =
71 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
72 frequency_hz = 32000;
73 in_file_stereo_.Open(file_name_stereo, frequency_hz, "rb");
74 in_file_stereo_.ReadStereo(true);
75 in_file_mono_.Open(file_name_mono, frequency_hz, "rb");
76 in_file_mono_.ReadStereo(false);
77
78 // Create Opus encoders for mono and stereo.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000079 ASSERT_GT(WebRtcOpus_EncoderCreate(&opus_mono_encoder_, 1, 0), -1);
80 ASSERT_GT(WebRtcOpus_EncoderCreate(&opus_stereo_encoder_, 2, 1), -1);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000081
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +000082 // Create Opus decoders for mono and stereo for stand-alone testing of Opus.
83 ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_mono_decoder_, 1), -1);
84 ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_stereo_decoder_, 2), -1);
Karl Wiberg43766482015-08-27 15:22:11 +020085 WebRtcOpus_DecoderInit(opus_mono_decoder_);
86 WebRtcOpus_DecoderInit(opus_stereo_decoder_);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +000087
andrew@webrtc.org89df0922013-09-12 01:27:43 +000088 ASSERT_TRUE(acm_receiver_.get() != NULL);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000089 EXPECT_EQ(0, acm_receiver_->InitializeReceiver());
90
91 // Register Opus stereo as receiving codec.
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +000092 CodecInst opus_codec_param;
93 int codec_id = acm_receiver_->Codec("opus", 48000, 2);
94 EXPECT_EQ(0, acm_receiver_->Codec(codec_id, &opus_codec_param));
95 payload_type_ = opus_codec_param.pltype;
96 EXPECT_EQ(true,
97 acm_receiver_->RegisterReceiveCodec(
98 opus_codec_param.pltype, CodecInstToSdp(opus_codec_param)));
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000099
100 // Create and connect the channel.
101 channel_a2b_ = new TestPackStereo;
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000102 channel_a2b_->RegisterReceiverACM(acm_receiver_.get());
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000103
104 //
105 // Test Stereo.
106 //
107
108 channel_a2b_->set_codec_mode(kStereo);
109 audio_channels = 2;
110 test_cntr++;
111 OpenOutFile(test_cntr);
112
113 // Run Opus with 2.5 ms frame size.
114 Run(channel_a2b_, audio_channels, 64000, 120);
115
116 // Run Opus with 5 ms frame size.
117 Run(channel_a2b_, audio_channels, 64000, 240);
118
119 // Run Opus with 10 ms frame size.
120 Run(channel_a2b_, audio_channels, 64000, 480);
121
122 // Run Opus with 20 ms frame size.
123 Run(channel_a2b_, audio_channels, 64000, 960);
124
125 // Run Opus with 40 ms frame size.
126 Run(channel_a2b_, audio_channels, 64000, 1920);
127
128 // Run Opus with 60 ms frame size.
129 Run(channel_a2b_, audio_channels, 64000, 2880);
130
131 out_file_.Close();
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000132 out_file_standalone_.Close();
133
134 //
135 // Test Opus stereo with packet-losses.
136 //
137
138 test_cntr++;
139 OpenOutFile(test_cntr);
140
141 // Run Opus with 20 ms frame size, 1% packet loss.
142 Run(channel_a2b_, audio_channels, 64000, 960, 1);
143
144 // Run Opus with 20 ms frame size, 5% packet loss.
145 Run(channel_a2b_, audio_channels, 64000, 960, 5);
146
147 // Run Opus with 20 ms frame size, 10% packet loss.
148 Run(channel_a2b_, audio_channels, 64000, 960, 10);
149
150 out_file_.Close();
151 out_file_standalone_.Close();
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000152
153 //
154 // Test Mono.
155 //
156 channel_a2b_->set_codec_mode(kMono);
157 audio_channels = 1;
158 test_cntr++;
159 OpenOutFile(test_cntr);
160
161 // Register Opus mono as receiving codec.
Fredrik Solenbergec0f45b2018-12-03 15:50:44 +0000162 opus_codec_param.channels = 1;
163 EXPECT_EQ(true,
164 acm_receiver_->RegisterReceiveCodec(
165 opus_codec_param.pltype, CodecInstToSdp(opus_codec_param)));
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000166
167 // Run Opus with 2.5 ms frame size.
168 Run(channel_a2b_, audio_channels, 32000, 120);
169
170 // Run Opus with 5 ms frame size.
171 Run(channel_a2b_, audio_channels, 32000, 240);
172
173 // Run Opus with 10 ms frame size.
174 Run(channel_a2b_, audio_channels, 32000, 480);
175
176 // Run Opus with 20 ms frame size.
177 Run(channel_a2b_, audio_channels, 32000, 960);
178
179 // Run Opus with 40 ms frame size.
180 Run(channel_a2b_, audio_channels, 32000, 1920);
181
182 // Run Opus with 60 ms frame size.
183 Run(channel_a2b_, audio_channels, 32000, 2880);
184
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000185 out_file_.Close();
186 out_file_standalone_.Close();
187
188 //
189 // Test Opus mono with packet-losses.
190 //
191 test_cntr++;
192 OpenOutFile(test_cntr);
193
194 // Run Opus with 20 ms frame size, 1% packet loss.
195 Run(channel_a2b_, audio_channels, 64000, 960, 1);
196
197 // Run Opus with 20 ms frame size, 5% packet loss.
198 Run(channel_a2b_, audio_channels, 64000, 960, 5);
199
200 // Run Opus with 20 ms frame size, 10% packet loss.
201 Run(channel_a2b_, audio_channels, 64000, 960, 10);
202
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000203 // Close the files.
204 in_file_stereo_.Close();
205 in_file_mono_.Close();
206 out_file_.Close();
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000207 out_file_standalone_.Close();
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000208#endif
209}
210
Yves Gerey665174f2018-06-19 15:03:05 +0200211void OpusTest::Run(TestPackStereo* channel,
212 size_t channels,
213 int bitrate,
214 size_t frame_length,
215 int percent_loss) {
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000216 AudioFrame audio_frame;
217 int32_t out_freq_hz_b = out_file_.SamplingFrequency();
pkasting25702cb2016-01-08 13:50:27 -0800218 const size_t kBufferSizeSamples = 480 * 12 * 2; // 120 ms stereo audio.
henrik.lundin@webrtc.org439a4c42014-04-24 19:05:33 +0000219 int16_t audio[kBufferSizeSamples];
220 int16_t out_audio[kBufferSizeSamples];
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000221 int16_t audio_type;
pkasting25702cb2016-01-08 13:50:27 -0800222 size_t written_samples = 0;
223 size_t read_samples = 0;
224 size_t decoded_samples = 0;
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000225 bool first_packet = true;
226 uint32_t start_time_stamp = 0;
minyue@webrtc.org3e427262013-11-11 22:03:52 +0000227
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000228 channel->reset_payload_size();
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000229 counter_ = 0;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000230
231 // Set encoder rate.
232 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_mono_encoder_, bitrate));
233 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_stereo_encoder_, bitrate));
234
tina.legrand@webrtc.org92c0e292014-03-24 14:38:36 +0000235#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_ARCH_ARM)
236 // If we are on Android, iOS and/or ARM, use a lower complexity setting as
237 // default.
238 const int kOpusComplexity5 = 5;
239 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_mono_encoder_, kOpusComplexity5));
Yves Gerey665174f2018-06-19 15:03:05 +0200240 EXPECT_EQ(0,
241 WebRtcOpus_SetComplexity(opus_stereo_encoder_, kOpusComplexity5));
tina.legrand@webrtc.org92c0e292014-03-24 14:38:36 +0000242#endif
243
Henrik Lundin4d682082015-12-10 16:24:39 +0100244 // Fast-forward 1 second (100 blocks) since the files start with silence.
245 in_file_stereo_.FastForward(100);
246 in_file_mono_.FastForward(100);
247
248 // Limit the runtime to 1000 blocks of 10 ms each.
249 for (size_t audio_length = 0; audio_length < 1000; audio_length += 10) {
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000250 bool lost_packet = false;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000251
252 // Get 10 msec of audio.
253 if (channels == 1) {
254 if (in_file_mono_.EndOfFile()) {
255 break;
256 }
257 in_file_mono_.Read10MsData(audio_frame);
258 } else {
259 if (in_file_stereo_.EndOfFile()) {
260 break;
261 }
262 in_file_stereo_.Read10MsData(audio_frame);
263 }
264
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000265 // If input audio is sampled at 32 kHz, resampling to 48 kHz is required.
Yves Gerey665174f2018-06-19 15:03:05 +0200266 EXPECT_EQ(480, resampler_.Resample10Msec(
267 audio_frame.data(), audio_frame.sample_rate_hz_, 48000,
268 channels, kBufferSizeSamples - written_samples,
269 &audio[written_samples]));
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000270 written_samples += 480 * channels;
271
272 // Sometimes we need to loop over the audio vector to produce the right
273 // number of packets.
Yves Gerey665174f2018-06-19 15:03:05 +0200274 size_t loop_encode =
275 (written_samples - read_samples) / (channels * frame_length);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000276
277 if (loop_encode > 0) {
pkasting25702cb2016-01-08 13:50:27 -0800278 const size_t kMaxBytes = 1000; // Maximum number of bytes for one packet.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700279 size_t bitstream_len_byte;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000280 uint8_t bitstream[kMaxBytes];
pkasting25702cb2016-01-08 13:50:27 -0800281 for (size_t i = 0; i < loop_encode; i++) {
Peter Kastingbba78072015-06-11 19:02:46 -0700282 int bitstream_len_byte_int = WebRtcOpus_Encode(
283 (channels == 1) ? opus_mono_encoder_ : opus_stereo_encoder_,
284 &audio[read_samples], frame_length, kMaxBytes, bitstream);
285 ASSERT_GE(bitstream_len_byte_int, 0);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700286 bitstream_len_byte = static_cast<size_t>(bitstream_len_byte_int);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000287
288 // Simulate packet loss by setting |packet_loss_| to "true" in
289 // |percent_loss| percent of the loops.
290 // TODO(tlegrand): Move handling of loss simulation to TestPackStereo.
291 if (percent_loss > 0) {
292 if (counter_ == floor((100 / percent_loss) + 0.5)) {
293 counter_ = 0;
294 lost_packet = true;
295 channel->set_lost_packet(true);
296 } else {
297 lost_packet = false;
298 channel->set_lost_packet(false);
299 }
300 counter_++;
301 }
302
303 // Run stand-alone Opus decoder, or decode PLC.
304 if (channels == 1) {
305 if (!lost_packet) {
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000306 decoded_samples += WebRtcOpus_Decode(
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000307 opus_mono_decoder_, bitstream, bitstream_len_byte,
308 &out_audio[decoded_samples * channels], &audio_type);
309 } else {
310 decoded_samples += WebRtcOpus_DecodePlc(
311 opus_mono_decoder_, &out_audio[decoded_samples * channels], 1);
312 }
313 } else {
314 if (!lost_packet) {
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000315 decoded_samples += WebRtcOpus_Decode(
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000316 opus_stereo_decoder_, bitstream, bitstream_len_byte,
317 &out_audio[decoded_samples * channels], &audio_type);
318 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200319 decoded_samples +=
320 WebRtcOpus_DecodePlc(opus_stereo_decoder_,
321 &out_audio[decoded_samples * channels], 1);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000322 }
323 }
324
325 // Send data to the channel. "channel" will handle the loss simulation.
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000326 channel->SendData(kAudioFrameSpeech, payload_type_, rtp_timestamp_,
327 bitstream, bitstream_len_byte, NULL);
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000328 if (first_packet) {
329 first_packet = false;
330 start_time_stamp = rtp_timestamp_;
331 }
pkasting25702cb2016-01-08 13:50:27 -0800332 rtp_timestamp_ += static_cast<uint32_t>(frame_length);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000333 read_samples += frame_length * channels;
334 }
335 if (read_samples == written_samples) {
336 read_samples = 0;
337 written_samples = 0;
338 }
339 }
340
341 // Run received side of ACM.
henrik.lundind4ccb002016-05-17 12:21:55 -0700342 bool muted;
343 ASSERT_EQ(
344 0, acm_receiver_->PlayoutData10Ms(out_freq_hz_b, &audio_frame, &muted));
345 ASSERT_FALSE(muted);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000346
347 // Write output speech to file.
348 out_file_.Write10MsData(
yujo36b1a5f2017-06-12 12:45:32 -0700349 audio_frame.data(),
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000350 audio_frame.samples_per_channel_ * audio_frame.num_channels_);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000351
352 // Write stand-alone speech to file.
pkasting25702cb2016-01-08 13:50:27 -0800353 out_file_standalone_.Write10MsData(out_audio, decoded_samples * channels);
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000354
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000355 if (audio_frame.timestamp_ > start_time_stamp) {
356 // Number of channels should be the same for both stand-alone and
357 // ACM-decoding.
358 EXPECT_EQ(audio_frame.num_channels_, channels);
359 }
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000360
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000361 decoded_samples = 0;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000362 }
363
364 if (in_file_mono_.EndOfFile()) {
365 in_file_mono_.Rewind();
366 }
367 if (in_file_stereo_.EndOfFile()) {
368 in_file_stereo_.Rewind();
369 }
370 // Reset in case we ended with a lost packet.
371 channel->set_lost_packet(false);
372}
373
374void OpusTest::OpenOutFile(int test_number) {
375 std::string file_name;
376 std::stringstream file_stream;
Yves Gerey665174f2018-06-19 15:03:05 +0200377 file_stream << webrtc::test::OutputPath() << "opustest_out_" << test_number
378 << ".pcm";
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000379 file_name = file_stream.str();
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000380 out_file_.Open(file_name, 48000, "wb");
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000381 file_stream.str("");
382 file_name = file_stream.str();
383 file_stream << webrtc::test::OutputPath() << "opusstandalone_out_"
Yves Gerey665174f2018-06-19 15:03:05 +0200384 << test_number << ".pcm";
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000385 file_name = file_stream.str();
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000386 out_file_standalone_.Open(file_name, 48000, "wb");
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000387}
388
389} // namespace webrtc