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; |
| 88 | |
| 89 | while (!input_->ended()) { |
| 90 | // Advance time to next event. |
| 91 | RTC_DCHECK(input_->NextEventTime()); |
| 92 | time_now_ms = *input_->NextEventTime(); |
| 93 | // Check if it is time to insert packet. |
| 94 | if (input_->NextPacketTime() && time_now_ms >= *input_->NextPacketTime()) { |
| 95 | std::unique_ptr<NetEqInput::PacketData> packet_data = input_->PopPacket(); |
| 96 | RTC_CHECK(packet_data); |
| 97 | int error = neteq_->InsertPacket( |
henrik.lundin | 246ef3e | 2017-04-24 09:14:32 -0700 | [diff] [blame] | 98 | packet_data->header, |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 99 | rtc::ArrayView<const uint8_t>(packet_data->payload), |
| 100 | static_cast<uint32_t>(packet_data->time_ms * sample_rate_hz_ / 1000)); |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 101 | if (error != NetEq::kOK && callbacks_.error_callback) { |
Henrik Lundin | c417d9e | 2017-06-14 12:29:03 +0200 | [diff] [blame] | 102 | callbacks_.error_callback->OnInsertPacketError(*packet_data); |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 103 | } |
| 104 | if (callbacks_.post_insert_packet) { |
| 105 | callbacks_.post_insert_packet->AfterInsertPacket(*packet_data, |
| 106 | neteq_.get()); |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | // Check if it is time to get output audio. |
| 111 | if (input_->NextOutputEventTime() && |
| 112 | time_now_ms >= *input_->NextOutputEventTime()) { |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 113 | if (callbacks_.get_audio_callback) { |
| 114 | callbacks_.get_audio_callback->BeforeGetAudio(neteq_.get()); |
| 115 | } |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 116 | AudioFrame out_frame; |
| 117 | bool muted; |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 118 | int error = neteq_->GetAudio(&out_frame, &muted, |
| 119 | ActionToOperations(next_action_)); |
| 120 | next_action_ = absl::nullopt; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 121 | RTC_CHECK(!muted) << "The code does not handle enable_muted_state"; |
| 122 | if (error != NetEq::kOK) { |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 123 | if (callbacks_.error_callback) { |
Henrik Lundin | c417d9e | 2017-06-14 12:29:03 +0200 | [diff] [blame] | 124 | callbacks_.error_callback->OnGetAudioError(); |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 125 | } |
| 126 | } else { |
| 127 | sample_rate_hz_ = out_frame.sample_rate_hz_; |
| 128 | } |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 129 | if (callbacks_.get_audio_callback) { |
| 130 | callbacks_.get_audio_callback->AfterGetAudio(time_now_ms, out_frame, |
| 131 | muted, neteq_.get()); |
| 132 | } |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 133 | |
| 134 | if (output_) { |
| 135 | RTC_CHECK(output_->WriteArray( |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 136 | out_frame.data(), |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 137 | out_frame.samples_per_channel_ * out_frame.num_channels_)); |
| 138 | } |
| 139 | |
| 140 | input_->AdvanceOutputEvent(); |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 141 | result.simulation_step_ms = time_now_ms - start_time_ms; |
| 142 | // TODO(ivoc): Set the result.<action>_ms values correctly. |
| 143 | result.is_simulation_finished = input_->ended(); |
| 144 | return result; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 147 | result.simulation_step_ms = time_now_ms - start_time_ms; |
| 148 | result.is_simulation_finished = true; |
| 149 | return result; |
| 150 | } |
| 151 | |
| 152 | void NetEqTest::SetNextAction(NetEqTest::Action next_operation) { |
| 153 | next_action_ = absl::optional<Action>(next_operation); |
| 154 | } |
| 155 | |
| 156 | NetEqTest::NetEqState NetEqTest::GetNetEqState() { |
| 157 | NetEqState state; |
| 158 | const auto network_stats = SimulationStats(); |
| 159 | state.current_delay_ms = network_stats.current_buffer_size_ms; |
| 160 | return state; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | NetEqNetworkStatistics NetEqTest::SimulationStats() { |
| 164 | NetEqNetworkStatistics stats; |
| 165 | RTC_CHECK_EQ(neteq_->NetworkStatistics(&stats), 0); |
| 166 | return stats; |
| 167 | } |
| 168 | |
Alex Narest | 7ff6ca5 | 2018-02-07 18:46:33 +0100 | [diff] [blame] | 169 | NetEqLifetimeStatistics NetEqTest::LifetimeStats() const { |
| 170 | return neteq_->GetLifetimeStatistics(); |
| 171 | } |
| 172 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 173 | NetEqTest::DecoderMap NetEqTest::StandardDecoderMap() { |
| 174 | DecoderMap codecs = { |
| 175 | {0, std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu")}, |
| 176 | {8, std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma")}, |
| 177 | #ifdef WEBRTC_CODEC_ILBC |
| 178 | {102, std::make_pair(NetEqDecoder::kDecoderILBC, "ilbc")}, |
| 179 | #endif |
| 180 | {103, std::make_pair(NetEqDecoder::kDecoderISAC, "isac")}, |
| 181 | #if !defined(WEBRTC_ANDROID) |
| 182 | {104, std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb")}, |
| 183 | #endif |
| 184 | #ifdef WEBRTC_CODEC_OPUS |
| 185 | {111, std::make_pair(NetEqDecoder::kDecoderOpus, "opus")}, |
| 186 | #endif |
| 187 | {93, std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb")}, |
| 188 | {94, std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb")}, |
| 189 | {95, std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32")}, |
| 190 | {96, std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48")}, |
| 191 | {9, std::make_pair(NetEqDecoder::kDecoderG722, "g722")}, |
| 192 | {106, std::make_pair(NetEqDecoder::kDecoderAVT, "avt")}, |
| 193 | {114, std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16")}, |
| 194 | {115, std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32")}, |
| 195 | {116, std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48")}, |
| 196 | {117, std::make_pair(NetEqDecoder::kDecoderRED, "red")}, |
| 197 | {13, std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb")}, |
| 198 | {98, std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb")}, |
| 199 | {99, std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32")}, |
| 200 | {100, std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48")} |
| 201 | }; |
| 202 | return codecs; |
| 203 | } |
| 204 | |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 205 | void NetEqTest::RegisterDecoders(const DecoderMap& codecs) { |
| 206 | for (const auto& c : codecs) { |
| 207 | RTC_CHECK_EQ( |
| 208 | neteq_->RegisterPayloadType(c.second.first, c.second.second, c.first), |
| 209 | NetEq::kOK) |
| 210 | << "Cannot register " << c.second.second << " to payload type " |
| 211 | << c.first; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void NetEqTest::RegisterExternalDecoders(const ExtDecoderMap& codecs) { |
| 216 | for (const auto& c : codecs) { |
| 217 | RTC_CHECK_EQ( |
| 218 | neteq_->RegisterExternalDecoder(c.second.decoder, c.second.codec, |
| 219 | c.second.codec_name, c.first), |
| 220 | NetEq::kOK) |
| 221 | << "Cannot register " << c.second.codec_name << " to payload type " |
| 222 | << c.first; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | } // namespace test |
| 227 | } // namespace webrtc |