• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
16 #include <string>
17 
18 #include "absl/types/optional.h"
19 #include "modules/audio_coding/neteq/tools/packet.h"
20 #include "modules/audio_coding/neteq/tools/packet_source.h"
21 #include "rtc_base/buffer.h"
22 
23 namespace webrtc {
24 namespace test {
25 
26 // Interface class for input to the NetEqTest class.
27 class NetEqInput {
28  public:
29   struct PacketData {
30     PacketData();
31     ~PacketData();
32     std::string ToString() const;
33 
34     RTPHeader header;
35     rtc::Buffer payload;
36     int64_t time_ms;
37   };
38 
39   virtual ~NetEqInput() = default;
40 
41   // Returns at what time (in ms) NetEq::InsertPacket should be called next, or
42   // empty if the source is out of packets.
43   virtual absl::optional<int64_t> NextPacketTime() const = 0;
44 
45   // Returns at what time (in ms) NetEq::GetAudio should be called next, or
46   // empty if no more output events are available.
47   virtual absl::optional<int64_t> NextOutputEventTime() const = 0;
48 
49   // Returns the time (in ms) for the next event from either NextPacketTime()
50   // or NextOutputEventTime(), or empty if both are out of events.
NextEventTime()51   absl::optional<int64_t> NextEventTime() const {
52     const auto a = NextPacketTime();
53     const auto b = NextOutputEventTime();
54     // Return the minimum of non-empty `a` and `b`, or empty if both are empty.
55     if (a) {
56       return b ? std::min(*a, *b) : a;
57     }
58     return b ? b : absl::nullopt;
59   }
60 
61   // Returns the next packet to be inserted into NetEq. The packet following the
62   // returned one is pre-fetched in the NetEqInput object, such that future
63   // calls to NextPacketTime() or NextHeader() will return information from that
64   // packet.
65   virtual std::unique_ptr<PacketData> PopPacket() = 0;
66 
67   // Move to the next output event. This will make NextOutputEventTime() return
68   // a new value (potentially the same if several output events share the same
69   // time).
70   virtual void AdvanceOutputEvent() = 0;
71 
72   // Returns true if the source has come to an end. An implementation must
73   // eventually return true from this method, or the test will end up in an
74   // infinite loop.
75   virtual bool ended() const = 0;
76 
77   // Returns the RTP header for the next packet, i.e., the packet that will be
78   // delivered next by PopPacket().
79   virtual absl::optional<RTPHeader> NextHeader() const = 0;
80 };
81 
82 // Wrapper class to impose a time limit on a NetEqInput object, typically
83 // another time limit than what the object itself provides. For example, an
84 // input taken from a file can be cut shorter by wrapping it in this class.
85 class TimeLimitedNetEqInput : public NetEqInput {
86  public:
87   TimeLimitedNetEqInput(std::unique_ptr<NetEqInput> input, int64_t duration_ms);
88   ~TimeLimitedNetEqInput() override;
89   absl::optional<int64_t> NextPacketTime() const override;
90   absl::optional<int64_t> NextOutputEventTime() const override;
91   std::unique_ptr<PacketData> PopPacket() override;
92   void AdvanceOutputEvent() override;
93   bool ended() const override;
94   absl::optional<RTPHeader> NextHeader() const override;
95 
96  private:
97   void MaybeSetEnded();
98 
99   std::unique_ptr<NetEqInput> input_;
100   const absl::optional<int64_t> start_time_ms_;
101   const int64_t duration_ms_;
102   bool ended_ = false;
103 };
104 
105 }  // namespace test
106 }  // namespace webrtc
107 #endif  // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_INPUT_H_
108