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