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