blob: 7e22823d7e465a44880b6fcd88bf219a6ef0f02d [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,
108 rtc::ArrayView<const uint8_t>(packet_data->payload),
109 static_cast<uint32_t>(packet_data->time_ms * sample_rate_hz_ /
110 1000));
111 if (error != NetEq::kOK && callbacks_.error_callback) {
112 callbacks_.error_callback->OnInsertPacketError(*packet_data);
113 }
114 if (callbacks_.post_insert_packet) {
115 callbacks_.post_insert_packet->AfterInsertPacket(*packet_data,
116 neteq_.get());
117 }
118 } else {
119 neteq_->InsertEmptyPacket(packet_data->header);
henrik.lundine8a77e32016-06-22 06:34:03 -0700120 }
Ivo Creusen4384f532018-09-07 17:19:56 +0200121 if (last_packet_time_ms_) {
122 current_state_.packet_iat_ms.push_back(time_now_ms -
123 *last_packet_time_ms_);
124 }
Ivo Creusen2db46b02018-12-14 16:49:12 +0100125 if (text_log_) {
126 const auto ops_state = neteq_->GetOperationsAndState();
127 const auto delta_wallclock =
128 last_packet_time_ms_ ? (time_now_ms - *last_packet_time_ms_) : -1;
129 const auto delta_timestamp =
130 last_packet_timestamp_
131 ? (static_cast<int64_t>(packet_data->header.timestamp) -
132 *last_packet_timestamp_) *
133 1000 / sample_rate_hz_
134 : -1;
135 const auto packet_size_bytes =
136 packet_data->payload.size() == 12
137 ? ByteReader<uint32_t>::ReadLittleEndian(
138 &packet_data->payload[8])
139 : -1;
140 *text_log_ << "Packet - wallclock: " << std::setw(5) << time_now_ms
141 << ", delta wc: " << std::setw(4) << delta_wallclock
Jakob Ivarssone98954c2019-02-06 15:37:50 +0100142 << ", seq_no: " << packet_data->header.sequenceNumber
Ivo Creusen2db46b02018-12-14 16:49:12 +0100143 << ", timestamp: " << std::setw(10)
144 << packet_data->header.timestamp
145 << ", delta ts: " << std::setw(4) << delta_timestamp
146 << ", size: " << std::setw(5) << packet_size_bytes
147 << ", frame size: " << std::setw(3)
148 << ops_state.current_frame_size_ms
149 << ", buffer size: " << std::setw(4)
150 << ops_state.current_buffer_size_ms << std::endl;
151 }
Ivo Creusen4384f532018-09-07 17:19:56 +0200152 last_packet_time_ms_ = absl::make_optional<int>(time_now_ms);
Ivo Creusen2db46b02018-12-14 16:49:12 +0100153 last_packet_timestamp_ =
154 absl::make_optional<uint32_t>(packet_data->header.timestamp);
henrik.lundine8a77e32016-06-22 06:34:03 -0700155 }
156
157 // Check if it is time to get output audio.
158 if (input_->NextOutputEventTime() &&
159 time_now_ms >= *input_->NextOutputEventTime()) {
henrik.lundin02739d92017-05-04 06:09:06 -0700160 if (callbacks_.get_audio_callback) {
161 callbacks_.get_audio_callback->BeforeGetAudio(neteq_.get());
162 }
henrik.lundine8a77e32016-06-22 06:34:03 -0700163 AudioFrame out_frame;
164 bool muted;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200165 int error = neteq_->GetAudio(&out_frame, &muted,
166 ActionToOperations(next_action_));
167 next_action_ = absl::nullopt;
henrik.lundine8a77e32016-06-22 06:34:03 -0700168 RTC_CHECK(!muted) << "The code does not handle enable_muted_state";
169 if (error != NetEq::kOK) {
henrik.lundin02739d92017-05-04 06:09:06 -0700170 if (callbacks_.error_callback) {
Henrik Lundinc417d9e2017-06-14 12:29:03 +0200171 callbacks_.error_callback->OnGetAudioError();
henrik.lundine8a77e32016-06-22 06:34:03 -0700172 }
173 } else {
174 sample_rate_hz_ = out_frame.sample_rate_hz_;
175 }
henrik.lundin02739d92017-05-04 06:09:06 -0700176 if (callbacks_.get_audio_callback) {
177 callbacks_.get_audio_callback->AfterGetAudio(time_now_ms, out_frame,
178 muted, neteq_.get());
179 }
henrik.lundine8a77e32016-06-22 06:34:03 -0700180
181 if (output_) {
182 RTC_CHECK(output_->WriteArray(
yujo36b1a5f2017-06-12 12:45:32 -0700183 out_frame.data(),
henrik.lundine8a77e32016-06-22 06:34:03 -0700184 out_frame.samples_per_channel_ * out_frame.num_channels_));
185 }
186
187 input_->AdvanceOutputEvent();
Henrik Lundin9be77452018-09-10 12:53:27 +0200188 result.simulation_step_ms =
189 input_->NextEventTime().value_or(time_now_ms) - start_time_ms;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200190 const auto operations_state = neteq_->GetOperationsAndState();
191 current_state_.current_delay_ms = operations_state.current_buffer_size_ms;
Ivo Creusendc6d5532018-09-27 11:43:42 +0200192 current_state_.packet_size_ms = operations_state.current_frame_size_ms;
193 current_state_.next_packet_available =
194 operations_state.next_packet_available;
195 current_state_.packet_buffer_flushed =
196 operations_state.packet_buffer_flushes >
197 prev_ops_state_.packet_buffer_flushes;
Ivo Creusend1c2f782018-09-13 14:39:55 +0200198 // TODO(ivoc): Add more accurate reporting by tracking the origin of
199 // samples in the sync buffer.
200 result.action_times_ms[Action::kExpand] = 0;
201 result.action_times_ms[Action::kAccelerate] = 0;
202 result.action_times_ms[Action::kPreemptiveExpand] = 0;
203 result.action_times_ms[Action::kNormal] = 0;
204
205 if (out_frame.speech_type_ == AudioFrame::SpeechType::kPLC ||
206 out_frame.speech_type_ == AudioFrame::SpeechType::kPLCCNG) {
207 // Consider the whole frame to be the result of expansion.
208 result.action_times_ms[Action::kExpand] = 10;
209 } else if (operations_state.accelerate_samples -
210 prev_ops_state_.accelerate_samples >
211 0) {
212 // Consider the whole frame to be the result of acceleration.
213 result.action_times_ms[Action::kAccelerate] = 10;
214 } else if (operations_state.preemptive_samples -
215 prev_ops_state_.preemptive_samples >
216 0) {
217 // Consider the whole frame to be the result of preemptive expansion.
218 result.action_times_ms[Action::kPreemptiveExpand] = 10;
219 } else {
220 // Consider the whole frame to be the result of normal playout.
221 result.action_times_ms[Action::kNormal] = 10;
222 }
Ivo Creusen2db46b02018-12-14 16:49:12 +0100223 auto lifetime_stats = LifetimeStats();
224 if (text_log_) {
225 const bool plc =
226 (out_frame.speech_type_ == AudioFrame::SpeechType::kPLC) ||
227 (out_frame.speech_type_ == AudioFrame::SpeechType::kPLCCNG);
228 const bool cng = out_frame.speech_type_ == AudioFrame::SpeechType::kCNG;
229 const bool voice_concealed =
Ivo Creusenbf4a2212019-04-24 14:06:24 +0200230 (lifetime_stats.concealed_samples -
231 lifetime_stats.silent_concealed_samples) >
232 (prev_lifetime_stats_.concealed_samples -
233 prev_lifetime_stats_.silent_concealed_samples);
Ivo Creusen2db46b02018-12-14 16:49:12 +0100234 *text_log_ << "GetAudio - wallclock: " << std::setw(5) << time_now_ms
235 << ", delta wc: " << std::setw(4)
236 << (input_->NextEventTime().value_or(time_now_ms) -
237 start_time_ms)
238 << ", CNG: " << cng << ", PLC: " << plc
239 << ", voice concealed: " << voice_concealed
240 << ", buffer size: " << std::setw(4)
241 << current_state_.current_delay_ms << std::endl;
242 if (operations_state.discarded_primary_packets >
243 prev_ops_state_.discarded_primary_packets) {
244 *text_log_ << "Discarded "
245 << (operations_state.discarded_primary_packets -
246 prev_ops_state_.discarded_primary_packets)
247 << " primary packets." << std::endl;
248 }
249 if (operations_state.packet_buffer_flushes >
250 prev_ops_state_.packet_buffer_flushes) {
251 *text_log_ << "Flushed packet buffer "
252 << (operations_state.packet_buffer_flushes -
253 prev_ops_state_.packet_buffer_flushes)
254 << " times." << std::endl;
255 }
256 }
257 prev_lifetime_stats_ = lifetime_stats;
Jakob Ivarsson9ce451a2019-05-22 16:41:22 +0200258 const bool no_more_packets_to_decode =
259 !input_->NextPacketTime() && !operations_state.next_packet_available;
260 result.is_simulation_finished =
261 no_more_packets_to_decode || input_->ended();
Ivo Creusend1c2f782018-09-13 14:39:55 +0200262 prev_ops_state_ = operations_state;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200263 return result;
henrik.lundine8a77e32016-06-22 06:34:03 -0700264 }
265 }
Henrik Lundin9be77452018-09-10 12:53:27 +0200266 result.simulation_step_ms =
267 input_->NextEventTime().value_or(time_now_ms) - start_time_ms;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200268 result.is_simulation_finished = true;
269 return result;
270}
271
272void NetEqTest::SetNextAction(NetEqTest::Action next_operation) {
273 next_action_ = absl::optional<Action>(next_operation);
274}
275
276NetEqTest::NetEqState NetEqTest::GetNetEqState() {
Ivo Creusen4384f532018-09-07 17:19:56 +0200277 return current_state_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700278}
279
280NetEqNetworkStatistics NetEqTest::SimulationStats() {
281 NetEqNetworkStatistics stats;
282 RTC_CHECK_EQ(neteq_->NetworkStatistics(&stats), 0);
283 return stats;
284}
285
Alex Narest7ff6ca52018-02-07 18:46:33 +0100286NetEqLifetimeStatistics NetEqTest::LifetimeStats() const {
287 return neteq_->GetLifetimeStatistics();
288}
289
Henrik Lundin7687ad52018-07-02 10:14:46 +0200290NetEqTest::DecoderMap NetEqTest::StandardDecoderMap() {
291 DecoderMap codecs = {
Niels Möller05543682019-01-10 16:55:06 +0100292 {0, SdpAudioFormat("pcmu", 8000, 1)},
293 {8, SdpAudioFormat("pcma", 8000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200294#ifdef WEBRTC_CODEC_ILBC
Niels Möller05543682019-01-10 16:55:06 +0100295 {102, SdpAudioFormat("ilbc", 8000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200296#endif
Niels Möller05543682019-01-10 16:55:06 +0100297 {103, SdpAudioFormat("isac", 16000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200298#if !defined(WEBRTC_ANDROID)
Niels Möller05543682019-01-10 16:55:06 +0100299 {104, SdpAudioFormat("isac", 32000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200300#endif
301#ifdef WEBRTC_CODEC_OPUS
Niels Möller05543682019-01-10 16:55:06 +0100302 {111, SdpAudioFormat("opus", 48000, 2)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200303#endif
Niels Möller05543682019-01-10 16:55:06 +0100304 {93, SdpAudioFormat("l16", 8000, 1)},
305 {94, SdpAudioFormat("l16", 16000, 1)},
306 {95, SdpAudioFormat("l16", 32000, 1)},
307 {96, SdpAudioFormat("l16", 48000, 1)},
308 {9, SdpAudioFormat("g722", 8000, 1)},
309 {106, SdpAudioFormat("telephone-event", 8000, 1)},
310 {114, SdpAudioFormat("telephone-event", 16000, 1)},
311 {115, SdpAudioFormat("telephone-event", 32000, 1)},
312 {116, SdpAudioFormat("telephone-event", 48000, 1)},
313 {117, SdpAudioFormat("red", 8000, 1)},
314 {13, SdpAudioFormat("cn", 8000, 1)},
315 {98, SdpAudioFormat("cn", 16000, 1)},
316 {99, SdpAudioFormat("cn", 32000, 1)},
317 {100, SdpAudioFormat("cn", 48000, 1)}
Henrik Lundin7687ad52018-07-02 10:14:46 +0200318 };
319 return codecs;
320}
321
henrik.lundine8a77e32016-06-22 06:34:03 -0700322void NetEqTest::RegisterDecoders(const DecoderMap& codecs) {
323 for (const auto& c : codecs) {
Niels Möller05543682019-01-10 16:55:06 +0100324 RTC_CHECK(neteq_->RegisterPayloadType(c.first, c.second))
325 << "Cannot register " << c.second.name << " to payload type "
henrik.lundine8a77e32016-06-22 06:34:03 -0700326 << c.first;
327 }
328}
329
henrik.lundine8a77e32016-06-22 06:34:03 -0700330} // namespace test
331} // namespace webrtc