ulpfec_generator.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_
  11. #define MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <list>
  15. #include <memory>
  16. #include <vector>
  17. #include "modules/include/module_fec_types.h"
  18. #include "modules/rtp_rtcp/source/forward_error_correction.h"
  19. #include "modules/rtp_rtcp/source/video_fec_generator.h"
  20. #include "rtc_base/race_checker.h"
  21. #include "rtc_base/rate_statistics.h"
  22. #include "rtc_base/synchronization/mutex.h"
  23. namespace webrtc {
  24. class FlexfecSender;
  25. class UlpfecGenerator : public VideoFecGenerator {
  26. friend class FlexfecSender;
  27. public:
  28. UlpfecGenerator(int red_payload_type, int ulpfec_payload_type, Clock* clock);
  29. ~UlpfecGenerator();
  30. FecType GetFecType() const override {
  31. return VideoFecGenerator::FecType::kUlpFec;
  32. }
  33. absl::optional<uint32_t> FecSsrc() override { return absl::nullopt; }
  34. void SetProtectionParameters(const FecProtectionParams& delta_params,
  35. const FecProtectionParams& key_params) override;
  36. // Adds a media packet to the internal buffer. When enough media packets
  37. // have been added, the FEC packets are generated and stored internally.
  38. // These FEC packets are then obtained by calling GetFecPacketsAsRed().
  39. void AddPacketAndGenerateFec(const RtpPacketToSend& packet) override;
  40. // Returns the overhead, per packet, for FEC (and possibly RED).
  41. size_t MaxPacketOverhead() const override;
  42. std::vector<std::unique_ptr<RtpPacketToSend>> GetFecPackets() override;
  43. // Current rate of FEC packets generated, including all RTP-level headers.
  44. DataRate CurrentFecRate() const override;
  45. absl::optional<RtpState> GetRtpState() override { return absl::nullopt; }
  46. private:
  47. struct Params {
  48. Params();
  49. Params(FecProtectionParams delta_params,
  50. FecProtectionParams keyframe_params);
  51. FecProtectionParams delta_params;
  52. FecProtectionParams keyframe_params;
  53. };
  54. UlpfecGenerator(std::unique_ptr<ForwardErrorCorrection> fec, Clock* clock);
  55. // Overhead is defined as relative to the number of media packets, and not
  56. // relative to total number of packets. This definition is inherited from the
  57. // protection factor produced by video_coding module and how the FEC
  58. // generation is implemented.
  59. int Overhead() const;
  60. // Returns true if the excess overhead (actual - target) for the FEC is below
  61. // the amount |kMaxExcessOverhead|. This effects the lower protection level
  62. // cases and low number of media packets/frame. The target overhead is given
  63. // by |params_.fec_rate|, and is only achievable in the limit of large number
  64. // of media packets.
  65. bool ExcessOverheadBelowMax() const;
  66. // Returns true if the number of added media packets is at least
  67. // |min_num_media_packets_|. This condition tries to capture the effect
  68. // that, for the same amount of protection/overhead, longer codes
  69. // (e.g. (2k,2m) vs (k,m)) are generally more effective at recovering losses.
  70. bool MinimumMediaPacketsReached() const;
  71. const FecProtectionParams& CurrentParams() const;
  72. void ResetState();
  73. const int red_payload_type_;
  74. const int ulpfec_payload_type_;
  75. Clock* const clock_;
  76. rtc::RaceChecker race_checker_;
  77. const std::unique_ptr<ForwardErrorCorrection> fec_
  78. RTC_GUARDED_BY(race_checker_);
  79. ForwardErrorCorrection::PacketList media_packets_
  80. RTC_GUARDED_BY(race_checker_);
  81. absl::optional<RtpPacketToSend> last_media_packet_
  82. RTC_GUARDED_BY(race_checker_);
  83. std::list<ForwardErrorCorrection::Packet*> generated_fec_packets_
  84. RTC_GUARDED_BY(race_checker_);
  85. int num_protected_frames_ RTC_GUARDED_BY(race_checker_);
  86. int min_num_media_packets_ RTC_GUARDED_BY(race_checker_);
  87. Params current_params_ RTC_GUARDED_BY(race_checker_);
  88. bool keyframe_in_process_ RTC_GUARDED_BY(race_checker_);
  89. mutable Mutex mutex_;
  90. absl::optional<Params> pending_params_ RTC_GUARDED_BY(mutex_);
  91. RateStatistics fec_bitrate_ RTC_GUARDED_BY(mutex_);
  92. };
  93. } // namespace webrtc
  94. #endif // MODULES_RTP_RTCP_SOURCE_ULPFEC_GENERATOR_H_