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> |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 15 | #include <atomic> |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 16 | #include <map> |
| 17 | #include <memory> |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 20 | #include "absl/types/optional.h" |
| 21 | #include "api/fec_controller.h" |
| 22 | #include "api/video/encoded_image.h" |
| 23 | #include "api/video/video_bitrate_allocation.h" |
Jiawei Ou | 4206a0a | 2018-07-20 15:49:43 -0700 | [diff] [blame] | 24 | #include "api/video/video_bitrate_allocator.h" |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 25 | #include "api/video/video_stream_encoder_interface.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 26 | #include "api/video_codecs/video_encoder.h" |
| 27 | #include "api/video_codecs/video_encoder_config.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 28 | #include "call/bitrate_allocator.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 29 | #include "call/rtp_config.h" |
| 30 | #include "call/rtp_transport_controller_send_interface.h" |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 31 | #include "call/rtp_video_sender_interface.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 32 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 33 | #include "modules/include/module_common_types.h" |
| 34 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 35 | #include "modules/utility/include/process_thread.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 36 | #include "modules/video_coding/include/video_codec_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 37 | #include "rtc_base/critical_section.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 38 | #include "rtc_base/task_queue.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" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 41 | #include "rtc_base/weak_ptr.h" |
| 42 | #include "video/call_stats.h" |
Niels Möller | fa89d84 | 2019-01-30 16:33:45 +0100 | [diff] [blame] | 43 | #include "video/encoder_key_frame_callback.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 44 | #include "video/send_delay_stats.h" |
| 45 | #include "video/send_statistics_proxy.h" |
| 46 | #include "video/video_send_stream.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 47 | |
| 48 | namespace webrtc { |
| 49 | namespace internal { |
| 50 | |
Christoffer Rodbro | 196c5ba | 2018-11-27 11:56:25 +0100 | [diff] [blame] | 51 | // Pacing buffer config; overridden by ALR config if provided. |
| 52 | struct PacingConfig { |
| 53 | PacingConfig(); |
| 54 | PacingConfig(const PacingConfig&); |
| 55 | PacingConfig& operator=(const PacingConfig&) = default; |
| 56 | ~PacingConfig(); |
| 57 | FieldTrialParameter<double> pacing_factor; |
| 58 | FieldTrialParameter<TimeDelta> max_pacing_delay; |
| 59 | }; |
| 60 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 61 | // VideoSendStreamImpl implements internal::VideoSendStream. |
| 62 | // It is created and destroyed on |worker_queue|. The intent is to decrease the |
| 63 | // need for locking and to ensure methods are called in sequence. |
| 64 | // Public methods except |DeliverRtcp| must be called on |worker_queue|. |
| 65 | // DeliverRtcp is called on the libjingle worker thread or a network thread. |
| 66 | // An encoder may deliver frames through the EncodedImageCallback on an |
| 67 | // arbitrary thread. |
| 68 | class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver, |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 69 | public VideoStreamEncoderInterface::EncoderSink, |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 70 | public VideoBitrateAllocationObserver { |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 71 | public: |
| 72 | VideoSendStreamImpl( |
| 73 | SendStatisticsProxy* stats_proxy, |
| 74 | rtc::TaskQueue* worker_queue, |
| 75 | CallStats* call_stats, |
| 76 | RtpTransportControllerSendInterface* transport, |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 77 | BitrateAllocatorInterface* bitrate_allocator, |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 78 | SendDelayStats* send_delay_stats, |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 79 | VideoStreamEncoderInterface* video_stream_encoder, |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 80 | RtcEventLog* event_log, |
| 81 | const VideoSendStream::Config* config, |
| 82 | int initial_encoder_max_bitrate, |
| 83 | double initial_encoder_bitrate_priority, |
| 84 | std::map<uint32_t, RtpState> suspended_ssrcs, |
| 85 | std::map<uint32_t, RtpPayloadState> suspended_payload_states, |
| 86 | VideoEncoderConfig::ContentType content_type, |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 87 | std::unique_ptr<FecController> fec_controller, |
| 88 | MediaTransportInterface* media_transport); |
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 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 99 | bool DeliverRtcp(const uint8_t* packet, size_t length); |
| 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 | |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame^] | 115 | void OnEncoderConfigurationChanged( |
| 116 | std::vector<VideoStream> streams, |
| 117 | VideoEncoderConfig::ContentType content_type, |
| 118 | int min_transmit_bitrate_bps) override; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 119 | |
| 120 | // Implements EncodedImageCallback. The implementation routes encoded frames |
| 121 | // to the |payload_router_| and |config.pre_encode_callback| if set. |
| 122 | // Called on an arbitrary encoder callback thread. |
| 123 | EncodedImageCallback::Result OnEncodedImage( |
| 124 | const EncodedImage& encoded_image, |
| 125 | const CodecSpecificInfo* codec_specific_info, |
| 126 | const RTPFragmentationHeader* fragmentation) override; |
| 127 | |
| 128 | // Implements VideoBitrateAllocationObserver. |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 129 | void OnBitrateAllocationUpdated( |
| 130 | const VideoBitrateAllocation& allocation) override; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 131 | |
| 132 | // Starts monitoring and sends a keyframe. |
| 133 | void StartupVideoSendStream(); |
| 134 | // Removes the bitrate observer, stops monitoring and notifies the video |
| 135 | // encoder of the bitrate update. |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 136 | void StopVideoSendStream() RTC_RUN_ON(worker_queue_); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 137 | |
| 138 | void ConfigureProtection(); |
| 139 | void ConfigureSsrcs(); |
| 140 | void SignalEncoderTimedOut(); |
| 141 | void SignalEncoderActive(); |
| 142 | |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 143 | const bool has_alr_probing_; |
Christoffer Rodbro | 196c5ba | 2018-11-27 11:56:25 +0100 | [diff] [blame] | 144 | const PacingConfig pacing_config_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 145 | |
| 146 | SendStatisticsProxy* const stats_proxy_; |
| 147 | const VideoSendStream::Config* const config_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 148 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 149 | rtc::TaskQueue* const worker_queue_; |
| 150 | |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 151 | RepeatingTaskHandle check_encoder_activity_task_ |
| 152 | RTC_GUARDED_BY(worker_queue_); |
| 153 | |
| 154 | std::atomic_bool activity_; |
| 155 | bool timed_out_ RTC_GUARDED_BY(worker_queue_); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 156 | |
| 157 | CallStats* const call_stats_; |
| 158 | RtpTransportControllerSendInterface* const transport_; |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 159 | BitrateAllocatorInterface* const bitrate_allocator_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 160 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 161 | rtc::CriticalSection ivf_writers_crit_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 162 | |
| 163 | int max_padding_bitrate_; |
| 164 | int encoder_min_bitrate_bps_; |
| 165 | uint32_t encoder_max_bitrate_bps_; |
| 166 | uint32_t encoder_target_rate_bps_; |
| 167 | double encoder_bitrate_priority_; |
| 168 | bool has_packet_feedback_; |
| 169 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 170 | VideoStreamEncoderInterface* const video_stream_encoder_; |
Niels Möller | fa89d84 | 2019-01-30 16:33:45 +0100 | [diff] [blame] | 171 | EncoderKeyFrameCallback encoder_feedback_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 172 | |
| 173 | RtcpBandwidthObserver* const bandwidth_observer_; |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 174 | RtpVideoSenderInterface* const rtp_video_sender_; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 175 | |
| 176 | // |weak_ptr_| to our self. This is used since we can not call |
| 177 | // |weak_ptr_factory_.GetWeakPtr| from multiple sequences but it is ok to copy |
| 178 | // an existing WeakPtr. |
| 179 | rtc::WeakPtr<VideoSendStreamImpl> weak_ptr_; |
| 180 | // |weak_ptr_factory_| must be declared last to make sure all WeakPtr's are |
| 181 | // invalidated before any other members are destroyed. |
| 182 | rtc::WeakPtrFactory<VideoSendStreamImpl> weak_ptr_factory_; |
| 183 | |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 184 | // Context for the most recent and last sent video bitrate allocation. Used to |
| 185 | // throttle sending of similar bitrate allocations. |
| 186 | struct VbaSendContext { |
| 187 | VideoBitrateAllocation last_sent_allocation; |
| 188 | absl::optional<VideoBitrateAllocation> throttled_allocation; |
| 189 | int64_t last_send_time_ms; |
| 190 | }; |
| 191 | absl::optional<VbaSendContext> video_bitrate_allocation_context_ |
| 192 | RTC_GUARDED_BY(worker_queue_); |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 193 | MediaTransportInterface* const media_transport_; |
| 194 | rtc::CriticalSection media_transport_id_lock_; |
| 195 | int64_t media_transport_frame_id_ RTC_GUARDED_BY(media_transport_id_lock_) = |
| 196 | 0; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 197 | }; |
| 198 | } // namespace internal |
| 199 | } // namespace webrtc |
| 200 | #endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_ |