neteq_input.h 3.7 KB

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