blob: 88956e9283a3b6319f0608138ce61e03bfd62254 [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;
eladalonf1841382017-06-12 01:16:46 -0700166 void OnReadyToSend(bool ready) override;
167 void OnNetworkRouteChanged(const std::string& transport_name,
168 const rtc::NetworkRoute& network_route) override;
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700169 void SetInterface(
170 NetworkInterface* iface,
171 const webrtc::MediaTransportConfig& media_transport_config) override;
eladalonf1841382017-06-12 01:16:46 -0700172
Benjamin Wright192eeec2018-10-17 17:27:25 -0700173 // E2E Encrypted Video Frame API
174 // Set a frame decryptor to a particular ssrc that will intercept all
175 // incoming video frames and attempt to decrypt them before forwarding the
176 // result.
177 void SetFrameDecryptor(uint32_t ssrc,
178 rtc::scoped_refptr<webrtc::FrameDecryptorInterface>
179 frame_decryptor) override;
180 // Set a frame encryptor to a particular ssrc that will intercept all
181 // outgoing video frames and attempt to encrypt them and forward the result
182 // to the packetizer.
183 void SetFrameEncryptor(uint32_t ssrc,
184 rtc::scoped_refptr<webrtc::FrameEncryptorInterface>
185 frame_encryptor) override;
186
Ruslan Burakov493a6502019-02-27 15:32:48 +0100187 bool SetBaseMinimumPlayoutDelayMs(uint32_t ssrc, int delay_ms) override;
188
189 absl::optional<int> GetBaseMinimumPlayoutDelayMs(
190 uint32_t ssrc) const override;
191
eladalonf1841382017-06-12 01:16:46 -0700192 // Implemented for VideoMediaChannelTest.
Steve Antonef50b252019-03-01 15:15:38 -0800193 bool sending() const {
194 RTC_DCHECK_RUN_ON(&thread_checker_);
195 return sending_;
196 }
eladalonf1841382017-06-12 01:16:46 -0700197
Danil Chapovalov00c71832018-06-15 15:58:38 +0200198 absl::optional<uint32_t> GetDefaultReceiveStreamSsrc();
eladalonf1841382017-06-12 01:16:46 -0700199
Steve Antonef50b252019-03-01 15:15:38 -0800200 StreamParams unsignaled_stream_params() {
201 RTC_DCHECK_RUN_ON(&thread_checker_);
202 return unsignaled_stream_params_;
203 }
Seth Hampson5897a6e2018-04-03 11:16:33 -0700204
eladalonf1841382017-06-12 01:16:46 -0700205 // AdaptReason is used for expressing why a WebRtcVideoSendStream request
206 // a lower input frame size than the currently configured camera input frame
207 // size. There can be more than one reason OR:ed together.
208 enum AdaptReason {
209 ADAPTREASON_NONE = 0,
210 ADAPTREASON_CPU = 1,
211 ADAPTREASON_BANDWIDTH = 2,
212 };
213
sprang67561a62017-06-15 06:34:42 -0700214 static constexpr int kDefaultQpMax = 56;
215
Jonas Oreland49ac5952018-09-26 16:04:32 +0200216 std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const override;
217
Jonas Oreland6d835922019-03-18 10:59:40 +0100218 // Take the buffered packets for |ssrcs| and feed them into DeliverPacket.
219 // This method does nothing unless unknown_ssrc_packet_buffer_ is configured.
220 void BackfillBufferedPackets(rtc::ArrayView<const uint32_t> ssrcs);
221
philipeld9cc8c02019-09-16 14:53:40 +0200222 // Implements webrtc::EncoderSwitchRequestCallback.
223 void RequestEncoderFallback() override;
224 void RequestEncoderSwitch(
225 const EncoderSwitchRequestCallback::Config& conf) override;
philipele8ed8302019-07-03 11:53:48 +0200226
eladalonf1841382017-06-12 01:16:46 -0700227 private:
228 class WebRtcVideoReceiveStream;
229 struct VideoCodecSettings {
230 VideoCodecSettings();
231
232 // Checks if all members of |*this| are equal to the corresponding members
233 // of |other|.
234 bool operator==(const VideoCodecSettings& other) const;
235 bool operator!=(const VideoCodecSettings& other) const;
236
237 // Checks if all members of |a|, except |flexfec_payload_type|, are equal
238 // to the corresponding members of |b|.
239 static bool EqualsDisregardingFlexfec(const VideoCodecSettings& a,
240 const VideoCodecSettings& b);
241
242 VideoCodec codec;
243 webrtc::UlpfecConfig ulpfec;
Steve Anton2d2bbb12019-08-07 09:57:59 -0700244 int flexfec_payload_type; // -1 if absent.
245 int rtx_payload_type; // -1 if absent.
eladalonf1841382017-06-12 01:16:46 -0700246 };
247
248 struct ChangedSendParameters {
249 // These optionals are unset if not changed.
philipele8ed8302019-07-03 11:53:48 +0200250 absl::optional<VideoCodecSettings> send_codec;
251 absl::optional<std::vector<VideoCodecSettings>> negotiated_codecs;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200252 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
253 absl::optional<std::string> mid;
Johannes Kron9190b822018-10-29 11:22:05 +0100254 absl::optional<bool> extmap_allow_mixed;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200255 absl::optional<int> max_bandwidth_bps;
256 absl::optional<bool> conference_mode;
257 absl::optional<webrtc::RtcpMode> rtcp_mode;
eladalonf1841382017-06-12 01:16:46 -0700258 };
259
260 struct ChangedRecvParameters {
261 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200262 absl::optional<std::vector<VideoCodecSettings>> codec_settings;
263 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
eladalonf1841382017-06-12 01:16:46 -0700264 // Keep track of the FlexFEC payload type separately from |codec_settings|.
265 // This allows us to recreate the FlexfecReceiveStream separately from the
266 // VideoReceiveStream when the FlexFEC payload type is changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200267 absl::optional<int> flexfec_payload_type;
eladalonf1841382017-06-12 01:16:46 -0700268 };
269
270 bool GetChangedSendParameters(const VideoSendParameters& params,
Steve Antonef50b252019-03-01 15:15:38 -0800271 ChangedSendParameters* changed_params) const
272 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
philipele8ed8302019-07-03 11:53:48 +0200273 bool ApplyChangedParams(const ChangedSendParameters& changed_params);
eladalonf1841382017-06-12 01:16:46 -0700274 bool GetChangedRecvParameters(const VideoRecvParameters& params,
Steve Antonef50b252019-03-01 15:15:38 -0800275 ChangedRecvParameters* changed_params) const
276 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700277
eladalonf1841382017-06-12 01:16:46 -0700278 void ConfigureReceiverRtp(
279 webrtc::VideoReceiveStream::Config* config,
280 webrtc::FlexfecReceiveStream::Config* flexfec_config,
Steve Antonef50b252019-03-01 15:15:38 -0800281 const StreamParams& sp) const
282 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700283 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
Steve Antonef50b252019-03-01 15:15:38 -0800284 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700285 bool ValidateReceiveSsrcAvailability(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 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
Steve Antonef50b252019-03-01 15:15:38 -0800288 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700289
290 static std::string CodecSettingsVectorToString(
291 const std::vector<VideoCodecSettings>& codecs);
292
293 // Wrapper for the sender part.
Christian Fremerey6c025412019-02-13 19:43:28 +0000294 class WebRtcVideoSendStream
295 : public rtc::VideoSourceInterface<webrtc::VideoFrame> {
eladalonf1841382017-06-12 01:16:46 -0700296 public:
297 WebRtcVideoSendStream(
298 webrtc::Call* call,
299 const StreamParams& sp,
300 webrtc::VideoSendStream::Config config,
301 const VideoOptions& options,
eladalonf1841382017-06-12 01:16:46 -0700302 bool enable_cpu_overuse_detection,
303 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200304 const absl::optional<VideoCodecSettings>& codec_settings,
305 const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions,
eladalonf1841382017-06-12 01:16:46 -0700306 const VideoSendParameters& send_params);
307 virtual ~WebRtcVideoSendStream();
308
309 void SetSendParameters(const ChangedSendParameters& send_params);
Zach Steinba37b4b2018-01-23 15:02:36 -0800310 webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters);
eladalonf1841382017-06-12 01:16:46 -0700311 webrtc::RtpParameters GetRtpParameters() const;
312
Benjamin Wright192eeec2018-10-17 17:27:25 -0700313 void SetFrameEncryptor(
314 rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor);
315
Christian Fremerey6c025412019-02-13 19:43:28 +0000316 // Implements rtc::VideoSourceInterface<webrtc::VideoFrame>.
317 // WebRtcVideoSendStream acts as a source to the webrtc::VideoSendStream
318 // in |stream_|. This is done to proxy VideoSinkWants from the encoder to
319 // the worker thread.
320 void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
321 const rtc::VideoSinkWants& wants) override;
322 void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
323
Niels Möllerff40b142018-04-09 08:49:14 +0200324 bool SetVideoSend(const VideoOptions* options,
eladalonf1841382017-06-12 01:16:46 -0700325 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
326
327 void SetSend(bool send);
328
329 const std::vector<uint32_t>& GetSsrcs() const;
330 VideoSenderInfo GetVideoSenderInfo(bool log_stats);
331 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
332
333 private:
334 // Parameters needed to reconstruct the underlying stream.
335 // webrtc::VideoSendStream doesn't support setting a lot of options on the
336 // fly, so when those need to be changed we tear down and reconstruct with
337 // similar parameters depending on which options changed etc.
338 struct VideoSendStreamParameters {
339 VideoSendStreamParameters(
340 webrtc::VideoSendStream::Config config,
341 const VideoOptions& options,
342 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200343 const absl::optional<VideoCodecSettings>& codec_settings);
eladalonf1841382017-06-12 01:16:46 -0700344 webrtc::VideoSendStream::Config config;
345 VideoOptions options;
346 int max_bitrate_bps;
347 bool conference_mode;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200348 absl::optional<VideoCodecSettings> codec_settings;
eladalonf1841382017-06-12 01:16:46 -0700349 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
350 // typically changes when setting a new resolution or reconfiguring
351 // bitrates.
352 webrtc::VideoEncoderConfig encoder_config;
353 };
354
eladalonf1841382017-06-12 01:16:46 -0700355 rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
356 ConfigureVideoEncoderSettings(const VideoCodec& codec);
Niels Möller5bf8ccd2018-03-15 14:16:11 +0100357 void SetCodec(const VideoCodecSettings& codec);
eladalonf1841382017-06-12 01:16:46 -0700358 void RecreateWebRtcStream();
359 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
360 const VideoCodec& codec) const;
361 void ReconfigureEncoder();
eladalonf1841382017-06-12 01:16:46 -0700362
363 // Calls Start or Stop according to whether or not |sending_| is true,
364 // and whether or not the encoding in |rtp_parameters_| is active.
365 void UpdateSendState();
366
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700367 webrtc::DegradationPreference GetDegradationPreference() const
368 RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700369
370 rtc::ThreadChecker thread_checker_;
eladalonf1841382017-06-12 01:16:46 -0700371 rtc::Thread* worker_thread_;
Niels Möller1e062892018-02-07 10:18:32 +0100372 const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_);
373 const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700374 webrtc::Call* const call_;
375 const bool enable_cpu_overuse_detection_;
376 rtc::VideoSourceInterface<webrtc::VideoFrame>* source_
Niels Möller1e062892018-02-07 10:18:32 +0100377 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700378
Niels Möller1e062892018-02-07 10:18:32 +0100379 webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_);
Christian Fremerey6c025412019-02-13 19:43:28 +0000380 rtc::VideoSinkInterface<webrtc::VideoFrame>* encoder_sink_
381 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700382 // Contains settings that are the same for all streams in the MediaChannel,
383 // such as codecs, header extensions, and the global bitrate limit for the
384 // entire channel.
Niels Möller1e062892018-02-07 10:18:32 +0100385 VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700386 // Contains settings that are unique for each stream, such as max_bitrate.
387 // Does *not* contain codecs, however.
388 // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_.
389 // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only
390 // one stream per MediaChannel.
Niels Möller1e062892018-02-07 10:18:32 +0100391 webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700392
Niels Möller1e062892018-02-07 10:18:32 +0100393 bool sending_ RTC_GUARDED_BY(&thread_checker_);
philipel98cbb222019-06-14 11:28:51 +0200394
Bjorn A Mellemda4f0932019-07-30 08:34:03 -0700395 const bool use_standard_bytes_stats_;
396
philipel98cbb222019-06-14 11:28:51 +0200397 // In order for the |invoker_| to protect other members from being
398 // destructed as they are used in asynchronous tasks it has to be destructed
399 // first.
400 rtc::AsyncInvoker invoker_;
eladalonf1841382017-06-12 01:16:46 -0700401 };
402
403 // Wrapper for the receiver part, contains configs etc. that are needed to
404 // reconstruct the underlying VideoReceiveStream.
405 class WebRtcVideoReceiveStream
406 : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
407 public:
408 WebRtcVideoReceiveStream(
Jonas Oreland6d835922019-03-18 10:59:40 +0100409 WebRtcVideoChannel* channel,
eladalonf1841382017-06-12 01:16:46 -0700410 webrtc::Call* call,
411 const StreamParams& sp,
412 webrtc::VideoReceiveStream::Config config,
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200413 webrtc::VideoDecoderFactory* decoder_factory,
eladalonf1841382017-06-12 01:16:46 -0700414 bool default_stream,
415 const std::vector<VideoCodecSettings>& recv_codecs,
416 const webrtc::FlexfecReceiveStream::Config& flexfec_config);
417 ~WebRtcVideoReceiveStream();
418
419 const std::vector<uint32_t>& GetSsrcs() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200420
Jonas Oreland49ac5952018-09-26 16:04:32 +0200421 std::vector<webrtc::RtpSource> GetSources();
422
Florent Castelliabe301f2018-06-12 18:33:49 +0200423 // Does not return codecs, they are filled by the owning WebRtcVideoChannel.
424 webrtc::RtpParameters GetRtpParameters() const;
eladalonf1841382017-06-12 01:16:46 -0700425
426 void SetLocalSsrc(uint32_t local_ssrc);
427 // TODO(deadbeef): Move these feedback parameters into the recv parameters.
Elad Alonfadb1812019-05-24 13:40:02 +0200428 void SetFeedbackParameters(bool lntf_enabled,
429 bool nack_enabled,
eladalonf1841382017-06-12 01:16:46 -0700430 bool transport_cc_enabled,
431 webrtc::RtcpMode rtcp_mode);
432 void SetRecvParameters(const ChangedRecvParameters& recv_params);
433
434 void OnFrame(const webrtc::VideoFrame& frame) override;
435 bool IsDefaultStream() const;
436
Benjamin Wright192eeec2018-10-17 17:27:25 -0700437 void SetFrameDecryptor(
438 rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor);
439
Ruslan Burakov493a6502019-02-27 15:32:48 +0100440 bool SetBaseMinimumPlayoutDelayMs(int delay_ms);
441
442 int GetBaseMinimumPlayoutDelayMs() const;
443
eladalonf1841382017-06-12 01:16:46 -0700444 void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
445
446 VideoReceiverInfo GetVideoReceiverInfo(bool log_stats);
447
448 private:
eladalonf1841382017-06-12 01:16:46 -0700449 void RecreateWebRtcVideoStream();
450 void MaybeRecreateWebRtcFlexfecStream();
451
eladalonc0d481a2017-08-02 07:39:07 -0700452 void MaybeAssociateFlexfecWithVideo();
453 void MaybeDissociateFlexfecFromVideo();
454
Niels Möllercbcbc222018-09-28 09:07:24 +0200455 void ConfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
eladalonf1841382017-06-12 01:16:46 -0700456 void ConfigureFlexfecCodec(int flexfec_payload_type);
eladalonf1841382017-06-12 01:16:46 -0700457
458 std::string GetCodecNameFromPayloadType(int payload_type);
459
Jonas Oreland6d835922019-03-18 10:59:40 +0100460 WebRtcVideoChannel* const channel_;
eladalonf1841382017-06-12 01:16:46 -0700461 webrtc::Call* const call_;
Niels Möllercbcbc222018-09-28 09:07:24 +0200462 const StreamParams stream_params_;
eladalonf1841382017-06-12 01:16:46 -0700463
464 // Both |stream_| and |flexfec_stream_| are managed by |this|. They are
465 // destroyed by calling call_->DestroyVideoReceiveStream and
466 // call_->DestroyFlexfecReceiveStream, respectively.
467 webrtc::VideoReceiveStream* stream_;
468 const bool default_stream_;
469 webrtc::VideoReceiveStream::Config config_;
470 webrtc::FlexfecReceiveStream::Config flexfec_config_;
471 webrtc::FlexfecReceiveStream* flexfec_stream_;
472
Niels Möllercbcbc222018-09-28 09:07:24 +0200473 webrtc::VideoDecoderFactory* const decoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700474
475 rtc::CriticalSection sink_lock_;
danilchapa37de392017-09-09 04:17:22 -0700476 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_
477 RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700478 // Expands remote RTP timestamps to int64_t to be able to estimate how long
479 // the stream has been running.
480 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
danilchapa37de392017-09-09 04:17:22 -0700481 RTC_GUARDED_BY(sink_lock_);
482 int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700483 // Start NTP time is estimated as current remote NTP time (estimated from
484 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
danilchapa37de392017-09-09 04:17:22 -0700485 int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_);
Bjorn A Mellemda4f0932019-07-30 08:34:03 -0700486
487 const bool use_standard_bytes_stats_;
eladalonf1841382017-06-12 01:16:46 -0700488 };
489
490 void Construct(webrtc::Call* call, WebRtcVideoEngine* engine);
491
492 bool SendRtp(const uint8_t* data,
493 size_t len,
494 const webrtc::PacketOptions& options) override;
495 bool SendRtcp(const uint8_t* data, size_t len) override;
496
Steve Anton2d2bbb12019-08-07 09:57:59 -0700497 // Generate the list of codec parameters to pass down based on the negotiated
498 // "codecs". Note that VideoCodecSettings correspond to concrete codecs like
499 // VP8, VP9, H264 while VideoCodecs correspond also to "virtual" codecs like
500 // RTX, ULPFEC, FLEXFEC.
eladalonf1841382017-06-12 01:16:46 -0700501 static std::vector<VideoCodecSettings> MapCodecs(
502 const std::vector<VideoCodec>& codecs);
philipele8ed8302019-07-03 11:53:48 +0200503 // Get all codecs that are compatible with the receiver.
504 std::vector<VideoCodecSettings> SelectSendVideoCodecs(
Steve Antonef50b252019-03-01 15:15:38 -0800505 const std::vector<VideoCodecSettings>& remote_mapped_codecs) const
506 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700507
508 static bool NonFlexfecReceiveCodecsHaveChanged(
509 std::vector<VideoCodecSettings> before,
510 std::vector<VideoCodecSettings> after);
511
Steve Antonef50b252019-03-01 15:15:38 -0800512 void FillSenderStats(VideoMediaInfo* info, bool log_stats)
513 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
514 void FillReceiverStats(VideoMediaInfo* info, bool log_stats)
515 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700516 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
Steve Antonef50b252019-03-01 15:15:38 -0800517 VideoMediaInfo* info)
518 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
519 void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info)
520 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700521
philipele8ed8302019-07-03 11:53:48 +0200522 rtc::Thread* worker_thread_;
eladalonf1841382017-06-12 01:16:46 -0700523 rtc::ThreadChecker thread_checker_;
524
Steve Antonef50b252019-03-01 15:15:38 -0800525 uint32_t rtcp_receiver_report_ssrc_ RTC_GUARDED_BY(thread_checker_);
526 bool sending_ RTC_GUARDED_BY(thread_checker_);
527 webrtc::Call* const call_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700528
Steve Antonef50b252019-03-01 15:15:38 -0800529 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_
530 RTC_GUARDED_BY(thread_checker_);
531 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_
532 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700533
Ruslan Burakov493a6502019-02-27 15:32:48 +0100534 // Delay for unsignaled streams, which may be set before the stream exists.
Steve Antonef50b252019-03-01 15:15:38 -0800535 int default_recv_base_minimum_delay_ms_ RTC_GUARDED_BY(thread_checker_) = 0;
Ruslan Burakov493a6502019-02-27 15:32:48 +0100536
Steve Antonef50b252019-03-01 15:15:38 -0800537 const MediaConfig::Video video_config_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700538
eladalonf1841382017-06-12 01:16:46 -0700539 // Using primary-ssrc (first ssrc) as key.
540 std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
Steve Antonef50b252019-03-01 15:15:38 -0800541 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700542 std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
Steve Antonef50b252019-03-01 15:15:38 -0800543 RTC_GUARDED_BY(thread_checker_);
544 std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(thread_checker_);
545 std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700546
Steve Antonef50b252019-03-01 15:15:38 -0800547 absl::optional<VideoCodecSettings> send_codec_
548 RTC_GUARDED_BY(thread_checker_);
philipele8ed8302019-07-03 11:53:48 +0200549 std::vector<VideoCodecSettings> negotiated_codecs_
550 RTC_GUARDED_BY(thread_checker_);
551
Steve Antonef50b252019-03-01 15:15:38 -0800552 absl::optional<std::vector<webrtc::RtpExtension>> send_rtp_extensions_
553 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700554
Steve Antonef50b252019-03-01 15:15:38 -0800555 webrtc::VideoEncoderFactory* const encoder_factory_
556 RTC_GUARDED_BY(thread_checker_);
557 webrtc::VideoDecoderFactory* const decoder_factory_
558 RTC_GUARDED_BY(thread_checker_);
559 webrtc::VideoBitrateAllocatorFactory* const bitrate_allocator_factory_
560 RTC_GUARDED_BY(thread_checker_);
561 std::vector<VideoCodecSettings> recv_codecs_ RTC_GUARDED_BY(thread_checker_);
562 std::vector<webrtc::RtpExtension> recv_rtp_extensions_
563 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700564 // See reason for keeping track of the FlexFEC payload type separately in
565 // comment in WebRtcVideoChannel::ChangedRecvParameters.
Steve Antonef50b252019-03-01 15:15:38 -0800566 int recv_flexfec_payload_type_ RTC_GUARDED_BY(thread_checker_);
567 webrtc::BitrateConstraints bitrate_config_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700568 // TODO(deadbeef): Don't duplicate information between
569 // send_params/recv_params, rtp_extensions, options, etc.
Steve Antonef50b252019-03-01 15:15:38 -0800570 VideoSendParameters send_params_ RTC_GUARDED_BY(thread_checker_);
Steve Antonef50b252019-03-01 15:15:38 -0800571 VideoOptions default_send_options_ RTC_GUARDED_BY(thread_checker_);
572 VideoRecvParameters recv_params_ RTC_GUARDED_BY(thread_checker_);
573 int64_t last_stats_log_ms_ RTC_GUARDED_BY(thread_checker_);
574 const bool discard_unknown_ssrc_packets_ RTC_GUARDED_BY(thread_checker_);
Seth Hampson5897a6e2018-04-03 11:16:33 -0700575 // This is a stream param that comes from the remote description, but wasn't
576 // signaled with any a=ssrc lines. It holds information that was signaled
577 // before the unsignaled receive stream is created when the first packet is
578 // received.
Steve Antonef50b252019-03-01 15:15:38 -0800579 StreamParams unsignaled_stream_params_ RTC_GUARDED_BY(thread_checker_);
Benjamin Wright192eeec2018-10-17 17:27:25 -0700580 // Per peer connection crypto options that last for the lifetime of the peer
581 // connection.
Steve Antonef50b252019-03-01 15:15:38 -0800582 const webrtc::CryptoOptions crypto_options_ RTC_GUARDED_BY(thread_checker_);
Jonas Oreland6d835922019-03-18 10:59:40 +0100583
584 // Buffer for unhandled packets.
585 std::unique_ptr<UnhandledPacketsBuffer> unknown_ssrc_packet_buffer_
586 RTC_GUARDED_BY(thread_checker_);
philipele8ed8302019-07-03 11:53:48 +0200587
588 // In order for the |invoker_| to protect other members from being destructed
589 // as they are used in asynchronous tasks it has to be destructed first.
590 rtc::AsyncInvoker invoker_;
eladalonf1841382017-06-12 01:16:46 -0700591};
592
ilnik6b826ef2017-06-16 06:53:48 -0700593class EncoderStreamFactory
594 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
595 public:
596 EncoderStreamFactory(std::string codec_name,
597 int max_qp,
Seth Hampson1370e302018-02-07 08:50:36 -0800598 bool is_screenshare,
Florent Castelli66b38602019-07-10 16:57:57 +0200599 bool conference_mode);
ilnik6b826ef2017-06-16 06:53:48 -0700600
601 private:
602 std::vector<webrtc::VideoStream> CreateEncoderStreams(
603 int width,
604 int height,
605 const webrtc::VideoEncoderConfig& encoder_config) override;
606
607 const std::string codec_name_;
608 const int max_qp_;
Seth Hampson1370e302018-02-07 08:50:36 -0800609 const bool is_screenshare_;
610 // Allows a screenshare specific configuration, which enables temporal
Florent Castelli66b38602019-07-10 16:57:57 +0200611 // layering and various settings.
612 const bool conference_mode_;
ilnik6b826ef2017-06-16 06:53:48 -0700613};
614
eladalonf1841382017-06-12 01:16:46 -0700615} // namespace cricket
616
Steve Anton10542f22019-01-11 09:11:00 -0800617#endif // MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_