fec_test_helper.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2012 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_RTP_RTCP_SOURCE_FEC_TEST_HELPER_H_
  11. #define MODULES_RTP_RTCP_SOURCE_FEC_TEST_HELPER_H_
  12. #include <memory>
  13. #include "modules/rtp_rtcp/source/forward_error_correction.h"
  14. #include "modules/rtp_rtcp/source/rtp_packet_received.h"
  15. #include "rtc_base/random.h"
  16. namespace webrtc {
  17. namespace test {
  18. namespace fec {
  19. struct AugmentedPacket : public ForwardErrorCorrection::Packet {
  20. RTPHeader header;
  21. };
  22. // TODO(brandtr): Consider merging MediaPacketGenerator and
  23. // AugmentedPacketGenerator into a single class, since their functionality is
  24. // similar.
  25. // This class generates media packets corresponding to a single frame.
  26. class MediaPacketGenerator {
  27. public:
  28. MediaPacketGenerator(uint32_t min_packet_size,
  29. uint32_t max_packet_size,
  30. uint32_t ssrc,
  31. Random* random);
  32. ~MediaPacketGenerator();
  33. // Construct the media packets, up to |num_media_packets| packets.
  34. ForwardErrorCorrection::PacketList ConstructMediaPackets(
  35. int num_media_packets,
  36. uint16_t start_seq_num);
  37. ForwardErrorCorrection::PacketList ConstructMediaPackets(
  38. int num_media_packets);
  39. uint16_t GetNextSeqNum();
  40. private:
  41. uint32_t min_packet_size_;
  42. uint32_t max_packet_size_;
  43. uint32_t ssrc_;
  44. Random* random_;
  45. ForwardErrorCorrection::PacketList media_packets_;
  46. uint16_t next_seq_num_;
  47. };
  48. // This class generates media packets with a certain structure of the payload.
  49. class AugmentedPacketGenerator {
  50. public:
  51. explicit AugmentedPacketGenerator(uint32_t ssrc);
  52. // Prepare for generating a new set of packets, corresponding to a frame.
  53. void NewFrame(size_t num_packets);
  54. // Increment and return the newly incremented sequence number.
  55. uint16_t NextPacketSeqNum();
  56. // Return the next packet in the current frame.
  57. std::unique_ptr<AugmentedPacket> NextPacket(size_t offset, size_t length);
  58. protected:
  59. // Given |header|, writes the appropriate RTP header fields in |data|.
  60. static void WriteRtpHeader(const RTPHeader& header, uint8_t* data);
  61. // Number of packets left to generate, in the current frame.
  62. size_t num_packets_;
  63. private:
  64. uint32_t ssrc_;
  65. uint16_t seq_num_;
  66. uint32_t timestamp_;
  67. };
  68. // This class generates media and FlexFEC packets for a single frame.
  69. class FlexfecPacketGenerator : public AugmentedPacketGenerator {
  70. public:
  71. FlexfecPacketGenerator(uint32_t media_ssrc, uint32_t flexfec_ssrc);
  72. // Creates a new AugmentedPacket (with RTP headers) from a
  73. // FlexFEC packet (without RTP headers).
  74. std::unique_ptr<AugmentedPacket> BuildFlexfecPacket(
  75. const ForwardErrorCorrection::Packet& packet);
  76. private:
  77. uint32_t flexfec_ssrc_;
  78. uint16_t flexfec_seq_num_;
  79. uint32_t flexfec_timestamp_;
  80. };
  81. // This class generates media and ULPFEC packets (both encapsulated in RED)
  82. // for a single frame.
  83. class UlpfecPacketGenerator : public AugmentedPacketGenerator {
  84. public:
  85. explicit UlpfecPacketGenerator(uint32_t ssrc);
  86. // Creates a new RtpPacket with the RED header added to the packet.
  87. static RtpPacketReceived BuildMediaRedPacket(const AugmentedPacket& packet,
  88. bool is_recovered);
  89. // Creates a new RtpPacket with FEC payload and RED header. Does this by
  90. // creating a new fake media AugmentedPacket, clears the marker bit and adds a
  91. // RED header. Finally replaces the payload with the content of
  92. // |packet->data|.
  93. RtpPacketReceived BuildUlpfecRedPacket(
  94. const ForwardErrorCorrection::Packet& packet);
  95. };
  96. } // namespace fec
  97. } // namespace test
  98. } // namespace webrtc
  99. #endif // MODULES_RTP_RTCP_SOURCE_FEC_TEST_HELPER_H_