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