blob: 9882fd7ac8dfd84209563eff75f5747d6824e525 [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"
Jonas Oreland6d835922019-03-18 10:59:40 +010032#include "media/engine/unhandled_packets_buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/async_invoker.h"
34#include "rtc_base/critical_section.h"
35#include "rtc_base/network_route.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#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).
Sebastian Jansson84848f22018-11-16 10:40:36 +010081class WebRtcVideoEngine : public VideoEngineInterface {
eladalonf1841382017-06-12 01:16:46 -070082 public:
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020083 // These video codec factories represents all video codecs, i.e. both software
84 // and external hardware codecs.
85 WebRtcVideoEngine(
86 std::unique_ptr<webrtc::VideoEncoderFactory> video_encoder_factory,
Jonas Orelanda3aa9bd2019-04-17 07:38:40 +020087 std::unique_ptr<webrtc::VideoDecoderFactory> video_decoder_factory);
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020088
Sebastian Jansson84848f22018-11-16 10:40:36 +010089 ~WebRtcVideoEngine() override;
eladalonf1841382017-06-12 01:16:46 -070090
Sebastian Jansson84848f22018-11-16 10:40:36 +010091 VideoMediaChannel* CreateMediaChannel(
Benjamin Wrightbfb444c2018-10-15 10:20:24 -070092 webrtc::Call* call,
93 const MediaConfig& config,
94 const VideoOptions& options,
Jonas Orelanda3aa9bd2019-04-17 07:38:40 +020095 const webrtc::CryptoOptions& crypto_options,
96 webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory)
97 override;
eladalonf1841382017-06-12 01:16:46 -070098
Sebastian Jansson84848f22018-11-16 10:40:36 +010099 std::vector<VideoCodec> codecs() const override;
100 RtpCapabilities GetCapabilities() const override;
eladalonf1841382017-06-12 01:16:46 -0700101
eladalonf1841382017-06-12 01:16:46 -0700102 private:
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200103 const std::unique_ptr<webrtc::VideoDecoderFactory> decoder_factory_;
Magnus Jedvert07e0d012017-10-31 11:24:54 +0100104 const std::unique_ptr<webrtc::VideoEncoderFactory> encoder_factory_;
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800105 const std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
106 bitrate_allocator_factory_;
eladalonf1841382017-06-12 01:16:46 -0700107};
108
philipele8ed8302019-07-03 11:53:48 +0200109class WebRtcVideoChannel : public VideoMediaChannel,
110 public webrtc::Transport,
111 public webrtc::EncoderFailureCallback {
eladalonf1841382017-06-12 01:16:46 -0700112 public:
Jiawei Ouc2ebe212018-11-08 10:02:56 -0800113 WebRtcVideoChannel(
114 webrtc::Call* call,
115 const MediaConfig& config,
116 const VideoOptions& options,
117 const webrtc::CryptoOptions& crypto_options,
118 webrtc::VideoEncoderFactory* encoder_factory,
119 webrtc::VideoDecoderFactory* decoder_factory,
120 webrtc::VideoBitrateAllocatorFactory* bitrate_allocator_factory);
eladalonf1841382017-06-12 01:16:46 -0700121 ~WebRtcVideoChannel() override;
122
123 // VideoMediaChannel implementation
eladalonf1841382017-06-12 01:16:46 -0700124 bool SetSendParameters(const VideoSendParameters& params) override;
125 bool SetRecvParameters(const VideoRecvParameters& params) override;
126 webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const override;
Zach Steinba37b4b2018-01-23 15:02:36 -0800127 webrtc::RTCError SetRtpSendParameters(
128 uint32_t ssrc,
129 const webrtc::RtpParameters& parameters) override;
eladalonf1841382017-06-12 01:16:46 -0700130 webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const override;
131 bool SetRtpReceiveParameters(
132 uint32_t ssrc,
133 const webrtc::RtpParameters& parameters) override;
134 bool GetSendCodec(VideoCodec* send_codec) override;
135 bool SetSend(bool send) override;
136 bool SetVideoSend(
137 uint32_t ssrc,
eladalonf1841382017-06-12 01:16:46 -0700138 const VideoOptions* options,
139 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) override;
140 bool AddSendStream(const StreamParams& sp) override;
141 bool RemoveSendStream(uint32_t ssrc) override;
142 bool AddRecvStream(const StreamParams& sp) override;
143 bool AddRecvStream(const StreamParams& sp, bool default_stream);
144 bool RemoveRecvStream(uint32_t ssrc) override;
145 bool SetSink(uint32_t ssrc,
146 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
147 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) override;
148 bool GetStats(VideoMediaInfo* info) override;
149
Amit Hilbuche7a5f7b2019-03-12 11:10:27 -0700150 void OnPacketReceived(rtc::CopyOnWriteBuffer packet,
Niels Möllere6933812018-11-05 13:01:41 +0100151 int64_t packet_time_us) override;
Amit Hilbuche7a5f7b2019-03-12 11:10:27 -0700152 void OnRtcpReceived(rtc::CopyOnWriteBuffer packet,
Niels Möllere6933812018-11-05 13:01:41 +0100153 int64_t packet_time_us) override;
eladalonf1841382017-06-12 01:16:46 -0700154 void OnReadyToSend(bool ready) override;
155 void OnNetworkRouteChanged(const std::string& transport_name,
156 const rtc::NetworkRoute& network_route) override;
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700157 void SetInterface(
158 NetworkInterface* iface,
159 const webrtc::MediaTransportConfig& media_transport_config) override;
eladalonf1841382017-06-12 01:16:46 -0700160
Benjamin Wright192eeec2018-10-17 17:27:25 -0700161 // E2E Encrypted Video Frame API
162 // Set a frame decryptor to a particular ssrc that will intercept all
163 // incoming video frames and attempt to decrypt them before forwarding the
164 // result.
165 void SetFrameDecryptor(uint32_t ssrc,
166 rtc::scoped_refptr<webrtc::FrameDecryptorInterface>
167 frame_decryptor) override;
168 // Set a frame encryptor to a particular ssrc that will intercept all
169 // outgoing video frames and attempt to encrypt them and forward the result
170 // to the packetizer.
171 void SetFrameEncryptor(uint32_t ssrc,
172 rtc::scoped_refptr<webrtc::FrameEncryptorInterface>
173 frame_encryptor) override;
174
Ruslan Burakov493a6502019-02-27 15:32:48 +0100175 bool SetBaseMinimumPlayoutDelayMs(uint32_t ssrc, int delay_ms) override;
176
177 absl::optional<int> GetBaseMinimumPlayoutDelayMs(
178 uint32_t ssrc) const override;
179
eladalonf1841382017-06-12 01:16:46 -0700180 // Implemented for VideoMediaChannelTest.
Steve Antonef50b252019-03-01 15:15:38 -0800181 bool sending() const {
182 RTC_DCHECK_RUN_ON(&thread_checker_);
183 return sending_;
184 }
eladalonf1841382017-06-12 01:16:46 -0700185
Danil Chapovalov00c71832018-06-15 15:58:38 +0200186 absl::optional<uint32_t> GetDefaultReceiveStreamSsrc();
eladalonf1841382017-06-12 01:16:46 -0700187
Steve Antonef50b252019-03-01 15:15:38 -0800188 StreamParams unsignaled_stream_params() {
189 RTC_DCHECK_RUN_ON(&thread_checker_);
190 return unsignaled_stream_params_;
191 }
Seth Hampson5897a6e2018-04-03 11:16:33 -0700192
eladalonf1841382017-06-12 01:16:46 -0700193 // AdaptReason is used for expressing why a WebRtcVideoSendStream request
194 // a lower input frame size than the currently configured camera input frame
195 // size. There can be more than one reason OR:ed together.
196 enum AdaptReason {
197 ADAPTREASON_NONE = 0,
198 ADAPTREASON_CPU = 1,
199 ADAPTREASON_BANDWIDTH = 2,
200 };
201
sprang67561a62017-06-15 06:34:42 -0700202 static constexpr int kDefaultQpMax = 56;
203
Jonas Oreland49ac5952018-09-26 16:04:32 +0200204 std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const override;
205
Jonas Oreland6d835922019-03-18 10:59:40 +0100206 // Take the buffered packets for |ssrcs| and feed them into DeliverPacket.
207 // This method does nothing unless unknown_ssrc_packet_buffer_ is configured.
208 void BackfillBufferedPackets(rtc::ArrayView<const uint32_t> ssrcs);
209
philipele8ed8302019-07-03 11:53:48 +0200210 // Implements webrtc::EncoderFailureCallback.
211 void OnEncoderFailure() override;
212
eladalonf1841382017-06-12 01:16:46 -0700213 private:
214 class WebRtcVideoReceiveStream;
215 struct VideoCodecSettings {
216 VideoCodecSettings();
217
218 // Checks if all members of |*this| are equal to the corresponding members
219 // of |other|.
220 bool operator==(const VideoCodecSettings& other) const;
221 bool operator!=(const VideoCodecSettings& other) const;
222
223 // Checks if all members of |a|, except |flexfec_payload_type|, are equal
224 // to the corresponding members of |b|.
225 static bool EqualsDisregardingFlexfec(const VideoCodecSettings& a,
226 const VideoCodecSettings& b);
227
228 VideoCodec codec;
229 webrtc::UlpfecConfig ulpfec;
230 int flexfec_payload_type;
231 int rtx_payload_type;
232 };
233
234 struct ChangedSendParameters {
235 // These optionals are unset if not changed.
philipele8ed8302019-07-03 11:53:48 +0200236 absl::optional<VideoCodecSettings> send_codec;
237 absl::optional<std::vector<VideoCodecSettings>> negotiated_codecs;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200238 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
239 absl::optional<std::string> mid;
Johannes Kron9190b822018-10-29 11:22:05 +0100240 absl::optional<bool> extmap_allow_mixed;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200241 absl::optional<int> max_bandwidth_bps;
242 absl::optional<bool> conference_mode;
243 absl::optional<webrtc::RtcpMode> rtcp_mode;
eladalonf1841382017-06-12 01:16:46 -0700244 };
245
246 struct ChangedRecvParameters {
247 // These optionals are unset if not changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200248 absl::optional<std::vector<VideoCodecSettings>> codec_settings;
249 absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
eladalonf1841382017-06-12 01:16:46 -0700250 // Keep track of the FlexFEC payload type separately from |codec_settings|.
251 // This allows us to recreate the FlexfecReceiveStream separately from the
252 // VideoReceiveStream when the FlexFEC payload type is changed.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200253 absl::optional<int> flexfec_payload_type;
eladalonf1841382017-06-12 01:16:46 -0700254 };
255
256 bool GetChangedSendParameters(const VideoSendParameters& params,
Steve Antonef50b252019-03-01 15:15:38 -0800257 ChangedSendParameters* changed_params) const
258 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
philipele8ed8302019-07-03 11:53:48 +0200259 bool ApplyChangedParams(const ChangedSendParameters& changed_params);
eladalonf1841382017-06-12 01:16:46 -0700260 bool GetChangedRecvParameters(const VideoRecvParameters& params,
Steve Antonef50b252019-03-01 15:15:38 -0800261 ChangedRecvParameters* changed_params) const
262 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700263
264 void SetMaxSendBandwidth(int bps);
265
266 void ConfigureReceiverRtp(
267 webrtc::VideoReceiveStream::Config* config,
268 webrtc::FlexfecReceiveStream::Config* flexfec_config,
Steve Antonef50b252019-03-01 15:15:38 -0800269 const StreamParams& sp) const
270 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700271 bool ValidateSendSsrcAvailability(const StreamParams& sp) const
Steve Antonef50b252019-03-01 15:15:38 -0800272 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700273 bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
Steve Antonef50b252019-03-01 15:15:38 -0800274 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700275 void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
Steve Antonef50b252019-03-01 15:15:38 -0800276 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700277
278 static std::string CodecSettingsVectorToString(
279 const std::vector<VideoCodecSettings>& codecs);
280
281 // Wrapper for the sender part.
Christian Fremerey6c025412019-02-13 19:43:28 +0000282 class WebRtcVideoSendStream
283 : public rtc::VideoSourceInterface<webrtc::VideoFrame> {
eladalonf1841382017-06-12 01:16:46 -0700284 public:
285 WebRtcVideoSendStream(
286 webrtc::Call* call,
287 const StreamParams& sp,
288 webrtc::VideoSendStream::Config config,
289 const VideoOptions& options,
eladalonf1841382017-06-12 01:16:46 -0700290 bool enable_cpu_overuse_detection,
291 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200292 const absl::optional<VideoCodecSettings>& codec_settings,
293 const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions,
eladalonf1841382017-06-12 01:16:46 -0700294 const VideoSendParameters& send_params);
295 virtual ~WebRtcVideoSendStream();
296
297 void SetSendParameters(const ChangedSendParameters& send_params);
Zach Steinba37b4b2018-01-23 15:02:36 -0800298 webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters);
eladalonf1841382017-06-12 01:16:46 -0700299 webrtc::RtpParameters GetRtpParameters() const;
300
Benjamin Wright192eeec2018-10-17 17:27:25 -0700301 void SetFrameEncryptor(
302 rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor);
303
Christian Fremerey6c025412019-02-13 19:43:28 +0000304 // Implements rtc::VideoSourceInterface<webrtc::VideoFrame>.
305 // WebRtcVideoSendStream acts as a source to the webrtc::VideoSendStream
306 // in |stream_|. This is done to proxy VideoSinkWants from the encoder to
307 // the worker thread.
308 void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
309 const rtc::VideoSinkWants& wants) override;
310 void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
311
Niels Möllerff40b142018-04-09 08:49:14 +0200312 bool SetVideoSend(const VideoOptions* options,
eladalonf1841382017-06-12 01:16:46 -0700313 rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
314
315 void SetSend(bool send);
316
317 const std::vector<uint32_t>& GetSsrcs() const;
318 VideoSenderInfo GetVideoSenderInfo(bool log_stats);
319 void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
320
321 private:
322 // Parameters needed to reconstruct the underlying stream.
323 // webrtc::VideoSendStream doesn't support setting a lot of options on the
324 // fly, so when those need to be changed we tear down and reconstruct with
325 // similar parameters depending on which options changed etc.
326 struct VideoSendStreamParameters {
327 VideoSendStreamParameters(
328 webrtc::VideoSendStream::Config config,
329 const VideoOptions& options,
330 int max_bitrate_bps,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200331 const absl::optional<VideoCodecSettings>& codec_settings);
eladalonf1841382017-06-12 01:16:46 -0700332 webrtc::VideoSendStream::Config config;
333 VideoOptions options;
334 int max_bitrate_bps;
335 bool conference_mode;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200336 absl::optional<VideoCodecSettings> codec_settings;
eladalonf1841382017-06-12 01:16:46 -0700337 // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
338 // typically changes when setting a new resolution or reconfiguring
339 // bitrates.
340 webrtc::VideoEncoderConfig encoder_config;
341 };
342
eladalonf1841382017-06-12 01:16:46 -0700343 rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
344 ConfigureVideoEncoderSettings(const VideoCodec& codec);
Niels Möller5bf8ccd2018-03-15 14:16:11 +0100345 void SetCodec(const VideoCodecSettings& codec);
eladalonf1841382017-06-12 01:16:46 -0700346 void RecreateWebRtcStream();
347 webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
348 const VideoCodec& codec) const;
349 void ReconfigureEncoder();
eladalonf1841382017-06-12 01:16:46 -0700350
351 // Calls Start or Stop according to whether or not |sending_| is true,
352 // and whether or not the encoding in |rtp_parameters_| is active.
353 void UpdateSendState();
354
Taylor Brandstetter49fcc102018-05-16 14:20:41 -0700355 webrtc::DegradationPreference GetDegradationPreference() const
356 RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700357
358 rtc::ThreadChecker thread_checker_;
eladalonf1841382017-06-12 01:16:46 -0700359 rtc::Thread* worker_thread_;
Niels Möller1e062892018-02-07 10:18:32 +0100360 const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_);
361 const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700362 webrtc::Call* const call_;
363 const bool enable_cpu_overuse_detection_;
364 rtc::VideoSourceInterface<webrtc::VideoFrame>* source_
Niels Möller1e062892018-02-07 10:18:32 +0100365 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700366
Niels Möller1e062892018-02-07 10:18:32 +0100367 webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_);
Christian Fremerey6c025412019-02-13 19:43:28 +0000368 rtc::VideoSinkInterface<webrtc::VideoFrame>* encoder_sink_
369 RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700370 // Contains settings that are the same for all streams in the MediaChannel,
371 // such as codecs, header extensions, and the global bitrate limit for the
372 // entire channel.
Niels Möller1e062892018-02-07 10:18:32 +0100373 VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700374 // Contains settings that are unique for each stream, such as max_bitrate.
375 // Does *not* contain codecs, however.
376 // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_.
377 // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only
378 // one stream per MediaChannel.
Niels Möller1e062892018-02-07 10:18:32 +0100379 webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700380
Niels Möller1e062892018-02-07 10:18:32 +0100381 bool sending_ RTC_GUARDED_BY(&thread_checker_);
philipel98cbb222019-06-14 11:28:51 +0200382
383 // In order for the |invoker_| to protect other members from being
384 // destructed as they are used in asynchronous tasks it has to be destructed
385 // first.
386 rtc::AsyncInvoker invoker_;
eladalonf1841382017-06-12 01:16:46 -0700387 };
388
389 // Wrapper for the receiver part, contains configs etc. that are needed to
390 // reconstruct the underlying VideoReceiveStream.
391 class WebRtcVideoReceiveStream
392 : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
393 public:
394 WebRtcVideoReceiveStream(
Jonas Oreland6d835922019-03-18 10:59:40 +0100395 WebRtcVideoChannel* channel,
eladalonf1841382017-06-12 01:16:46 -0700396 webrtc::Call* call,
397 const StreamParams& sp,
398 webrtc::VideoReceiveStream::Config config,
Magnus Jedvert59ab3532018-09-03 18:07:56 +0200399 webrtc::VideoDecoderFactory* decoder_factory,
eladalonf1841382017-06-12 01:16:46 -0700400 bool default_stream,
401 const std::vector<VideoCodecSettings>& recv_codecs,
402 const webrtc::FlexfecReceiveStream::Config& flexfec_config);
403 ~WebRtcVideoReceiveStream();
404
405 const std::vector<uint32_t>& GetSsrcs() const;
Florent Castelliabe301f2018-06-12 18:33:49 +0200406
Jonas Oreland49ac5952018-09-26 16:04:32 +0200407 std::vector<webrtc::RtpSource> GetSources();
408
Florent Castelliabe301f2018-06-12 18:33:49 +0200409 // Does not return codecs, they are filled by the owning WebRtcVideoChannel.
410 webrtc::RtpParameters GetRtpParameters() const;
eladalonf1841382017-06-12 01:16:46 -0700411
412 void SetLocalSsrc(uint32_t local_ssrc);
413 // TODO(deadbeef): Move these feedback parameters into the recv parameters.
Elad Alonfadb1812019-05-24 13:40:02 +0200414 void SetFeedbackParameters(bool lntf_enabled,
415 bool nack_enabled,
eladalonf1841382017-06-12 01:16:46 -0700416 bool remb_enabled,
417 bool transport_cc_enabled,
418 webrtc::RtcpMode rtcp_mode);
419 void SetRecvParameters(const ChangedRecvParameters& recv_params);
420
421 void OnFrame(const webrtc::VideoFrame& frame) override;
422 bool IsDefaultStream() const;
423
Benjamin Wright192eeec2018-10-17 17:27:25 -0700424 void SetFrameDecryptor(
425 rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor);
426
Ruslan Burakov493a6502019-02-27 15:32:48 +0100427 bool SetBaseMinimumPlayoutDelayMs(int delay_ms);
428
429 int GetBaseMinimumPlayoutDelayMs() const;
430
eladalonf1841382017-06-12 01:16:46 -0700431 void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
432
433 VideoReceiverInfo GetVideoReceiverInfo(bool log_stats);
434
435 private:
eladalonf1841382017-06-12 01:16:46 -0700436 void RecreateWebRtcVideoStream();
437 void MaybeRecreateWebRtcFlexfecStream();
438
eladalonc0d481a2017-08-02 07:39:07 -0700439 void MaybeAssociateFlexfecWithVideo();
440 void MaybeDissociateFlexfecFromVideo();
441
Niels Möllercbcbc222018-09-28 09:07:24 +0200442 void ConfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
eladalonf1841382017-06-12 01:16:46 -0700443 void ConfigureFlexfecCodec(int flexfec_payload_type);
eladalonf1841382017-06-12 01:16:46 -0700444
445 std::string GetCodecNameFromPayloadType(int payload_type);
446
Jonas Oreland6d835922019-03-18 10:59:40 +0100447 WebRtcVideoChannel* const channel_;
eladalonf1841382017-06-12 01:16:46 -0700448 webrtc::Call* const call_;
Niels Möllercbcbc222018-09-28 09:07:24 +0200449 const StreamParams stream_params_;
eladalonf1841382017-06-12 01:16:46 -0700450
451 // Both |stream_| and |flexfec_stream_| are managed by |this|. They are
452 // destroyed by calling call_->DestroyVideoReceiveStream and
453 // call_->DestroyFlexfecReceiveStream, respectively.
454 webrtc::VideoReceiveStream* stream_;
455 const bool default_stream_;
456 webrtc::VideoReceiveStream::Config config_;
457 webrtc::FlexfecReceiveStream::Config flexfec_config_;
458 webrtc::FlexfecReceiveStream* flexfec_stream_;
459
Niels Möllercbcbc222018-09-28 09:07:24 +0200460 webrtc::VideoDecoderFactory* const decoder_factory_;
eladalonf1841382017-06-12 01:16:46 -0700461
462 rtc::CriticalSection sink_lock_;
danilchapa37de392017-09-09 04:17:22 -0700463 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_
464 RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700465 // Expands remote RTP timestamps to int64_t to be able to estimate how long
466 // the stream has been running.
467 rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
danilchapa37de392017-09-09 04:17:22 -0700468 RTC_GUARDED_BY(sink_lock_);
469 int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700470 // Start NTP time is estimated as current remote NTP time (estimated from
471 // RTCP) minus the elapsed time, as soon as remote NTP time is available.
danilchapa37de392017-09-09 04:17:22 -0700472 int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_);
eladalonf1841382017-06-12 01:16:46 -0700473 };
474
475 void Construct(webrtc::Call* call, WebRtcVideoEngine* engine);
476
477 bool SendRtp(const uint8_t* data,
478 size_t len,
479 const webrtc::PacketOptions& options) override;
480 bool SendRtcp(const uint8_t* data, size_t len) override;
481
482 static std::vector<VideoCodecSettings> MapCodecs(
483 const std::vector<VideoCodec>& codecs);
philipele8ed8302019-07-03 11:53:48 +0200484 // Get all codecs that are compatible with the receiver.
485 std::vector<VideoCodecSettings> SelectSendVideoCodecs(
Steve Antonef50b252019-03-01 15:15:38 -0800486 const std::vector<VideoCodecSettings>& remote_mapped_codecs) const
487 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700488
489 static bool NonFlexfecReceiveCodecsHaveChanged(
490 std::vector<VideoCodecSettings> before,
491 std::vector<VideoCodecSettings> after);
492
Steve Antonef50b252019-03-01 15:15:38 -0800493 void FillSenderStats(VideoMediaInfo* info, bool log_stats)
494 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
495 void FillReceiverStats(VideoMediaInfo* info, bool log_stats)
496 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700497 void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
Steve Antonef50b252019-03-01 15:15:38 -0800498 VideoMediaInfo* info)
499 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
500 void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info)
501 RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700502
philipele8ed8302019-07-03 11:53:48 +0200503 rtc::Thread* worker_thread_;
eladalonf1841382017-06-12 01:16:46 -0700504 rtc::ThreadChecker thread_checker_;
505
Steve Antonef50b252019-03-01 15:15:38 -0800506 uint32_t rtcp_receiver_report_ssrc_ RTC_GUARDED_BY(thread_checker_);
507 bool sending_ RTC_GUARDED_BY(thread_checker_);
508 webrtc::Call* const call_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700509
Steve Antonef50b252019-03-01 15:15:38 -0800510 DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_
511 RTC_GUARDED_BY(thread_checker_);
512 UnsignalledSsrcHandler* const unsignalled_ssrc_handler_
513 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700514
Ruslan Burakov493a6502019-02-27 15:32:48 +0100515 // Delay for unsignaled streams, which may be set before the stream exists.
Steve Antonef50b252019-03-01 15:15:38 -0800516 int default_recv_base_minimum_delay_ms_ RTC_GUARDED_BY(thread_checker_) = 0;
Ruslan Burakov493a6502019-02-27 15:32:48 +0100517
Steve Antonef50b252019-03-01 15:15:38 -0800518 const MediaConfig::Video video_config_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700519
eladalonf1841382017-06-12 01:16:46 -0700520 // Using primary-ssrc (first ssrc) as key.
521 std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
Steve Antonef50b252019-03-01 15:15:38 -0800522 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700523 std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
Steve Antonef50b252019-03-01 15:15:38 -0800524 RTC_GUARDED_BY(thread_checker_);
525 std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(thread_checker_);
526 std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700527
Steve Antonef50b252019-03-01 15:15:38 -0800528 absl::optional<VideoCodecSettings> send_codec_
529 RTC_GUARDED_BY(thread_checker_);
philipele8ed8302019-07-03 11:53:48 +0200530 std::vector<VideoCodecSettings> negotiated_codecs_
531 RTC_GUARDED_BY(thread_checker_);
532
Steve Antonef50b252019-03-01 15:15:38 -0800533 absl::optional<std::vector<webrtc::RtpExtension>> send_rtp_extensions_
534 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700535
Steve Antonef50b252019-03-01 15:15:38 -0800536 webrtc::VideoEncoderFactory* const encoder_factory_
537 RTC_GUARDED_BY(thread_checker_);
538 webrtc::VideoDecoderFactory* const decoder_factory_
539 RTC_GUARDED_BY(thread_checker_);
540 webrtc::VideoBitrateAllocatorFactory* const bitrate_allocator_factory_
541 RTC_GUARDED_BY(thread_checker_);
542 std::vector<VideoCodecSettings> recv_codecs_ RTC_GUARDED_BY(thread_checker_);
543 std::vector<webrtc::RtpExtension> recv_rtp_extensions_
544 RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700545 // See reason for keeping track of the FlexFEC payload type separately in
546 // comment in WebRtcVideoChannel::ChangedRecvParameters.
Steve Antonef50b252019-03-01 15:15:38 -0800547 int recv_flexfec_payload_type_ RTC_GUARDED_BY(thread_checker_);
548 webrtc::BitrateConstraints bitrate_config_ RTC_GUARDED_BY(thread_checker_);
eladalonf1841382017-06-12 01:16:46 -0700549 // TODO(deadbeef): Don't duplicate information between
550 // send_params/recv_params, rtp_extensions, options, etc.
Steve Antonef50b252019-03-01 15:15:38 -0800551 VideoSendParameters send_params_ RTC_GUARDED_BY(thread_checker_);
Steve Antonef50b252019-03-01 15:15:38 -0800552 VideoOptions default_send_options_ RTC_GUARDED_BY(thread_checker_);
553 VideoRecvParameters recv_params_ RTC_GUARDED_BY(thread_checker_);
554 int64_t last_stats_log_ms_ RTC_GUARDED_BY(thread_checker_);
555 const bool discard_unknown_ssrc_packets_ RTC_GUARDED_BY(thread_checker_);
Seth Hampson5897a6e2018-04-03 11:16:33 -0700556 // This is a stream param that comes from the remote description, but wasn't
557 // signaled with any a=ssrc lines. It holds information that was signaled
558 // before the unsignaled receive stream is created when the first packet is
559 // received.
Steve Antonef50b252019-03-01 15:15:38 -0800560 StreamParams unsignaled_stream_params_ RTC_GUARDED_BY(thread_checker_);
Benjamin Wright192eeec2018-10-17 17:27:25 -0700561 // Per peer connection crypto options that last for the lifetime of the peer
562 // connection.
Steve Antonef50b252019-03-01 15:15:38 -0800563 const webrtc::CryptoOptions crypto_options_ RTC_GUARDED_BY(thread_checker_);
Jonas Oreland6d835922019-03-18 10:59:40 +0100564
565 // Buffer for unhandled packets.
566 std::unique_ptr<UnhandledPacketsBuffer> unknown_ssrc_packet_buffer_
567 RTC_GUARDED_BY(thread_checker_);
philipele8ed8302019-07-03 11:53:48 +0200568
569 // In order for the |invoker_| to protect other members from being destructed
570 // as they are used in asynchronous tasks it has to be destructed first.
571 rtc::AsyncInvoker invoker_;
eladalonf1841382017-06-12 01:16:46 -0700572};
573
ilnik6b826ef2017-06-16 06:53:48 -0700574class EncoderStreamFactory
575 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
576 public:
577 EncoderStreamFactory(std::string codec_name,
578 int max_qp,
Seth Hampson1370e302018-02-07 08:50:36 -0800579 bool is_screenshare,
580 bool screenshare_config_explicitly_enabled);
ilnik6b826ef2017-06-16 06:53:48 -0700581
582 private:
583 std::vector<webrtc::VideoStream> CreateEncoderStreams(
584 int width,
585 int height,
586 const webrtc::VideoEncoderConfig& encoder_config) override;
587
588 const std::string codec_name_;
589 const int max_qp_;
Seth Hampson1370e302018-02-07 08:50:36 -0800590 const bool is_screenshare_;
591 // Allows a screenshare specific configuration, which enables temporal
592 // layering and allows simulcast.
593 const bool screenshare_config_explicitly_enabled_;
ilnik6b826ef2017-06-16 06:53:48 -0700594};
595
eladalonf1841382017-06-12 01:16:46 -0700596} // namespace cricket
597
Steve Anton10542f22019-01-11 09:11:00 -0800598#endif // MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_