blob: 083fc9f41d128cb7db400d269bc3e532570f9be6 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MEDIA_ENGINE_WEBRTCVIDEOENGINE_H_
12#define MEDIA_ENGINE_WEBRTCVIDEOENGINE_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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/video/video_frame.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020023#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020024#include "api/video/video_source_interface.h"
25#include "api/video_codecs/sdp_video_format.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "call/call.h"
27#include "call/flexfec_receive_stream.h"
28#include "call/video_receive_stream.h"
29#include "call/video_send_stream.h"
30#include "media/base/mediaengine.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "media/engine/webrtcvideodecoderfactory.h"
32#include "media/engine/webrtcvideoencoderfactory.h"
33#include "rtc_base/asyncinvoker.h"
34#include "rtc_base/criticalsection.h"
35#include "rtc_base/networkroute.h"
36#include "rtc_base/thread_annotations.h"
37#include "rtc_base/thread_checker.h"
eladalonf1841382017-06-12 01:16:46 -070038
39namespace webrtc {
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020040class VideoDecoderFactory;
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020041class VideoEncoderFactory;
eladalonf1841382017-06-12 01:16:46 -070042struct MediaConfig;
Yves Gerey665174f2018-06-19 15:03:05 +020043} // namespace webrtc
eladalonf1841382017-06-12 01:16:46 -070044
45namespace rtc {
46class Thread;
47} // namespace rtc
48
49namespace cricket {
50
eladalonf1841382017-06-12 01:16:46 -070051class WebRtcVideoChannel;
eladalonf1841382017-06-12 01:16:46 -070052
eladalonf1841382017-06-12 01:16:46 -070053class UnsignalledSsrcHandler {
54 public:
55 enum Action {
56 kDropPacket,
57 kDeliverPacket,
58 };
59 virtual Action OnUnsignalledSsrc(WebRtcVideoChannel* channel,
60 uint32_t ssrc) = 0;
61 virtual ~UnsignalledSsrcHandler() = default;
62};
63
64// TODO(pbos): Remove, use external handlers only.
65class DefaultUnsignalledSsrcHandler : public UnsignalledSsrcHandler {
66 public:
67 DefaultUnsignalledSsrcHandler();
Yves Gerey665174f2018-06-19 15:03:05 +020068 Action OnUnsignalledSsrc(WebRtcVideoChannel* channel, uint32_t ssrc) override;
eladalonf1841382017-06-12 01:16:46 -070069
70 rtc::VideoSinkInterface<webrtc::VideoFrame>* GetDefaultSink() const;
71 void SetDefaultSink(WebRtcVideoChannel* channel,
72 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
73
74 virtual ~DefaultUnsignalledSsrcHandler() = default;
75
76 private:
77 rtc::VideoSinkInterface<webrtc::VideoFrame>* default_sink_;
78};
79
80// WebRtcVideoEngine is used for the new native WebRTC Video API (webrtc:1667).
81class WebRtcVideoEngine {
82 public:
Anders Carlssondd8c1652018-01-30 10:32:13 +010083#if defined(USE_BUILTIN_SW_CODECS)
Magnus Jedvert02e7a192017-09-23 17:21:32 +020084 // Internal SW video codecs will be added on top of the external codecs.
85 WebRtcVideoEngine(
86 std::unique_ptr<WebRtcVideoEncoderFactory> external_video_encoder_factory,
Qingsi Wang59844ce2018-11-01 04:45:53 +000087 std::unique_ptr<WebRtcVideoDecoderFactory>
88 external_video_decoder_factory);
Anders Carlssondd8c1652018-01-30 10:32:13 +010089#endif
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020090
91 // These video codec factories represents all video codecs, i.e. both software
92 // and external hardware codecs.
93 WebRtcVideoEngine(
94 std::unique_ptr<webrtc::VideoEncoderFactory> video_encoder_factory,
Qingsi Wang59844ce2018-11-01 04:45:53 +000095 std::unique_ptr<webrtc::VideoDecoderFactory> video_decoder_factory);
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020096
eladalonf1841382017-06-12 01:16:46 -070097 virtual ~WebRtcVideoEngine();
98
Benjamin Wrightbfb444c2018-10-15 10:20:24 -070099 WebRtcVideoChannel* CreateChannel(
100 webrtc::Call* call,
101 const MediaConfig& config,
102 const VideoOptions& options,
103 const webrtc::CryptoOptions& crypto_options);
eladalonf1841382017-06-12 01:16:46 -0700104
105 std::vector<VideoCodec> codecs() const;
106 RtpCapabilities GetCapabilities() const;
107
eladalonf1841382017-06-12 01:16:46 -0700108 private:
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200109 const std::unique_ptr<webrtc::VideoDecoderFactory> decoder_factory_;
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100110 const std::unique_ptr<webrtc::VideoEncoderFactory> encoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700111};
112
113class WebRtcVideoChannel : public VideoMediaChannel, public webrtc::Transport {
114 public:
Qingsi Wang59844ce2018-11-01 04:45:53 +0000115 WebRtcVideoChannel(webrtc::Call* call,
116 const MediaConfig& config,
117 const VideoOptions& options,
118 const webrtc::CryptoOptions& crypto_options,
119 webrtc::VideoEncoderFactory* encoder_factory,
120 webrtc::VideoDecoderFactory* decoder_factory);
eladalonf1841382017-06-12 01:16:46 -0700121 ~WebRtcVideoChannel() override;
122
123 // VideoMediaChannel implementation
124 rtc::DiffServCodePoint PreferredDscp() const override;
125
126 bool SetSendParameters(const VideoSendParameters& params) override;
127 bool SetRecvParameters(const VideoRecvParameters& params) override;
128 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800129 webrtc::RTCError SetRtpSendParameters(
130 uint32_t ssrc,
131 const webrtc::RtpParameters& parameters) override;
eladalonf1841382017-06-12 01:16:46 -0700132 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const override;
133 bool SetRtpReceiveParameters(
134 uint32_t ssrc,
135 const webrtc::RtpParameters& parameters) override;
136 bool GetSendCodec(VideoCodec* send_codec) override;
137 bool SetSend(bool send) override;
138 bool SetVideoSend(
139 uint32_t ssrc,
eladalonf1841382017-06-12 01:16:46 -0700140 const VideoOptions* options,
141 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) override;
142 bool AddSendStream(const StreamParams& sp) override;
143 bool RemoveSendStream(uint32_t ssrc) override;
144 bool AddRecvStream(const StreamParams& sp) override;
145 bool AddRecvStream(const StreamParams& sp, bool default_stream);
146 bool RemoveRecvStream(uint32_t ssrc) override;
147 bool SetSink(uint32_t ssrc,
148 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
149 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) override;
150 bool GetStats(VideoMediaInfo* info) override;
151
152 void OnPacketReceived(rtc::CopyOnWriteBuffer* packet,
153 const rtc::PacketTime& packet_time) override;
154 void OnRtcpReceived(rtc::CopyOnWriteBuffer* packet,
155 const rtc::PacketTime& packet_time) override;
156 void OnReadyToSend(bool ready) override;
157 void OnNetworkRouteChanged(const std::string& transport_name,
158 const rtc::NetworkRoute& network_route) override;
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700159 void SetInterface(NetworkInterface* iface,
160 webrtc::MediaTransportInterface* media_transport) override;
eladalonf1841382017-06-12 01:16:46 -0700161
Benjamin Wright192eeec2018-10-17 17:27:25 -0700162 // E2E Encrypted Video Frame API
163 // Set a frame decryptor to a particular ssrc that will intercept all
164 // incoming video frames and attempt to decrypt them before forwarding the
165 // result.
166 void SetFrameDecryptor(uint32_t ssrc,
167 rtc::scoped_refptr<webrtc::FrameDecryptorInterface>
168 frame_decryptor) override;
169 // Set a frame encryptor to a particular ssrc that will intercept all
170 // outgoing video frames and attempt to encrypt them and forward the result
171 // to the packetizer.
172 void SetFrameEncryptor(uint32_t ssrc,
173 rtc::scoped_refptr<webrtc::FrameEncryptorInterface>
174 frame_encryptor) override;
175
eladalonf1841382017-06-12 01:16:46 -0700176 // Implemented for VideoMediaChannelTest.
177 bool sending() const { return sending_; }
178
Danil Chapovalov00c71832018-06-15 15:58:38 +0200179 absl::optional<uint32_t> GetDefaultReceiveStreamSsrc();
eladalonf1841382017-06-12 01:16:46 -0700180
Seth Hampson5897a6e2018-04-03 11:16:33 -0700181 StreamParams unsignaled_stream_params() { return unsignaled_stream_params_; }
182
eladalonf1841382017-06-12 01:16:46 -0700183 // AdaptReason is used for expressing why a WebRtcVideoSendStream request
184 // a lower input frame size than the currently configured camera input frame
185 // size. There can be more than one reason OR:ed together.
186 enum AdaptReason {
187 ADAPTREASON_NONE = 0,
188 ADAPTREASON_CPU = 1,
189 ADAPTREASON_BANDWIDTH = 2,
190 };
191
sprang67561a62017-06-15 06:34:42 -0700192 static constexpr int kDefaultQpMax = 56;
193
Jonas Oreland49ac5952018-09-26 16:04:32 +0200194 std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const override;
195
eladalonf1841382017-06-12 01:16:46 -0700196 private:
197 class WebRtcVideoReceiveStream;
198 struct VideoCodecSettings {
199 VideoCodecSettings();
200
201 // Checks if all members of |*this| are equal to the corresponding members
202 // of |other|.
203 bool operator==(const VideoCodecSettings& other) const;
204 bool operator!=(const VideoCodecSettings& other) const;
205
206 // Checks if all members of |a|, except |flexfec_payload_type|, are equal
207 // to the corresponding members of |b|.
208 static bool EqualsDisregardingFlexfec(const VideoCodecSettings& a,
209 const VideoCodecSettings& b);
210
211 VideoCodec codec;
212 webrtc::UlpfecConfig ulpfec;
213 int flexfec_payload_type;
214 int rtx_payload_type;
215 };
216
217 struct ChangedSendParameters {
218 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200219 absl::optional<VideoCodecSettings> codec;
220 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
221 absl::optional<std::string> mid;
Johannes Kron9190b822018-10-29 11:22:05 +0100222 absl::optional<bool> extmap_allow_mixed;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200223 absl::optional<int> max_bandwidth_bps;
224 absl::optional<bool> conference_mode;
225 absl::optional<webrtc::RtcpMode> rtcp_mode;
eladalonf1841382017-06-12 01:16:46 -0700226 };
227
228 struct ChangedRecvParameters {
229 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200230 absl::optional<std::vector<VideoCodecSettings>> codec_settings;
231 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
eladalonf1841382017-06-12 01:16:46 -0700232 // Keep track of the FlexFEC payload type separately from |codec_settings|.
233 // This allows us to recreate the FlexfecReceiveStream separately from the
234 // VideoReceiveStream when the FlexFEC payload type is changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200235 absl::optional<int> flexfec_payload_type;
eladalonf1841382017-06-12 01:16:46 -0700236 };
237
238 bool GetChangedSendParameters(const VideoSendParameters& params,
239 ChangedSendParameters* changed_params) const;
240 bool GetChangedRecvParameters(const VideoRecvParameters& params,
241 ChangedRecvParameters* changed_params) const;
242
243 void SetMaxSendBandwidth(int bps);
244
245 void ConfigureReceiverRtp(
246 webrtc::VideoReceiveStream::Config* config,
247 webrtc::FlexfecReceiveStream::Config* flexfec_config,
248 const StreamParams& sp) const;
249 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
danilchapa37de392017-09-09 04:17:22 -0700250 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700251 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
danilchapa37de392017-09-09 04:17:22 -0700252 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700253 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
danilchapa37de392017-09-09 04:17:22 -0700254 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700255
256 static std::string CodecSettingsVectorToString(
257 const std::vector<VideoCodecSettings>& codecs);
258
259 // Wrapper for the sender part.
260 class WebRtcVideoSendStream
261 : public rtc::VideoSourceInterface<webrtc::VideoFrame> {
262 public:
263 WebRtcVideoSendStream(
264 webrtc::Call* call,
265 const StreamParams& sp,
266 webrtc::VideoSendStream::Config config,
267 const VideoOptions& options,
eladalonf1841382017-06-12 01:16:46 -0700268 bool enable_cpu_overuse_detection,
269 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200270 const absl::optional<VideoCodecSettings>& codec_settings,
271 const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions,
eladalonf1841382017-06-12 01:16:46 -0700272 const VideoSendParameters& send_params);
273 virtual ~WebRtcVideoSendStream();
274
275 void SetSendParameters(const ChangedSendParameters& send_params);
Zach Steinba37b4b2018-01-23 15:02:36 -0800276 webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters);
eladalonf1841382017-06-12 01:16:46 -0700277 webrtc::RtpParameters GetRtpParameters() const;
278
Benjamin Wright192eeec2018-10-17 17:27:25 -0700279 void SetFrameEncryptor(
280 rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor);
281
eladalonf1841382017-06-12 01:16:46 -0700282 // Implements rtc::VideoSourceInterface<webrtc::VideoFrame>.
283 // WebRtcVideoSendStream acts as a source to the webrtc::VideoSendStream
284 // in |stream_|. This is done to proxy VideoSinkWants from the encoder to
285 // the worker thread.
286 void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
287 const rtc::VideoSinkWants& wants) override;
288 void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
289
Niels Möllerff40b142018-04-09 08:49:14 +0200290 bool SetVideoSend(const VideoOptions* options,
eladalonf1841382017-06-12 01:16:46 -0700291 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
292
293 void SetSend(bool send);
294
295 const std::vector<uint32_t>& GetSsrcs() const;
296 VideoSenderInfo GetVideoSenderInfo(bool log_stats);
297 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
298
299 private:
300 // Parameters needed to reconstruct the underlying stream.
301 // webrtc::VideoSendStream doesn't support setting a lot of options on the
302 // fly, so when those need to be changed we tear down and reconstruct with
303 // similar parameters depending on which options changed etc.
304 struct VideoSendStreamParameters {
305 VideoSendStreamParameters(
306 webrtc::VideoSendStream::Config config,
307 const VideoOptions& options,
308 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200309 const absl::optional<VideoCodecSettings>& codec_settings);
eladalonf1841382017-06-12 01:16:46 -0700310 webrtc::VideoSendStream::Config config;
311 VideoOptions options;
312 int max_bitrate_bps;
313 bool conference_mode;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200314 absl::optional<VideoCodecSettings> codec_settings;
eladalonf1841382017-06-12 01:16:46 -0700315 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
316 // typically changes when setting a new resolution or reconfiguring
317 // bitrates.
318 webrtc::VideoEncoderConfig encoder_config;
319 };
320
eladalonf1841382017-06-12 01:16:46 -0700321 rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
322 ConfigureVideoEncoderSettings(const VideoCodec& codec);
Niels Möller5bf8ccd2018-03-15 14:16:11 +0100323 void SetCodec(const VideoCodecSettings& codec);
eladalonf1841382017-06-12 01:16:46 -0700324 void RecreateWebRtcStream();
325 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
326 const VideoCodec& codec) const;
327 void ReconfigureEncoder();
eladalonf1841382017-06-12 01:16:46 -0700328
329 // Calls Start or Stop according to whether or not |sending_| is true,
330 // and whether or not the encoding in |rtp_parameters_| is active.
331 void UpdateSendState();
332
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700333 webrtc::DegradationPreference GetDegradationPreference() const
334 RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700335
336 rtc::ThreadChecker thread_checker_;
337 rtc::AsyncInvoker invoker_;
338 rtc::Thread* worker_thread_;
Niels Möller1e062892018-02-07 10:18:32 +0100339 const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_);
340 const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700341 webrtc::Call* const call_;
342 const bool enable_cpu_overuse_detection_;
343 rtc::VideoSourceInterface<webrtc::VideoFrame>* source_
Niels Möller1e062892018-02-07 10:18:32 +0100344 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700345
Niels Möller1e062892018-02-07 10:18:32 +0100346 webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700347 rtc::VideoSinkInterface<webrtc::VideoFrame>* encoder_sink_
Niels Möller1e062892018-02-07 10:18:32 +0100348 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700349 // Contains settings that are the same for all streams in the MediaChannel,
350 // such as codecs, header extensions, and the global bitrate limit for the
351 // entire channel.
Niels Möller1e062892018-02-07 10:18:32 +0100352 VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700353 // Contains settings that are unique for each stream, such as max_bitrate.
354 // Does *not* contain codecs, however.
355 // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_.
356 // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only
357 // one stream per MediaChannel.
Niels Möller1e062892018-02-07 10:18:32 +0100358 webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700359
Niels Möller1e062892018-02-07 10:18:32 +0100360 bool sending_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700361 };
362
363 // Wrapper for the receiver part, contains configs etc. that are needed to
364 // reconstruct the underlying VideoReceiveStream.
365 class WebRtcVideoReceiveStream
366 : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
367 public:
368 WebRtcVideoReceiveStream(
369 webrtc::Call* call,
370 const StreamParams& sp,
371 webrtc::VideoReceiveStream::Config config,
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200372 webrtc::VideoDecoderFactory* decoder_factory,
eladalonf1841382017-06-12 01:16:46 -0700373 bool default_stream,
374 const std::vector<VideoCodecSettings>& recv_codecs,
375 const webrtc::FlexfecReceiveStream::Config& flexfec_config);
376 ~WebRtcVideoReceiveStream();
377
378 const std::vector<uint32_t>& GetSsrcs() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200379
Jonas Oreland49ac5952018-09-26 16:04:32 +0200380 std::vector<webrtc::RtpSource> GetSources();
381
Florent Castelliabe301f2018-06-12 18:33:49 +0200382 // Does not return codecs, they are filled by the owning WebRtcVideoChannel.
383 webrtc::RtpParameters GetRtpParameters() const;
eladalonf1841382017-06-12 01:16:46 -0700384
385 void SetLocalSsrc(uint32_t local_ssrc);
386 // TODO(deadbeef): Move these feedback parameters into the recv parameters.
387 void SetFeedbackParameters(bool nack_enabled,
388 bool remb_enabled,
389 bool transport_cc_enabled,
390 webrtc::RtcpMode rtcp_mode);
391 void SetRecvParameters(const ChangedRecvParameters& recv_params);
392
393 void OnFrame(const webrtc::VideoFrame& frame) override;
394 bool IsDefaultStream() const;
395
Benjamin Wright192eeec2018-10-17 17:27:25 -0700396 void SetFrameDecryptor(
397 rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor);
398
eladalonf1841382017-06-12 01:16:46 -0700399 void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
400
401 VideoReceiverInfo GetVideoReceiverInfo(bool log_stats);
402
403 private:
eladalonf1841382017-06-12 01:16:46 -0700404 void RecreateWebRtcVideoStream();
405 void MaybeRecreateWebRtcFlexfecStream();
406
eladalonc0d481a2017-08-02 07:39:07 -0700407 void MaybeAssociateFlexfecWithVideo();
408 void MaybeDissociateFlexfecFromVideo();
409
Niels Möllercbcbc222018-09-28 09:07:24 +0200410 void ConfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
eladalonf1841382017-06-12 01:16:46 -0700411 void ConfigureFlexfecCodec(int flexfec_payload_type);
eladalonf1841382017-06-12 01:16:46 -0700412
413 std::string GetCodecNameFromPayloadType(int payload_type);
414
Danil Chapovalov00c71832018-06-15 15:58:38 +0200415 absl::optional<uint32_t> GetFirstPrimarySsrc() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200416
eladalonf1841382017-06-12 01:16:46 -0700417 webrtc::Call* const call_;
Niels Möllercbcbc222018-09-28 09:07:24 +0200418 const StreamParams stream_params_;
eladalonf1841382017-06-12 01:16:46 -0700419
420 // Both |stream_| and |flexfec_stream_| are managed by |this|. They are
421 // destroyed by calling call_->DestroyVideoReceiveStream and
422 // call_->DestroyFlexfecReceiveStream, respectively.
423 webrtc::VideoReceiveStream* stream_;
424 const bool default_stream_;
425 webrtc::VideoReceiveStream::Config config_;
426 webrtc::FlexfecReceiveStream::Config flexfec_config_;
427 webrtc::FlexfecReceiveStream* flexfec_stream_;
428
Niels Möllercbcbc222018-09-28 09:07:24 +0200429 webrtc::VideoDecoderFactory* const decoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700430
431 rtc::CriticalSection sink_lock_;
danilchapa37de392017-09-09 04:17:22 -0700432 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_
433 RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700434 // Expands remote RTP timestamps to int64_t to be able to estimate how long
435 // the stream has been running.
436 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
danilchapa37de392017-09-09 04:17:22 -0700437 RTC_GUARDED_BY(sink_lock_);
438 int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700439 // Start NTP time is estimated as current remote NTP time (estimated from
440 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
danilchapa37de392017-09-09 04:17:22 -0700441 int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700442 };
443
444 void Construct(webrtc::Call* call, WebRtcVideoEngine* engine);
445
446 bool SendRtp(const uint8_t* data,
447 size_t len,
448 const webrtc::PacketOptions& options) override;
449 bool SendRtcp(const uint8_t* data, size_t len) override;
450
451 static std::vector<VideoCodecSettings> MapCodecs(
452 const std::vector<VideoCodec>& codecs);
453 // Select what video codec will be used for sending, i.e. what codec is used
454 // for local encoding, based on supported remote codecs. The first remote
455 // codec that is supported locally will be selected.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200456 absl::optional<VideoCodecSettings> SelectSendVideoCodec(
eladalonf1841382017-06-12 01:16:46 -0700457 const std::vector<VideoCodecSettings>& remote_mapped_codecs) const;
458
459 static bool NonFlexfecReceiveCodecsHaveChanged(
460 std::vector<VideoCodecSettings> before,
461 std::vector<VideoCodecSettings> after);
462
463 void FillSenderStats(VideoMediaInfo* info, bool log_stats);
464 void FillReceiverStats(VideoMediaInfo* info, bool log_stats);
465 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
466 VideoMediaInfo* info);
467 void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info);
468
469 rtc::ThreadChecker thread_checker_;
470
471 uint32_t rtcp_receiver_report_ssrc_;
472 bool sending_;
473 webrtc::Call* const call_;
474
475 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
476 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
477
478 const MediaConfig::Video video_config_;
479
480 rtc::CriticalSection stream_crit_;
481 // Using primary-ssrc (first ssrc) as key.
482 std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
danilchapa37de392017-09-09 04:17:22 -0700483 RTC_GUARDED_BY(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700484 std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700485 RTC_GUARDED_BY(stream_crit_);
486 std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(stream_crit_);
487 std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700488
Danil Chapovalov00c71832018-06-15 15:58:38 +0200489 absl::optional<VideoCodecSettings> send_codec_;
490 absl::optional<std::vector<webrtc::RtpExtension>> send_rtp_extensions_;
eladalonf1841382017-06-12 01:16:46 -0700491
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100492 webrtc::VideoEncoderFactory* const encoder_factory_;
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200493 webrtc::VideoDecoderFactory* const decoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700494 std::vector<VideoCodecSettings> recv_codecs_;
495 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
496 // See reason for keeping track of the FlexFEC payload type separately in
497 // comment in WebRtcVideoChannel::ChangedRecvParameters.
498 int recv_flexfec_payload_type_;
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +0100499 webrtc::BitrateConstraints bitrate_config_;
eladalonf1841382017-06-12 01:16:46 -0700500 // TODO(deadbeef): Don't duplicate information between
501 // send_params/recv_params, rtp_extensions, options, etc.
502 VideoSendParameters send_params_;
Tim Haloun648d28a2018-10-18 16:52:22 -0700503 rtc::DiffServCodePoint preferred_dscp_;
eladalonf1841382017-06-12 01:16:46 -0700504 VideoOptions default_send_options_;
505 VideoRecvParameters recv_params_;
506 int64_t last_stats_log_ms_;
Åsa Persson2c7149b2018-10-15 09:36:10 +0200507 const bool discard_unknown_ssrc_packets_;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700508 // This is a stream param that comes from the remote description, but wasn't
509 // signaled with any a=ssrc lines. It holds information that was signaled
510 // before the unsignaled receive stream is created when the first packet is
511 // received.
512 StreamParams unsignaled_stream_params_;
Benjamin Wright192eeec2018-10-17 17:27:25 -0700513 // Per peer connection crypto options that last for the lifetime of the peer
514 // connection.
515 const webrtc::CryptoOptions crypto_options_;
eladalonf1841382017-06-12 01:16:46 -0700516};
517
ilnik6b826ef2017-06-16 06:53:48 -0700518class EncoderStreamFactory
519 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
520 public:
521 EncoderStreamFactory(std::string codec_name,
522 int max_qp,
Seth Hampson1370e302018-02-07 08:50:36 -0800523 bool is_screenshare,
524 bool screenshare_config_explicitly_enabled);
ilnik6b826ef2017-06-16 06:53:48 -0700525
526 private:
527 std::vector<webrtc::VideoStream> CreateEncoderStreams(
528 int width,
529 int height,
530 const webrtc::VideoEncoderConfig& encoder_config) override;
531
532 const std::string codec_name_;
533 const int max_qp_;
Seth Hampson1370e302018-02-07 08:50:36 -0800534 const bool is_screenshare_;
535 // Allows a screenshare specific configuration, which enables temporal
536 // layering and allows simulcast.
537 const bool screenshare_config_explicitly_enabled_;
ilnik6b826ef2017-06-16 06:53:48 -0700538};
539
eladalonf1841382017-06-12 01:16:46 -0700540} // namespace cricket
541
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200542#endif // MEDIA_ENGINE_WEBRTCVIDEOENGINE_H_