tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "modules/audio_coding/test/opus_test.h" |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 12 | |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 13 | #include <string> |
| 14 | |
Karl Wiberg | 5817d3d | 2018-04-06 08:06:42 | [diff] [blame] | 15 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #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 Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 19 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "test/testsupport/file_utils.h" |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
henrik.lundin@webrtc.org | adaf809 | 2014-04-17 08:29:10 | [diff] [blame] | 24 | OpusTest::OpusTest() |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 25 | : acm_receiver_(std::make_unique<acm2::AcmReceiver>( |
| 26 | acm2::AcmReceiver::Config(CreateBuiltinAudioDecoderFactory()))), |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 27 | channel_a2b_(NULL), |
| 28 | counter_(0), |
| 29 | payload_type_(255), |
henrik.lundin@webrtc.org | adaf809 | 2014-04-17 08:29:10 | [diff] [blame] | 30 | rtp_timestamp_(0) {} |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 31 | |
| 32 | OpusTest::~OpusTest() { |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 33 | 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.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 45 | 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.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void OpusTest::Perform() { |
| 56 | #ifndef WEBRTC_CODEC_OPUS |
| 57 | // Opus isn't defined, exit. |
| 58 | return; |
| 59 | #else |
| 60 | uint16_t frequency_hz; |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 61 | size_t audio_channels; |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 62 | 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 Wiberg | 7e7c5c3 | 2019-05-21 09:50:32 | [diff] [blame] | 76 | 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.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 78 | |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 79 | // Create Opus decoders for mono and stereo for stand-alone testing of Opus. |
Karl Wiberg | a1d1a1e | 2019-05-28 12:41:07 | [diff] [blame] | 80 | ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_mono_decoder_, 1, 48000), -1); |
| 81 | ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_stereo_decoder_, 2, 48000), -1); |
Karl Wiberg | 4376648 | 2015-08-27 13:22:11 | [diff] [blame] | 82 | WebRtcOpus_DecoderInit(opus_mono_decoder_); |
| 83 | WebRtcOpus_DecoderInit(opus_stereo_decoder_); |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 84 | |
andrew@webrtc.org | 89df092 | 2013-09-12 01:27:43 | [diff] [blame] | 85 | ASSERT_TRUE(acm_receiver_.get() != NULL); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 86 | acm_receiver_->FlushBuffers(); |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 87 | |
| 88 | // Register Opus stereo as receiving codec. |
Fredrik Solenberg | 657b296 | 2018-12-05 09:30:25 | [diff] [blame] | 89 | constexpr int kOpusPayloadType = 120; |
| 90 | const SdpAudioFormat kOpusFormatStereo("opus", 48000, 2, {{"stereo", "1"}}); |
| 91 | payload_type_ = kOpusPayloadType; |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 92 | acm_receiver_->SetCodecs({{kOpusPayloadType, kOpusFormatStereo}}); |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 93 | |
| 94 | // Create and connect the channel. |
| 95 | channel_a2b_ = new TestPackStereo; |
andrew@webrtc.org | 89df092 | 2013-09-12 01:27:43 | [diff] [blame] | 96 | channel_a2b_->RegisterReceiverACM(acm_receiver_.get()); |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 97 | |
| 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.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 126 | 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.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 146 | |
| 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 Solenberg | 657b296 | 2018-12-05 09:30:25 | [diff] [blame] | 156 | const SdpAudioFormat kOpusFormatMono("opus", 48000, 2); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 157 | acm_receiver_->SetCodecs({{kOpusPayloadType, kOpusFormatMono}}); |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 158 | |
| 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.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 177 | 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.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 195 | // Close the files. |
| 196 | in_file_stereo_.Close(); |
| 197 | in_file_mono_.Close(); |
| 198 | out_file_.Close(); |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 199 | out_file_standalone_.Close(); |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 200 | #endif |
| 201 | } |
| 202 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 203 | void OpusTest::Run(TestPackStereo* channel, |
| 204 | size_t channels, |
| 205 | int bitrate, |
| 206 | size_t frame_length, |
| 207 | int percent_loss) { |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 208 | AudioFrame audio_frame; |
| 209 | int32_t out_freq_hz_b = out_file_.SamplingFrequency(); |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 210 | const size_t kBufferSizeSamples = 480 * 12 * 2; // 120 ms stereo audio. |
henrik.lundin@webrtc.org | 439a4c4 | 2014-04-24 19:05:33 | [diff] [blame] | 211 | int16_t audio[kBufferSizeSamples]; |
| 212 | int16_t out_audio[kBufferSizeSamples]; |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 213 | int16_t audio_type; |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 214 | size_t written_samples = 0; |
| 215 | size_t read_samples = 0; |
| 216 | size_t decoded_samples = 0; |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 | [diff] [blame] | 217 | bool first_packet = true; |
| 218 | uint32_t start_time_stamp = 0; |
minyue@webrtc.org | 3e42726 | 2013-11-11 22:03:52 | [diff] [blame] | 219 | |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 220 | channel->reset_payload_size(); |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 221 | counter_ = 0; |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 222 | |
| 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.org | 92c0e29 | 2014-03-24 14:38:36 | [diff] [blame] | 227 | #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 Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 232 | EXPECT_EQ(0, |
| 233 | WebRtcOpus_SetComplexity(opus_stereo_encoder_, kOpusComplexity5)); |
tina.legrand@webrtc.org | 92c0e29 | 2014-03-24 14:38:36 | [diff] [blame] | 234 | #endif |
| 235 | |
Henrik Lundin | 4d68208 | 2015-12-10 15:24:39 | [diff] [blame] | 236 | // 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.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 242 | bool lost_packet = false; |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 243 | |
| 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.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 257 | // If input audio is sampled at 32 kHz, resampling to 48 kHz is required. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 258 | 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.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 262 | written_samples += 480 * channels; |
| 263 | |
| 264 | // Sometimes we need to loop over the audio vector to produce the right |
| 265 | // number of packets. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 266 | size_t loop_encode = |
| 267 | (written_samples - read_samples) / (channels * frame_length); |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 268 | |
| 269 | if (loop_encode > 0) { |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 270 | const size_t kMaxBytes = 1000; // Maximum number of bytes for one packet. |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 271 | size_t bitstream_len_byte; |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 272 | uint8_t bitstream[kMaxBytes]; |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 273 | for (size_t i = 0; i < loop_encode; i++) { |
Peter Kasting | bba7807 | 2015-06-12 02:02:46 | [diff] [blame] | 274 | 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 Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 278 | bitstream_len_byte = static_cast<size_t>(bitstream_len_byte_int); |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 279 | |
Artem Titov | d00ce74 | 2021-07-28 18:00:17 | [diff] [blame] | 280 | // Simulate packet loss by setting `packet_loss_` to "true" in |
| 281 | // `percent_loss` percent of the loops. |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 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.org | 33ccdfa | 2014-12-04 12:14:12 | [diff] [blame] | 298 | decoded_samples += WebRtcOpus_Decode( |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 299 | opus_mono_decoder_, bitstream, bitstream_len_byte, |
| 300 | &out_audio[decoded_samples * channels], &audio_type); |
| 301 | } else { |
Minyue Li | 8e83c7a | 2019-11-04 13:47:52 | [diff] [blame] | 302 | // 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.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 315 | } |
| 316 | } else { |
| 317 | if (!lost_packet) { |
minyue@webrtc.org | 33ccdfa | 2014-12-04 12:14:12 | [diff] [blame] | 318 | decoded_samples += WebRtcOpus_Decode( |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 319 | opus_stereo_decoder_, bitstream, bitstream_len_byte, |
| 320 | &out_audio[decoded_samples * channels], &audio_type); |
| 321 | } else { |
Minyue Li | 8e83c7a | 2019-11-04 13:47:52 | [diff] [blame] | 322 | // 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.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | // Send data to the channel. "channel" will handle the loss simulation. |
Niels Möller | c936cb6 | 2019-03-19 13:10:16 | [diff] [blame] | 339 | channel->SendData(AudioFrameType::kAudioFrameSpeech, payload_type_, |
Minyue Li | ff0e4db | 2020-01-23 12:45:50 | [diff] [blame] | 340 | rtp_timestamp_, bitstream, bitstream_len_byte, 0); |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 | [diff] [blame] | 341 | if (first_packet) { |
| 342 | first_packet = false; |
| 343 | start_time_stamp = rtp_timestamp_; |
| 344 | } |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 345 | rtp_timestamp_ += static_cast<uint32_t>(frame_length); |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 346 | 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.lundin | d4ccb00 | 2016-05-17 19:21:55 | [diff] [blame] | 355 | bool muted; |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 356 | ASSERT_EQ(0, acm_receiver_->GetAudio(out_freq_hz_b, &audio_frame, &muted)); |
henrik.lundin | d4ccb00 | 2016-05-17 19:21:55 | [diff] [blame] | 357 | ASSERT_FALSE(muted); |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 358 | |
| 359 | // Write output speech to file. |
| 360 | out_file_.Write10MsData( |
yujo | 36b1a5f | 2017-06-12 19:45:32 | [diff] [blame] | 361 | audio_frame.data(), |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 362 | audio_frame.samples_per_channel_ * audio_frame.num_channels_); |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 363 | |
| 364 | // Write stand-alone speech to file. |
pkasting | 25702cb | 2016-01-08 21:50:27 | [diff] [blame] | 365 | out_file_standalone_.Write10MsData(out_audio, decoded_samples * channels); |
tina.legrand@webrtc.org | ba5a6c3 | 2014-03-23 09:58:48 | [diff] [blame] | 366 | |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 | [diff] [blame] | 367 | 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.org | ba5a6c3 | 2014-03-23 09:58:48 | [diff] [blame] | 372 | |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 373 | decoded_samples = 0; |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 374 | } |
| 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 | |
| 386 | void OpusTest::OpenOutFile(int test_number) { |
| 387 | std::string file_name; |
| 388 | std::stringstream file_stream; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 389 | file_stream << webrtc::test::OutputPath() << "opustest_out_" << test_number |
| 390 | << ".pcm"; |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 391 | file_name = file_stream.str(); |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 | [diff] [blame] | 392 | out_file_.Open(file_name, 48000, "wb"); |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 393 | file_stream.str(""); |
| 394 | file_name = file_stream.str(); |
| 395 | file_stream << webrtc::test::OutputPath() << "opusstandalone_out_" |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 396 | << test_number << ".pcm"; |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 | [diff] [blame] | 397 | file_name = file_stream.str(); |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 | [diff] [blame] | 398 | out_file_standalone_.Open(file_name, 48000, "wb"); |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | } // namespace webrtc |