blob: b989e22f88475a100c7194490fc00c3801af99aa [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;
Saurav Dasff27da52019-09-20 11:05:30 -0700159 void ResetUnsignaledRecvStream() override;
eladalonf1841382017-06-12 01:16:46 -0700160 bool SetSink(uint32_t ssrc,
161 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
162 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) override;
163 bool GetStats(VideoMediaInfo* info) override;
164
Amit Hilbuche7a5f7b2019-03-12 11:10:27 -0700165 void OnPacketReceived(rtc::CopyOnWriteBuffer packet,
Niels Möllere6933812018-11-05 13:01:41 +0100166 int64_t packet_time_us) override;
eladalonf1841382017-06-12 01:16:46 -0700167 void OnReadyToSend(bool ready) override;
168 void OnNetworkRouteChanged(const std::string& transport_name,
169 const rtc::NetworkRoute& network_route) override;
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700170 void SetInterface(
171 NetworkInterface* iface,
172 const webrtc::MediaTransportConfig& media_transport_config) override;
eladalonf1841382017-06-12 01:16:46 -0700173
Benjamin Wright192eeec2018-10-17 17:27:25 -0700174 // E2E Encrypted Video Frame API
175 // Set a frame decryptor to a particular ssrc that will intercept all
176 // incoming video frames and attempt to decrypt them before forwarding the
177 // result.
178 void SetFrameDecryptor(uint32_t ssrc,
179 rtc::scoped_refptr<webrtc::FrameDecryptorInterface>
180 frame_decryptor) override;
181 // Set a frame encryptor to a particular ssrc that will intercept all
182 // outgoing video frames and attempt to encrypt them and forward the result
183 // to the packetizer.
184 void SetFrameEncryptor(uint32_t ssrc,
185 rtc::scoped_refptr<webrtc::FrameEncryptorInterface>
186 frame_encryptor) override;
187
Ruslan Burakov493a6502019-02-27 15:32:48 +0100188 bool SetBaseMinimumPlayoutDelayMs(uint32_t ssrc, int delay_ms) override;
189
190 absl::optional<int> GetBaseMinimumPlayoutDelayMs(
191 uint32_t ssrc) const override;
192
eladalonf1841382017-06-12 01:16:46 -0700193 // Implemented for VideoMediaChannelTest.
Steve Antonef50b252019-03-01 15:15:38 -0800194 bool sending() const {
195 RTC_DCHECK_RUN_ON(&thread_checker_);
196 return sending_;
197 }
eladalonf1841382017-06-12 01:16:46 -0700198
Danil Chapovalov00c71832018-06-15 15:58:38 +0200199 absl::optional<uint32_t> GetDefaultReceiveStreamSsrc();
eladalonf1841382017-06-12 01:16:46 -0700200
Steve Antonef50b252019-03-01 15:15:38 -0800201 StreamParams unsignaled_stream_params() {
202 RTC_DCHECK_RUN_ON(&thread_checker_);
203 return unsignaled_stream_params_;
204 }
Seth Hampson5897a6e2018-04-03 11:16:33 -0700205
eladalonf1841382017-06-12 01:16:46 -0700206 // AdaptReason is used for expressing why a WebRtcVideoSendStream request
207 // a lower input frame size than the currently configured camera input frame
208 // size. There can be more than one reason OR:ed together.
209 enum AdaptReason {
210 ADAPTREASON_NONE = 0,
211 ADAPTREASON_CPU = 1,
212 ADAPTREASON_BANDWIDTH = 2,
213 };
214
sprang67561a62017-06-15 06:34:42 -0700215 static constexpr int kDefaultQpMax = 56;
216
Jonas Oreland49ac5952018-09-26 16:04:32 +0200217 std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const override;
218
Jonas Oreland6d835922019-03-18 10:59:40 +0100219 // Take the buffered packets for |ssrcs| and feed them into DeliverPacket.
220 // This method does nothing unless unknown_ssrc_packet_buffer_ is configured.
221 void BackfillBufferedPackets(rtc::ArrayView<const uint32_t> ssrcs);
222
philipeld9cc8c02019-09-16 14:53:40 +0200223 // Implements webrtc::EncoderSwitchRequestCallback.
224 void RequestEncoderFallback() override;
225 void RequestEncoderSwitch(
226 const EncoderSwitchRequestCallback::Config& conf) override;
philipele8ed8302019-07-03 11:53:48 +0200227
eladalonf1841382017-06-12 01:16:46 -0700228 private:
229 class WebRtcVideoReceiveStream;
230 struct VideoCodecSettings {
231 VideoCodecSettings();
232
233 // Checks if all members of |*this| are equal to the corresponding members
234 // of |other|.
235 bool operator==(const VideoCodecSettings& other) const;
236 bool operator!=(const VideoCodecSettings& other) const;
237
238 // Checks if all members of |a|, except |flexfec_payload_type|, are equal
239 // to the corresponding members of |b|.
240 static bool EqualsDisregardingFlexfec(const VideoCodecSettings& a,
241 const VideoCodecSettings& b);
242
243 VideoCodec codec;
244 webrtc::UlpfecConfig ulpfec;
Steve Anton2d2bbb12019-08-07 09:57:59 -0700245 int flexfec_payload_type; // -1 if absent.
246 int rtx_payload_type; // -1 if absent.
eladalonf1841382017-06-12 01:16:46 -0700247 };
248
249 struct ChangedSendParameters {
250 // These optionals are unset if not changed.
philipele8ed8302019-07-03 11:53:48 +0200251 absl::optional<VideoCodecSettings> send_codec;
252 absl::optional<std::vector<VideoCodecSettings>> negotiated_codecs;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200253 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
254 absl::optional<std::string> mid;
Johannes Kron9190b822018-10-29 11:22:05 +0100255 absl::optional<bool> extmap_allow_mixed;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200256 absl::optional<int> max_bandwidth_bps;
257 absl::optional<bool> conference_mode;
258 absl::optional<webrtc::RtcpMode> rtcp_mode;
eladalonf1841382017-06-12 01:16:46 -0700259 };
260
261 struct ChangedRecvParameters {
262 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200263 absl::optional<std::vector<VideoCodecSettings>> codec_settings;
264 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
eladalonf1841382017-06-12 01:16:46 -0700265 // Keep track of the FlexFEC payload type separately from |codec_settings|.
266 // This allows us to recreate the FlexfecReceiveStream separately from the
267 // VideoReceiveStream when the FlexFEC payload type is changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200268 absl::optional<int> flexfec_payload_type;
eladalonf1841382017-06-12 01:16:46 -0700269 };
270
271 bool GetChangedSendParameters(const VideoSendParameters& params,
Steve Antonef50b252019-03-01 15:15:38 -0800272 ChangedSendParameters* changed_params) const
273 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
philipele8ed8302019-07-03 11:53:48 +0200274 bool ApplyChangedParams(const ChangedSendParameters& changed_params);
eladalonf1841382017-06-12 01:16:46 -0700275 bool GetChangedRecvParameters(const VideoRecvParameters& params,
Steve Antonef50b252019-03-01 15:15:38 -0800276 ChangedRecvParameters* changed_params) const
277 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700278
eladalonf1841382017-06-12 01:16:46 -0700279 void ConfigureReceiverRtp(
280 webrtc::VideoReceiveStream::Config* config,
281 webrtc::FlexfecReceiveStream::Config* flexfec_config,
Steve Antonef50b252019-03-01 15:15:38 -0800282 const StreamParams& sp) const
283 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700284 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
Steve Antonef50b252019-03-01 15:15:38 -0800285 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700286 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
Steve Antonef50b252019-03-01 15:15:38 -0800287 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700288 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
Steve Antonef50b252019-03-01 15:15:38 -0800289 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700290
291 static std::string CodecSettingsVectorToString(
292 const std::vector<VideoCodecSettings>& codecs);
293
294 // Wrapper for the sender part.
Christian Fremerey6c025412019-02-13 19:43:28 +0000295 class WebRtcVideoSendStream
296 : public rtc::VideoSourceInterface<webrtc::VideoFrame> {
eladalonf1841382017-06-12 01:16:46 -0700297 public:
298 WebRtcVideoSendStream(
299 webrtc::Call* call,
300 const StreamParams& sp,
301 webrtc::VideoSendStream::Config config,
302 const VideoOptions& options,
eladalonf1841382017-06-12 01:16:46 -0700303 bool enable_cpu_overuse_detection,
304 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200305 const absl::optional<VideoCodecSettings>& codec_settings,
306 const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions,
eladalonf1841382017-06-12 01:16:46 -0700307 const VideoSendParameters& send_params);
308 virtual ~WebRtcVideoSendStream();
309
310 void SetSendParameters(const ChangedSendParameters& send_params);
Zach Steinba37b4b2018-01-23 15:02:36 -0800311 webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters);
eladalonf1841382017-06-12 01:16:46 -0700312 webrtc::RtpParameters GetRtpParameters() const;
313
Benjamin Wright192eeec2018-10-17 17:27:25 -0700314 void SetFrameEncryptor(
315 rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor);
316
Christian Fremerey6c025412019-02-13 19:43:28 +0000317 // Implements rtc::VideoSourceInterface<webrtc::VideoFrame>.
318 // WebRtcVideoSendStream acts as a source to the webrtc::VideoSendStream
319 // in |stream_|. This is done to proxy VideoSinkWants from the encoder to
320 // the worker thread.
321 void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
322 const rtc::VideoSinkWants& wants) override;
323 void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
324
Niels Möllerff40b142018-04-09 08:49:14 +0200325 bool SetVideoSend(const VideoOptions* options,
eladalonf1841382017-06-12 01:16:46 -0700326 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
327
328 void SetSend(bool send);
329
330 const std::vector<uint32_t>& GetSsrcs() const;
331 VideoSenderInfo GetVideoSenderInfo(bool log_stats);
332 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
333
334 private:
335 // Parameters needed to reconstruct the underlying stream.
336 // webrtc::VideoSendStream doesn't support setting a lot of options on the
337 // fly, so when those need to be changed we tear down and reconstruct with
338 // similar parameters depending on which options changed etc.
339 struct VideoSendStreamParameters {
340 VideoSendStreamParameters(
341 webrtc::VideoSendStream::Config config,
342 const VideoOptions& options,
343 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200344 const absl::optional<VideoCodecSettings>& codec_settings);
eladalonf1841382017-06-12 01:16:46 -0700345 webrtc::VideoSendStream::Config config;
346 VideoOptions options;
347 int max_bitrate_bps;
348 bool conference_mode;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200349 absl::optional<VideoCodecSettings> codec_settings;
eladalonf1841382017-06-12 01:16:46 -0700350 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
351 // typically changes when setting a new resolution or reconfiguring
352 // bitrates.
353 webrtc::VideoEncoderConfig encoder_config;
354 };
355
eladalonf1841382017-06-12 01:16:46 -0700356 rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
357 ConfigureVideoEncoderSettings(const VideoCodec& codec);
Niels Möller5bf8ccd2018-03-15 14:16:11 +0100358 void SetCodec(const VideoCodecSettings& codec);
eladalonf1841382017-06-12 01:16:46 -0700359 void RecreateWebRtcStream();
360 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
361 const VideoCodec& codec) const;
362 void ReconfigureEncoder();
eladalonf1841382017-06-12 01:16:46 -0700363
364 // Calls Start or Stop according to whether or not |sending_| is true,
365 // and whether or not the encoding in |rtp_parameters_| is active.
366 void UpdateSendState();
367
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700368 webrtc::DegradationPreference GetDegradationPreference() const
369 RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700370
371 rtc::ThreadChecker thread_checker_;
eladalonf1841382017-06-12 01:16:46 -0700372 rtc::Thread* worker_thread_;
Niels Möller1e062892018-02-07 10:18:32 +0100373 const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_);
374 const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700375 webrtc::Call* const call_;
376 const bool enable_cpu_overuse_detection_;
377 rtc::VideoSourceInterface<webrtc::VideoFrame>* source_
Niels Möller1e062892018-02-07 10:18:32 +0100378 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700379
Niels Möller1e062892018-02-07 10:18:32 +0100380 webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_);
Christian Fremerey6c025412019-02-13 19:43:28 +0000381 rtc::VideoSinkInterface<webrtc::VideoFrame>* encoder_sink_
382 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700383 // Contains settings that are the same for all streams in the MediaChannel,
384 // such as codecs, header extensions, and the global bitrate limit for the
385 // entire channel.
Niels Möller1e062892018-02-07 10:18:32 +0100386 VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700387 // Contains settings that are unique for each stream, such as max_bitrate.
388 // Does *not* contain codecs, however.
389 // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_.
390 // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only
391 // one stream per MediaChannel.
Niels Möller1e062892018-02-07 10:18:32 +0100392 webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700393
Niels Möller1e062892018-02-07 10:18:32 +0100394 bool sending_ RTC_GUARDED_BY(&thread_checker_);
philipel98cbb222019-06-14 11:28:51 +0200395
Bjorn A Mellemda4f0932019-07-30 08:34:03 -0700396 const bool use_standard_bytes_stats_;
397
philipel98cbb222019-06-14 11:28:51 +0200398 // In order for the |invoker_| to protect other members from being
399 // destructed as they are used in asynchronous tasks it has to be destructed
400 // first.
401 rtc::AsyncInvoker invoker_;
eladalonf1841382017-06-12 01:16:46 -0700402 };
403
404 // Wrapper for the receiver part, contains configs etc. that are needed to
405 // reconstruct the underlying VideoReceiveStream.
406 class WebRtcVideoReceiveStream
407 : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
408 public:
409 WebRtcVideoReceiveStream(
Jonas Oreland6d835922019-03-18 10:59:40 +0100410 WebRtcVideoChannel* channel,
eladalonf1841382017-06-12 01:16:46 -0700411 webrtc::Call* call,
412 const StreamParams& sp,
413 webrtc::VideoReceiveStream::Config config,
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200414 webrtc::VideoDecoderFactory* decoder_factory,
eladalonf1841382017-06-12 01:16:46 -0700415 bool default_stream,
416 const std::vector<VideoCodecSettings>& recv_codecs,
417 const webrtc::FlexfecReceiveStream::Config& flexfec_config);
418 ~WebRtcVideoReceiveStream();
419
420 const std::vector<uint32_t>& GetSsrcs() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200421
Jonas Oreland49ac5952018-09-26 16:04:32 +0200422 std::vector<webrtc::RtpSource> GetSources();
423
Florent Castelliabe301f2018-06-12 18:33:49 +0200424 // Does not return codecs, they are filled by the owning WebRtcVideoChannel.
425 webrtc::RtpParameters GetRtpParameters() const;
eladalonf1841382017-06-12 01:16:46 -0700426
427 void SetLocalSsrc(uint32_t local_ssrc);
428 // TODO(deadbeef): Move these feedback parameters into the recv parameters.
Elad Alonfadb1812019-05-24 13:40:02 +0200429 void SetFeedbackParameters(bool lntf_enabled,
430 bool nack_enabled,
eladalonf1841382017-06-12 01:16:46 -0700431 bool transport_cc_enabled,
432 webrtc::RtcpMode rtcp_mode);
433 void SetRecvParameters(const ChangedRecvParameters& recv_params);
434
435 void OnFrame(const webrtc::VideoFrame& frame) override;
436 bool IsDefaultStream() const;
437
Benjamin Wright192eeec2018-10-17 17:27:25 -0700438 void SetFrameDecryptor(
439 rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor);
440
Ruslan Burakov493a6502019-02-27 15:32:48 +0100441 bool SetBaseMinimumPlayoutDelayMs(int delay_ms);
442
443 int GetBaseMinimumPlayoutDelayMs() const;
444
eladalonf1841382017-06-12 01:16:46 -0700445 void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
446
447 VideoReceiverInfo GetVideoReceiverInfo(bool log_stats);
448
449 private:
eladalonf1841382017-06-12 01:16:46 -0700450 void RecreateWebRtcVideoStream();
451 void MaybeRecreateWebRtcFlexfecStream();
452
eladalonc0d481a2017-08-02 07:39:07 -0700453 void MaybeAssociateFlexfecWithVideo();
454 void MaybeDissociateFlexfecFromVideo();
455
Niels Möllercbcbc222018-09-28 09:07:24 +0200456 void ConfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
eladalonf1841382017-06-12 01:16:46 -0700457 void ConfigureFlexfecCodec(int flexfec_payload_type);
eladalonf1841382017-06-12 01:16:46 -0700458
459 std::string GetCodecNameFromPayloadType(int payload_type);
460
Jonas Oreland6d835922019-03-18 10:59:40 +0100461 WebRtcVideoChannel* const channel_;
eladalonf1841382017-06-12 01:16:46 -0700462 webrtc::Call* const call_;
Niels Möllercbcbc222018-09-28 09:07:24 +0200463 const StreamParams stream_params_;
eladalonf1841382017-06-12 01:16:46 -0700464
465 // Both |stream_| and |flexfec_stream_| are managed by |this|. They are
466 // destroyed by calling call_->DestroyVideoReceiveStream and
467 // call_->DestroyFlexfecReceiveStream, respectively.
468 webrtc::VideoReceiveStream* stream_;
469 const bool default_stream_;
470 webrtc::VideoReceiveStream::Config config_;
471 webrtc::FlexfecReceiveStream::Config flexfec_config_;
472 webrtc::FlexfecReceiveStream* flexfec_stream_;
473
Niels Möllercbcbc222018-09-28 09:07:24 +0200474 webrtc::VideoDecoderFactory* const decoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700475
476 rtc::CriticalSection sink_lock_;
danilchapa37de392017-09-09 04:17:22 -0700477 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_
478 RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700479 // Expands remote RTP timestamps to int64_t to be able to estimate how long
480 // the stream has been running.
481 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
danilchapa37de392017-09-09 04:17:22 -0700482 RTC_GUARDED_BY(sink_lock_);
483 int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700484 // Start NTP time is estimated as current remote NTP time (estimated from
485 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
danilchapa37de392017-09-09 04:17:22 -0700486 int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_);
Bjorn A Mellemda4f0932019-07-30 08:34:03 -0700487
488 const bool use_standard_bytes_stats_;
eladalonf1841382017-06-12 01:16:46 -0700489 };
490
491 void Construct(webrtc::Call* call, WebRtcVideoEngine* engine);
492
493 bool SendRtp(const uint8_t* data,
494 size_t len,
495 const webrtc::PacketOptions& options) override;
496 bool SendRtcp(const uint8_t* data, size_t len) override;
497
Steve Anton2d2bbb12019-08-07 09:57:59 -0700498 // Generate the list of codec parameters to pass down based on the negotiated
499 // "codecs". Note that VideoCodecSettings correspond to concrete codecs like
500 // VP8, VP9, H264 while VideoCodecs correspond also to "virtual" codecs like
501 // RTX, ULPFEC, FLEXFEC.
eladalonf1841382017-06-12 01:16:46 -0700502 static std::vector<VideoCodecSettings> MapCodecs(
503 const std::vector<VideoCodec>& codecs);
philipele8ed8302019-07-03 11:53:48 +0200504 // Get all codecs that are compatible with the receiver.
505 std::vector<VideoCodecSettings> SelectSendVideoCodecs(
Steve Antonef50b252019-03-01 15:15:38 -0800506 const std::vector<VideoCodecSettings>& remote_mapped_codecs) const
507 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700508
509 static bool NonFlexfecReceiveCodecsHaveChanged(
510 std::vector<VideoCodecSettings> before,
511 std::vector<VideoCodecSettings> after);
512
Steve Antonef50b252019-03-01 15:15:38 -0800513 void FillSenderStats(VideoMediaInfo* info, bool log_stats)
514 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
515 void FillReceiverStats(VideoMediaInfo* info, bool log_stats)
516 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700517 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
Steve Antonef50b252019-03-01 15:15:38 -0800518 VideoMediaInfo* info)
519 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
520 void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info)
521 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700522
philipele8ed8302019-07-03 11:53:48 +0200523 rtc::Thread* worker_thread_;
eladalonf1841382017-06-12 01:16:46 -0700524 rtc::ThreadChecker thread_checker_;
525
Steve Antonef50b252019-03-01 15:15:38 -0800526 uint32_t rtcp_receiver_report_ssrc_ RTC_GUARDED_BY(thread_checker_);
527 bool sending_ RTC_GUARDED_BY(thread_checker_);
528 webrtc::Call* const call_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700529
Steve Antonef50b252019-03-01 15:15:38 -0800530 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_
531 RTC_GUARDED_BY(thread_checker_);
532 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_
533 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700534
Ruslan Burakov493a6502019-02-27 15:32:48 +0100535 // Delay for unsignaled streams, which may be set before the stream exists.
Steve Antonef50b252019-03-01 15:15:38 -0800536 int default_recv_base_minimum_delay_ms_ RTC_GUARDED_BY(thread_checker_) = 0;
Ruslan Burakov493a6502019-02-27 15:32:48 +0100537
Steve Antonef50b252019-03-01 15:15:38 -0800538 const MediaConfig::Video video_config_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700539
eladalonf1841382017-06-12 01:16:46 -0700540 // Using primary-ssrc (first ssrc) as key.
541 std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
Steve Antonef50b252019-03-01 15:15:38 -0800542 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700543 std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
Steve Antonef50b252019-03-01 15:15:38 -0800544 RTC_GUARDED_BY(thread_checker_);
545 std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(thread_checker_);
546 std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700547
Steve Antonef50b252019-03-01 15:15:38 -0800548 absl::optional<VideoCodecSettings> send_codec_
549 RTC_GUARDED_BY(thread_checker_);
philipele8ed8302019-07-03 11:53:48 +0200550 std::vector<VideoCodecSettings> negotiated_codecs_
551 RTC_GUARDED_BY(thread_checker_);
552
Steve Antonef50b252019-03-01 15:15:38 -0800553 absl::optional<std::vector<webrtc::RtpExtension>> send_rtp_extensions_
554 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700555
Steve Antonef50b252019-03-01 15:15:38 -0800556 webrtc::VideoEncoderFactory* const encoder_factory_
557 RTC_GUARDED_BY(thread_checker_);
558 webrtc::VideoDecoderFactory* const decoder_factory_
559 RTC_GUARDED_BY(thread_checker_);
560 webrtc::VideoBitrateAllocatorFactory* const bitrate_allocator_factory_
561 RTC_GUARDED_BY(thread_checker_);
562 std::vector<VideoCodecSettings> recv_codecs_ RTC_GUARDED_BY(thread_checker_);
563 std::vector<webrtc::RtpExtension> recv_rtp_extensions_
564 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700565 // See reason for keeping track of the FlexFEC payload type separately in
566 // comment in WebRtcVideoChannel::ChangedRecvParameters.
Steve Antonef50b252019-03-01 15:15:38 -0800567 int recv_flexfec_payload_type_ RTC_GUARDED_BY(thread_checker_);
568 webrtc::BitrateConstraints bitrate_config_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700569 // TODO(deadbeef): Don't duplicate information between
570 // send_params/recv_params, rtp_extensions, options, etc.
Steve Antonef50b252019-03-01 15:15:38 -0800571 VideoSendParameters send_params_ RTC_GUARDED_BY(thread_checker_);
Steve Antonef50b252019-03-01 15:15:38 -0800572 VideoOptions default_send_options_ RTC_GUARDED_BY(thread_checker_);
573 VideoRecvParameters recv_params_ RTC_GUARDED_BY(thread_checker_);
574 int64_t last_stats_log_ms_ RTC_GUARDED_BY(thread_checker_);
575 const bool discard_unknown_ssrc_packets_ RTC_GUARDED_BY(thread_checker_);
Seth Hampson5897a6e2018-04-03 11:16:33 -0700576 // This is a stream param that comes from the remote description, but wasn't
577 // signaled with any a=ssrc lines. It holds information that was signaled
578 // before the unsignaled receive stream is created when the first packet is
579 // received.
Steve Antonef50b252019-03-01 15:15:38 -0800580 StreamParams unsignaled_stream_params_ RTC_GUARDED_BY(thread_checker_);
Benjamin Wright192eeec2018-10-17 17:27:25 -0700581 // Per peer connection crypto options that last for the lifetime of the peer
582 // connection.
Steve Antonef50b252019-03-01 15:15:38 -0800583 const webrtc::CryptoOptions crypto_options_ RTC_GUARDED_BY(thread_checker_);
Jonas Oreland6d835922019-03-18 10:59:40 +0100584
585 // Buffer for unhandled packets.
586 std::unique_ptr<UnhandledPacketsBuffer> unknown_ssrc_packet_buffer_
587 RTC_GUARDED_BY(thread_checker_);
philipele8ed8302019-07-03 11:53:48 +0200588
589 // In order for the |invoker_| to protect other members from being destructed
590 // as they are used in asynchronous tasks it has to be destructed first.
591 rtc::AsyncInvoker invoker_;
eladalonf1841382017-06-12 01:16:46 -0700592};
593
ilnik6b826ef2017-06-16 06:53:48 -0700594class EncoderStreamFactory
595 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
596 public:
597 EncoderStreamFactory(std::string codec_name,
598 int max_qp,
Seth Hampson1370e302018-02-07 08:50:36 -0800599 bool is_screenshare,
Florent Castelli66b38602019-07-10 16:57:57 +0200600 bool conference_mode);
ilnik6b826ef2017-06-16 06:53:48 -0700601
602 private:
603 std::vector<webrtc::VideoStream> CreateEncoderStreams(
604 int width,
605 int height,
606 const webrtc::VideoEncoderConfig& encoder_config) override;
607
608 const std::string codec_name_;
609 const int max_qp_;
Seth Hampson1370e302018-02-07 08:50:36 -0800610 const bool is_screenshare_;
611 // Allows a screenshare specific configuration, which enables temporal
Florent Castelli66b38602019-07-10 16:57:57 +0200612 // layering and various settings.
613 const bool conference_mode_;
ilnik6b826ef2017-06-16 06:53:48 -0700614};
615
eladalonf1841382017-06-12 01:16:46 -0700616} // namespace cricket
617
Steve Anton10542f22019-01-11 09:11:00 -0800618#endif // MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_