blob: d949d786b78b609e59ae2cdf5716ddbaaafa7767 [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
eladalonf1841382017-06-12 01:16:46 -070053class WebRtcVideoChannel;
eladalonf1841382017-06-12 01:16:46 -070054
eladalonf1841382017-06-12 01:16:46 -070055class UnsignalledSsrcHandler {
56 public:
57 enum Action {
58 kDropPacket,
59 kDeliverPacket,
60 };
61 virtual Action OnUnsignalledSsrc(WebRtcVideoChannel* channel,
62 uint32_t ssrc) = 0;
63 virtual ~UnsignalledSsrcHandler() = default;
64};
65
66// TODO(pbos): Remove, use external handlers only.
67class DefaultUnsignalledSsrcHandler : public UnsignalledSsrcHandler {
68 public:
69 DefaultUnsignalledSsrcHandler();
Yves Gerey665174f2018-06-19 15:03:05 +020070 Action OnUnsignalledSsrc(WebRtcVideoChannel* channel, uint32_t ssrc) override;
eladalonf1841382017-06-12 01:16:46 -070071
72 rtc::VideoSinkInterface<webrtc::VideoFrame>* GetDefaultSink() const;
73 void SetDefaultSink(WebRtcVideoChannel* channel,
74 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
75
76 virtual ~DefaultUnsignalledSsrcHandler() = default;
77
78 private:
79 rtc::VideoSinkInterface<webrtc::VideoFrame>* default_sink_;
80};
81
82// WebRtcVideoEngine is used for the new native WebRTC Video API (webrtc:1667).
83class WebRtcVideoEngine {
84 public:
Anders Carlssondd8c1652018-01-30 10:32:13 +010085#if defined(USE_BUILTIN_SW_CODECS)
Magnus Jedvert02e7a192017-09-23 17:21:32 +020086 // Internal SW video codecs will be added on top of the external codecs.
87 WebRtcVideoEngine(
88 std::unique_ptr<WebRtcVideoEncoderFactory> external_video_encoder_factory,
89 std::unique_ptr<WebRtcVideoDecoderFactory>
90 external_video_decoder_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,
97 std::unique_ptr<webrtc::VideoDecoderFactory> video_decoder_factory);
98
eladalonf1841382017-06-12 01:16:46 -070099 virtual ~WebRtcVideoEngine();
100
eladalonf1841382017-06-12 01:16:46 -0700101 WebRtcVideoChannel* CreateChannel(webrtc::Call* call,
102 const MediaConfig& config,
103 const VideoOptions& options);
104
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:
115 WebRtcVideoChannel(webrtc::Call* call,
116 const MediaConfig& config,
117 const VideoOptions& options,
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100118 webrtc::VideoEncoderFactory* encoder_factory,
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200119 webrtc::VideoDecoderFactory* decoder_factory);
eladalonf1841382017-06-12 01:16:46 -0700120 ~WebRtcVideoChannel() override;
121
122 // VideoMediaChannel implementation
123 rtc::DiffServCodePoint PreferredDscp() const override;
124
125 bool SetSendParameters(const VideoSendParameters& params) override;
126 bool SetRecvParameters(const VideoRecvParameters& params) override;
127 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800128 webrtc::RTCError SetRtpSendParameters(
129 uint32_t ssrc,
130 const webrtc::RtpParameters& parameters) override;
eladalonf1841382017-06-12 01:16:46 -0700131 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const override;
132 bool SetRtpReceiveParameters(
133 uint32_t ssrc,
134 const webrtc::RtpParameters& parameters) override;
135 bool GetSendCodec(VideoCodec* send_codec) override;
136 bool SetSend(bool send) override;
137 bool SetVideoSend(
138 uint32_t ssrc,
eladalonf1841382017-06-12 01:16:46 -0700139 const VideoOptions* options,
140 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) override;
141 bool AddSendStream(const StreamParams& sp) override;
142 bool RemoveSendStream(uint32_t ssrc) override;
143 bool AddRecvStream(const StreamParams& sp) override;
144 bool AddRecvStream(const StreamParams& sp, bool default_stream);
145 bool RemoveRecvStream(uint32_t ssrc) override;
146 bool SetSink(uint32_t ssrc,
147 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
148 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) override;
149 bool GetStats(VideoMediaInfo* info) override;
150
151 void OnPacketReceived(rtc::CopyOnWriteBuffer* packet,
152 const rtc::PacketTime& packet_time) override;
153 void OnRtcpReceived(rtc::CopyOnWriteBuffer* packet,
154 const rtc::PacketTime& packet_time) override;
155 void OnReadyToSend(bool ready) override;
156 void OnNetworkRouteChanged(const std::string& transport_name,
157 const rtc::NetworkRoute& network_route) override;
eladalonf1841382017-06-12 01:16:46 -0700158 void SetInterface(NetworkInterface* iface) override;
159
160 // Implemented for VideoMediaChannelTest.
161 bool sending() const { return sending_; }
162
Danil Chapovalov00c71832018-06-15 15:58:38 +0200163 absl::optional<uint32_t> GetDefaultReceiveStreamSsrc();
eladalonf1841382017-06-12 01:16:46 -0700164
Seth Hampson5897a6e2018-04-03 11:16:33 -0700165 StreamParams unsignaled_stream_params() { return unsignaled_stream_params_; }
166
eladalonf1841382017-06-12 01:16:46 -0700167 // AdaptReason is used for expressing why a WebRtcVideoSendStream request
168 // a lower input frame size than the currently configured camera input frame
169 // size. There can be more than one reason OR:ed together.
170 enum AdaptReason {
171 ADAPTREASON_NONE = 0,
172 ADAPTREASON_CPU = 1,
173 ADAPTREASON_BANDWIDTH = 2,
174 };
175
sprang67561a62017-06-15 06:34:42 -0700176 static constexpr int kDefaultQpMax = 56;
177
Jonas Oreland49ac5952018-09-26 16:04:32 +0200178 std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const override;
179
eladalonf1841382017-06-12 01:16:46 -0700180 private:
181 class WebRtcVideoReceiveStream;
182 struct VideoCodecSettings {
183 VideoCodecSettings();
184
185 // Checks if all members of |*this| are equal to the corresponding members
186 // of |other|.
187 bool operator==(const VideoCodecSettings& other) const;
188 bool operator!=(const VideoCodecSettings& other) const;
189
190 // Checks if all members of |a|, except |flexfec_payload_type|, are equal
191 // to the corresponding members of |b|.
192 static bool EqualsDisregardingFlexfec(const VideoCodecSettings& a,
193 const VideoCodecSettings& b);
194
195 VideoCodec codec;
196 webrtc::UlpfecConfig ulpfec;
197 int flexfec_payload_type;
198 int rtx_payload_type;
199 };
200
201 struct ChangedSendParameters {
202 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200203 absl::optional<VideoCodecSettings> codec;
204 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
205 absl::optional<std::string> mid;
206 absl::optional<int> max_bandwidth_bps;
207 absl::optional<bool> conference_mode;
208 absl::optional<webrtc::RtcpMode> rtcp_mode;
eladalonf1841382017-06-12 01:16:46 -0700209 };
210
211 struct ChangedRecvParameters {
212 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200213 absl::optional<std::vector<VideoCodecSettings>> codec_settings;
214 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
eladalonf1841382017-06-12 01:16:46 -0700215 // Keep track of the FlexFEC payload type separately from |codec_settings|.
216 // This allows us to recreate the FlexfecReceiveStream separately from the
217 // VideoReceiveStream when the FlexFEC payload type is changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200218 absl::optional<int> flexfec_payload_type;
eladalonf1841382017-06-12 01:16:46 -0700219 };
220
221 bool GetChangedSendParameters(const VideoSendParameters& params,
222 ChangedSendParameters* changed_params) const;
223 bool GetChangedRecvParameters(const VideoRecvParameters& params,
224 ChangedRecvParameters* changed_params) const;
225
226 void SetMaxSendBandwidth(int bps);
227
228 void ConfigureReceiverRtp(
229 webrtc::VideoReceiveStream::Config* config,
230 webrtc::FlexfecReceiveStream::Config* flexfec_config,
231 const StreamParams& sp) const;
232 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
danilchapa37de392017-09-09 04:17:22 -0700233 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700234 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
danilchapa37de392017-09-09 04:17:22 -0700235 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700236 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
danilchapa37de392017-09-09 04:17:22 -0700237 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700238
239 static std::string CodecSettingsVectorToString(
240 const std::vector<VideoCodecSettings>& codecs);
241
242 // Wrapper for the sender part.
243 class WebRtcVideoSendStream
244 : public rtc::VideoSourceInterface<webrtc::VideoFrame> {
245 public:
246 WebRtcVideoSendStream(
247 webrtc::Call* call,
248 const StreamParams& sp,
249 webrtc::VideoSendStream::Config config,
250 const VideoOptions& options,
eladalonf1841382017-06-12 01:16:46 -0700251 bool enable_cpu_overuse_detection,
252 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200253 const absl::optional<VideoCodecSettings>& codec_settings,
254 const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions,
eladalonf1841382017-06-12 01:16:46 -0700255 const VideoSendParameters& send_params);
256 virtual ~WebRtcVideoSendStream();
257
258 void SetSendParameters(const ChangedSendParameters& send_params);
Zach Steinba37b4b2018-01-23 15:02:36 -0800259 webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters);
eladalonf1841382017-06-12 01:16:46 -0700260 webrtc::RtpParameters GetRtpParameters() const;
261
262 // Implements rtc::VideoSourceInterface<webrtc::VideoFrame>.
263 // WebRtcVideoSendStream acts as a source to the webrtc::VideoSendStream
264 // in |stream_|. This is done to proxy VideoSinkWants from the encoder to
265 // the worker thread.
266 void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
267 const rtc::VideoSinkWants& wants) override;
268 void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
269
Niels Möllerff40b142018-04-09 08:49:14 +0200270 bool SetVideoSend(const VideoOptions* options,
eladalonf1841382017-06-12 01:16:46 -0700271 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
272
273 void SetSend(bool send);
274
275 const std::vector<uint32_t>& GetSsrcs() const;
276 VideoSenderInfo GetVideoSenderInfo(bool log_stats);
277 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
278
279 private:
280 // Parameters needed to reconstruct the underlying stream.
281 // webrtc::VideoSendStream doesn't support setting a lot of options on the
282 // fly, so when those need to be changed we tear down and reconstruct with
283 // similar parameters depending on which options changed etc.
284 struct VideoSendStreamParameters {
285 VideoSendStreamParameters(
286 webrtc::VideoSendStream::Config config,
287 const VideoOptions& options,
288 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200289 const absl::optional<VideoCodecSettings>& codec_settings);
eladalonf1841382017-06-12 01:16:46 -0700290 webrtc::VideoSendStream::Config config;
291 VideoOptions options;
292 int max_bitrate_bps;
293 bool conference_mode;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200294 absl::optional<VideoCodecSettings> codec_settings;
eladalonf1841382017-06-12 01:16:46 -0700295 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
296 // typically changes when setting a new resolution or reconfiguring
297 // bitrates.
298 webrtc::VideoEncoderConfig encoder_config;
299 };
300
eladalonf1841382017-06-12 01:16:46 -0700301 rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
302 ConfigureVideoEncoderSettings(const VideoCodec& codec);
Niels Möller5bf8ccd2018-03-15 14:16:11 +0100303 void SetCodec(const VideoCodecSettings& codec);
eladalonf1841382017-06-12 01:16:46 -0700304 void RecreateWebRtcStream();
305 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
306 const VideoCodec& codec) const;
307 void ReconfigureEncoder();
Zach Steinba37b4b2018-01-23 15:02:36 -0800308 webrtc::RTCError ValidateRtpParameters(
309 const webrtc::RtpParameters& parameters);
eladalonf1841382017-06-12 01:16:46 -0700310
311 // Calls Start or Stop according to whether or not |sending_| is true,
312 // and whether or not the encoding in |rtp_parameters_| is active.
313 void UpdateSendState();
314
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700315 webrtc::DegradationPreference GetDegradationPreference() const
316 RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700317
318 rtc::ThreadChecker thread_checker_;
319 rtc::AsyncInvoker invoker_;
320 rtc::Thread* worker_thread_;
Niels Möller1e062892018-02-07 10:18:32 +0100321 const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_);
322 const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700323 webrtc::Call* const call_;
324 const bool enable_cpu_overuse_detection_;
325 rtc::VideoSourceInterface<webrtc::VideoFrame>* source_
Niels Möller1e062892018-02-07 10:18:32 +0100326 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700327
Niels Möller1e062892018-02-07 10:18:32 +0100328 webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700329 rtc::VideoSinkInterface<webrtc::VideoFrame>* encoder_sink_
Niels Möller1e062892018-02-07 10:18:32 +0100330 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700331 // Contains settings that are the same for all streams in the MediaChannel,
332 // such as codecs, header extensions, and the global bitrate limit for the
333 // entire channel.
Niels Möller1e062892018-02-07 10:18:32 +0100334 VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700335 // Contains settings that are unique for each stream, such as max_bitrate.
336 // Does *not* contain codecs, however.
337 // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_.
338 // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only
339 // one stream per MediaChannel.
Niels Möller1e062892018-02-07 10:18:32 +0100340 webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700341
Niels Möller1e062892018-02-07 10:18:32 +0100342 bool sending_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700343 };
344
345 // Wrapper for the receiver part, contains configs etc. that are needed to
346 // reconstruct the underlying VideoReceiveStream.
347 class WebRtcVideoReceiveStream
348 : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
349 public:
350 WebRtcVideoReceiveStream(
351 webrtc::Call* call,
352 const StreamParams& sp,
353 webrtc::VideoReceiveStream::Config config,
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200354 webrtc::VideoDecoderFactory* decoder_factory,
eladalonf1841382017-06-12 01:16:46 -0700355 bool default_stream,
356 const std::vector<VideoCodecSettings>& recv_codecs,
357 const webrtc::FlexfecReceiveStream::Config& flexfec_config);
358 ~WebRtcVideoReceiveStream();
359
360 const std::vector<uint32_t>& GetSsrcs() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200361
Jonas Oreland49ac5952018-09-26 16:04:32 +0200362 std::vector<webrtc::RtpSource> GetSources();
363
Florent Castelliabe301f2018-06-12 18:33:49 +0200364 // Does not return codecs, they are filled by the owning WebRtcVideoChannel.
365 webrtc::RtpParameters GetRtpParameters() const;
eladalonf1841382017-06-12 01:16:46 -0700366
367 void SetLocalSsrc(uint32_t local_ssrc);
368 // TODO(deadbeef): Move these feedback parameters into the recv parameters.
369 void SetFeedbackParameters(bool nack_enabled,
370 bool remb_enabled,
371 bool transport_cc_enabled,
372 webrtc::RtcpMode rtcp_mode);
373 void SetRecvParameters(const ChangedRecvParameters& recv_params);
374
375 void OnFrame(const webrtc::VideoFrame& frame) override;
376 bool IsDefaultStream() const;
377
378 void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
379
380 VideoReceiverInfo GetVideoReceiverInfo(bool log_stats);
381
382 private:
andersc063f0c02017-09-11 11:50:51 -0700383 struct SdpVideoFormatCompare {
384 bool operator()(const webrtc::SdpVideoFormat& lhs,
385 const webrtc::SdpVideoFormat& rhs) const {
386 return std::tie(lhs.name, lhs.parameters) <
387 std::tie(rhs.name, rhs.parameters);
388 }
perkj1f885312017-09-04 02:43:10 -0700389 };
andersc063f0c02017-09-11 11:50:51 -0700390 typedef std::map<webrtc::SdpVideoFormat,
391 std::unique_ptr<webrtc::VideoDecoder>,
392 SdpVideoFormatCompare>
393 DecoderMap;
perkj1f885312017-09-04 02:43:10 -0700394
eladalonf1841382017-06-12 01:16:46 -0700395 void RecreateWebRtcVideoStream();
396 void MaybeRecreateWebRtcFlexfecStream();
397
eladalonc0d481a2017-08-02 07:39:07 -0700398 void MaybeAssociateFlexfecWithVideo();
399 void MaybeDissociateFlexfecFromVideo();
400
perkj1f885312017-09-04 02:43:10 -0700401 void ConfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs,
andersc063f0c02017-09-11 11:50:51 -0700402 DecoderMap* old_codecs);
eladalonf1841382017-06-12 01:16:46 -0700403 void ConfigureFlexfecCodec(int flexfec_payload_type);
eladalonf1841382017-06-12 01:16:46 -0700404
405 std::string GetCodecNameFromPayloadType(int payload_type);
406
Danil Chapovalov00c71832018-06-15 15:58:38 +0200407 absl::optional<uint32_t> GetFirstPrimarySsrc() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200408
eladalonf1841382017-06-12 01:16:46 -0700409 webrtc::Call* const call_;
410 StreamParams stream_params_;
411
412 // Both |stream_| and |flexfec_stream_| are managed by |this|. They are
413 // destroyed by calling call_->DestroyVideoReceiveStream and
414 // call_->DestroyFlexfecReceiveStream, respectively.
415 webrtc::VideoReceiveStream* stream_;
416 const bool default_stream_;
417 webrtc::VideoReceiveStream::Config config_;
418 webrtc::FlexfecReceiveStream::Config flexfec_config_;
419 webrtc::FlexfecReceiveStream* flexfec_stream_;
420
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200421 webrtc::VideoDecoderFactory* decoder_factory_;
andersc063f0c02017-09-11 11:50:51 -0700422 DecoderMap allocated_decoders_;
eladalonf1841382017-06-12 01:16:46 -0700423
424 rtc::CriticalSection sink_lock_;
danilchapa37de392017-09-09 04:17:22 -0700425 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_
426 RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700427 // Expands remote RTP timestamps to int64_t to be able to estimate how long
428 // the stream has been running.
429 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
danilchapa37de392017-09-09 04:17:22 -0700430 RTC_GUARDED_BY(sink_lock_);
431 int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700432 // Start NTP time is estimated as current remote NTP time (estimated from
433 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
danilchapa37de392017-09-09 04:17:22 -0700434 int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700435 };
436
437 void Construct(webrtc::Call* call, WebRtcVideoEngine* engine);
438
439 bool SendRtp(const uint8_t* data,
440 size_t len,
441 const webrtc::PacketOptions& options) override;
442 bool SendRtcp(const uint8_t* data, size_t len) override;
443
444 static std::vector<VideoCodecSettings> MapCodecs(
445 const std::vector<VideoCodec>& codecs);
446 // Select what video codec will be used for sending, i.e. what codec is used
447 // for local encoding, based on supported remote codecs. The first remote
448 // codec that is supported locally will be selected.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200449 absl::optional<VideoCodecSettings> SelectSendVideoCodec(
eladalonf1841382017-06-12 01:16:46 -0700450 const std::vector<VideoCodecSettings>& remote_mapped_codecs) const;
451
452 static bool NonFlexfecReceiveCodecsHaveChanged(
453 std::vector<VideoCodecSettings> before,
454 std::vector<VideoCodecSettings> after);
455
456 void FillSenderStats(VideoMediaInfo* info, bool log_stats);
457 void FillReceiverStats(VideoMediaInfo* info, bool log_stats);
458 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
459 VideoMediaInfo* info);
460 void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info);
461
462 rtc::ThreadChecker thread_checker_;
463
464 uint32_t rtcp_receiver_report_ssrc_;
465 bool sending_;
466 webrtc::Call* const call_;
467
468 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
469 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
470
471 const MediaConfig::Video video_config_;
472
473 rtc::CriticalSection stream_crit_;
474 // Using primary-ssrc (first ssrc) as key.
475 std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
danilchapa37de392017-09-09 04:17:22 -0700476 RTC_GUARDED_BY(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700477 std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700478 RTC_GUARDED_BY(stream_crit_);
479 std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(stream_crit_);
480 std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700481
Danil Chapovalov00c71832018-06-15 15:58:38 +0200482 absl::optional<VideoCodecSettings> send_codec_;
483 absl::optional<std::vector<webrtc::RtpExtension>> send_rtp_extensions_;
eladalonf1841382017-06-12 01:16:46 -0700484
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100485 webrtc::VideoEncoderFactory* const encoder_factory_;
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200486 webrtc::VideoDecoderFactory* const decoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700487 std::vector<VideoCodecSettings> recv_codecs_;
488 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
489 // See reason for keeping track of the FlexFEC payload type separately in
490 // comment in WebRtcVideoChannel::ChangedRecvParameters.
491 int recv_flexfec_payload_type_;
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +0100492 webrtc::BitrateConstraints bitrate_config_;
eladalonf1841382017-06-12 01:16:46 -0700493 // TODO(deadbeef): Don't duplicate information between
494 // send_params/recv_params, rtp_extensions, options, etc.
495 VideoSendParameters send_params_;
496 VideoOptions default_send_options_;
497 VideoRecvParameters recv_params_;
498 int64_t last_stats_log_ms_;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700499 // This is a stream param that comes from the remote description, but wasn't
500 // signaled with any a=ssrc lines. It holds information that was signaled
501 // before the unsignaled receive stream is created when the first packet is
502 // received.
503 StreamParams unsignaled_stream_params_;
eladalonf1841382017-06-12 01:16:46 -0700504};
505
ilnik6b826ef2017-06-16 06:53:48 -0700506class EncoderStreamFactory
507 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
508 public:
509 EncoderStreamFactory(std::string codec_name,
510 int max_qp,
Seth Hampson1370e302018-02-07 08:50:36 -0800511 bool is_screenshare,
512 bool screenshare_config_explicitly_enabled);
ilnik6b826ef2017-06-16 06:53:48 -0700513
514 private:
515 std::vector<webrtc::VideoStream> CreateEncoderStreams(
516 int width,
517 int height,
518 const webrtc::VideoEncoderConfig& encoder_config) override;
519
520 const std::string codec_name_;
521 const int max_qp_;
Seth Hampson1370e302018-02-07 08:50:36 -0800522 const bool is_screenshare_;
523 // Allows a screenshare specific configuration, which enables temporal
524 // layering and allows simulcast.
525 const bool screenshare_config_explicitly_enabled_;
ilnik6b826ef2017-06-16 06:53:48 -0700526};
527
eladalonf1841382017-06-12 01:16:46 -0700528} // namespace cricket
529
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200530#endif // MEDIA_ENGINE_WEBRTCVIDEOENGINE_H_