blob: 464c78f43e2986228e61faf248b72490854bccab [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_
12#define MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_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"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "media/base/media_engine.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/async_invoker.h"
33#include "rtc_base/critical_section.h"
34#include "rtc_base/network_route.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "rtc_base/thread_annotations.h"
36#include "rtc_base/thread_checker.h"
eladalonf1841382017-06-12 01:16:46 -070037
38namespace webrtc {
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020039class VideoDecoderFactory;
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020040class VideoEncoderFactory;
eladalonf1841382017-06-12 01:16:46 -070041struct MediaConfig;
Yves Gerey665174f2018-06-19 15:03:05 +020042} // namespace webrtc
eladalonf1841382017-06-12 01:16:46 -070043
44namespace rtc {
45class Thread;
46} // namespace rtc
47
48namespace cricket {
49
eladalonf1841382017-06-12 01:16:46 -070050class WebRtcVideoChannel;
eladalonf1841382017-06-12 01:16:46 -070051
eladalonf1841382017-06-12 01:16:46 -070052class UnsignalledSsrcHandler {
53 public:
54 enum Action {
55 kDropPacket,
56 kDeliverPacket,
57 };
58 virtual Action OnUnsignalledSsrc(WebRtcVideoChannel* channel,
59 uint32_t ssrc) = 0;
60 virtual ~UnsignalledSsrcHandler() = default;
61};
62
63// TODO(pbos): Remove, use external handlers only.
64class DefaultUnsignalledSsrcHandler : public UnsignalledSsrcHandler {
65 public:
66 DefaultUnsignalledSsrcHandler();
Yves Gerey665174f2018-06-19 15:03:05 +020067 Action OnUnsignalledSsrc(WebRtcVideoChannel* channel, uint32_t ssrc) override;
eladalonf1841382017-06-12 01:16:46 -070068
69 rtc::VideoSinkInterface<webrtc::VideoFrame>* GetDefaultSink() const;
70 void SetDefaultSink(WebRtcVideoChannel* channel,
71 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
72
73 virtual ~DefaultUnsignalledSsrcHandler() = default;
74
75 private:
76 rtc::VideoSinkInterface<webrtc::VideoFrame>* default_sink_;
77};
78
79// WebRtcVideoEngine is used for the new native WebRTC Video API (webrtc:1667).
Sebastian Jansson84848f22018-11-16 10:40:36 +010080class WebRtcVideoEngine : public VideoEngineInterface {
eladalonf1841382017-06-12 01:16:46 -070081 public:
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020082 // These video codec factories represents all video codecs, i.e. both software
83 // and external hardware codecs.
84 WebRtcVideoEngine(
85 std::unique_ptr<webrtc::VideoEncoderFactory> video_encoder_factory,
Jiawei Ouc2ebe212018-11-08 10:02:56 -080086 std::unique_ptr<webrtc::VideoDecoderFactory> video_decoder_factory,
87 std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
88 video_bitrate_allocator_factory);
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020089
Sebastian Jansson84848f22018-11-16 10:40:36 +010090 ~WebRtcVideoEngine() override;
eladalonf1841382017-06-12 01:16:46 -070091
Sebastian Jansson84848f22018-11-16 10:40:36 +010092 VideoMediaChannel* CreateMediaChannel(
Benjamin Wrightbfb444c2018-10-15 10:20:24 -070093 webrtc::Call* call,
94 const MediaConfig& config,
95 const VideoOptions& options,
Sebastian Jansson84848f22018-11-16 10:40:36 +010096 const webrtc::CryptoOptions& crypto_options) override;
eladalonf1841382017-06-12 01:16:46 -070097
Sebastian Jansson84848f22018-11-16 10:40:36 +010098 std::vector<VideoCodec> codecs() const override;
99 RtpCapabilities GetCapabilities() const override;
eladalonf1841382017-06-12 01:16:46 -0700100
eladalonf1841382017-06-12 01:16:46 -0700101 private:
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200102 const std::unique_ptr<webrtc::VideoDecoderFactory> decoder_factory_;
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100103 const std::unique_ptr<webrtc::VideoEncoderFactory> encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800104 const std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
105 bitrate_allocator_factory_;
eladalonf1841382017-06-12 01:16:46 -0700106};
107
108class WebRtcVideoChannel : public VideoMediaChannel, public webrtc::Transport {
109 public:
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800110 WebRtcVideoChannel(
111 webrtc::Call* call,
112 const MediaConfig& config,
113 const VideoOptions& options,
114 const webrtc::CryptoOptions& crypto_options,
115 webrtc::VideoEncoderFactory* encoder_factory,
116 webrtc::VideoDecoderFactory* decoder_factory,
117 webrtc::VideoBitrateAllocatorFactory* bitrate_allocator_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,
Niels Möllere6933812018-11-05 13:01:41 +0100150 int64_t packet_time_us) override;
eladalonf1841382017-06-12 01:16:46 -0700151 void OnRtcpReceived(rtc::CopyOnWriteBuffer* packet,
Niels Möllere6933812018-11-05 13:01:41 +0100152 int64_t packet_time_us) override;
eladalonf1841382017-06-12 01:16:46 -0700153 void OnReadyToSend(bool ready) override;
154 void OnNetworkRouteChanged(const std::string& transport_name,
155 const rtc::NetworkRoute& network_route) override;
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700156 void SetInterface(NetworkInterface* iface,
157 webrtc::MediaTransportInterface* media_transport) override;
eladalonf1841382017-06-12 01:16:46 -0700158
Benjamin Wright192eeec2018-10-17 17:27:25 -0700159 // E2E Encrypted Video Frame API
160 // Set a frame decryptor to a particular ssrc that will intercept all
161 // incoming video frames and attempt to decrypt them before forwarding the
162 // result.
163 void SetFrameDecryptor(uint32_t ssrc,
164 rtc::scoped_refptr<webrtc::FrameDecryptorInterface>
165 frame_decryptor) override;
166 // Set a frame encryptor to a particular ssrc that will intercept all
167 // outgoing video frames and attempt to encrypt them and forward the result
168 // to the packetizer.
169 void SetFrameEncryptor(uint32_t ssrc,
170 rtc::scoped_refptr<webrtc::FrameEncryptorInterface>
171 frame_encryptor) override;
172
eladalonf1841382017-06-12 01:16:46 -0700173 // Implemented for VideoMediaChannelTest.
174 bool sending() const { return sending_; }
175
Danil Chapovalov00c71832018-06-15 15:58:38 +0200176 absl::optional<uint32_t> GetDefaultReceiveStreamSsrc();
eladalonf1841382017-06-12 01:16:46 -0700177
Seth Hampson5897a6e2018-04-03 11:16:33 -0700178 StreamParams unsignaled_stream_params() { return unsignaled_stream_params_; }
179
eladalonf1841382017-06-12 01:16:46 -0700180 // AdaptReason is used for expressing why a WebRtcVideoSendStream request
181 // a lower input frame size than the currently configured camera input frame
182 // size. There can be more than one reason OR:ed together.
183 enum AdaptReason {
184 ADAPTREASON_NONE = 0,
185 ADAPTREASON_CPU = 1,
186 ADAPTREASON_BANDWIDTH = 2,
187 };
188
sprang67561a62017-06-15 06:34:42 -0700189 static constexpr int kDefaultQpMax = 56;
190
Jonas Oreland49ac5952018-09-26 16:04:32 +0200191 std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const override;
192
eladalonf1841382017-06-12 01:16:46 -0700193 private:
194 class WebRtcVideoReceiveStream;
195 struct VideoCodecSettings {
196 VideoCodecSettings();
197
198 // Checks if all members of |*this| are equal to the corresponding members
199 // of |other|.
200 bool operator==(const VideoCodecSettings& other) const;
201 bool operator!=(const VideoCodecSettings& other) const;
202
203 // Checks if all members of |a|, except |flexfec_payload_type|, are equal
204 // to the corresponding members of |b|.
205 static bool EqualsDisregardingFlexfec(const VideoCodecSettings& a,
206 const VideoCodecSettings& b);
207
208 VideoCodec codec;
209 webrtc::UlpfecConfig ulpfec;
210 int flexfec_payload_type;
211 int rtx_payload_type;
212 };
213
214 struct ChangedSendParameters {
215 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200216 absl::optional<VideoCodecSettings> codec;
217 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
218 absl::optional<std::string> mid;
Johannes Kron9190b822018-10-29 11:22:05 +0100219 absl::optional<bool> extmap_allow_mixed;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200220 absl::optional<int> max_bandwidth_bps;
221 absl::optional<bool> conference_mode;
222 absl::optional<webrtc::RtcpMode> rtcp_mode;
eladalonf1841382017-06-12 01:16:46 -0700223 };
224
225 struct ChangedRecvParameters {
226 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200227 absl::optional<std::vector<VideoCodecSettings>> codec_settings;
228 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
eladalonf1841382017-06-12 01:16:46 -0700229 // Keep track of the FlexFEC payload type separately from |codec_settings|.
230 // This allows us to recreate the FlexfecReceiveStream separately from the
231 // VideoReceiveStream when the FlexFEC payload type is changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200232 absl::optional<int> flexfec_payload_type;
eladalonf1841382017-06-12 01:16:46 -0700233 };
234
235 bool GetChangedSendParameters(const VideoSendParameters& params,
236 ChangedSendParameters* changed_params) const;
237 bool GetChangedRecvParameters(const VideoRecvParameters& params,
238 ChangedRecvParameters* changed_params) const;
239
240 void SetMaxSendBandwidth(int bps);
241
242 void ConfigureReceiverRtp(
243 webrtc::VideoReceiveStream::Config* config,
244 webrtc::FlexfecReceiveStream::Config* flexfec_config,
245 const StreamParams& sp) const;
246 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
danilchapa37de392017-09-09 04:17:22 -0700247 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700248 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
danilchapa37de392017-09-09 04:17:22 -0700249 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700250 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
danilchapa37de392017-09-09 04:17:22 -0700251 RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700252
253 static std::string CodecSettingsVectorToString(
254 const std::vector<VideoCodecSettings>& codecs);
255
256 // Wrapper for the sender part.
Niels Möllerb66003c2019-02-05 22:41:20 +0100257 class WebRtcVideoSendStream {
eladalonf1841382017-06-12 01:16:46 -0700258 public:
259 WebRtcVideoSendStream(
260 webrtc::Call* call,
261 const StreamParams& sp,
262 webrtc::VideoSendStream::Config config,
263 const VideoOptions& options,
eladalonf1841382017-06-12 01:16:46 -0700264 bool enable_cpu_overuse_detection,
265 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200266 const absl::optional<VideoCodecSettings>& codec_settings,
267 const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions,
eladalonf1841382017-06-12 01:16:46 -0700268 const VideoSendParameters& send_params);
269 virtual ~WebRtcVideoSendStream();
270
271 void SetSendParameters(const ChangedSendParameters& send_params);
Zach Steinba37b4b2018-01-23 15:02:36 -0800272 webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters);
eladalonf1841382017-06-12 01:16:46 -0700273 webrtc::RtpParameters GetRtpParameters() const;
274
Benjamin Wright192eeec2018-10-17 17:27:25 -0700275 void SetFrameEncryptor(
276 rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor);
277
Niels Möllerff40b142018-04-09 08:49:14 +0200278 bool SetVideoSend(const VideoOptions* options,
eladalonf1841382017-06-12 01:16:46 -0700279 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
280
281 void SetSend(bool send);
282
283 const std::vector<uint32_t>& GetSsrcs() const;
284 VideoSenderInfo GetVideoSenderInfo(bool log_stats);
285 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
286
287 private:
288 // Parameters needed to reconstruct the underlying stream.
289 // webrtc::VideoSendStream doesn't support setting a lot of options on the
290 // fly, so when those need to be changed we tear down and reconstruct with
291 // similar parameters depending on which options changed etc.
292 struct VideoSendStreamParameters {
293 VideoSendStreamParameters(
294 webrtc::VideoSendStream::Config config,
295 const VideoOptions& options,
296 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200297 const absl::optional<VideoCodecSettings>& codec_settings);
eladalonf1841382017-06-12 01:16:46 -0700298 webrtc::VideoSendStream::Config config;
299 VideoOptions options;
300 int max_bitrate_bps;
301 bool conference_mode;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200302 absl::optional<VideoCodecSettings> codec_settings;
eladalonf1841382017-06-12 01:16:46 -0700303 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
304 // typically changes when setting a new resolution or reconfiguring
305 // bitrates.
306 webrtc::VideoEncoderConfig encoder_config;
307 };
308
eladalonf1841382017-06-12 01:16:46 -0700309 rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
310 ConfigureVideoEncoderSettings(const VideoCodec& codec);
Niels Möller5bf8ccd2018-03-15 14:16:11 +0100311 void SetCodec(const VideoCodecSettings& codec);
eladalonf1841382017-06-12 01:16:46 -0700312 void RecreateWebRtcStream();
313 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
314 const VideoCodec& codec) const;
315 void ReconfigureEncoder();
eladalonf1841382017-06-12 01:16:46 -0700316
317 // Calls Start or Stop according to whether or not |sending_| is true,
318 // and whether or not the encoding in |rtp_parameters_| is active.
319 void UpdateSendState();
320
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700321 webrtc::DegradationPreference GetDegradationPreference() const
322 RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700323
324 rtc::ThreadChecker thread_checker_;
325 rtc::AsyncInvoker invoker_;
326 rtc::Thread* worker_thread_;
Niels Möller1e062892018-02-07 10:18:32 +0100327 const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_);
328 const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700329 webrtc::Call* const call_;
330 const bool enable_cpu_overuse_detection_;
331 rtc::VideoSourceInterface<webrtc::VideoFrame>* source_
Niels Möller1e062892018-02-07 10:18:32 +0100332 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700333
Niels Möller1e062892018-02-07 10:18:32 +0100334 webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_);
Niels Möllerb66003c2019-02-05 22:41:20 +0100335
eladalonf1841382017-06-12 01:16:46 -0700336 // Contains settings that are the same for all streams in the MediaChannel,
337 // such as codecs, header extensions, and the global bitrate limit for the
338 // entire channel.
Niels Möller1e062892018-02-07 10:18:32 +0100339 VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700340 // Contains settings that are unique for each stream, such as max_bitrate.
341 // Does *not* contain codecs, however.
342 // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_.
343 // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only
344 // one stream per MediaChannel.
Niels Möller1e062892018-02-07 10:18:32 +0100345 webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700346
Niels Möller1e062892018-02-07 10:18:32 +0100347 bool sending_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700348 };
349
350 // Wrapper for the receiver part, contains configs etc. that are needed to
351 // reconstruct the underlying VideoReceiveStream.
352 class WebRtcVideoReceiveStream
353 : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
354 public:
355 WebRtcVideoReceiveStream(
356 webrtc::Call* call,
357 const StreamParams& sp,
358 webrtc::VideoReceiveStream::Config config,
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200359 webrtc::VideoDecoderFactory* decoder_factory,
eladalonf1841382017-06-12 01:16:46 -0700360 bool default_stream,
361 const std::vector<VideoCodecSettings>& recv_codecs,
362 const webrtc::FlexfecReceiveStream::Config& flexfec_config);
363 ~WebRtcVideoReceiveStream();
364
365 const std::vector<uint32_t>& GetSsrcs() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200366
Jonas Oreland49ac5952018-09-26 16:04:32 +0200367 std::vector<webrtc::RtpSource> GetSources();
368
Florent Castelliabe301f2018-06-12 18:33:49 +0200369 // Does not return codecs, they are filled by the owning WebRtcVideoChannel.
370 webrtc::RtpParameters GetRtpParameters() const;
eladalonf1841382017-06-12 01:16:46 -0700371
372 void SetLocalSsrc(uint32_t local_ssrc);
373 // TODO(deadbeef): Move these feedback parameters into the recv parameters.
374 void SetFeedbackParameters(bool nack_enabled,
375 bool remb_enabled,
376 bool transport_cc_enabled,
377 webrtc::RtcpMode rtcp_mode);
378 void SetRecvParameters(const ChangedRecvParameters& recv_params);
379
380 void OnFrame(const webrtc::VideoFrame& frame) override;
381 bool IsDefaultStream() const;
382
Benjamin Wright192eeec2018-10-17 17:27:25 -0700383 void SetFrameDecryptor(
384 rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor);
385
eladalonf1841382017-06-12 01:16:46 -0700386 void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
387
388 VideoReceiverInfo GetVideoReceiverInfo(bool log_stats);
389
390 private:
eladalonf1841382017-06-12 01:16:46 -0700391 void RecreateWebRtcVideoStream();
392 void MaybeRecreateWebRtcFlexfecStream();
393
eladalonc0d481a2017-08-02 07:39:07 -0700394 void MaybeAssociateFlexfecWithVideo();
395 void MaybeDissociateFlexfecFromVideo();
396
Niels Möllercbcbc222018-09-28 09:07:24 +0200397 void ConfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
eladalonf1841382017-06-12 01:16:46 -0700398 void ConfigureFlexfecCodec(int flexfec_payload_type);
eladalonf1841382017-06-12 01:16:46 -0700399
400 std::string GetCodecNameFromPayloadType(int payload_type);
401
402 webrtc::Call* const call_;
Niels Möllercbcbc222018-09-28 09:07:24 +0200403 const StreamParams stream_params_;
eladalonf1841382017-06-12 01:16:46 -0700404
405 // Both |stream_| and |flexfec_stream_| are managed by |this|. They are
406 // destroyed by calling call_->DestroyVideoReceiveStream and
407 // call_->DestroyFlexfecReceiveStream, respectively.
408 webrtc::VideoReceiveStream* stream_;
409 const bool default_stream_;
410 webrtc::VideoReceiveStream::Config config_;
411 webrtc::FlexfecReceiveStream::Config flexfec_config_;
412 webrtc::FlexfecReceiveStream* flexfec_stream_;
413
Niels Möllercbcbc222018-09-28 09:07:24 +0200414 webrtc::VideoDecoderFactory* const decoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700415
416 rtc::CriticalSection sink_lock_;
danilchapa37de392017-09-09 04:17:22 -0700417 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_
418 RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700419 // Expands remote RTP timestamps to int64_t to be able to estimate how long
420 // the stream has been running.
421 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
danilchapa37de392017-09-09 04:17:22 -0700422 RTC_GUARDED_BY(sink_lock_);
423 int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700424 // Start NTP time is estimated as current remote NTP time (estimated from
425 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
danilchapa37de392017-09-09 04:17:22 -0700426 int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700427 };
428
429 void Construct(webrtc::Call* call, WebRtcVideoEngine* engine);
430
431 bool SendRtp(const uint8_t* data,
432 size_t len,
433 const webrtc::PacketOptions& options) override;
434 bool SendRtcp(const uint8_t* data, size_t len) override;
435
436 static std::vector<VideoCodecSettings> MapCodecs(
437 const std::vector<VideoCodec>& codecs);
438 // Select what video codec will be used for sending, i.e. what codec is used
439 // for local encoding, based on supported remote codecs. The first remote
440 // codec that is supported locally will be selected.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200441 absl::optional<VideoCodecSettings> SelectSendVideoCodec(
eladalonf1841382017-06-12 01:16:46 -0700442 const std::vector<VideoCodecSettings>& remote_mapped_codecs) const;
443
444 static bool NonFlexfecReceiveCodecsHaveChanged(
445 std::vector<VideoCodecSettings> before,
446 std::vector<VideoCodecSettings> after);
447
448 void FillSenderStats(VideoMediaInfo* info, bool log_stats);
449 void FillReceiverStats(VideoMediaInfo* info, bool log_stats);
450 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
451 VideoMediaInfo* info);
452 void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info);
453
454 rtc::ThreadChecker thread_checker_;
455
456 uint32_t rtcp_receiver_report_ssrc_;
457 bool sending_;
458 webrtc::Call* const call_;
459
460 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_;
461 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_;
462
463 const MediaConfig::Video video_config_;
464
465 rtc::CriticalSection stream_crit_;
466 // Using primary-ssrc (first ssrc) as key.
467 std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
danilchapa37de392017-09-09 04:17:22 -0700468 RTC_GUARDED_BY(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700469 std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
danilchapa37de392017-09-09 04:17:22 -0700470 RTC_GUARDED_BY(stream_crit_);
471 std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(stream_crit_);
472 std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(stream_crit_);
eladalonf1841382017-06-12 01:16:46 -0700473
Danil Chapovalov00c71832018-06-15 15:58:38 +0200474 absl::optional<VideoCodecSettings> send_codec_;
475 absl::optional<std::vector<webrtc::RtpExtension>> send_rtp_extensions_;
eladalonf1841382017-06-12 01:16:46 -0700476
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100477 webrtc::VideoEncoderFactory* const encoder_factory_;
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200478 webrtc::VideoDecoderFactory* const decoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800479 webrtc::VideoBitrateAllocatorFactory* const bitrate_allocator_factory_;
eladalonf1841382017-06-12 01:16:46 -0700480 std::vector<VideoCodecSettings> recv_codecs_;
481 std::vector<webrtc::RtpExtension> recv_rtp_extensions_;
482 // See reason for keeping track of the FlexFEC payload type separately in
483 // comment in WebRtcVideoChannel::ChangedRecvParameters.
484 int recv_flexfec_payload_type_;
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +0100485 webrtc::BitrateConstraints bitrate_config_;
eladalonf1841382017-06-12 01:16:46 -0700486 // TODO(deadbeef): Don't duplicate information between
487 // send_params/recv_params, rtp_extensions, options, etc.
488 VideoSendParameters send_params_;
Tim Haloun648d28a2018-10-18 16:52:22 -0700489 rtc::DiffServCodePoint preferred_dscp_;
eladalonf1841382017-06-12 01:16:46 -0700490 VideoOptions default_send_options_;
491 VideoRecvParameters recv_params_;
492 int64_t last_stats_log_ms_;
Åsa Persson2c7149b2018-10-15 09:36:10 +0200493 const bool discard_unknown_ssrc_packets_;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700494 // This is a stream param that comes from the remote description, but wasn't
495 // signaled with any a=ssrc lines. It holds information that was signaled
496 // before the unsignaled receive stream is created when the first packet is
497 // received.
498 StreamParams unsignaled_stream_params_;
Benjamin Wright192eeec2018-10-17 17:27:25 -0700499 // Per peer connection crypto options that last for the lifetime of the peer
500 // connection.
501 const webrtc::CryptoOptions crypto_options_;
eladalonf1841382017-06-12 01:16:46 -0700502};
503
ilnik6b826ef2017-06-16 06:53:48 -0700504class EncoderStreamFactory
505 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
506 public:
507 EncoderStreamFactory(std::string codec_name,
508 int max_qp,
Seth Hampson1370e302018-02-07 08:50:36 -0800509 bool is_screenshare,
510 bool screenshare_config_explicitly_enabled);
ilnik6b826ef2017-06-16 06:53:48 -0700511
512 private:
513 std::vector<webrtc::VideoStream> CreateEncoderStreams(
514 int width,
515 int height,
516 const webrtc::VideoEncoderConfig& encoder_config) override;
517
518 const std::string codec_name_;
519 const int max_qp_;
Seth Hampson1370e302018-02-07 08:50:36 -0800520 const bool is_screenshare_;
521 // Allows a screenshare specific configuration, which enables temporal
522 // layering and allows simulcast.
523 const bool screenshare_config_explicitly_enabled_;
ilnik6b826ef2017-06-16 06:53:48 -0700524};
525
eladalonf1841382017-06-12 01:16:46 -0700526} // namespace cricket
527
Steve Anton10542f22019-01-11 09:11:00 -0800528#endif // MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_