flexfec_sender.h 3.4 KB

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