blob: d444eabc21e1734bea7a46c97867324f394f3d2e [file] [log] [blame]
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +02001/*
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 Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
14#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Sebastian Janssonecb68972019-01-18 10:30:54 +010016#include <atomic>
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020017#include <map>
18#include <memory>
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020019#include <vector>
20
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "absl/types/optional.h"
Jonas Orelande62c2f22022-03-29 11:04:48 +020022#include "api/field_trials_view.h"
Artem Titovc374d112022-06-16 21:27:45 +020023#include "api/task_queue/pending_task_safety_flag.h"
Danil Chapovalov03f8b8a2022-07-18 13:11:42 +020024#include "api/task_queue/task_queue_base.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "api/video/encoded_image.h"
26#include "api/video/video_bitrate_allocation.h"
Jiawei Ou4206a0a2018-07-20 15:49:43 -070027#include "api/video/video_bitrate_allocator.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "api/video_codecs/video_encoder.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020029#include "call/bitrate_allocator.h"
Yves Gerey3e707812018-11-28 16:47:49 +010030#include "call/rtp_config.h"
31#include "call/rtp_transport_controller_send_interface.h"
Stefan Holmer9416ef82018-07-19 10:34:38 +020032#include "call/rtp_video_sender_interface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010033#include "modules/include/module_common_types.h"
34#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Per Kjellander828ef912022-10-10 12:53:41 +020035#include "modules/utility/maybe_worker_thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010036#include "modules/video_coding/include/video_codec_interface.h"
Jonas Olsson0182a032019-07-09 12:31:20 +020037#include "rtc_base/experiments/field_trial_parser.h"
Tommi1050fbc2021-06-03 17:58:28 +020038#include "rtc_base/system/no_unique_address.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010039#include "rtc_base/task_utils/repeating_task.h"
Yves Gerey3e707812018-11-28 16:47:49 +010040#include "rtc_base/thread_annotations.h"
Jonas Oreland6c2dae22022-09-29 10:28:24 +020041#include "video/config/video_encoder_config.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020042#include "video/send_statistics_proxy.h"
Jonas Oreland6c2dae22022-09-29 10:28:24 +020043#include "video/video_stream_encoder_interface.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020044
45namespace webrtc {
46namespace internal {
47
Christoffer Rodbro196c5ba2018-11-27 11:56:25 +010048// Pacing buffer config; overridden by ALR config if provided.
49struct PacingConfig {
Jonas Orelande62c2f22022-03-29 11:04:48 +020050 explicit PacingConfig(const FieldTrialsView& field_trials);
Christoffer Rodbro196c5ba2018-11-27 11:56:25 +010051 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 Jansson8e0b15b2018-04-18 19:19:22 +020058// VideoSendStreamImpl implements internal::VideoSendStream.
Tommifa3ce632021-06-03 12:06:02 +020059// 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 Jansson8e0b15b2018-04-18 19:19:22 +020062// 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.
65class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver,
Per Kjellanderdcef6412020-10-07 15:09:05 +020066 public VideoStreamEncoderInterface::EncoderSink {
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020067 public:
Tommi1050fbc2021-06-03 17:58:28 +020068 VideoSendStreamImpl(Clock* clock,
69 SendStatisticsProxy* stats_proxy,
Tommi1050fbc2021-06-03 17:58:28 +020070 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 Oreland8ca06132022-03-14 12:52:48 +010077 RtpVideoSenderInterface* rtp_video_sender,
Jonas Orelande62c2f22022-03-29 11:04:48 +020078 const FieldTrialsView& field_trials);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020079 ~VideoSendStreamImpl() override;
80
Niels Möller8fb1a6a2019-03-05 14:29:42 +010081 void DeliverRtcp(const uint8_t* packet, size_t length);
Per Kjellander75170be2022-11-24 12:32:19 +000082 void UpdateActiveSimulcastLayers(std::vector<bool> active_layers);
83 void Start();
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020084 void Stop();
85
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020086 // TODO(holmer): Move these to RtpTransportControllerSend.
87 std::map<uint32_t, RtpState> GetRtpStates() const;
88
89 std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020090
Tommie902f282021-06-03 12:02:02 +020091 const absl::optional<float>& configured_pacing_factor() const {
92 return configured_pacing_factor_;
93 }
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020094
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020095 private:
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020096 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020097 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020098
Per Kjellanderdcef6412020-10-07 15:09:05 +020099 // Implements VideoStreamEncoderInterface::EncoderSink
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100100 void OnEncoderConfigurationChanged(
101 std::vector<VideoStream> streams,
Ilya Nikolaevskiy93be66c2020-04-02 14:10:27 +0200102 bool is_svc,
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100103 VideoEncoderConfig::ContentType content_type,
104 int min_transmit_bitrate_bps) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200105
Per Kjellanderdcef6412020-10-07 15:09:05 +0200106 void OnBitrateAllocationUpdated(
107 const VideoBitrateAllocation& allocation) override;
Per Kjellandera9434842020-10-15 17:53:22 +0200108 void OnVideoLayersAllocationUpdated(
109 VideoLayersAllocation allocation) override;
Per Kjellanderdcef6412020-10-07 15:09:05 +0200110
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200111 // Implements EncodedImageCallback. The implementation routes encoded frames
Artem Titovcfea2182021-08-10 01:22:31 +0200112 // to the `payload_router_` and `config.pre_encode_callback` if set.
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200113 // Called on an arbitrary encoder callback thread.
114 EncodedImageCallback::Result OnEncodedImage(
115 const EncodedImage& encoded_image,
Danil Chapovalov2549f172020-08-12 17:30:36 +0200116 const CodecSpecificInfo* codec_specific_info) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200117
Jakob Ivarsson159b4172019-10-30 14:02:14 +0100118 // Implements EncodedImageCallback.
119 void OnDroppedFrame(EncodedImageCallback::DropReason reason) override;
120
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200121 // 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.
Tommifa3ce632021-06-03 12:06:02 +0200125 void StopVideoSendStream() RTC_RUN_ON(rtp_transport_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200126
127 void ConfigureProtection();
128 void ConfigureSsrcs();
129 void SignalEncoderTimedOut();
130 void SignalEncoderActive();
Sebastian Jansson464a5572019-02-12 13:32:32 +0100131 MediaStreamAllocationConfig GetAllocationConfig() const
Tommifa3ce632021-06-03 12:06:02 +0200132 RTC_RUN_ON(rtp_transport_queue_);
Tommi1050fbc2021-06-03 17:58:28 +0200133
134 RTC_NO_UNIQUE_ADDRESS SequenceChecker thread_checker_;
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100135 Clock* const clock_;
Erik Språngb57ab382018-09-13 10:52:38 +0200136 const bool has_alr_probing_;
Christoffer Rodbro196c5ba2018-11-27 11:56:25 +0100137 const PacingConfig pacing_config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200138
139 SendStatisticsProxy* const stats_proxy_;
140 const VideoSendStream::Config* const config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200141
Per Kjellander828ef912022-10-10 12:53:41 +0200142 MaybeWorkerThread* const rtp_transport_queue_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200143
Sebastian Janssonecb68972019-01-18 10:30:54 +0100144 RepeatingTaskHandle check_encoder_activity_task_
Tommifa3ce632021-06-03 12:06:02 +0200145 RTC_GUARDED_BY(rtp_transport_queue_);
Sebastian Janssonecb68972019-01-18 10:30:54 +0100146
147 std::atomic_bool activity_;
Tommifa3ce632021-06-03 12:06:02 +0200148 bool timed_out_ RTC_GUARDED_BY(rtp_transport_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200149
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200150 RtpTransportControllerSendInterface* const transport_;
Sebastian Jansson652dc912018-04-19 17:09:15 +0200151 BitrateAllocatorInterface* const bitrate_allocator_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200152
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200153 bool disable_padding_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200154 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 Jansson8e0b15b2018-04-18 19:19:22 +0200159
Sebastian Jansson652dc912018-04-19 17:09:15 +0200160 VideoStreamEncoderInterface* const video_stream_encoder_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200161
162 RtcpBandwidthObserver* const bandwidth_observer_;
Stefan Holmer9416ef82018-07-19 10:34:38 +0200163 RtpVideoSenderInterface* const rtp_video_sender_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200164
Tommi1050fbc2021-06-03 17:58:28 +0200165 rtc::scoped_refptr<PendingTaskSafetyFlag> transport_queue_safety_ =
166 PendingTaskSafetyFlag::CreateDetached();
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200167
Erik Språng4e193e42018-09-14 19:01:58 +0200168 // 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_
Tommifa3ce632021-06-03 12:06:02 +0200176 RTC_GUARDED_BY(rtp_transport_queue_);
Tommie902f282021-06-03 12:02:02 +0200177 const absl::optional<float> configured_pacing_factor_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200178};
179} // namespace internal
180} // namespace webrtc
181#endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_