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 | #ifndef MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_ |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 13 | |
Ivo Creusen | 2db46b0 | 2018-12-14 16:49:12 +0100 | [diff] [blame] | 14 | #include <fstream> |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 15 | #include <map> |
| 16 | #include <memory> |
| 17 | #include <string> |
| 18 | #include <utility> |
| 19 | |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 20 | #include "absl/types/optional.h" |
Niels Möller | 3f651d8 | 2018-12-19 15:06:17 +0100 | [diff] [blame] | 21 | #include "api/audio_codecs/audio_decoder_factory.h" |
Ivo Creusen | 3ce44a3 | 2019-10-31 14:38:11 +0100 | [diff] [blame] | 22 | #include "api/neteq/neteq.h" |
Ivo Creusen | cee751a | 2020-01-16 17:17:09 +0100 | [diff] [blame] | 23 | #include "api/neteq/neteq_factory.h" |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 24 | #include "api/test/neteq_simulator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "modules/audio_coding/neteq/tools/audio_sink.h" |
| 26 | #include "modules/audio_coding/neteq/tools/neteq_input.h" |
Alessio Bazzica | 8f319a3 | 2019-07-24 16:47:02 +0000 | [diff] [blame] | 27 | #include "system_wrappers/include/clock.h" |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | namespace test { |
| 31 | |
| 32 | class NetEqTestErrorCallback { |
| 33 | public: |
| 34 | virtual ~NetEqTestErrorCallback() = default; |
Henrik Lundin | c417d9e | 2017-06-14 12:29:03 +0200 | [diff] [blame] | 35 | virtual void OnInsertPacketError(const NetEqInput::PacketData& packet) {} |
| 36 | virtual void OnGetAudioError() {} |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | class DefaultNetEqTestErrorCallback : public NetEqTestErrorCallback { |
Henrik Lundin | c417d9e | 2017-06-14 12:29:03 +0200 | [diff] [blame] | 40 | void OnInsertPacketError(const NetEqInput::PacketData& packet) override; |
| 41 | void OnGetAudioError() override; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 44 | class NetEqPostInsertPacket { |
| 45 | public: |
| 46 | virtual ~NetEqPostInsertPacket() = default; |
| 47 | virtual void AfterInsertPacket(const NetEqInput::PacketData& packet, |
| 48 | NetEq* neteq) = 0; |
| 49 | }; |
| 50 | |
| 51 | class NetEqGetAudioCallback { |
| 52 | public: |
| 53 | virtual ~NetEqGetAudioCallback() = default; |
| 54 | virtual void BeforeGetAudio(NetEq* neteq) = 0; |
| 55 | virtual void AfterGetAudio(int64_t time_now_ms, |
| 56 | const AudioFrame& audio_frame, |
| 57 | bool muted, |
| 58 | NetEq* neteq) = 0; |
| 59 | }; |
| 60 | |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 61 | class NetEqSimulationEndedCallback { |
| 62 | public: |
| 63 | virtual ~NetEqSimulationEndedCallback() = default; |
Henrik Lundin | c49e9c2 | 2020-05-25 11:26:15 +0200 | [diff] [blame] | 64 | virtual void SimulationEnded(int64_t simulation_time_ms, NetEq* neteq) = 0; |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 67 | // Class that provides an input--output test for NetEq. The input (both packets |
| 68 | // and output events) is provided by a NetEqInput object, while the output is |
| 69 | // directed to an AudioSink object. |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 70 | class NetEqTest : public NetEqSimulator { |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 71 | public: |
Niels Möller | 0554368 | 2019-01-10 16:55:06 +0100 | [diff] [blame] | 72 | using DecoderMap = std::map<int, SdpAudioFormat>; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 73 | |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 74 | struct Callbacks { |
| 75 | NetEqTestErrorCallback* error_callback = nullptr; |
| 76 | NetEqPostInsertPacket* post_insert_packet = nullptr; |
| 77 | NetEqGetAudioCallback* get_audio_callback = nullptr; |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 78 | NetEqSimulationEndedCallback* simulation_ended_callback = nullptr; |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 81 | // Sets up the test with given configuration, codec mappings, input, ouput, |
| 82 | // and callback objects for error reporting. |
| 83 | NetEqTest(const NetEq::Config& config, |
Niels Möller | 3f651d8 | 2018-12-19 15:06:17 +0100 | [diff] [blame] | 84 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory, |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 85 | const DecoderMap& codecs, |
Ivo Creusen | 2db46b0 | 2018-12-14 16:49:12 +0100 | [diff] [blame] | 86 | std::unique_ptr<std::ofstream> text_log, |
Ivo Creusen | cee751a | 2020-01-16 17:17:09 +0100 | [diff] [blame] | 87 | NetEqFactory* neteq_factory, |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 88 | std::unique_ptr<NetEqInput> input, |
| 89 | std::unique_ptr<AudioSink> output, |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 90 | Callbacks callbacks); |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 91 | |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 92 | ~NetEqTest() override; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 93 | |
| 94 | // Runs the test. Returns the duration of the produced audio in ms. |
Ivo Creusen | 182c2b8 | 2020-01-28 17:12:23 +0100 | [diff] [blame] | 95 | int64_t Run() override; |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 96 | // Runs the simulation until we hit the next GetAudio event. If the simulation |
| 97 | // is finished, is_simulation_finished will be set to true in the returned |
| 98 | // SimulationStepResult. |
| 99 | SimulationStepResult RunToNextGetAudio() override; |
| 100 | |
| 101 | void SetNextAction(Action next_operation) override; |
| 102 | NetEqState GetNetEqState() override; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 103 | |
| 104 | // Returns the statistics from NetEq. |
| 105 | NetEqNetworkStatistics SimulationStats(); |
Alex Narest | 7ff6ca5 | 2018-02-07 18:46:33 +0100 | [diff] [blame] | 106 | NetEqLifetimeStatistics LifetimeStats() const; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 107 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 108 | static DecoderMap StandardDecoderMap(); |
| 109 | |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 110 | private: |
| 111 | void RegisterDecoders(const DecoderMap& codecs); |
Alessio Bazzica | 8f319a3 | 2019-07-24 16:47:02 +0000 | [diff] [blame] | 112 | SimulatedClock clock_; |
Ivo Creusen | 55de08e | 2018-09-03 11:49:27 +0200 | [diff] [blame] | 113 | absl::optional<Action> next_action_; |
Ivo Creusen | 4384f53 | 2018-09-07 17:19:56 +0200 | [diff] [blame] | 114 | absl::optional<int> last_packet_time_ms_; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 115 | std::unique_ptr<NetEq> neteq_; |
| 116 | std::unique_ptr<NetEqInput> input_; |
| 117 | std::unique_ptr<AudioSink> output_; |
henrik.lundin | 02739d9 | 2017-05-04 06:09:06 -0700 | [diff] [blame] | 118 | Callbacks callbacks_; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 119 | int sample_rate_hz_; |
Ivo Creusen | 4384f53 | 2018-09-07 17:19:56 +0200 | [diff] [blame] | 120 | NetEqState current_state_; |
Ivo Creusen | d1c2f78 | 2018-09-13 14:39:55 +0200 | [diff] [blame] | 121 | NetEqOperationsAndState prev_ops_state_; |
Ivo Creusen | 2db46b0 | 2018-12-14 16:49:12 +0100 | [diff] [blame] | 122 | NetEqLifetimeStatistics prev_lifetime_stats_; |
| 123 | absl::optional<uint32_t> last_packet_timestamp_; |
| 124 | std::unique_ptr<std::ofstream> text_log_; |
henrik.lundin | e8a77e3 | 2016-06-22 06:34:03 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | } // namespace test |
| 128 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 129 | #endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_ |