blob: 712b20143b6f3da45bfae1bd5fc1eed2fb229e7c [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/include/module_common_types.h"
17#include "modules/rtp_rtcp/source/forward_error_correction.h"
18#include "rtc_base/basictypes.h"
19#include "rtc_base/random.h"
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000020
21namespace webrtc {
Danil Chapovalov1e80ce42016-02-19 16:02:15 +010022namespace test {
brandtrece4aba2016-09-20 23:16:28 -070023namespace fec {
24
brandtr0aabdac2016-10-03 06:36:43 -070025struct AugmentedPacket : public ForwardErrorCorrection::Packet {
Danil Chapovalov1e80ce42016-02-19 16:02:15 +010026 WebRtcRTPHeader header;
27};
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000028
brandtr0aabdac2016-10-03 06:36:43 -070029// TODO(brandtr): Consider merging MediaPacketGenerator and
30// AugmentedPacketGenerator into a single class, since their functionality is
31// similar.
32
brandtra4545ee2016-10-03 02:58:45 -070033// This class generates media packets corresponding to a single frame.
brandtrece4aba2016-09-20 23:16:28 -070034class MediaPacketGenerator {
35 public:
36 MediaPacketGenerator(uint32_t min_packet_size,
37 uint32_t max_packet_size,
38 uint32_t ssrc,
39 Random* random)
40 : min_packet_size_(min_packet_size),
41 max_packet_size_(max_packet_size),
42 ssrc_(ssrc),
43 random_(random) {}
44
45 // Construct the media packets, up to |num_media_packets| packets.
46 ForwardErrorCorrection::PacketList ConstructMediaPackets(
47 int num_media_packets,
48 uint16_t start_seq_num);
49 ForwardErrorCorrection::PacketList ConstructMediaPackets(
50 int num_media_packets);
51
brandtrd726a3f2017-06-29 02:45:35 -070052 uint16_t GetNextSeqNum();
brandtrece4aba2016-09-20 23:16:28 -070053
54 private:
55 uint32_t min_packet_size_;
56 uint32_t max_packet_size_;
57 uint32_t ssrc_;
58 Random* random_;
59
60 ForwardErrorCorrection::PacketList media_packets_;
brandtrd726a3f2017-06-29 02:45:35 -070061 uint16_t next_seq_num_;
brandtrece4aba2016-09-20 23:16:28 -070062};
63
brandtr0aabdac2016-10-03 06:36:43 -070064// This class generates media packets with a certain structure of the payload.
65class AugmentedPacketGenerator {
brandtra4545ee2016-10-03 02:58:45 -070066 public:
brandtr0aabdac2016-10-03 06:36:43 -070067 explicit AugmentedPacketGenerator(uint32_t ssrc);
brandtra4545ee2016-10-03 02:58:45 -070068
brandtr0aabdac2016-10-03 06:36:43 -070069 // Prepare for generating a new set of packets, corresponding to a frame.
70 void NewFrame(size_t num_packets);
brandtra4545ee2016-10-03 02:58:45 -070071
brandtr0aabdac2016-10-03 06:36:43 -070072 // Increment and return the newly incremented sequence number.
73 uint16_t NextPacketSeqNum();
brandtra4545ee2016-10-03 02:58:45 -070074
brandtr0aabdac2016-10-03 06:36:43 -070075 // Return the next packet in the current frame.
76 std::unique_ptr<AugmentedPacket> NextPacket(size_t offset, size_t length);
brandtra4545ee2016-10-03 02:58:45 -070077
brandtr0aabdac2016-10-03 06:36:43 -070078 protected:
79 // Given |header|, writes the appropriate RTP header fields in |data|.
80 static void WriteRtpHeader(const RTPHeader& header, uint8_t* data);
brandtra4545ee2016-10-03 02:58:45 -070081
brandtr0aabdac2016-10-03 06:36:43 -070082 // Number of packets left to generate, in the current frame.
83 size_t num_packets_;
brandtra4545ee2016-10-03 02:58:45 -070084
85 private:
brandtr0aabdac2016-10-03 06:36:43 -070086 uint32_t ssrc_;
brandtra4545ee2016-10-03 02:58:45 -070087 uint16_t seq_num_;
88 uint32_t timestamp_;
89};
90
brandtr8d02ea72016-10-03 23:47:05 -070091// This class generates media and FlexFEC packets for a single frame.
92class FlexfecPacketGenerator : public AugmentedPacketGenerator {
93 public:
94 FlexfecPacketGenerator(uint32_t media_ssrc, uint32_t flexfec_ssrc);
95
96 // Creates a new AugmentedPacket (with RTP headers) from a
97 // FlexFEC packet (without RTP headers).
98 std::unique_ptr<AugmentedPacket> BuildFlexfecPacket(
99 const ForwardErrorCorrection::Packet& packet);
100
101 private:
102 uint32_t flexfec_ssrc_;
103 uint16_t flexfec_seq_num_;
104 uint32_t flexfec_timestamp_;
105};
106
brandtr0aabdac2016-10-03 06:36:43 -0700107// This class generates media and ULPFEC packets (both encapsulated in RED)
108// for a single frame.
109class UlpfecPacketGenerator : public AugmentedPacketGenerator {
110 public:
111 explicit UlpfecPacketGenerator(uint32_t ssrc);
112
113 // Creates a new AugmentedPacket with the RED header added to the packet.
114 static std::unique_ptr<AugmentedPacket> BuildMediaRedPacket(
115 const AugmentedPacket& packet);
116
117 // Creates a new AugmentedPacket with FEC payload and RED header. Does this by
118 // creating a new fake media AugmentedPacket, clears the marker bit and adds a
119 // RED header. Finally replaces the payload with the content of
120 // |packet->data|.
121 std::unique_ptr<AugmentedPacket> BuildUlpfecRedPacket(
122 const ForwardErrorCorrection::Packet& packet);
123
124 private:
125 static void SetRedHeader(uint8_t payload_type,
126 size_t header_length,
127 AugmentedPacket* red_packet);
128};
129
brandtrece4aba2016-09-20 23:16:28 -0700130} // namespace fec
131} // namespace test
danilchap6a6f0892015-12-10 12:39:08 -0800132} // namespace webrtc
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000133
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200134#endif // MODULES_RTP_RTCP_SOURCE_FEC_TEST_HELPER_H_