blob: cdfa1ff67d80c4e9901f646b427e2ec3f2a56327 [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_ULPFEC_GENERATOR_H_
12#define MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
brandtr35c480c2016-08-09 01:23:23 -070017#include <list>
brandtr74811e52016-08-10 00:51:50 -070018#include <memory>
mflodmanfcf54bd2015-04-14 21:28:08 +020019#include <vector>
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000020
Yves Gerey988cc082018-10-23 12:03:01 +020021#include "modules/include/module_fec_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/rtp_rtcp/source/forward_error_correction.h"
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000023
24namespace webrtc {
25
brandtr9ed35a42016-11-08 00:28:58 -080026class FlexfecSender;
brandtrc295e002016-11-03 09:22:33 -070027
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000028class RedPacket {
29 public:
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000030 explicit RedPacket(size_t length);
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010031 ~RedPacket();
brandtr74811e52016-08-10 00:51:50 -070032
33 void CreateHeader(const uint8_t* rtp_header,
34 size_t header_length,
35 int red_payload_type,
36 int payload_type);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000037 void SetSeqNum(int seq_num);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000038 void AssignPayload(const uint8_t* payload, size_t length);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000039 void ClearMarkerBit();
40 uint8_t* data() const;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000041 size_t length() const;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000042
43 private:
brandtr74811e52016-08-10 00:51:50 -070044 std::unique_ptr<uint8_t[]> data_;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000045 size_t length_;
46 size_t header_length_;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000047};
48
brandtr869e7cd2016-10-31 05:27:07 -070049class UlpfecGenerator {
brandtrc295e002016-11-03 09:22:33 -070050 friend class FlexfecSender;
51
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000052 public:
brandtr869e7cd2016-10-31 05:27:07 -070053 UlpfecGenerator();
54 ~UlpfecGenerator();
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000055
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().
Ilya Nikolaevskiya5d952f2019-09-03 11:07:37 +020061 int AddRtpPacketAndGenerateFec(const rtc::CopyOnWriteBuffer& data_buffer,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000062 size_t rtp_header_length);
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +000063
Rasmus Brandtc07ebb32016-10-04 10:57:36 +020064 // Returns true if there are generated FEC packets available.
65 bool FecAvailable() const;
66
67 size_t NumAvailableFecPackets() const;
68
69 // Returns the overhead, per packet, for FEC (and possibly RED).
70 size_t MaxPacketOverhead() const;
71
72 // Returns generated FEC packets with RED headers added.
73 std::vector<std::unique_ptr<RedPacket>> GetUlpfecPacketsAsRed(
74 int red_payload_type,
75 int ulpfec_payload_type,
Rasmus Brandt393e2662018-01-22 12:52:36 +010076 uint16_t first_seq_num);
Rasmus Brandtc07ebb32016-10-04 10:57:36 +020077
78 private:
brandtrc295e002016-11-03 09:22:33 -070079 explicit UlpfecGenerator(std::unique_ptr<ForwardErrorCorrection> fec);
80
Rasmus Brandtc07ebb32016-10-04 10:57:36 +020081 // Overhead is defined as relative to the number of media packets, and not
82 // relative to total number of packets. This definition is inherited from the
83 // protection factor produced by video_coding module and how the FEC
84 // generation is implemented.
85 int Overhead() const;
86
brandtr74811e52016-08-10 00:51:50 -070087 // Returns true if the excess overhead (actual - target) for the FEC is below
88 // the amount |kMaxExcessOverhead|. This effects the lower protection level
89 // cases and low number of media packets/frame. The target overhead is given
90 // by |params_.fec_rate|, and is only achievable in the limit of large number
91 // of media packets.
92 bool ExcessOverheadBelowMax() const;
marpan@webrtc.org747cd872012-05-22 16:50:00 +000093
brandtr74811e52016-08-10 00:51:50 -070094 // Returns true if the number of added media packets is at least
95 // |min_num_media_packets_|. This condition tries to capture the effect
96 // that, for the same amount of protection/overhead, longer codes
97 // (e.g. (2k,2m) vs (k,m)) are generally more effective at recovering losses.
98 bool MinimumMediaPacketsReached() const;
marpan@webrtc.org747cd872012-05-22 16:50:00 +000099
Rasmus Brandtc07ebb32016-10-04 10:57:36 +0200100 void ResetState();
brandtr74811e52016-08-10 00:51:50 -0700101
Rasmus Brandt78db1582016-09-21 09:19:34 +0200102 std::unique_ptr<ForwardErrorCorrection> fec_;
brandtr74811e52016-08-10 00:51:50 -0700103 ForwardErrorCorrection::PacketList media_packets_;
Rasmus Brandt393e2662018-01-22 12:52:36 +0100104 size_t last_media_packet_rtp_header_length_;
brandtr74811e52016-08-10 00:51:50 -0700105 std::list<ForwardErrorCorrection::Packet*> generated_fec_packets_;
106 int num_protected_frames_;
brandtr74811e52016-08-10 00:51:50 -0700107 int min_num_media_packets_;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000108 FecProtectionParams params_;
stefan@webrtc.orgc35f5ce2012-04-11 07:42:25 +0000109 FecProtectionParams new_params_;
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000110};
111
112} // namespace webrtc
113
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200114#endif // MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_