blob: 004edf77d46ee2c38bba18d37d5a0506e9c9d3f2 [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/random.h"
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000019
20namespace webrtc {
Danil Chapovalov1e80ce42016-02-19 16:02:15 +010021namespace test {
brandtrece4aba2016-09-20 23:16:28 -070022namespace fec {
23
brandtr0aabdac2016-10-03 06:36:43 -070024struct AugmentedPacket : public ForwardErrorCorrection::Packet {
Danil Chapovalov1e80ce42016-02-19 16:02:15 +010025 WebRtcRTPHeader header;
26};
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000027
brandtr0aabdac2016-10-03 06:36:43 -070028// TODO(brandtr): Consider merging MediaPacketGenerator and
29// AugmentedPacketGenerator into a single class, since their functionality is
30// similar.
31
brandtra4545ee2016-10-03 02:58:45 -070032// This class generates media packets corresponding to a single frame.
brandtrece4aba2016-09-20 23:16:28 -070033class MediaPacketGenerator {
34 public:
35 MediaPacketGenerator(uint32_t min_packet_size,
36 uint32_t max_packet_size,
37 uint32_t ssrc,
Danil Chapovalovdd7e2842018-03-09 15:37:03 +000038 Random* random);
39 ~MediaPacketGenerator();
brandtrece4aba2016-09-20 23:16:28 -070040
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
brandtrd726a3f2017-06-29 02:45:35 -070048 uint16_t GetNextSeqNum();
brandtrece4aba2016-09-20 23:16:28 -070049
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_;
brandtrd726a3f2017-06-29 02:45:35 -070057 uint16_t next_seq_num_;
brandtrece4aba2016-09-20 23:16:28 -070058};
59
brandtr0aabdac2016-10-03 06:36:43 -070060// This class generates media packets with a certain structure of the payload.
61class AugmentedPacketGenerator {
brandtra4545ee2016-10-03 02:58:45 -070062 public:
brandtr0aabdac2016-10-03 06:36:43 -070063 explicit AugmentedPacketGenerator(uint32_t ssrc);
brandtra4545ee2016-10-03 02:58:45 -070064
brandtr0aabdac2016-10-03 06:36:43 -070065 // Prepare for generating a new set of packets, corresponding to a frame.
66 void NewFrame(size_t num_packets);
brandtra4545ee2016-10-03 02:58:45 -070067
brandtr0aabdac2016-10-03 06:36:43 -070068 // Increment and return the newly incremented sequence number.
69 uint16_t NextPacketSeqNum();
brandtra4545ee2016-10-03 02:58:45 -070070
brandtr0aabdac2016-10-03 06:36:43 -070071 // Return the next packet in the current frame.
72 std::unique_ptr<AugmentedPacket> NextPacket(size_t offset, size_t length);
brandtra4545ee2016-10-03 02:58:45 -070073
brandtr0aabdac2016-10-03 06:36:43 -070074 protected:
75 // Given |header|, writes the appropriate RTP header fields in |data|.
76 static void WriteRtpHeader(const RTPHeader& header, uint8_t* data);
brandtra4545ee2016-10-03 02:58:45 -070077
brandtr0aabdac2016-10-03 06:36:43 -070078 // Number of packets left to generate, in the current frame.
79 size_t num_packets_;
brandtra4545ee2016-10-03 02:58:45 -070080
81 private:
brandtr0aabdac2016-10-03 06:36:43 -070082 uint32_t ssrc_;
brandtra4545ee2016-10-03 02:58:45 -070083 uint16_t seq_num_;
84 uint32_t timestamp_;
85};
86
brandtr8d02ea72016-10-03 23:47:05 -070087// This class generates media and FlexFEC packets for a single frame.
88class 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
brandtr0aabdac2016-10-03 06:36:43 -0700103// This class generates media and ULPFEC packets (both encapsulated in RED)
104// for a single frame.
105class UlpfecPacketGenerator : public AugmentedPacketGenerator {
106 public:
107 explicit UlpfecPacketGenerator(uint32_t ssrc);
108
109 // Creates a new AugmentedPacket with the RED header added to the packet.
110 static std::unique_ptr<AugmentedPacket> BuildMediaRedPacket(
111 const AugmentedPacket& packet);
112
113 // Creates a new AugmentedPacket 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 std::unique_ptr<AugmentedPacket> BuildUlpfecRedPacket(
118 const ForwardErrorCorrection::Packet& packet);
119
120 private:
121 static void SetRedHeader(uint8_t payload_type,
122 size_t header_length,
123 AugmentedPacket* red_packet);
124};
125
brandtrece4aba2016-09-20 23:16:28 -0700126} // namespace fec
127} // namespace test
danilchap6a6f0892015-12-10 12:39:08 -0800128} // namespace webrtc
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000129
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200130#endif // MODULES_RTP_RTCP_SOURCE_FEC_TEST_HELPER_H_