henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/acm2/acm_receive_test.h" |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | #include <stdio.h> |
| 15 | |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
| 19 | #include "modules/audio_coding/codecs/audio_format_conversion.h" |
| 20 | #include "modules/audio_coding/include/audio_coding_module.h" |
| 21 | #include "modules/audio_coding/neteq/tools/audio_sink.h" |
| 22 | #include "modules/audio_coding/neteq/tools/packet.h" |
| 23 | #include "modules/audio_coding/neteq/tools/packet_source.h" |
Fredrik Solenberg | bbf21a3 | 2018-04-12 22:44:09 +0200 | [diff] [blame] | 24 | #include "modules/include/module_common_types.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "test/gtest.h" |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace test { |
| 29 | |
| 30 | namespace { |
| 31 | // Returns true if the codec should be registered, otherwise false. Changes |
| 32 | // the number of channels for the Opus codec to always be 1. |
| 33 | bool ModifyAndUseThisCodec(CodecInst* codec_param) { |
| 34 | if (STR_CASE_CMP(codec_param->plname, "CN") == 0 && |
| 35 | codec_param->plfreq == 48000) |
| 36 | return false; // Skip 48 kHz comfort noise. |
| 37 | |
| 38 | if (STR_CASE_CMP(codec_param->plname, "telephone-event") == 0) |
| 39 | return false; // Skip DTFM. |
| 40 | |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | // Remaps payload types from ACM's default to those used in the resource file |
| 45 | // neteq_universal_new.rtp. Returns true if the codec should be registered, |
| 46 | // otherwise false. The payload types are set as follows (all are mono codecs): |
| 47 | // PCMu = 0; |
| 48 | // PCMa = 8; |
| 49 | // Comfort noise 8 kHz = 13 |
| 50 | // Comfort noise 16 kHz = 98 |
| 51 | // Comfort noise 32 kHz = 99 |
| 52 | // iLBC = 102 |
| 53 | // iSAC wideband = 103 |
| 54 | // iSAC super-wideband = 104 |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 55 | // AVT/DTMF = 106 |
| 56 | // RED = 117 |
| 57 | // PCM16b 8 kHz = 93 |
| 58 | // PCM16b 16 kHz = 94 |
| 59 | // PCM16b 32 kHz = 95 |
| 60 | // G.722 = 94 |
| 61 | bool RemapPltypeAndUseThisCodec(const char* plname, |
| 62 | int plfreq, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 63 | size_t channels, |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 64 | int* pltype) { |
| 65 | if (channels != 1) |
| 66 | return false; // Don't use non-mono codecs. |
| 67 | |
| 68 | // Re-map pltypes to those used in the NetEq test files. |
| 69 | if (STR_CASE_CMP(plname, "PCMU") == 0 && plfreq == 8000) { |
| 70 | *pltype = 0; |
| 71 | } else if (STR_CASE_CMP(plname, "PCMA") == 0 && plfreq == 8000) { |
| 72 | *pltype = 8; |
| 73 | } else if (STR_CASE_CMP(plname, "CN") == 0 && plfreq == 8000) { |
| 74 | *pltype = 13; |
| 75 | } else if (STR_CASE_CMP(plname, "CN") == 0 && plfreq == 16000) { |
| 76 | *pltype = 98; |
| 77 | } else if (STR_CASE_CMP(plname, "CN") == 0 && plfreq == 32000) { |
| 78 | *pltype = 99; |
| 79 | } else if (STR_CASE_CMP(plname, "ILBC") == 0) { |
| 80 | *pltype = 102; |
| 81 | } else if (STR_CASE_CMP(plname, "ISAC") == 0 && plfreq == 16000) { |
| 82 | *pltype = 103; |
| 83 | } else if (STR_CASE_CMP(plname, "ISAC") == 0 && plfreq == 32000) { |
| 84 | *pltype = 104; |
solenberg | 2779bab | 2016-11-17 04:45:19 -0800 | [diff] [blame] | 85 | } else if (STR_CASE_CMP(plname, "telephone-event") == 0 && plfreq == 8000) { |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 86 | *pltype = 106; |
solenberg | 2779bab | 2016-11-17 04:45:19 -0800 | [diff] [blame] | 87 | } else if (STR_CASE_CMP(plname, "telephone-event") == 0 && plfreq == 16000) { |
| 88 | *pltype = 114; |
| 89 | } else if (STR_CASE_CMP(plname, "telephone-event") == 0 && plfreq == 32000) { |
| 90 | *pltype = 115; |
| 91 | } else if (STR_CASE_CMP(plname, "telephone-event") == 0 && plfreq == 48000) { |
| 92 | *pltype = 116; |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 93 | } else if (STR_CASE_CMP(plname, "red") == 0) { |
| 94 | *pltype = 117; |
| 95 | } else if (STR_CASE_CMP(plname, "L16") == 0 && plfreq == 8000) { |
| 96 | *pltype = 93; |
| 97 | } else if (STR_CASE_CMP(plname, "L16") == 0 && plfreq == 16000) { |
| 98 | *pltype = 94; |
| 99 | } else if (STR_CASE_CMP(plname, "L16") == 0 && plfreq == 32000) { |
| 100 | *pltype = 95; |
| 101 | } else if (STR_CASE_CMP(plname, "G722") == 0) { |
| 102 | *pltype = 9; |
| 103 | } else { |
| 104 | // Don't use any other codecs. |
| 105 | return false; |
| 106 | } |
| 107 | return true; |
| 108 | } |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 109 | |
| 110 | AudioCodingModule::Config MakeAcmConfig( |
| 111 | Clock* clock, |
| 112 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory) { |
| 113 | AudioCodingModule::Config config; |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 114 | config.clock = clock; |
| 115 | config.decoder_factory = std::move(decoder_factory); |
| 116 | return config; |
| 117 | } |
| 118 | |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 119 | } // namespace |
| 120 | |
| 121 | AcmReceiveTestOldApi::AcmReceiveTestOldApi( |
| 122 | PacketSource* packet_source, |
| 123 | AudioSink* audio_sink, |
| 124 | int output_freq_hz, |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 125 | NumOutputChannels exptected_output_channels, |
| 126 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory) |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 127 | : clock_(0), |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 128 | acm_(webrtc::AudioCodingModule::Create( |
| 129 | MakeAcmConfig(&clock_, std::move(decoder_factory)))), |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 130 | packet_source_(packet_source), |
| 131 | audio_sink_(audio_sink), |
| 132 | output_freq_hz_(output_freq_hz), |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 133 | exptected_output_channels_(exptected_output_channels) {} |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 134 | |
kwiberg | b8e56ee | 2016-08-29 06:37:33 -0700 | [diff] [blame] | 135 | AcmReceiveTestOldApi::~AcmReceiveTestOldApi() = default; |
| 136 | |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 137 | void AcmReceiveTestOldApi::RegisterDefaultCodecs() { |
| 138 | CodecInst my_codec_param; |
| 139 | for (int n = 0; n < acm_->NumberOfCodecs(); n++) { |
| 140 | ASSERT_EQ(0, acm_->Codec(n, &my_codec_param)) << "Failed to get codec."; |
| 141 | if (ModifyAndUseThisCodec(&my_codec_param)) { |
kwiberg | da2bf4e | 2016-10-24 13:47:09 -0700 | [diff] [blame] | 142 | ASSERT_EQ(true, |
| 143 | acm_->RegisterReceiveCodec(my_codec_param.pltype, |
| 144 | CodecInstToSdp(my_codec_param))) |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 145 | << "Couldn't register receive codec.\n"; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void AcmReceiveTestOldApi::RegisterNetEqTestCodecs() { |
| 151 | CodecInst my_codec_param; |
| 152 | for (int n = 0; n < acm_->NumberOfCodecs(); n++) { |
| 153 | ASSERT_EQ(0, acm_->Codec(n, &my_codec_param)) << "Failed to get codec."; |
| 154 | if (!ModifyAndUseThisCodec(&my_codec_param)) { |
| 155 | // Skip this codec. |
| 156 | continue; |
| 157 | } |
| 158 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 159 | if (RemapPltypeAndUseThisCodec(my_codec_param.plname, my_codec_param.plfreq, |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 160 | my_codec_param.channels, |
| 161 | &my_codec_param.pltype)) { |
kwiberg | da2bf4e | 2016-10-24 13:47:09 -0700 | [diff] [blame] | 162 | ASSERT_EQ(true, |
| 163 | acm_->RegisterReceiveCodec(my_codec_param.pltype, |
| 164 | CodecInstToSdp(my_codec_param))) |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 165 | << "Couldn't register receive codec.\n"; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void AcmReceiveTestOldApi::Run() { |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 171 | for (std::unique_ptr<Packet> packet(packet_source_->NextPacket()); packet; |
henrik.lundin | 46ba49c | 2016-05-24 22:50:47 -0700 | [diff] [blame] | 172 | packet = packet_source_->NextPacket()) { |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 173 | // Pull audio until time to insert packet. |
| 174 | while (clock_.TimeInMilliseconds() < packet->time_ms()) { |
| 175 | AudioFrame output_frame; |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 176 | bool muted; |
| 177 | EXPECT_EQ(0, |
| 178 | acm_->PlayoutData10Ms(output_freq_hz_, &output_frame, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 179 | ASSERT_EQ(output_freq_hz_, output_frame.sample_rate_hz_); |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 180 | ASSERT_FALSE(muted); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 181 | const size_t samples_per_block = |
| 182 | static_cast<size_t>(output_freq_hz_ * 10 / 1000); |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 183 | EXPECT_EQ(samples_per_block, output_frame.samples_per_channel_); |
| 184 | if (exptected_output_channels_ != kArbitraryChannels) { |
| 185 | if (output_frame.speech_type_ == webrtc::AudioFrame::kPLC) { |
| 186 | // Don't check number of channels for PLC output, since each test run |
| 187 | // usually starts with a short period of mono PLC before decoding the |
| 188 | // first packet. |
| 189 | } else { |
| 190 | EXPECT_EQ(exptected_output_channels_, output_frame.num_channels_); |
| 191 | } |
| 192 | } |
| 193 | ASSERT_TRUE(audio_sink_->WriteAudioFrame(output_frame)); |
| 194 | clock_.AdvanceTimeMilliseconds(10); |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 +0000 | [diff] [blame] | 195 | AfterGetAudio(); |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // Insert packet after converting from RTPHeader to WebRtcRTPHeader. |
| 199 | WebRtcRTPHeader header; |
| 200 | header.header = packet->header(); |
| 201 | header.frameType = kAudioFrameSpeech; |
| 202 | memset(&header.type.Audio, 0, sizeof(RTPAudioHeader)); |
| 203 | EXPECT_EQ(0, |
| 204 | acm_->IncomingPacket( |
| 205 | packet->payload(), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 206 | static_cast<int32_t>(packet->payload_length_bytes()), header)) |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 207 | << "Failure when inserting packet:" << std::endl |
| 208 | << " PT = " << static_cast<int>(header.header.payloadType) << std::endl |
| 209 | << " TS = " << header.header.timestamp << std::endl |
| 210 | << " SN = " << header.header.sequenceNumber; |
| 211 | } |
| 212 | } |
| 213 | |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 +0000 | [diff] [blame] | 214 | AcmReceiveTestToggleOutputFreqOldApi::AcmReceiveTestToggleOutputFreqOldApi( |
| 215 | PacketSource* packet_source, |
| 216 | AudioSink* audio_sink, |
| 217 | int output_freq_hz_1, |
| 218 | int output_freq_hz_2, |
| 219 | int toggle_period_ms, |
| 220 | NumOutputChannels exptected_output_channels) |
| 221 | : AcmReceiveTestOldApi(packet_source, |
| 222 | audio_sink, |
| 223 | output_freq_hz_1, |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 224 | exptected_output_channels, |
| 225 | CreateBuiltinAudioDecoderFactory()), |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 +0000 | [diff] [blame] | 226 | output_freq_hz_1_(output_freq_hz_1), |
| 227 | output_freq_hz_2_(output_freq_hz_2), |
| 228 | toggle_period_ms_(toggle_period_ms), |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 229 | last_toggle_time_ms_(clock_.TimeInMilliseconds()) {} |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 +0000 | [diff] [blame] | 230 | |
| 231 | void AcmReceiveTestToggleOutputFreqOldApi::AfterGetAudio() { |
| 232 | if (clock_.TimeInMilliseconds() >= last_toggle_time_ms_ + toggle_period_ms_) { |
| 233 | output_freq_hz_ = (output_freq_hz_ == output_freq_hz_1_) |
| 234 | ? output_freq_hz_2_ |
| 235 | : output_freq_hz_1_; |
| 236 | last_toggle_time_ms_ = clock_.TimeInMilliseconds(); |
| 237 | } |
| 238 | } |
| 239 | |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 240 | } // namespace test |
| 241 | } // namespace webrtc |