blob: 50d8ba1a182a7c48b924142b4d338b18460cb313 [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 Creusen3ce44a32019-10-31 14:38:11 +010016#include "api/neteq/custom_neteq_factory.h"
17#include "api/neteq/default_neteq_controller_factory.h"
Ivo Creusen2db46b02018-12-14 16:49:12 +010018#include "modules/rtp_rtcp/source/byte_io.h"
Alessio Bazzica8f319a32019-07-24 16:47:02 +000019#include "system_wrappers/include/clock.h"
henrik.lundine8a77e32016-06-22 06:34:03 -070020
21namespace webrtc {
22namespace test {
Ivo Creusen55de08e2018-09-03 11:49:27 +020023namespace {
24
Ivo Creusen3ce44a32019-10-31 14:38:11 +010025absl::optional<NetEq::Operation> ActionToOperations(
Ivo Creusen55de08e2018-09-03 11:49:27 +020026 absl::optional<NetEqSimulator::Action> a) {
27 if (!a) {
28 return absl::nullopt;
29 }
30 switch (*a) {
31 case NetEqSimulator::Action::kAccelerate:
Ivo Creusen3ce44a32019-10-31 14:38:11 +010032 return absl::make_optional(NetEq::Operation::kAccelerate);
Ivo Creusen55de08e2018-09-03 11:49:27 +020033 case NetEqSimulator::Action::kExpand:
Ivo Creusen3ce44a32019-10-31 14:38:11 +010034 return absl::make_optional(NetEq::Operation::kExpand);
Ivo Creusen55de08e2018-09-03 11:49:27 +020035 case NetEqSimulator::Action::kNormal:
Ivo Creusen3ce44a32019-10-31 14:38:11 +010036 return absl::make_optional(NetEq::Operation::kNormal);
Ivo Creusen55de08e2018-09-03 11:49:27 +020037 case NetEqSimulator::Action::kPreemptiveExpand:
Ivo Creusen3ce44a32019-10-31 14:38:11 +010038 return absl::make_optional(NetEq::Operation::kPreemptiveExpand);
Ivo Creusen55de08e2018-09-03 11:49:27 +020039 }
40}
41
Ivo Creusen3ce44a32019-10-31 14:38:11 +010042std::unique_ptr<NetEq> CreateNetEq(
43 const NetEq::Config& config,
44 Clock* clock,
45 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
46 CustomNetEqFactory neteq_factory(
47 decoder_factory, std::make_unique<DefaultNetEqControllerFactory>());
48 return neteq_factory.CreateNetEq(config, clock);
49}
50
Ivo Creusen55de08e2018-09-03 11:49:27 +020051} // namespace
henrik.lundine8a77e32016-06-22 06:34:03 -070052
53void DefaultNetEqTestErrorCallback::OnInsertPacketError(
henrik.lundine8a77e32016-06-22 06:34:03 -070054 const NetEqInput::PacketData& packet) {
Henrik Lundinc417d9e2017-06-14 12:29:03 +020055 std::cerr << "InsertPacket returned an error." << std::endl;
henrik.lundin7a38fd22017-04-28 01:35:53 -070056 std::cerr << "Packet data: " << packet.ToString() << std::endl;
henrik.lundine8a77e32016-06-22 06:34:03 -070057 FATAL();
58}
59
Henrik Lundinc417d9e2017-06-14 12:29:03 +020060void DefaultNetEqTestErrorCallback::OnGetAudioError() {
61 std::cerr << "GetAudio returned an error." << std::endl;
henrik.lundine8a77e32016-06-22 06:34:03 -070062 FATAL();
63}
64
65NetEqTest::NetEqTest(const NetEq::Config& config,
Niels Möller3f651d82018-12-19 15:06:17 +010066 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
henrik.lundine8a77e32016-06-22 06:34:03 -070067 const DecoderMap& codecs,
Ivo Creusen2db46b02018-12-14 16:49:12 +010068 std::unique_ptr<std::ofstream> text_log,
henrik.lundine8a77e32016-06-22 06:34:03 -070069 std::unique_ptr<NetEqInput> input,
70 std::unique_ptr<AudioSink> output,
henrik.lundin02739d92017-05-04 06:09:06 -070071 Callbacks callbacks)
Alessio Bazzica8f319a32019-07-24 16:47:02 +000072 : clock_(0),
Ivo Creusen3ce44a32019-10-31 14:38:11 +010073 neteq_(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) {
94 callbacks_.simulation_ended_callback->SimulationEnded(simulation_time);
95 }
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;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200174 int error = neteq_->GetAudio(&out_frame, &muted,
175 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;
251 if (operations_state.discarded_primary_packets >
252 prev_ops_state_.discarded_primary_packets) {
253 *text_log_ << "Discarded "
254 << (operations_state.discarded_primary_packets -
255 prev_ops_state_.discarded_primary_packets)
256 << " 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;
269 result.is_simulation_finished =
270 no_more_packets_to_decode || input_->ended();
Ivo Creusend1c2f782018-09-13 14:39:55 +0200271 prev_ops_state_ = operations_state;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200272 return result;
henrik.lundine8a77e32016-06-22 06:34:03 -0700273 }
274 }
Henrik Lundin9be77452018-09-10 12:53:27 +0200275 result.simulation_step_ms =
276 input_->NextEventTime().value_or(time_now_ms) - start_time_ms;
Ivo Creusen55de08e2018-09-03 11:49:27 +0200277 result.is_simulation_finished = true;
278 return result;
279}
280
281void NetEqTest::SetNextAction(NetEqTest::Action next_operation) {
282 next_action_ = absl::optional<Action>(next_operation);
283}
284
285NetEqTest::NetEqState NetEqTest::GetNetEqState() {
Ivo Creusen4384f532018-09-07 17:19:56 +0200286 return current_state_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700287}
288
289NetEqNetworkStatistics NetEqTest::SimulationStats() {
290 NetEqNetworkStatistics stats;
291 RTC_CHECK_EQ(neteq_->NetworkStatistics(&stats), 0);
292 return stats;
293}
294
Alex Narest7ff6ca52018-02-07 18:46:33 +0100295NetEqLifetimeStatistics NetEqTest::LifetimeStats() const {
296 return neteq_->GetLifetimeStatistics();
297}
298
Henrik Lundin7687ad52018-07-02 10:14:46 +0200299NetEqTest::DecoderMap NetEqTest::StandardDecoderMap() {
300 DecoderMap codecs = {
Niels Möller05543682019-01-10 16:55:06 +0100301 {0, SdpAudioFormat("pcmu", 8000, 1)},
302 {8, SdpAudioFormat("pcma", 8000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200303#ifdef WEBRTC_CODEC_ILBC
Niels Möller05543682019-01-10 16:55:06 +0100304 {102, SdpAudioFormat("ilbc", 8000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200305#endif
Niels Möller05543682019-01-10 16:55:06 +0100306 {103, SdpAudioFormat("isac", 16000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200307#if !defined(WEBRTC_ANDROID)
Niels Möller05543682019-01-10 16:55:06 +0100308 {104, SdpAudioFormat("isac", 32000, 1)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200309#endif
310#ifdef WEBRTC_CODEC_OPUS
Niels Möller05543682019-01-10 16:55:06 +0100311 {111, SdpAudioFormat("opus", 48000, 2)},
Henrik Lundin7687ad52018-07-02 10:14:46 +0200312#endif
Niels Möller05543682019-01-10 16:55:06 +0100313 {93, SdpAudioFormat("l16", 8000, 1)},
314 {94, SdpAudioFormat("l16", 16000, 1)},
315 {95, SdpAudioFormat("l16", 32000, 1)},
316 {96, SdpAudioFormat("l16", 48000, 1)},
317 {9, SdpAudioFormat("g722", 8000, 1)},
318 {106, SdpAudioFormat("telephone-event", 8000, 1)},
319 {114, SdpAudioFormat("telephone-event", 16000, 1)},
320 {115, SdpAudioFormat("telephone-event", 32000, 1)},
321 {116, SdpAudioFormat("telephone-event", 48000, 1)},
322 {117, SdpAudioFormat("red", 8000, 1)},
323 {13, SdpAudioFormat("cn", 8000, 1)},
324 {98, SdpAudioFormat("cn", 16000, 1)},
325 {99, SdpAudioFormat("cn", 32000, 1)},
326 {100, SdpAudioFormat("cn", 48000, 1)}
Henrik Lundin7687ad52018-07-02 10:14:46 +0200327 };
328 return codecs;
329}
330
henrik.lundine8a77e32016-06-22 06:34:03 -0700331void NetEqTest::RegisterDecoders(const DecoderMap& codecs) {
332 for (const auto& c : codecs) {
Niels Möller05543682019-01-10 16:55:06 +0100333 RTC_CHECK(neteq_->RegisterPayloadType(c.first, c.second))
334 << "Cannot register " << c.second.name << " to payload type "
henrik.lundine8a77e32016-06-22 06:34:03 -0700335 << c.first;
336 }
337}
338
henrik.lundine8a77e32016-06-22 06:34:03 -0700339} // namespace test
340} // namespace webrtc