neteq_input.h (4857B)
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_INPUT_H_ 12 #define MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_INPUT_H_ 13 14 #include <algorithm> 15 #include <cstdint> 16 #include <memory> 17 #include <optional> 18 #include <string> 19 20 #include "modules/rtp_rtcp/source/rtp_packet_received.h" 21 22 namespace webrtc { 23 namespace test { 24 25 // Interface class for input to the NetEqTest class. 26 class NetEqInput { 27 public: 28 struct SetMinimumDelayInfo { 29 SetMinimumDelayInfo(int64_t timestamp_ms_in, int delay_ms_in) 30 : timestamp_ms(timestamp_ms_in), delay_ms(delay_ms_in) {} 31 int64_t timestamp_ms; 32 int delay_ms; 33 }; 34 35 static std::string ToString(const RtpPacketReceived& packet); 36 37 virtual ~NetEqInput() = default; 38 39 // Returns at what time (in ms) NetEq::InsertPacket should be called next, or 40 // empty if the source is out of packets. 41 virtual std::optional<int64_t> NextPacketTime() const = 0; 42 43 // Returns at what time (in ms) NetEq::GetAudio should be called next, or 44 // empty if no more output events are available. 45 virtual std::optional<int64_t> NextOutputEventTime() const = 0; 46 47 // Returns the information related to the next NetEq set minimum delay event 48 // if available. 49 virtual std::optional<SetMinimumDelayInfo> NextSetMinimumDelayInfo() 50 const = 0; 51 52 // Returns the time (in ms) for the next event (packet, output or set minimum 53 // delay event) or empty if there are no more events. 54 std::optional<int64_t> NextEventTime() const { 55 std::optional<int64_t> next_event_time = NextPacketTime(); 56 const auto next_output_time = NextOutputEventTime(); 57 // Return the minimum of non-empty `a` and `b`, or empty if both are empty. 58 if (next_output_time) { 59 next_event_time = next_event_time ? std::min(next_event_time.value(), 60 next_output_time.value()) 61 : next_output_time; 62 } 63 const auto next_neteq_minimum_delay = NextSetMinimumDelayInfo(); 64 if (next_neteq_minimum_delay) { 65 next_event_time = 66 next_event_time 67 ? std::min(next_event_time.value(), 68 next_neteq_minimum_delay.value().timestamp_ms) 69 : next_neteq_minimum_delay.value().timestamp_ms; 70 } 71 return next_event_time; 72 } 73 74 // Returns the next packet to be inserted into NetEq. The packet following the 75 // returned one is pre-fetched in the NetEqInput object, such that future 76 // calls to NextPacketTime() or NextPacket() will return information from that 77 // packet. 78 virtual std::unique_ptr<RtpPacketReceived> PopPacket() = 0; 79 80 // Move to the next output event. This will make NextOutputEventTime() return 81 // a new value (potentially the same if several output events share the same 82 // time). 83 virtual void AdvanceOutputEvent() = 0; 84 85 // Move to the next NetEq set minimum delay. This will make 86 // `NextSetMinimumDelayInfo` return a new value. 87 virtual void AdvanceSetMinimumDelay() = 0; 88 89 // Returns true if the source has come to an end. An implementation must 90 // eventually return true from this method, or the test will end up in an 91 // infinite loop. 92 virtual bool ended() const = 0; 93 94 // Returns the next RTP packet, i.e., the packet that will be delivered next 95 // by PopPacket(). 96 virtual const RtpPacketReceived* NextPacket() const = 0; 97 }; 98 99 // Wrapper class to impose a time limit on a NetEqInput object, typically 100 // another time limit than what the object itself provides. For example, an 101 // input taken from a file can be cut shorter by wrapping it in this class. 102 class TimeLimitedNetEqInput : public NetEqInput { 103 public: 104 TimeLimitedNetEqInput(std::unique_ptr<NetEqInput> input, int64_t duration_ms); 105 ~TimeLimitedNetEqInput() override; 106 std::optional<int64_t> NextPacketTime() const override; 107 std::optional<int64_t> NextOutputEventTime() const override; 108 std::optional<SetMinimumDelayInfo> NextSetMinimumDelayInfo() const override; 109 std::unique_ptr<RtpPacketReceived> PopPacket() override; 110 void AdvanceOutputEvent() override; 111 void AdvanceSetMinimumDelay() override; 112 bool ended() const override; 113 const RtpPacketReceived* NextPacket() const override; 114 115 private: 116 void MaybeSetEnded(); 117 118 std::unique_ptr<NetEqInput> input_; 119 const std::optional<int64_t> start_time_ms_; 120 const int64_t duration_ms_; 121 bool ended_ = false; 122 }; 123 124 } // namespace test 125 } // namespace webrtc 126 #endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_INPUT_H_