eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MEDIA_ENGINE_WEBRTCVIDEOENGINE_H_ |
| 12 | #define MEDIA_ENGINE_WEBRTCVIDEOENGINE_H_ |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 13 | |
| 14 | #include <map> |
| 15 | #include <memory> |
| 16 | #include <set> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 20 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "api/call/transport.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "api/video/video_frame.h" |
Niels Möller | c6ce9c5 | 2018-05-11 11:15:30 +0200 | [diff] [blame] | 23 | #include "api/video/video_sink_interface.h" |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 24 | #include "api/video/video_source_interface.h" |
| 25 | #include "api/video_codecs/sdp_video_format.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #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" |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 38 | |
| 39 | namespace webrtc { |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 40 | class VideoDecoderFactory; |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 41 | class VideoEncoderFactory; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 42 | struct MediaConfig; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 43 | } // namespace webrtc |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 44 | |
| 45 | namespace rtc { |
| 46 | class Thread; |
| 47 | } // namespace rtc |
| 48 | |
| 49 | namespace cricket { |
| 50 | |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 51 | class WebRtcVideoChannel; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 52 | |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 53 | class 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. |
| 65 | class DefaultUnsignalledSsrcHandler : public UnsignalledSsrcHandler { |
| 66 | public: |
| 67 | DefaultUnsignalledSsrcHandler(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 68 | Action OnUnsignalledSsrc(WebRtcVideoChannel* channel, uint32_t ssrc) override; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 69 | |
| 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). |
| 81 | class WebRtcVideoEngine { |
| 82 | public: |
Anders Carlsson | dd8c165 | 2018-01-30 10:32:13 +0100 | [diff] [blame] | 83 | #if defined(USE_BUILTIN_SW_CODECS) |
Magnus Jedvert | 02e7a19 | 2017-09-23 17:21:32 +0200 | [diff] [blame] | 84 | // 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 Carlsson | dd8c165 | 2018-01-30 10:32:13 +0100 | [diff] [blame] | 89 | #endif |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 90 | |
| 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 | |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 97 | virtual ~WebRtcVideoEngine(); |
| 98 | |
Benjamin Wright | bfb444c | 2018-10-15 10:20:24 -0700 | [diff] [blame] | 99 | WebRtcVideoChannel* CreateChannel( |
| 100 | webrtc::Call* call, |
| 101 | const MediaConfig& config, |
| 102 | const VideoOptions& options, |
| 103 | const webrtc::CryptoOptions& crypto_options); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 104 | |
| 105 | std::vector<VideoCodec> codecs() const; |
| 106 | RtpCapabilities GetCapabilities() const; |
| 107 | |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 108 | private: |
Magnus Jedvert | 59ab353 | 2018-09-03 18:07:56 +0200 | [diff] [blame] | 109 | const std::unique_ptr<webrtc::VideoDecoderFactory> decoder_factory_; |
Magnus Jedvert | 07e0d01 | 2017-10-31 11:24:54 +0100 | [diff] [blame] | 110 | const std::unique_ptr<webrtc::VideoEncoderFactory> encoder_factory_; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | class WebRtcVideoChannel : public VideoMediaChannel, public webrtc::Transport { |
| 114 | public: |
| 115 | WebRtcVideoChannel(webrtc::Call* call, |
| 116 | const MediaConfig& config, |
| 117 | const VideoOptions& options, |
Benjamin Wright | bfb444c | 2018-10-15 10:20:24 -0700 | [diff] [blame] | 118 | const webrtc::CryptoOptions& crypto_options, |
Magnus Jedvert | 07e0d01 | 2017-10-31 11:24:54 +0100 | [diff] [blame] | 119 | webrtc::VideoEncoderFactory* encoder_factory, |
Magnus Jedvert | 59ab353 | 2018-09-03 18:07:56 +0200 | [diff] [blame] | 120 | webrtc::VideoDecoderFactory* decoder_factory); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 121 | ~WebRtcVideoChannel() override; |
| 122 | |
| 123 | // VideoMediaChannel implementation |
| 124 | rtc::DiffServCodePoint PreferredDscp() const override; |
| 125 | |
| 126 | bool SetSendParameters(const VideoSendParameters& params) override; |
| 127 | bool SetRecvParameters(const VideoRecvParameters& params) override; |
| 128 | webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const override; |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 129 | webrtc::RTCError SetRtpSendParameters( |
| 130 | uint32_t ssrc, |
| 131 | const webrtc::RtpParameters& parameters) override; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 132 | webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const override; |
| 133 | bool SetRtpReceiveParameters( |
| 134 | uint32_t ssrc, |
| 135 | const webrtc::RtpParameters& parameters) override; |
| 136 | bool GetSendCodec(VideoCodec* send_codec) override; |
| 137 | bool SetSend(bool send) override; |
| 138 | bool SetVideoSend( |
| 139 | uint32_t ssrc, |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 140 | const VideoOptions* options, |
| 141 | rtc::VideoSourceInterface<webrtc::VideoFrame>* source) override; |
| 142 | bool AddSendStream(const StreamParams& sp) override; |
| 143 | bool RemoveSendStream(uint32_t ssrc) override; |
| 144 | bool AddRecvStream(const StreamParams& sp) override; |
| 145 | bool AddRecvStream(const StreamParams& sp, bool default_stream); |
| 146 | bool RemoveRecvStream(uint32_t ssrc) override; |
| 147 | bool SetSink(uint32_t ssrc, |
| 148 | rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override; |
| 149 | void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) override; |
| 150 | bool GetStats(VideoMediaInfo* info) override; |
| 151 | |
| 152 | void OnPacketReceived(rtc::CopyOnWriteBuffer* packet, |
| 153 | const rtc::PacketTime& packet_time) override; |
| 154 | void OnRtcpReceived(rtc::CopyOnWriteBuffer* packet, |
| 155 | const rtc::PacketTime& packet_time) override; |
| 156 | void OnReadyToSend(bool ready) override; |
| 157 | void OnNetworkRouteChanged(const std::string& transport_name, |
| 158 | const rtc::NetworkRoute& network_route) override; |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 159 | void SetInterface(NetworkInterface* iface, |
| 160 | webrtc::MediaTransportInterface* media_transport) override; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 161 | |
Benjamin Wright | 192eeec | 2018-10-17 17:27:25 -0700 | [diff] [blame] | 162 | // E2E Encrypted Video Frame API |
| 163 | // Set a frame decryptor to a particular ssrc that will intercept all |
| 164 | // incoming video frames and attempt to decrypt them before forwarding the |
| 165 | // result. |
| 166 | void SetFrameDecryptor(uint32_t ssrc, |
| 167 | rtc::scoped_refptr<webrtc::FrameDecryptorInterface> |
| 168 | frame_decryptor) override; |
| 169 | // Set a frame encryptor to a particular ssrc that will intercept all |
| 170 | // outgoing video frames and attempt to encrypt them and forward the result |
| 171 | // to the packetizer. |
| 172 | void SetFrameEncryptor(uint32_t ssrc, |
| 173 | rtc::scoped_refptr<webrtc::FrameEncryptorInterface> |
| 174 | frame_encryptor) override; |
| 175 | |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 176 | // Implemented for VideoMediaChannelTest. |
| 177 | bool sending() const { return sending_; } |
| 178 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 179 | absl::optional<uint32_t> GetDefaultReceiveStreamSsrc(); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 180 | |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 181 | StreamParams unsignaled_stream_params() { return unsignaled_stream_params_; } |
| 182 | |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 183 | // AdaptReason is used for expressing why a WebRtcVideoSendStream request |
| 184 | // a lower input frame size than the currently configured camera input frame |
| 185 | // size. There can be more than one reason OR:ed together. |
| 186 | enum AdaptReason { |
| 187 | ADAPTREASON_NONE = 0, |
| 188 | ADAPTREASON_CPU = 1, |
| 189 | ADAPTREASON_BANDWIDTH = 2, |
| 190 | }; |
| 191 | |
sprang | 67561a6 | 2017-06-15 06:34:42 -0700 | [diff] [blame] | 192 | static constexpr int kDefaultQpMax = 56; |
| 193 | |
Jonas Oreland | 49ac595 | 2018-09-26 16:04:32 +0200 | [diff] [blame] | 194 | std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const override; |
| 195 | |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 196 | private: |
| 197 | class WebRtcVideoReceiveStream; |
| 198 | struct VideoCodecSettings { |
| 199 | VideoCodecSettings(); |
| 200 | |
| 201 | // Checks if all members of |*this| are equal to the corresponding members |
| 202 | // of |other|. |
| 203 | bool operator==(const VideoCodecSettings& other) const; |
| 204 | bool operator!=(const VideoCodecSettings& other) const; |
| 205 | |
| 206 | // Checks if all members of |a|, except |flexfec_payload_type|, are equal |
| 207 | // to the corresponding members of |b|. |
| 208 | static bool EqualsDisregardingFlexfec(const VideoCodecSettings& a, |
| 209 | const VideoCodecSettings& b); |
| 210 | |
| 211 | VideoCodec codec; |
| 212 | webrtc::UlpfecConfig ulpfec; |
| 213 | int flexfec_payload_type; |
| 214 | int rtx_payload_type; |
| 215 | }; |
| 216 | |
| 217 | struct ChangedSendParameters { |
| 218 | // These optionals are unset if not changed. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 219 | absl::optional<VideoCodecSettings> codec; |
| 220 | absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions; |
| 221 | absl::optional<std::string> mid; |
| 222 | absl::optional<int> max_bandwidth_bps; |
| 223 | absl::optional<bool> conference_mode; |
| 224 | absl::optional<webrtc::RtcpMode> rtcp_mode; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | struct ChangedRecvParameters { |
| 228 | // These optionals are unset if not changed. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 229 | absl::optional<std::vector<VideoCodecSettings>> codec_settings; |
| 230 | absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 231 | // Keep track of the FlexFEC payload type separately from |codec_settings|. |
| 232 | // This allows us to recreate the FlexfecReceiveStream separately from the |
| 233 | // VideoReceiveStream when the FlexFEC payload type is changed. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 234 | absl::optional<int> flexfec_payload_type; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | bool GetChangedSendParameters(const VideoSendParameters& params, |
| 238 | ChangedSendParameters* changed_params) const; |
| 239 | bool GetChangedRecvParameters(const VideoRecvParameters& params, |
| 240 | ChangedRecvParameters* changed_params) const; |
| 241 | |
| 242 | void SetMaxSendBandwidth(int bps); |
| 243 | |
| 244 | void ConfigureReceiverRtp( |
| 245 | webrtc::VideoReceiveStream::Config* config, |
| 246 | webrtc::FlexfecReceiveStream::Config* flexfec_config, |
| 247 | const StreamParams& sp) const; |
| 248 | bool ValidateSendSsrcAvailability(const StreamParams& sp) const |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 249 | RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 250 | bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 251 | RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 252 | void DeleteReceiveStream(WebRtcVideoReceiveStream* stream) |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 253 | RTC_EXCLUSIVE_LOCKS_REQUIRED(stream_crit_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 254 | |
| 255 | static std::string CodecSettingsVectorToString( |
| 256 | const std::vector<VideoCodecSettings>& codecs); |
| 257 | |
| 258 | // Wrapper for the sender part. |
| 259 | class WebRtcVideoSendStream |
| 260 | : public rtc::VideoSourceInterface<webrtc::VideoFrame> { |
| 261 | public: |
| 262 | WebRtcVideoSendStream( |
| 263 | webrtc::Call* call, |
| 264 | const StreamParams& sp, |
| 265 | webrtc::VideoSendStream::Config config, |
| 266 | const VideoOptions& options, |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 267 | bool enable_cpu_overuse_detection, |
| 268 | int max_bitrate_bps, |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 269 | const absl::optional<VideoCodecSettings>& codec_settings, |
| 270 | const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions, |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 271 | const VideoSendParameters& send_params); |
| 272 | virtual ~WebRtcVideoSendStream(); |
| 273 | |
| 274 | void SetSendParameters(const ChangedSendParameters& send_params); |
Zach Stein | ba37b4b | 2018-01-23 15:02:36 -0800 | [diff] [blame] | 275 | webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 276 | webrtc::RtpParameters GetRtpParameters() const; |
| 277 | |
Benjamin Wright | 192eeec | 2018-10-17 17:27:25 -0700 | [diff] [blame] | 278 | void SetFrameEncryptor( |
| 279 | rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor); |
| 280 | |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 281 | // Implements rtc::VideoSourceInterface<webrtc::VideoFrame>. |
| 282 | // WebRtcVideoSendStream acts as a source to the webrtc::VideoSendStream |
| 283 | // in |stream_|. This is done to proxy VideoSinkWants from the encoder to |
| 284 | // the worker thread. |
| 285 | void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink, |
| 286 | const rtc::VideoSinkWants& wants) override; |
| 287 | void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override; |
| 288 | |
Niels Möller | ff40b14 | 2018-04-09 08:49:14 +0200 | [diff] [blame] | 289 | bool SetVideoSend(const VideoOptions* options, |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 290 | rtc::VideoSourceInterface<webrtc::VideoFrame>* source); |
| 291 | |
| 292 | void SetSend(bool send); |
| 293 | |
| 294 | const std::vector<uint32_t>& GetSsrcs() const; |
| 295 | VideoSenderInfo GetVideoSenderInfo(bool log_stats); |
| 296 | void FillBitrateInfo(BandwidthEstimationInfo* bwe_info); |
| 297 | |
| 298 | private: |
| 299 | // Parameters needed to reconstruct the underlying stream. |
| 300 | // webrtc::VideoSendStream doesn't support setting a lot of options on the |
| 301 | // fly, so when those need to be changed we tear down and reconstruct with |
| 302 | // similar parameters depending on which options changed etc. |
| 303 | struct VideoSendStreamParameters { |
| 304 | VideoSendStreamParameters( |
| 305 | webrtc::VideoSendStream::Config config, |
| 306 | const VideoOptions& options, |
| 307 | int max_bitrate_bps, |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 308 | const absl::optional<VideoCodecSettings>& codec_settings); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 309 | webrtc::VideoSendStream::Config config; |
| 310 | VideoOptions options; |
| 311 | int max_bitrate_bps; |
| 312 | bool conference_mode; |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 313 | absl::optional<VideoCodecSettings> codec_settings; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 314 | // Sent resolutions + bitrates etc. by the underlying VideoSendStream, |
| 315 | // typically changes when setting a new resolution or reconfiguring |
| 316 | // bitrates. |
| 317 | webrtc::VideoEncoderConfig encoder_config; |
| 318 | }; |
| 319 | |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 320 | rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings> |
| 321 | ConfigureVideoEncoderSettings(const VideoCodec& codec); |
Niels Möller | 5bf8ccd | 2018-03-15 14:16:11 +0100 | [diff] [blame] | 322 | void SetCodec(const VideoCodecSettings& codec); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 323 | void RecreateWebRtcStream(); |
| 324 | webrtc::VideoEncoderConfig CreateVideoEncoderConfig( |
| 325 | const VideoCodec& codec) const; |
| 326 | void ReconfigureEncoder(); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 327 | |
| 328 | // Calls Start or Stop according to whether or not |sending_| is true, |
| 329 | // and whether or not the encoding in |rtp_parameters_| is active. |
| 330 | void UpdateSendState(); |
| 331 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 332 | webrtc::DegradationPreference GetDegradationPreference() const |
| 333 | RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 334 | |
| 335 | rtc::ThreadChecker thread_checker_; |
| 336 | rtc::AsyncInvoker invoker_; |
| 337 | rtc::Thread* worker_thread_; |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 338 | const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_); |
| 339 | const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 340 | webrtc::Call* const call_; |
| 341 | const bool enable_cpu_overuse_detection_; |
| 342 | rtc::VideoSourceInterface<webrtc::VideoFrame>* source_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 343 | RTC_GUARDED_BY(&thread_checker_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 344 | |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 345 | webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 346 | rtc::VideoSinkInterface<webrtc::VideoFrame>* encoder_sink_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 347 | RTC_GUARDED_BY(&thread_checker_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 348 | // Contains settings that are the same for all streams in the MediaChannel, |
| 349 | // such as codecs, header extensions, and the global bitrate limit for the |
| 350 | // entire channel. |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 351 | VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 352 | // Contains settings that are unique for each stream, such as max_bitrate. |
| 353 | // Does *not* contain codecs, however. |
| 354 | // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_. |
| 355 | // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only |
| 356 | // one stream per MediaChannel. |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 357 | webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 358 | |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 359 | bool sending_ RTC_GUARDED_BY(&thread_checker_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 360 | }; |
| 361 | |
| 362 | // Wrapper for the receiver part, contains configs etc. that are needed to |
| 363 | // reconstruct the underlying VideoReceiveStream. |
| 364 | class WebRtcVideoReceiveStream |
| 365 | : public rtc::VideoSinkInterface<webrtc::VideoFrame> { |
| 366 | public: |
| 367 | WebRtcVideoReceiveStream( |
| 368 | webrtc::Call* call, |
| 369 | const StreamParams& sp, |
| 370 | webrtc::VideoReceiveStream::Config config, |
Magnus Jedvert | 59ab353 | 2018-09-03 18:07:56 +0200 | [diff] [blame] | 371 | webrtc::VideoDecoderFactory* decoder_factory, |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 372 | bool default_stream, |
| 373 | const std::vector<VideoCodecSettings>& recv_codecs, |
| 374 | const webrtc::FlexfecReceiveStream::Config& flexfec_config); |
| 375 | ~WebRtcVideoReceiveStream(); |
| 376 | |
| 377 | const std::vector<uint32_t>& GetSsrcs() const; |
Florent Castelli | abe301f | 2018-06-12 18:33:49 +0200 | [diff] [blame] | 378 | |
Jonas Oreland | 49ac595 | 2018-09-26 16:04:32 +0200 | [diff] [blame] | 379 | std::vector<webrtc::RtpSource> GetSources(); |
| 380 | |
Florent Castelli | abe301f | 2018-06-12 18:33:49 +0200 | [diff] [blame] | 381 | // Does not return codecs, they are filled by the owning WebRtcVideoChannel. |
| 382 | webrtc::RtpParameters GetRtpParameters() const; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 383 | |
| 384 | void SetLocalSsrc(uint32_t local_ssrc); |
| 385 | // TODO(deadbeef): Move these feedback parameters into the recv parameters. |
| 386 | void SetFeedbackParameters(bool nack_enabled, |
| 387 | bool remb_enabled, |
| 388 | bool transport_cc_enabled, |
| 389 | webrtc::RtcpMode rtcp_mode); |
| 390 | void SetRecvParameters(const ChangedRecvParameters& recv_params); |
| 391 | |
| 392 | void OnFrame(const webrtc::VideoFrame& frame) override; |
| 393 | bool IsDefaultStream() const; |
| 394 | |
Benjamin Wright | 192eeec | 2018-10-17 17:27:25 -0700 | [diff] [blame] | 395 | void SetFrameDecryptor( |
| 396 | rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor); |
| 397 | |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 398 | void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink); |
| 399 | |
| 400 | VideoReceiverInfo GetVideoReceiverInfo(bool log_stats); |
| 401 | |
| 402 | private: |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 403 | void RecreateWebRtcVideoStream(); |
| 404 | void MaybeRecreateWebRtcFlexfecStream(); |
| 405 | |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 406 | void MaybeAssociateFlexfecWithVideo(); |
| 407 | void MaybeDissociateFlexfecFromVideo(); |
| 408 | |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 409 | void ConfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 410 | void ConfigureFlexfecCodec(int flexfec_payload_type); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 411 | |
| 412 | std::string GetCodecNameFromPayloadType(int payload_type); |
| 413 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 414 | absl::optional<uint32_t> GetFirstPrimarySsrc() const; |
Florent Castelli | abe301f | 2018-06-12 18:33:49 +0200 | [diff] [blame] | 415 | |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 416 | webrtc::Call* const call_; |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 417 | const StreamParams stream_params_; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 418 | |
| 419 | // Both |stream_| and |flexfec_stream_| are managed by |this|. They are |
| 420 | // destroyed by calling call_->DestroyVideoReceiveStream and |
| 421 | // call_->DestroyFlexfecReceiveStream, respectively. |
| 422 | webrtc::VideoReceiveStream* stream_; |
| 423 | const bool default_stream_; |
| 424 | webrtc::VideoReceiveStream::Config config_; |
| 425 | webrtc::FlexfecReceiveStream::Config flexfec_config_; |
| 426 | webrtc::FlexfecReceiveStream* flexfec_stream_; |
| 427 | |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 428 | webrtc::VideoDecoderFactory* const decoder_factory_; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 429 | |
| 430 | rtc::CriticalSection sink_lock_; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 431 | rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_ |
| 432 | RTC_GUARDED_BY(sink_lock_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 433 | // Expands remote RTP timestamps to int64_t to be able to estimate how long |
| 434 | // the stream has been running. |
| 435 | rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_ |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 436 | RTC_GUARDED_BY(sink_lock_); |
| 437 | int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 438 | // Start NTP time is estimated as current remote NTP time (estimated from |
| 439 | // RTCP) minus the elapsed time, as soon as remote NTP time is available. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 440 | int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 441 | }; |
| 442 | |
| 443 | void Construct(webrtc::Call* call, WebRtcVideoEngine* engine); |
| 444 | |
| 445 | bool SendRtp(const uint8_t* data, |
| 446 | size_t len, |
| 447 | const webrtc::PacketOptions& options) override; |
| 448 | bool SendRtcp(const uint8_t* data, size_t len) override; |
| 449 | |
| 450 | static std::vector<VideoCodecSettings> MapCodecs( |
| 451 | const std::vector<VideoCodec>& codecs); |
| 452 | // Select what video codec will be used for sending, i.e. what codec is used |
| 453 | // for local encoding, based on supported remote codecs. The first remote |
| 454 | // codec that is supported locally will be selected. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 455 | absl::optional<VideoCodecSettings> SelectSendVideoCodec( |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 456 | const std::vector<VideoCodecSettings>& remote_mapped_codecs) const; |
| 457 | |
| 458 | static bool NonFlexfecReceiveCodecsHaveChanged( |
| 459 | std::vector<VideoCodecSettings> before, |
| 460 | std::vector<VideoCodecSettings> after); |
| 461 | |
| 462 | void FillSenderStats(VideoMediaInfo* info, bool log_stats); |
| 463 | void FillReceiverStats(VideoMediaInfo* info, bool log_stats); |
| 464 | void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats, |
| 465 | VideoMediaInfo* info); |
| 466 | void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info); |
| 467 | |
| 468 | rtc::ThreadChecker thread_checker_; |
| 469 | |
| 470 | uint32_t rtcp_receiver_report_ssrc_; |
| 471 | bool sending_; |
| 472 | webrtc::Call* const call_; |
| 473 | |
| 474 | DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_; |
| 475 | UnsignalledSsrcHandler* const unsignalled_ssrc_handler_; |
| 476 | |
| 477 | const MediaConfig::Video video_config_; |
| 478 | |
| 479 | rtc::CriticalSection stream_crit_; |
| 480 | // Using primary-ssrc (first ssrc) as key. |
| 481 | std::map<uint32_t, WebRtcVideoSendStream*> send_streams_ |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 482 | RTC_GUARDED_BY(stream_crit_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 483 | std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_ |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 484 | RTC_GUARDED_BY(stream_crit_); |
| 485 | std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(stream_crit_); |
| 486 | std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(stream_crit_); |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 487 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 488 | absl::optional<VideoCodecSettings> send_codec_; |
| 489 | absl::optional<std::vector<webrtc::RtpExtension>> send_rtp_extensions_; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 490 | |
Magnus Jedvert | 07e0d01 | 2017-10-31 11:24:54 +0100 | [diff] [blame] | 491 | webrtc::VideoEncoderFactory* const encoder_factory_; |
Magnus Jedvert | 59ab353 | 2018-09-03 18:07:56 +0200 | [diff] [blame] | 492 | webrtc::VideoDecoderFactory* const decoder_factory_; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 493 | std::vector<VideoCodecSettings> recv_codecs_; |
| 494 | std::vector<webrtc::RtpExtension> recv_rtp_extensions_; |
| 495 | // See reason for keeping track of the FlexFEC payload type separately in |
| 496 | // comment in WebRtcVideoChannel::ChangedRecvParameters. |
| 497 | int recv_flexfec_payload_type_; |
Sebastian Jansson | fc8d26b | 2018-02-21 09:52:06 +0100 | [diff] [blame] | 498 | webrtc::BitrateConstraints bitrate_config_; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 499 | // TODO(deadbeef): Don't duplicate information between |
| 500 | // send_params/recv_params, rtp_extensions, options, etc. |
| 501 | VideoSendParameters send_params_; |
Tim Haloun | 648d28a | 2018-10-18 16:52:22 -0700 | [diff] [blame] | 502 | rtc::DiffServCodePoint preferred_dscp_; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 503 | VideoOptions default_send_options_; |
| 504 | VideoRecvParameters recv_params_; |
| 505 | int64_t last_stats_log_ms_; |
Åsa Persson | 2c7149b | 2018-10-15 09:36:10 +0200 | [diff] [blame] | 506 | const bool discard_unknown_ssrc_packets_; |
Seth Hampson | 5897a6e | 2018-04-03 11:16:33 -0700 | [diff] [blame] | 507 | // This is a stream param that comes from the remote description, but wasn't |
| 508 | // signaled with any a=ssrc lines. It holds information that was signaled |
| 509 | // before the unsignaled receive stream is created when the first packet is |
| 510 | // received. |
| 511 | StreamParams unsignaled_stream_params_; |
Benjamin Wright | 192eeec | 2018-10-17 17:27:25 -0700 | [diff] [blame] | 512 | // Per peer connection crypto options that last for the lifetime of the peer |
| 513 | // connection. |
| 514 | const webrtc::CryptoOptions crypto_options_; |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 515 | }; |
| 516 | |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 517 | class EncoderStreamFactory |
| 518 | : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface { |
| 519 | public: |
| 520 | EncoderStreamFactory(std::string codec_name, |
| 521 | int max_qp, |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 522 | bool is_screenshare, |
| 523 | bool screenshare_config_explicitly_enabled); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 524 | |
| 525 | private: |
| 526 | std::vector<webrtc::VideoStream> CreateEncoderStreams( |
| 527 | int width, |
| 528 | int height, |
| 529 | const webrtc::VideoEncoderConfig& encoder_config) override; |
| 530 | |
| 531 | const std::string codec_name_; |
| 532 | const int max_qp_; |
Seth Hampson | 1370e30 | 2018-02-07 08:50:36 -0800 | [diff] [blame] | 533 | const bool is_screenshare_; |
| 534 | // Allows a screenshare specific configuration, which enables temporal |
| 535 | // layering and allows simulcast. |
| 536 | const bool screenshare_config_explicitly_enabled_; |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 537 | }; |
| 538 | |
eladalon | f184138 | 2017-06-12 01:16:46 -0700 | [diff] [blame] | 539 | } // namespace cricket |
| 540 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 541 | #endif // MEDIA_ENGINE_WEBRTCVIDEOENGINE_H_ |