blob: 06c1cfdeed8ee3860bf404dd994774af0b4ca312 [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 {
40class VideoDecoder;
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020041class VideoDecoderFactory;
eladalonf1841382017-06-12 01:16:46 -070042class VideoEncoder;
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020043class VideoEncoderFactory;
eladalonf1841382017-06-12 01:16:46 -070044struct MediaConfig;
Yves Gerey665174f2018-06-19 15:03:05 +020045} // namespace webrtc
eladalonf1841382017-06-12 01:16:46 -070046
47namespace rtc {
48class Thread;
49} // namespace rtc
50
51namespace cricket {
52
andersc063f0c02017-09-11 11:50:51 -070053class DecoderFactoryAdapter;
eladalonf1841382017-06-12 01:16:46 -070054class WebRtcVideoChannel;
eladalonf1841382017-06-12 01:16:46 -070055
eladalonf1841382017-06-12 01:16:46 -070056class UnsignalledSsrcHandler {
57 public:
58 enum Action {
59 kDropPacket,
60 kDeliverPacket,
61 };
62 virtual Action OnUnsignalledSsrc(WebRtcVideoChannel* channel,
63 uint32_t ssrc) = 0;
64 virtual ~UnsignalledSsrcHandler() = default;
65};
66
67// TODO(pbos): Remove, use external handlers only.
68class DefaultUnsignalledSsrcHandler : public UnsignalledSsrcHandler {
69 public:
70 DefaultUnsignalledSsrcHandler();
Yves Gerey665174f2018-06-19 15:03:05 +020071 Action OnUnsignalledSsrc(WebRtcVideoChannel* channel, uint32_t ssrc) override;
eladalonf1841382017-06-12 01:16:46 -070072
73 rtc::VideoSinkInterface<webrtc::VideoFrame>* GetDefaultSink() const;
74 void SetDefaultSink(WebRtcVideoChannel* channel,
75 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
76
77 virtual ~DefaultUnsignalledSsrcHandler() = default;
78
79 private:
80 rtc::VideoSinkInterface<webrtc::VideoFrame>* default_sink_;
81};
82
83// WebRtcVideoEngine is used for the new native WebRTC Video API (webrtc:1667).
84class WebRtcVideoEngine {
85 public:
Anders Carlssondd8c1652018-01-30 10:32:13 +010086#if defined(USE_BUILTIN_SW_CODECS)
Magnus Jedvert02e7a192017-09-23 17:21:32 +020087 // Internal SW video codecs will be added on top of the external codecs.
88 WebRtcVideoEngine(
89 std::unique_ptr<WebRtcVideoEncoderFactory> external_video_encoder_factory,
90 std::unique_ptr<WebRtcVideoDecoderFactory>
91 external_video_decoder_factory);
Anders Carlssondd8c1652018-01-30 10:32:13 +010092#endif
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020093
94 // These video codec factories represents all video codecs, i.e. both software
95 // and external hardware codecs.
96 WebRtcVideoEngine(
97 std::unique_ptr<webrtc::VideoEncoderFactory> video_encoder_factory,
98 std::unique_ptr<webrtc::VideoDecoderFactory> video_decoder_factory);
99
eladalonf1841382017-06-12 01:16:46 -0700100 virtual ~WebRtcVideoEngine();
101
eladalonf1841382017-06-12 01:16:46 -0700102 WebRtcVideoChannel* CreateChannel(webrtc::Call* call,
103 const MediaConfig& config,
104 const VideoOptions& options);
105
106 std::vector<VideoCodec> codecs() const;
107 RtpCapabilities GetCapabilities() const;
108
eladalonf1841382017-06-12 01:16:46 -0700109 private:
magjed2475ae22017-09-12 04:42:15 -0700110 const std::unique_ptr<DecoderFactoryAdapter> decoder_factory_;
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100111 const std::unique_ptr<webrtc::VideoEncoderFactory> encoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700112};
113
114class WebRtcVideoChannel : public VideoMediaChannel, public webrtc::Transport {
115 public:
116 WebRtcVideoChannel(webrtc::Call* call,
117 const MediaConfig& config,
118 const VideoOptions& options,
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100119 webrtc::VideoEncoderFactory* encoder_factory,
120 DecoderFactoryAdapter* 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;
eladalonf1841382017-06-12 01:16:46 -0700159 void SetInterface(NetworkInterface* iface) override;
160
161 // Implemented for VideoMediaChannelTest.
162 bool sending() const { return sending_; }
163
Danil Chapovalov00c71832018-06-15 15:58:38 +0200164 absl::optional<uint32_t> GetDefaultReceiveStreamSsrc();
eladalonf1841382017-06-12 01:16:46 -0700165
Seth Hampson5897a6e2018-04-03 11:16:33 -0700166 StreamParams unsignaled_stream_params() { return unsignaled_stream_params_; }
167
eladalonf1841382017-06-12 01:16:46 -0700168 // AdaptReason is used for expressing why a WebRtcVideoSendStream request
169 // a lower input frame size than the currently configured camera input frame
170 // size. There can be more than one reason OR:ed together.
171 enum AdaptReason {
172 ADAPTREASON_NONE = 0,
173 ADAPTREASON_CPU = 1,
174 ADAPTREASON_BANDWIDTH = 2,
175 };
176
sprang67561a62017-06-15 06:34:42 -0700177 static constexpr int kDefaultQpMax = 56;
178
eladalonf1841382017-06-12 01:16:46 -0700179 private:
180 class WebRtcVideoReceiveStream;
181 struct VideoCodecSettings {
182 VideoCodecSettings();
183
184 // Checks if all members of |*this| are equal to the corresponding members
185 // of |other|.
186 bool operator==(const VideoCodecSettings& other) const;
187 bool operator!=(const VideoCodecSettings& other) const;
188
189 // Checks if all members of |a|, except |flexfec_payload_type|, are equal
190 // to the corresponding members of |b|.
191 static bool EqualsDisregardingFlexfec(const VideoCodecSettings& a,
192 const VideoCodecSettings& b);
193
194 VideoCodec codec;
195 webrtc::UlpfecConfig ulpfec;
196 int flexfec_payload_type;
197 int rtx_payload_type;
198 };
199
200 struct ChangedSendParameters {
201 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200202 absl::optional<VideoCodecSettings> codec;
203 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
204 absl::optional<std::string> mid;
205 absl::optional<int> max_bandwidth_bps;
206 absl::optional<bool> conference_mode;
207 absl::optional<webrtc::RtcpMode> rtcp_mode;
eladalonf1841382017-06-12 01:16:46 -0700208 };
209
210 struct ChangedRecvParameters {
211 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200212 absl::optional<std::vector<VideoCodecSettings>> codec_settings;
213 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
eladalonf1841382017-06-12 01:16:46 -0700214 // Keep track of the FlexFEC payload type separately from |codec_settings|.
215 // This allows us to recreate the FlexfecReceiveStream separately from the
216 // VideoReceiveStream when the FlexFEC payload type is changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200217 absl::optional<int> flexfec_payload_type;
eladalonf1841382017-06-12 01:16:46 -0700218 };
219
220 bool GetChangedSendParameters(const VideoSendParameters& params,
221 ChangedSendParameters* changed_params) const;
222 bool GetChangedRecvParameters(const VideoRecvParameters& params,
223 ChangedRecvParameters* changed_params) const;
224
225 void SetMaxSendBandwidth(int bps);
226
227 void ConfigureReceiverRtp(
228 webrtc::VideoReceiveStream::Config* config,
229 webrtc::FlexfecReceiveStream::Config* flexfec_config,
230 const StreamParams& sp) const;
231 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
danilchapa37de392017-09-09 04:17:22 -0700232 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700233 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
danilchapa37de392017-09-09 04:17:22 -0700234 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700235 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
danilchapa37de392017-09-09 04:17:22 -0700236 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700237
238 static std::string CodecSettingsVectorToString(
239 const std::vector<VideoCodecSettings>& codecs);
240
241 // Wrapper for the sender part.
242 class WebRtcVideoSendStream
243 : public rtc::VideoSourceInterface<webrtc::VideoFrame> {
244 public:
245 WebRtcVideoSendStream(
246 webrtc::Call* call,
247 const StreamParams& sp,
248 webrtc::VideoSendStream::Config config,
249 const VideoOptions& options,
eladalonf1841382017-06-12 01:16:46 -0700250 bool enable_cpu_overuse_detection,
251 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200252 const absl::optional<VideoCodecSettings>& codec_settings,
253 const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions,
eladalonf1841382017-06-12 01:16:46 -0700254 const VideoSendParameters& send_params);
255 virtual ~WebRtcVideoSendStream();
256
257 void SetSendParameters(const ChangedSendParameters& send_params);
Zach Steinba37b4b2018-01-23 15:02:36 -0800258 webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters);
eladalonf1841382017-06-12 01:16:46 -0700259 webrtc::RtpParameters GetRtpParameters() const;
260
261 // Implements rtc::VideoSourceInterface<webrtc::VideoFrame>.
262 // WebRtcVideoSendStream acts as a source to the webrtc::VideoSendStream
263 // in |stream_|. This is done to proxy VideoSinkWants from the encoder to
264 // the worker thread.
265 void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
266 const rtc::VideoSinkWants& wants) override;
267 void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
268
Niels Möllerff40b142018-04-09 08:49:14 +0200269 bool SetVideoSend(const VideoOptions* options,
eladalonf1841382017-06-12 01:16:46 -0700270 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
271
272 void SetSend(bool send);
273
274 const std::vector<uint32_t>& GetSsrcs() const;
275 VideoSenderInfo GetVideoSenderInfo(bool log_stats);
276 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
277
278 private:
279 // Parameters needed to reconstruct the underlying stream.
280 // webrtc::VideoSendStream doesn't support setting a lot of options on the
281 // fly, so when those need to be changed we tear down and reconstruct with
282 // similar parameters depending on which options changed etc.
283 struct VideoSendStreamParameters {
284 VideoSendStreamParameters(
285 webrtc::VideoSendStream::Config config,
286 const VideoOptions& options,
287 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200288 const absl::optional<VideoCodecSettings>& codec_settings);
eladalonf1841382017-06-12 01:16:46 -0700289 webrtc::VideoSendStream::Config config;
290 VideoOptions options;
291 int max_bitrate_bps;
292 bool conference_mode;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200293 absl::optional<VideoCodecSettings> codec_settings;
eladalonf1841382017-06-12 01:16:46 -0700294 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
295 // typically changes when setting a new resolution or reconfiguring
296 // bitrates.
297 webrtc::VideoEncoderConfig encoder_config;
298 };
299
eladalonf1841382017-06-12 01:16:46 -0700300 rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
301 ConfigureVideoEncoderSettings(const VideoCodec& codec);
Niels Möller5bf8ccd2018-03-15 14:16:11 +0100302 void SetCodec(const VideoCodecSettings& codec);
eladalonf1841382017-06-12 01:16:46 -0700303 void RecreateWebRtcStream();
304 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
305 const VideoCodec& codec) const;
306 void ReconfigureEncoder();
Zach Steinba37b4b2018-01-23 15:02:36 -0800307 webrtc::RTCError ValidateRtpParameters(
308 const webrtc::RtpParameters& parameters);
eladalonf1841382017-06-12 01:16:46 -0700309
310 // Calls Start or Stop according to whether or not |sending_| is true,
311 // and whether or not the encoding in |rtp_parameters_| is active.
312 void UpdateSendState();
313
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700314 webrtc::DegradationPreference GetDegradationPreference() const
315 RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700316
317 rtc::ThreadChecker thread_checker_;
318 rtc::AsyncInvoker invoker_;
319 rtc::Thread* worker_thread_;
Niels Möller1e062892018-02-07 10:18:32 +0100320 const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_);
321 const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700322 webrtc::Call* const call_;
323 const bool enable_cpu_overuse_detection_;
324 rtc::VideoSourceInterface<webrtc::VideoFrame>* source_
Niels Möller1e062892018-02-07 10:18:32 +0100325 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700326
Niels Möller1e062892018-02-07 10:18:32 +0100327 webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700328 rtc::VideoSinkInterface<webrtc::VideoFrame>* encoder_sink_
Niels Möller1e062892018-02-07 10:18:32 +0100329 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700330 // Contains settings that are the same for all streams in the MediaChannel,
331 // such as codecs, header extensions, and the global bitrate limit for the
332 // entire channel.
Niels Möller1e062892018-02-07 10:18:32 +0100333 VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700334 // Contains settings that are unique for each stream, such as max_bitrate.
335 // Does *not* contain codecs, however.
336 // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_.
337 // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only
338 // one stream per MediaChannel.
Niels Möller1e062892018-02-07 10:18:32 +0100339 webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700340
Niels Möller1e062892018-02-07 10:18:32 +0100341 bool sending_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700342 };
343
344 // Wrapper for the receiver part, contains configs etc. that are needed to
345 // reconstruct the underlying VideoReceiveStream.
346 class WebRtcVideoReceiveStream
347 : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
348 public:
349 WebRtcVideoReceiveStream(
350 webrtc::Call* call,
351 const StreamParams& sp,
352 webrtc::VideoReceiveStream::Config config,
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100353 DecoderFactoryAdapter* decoder_factory,
eladalonf1841382017-06-12 01:16:46 -0700354 bool default_stream,
355 const std::vector<VideoCodecSettings>& recv_codecs,
356 const webrtc::FlexfecReceiveStream::Config& flexfec_config);
357 ~WebRtcVideoReceiveStream();
358
359 const std::vector<uint32_t>& GetSsrcs() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200360
361 // Does not return codecs, they are filled by the owning WebRtcVideoChannel.
362 webrtc::RtpParameters GetRtpParameters() const;
eladalonf1841382017-06-12 01:16:46 -0700363
364 void SetLocalSsrc(uint32_t local_ssrc);
365 // TODO(deadbeef): Move these feedback parameters into the recv parameters.
366 void SetFeedbackParameters(bool nack_enabled,
367 bool remb_enabled,
368 bool transport_cc_enabled,
369 webrtc::RtcpMode rtcp_mode);
370 void SetRecvParameters(const ChangedRecvParameters& recv_params);
371
372 void OnFrame(const webrtc::VideoFrame& frame) override;
373 bool IsDefaultStream() const;
374
375 void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
376
377 VideoReceiverInfo GetVideoReceiverInfo(bool log_stats);
378
379 private:
andersc063f0c02017-09-11 11:50:51 -0700380 struct SdpVideoFormatCompare {
381 bool operator()(const webrtc::SdpVideoFormat& lhs,
382 const webrtc::SdpVideoFormat& rhs) const {
383 return std::tie(lhs.name, lhs.parameters) <
384 std::tie(rhs.name, rhs.parameters);
385 }
perkj1f885312017-09-04 02:43:10 -0700386 };
andersc063f0c02017-09-11 11:50:51 -0700387 typedef std::map<webrtc::SdpVideoFormat,
388 std::unique_ptr<webrtc::VideoDecoder>,
389 SdpVideoFormatCompare>
390 DecoderMap;
perkj1f885312017-09-04 02:43:10 -0700391
eladalonf1841382017-06-12 01:16:46 -0700392 void RecreateWebRtcVideoStream();
393 void MaybeRecreateWebRtcFlexfecStream();
394
eladalonc0d481a2017-08-02 07:39:07 -0700395 void MaybeAssociateFlexfecWithVideo();
396 void MaybeDissociateFlexfecFromVideo();
397
perkj1f885312017-09-04 02:43:10 -0700398 void ConfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs,
andersc063f0c02017-09-11 11:50:51 -0700399 DecoderMap* old_codecs);
eladalonf1841382017-06-12 01:16:46 -0700400 void ConfigureFlexfecCodec(int flexfec_payload_type);
eladalonf1841382017-06-12 01:16:46 -0700401
402 std::string GetCodecNameFromPayloadType(int payload_type);
403
Danil Chapovalov00c71832018-06-15 15:58:38 +0200404 absl::optional<uint32_t> GetFirstPrimarySsrc() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200405
eladalonf1841382017-06-12 01:16:46 -0700406 webrtc::Call* const call_;
407 StreamParams stream_params_;
408
409 // Both |stream_| and |flexfec_stream_| are managed by |this|. They are
410 // destroyed by calling call_->DestroyVideoReceiveStream and
411 // call_->DestroyFlexfecReceiveStream, respectively.
412 webrtc::VideoReceiveStream* stream_;
413 const bool default_stream_;
414 webrtc::VideoReceiveStream::Config config_;
415 webrtc::FlexfecReceiveStream::Config flexfec_config_;
416 webrtc::FlexfecReceiveStream* flexfec_stream_;
417
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100418 DecoderFactoryAdapter* decoder_factory_;
andersc063f0c02017-09-11 11:50:51 -0700419 DecoderMap allocated_decoders_;
eladalonf1841382017-06-12 01:16:46 -0700420
421 rtc::CriticalSection sink_lock_;
danilchapa37de392017-09-09 04:17:22 -0700422 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_
423 RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700424 // Expands remote RTP timestamps to int64_t to be able to estimate how long
425 // the stream has been running.
426 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
danilchapa37de392017-09-09 04:17:22 -0700427 RTC_GUARDED_BY(sink_lock_);
428 int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700429 // Start NTP time is estimated as current remote NTP time (estimated from
430 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
danilchapa37de392017-09-09 04:17:22 -0700431 int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700432 };
433
434 void Construct(webrtc::Call* call, WebRtcVideoEngine* engine);
435
436 bool SendRtp(const uint8_t* data,
437 size_t len,
438 const webrtc::PacketOptions& options) override;
439 bool SendRtcp(const uint8_t* data, size_t len) override;
440
441 static std::vector<VideoCodecSettings> MapCodecs(
442 const std::vector<VideoCodec>& codecs);
443 // Select what video codec will be used for sending, i.e. what codec is used
444 // for local encoding, based on supported remote codecs. The first remote
445 // codec that is supported locally will be selected.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200446 absl::optional<VideoCodecSettings> SelectSendVideoCodec(
eladalonf1841382017-06-12 01:16:46 -0700447 const std::vector<VideoCodecSettings>& remote_mapped_codecs) const;
448
449 static bool NonFlexfecReceiveCodecsHaveChanged(
450 std::vector<VideoCodecSettings> before,
451 std::vector<VideoCodecSettings> after);
452
453 void FillSenderStats(VideoMediaInfo* info, bool log_stats);
454 void FillReceiverStats(VideoMediaInfo* info, bool log_stats);
455 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
456 VideoMediaInfo* info);
457 void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info);
458
459 rtc::ThreadChecker thread_checker_;
460
461 uint32_t rtcp_receiver_report_ssrc_;
462 bool sending_;
463 webrtc::Call* const call_;
464
465 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
466 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
467
468 const MediaConfig::Video video_config_;
469
470 rtc::CriticalSection stream_crit_;
471 // Using primary-ssrc (first ssrc) as key.
472 std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
danilchapa37de392017-09-09 04:17:22 -0700473 RTC_GUARDED_BY(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700474 std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700475 RTC_GUARDED_BY(stream_crit_);
476 std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(stream_crit_);
477 std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700478
Danil Chapovalov00c71832018-06-15 15:58:38 +0200479 absl::optional<VideoCodecSettings> send_codec_;
480 absl::optional<std::vector<webrtc::RtpExtension>> send_rtp_extensions_;
eladalonf1841382017-06-12 01:16:46 -0700481
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100482 webrtc::VideoEncoderFactory* const encoder_factory_;
483 DecoderFactoryAdapter* const decoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700484 std::vector<VideoCodecSettings> recv_codecs_;
485 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
486 // See reason for keeping track of the FlexFEC payload type separately in
487 // comment in WebRtcVideoChannel::ChangedRecvParameters.
488 int recv_flexfec_payload_type_;
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +0100489 webrtc::BitrateConstraints bitrate_config_;
eladalonf1841382017-06-12 01:16:46 -0700490 // TODO(deadbeef): Don't duplicate information between
491 // send_params/recv_params, rtp_extensions, options, etc.
492 VideoSendParameters send_params_;
493 VideoOptions default_send_options_;
494 VideoRecvParameters recv_params_;
495 int64_t last_stats_log_ms_;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700496 // This is a stream param that comes from the remote description, but wasn't
497 // signaled with any a=ssrc lines. It holds information that was signaled
498 // before the unsignaled receive stream is created when the first packet is
499 // received.
500 StreamParams unsignaled_stream_params_;
eladalonf1841382017-06-12 01:16:46 -0700501};
502
ilnik6b826ef2017-06-16 06:53:48 -0700503class EncoderStreamFactory
504 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
505 public:
506 EncoderStreamFactory(std::string codec_name,
507 int max_qp,
Seth Hampson1370e302018-02-07 08:50:36 -0800508 bool is_screenshare,
509 bool screenshare_config_explicitly_enabled);
ilnik6b826ef2017-06-16 06:53:48 -0700510
511 private:
512 std::vector<webrtc::VideoStream> CreateEncoderStreams(
513 int width,
514 int height,
515 const webrtc::VideoEncoderConfig& encoder_config) override;
516
517 const std::string codec_name_;
518 const int max_qp_;
Seth Hampson1370e302018-02-07 08:50:36 -0800519 const bool is_screenshare_;
520 // Allows a screenshare specific configuration, which enables temporal
521 // layering and allows simulcast.
522 const bool screenshare_config_explicitly_enabled_;
ilnik6b826ef2017-06-16 06:53:48 -0700523};
524
eladalonf1841382017-06-12 01:16:46 -0700525} // namespace cricket
526
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200527#endif // MEDIA_ENGINE_WEBRTCVIDEOENGINE_H_