xref: /aosp_15_r20/external/webrtc/modules/rtp_rtcp/include/flexfec_sender.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2016 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_INCLUDE_FLEXFEC_SENDER_H_
12 #define MODULES_RTP_RTCP_INCLUDE_FLEXFEC_SENDER_H_
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include "absl/strings/string_view.h"
19 #include "api/array_view.h"
20 #include "api/rtp_parameters.h"
21 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
22 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
23 #include "modules/rtp_rtcp/source/rtp_header_extension_size.h"
24 #include "modules/rtp_rtcp/source/ulpfec_generator.h"
25 #include "modules/rtp_rtcp/source/video_fec_generator.h"
26 #include "rtc_base/random.h"
27 #include "rtc_base/rate_statistics.h"
28 #include "rtc_base/synchronization/mutex.h"
29 
30 namespace webrtc {
31 
32 class Clock;
33 class RtpPacketToSend;
34 
35 // Note that this class is not thread safe, and thus requires external
36 // synchronization. Currently, this is done using the lock in PayloadRouter.
37 
38 class FlexfecSender : public VideoFecGenerator {
39  public:
40   FlexfecSender(int payload_type,
41                 uint32_t ssrc,
42                 uint32_t protected_media_ssrc,
43                 absl::string_view mid,
44                 const std::vector<RtpExtension>& rtp_header_extensions,
45                 rtc::ArrayView<const RtpExtensionSize> extension_sizes,
46                 const RtpState* rtp_state,
47                 Clock* clock);
48   ~FlexfecSender();
49 
GetFecType()50   FecType GetFecType() const override {
51     return VideoFecGenerator::FecType::kFlexFec;
52   }
FecSsrc()53   absl::optional<uint32_t> FecSsrc() override { return ssrc_; }
54 
55   // Sets the FEC rate, max frames sent before FEC packets are sent,
56   // and what type of generator matrices are used.
57   void SetProtectionParameters(const FecProtectionParams& delta_params,
58                                const FecProtectionParams& key_params) override;
59 
60   // Adds a media packet to the internal buffer. When enough media packets
61   // have been added, the FEC packets are generated and stored internally.
62   // These FEC packets are then obtained by calling GetFecPackets().
63   void AddPacketAndGenerateFec(const RtpPacketToSend& packet) override;
64 
65   // Returns generated FlexFEC packets.
66   std::vector<std::unique_ptr<RtpPacketToSend>> GetFecPackets() override;
67 
68   // Returns the overhead, per packet, for FlexFEC.
69   size_t MaxPacketOverhead() const override;
70 
71   DataRate CurrentFecRate() const override;
72 
73   // Only called on the VideoSendStream queue, after operation has shut down.
74   absl::optional<RtpState> GetRtpState() override;
75 
76  private:
77   // Utility.
78   Clock* const clock_;
79   Random random_;
80   int64_t last_generated_packet_ms_;
81 
82   // Config.
83   const int payload_type_;
84   const uint32_t timestamp_offset_;
85   const uint32_t ssrc_;
86   const uint32_t protected_media_ssrc_;
87   // MID value to send in the MID header extension.
88   const std::string mid_;
89   // Sequence number of next packet to generate.
90   uint16_t seq_num_;
91 
92   // Implementation.
93   UlpfecGenerator ulpfec_generator_;
94   const RtpHeaderExtensionMap rtp_header_extension_map_;
95   const size_t header_extensions_size_;
96 
97   mutable Mutex mutex_;
98   RateStatistics fec_bitrate_ RTC_GUARDED_BY(mutex_);
99 };
100 
101 }  // namespace webrtc
102 
103 #endif  // MODULES_RTP_RTCP_INCLUDE_FLEXFEC_SENDER_H_
104