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