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 11 #ifndef MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_ 12 #define MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <list> 18 #include <memory> 19 #include <vector> 20 21 #include "modules/include/module_fec_types.h" 22 #include "modules/rtp_rtcp/source/forward_error_correction.h" 23 #include "modules/rtp_rtcp/source/video_fec_generator.h" 24 #include "rtc_base/race_checker.h" 25 #include "rtc_base/rate_statistics.h" 26 #include "rtc_base/synchronization/mutex.h" 27 28 namespace webrtc { 29 30 class FlexfecSender; 31 32 class UlpfecGenerator : public VideoFecGenerator { 33 friend class FlexfecSender; 34 35 public: 36 UlpfecGenerator(int red_payload_type, int ulpfec_payload_type, Clock* clock); 37 ~UlpfecGenerator(); 38 GetFecType()39 FecType GetFecType() const override { 40 return VideoFecGenerator::FecType::kUlpFec; 41 } FecSsrc()42 absl::optional<uint32_t> FecSsrc() override { return absl::nullopt; } 43 44 void SetProtectionParameters(const FecProtectionParams& delta_params, 45 const FecProtectionParams& key_params) override; 46 47 // Adds a media packet to the internal buffer. When enough media packets 48 // have been added, the FEC packets are generated and stored internally. 49 // These FEC packets are then obtained by calling GetFecPacketsAsRed(). 50 void AddPacketAndGenerateFec(const RtpPacketToSend& packet) override; 51 52 // Returns the overhead, per packet, for FEC (and possibly RED). 53 size_t MaxPacketOverhead() const override; 54 55 std::vector<std::unique_ptr<RtpPacketToSend>> GetFecPackets() override; 56 57 // Current rate of FEC packets generated, including all RTP-level headers. 58 DataRate CurrentFecRate() const override; 59 GetRtpState()60 absl::optional<RtpState> GetRtpState() override { return absl::nullopt; } 61 62 private: 63 struct Params { 64 Params(); 65 Params(FecProtectionParams delta_params, 66 FecProtectionParams keyframe_params); 67 68 FecProtectionParams delta_params; 69 FecProtectionParams keyframe_params; 70 }; 71 72 UlpfecGenerator(std::unique_ptr<ForwardErrorCorrection> fec, Clock* clock); 73 74 // Overhead is defined as relative to the number of media packets, and not 75 // relative to total number of packets. This definition is inherited from the 76 // protection factor produced by video_coding module and how the FEC 77 // generation is implemented. 78 int Overhead() const; 79 80 // Returns true if the excess overhead (actual - target) for the FEC is below 81 // the amount |kMaxExcessOverhead|. This effects the lower protection level 82 // cases and low number of media packets/frame. The target overhead is given 83 // by |params_.fec_rate|, and is only achievable in the limit of large number 84 // of media packets. 85 bool ExcessOverheadBelowMax() const; 86 87 // Returns true if the number of added media packets is at least 88 // |min_num_media_packets_|. This condition tries to capture the effect 89 // that, for the same amount of protection/overhead, longer codes 90 // (e.g. (2k,2m) vs (k,m)) are generally more effective at recovering losses. 91 bool MinimumMediaPacketsReached() const; 92 93 const FecProtectionParams& CurrentParams() const; 94 95 void ResetState(); 96 97 const int red_payload_type_; 98 const int ulpfec_payload_type_; 99 Clock* const clock_; 100 101 rtc::RaceChecker race_checker_; 102 const std::unique_ptr<ForwardErrorCorrection> fec_ 103 RTC_GUARDED_BY(race_checker_); 104 ForwardErrorCorrection::PacketList media_packets_ 105 RTC_GUARDED_BY(race_checker_); 106 absl::optional<RtpPacketToSend> last_media_packet_ 107 RTC_GUARDED_BY(race_checker_); 108 std::list<ForwardErrorCorrection::Packet*> generated_fec_packets_ 109 RTC_GUARDED_BY(race_checker_); 110 int num_protected_frames_ RTC_GUARDED_BY(race_checker_); 111 int min_num_media_packets_ RTC_GUARDED_BY(race_checker_); 112 Params current_params_ RTC_GUARDED_BY(race_checker_); 113 bool keyframe_in_process_ RTC_GUARDED_BY(race_checker_); 114 115 mutable Mutex mutex_; 116 absl::optional<Params> pending_params_ RTC_GUARDED_BY(mutex_); 117 RateStatistics fec_bitrate_ RTC_GUARDED_BY(mutex_); 118 }; 119 120 } // namespace webrtc 121 122 #endif // MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_ 123