1 /* 2 * Copyright 2018 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 VIDEO_VIDEO_SEND_STREAM_IMPL_H_ 11 #define VIDEO_VIDEO_SEND_STREAM_IMPL_H_ 12 13 #include <stddef.h> 14 #include <stdint.h> 15 16 #include <atomic> 17 #include <map> 18 #include <memory> 19 #include <vector> 20 21 #include "absl/types/optional.h" 22 #include "api/field_trials_view.h" 23 #include "api/task_queue/pending_task_safety_flag.h" 24 #include "api/task_queue/task_queue_base.h" 25 #include "api/video/encoded_image.h" 26 #include "api/video/video_bitrate_allocation.h" 27 #include "api/video/video_bitrate_allocator.h" 28 #include "api/video_codecs/video_encoder.h" 29 #include "call/bitrate_allocator.h" 30 #include "call/rtp_config.h" 31 #include "call/rtp_transport_controller_send_interface.h" 32 #include "call/rtp_video_sender_interface.h" 33 #include "modules/include/module_common_types.h" 34 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" 35 #include "modules/utility/maybe_worker_thread.h" 36 #include "modules/video_coding/include/video_codec_interface.h" 37 #include "rtc_base/experiments/field_trial_parser.h" 38 #include "rtc_base/system/no_unique_address.h" 39 #include "rtc_base/task_utils/repeating_task.h" 40 #include "rtc_base/thread_annotations.h" 41 #include "video/config/video_encoder_config.h" 42 #include "video/send_statistics_proxy.h" 43 #include "video/video_stream_encoder_interface.h" 44 45 namespace webrtc { 46 namespace internal { 47 48 // Pacing buffer config; overridden by ALR config if provided. 49 struct PacingConfig { 50 explicit PacingConfig(const FieldTrialsView& field_trials); 51 PacingConfig(const PacingConfig&); 52 PacingConfig& operator=(const PacingConfig&) = default; 53 ~PacingConfig(); 54 FieldTrialParameter<double> pacing_factor; 55 FieldTrialParameter<TimeDelta> max_pacing_delay; 56 }; 57 58 // VideoSendStreamImpl implements internal::VideoSendStream. 59 // It is created and destroyed on `rtp_transport_queue`. The intent is to 60 // decrease the need for locking and to ensure methods are called in sequence. 61 // Public methods except `DeliverRtcp` must be called on `rtp_transport_queue`. 62 // DeliverRtcp is called on the libjingle worker thread or a network thread. 63 // An encoder may deliver frames through the EncodedImageCallback on an 64 // arbitrary thread. 65 class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver, 66 public VideoStreamEncoderInterface::EncoderSink { 67 public: 68 VideoSendStreamImpl(Clock* clock, 69 SendStatisticsProxy* stats_proxy, 70 RtpTransportControllerSendInterface* transport, 71 BitrateAllocatorInterface* bitrate_allocator, 72 VideoStreamEncoderInterface* video_stream_encoder, 73 const VideoSendStream::Config* config, 74 int initial_encoder_max_bitrate, 75 double initial_encoder_bitrate_priority, 76 VideoEncoderConfig::ContentType content_type, 77 RtpVideoSenderInterface* rtp_video_sender, 78 const FieldTrialsView& field_trials); 79 ~VideoSendStreamImpl() override; 80 81 void DeliverRtcp(const uint8_t* packet, size_t length); 82 void StartPerRtpStream(std::vector<bool> active_layers); 83 void Stop(); 84 85 // TODO(holmer): Move these to RtpTransportControllerSend. 86 std::map<uint32_t, RtpState> GetRtpStates() const; 87 88 std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const; 89 configured_pacing_factor()90 const absl::optional<float>& configured_pacing_factor() const { 91 return configured_pacing_factor_; 92 } 93 94 private: 95 // Implements BitrateAllocatorObserver. 96 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override; 97 98 // Implements VideoStreamEncoderInterface::EncoderSink 99 void OnEncoderConfigurationChanged( 100 std::vector<VideoStream> streams, 101 bool is_svc, 102 VideoEncoderConfig::ContentType content_type, 103 int min_transmit_bitrate_bps) override; 104 105 void OnBitrateAllocationUpdated( 106 const VideoBitrateAllocation& allocation) override; 107 void OnVideoLayersAllocationUpdated( 108 VideoLayersAllocation allocation) override; 109 110 // Implements EncodedImageCallback. The implementation routes encoded frames 111 // to the `payload_router_` and `config.pre_encode_callback` if set. 112 // Called on an arbitrary encoder callback thread. 113 EncodedImageCallback::Result OnEncodedImage( 114 const EncodedImage& encoded_image, 115 const CodecSpecificInfo* codec_specific_info) override; 116 117 // Implements EncodedImageCallback. 118 void OnDroppedFrame(EncodedImageCallback::DropReason reason) override; 119 120 // Starts monitoring and sends a keyframe. 121 void StartupVideoSendStream(); 122 // Removes the bitrate observer, stops monitoring and notifies the video 123 // encoder of the bitrate update. 124 void StopVideoSendStream() RTC_RUN_ON(rtp_transport_queue_); 125 126 void ConfigureProtection(); 127 void ConfigureSsrcs(); 128 void SignalEncoderTimedOut(); 129 void SignalEncoderActive(); 130 MediaStreamAllocationConfig GetAllocationConfig() const 131 RTC_RUN_ON(rtp_transport_queue_); 132 133 RTC_NO_UNIQUE_ADDRESS SequenceChecker thread_checker_; 134 Clock* const clock_; 135 const bool has_alr_probing_; 136 const PacingConfig pacing_config_; 137 138 SendStatisticsProxy* const stats_proxy_; 139 const VideoSendStream::Config* const config_; 140 141 MaybeWorkerThread* const rtp_transport_queue_; 142 143 RepeatingTaskHandle check_encoder_activity_task_ 144 RTC_GUARDED_BY(rtp_transport_queue_); 145 146 std::atomic_bool activity_; 147 bool timed_out_ RTC_GUARDED_BY(rtp_transport_queue_); 148 149 RtpTransportControllerSendInterface* const transport_; 150 BitrateAllocatorInterface* const bitrate_allocator_; 151 152 bool disable_padding_; 153 int max_padding_bitrate_; 154 int encoder_min_bitrate_bps_; 155 uint32_t encoder_max_bitrate_bps_; 156 uint32_t encoder_target_rate_bps_; 157 double encoder_bitrate_priority_; 158 159 VideoStreamEncoderInterface* const video_stream_encoder_; 160 161 RtcpBandwidthObserver* const bandwidth_observer_; 162 RtpVideoSenderInterface* const rtp_video_sender_; 163 164 rtc::scoped_refptr<PendingTaskSafetyFlag> transport_queue_safety_ = 165 PendingTaskSafetyFlag::CreateDetached(); 166 167 // Context for the most recent and last sent video bitrate allocation. Used to 168 // throttle sending of similar bitrate allocations. 169 struct VbaSendContext { 170 VideoBitrateAllocation last_sent_allocation; 171 absl::optional<VideoBitrateAllocation> throttled_allocation; 172 int64_t last_send_time_ms; 173 }; 174 absl::optional<VbaSendContext> video_bitrate_allocation_context_ 175 RTC_GUARDED_BY(rtp_transport_queue_); 176 const absl::optional<float> configured_pacing_factor_; 177 }; 178 } // namespace internal 179 } // namespace webrtc 180 #endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_ 181