blob: 66f6255b018aa59967f7bdc15506954847626c57 [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"
22#include "test/gtest.h"
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000023
24namespace webrtc {
25namespace test {
26
27namespace {
Henrik Lundin84f75692023-02-01 12:07:10 +000028acm2::AcmReceiver::Config MakeAcmConfig(
29 Clock& clock,
kwiberg5adaf732016-10-04 09:33:27 -070030 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory) {
Henrik Lundin84f75692023-02-01 12:07:10 +000031 acm2::AcmReceiver::Config config;
kwiberg5adaf732016-10-04 09:33:27 -070032 config.clock = clock;
33 config.decoder_factory = std::move(decoder_factory);
34 return config;
35}
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000036} // namespace
37
38AcmReceiveTestOldApi::AcmReceiveTestOldApi(
39 PacketSource* packet_source,
40 AudioSink* audio_sink,
41 int output_freq_hz,
kwiberg5adaf732016-10-04 09:33:27 -070042 NumOutputChannels exptected_output_channels,
43 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory)
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000044 : clock_(0),
Henrik Lundin84f75692023-02-01 12:07:10 +000045 acm_receiver_(std::make_unique<acm2::AcmReceiver>(
46 MakeAcmConfig(clock_, std::move(decoder_factory)))),
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000047 packet_source_(packet_source),
48 audio_sink_(audio_sink),
49 output_freq_hz_(output_freq_hz),
kwiberg5adaf732016-10-04 09:33:27 -070050 exptected_output_channels_(exptected_output_channels) {}
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000051
kwibergb8e56ee2016-08-29 06:37:33 -070052AcmReceiveTestOldApi::~AcmReceiveTestOldApi() = default;
53
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000054void AcmReceiveTestOldApi::RegisterDefaultCodecs() {
Henrik Lundin84f75692023-02-01 12:07:10 +000055 acm_receiver_->SetCodecs({{103, {"ISAC", 16000, 1}},
56 {104, {"ISAC", 32000, 1}},
57 {107, {"L16", 8000, 1}},
58 {108, {"L16", 16000, 1}},
59 {109, {"L16", 32000, 1}},
60 {111, {"L16", 8000, 2}},
61 {112, {"L16", 16000, 2}},
62 {113, {"L16", 32000, 2}},
63 {0, {"PCMU", 8000, 1}},
64 {110, {"PCMU", 8000, 2}},
65 {8, {"PCMA", 8000, 1}},
66 {118, {"PCMA", 8000, 2}},
67 {102, {"ILBC", 8000, 1}},
68 {9, {"G722", 8000, 1}},
69 {119, {"G722", 8000, 2}},
70 {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}},
71 {13, {"CN", 8000, 1}},
72 {98, {"CN", 16000, 1}},
73 {99, {"CN", 32000, 1}}});
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000074}
75
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010076// Remaps payload types from ACM's default to those used in the resource file
77// neteq_universal_new.rtp.
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000078void AcmReceiveTestOldApi::RegisterNetEqTestCodecs() {
Henrik Lundin84f75692023-02-01 12:07:10 +000079 acm_receiver_->SetCodecs({{103, {"ISAC", 16000, 1}},
80 {104, {"ISAC", 32000, 1}},
81 {93, {"L16", 8000, 1}},
82 {94, {"L16", 16000, 1}},
83 {95, {"L16", 32000, 1}},
84 {0, {"PCMU", 8000, 1}},
85 {8, {"PCMA", 8000, 1}},
86 {102, {"ILBC", 8000, 1}},
87 {9, {"G722", 8000, 1}},
88 {120, {"OPUS", 48000, 2}},
89 {13, {"CN", 8000, 1}},
90 {98, {"CN", 16000, 1}},
91 {99, {"CN", 32000, 1}}});
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000092}
93
94void AcmReceiveTestOldApi::Run() {
kwiberg16c5a962016-02-15 02:27:22 -080095 for (std::unique_ptr<Packet> packet(packet_source_->NextPacket()); packet;
henrik.lundin46ba49c2016-05-24 22:50:47 -070096 packet = packet_source_->NextPacket()) {
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000097 // Pull audio until time to insert packet.
98 while (clock_.TimeInMilliseconds() < packet->time_ms()) {
99 AudioFrame output_frame;
henrik.lundin834a6ea2016-05-13 03:45:24 -0700100 bool muted;
Henrik Lundin84f75692023-02-01 12:07:10 +0000101 EXPECT_EQ(
102 0, acm_receiver_->GetAudio(output_freq_hz_, &output_frame, &muted));
henrik.lundin6d8e0112016-03-04 10:34:21 -0800103 ASSERT_EQ(output_freq_hz_, output_frame.sample_rate_hz_);
henrik.lundin834a6ea2016-05-13 03:45:24 -0700104 ASSERT_FALSE(muted);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700105 const size_t samples_per_block =
106 static_cast<size_t>(output_freq_hz_ * 10 / 1000);
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000107 EXPECT_EQ(samples_per_block, output_frame.samples_per_channel_);
108 if (exptected_output_channels_ != kArbitraryChannels) {
109 if (output_frame.speech_type_ == webrtc::AudioFrame::kPLC) {
110 // Don't check number of channels for PLC output, since each test run
111 // usually starts with a short period of mono PLC before decoding the
112 // first packet.
113 } else {
114 EXPECT_EQ(exptected_output_channels_, output_frame.num_channels_);
115 }
116 }
117 ASSERT_TRUE(audio_sink_->WriteAudioFrame(output_frame));
118 clock_.AdvanceTimeMilliseconds(10);
henrik.lundin@webrtc.org81a78932014-10-14 10:49:58 +0000119 AfterGetAudio();
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000120 }
121
Henrik Lundin84f75692023-02-01 12:07:10 +0000122 EXPECT_EQ(0, acm_receiver_->InsertPacket(
123 packet->header(),
124 rtc::ArrayView<const uint8_t>(
125 packet->payload(), packet->payload_length_bytes())))
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000126 << "Failure when inserting packet:" << std::endl
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100127 << " PT = " << static_cast<int>(packet->header().payloadType)
128 << std::endl
129 << " TS = " << packet->header().timestamp << std::endl
130 << " SN = " << packet->header().sequenceNumber;
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000131 }
132}
133
henrik.lundin@webrtc.org81a78932014-10-14 10:49:58 +0000134AcmReceiveTestToggleOutputFreqOldApi::AcmReceiveTestToggleOutputFreqOldApi(
135 PacketSource* packet_source,
136 AudioSink* audio_sink,
137 int output_freq_hz_1,
138 int output_freq_hz_2,
139 int toggle_period_ms,
140 NumOutputChannels exptected_output_channels)
141 : AcmReceiveTestOldApi(packet_source,
142 audio_sink,
143 output_freq_hz_1,
kwiberg5adaf732016-10-04 09:33:27 -0700144 exptected_output_channels,
145 CreateBuiltinAudioDecoderFactory()),
henrik.lundin@webrtc.org81a78932014-10-14 10:49:58 +0000146 output_freq_hz_1_(output_freq_hz_1),
147 output_freq_hz_2_(output_freq_hz_2),
148 toggle_period_ms_(toggle_period_ms),
kwiberg5adaf732016-10-04 09:33:27 -0700149 last_toggle_time_ms_(clock_.TimeInMilliseconds()) {}
henrik.lundin@webrtc.org81a78932014-10-14 10:49:58 +0000150
151void AcmReceiveTestToggleOutputFreqOldApi::AfterGetAudio() {
152 if (clock_.TimeInMilliseconds() >= last_toggle_time_ms_ + toggle_period_ms_) {
153 output_freq_hz_ = (output_freq_hz_ == output_freq_hz_1_)
154 ? output_freq_hz_2_
155 : output_freq_hz_1_;
156 last_toggle_time_ms_ = clock_.TimeInMilliseconds();
157 }
158}
159
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000160} // namespace test
161} // namespace webrtc