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