blob: b9cba098d861829ceb07870f8f9ad143c64aa7a5 [file] [log] [blame]
henrik.lundine8a77e32016-06-22 06:34:03 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_INPUT_H_
12#define MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_INPUT_H_
henrik.lundine8a77e32016-06-22 06:34:03 -070013
14#include <algorithm>
15#include <memory>
henrik.lundin7a38fd22017-04-28 01:35:53 -070016#include <string>
henrik.lundine8a77e32016-06-22 06:34:03 -070017
Danil Chapovalovb6021232018-06-19 13:26:36 +020018#include "absl/types/optional.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020019#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/neteq/tools/packet.h"
21#include "modules/audio_coding/neteq/tools/packet_source.h"
22#include "rtc_base/buffer.h"
henrik.lundine8a77e32016-06-22 06:34:03 -070023
24namespace webrtc {
25namespace test {
26
27// Interface class for input to the NetEqTest class.
28class NetEqInput {
29 public:
30 struct PacketData {
Mirko Bonadei682aac52018-07-20 13:59:20 +020031 PacketData();
32 ~PacketData();
henrik.lundin7a38fd22017-04-28 01:35:53 -070033 std::string ToString() const;
34
henrik.lundin246ef3e2017-04-24 09:14:32 -070035 RTPHeader header;
henrik.lundine8a77e32016-06-22 06:34:03 -070036 rtc::Buffer payload;
Minyue Li27e2b7d2018-05-07 15:20:24 +020037 int64_t time_ms;
henrik.lundine8a77e32016-06-22 06:34:03 -070038 };
39
40 virtual ~NetEqInput() = default;
41
42 // Returns at what time (in ms) NetEq::InsertPacket should be called next, or
43 // empty if the source is out of packets.
Danil Chapovalovb6021232018-06-19 13:26:36 +020044 virtual absl::optional<int64_t> NextPacketTime() const = 0;
henrik.lundine8a77e32016-06-22 06:34:03 -070045
46 // Returns at what time (in ms) NetEq::GetAudio should be called next, or
47 // empty if no more output events are available.
Danil Chapovalovb6021232018-06-19 13:26:36 +020048 virtual absl::optional<int64_t> NextOutputEventTime() const = 0;
henrik.lundine8a77e32016-06-22 06:34:03 -070049
50 // Returns the time (in ms) for the next event from either NextPacketTime()
51 // or NextOutputEventTime(), or empty if both are out of events.
Danil Chapovalovb6021232018-06-19 13:26:36 +020052 absl::optional<int64_t> NextEventTime() const {
henrik.lundine8a77e32016-06-22 06:34:03 -070053 const auto a = NextPacketTime();
54 const auto b = NextOutputEventTime();
55 // Return the minimum of non-empty |a| and |b|, or empty if both are empty.
56 if (a) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010057 return b ? std::min(*a, *b) : a;
henrik.lundine8a77e32016-06-22 06:34:03 -070058 }
Danil Chapovalovb6021232018-06-19 13:26:36 +020059 return b ? b : absl::nullopt;
henrik.lundine8a77e32016-06-22 06:34:03 -070060 }
61
62 // Returns the next packet to be inserted into NetEq. The packet following the
63 // returned one is pre-fetched in the NetEqInput object, such that future
64 // calls to NextPacketTime() or NextHeader() will return information from that
65 // packet.
66 virtual std::unique_ptr<PacketData> PopPacket() = 0;
67
68 // Move to the next output event. This will make NextOutputEventTime() return
69 // a new value (potentially the same if several output events share the same
70 // time).
71 virtual void AdvanceOutputEvent() = 0;
72
henrik.lundin58466f62016-10-05 02:27:42 -070073 // Returns true if the source has come to an end. An implementation must
74 // eventually return true from this method, or the test will end up in an
75 // infinite loop.
henrik.lundine8a77e32016-06-22 06:34:03 -070076 virtual bool ended() const = 0;
77
78 // Returns the RTP header for the next packet, i.e., the packet that will be
79 // delivered next by PopPacket().
Danil Chapovalovb6021232018-06-19 13:26:36 +020080 virtual absl::optional<RTPHeader> NextHeader() const = 0;
henrik.lundine8a77e32016-06-22 06:34:03 -070081};
82
Henrik Lundin7687ad52018-07-02 10:14:46 +020083// Wrapper class to impose a time limit on a NetEqInput object, typically
84// another time limit than what the object itself provides. For example, an
85// input taken from a file can be cut shorter by wrapping it in this class.
86class TimeLimitedNetEqInput : public NetEqInput {
87 public:
88 TimeLimitedNetEqInput(std::unique_ptr<NetEqInput> input, int64_t duration_ms);
Mirko Bonadei682aac52018-07-20 13:59:20 +020089 ~TimeLimitedNetEqInput() override;
Danil Chapovalov065a52a2018-07-09 10:58:54 +020090 absl::optional<int64_t> NextPacketTime() const override;
91 absl::optional<int64_t> NextOutputEventTime() const override;
Henrik Lundin7687ad52018-07-02 10:14:46 +020092 std::unique_ptr<PacketData> PopPacket() override;
93 void AdvanceOutputEvent() override;
94 bool ended() const override;
Danil Chapovalov065a52a2018-07-09 10:58:54 +020095 absl::optional<RTPHeader> NextHeader() const override;
Henrik Lundin7687ad52018-07-02 10:14:46 +020096
97 private:
98 void MaybeSetEnded();
99
100 std::unique_ptr<NetEqInput> input_;
Danil Chapovalov065a52a2018-07-09 10:58:54 +0200101 const absl::optional<int64_t> start_time_ms_;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200102 const int64_t duration_ms_;
103 bool ended_ = false;
104};
105
henrik.lundine8a77e32016-06-22 06:34:03 -0700106} // namespace test
107} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200108#endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_INPUT_H_