blob: 834fed46931c6f73c87fb1681d5d2409cd3c8279 [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.
63// It is created and destroyed on |worker_queue|. The intent is to decrease the
64// need for locking and to ensure methods are called in sequence.
65// Public methods except |DeliverRtcp| must be called on |worker_queue|.
66// 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,
Niels Möller213618e2018-07-24 09:29:58 +020070 public VideoStreamEncoderInterface::EncoderSink,
Stefan Holmer64be7fa2018-10-04 15:21:55 +020071 public VideoBitrateAllocationObserver {
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020072 public:
73 VideoSendStreamImpl(
Sebastian Jansson572c60f2019-03-04 18:30:41 +010074 Clock* clock,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020075 SendStatisticsProxy* stats_proxy,
76 rtc::TaskQueue* worker_queue,
Tommi8ae18ad2020-05-03 22:45:02 +020077 RtcpRttStats* call_stats,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020078 RtpTransportControllerSendInterface* transport,
Sebastian Jansson652dc912018-04-19 17:09:15 +020079 BitrateAllocatorInterface* bitrate_allocator,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020080 SendDelayStats* send_delay_stats,
Sebastian Jansson652dc912018-04-19 17:09:15 +020081 VideoStreamEncoderInterface* video_stream_encoder,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020082 RtcEventLog* event_log,
83 const VideoSendStream::Config* config,
84 int initial_encoder_max_bitrate,
85 double initial_encoder_bitrate_priority,
86 std::map<uint32_t, RtpState> suspended_ssrcs,
87 std::map<uint32_t, RtpPayloadState> suspended_payload_states,
88 VideoEncoderConfig::ContentType content_type,
Bjorn A Mellem7a9a0922019-11-26 09:19:40 -080089 std::unique_ptr<FecController> fec_controller);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020090 ~VideoSendStreamImpl() override;
91
92 // RegisterProcessThread register |module_process_thread| with those objects
93 // that use it. Registration has to happen on the thread were
94 // |module_process_thread| was created (libjingle's worker thread).
95 // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue,
96 // maybe |worker_queue|.
97 void RegisterProcessThread(ProcessThread* module_process_thread);
98 void DeRegisterProcessThread();
99
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100100 void DeliverRtcp(const uint8_t* packet, size_t length);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200101 void UpdateActiveSimulcastLayers(const std::vector<bool> active_layers);
102 void Start();
103 void Stop();
104
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200105 // TODO(holmer): Move these to RtpTransportControllerSend.
106 std::map<uint32_t, RtpState> GetRtpStates() const;
107
108 std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200109
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200110 absl::optional<float> configured_pacing_factor_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200111
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200112 private:
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200113 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200114 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200115
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100116 void OnEncoderConfigurationChanged(
117 std::vector<VideoStream> streams,
Ilya Nikolaevskiy93be66c2020-04-02 14:10:27 +0200118 bool is_svc,
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100119 VideoEncoderConfig::ContentType content_type,
120 int min_transmit_bitrate_bps) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200121
122 // Implements EncodedImageCallback. The implementation routes encoded frames
123 // to the |payload_router_| and |config.pre_encode_callback| if set.
124 // Called on an arbitrary encoder callback thread.
125 EncodedImageCallback::Result OnEncodedImage(
126 const EncodedImage& encoded_image,
127 const CodecSpecificInfo* codec_specific_info,
128 const RTPFragmentationHeader* fragmentation) override;
129
Jakob Ivarsson159b4172019-10-30 14:02:14 +0100130 // Implements EncodedImageCallback.
131 void OnDroppedFrame(EncodedImageCallback::DropReason reason) override;
132
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200133 // Implements VideoBitrateAllocationObserver.
Erik Språng566124a2018-04-23 12:32:22 +0200134 void OnBitrateAllocationUpdated(
135 const VideoBitrateAllocation& allocation) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200136
137 // Starts monitoring and sends a keyframe.
138 void StartupVideoSendStream();
139 // Removes the bitrate observer, stops monitoring and notifies the video
140 // encoder of the bitrate update.
Sebastian Janssonecb68972019-01-18 10:30:54 +0100141 void StopVideoSendStream() RTC_RUN_ON(worker_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200142
143 void ConfigureProtection();
144 void ConfigureSsrcs();
145 void SignalEncoderTimedOut();
146 void SignalEncoderActive();
Sebastian Jansson464a5572019-02-12 13:32:32 +0100147 MediaStreamAllocationConfig GetAllocationConfig() const
148 RTC_RUN_ON(worker_queue_);
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100149 Clock* const clock_;
Erik Språngb57ab382018-09-13 10:52:38 +0200150 const bool has_alr_probing_;
Christoffer Rodbro196c5ba2018-11-27 11:56:25 +0100151 const PacingConfig pacing_config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200152
153 SendStatisticsProxy* const stats_proxy_;
154 const VideoSendStream::Config* const config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200155
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200156 rtc::TaskQueue* const worker_queue_;
157
Sebastian Janssonecb68972019-01-18 10:30:54 +0100158 RepeatingTaskHandle check_encoder_activity_task_
159 RTC_GUARDED_BY(worker_queue_);
160
161 std::atomic_bool activity_;
162 bool timed_out_ RTC_GUARDED_BY(worker_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200163
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200164 RtpTransportControllerSendInterface* const transport_;
Sebastian Jansson652dc912018-04-19 17:09:15 +0200165 BitrateAllocatorInterface* const bitrate_allocator_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200166
Markus Handella3765182020-07-08 13:13:32 +0200167 Mutex ivf_writers_mutex_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200168
Ilya Nikolaevskiyaa9aa572019-04-11 09:20:24 +0200169 bool disable_padding_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200170 int max_padding_bitrate_;
171 int encoder_min_bitrate_bps_;
172 uint32_t encoder_max_bitrate_bps_;
173 uint32_t encoder_target_rate_bps_;
174 double encoder_bitrate_priority_;
175 bool has_packet_feedback_;
176
Sebastian Jansson652dc912018-04-19 17:09:15 +0200177 VideoStreamEncoderInterface* const video_stream_encoder_;
Elad Alon14d1c9d2019-04-08 14:16:17 +0200178 EncoderRtcpFeedback encoder_feedback_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200179
180 RtcpBandwidthObserver* const bandwidth_observer_;
Stefan Holmer9416ef82018-07-19 10:34:38 +0200181 RtpVideoSenderInterface* const rtp_video_sender_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200182
183 // |weak_ptr_| to our self. This is used since we can not call
184 // |weak_ptr_factory_.GetWeakPtr| from multiple sequences but it is ok to copy
185 // an existing WeakPtr.
186 rtc::WeakPtr<VideoSendStreamImpl> weak_ptr_;
187 // |weak_ptr_factory_| must be declared last to make sure all WeakPtr's are
188 // invalidated before any other members are destroyed.
189 rtc::WeakPtrFactory<VideoSendStreamImpl> weak_ptr_factory_;
190
Erik Språng4e193e42018-09-14 19:01:58 +0200191 // Context for the most recent and last sent video bitrate allocation. Used to
192 // throttle sending of similar bitrate allocations.
193 struct VbaSendContext {
194 VideoBitrateAllocation last_sent_allocation;
195 absl::optional<VideoBitrateAllocation> throttled_allocation;
196 int64_t last_send_time_ms;
197 };
198 absl::optional<VbaSendContext> video_bitrate_allocation_context_
199 RTC_GUARDED_BY(worker_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200200};
201} // namespace internal
202} // namespace webrtc
203#endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_