blob: b12a407bea57d07db7f51a8d097840e7611b8cf8 [file] [log] [blame]
henrik.lundine8a77e32016-06-22 06:34:03 -07001/*
2 * Copyright (c) 2016 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/neteq/tools/neteq_test.h"
henrik.lundine8a77e32016-06-22 06:34:03 -070012
Ivo Creusen2db46b02018-12-14 16:49:12 +010013#include <iomanip>
henrik.lundine8a77e32016-06-22 06:34:03 -070014#include <iostream>
15
Ivo Creusen2db46b02018-12-14 16:49:12 +010016#include "modules/rtp_rtcp/source/byte_io.h"
henrik.lundine8a77e32016-06-22 06:34:03 -070017
18namespace webrtc {
19namespace test {
Ivo Creusen55de08e2018-09-03 11:49:27 +020020namespace {
21
22absl::optional<Operations> ActionToOperations(
23 absl::optional<NetEqSimulator::Action> a) {
24 if (!a) {
25 return absl::nullopt;
26 }
27 switch (*a) {
28 case NetEqSimulator::Action::kAccelerate:
29 return absl::make_optional(kAccelerate);
30 case NetEqSimulator::Action::kExpand:
31 return absl::make_optional(kExpand);
32 case NetEqSimulator::Action::kNormal:
33 return absl::make_optional(kNormal);
34 case NetEqSimulator::Action::kPreemptiveExpand:
35 return absl::make_optional(kPreemptiveExpand);
36 }
37}
38
39} // namespace
henrik.lundine8a77e32016-06-22 06:34:03 -070040
41void DefaultNetEqTestErrorCallback::OnInsertPacketError(
henrik.lundine8a77e32016-06-22 06:34:03 -070042 const NetEqInput::PacketData& packet) {
Henrik Lundinc417d9e2017-06-14 12:29:03 +020043 std::cerr << "InsertPacket returned an error." << std::endl;
henrik.lundin7a38fd22017-04-28 01:35:53 -070044 std::cerr << "Packet data: " << packet.ToString() << std::endl;
henrik.lundine8a77e32016-06-22 06:34:03 -070045 FATAL();
46}
47
Henrik Lundinc417d9e2017-06-14 12:29:03 +020048void DefaultNetEqTestErrorCallback::OnGetAudioError() {
49 std::cerr << "GetAudio returned an error." << std::endl;
henrik.lundine8a77e32016-06-22 06:34:03 -070050 FATAL();
51}
52
53NetEqTest::NetEqTest(const NetEq::Config& config,
Niels Möller3f651d82018-12-19 15:06:17 +010054 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
henrik.lundine8a77e32016-06-22 06:34:03 -070055 const DecoderMap& codecs,
Ivo Creusen2db46b02018-12-14 16:49:12 +010056 std::unique_ptr<std::ofstream> text_log,
henrik.lundine8a77e32016-06-22 06:34:03 -070057 std::unique_ptr<NetEqInput> input,
58 std::unique_ptr<AudioSink> output,
henrik.lundin02739d92017-05-04 06:09:06 -070059 Callbacks callbacks)
Niels Möller3f651d82018-12-19 15:06:17 +010060 : neteq_(NetEq::Create(config, decoder_factory)),
henrik.lundine8a77e32016-06-22 06:34:03 -070061 input_(std::move(input)),
62 output_(std::move(output)),
henrik.lundin02739d92017-05-04 06:09:06 -070063 callbacks_(callbacks),
Ivo Creusen2db46b02018-12-14 16:49:12 +010064 sample_rate_hz_(config.sample_rate_hz),
65 text_log_(std::move(text_log)) {
henrik.lundine8a77e32016-06-22 06:34:03 -070066 RTC_CHECK(!config.enable_muted_state)
67 << "The code does not handle enable_muted_state";
68 RegisterDecoders(codecs);
henrik.lundine8a77e32016-06-22 06:34:03 -070069}
70
Mirko Bonadei682aac52018-07-20 13:59:20 +020071NetEqTest::~NetEqTest() = default;
72
henrik.lundine8a77e32016-06-22 06:34:03 -070073int64_t NetEqTest::Run() {
Ivo Creusen55de08e2018-09-03 11:49:27 +020074 int64_t simulation_time = 0;
75 SimulationStepResult step_result;
76 do {
77 step_result = RunToNextGetAudio();
78 simulation_time += step_result.simulation_step_ms;
79 } while (!step_result.is_simulation_finished);
80 if (callbacks_.simulation_ended_callback) {
81 callbacks_.simulation_ended_callback->SimulationEnded(simulation_time);
82 }
83 return simulation_time;
84}
85
86NetEqTest::SimulationStepResult NetEqTest::RunToNextGetAudio() {
87 SimulationStepResult result;
henrik.lundine8a77e32016-06-22 06:34:03 -070088 const int64_t start_time_ms = *input_->NextEventTime();
89 int64_t time_now_ms = start_time_ms;
Ivo Creusen4384f532018-09-07 17:19:56 +020090 current_state_.packet_iat_ms.clear();
henrik.lundine8a77e32016-06-22 06:34:03 -070091
92 while (!input_->ended()) {
93 // Advance time to next event.
94 RTC_DCHECK(input_->NextEventTime());
95 time_now_ms = *input_->NextEventTime();
96 // Check if it is time to insert packet.
97 if (input_->NextPacketTime() && time_now_ms >= *input_->NextPacketTime()) {
98 std::unique_ptr<NetEqInput::PacketData> packet_data = input_->PopPacket();
99 RTC_CHECK(packet_data);
Minyue Li1a800182018-09-12 12:52:48 +0200100 const size_t payload_data_length =
101 packet_data->payload.size() - packet_data->header.paddingLength;
102 if (payload_data_length != 0) {
103 int error = neteq_->InsertPacket(
104 packet_data->header,
105 rtc::ArrayView<const uint8_t>(packet_data->payload),
106 static_cast<uint32_t>(packet_data->time_ms * sample_rate_hz_ /
107 1000));
108 if (error != NetEq::kOK && callbacks_.error_callback) {
109 callbacks_.error_callback->OnInsertPacketError(*packet_data);
110 }
111 if (callbacks_.post_insert_packet) {
112 callbacks_.post_insert_packet->AfterInsertPacket(*packet_data,
113 neteq_.get());
114 }
115 } else {
116 neteq_->InsertEmptyPacket(packet_data->header);
henrik.lundine8a77e32016-06-22 06:34:03 -0700117 }
Ivo Creusen4384f532018-09-07 17:19:56 +0200118 if (last_packet_time_ms_) {
119 current_state_.packet_iat_ms.push_back(time_now_ms -
120 *last_packet_time_ms_);
121 }
Ivo Creusen2db46b02018-12-14 16:49:12 +0100122 if (text_log_) {
123 const auto ops_state = neteq_->GetOperationsAndState();
124 const auto delta_wallclock =
125 last_packet_time_ms_ ? (time_now_ms - *last_packet_time_ms_) : -1;
126 const auto delta_timestamp =
127 last_packet_timestamp_
128 ? (static_cast<int64_t>(packet_data->header.timestamp) -
129 *last_packet_timestamp_) *
130 1000 / sample_rate_hz_
131 : -1;
132 const auto packet_size_bytes =
133 packet_data->payload.size() == 12
134 ? ByteReader<uint32_t>::ReadLittleEndian(
135 &packet_data->payload[8])
136 : -1;
137 *text_log_ << "Packet - wallclock: " << std::setw(5) << time_now_ms
138 << ", delta wc: " << std::setw(4) << delta_wallclock
139 << ", timestamp: " << std::setw(10)
140 << packet_data->header.timestamp
141 << ", delta ts: " << std::setw(4) << delta_timestamp
142 << ", size: " << std::setw(5) << packet_size_bytes
143 << ", frame size: " << std::setw(3)
144 << ops_state.current_frame_size_ms
145 << ", buffer size: " << std::setw(4)
146 << ops_state.current_buffer_size_ms << std::endl;
147 }
Ivo Creusen4384f532018-09-07 17:19:56 +0200148 last_packet_time_ms_ = absl::make_optional<int>(time_now_ms);
Ivo Creusen2db46b02018-12-14 16:49:12 +0100149 last_packet_timestamp_ =
150 absl::make_optional<uint32_t>(packet_data->header.timestamp);
henrik.lundine8a77e32016-06-22 06:34:03 -0700151 }
152
153 // Check if it is time to get output audio.
154 if (input_->NextOutputEventTime() &&
155 time_now_ms >= *input_->NextOutputEventTime()) {
henrik.lundin02739d92017-05-04 06:09:06 -0700156 if (callbacks_.get_audio_callback) {
157 callbacks_.get_audio_callback->BeforeGetAudio(neteq_.get());
158 }
henrik.lundine8a77e32016-06-22 06:34:03 -0700159 AudioFrame out_frame;
160 bool muted;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200161 int error = neteq_->GetAudio(&out_frame, &muted,
162 ActionToOperations(next_action_));
163 next_action_ = absl::nullopt;
henrik.lundine8a77e32016-06-22 06:34:03 -0700164 RTC_CHECK(!muted) << "The code does not handle enable_muted_state";
165 if (error != NetEq::kOK) {
henrik.lundin02739d92017-05-04 06:09:06 -0700166 if (callbacks_.error_callback) {
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200167 callbacks_.error_callback->OnGetAudioError();
henrik.lundine8a77e32016-06-22 06:34:03 -0700168 }
169 } else {
170 sample_rate_hz_ = out_frame.sample_rate_hz_;
171 }
henrik.lundin02739d92017-05-04 06:09:06 -0700172 if (callbacks_.get_audio_callback) {
173 callbacks_.get_audio_callback->AfterGetAudio(time_now_ms, out_frame,
174 muted, neteq_.get());
175 }
henrik.lundine8a77e32016-06-22 06:34:03 -0700176
177 if (output_) {
178 RTC_CHECK(output_->WriteArray(
yujo36b1a5f2017-06-12 12:45:32 -0700179 out_frame.data(),
henrik.lundine8a77e32016-06-22 06:34:03 -0700180 out_frame.samples_per_channel_ * out_frame.num_channels_));
181 }
182
183 input_->AdvanceOutputEvent();
Henrik Lundin9be77452018-09-10 12:53:27 +0200184 result.simulation_step_ms =
185 input_->NextEventTime().value_or(time_now_ms) - start_time_ms;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200186 const auto operations_state = neteq_->GetOperationsAndState();
187 current_state_.current_delay_ms = operations_state.current_buffer_size_ms;
Ivo Creusendc6d5532018-09-27 11:43:42 +0200188 current_state_.packet_size_ms = operations_state.current_frame_size_ms;
189 current_state_.next_packet_available =
190 operations_state.next_packet_available;
191 current_state_.packet_buffer_flushed =
192 operations_state.packet_buffer_flushes >
193 prev_ops_state_.packet_buffer_flushes;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200194 // TODO(ivoc): Add more accurate reporting by tracking the origin of
195 // samples in the sync buffer.
196 result.action_times_ms[Action::kExpand] = 0;
197 result.action_times_ms[Action::kAccelerate] = 0;
198 result.action_times_ms[Action::kPreemptiveExpand] = 0;
199 result.action_times_ms[Action::kNormal] = 0;
200
201 if (out_frame.speech_type_ == AudioFrame::SpeechType::kPLC ||
202 out_frame.speech_type_ == AudioFrame::SpeechType::kPLCCNG) {
203 // Consider the whole frame to be the result of expansion.
204 result.action_times_ms[Action::kExpand] = 10;
205 } else if (operations_state.accelerate_samples -
206 prev_ops_state_.accelerate_samples >
207 0) {
208 // Consider the whole frame to be the result of acceleration.
209 result.action_times_ms[Action::kAccelerate] = 10;
210 } else if (operations_state.preemptive_samples -
211 prev_ops_state_.preemptive_samples >
212 0) {
213 // Consider the whole frame to be the result of preemptive expansion.
214 result.action_times_ms[Action::kPreemptiveExpand] = 10;
215 } else {
216 // Consider the whole frame to be the result of normal playout.
217 result.action_times_ms[Action::kNormal] = 10;
218 }
Ivo Creusen2db46b02018-12-14 16:49:12 +0100219 auto lifetime_stats = LifetimeStats();
220 if (text_log_) {
221 const bool plc =
222 (out_frame.speech_type_ == AudioFrame::SpeechType::kPLC) ||
223 (out_frame.speech_type_ == AudioFrame::SpeechType::kPLCCNG);
224 const bool cng = out_frame.speech_type_ == AudioFrame::SpeechType::kCNG;
225 const bool voice_concealed =
226 lifetime_stats.voice_concealed_samples >
227 prev_lifetime_stats_.voice_concealed_samples;
228 *text_log_ << "GetAudio - wallclock: " << std::setw(5) << time_now_ms
229 << ", delta wc: " << std::setw(4)
230 << (input_->NextEventTime().value_or(time_now_ms) -
231 start_time_ms)
232 << ", CNG: " << cng << ", PLC: " << plc
233 << ", voice concealed: " << voice_concealed
234 << ", buffer size: " << std::setw(4)
235 << current_state_.current_delay_ms << std::endl;
236 if (operations_state.discarded_primary_packets >
237 prev_ops_state_.discarded_primary_packets) {
238 *text_log_ << "Discarded "
239 << (operations_state.discarded_primary_packets -
240 prev_ops_state_.discarded_primary_packets)
241 << " primary packets." << std::endl;
242 }
243 if (operations_state.packet_buffer_flushes >
244 prev_ops_state_.packet_buffer_flushes) {
245 *text_log_ << "Flushed packet buffer "
246 << (operations_state.packet_buffer_flushes -
247 prev_ops_state_.packet_buffer_flushes)
248 << " times." << std::endl;
249 }
250 }
251 prev_lifetime_stats_ = lifetime_stats;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200252 result.is_simulation_finished = input_->ended();
Ivo Creusend1c2f782018-09-13 14:39:55 +0200253 prev_ops_state_ = operations_state;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200254 return result;
henrik.lundine8a77e32016-06-22 06:34:03 -0700255 }
256 }
Henrik Lundin9be77452018-09-10 12:53:27 +0200257 result.simulation_step_ms =
258 input_->NextEventTime().value_or(time_now_ms) - start_time_ms;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200259 result.is_simulation_finished = true;
260 return result;
261}
262
263void NetEqTest::SetNextAction(NetEqTest::Action next_operation) {
264 next_action_ = absl::optional<Action>(next_operation);
265}
266
267NetEqTest::NetEqState NetEqTest::GetNetEqState() {
Ivo Creusen4384f532018-09-07 17:19:56 +0200268 return current_state_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700269}
270
271NetEqNetworkStatistics NetEqTest::SimulationStats() {
272 NetEqNetworkStatistics stats;
273 RTC_CHECK_EQ(neteq_->NetworkStatistics(&stats), 0);
274 return stats;
275}
276
Alex Narest7ff6ca52018-02-07 18:46:33 +0100277NetEqLifetimeStatistics NetEqTest::LifetimeStats() const {
278 return neteq_->GetLifetimeStatistics();
279}
280
Henrik Lundin7687ad52018-07-02 10:14:46 +0200281NetEqTest::DecoderMap NetEqTest::StandardDecoderMap() {
282 DecoderMap codecs = {
Niels Möller05543682019-01-10 16:55:06 +0100283 {0, SdpAudioFormat("pcmu", 8000, 1)},
284 {8, SdpAudioFormat("pcma", 8000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200285#ifdef WEBRTC_CODEC_ILBC
Niels Möller05543682019-01-10 16:55:06 +0100286 {102, SdpAudioFormat("ilbc", 8000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200287#endif
Niels Möller05543682019-01-10 16:55:06 +0100288 {103, SdpAudioFormat("isac", 16000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200289#if !defined(WEBRTC_ANDROID)
Niels Möller05543682019-01-10 16:55:06 +0100290 {104, SdpAudioFormat("isac", 32000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200291#endif
292#ifdef WEBRTC_CODEC_OPUS
Niels Möller05543682019-01-10 16:55:06 +0100293 {111, SdpAudioFormat("opus", 48000, 2)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200294#endif
Niels Möller05543682019-01-10 16:55:06 +0100295 {93, SdpAudioFormat("l16", 8000, 1)},
296 {94, SdpAudioFormat("l16", 16000, 1)},
297 {95, SdpAudioFormat("l16", 32000, 1)},
298 {96, SdpAudioFormat("l16", 48000, 1)},
299 {9, SdpAudioFormat("g722", 8000, 1)},
300 {106, SdpAudioFormat("telephone-event", 8000, 1)},
301 {114, SdpAudioFormat("telephone-event", 16000, 1)},
302 {115, SdpAudioFormat("telephone-event", 32000, 1)},
303 {116, SdpAudioFormat("telephone-event", 48000, 1)},
304 {117, SdpAudioFormat("red", 8000, 1)},
305 {13, SdpAudioFormat("cn", 8000, 1)},
306 {98, SdpAudioFormat("cn", 16000, 1)},
307 {99, SdpAudioFormat("cn", 32000, 1)},
308 {100, SdpAudioFormat("cn", 48000, 1)}
Henrik Lundin7687ad52018-07-02 10:14:46 +0200309 };
310 return codecs;
311}
312
henrik.lundine8a77e32016-06-22 06:34:03 -0700313void NetEqTest::RegisterDecoders(const DecoderMap& codecs) {
314 for (const auto& c : codecs) {
Niels Möller05543682019-01-10 16:55:06 +0100315 RTC_CHECK(neteq_->RegisterPayloadType(c.first, c.second))
316 << "Cannot register " << c.second.name << " to payload type "
henrik.lundine8a77e32016-06-22 06:34:03 -0700317 << c.first;
318 }
319}
320
henrik.lundine8a77e32016-06-22 06:34:03 -0700321} // namespace test
322} // namespace webrtc