blob: 078c991e1c6adce56d3f3b3ea2e3d14d33577d1d [file] [log] [blame]
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/acm2/acm_receive_test.h"
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000012
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000013#include <stdio.h>
14
kwiberg16c5a962016-02-15 02:27:22 -080015#include <memory>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#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 Solenbergbbf21a32018-04-12 22:44:09 +020022#include "modules/include/module_common_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "test/gtest.h"
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000024
25namespace webrtc {
26namespace test {
27
28namespace {
kwiberg5adaf732016-10-04 09:33:27 -070029AudioCodingModule::Config MakeAcmConfig(
30 Clock* clock,
31 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory) {
32 AudioCodingModule::Config config;
kwiberg5adaf732016-10-04 09:33:27 -070033 config.clock = clock;
34 config.decoder_factory = std::move(decoder_factory);
35 return config;
36}
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000037} // namespace
38
39AcmReceiveTestOldApi::AcmReceiveTestOldApi(
40 PacketSource* packet_source,
41 AudioSink* audio_sink,
42 int output_freq_hz,
kwiberg5adaf732016-10-04 09:33:27 -070043 NumOutputChannels exptected_output_channels,
44 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory)
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000045 : clock_(0),
kwiberg5adaf732016-10-04 09:33:27 -070046 acm_(webrtc::AudioCodingModule::Create(
47 MakeAcmConfig(&clock_, std::move(decoder_factory)))),
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000048 packet_source_(packet_source),
49 audio_sink_(audio_sink),
50 output_freq_hz_(output_freq_hz),
kwiberg5adaf732016-10-04 09:33:27 -070051 exptected_output_channels_(exptected_output_channels) {}
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000052
kwibergb8e56ee2016-08-29 06:37:33 -070053AcmReceiveTestOldApi::~AcmReceiveTestOldApi() = default;
54
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000055void AcmReceiveTestOldApi::RegisterDefaultCodecs() {
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010056 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.org0e6e4d22014-09-23 12:05:34 +000075}
76
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010077// Remaps payload types from ACM's default to those used in the resource file
78// neteq_universal_new.rtp.
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000079void AcmReceiveTestOldApi::RegisterNetEqTestCodecs() {
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010080 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.org0e6e4d22014-09-23 12:05:34 +000093}
94
95void AcmReceiveTestOldApi::Run() {
kwiberg16c5a962016-02-15 02:27:22 -080096 for (std::unique_ptr<Packet> packet(packet_source_->NextPacket()); packet;
henrik.lundin46ba49c2016-05-24 22:50:47 -070097 packet = packet_source_->NextPacket()) {
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000098 // Pull audio until time to insert packet.
99 while (clock_.TimeInMilliseconds() < packet->time_ms()) {
100 AudioFrame output_frame;
henrik.lundin834a6ea2016-05-13 03:45:24 -0700101 bool muted;
102 EXPECT_EQ(0,
103 acm_->PlayoutData10Ms(output_freq_hz_, &output_frame, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800104 ASSERT_EQ(output_freq_hz_, output_frame.sample_rate_hz_);
henrik.lundin834a6ea2016-05-13 03:45:24 -0700105 ASSERT_FALSE(muted);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700106 const size_t samples_per_block =
107 static_cast<size_t>(output_freq_hz_ * 10 / 1000);
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000108 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.org81a78932014-10-14 10:49:58 +0000120 AfterGetAudio();
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000121 }
122
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100123 EXPECT_EQ(0, acm_->IncomingPacket(
124 packet->payload(),
125 static_cast<int32_t>(packet->payload_length_bytes()),
126 packet->header()))
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000127 << "Failure when inserting packet:" << std::endl
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100128 << " PT = " << static_cast<int>(packet->header().payloadType)
129 << std::endl
130 << " TS = " << packet->header().timestamp << std::endl
131 << " SN = " << packet->header().sequenceNumber;
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000132 }
133}
134
henrik.lundin@webrtc.org81a78932014-10-14 10:49:58 +0000135AcmReceiveTestToggleOutputFreqOldApi::AcmReceiveTestToggleOutputFreqOldApi(
136 PacketSource* packet_source,
137 AudioSink* audio_sink,
138 int output_freq_hz_1,
139 int output_freq_hz_2,
140 int toggle_period_ms,
141 NumOutputChannels exptected_output_channels)
142 : AcmReceiveTestOldApi(packet_source,
143 audio_sink,
144 output_freq_hz_1,
kwiberg5adaf732016-10-04 09:33:27 -0700145 exptected_output_channels,
146 CreateBuiltinAudioDecoderFactory()),
henrik.lundin@webrtc.org81a78932014-10-14 10:49:58 +0000147 output_freq_hz_1_(output_freq_hz_1),
148 output_freq_hz_2_(output_freq_hz_2),
149 toggle_period_ms_(toggle_period_ms),
kwiberg5adaf732016-10-04 09:33:27 -0700150 last_toggle_time_ms_(clock_.TimeInMilliseconds()) {}
henrik.lundin@webrtc.org81a78932014-10-14 10:49:58 +0000151
152void AcmReceiveTestToggleOutputFreqOldApi::AfterGetAudio() {
153 if (clock_.TimeInMilliseconds() >= last_toggle_time_ms_ + toggle_period_ms_) {
154 output_freq_hz_ = (output_freq_hz_ == output_freq_hz_1_)
155 ? output_freq_hz_2_
156 : output_freq_hz_1_;
157 last_toggle_time_ms_ = clock_.TimeInMilliseconds();
158 }
159}
160
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000161} // namespace test
162} // namespace webrtc