blob: 19b1df11a18650a7a4c841d159422ecd511f59a3 [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 Creusen39cf3c72019-11-28 14:07:14 +010016#include "modules/audio_coding/neteq/default_neteq_factory.h"
Ivo Creusen2db46b02018-12-14 16:49:12 +010017#include "modules/rtp_rtcp/source/byte_io.h"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000018#include "system_wrappers/include/clock.h"
henrik.lundine8a77e32016-06-22 06:34:03 -070019
20namespace webrtc {
21namespace test {
Ivo Creusen55de08e2018-09-03 11:49:27 +020022namespace {
23
Ivo Creusen3ce44a32019-10-31 14:38:11 +010024absl::optional<NetEq::Operation> ActionToOperations(
Ivo Creusen55de08e2018-09-03 11:49:27 +020025 absl::optional<NetEqSimulator::Action> a) {
26 if (!a) {
27 return absl::nullopt;
28 }
29 switch (*a) {
30 case NetEqSimulator::Action::kAccelerate:
Ivo Creusen3ce44a32019-10-31 14:38:11 +010031 return absl::make_optional(NetEq::Operation::kAccelerate);
Ivo Creusen55de08e2018-09-03 11:49:27 +020032 case NetEqSimulator::Action::kExpand:
Ivo Creusen3ce44a32019-10-31 14:38:11 +010033 return absl::make_optional(NetEq::Operation::kExpand);
Ivo Creusen55de08e2018-09-03 11:49:27 +020034 case NetEqSimulator::Action::kNormal:
Ivo Creusen3ce44a32019-10-31 14:38:11 +010035 return absl::make_optional(NetEq::Operation::kNormal);
Ivo Creusen55de08e2018-09-03 11:49:27 +020036 case NetEqSimulator::Action::kPreemptiveExpand:
Ivo Creusen3ce44a32019-10-31 14:38:11 +010037 return absl::make_optional(NetEq::Operation::kPreemptiveExpand);
Ivo Creusen55de08e2018-09-03 11:49:27 +020038 }
39}
40
Ivo Creusen3ce44a32019-10-31 14:38:11 +010041std::unique_ptr<NetEq> CreateNetEq(
42 const NetEq::Config& config,
43 Clock* clock,
44 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
Ivo Creusen39cf3c72019-11-28 14:07:14 +010045 return DefaultNetEqFactory().CreateNetEq(config, decoder_factory, clock);
Ivo Creusen3ce44a32019-10-31 14:38:11 +010046}
47
Ivo Creusen55de08e2018-09-03 11:49:27 +020048} // namespace
henrik.lundine8a77e32016-06-22 06:34:03 -070049
50void DefaultNetEqTestErrorCallback::OnInsertPacketError(
henrik.lundine8a77e32016-06-22 06:34:03 -070051 const NetEqInput::PacketData& packet) {
Henrik Lundinc417d9e2017-06-14 12:29:03 +020052 std::cerr << "InsertPacket returned an error." << std::endl;
henrik.lundin7a38fd22017-04-28 01:35:53 -070053 std::cerr << "Packet data: " << packet.ToString() << std::endl;
Mirko Bonadei01719fb2020-11-17 14:50:54 +010054 RTC_FATAL();
henrik.lundine8a77e32016-06-22 06:34:03 -070055}
56
Henrik Lundinc417d9e2017-06-14 12:29:03 +020057void DefaultNetEqTestErrorCallback::OnGetAudioError() {
58 std::cerr << "GetAudio returned an error." << std::endl;
Mirko Bonadei01719fb2020-11-17 14:50:54 +010059 RTC_FATAL();
henrik.lundine8a77e32016-06-22 06:34:03 -070060}
61
62NetEqTest::NetEqTest(const NetEq::Config& config,
Niels Möller3f651d82018-12-19 15:06:17 +010063 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
henrik.lundine8a77e32016-06-22 06:34:03 -070064 const DecoderMap& codecs,
Ivo Creusen2db46b02018-12-14 16:49:12 +010065 std::unique_ptr<std::ofstream> text_log,
Ivo Creusencee751a2020-01-16 17:17:09 +010066 NetEqFactory* neteq_factory,
henrik.lundine8a77e32016-06-22 06:34:03 -070067 std::unique_ptr<NetEqInput> input,
68 std::unique_ptr<AudioSink> output,
henrik.lundin02739d92017-05-04 06:09:06 -070069 Callbacks callbacks)
Alessio Bazzica8f319a32019-07-24 16:47:02 +000070 : clock_(0),
Ivo Creusencee751a2020-01-16 17:17:09 +010071 neteq_(neteq_factory
72 ? neteq_factory->CreateNetEq(config, decoder_factory, &clock_)
73 : CreateNetEq(config, &clock_, decoder_factory)),
henrik.lundine8a77e32016-06-22 06:34:03 -070074 input_(std::move(input)),
75 output_(std::move(output)),
henrik.lundin02739d92017-05-04 06:09:06 -070076 callbacks_(callbacks),
Ivo Creusen2db46b02018-12-14 16:49:12 +010077 sample_rate_hz_(config.sample_rate_hz),
78 text_log_(std::move(text_log)) {
henrik.lundine8a77e32016-06-22 06:34:03 -070079 RTC_CHECK(!config.enable_muted_state)
80 << "The code does not handle enable_muted_state";
81 RegisterDecoders(codecs);
henrik.lundine8a77e32016-06-22 06:34:03 -070082}
83
Mirko Bonadei682aac52018-07-20 13:59:20 +020084NetEqTest::~NetEqTest() = default;
85
henrik.lundine8a77e32016-06-22 06:34:03 -070086int64_t NetEqTest::Run() {
Ivo Creusen55de08e2018-09-03 11:49:27 +020087 int64_t simulation_time = 0;
88 SimulationStepResult step_result;
89 do {
90 step_result = RunToNextGetAudio();
91 simulation_time += step_result.simulation_step_ms;
92 } while (!step_result.is_simulation_finished);
93 if (callbacks_.simulation_ended_callback) {
Jakob Ivarsson4a97d722021-11-08 17:22:51 +010094 callbacks_.simulation_ended_callback->SimulationEnded(simulation_time);
Ivo Creusen55de08e2018-09-03 11:49:27 +020095 }
96 return simulation_time;
97}
98
99NetEqTest::SimulationStepResult NetEqTest::RunToNextGetAudio() {
100 SimulationStepResult result;
henrik.lundine8a77e32016-06-22 06:34:03 -0700101 const int64_t start_time_ms = *input_->NextEventTime();
102 int64_t time_now_ms = start_time_ms;
Ivo Creusen4384f532018-09-07 17:19:56 +0200103 current_state_.packet_iat_ms.clear();
henrik.lundine8a77e32016-06-22 06:34:03 -0700104
105 while (!input_->ended()) {
106 // Advance time to next event.
107 RTC_DCHECK(input_->NextEventTime());
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000108 clock_.AdvanceTimeMilliseconds(*input_->NextEventTime() - time_now_ms);
henrik.lundine8a77e32016-06-22 06:34:03 -0700109 time_now_ms = *input_->NextEventTime();
110 // Check if it is time to insert packet.
111 if (input_->NextPacketTime() && time_now_ms >= *input_->NextPacketTime()) {
112 std::unique_ptr<NetEqInput::PacketData> packet_data = input_->PopPacket();
113 RTC_CHECK(packet_data);
Minyue Li1a800182018-09-12 12:52:48 +0200114 const size_t payload_data_length =
115 packet_data->payload.size() - packet_data->header.paddingLength;
116 if (payload_data_length != 0) {
117 int error = neteq_->InsertPacket(
118 packet_data->header,
Karl Wiberg45eb1352019-10-10 14:23:00 +0200119 rtc::ArrayView<const uint8_t>(packet_data->payload));
Minyue Li1a800182018-09-12 12:52:48 +0200120 if (error != NetEq::kOK && callbacks_.error_callback) {
121 callbacks_.error_callback->OnInsertPacketError(*packet_data);
122 }
123 if (callbacks_.post_insert_packet) {
124 callbacks_.post_insert_packet->AfterInsertPacket(*packet_data,
125 neteq_.get());
126 }
127 } else {
128 neteq_->InsertEmptyPacket(packet_data->header);
henrik.lundine8a77e32016-06-22 06:34:03 -0700129 }
Ivo Creusen4384f532018-09-07 17:19:56 +0200130 if (last_packet_time_ms_) {
131 current_state_.packet_iat_ms.push_back(time_now_ms -
132 *last_packet_time_ms_);
133 }
Ivo Creusen2db46b02018-12-14 16:49:12 +0100134 if (text_log_) {
135 const auto ops_state = neteq_->GetOperationsAndState();
136 const auto delta_wallclock =
137 last_packet_time_ms_ ? (time_now_ms - *last_packet_time_ms_) : -1;
138 const auto delta_timestamp =
139 last_packet_timestamp_
140 ? (static_cast<int64_t>(packet_data->header.timestamp) -
141 *last_packet_timestamp_) *
142 1000 / sample_rate_hz_
143 : -1;
144 const auto packet_size_bytes =
145 packet_data->payload.size() == 12
146 ? ByteReader<uint32_t>::ReadLittleEndian(
147 &packet_data->payload[8])
148 : -1;
149 *text_log_ << "Packet - wallclock: " << std::setw(5) << time_now_ms
150 << ", delta wc: " << std::setw(4) << delta_wallclock
Jakob Ivarssone98954c2019-02-06 15:37:50 +0100151 << ", seq_no: " << packet_data->header.sequenceNumber
Ivo Creusen2db46b02018-12-14 16:49:12 +0100152 << ", timestamp: " << std::setw(10)
153 << packet_data->header.timestamp
154 << ", delta ts: " << std::setw(4) << delta_timestamp
155 << ", size: " << std::setw(5) << packet_size_bytes
156 << ", frame size: " << std::setw(3)
157 << ops_state.current_frame_size_ms
158 << ", buffer size: " << std::setw(4)
159 << ops_state.current_buffer_size_ms << std::endl;
160 }
Ivo Creusen4384f532018-09-07 17:19:56 +0200161 last_packet_time_ms_ = absl::make_optional<int>(time_now_ms);
Ivo Creusen2db46b02018-12-14 16:49:12 +0100162 last_packet_timestamp_ =
163 absl::make_optional<uint32_t>(packet_data->header.timestamp);
henrik.lundine8a77e32016-06-22 06:34:03 -0700164 }
165
166 // Check if it is time to get output audio.
167 if (input_->NextOutputEventTime() &&
168 time_now_ms >= *input_->NextOutputEventTime()) {
henrik.lundin02739d92017-05-04 06:09:06 -0700169 if (callbacks_.get_audio_callback) {
170 callbacks_.get_audio_callback->BeforeGetAudio(neteq_.get());
171 }
henrik.lundine8a77e32016-06-22 06:34:03 -0700172 AudioFrame out_frame;
173 bool muted;
Tommi3cc68ec2021-06-09 19:30:41 +0200174 int error = neteq_->GetAudio(&out_frame, &muted, nullptr,
Ivo Creusen55de08e2018-09-03 11:49:27 +0200175 ActionToOperations(next_action_));
176 next_action_ = absl::nullopt;
henrik.lundine8a77e32016-06-22 06:34:03 -0700177 RTC_CHECK(!muted) << "The code does not handle enable_muted_state";
178 if (error != NetEq::kOK) {
henrik.lundin02739d92017-05-04 06:09:06 -0700179 if (callbacks_.error_callback) {
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200180 callbacks_.error_callback->OnGetAudioError();
henrik.lundine8a77e32016-06-22 06:34:03 -0700181 }
182 } else {
183 sample_rate_hz_ = out_frame.sample_rate_hz_;
184 }
henrik.lundin02739d92017-05-04 06:09:06 -0700185 if (callbacks_.get_audio_callback) {
186 callbacks_.get_audio_callback->AfterGetAudio(time_now_ms, out_frame,
187 muted, neteq_.get());
188 }
henrik.lundine8a77e32016-06-22 06:34:03 -0700189
190 if (output_) {
191 RTC_CHECK(output_->WriteArray(
yujo36b1a5f2017-06-12 12:45:32 -0700192 out_frame.data(),
henrik.lundine8a77e32016-06-22 06:34:03 -0700193 out_frame.samples_per_channel_ * out_frame.num_channels_));
194 }
195
196 input_->AdvanceOutputEvent();
Henrik Lundin9be77452018-09-10 12:53:27 +0200197 result.simulation_step_ms =
198 input_->NextEventTime().value_or(time_now_ms) - start_time_ms;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200199 const auto operations_state = neteq_->GetOperationsAndState();
200 current_state_.current_delay_ms = operations_state.current_buffer_size_ms;
Ivo Creusendc6d5532018-09-27 11:43:42 +0200201 current_state_.packet_size_ms = operations_state.current_frame_size_ms;
202 current_state_.next_packet_available =
203 operations_state.next_packet_available;
204 current_state_.packet_buffer_flushed =
205 operations_state.packet_buffer_flushes >
206 prev_ops_state_.packet_buffer_flushes;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200207 // TODO(ivoc): Add more accurate reporting by tracking the origin of
208 // samples in the sync buffer.
209 result.action_times_ms[Action::kExpand] = 0;
210 result.action_times_ms[Action::kAccelerate] = 0;
211 result.action_times_ms[Action::kPreemptiveExpand] = 0;
212 result.action_times_ms[Action::kNormal] = 0;
213
214 if (out_frame.speech_type_ == AudioFrame::SpeechType::kPLC ||
215 out_frame.speech_type_ == AudioFrame::SpeechType::kPLCCNG) {
216 // Consider the whole frame to be the result of expansion.
217 result.action_times_ms[Action::kExpand] = 10;
218 } else if (operations_state.accelerate_samples -
219 prev_ops_state_.accelerate_samples >
220 0) {
221 // Consider the whole frame to be the result of acceleration.
222 result.action_times_ms[Action::kAccelerate] = 10;
223 } else if (operations_state.preemptive_samples -
224 prev_ops_state_.preemptive_samples >
225 0) {
226 // Consider the whole frame to be the result of preemptive expansion.
227 result.action_times_ms[Action::kPreemptiveExpand] = 10;
228 } else {
229 // Consider the whole frame to be the result of normal playout.
230 result.action_times_ms[Action::kNormal] = 10;
231 }
Ivo Creusen2db46b02018-12-14 16:49:12 +0100232 auto lifetime_stats = LifetimeStats();
233 if (text_log_) {
234 const bool plc =
235 (out_frame.speech_type_ == AudioFrame::SpeechType::kPLC) ||
236 (out_frame.speech_type_ == AudioFrame::SpeechType::kPLCCNG);
237 const bool cng = out_frame.speech_type_ == AudioFrame::SpeechType::kCNG;
238 const bool voice_concealed =
Ivo Creusenbf4a2212019-04-24 14:06:24 +0200239 (lifetime_stats.concealed_samples -
240 lifetime_stats.silent_concealed_samples) >
241 (prev_lifetime_stats_.concealed_samples -
242 prev_lifetime_stats_.silent_concealed_samples);
Ivo Creusen2db46b02018-12-14 16:49:12 +0100243 *text_log_ << "GetAudio - wallclock: " << std::setw(5) << time_now_ms
244 << ", delta wc: " << std::setw(4)
245 << (input_->NextEventTime().value_or(time_now_ms) -
246 start_time_ms)
247 << ", CNG: " << cng << ", PLC: " << plc
248 << ", voice concealed: " << voice_concealed
249 << ", buffer size: " << std::setw(4)
250 << current_state_.current_delay_ms << std::endl;
Jakob Ivarsson1a5a8132022-05-25 22:00:14 +0200251 if (lifetime_stats.packets_discarded >
252 prev_lifetime_stats_.packets_discarded) {
Ivo Creusen2db46b02018-12-14 16:49:12 +0100253 *text_log_ << "Discarded "
Jakob Ivarsson1a5a8132022-05-25 22:00:14 +0200254 << (lifetime_stats.packets_discarded -
255 prev_lifetime_stats_.packets_discarded)
Ivo Creusen2db46b02018-12-14 16:49:12 +0100256 << " primary packets." << std::endl;
257 }
258 if (operations_state.packet_buffer_flushes >
259 prev_ops_state_.packet_buffer_flushes) {
260 *text_log_ << "Flushed packet buffer "
261 << (operations_state.packet_buffer_flushes -
262 prev_ops_state_.packet_buffer_flushes)
263 << " times." << std::endl;
264 }
265 }
266 prev_lifetime_stats_ = lifetime_stats;
Jakob Ivarsson9ce451a2019-05-22 16:41:22 +0200267 const bool no_more_packets_to_decode =
268 !input_->NextPacketTime() && !operations_state.next_packet_available;
Ivo Creusen876a3dc2020-08-18 17:08:18 +0200269 // End the simulation if the gap is too large. This indicates an issue
270 // with the event log file.
271 const bool simulation_step_too_large = result.simulation_step_ms > 1000;
Ivo Creusenb9b74562020-10-26 17:49:25 +0100272 if (simulation_step_too_large) {
273 // If we don't reset the step time, the large gap will be included in
274 // the simulation time, which can be a large distortion.
275 result.simulation_step_ms = 10;
276 }
Ivo Creusen876a3dc2020-08-18 17:08:18 +0200277 result.is_simulation_finished = simulation_step_too_large ||
278 no_more_packets_to_decode ||
279 input_->ended();
Ivo Creusend1c2f782018-09-13 14:39:55 +0200280 prev_ops_state_ = operations_state;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200281 return result;
henrik.lundine8a77e32016-06-22 06:34:03 -0700282 }
283 }
Henrik Lundin9be77452018-09-10 12:53:27 +0200284 result.simulation_step_ms =
285 input_->NextEventTime().value_or(time_now_ms) - start_time_ms;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200286 result.is_simulation_finished = true;
287 return result;
288}
289
290void NetEqTest::SetNextAction(NetEqTest::Action next_operation) {
291 next_action_ = absl::optional<Action>(next_operation);
292}
293
294NetEqTest::NetEqState NetEqTest::GetNetEqState() {
Ivo Creusen4384f532018-09-07 17:19:56 +0200295 return current_state_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700296}
297
298NetEqNetworkStatistics NetEqTest::SimulationStats() {
299 NetEqNetworkStatistics stats;
300 RTC_CHECK_EQ(neteq_->NetworkStatistics(&stats), 0);
301 return stats;
302}
303
Alex Narest7ff6ca52018-02-07 18:46:33 +0100304NetEqLifetimeStatistics NetEqTest::LifetimeStats() const {
305 return neteq_->GetLifetimeStatistics();
306}
307
Henrik Lundin7687ad52018-07-02 10:14:46 +0200308NetEqTest::DecoderMap NetEqTest::StandardDecoderMap() {
309 DecoderMap codecs = {
Niels Möller05543682019-01-10 16:55:06 +0100310 {0, SdpAudioFormat("pcmu", 8000, 1)},
311 {8, SdpAudioFormat("pcma", 8000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200312#ifdef WEBRTC_CODEC_ILBC
Niels Möller05543682019-01-10 16:55:06 +0100313 {102, SdpAudioFormat("ilbc", 8000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200314#endif
Niels Möller05543682019-01-10 16:55:06 +0100315 {103, SdpAudioFormat("isac", 16000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200316#if !defined(WEBRTC_ANDROID)
Niels Möller05543682019-01-10 16:55:06 +0100317 {104, SdpAudioFormat("isac", 32000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200318#endif
319#ifdef WEBRTC_CODEC_OPUS
Niels Möller05543682019-01-10 16:55:06 +0100320 {111, SdpAudioFormat("opus", 48000, 2)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200321#endif
Niels Möller05543682019-01-10 16:55:06 +0100322 {93, SdpAudioFormat("l16", 8000, 1)},
323 {94, SdpAudioFormat("l16", 16000, 1)},
324 {95, SdpAudioFormat("l16", 32000, 1)},
325 {96, SdpAudioFormat("l16", 48000, 1)},
326 {9, SdpAudioFormat("g722", 8000, 1)},
327 {106, SdpAudioFormat("telephone-event", 8000, 1)},
328 {114, SdpAudioFormat("telephone-event", 16000, 1)},
329 {115, SdpAudioFormat("telephone-event", 32000, 1)},
330 {116, SdpAudioFormat("telephone-event", 48000, 1)},
331 {117, SdpAudioFormat("red", 8000, 1)},
332 {13, SdpAudioFormat("cn", 8000, 1)},
333 {98, SdpAudioFormat("cn", 16000, 1)},
334 {99, SdpAudioFormat("cn", 32000, 1)},
335 {100, SdpAudioFormat("cn", 48000, 1)}
Henrik Lundin7687ad52018-07-02 10:14:46 +0200336 };
337 return codecs;
338}
339
henrik.lundine8a77e32016-06-22 06:34:03 -0700340void NetEqTest::RegisterDecoders(const DecoderMap& codecs) {
341 for (const auto& c : codecs) {
Niels Möller05543682019-01-10 16:55:06 +0100342 RTC_CHECK(neteq_->RegisterPayloadType(c.first, c.second))
343 << "Cannot register " << c.second.name << " to payload type "
henrik.lundine8a77e32016-06-22 06:34:03 -0700344 << c.first;
345 }
346}
347
henrik.lundine8a77e32016-06-22 06:34:03 -0700348} // namespace test
349} // namespace webrtc