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