2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/audio_coding/main/acm2/acm_receive_test_oldapi.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h"
18 #include "webrtc/modules/audio_coding/neteq/tools/audio_sink.h"
19 #include "webrtc/modules/audio_coding/neteq/tools/packet.h"
20 #include "webrtc/modules/audio_coding/neteq/tools/packet_source.h"
26 // Returns true if the codec should be registered, otherwise false. Changes
27 // the number of channels for the Opus codec to always be 1.
28 bool ModifyAndUseThisCodec(CodecInst* codec_param) {
29 if (STR_CASE_CMP(codec_param->plname, "CN") == 0 &&
30 codec_param->plfreq == 48000)
31 return false; // Skip 48 kHz comfort noise.
33 if (STR_CASE_CMP(codec_param->plname, "telephone-event") == 0)
34 return false; // Skip DTFM.
39 // Remaps payload types from ACM's default to those used in the resource file
40 // neteq_universal_new.rtp. Returns true if the codec should be registered,
41 // otherwise false. The payload types are set as follows (all are mono codecs):
44 // Comfort noise 8 kHz = 13
45 // Comfort noise 16 kHz = 98
46 // Comfort noise 32 kHz = 99
48 // iSAC wideband = 103
49 // iSAC super-wideband = 104
50 // iSAC fullband = 124
57 bool RemapPltypeAndUseThisCodec(const char* plname,
62 return false; // Don't use non-mono codecs.
64 // Re-map pltypes to those used in the NetEq test files.
65 if (STR_CASE_CMP(plname, "PCMU") == 0 && plfreq == 8000) {
67 } else if (STR_CASE_CMP(plname, "PCMA") == 0 && plfreq == 8000) {
69 } else if (STR_CASE_CMP(plname, "CN") == 0 && plfreq == 8000) {
71 } else if (STR_CASE_CMP(plname, "CN") == 0 && plfreq == 16000) {
73 } else if (STR_CASE_CMP(plname, "CN") == 0 && plfreq == 32000) {
75 } else if (STR_CASE_CMP(plname, "ILBC") == 0) {
77 } else if (STR_CASE_CMP(plname, "ISAC") == 0 && plfreq == 16000) {
79 } else if (STR_CASE_CMP(plname, "ISAC") == 0 && plfreq == 32000) {
81 } else if (STR_CASE_CMP(plname, "ISAC") == 0 && plfreq == 48000) {
83 } else if (STR_CASE_CMP(plname, "telephone-event") == 0) {
85 } else if (STR_CASE_CMP(plname, "red") == 0) {
87 } else if (STR_CASE_CMP(plname, "L16") == 0 && plfreq == 8000) {
89 } else if (STR_CASE_CMP(plname, "L16") == 0 && plfreq == 16000) {
91 } else if (STR_CASE_CMP(plname, "L16") == 0 && plfreq == 32000) {
93 } else if (STR_CASE_CMP(plname, "G722") == 0) {
96 // Don't use any other codecs.
103 AcmReceiveTestOldApi::AcmReceiveTestOldApi(
104 PacketSource* packet_source,
105 AudioSink* audio_sink,
107 NumOutputChannels exptected_output_channels)
109 acm_(webrtc::AudioCodingModule::Create(0, &clock_)),
110 packet_source_(packet_source),
111 audio_sink_(audio_sink),
112 output_freq_hz_(output_freq_hz),
113 exptected_output_channels_(exptected_output_channels) {
116 void AcmReceiveTestOldApi::RegisterDefaultCodecs() {
117 CodecInst my_codec_param;
118 for (int n = 0; n < acm_->NumberOfCodecs(); n++) {
119 ASSERT_EQ(0, acm_->Codec(n, &my_codec_param)) << "Failed to get codec.";
120 if (ModifyAndUseThisCodec(&my_codec_param)) {
121 ASSERT_EQ(0, acm_->RegisterReceiveCodec(my_codec_param))
122 << "Couldn't register receive codec.\n";
127 void AcmReceiveTestOldApi::RegisterNetEqTestCodecs() {
128 CodecInst my_codec_param;
129 for (int n = 0; n < acm_->NumberOfCodecs(); n++) {
130 ASSERT_EQ(0, acm_->Codec(n, &my_codec_param)) << "Failed to get codec.";
131 if (!ModifyAndUseThisCodec(&my_codec_param)) {
136 if (RemapPltypeAndUseThisCodec(my_codec_param.plname,
137 my_codec_param.plfreq,
138 my_codec_param.channels,
139 &my_codec_param.pltype)) {
140 ASSERT_EQ(0, acm_->RegisterReceiveCodec(my_codec_param))
141 << "Couldn't register receive codec.\n";
146 void AcmReceiveTestOldApi::Run() {
147 for (scoped_ptr<Packet> packet(packet_source_->NextPacket()); packet;
148 packet.reset(packet_source_->NextPacket())) {
149 // Pull audio until time to insert packet.
150 while (clock_.TimeInMilliseconds() < packet->time_ms()) {
151 AudioFrame output_frame;
152 EXPECT_EQ(0, acm_->PlayoutData10Ms(output_freq_hz_, &output_frame));
153 EXPECT_EQ(output_freq_hz_, output_frame.sample_rate_hz_);
154 const int samples_per_block = output_freq_hz_ * 10 / 1000;
155 EXPECT_EQ(samples_per_block, output_frame.samples_per_channel_);
156 if (exptected_output_channels_ != kArbitraryChannels) {
157 if (output_frame.speech_type_ == webrtc::AudioFrame::kPLC) {
158 // Don't check number of channels for PLC output, since each test run
159 // usually starts with a short period of mono PLC before decoding the
162 EXPECT_EQ(exptected_output_channels_, output_frame.num_channels_);
165 ASSERT_TRUE(audio_sink_->WriteAudioFrame(output_frame));
166 clock_.AdvanceTimeMilliseconds(10);
169 // Insert packet after converting from RTPHeader to WebRtcRTPHeader.
170 WebRtcRTPHeader header;
171 header.header = packet->header();
172 header.frameType = kAudioFrameSpeech;
173 memset(&header.type.Audio, 0, sizeof(RTPAudioHeader));
175 acm_->IncomingPacket(
177 static_cast<int32_t>(packet->payload_length_bytes()),
179 << "Failure when inserting packet:" << std::endl
180 << " PT = " << static_cast<int>(header.header.payloadType) << std::endl
181 << " TS = " << header.header.timestamp << std::endl
182 << " SN = " << header.header.sequenceNumber;
187 } // namespace webrtc