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 | |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
| 14 | |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 15 | #include <memory> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/audio_coding/include/audio_coding_module.h" |
| 19 | #include "modules/audio_coding/neteq/tools/audio_sink.h" |
| 20 | #include "modules/audio_coding/neteq/tools/packet.h" |
| 21 | #include "modules/audio_coding/neteq/tools/packet_source.h" |
Fredrik Solenberg | bbf21a3 | 2018-04-12 22:44:09 +0200 | [diff] [blame] | 22 | #include "modules/include/module_common_types.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "test/gtest.h" |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | namespace test { |
| 27 | |
| 28 | namespace { |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 29 | AudioCodingModule::Config MakeAcmConfig( |
| 30 | Clock* clock, |
| 31 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory) { |
| 32 | AudioCodingModule::Config config; |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 33 | config.clock = clock; |
| 34 | config.decoder_factory = std::move(decoder_factory); |
| 35 | return config; |
| 36 | } |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 37 | } // namespace |
| 38 | |
| 39 | AcmReceiveTestOldApi::AcmReceiveTestOldApi( |
| 40 | PacketSource* packet_source, |
| 41 | AudioSink* audio_sink, |
| 42 | int output_freq_hz, |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 43 | NumOutputChannels exptected_output_channels, |
| 44 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory) |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 45 | : clock_(0), |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 46 | acm_(webrtc::AudioCodingModule::Create( |
| 47 | MakeAcmConfig(&clock_, std::move(decoder_factory)))), |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 48 | packet_source_(packet_source), |
| 49 | audio_sink_(audio_sink), |
| 50 | output_freq_hz_(output_freq_hz), |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 51 | exptected_output_channels_(exptected_output_channels) {} |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 52 | |
kwiberg | b8e56ee | 2016-08-29 06:37:33 -0700 | [diff] [blame] | 53 | AcmReceiveTestOldApi::~AcmReceiveTestOldApi() = default; |
| 54 | |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 55 | void AcmReceiveTestOldApi::RegisterDefaultCodecs() { |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame^] | 56 | acm_->SetReceiveCodecs({{103, {"ISAC", 16000, 1}}, |
| 57 | {104, {"ISAC", 32000, 1}}, |
| 58 | {107, {"L16", 8000, 1}}, |
| 59 | {108, {"L16", 16000, 1}}, |
| 60 | {109, {"L16", 32000, 1}}, |
| 61 | {111, {"L16", 8000, 2}}, |
| 62 | {112, {"L16", 16000, 2}}, |
| 63 | {113, {"L16", 32000, 2}}, |
| 64 | {0, {"PCMU", 8000, 1}}, |
| 65 | {110, {"PCMU", 8000, 2}}, |
| 66 | {8, {"PCMA", 8000, 1}}, |
| 67 | {118, {"PCMA", 8000, 2}}, |
| 68 | {102, {"ILBC", 8000, 1}}, |
| 69 | {9, {"G722", 8000, 1}}, |
| 70 | {119, {"G722", 8000, 2}}, |
| 71 | {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}, |
| 72 | {13, {"CN", 8000, 1}}, |
| 73 | {98, {"CN", 16000, 1}}, |
| 74 | {99, {"CN", 32000, 1}}}); |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame^] | 77 | // Remaps payload types from ACM's default to those used in the resource file |
| 78 | // neteq_universal_new.rtp. |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 79 | void AcmReceiveTestOldApi::RegisterNetEqTestCodecs() { |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame^] | 80 | acm_->SetReceiveCodecs({{103, {"ISAC", 16000, 1}}, |
| 81 | {104, {"ISAC", 32000, 1}}, |
| 82 | {93, {"L16", 8000, 1}}, |
| 83 | {94, {"L16", 16000, 1}}, |
| 84 | {95, {"L16", 32000, 1}}, |
| 85 | {0, {"PCMU", 8000, 1}}, |
| 86 | {8, {"PCMA", 8000, 1}}, |
| 87 | {102, {"ILBC", 8000, 1}}, |
| 88 | {9, {"G722", 8000, 1}}, |
| 89 | {120, {"OPUS", 48000, 2}}, |
| 90 | {13, {"CN", 8000, 1}}, |
| 91 | {98, {"CN", 16000, 1}}, |
| 92 | {99, {"CN", 32000, 1}}}); |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void AcmReceiveTestOldApi::Run() { |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 96 | for (std::unique_ptr<Packet> packet(packet_source_->NextPacket()); packet; |
henrik.lundin | 46ba49c | 2016-05-24 22:50:47 -0700 | [diff] [blame] | 97 | packet = packet_source_->NextPacket()) { |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 98 | // Pull audio until time to insert packet. |
| 99 | while (clock_.TimeInMilliseconds() < packet->time_ms()) { |
| 100 | AudioFrame output_frame; |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 101 | bool muted; |
| 102 | EXPECT_EQ(0, |
| 103 | acm_->PlayoutData10Ms(output_freq_hz_, &output_frame, &muted)); |
henrik.lundin | 6d8e011 | 2016-03-04 10:34:21 -0800 | [diff] [blame] | 104 | ASSERT_EQ(output_freq_hz_, output_frame.sample_rate_hz_); |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 105 | ASSERT_FALSE(muted); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 106 | const size_t samples_per_block = |
| 107 | static_cast<size_t>(output_freq_hz_ * 10 / 1000); |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 108 | EXPECT_EQ(samples_per_block, output_frame.samples_per_channel_); |
| 109 | if (exptected_output_channels_ != kArbitraryChannels) { |
| 110 | if (output_frame.speech_type_ == webrtc::AudioFrame::kPLC) { |
| 111 | // Don't check number of channels for PLC output, since each test run |
| 112 | // usually starts with a short period of mono PLC before decoding the |
| 113 | // first packet. |
| 114 | } else { |
| 115 | EXPECT_EQ(exptected_output_channels_, output_frame.num_channels_); |
| 116 | } |
| 117 | } |
| 118 | ASSERT_TRUE(audio_sink_->WriteAudioFrame(output_frame)); |
| 119 | clock_.AdvanceTimeMilliseconds(10); |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 +0000 | [diff] [blame] | 120 | AfterGetAudio(); |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Insert packet after converting from RTPHeader to WebRtcRTPHeader. |
| 124 | WebRtcRTPHeader header; |
| 125 | header.header = packet->header(); |
| 126 | header.frameType = kAudioFrameSpeech; |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 127 | EXPECT_EQ(0, |
| 128 | acm_->IncomingPacket( |
| 129 | packet->payload(), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 130 | static_cast<int32_t>(packet->payload_length_bytes()), header)) |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 131 | << "Failure when inserting packet:" << std::endl |
| 132 | << " PT = " << static_cast<int>(header.header.payloadType) << std::endl |
| 133 | << " TS = " << header.header.timestamp << std::endl |
| 134 | << " SN = " << header.header.sequenceNumber; |
| 135 | } |
| 136 | } |
| 137 | |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 +0000 | [diff] [blame] | 138 | AcmReceiveTestToggleOutputFreqOldApi::AcmReceiveTestToggleOutputFreqOldApi( |
| 139 | PacketSource* packet_source, |
| 140 | AudioSink* audio_sink, |
| 141 | int output_freq_hz_1, |
| 142 | int output_freq_hz_2, |
| 143 | int toggle_period_ms, |
| 144 | NumOutputChannels exptected_output_channels) |
| 145 | : AcmReceiveTestOldApi(packet_source, |
| 146 | audio_sink, |
| 147 | output_freq_hz_1, |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 148 | exptected_output_channels, |
| 149 | CreateBuiltinAudioDecoderFactory()), |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 +0000 | [diff] [blame] | 150 | output_freq_hz_1_(output_freq_hz_1), |
| 151 | output_freq_hz_2_(output_freq_hz_2), |
| 152 | toggle_period_ms_(toggle_period_ms), |
kwiberg | 5adaf73 | 2016-10-04 09:33:27 -0700 | [diff] [blame] | 153 | last_toggle_time_ms_(clock_.TimeInMilliseconds()) {} |
henrik.lundin@webrtc.org | 81a7893 | 2014-10-14 10:49:58 +0000 | [diff] [blame] | 154 | |
| 155 | void AcmReceiveTestToggleOutputFreqOldApi::AfterGetAudio() { |
| 156 | if (clock_.TimeInMilliseconds() >= last_toggle_time_ms_ + toggle_period_ms_) { |
| 157 | output_freq_hz_ = (output_freq_hz_ == output_freq_hz_1_) |
| 158 | ? output_freq_hz_2_ |
| 159 | : output_freq_hz_1_; |
| 160 | last_toggle_time_ms_ = clock_.TimeInMilliseconds(); |
| 161 | } |
| 162 | } |
| 163 | |
henrik.lundin@webrtc.org | 0e6e4d2 | 2014-09-23 12:05:34 +0000 | [diff] [blame] | 164 | } // namespace test |
| 165 | } // namespace webrtc |