blob: 091ac0f8daaabc54c80517e709c07321459afc5f [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"
Steve Anton10542f22019-01-11 09:11:00 -080038#include "rtc_base/critical_section.h"
Jonas Olsson0182a032019-07-09 12:31:20 +020039#include "rtc_base/experiments/field_trial_parser.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"
44#include "video/call_stats.h"
Elad Alon14d1c9d2019-04-08 14:16:17 +020045#include "video/encoder_rtcp_feedback.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020046#include "video/send_delay_stats.h"
47#include "video/send_statistics_proxy.h"
48#include "video/video_send_stream.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020049
50namespace webrtc {
51namespace internal {
52
Christoffer Rodbro196c5ba2018-11-27 11:56:25 +010053// Pacing buffer config; overridden by ALR config if provided.
54struct PacingConfig {
55 PacingConfig();
56 PacingConfig(const PacingConfig&);
57 PacingConfig& operator=(const PacingConfig&) = default;
58 ~PacingConfig();
59 FieldTrialParameter<double> pacing_factor;
60 FieldTrialParameter<TimeDelta> max_pacing_delay;
61};
62
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020063// VideoSendStreamImpl implements internal::VideoSendStream.
64// It is created and destroyed on |worker_queue|. The intent is to decrease the
65// need for locking and to ensure methods are called in sequence.
66// Public methods except |DeliverRtcp| must be called on |worker_queue|.
67// DeliverRtcp is called on the libjingle worker thread or a network thread.
68// An encoder may deliver frames through the EncodedImageCallback on an
69// arbitrary thread.
70class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver,
Niels Möller213618e2018-07-24 09:29:58 +020071 public VideoStreamEncoderInterface::EncoderSink,
Stefan Holmer64be7fa2018-10-04 15:21:55 +020072 public VideoBitrateAllocationObserver {
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020073 public:
74 VideoSendStreamImpl(
Sebastian Jansson572c60f2019-03-04 18:30:41 +010075 Clock* clock,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020076 SendStatisticsProxy* stats_proxy,
77 rtc::TaskQueue* worker_queue,
78 CallStats* call_stats,
79 RtpTransportControllerSendInterface* transport,
Sebastian Jansson652dc912018-04-19 17:09:15 +020080 BitrateAllocatorInterface* bitrate_allocator,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020081 SendDelayStats* send_delay_stats,
Sebastian Jansson652dc912018-04-19 17:09:15 +020082 VideoStreamEncoderInterface* video_stream_encoder,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020083 RtcEventLog* event_log,
84 const VideoSendStream::Config* config,
85 int initial_encoder_max_bitrate,
86 double initial_encoder_bitrate_priority,
87 std::map<uint32_t, RtpState> suspended_ssrcs,
88 std::map<uint32_t, RtpPayloadState> suspended_payload_states,
89 VideoEncoderConfig::ContentType content_type,
Niels Möller46879152019-01-07 15:54:47 +010090 std::unique_ptr<FecController> fec_controller,
91 MediaTransportInterface* media_transport);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020092 ~VideoSendStreamImpl() override;
93
94 // RegisterProcessThread register |module_process_thread| with those objects
95 // that use it. Registration has to happen on the thread were
96 // |module_process_thread| was created (libjingle's worker thread).
97 // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue,
98 // maybe |worker_queue|.
99 void RegisterProcessThread(ProcessThread* module_process_thread);
100 void DeRegisterProcessThread();
101
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100102 void DeliverRtcp(const uint8_t* packet, size_t length);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200103 void UpdateActiveSimulcastLayers(const std::vector<bool> active_layers);
104 void Start();
105 void Stop();
106
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200107 // TODO(holmer): Move these to RtpTransportControllerSend.
108 std::map<uint32_t, RtpState> GetRtpStates() const;
109
110 std::map<uint32_t, RtpPayloadState> GetRtpPayloadStates() const;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200111
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200112 absl::optional<float> configured_pacing_factor_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200113
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200114 private:
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200115 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200116 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200117
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100118 void OnEncoderConfigurationChanged(
119 std::vector<VideoStream> streams,
120 VideoEncoderConfig::ContentType content_type,
121 int min_transmit_bitrate_bps) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200122
123 // Implements EncodedImageCallback. The implementation routes encoded frames
124 // to the |payload_router_| and |config.pre_encode_callback| if set.
125 // Called on an arbitrary encoder callback thread.
126 EncodedImageCallback::Result OnEncodedImage(
127 const EncodedImage& encoded_image,
128 const CodecSpecificInfo* codec_specific_info,
129 const RTPFragmentationHeader* fragmentation) override;
130
Jakob Ivarsson159b4172019-10-30 14:02:14 +0100131 // Implements EncodedImageCallback.
132 void OnDroppedFrame(EncodedImageCallback::DropReason reason) override;
133
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200134 // Implements VideoBitrateAllocationObserver.
Erik Språng566124a2018-04-23 12:32:22 +0200135 void OnBitrateAllocationUpdated(
136 const VideoBitrateAllocation& allocation) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200137
138 // Starts monitoring and sends a keyframe.
139 void StartupVideoSendStream();
140 // Removes the bitrate observer, stops monitoring and notifies the video
141 // encoder of the bitrate update.
Sebastian Janssonecb68972019-01-18 10:30:54 +0100142 void StopVideoSendStream() RTC_RUN_ON(worker_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200143
144 void ConfigureProtection();
145 void ConfigureSsrcs();
146 void SignalEncoderTimedOut();
147 void SignalEncoderActive();
Sebastian Jansson464a5572019-02-12 13:32:32 +0100148 MediaStreamAllocationConfig GetAllocationConfig() const
149 RTC_RUN_ON(worker_queue_);
Sebastian Jansson572c60f2019-03-04 18:30:41 +0100150 Clock* const clock_;
Erik Språngb57ab382018-09-13 10:52:38 +0200151 const bool has_alr_probing_;
Christoffer Rodbro196c5ba2018-11-27 11:56:25 +0100152 const PacingConfig pacing_config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200153
154 SendStatisticsProxy* const stats_proxy_;
155 const VideoSendStream::Config* const config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200156
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200157 rtc::TaskQueue* const worker_queue_;
158
Sebastian Janssonecb68972019-01-18 10:30:54 +0100159 RepeatingTaskHandle check_encoder_activity_task_
160 RTC_GUARDED_BY(worker_queue_);
161
162 std::atomic_bool activity_;
163 bool timed_out_ RTC_GUARDED_BY(worker_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200164
165 CallStats* const call_stats_;
166 RtpTransportControllerSendInterface* const transport_;
Sebastian Jansson652dc912018-04-19 17:09:15 +0200167 BitrateAllocatorInterface* const bitrate_allocator_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200168
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200169 rtc::CriticalSection ivf_writers_crit_;
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_;
177 bool has_packet_feedback_;
178
Sebastian Jansson652dc912018-04-19 17:09:15 +0200179 VideoStreamEncoderInterface* const video_stream_encoder_;
Elad Alon14d1c9d2019-04-08 14:16:17 +0200180 EncoderRtcpFeedback encoder_feedback_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200181
182 RtcpBandwidthObserver* const bandwidth_observer_;
Stefan Holmer9416ef82018-07-19 10:34:38 +0200183 RtpVideoSenderInterface* const rtp_video_sender_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200184
185 // |weak_ptr_| to our self. This is used since we can not call
186 // |weak_ptr_factory_.GetWeakPtr| from multiple sequences but it is ok to copy
187 // an existing WeakPtr.
188 rtc::WeakPtr<VideoSendStreamImpl> weak_ptr_;
189 // |weak_ptr_factory_| must be declared last to make sure all WeakPtr's are
190 // invalidated before any other members are destroyed.
191 rtc::WeakPtrFactory<VideoSendStreamImpl> weak_ptr_factory_;
192
Erik Språng4e193e42018-09-14 19:01:58 +0200193 // Context for the most recent and last sent video bitrate allocation. Used to
194 // throttle sending of similar bitrate allocations.
195 struct VbaSendContext {
196 VideoBitrateAllocation last_sent_allocation;
197 absl::optional<VideoBitrateAllocation> throttled_allocation;
198 int64_t last_send_time_ms;
199 };
200 absl::optional<VbaSendContext> video_bitrate_allocation_context_
201 RTC_GUARDED_BY(worker_queue_);
Niels Möller46879152019-01-07 15:54:47 +0100202 MediaTransportInterface* const media_transport_;
203 rtc::CriticalSection media_transport_id_lock_;
204 int64_t media_transport_frame_id_ RTC_GUARDED_BY(media_transport_id_lock_) =
205 0;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200206};
207} // namespace internal
208} // namespace webrtc
209#endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_