blob: 7c963e482cab0cabf5bac39cbc3a3e8f2e67f7fd [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"
22#include "api/fec_controller.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020023#include "api/rtc_event_log/rtc_event_log.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "api/video/encoded_image.h"
25#include "api/video/video_bitrate_allocation.h"
Jiawei Ou4206a0a2018-07-20 15:49:43 -070026#include "api/video/video_bitrate_allocator.h"
Niels Möller213618e2018-07-24 09:29:58 +020027#include "api/video/video_stream_encoder_interface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "api/video_codecs/video_encoder.h"
29#include "api/video_codecs/video_encoder_config.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020030#include "call/bitrate_allocator.h"
Yves Gerey3e707812018-11-28 16:47:49 +010031#include "call/rtp_config.h"
32#include "call/rtp_transport_controller_send_interface.h"
Stefan Holmer9416ef82018-07-19 10:34:38 +020033#include "call/rtp_video_sender_interface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010034#include "modules/include/module_common_types.h"
35#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020036#include "modules/utility/include/process_thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010037#include "modules/video_coding/include/video_codec_interface.h"
Jonas Olsson0182a032019-07-09 12:31:20 +020038#include "rtc_base/experiments/field_trial_parser.h"
Markus Handella3765182020-07-08 13:13:32 +020039#include "rtc_base/synchronization/mutex.h"
Yves Gerey3e707812018-11-28 16:47:49 +010040#include "rtc_base/task_queue.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010041#include "rtc_base/task_utils/repeating_task.h"
Yves Gerey3e707812018-11-28 16:47:49 +010042#include "rtc_base/thread_annotations.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020043#include "rtc_base/weak_ptr.h"
Elad Alon14d1c9d2019-04-08 14:16:17 +020044#include "video/encoder_rtcp_feedback.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020045#include "video/send_delay_stats.h"
46#include "video/send_statistics_proxy.h"
47#include "video/video_send_stream.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020048
49namespace webrtc {
50namespace internal {
51
Christoffer Rodbro196c5ba2018-11-27 11:56:25 +010052// Pacing buffer config; overridden by ALR config if provided.
53struct PacingConfig {
54 PacingConfig();
55 PacingConfig(const PacingConfig&);
56 PacingConfig& operator=(const PacingConfig&) = default;
57 ~PacingConfig();
58 FieldTrialParameter<double> pacing_factor;
59 FieldTrialParameter<TimeDelta> max_pacing_delay;
60};
61
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020062// VideoSendStreamImpl implements internal::VideoSendStream.
Tommifa3ce632021-06-03 12:06:02 +020063// It is created and destroyed on `rtp_transport_queue`. The intent is to
64// decrease the need for locking and to ensure methods are called in sequence.
65// Public methods except `DeliverRtcp` must be called on `rtp_transport_queue`.
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020066// DeliverRtcp is called on the libjingle worker thread or a network thread.
67// An encoder may deliver frames through the EncodedImageCallback on an
68// arbitrary thread.
69class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver,
Per Kjellanderdcef6412020-10-07 15:09:05 +020070 public VideoStreamEncoderInterface::EncoderSink {
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020071 public:
72 VideoSendStreamImpl(
Sebastian Jansson572c60f2019-03-04 18:30:41 +010073 Clock* clock,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020074 SendStatisticsProxy* stats_proxy,
Tommifa3ce632021-06-03 12:06:02 +020075 rtc::TaskQueue* rtp_transport_queue,
Tommi8ae18ad2020-05-03 22:45:02 +020076 RtcpRttStats* call_stats,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020077 RtpTransportControllerSendInterface* transport,
Sebastian Jansson652dc912018-04-19 17:09:15 +020078 BitrateAllocatorInterface* bitrate_allocator,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020079 SendDelayStats* send_delay_stats,
Sebastian Jansson652dc912018-04-19 17:09:15 +020080 VideoStreamEncoderInterface* video_stream_encoder,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020081 RtcEventLog* event_log,
82 const VideoSendStream::Config* config,
83 int initial_encoder_max_bitrate,
84 double initial_encoder_bitrate_priority,
85 std::map<uint32_t, RtpState> suspended_ssrcs,
86 std::map<uint32_t, RtpPayloadState> suspended_payload_states,
87 VideoEncoderConfig::ContentType content_type,
Bjorn A Mellem7a9a0922019-11-26 09:19:40 -080088 std::unique_ptr<FecController> fec_controller);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020089 ~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,
Tommifa3ce632021-06-03 12:06:02 +020095 // maybe |rtp_transport_queue|.
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020096 void RegisterProcessThread(ProcessThread* module_process_thread);
97 void DeRegisterProcessThread();
98
Niels Möller8fb1a6a2019-03-05 14:29:42 +010099 void DeliverRtcp(const uint8_t* packet, size_t length);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200100 void UpdateActiveSimulcastLayers(const std::vector<bool> active_layers);
101 void Start();
102 void Stop();
103
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200104 // TODO(holmer): Move these to RtpTransportControllerSend.
105 std::map<uint32_t, RtpState> GetRtpStates() const;
106
107 std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200108
Tommie902f282021-06-03 12:02:02 +0200109 const absl::optional<float>& configured_pacing_factor() const {
110 return configured_pacing_factor_;
111 }
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200112
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200113 private:
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200114 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200115 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200116
Per Kjellanderdcef6412020-10-07 15:09:05 +0200117 // Implements VideoStreamEncoderInterface::EncoderSink
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100118 void OnEncoderConfigurationChanged(
119 std::vector<VideoStream> streams,
Ilya Nikolaevskiy93be66c2020-04-02 14:10:27 +0200120 bool is_svc,
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100121 VideoEncoderConfig::ContentType content_type,
122 int min_transmit_bitrate_bps) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200123
Per Kjellanderdcef6412020-10-07 15:09:05 +0200124 void OnBitrateAllocationUpdated(
125 const VideoBitrateAllocation& allocation) override;
Per Kjellandera9434842020-10-15 17:53:22 +0200126 void OnVideoLayersAllocationUpdated(
127 VideoLayersAllocation allocation) override;
Per Kjellanderdcef6412020-10-07 15:09:05 +0200128
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200129 // Implements EncodedImageCallback. The implementation routes encoded frames
130 // to the |payload_router_| and |config.pre_encode_callback| if set.
131 // Called on an arbitrary encoder callback thread.
132 EncodedImageCallback::Result OnEncodedImage(
133 const EncodedImage& encoded_image,
Danil Chapovalov2549f172020-08-12 17:30:36 +0200134 const CodecSpecificInfo* codec_specific_info) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200135
Jakob Ivarsson159b4172019-10-30 14:02:14 +0100136 // Implements EncodedImageCallback.
137 void OnDroppedFrame(EncodedImageCallback::DropReason reason) override;
138
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200139 // Starts monitoring and sends a keyframe.
140 void StartupVideoSendStream();
141 // Removes the bitrate observer, stops monitoring and notifies the video
142 // encoder of the bitrate update.
Tommifa3ce632021-06-03 12:06:02 +0200143 void StopVideoSendStream() RTC_RUN_ON(rtp_transport_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200144
145 void ConfigureProtection();
146 void ConfigureSsrcs();
147 void SignalEncoderTimedOut();
148 void SignalEncoderActive();
Sebastian Jansson464a5572019-02-12 13:32:32 +0100149 MediaStreamAllocationConfig GetAllocationConfig() const
Tommifa3ce632021-06-03 12:06:02 +0200150 RTC_RUN_ON(rtp_transport_queue_);
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100151 Clock* const clock_;
Erik Språngb57ab382018-09-13 10:52:38 +0200152 const bool has_alr_probing_;
Christoffer Rodbro196c5ba2018-11-27 11:56:25 +0100153 const PacingConfig pacing_config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200154
155 SendStatisticsProxy* const stats_proxy_;
156 const VideoSendStream::Config* const config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200157
Tommifa3ce632021-06-03 12:06:02 +0200158 rtc::TaskQueue* const rtp_transport_queue_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200159
Sebastian Janssonecb68972019-01-18 10:30:54 +0100160 RepeatingTaskHandle check_encoder_activity_task_
Tommifa3ce632021-06-03 12:06:02 +0200161 RTC_GUARDED_BY(rtp_transport_queue_);
Sebastian Janssonecb68972019-01-18 10:30:54 +0100162
163 std::atomic_bool activity_;
Tommifa3ce632021-06-03 12:06:02 +0200164 bool timed_out_ RTC_GUARDED_BY(rtp_transport_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200165
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200166 RtpTransportControllerSendInterface* const transport_;
Sebastian Jansson652dc912018-04-19 17:09:15 +0200167 BitrateAllocatorInterface* const bitrate_allocator_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200168
Markus Handella3765182020-07-08 13:13:32 +0200169 Mutex ivf_writers_mutex_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200170
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200171 bool disable_padding_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200172 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_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200177
Sebastian Jansson652dc912018-04-19 17:09:15 +0200178 VideoStreamEncoderInterface* const video_stream_encoder_;
Elad Alon14d1c9d2019-04-08 14:16:17 +0200179 EncoderRtcpFeedback encoder_feedback_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200180
181 RtcpBandwidthObserver* const bandwidth_observer_;
Stefan Holmer9416ef82018-07-19 10:34:38 +0200182 RtpVideoSenderInterface* const rtp_video_sender_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200183
184 // |weak_ptr_| to our self. This is used since we can not call
185 // |weak_ptr_factory_.GetWeakPtr| from multiple sequences but it is ok to copy
186 // an existing WeakPtr.
187 rtc::WeakPtr<VideoSendStreamImpl> weak_ptr_;
188 // |weak_ptr_factory_| must be declared last to make sure all WeakPtr's are
189 // invalidated before any other members are destroyed.
190 rtc::WeakPtrFactory<VideoSendStreamImpl> weak_ptr_factory_;
191
Erik Språng4e193e42018-09-14 19:01:58 +0200192 // Context for the most recent and last sent video bitrate allocation. Used to
193 // throttle sending of similar bitrate allocations.
194 struct VbaSendContext {
195 VideoBitrateAllocation last_sent_allocation;
196 absl::optional<VideoBitrateAllocation> throttled_allocation;
197 int64_t last_send_time_ms;
198 };
199 absl::optional<VbaSendContext> video_bitrate_allocation_context_
Tommifa3ce632021-06-03 12:06:02 +0200200 RTC_GUARDED_BY(rtp_transport_queue_);
Tommie902f282021-06-03 12:02:02 +0200201 const absl::optional<float> configured_pacing_factor_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200202};
203} // namespace internal
204} // namespace webrtc
205#endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_