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" |
Jonas Oreland | e62c2f2 | 2022-03-29 11:04:48 +0200 | [diff] [blame] | 22 | #include "api/field_trials_view.h" |
Artem Titov | c374d11 | 2022-06-16 21:27:45 +0200 | [diff] [blame] | 23 | #include "api/task_queue/pending_task_safety_flag.h" |
Danil Chapovalov | 03f8b8a | 2022-07-18 13:11:42 +0200 | [diff] [blame] | 24 | #include "api/task_queue/task_queue_base.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 25 | #include "api/video/encoded_image.h" |
| 26 | #include "api/video/video_bitrate_allocation.h" |
Jiawei Ou | 4206a0a | 2018-07-20 15:49:43 -0700 | [diff] [blame] | 27 | #include "api/video/video_bitrate_allocator.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 28 | #include "api/video_codecs/video_encoder.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 29 | #include "call/bitrate_allocator.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 30 | #include "call/rtp_config.h" |
| 31 | #include "call/rtp_transport_controller_send_interface.h" |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 32 | #include "call/rtp_video_sender_interface.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 33 | #include "modules/include/module_common_types.h" |
| 34 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Per Kjellander | 828ef91 | 2022-10-10 12:53:41 +0200 | [diff] [blame] | 35 | #include "modules/utility/maybe_worker_thread.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 36 | #include "modules/video_coding/include/video_codec_interface.h" |
Jonas Olsson | 0182a03 | 2019-07-09 12:31:20 +0200 | [diff] [blame] | 37 | #include "rtc_base/experiments/field_trial_parser.h" |
Tommi | 1050fbc | 2021-06-03 17:58:28 +0200 | [diff] [blame] | 38 | #include "rtc_base/system/no_unique_address.h" |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 39 | #include "rtc_base/task_utils/repeating_task.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 40 | #include "rtc_base/thread_annotations.h" |
Jonas Oreland | 6c2dae2 | 2022-09-29 10:28:24 +0200 | [diff] [blame] | 41 | #include "video/config/video_encoder_config.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 42 | #include "video/send_statistics_proxy.h" |
Jonas Oreland | 6c2dae2 | 2022-09-29 10:28:24 +0200 | [diff] [blame] | 43 | #include "video/video_stream_encoder_interface.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 44 | |
| 45 | namespace webrtc { |
| 46 | namespace internal { |
| 47 | |
Christoffer Rodbro | 196c5ba | 2018-11-27 11:56:25 +0100 | [diff] [blame] | 48 | // Pacing buffer config; overridden by ALR config if provided. |
| 49 | struct PacingConfig { |
Jonas Oreland | e62c2f2 | 2022-03-29 11:04:48 +0200 | [diff] [blame] | 50 | explicit PacingConfig(const FieldTrialsView& field_trials); |
Christoffer Rodbro | 196c5ba | 2018-11-27 11:56:25 +0100 | [diff] [blame] | 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 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 58 | // VideoSendStreamImpl implements internal::VideoSendStream. |
Tommi | fa3ce63 | 2021-06-03 12:06:02 +0200 | [diff] [blame] | 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`. |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 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, |
Per Kjellander | dcef641 | 2020-10-07 15:09:05 +0200 | [diff] [blame] | 66 | public VideoStreamEncoderInterface::EncoderSink { |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 67 | public: |
Tommi | 1050fbc | 2021-06-03 17:58:28 +0200 | [diff] [blame] | 68 | VideoSendStreamImpl(Clock* clock, |
| 69 | SendStatisticsProxy* stats_proxy, |
Tommi | 1050fbc | 2021-06-03 17:58:28 +0200 | [diff] [blame] | 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, |
Jonas Oreland | 8ca0613 | 2022-03-14 12:52:48 +0100 | [diff] [blame] | 77 | RtpVideoSenderInterface* rtp_video_sender, |
Jonas Oreland | e62c2f2 | 2022-03-29 11:04:48 +0200 | [diff] [blame] | 78 | const FieldTrialsView& field_trials); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 79 | ~VideoSendStreamImpl() override; |
| 80 | |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 81 | void DeliverRtcp(const uint8_t* packet, size_t length); |
Per Kjellander | 75170be | 2022-11-24 12:32:19 +0000 | [diff] [blame] | 82 | void UpdateActiveSimulcastLayers(std::vector<bool> active_layers); |
| 83 | void Start(); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 84 | void Stop(); |
| 85 | |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 86 | // TODO(holmer): Move these to RtpTransportControllerSend. |
| 87 | std::map<uint32_t, RtpState> GetRtpStates() const; |
| 88 | |
| 89 | std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 90 | |
Tommi | e902f28 | 2021-06-03 12:02:02 +0200 | [diff] [blame] | 91 | const absl::optional<float>& configured_pacing_factor() const { |
| 92 | return configured_pacing_factor_; |
| 93 | } |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 94 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 95 | private: |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 96 | // Implements BitrateAllocatorObserver. |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 97 | uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 98 | |
Per Kjellander | dcef641 | 2020-10-07 15:09:05 +0200 | [diff] [blame] | 99 | // Implements VideoStreamEncoderInterface::EncoderSink |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 100 | void OnEncoderConfigurationChanged( |
| 101 | std::vector<VideoStream> streams, |
Ilya Nikolaevskiy | 93be66c | 2020-04-02 14:10:27 +0200 | [diff] [blame] | 102 | bool is_svc, |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 103 | VideoEncoderConfig::ContentType content_type, |
| 104 | int min_transmit_bitrate_bps) override; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 105 | |
Per Kjellander | dcef641 | 2020-10-07 15:09:05 +0200 | [diff] [blame] | 106 | void OnBitrateAllocationUpdated( |
| 107 | const VideoBitrateAllocation& allocation) override; |
Per Kjellander | a943484 | 2020-10-15 17:53:22 +0200 | [diff] [blame] | 108 | void OnVideoLayersAllocationUpdated( |
| 109 | VideoLayersAllocation allocation) override; |
Per Kjellander | dcef641 | 2020-10-07 15:09:05 +0200 | [diff] [blame] | 110 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 111 | // Implements EncodedImageCallback. The implementation routes encoded frames |
Artem Titov | cfea218 | 2021-08-10 01:22:31 +0200 | [diff] [blame] | 112 | // to the `payload_router_` and `config.pre_encode_callback` if set. |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 113 | // Called on an arbitrary encoder callback thread. |
| 114 | EncodedImageCallback::Result OnEncodedImage( |
| 115 | const EncodedImage& encoded_image, |
Danil Chapovalov | 2549f17 | 2020-08-12 17:30:36 +0200 | [diff] [blame] | 116 | const CodecSpecificInfo* codec_specific_info) override; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 117 | |
Jakob Ivarsson | 159b417 | 2019-10-30 14:02:14 +0100 | [diff] [blame] | 118 | // Implements EncodedImageCallback. |
| 119 | void OnDroppedFrame(EncodedImageCallback::DropReason reason) override; |
| 120 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 121 | // Starts monitoring and sends a keyframe. |
| 122 | void StartupVideoSendStream(); |
| 123 | // Removes the bitrate observer, stops monitoring and notifies the video |
| 124 | // encoder of the bitrate update. |
Tommi | fa3ce63 | 2021-06-03 12:06:02 +0200 | [diff] [blame] | 125 | void StopVideoSendStream() RTC_RUN_ON(rtp_transport_queue_); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 126 | |
| 127 | void ConfigureProtection(); |
| 128 | void ConfigureSsrcs(); |
| 129 | void SignalEncoderTimedOut(); |
| 130 | void SignalEncoderActive(); |
Sebastian Jansson | 464a557 | 2019-02-12 13:32:32 +0100 | [diff] [blame] | 131 | MediaStreamAllocationConfig GetAllocationConfig() const |
Tommi | fa3ce63 | 2021-06-03 12:06:02 +0200 | [diff] [blame] | 132 | RTC_RUN_ON(rtp_transport_queue_); |
Tommi | 1050fbc | 2021-06-03 17:58:28 +0200 | [diff] [blame] | 133 | |
| 134 | RTC_NO_UNIQUE_ADDRESS SequenceChecker thread_checker_; |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 135 | Clock* const clock_; |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 136 | const bool has_alr_probing_; |
Christoffer Rodbro | 196c5ba | 2018-11-27 11:56:25 +0100 | [diff] [blame] | 137 | const PacingConfig pacing_config_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 138 | |
| 139 | SendStatisticsProxy* const stats_proxy_; |
| 140 | const VideoSendStream::Config* const config_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 141 | |
Per Kjellander | 828ef91 | 2022-10-10 12:53:41 +0200 | [diff] [blame] | 142 | MaybeWorkerThread* const rtp_transport_queue_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 143 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 144 | RepeatingTaskHandle check_encoder_activity_task_ |
Tommi | fa3ce63 | 2021-06-03 12:06:02 +0200 | [diff] [blame] | 145 | RTC_GUARDED_BY(rtp_transport_queue_); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 146 | |
| 147 | std::atomic_bool activity_; |
Tommi | fa3ce63 | 2021-06-03 12:06:02 +0200 | [diff] [blame] | 148 | bool timed_out_ RTC_GUARDED_BY(rtp_transport_queue_); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 149 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 150 | RtpTransportControllerSendInterface* const transport_; |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 151 | BitrateAllocatorInterface* const bitrate_allocator_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 152 | |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 153 | bool disable_padding_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 154 | int max_padding_bitrate_; |
| 155 | int encoder_min_bitrate_bps_; |
| 156 | uint32_t encoder_max_bitrate_bps_; |
| 157 | uint32_t encoder_target_rate_bps_; |
| 158 | double encoder_bitrate_priority_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 159 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 160 | VideoStreamEncoderInterface* const video_stream_encoder_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 161 | |
| 162 | RtcpBandwidthObserver* const bandwidth_observer_; |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 163 | RtpVideoSenderInterface* const rtp_video_sender_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 164 | |
Tommi | 1050fbc | 2021-06-03 17:58:28 +0200 | [diff] [blame] | 165 | rtc::scoped_refptr<PendingTaskSafetyFlag> transport_queue_safety_ = |
| 166 | PendingTaskSafetyFlag::CreateDetached(); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 167 | |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 168 | // Context for the most recent and last sent video bitrate allocation. Used to |
| 169 | // throttle sending of similar bitrate allocations. |
| 170 | struct VbaSendContext { |
| 171 | VideoBitrateAllocation last_sent_allocation; |
| 172 | absl::optional<VideoBitrateAllocation> throttled_allocation; |
| 173 | int64_t last_send_time_ms; |
| 174 | }; |
| 175 | absl::optional<VbaSendContext> video_bitrate_allocation_context_ |
Tommi | fa3ce63 | 2021-06-03 12:06:02 +0200 | [diff] [blame] | 176 | RTC_GUARDED_BY(rtp_transport_queue_); |
Tommi | e902f28 | 2021-06-03 12:02:02 +0200 | [diff] [blame] | 177 | const absl::optional<float> configured_pacing_factor_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 178 | }; |
| 179 | } // namespace internal |
| 180 | } // namespace webrtc |
| 181 | #endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_ |