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