blob: 732b8070f64732503c3747de64047aca3f21f4bc [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 Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/neteq/tools/packet.h"
20#include "modules/audio_coding/neteq/tools/packet_source.h"
21#include "rtc_base/buffer.h"
henrik.lundine8a77e32016-06-22 06:34:03 -070022
23namespace webrtc {
24namespace test {
25
26// Interface class for input to the NetEqTest class.
27class NetEqInput {
28 public:
29 struct PacketData {
Mirko Bonadei682aac52018-07-20 13:59:20 +020030 PacketData();
31 ~PacketData();
henrik.lundin7a38fd22017-04-28 01:35:53 -070032 std::string ToString() const;
33
henrik.lundin246ef3e2017-04-24 09:14:32 -070034 RTPHeader header;
henrik.lundine8a77e32016-06-22 06:34:03 -070035 rtc::Buffer payload;
Minyue Li27e2b7d2018-05-07 15:20:24 +020036 int64_t time_ms;
henrik.lundine8a77e32016-06-22 06:34:03 -070037 };
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.
Danil Chapovalovb6021232018-06-19 13:26:36 +020043 virtual absl::optional<int64_t> NextPacketTime() const = 0;
henrik.lundine8a77e32016-06-22 06:34:03 -070044
45 // Returns at what time (in ms) NetEq::GetAudio should be called next, or
46 // empty if no more output events are available.
Danil Chapovalovb6021232018-06-19 13:26:36 +020047 virtual absl::optional<int64_t> NextOutputEventTime() const = 0;
henrik.lundine8a77e32016-06-22 06:34:03 -070048
49 // Returns the time (in ms) for the next event from either NextPacketTime()
50 // or NextOutputEventTime(), or empty if both are out of events.
Danil Chapovalovb6021232018-06-19 13:26:36 +020051 absl::optional<int64_t> NextEventTime() const {
henrik.lundine8a77e32016-06-22 06:34:03 -070052 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) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010056 return b ? std::min(*a, *b) : a;
henrik.lundine8a77e32016-06-22 06:34:03 -070057 }
Danil Chapovalovb6021232018-06-19 13:26:36 +020058 return b ? b : absl::nullopt;
henrik.lundine8a77e32016-06-22 06:34:03 -070059 }
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
henrik.lundin58466f62016-10-05 02:27:42 -070072 // 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.
henrik.lundine8a77e32016-06-22 06:34:03 -070075 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().
Danil Chapovalovb6021232018-06-19 13:26:36 +020079 virtual absl::optional<RTPHeader> NextHeader() const = 0;
henrik.lundine8a77e32016-06-22 06:34:03 -070080};
81
Henrik Lundin7687ad52018-07-02 10:14:46 +020082// 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.
85class TimeLimitedNetEqInput : public NetEqInput {
86 public:
87 TimeLimitedNetEqInput(std::unique_ptr<NetEqInput> input, int64_t duration_ms);
Mirko Bonadei682aac52018-07-20 13:59:20 +020088 ~TimeLimitedNetEqInput() override;
Danil Chapovalov065a52a2018-07-09 10:58:54 +020089 absl::optional<int64_t> NextPacketTime() const override;
90 absl::optional<int64_t> NextOutputEventTime() const override;
Henrik Lundin7687ad52018-07-02 10:14:46 +020091 std::unique_ptr<PacketData> PopPacket() override;
92 void AdvanceOutputEvent() override;
93 bool ended() const override;
Danil Chapovalov065a52a2018-07-09 10:58:54 +020094 absl::optional<RTPHeader> NextHeader() const override;
Henrik Lundin7687ad52018-07-02 10:14:46 +020095
96 private:
97 void MaybeSetEnded();
98
99 std::unique_ptr<NetEqInput> input_;
Danil Chapovalov065a52a2018-07-09 10:58:54 +0200100 const absl::optional<int64_t> start_time_ms_;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200101 const int64_t duration_ms_;
102 bool ended_ = false;
103};
104
henrik.lundine8a77e32016-06-22 06:34:03 -0700105} // namespace test
106} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200107#endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_INPUT_H_