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