blob: 40b51475bf3834b0bb033933485fa8ddbf2fe160 [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
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
14
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000015#include <string>
16
Karl Wiberg5817d3d2018-04-06 10:06:42 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/codecs/audio_format_conversion.h"
20#include "modules/audio_coding/codecs/opus/opus_interface.h"
21#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
22#include "modules/audio_coding/test/TestStereo.h"
23#include "modules/audio_coding/test/utility.h"
24#include "test/gtest.h"
25#include "test/testsupport/fileutils.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020026#include "typedefs.h" // NOLINT(build/include)
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000027
28namespace webrtc {
29
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000030OpusTest::OpusTest()
Karl Wiberg5817d3d2018-04-06 10:06:42 +020031 : acm_receiver_(AudioCodingModule::Create(
32 AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000033 channel_a2b_(NULL),
34 counter_(0),
35 payload_type_(255),
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:10 +000036 rtp_timestamp_(0) {}
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000037
38OpusTest::~OpusTest() {
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000039 if (channel_a2b_ != NULL) {
40 delete channel_a2b_;
41 channel_a2b_ = NULL;
42 }
43 if (opus_mono_encoder_ != NULL) {
44 WebRtcOpus_EncoderFree(opus_mono_encoder_);
45 opus_mono_encoder_ = NULL;
46 }
47 if (opus_stereo_encoder_ != NULL) {
48 WebRtcOpus_EncoderFree(opus_stereo_encoder_);
49 opus_stereo_encoder_ = NULL;
50 }
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +000051 if (opus_mono_decoder_ != NULL) {
52 WebRtcOpus_DecoderFree(opus_mono_decoder_);
53 opus_mono_decoder_ = NULL;
54 }
55 if (opus_stereo_decoder_ != NULL) {
56 WebRtcOpus_DecoderFree(opus_stereo_decoder_);
57 opus_stereo_decoder_ = NULL;
58 }
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000059}
60
61void OpusTest::Perform() {
62#ifndef WEBRTC_CODEC_OPUS
63 // Opus isn't defined, exit.
64 return;
65#else
66 uint16_t frequency_hz;
Peter Kasting69558702016-01-12 16:26:35 -080067 size_t audio_channels;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000068 int16_t test_cntr = 0;
69
70 // Open both mono and stereo test files in 32 kHz.
71 const std::string file_name_stereo =
72 webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm");
73 const std::string file_name_mono =
74 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
75 frequency_hz = 32000;
76 in_file_stereo_.Open(file_name_stereo, frequency_hz, "rb");
77 in_file_stereo_.ReadStereo(true);
78 in_file_mono_.Open(file_name_mono, frequency_hz, "rb");
79 in_file_mono_.ReadStereo(false);
80
81 // Create Opus encoders for mono and stereo.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000082 ASSERT_GT(WebRtcOpus_EncoderCreate(&opus_mono_encoder_, 1, 0), -1);
83 ASSERT_GT(WebRtcOpus_EncoderCreate(&opus_stereo_encoder_, 2, 1), -1);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000084
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +000085 // Create Opus decoders for mono and stereo for stand-alone testing of Opus.
86 ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_mono_decoder_, 1), -1);
87 ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_stereo_decoder_, 2), -1);
Karl Wiberg43766482015-08-27 15:22:11 +020088 WebRtcOpus_DecoderInit(opus_mono_decoder_);
89 WebRtcOpus_DecoderInit(opus_stereo_decoder_);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +000090
andrew@webrtc.org89df0922013-09-12 01:27:43 +000091 ASSERT_TRUE(acm_receiver_.get() != NULL);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +000092 EXPECT_EQ(0, acm_receiver_->InitializeReceiver());
93
94 // Register Opus stereo as receiving codec.
95 CodecInst opus_codec_param;
96 int codec_id = acm_receiver_->Codec("opus", 48000, 2);
97 EXPECT_EQ(0, acm_receiver_->Codec(codec_id, &opus_codec_param));
98 payload_type_ = opus_codec_param.pltype;
kwibergda2bf4e2016-10-24 13:47:09 -070099 EXPECT_EQ(true,
100 acm_receiver_->RegisterReceiveCodec(
101 opus_codec_param.pltype, CodecInstToSdp(opus_codec_param)));
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000102
103 // Create and connect the channel.
104 channel_a2b_ = new TestPackStereo;
andrew@webrtc.org89df0922013-09-12 01:27:43 +0000105 channel_a2b_->RegisterReceiverACM(acm_receiver_.get());
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000106
107 //
108 // Test Stereo.
109 //
110
111 channel_a2b_->set_codec_mode(kStereo);
112 audio_channels = 2;
113 test_cntr++;
114 OpenOutFile(test_cntr);
115
116 // Run Opus with 2.5 ms frame size.
117 Run(channel_a2b_, audio_channels, 64000, 120);
118
119 // Run Opus with 5 ms frame size.
120 Run(channel_a2b_, audio_channels, 64000, 240);
121
122 // Run Opus with 10 ms frame size.
123 Run(channel_a2b_, audio_channels, 64000, 480);
124
125 // Run Opus with 20 ms frame size.
126 Run(channel_a2b_, audio_channels, 64000, 960);
127
128 // Run Opus with 40 ms frame size.
129 Run(channel_a2b_, audio_channels, 64000, 1920);
130
131 // Run Opus with 60 ms frame size.
132 Run(channel_a2b_, audio_channels, 64000, 2880);
133
134 out_file_.Close();
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000135 out_file_standalone_.Close();
136
137 //
138 // Test Opus stereo with packet-losses.
139 //
140
141 test_cntr++;
142 OpenOutFile(test_cntr);
143
144 // Run Opus with 20 ms frame size, 1% packet loss.
145 Run(channel_a2b_, audio_channels, 64000, 960, 1);
146
147 // Run Opus with 20 ms frame size, 5% packet loss.
148 Run(channel_a2b_, audio_channels, 64000, 960, 5);
149
150 // Run Opus with 20 ms frame size, 10% packet loss.
151 Run(channel_a2b_, audio_channels, 64000, 960, 10);
152
153 out_file_.Close();
154 out_file_standalone_.Close();
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000155
156 //
157 // Test Mono.
158 //
159 channel_a2b_->set_codec_mode(kMono);
160 audio_channels = 1;
161 test_cntr++;
162 OpenOutFile(test_cntr);
163
164 // Register Opus mono as receiving codec.
165 opus_codec_param.channels = 1;
kwibergda2bf4e2016-10-24 13:47:09 -0700166 EXPECT_EQ(true,
167 acm_receiver_->RegisterReceiveCodec(
168 opus_codec_param.pltype, CodecInstToSdp(opus_codec_param)));
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000169
170 // Run Opus with 2.5 ms frame size.
171 Run(channel_a2b_, audio_channels, 32000, 120);
172
173 // Run Opus with 5 ms frame size.
174 Run(channel_a2b_, audio_channels, 32000, 240);
175
176 // Run Opus with 10 ms frame size.
177 Run(channel_a2b_, audio_channels, 32000, 480);
178
179 // Run Opus with 20 ms frame size.
180 Run(channel_a2b_, audio_channels, 32000, 960);
181
182 // Run Opus with 40 ms frame size.
183 Run(channel_a2b_, audio_channels, 32000, 1920);
184
185 // Run Opus with 60 ms frame size.
186 Run(channel_a2b_, audio_channels, 32000, 2880);
187
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000188 out_file_.Close();
189 out_file_standalone_.Close();
190
191 //
192 // Test Opus mono with packet-losses.
193 //
194 test_cntr++;
195 OpenOutFile(test_cntr);
196
197 // Run Opus with 20 ms frame size, 1% packet loss.
198 Run(channel_a2b_, audio_channels, 64000, 960, 1);
199
200 // Run Opus with 20 ms frame size, 5% packet loss.
201 Run(channel_a2b_, audio_channels, 64000, 960, 5);
202
203 // Run Opus with 20 ms frame size, 10% packet loss.
204 Run(channel_a2b_, audio_channels, 64000, 960, 10);
205
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000206 // Close the files.
207 in_file_stereo_.Close();
208 in_file_mono_.Close();
209 out_file_.Close();
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000210 out_file_standalone_.Close();
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000211#endif
212}
213
Yves Gerey665174f2018-06-19 15:03:05 +0200214void OpusTest::Run(TestPackStereo* channel,
215 size_t channels,
216 int bitrate,
217 size_t frame_length,
218 int percent_loss) {
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000219 AudioFrame audio_frame;
220 int32_t out_freq_hz_b = out_file_.SamplingFrequency();
pkasting25702cb2016-01-08 13:50:27 -0800221 const size_t kBufferSizeSamples = 480 * 12 * 2; // 120 ms stereo audio.
henrik.lundin@webrtc.org439a4c42014-04-24 19:05:33 +0000222 int16_t audio[kBufferSizeSamples];
223 int16_t out_audio[kBufferSizeSamples];
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000224 int16_t audio_type;
pkasting25702cb2016-01-08 13:50:27 -0800225 size_t written_samples = 0;
226 size_t read_samples = 0;
227 size_t decoded_samples = 0;
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000228 bool first_packet = true;
229 uint32_t start_time_stamp = 0;
minyue@webrtc.org3e427262013-11-11 22:03:52 +0000230
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000231 channel->reset_payload_size();
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000232 counter_ = 0;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000233
234 // Set encoder rate.
235 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_mono_encoder_, bitrate));
236 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_stereo_encoder_, bitrate));
237
tina.legrand@webrtc.org92c0e292014-03-24 14:38:36 +0000238#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_ARCH_ARM)
239 // If we are on Android, iOS and/or ARM, use a lower complexity setting as
240 // default.
241 const int kOpusComplexity5 = 5;
242 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_mono_encoder_, kOpusComplexity5));
Yves Gerey665174f2018-06-19 15:03:05 +0200243 EXPECT_EQ(0,
244 WebRtcOpus_SetComplexity(opus_stereo_encoder_, kOpusComplexity5));
tina.legrand@webrtc.org92c0e292014-03-24 14:38:36 +0000245#endif
246
Henrik Lundin4d682082015-12-10 16:24:39 +0100247 // Fast-forward 1 second (100 blocks) since the files start with silence.
248 in_file_stereo_.FastForward(100);
249 in_file_mono_.FastForward(100);
250
251 // Limit the runtime to 1000 blocks of 10 ms each.
252 for (size_t audio_length = 0; audio_length < 1000; audio_length += 10) {
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000253 bool lost_packet = false;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000254
255 // Get 10 msec of audio.
256 if (channels == 1) {
257 if (in_file_mono_.EndOfFile()) {
258 break;
259 }
260 in_file_mono_.Read10MsData(audio_frame);
261 } else {
262 if (in_file_stereo_.EndOfFile()) {
263 break;
264 }
265 in_file_stereo_.Read10MsData(audio_frame);
266 }
267
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000268 // If input audio is sampled at 32 kHz, resampling to 48 kHz is required.
Yves Gerey665174f2018-06-19 15:03:05 +0200269 EXPECT_EQ(480, resampler_.Resample10Msec(
270 audio_frame.data(), audio_frame.sample_rate_hz_, 48000,
271 channels, kBufferSizeSamples - written_samples,
272 &audio[written_samples]));
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000273 written_samples += 480 * channels;
274
275 // Sometimes we need to loop over the audio vector to produce the right
276 // number of packets.
Yves Gerey665174f2018-06-19 15:03:05 +0200277 size_t loop_encode =
278 (written_samples - read_samples) / (channels * frame_length);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000279
280 if (loop_encode > 0) {
pkasting25702cb2016-01-08 13:50:27 -0800281 const size_t kMaxBytes = 1000; // Maximum number of bytes for one packet.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700282 size_t bitstream_len_byte;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000283 uint8_t bitstream[kMaxBytes];
pkasting25702cb2016-01-08 13:50:27 -0800284 for (size_t i = 0; i < loop_encode; i++) {
Peter Kastingbba78072015-06-11 19:02:46 -0700285 int bitstream_len_byte_int = WebRtcOpus_Encode(
286 (channels == 1) ? opus_mono_encoder_ : opus_stereo_encoder_,
287 &audio[read_samples], frame_length, kMaxBytes, bitstream);
288 ASSERT_GE(bitstream_len_byte_int, 0);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700289 bitstream_len_byte = static_cast<size_t>(bitstream_len_byte_int);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000290
291 // Simulate packet loss by setting |packet_loss_| to "true" in
292 // |percent_loss| percent of the loops.
293 // TODO(tlegrand): Move handling of loss simulation to TestPackStereo.
294 if (percent_loss > 0) {
295 if (counter_ == floor((100 / percent_loss) + 0.5)) {
296 counter_ = 0;
297 lost_packet = true;
298 channel->set_lost_packet(true);
299 } else {
300 lost_packet = false;
301 channel->set_lost_packet(false);
302 }
303 counter_++;
304 }
305
306 // Run stand-alone Opus decoder, or decode PLC.
307 if (channels == 1) {
308 if (!lost_packet) {
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000309 decoded_samples += WebRtcOpus_Decode(
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000310 opus_mono_decoder_, bitstream, bitstream_len_byte,
311 &out_audio[decoded_samples * channels], &audio_type);
312 } else {
313 decoded_samples += WebRtcOpus_DecodePlc(
314 opus_mono_decoder_, &out_audio[decoded_samples * channels], 1);
315 }
316 } else {
317 if (!lost_packet) {
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000318 decoded_samples += WebRtcOpus_Decode(
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000319 opus_stereo_decoder_, bitstream, bitstream_len_byte,
320 &out_audio[decoded_samples * channels], &audio_type);
321 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200322 decoded_samples +=
323 WebRtcOpus_DecodePlc(opus_stereo_decoder_,
324 &out_audio[decoded_samples * channels], 1);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000325 }
326 }
327
328 // Send data to the channel. "channel" will handle the loss simulation.
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000329 channel->SendData(kAudioFrameSpeech, payload_type_, rtp_timestamp_,
330 bitstream, bitstream_len_byte, NULL);
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000331 if (first_packet) {
332 first_packet = false;
333 start_time_stamp = rtp_timestamp_;
334 }
pkasting25702cb2016-01-08 13:50:27 -0800335 rtp_timestamp_ += static_cast<uint32_t>(frame_length);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000336 read_samples += frame_length * channels;
337 }
338 if (read_samples == written_samples) {
339 read_samples = 0;
340 written_samples = 0;
341 }
342 }
343
344 // Run received side of ACM.
henrik.lundind4ccb002016-05-17 12:21:55 -0700345 bool muted;
346 ASSERT_EQ(
347 0, acm_receiver_->PlayoutData10Ms(out_freq_hz_b, &audio_frame, &muted));
348 ASSERT_FALSE(muted);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000349
350 // Write output speech to file.
351 out_file_.Write10MsData(
yujo36b1a5f2017-06-12 12:45:32 -0700352 audio_frame.data(),
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000353 audio_frame.samples_per_channel_ * audio_frame.num_channels_);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000354
355 // Write stand-alone speech to file.
pkasting25702cb2016-01-08 13:50:27 -0800356 out_file_standalone_.Write10MsData(out_audio, decoded_samples * channels);
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000357
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000358 if (audio_frame.timestamp_ > start_time_stamp) {
359 // Number of channels should be the same for both stand-alone and
360 // ACM-decoding.
361 EXPECT_EQ(audio_frame.num_channels_, channels);
362 }
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48 +0000363
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000364 decoded_samples = 0;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000365 }
366
367 if (in_file_mono_.EndOfFile()) {
368 in_file_mono_.Rewind();
369 }
370 if (in_file_stereo_.EndOfFile()) {
371 in_file_stereo_.Rewind();
372 }
373 // Reset in case we ended with a lost packet.
374 channel->set_lost_packet(false);
375}
376
377void OpusTest::OpenOutFile(int test_number) {
378 std::string file_name;
379 std::stringstream file_stream;
Yves Gerey665174f2018-06-19 15:03:05 +0200380 file_stream << webrtc::test::OutputPath() << "opustest_out_" << test_number
381 << ".pcm";
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000382 file_name = file_stream.str();
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000383 out_file_.Open(file_name, 48000, "wb");
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000384 file_stream.str("");
385 file_name = file_stream.str();
386 file_stream << webrtc::test::OutputPath() << "opusstandalone_out_"
Yves Gerey665174f2018-06-19 15:03:05 +0200387 << test_number << ".pcm";
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000388 file_name = file_stream.str();
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000389 out_file_standalone_.Open(file_name, 48000, "wb");
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17 +0000390}
391
392} // namespace webrtc