xref: /aosp_15_r20/external/webrtc/modules/rtp_rtcp/source/ulpfec_generator.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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   // Currently used protection params.
63   const FecProtectionParams& CurrentParams() const;
64 
65  private:
66   struct Params {
67     Params();
68     Params(FecProtectionParams delta_params,
69            FecProtectionParams keyframe_params);
70 
71     FecProtectionParams delta_params;
72     FecProtectionParams keyframe_params;
73   };
74 
75   UlpfecGenerator(std::unique_ptr<ForwardErrorCorrection> fec, Clock* clock);
76 
77   // Overhead is defined as relative to the number of media packets, and not
78   // relative to total number of packets. This definition is inherited from the
79   // protection factor produced by video_coding module and how the FEC
80   // generation is implemented.
81   int Overhead() const;
82 
83   // Returns true if the excess overhead (actual - target) for the FEC is below
84   // the amount `kMaxExcessOverhead`. This effects the lower protection level
85   // cases and low number of media packets/frame. The target overhead is given
86   // by `params_.fec_rate`, and is only achievable in the limit of large number
87   // of media packets.
88   bool ExcessOverheadBelowMax() const;
89 
90   // Returns true if the number of added media packets is at least
91   // `min_num_media_packets_`. This condition tries to capture the effect
92   // that, for the same amount of protection/overhead, longer codes
93   // (e.g. (2k,2m) vs (k,m)) are generally more effective at recovering losses.
94   bool MinimumMediaPacketsReached() const;
95 
96   void ResetState();
97 
98   const int red_payload_type_;
99   const int ulpfec_payload_type_;
100   Clock* const clock_;
101 
102   rtc::RaceChecker race_checker_;
103   const std::unique_ptr<ForwardErrorCorrection> fec_
104       RTC_GUARDED_BY(race_checker_);
105   ForwardErrorCorrection::PacketList media_packets_
106       RTC_GUARDED_BY(race_checker_);
107   absl::optional<RtpPacketToSend> last_media_packet_
108       RTC_GUARDED_BY(race_checker_);
109   std::list<ForwardErrorCorrection::Packet*> generated_fec_packets_
110       RTC_GUARDED_BY(race_checker_);
111   int num_protected_frames_ RTC_GUARDED_BY(race_checker_);
112   int min_num_media_packets_ RTC_GUARDED_BY(race_checker_);
113   Params current_params_ RTC_GUARDED_BY(race_checker_);
114   bool media_contains_keyframe_ RTC_GUARDED_BY(race_checker_);
115 
116   mutable Mutex mutex_;
117   absl::optional<Params> pending_params_ RTC_GUARDED_BY(mutex_);
118   RateStatistics fec_bitrate_ RTC_GUARDED_BY(mutex_);
119 };
120 
121 }  // namespace webrtc
122 
123 #endif  // MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_
124