blob: 16915853d19a6fd89d0afb3c1c623a49bc2ea58e [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 {
henrik.lundin7a38fd22017-04-28 01:35:53 -070031 std::string ToString() const;
32
henrik.lundin246ef3e2017-04-24 09:14:32 -070033 RTPHeader header;
henrik.lundine8a77e32016-06-22 06:34:03 -070034 rtc::Buffer payload;
Minyue Li27e2b7d2018-05-07 15:20:24 +020035 int64_t time_ms;
henrik.lundine8a77e32016-06-22 06:34:03 -070036 };
37
38 virtual ~NetEqInput() = default;
39
40 // Returns at what time (in ms) NetEq::InsertPacket should be called next, or
41 // empty if the source is out of packets.
Danil Chapovalovb6021232018-06-19 13:26:36 +020042 virtual absl::optional<int64_t> NextPacketTime() const = 0;
henrik.lundine8a77e32016-06-22 06:34:03 -070043
44 // Returns at what time (in ms) NetEq::GetAudio should be called next, or
45 // empty if no more output events are available.
Danil Chapovalovb6021232018-06-19 13:26:36 +020046 virtual absl::optional<int64_t> NextOutputEventTime() const = 0;
henrik.lundine8a77e32016-06-22 06:34:03 -070047
48 // Returns the time (in ms) for the next event from either NextPacketTime()
49 // or NextOutputEventTime(), or empty if both are out of events.
Danil Chapovalovb6021232018-06-19 13:26:36 +020050 absl::optional<int64_t> NextEventTime() const {
henrik.lundine8a77e32016-06-22 06:34:03 -070051 const auto a = NextPacketTime();
52 const auto b = NextOutputEventTime();
53 // Return the minimum of non-empty |a| and |b|, or empty if both are empty.
54 if (a) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +010055 return b ? std::min(*a, *b) : a;
henrik.lundine8a77e32016-06-22 06:34:03 -070056 }
Danil Chapovalovb6021232018-06-19 13:26:36 +020057 return b ? b : absl::nullopt;
henrik.lundine8a77e32016-06-22 06:34:03 -070058 }
59
60 // Returns the next packet to be inserted into NetEq. The packet following the
61 // returned one is pre-fetched in the NetEqInput object, such that future
62 // calls to NextPacketTime() or NextHeader() will return information from that
63 // packet.
64 virtual std::unique_ptr<PacketData> PopPacket() = 0;
65
66 // Move to the next output event. This will make NextOutputEventTime() return
67 // a new value (potentially the same if several output events share the same
68 // time).
69 virtual void AdvanceOutputEvent() = 0;
70
henrik.lundin58466f62016-10-05 02:27:42 -070071 // Returns true if the source has come to an end. An implementation must
72 // eventually return true from this method, or the test will end up in an
73 // infinite loop.
henrik.lundine8a77e32016-06-22 06:34:03 -070074 virtual bool ended() const = 0;
75
76 // Returns the RTP header for the next packet, i.e., the packet that will be
77 // delivered next by PopPacket().
Danil Chapovalovb6021232018-06-19 13:26:36 +020078 virtual absl::optional<RTPHeader> NextHeader() const = 0;
henrik.lundine8a77e32016-06-22 06:34:03 -070079};
80
Henrik Lundin7687ad52018-07-02 10:14:46 +020081// Wrapper class to impose a time limit on a NetEqInput object, typically
82// another time limit than what the object itself provides. For example, an
83// input taken from a file can be cut shorter by wrapping it in this class.
84class TimeLimitedNetEqInput : public NetEqInput {
85 public:
86 TimeLimitedNetEqInput(std::unique_ptr<NetEqInput> input, int64_t duration_ms);
Danil Chapovalov065a52a2018-07-09 10:58:54 +020087 absl::optional<int64_t> NextPacketTime() const override;
88 absl::optional<int64_t> NextOutputEventTime() const override;
Henrik Lundin7687ad52018-07-02 10:14:46 +020089 std::unique_ptr<PacketData> PopPacket() override;
90 void AdvanceOutputEvent() override;
91 bool ended() const override;
Danil Chapovalov065a52a2018-07-09 10:58:54 +020092 absl::optional<RTPHeader> NextHeader() const override;
Henrik Lundin7687ad52018-07-02 10:14:46 +020093
94 private:
95 void MaybeSetEnded();
96
97 std::unique_ptr<NetEqInput> input_;
Danil Chapovalov065a52a2018-07-09 10:58:54 +020098 const absl::optional<int64_t> start_time_ms_;
Henrik Lundin7687ad52018-07-02 10:14:46 +020099 const int64_t duration_ms_;
100 bool ended_ = false;
101};
102
henrik.lundine8a77e32016-06-22 06:34:03 -0700103} // namespace test
104} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200105#endif // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_INPUT_H_