| 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 CALL_RTP_VIDEO_SENDER_H_ |
| 12 | #define CALL_RTP_VIDEO_SENDER_H_ |
| 13 |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <unordered_set> |
| 17 | #include <vector> |
| 18 |
| 19 | #include "absl/types/optional.h" |
| 20 | #include "api/array_view.h" |
| 21 | #include "api/call/transport.h" |
| 22 | #include "api/fec_controller.h" |
| 23 | #include "api/fec_controller_override.h" |
| 24 | #include "api/field_trials_view.h" |
| 25 | #include "api/rtc_event_log/rtc_event_log.h" |
| 26 | #include "api/sequence_checker.h" |
| 27 | #include "api/task_queue/task_queue_base.h" |
| 28 | #include "api/task_queue/task_queue_factory.h" |
| 29 | #include "api/video_codecs/video_encoder.h" |
| 30 | #include "call/rtp_config.h" |
| 31 | #include "call/rtp_payload_params.h" |
| 32 | #include "call/rtp_transport_controller_send_interface.h" |
| 33 | #include "call/rtp_video_sender_interface.h" |
| 34 | #include "modules/rtp_rtcp/include/flexfec_sender.h" |
| 35 | #include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h" |
| 36 | #include "modules/rtp_rtcp/source/rtp_sender.h" |
| 37 | #include "modules/rtp_rtcp/source/rtp_sender_video.h" |
| 38 | #include "modules/rtp_rtcp/source/rtp_sequence_number_map.h" |
| 39 | #include "modules/rtp_rtcp/source/rtp_video_header.h" |
| 40 | #include "rtc_base/rate_limiter.h" |
| 41 | #include "rtc_base/synchronization/mutex.h" |
| 42 | #include "rtc_base/thread_annotations.h" |
| 43 |
| 44 | namespace webrtc { |
| 45 |
| 46 | class FrameEncryptorInterface; |
| 47 | class RtpTransportControllerSendInterface; |
| 48 |
| 49 | namespace webrtc_internal_rtp_video_sender { |
| 50 | // RTP state for a single simulcast stream. Internal to the implementation of |
| 51 | // RtpVideoSender. |
| 52 | struct RtpStreamSender { |
| 53 | RtpStreamSender(std::unique_ptr<ModuleRtpRtcpImpl2> rtp_rtcp, |
| 54 | std::unique_ptr<RTPSenderVideo> sender_video, |
| 55 | std::unique_ptr<VideoFecGenerator> fec_generator); |
| 56 | ~RtpStreamSender(); |
| 57 |
| 58 | RtpStreamSender(RtpStreamSender&&) = default; |
| 59 | RtpStreamSender& operator=(RtpStreamSender&&) = default; |
| 60 |
| 61 | // Note: Needs pointer stability. |
| 62 | std::unique_ptr<ModuleRtpRtcpImpl2> rtp_rtcp; |
| 63 | std::unique_ptr<RTPSenderVideo> sender_video; |
| 64 | std::unique_ptr<VideoFecGenerator> fec_generator; |
| 65 | }; |
| 66 |
| 67 | } // namespace webrtc_internal_rtp_video_sender |
| 68 |
| 69 | // RtpVideoSender routes outgoing data to the correct sending RTP module, based |
| 70 | // on the simulcast layer in RTPVideoHeader. |
| 71 | class RtpVideoSender : public RtpVideoSenderInterface, |
| 72 | public VCMProtectionCallback, |
| 73 | public StreamFeedbackObserver { |
| 74 | public: |
| 75 | // Rtp modules are assumed to be sorted in simulcast index order. |
| 76 | RtpVideoSender( |
| 77 | Clock* clock, |
| 78 | const std::map<uint32_t, RtpState>& suspended_ssrcs, |
| 79 | const std::map<uint32_t, RtpPayloadState>& states, |
| 80 | const RtpConfig& rtp_config, |
| 81 | int rtcp_report_interval_ms, |
| 82 | Transport* send_transport, |
| 83 | const RtpSenderObservers& observers, |
| 84 | RtpTransportControllerSendInterface* transport, |
| 85 | RtcEventLog* event_log, |
| 86 | RateLimiter* retransmission_limiter, // move inside RtpTransport |
| 87 | std::unique_ptr<FecController> fec_controller, |
| 88 | FrameEncryptorInterface* frame_encryptor, |
| 89 | const CryptoOptions& crypto_options, // move inside RtpTransport |
| 90 | rtc::scoped_refptr<FrameTransformerInterface> frame_transformer, |
| 91 | const FieldTrialsView& field_trials, |
| 92 | TaskQueueFactory* task_queue_factory); |
| 93 | ~RtpVideoSender() override; |
| 94 |
| 95 | RtpVideoSender(const RtpVideoSender&) = delete; |
| 96 | RtpVideoSender& operator=(const RtpVideoSender&) = delete; |
| 97 |
| 98 | void SetSending(bool enabled) RTC_LOCKS_EXCLUDED(mutex_) override; |
| 99 | bool IsActive() RTC_LOCKS_EXCLUDED(mutex_) override; |
| 100 |
| 101 | void OnNetworkAvailability(bool network_available) |
| 102 | RTC_LOCKS_EXCLUDED(mutex_) override; |
| 103 | std::map<uint32_t, RtpState> GetRtpStates() const |
| 104 | RTC_LOCKS_EXCLUDED(mutex_) override; |
| 105 | std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const |
| 106 | RTC_LOCKS_EXCLUDED(mutex_) override; |
| 107 |
| 108 | void DeliverRtcp(const uint8_t* packet, size_t length) |
| 109 | RTC_LOCKS_EXCLUDED(mutex_) override; |
| 110 |
| 111 | // Implements webrtc::VCMProtectionCallback. |
| 112 | int ProtectionRequest(const FecProtectionParams* delta_params, |
| 113 | const FecProtectionParams* key_params, |
| 114 | uint32_t* sent_video_rate_bps, |
| 115 | uint32_t* sent_nack_rate_bps, |
| 116 | uint32_t* sent_fec_rate_bps) |
| 117 | RTC_LOCKS_EXCLUDED(mutex_) override; |
| 118 |
| 119 | // 'retransmission_mode' is either a value of enum RetransmissionMode, or |
| 120 | // computed with bitwise operators on values of enum RetransmissionMode. |
| 121 | void SetRetransmissionMode(int retransmission_mode) |
| 122 | RTC_LOCKS_EXCLUDED(mutex_) override; |
| 123 |
| 124 | // Implements FecControllerOverride. |
| 125 | void SetFecAllowed(bool fec_allowed) RTC_LOCKS_EXCLUDED(mutex_) override; |
| 126 |
| 127 | // Implements EncodedImageCallback. |
| 128 | // Returns 0 if the packet was routed / sent, -1 otherwise. |
| 129 | EncodedImageCallback::Result OnEncodedImage( |
| 130 | const EncodedImage& encoded_image, |
| 131 | const CodecSpecificInfo* codec_specific_info) |
| 132 | RTC_LOCKS_EXCLUDED(mutex_) override; |
| 133 |
| 134 | void OnBitrateAllocationUpdated(const VideoBitrateAllocation& bitrate) |
| 135 | RTC_LOCKS_EXCLUDED(mutex_) override; |
| 136 | void OnVideoLayersAllocationUpdated( |
| 137 | const VideoLayersAllocation& layers) override; |
| 138 | void OnTransportOverheadChanged(size_t transport_overhead_bytes_per_packet) |
| 139 | RTC_LOCKS_EXCLUDED(mutex_) override; |
| 140 | void OnBitrateUpdated(BitrateAllocationUpdate update, int framerate) |
| 141 | RTC_LOCKS_EXCLUDED(mutex_) override; |
| 142 | uint32_t GetPayloadBitrateBps() const RTC_LOCKS_EXCLUDED(mutex_) override; |
| 143 | uint32_t GetProtectionBitrateBps() const RTC_LOCKS_EXCLUDED(mutex_) override; |
| 144 | void SetEncodingData(size_t width, size_t height, size_t num_temporal_layers) |
| 145 | RTC_LOCKS_EXCLUDED(mutex_) override; |
| 146 |
| 147 | std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos( |
| 148 | uint32_t ssrc, |
| 149 | rtc::ArrayView<const uint16_t> sequence_numbers) const |
| 150 | RTC_LOCKS_EXCLUDED(mutex_) override; |
| 151 |
| 152 | // From StreamFeedbackObserver. |
| 153 | void OnPacketFeedbackVector( |
| 154 | std::vector<StreamPacketInfo> packet_feedback_vector) |
| 155 | RTC_LOCKS_EXCLUDED(mutex_) override; |
| 156 |
| 157 | private: |
| 158 | bool IsActiveLocked() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 159 | void SetActiveModulesLocked(bool sending) |
| 160 | RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 161 | void UpdateModuleSendingState() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 162 | void ConfigureProtection(); |
| 163 | void ConfigureSsrcs(const std::map<uint32_t, RtpState>& suspended_ssrcs); |
| 164 | bool NackEnabled() const; |
| 165 | DataRate GetPostEncodeOverhead() const; |
| 166 | DataRate CalculateOverheadRate(DataRate data_rate, |
| 167 | DataSize packet_size, |
| 168 | DataSize overhead_per_packet, |
| 169 | Frequency framerate) const; |
| 170 |
| 171 | const FieldTrialsView& field_trials_; |
| 172 | const bool use_frame_rate_for_overhead_; |
| 173 | const bool has_packet_feedback_; |
| 174 |
| 175 | // Semantically equivalent to checking for `transport_->GetWorkerQueue()` |
| 176 | // but some tests need to be updated to call from the correct context. |
| 177 | RTC_NO_UNIQUE_ADDRESS SequenceChecker transport_checker_; |
| 178 |
| 179 | // TODO(bugs.webrtc.org/13517): Remove mutex_ once RtpVideoSender runs on the |
| 180 | // transport task queue. |
| 181 | mutable Mutex mutex_; |
| 182 | bool active_ RTC_GUARDED_BY(mutex_); |
| 183 |
| 184 | const std::unique_ptr<FecController> fec_controller_; |
| 185 | bool fec_allowed_ RTC_GUARDED_BY(mutex_); |
| 186 |
| 187 | // Rtp modules are assumed to be sorted in simulcast index order. |
| 188 | const std::vector<webrtc_internal_rtp_video_sender::RtpStreamSender> |
| 189 | rtp_streams_; |
| 190 | const RtpConfig rtp_config_; |
| 191 | const absl::optional<VideoCodecType> codec_type_; |
| 192 | RtpTransportControllerSendInterface* const transport_; |
| 193 |
| 194 | // When using the generic descriptor we want all simulcast streams to share |
| 195 | // one frame id space (so that the SFU can switch stream without having to |
| 196 | // rewrite the frame id), therefore `shared_frame_id` has to live in a place |
| 197 | // where we are aware of all the different streams. |
| 198 | int64_t shared_frame_id_ = 0; |
| 199 | std::vector<RtpPayloadParams> params_ RTC_GUARDED_BY(mutex_); |
| 200 |
| 201 | size_t transport_overhead_bytes_per_packet_ RTC_GUARDED_BY(mutex_); |
| 202 | uint32_t protection_bitrate_bps_; |
| 203 | uint32_t encoder_target_rate_bps_; |
| 204 |
| 205 | std::vector<bool> loss_mask_vector_ RTC_GUARDED_BY(mutex_); |
| 206 |
| 207 | std::vector<FrameCounts> frame_counts_ RTC_GUARDED_BY(mutex_); |
| 208 | FrameCountObserver* const frame_count_observer_; |
| 209 |
| 210 | // Effectively const map from SSRC to RtpRtcp, for all media SSRCs. |
| 211 | // This map is set at construction time and never changed, but it's |
| 212 | // non-trivial to make it properly const. |
| 213 | std::map<uint32_t, RtpRtcpInterface*> ssrc_to_rtp_module_; |
| 214 | }; |
| 215 |
| 216 | } // namespace webrtc |
| 217 |
| 218 | #endif // CALL_RTP_VIDEO_SENDER_H_ |
| 219 |