Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 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 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <stddef.h> |
| 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 16 | #include <atomic> |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 17 | #include <map> |
| 18 | #include <memory> |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 21 | #include "absl/types/optional.h" |
| 22 | #include "api/fec_controller.h" |
Danil Chapovalov | 83bbe91 | 2019-08-07 12:24:53 +0200 | [diff] [blame] | 23 | #include "api/rtc_event_log/rtc_event_log.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 24 | #include "api/video/encoded_image.h" |
| 25 | #include "api/video/video_bitrate_allocation.h" |
Jiawei Ou | 4206a0a | 2018-07-20 15:49:43 -0700 | [diff] [blame] | 26 | #include "api/video/video_bitrate_allocator.h" |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 27 | #include "api/video/video_stream_encoder_interface.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 28 | #include "api/video_codecs/video_encoder.h" |
| 29 | #include "api/video_codecs/video_encoder_config.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 30 | #include "call/bitrate_allocator.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 31 | #include "call/rtp_config.h" |
| 32 | #include "call/rtp_transport_controller_send_interface.h" |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 33 | #include "call/rtp_video_sender_interface.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 34 | #include "modules/include/module_common_types.h" |
| 35 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 36 | #include "modules/utility/include/process_thread.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 37 | #include "modules/video_coding/include/video_codec_interface.h" |
Jonas Olsson | 0182a03 | 2019-07-09 12:31:20 +0200 | [diff] [blame] | 38 | #include "rtc_base/experiments/field_trial_parser.h" |
Markus Handell | a376518 | 2020-07-08 13:13:32 +0200 | [diff] [blame] | 39 | #include "rtc_base/synchronization/mutex.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 40 | #include "rtc_base/task_queue.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 41 | #include "rtc_base/task_utils/repeating_task.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 42 | #include "rtc_base/thread_annotations.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 43 | #include "rtc_base/weak_ptr.h" |
Elad Alon | 14d1c9d | 2019-04-08 14:16:17 +0200 | [diff] [blame] | 44 | #include "video/encoder_rtcp_feedback.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 45 | #include "video/send_delay_stats.h" |
| 46 | #include "video/send_statistics_proxy.h" |
| 47 | #include "video/video_send_stream.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 48 | |
| 49 | namespace webrtc { |
| 50 | namespace internal { |
| 51 | |
Christoffer Rodbro | 196c5ba | 2018-11-27 11:56:25 +0100 | [diff] [blame] | 52 | // Pacing buffer config; overridden by ALR config if provided. |
| 53 | struct PacingConfig { |
| 54 | PacingConfig(); |
| 55 | PacingConfig(const PacingConfig&); |
| 56 | PacingConfig& operator=(const PacingConfig&) = default; |
| 57 | ~PacingConfig(); |
| 58 | FieldTrialParameter<double> pacing_factor; |
| 59 | FieldTrialParameter<TimeDelta> max_pacing_delay; |
| 60 | }; |
| 61 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 62 | // VideoSendStreamImpl implements internal::VideoSendStream. |
| 63 | // It is created and destroyed on |worker_queue|. The intent is to decrease the |
| 64 | // need for locking and to ensure methods are called in sequence. |
| 65 | // Public methods except |DeliverRtcp| must be called on |worker_queue|. |
| 66 | // DeliverRtcp is called on the libjingle worker thread or a network thread. |
| 67 | // An encoder may deliver frames through the EncodedImageCallback on an |
| 68 | // arbitrary thread. |
| 69 | class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver, |
Per Kjellander | dcef641 | 2020-10-07 15:09:05 +0200 | [diff] [blame] | 70 | public VideoStreamEncoderInterface::EncoderSink { |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 71 | public: |
| 72 | VideoSendStreamImpl( |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 73 | Clock* clock, |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 74 | SendStatisticsProxy* stats_proxy, |
| 75 | rtc::TaskQueue* worker_queue, |
Tommi | 8ae18ad | 2020-05-03 22:45:02 +0200 | [diff] [blame] | 76 | RtcpRttStats* call_stats, |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 77 | RtpTransportControllerSendInterface* transport, |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 78 | BitrateAllocatorInterface* bitrate_allocator, |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 79 | SendDelayStats* send_delay_stats, |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 80 | VideoStreamEncoderInterface* video_stream_encoder, |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 81 | RtcEventLog* event_log, |
| 82 | const VideoSendStream::Config* config, |
| 83 | int initial_encoder_max_bitrate, |
| 84 | double initial_encoder_bitrate_priority, |
| 85 | std::map<uint32_t, RtpState> suspended_ssrcs, |
| 86 | std::map<uint32_t, RtpPayloadState> suspended_payload_states, |
| 87 | VideoEncoderConfig::ContentType content_type, |
Bjorn A Mellem | 7a9a092 | 2019-11-26 09:19:40 -0800 | [diff] [blame] | 88 | std::unique_ptr<FecController> fec_controller); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 89 | ~VideoSendStreamImpl() override; |
| 90 | |
| 91 | // RegisterProcessThread register |module_process_thread| with those objects |
| 92 | // that use it. Registration has to happen on the thread were |
| 93 | // |module_process_thread| was created (libjingle's worker thread). |
| 94 | // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue, |
| 95 | // maybe |worker_queue|. |
| 96 | void RegisterProcessThread(ProcessThread* module_process_thread); |
| 97 | void DeRegisterProcessThread(); |
| 98 | |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 99 | void DeliverRtcp(const uint8_t* packet, size_t length); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 100 | void UpdateActiveSimulcastLayers(const std::vector<bool> active_layers); |
| 101 | void Start(); |
| 102 | void Stop(); |
| 103 | |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 104 | // TODO(holmer): Move these to RtpTransportControllerSend. |
| 105 | std::map<uint32_t, RtpState> GetRtpStates() const; |
| 106 | |
| 107 | std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 108 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 109 | absl::optional<float> configured_pacing_factor_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 110 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 111 | private: |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 112 | // Implements BitrateAllocatorObserver. |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 113 | uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 114 | |
Per Kjellander | dcef641 | 2020-10-07 15:09:05 +0200 | [diff] [blame] | 115 | // Implements VideoStreamEncoderInterface::EncoderSink |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 116 | void OnEncoderConfigurationChanged( |
| 117 | std::vector<VideoStream> streams, |
Ilya Nikolaevskiy | 93be66c | 2020-04-02 14:10:27 +0200 | [diff] [blame] | 118 | bool is_svc, |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 119 | VideoEncoderConfig::ContentType content_type, |
| 120 | int min_transmit_bitrate_bps) override; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 121 | |
Per Kjellander | dcef641 | 2020-10-07 15:09:05 +0200 | [diff] [blame] | 122 | void OnBitrateAllocationUpdated( |
| 123 | const VideoBitrateAllocation& allocation) override; |
Per Kjellander | a943484 | 2020-10-15 17:53:22 +0200 | [diff] [blame] | 124 | void OnVideoLayersAllocationUpdated( |
| 125 | VideoLayersAllocation allocation) override; |
Per Kjellander | dcef641 | 2020-10-07 15:09:05 +0200 | [diff] [blame] | 126 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 127 | // Implements EncodedImageCallback. The implementation routes encoded frames |
| 128 | // to the |payload_router_| and |config.pre_encode_callback| if set. |
| 129 | // Called on an arbitrary encoder callback thread. |
| 130 | EncodedImageCallback::Result OnEncodedImage( |
| 131 | const EncodedImage& encoded_image, |
Danil Chapovalov | 2549f17 | 2020-08-12 17:30:36 +0200 | [diff] [blame] | 132 | const CodecSpecificInfo* codec_specific_info) override; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 133 | |
Jakob Ivarsson | 159b417 | 2019-10-30 14:02:14 +0100 | [diff] [blame] | 134 | // Implements EncodedImageCallback. |
| 135 | void OnDroppedFrame(EncodedImageCallback::DropReason reason) override; |
| 136 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 137 | // Starts monitoring and sends a keyframe. |
| 138 | void StartupVideoSendStream(); |
| 139 | // Removes the bitrate observer, stops monitoring and notifies the video |
| 140 | // encoder of the bitrate update. |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 141 | void StopVideoSendStream() RTC_RUN_ON(worker_queue_); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 142 | |
| 143 | void ConfigureProtection(); |
| 144 | void ConfigureSsrcs(); |
| 145 | void SignalEncoderTimedOut(); |
| 146 | void SignalEncoderActive(); |
Sebastian Jansson | 464a557 | 2019-02-12 13:32:32 +0100 | [diff] [blame] | 147 | MediaStreamAllocationConfig GetAllocationConfig() const |
| 148 | RTC_RUN_ON(worker_queue_); |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 149 | Clock* const clock_; |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 150 | const bool has_alr_probing_; |
Christoffer Rodbro | 196c5ba | 2018-11-27 11:56:25 +0100 | [diff] [blame] | 151 | const PacingConfig pacing_config_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 152 | |
| 153 | SendStatisticsProxy* const stats_proxy_; |
| 154 | const VideoSendStream::Config* const config_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 155 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 156 | rtc::TaskQueue* const worker_queue_; |
| 157 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 158 | RepeatingTaskHandle check_encoder_activity_task_ |
| 159 | RTC_GUARDED_BY(worker_queue_); |
| 160 | |
| 161 | std::atomic_bool activity_; |
| 162 | bool timed_out_ RTC_GUARDED_BY(worker_queue_); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 163 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 164 | RtpTransportControllerSendInterface* const transport_; |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 165 | BitrateAllocatorInterface* const bitrate_allocator_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 166 | |
Markus Handell | a376518 | 2020-07-08 13:13:32 +0200 | [diff] [blame] | 167 | Mutex ivf_writers_mutex_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 168 | |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 169 | bool disable_padding_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 170 | int max_padding_bitrate_; |
| 171 | int encoder_min_bitrate_bps_; |
| 172 | uint32_t encoder_max_bitrate_bps_; |
| 173 | uint32_t encoder_target_rate_bps_; |
| 174 | double encoder_bitrate_priority_; |
| 175 | bool has_packet_feedback_; |
| 176 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 177 | VideoStreamEncoderInterface* const video_stream_encoder_; |
Elad Alon | 14d1c9d | 2019-04-08 14:16:17 +0200 | [diff] [blame] | 178 | EncoderRtcpFeedback encoder_feedback_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 179 | |
| 180 | RtcpBandwidthObserver* const bandwidth_observer_; |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 181 | RtpVideoSenderInterface* const rtp_video_sender_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 182 | |
| 183 | // |weak_ptr_| to our self. This is used since we can not call |
| 184 | // |weak_ptr_factory_.GetWeakPtr| from multiple sequences but it is ok to copy |
| 185 | // an existing WeakPtr. |
| 186 | rtc::WeakPtr<VideoSendStreamImpl> weak_ptr_; |
| 187 | // |weak_ptr_factory_| must be declared last to make sure all WeakPtr's are |
| 188 | // invalidated before any other members are destroyed. |
| 189 | rtc::WeakPtrFactory<VideoSendStreamImpl> weak_ptr_factory_; |
| 190 | |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 191 | // Context for the most recent and last sent video bitrate allocation. Used to |
| 192 | // throttle sending of similar bitrate allocations. |
| 193 | struct VbaSendContext { |
| 194 | VideoBitrateAllocation last_sent_allocation; |
| 195 | absl::optional<VideoBitrateAllocation> throttled_allocation; |
| 196 | int64_t last_send_time_ms; |
| 197 | }; |
| 198 | absl::optional<VbaSendContext> video_bitrate_allocation_context_ |
| 199 | RTC_GUARDED_BY(worker_queue_); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 200 | }; |
| 201 | } // namespace internal |
| 202 | } // namespace webrtc |
| 203 | #endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_ |