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 <fstream>
15 #include <map>
16 #include <memory>
17 #include <string>
18 #include <utility>
19 
20 #include "absl/types/optional.h"
21 #include "api/audio_codecs/audio_decoder_factory.h"
22 #include "api/neteq/neteq.h"
23 #include "api/neteq/neteq_factory.h"
24 #include "api/test/neteq_simulator.h"
25 #include "modules/audio_coding/neteq/tools/audio_sink.h"
26 #include "modules/audio_coding/neteq/tools/neteq_input.h"
27 #include "system_wrappers/include/clock.h"
28 
29 namespace webrtc {
30 namespace test {
31 
32 class NetEqTestErrorCallback {
33  public:
34   virtual ~NetEqTestErrorCallback() = default;
OnInsertPacketError(const NetEqInput::PacketData & packet)35   virtual void OnInsertPacketError(const NetEqInput::PacketData& packet) {}
OnGetAudioError()36   virtual void OnGetAudioError() {}
37 };
38 
39 class DefaultNetEqTestErrorCallback : public NetEqTestErrorCallback {
40   void OnInsertPacketError(const NetEqInput::PacketData& packet) override;
41   void OnGetAudioError() override;
42 };
43 
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 
61 class NetEqSimulationEndedCallback {
62  public:
63   virtual ~NetEqSimulationEndedCallback() = default;
64   virtual void SimulationEnded(int64_t simulation_time_ms, NetEq* neteq) = 0;
65 };
66 
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.
70 class NetEqTest : public NetEqSimulator {
71  public:
72   using DecoderMap = std::map<int, SdpAudioFormat>;
73 
74   struct Callbacks {
75     NetEqTestErrorCallback* error_callback = nullptr;
76     NetEqPostInsertPacket* post_insert_packet = nullptr;
77     NetEqGetAudioCallback* get_audio_callback = nullptr;
78     NetEqSimulationEndedCallback* simulation_ended_callback = nullptr;
79   };
80 
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,
84             rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
85             const DecoderMap& codecs,
86             std::unique_ptr<std::ofstream> text_log,
87             NetEqFactory* neteq_factory,
88             std::unique_ptr<NetEqInput> input,
89             std::unique_ptr<AudioSink> output,
90             Callbacks callbacks);
91 
92   ~NetEqTest() override;
93 
94   // Runs the test. Returns the duration of the produced audio in ms.
95   int64_t Run() override;
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;
103 
104   // Returns the statistics from NetEq.
105   NetEqNetworkStatistics SimulationStats();
106   NetEqLifetimeStatistics LifetimeStats() const;
107 
108   static DecoderMap StandardDecoderMap();
109 
110  private:
111   void RegisterDecoders(const DecoderMap& codecs);
112   SimulatedClock clock_;
113   absl::optional<Action> next_action_;
114   absl::optional<int> last_packet_time_ms_;
115   std::unique_ptr<NetEq> neteq_;
116   std::unique_ptr<NetEqInput> input_;
117   std::unique_ptr<AudioSink> output_;
118   Callbacks callbacks_;
119   int sample_rate_hz_;
120   NetEqState current_state_;
121   NetEqOperationsAndState prev_ops_state_;
122   NetEqLifetimeStatistics prev_lifetime_stats_;
123   absl::optional<uint32_t> last_packet_timestamp_;
124   std::unique_ptr<std::ofstream> text_log_;
125 };
126 
127 }  // namespace test
128 }  // namespace webrtc
129 #endif  // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_TEST_H_
130