henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/neteq/tools/neteq_test.h" |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 12 | |
| 13 | #include <iostream> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | namespace test { |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | absl::optional<Operations> ActionToOperations( |
| 22 | absl::optional<NetEqSimulator::Action> a) { |
| 23 | if (!a) { |
| 24 | return absl::nullopt; |
| 25 | } |
| 26 | switch (*a) { |
| 27 | case NetEqSimulator::Action::kAccelerate: |
| 28 | return absl::make_optional(kAccelerate); |
| 29 | case NetEqSimulator::Action::kExpand: |
| 30 | return absl::make_optional(kExpand); |
| 31 | case NetEqSimulator::Action::kNormal: |
| 32 | return absl::make_optional(kNormal); |
| 33 | case NetEqSimulator::Action::kPreemptiveExpand: |
| 34 | return absl::make_optional(kPreemptiveExpand); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | } // namespace |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 39 | |
| 40 | void DefaultNetEqTestErrorCallback::OnInsertPacketError( |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 41 | const NetEqInput::PacketData& packet) { |
Henrik Lundin | c417d9e | 2017-06-14 12:29:03 +0200 | [diff] [blame] | 42 | std::cerr << "InsertPacket returned an error." << std::endl; |
henrik.lundin | 7a38fd2 | 2017-04-28 01:35:53 -0700 | [diff] [blame] | 43 | std::cerr << "Packet data: " << packet.ToString() << std::endl; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 44 | FATAL(); |
| 45 | } |
| 46 | |
Henrik Lundin | c417d9e | 2017-06-14 12:29:03 +0200 | [diff] [blame] | 47 | void DefaultNetEqTestErrorCallback::OnGetAudioError() { |
| 48 | std::cerr << "GetAudio returned an error." << std::endl; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 49 | FATAL(); |
| 50 | } |
| 51 | |
| 52 | NetEqTest::NetEqTest(const NetEq::Config& config, |
| 53 | const DecoderMap& codecs, |
| 54 | const ExtDecoderMap& ext_codecs, |
| 55 | std::unique_ptr<NetEqInput> input, |
| 56 | std::unique_ptr<AudioSink> output, |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 57 | Callbacks callbacks) |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 58 | : neteq_(NetEq::Create(config, CreateBuiltinAudioDecoderFactory())), |
| 59 | input_(std::move(input)), |
| 60 | output_(std::move(output)), |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 61 | callbacks_(callbacks), |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 62 | sample_rate_hz_(config.sample_rate_hz) { |
| 63 | RTC_CHECK(!config.enable_muted_state) |
| 64 | << "The code does not handle enable_muted_state"; |
| 65 | RegisterDecoders(codecs); |
| 66 | RegisterExternalDecoders(ext_codecs); |
| 67 | } |
| 68 | |
Mirko Bonadei | 682aac5 | 2018-07-20 13:59:20 +0200 | [diff] [blame] | 69 | NetEqTest::~NetEqTest() = default; |
| 70 | |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 71 | int64_t NetEqTest::Run() { |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 72 | int64_t simulation_time = 0; |
| 73 | SimulationStepResult step_result; |
| 74 | do { |
| 75 | step_result = RunToNextGetAudio(); |
| 76 | simulation_time += step_result.simulation_step_ms; |
| 77 | } while (!step_result.is_simulation_finished); |
| 78 | if (callbacks_.simulation_ended_callback) { |
| 79 | callbacks_.simulation_ended_callback->SimulationEnded(simulation_time); |
| 80 | } |
| 81 | return simulation_time; |
| 82 | } |
| 83 | |
| 84 | NetEqTest::SimulationStepResult NetEqTest::RunToNextGetAudio() { |
| 85 | SimulationStepResult result; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 86 | const int64_t start_time_ms = *input_->NextEventTime(); |
| 87 | int64_t time_now_ms = start_time_ms; |
Ivo Creusen | 4384f53 | 2018-09-07 17:19:56 +0200 | [diff] [blame] | 88 | current_state_.packet_iat_ms.clear(); |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 89 | |
| 90 | while (!input_->ended()) { |
| 91 | // Advance time to next event. |
| 92 | RTC_DCHECK(input_->NextEventTime()); |
| 93 | time_now_ms = *input_->NextEventTime(); |
| 94 | // Check if it is time to insert packet. |
| 95 | if (input_->NextPacketTime() && time_now_ms >= *input_->NextPacketTime()) { |
| 96 | std::unique_ptr<NetEqInput::PacketData> packet_data = input_->PopPacket(); |
| 97 | RTC_CHECK(packet_data); |
Minyue Li | 1a80018 | 2018-09-12 12:52:48 +0200 | [diff] [blame] | 98 | const size_t payload_data_length = |
| 99 | packet_data->payload.size() - packet_data->header.paddingLength; |
| 100 | if (payload_data_length != 0) { |
| 101 | int error = neteq_->InsertPacket( |
| 102 | packet_data->header, |
| 103 | rtc::ArrayView<const uint8_t>(packet_data->payload), |
| 104 | static_cast<uint32_t>(packet_data->time_ms * sample_rate_hz_ / |
| 105 | 1000)); |
| 106 | if (error != NetEq::kOK && callbacks_.error_callback) { |
| 107 | callbacks_.error_callback->OnInsertPacketError(*packet_data); |
| 108 | } |
| 109 | if (callbacks_.post_insert_packet) { |
| 110 | callbacks_.post_insert_packet->AfterInsertPacket(*packet_data, |
| 111 | neteq_.get()); |
| 112 | } |
| 113 | } else { |
| 114 | neteq_->InsertEmptyPacket(packet_data->header); |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 115 | } |
Ivo Creusen | 4384f53 | 2018-09-07 17:19:56 +0200 | [diff] [blame] | 116 | if (last_packet_time_ms_) { |
| 117 | current_state_.packet_iat_ms.push_back(time_now_ms - |
| 118 | *last_packet_time_ms_); |
| 119 | } |
| 120 | last_packet_time_ms_ = absl::make_optional<int>(time_now_ms); |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Check if it is time to get output audio. |
| 124 | if (input_->NextOutputEventTime() && |
| 125 | time_now_ms >= *input_->NextOutputEventTime()) { |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 126 | if (callbacks_.get_audio_callback) { |
| 127 | callbacks_.get_audio_callback->BeforeGetAudio(neteq_.get()); |
| 128 | } |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 129 | AudioFrame out_frame; |
| 130 | bool muted; |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 131 | int error = neteq_->GetAudio(&out_frame, &muted, |
| 132 | ActionToOperations(next_action_)); |
| 133 | next_action_ = absl::nullopt; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 134 | RTC_CHECK(!muted) << "The code does not handle enable_muted_state"; |
| 135 | if (error != NetEq::kOK) { |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 136 | if (callbacks_.error_callback) { |
Henrik Lundin | c417d9e | 2017-06-14 12:29:03 +0200 | [diff] [blame] | 137 | callbacks_.error_callback->OnGetAudioError(); |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 138 | } |
| 139 | } else { |
| 140 | sample_rate_hz_ = out_frame.sample_rate_hz_; |
| 141 | } |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 142 | if (callbacks_.get_audio_callback) { |
| 143 | callbacks_.get_audio_callback->AfterGetAudio(time_now_ms, out_frame, |
| 144 | muted, neteq_.get()); |
| 145 | } |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 146 | |
| 147 | if (output_) { |
| 148 | RTC_CHECK(output_->WriteArray( |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 149 | out_frame.data(), |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 150 | out_frame.samples_per_channel_ * out_frame.num_channels_)); |
| 151 | } |
| 152 | |
| 153 | input_->AdvanceOutputEvent(); |
Henrik Lundin | 9be7745 | 2018-09-10 12:53:27 +0200 | [diff] [blame] | 154 | result.simulation_step_ms = |
| 155 | input_->NextEventTime().value_or(time_now_ms) - start_time_ms; |
Ivo Creusen | 4384f53 | 2018-09-07 17:19:56 +0200 | [diff] [blame] | 156 | const auto network_stats = SimulationStats(); |
| 157 | current_state_.current_delay_ms = network_stats.current_buffer_size_ms; |
| 158 | current_state_.packet_loss_occurred = network_stats.packet_loss_rate > 0; |
| 159 | int scaling_factor = std::max(1 << 14, network_stats.accelerate_rate + |
| 160 | network_stats.expand_rate + |
| 161 | network_stats.preemptive_rate); |
| 162 | // TODO(ivoc): Improve the accuracy of these numbers by adding a new API |
| 163 | // to NetEq. |
| 164 | result.action_times_ms[Action::kAccelerate] = |
| 165 | (10 * network_stats.accelerate_rate) / scaling_factor; |
| 166 | result.action_times_ms[Action::kExpand] = |
| 167 | (10 * network_stats.expand_rate) / scaling_factor; |
| 168 | result.action_times_ms[Action::kPreemptiveExpand] = |
| 169 | (10 * network_stats.preemptive_rate) / scaling_factor; |
| 170 | result.action_times_ms[Action::kNormal] = |
| 171 | 10 - result.action_times_ms[Action::kAccelerate] - |
| 172 | result.action_times_ms[Action::kExpand] - |
| 173 | result.action_times_ms[Action::kPreemptiveExpand]; |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 174 | result.is_simulation_finished = input_->ended(); |
| 175 | return result; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
Henrik Lundin | 9be7745 | 2018-09-10 12:53:27 +0200 | [diff] [blame] | 178 | result.simulation_step_ms = |
| 179 | input_->NextEventTime().value_or(time_now_ms) - start_time_ms; |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 180 | result.is_simulation_finished = true; |
| 181 | return result; |
| 182 | } |
| 183 | |
| 184 | void NetEqTest::SetNextAction(NetEqTest::Action next_operation) { |
| 185 | next_action_ = absl::optional<Action>(next_operation); |
| 186 | } |
| 187 | |
| 188 | NetEqTest::NetEqState NetEqTest::GetNetEqState() { |
Ivo Creusen | 4384f53 | 2018-09-07 17:19:56 +0200 | [diff] [blame] | 189 | return current_state_; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | NetEqNetworkStatistics NetEqTest::SimulationStats() { |
| 193 | NetEqNetworkStatistics stats; |
| 194 | RTC_CHECK_EQ(neteq_->NetworkStatistics(&stats), 0); |
| 195 | return stats; |
| 196 | } |
| 197 | |
Alex Narest | 7ff6ca5 | 2018-02-07 18:46:33 +0100 | [diff] [blame] | 198 | NetEqLifetimeStatistics NetEqTest::LifetimeStats() const { |
| 199 | return neteq_->GetLifetimeStatistics(); |
| 200 | } |
| 201 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 202 | NetEqTest::DecoderMap NetEqTest::StandardDecoderMap() { |
| 203 | DecoderMap codecs = { |
| 204 | {0, std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu")}, |
| 205 | {8, std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma")}, |
| 206 | #ifdef WEBRTC_CODEC_ILBC |
| 207 | {102, std::make_pair(NetEqDecoder::kDecoderILBC, "ilbc")}, |
| 208 | #endif |
| 209 | {103, std::make_pair(NetEqDecoder::kDecoderISAC, "isac")}, |
| 210 | #if !defined(WEBRTC_ANDROID) |
| 211 | {104, std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb")}, |
| 212 | #endif |
| 213 | #ifdef WEBRTC_CODEC_OPUS |
| 214 | {111, std::make_pair(NetEqDecoder::kDecoderOpus, "opus")}, |
| 215 | #endif |
| 216 | {93, std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb")}, |
| 217 | {94, std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb")}, |
| 218 | {95, std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32")}, |
| 219 | {96, std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48")}, |
| 220 | {9, std::make_pair(NetEqDecoder::kDecoderG722, "g722")}, |
| 221 | {106, std::make_pair(NetEqDecoder::kDecoderAVT, "avt")}, |
| 222 | {114, std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16")}, |
| 223 | {115, std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32")}, |
| 224 | {116, std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48")}, |
| 225 | {117, std::make_pair(NetEqDecoder::kDecoderRED, "red")}, |
| 226 | {13, std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb")}, |
| 227 | {98, std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb")}, |
| 228 | {99, std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32")}, |
| 229 | {100, std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48")} |
| 230 | }; |
| 231 | return codecs; |
| 232 | } |
| 233 | |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 234 | void NetEqTest::RegisterDecoders(const DecoderMap& codecs) { |
| 235 | for (const auto& c : codecs) { |
| 236 | RTC_CHECK_EQ( |
| 237 | neteq_->RegisterPayloadType(c.second.first, c.second.second, c.first), |
| 238 | NetEq::kOK) |
| 239 | << "Cannot register " << c.second.second << " to payload type " |
| 240 | << c.first; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void NetEqTest::RegisterExternalDecoders(const ExtDecoderMap& codecs) { |
| 245 | for (const auto& c : codecs) { |
| 246 | RTC_CHECK_EQ( |
| 247 | neteq_->RegisterExternalDecoder(c.second.decoder, c.second.codec, |
| 248 | c.second.codec_name, c.first), |
| 249 | NetEq::kOK) |
| 250 | << "Cannot register " << c.second.codec_name << " to payload type " |
| 251 | << c.first; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | } // namespace test |
| 256 | } // namespace webrtc |