blob: 79dfb5eec199b9be5d4c7ff222ccbdf1db6aa394 [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,
Danil Chapovalovdd7e2842018-03-09 15:37:03 +000039 Random* random);
40 ~MediaPacketGenerator();
brandtrece4aba2016-09-20 23:16:28 -070041
42 // Construct the media packets, up to |num_media_packets| packets.
43 ForwardErrorCorrection::PacketList ConstructMediaPackets(
44 int num_media_packets,
45 uint16_t start_seq_num);
46 ForwardErrorCorrection::PacketList ConstructMediaPackets(
47 int num_media_packets);
48
brandtrd726a3f2017-06-29 02:45:35 -070049 uint16_t GetNextSeqNum();
brandtrece4aba2016-09-20 23:16:28 -070050
51 private:
52 uint32_t min_packet_size_;
53 uint32_t max_packet_size_;
54 uint32_t ssrc_;
55 Random* random_;
56
57 ForwardErrorCorrection::PacketList media_packets_;
brandtrd726a3f2017-06-29 02:45:35 -070058 uint16_t next_seq_num_;
brandtrece4aba2016-09-20 23:16:28 -070059};
60
brandtr0aabdac2016-10-03 06:36:43 -070061// This class generates media packets with a certain structure of the payload.
62class AugmentedPacketGenerator {
brandtra4545ee2016-10-03 02:58:45 -070063 public:
brandtr0aabdac2016-10-03 06:36:43 -070064 explicit AugmentedPacketGenerator(uint32_t ssrc);
brandtra4545ee2016-10-03 02:58:45 -070065
brandtr0aabdac2016-10-03 06:36:43 -070066 // Prepare for generating a new set of packets, corresponding to a frame.
67 void NewFrame(size_t num_packets);
brandtra4545ee2016-10-03 02:58:45 -070068
brandtr0aabdac2016-10-03 06:36:43 -070069 // Increment and return the newly incremented sequence number.
70 uint16_t NextPacketSeqNum();
brandtra4545ee2016-10-03 02:58:45 -070071
brandtr0aabdac2016-10-03 06:36:43 -070072 // Return the next packet in the current frame.
73 std::unique_ptr<AugmentedPacket> NextPacket(size_t offset, size_t length);
brandtra4545ee2016-10-03 02:58:45 -070074
brandtr0aabdac2016-10-03 06:36:43 -070075 protected:
76 // Given |header|, writes the appropriate RTP header fields in |data|.
77 static void WriteRtpHeader(const RTPHeader& header, uint8_t* data);
brandtra4545ee2016-10-03 02:58:45 -070078
brandtr0aabdac2016-10-03 06:36:43 -070079 // Number of packets left to generate, in the current frame.
80 size_t num_packets_;
brandtra4545ee2016-10-03 02:58:45 -070081
82 private:
brandtr0aabdac2016-10-03 06:36:43 -070083 uint32_t ssrc_;
brandtra4545ee2016-10-03 02:58:45 -070084 uint16_t seq_num_;
85 uint32_t timestamp_;
86};
87
brandtr8d02ea72016-10-03 23:47:05 -070088// This class generates media and FlexFEC packets for a single frame.
89class FlexfecPacketGenerator : public AugmentedPacketGenerator {
90 public:
91 FlexfecPacketGenerator(uint32_t media_ssrc, uint32_t flexfec_ssrc);
92
93 // Creates a new AugmentedPacket (with RTP headers) from a
94 // FlexFEC packet (without RTP headers).
95 std::unique_ptr<AugmentedPacket> BuildFlexfecPacket(
96 const ForwardErrorCorrection::Packet& packet);
97
98 private:
99 uint32_t flexfec_ssrc_;
100 uint16_t flexfec_seq_num_;
101 uint32_t flexfec_timestamp_;
102};
103
brandtr0aabdac2016-10-03 06:36:43 -0700104// This class generates media and ULPFEC packets (both encapsulated in RED)
105// for a single frame.
106class UlpfecPacketGenerator : public AugmentedPacketGenerator {
107 public:
108 explicit UlpfecPacketGenerator(uint32_t ssrc);
109
110 // Creates a new AugmentedPacket with the RED header added to the packet.
111 static std::unique_ptr<AugmentedPacket> BuildMediaRedPacket(
112 const AugmentedPacket& packet);
113
114 // Creates a new AugmentedPacket with FEC payload and RED header. Does this by
115 // creating a new fake media AugmentedPacket, clears the marker bit and adds a
116 // RED header. Finally replaces the payload with the content of
117 // |packet->data|.
118 std::unique_ptr<AugmentedPacket> BuildUlpfecRedPacket(
119 const ForwardErrorCorrection::Packet& packet);
120
121 private:
122 static void SetRedHeader(uint8_t payload_type,
123 size_t header_length,
124 AugmentedPacket* red_packet);
125};
126
brandtrece4aba2016-09-20 23:16:28 -0700127} // namespace fec
128} // namespace test
danilchap6a6f0892015-12-10 12:39:08 -0800129} // namespace webrtc
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000130
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200131#endif // MODULES_RTP_RTCP_SOURCE_FEC_TEST_HELPER_H_