neteq_test.h (4732B)
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 11 #ifndef MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_ 12 #define MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_ 13 14 #include <cstdint> 15 #include <fstream> 16 #include <map> 17 #include <memory> 18 #include <optional> 19 20 #include "absl/strings/string_view.h" 21 #include "api/audio_codecs/audio_decoder_factory.h" 22 #include "api/audio_codecs/audio_format.h" 23 #include "api/environment/environment.h" 24 #include "api/neteq/neteq.h" 25 #include "api/neteq/neteq_factory.h" 26 #include "api/scoped_refptr.h" 27 #include "api/test/neteq_simulator.h" 28 #include "modules/audio_coding/neteq/tools/audio_sink.h" 29 #include "modules/audio_coding/neteq/tools/neteq_input.h" 30 #include "modules/rtp_rtcp/source/rtp_packet_received.h" 31 #include "system_wrappers/include/clock.h" 32 33 namespace webrtc { 34 namespace test { 35 36 class NetEqTestErrorCallback { 37 public: 38 virtual ~NetEqTestErrorCallback() = default; 39 virtual void OnInsertPacketError(const RtpPacketReceived& /* packet */) {} 40 virtual void OnGetAudioError() {} 41 }; 42 43 class DefaultNetEqTestErrorCallback : public NetEqTestErrorCallback { 44 void OnInsertPacketError(const RtpPacketReceived& packet) override; 45 void OnGetAudioError() override; 46 }; 47 48 class NetEqPostInsertPacket { 49 public: 50 virtual ~NetEqPostInsertPacket() = default; 51 virtual void AfterInsertPacket(const RtpPacketReceived& packet, 52 NetEq* neteq) = 0; 53 }; 54 55 class NetEqGetAudioCallback { 56 public: 57 virtual ~NetEqGetAudioCallback() = default; 58 virtual void BeforeGetAudio(NetEq* neteq) = 0; 59 virtual void AfterGetAudio(int64_t time_now_ms, 60 const AudioFrame& audio_frame, 61 bool muted, 62 NetEq* neteq) = 0; 63 }; 64 65 class NetEqSimulationEndedCallback { 66 public: 67 virtual ~NetEqSimulationEndedCallback() = default; 68 virtual void SimulationEnded(int64_t simulation_time_ms) = 0; 69 }; 70 71 // Class that provides an input--output test for NetEq. The input (both packets 72 // and output events) is provided by a NetEqInput object, while the output is 73 // directed to an AudioSink object. 74 class NetEqTest : public NetEqSimulator { 75 public: 76 using DecoderMap = std::map<int, SdpAudioFormat>; 77 78 struct Callbacks { 79 NetEqTestErrorCallback* error_callback = nullptr; 80 NetEqPostInsertPacket* post_insert_packet = nullptr; 81 NetEqGetAudioCallback* get_audio_callback = nullptr; 82 NetEqSimulationEndedCallback* simulation_ended_callback = nullptr; 83 }; 84 85 // Sets up the test with given configuration, codec mappings, input, ouput, 86 // and callback objects for error reporting. 87 NetEqTest(const NetEq::Config& config, 88 scoped_refptr<AudioDecoderFactory> decoder_factory, 89 const DecoderMap& codecs, 90 std::unique_ptr<std::ofstream> text_log, 91 NetEqFactory* neteq_factory, 92 std::unique_ptr<NetEqInput> input, 93 std::unique_ptr<AudioSink> output, 94 Callbacks callbacks, 95 absl::string_view field_trials = ""); 96 97 ~NetEqTest() override; 98 99 // Runs the test. Returns the duration of the produced audio in ms. 100 int64_t Run() override; 101 // Runs the simulation until we hit the next GetAudio event. If the simulation 102 // is finished, is_simulation_finished will be set to true in the returned 103 // SimulationStepResult. 104 SimulationStepResult RunToNextGetAudio() override; 105 106 void SetNextAction(Action next_operation) override; 107 NetEqState GetNetEqState() override; 108 NetEq* GetNetEq() override { return neteq_.get(); } 109 110 // Returns the statistics from NetEq. 111 NetEqNetworkStatistics SimulationStats(); 112 NetEqLifetimeStatistics LifetimeStats() const; 113 114 static DecoderMap StandardDecoderMap(); 115 116 private: 117 void RegisterDecoders(const DecoderMap& codecs); 118 std::unique_ptr<NetEqInput> input_; 119 SimulatedClock clock_; 120 const Environment env_; 121 std::optional<Action> next_action_; 122 std::optional<int> last_packet_time_ms_; 123 std::unique_ptr<NetEq> neteq_; 124 std::unique_ptr<AudioSink> output_; 125 Callbacks callbacks_; 126 int sample_rate_hz_; 127 NetEqState current_state_; 128 NetEqOperationsAndState prev_ops_state_; 129 NetEqLifetimeStatistics prev_lifetime_stats_; 130 std::optional<uint32_t> last_packet_timestamp_; 131 std::unique_ptr<std::ofstream> text_log_; 132 }; 133 134 } // namespace test 135 } // namespace webrtc 136 #endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_