blob: c4fdef0893189aa407eec697f08e5143996fd86b [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"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000017#include "system_wrappers/include/clock.h"
henrik.lundine8a77e32016-06-22 06:34:03 -070018
19namespace webrtc {
20namespace test {
Ivo Creusen55de08e2018-09-03 11:49:27 +020021namespace {
22
23absl::optional<Operations> ActionToOperations(
24 absl::optional<NetEqSimulator::Action> a) {
25 if (!a) {
26 return absl::nullopt;
27 }
28 switch (*a) {
29 case NetEqSimulator::Action::kAccelerate:
30 return absl::make_optional(kAccelerate);
31 case NetEqSimulator::Action::kExpand:
32 return absl::make_optional(kExpand);
33 case NetEqSimulator::Action::kNormal:
34 return absl::make_optional(kNormal);
35 case NetEqSimulator::Action::kPreemptiveExpand:
36 return absl::make_optional(kPreemptiveExpand);
37 }
38}
39
40} // namespace
henrik.lundine8a77e32016-06-22 06:34:03 -070041
42void DefaultNetEqTestErrorCallback::OnInsertPacketError(
henrik.lundine8a77e32016-06-22 06:34:03 -070043 const NetEqInput::PacketData& packet) {
Henrik Lundinc417d9e2017-06-14 12:29:03 +020044 std::cerr << "InsertPacket returned an error." << std::endl;
henrik.lundin7a38fd22017-04-28 01:35:53 -070045 std::cerr << "Packet data: " << packet.ToString() << std::endl;
henrik.lundine8a77e32016-06-22 06:34:03 -070046 FATAL();
47}
48
Henrik Lundinc417d9e2017-06-14 12:29:03 +020049void DefaultNetEqTestErrorCallback::OnGetAudioError() {
50 std::cerr << "GetAudio returned an error." << std::endl;
henrik.lundine8a77e32016-06-22 06:34:03 -070051 FATAL();
52}
53
54NetEqTest::NetEqTest(const NetEq::Config& config,
Niels Möller3f651d82018-12-19 15:06:17 +010055 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
henrik.lundine8a77e32016-06-22 06:34:03 -070056 const DecoderMap& codecs,
Ivo Creusen2db46b02018-12-14 16:49:12 +010057 std::unique_ptr<std::ofstream> text_log,
henrik.lundine8a77e32016-06-22 06:34:03 -070058 std::unique_ptr<NetEqInput> input,
59 std::unique_ptr<AudioSink> output,
henrik.lundin02739d92017-05-04 06:09:06 -070060 Callbacks callbacks)
Alessio Bazzica8f319a32019-07-24 16:47:02 +000061 : clock_(0),
62 neteq_(NetEq::Create(config, &clock_, decoder_factory)),
henrik.lundine8a77e32016-06-22 06:34:03 -070063 input_(std::move(input)),
64 output_(std::move(output)),
henrik.lundin02739d92017-05-04 06:09:06 -070065 callbacks_(callbacks),
Ivo Creusen2db46b02018-12-14 16:49:12 +010066 sample_rate_hz_(config.sample_rate_hz),
67 text_log_(std::move(text_log)) {
henrik.lundine8a77e32016-06-22 06:34:03 -070068 RTC_CHECK(!config.enable_muted_state)
69 << "The code does not handle enable_muted_state";
70 RegisterDecoders(codecs);
henrik.lundine8a77e32016-06-22 06:34:03 -070071}
72
Mirko Bonadei682aac52018-07-20 13:59:20 +020073NetEqTest::~NetEqTest() = default;
74
henrik.lundine8a77e32016-06-22 06:34:03 -070075int64_t NetEqTest::Run() {
Ivo Creusen55de08e2018-09-03 11:49:27 +020076 int64_t simulation_time = 0;
77 SimulationStepResult step_result;
78 do {
79 step_result = RunToNextGetAudio();
80 simulation_time += step_result.simulation_step_ms;
81 } while (!step_result.is_simulation_finished);
82 if (callbacks_.simulation_ended_callback) {
83 callbacks_.simulation_ended_callback->SimulationEnded(simulation_time);
84 }
85 return simulation_time;
86}
87
88NetEqTest::SimulationStepResult NetEqTest::RunToNextGetAudio() {
89 SimulationStepResult result;
henrik.lundine8a77e32016-06-22 06:34:03 -070090 const int64_t start_time_ms = *input_->NextEventTime();
91 int64_t time_now_ms = start_time_ms;
Ivo Creusen4384f532018-09-07 17:19:56 +020092 current_state_.packet_iat_ms.clear();
henrik.lundine8a77e32016-06-22 06:34:03 -070093
94 while (!input_->ended()) {
95 // Advance time to next event.
96 RTC_DCHECK(input_->NextEventTime());
Alessio Bazzica8f319a32019-07-24 16:47:02 +000097 clock_.AdvanceTimeMilliseconds(*input_->NextEventTime() - time_now_ms);
henrik.lundine8a77e32016-06-22 06:34:03 -070098 time_now_ms = *input_->NextEventTime();
99 // Check if it is time to insert packet.
100 if (input_->NextPacketTime() && time_now_ms >= *input_->NextPacketTime()) {
101 std::unique_ptr<NetEqInput::PacketData> packet_data = input_->PopPacket();
102 RTC_CHECK(packet_data);
Minyue Li1a800182018-09-12 12:52:48 +0200103 const size_t payload_data_length =
104 packet_data->payload.size() - packet_data->header.paddingLength;
105 if (payload_data_length != 0) {
106 int error = neteq_->InsertPacket(
107 packet_data->header,
Karl Wiberg45eb1352019-10-10 14:23:00 +0200108 rtc::ArrayView<const uint8_t>(packet_data->payload));
Minyue Li1a800182018-09-12 12:52:48 +0200109 if (error != NetEq::kOK && callbacks_.error_callback) {
110 callbacks_.error_callback->OnInsertPacketError(*packet_data);
111 }
112 if (callbacks_.post_insert_packet) {
113 callbacks_.post_insert_packet->AfterInsertPacket(*packet_data,
114 neteq_.get());
115 }
116 } else {
117 neteq_->InsertEmptyPacket(packet_data->header);
henrik.lundine8a77e32016-06-22 06:34:03 -0700118 }
Ivo Creusen4384f532018-09-07 17:19:56 +0200119 if (last_packet_time_ms_) {
120 current_state_.packet_iat_ms.push_back(time_now_ms -
121 *last_packet_time_ms_);
122 }
Ivo Creusen2db46b02018-12-14 16:49:12 +0100123 if (text_log_) {
124 const auto ops_state = neteq_->GetOperationsAndState();
125 const auto delta_wallclock =
126 last_packet_time_ms_ ? (time_now_ms - *last_packet_time_ms_) : -1;
127 const auto delta_timestamp =
128 last_packet_timestamp_
129 ? (static_cast<int64_t>(packet_data->header.timestamp) -
130 *last_packet_timestamp_) *
131 1000 / sample_rate_hz_
132 : -1;
133 const auto packet_size_bytes =
134 packet_data->payload.size() == 12
135 ? ByteReader<uint32_t>::ReadLittleEndian(
136 &packet_data->payload[8])
137 : -1;
138 *text_log_ << "Packet - wallclock: " << std::setw(5) << time_now_ms
139 << ", delta wc: " << std::setw(4) << delta_wallclock
Jakob Ivarssone98954c2019-02-06 15:37:50 +0100140 << ", seq_no: " << packet_data->header.sequenceNumber
Ivo Creusen2db46b02018-12-14 16:49:12 +0100141 << ", timestamp: " << std::setw(10)
142 << packet_data->header.timestamp
143 << ", delta ts: " << std::setw(4) << delta_timestamp
144 << ", size: " << std::setw(5) << packet_size_bytes
145 << ", frame size: " << std::setw(3)
146 << ops_state.current_frame_size_ms
147 << ", buffer size: " << std::setw(4)
148 << ops_state.current_buffer_size_ms << std::endl;
149 }
Ivo Creusen4384f532018-09-07 17:19:56 +0200150 last_packet_time_ms_ = absl::make_optional<int>(time_now_ms);
Ivo Creusen2db46b02018-12-14 16:49:12 +0100151 last_packet_timestamp_ =
152 absl::make_optional<uint32_t>(packet_data->header.timestamp);
henrik.lundine8a77e32016-06-22 06:34:03 -0700153 }
154
155 // Check if it is time to get output audio.
156 if (input_->NextOutputEventTime() &&
157 time_now_ms >= *input_->NextOutputEventTime()) {
henrik.lundin02739d92017-05-04 06:09:06 -0700158 if (callbacks_.get_audio_callback) {
159 callbacks_.get_audio_callback->BeforeGetAudio(neteq_.get());
160 }
henrik.lundine8a77e32016-06-22 06:34:03 -0700161 AudioFrame out_frame;
162 bool muted;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200163 int error = neteq_->GetAudio(&out_frame, &muted,
164 ActionToOperations(next_action_));
165 next_action_ = absl::nullopt;
henrik.lundine8a77e32016-06-22 06:34:03 -0700166 RTC_CHECK(!muted) << "The code does not handle enable_muted_state";
167 if (error != NetEq::kOK) {
henrik.lundin02739d92017-05-04 06:09:06 -0700168 if (callbacks_.error_callback) {
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200169 callbacks_.error_callback->OnGetAudioError();
henrik.lundine8a77e32016-06-22 06:34:03 -0700170 }
171 } else {
172 sample_rate_hz_ = out_frame.sample_rate_hz_;
173 }
henrik.lundin02739d92017-05-04 06:09:06 -0700174 if (callbacks_.get_audio_callback) {
175 callbacks_.get_audio_callback->AfterGetAudio(time_now_ms, out_frame,
176 muted, neteq_.get());
177 }
henrik.lundine8a77e32016-06-22 06:34:03 -0700178
179 if (output_) {
180 RTC_CHECK(output_->WriteArray(
yujo36b1a5f2017-06-12 12:45:32 -0700181 out_frame.data(),
henrik.lundine8a77e32016-06-22 06:34:03 -0700182 out_frame.samples_per_channel_ * out_frame.num_channels_));
183 }
184
185 input_->AdvanceOutputEvent();
Henrik Lundin9be77452018-09-10 12:53:27 +0200186 result.simulation_step_ms =
187 input_->NextEventTime().value_or(time_now_ms) - start_time_ms;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200188 const auto operations_state = neteq_->GetOperationsAndState();
189 current_state_.current_delay_ms = operations_state.current_buffer_size_ms;
Ivo Creusendc6d5532018-09-27 11:43:42 +0200190 current_state_.packet_size_ms = operations_state.current_frame_size_ms;
191 current_state_.next_packet_available =
192 operations_state.next_packet_available;
193 current_state_.packet_buffer_flushed =
194 operations_state.packet_buffer_flushes >
195 prev_ops_state_.packet_buffer_flushes;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200196 // TODO(ivoc): Add more accurate reporting by tracking the origin of
197 // samples in the sync buffer.
198 result.action_times_ms[Action::kExpand] = 0;
199 result.action_times_ms[Action::kAccelerate] = 0;
200 result.action_times_ms[Action::kPreemptiveExpand] = 0;
201 result.action_times_ms[Action::kNormal] = 0;
202
203 if (out_frame.speech_type_ == AudioFrame::SpeechType::kPLC ||
204 out_frame.speech_type_ == AudioFrame::SpeechType::kPLCCNG) {
205 // Consider the whole frame to be the result of expansion.
206 result.action_times_ms[Action::kExpand] = 10;
207 } else if (operations_state.accelerate_samples -
208 prev_ops_state_.accelerate_samples >
209 0) {
210 // Consider the whole frame to be the result of acceleration.
211 result.action_times_ms[Action::kAccelerate] = 10;
212 } else if (operations_state.preemptive_samples -
213 prev_ops_state_.preemptive_samples >
214 0) {
215 // Consider the whole frame to be the result of preemptive expansion.
216 result.action_times_ms[Action::kPreemptiveExpand] = 10;
217 } else {
218 // Consider the whole frame to be the result of normal playout.
219 result.action_times_ms[Action::kNormal] = 10;
220 }
Ivo Creusen2db46b02018-12-14 16:49:12 +0100221 auto lifetime_stats = LifetimeStats();
222 if (text_log_) {
223 const bool plc =
224 (out_frame.speech_type_ == AudioFrame::SpeechType::kPLC) ||
225 (out_frame.speech_type_ == AudioFrame::SpeechType::kPLCCNG);
226 const bool cng = out_frame.speech_type_ == AudioFrame::SpeechType::kCNG;
227 const bool voice_concealed =
Ivo Creusenbf4a2212019-04-24 14:06:24 +0200228 (lifetime_stats.concealed_samples -
229 lifetime_stats.silent_concealed_samples) >
230 (prev_lifetime_stats_.concealed_samples -
231 prev_lifetime_stats_.silent_concealed_samples);
Ivo Creusen2db46b02018-12-14 16:49:12 +0100232 *text_log_ << "GetAudio - wallclock: " << std::setw(5) << time_now_ms
233 << ", delta wc: " << std::setw(4)
234 << (input_->NextEventTime().value_or(time_now_ms) -
235 start_time_ms)
236 << ", CNG: " << cng << ", PLC: " << plc
237 << ", voice concealed: " << voice_concealed
238 << ", buffer size: " << std::setw(4)
239 << current_state_.current_delay_ms << std::endl;
240 if (operations_state.discarded_primary_packets >
241 prev_ops_state_.discarded_primary_packets) {
242 *text_log_ << "Discarded "
243 << (operations_state.discarded_primary_packets -
244 prev_ops_state_.discarded_primary_packets)
245 << " primary packets." << std::endl;
246 }
247 if (operations_state.packet_buffer_flushes >
248 prev_ops_state_.packet_buffer_flushes) {
249 *text_log_ << "Flushed packet buffer "
250 << (operations_state.packet_buffer_flushes -
251 prev_ops_state_.packet_buffer_flushes)
252 << " times." << std::endl;
253 }
254 }
255 prev_lifetime_stats_ = lifetime_stats;
Jakob Ivarsson9ce451a2019-05-22 16:41:22 +0200256 const bool no_more_packets_to_decode =
257 !input_->NextPacketTime() && !operations_state.next_packet_available;
258 result.is_simulation_finished =
259 no_more_packets_to_decode || input_->ended();
Ivo Creusend1c2f782018-09-13 14:39:55 +0200260 prev_ops_state_ = operations_state;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200261 return result;
henrik.lundine8a77e32016-06-22 06:34:03 -0700262 }
263 }
Henrik Lundin9be77452018-09-10 12:53:27 +0200264 result.simulation_step_ms =
265 input_->NextEventTime().value_or(time_now_ms) - start_time_ms;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200266 result.is_simulation_finished = true;
267 return result;
268}
269
270void NetEqTest::SetNextAction(NetEqTest::Action next_operation) {
271 next_action_ = absl::optional<Action>(next_operation);
272}
273
274NetEqTest::NetEqState NetEqTest::GetNetEqState() {
Ivo Creusen4384f532018-09-07 17:19:56 +0200275 return current_state_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700276}
277
278NetEqNetworkStatistics NetEqTest::SimulationStats() {
279 NetEqNetworkStatistics stats;
280 RTC_CHECK_EQ(neteq_->NetworkStatistics(&stats), 0);
281 return stats;
282}
283
Alex Narest7ff6ca52018-02-07 18:46:33 +0100284NetEqLifetimeStatistics NetEqTest::LifetimeStats() const {
285 return neteq_->GetLifetimeStatistics();
286}
287
Henrik Lundin7687ad52018-07-02 10:14:46 +0200288NetEqTest::DecoderMap NetEqTest::StandardDecoderMap() {
289 DecoderMap codecs = {
Niels Möller05543682019-01-10 16:55:06 +0100290 {0, SdpAudioFormat("pcmu", 8000, 1)},
291 {8, SdpAudioFormat("pcma", 8000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200292#ifdef WEBRTC_CODEC_ILBC
Niels Möller05543682019-01-10 16:55:06 +0100293 {102, SdpAudioFormat("ilbc", 8000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200294#endif
Niels Möller05543682019-01-10 16:55:06 +0100295 {103, SdpAudioFormat("isac", 16000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200296#if !defined(WEBRTC_ANDROID)
Niels Möller05543682019-01-10 16:55:06 +0100297 {104, SdpAudioFormat("isac", 32000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200298#endif
299#ifdef WEBRTC_CODEC_OPUS
Niels Möller05543682019-01-10 16:55:06 +0100300 {111, SdpAudioFormat("opus", 48000, 2)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200301#endif
Niels Möller05543682019-01-10 16:55:06 +0100302 {93, SdpAudioFormat("l16", 8000, 1)},
303 {94, SdpAudioFormat("l16", 16000, 1)},
304 {95, SdpAudioFormat("l16", 32000, 1)},
305 {96, SdpAudioFormat("l16", 48000, 1)},
306 {9, SdpAudioFormat("g722", 8000, 1)},
307 {106, SdpAudioFormat("telephone-event", 8000, 1)},
308 {114, SdpAudioFormat("telephone-event", 16000, 1)},
309 {115, SdpAudioFormat("telephone-event", 32000, 1)},
310 {116, SdpAudioFormat("telephone-event", 48000, 1)},
311 {117, SdpAudioFormat("red", 8000, 1)},
312 {13, SdpAudioFormat("cn", 8000, 1)},
313 {98, SdpAudioFormat("cn", 16000, 1)},
314 {99, SdpAudioFormat("cn", 32000, 1)},
315 {100, SdpAudioFormat("cn", 48000, 1)}
Henrik Lundin7687ad52018-07-02 10:14:46 +0200316 };
317 return codecs;
318}
319
henrik.lundine8a77e32016-06-22 06:34:03 -0700320void NetEqTest::RegisterDecoders(const DecoderMap& codecs) {
321 for (const auto& c : codecs) {
Niels Möller05543682019-01-10 16:55:06 +0100322 RTC_CHECK(neteq_->RegisterPayloadType(c.first, c.second))
323 << "Cannot register " << c.second.name << " to payload type "
henrik.lundine8a77e32016-06-22 06:34:03 -0700324 << c.first;
325 }
326}
327
henrik.lundine8a77e32016-06-22 06:34:03 -0700328} // namespace test
329} // namespace webrtc