blob: a93453d97b7324211337e93d81b17dff853a094a [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"
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"
31#include "media/base/mediaengine.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "media/engine/webrtcvideodecoderfactory.h"
33#include "media/engine/webrtcvideoencoderfactory.h"
34#include "rtc_base/asyncinvoker.h"
35#include "rtc_base/criticalsection.h"
36#include "rtc_base/networkroute.h"
37#include "rtc_base/thread_annotations.h"
38#include "rtc_base/thread_checker.h"
eladalonf1841382017-06-12 01:16:46 -070039
40namespace webrtc {
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020041class VideoDecoderFactory;
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020042class VideoEncoderFactory;
eladalonf1841382017-06-12 01:16:46 -070043struct MediaConfig;
Yves Gerey665174f2018-06-19 15:03:05 +020044} // namespace webrtc
eladalonf1841382017-06-12 01:16:46 -070045
46namespace rtc {
47class Thread;
48} // namespace rtc
49
50namespace cricket {
51
eladalonf1841382017-06-12 01:16:46 -070052class WebRtcVideoChannel;
eladalonf1841382017-06-12 01:16:46 -070053
eladalonf1841382017-06-12 01:16:46 -070054class UnsignalledSsrcHandler {
55 public:
56 enum Action {
57 kDropPacket,
58 kDeliverPacket,
59 };
60 virtual Action OnUnsignalledSsrc(WebRtcVideoChannel* channel,
61 uint32_t ssrc) = 0;
62 virtual ~UnsignalledSsrcHandler() = default;
63};
64
65// TODO(pbos): Remove, use external handlers only.
66class DefaultUnsignalledSsrcHandler : public UnsignalledSsrcHandler {
67 public:
68 DefaultUnsignalledSsrcHandler();
Yves Gerey665174f2018-06-19 15:03:05 +020069 Action OnUnsignalledSsrc(WebRtcVideoChannel* channel, uint32_t ssrc) override;
eladalonf1841382017-06-12 01:16:46 -070070
71 rtc::VideoSinkInterface<webrtc::VideoFrame>* GetDefaultSink() const;
72 void SetDefaultSink(WebRtcVideoChannel* channel,
73 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
74
75 virtual ~DefaultUnsignalledSsrcHandler() = default;
76
77 private:
78 rtc::VideoSinkInterface<webrtc::VideoFrame>* default_sink_;
79};
80
81// WebRtcVideoEngine is used for the new native WebRTC Video API (webrtc:1667).
Sebastian Jansson84848f22018-11-16 10:40:36 +010082class WebRtcVideoEngine : public VideoEngineInterface {
eladalonf1841382017-06-12 01:16:46 -070083 public:
Anders Carlssondd8c1652018-01-30 10:32:13 +010084#if defined(USE_BUILTIN_SW_CODECS)
Magnus Jedvert02e7a192017-09-23 17:21:32 +020085 // Internal SW video codecs will be added on top of the external codecs.
Anders Carlsson01092952018-12-11 15:44:54 +010086 RTC_DEPRECATED WebRtcVideoEngine(
Magnus Jedvert02e7a192017-09-23 17:21:32 +020087 std::unique_ptr<WebRtcVideoEncoderFactory> external_video_encoder_factory,
Jiawei Ouc2ebe212018-11-08 10:02:56 -080088 std::unique_ptr<WebRtcVideoDecoderFactory> external_video_decoder_factory,
89 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
90 video_bitrate_allocator_factory);
Anders Carlssondd8c1652018-01-30 10:32:13 +010091#endif
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020092
93 // These video codec factories represents all video codecs, i.e. both software
94 // and external hardware codecs.
95 WebRtcVideoEngine(
96 std::unique_ptr<webrtc::VideoEncoderFactory> video_encoder_factory,
Jiawei Ouc2ebe212018-11-08 10:02:56 -080097 std::unique_ptr<webrtc::VideoDecoderFactory> video_decoder_factory,
98 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
99 video_bitrate_allocator_factory);
Magnus Jedvertd4b0c052017-09-14 10:24:54 +0200100
Sebastian Jansson84848f22018-11-16 10:40:36 +0100101 ~WebRtcVideoEngine() override;
eladalonf1841382017-06-12 01:16:46 -0700102
Sebastian Jansson84848f22018-11-16 10:40:36 +0100103 VideoMediaChannel* CreateMediaChannel(
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700104 webrtc::Call* call,
105 const MediaConfig& config,
106 const VideoOptions& options,
Sebastian Jansson84848f22018-11-16 10:40:36 +0100107 const webrtc::CryptoOptions& crypto_options) override;
eladalonf1841382017-06-12 01:16:46 -0700108
Sebastian Jansson84848f22018-11-16 10:40:36 +0100109 std::vector<VideoCodec> codecs() const override;
110 RtpCapabilities GetCapabilities() const override;
eladalonf1841382017-06-12 01:16:46 -0700111
eladalonf1841382017-06-12 01:16:46 -0700112 private:
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200113 const std::unique_ptr<webrtc::VideoDecoderFactory> decoder_factory_;
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100114 const std::unique_ptr<webrtc::VideoEncoderFactory> encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800115 const std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
116 bitrate_allocator_factory_;
eladalonf1841382017-06-12 01:16:46 -0700117};
118
119class WebRtcVideoChannel : public VideoMediaChannel, public webrtc::Transport {
120 public:
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800121 WebRtcVideoChannel(
122 webrtc::Call* call,
123 const MediaConfig& config,
124 const VideoOptions& options,
125 const webrtc::CryptoOptions& crypto_options,
126 webrtc::VideoEncoderFactory* encoder_factory,
127 webrtc::VideoDecoderFactory* decoder_factory,
128 webrtc::VideoBitrateAllocatorFactory* bitrate_allocator_factory);
eladalonf1841382017-06-12 01:16:46 -0700129 ~WebRtcVideoChannel() override;
130
131 // VideoMediaChannel implementation
132 rtc::DiffServCodePoint PreferredDscp() const override;
133
134 bool SetSendParameters(const VideoSendParameters& params) override;
135 bool SetRecvParameters(const VideoRecvParameters& params) override;
136 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800137 webrtc::RTCError SetRtpSendParameters(
138 uint32_t ssrc,
139 const webrtc::RtpParameters& parameters) override;
eladalonf1841382017-06-12 01:16:46 -0700140 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const override;
141 bool SetRtpReceiveParameters(
142 uint32_t ssrc,
143 const webrtc::RtpParameters& parameters) override;
144 bool GetSendCodec(VideoCodec* send_codec) override;
145 bool SetSend(bool send) override;
146 bool SetVideoSend(
147 uint32_t ssrc,
eladalonf1841382017-06-12 01:16:46 -0700148 const VideoOptions* options,
149 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) override;
150 bool AddSendStream(const StreamParams& sp) override;
151 bool RemoveSendStream(uint32_t ssrc) override;
152 bool AddRecvStream(const StreamParams& sp) override;
153 bool AddRecvStream(const StreamParams& sp, bool default_stream);
154 bool RemoveRecvStream(uint32_t ssrc) override;
155 bool SetSink(uint32_t ssrc,
156 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
157 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) override;
158 bool GetStats(VideoMediaInfo* info) override;
159
160 void OnPacketReceived(rtc::CopyOnWriteBuffer* packet,
Niels Möllere6933812018-11-05 13:01:41 +0100161 int64_t packet_time_us) override;
eladalonf1841382017-06-12 01:16:46 -0700162 void OnRtcpReceived(rtc::CopyOnWriteBuffer* packet,
Niels Möllere6933812018-11-05 13:01:41 +0100163 int64_t packet_time_us) override;
eladalonf1841382017-06-12 01:16:46 -0700164 void OnReadyToSend(bool ready) override;
165 void OnNetworkRouteChanged(const std::string& transport_name,
166 const rtc::NetworkRoute& network_route) override;
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700167 void SetInterface(NetworkInterface* iface,
168 webrtc::MediaTransportInterface* media_transport) override;
eladalonf1841382017-06-12 01:16:46 -0700169
Benjamin Wright192eeec2018-10-17 17:27:25 -0700170 // E2E Encrypted Video Frame API
171 // Set a frame decryptor to a particular ssrc that will intercept all
172 // incoming video frames and attempt to decrypt them before forwarding the
173 // result.
174 void SetFrameDecryptor(uint32_t ssrc,
175 rtc::scoped_refptr<webrtc::FrameDecryptorInterface>
176 frame_decryptor) override;
177 // Set a frame encryptor to a particular ssrc that will intercept all
178 // outgoing video frames and attempt to encrypt them and forward the result
179 // to the packetizer.
180 void SetFrameEncryptor(uint32_t ssrc,
181 rtc::scoped_refptr<webrtc::FrameEncryptorInterface>
182 frame_encryptor) override;
183
eladalonf1841382017-06-12 01:16:46 -0700184 // Implemented for VideoMediaChannelTest.
185 bool sending() const { return sending_; }
186
Danil Chapovalov00c71832018-06-15 15:58:38 +0200187 absl::optional<uint32_t> GetDefaultReceiveStreamSsrc();
eladalonf1841382017-06-12 01:16:46 -0700188
Seth Hampson5897a6e2018-04-03 11:16:33 -0700189 StreamParams unsignaled_stream_params() { return unsignaled_stream_params_; }
190
eladalonf1841382017-06-12 01:16:46 -0700191 // AdaptReason is used for expressing why a WebRtcVideoSendStream request
192 // a lower input frame size than the currently configured camera input frame
193 // size. There can be more than one reason OR:ed together.
194 enum AdaptReason {
195 ADAPTREASON_NONE = 0,
196 ADAPTREASON_CPU = 1,
197 ADAPTREASON_BANDWIDTH = 2,
198 };
199
sprang67561a62017-06-15 06:34:42 -0700200 static constexpr int kDefaultQpMax = 56;
201
Jonas Oreland49ac5952018-09-26 16:04:32 +0200202 std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const override;
203
eladalonf1841382017-06-12 01:16:46 -0700204 private:
205 class WebRtcVideoReceiveStream;
206 struct VideoCodecSettings {
207 VideoCodecSettings();
208
209 // Checks if all members of |*this| are equal to the corresponding members
210 // of |other|.
211 bool operator==(const VideoCodecSettings& other) const;
212 bool operator!=(const VideoCodecSettings& other) const;
213
214 // Checks if all members of |a|, except |flexfec_payload_type|, are equal
215 // to the corresponding members of |b|.
216 static bool EqualsDisregardingFlexfec(const VideoCodecSettings& a,
217 const VideoCodecSettings& b);
218
219 VideoCodec codec;
220 webrtc::UlpfecConfig ulpfec;
221 int flexfec_payload_type;
222 int rtx_payload_type;
223 };
224
225 struct ChangedSendParameters {
226 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200227 absl::optional<VideoCodecSettings> codec;
228 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
229 absl::optional<std::string> mid;
Johannes Kron9190b822018-10-29 11:22:05 +0100230 absl::optional<bool> extmap_allow_mixed;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200231 absl::optional<int> max_bandwidth_bps;
232 absl::optional<bool> conference_mode;
233 absl::optional<webrtc::RtcpMode> rtcp_mode;
eladalonf1841382017-06-12 01:16:46 -0700234 };
235
236 struct ChangedRecvParameters {
237 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200238 absl::optional<std::vector<VideoCodecSettings>> codec_settings;
239 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
eladalonf1841382017-06-12 01:16:46 -0700240 // Keep track of the FlexFEC payload type separately from |codec_settings|.
241 // This allows us to recreate the FlexfecReceiveStream separately from the
242 // VideoReceiveStream when the FlexFEC payload type is changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200243 absl::optional<int> flexfec_payload_type;
eladalonf1841382017-06-12 01:16:46 -0700244 };
245
246 bool GetChangedSendParameters(const VideoSendParameters& params,
247 ChangedSendParameters* changed_params) const;
248 bool GetChangedRecvParameters(const VideoRecvParameters& params,
249 ChangedRecvParameters* changed_params) const;
250
251 void SetMaxSendBandwidth(int bps);
252
253 void ConfigureReceiverRtp(
254 webrtc::VideoReceiveStream::Config* config,
255 webrtc::FlexfecReceiveStream::Config* flexfec_config,
256 const StreamParams& sp) const;
257 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
danilchapa37de392017-09-09 04:17:22 -0700258 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700259 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
danilchapa37de392017-09-09 04:17:22 -0700260 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700261 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
danilchapa37de392017-09-09 04:17:22 -0700262 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700263
264 static std::string CodecSettingsVectorToString(
265 const std::vector<VideoCodecSettings>& codecs);
266
267 // Wrapper for the sender part.
268 class WebRtcVideoSendStream
269 : public rtc::VideoSourceInterface<webrtc::VideoFrame> {
270 public:
271 WebRtcVideoSendStream(
272 webrtc::Call* call,
273 const StreamParams& sp,
274 webrtc::VideoSendStream::Config config,
275 const VideoOptions& options,
eladalonf1841382017-06-12 01:16:46 -0700276 bool enable_cpu_overuse_detection,
277 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200278 const absl::optional<VideoCodecSettings>& codec_settings,
279 const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions,
eladalonf1841382017-06-12 01:16:46 -0700280 const VideoSendParameters& send_params);
281 virtual ~WebRtcVideoSendStream();
282
283 void SetSendParameters(const ChangedSendParameters& send_params);
Zach Steinba37b4b2018-01-23 15:02:36 -0800284 webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters);
eladalonf1841382017-06-12 01:16:46 -0700285 webrtc::RtpParameters GetRtpParameters() const;
286
Benjamin Wright192eeec2018-10-17 17:27:25 -0700287 void SetFrameEncryptor(
288 rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor);
289
eladalonf1841382017-06-12 01:16:46 -0700290 // Implements rtc::VideoSourceInterface<webrtc::VideoFrame>.
291 // WebRtcVideoSendStream acts as a source to the webrtc::VideoSendStream
292 // in |stream_|. This is done to proxy VideoSinkWants from the encoder to
293 // the worker thread.
294 void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
295 const rtc::VideoSinkWants& wants) override;
296 void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
297
Niels Möllerff40b142018-04-09 08:49:14 +0200298 bool SetVideoSend(const VideoOptions* options,
eladalonf1841382017-06-12 01:16:46 -0700299 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
300
301 void SetSend(bool send);
302
303 const std::vector<uint32_t>& GetSsrcs() const;
304 VideoSenderInfo GetVideoSenderInfo(bool log_stats);
305 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
306
307 private:
308 // Parameters needed to reconstruct the underlying stream.
309 // webrtc::VideoSendStream doesn't support setting a lot of options on the
310 // fly, so when those need to be changed we tear down and reconstruct with
311 // similar parameters depending on which options changed etc.
312 struct VideoSendStreamParameters {
313 VideoSendStreamParameters(
314 webrtc::VideoSendStream::Config config,
315 const VideoOptions& options,
316 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200317 const absl::optional<VideoCodecSettings>& codec_settings);
eladalonf1841382017-06-12 01:16:46 -0700318 webrtc::VideoSendStream::Config config;
319 VideoOptions options;
320 int max_bitrate_bps;
321 bool conference_mode;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200322 absl::optional<VideoCodecSettings> codec_settings;
eladalonf1841382017-06-12 01:16:46 -0700323 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
324 // typically changes when setting a new resolution or reconfiguring
325 // bitrates.
326 webrtc::VideoEncoderConfig encoder_config;
327 };
328
eladalonf1841382017-06-12 01:16:46 -0700329 rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
330 ConfigureVideoEncoderSettings(const VideoCodec& codec);
Niels Möller5bf8ccd2018-03-15 14:16:11 +0100331 void SetCodec(const VideoCodecSettings& codec);
eladalonf1841382017-06-12 01:16:46 -0700332 void RecreateWebRtcStream();
333 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
334 const VideoCodec& codec) const;
335 void ReconfigureEncoder();
eladalonf1841382017-06-12 01:16:46 -0700336
337 // Calls Start or Stop according to whether or not |sending_| is true,
338 // and whether or not the encoding in |rtp_parameters_| is active.
339 void UpdateSendState();
340
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700341 webrtc::DegradationPreference GetDegradationPreference() const
342 RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700343
344 rtc::ThreadChecker thread_checker_;
345 rtc::AsyncInvoker invoker_;
346 rtc::Thread* worker_thread_;
Niels Möller1e062892018-02-07 10:18:32 +0100347 const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_);
348 const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700349 webrtc::Call* const call_;
350 const bool enable_cpu_overuse_detection_;
351 rtc::VideoSourceInterface<webrtc::VideoFrame>* source_
Niels Möller1e062892018-02-07 10:18:32 +0100352 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700353
Niels Möller1e062892018-02-07 10:18:32 +0100354 webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700355 rtc::VideoSinkInterface<webrtc::VideoFrame>* encoder_sink_
Niels Möller1e062892018-02-07 10:18:32 +0100356 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700357 // Contains settings that are the same for all streams in the MediaChannel,
358 // such as codecs, header extensions, and the global bitrate limit for the
359 // entire channel.
Niels Möller1e062892018-02-07 10:18:32 +0100360 VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700361 // Contains settings that are unique for each stream, such as max_bitrate.
362 // Does *not* contain codecs, however.
363 // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_.
364 // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only
365 // one stream per MediaChannel.
Niels Möller1e062892018-02-07 10:18:32 +0100366 webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700367
Niels Möller1e062892018-02-07 10:18:32 +0100368 bool sending_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700369 };
370
371 // Wrapper for the receiver part, contains configs etc. that are needed to
372 // reconstruct the underlying VideoReceiveStream.
373 class WebRtcVideoReceiveStream
374 : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
375 public:
376 WebRtcVideoReceiveStream(
377 webrtc::Call* call,
378 const StreamParams& sp,
379 webrtc::VideoReceiveStream::Config config,
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200380 webrtc::VideoDecoderFactory* decoder_factory,
eladalonf1841382017-06-12 01:16:46 -0700381 bool default_stream,
382 const std::vector<VideoCodecSettings>& recv_codecs,
383 const webrtc::FlexfecReceiveStream::Config& flexfec_config);
384 ~WebRtcVideoReceiveStream();
385
386 const std::vector<uint32_t>& GetSsrcs() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200387
Jonas Oreland49ac5952018-09-26 16:04:32 +0200388 std::vector<webrtc::RtpSource> GetSources();
389
Florent Castelliabe301f2018-06-12 18:33:49 +0200390 // Does not return codecs, they are filled by the owning WebRtcVideoChannel.
391 webrtc::RtpParameters GetRtpParameters() const;
eladalonf1841382017-06-12 01:16:46 -0700392
393 void SetLocalSsrc(uint32_t local_ssrc);
394 // TODO(deadbeef): Move these feedback parameters into the recv parameters.
395 void SetFeedbackParameters(bool nack_enabled,
396 bool remb_enabled,
397 bool transport_cc_enabled,
398 webrtc::RtcpMode rtcp_mode);
399 void SetRecvParameters(const ChangedRecvParameters& recv_params);
400
401 void OnFrame(const webrtc::VideoFrame& frame) override;
402 bool IsDefaultStream() const;
403
Benjamin Wright192eeec2018-10-17 17:27:25 -0700404 void SetFrameDecryptor(
405 rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor);
406
eladalonf1841382017-06-12 01:16:46 -0700407 void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
408
409 VideoReceiverInfo GetVideoReceiverInfo(bool log_stats);
410
411 private:
eladalonf1841382017-06-12 01:16:46 -0700412 void RecreateWebRtcVideoStream();
413 void MaybeRecreateWebRtcFlexfecStream();
414
eladalonc0d481a2017-08-02 07:39:07 -0700415 void MaybeAssociateFlexfecWithVideo();
416 void MaybeDissociateFlexfecFromVideo();
417
Niels Möllercbcbc222018-09-28 09:07:24 +0200418 void ConfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
eladalonf1841382017-06-12 01:16:46 -0700419 void ConfigureFlexfecCodec(int flexfec_payload_type);
eladalonf1841382017-06-12 01:16:46 -0700420
421 std::string GetCodecNameFromPayloadType(int payload_type);
422
423 webrtc::Call* const call_;
Niels Möllercbcbc222018-09-28 09:07:24 +0200424 const StreamParams stream_params_;
eladalonf1841382017-06-12 01:16:46 -0700425
426 // Both |stream_| and |flexfec_stream_| are managed by |this|. They are
427 // destroyed by calling call_->DestroyVideoReceiveStream and
428 // call_->DestroyFlexfecReceiveStream, respectively.
429 webrtc::VideoReceiveStream* stream_;
430 const bool default_stream_;
431 webrtc::VideoReceiveStream::Config config_;
432 webrtc::FlexfecReceiveStream::Config flexfec_config_;
433 webrtc::FlexfecReceiveStream* flexfec_stream_;
434
Niels Möllercbcbc222018-09-28 09:07:24 +0200435 webrtc::VideoDecoderFactory* const decoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700436
437 rtc::CriticalSection sink_lock_;
danilchapa37de392017-09-09 04:17:22 -0700438 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_
439 RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700440 // Expands remote RTP timestamps to int64_t to be able to estimate how long
441 // the stream has been running.
442 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
danilchapa37de392017-09-09 04:17:22 -0700443 RTC_GUARDED_BY(sink_lock_);
444 int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700445 // Start NTP time is estimated as current remote NTP time (estimated from
446 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
danilchapa37de392017-09-09 04:17:22 -0700447 int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700448 };
449
450 void Construct(webrtc::Call* call, WebRtcVideoEngine* engine);
451
452 bool SendRtp(const uint8_t* data,
453 size_t len,
454 const webrtc::PacketOptions& options) override;
455 bool SendRtcp(const uint8_t* data, size_t len) override;
456
457 static std::vector<VideoCodecSettings> MapCodecs(
458 const std::vector<VideoCodec>& codecs);
459 // Select what video codec will be used for sending, i.e. what codec is used
460 // for local encoding, based on supported remote codecs. The first remote
461 // codec that is supported locally will be selected.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200462 absl::optional<VideoCodecSettings> SelectSendVideoCodec(
eladalonf1841382017-06-12 01:16:46 -0700463 const std::vector<VideoCodecSettings>& remote_mapped_codecs) const;
464
465 static bool NonFlexfecReceiveCodecsHaveChanged(
466 std::vector<VideoCodecSettings> before,
467 std::vector<VideoCodecSettings> after);
468
469 void FillSenderStats(VideoMediaInfo* info, bool log_stats);
470 void FillReceiverStats(VideoMediaInfo* info, bool log_stats);
471 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
472 VideoMediaInfo* info);
473 void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info);
474
475 rtc::ThreadChecker thread_checker_;
476
477 uint32_t rtcp_receiver_report_ssrc_;
478 bool sending_;
479 webrtc::Call* const call_;
480
481 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
482 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
483
484 const MediaConfig::Video video_config_;
485
486 rtc::CriticalSection stream_crit_;
487 // Using primary-ssrc (first ssrc) as key.
488 std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
danilchapa37de392017-09-09 04:17:22 -0700489 RTC_GUARDED_BY(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700490 std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700491 RTC_GUARDED_BY(stream_crit_);
492 std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(stream_crit_);
493 std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700494
Danil Chapovalov00c71832018-06-15 15:58:38 +0200495 absl::optional<VideoCodecSettings> send_codec_;
496 absl::optional<std::vector<webrtc::RtpExtension>> send_rtp_extensions_;
eladalonf1841382017-06-12 01:16:46 -0700497
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100498 webrtc::VideoEncoderFactory* const encoder_factory_;
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200499 webrtc::VideoDecoderFactory* const decoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800500 webrtc::VideoBitrateAllocatorFactory* const bitrate_allocator_factory_;
eladalonf1841382017-06-12 01:16:46 -0700501 std::vector<VideoCodecSettings> recv_codecs_;
502 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
503 // See reason for keeping track of the FlexFEC payload type separately in
504 // comment in WebRtcVideoChannel::ChangedRecvParameters.
505 int recv_flexfec_payload_type_;
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +0100506 webrtc::BitrateConstraints bitrate_config_;
eladalonf1841382017-06-12 01:16:46 -0700507 // TODO(deadbeef): Don't duplicate information between
508 // send_params/recv_params, rtp_extensions, options, etc.
509 VideoSendParameters send_params_;
Tim Haloun648d28a2018-10-18 16:52:22 -0700510 rtc::DiffServCodePoint preferred_dscp_;
eladalonf1841382017-06-12 01:16:46 -0700511 VideoOptions default_send_options_;
512 VideoRecvParameters recv_params_;
513 int64_t last_stats_log_ms_;
Åsa Persson2c7149b2018-10-15 09:36:10 +0200514 const bool discard_unknown_ssrc_packets_;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700515 // This is a stream param that comes from the remote description, but wasn't
516 // signaled with any a=ssrc lines. It holds information that was signaled
517 // before the unsignaled receive stream is created when the first packet is
518 // received.
519 StreamParams unsignaled_stream_params_;
Benjamin Wright192eeec2018-10-17 17:27:25 -0700520 // Per peer connection crypto options that last for the lifetime of the peer
521 // connection.
522 const webrtc::CryptoOptions crypto_options_;
eladalonf1841382017-06-12 01:16:46 -0700523};
524
ilnik6b826ef2017-06-16 06:53:48 -0700525class EncoderStreamFactory
526 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
527 public:
528 EncoderStreamFactory(std::string codec_name,
529 int max_qp,
Seth Hampson1370e302018-02-07 08:50:36 -0800530 bool is_screenshare,
531 bool screenshare_config_explicitly_enabled);
ilnik6b826ef2017-06-16 06:53:48 -0700532
533 private:
534 std::vector<webrtc::VideoStream> CreateEncoderStreams(
535 int width,
536 int height,
537 const webrtc::VideoEncoderConfig& encoder_config) override;
538
539 const std::string codec_name_;
540 const int max_qp_;
Seth Hampson1370e302018-02-07 08:50:36 -0800541 const bool is_screenshare_;
542 // Allows a screenshare specific configuration, which enables temporal
543 // layering and allows simulcast.
544 const bool screenshare_config_explicitly_enabled_;
ilnik6b826ef2017-06-16 06:53:48 -0700545};
546
eladalonf1841382017-06-12 01:16:46 -0700547} // namespace cricket
548
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200549#endif // MEDIA_ENGINE_WEBRTCVIDEOENGINE_H_