stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_ |
| 12 | #define MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_ |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 13 | |
brandtr | 35c480c | 2016-08-09 01:23:23 -0700 | [diff] [blame] | 14 | #include <list> |
brandtr | 74811e5 | 2016-08-10 00:51:50 -0700 | [diff] [blame] | 15 | #include <memory> |
mflodman | fcf54bd | 2015-04-14 21:28:08 +0200 | [diff] [blame] | 16 | #include <vector> |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/rtp_rtcp/source/forward_error_correction.h" |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
brandtr | 9ed35a4 | 2016-11-08 00:28:58 -0800 | [diff] [blame] | 22 | class FlexfecSender; |
brandtr | c295e00 | 2016-11-03 09:22:33 -0700 | [diff] [blame] | 23 | |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 24 | class RedPacket { |
| 25 | public: |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 26 | explicit RedPacket(size_t length); |
Danil Chapovalov | 2a5ce2b | 2018-02-07 09:38:31 +0100 | [diff] [blame] | 27 | ~RedPacket(); |
brandtr | 74811e5 | 2016-08-10 00:51:50 -0700 | [diff] [blame] | 28 | |
| 29 | void CreateHeader(const uint8_t* rtp_header, |
| 30 | size_t header_length, |
| 31 | int red_payload_type, |
| 32 | int payload_type); |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 33 | void SetSeqNum(int seq_num); |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 34 | void AssignPayload(const uint8_t* payload, size_t length); |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 35 | void ClearMarkerBit(); |
| 36 | uint8_t* data() const; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 37 | size_t length() const; |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 38 | |
| 39 | private: |
brandtr | 74811e5 | 2016-08-10 00:51:50 -0700 | [diff] [blame] | 40 | std::unique_ptr<uint8_t[]> data_; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 41 | size_t length_; |
| 42 | size_t header_length_; |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
brandtr | 869e7cd | 2016-10-31 05:27:07 -0700 | [diff] [blame] | 45 | class UlpfecGenerator { |
brandtr | c295e00 | 2016-11-03 09:22:33 -0700 | [diff] [blame] | 46 | friend class FlexfecSender; |
| 47 | |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 48 | public: |
brandtr | 869e7cd | 2016-10-31 05:27:07 -0700 | [diff] [blame] | 49 | UlpfecGenerator(); |
| 50 | ~UlpfecGenerator(); |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 51 | |
brandtr | 1743a19 | 2016-11-07 03:36:05 -0800 | [diff] [blame] | 52 | void SetFecParameters(const FecProtectionParams& params); |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 53 | |
brandtr | 74811e5 | 2016-08-10 00:51:50 -0700 | [diff] [blame] | 54 | // Adds a media packet to the internal buffer. When enough media packets |
| 55 | // have been added, the FEC packets are generated and stored internally. |
| 56 | // These FEC packets are then obtained by calling GetFecPacketsAsRed(). |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 57 | int AddRtpPacketAndGenerateFec(const uint8_t* data_buffer, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 58 | size_t payload_length, |
| 59 | size_t rtp_header_length); |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 60 | |
Rasmus Brandt | c07ebb3 | 2016-10-04 10:57:36 +0200 | [diff] [blame] | 61 | // Returns true if there are generated FEC packets available. |
| 62 | bool FecAvailable() const; |
| 63 | |
| 64 | size_t NumAvailableFecPackets() const; |
| 65 | |
| 66 | // Returns the overhead, per packet, for FEC (and possibly RED). |
| 67 | size_t MaxPacketOverhead() const; |
| 68 | |
| 69 | // Returns generated FEC packets with RED headers added. |
| 70 | std::vector<std::unique_ptr<RedPacket>> GetUlpfecPacketsAsRed( |
| 71 | int red_payload_type, |
| 72 | int ulpfec_payload_type, |
Rasmus Brandt | 393e266 | 2018-01-22 12:52:36 +0100 | [diff] [blame] | 73 | uint16_t first_seq_num); |
Rasmus Brandt | c07ebb3 | 2016-10-04 10:57:36 +0200 | [diff] [blame] | 74 | |
| 75 | private: |
brandtr | c295e00 | 2016-11-03 09:22:33 -0700 | [diff] [blame] | 76 | explicit UlpfecGenerator(std::unique_ptr<ForwardErrorCorrection> fec); |
| 77 | |
Rasmus Brandt | c07ebb3 | 2016-10-04 10:57:36 +0200 | [diff] [blame] | 78 | // Overhead is defined as relative to the number of media packets, and not |
| 79 | // relative to total number of packets. This definition is inherited from the |
| 80 | // protection factor produced by video_coding module and how the FEC |
| 81 | // generation is implemented. |
| 82 | int Overhead() const; |
| 83 | |
brandtr | 74811e5 | 2016-08-10 00:51:50 -0700 | [diff] [blame] | 84 | // Returns true if the excess overhead (actual - target) for the FEC is below |
| 85 | // the amount |kMaxExcessOverhead|. This effects the lower protection level |
| 86 | // cases and low number of media packets/frame. The target overhead is given |
| 87 | // by |params_.fec_rate|, and is only achievable in the limit of large number |
| 88 | // of media packets. |
| 89 | bool ExcessOverheadBelowMax() const; |
marpan@webrtc.org | 747cd87 | 2012-05-22 16:50:00 +0000 | [diff] [blame] | 90 | |
brandtr | 74811e5 | 2016-08-10 00:51:50 -0700 | [diff] [blame] | 91 | // Returns true if the number of added media packets is at least |
| 92 | // |min_num_media_packets_|. This condition tries to capture the effect |
| 93 | // that, for the same amount of protection/overhead, longer codes |
| 94 | // (e.g. (2k,2m) vs (k,m)) are generally more effective at recovering losses. |
| 95 | bool MinimumMediaPacketsReached() const; |
marpan@webrtc.org | 747cd87 | 2012-05-22 16:50:00 +0000 | [diff] [blame] | 96 | |
Rasmus Brandt | c07ebb3 | 2016-10-04 10:57:36 +0200 | [diff] [blame] | 97 | void ResetState(); |
brandtr | 74811e5 | 2016-08-10 00:51:50 -0700 | [diff] [blame] | 98 | |
Rasmus Brandt | 78db158 | 2016-09-21 09:19:34 +0200 | [diff] [blame] | 99 | std::unique_ptr<ForwardErrorCorrection> fec_; |
brandtr | 74811e5 | 2016-08-10 00:51:50 -0700 | [diff] [blame] | 100 | ForwardErrorCorrection::PacketList media_packets_; |
Rasmus Brandt | 393e266 | 2018-01-22 12:52:36 +0100 | [diff] [blame] | 101 | size_t last_media_packet_rtp_header_length_; |
brandtr | 74811e5 | 2016-08-10 00:51:50 -0700 | [diff] [blame] | 102 | std::list<ForwardErrorCorrection::Packet*> generated_fec_packets_; |
| 103 | int num_protected_frames_; |
brandtr | 74811e5 | 2016-08-10 00:51:50 -0700 | [diff] [blame] | 104 | int min_num_media_packets_; |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 105 | FecProtectionParams params_; |
stefan@webrtc.org | c35f5ce | 2012-04-11 07:42:25 +0000 | [diff] [blame] | 106 | FecProtectionParams new_params_; |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace webrtc |
| 110 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 111 | #endif // MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_ |