blob: 4c5e3e4634bde3c335ee56227bb9dd8b3163628f [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>
Sebastian Janssonecb68972019-01-18 10:30:54 +010015#include <atomic>
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020016#include <map>
17#include <memory>
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020018#include <vector>
19
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "absl/types/optional.h"
21#include "api/fec_controller.h"
22#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 "logging/rtc_event_log/rtc_event_log.h"
33#include "modules/include/module_common_types.h"
34#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020035#include "modules/utility/include/process_thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010036#include "modules/video_coding/include/video_codec_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080037#include "rtc_base/critical_section.h"
Yves Gerey3e707812018-11-28 16:47:49 +010038#include "rtc_base/task_queue.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 "rtc_base/weak_ptr.h"
42#include "video/call_stats.h"
Niels Möllerfa89d842019-01-30 16:33:45 +010043#include "video/encoder_key_frame_callback.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020044#include "video/send_delay_stats.h"
45#include "video/send_statistics_proxy.h"
46#include "video/video_send_stream.h"
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020047
48namespace webrtc {
49namespace internal {
50
Christoffer Rodbro196c5ba2018-11-27 11:56:25 +010051// Pacing buffer config; overridden by ALR config if provided.
52struct PacingConfig {
53 PacingConfig();
54 PacingConfig(const PacingConfig&);
55 PacingConfig& operator=(const PacingConfig&) = default;
56 ~PacingConfig();
57 FieldTrialParameter<double> pacing_factor;
58 FieldTrialParameter<TimeDelta> max_pacing_delay;
59};
60
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020061// VideoSendStreamImpl implements internal::VideoSendStream.
62// It is created and destroyed on |worker_queue|. The intent is to decrease the
63// need for locking and to ensure methods are called in sequence.
64// Public methods except |DeliverRtcp| must be called on |worker_queue|.
65// DeliverRtcp is called on the libjingle worker thread or a network thread.
66// An encoder may deliver frames through the EncodedImageCallback on an
67// arbitrary thread.
68class VideoSendStreamImpl : public webrtc::BitrateAllocatorObserver,
Niels Möller213618e2018-07-24 09:29:58 +020069 public VideoStreamEncoderInterface::EncoderSink,
Stefan Holmer64be7fa2018-10-04 15:21:55 +020070 public VideoBitrateAllocationObserver {
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020071 public:
72 VideoSendStreamImpl(
73 SendStatisticsProxy* stats_proxy,
74 rtc::TaskQueue* worker_queue,
75 CallStats* call_stats,
76 RtpTransportControllerSendInterface* transport,
Sebastian Jansson652dc912018-04-19 17:09:15 +020077 BitrateAllocatorInterface* bitrate_allocator,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020078 SendDelayStats* send_delay_stats,
Sebastian Jansson652dc912018-04-19 17:09:15 +020079 VideoStreamEncoderInterface* video_stream_encoder,
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020080 RtcEventLog* event_log,
81 const VideoSendStream::Config* config,
82 int initial_encoder_max_bitrate,
83 double initial_encoder_bitrate_priority,
84 std::map<uint32_t, RtpState> suspended_ssrcs,
85 std::map<uint32_t, RtpPayloadState> suspended_payload_states,
86 VideoEncoderConfig::ContentType content_type,
Niels Möller46879152019-01-07 15:54:47 +010087 std::unique_ptr<FecController> fec_controller,
88 MediaTransportInterface* media_transport);
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,
95 // maybe |worker_queue|.
96 void RegisterProcessThread(ProcessThread* module_process_thread);
97 void DeRegisterProcessThread();
98
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +020099 bool DeliverRtcp(const uint8_t* packet, size_t length);
100 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
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200109 absl::optional<float> configured_pacing_factor_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200110
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200111 private:
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200112 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200113 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200114
Rasmus Brandtc402dbe2019-02-04 11:09:46 +0100115 void OnEncoderConfigurationChanged(
116 std::vector<VideoStream> streams,
117 VideoEncoderConfig::ContentType content_type,
118 int min_transmit_bitrate_bps) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200119
120 // Implements EncodedImageCallback. The implementation routes encoded frames
121 // to the |payload_router_| and |config.pre_encode_callback| if set.
122 // Called on an arbitrary encoder callback thread.
123 EncodedImageCallback::Result OnEncodedImage(
124 const EncodedImage& encoded_image,
125 const CodecSpecificInfo* codec_specific_info,
126 const RTPFragmentationHeader* fragmentation) override;
127
128 // Implements VideoBitrateAllocationObserver.
Erik Språng566124a2018-04-23 12:32:22 +0200129 void OnBitrateAllocationUpdated(
130 const VideoBitrateAllocation& allocation) override;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200131
132 // Starts monitoring and sends a keyframe.
133 void StartupVideoSendStream();
134 // Removes the bitrate observer, stops monitoring and notifies the video
135 // encoder of the bitrate update.
Sebastian Janssonecb68972019-01-18 10:30:54 +0100136 void StopVideoSendStream() RTC_RUN_ON(worker_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200137
138 void ConfigureProtection();
139 void ConfigureSsrcs();
140 void SignalEncoderTimedOut();
141 void SignalEncoderActive();
142
Erik Språngb57ab382018-09-13 10:52:38 +0200143 const bool has_alr_probing_;
Christoffer Rodbro196c5ba2018-11-27 11:56:25 +0100144 const PacingConfig pacing_config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200145
146 SendStatisticsProxy* const stats_proxy_;
147 const VideoSendStream::Config* const config_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200148
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200149 rtc::TaskQueue* const worker_queue_;
150
Sebastian Janssonecb68972019-01-18 10:30:54 +0100151 RepeatingTaskHandle check_encoder_activity_task_
152 RTC_GUARDED_BY(worker_queue_);
153
154 std::atomic_bool activity_;
155 bool timed_out_ RTC_GUARDED_BY(worker_queue_);
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200156
157 CallStats* const call_stats_;
158 RtpTransportControllerSendInterface* const transport_;
Sebastian Jansson652dc912018-04-19 17:09:15 +0200159 BitrateAllocatorInterface* const bitrate_allocator_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200160
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200161 rtc::CriticalSection ivf_writers_crit_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200162
163 int max_padding_bitrate_;
164 int encoder_min_bitrate_bps_;
165 uint32_t encoder_max_bitrate_bps_;
166 uint32_t encoder_target_rate_bps_;
167 double encoder_bitrate_priority_;
168 bool has_packet_feedback_;
169
Sebastian Jansson652dc912018-04-19 17:09:15 +0200170 VideoStreamEncoderInterface* const video_stream_encoder_;
Niels Möllerfa89d842019-01-30 16:33:45 +0100171 EncoderKeyFrameCallback encoder_feedback_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200172
173 RtcpBandwidthObserver* const bandwidth_observer_;
Stefan Holmer9416ef82018-07-19 10:34:38 +0200174 RtpVideoSenderInterface* const rtp_video_sender_;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200175
176 // |weak_ptr_| to our self. This is used since we can not call
177 // |weak_ptr_factory_.GetWeakPtr| from multiple sequences but it is ok to copy
178 // an existing WeakPtr.
179 rtc::WeakPtr<VideoSendStreamImpl> weak_ptr_;
180 // |weak_ptr_factory_| must be declared last to make sure all WeakPtr's are
181 // invalidated before any other members are destroyed.
182 rtc::WeakPtrFactory<VideoSendStreamImpl> weak_ptr_factory_;
183
Erik Språng4e193e42018-09-14 19:01:58 +0200184 // Context for the most recent and last sent video bitrate allocation. Used to
185 // throttle sending of similar bitrate allocations.
186 struct VbaSendContext {
187 VideoBitrateAllocation last_sent_allocation;
188 absl::optional<VideoBitrateAllocation> throttled_allocation;
189 int64_t last_send_time_ms;
190 };
191 absl::optional<VbaSendContext> video_bitrate_allocation_context_
192 RTC_GUARDED_BY(worker_queue_);
Niels Möller46879152019-01-07 15:54:47 +0100193 MediaTransportInterface* const media_transport_;
194 rtc::CriticalSection media_transport_id_lock_;
195 int64_t media_transport_frame_id_ RTC_GUARDED_BY(media_transport_id_lock_) =
196 0;
Sebastian Jansson8e0b15b2018-04-18 19:19:22 +0200197};
198} // namespace internal
199} // namespace webrtc
200#endif // VIDEO_VIDEO_SEND_STREAM_IMPL_H_