blob: 3c6b86a3c92d1970a6d8aae1c9630235b2353663 [file] [log] [blame]
eladalonf1841382017-06-12 01:16:46 -07001/*
2 * Copyright (c) 2014 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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_
12#define MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_
eladalonf1841382017-06-12 01:16:46 -070013
14#include <map>
15#include <memory>
16#include <set>
17#include <string>
18#include <vector>
19
Danil Chapovalov00c71832018-06-15 15:58:38 +020020#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/call/transport.h"
Jiawei Ouc2ebe212018-11-08 10:02:56 -080022#include "api/video/video_bitrate_allocator_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/video/video_frame.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020024#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020025#include "api/video/video_source_interface.h"
26#include "api/video_codecs/sdp_video_format.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "call/call.h"
28#include "call/flexfec_receive_stream.h"
29#include "call/video_receive_stream.h"
30#include "call/video_send_stream.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "media/base/media_engine.h"
Ying Wang4271afb2019-08-27 12:16:38 +020032#include "media/engine/constants.h"
Jonas Oreland6d835922019-03-18 10:59:40 +010033#include "media/engine/unhandled_packets_buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080034#include "rtc_base/async_invoker.h"
35#include "rtc_base/critical_section.h"
Ying Wang4271afb2019-08-27 12:16:38 +020036#include "rtc_base/experiments/field_trial_parser.h"
Steve Anton10542f22019-01-11 09:11:00 -080037#include "rtc_base/network_route.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "rtc_base/thread_annotations.h"
39#include "rtc_base/thread_checker.h"
Ying Wang4271afb2019-08-27 12:16:38 +020040#include "system_wrappers/include/field_trial.h"
eladalonf1841382017-06-12 01:16:46 -070041
42namespace webrtc {
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020043class VideoDecoderFactory;
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020044class VideoEncoderFactory;
eladalonf1841382017-06-12 01:16:46 -070045struct MediaConfig;
Yves Gerey665174f2018-06-19 15:03:05 +020046} // namespace webrtc
eladalonf1841382017-06-12 01:16:46 -070047
48namespace rtc {
49class Thread;
50} // namespace rtc
51
52namespace cricket {
53
Ying Wang4271afb2019-08-27 12:16:38 +020054struct MinVideoBitrateConfig {
55 webrtc::FieldTrialParameter<webrtc::DataRate> min_video_bitrate;
56
57 MinVideoBitrateConfig()
Ying Wang8c5520c2019-09-03 15:25:21 +000058 : min_video_bitrate("br", webrtc::DataRate::bps(kMinVideoBitrateBps)) {
Ying Wang4271afb2019-08-27 12:16:38 +020059 webrtc::ParseFieldTrial(
60 {&min_video_bitrate},
61 webrtc::field_trial::FindFullName(kMinVideoBitrateExperiment));
62 }
63};
64
eladalonf1841382017-06-12 01:16:46 -070065class WebRtcVideoChannel;
eladalonf1841382017-06-12 01:16:46 -070066
eladalonf1841382017-06-12 01:16:46 -070067class UnsignalledSsrcHandler {
68 public:
69 enum Action {
70 kDropPacket,
71 kDeliverPacket,
72 };
73 virtual Action OnUnsignalledSsrc(WebRtcVideoChannel* channel,
74 uint32_t ssrc) = 0;
75 virtual ~UnsignalledSsrcHandler() = default;
76};
77
78// TODO(pbos): Remove, use external handlers only.
79class DefaultUnsignalledSsrcHandler : public UnsignalledSsrcHandler {
80 public:
81 DefaultUnsignalledSsrcHandler();
Yves Gerey665174f2018-06-19 15:03:05 +020082 Action OnUnsignalledSsrc(WebRtcVideoChannel* channel, uint32_t ssrc) override;
eladalonf1841382017-06-12 01:16:46 -070083
84 rtc::VideoSinkInterface<webrtc::VideoFrame>* GetDefaultSink() const;
85 void SetDefaultSink(WebRtcVideoChannel* channel,
86 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
87
88 virtual ~DefaultUnsignalledSsrcHandler() = default;
89
90 private:
91 rtc::VideoSinkInterface<webrtc::VideoFrame>* default_sink_;
92};
93
94// WebRtcVideoEngine is used for the new native WebRTC Video API (webrtc:1667).
Sebastian Jansson84848f22018-11-16 10:40:36 +010095class WebRtcVideoEngine : public VideoEngineInterface {
eladalonf1841382017-06-12 01:16:46 -070096 public:
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020097 // These video codec factories represents all video codecs, i.e. both software
98 // and external hardware codecs.
99 WebRtcVideoEngine(
100 std::unique_ptr<webrtc::VideoEncoderFactory> video_encoder_factory,
Jonas Orelanda3aa9bd2019-04-17 07:38:40 +0200101 std::unique_ptr<webrtc::VideoDecoderFactory> video_decoder_factory);
Magnus Jedvertd4b0c052017-09-14 10:24:54 +0200102
Sebastian Jansson84848f22018-11-16 10:40:36 +0100103 ~WebRtcVideoEngine() override;
eladalonf1841382017-06-12 01:16:46 -0700104
Sebastian Jansson84848f22018-11-16 10:40:36 +0100105 VideoMediaChannel* CreateMediaChannel(
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700106 webrtc::Call* call,
107 const MediaConfig& config,
108 const VideoOptions& options,
Jonas Orelanda3aa9bd2019-04-17 07:38:40 +0200109 const webrtc::CryptoOptions& crypto_options,
110 webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory)
111 override;
eladalonf1841382017-06-12 01:16:46 -0700112
Sebastian Jansson84848f22018-11-16 10:40:36 +0100113 std::vector<VideoCodec> codecs() const override;
114 RtpCapabilities GetCapabilities() const override;
eladalonf1841382017-06-12 01:16:46 -0700115
eladalonf1841382017-06-12 01:16:46 -0700116 private:
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200117 const std::unique_ptr<webrtc::VideoDecoderFactory> decoder_factory_;
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100118 const std::unique_ptr<webrtc::VideoEncoderFactory> encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800119 const std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
120 bitrate_allocator_factory_;
eladalonf1841382017-06-12 01:16:46 -0700121};
122
philipele8ed8302019-07-03 11:53:48 +0200123class WebRtcVideoChannel : public VideoMediaChannel,
124 public webrtc::Transport,
philipeld9cc8c02019-09-16 14:53:40 +0200125 public webrtc::EncoderSwitchRequestCallback {
eladalonf1841382017-06-12 01:16:46 -0700126 public:
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800127 WebRtcVideoChannel(
128 webrtc::Call* call,
129 const MediaConfig& config,
130 const VideoOptions& options,
131 const webrtc::CryptoOptions& crypto_options,
132 webrtc::VideoEncoderFactory* encoder_factory,
133 webrtc::VideoDecoderFactory* decoder_factory,
134 webrtc::VideoBitrateAllocatorFactory* bitrate_allocator_factory);
eladalonf1841382017-06-12 01:16:46 -0700135 ~WebRtcVideoChannel() override;
136
137 // VideoMediaChannel implementation
eladalonf1841382017-06-12 01:16:46 -0700138 bool SetSendParameters(const VideoSendParameters& params) override;
139 bool SetRecvParameters(const VideoRecvParameters& params) override;
140 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800141 webrtc::RTCError SetRtpSendParameters(
142 uint32_t ssrc,
143 const webrtc::RtpParameters& parameters) override;
eladalonf1841382017-06-12 01:16:46 -0700144 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const override;
145 bool SetRtpReceiveParameters(
146 uint32_t ssrc,
147 const webrtc::RtpParameters& parameters) override;
148 bool GetSendCodec(VideoCodec* send_codec) override;
149 bool SetSend(bool send) override;
150 bool SetVideoSend(
151 uint32_t ssrc,
eladalonf1841382017-06-12 01:16:46 -0700152 const VideoOptions* options,
153 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) override;
154 bool AddSendStream(const StreamParams& sp) override;
155 bool RemoveSendStream(uint32_t ssrc) override;
156 bool AddRecvStream(const StreamParams& sp) override;
157 bool AddRecvStream(const StreamParams& sp, bool default_stream);
158 bool RemoveRecvStream(uint32_t ssrc) override;
159 bool SetSink(uint32_t ssrc,
160 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
161 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) override;
162 bool GetStats(VideoMediaInfo* info) override;
163
Amit Hilbuche7a5f7b2019-03-12 11:10:27 -0700164 void OnPacketReceived(rtc::CopyOnWriteBuffer packet,
Niels Möllere6933812018-11-05 13:01:41 +0100165 int64_t packet_time_us) override;
Amit Hilbuche7a5f7b2019-03-12 11:10:27 -0700166 void OnRtcpReceived(rtc::CopyOnWriteBuffer packet,
Niels Möllere6933812018-11-05 13:01:41 +0100167 int64_t packet_time_us) override;
eladalonf1841382017-06-12 01:16:46 -0700168 void OnReadyToSend(bool ready) override;
169 void OnNetworkRouteChanged(const std::string& transport_name,
170 const rtc::NetworkRoute& network_route) override;
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700171 void SetInterface(
172 NetworkInterface* iface,
173 const webrtc::MediaTransportConfig& media_transport_config) override;
eladalonf1841382017-06-12 01:16:46 -0700174
Benjamin Wright192eeec2018-10-17 17:27:25 -0700175 // E2E Encrypted Video Frame API
176 // Set a frame decryptor to a particular ssrc that will intercept all
177 // incoming video frames and attempt to decrypt them before forwarding the
178 // result.
179 void SetFrameDecryptor(uint32_t ssrc,
180 rtc::scoped_refptr<webrtc::FrameDecryptorInterface>
181 frame_decryptor) override;
182 // Set a frame encryptor to a particular ssrc that will intercept all
183 // outgoing video frames and attempt to encrypt them and forward the result
184 // to the packetizer.
185 void SetFrameEncryptor(uint32_t ssrc,
186 rtc::scoped_refptr<webrtc::FrameEncryptorInterface>
187 frame_encryptor) override;
188
Ruslan Burakov493a6502019-02-27 15:32:48 +0100189 bool SetBaseMinimumPlayoutDelayMs(uint32_t ssrc, int delay_ms) override;
190
191 absl::optional<int> GetBaseMinimumPlayoutDelayMs(
192 uint32_t ssrc) const override;
193
eladalonf1841382017-06-12 01:16:46 -0700194 // Implemented for VideoMediaChannelTest.
Steve Antonef50b252019-03-01 15:15:38 -0800195 bool sending() const {
196 RTC_DCHECK_RUN_ON(&thread_checker_);
197 return sending_;
198 }
eladalonf1841382017-06-12 01:16:46 -0700199
Danil Chapovalov00c71832018-06-15 15:58:38 +0200200 absl::optional<uint32_t> GetDefaultReceiveStreamSsrc();
eladalonf1841382017-06-12 01:16:46 -0700201
Steve Antonef50b252019-03-01 15:15:38 -0800202 StreamParams unsignaled_stream_params() {
203 RTC_DCHECK_RUN_ON(&thread_checker_);
204 return unsignaled_stream_params_;
205 }
Seth Hampson5897a6e2018-04-03 11:16:33 -0700206
eladalonf1841382017-06-12 01:16:46 -0700207 // AdaptReason is used for expressing why a WebRtcVideoSendStream request
208 // a lower input frame size than the currently configured camera input frame
209 // size. There can be more than one reason OR:ed together.
210 enum AdaptReason {
211 ADAPTREASON_NONE = 0,
212 ADAPTREASON_CPU = 1,
213 ADAPTREASON_BANDWIDTH = 2,
214 };
215
sprang67561a62017-06-15 06:34:42 -0700216 static constexpr int kDefaultQpMax = 56;
217
Jonas Oreland49ac5952018-09-26 16:04:32 +0200218 std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const override;
219
Jonas Oreland6d835922019-03-18 10:59:40 +0100220 // Take the buffered packets for |ssrcs| and feed them into DeliverPacket.
221 // This method does nothing unless unknown_ssrc_packet_buffer_ is configured.
222 void BackfillBufferedPackets(rtc::ArrayView<const uint32_t> ssrcs);
223
philipeld9cc8c02019-09-16 14:53:40 +0200224 // Implements webrtc::EncoderSwitchRequestCallback.
225 void RequestEncoderFallback() override;
226 void RequestEncoderSwitch(
227 const EncoderSwitchRequestCallback::Config& conf) override;
philipele8ed8302019-07-03 11:53:48 +0200228
eladalonf1841382017-06-12 01:16:46 -0700229 private:
230 class WebRtcVideoReceiveStream;
231 struct VideoCodecSettings {
232 VideoCodecSettings();
233
234 // Checks if all members of |*this| are equal to the corresponding members
235 // of |other|.
236 bool operator==(const VideoCodecSettings& other) const;
237 bool operator!=(const VideoCodecSettings& other) const;
238
239 // Checks if all members of |a|, except |flexfec_payload_type|, are equal
240 // to the corresponding members of |b|.
241 static bool EqualsDisregardingFlexfec(const VideoCodecSettings& a,
242 const VideoCodecSettings& b);
243
244 VideoCodec codec;
245 webrtc::UlpfecConfig ulpfec;
Steve Anton2d2bbb12019-08-07 09:57:59 -0700246 int flexfec_payload_type; // -1 if absent.
247 int rtx_payload_type; // -1 if absent.
eladalonf1841382017-06-12 01:16:46 -0700248 };
249
250 struct ChangedSendParameters {
251 // These optionals are unset if not changed.
philipele8ed8302019-07-03 11:53:48 +0200252 absl::optional<VideoCodecSettings> send_codec;
253 absl::optional<std::vector<VideoCodecSettings>> negotiated_codecs;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200254 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
255 absl::optional<std::string> mid;
Johannes Kron9190b822018-10-29 11:22:05 +0100256 absl::optional<bool> extmap_allow_mixed;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200257 absl::optional<int> max_bandwidth_bps;
258 absl::optional<bool> conference_mode;
259 absl::optional<webrtc::RtcpMode> rtcp_mode;
eladalonf1841382017-06-12 01:16:46 -0700260 };
261
262 struct ChangedRecvParameters {
263 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200264 absl::optional<std::vector<VideoCodecSettings>> codec_settings;
265 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
eladalonf1841382017-06-12 01:16:46 -0700266 // Keep track of the FlexFEC payload type separately from |codec_settings|.
267 // This allows us to recreate the FlexfecReceiveStream separately from the
268 // VideoReceiveStream when the FlexFEC payload type is changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200269 absl::optional<int> flexfec_payload_type;
eladalonf1841382017-06-12 01:16:46 -0700270 };
271
272 bool GetChangedSendParameters(const VideoSendParameters& params,
Steve Antonef50b252019-03-01 15:15:38 -0800273 ChangedSendParameters* changed_params) const
274 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
philipele8ed8302019-07-03 11:53:48 +0200275 bool ApplyChangedParams(const ChangedSendParameters& changed_params);
eladalonf1841382017-06-12 01:16:46 -0700276 bool GetChangedRecvParameters(const VideoRecvParameters& params,
Steve Antonef50b252019-03-01 15:15:38 -0800277 ChangedRecvParameters* changed_params) const
278 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700279
eladalonf1841382017-06-12 01:16:46 -0700280 void ConfigureReceiverRtp(
281 webrtc::VideoReceiveStream::Config* config,
282 webrtc::FlexfecReceiveStream::Config* flexfec_config,
Steve Antonef50b252019-03-01 15:15:38 -0800283 const StreamParams& sp) const
284 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700285 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
Steve Antonef50b252019-03-01 15:15:38 -0800286 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700287 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
Steve Antonef50b252019-03-01 15:15:38 -0800288 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700289 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
Steve Antonef50b252019-03-01 15:15:38 -0800290 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700291
292 static std::string CodecSettingsVectorToString(
293 const std::vector<VideoCodecSettings>& codecs);
294
295 // Wrapper for the sender part.
Christian Fremerey6c025412019-02-13 19:43:28 +0000296 class WebRtcVideoSendStream
297 : public rtc::VideoSourceInterface<webrtc::VideoFrame> {
eladalonf1841382017-06-12 01:16:46 -0700298 public:
299 WebRtcVideoSendStream(
300 webrtc::Call* call,
301 const StreamParams& sp,
302 webrtc::VideoSendStream::Config config,
303 const VideoOptions& options,
eladalonf1841382017-06-12 01:16:46 -0700304 bool enable_cpu_overuse_detection,
305 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200306 const absl::optional<VideoCodecSettings>& codec_settings,
307 const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions,
eladalonf1841382017-06-12 01:16:46 -0700308 const VideoSendParameters& send_params);
309 virtual ~WebRtcVideoSendStream();
310
311 void SetSendParameters(const ChangedSendParameters& send_params);
Zach Steinba37b4b2018-01-23 15:02:36 -0800312 webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters);
eladalonf1841382017-06-12 01:16:46 -0700313 webrtc::RtpParameters GetRtpParameters() const;
314
Benjamin Wright192eeec2018-10-17 17:27:25 -0700315 void SetFrameEncryptor(
316 rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor);
317
Christian Fremerey6c025412019-02-13 19:43:28 +0000318 // Implements rtc::VideoSourceInterface<webrtc::VideoFrame>.
319 // WebRtcVideoSendStream acts as a source to the webrtc::VideoSendStream
320 // in |stream_|. This is done to proxy VideoSinkWants from the encoder to
321 // the worker thread.
322 void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
323 const rtc::VideoSinkWants& wants) override;
324 void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
325
Niels Möllerff40b142018-04-09 08:49:14 +0200326 bool SetVideoSend(const VideoOptions* options,
eladalonf1841382017-06-12 01:16:46 -0700327 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
328
329 void SetSend(bool send);
330
331 const std::vector<uint32_t>& GetSsrcs() const;
332 VideoSenderInfo GetVideoSenderInfo(bool log_stats);
333 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
334
335 private:
336 // Parameters needed to reconstruct the underlying stream.
337 // webrtc::VideoSendStream doesn't support setting a lot of options on the
338 // fly, so when those need to be changed we tear down and reconstruct with
339 // similar parameters depending on which options changed etc.
340 struct VideoSendStreamParameters {
341 VideoSendStreamParameters(
342 webrtc::VideoSendStream::Config config,
343 const VideoOptions& options,
344 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200345 const absl::optional<VideoCodecSettings>& codec_settings);
eladalonf1841382017-06-12 01:16:46 -0700346 webrtc::VideoSendStream::Config config;
347 VideoOptions options;
348 int max_bitrate_bps;
349 bool conference_mode;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200350 absl::optional<VideoCodecSettings> codec_settings;
eladalonf1841382017-06-12 01:16:46 -0700351 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
352 // typically changes when setting a new resolution or reconfiguring
353 // bitrates.
354 webrtc::VideoEncoderConfig encoder_config;
355 };
356
eladalonf1841382017-06-12 01:16:46 -0700357 rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
358 ConfigureVideoEncoderSettings(const VideoCodec& codec);
Niels Möller5bf8ccd2018-03-15 14:16:11 +0100359 void SetCodec(const VideoCodecSettings& codec);
eladalonf1841382017-06-12 01:16:46 -0700360 void RecreateWebRtcStream();
361 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
362 const VideoCodec& codec) const;
363 void ReconfigureEncoder();
eladalonf1841382017-06-12 01:16:46 -0700364
365 // Calls Start or Stop according to whether or not |sending_| is true,
366 // and whether or not the encoding in |rtp_parameters_| is active.
367 void UpdateSendState();
368
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700369 webrtc::DegradationPreference GetDegradationPreference() const
370 RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700371
372 rtc::ThreadChecker thread_checker_;
eladalonf1841382017-06-12 01:16:46 -0700373 rtc::Thread* worker_thread_;
Niels Möller1e062892018-02-07 10:18:32 +0100374 const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_);
375 const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700376 webrtc::Call* const call_;
377 const bool enable_cpu_overuse_detection_;
378 rtc::VideoSourceInterface<webrtc::VideoFrame>* source_
Niels Möller1e062892018-02-07 10:18:32 +0100379 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700380
Niels Möller1e062892018-02-07 10:18:32 +0100381 webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_);
Christian Fremerey6c025412019-02-13 19:43:28 +0000382 rtc::VideoSinkInterface<webrtc::VideoFrame>* encoder_sink_
383 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700384 // Contains settings that are the same for all streams in the MediaChannel,
385 // such as codecs, header extensions, and the global bitrate limit for the
386 // entire channel.
Niels Möller1e062892018-02-07 10:18:32 +0100387 VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700388 // Contains settings that are unique for each stream, such as max_bitrate.
389 // Does *not* contain codecs, however.
390 // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_.
391 // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only
392 // one stream per MediaChannel.
Niels Möller1e062892018-02-07 10:18:32 +0100393 webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700394
Niels Möller1e062892018-02-07 10:18:32 +0100395 bool sending_ RTC_GUARDED_BY(&thread_checker_);
philipel98cbb222019-06-14 11:28:51 +0200396
Bjorn A Mellemda4f0932019-07-30 08:34:03 -0700397 const bool use_standard_bytes_stats_;
398
philipel98cbb222019-06-14 11:28:51 +0200399 // In order for the |invoker_| to protect other members from being
400 // destructed as they are used in asynchronous tasks it has to be destructed
401 // first.
402 rtc::AsyncInvoker invoker_;
eladalonf1841382017-06-12 01:16:46 -0700403 };
404
405 // Wrapper for the receiver part, contains configs etc. that are needed to
406 // reconstruct the underlying VideoReceiveStream.
407 class WebRtcVideoReceiveStream
408 : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
409 public:
410 WebRtcVideoReceiveStream(
Jonas Oreland6d835922019-03-18 10:59:40 +0100411 WebRtcVideoChannel* channel,
eladalonf1841382017-06-12 01:16:46 -0700412 webrtc::Call* call,
413 const StreamParams& sp,
414 webrtc::VideoReceiveStream::Config config,
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200415 webrtc::VideoDecoderFactory* decoder_factory,
eladalonf1841382017-06-12 01:16:46 -0700416 bool default_stream,
417 const std::vector<VideoCodecSettings>& recv_codecs,
418 const webrtc::FlexfecReceiveStream::Config& flexfec_config);
419 ~WebRtcVideoReceiveStream();
420
421 const std::vector<uint32_t>& GetSsrcs() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200422
Jonas Oreland49ac5952018-09-26 16:04:32 +0200423 std::vector<webrtc::RtpSource> GetSources();
424
Florent Castelliabe301f2018-06-12 18:33:49 +0200425 // Does not return codecs, they are filled by the owning WebRtcVideoChannel.
426 webrtc::RtpParameters GetRtpParameters() const;
eladalonf1841382017-06-12 01:16:46 -0700427
428 void SetLocalSsrc(uint32_t local_ssrc);
429 // TODO(deadbeef): Move these feedback parameters into the recv parameters.
Elad Alonfadb1812019-05-24 13:40:02 +0200430 void SetFeedbackParameters(bool lntf_enabled,
431 bool nack_enabled,
eladalonf1841382017-06-12 01:16:46 -0700432 bool transport_cc_enabled,
433 webrtc::RtcpMode rtcp_mode);
434 void SetRecvParameters(const ChangedRecvParameters& recv_params);
435
436 void OnFrame(const webrtc::VideoFrame& frame) override;
437 bool IsDefaultStream() const;
438
Benjamin Wright192eeec2018-10-17 17:27:25 -0700439 void SetFrameDecryptor(
440 rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor);
441
Ruslan Burakov493a6502019-02-27 15:32:48 +0100442 bool SetBaseMinimumPlayoutDelayMs(int delay_ms);
443
444 int GetBaseMinimumPlayoutDelayMs() const;
445
eladalonf1841382017-06-12 01:16:46 -0700446 void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
447
448 VideoReceiverInfo GetVideoReceiverInfo(bool log_stats);
449
450 private:
eladalonf1841382017-06-12 01:16:46 -0700451 void RecreateWebRtcVideoStream();
452 void MaybeRecreateWebRtcFlexfecStream();
453
eladalonc0d481a2017-08-02 07:39:07 -0700454 void MaybeAssociateFlexfecWithVideo();
455 void MaybeDissociateFlexfecFromVideo();
456
Niels Möllercbcbc222018-09-28 09:07:24 +0200457 void ConfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
eladalonf1841382017-06-12 01:16:46 -0700458 void ConfigureFlexfecCodec(int flexfec_payload_type);
eladalonf1841382017-06-12 01:16:46 -0700459
460 std::string GetCodecNameFromPayloadType(int payload_type);
461
Jonas Oreland6d835922019-03-18 10:59:40 +0100462 WebRtcVideoChannel* const channel_;
eladalonf1841382017-06-12 01:16:46 -0700463 webrtc::Call* const call_;
Niels Möllercbcbc222018-09-28 09:07:24 +0200464 const StreamParams stream_params_;
eladalonf1841382017-06-12 01:16:46 -0700465
466 // Both |stream_| and |flexfec_stream_| are managed by |this|. They are
467 // destroyed by calling call_->DestroyVideoReceiveStream and
468 // call_->DestroyFlexfecReceiveStream, respectively.
469 webrtc::VideoReceiveStream* stream_;
470 const bool default_stream_;
471 webrtc::VideoReceiveStream::Config config_;
472 webrtc::FlexfecReceiveStream::Config flexfec_config_;
473 webrtc::FlexfecReceiveStream* flexfec_stream_;
474
Niels Möllercbcbc222018-09-28 09:07:24 +0200475 webrtc::VideoDecoderFactory* const decoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700476
477 rtc::CriticalSection sink_lock_;
danilchapa37de392017-09-09 04:17:22 -0700478 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_
479 RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700480 // Expands remote RTP timestamps to int64_t to be able to estimate how long
481 // the stream has been running.
482 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
danilchapa37de392017-09-09 04:17:22 -0700483 RTC_GUARDED_BY(sink_lock_);
484 int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700485 // Start NTP time is estimated as current remote NTP time (estimated from
486 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
danilchapa37de392017-09-09 04:17:22 -0700487 int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_);
Bjorn A Mellemda4f0932019-07-30 08:34:03 -0700488
489 const bool use_standard_bytes_stats_;
eladalonf1841382017-06-12 01:16:46 -0700490 };
491
492 void Construct(webrtc::Call* call, WebRtcVideoEngine* engine);
493
494 bool SendRtp(const uint8_t* data,
495 size_t len,
496 const webrtc::PacketOptions& options) override;
497 bool SendRtcp(const uint8_t* data, size_t len) override;
498
Steve Anton2d2bbb12019-08-07 09:57:59 -0700499 // Generate the list of codec parameters to pass down based on the negotiated
500 // "codecs". Note that VideoCodecSettings correspond to concrete codecs like
501 // VP8, VP9, H264 while VideoCodecs correspond also to "virtual" codecs like
502 // RTX, ULPFEC, FLEXFEC.
eladalonf1841382017-06-12 01:16:46 -0700503 static std::vector<VideoCodecSettings> MapCodecs(
504 const std::vector<VideoCodec>& codecs);
philipele8ed8302019-07-03 11:53:48 +0200505 // Get all codecs that are compatible with the receiver.
506 std::vector<VideoCodecSettings> SelectSendVideoCodecs(
Steve Antonef50b252019-03-01 15:15:38 -0800507 const std::vector<VideoCodecSettings>& remote_mapped_codecs) const
508 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700509
510 static bool NonFlexfecReceiveCodecsHaveChanged(
511 std::vector<VideoCodecSettings> before,
512 std::vector<VideoCodecSettings> after);
513
Steve Antonef50b252019-03-01 15:15:38 -0800514 void FillSenderStats(VideoMediaInfo* info, bool log_stats)
515 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
516 void FillReceiverStats(VideoMediaInfo* info, bool log_stats)
517 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700518 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
Steve Antonef50b252019-03-01 15:15:38 -0800519 VideoMediaInfo* info)
520 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
521 void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info)
522 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700523
philipele8ed8302019-07-03 11:53:48 +0200524 rtc::Thread* worker_thread_;
eladalonf1841382017-06-12 01:16:46 -0700525 rtc::ThreadChecker thread_checker_;
526
Steve Antonef50b252019-03-01 15:15:38 -0800527 uint32_t rtcp_receiver_report_ssrc_ RTC_GUARDED_BY(thread_checker_);
528 bool sending_ RTC_GUARDED_BY(thread_checker_);
529 webrtc::Call* const call_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700530
Steve Antonef50b252019-03-01 15:15:38 -0800531 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_
532 RTC_GUARDED_BY(thread_checker_);
533 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_
534 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700535
Ruslan Burakov493a6502019-02-27 15:32:48 +0100536 // Delay for unsignaled streams, which may be set before the stream exists.
Steve Antonef50b252019-03-01 15:15:38 -0800537 int default_recv_base_minimum_delay_ms_ RTC_GUARDED_BY(thread_checker_) = 0;
Ruslan Burakov493a6502019-02-27 15:32:48 +0100538
Steve Antonef50b252019-03-01 15:15:38 -0800539 const MediaConfig::Video video_config_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700540
eladalonf1841382017-06-12 01:16:46 -0700541 // Using primary-ssrc (first ssrc) as key.
542 std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
Steve Antonef50b252019-03-01 15:15:38 -0800543 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700544 std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
Steve Antonef50b252019-03-01 15:15:38 -0800545 RTC_GUARDED_BY(thread_checker_);
546 std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(thread_checker_);
547 std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700548
Steve Antonef50b252019-03-01 15:15:38 -0800549 absl::optional<VideoCodecSettings> send_codec_
550 RTC_GUARDED_BY(thread_checker_);
philipele8ed8302019-07-03 11:53:48 +0200551 std::vector<VideoCodecSettings> negotiated_codecs_
552 RTC_GUARDED_BY(thread_checker_);
553
Steve Antonef50b252019-03-01 15:15:38 -0800554 absl::optional<std::vector<webrtc::RtpExtension>> send_rtp_extensions_
555 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700556
Steve Antonef50b252019-03-01 15:15:38 -0800557 webrtc::VideoEncoderFactory* const encoder_factory_
558 RTC_GUARDED_BY(thread_checker_);
559 webrtc::VideoDecoderFactory* const decoder_factory_
560 RTC_GUARDED_BY(thread_checker_);
561 webrtc::VideoBitrateAllocatorFactory* const bitrate_allocator_factory_
562 RTC_GUARDED_BY(thread_checker_);
563 std::vector<VideoCodecSettings> recv_codecs_ RTC_GUARDED_BY(thread_checker_);
564 std::vector<webrtc::RtpExtension> recv_rtp_extensions_
565 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700566 // See reason for keeping track of the FlexFEC payload type separately in
567 // comment in WebRtcVideoChannel::ChangedRecvParameters.
Steve Antonef50b252019-03-01 15:15:38 -0800568 int recv_flexfec_payload_type_ RTC_GUARDED_BY(thread_checker_);
569 webrtc::BitrateConstraints bitrate_config_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700570 // TODO(deadbeef): Don't duplicate information between
571 // send_params/recv_params, rtp_extensions, options, etc.
Steve Antonef50b252019-03-01 15:15:38 -0800572 VideoSendParameters send_params_ RTC_GUARDED_BY(thread_checker_);
Steve Antonef50b252019-03-01 15:15:38 -0800573 VideoOptions default_send_options_ RTC_GUARDED_BY(thread_checker_);
574 VideoRecvParameters recv_params_ RTC_GUARDED_BY(thread_checker_);
575 int64_t last_stats_log_ms_ RTC_GUARDED_BY(thread_checker_);
576 const bool discard_unknown_ssrc_packets_ RTC_GUARDED_BY(thread_checker_);
Seth Hampson5897a6e2018-04-03 11:16:33 -0700577 // This is a stream param that comes from the remote description, but wasn't
578 // signaled with any a=ssrc lines. It holds information that was signaled
579 // before the unsignaled receive stream is created when the first packet is
580 // received.
Steve Antonef50b252019-03-01 15:15:38 -0800581 StreamParams unsignaled_stream_params_ RTC_GUARDED_BY(thread_checker_);
Benjamin Wright192eeec2018-10-17 17:27:25 -0700582 // Per peer connection crypto options that last for the lifetime of the peer
583 // connection.
Steve Antonef50b252019-03-01 15:15:38 -0800584 const webrtc::CryptoOptions crypto_options_ RTC_GUARDED_BY(thread_checker_);
Jonas Oreland6d835922019-03-18 10:59:40 +0100585
586 // Buffer for unhandled packets.
587 std::unique_ptr<UnhandledPacketsBuffer> unknown_ssrc_packet_buffer_
588 RTC_GUARDED_BY(thread_checker_);
philipele8ed8302019-07-03 11:53:48 +0200589
590 // In order for the |invoker_| to protect other members from being destructed
591 // as they are used in asynchronous tasks it has to be destructed first.
592 rtc::AsyncInvoker invoker_;
eladalonf1841382017-06-12 01:16:46 -0700593};
594
ilnik6b826ef2017-06-16 06:53:48 -0700595class EncoderStreamFactory
596 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
597 public:
598 EncoderStreamFactory(std::string codec_name,
599 int max_qp,
Seth Hampson1370e302018-02-07 08:50:36 -0800600 bool is_screenshare,
Florent Castelli66b38602019-07-10 16:57:57 +0200601 bool conference_mode);
ilnik6b826ef2017-06-16 06:53:48 -0700602
603 private:
604 std::vector<webrtc::VideoStream> CreateEncoderStreams(
605 int width,
606 int height,
607 const webrtc::VideoEncoderConfig& encoder_config) override;
608
609 const std::string codec_name_;
610 const int max_qp_;
Seth Hampson1370e302018-02-07 08:50:36 -0800611 const bool is_screenshare_;
612 // Allows a screenshare specific configuration, which enables temporal
Florent Castelli66b38602019-07-10 16:57:57 +0200613 // layering and various settings.
614 const bool conference_mode_;
ilnik6b826ef2017-06-16 06:53:48 -0700615};
616
eladalonf1841382017-06-12 01:16:46 -0700617} // namespace cricket
618
Steve Anton10542f22019-01-11 09:11:00 -0800619#endif // MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_