blob: dfebb5f6d7cbe0edd242cb8d26228e847fa362da [file] [log] [blame]
tina.legrand@webrtc.org73222cf2013-03-15 13:29:171/*
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 04:47:3111#include "modules/audio_coding/test/opus_test.h"
tina.legrand@webrtc.org73222cf2013-03-15 13:29:1712
tina.legrand@webrtc.org73222cf2013-03-15 13:29:1713#include <string>
14
Karl Wiberg5817d3d2018-04-06 08:06:4215#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3116#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 04:47:3119#include "test/gtest.h"
Steve Anton10542f22019-01-11 17:11:0020#include "test/testsupport/file_utils.h"
tina.legrand@webrtc.org73222cf2013-03-15 13:29:1721
22namespace webrtc {
23
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:1024OpusTest::OpusTest()
Henrik Lundin84f75692023-02-01 12:07:1025 : acm_receiver_(std::make_unique<acm2::AcmReceiver>(
26 acm2::AcmReceiver::Config(CreateBuiltinAudioDecoderFactory()))),
tina.legrand@webrtc.org73222cf2013-03-15 13:29:1727 channel_a2b_(NULL),
28 counter_(0),
29 payload_type_(255),
henrik.lundin@webrtc.orgadaf8092014-04-17 08:29:1030 rtp_timestamp_(0) {}
tina.legrand@webrtc.org73222cf2013-03-15 13:29:1731
32OpusTest::~OpusTest() {
tina.legrand@webrtc.org73222cf2013-03-15 13:29:1733 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:0745 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:1753}
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-13 00:26:3561 size_t audio_channels;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:1762 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.
Karl Wiberg7e7c5c32019-05-21 09:50:3276 ASSERT_GT(WebRtcOpus_EncoderCreate(&opus_mono_encoder_, 1, 0, 48000), -1);
77 ASSERT_GT(WebRtcOpus_EncoderCreate(&opus_stereo_encoder_, 2, 1, 48000), -1);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:1778
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:0779 // Create Opus decoders for mono and stereo for stand-alone testing of Opus.
Karl Wiberga1d1a1e2019-05-28 12:41:0780 ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_mono_decoder_, 1, 48000), -1);
81 ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_stereo_decoder_, 2, 48000), -1);
Karl Wiberg43766482015-08-27 13:22:1182 WebRtcOpus_DecoderInit(opus_mono_decoder_);
83 WebRtcOpus_DecoderInit(opus_stereo_decoder_);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:0784
andrew@webrtc.org89df0922013-09-12 01:27:4385 ASSERT_TRUE(acm_receiver_.get() != NULL);
Henrik Lundin84f75692023-02-01 12:07:1086 acm_receiver_->FlushBuffers();
tina.legrand@webrtc.org73222cf2013-03-15 13:29:1787
88 // Register Opus stereo as receiving codec.
Fredrik Solenberg657b2962018-12-05 09:30:2589 constexpr int kOpusPayloadType = 120;
90 const SdpAudioFormat kOpusFormatStereo("opus", 48000, 2, {{"stereo", "1"}});
91 payload_type_ = kOpusPayloadType;
Henrik Lundin84f75692023-02-01 12:07:1092 acm_receiver_->SetCodecs({{kOpusPayloadType, kOpusFormatStereo}});
tina.legrand@webrtc.org73222cf2013-03-15 13:29:1793
94 // Create and connect the channel.
95 channel_a2b_ = new TestPackStereo;
andrew@webrtc.org89df0922013-09-12 01:27:4396 channel_a2b_->RegisterReceiverACM(acm_receiver_.get());
tina.legrand@webrtc.org73222cf2013-03-15 13:29:1797
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:07126 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:17146
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 09:30:25156 const SdpAudioFormat kOpusFormatMono("opus", 48000, 2);
Henrik Lundin84f75692023-02-01 12:07:10157 acm_receiver_->SetCodecs({{kOpusPayloadType, kOpusFormatMono}});
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17158
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:07177 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:17195 // 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:07199 out_file_standalone_.Close();
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17200#endif
201}
202
Yves Gerey665174f2018-06-19 13:03:05203void 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:17208 AudioFrame audio_frame;
209 int32_t out_freq_hz_b = out_file_.SamplingFrequency();
pkasting25702cb2016-01-08 21:50:27210 const size_t kBufferSizeSamples = 480 * 12 * 2; // 120 ms stereo audio.
henrik.lundin@webrtc.org439a4c42014-04-24 19:05:33211 int16_t audio[kBufferSizeSamples];
212 int16_t out_audio[kBufferSizeSamples];
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07213 int16_t audio_type;
pkasting25702cb2016-01-08 21:50:27214 size_t written_samples = 0;
215 size_t read_samples = 0;
216 size_t decoded_samples = 0;
minyue@webrtc.orgf563e852014-07-18 21:11:27217 bool first_packet = true;
218 uint32_t start_time_stamp = 0;
minyue@webrtc.org3e427262013-11-11 22:03:52219
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17220 channel->reset_payload_size();
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07221 counter_ = 0;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17222
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:36227#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 13:03:05232 EXPECT_EQ(0,
233 WebRtcOpus_SetComplexity(opus_stereo_encoder_, kOpusComplexity5));
tina.legrand@webrtc.org92c0e292014-03-24 14:38:36234#endif
235
Henrik Lundin4d682082015-12-10 15:24:39236 // 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:07242 bool lost_packet = false;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17243
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:07257 // If input audio is sampled at 32 kHz, resampling to 48 kHz is required.
Yves Gerey665174f2018-06-19 13:03:05258 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:17262 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 13:03:05266 size_t loop_encode =
267 (written_samples - read_samples) / (channels * frame_length);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17268
269 if (loop_encode > 0) {
pkasting25702cb2016-01-08 21:50:27270 const size_t kMaxBytes = 1000; // Maximum number of bytes for one packet.
Peter Kastingdce40cf2015-08-24 21:52:23271 size_t bitstream_len_byte;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17272 uint8_t bitstream[kMaxBytes];
pkasting25702cb2016-01-08 21:50:27273 for (size_t i = 0; i < loop_encode; i++) {
Peter Kastingbba78072015-06-12 02:02:46274 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 21:52:23278 bitstream_len_byte = static_cast<size_t>(bitstream_len_byte_int);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07279
Artem Titovd00ce742021-07-28 18:00:17280 // Simulate packet loss by setting `packet_loss_` to "true" in
281 // `percent_loss` percent of the loops.
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07282 // 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:12298 decoded_samples += WebRtcOpus_Decode(
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07299 opus_mono_decoder_, bitstream, bitstream_len_byte,
300 &out_audio[decoded_samples * channels], &audio_type);
301 } else {
Minyue Li8e83c7a2019-11-04 13:47:52302 // Call decoder PLC.
303 constexpr int kPlcDurationMs = 10;
304 constexpr int kPlcSamples = 48 * kPlcDurationMs;
305 size_t total_plc_samples = 0;
306 while (total_plc_samples < frame_length) {
307 int ret = WebRtcOpus_Decode(
308 opus_mono_decoder_, NULL, 0,
309 &out_audio[decoded_samples * channels], &audio_type);
310 EXPECT_EQ(ret, kPlcSamples);
311 decoded_samples += ret;
312 total_plc_samples += ret;
313 }
314 EXPECT_EQ(total_plc_samples, frame_length);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07315 }
316 } else {
317 if (!lost_packet) {
minyue@webrtc.org33ccdfa2014-12-04 12:14:12318 decoded_samples += WebRtcOpus_Decode(
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07319 opus_stereo_decoder_, bitstream, bitstream_len_byte,
320 &out_audio[decoded_samples * channels], &audio_type);
321 } else {
Minyue Li8e83c7a2019-11-04 13:47:52322 // Call decoder PLC.
323 constexpr int kPlcDurationMs = 10;
324 constexpr int kPlcSamples = 48 * kPlcDurationMs;
325 size_t total_plc_samples = 0;
326 while (total_plc_samples < frame_length) {
327 int ret = WebRtcOpus_Decode(
328 opus_stereo_decoder_, NULL, 0,
329 &out_audio[decoded_samples * channels], &audio_type);
330 EXPECT_EQ(ret, kPlcSamples);
331 decoded_samples += ret;
332 total_plc_samples += ret;
333 }
334 EXPECT_EQ(total_plc_samples, frame_length);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07335 }
336 }
337
338 // Send data to the channel. "channel" will handle the loss simulation.
Niels Möllerc936cb62019-03-19 13:10:16339 channel->SendData(AudioFrameType::kAudioFrameSpeech, payload_type_,
Minyue Liff0e4db2020-01-23 12:45:50340 rtp_timestamp_, bitstream, bitstream_len_byte, 0);
minyue@webrtc.orgf563e852014-07-18 21:11:27341 if (first_packet) {
342 first_packet = false;
343 start_time_stamp = rtp_timestamp_;
344 }
pkasting25702cb2016-01-08 21:50:27345 rtp_timestamp_ += static_cast<uint32_t>(frame_length);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17346 read_samples += frame_length * channels;
347 }
348 if (read_samples == written_samples) {
349 read_samples = 0;
350 written_samples = 0;
351 }
352 }
353
354 // Run received side of ACM.
henrik.lundind4ccb002016-05-17 19:21:55355 bool muted;
Henrik Lundin84f75692023-02-01 12:07:10356 ASSERT_EQ(0, acm_receiver_->GetAudio(out_freq_hz_b, &audio_frame, &muted));
henrik.lundind4ccb002016-05-17 19:21:55357 ASSERT_FALSE(muted);
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17358
359 // Write output speech to file.
360 out_file_.Write10MsData(
yujo36b1a5f2017-06-12 19:45:32361 audio_frame.data(),
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17362 audio_frame.samples_per_channel_ * audio_frame.num_channels_);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07363
364 // Write stand-alone speech to file.
pkasting25702cb2016-01-08 21:50:27365 out_file_standalone_.Write10MsData(out_audio, decoded_samples * channels);
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48366
minyue@webrtc.orgf563e852014-07-18 21:11:27367 if (audio_frame.timestamp_ > start_time_stamp) {
368 // Number of channels should be the same for both stand-alone and
369 // ACM-decoding.
370 EXPECT_EQ(audio_frame.num_channels_, channels);
371 }
tina.legrand@webrtc.orgba5a6c32014-03-23 09:58:48372
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07373 decoded_samples = 0;
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17374 }
375
376 if (in_file_mono_.EndOfFile()) {
377 in_file_mono_.Rewind();
378 }
379 if (in_file_stereo_.EndOfFile()) {
380 in_file_stereo_.Rewind();
381 }
382 // Reset in case we ended with a lost packet.
383 channel->set_lost_packet(false);
384}
385
386void OpusTest::OpenOutFile(int test_number) {
387 std::string file_name;
388 std::stringstream file_stream;
Yves Gerey665174f2018-06-19 13:03:05389 file_stream << webrtc::test::OutputPath() << "opustest_out_" << test_number
390 << ".pcm";
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17391 file_name = file_stream.str();
minyue@webrtc.orgf563e852014-07-18 21:11:27392 out_file_.Open(file_name, 48000, "wb");
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07393 file_stream.str("");
394 file_name = file_stream.str();
395 file_stream << webrtc::test::OutputPath() << "opusstandalone_out_"
Yves Gerey665174f2018-06-19 13:03:05396 << test_number << ".pcm";
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07397 file_name = file_stream.str();
minyue@webrtc.orgf563e852014-07-18 21:11:27398 out_file_standalone_.Open(file_name, 48000, "wb");
tina.legrand@webrtc.org73222cf2013-03-15 13:29:17399}
400
401} // namespace webrtc