blob: 635062a5b639ff11921b342e0d7bdcd1a841985c [file] [log] [blame]
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_RTP_RTCP_SOURCE_FEC_TEST_HELPER_H_
12#define MODULES_RTP_RTCP_SOURCE_FEC_TEST_HELPER_H_
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000013
brandtr0aabdac2016-10-03 06:36:43 -070014#include <memory>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/rtp_rtcp/source/forward_error_correction.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/random.h"
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000018
19namespace webrtc {
Danil Chapovalov1e80ce42016-02-19 16:02:15 +010020namespace test {
brandtrece4aba2016-09-20 23:16:28 -070021namespace fec {
22
brandtr0aabdac2016-10-03 06:36:43 -070023struct AugmentedPacket : public ForwardErrorCorrection::Packet {
Niels Möller70a83942019-03-12 09:54:30 +010024 RTPHeader header;
Danil Chapovalov1e80ce42016-02-19 16:02:15 +010025};
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000026
brandtr0aabdac2016-10-03 06:36:43 -070027// TODO(brandtr): Consider merging MediaPacketGenerator and
28// AugmentedPacketGenerator into a single class, since their functionality is
29// similar.
30
brandtra4545ee2016-10-03 02:58:45 -070031// This class generates media packets corresponding to a single frame.
brandtrece4aba2016-09-20 23:16:28 -070032class MediaPacketGenerator {
33 public:
34 MediaPacketGenerator(uint32_t min_packet_size,
35 uint32_t max_packet_size,
36 uint32_t ssrc,
Danil Chapovalovdd7e2842018-03-09 15:37:03 +000037 Random* random);
38 ~MediaPacketGenerator();
brandtrece4aba2016-09-20 23:16:28 -070039
40 // Construct the media packets, up to |num_media_packets| packets.
41 ForwardErrorCorrection::PacketList ConstructMediaPackets(
42 int num_media_packets,
43 uint16_t start_seq_num);
44 ForwardErrorCorrection::PacketList ConstructMediaPackets(
45 int num_media_packets);
46
brandtrd726a3f2017-06-29 02:45:35 -070047 uint16_t GetNextSeqNum();
brandtrece4aba2016-09-20 23:16:28 -070048
49 private:
50 uint32_t min_packet_size_;
51 uint32_t max_packet_size_;
52 uint32_t ssrc_;
53 Random* random_;
54
55 ForwardErrorCorrection::PacketList media_packets_;
brandtrd726a3f2017-06-29 02:45:35 -070056 uint16_t next_seq_num_;
brandtrece4aba2016-09-20 23:16:28 -070057};
58
brandtr0aabdac2016-10-03 06:36:43 -070059// This class generates media packets with a certain structure of the payload.
60class AugmentedPacketGenerator {
brandtra4545ee2016-10-03 02:58:45 -070061 public:
brandtr0aabdac2016-10-03 06:36:43 -070062 explicit AugmentedPacketGenerator(uint32_t ssrc);
brandtra4545ee2016-10-03 02:58:45 -070063
brandtr0aabdac2016-10-03 06:36:43 -070064 // Prepare for generating a new set of packets, corresponding to a frame.
65 void NewFrame(size_t num_packets);
brandtra4545ee2016-10-03 02:58:45 -070066
brandtr0aabdac2016-10-03 06:36:43 -070067 // Increment and return the newly incremented sequence number.
68 uint16_t NextPacketSeqNum();
brandtra4545ee2016-10-03 02:58:45 -070069
brandtr0aabdac2016-10-03 06:36:43 -070070 // Return the next packet in the current frame.
71 std::unique_ptr<AugmentedPacket> NextPacket(size_t offset, size_t length);
brandtra4545ee2016-10-03 02:58:45 -070072
brandtr0aabdac2016-10-03 06:36:43 -070073 protected:
74 // Given |header|, writes the appropriate RTP header fields in |data|.
75 static void WriteRtpHeader(const RTPHeader& header, uint8_t* data);
brandtra4545ee2016-10-03 02:58:45 -070076
brandtr0aabdac2016-10-03 06:36:43 -070077 // Number of packets left to generate, in the current frame.
78 size_t num_packets_;
brandtra4545ee2016-10-03 02:58:45 -070079
80 private:
brandtr0aabdac2016-10-03 06:36:43 -070081 uint32_t ssrc_;
brandtra4545ee2016-10-03 02:58:45 -070082 uint16_t seq_num_;
83 uint32_t timestamp_;
84};
85
brandtr8d02ea72016-10-03 23:47:05 -070086// This class generates media and FlexFEC packets for a single frame.
87class FlexfecPacketGenerator : public AugmentedPacketGenerator {
88 public:
89 FlexfecPacketGenerator(uint32_t media_ssrc, uint32_t flexfec_ssrc);
90
91 // Creates a new AugmentedPacket (with RTP headers) from a
92 // FlexFEC packet (without RTP headers).
93 std::unique_ptr<AugmentedPacket> BuildFlexfecPacket(
94 const ForwardErrorCorrection::Packet& packet);
95
96 private:
97 uint32_t flexfec_ssrc_;
98 uint16_t flexfec_seq_num_;
99 uint32_t flexfec_timestamp_;
100};
101
brandtr0aabdac2016-10-03 06:36:43 -0700102// This class generates media and ULPFEC packets (both encapsulated in RED)
103// for a single frame.
104class UlpfecPacketGenerator : public AugmentedPacketGenerator {
105 public:
106 explicit UlpfecPacketGenerator(uint32_t ssrc);
107
108 // Creates a new AugmentedPacket with the RED header added to the packet.
109 static std::unique_ptr<AugmentedPacket> BuildMediaRedPacket(
110 const AugmentedPacket& packet);
111
112 // Creates a new AugmentedPacket with FEC payload and RED header. Does this by
113 // creating a new fake media AugmentedPacket, clears the marker bit and adds a
114 // RED header. Finally replaces the payload with the content of
115 // |packet->data|.
116 std::unique_ptr<AugmentedPacket> BuildUlpfecRedPacket(
117 const ForwardErrorCorrection::Packet& packet);
118
119 private:
120 static void SetRedHeader(uint8_t payload_type,
121 size_t header_length,
122 AugmentedPacket* red_packet);
123};
124
brandtrece4aba2016-09-20 23:16:28 -0700125} // namespace fec
126} // namespace test
danilchap6a6f0892015-12-10 12:39:08 -0800127} // namespace webrtc
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000128
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200129#endif // MODULES_RTP_RTCP_SOURCE_FEC_TEST_HELPER_H_