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