blob: 8a1e6ad4d1703648edcf4570df47a33917d3c61a [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,
87 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,
95 std::unique_ptr<webrtc::VideoDecoderFactory> video_decoder_factory);
96
eladalonf1841382017-06-12 01:16:46 -070097 virtual ~WebRtcVideoEngine();
98
eladalonf1841382017-06-12 01:16:46 -070099 WebRtcVideoChannel* CreateChannel(webrtc::Call* call,
100 const MediaConfig& config,
101 const VideoOptions& options);
102
103 std::vector<VideoCodec> codecs() const;
104 RtpCapabilities GetCapabilities() const;
105
eladalonf1841382017-06-12 01:16:46 -0700106 private:
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200107 const std::unique_ptr<webrtc::VideoDecoderFactory> decoder_factory_;
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100108 const std::unique_ptr<webrtc::VideoEncoderFactory> encoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700109};
110
111class WebRtcVideoChannel : public VideoMediaChannel, public webrtc::Transport {
112 public:
113 WebRtcVideoChannel(webrtc::Call* call,
114 const MediaConfig& config,
115 const VideoOptions& options,
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100116 webrtc::VideoEncoderFactory* encoder_factory,
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200117 webrtc::VideoDecoderFactory* decoder_factory);
eladalonf1841382017-06-12 01:16:46 -0700118 ~WebRtcVideoChannel() override;
119
120 // VideoMediaChannel implementation
121 rtc::DiffServCodePoint PreferredDscp() const override;
122
123 bool SetSendParameters(const VideoSendParameters& params) override;
124 bool SetRecvParameters(const VideoRecvParameters& params) override;
125 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800126 webrtc::RTCError SetRtpSendParameters(
127 uint32_t ssrc,
128 const webrtc::RtpParameters& parameters) override;
eladalonf1841382017-06-12 01:16:46 -0700129 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const override;
130 bool SetRtpReceiveParameters(
131 uint32_t ssrc,
132 const webrtc::RtpParameters& parameters) override;
133 bool GetSendCodec(VideoCodec* send_codec) override;
134 bool SetSend(bool send) override;
135 bool SetVideoSend(
136 uint32_t ssrc,
eladalonf1841382017-06-12 01:16:46 -0700137 const VideoOptions* options,
138 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) override;
139 bool AddSendStream(const StreamParams& sp) override;
140 bool RemoveSendStream(uint32_t ssrc) override;
141 bool AddRecvStream(const StreamParams& sp) override;
142 bool AddRecvStream(const StreamParams& sp, bool default_stream);
143 bool RemoveRecvStream(uint32_t ssrc) override;
144 bool SetSink(uint32_t ssrc,
145 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
146 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) override;
147 bool GetStats(VideoMediaInfo* info) override;
148
149 void OnPacketReceived(rtc::CopyOnWriteBuffer* packet,
150 const rtc::PacketTime& packet_time) override;
151 void OnRtcpReceived(rtc::CopyOnWriteBuffer* packet,
152 const rtc::PacketTime& packet_time) override;
153 void OnReadyToSend(bool ready) override;
154 void OnNetworkRouteChanged(const std::string& transport_name,
155 const rtc::NetworkRoute& network_route) override;
Oleh Prypin37cf2452018-10-14 19:44:29 +0000156 void SetInterface(NetworkInterface* iface) override;
eladalonf1841382017-06-12 01:16:46 -0700157
158 // Implemented for VideoMediaChannelTest.
159 bool sending() const { return sending_; }
160
Danil Chapovalov00c71832018-06-15 15:58:38 +0200161 absl::optional<uint32_t> GetDefaultReceiveStreamSsrc();
eladalonf1841382017-06-12 01:16:46 -0700162
Seth Hampson5897a6e2018-04-03 11:16:33 -0700163 StreamParams unsignaled_stream_params() { return unsignaled_stream_params_; }
164
eladalonf1841382017-06-12 01:16:46 -0700165 // AdaptReason is used for expressing why a WebRtcVideoSendStream request
166 // a lower input frame size than the currently configured camera input frame
167 // size. There can be more than one reason OR:ed together.
168 enum AdaptReason {
169 ADAPTREASON_NONE = 0,
170 ADAPTREASON_CPU = 1,
171 ADAPTREASON_BANDWIDTH = 2,
172 };
173
sprang67561a62017-06-15 06:34:42 -0700174 static constexpr int kDefaultQpMax = 56;
175
Jonas Oreland49ac5952018-09-26 16:04:32 +0200176 std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const override;
177
eladalonf1841382017-06-12 01:16:46 -0700178 private:
179 class WebRtcVideoReceiveStream;
180 struct VideoCodecSettings {
181 VideoCodecSettings();
182
183 // Checks if all members of |*this| are equal to the corresponding members
184 // of |other|.
185 bool operator==(const VideoCodecSettings& other) const;
186 bool operator!=(const VideoCodecSettings& other) const;
187
188 // Checks if all members of |a|, except |flexfec_payload_type|, are equal
189 // to the corresponding members of |b|.
190 static bool EqualsDisregardingFlexfec(const VideoCodecSettings& a,
191 const VideoCodecSettings& b);
192
193 VideoCodec codec;
194 webrtc::UlpfecConfig ulpfec;
195 int flexfec_payload_type;
196 int rtx_payload_type;
197 };
198
199 struct ChangedSendParameters {
200 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200201 absl::optional<VideoCodecSettings> codec;
202 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
203 absl::optional<std::string> mid;
204 absl::optional<int> max_bandwidth_bps;
205 absl::optional<bool> conference_mode;
206 absl::optional<webrtc::RtcpMode> rtcp_mode;
eladalonf1841382017-06-12 01:16:46 -0700207 };
208
209 struct ChangedRecvParameters {
210 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200211 absl::optional<std::vector<VideoCodecSettings>> codec_settings;
212 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
eladalonf1841382017-06-12 01:16:46 -0700213 // Keep track of the FlexFEC payload type separately from |codec_settings|.
214 // This allows us to recreate the FlexfecReceiveStream separately from the
215 // VideoReceiveStream when the FlexFEC payload type is changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200216 absl::optional<int> flexfec_payload_type;
eladalonf1841382017-06-12 01:16:46 -0700217 };
218
219 bool GetChangedSendParameters(const VideoSendParameters& params,
220 ChangedSendParameters* changed_params) const;
221 bool GetChangedRecvParameters(const VideoRecvParameters& params,
222 ChangedRecvParameters* changed_params) const;
223
224 void SetMaxSendBandwidth(int bps);
225
226 void ConfigureReceiverRtp(
227 webrtc::VideoReceiveStream::Config* config,
228 webrtc::FlexfecReceiveStream::Config* flexfec_config,
229 const StreamParams& sp) const;
230 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
danilchapa37de392017-09-09 04:17:22 -0700231 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700232 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
danilchapa37de392017-09-09 04:17:22 -0700233 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700234 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
danilchapa37de392017-09-09 04:17:22 -0700235 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700236
237 static std::string CodecSettingsVectorToString(
238 const std::vector<VideoCodecSettings>& codecs);
239
240 // Wrapper for the sender part.
241 class WebRtcVideoSendStream
242 : public rtc::VideoSourceInterface<webrtc::VideoFrame> {
243 public:
244 WebRtcVideoSendStream(
245 webrtc::Call* call,
246 const StreamParams& sp,
247 webrtc::VideoSendStream::Config config,
248 const VideoOptions& options,
eladalonf1841382017-06-12 01:16:46 -0700249 bool enable_cpu_overuse_detection,
250 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200251 const absl::optional<VideoCodecSettings>& codec_settings,
252 const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions,
eladalonf1841382017-06-12 01:16:46 -0700253 const VideoSendParameters& send_params);
254 virtual ~WebRtcVideoSendStream();
255
256 void SetSendParameters(const ChangedSendParameters& send_params);
Zach Steinba37b4b2018-01-23 15:02:36 -0800257 webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters);
eladalonf1841382017-06-12 01:16:46 -0700258 webrtc::RtpParameters GetRtpParameters() const;
259
260 // Implements rtc::VideoSourceInterface<webrtc::VideoFrame>.
261 // WebRtcVideoSendStream acts as a source to the webrtc::VideoSendStream
262 // in |stream_|. This is done to proxy VideoSinkWants from the encoder to
263 // the worker thread.
264 void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
265 const rtc::VideoSinkWants& wants) override;
266 void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
267
Niels Möllerff40b142018-04-09 08:49:14 +0200268 bool SetVideoSend(const VideoOptions* options,
eladalonf1841382017-06-12 01:16:46 -0700269 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
270
271 void SetSend(bool send);
272
273 const std::vector<uint32_t>& GetSsrcs() const;
274 VideoSenderInfo GetVideoSenderInfo(bool log_stats);
275 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
276
277 private:
278 // Parameters needed to reconstruct the underlying stream.
279 // webrtc::VideoSendStream doesn't support setting a lot of options on the
280 // fly, so when those need to be changed we tear down and reconstruct with
281 // similar parameters depending on which options changed etc.
282 struct VideoSendStreamParameters {
283 VideoSendStreamParameters(
284 webrtc::VideoSendStream::Config config,
285 const VideoOptions& options,
286 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200287 const absl::optional<VideoCodecSettings>& codec_settings);
eladalonf1841382017-06-12 01:16:46 -0700288 webrtc::VideoSendStream::Config config;
289 VideoOptions options;
290 int max_bitrate_bps;
291 bool conference_mode;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200292 absl::optional<VideoCodecSettings> codec_settings;
eladalonf1841382017-06-12 01:16:46 -0700293 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
294 // typically changes when setting a new resolution or reconfiguring
295 // bitrates.
296 webrtc::VideoEncoderConfig encoder_config;
297 };
298
eladalonf1841382017-06-12 01:16:46 -0700299 rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
300 ConfigureVideoEncoderSettings(const VideoCodec& codec);
Niels Möller5bf8ccd2018-03-15 14:16:11 +0100301 void SetCodec(const VideoCodecSettings& codec);
eladalonf1841382017-06-12 01:16:46 -0700302 void RecreateWebRtcStream();
303 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
304 const VideoCodec& codec) const;
305 void ReconfigureEncoder();
eladalonf1841382017-06-12 01:16:46 -0700306
307 // Calls Start or Stop according to whether or not |sending_| is true,
308 // and whether or not the encoding in |rtp_parameters_| is active.
309 void UpdateSendState();
310
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700311 webrtc::DegradationPreference GetDegradationPreference() const
312 RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700313
314 rtc::ThreadChecker thread_checker_;
315 rtc::AsyncInvoker invoker_;
316 rtc::Thread* worker_thread_;
Niels Möller1e062892018-02-07 10:18:32 +0100317 const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_);
318 const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700319 webrtc::Call* const call_;
320 const bool enable_cpu_overuse_detection_;
321 rtc::VideoSourceInterface<webrtc::VideoFrame>* source_
Niels Möller1e062892018-02-07 10:18:32 +0100322 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700323
Niels Möller1e062892018-02-07 10:18:32 +0100324 webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700325 rtc::VideoSinkInterface<webrtc::VideoFrame>* encoder_sink_
Niels Möller1e062892018-02-07 10:18:32 +0100326 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700327 // Contains settings that are the same for all streams in the MediaChannel,
328 // such as codecs, header extensions, and the global bitrate limit for the
329 // entire channel.
Niels Möller1e062892018-02-07 10:18:32 +0100330 VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700331 // Contains settings that are unique for each stream, such as max_bitrate.
332 // Does *not* contain codecs, however.
333 // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_.
334 // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only
335 // one stream per MediaChannel.
Niels Möller1e062892018-02-07 10:18:32 +0100336 webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700337
Niels Möller1e062892018-02-07 10:18:32 +0100338 bool sending_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700339 };
340
341 // Wrapper for the receiver part, contains configs etc. that are needed to
342 // reconstruct the underlying VideoReceiveStream.
343 class WebRtcVideoReceiveStream
344 : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
345 public:
346 WebRtcVideoReceiveStream(
347 webrtc::Call* call,
348 const StreamParams& sp,
349 webrtc::VideoReceiveStream::Config config,
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200350 webrtc::VideoDecoderFactory* decoder_factory,
eladalonf1841382017-06-12 01:16:46 -0700351 bool default_stream,
352 const std::vector<VideoCodecSettings>& recv_codecs,
353 const webrtc::FlexfecReceiveStream::Config& flexfec_config);
354 ~WebRtcVideoReceiveStream();
355
356 const std::vector<uint32_t>& GetSsrcs() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200357
Jonas Oreland49ac5952018-09-26 16:04:32 +0200358 std::vector<webrtc::RtpSource> GetSources();
359
Florent Castelliabe301f2018-06-12 18:33:49 +0200360 // Does not return codecs, they are filled by the owning WebRtcVideoChannel.
361 webrtc::RtpParameters GetRtpParameters() const;
eladalonf1841382017-06-12 01:16:46 -0700362
363 void SetLocalSsrc(uint32_t local_ssrc);
364 // TODO(deadbeef): Move these feedback parameters into the recv parameters.
365 void SetFeedbackParameters(bool nack_enabled,
366 bool remb_enabled,
367 bool transport_cc_enabled,
368 webrtc::RtcpMode rtcp_mode);
369 void SetRecvParameters(const ChangedRecvParameters& recv_params);
370
371 void OnFrame(const webrtc::VideoFrame& frame) override;
372 bool IsDefaultStream() const;
373
374 void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
375
376 VideoReceiverInfo GetVideoReceiverInfo(bool log_stats);
377
378 private:
eladalonf1841382017-06-12 01:16:46 -0700379 void RecreateWebRtcVideoStream();
380 void MaybeRecreateWebRtcFlexfecStream();
381
eladalonc0d481a2017-08-02 07:39:07 -0700382 void MaybeAssociateFlexfecWithVideo();
383 void MaybeDissociateFlexfecFromVideo();
384
Niels Möllercbcbc222018-09-28 09:07:24 +0200385 void ConfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
eladalonf1841382017-06-12 01:16:46 -0700386 void ConfigureFlexfecCodec(int flexfec_payload_type);
eladalonf1841382017-06-12 01:16:46 -0700387
388 std::string GetCodecNameFromPayloadType(int payload_type);
389
Danil Chapovalov00c71832018-06-15 15:58:38 +0200390 absl::optional<uint32_t> GetFirstPrimarySsrc() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200391
eladalonf1841382017-06-12 01:16:46 -0700392 webrtc::Call* const call_;
Niels Möllercbcbc222018-09-28 09:07:24 +0200393 const StreamParams stream_params_;
eladalonf1841382017-06-12 01:16:46 -0700394
395 // Both |stream_| and |flexfec_stream_| are managed by |this|. They are
396 // destroyed by calling call_->DestroyVideoReceiveStream and
397 // call_->DestroyFlexfecReceiveStream, respectively.
398 webrtc::VideoReceiveStream* stream_;
399 const bool default_stream_;
400 webrtc::VideoReceiveStream::Config config_;
401 webrtc::FlexfecReceiveStream::Config flexfec_config_;
402 webrtc::FlexfecReceiveStream* flexfec_stream_;
403
Niels Möllercbcbc222018-09-28 09:07:24 +0200404 webrtc::VideoDecoderFactory* const decoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700405
406 rtc::CriticalSection sink_lock_;
danilchapa37de392017-09-09 04:17:22 -0700407 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_
408 RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700409 // Expands remote RTP timestamps to int64_t to be able to estimate how long
410 // the stream has been running.
411 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
danilchapa37de392017-09-09 04:17:22 -0700412 RTC_GUARDED_BY(sink_lock_);
413 int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700414 // Start NTP time is estimated as current remote NTP time (estimated from
415 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
danilchapa37de392017-09-09 04:17:22 -0700416 int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700417 };
418
419 void Construct(webrtc::Call* call, WebRtcVideoEngine* engine);
420
421 bool SendRtp(const uint8_t* data,
422 size_t len,
423 const webrtc::PacketOptions& options) override;
424 bool SendRtcp(const uint8_t* data, size_t len) override;
425
426 static std::vector<VideoCodecSettings> MapCodecs(
427 const std::vector<VideoCodec>& codecs);
428 // Select what video codec will be used for sending, i.e. what codec is used
429 // for local encoding, based on supported remote codecs. The first remote
430 // codec that is supported locally will be selected.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200431 absl::optional<VideoCodecSettings> SelectSendVideoCodec(
eladalonf1841382017-06-12 01:16:46 -0700432 const std::vector<VideoCodecSettings>& remote_mapped_codecs) const;
433
434 static bool NonFlexfecReceiveCodecsHaveChanged(
435 std::vector<VideoCodecSettings> before,
436 std::vector<VideoCodecSettings> after);
437
438 void FillSenderStats(VideoMediaInfo* info, bool log_stats);
439 void FillReceiverStats(VideoMediaInfo* info, bool log_stats);
440 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
441 VideoMediaInfo* info);
442 void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info);
443
444 rtc::ThreadChecker thread_checker_;
445
446 uint32_t rtcp_receiver_report_ssrc_;
447 bool sending_;
448 webrtc::Call* const call_;
449
450 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
451 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
452
453 const MediaConfig::Video video_config_;
454
455 rtc::CriticalSection stream_crit_;
456 // Using primary-ssrc (first ssrc) as key.
457 std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
danilchapa37de392017-09-09 04:17:22 -0700458 RTC_GUARDED_BY(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700459 std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700460 RTC_GUARDED_BY(stream_crit_);
461 std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(stream_crit_);
462 std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700463
Danil Chapovalov00c71832018-06-15 15:58:38 +0200464 absl::optional<VideoCodecSettings> send_codec_;
465 absl::optional<std::vector<webrtc::RtpExtension>> send_rtp_extensions_;
eladalonf1841382017-06-12 01:16:46 -0700466
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100467 webrtc::VideoEncoderFactory* const encoder_factory_;
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200468 webrtc::VideoDecoderFactory* const decoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700469 std::vector<VideoCodecSettings> recv_codecs_;
470 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
471 // See reason for keeping track of the FlexFEC payload type separately in
472 // comment in WebRtcVideoChannel::ChangedRecvParameters.
473 int recv_flexfec_payload_type_;
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +0100474 webrtc::BitrateConstraints bitrate_config_;
eladalonf1841382017-06-12 01:16:46 -0700475 // TODO(deadbeef): Don't duplicate information between
476 // send_params/recv_params, rtp_extensions, options, etc.
477 VideoSendParameters send_params_;
478 VideoOptions default_send_options_;
479 VideoRecvParameters recv_params_;
480 int64_t last_stats_log_ms_;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700481 // This is a stream param that comes from the remote description, but wasn't
482 // signaled with any a=ssrc lines. It holds information that was signaled
483 // before the unsignaled receive stream is created when the first packet is
484 // received.
485 StreamParams unsignaled_stream_params_;
eladalonf1841382017-06-12 01:16:46 -0700486};
487
ilnik6b826ef2017-06-16 06:53:48 -0700488class EncoderStreamFactory
489 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
490 public:
491 EncoderStreamFactory(std::string codec_name,
492 int max_qp,
Seth Hampson1370e302018-02-07 08:50:36 -0800493 bool is_screenshare,
494 bool screenshare_config_explicitly_enabled);
ilnik6b826ef2017-06-16 06:53:48 -0700495
496 private:
497 std::vector<webrtc::VideoStream> CreateEncoderStreams(
498 int width,
499 int height,
500 const webrtc::VideoEncoderConfig& encoder_config) override;
501
502 const std::string codec_name_;
503 const int max_qp_;
Seth Hampson1370e302018-02-07 08:50:36 -0800504 const bool is_screenshare_;
505 // Allows a screenshare specific configuration, which enables temporal
506 // layering and allows simulcast.
507 const bool screenshare_config_explicitly_enabled_;
ilnik6b826ef2017-06-16 06:53:48 -0700508};
509
eladalonf1841382017-06-12 01:16:46 -0700510} // namespace cricket
511
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200512#endif // MEDIA_ENGINE_WEBRTCVIDEOENGINE_H_