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