blob: 52e03cdad99168b6922dd0cb487ed0080a8cf25c [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
brandtr869e7cd2016-10-31 05:27:07 -070011#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_
12#define WEBRTC_MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000013
brandtr35c480c2016-08-09 01:23:23 -070014#include <list>
brandtr74811e52016-08-10 00:51:50 -070015#include <memory>
mflodmanfcf54bd2015-04-14 21:28:08 +020016#include <vector>
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000017
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000018#include "webrtc/modules/rtp_rtcp/source/forward_error_correction.h"
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000019
20namespace webrtc {
21
brandtr9ed35a42016-11-08 00:28:58 -080022class FlexfecSender;
brandtrc295e002016-11-03 09:22:33 -070023
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000024class RedPacket {
25 public:
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000026 explicit RedPacket(size_t length);
brandtr74811e52016-08-10 00:51:50 -070027
28 void CreateHeader(const uint8_t* rtp_header,
29 size_t header_length,
30 int red_payload_type,
31 int payload_type);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000032 void SetSeqNum(int seq_num);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000033 void AssignPayload(const uint8_t* payload, size_t length);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000034 void ClearMarkerBit();
35 uint8_t* data() const;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000036 size_t length() const;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000037
38 private:
brandtr74811e52016-08-10 00:51:50 -070039 std::unique_ptr<uint8_t[]> data_;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000040 size_t length_;
41 size_t header_length_;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000042};
43
brandtr869e7cd2016-10-31 05:27:07 -070044class UlpfecGenerator {
brandtrc295e002016-11-03 09:22:33 -070045 friend class FlexfecSender;
46
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000047 public:
brandtr869e7cd2016-10-31 05:27:07 -070048 UlpfecGenerator();
49 ~UlpfecGenerator();
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000050
brandtr74811e52016-08-10 00:51:50 -070051 static std::unique_ptr<RedPacket> BuildRedPacket(const uint8_t* data_buffer,
52 size_t payload_length,
53 size_t rtp_header_length,
54 int red_payload_type);
55
brandtr1743a192016-11-07 03:36:05 -080056 void SetFecParameters(const FecProtectionParams& params);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000057
brandtr74811e52016-08-10 00:51:50 -070058 // Adds a media packet to the internal buffer. When enough media packets
59 // have been added, the FEC packets are generated and stored internally.
60 // These FEC packets are then obtained by calling GetFecPacketsAsRed().
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000061 int AddRtpPacketAndGenerateFec(const uint8_t* data_buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000062 size_t payload_length,
63 size_t rtp_header_length);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000064
Rasmus Brandtc07ebb32016-10-04 10:57:36 +020065 // Returns true if there are generated FEC packets available.
66 bool FecAvailable() const;
67
68 size_t NumAvailableFecPackets() const;
69
70 // Returns the overhead, per packet, for FEC (and possibly RED).
71 size_t MaxPacketOverhead() const;
72
73 // Returns generated FEC packets with RED headers added.
74 std::vector<std::unique_ptr<RedPacket>> GetUlpfecPacketsAsRed(
75 int red_payload_type,
76 int ulpfec_payload_type,
77 uint16_t first_seq_num,
78 size_t rtp_header_length);
79
80 private:
brandtrc295e002016-11-03 09:22:33 -070081 explicit UlpfecGenerator(std::unique_ptr<ForwardErrorCorrection> fec);
82
Rasmus Brandtc07ebb32016-10-04 10:57:36 +020083 // Overhead is defined as relative to the number of media packets, and not
84 // relative to total number of packets. This definition is inherited from the
85 // protection factor produced by video_coding module and how the FEC
86 // generation is implemented.
87 int Overhead() const;
88
brandtr74811e52016-08-10 00:51:50 -070089 // Returns true if the excess overhead (actual - target) for the FEC is below
90 // the amount |kMaxExcessOverhead|. This effects the lower protection level
91 // cases and low number of media packets/frame. The target overhead is given
92 // by |params_.fec_rate|, and is only achievable in the limit of large number
93 // of media packets.
94 bool ExcessOverheadBelowMax() const;
marpan@webrtc.org747cd872012-05-22 16:50:00 +000095
brandtr74811e52016-08-10 00:51:50 -070096 // Returns true if the number of added media packets is at least
97 // |min_num_media_packets_|. This condition tries to capture the effect
98 // that, for the same amount of protection/overhead, longer codes
99 // (e.g. (2k,2m) vs (k,m)) are generally more effective at recovering losses.
100 bool MinimumMediaPacketsReached() const;
marpan@webrtc.org747cd872012-05-22 16:50:00 +0000101
Rasmus Brandtc07ebb32016-10-04 10:57:36 +0200102 void ResetState();
brandtr74811e52016-08-10 00:51:50 -0700103
Rasmus Brandt78db1582016-09-21 09:19:34 +0200104 std::unique_ptr<ForwardErrorCorrection> fec_;
brandtr74811e52016-08-10 00:51:50 -0700105 ForwardErrorCorrection::PacketList media_packets_;
106 std::list<ForwardErrorCorrection::Packet*> generated_fec_packets_;
107 int num_protected_frames_;
brandtr74811e52016-08-10 00:51:50 -0700108 int min_num_media_packets_;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000109 FecProtectionParams params_;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000110 FecProtectionParams new_params_;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000111};
112
113} // namespace webrtc
114
brandtr869e7cd2016-10-31 05:27:07 -0700115#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_