blob: babf1dcfe55bd25eb6c7cac309a098dd2d57882b [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"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "api/video/encoded_image.h"
23#include "api/video/video_bitrate_allocation.h"
Jiawei Ou4206a0a2018-07-20 15:49:43 -070024#include "api/video/video_bitrate_allocator.h"
Niels Möller213618e2018-07-24 09:29:58 +020025#include "api/video/video_stream_encoder_interface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010026#include "api/video_codecs/video_encoder.h"
27#include "api/video_codecs/video_encoder_config.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020028#include "call/bitrate_allocator.h"
Yves Gerey3e707812018-11-28 16:47:49 +010029#include "call/rtp_config.h"
30#include "call/rtp_transport_controller_send_interface.h"
Stefan Holmer9416ef82018-07-19 10:34:38 +020031#include "call/rtp_video_sender_interface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010032#include "modules/include/module_common_types.h"
33#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Yves Gerey3e707812018-11-28 16:47:49 +010034#include "modules/video_coding/include/video_codec_interface.h"
Jonas Olsson0182a032019-07-09 12:31:20 +020035#include "rtc_base/experiments/field_trial_parser.h"
Tommi1050fbc2021-06-03 17:58:28 +020036#include "rtc_base/system/no_unique_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010037#include "rtc_base/task_queue.h"
Tommi1050fbc2021-06-03 17:58:28 +020038#include "rtc_base/task_utils/pending_task_safety_flag.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"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020041#include "video/send_statistics_proxy.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020042
43namespace webrtc {
44namespace internal {
45
Christoffer Rodbro196c5ba2018-11-27 11:56:25 +010046// Pacing buffer config; overridden by ALR config if provided.
47struct PacingConfig {
48 PacingConfig();
49 PacingConfig(const PacingConfig&);
50 PacingConfig& operator=(const PacingConfig&) = default;
51 ~PacingConfig();
52 FieldTrialParameter<double> pacing_factor;
53 FieldTrialParameter<TimeDelta> max_pacing_delay;
54};
55
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020056// VideoSendStreamImpl implements internal::VideoSendStream.
Tommifa3ce632021-06-03 12:06:02 +020057// It is created and destroyed on `rtp_transport_queue`. The intent is to
58// decrease the need for locking and to ensure methods are called in sequence.
59// Public methods except `DeliverRtcp` must be called on `rtp_transport_queue`.
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020060// DeliverRtcp is called on the libjingle worker thread or a network thread.
61// An encoder may deliver frames through the EncodedImageCallback on an
62// arbitrary thread.
63class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver,
Per Kjellanderdcef6412020-10-07 15:09:05 +020064 public VideoStreamEncoderInterface::EncoderSink {
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020065 public:
Tommi1050fbc2021-06-03 17:58:28 +020066 VideoSendStreamImpl(Clock* clock,
67 SendStatisticsProxy* stats_proxy,
68 rtc::TaskQueue* rtp_transport_queue,
69 RtpTransportControllerSendInterface* transport,
70 BitrateAllocatorInterface* bitrate_allocator,
71 VideoStreamEncoderInterface* video_stream_encoder,
72 const VideoSendStream::Config* config,
73 int initial_encoder_max_bitrate,
74 double initial_encoder_bitrate_priority,
75 VideoEncoderConfig::ContentType content_type,
76 RtpVideoSenderInterface* rtp_video_sender);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020077 ~VideoSendStreamImpl() override;
78
Niels Möller8fb1a6a2019-03-05 14:29:42 +010079 void DeliverRtcp(const uint8_t* packet, size_t length);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020080 void UpdateActiveSimulcastLayers(const std::vector<bool> active_layers);
81 void Start();
82 void Stop();
83
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020084 // TODO(holmer): Move these to RtpTransportControllerSend.
85 std::map<uint32_t, RtpState> GetRtpStates() const;
86
87 std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020088
Tommie902f282021-06-03 12:02:02 +020089 const absl::optional<float>& configured_pacing_factor() const {
90 return configured_pacing_factor_;
91 }
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020092
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020093 private:
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020094 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020095 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020096
Per Kjellanderdcef6412020-10-07 15:09:05 +020097 // Implements VideoStreamEncoderInterface::EncoderSink
Rasmus Brandtc402dbe2019-02-04 11:09:46 +010098 void OnEncoderConfigurationChanged(
99 std::vector<VideoStream> streams,
Ilya Nikolaevskiy93be66c2020-04-02 14:10:27 +0200100 bool is_svc,
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100101 VideoEncoderConfig::ContentType content_type,
102 int min_transmit_bitrate_bps) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200103
Per Kjellanderdcef6412020-10-07 15:09:05 +0200104 void OnBitrateAllocationUpdated(
105 const VideoBitrateAllocation& allocation) override;
Per Kjellandera9434842020-10-15 17:53:22 +0200106 void OnVideoLayersAllocationUpdated(
107 VideoLayersAllocation allocation) override;
Per Kjellanderdcef6412020-10-07 15:09:05 +0200108
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200109 // Implements EncodedImageCallback. The implementation routes encoded frames
110 // to the |payload_router_| and |config.pre_encode_callback| if set.
111 // Called on an arbitrary encoder callback thread.
112 EncodedImageCallback::Result OnEncodedImage(
113 const EncodedImage& encoded_image,
Danil Chapovalov2549f172020-08-12 17:30:36 +0200114 const CodecSpecificInfo* codec_specific_info) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200115
Jakob Ivarsson159b4172019-10-30 14:02:14 +0100116 // Implements EncodedImageCallback.
117 void OnDroppedFrame(EncodedImageCallback::DropReason reason) override;
118
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200119 // Starts monitoring and sends a keyframe.
120 void StartupVideoSendStream();
121 // Removes the bitrate observer, stops monitoring and notifies the video
122 // encoder of the bitrate update.
Tommifa3ce632021-06-03 12:06:02 +0200123 void StopVideoSendStream() RTC_RUN_ON(rtp_transport_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200124
125 void ConfigureProtection();
126 void ConfigureSsrcs();
127 void SignalEncoderTimedOut();
128 void SignalEncoderActive();
Sebastian Jansson464a5572019-02-12 13:32:32 +0100129 MediaStreamAllocationConfig GetAllocationConfig() const
Tommifa3ce632021-06-03 12:06:02 +0200130 RTC_RUN_ON(rtp_transport_queue_);
Tommi1050fbc2021-06-03 17:58:28 +0200131
132 RTC_NO_UNIQUE_ADDRESS SequenceChecker thread_checker_;
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100133 Clock* const clock_;
Erik Språngb57ab382018-09-13 10:52:38 +0200134 const bool has_alr_probing_;
Christoffer Rodbro196c5ba2018-11-27 11:56:25 +0100135 const PacingConfig pacing_config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200136
137 SendStatisticsProxy* const stats_proxy_;
138 const VideoSendStream::Config* const config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200139
Tommifa3ce632021-06-03 12:06:02 +0200140 rtc::TaskQueue* const rtp_transport_queue_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200141
Sebastian Janssonecb68972019-01-18 10:30:54 +0100142 RepeatingTaskHandle check_encoder_activity_task_
Tommifa3ce632021-06-03 12:06:02 +0200143 RTC_GUARDED_BY(rtp_transport_queue_);
Sebastian Janssonecb68972019-01-18 10:30:54 +0100144
145 std::atomic_bool activity_;
Tommifa3ce632021-06-03 12:06:02 +0200146 bool timed_out_ RTC_GUARDED_BY(rtp_transport_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200147
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200148 RtpTransportControllerSendInterface* const transport_;
Sebastian Jansson652dc912018-04-19 17:09:15 +0200149 BitrateAllocatorInterface* const bitrate_allocator_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200150
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200151 bool disable_padding_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200152 int max_padding_bitrate_;
153 int encoder_min_bitrate_bps_;
154 uint32_t encoder_max_bitrate_bps_;
155 uint32_t encoder_target_rate_bps_;
156 double encoder_bitrate_priority_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200157
Sebastian Jansson652dc912018-04-19 17:09:15 +0200158 VideoStreamEncoderInterface* const video_stream_encoder_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200159
160 RtcpBandwidthObserver* const bandwidth_observer_;
Stefan Holmer9416ef82018-07-19 10:34:38 +0200161 RtpVideoSenderInterface* const rtp_video_sender_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200162
Tommi1050fbc2021-06-03 17:58:28 +0200163 rtc::scoped_refptr<PendingTaskSafetyFlag> transport_queue_safety_ =
164 PendingTaskSafetyFlag::CreateDetached();
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200165
Erik Språng4e193e42018-09-14 19:01:58 +0200166 // Context for the most recent and last sent video bitrate allocation. Used to
167 // throttle sending of similar bitrate allocations.
168 struct VbaSendContext {
169 VideoBitrateAllocation last_sent_allocation;
170 absl::optional<VideoBitrateAllocation> throttled_allocation;
171 int64_t last_send_time_ms;
172 };
173 absl::optional<VbaSendContext> video_bitrate_allocation_context_
Tommifa3ce632021-06-03 12:06:02 +0200174 RTC_GUARDED_BY(rtp_transport_queue_);
Tommie902f282021-06-03 12:02:02 +0200175 const absl::optional<float> configured_pacing_factor_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200176};
177} // namespace internal
178} // namespace webrtc
179#endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_