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