acm_receive_test.cc (6325B)
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 11 #include "modules/audio_coding/acm2/acm_receive_test.h" 12 13 #include <cstdio> 14 #include <memory> 15 #include <ostream> 16 #include <utility> 17 18 #include "api/audio_codecs/audio_decoder_factory.h" 19 #include "api/audio_codecs/builtin_audio_decoder_factory.h" 20 #include "api/environment/environment_factory.h" 21 #include "api/neteq/default_neteq_factory.h" 22 #include "api/neteq/neteq.h" 23 #include "api/scoped_refptr.h" 24 #include "modules/audio_coding/include/audio_coding_module.h" 25 #include "modules/audio_coding/neteq/tools/audio_sink.h" 26 #include "modules/audio_coding/neteq/tools/packet_source.h" 27 #include "modules/rtp_rtcp/source/rtp_packet_received.h" 28 #include "test/gtest.h" 29 30 namespace webrtc { 31 namespace test { 32 33 AcmReceiveTestOldApi::AcmReceiveTestOldApi( 34 PacketSource* packet_source, 35 AudioSink* audio_sink, 36 int output_freq_hz, 37 NumOutputChannels exptected_output_channels, 38 scoped_refptr<AudioDecoderFactory> decoder_factory) 39 : clock_(0), 40 neteq_(DefaultNetEqFactory().Create(CreateEnvironment(&clock_), 41 NetEq::Config(), 42 std::move(decoder_factory))), 43 packet_source_(packet_source), 44 audio_sink_(audio_sink), 45 output_freq_hz_(output_freq_hz), 46 exptected_output_channels_(exptected_output_channels) {} 47 48 AcmReceiveTestOldApi::~AcmReceiveTestOldApi() = default; 49 50 void AcmReceiveTestOldApi::RegisterDefaultCodecs() { 51 neteq_->SetCodecs({{103, {"ISAC", 16000, 1}}, 52 {104, {"ISAC", 32000, 1}}, 53 {107, {"L16", 8000, 1}}, 54 {108, {"L16", 16000, 1}}, 55 {109, {"L16", 32000, 1}}, 56 {111, {"L16", 8000, 2}}, 57 {112, {"L16", 16000, 2}}, 58 {113, {"L16", 32000, 2}}, 59 {0, {"PCMU", 8000, 1}}, 60 {110, {"PCMU", 8000, 2}}, 61 {8, {"PCMA", 8000, 1}}, 62 {118, {"PCMA", 8000, 2}}, 63 {9, {"G722", 8000, 1}}, 64 {119, {"G722", 8000, 2}}, 65 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}, 66 {13, {"CN", 8000, 1}}, 67 {98, {"CN", 16000, 1}}, 68 {99, {"CN", 32000, 1}}}); 69 } 70 71 // Remaps payload types from ACM's default to those used in the resource file 72 // neteq_universal_new.rtp. 73 void AcmReceiveTestOldApi::RegisterNetEqTestCodecs() { 74 neteq_->SetCodecs({{103, {"ISAC", 16000, 1}}, 75 {104, {"ISAC", 32000, 1}}, 76 {93, {"L16", 8000, 1}}, 77 {94, {"L16", 16000, 1}}, 78 {95, {"L16", 32000, 1}}, 79 {0, {"PCMU", 8000, 1}}, 80 {8, {"PCMA", 8000, 1}}, 81 {102, {"ILBC", 8000, 1}}, 82 {9, {"G722", 8000, 1}}, 83 {120, {"OPUS", 48000, 2}}, 84 {13, {"CN", 8000, 1}}, 85 {98, {"CN", 16000, 1}}, 86 {99, {"CN", 32000, 1}}}); 87 } 88 89 void AcmReceiveTestOldApi::Run() { 90 for (std::unique_ptr<RtpPacketReceived> packet = packet_source_->NextPacket(); 91 packet != nullptr; packet = packet_source_->NextPacket()) { 92 // Pull audio until time to insert packet. 93 while (clock_.CurrentTime() < packet->arrival_time()) { 94 AudioFrame output_frame; 95 bool muted; 96 EXPECT_EQ(NetEq::kOK, neteq_->GetAudio(&output_frame, &muted)); 97 EXPECT_TRUE( 98 resampler_helper_.MaybeResample(output_freq_hz_, &output_frame)); 99 ASSERT_EQ(output_freq_hz_, output_frame.sample_rate_hz_); 100 ASSERT_FALSE(muted); 101 const size_t samples_per_block = 102 static_cast<size_t>(output_freq_hz_ * 10 / 1000); 103 EXPECT_EQ(samples_per_block, output_frame.samples_per_channel_); 104 if (exptected_output_channels_ != kArbitraryChannels) { 105 if (output_frame.speech_type_ == AudioFrame::kPLC) { 106 // Don't check number of channels for PLC output, since each test run 107 // usually starts with a short period of mono PLC before decoding the 108 // first packet. 109 } else { 110 EXPECT_EQ(exptected_output_channels_, output_frame.num_channels_); 111 } 112 } 113 ASSERT_TRUE(audio_sink_->WriteAudioFrame(output_frame)); 114 clock_.AdvanceTimeMilliseconds(10); 115 AfterGetAudio(); 116 } 117 118 RTPHeader rtp_header; 119 packet->GetHeader(&rtp_header); 120 EXPECT_EQ(0, neteq_->InsertPacket(rtp_header, packet->payload(), 121 clock_.CurrentTime())) 122 << "Failure when inserting packet:" << std::endl 123 << " PT = " << static_cast<int>(packet->PayloadType()) << std::endl 124 << " TS = " << packet->Timestamp() << std::endl 125 << " SN = " << packet->SequenceNumber(); 126 } 127 } 128 129 AcmReceiveTestToggleOutputFreqOldApi::AcmReceiveTestToggleOutputFreqOldApi( 130 PacketSource* packet_source, 131 AudioSink* audio_sink, 132 int output_freq_hz_1, 133 int output_freq_hz_2, 134 int toggle_period_ms, 135 NumOutputChannels exptected_output_channels) 136 : AcmReceiveTestOldApi(packet_source, 137 audio_sink, 138 output_freq_hz_1, 139 exptected_output_channels, 140 CreateBuiltinAudioDecoderFactory()), 141 output_freq_hz_1_(output_freq_hz_1), 142 output_freq_hz_2_(output_freq_hz_2), 143 toggle_period_ms_(toggle_period_ms), 144 last_toggle_time_ms_(clock_.TimeInMilliseconds()) {} 145 146 void AcmReceiveTestToggleOutputFreqOldApi::AfterGetAudio() { 147 if (clock_.TimeInMilliseconds() >= last_toggle_time_ms_ + toggle_period_ms_) { 148 output_freq_hz_ = (output_freq_hz_ == output_freq_hz_1_) 149 ? output_freq_hz_2_ 150 : output_freq_hz_1_; 151 last_toggle_time_ms_ = clock_.TimeInMilliseconds(); 152 } 153 } 154 155 } // namespace test 156 } // namespace webrtc