blob: f1454506555a44fdfcb293aad1fcc7cf4e3e829e [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 Kjellander59ade012022-12-02 08:09:37 +000082 void StartPerRtpStream(std::vector<bool> active_layers);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020083 void Stop();
84
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020085 // TODO(holmer): Move these to RtpTransportControllerSend.
86 std::map<uint32_t, RtpState> GetRtpStates() const;
87
88 std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020089
Tommie902f282021-06-03 12:02:02 +020090 const absl::optional<float>& configured_pacing_factor() const {
91 return configured_pacing_factor_;
92 }
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020093
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020094 private:
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020095 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020096 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020097
Per Kjellanderdcef6412020-10-07 15:09:05 +020098 // Implements VideoStreamEncoderInterface::EncoderSink
Rasmus Brandtc402dbe2019-02-04 11:09:46 +010099 void OnEncoderConfigurationChanged(
100 std::vector<VideoStream> streams,
Ilya Nikolaevskiy93be66c2020-04-02 14:10:27 +0200101 bool is_svc,
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100102 VideoEncoderConfig::ContentType content_type,
103 int min_transmit_bitrate_bps) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200104
Per Kjellanderdcef6412020-10-07 15:09:05 +0200105 void OnBitrateAllocationUpdated(
106 const VideoBitrateAllocation& allocation) override;
Per Kjellandera9434842020-10-15 17:53:22 +0200107 void OnVideoLayersAllocationUpdated(
108 VideoLayersAllocation allocation) override;
Per Kjellanderdcef6412020-10-07 15:09:05 +0200109
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200110 // Implements EncodedImageCallback. The implementation routes encoded frames
Artem Titovcfea2182021-08-10 01:22:31 +0200111 // to the `payload_router_` and `config.pre_encode_callback` if set.
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200112 // Called on an arbitrary encoder callback thread.
113 EncodedImageCallback::Result OnEncodedImage(
114 const EncodedImage& encoded_image,
Danil Chapovalov2549f172020-08-12 17:30:36 +0200115 const CodecSpecificInfo* codec_specific_info) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200116
Jakob Ivarsson159b4172019-10-30 14:02:14 +0100117 // Implements EncodedImageCallback.
118 void OnDroppedFrame(EncodedImageCallback::DropReason reason) override;
119
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200120 // Starts monitoring and sends a keyframe.
121 void StartupVideoSendStream();
122 // Removes the bitrate observer, stops monitoring and notifies the video
123 // encoder of the bitrate update.
Tommifa3ce632021-06-03 12:06:02 +0200124 void StopVideoSendStream() RTC_RUN_ON(rtp_transport_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200125
126 void ConfigureProtection();
127 void ConfigureSsrcs();
128 void SignalEncoderTimedOut();
129 void SignalEncoderActive();
Sebastian Jansson464a5572019-02-12 13:32:32 +0100130 MediaStreamAllocationConfig GetAllocationConfig() const
Tommifa3ce632021-06-03 12:06:02 +0200131 RTC_RUN_ON(rtp_transport_queue_);
Tommi1050fbc2021-06-03 17:58:28 +0200132
133 RTC_NO_UNIQUE_ADDRESS SequenceChecker thread_checker_;
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100134 Clock* const clock_;
Erik Språngb57ab382018-09-13 10:52:38 +0200135 const bool has_alr_probing_;
Christoffer Rodbro196c5ba2018-11-27 11:56:25 +0100136 const PacingConfig pacing_config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200137
138 SendStatisticsProxy* const stats_proxy_;
139 const VideoSendStream::Config* const config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200140
Per Kjellander828ef912022-10-10 12:53:41 +0200141 MaybeWorkerThread* const rtp_transport_queue_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200142
Sebastian Janssonecb68972019-01-18 10:30:54 +0100143 RepeatingTaskHandle check_encoder_activity_task_
Tommifa3ce632021-06-03 12:06:02 +0200144 RTC_GUARDED_BY(rtp_transport_queue_);
Sebastian Janssonecb68972019-01-18 10:30:54 +0100145
146 std::atomic_bool activity_;
Tommifa3ce632021-06-03 12:06:02 +0200147 bool timed_out_ RTC_GUARDED_BY(rtp_transport_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200148
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200149 RtpTransportControllerSendInterface* const transport_;
Sebastian Jansson652dc912018-04-19 17:09:15 +0200150 BitrateAllocatorInterface* const bitrate_allocator_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200151
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200152 bool disable_padding_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200153 int max_padding_bitrate_;
154 int encoder_min_bitrate_bps_;
155 uint32_t encoder_max_bitrate_bps_;
156 uint32_t encoder_target_rate_bps_;
157 double encoder_bitrate_priority_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200158
Sebastian Jansson652dc912018-04-19 17:09:15 +0200159 VideoStreamEncoderInterface* const video_stream_encoder_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200160
161 RtcpBandwidthObserver* const bandwidth_observer_;
Stefan Holmer9416ef82018-07-19 10:34:38 +0200162 RtpVideoSenderInterface* const rtp_video_sender_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200163
Tommi1050fbc2021-06-03 17:58:28 +0200164 rtc::scoped_refptr<PendingTaskSafetyFlag> transport_queue_safety_ =
165 PendingTaskSafetyFlag::CreateDetached();
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200166
Erik Språng4e193e42018-09-14 19:01:58 +0200167 // Context for the most recent and last sent video bitrate allocation. Used to
168 // throttle sending of similar bitrate allocations.
169 struct VbaSendContext {
170 VideoBitrateAllocation last_sent_allocation;
171 absl::optional<VideoBitrateAllocation> throttled_allocation;
172 int64_t last_send_time_ms;
173 };
174 absl::optional<VbaSendContext> video_bitrate_allocation_context_
Tommifa3ce632021-06-03 12:06:02 +0200175 RTC_GUARDED_BY(rtp_transport_queue_);
Tommie902f282021-06-03 12:02:02 +0200176 const absl::optional<float> configured_pacing_factor_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200177};
178} // namespace internal
179} // namespace webrtc
180#endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_