webrtc_video_engine.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. #ifndef MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_
  11. #define MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_
  12. #include <map>
  13. #include <memory>
  14. #include <set>
  15. #include <string>
  16. #include <vector>
  17. #include "absl/types/optional.h"
  18. #include "api/call/transport.h"
  19. #include "api/transport/field_trial_based_config.h"
  20. #include "api/video/video_bitrate_allocator_factory.h"
  21. #include "api/video/video_frame.h"
  22. #include "api/video/video_sink_interface.h"
  23. #include "api/video/video_source_interface.h"
  24. #include "api/video_codecs/sdp_video_format.h"
  25. #include "call/call.h"
  26. #include "call/flexfec_receive_stream.h"
  27. #include "call/video_receive_stream.h"
  28. #include "call/video_send_stream.h"
  29. #include "media/base/media_engine.h"
  30. #include "media/engine/constants.h"
  31. #include "media/engine/unhandled_packets_buffer.h"
  32. #include "rtc_base/network_route.h"
  33. #include "rtc_base/synchronization/mutex.h"
  34. #include "rtc_base/thread_annotations.h"
  35. #include "rtc_base/thread_checker.h"
  36. namespace webrtc {
  37. class VideoDecoderFactory;
  38. class VideoEncoderFactory;
  39. struct MediaConfig;
  40. } // namespace webrtc
  41. namespace rtc {
  42. class Thread;
  43. } // namespace rtc
  44. namespace cricket {
  45. class WebRtcVideoChannel;
  46. // Public for testing.
  47. // Inputs StreamStats for all types of substreams (kMedia, kRtx, kFlexfec) and
  48. // merges any non-kMedia substream stats object into its referenced kMedia-type
  49. // substream. The resulting substreams are all kMedia. This means, for example,
  50. // that packet and byte counters of RTX and FlexFEC streams are accounted for in
  51. // the relevant RTP media stream's stats. This makes the resulting StreamStats
  52. // objects ready to be turned into "outbound-rtp" stats objects for GetStats()
  53. // which does not create separate stream stats objects for complementary
  54. // streams.
  55. std::map<uint32_t, webrtc::VideoSendStream::StreamStats>
  56. MergeInfoAboutOutboundRtpSubstreamsForTesting(
  57. const std::map<uint32_t, webrtc::VideoSendStream::StreamStats>& substreams);
  58. class UnsignalledSsrcHandler {
  59. public:
  60. enum Action {
  61. kDropPacket,
  62. kDeliverPacket,
  63. };
  64. virtual Action OnUnsignalledSsrc(WebRtcVideoChannel* channel,
  65. uint32_t ssrc) = 0;
  66. virtual ~UnsignalledSsrcHandler() = default;
  67. };
  68. // TODO(pbos): Remove, use external handlers only.
  69. class DefaultUnsignalledSsrcHandler : public UnsignalledSsrcHandler {
  70. public:
  71. DefaultUnsignalledSsrcHandler();
  72. Action OnUnsignalledSsrc(WebRtcVideoChannel* channel, uint32_t ssrc) override;
  73. rtc::VideoSinkInterface<webrtc::VideoFrame>* GetDefaultSink() const;
  74. void SetDefaultSink(WebRtcVideoChannel* channel,
  75. rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
  76. virtual ~DefaultUnsignalledSsrcHandler() = default;
  77. private:
  78. rtc::VideoSinkInterface<webrtc::VideoFrame>* default_sink_;
  79. };
  80. // WebRtcVideoEngine is used for the new native WebRTC Video API (webrtc:1667).
  81. class WebRtcVideoEngine : public VideoEngineInterface {
  82. public:
  83. // These video codec factories represents all video codecs, i.e. both software
  84. // and external hardware codecs.
  85. WebRtcVideoEngine(
  86. std::unique_ptr<webrtc::VideoEncoderFactory> video_encoder_factory,
  87. std::unique_ptr<webrtc::VideoDecoderFactory> video_decoder_factory,
  88. const webrtc::WebRtcKeyValueConfig& trials);
  89. ~WebRtcVideoEngine() override;
  90. VideoMediaChannel* CreateMediaChannel(
  91. webrtc::Call* call,
  92. const MediaConfig& config,
  93. const VideoOptions& options,
  94. const webrtc::CryptoOptions& crypto_options,
  95. webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory)
  96. override;
  97. std::vector<VideoCodec> send_codecs() const override;
  98. std::vector<VideoCodec> recv_codecs() const override;
  99. std::vector<webrtc::RtpHeaderExtensionCapability> GetRtpHeaderExtensions()
  100. const override;
  101. private:
  102. const std::unique_ptr<webrtc::VideoDecoderFactory> decoder_factory_;
  103. const std::unique_ptr<webrtc::VideoEncoderFactory> encoder_factory_;
  104. const std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
  105. bitrate_allocator_factory_;
  106. const webrtc::WebRtcKeyValueConfig& trials_;
  107. };
  108. class WebRtcVideoChannel : public VideoMediaChannel,
  109. public webrtc::Transport,
  110. public webrtc::EncoderSwitchRequestCallback {
  111. public:
  112. WebRtcVideoChannel(
  113. webrtc::Call* call,
  114. const MediaConfig& config,
  115. const VideoOptions& options,
  116. const webrtc::CryptoOptions& crypto_options,
  117. webrtc::VideoEncoderFactory* encoder_factory,
  118. webrtc::VideoDecoderFactory* decoder_factory,
  119. webrtc::VideoBitrateAllocatorFactory* bitrate_allocator_factory);
  120. ~WebRtcVideoChannel() override;
  121. // VideoMediaChannel implementation
  122. bool SetSendParameters(const VideoSendParameters& params) override;
  123. bool SetRecvParameters(const VideoRecvParameters& params) override;
  124. webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const override;
  125. webrtc::RTCError SetRtpSendParameters(
  126. uint32_t ssrc,
  127. const webrtc::RtpParameters& parameters) override;
  128. webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const override;
  129. webrtc::RtpParameters GetDefaultRtpReceiveParameters() const override;
  130. bool GetSendCodec(VideoCodec* send_codec) override;
  131. bool SetSend(bool send) override;
  132. bool SetVideoSend(
  133. uint32_t ssrc,
  134. const VideoOptions* options,
  135. rtc::VideoSourceInterface<webrtc::VideoFrame>* source) override;
  136. bool AddSendStream(const StreamParams& sp) override;
  137. bool RemoveSendStream(uint32_t ssrc) override;
  138. bool AddRecvStream(const StreamParams& sp) override;
  139. bool AddRecvStream(const StreamParams& sp, bool default_stream);
  140. bool RemoveRecvStream(uint32_t ssrc) override;
  141. void ResetUnsignaledRecvStream() override;
  142. bool SetSink(uint32_t ssrc,
  143. rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
  144. void SetDefaultSink(
  145. rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
  146. void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) override;
  147. bool GetStats(VideoMediaInfo* info) override;
  148. void OnPacketReceived(rtc::CopyOnWriteBuffer packet,
  149. int64_t packet_time_us) override;
  150. void OnReadyToSend(bool ready) override;
  151. void OnNetworkRouteChanged(const std::string& transport_name,
  152. const rtc::NetworkRoute& network_route) override;
  153. void SetInterface(NetworkInterface* iface) override;
  154. // E2E Encrypted Video Frame API
  155. // Set a frame decryptor to a particular ssrc that will intercept all
  156. // incoming video frames and attempt to decrypt them before forwarding the
  157. // result.
  158. void SetFrameDecryptor(uint32_t ssrc,
  159. rtc::scoped_refptr<webrtc::FrameDecryptorInterface>
  160. frame_decryptor) override;
  161. // Set a frame encryptor to a particular ssrc that will intercept all
  162. // outgoing video frames and attempt to encrypt them and forward the result
  163. // to the packetizer.
  164. void SetFrameEncryptor(uint32_t ssrc,
  165. rtc::scoped_refptr<webrtc::FrameEncryptorInterface>
  166. frame_encryptor) override;
  167. void SetVideoCodecSwitchingEnabled(bool enabled) override;
  168. bool SetBaseMinimumPlayoutDelayMs(uint32_t ssrc, int delay_ms) override;
  169. absl::optional<int> GetBaseMinimumPlayoutDelayMs(
  170. uint32_t ssrc) const override;
  171. // Implemented for VideoMediaChannelTest.
  172. bool sending() const {
  173. RTC_DCHECK_RUN_ON(&thread_checker_);
  174. return sending_;
  175. }
  176. absl::optional<uint32_t> GetDefaultReceiveStreamSsrc();
  177. StreamParams unsignaled_stream_params() {
  178. RTC_DCHECK_RUN_ON(&thread_checker_);
  179. return unsignaled_stream_params_;
  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. static constexpr int kDefaultQpMax = 56;
  190. std::vector<webrtc::RtpSource> GetSources(uint32_t ssrc) const override;
  191. // Take the buffered packets for |ssrcs| and feed them into DeliverPacket.
  192. // This method does nothing unless unknown_ssrc_packet_buffer_ is configured.
  193. void BackfillBufferedPackets(rtc::ArrayView<const uint32_t> ssrcs);
  194. // Implements webrtc::EncoderSwitchRequestCallback.
  195. void RequestEncoderFallback() override;
  196. // TODO(bugs.webrtc.org/11341) : Remove this version of RequestEncoderSwitch.
  197. void RequestEncoderSwitch(
  198. const EncoderSwitchRequestCallback::Config& conf) override;
  199. void RequestEncoderSwitch(const webrtc::SdpVideoFormat& format) override;
  200. void SetRecordableEncodedFrameCallback(
  201. uint32_t ssrc,
  202. std::function<void(const webrtc::RecordableEncodedFrame&)> callback)
  203. override;
  204. void ClearRecordableEncodedFrameCallback(uint32_t ssrc) override;
  205. void GenerateKeyFrame(uint32_t ssrc) override;
  206. void SetEncoderToPacketizerFrameTransformer(
  207. uint32_t ssrc,
  208. rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)
  209. override;
  210. void SetDepacketizerToDecoderFrameTransformer(
  211. uint32_t ssrc,
  212. rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)
  213. override;
  214. private:
  215. class WebRtcVideoReceiveStream;
  216. // Finds VideoReceiveStream corresponding to ssrc. Aware of unsignalled ssrc
  217. // handling.
  218. WebRtcVideoReceiveStream* FindReceiveStream(uint32_t ssrc)
  219. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  220. struct VideoCodecSettings {
  221. VideoCodecSettings();
  222. // Checks if all members of |*this| are equal to the corresponding members
  223. // of |other|.
  224. bool operator==(const VideoCodecSettings& other) const;
  225. bool operator!=(const VideoCodecSettings& other) const;
  226. // Checks if all members of |a|, except |flexfec_payload_type|, are equal
  227. // to the corresponding members of |b|.
  228. static bool EqualsDisregardingFlexfec(const VideoCodecSettings& a,
  229. const VideoCodecSettings& b);
  230. VideoCodec codec;
  231. webrtc::UlpfecConfig ulpfec;
  232. int flexfec_payload_type; // -1 if absent.
  233. int rtx_payload_type; // -1 if absent.
  234. };
  235. struct ChangedSendParameters {
  236. // These optionals are unset if not changed.
  237. absl::optional<VideoCodecSettings> send_codec;
  238. absl::optional<std::vector<VideoCodecSettings>> negotiated_codecs;
  239. absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
  240. absl::optional<std::string> mid;
  241. absl::optional<bool> extmap_allow_mixed;
  242. absl::optional<int> max_bandwidth_bps;
  243. absl::optional<bool> conference_mode;
  244. absl::optional<webrtc::RtcpMode> rtcp_mode;
  245. };
  246. struct ChangedRecvParameters {
  247. // These optionals are unset if not changed.
  248. absl::optional<std::vector<VideoCodecSettings>> codec_settings;
  249. absl::optional<std::vector<webrtc::RtpExtension>> rtp_header_extensions;
  250. // Keep track of the FlexFEC payload type separately from |codec_settings|.
  251. // This allows us to recreate the FlexfecReceiveStream separately from the
  252. // VideoReceiveStream when the FlexFEC payload type is changed.
  253. absl::optional<int> flexfec_payload_type;
  254. };
  255. bool GetChangedSendParameters(const VideoSendParameters& params,
  256. ChangedSendParameters* changed_params) const
  257. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  258. bool ApplyChangedParams(const ChangedSendParameters& changed_params);
  259. bool GetChangedRecvParameters(const VideoRecvParameters& params,
  260. ChangedRecvParameters* changed_params) const
  261. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  262. void ConfigureReceiverRtp(
  263. webrtc::VideoReceiveStream::Config* config,
  264. webrtc::FlexfecReceiveStream::Config* flexfec_config,
  265. const StreamParams& sp) const
  266. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  267. bool ValidateSendSsrcAvailability(const StreamParams& sp) const
  268. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  269. bool ValidateReceiveSsrcAvailability(const StreamParams& sp) const
  270. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  271. void DeleteReceiveStream(WebRtcVideoReceiveStream* stream)
  272. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  273. static std::string CodecSettingsVectorToString(
  274. const std::vector<VideoCodecSettings>& codecs);
  275. // Wrapper for the sender part.
  276. class WebRtcVideoSendStream {
  277. public:
  278. WebRtcVideoSendStream(
  279. webrtc::Call* call,
  280. const StreamParams& sp,
  281. webrtc::VideoSendStream::Config config,
  282. const VideoOptions& options,
  283. bool enable_cpu_overuse_detection,
  284. int max_bitrate_bps,
  285. const absl::optional<VideoCodecSettings>& codec_settings,
  286. const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions,
  287. const VideoSendParameters& send_params);
  288. ~WebRtcVideoSendStream();
  289. void SetSendParameters(const ChangedSendParameters& send_params);
  290. webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters);
  291. webrtc::RtpParameters GetRtpParameters() const;
  292. void SetFrameEncryptor(
  293. rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor);
  294. bool SetVideoSend(const VideoOptions* options,
  295. rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
  296. void SetSend(bool send);
  297. const std::vector<uint32_t>& GetSsrcs() const;
  298. // Returns per ssrc VideoSenderInfos. Useful for simulcast scenario.
  299. std::vector<VideoSenderInfo> GetPerLayerVideoSenderInfos(bool log_stats);
  300. // Aggregates per ssrc VideoSenderInfos to single VideoSenderInfo for
  301. // legacy reasons. Used in old GetStats API and track stats.
  302. VideoSenderInfo GetAggregatedVideoSenderInfo(
  303. const std::vector<VideoSenderInfo>& infos) const;
  304. void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
  305. void SetEncoderToPacketizerFrameTransformer(
  306. rtc::scoped_refptr<webrtc::FrameTransformerInterface>
  307. frame_transformer);
  308. private:
  309. // Parameters needed to reconstruct the underlying stream.
  310. // webrtc::VideoSendStream doesn't support setting a lot of options on the
  311. // fly, so when those need to be changed we tear down and reconstruct with
  312. // similar parameters depending on which options changed etc.
  313. struct VideoSendStreamParameters {
  314. VideoSendStreamParameters(
  315. webrtc::VideoSendStream::Config config,
  316. const VideoOptions& options,
  317. int max_bitrate_bps,
  318. const absl::optional<VideoCodecSettings>& codec_settings);
  319. webrtc::VideoSendStream::Config config;
  320. VideoOptions options;
  321. int max_bitrate_bps;
  322. bool conference_mode;
  323. absl::optional<VideoCodecSettings> codec_settings;
  324. // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
  325. // typically changes when setting a new resolution or reconfiguring
  326. // bitrates.
  327. webrtc::VideoEncoderConfig encoder_config;
  328. };
  329. rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
  330. ConfigureVideoEncoderSettings(const VideoCodec& codec);
  331. void SetCodec(const VideoCodecSettings& codec);
  332. void RecreateWebRtcStream();
  333. webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
  334. const VideoCodec& codec) const;
  335. void ReconfigureEncoder();
  336. // Calls Start or Stop according to whether or not |sending_| is true,
  337. // and whether or not the encoding in |rtp_parameters_| is active.
  338. void UpdateSendState();
  339. webrtc::DegradationPreference GetDegradationPreference() const
  340. RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_);
  341. rtc::ThreadChecker thread_checker_;
  342. rtc::Thread* worker_thread_;
  343. const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_);
  344. const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_);
  345. webrtc::Call* const call_;
  346. const bool enable_cpu_overuse_detection_;
  347. rtc::VideoSourceInterface<webrtc::VideoFrame>* source_
  348. RTC_GUARDED_BY(&thread_checker_);
  349. webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_);
  350. // Contains settings that are the same for all streams in the MediaChannel,
  351. // such as codecs, header extensions, and the global bitrate limit for the
  352. // entire channel.
  353. VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_);
  354. // Contains settings that are unique for each stream, such as max_bitrate.
  355. // Does *not* contain codecs, however.
  356. // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_.
  357. // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only
  358. // one stream per MediaChannel.
  359. webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_);
  360. bool sending_ RTC_GUARDED_BY(&thread_checker_);
  361. // TODO(asapersson): investigate why setting
  362. // DegrationPreferences::MAINTAIN_RESOLUTION isn't sufficient to disable
  363. // downscaling everywhere in the pipeline.
  364. const bool disable_automatic_resize_;
  365. };
  366. // Wrapper for the receiver part, contains configs etc. that are needed to
  367. // reconstruct the underlying VideoReceiveStream.
  368. class WebRtcVideoReceiveStream
  369. : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
  370. public:
  371. WebRtcVideoReceiveStream(
  372. WebRtcVideoChannel* channel,
  373. webrtc::Call* call,
  374. const StreamParams& sp,
  375. webrtc::VideoReceiveStream::Config config,
  376. webrtc::VideoDecoderFactory* decoder_factory,
  377. bool default_stream,
  378. const std::vector<VideoCodecSettings>& recv_codecs,
  379. const webrtc::FlexfecReceiveStream::Config& flexfec_config);
  380. ~WebRtcVideoReceiveStream();
  381. const std::vector<uint32_t>& GetSsrcs() const;
  382. std::vector<webrtc::RtpSource> GetSources();
  383. // Does not return codecs, they are filled by the owning WebRtcVideoChannel.
  384. webrtc::RtpParameters GetRtpParameters() const;
  385. void SetLocalSsrc(uint32_t local_ssrc);
  386. // TODO(deadbeef): Move these feedback parameters into the recv parameters.
  387. void SetFeedbackParameters(bool lntf_enabled,
  388. bool nack_enabled,
  389. bool transport_cc_enabled,
  390. webrtc::RtcpMode rtcp_mode);
  391. void SetRecvParameters(const ChangedRecvParameters& recv_params);
  392. void OnFrame(const webrtc::VideoFrame& frame) override;
  393. bool IsDefaultStream() const;
  394. void SetFrameDecryptor(
  395. rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor);
  396. bool SetBaseMinimumPlayoutDelayMs(int delay_ms);
  397. int GetBaseMinimumPlayoutDelayMs() const;
  398. void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
  399. VideoReceiverInfo GetVideoReceiverInfo(bool log_stats);
  400. void SetRecordableEncodedFrameCallback(
  401. std::function<void(const webrtc::RecordableEncodedFrame&)> callback);
  402. void ClearRecordableEncodedFrameCallback();
  403. void GenerateKeyFrame();
  404. void SetDepacketizerToDecoderFrameTransformer(
  405. rtc::scoped_refptr<webrtc::FrameTransformerInterface>
  406. frame_transformer);
  407. private:
  408. void RecreateWebRtcVideoStream();
  409. void MaybeRecreateWebRtcFlexfecStream();
  410. void MaybeAssociateFlexfecWithVideo();
  411. void MaybeDissociateFlexfecFromVideo();
  412. void ConfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
  413. void ConfigureFlexfecCodec(int flexfec_payload_type);
  414. std::string GetCodecNameFromPayloadType(int payload_type);
  415. WebRtcVideoChannel* const channel_;
  416. webrtc::Call* const call_;
  417. const StreamParams stream_params_;
  418. // Both |stream_| and |flexfec_stream_| are managed by |this|. They are
  419. // destroyed by calling call_->DestroyVideoReceiveStream and
  420. // call_->DestroyFlexfecReceiveStream, respectively.
  421. webrtc::VideoReceiveStream* stream_;
  422. const bool default_stream_;
  423. webrtc::VideoReceiveStream::Config config_;
  424. webrtc::FlexfecReceiveStream::Config flexfec_config_;
  425. webrtc::FlexfecReceiveStream* flexfec_stream_;
  426. webrtc::VideoDecoderFactory* const decoder_factory_;
  427. webrtc::Mutex sink_lock_;
  428. rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_
  429. RTC_GUARDED_BY(sink_lock_);
  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_
  433. RTC_GUARDED_BY(sink_lock_);
  434. int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_);
  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.
  437. int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_);
  438. };
  439. void Construct(webrtc::Call* call, WebRtcVideoEngine* engine);
  440. bool SendRtp(const uint8_t* data,
  441. size_t len,
  442. const webrtc::PacketOptions& options) override;
  443. bool SendRtcp(const uint8_t* data, size_t len) override;
  444. // Generate the list of codec parameters to pass down based on the negotiated
  445. // "codecs". Note that VideoCodecSettings correspond to concrete codecs like
  446. // VP8, VP9, H264 while VideoCodecs correspond also to "virtual" codecs like
  447. // RTX, ULPFEC, FLEXFEC.
  448. static std::vector<VideoCodecSettings> MapCodecs(
  449. const std::vector<VideoCodec>& codecs);
  450. // Get all codecs that are compatible with the receiver.
  451. std::vector<VideoCodecSettings> SelectSendVideoCodecs(
  452. const std::vector<VideoCodecSettings>& remote_mapped_codecs) const
  453. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  454. static bool NonFlexfecReceiveCodecsHaveChanged(
  455. std::vector<VideoCodecSettings> before,
  456. std::vector<VideoCodecSettings> after);
  457. void FillSenderStats(VideoMediaInfo* info, bool log_stats)
  458. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  459. void FillReceiverStats(VideoMediaInfo* info, bool log_stats)
  460. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  461. void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
  462. VideoMediaInfo* info)
  463. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  464. void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info)
  465. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  466. rtc::Thread* const worker_thread_;
  467. rtc::ThreadChecker thread_checker_;
  468. uint32_t rtcp_receiver_report_ssrc_ RTC_GUARDED_BY(thread_checker_);
  469. bool sending_ RTC_GUARDED_BY(thread_checker_);
  470. webrtc::Call* const call_ RTC_GUARDED_BY(thread_checker_);
  471. DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_
  472. RTC_GUARDED_BY(thread_checker_);
  473. UnsignalledSsrcHandler* const unsignalled_ssrc_handler_
  474. RTC_GUARDED_BY(thread_checker_);
  475. // Delay for unsignaled streams, which may be set before the stream exists.
  476. int default_recv_base_minimum_delay_ms_ RTC_GUARDED_BY(thread_checker_) = 0;
  477. const MediaConfig::Video video_config_ RTC_GUARDED_BY(thread_checker_);
  478. // Using primary-ssrc (first ssrc) as key.
  479. std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
  480. RTC_GUARDED_BY(thread_checker_);
  481. std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
  482. RTC_GUARDED_BY(thread_checker_);
  483. std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(thread_checker_);
  484. std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(thread_checker_);
  485. absl::optional<VideoCodecSettings> send_codec_
  486. RTC_GUARDED_BY(thread_checker_);
  487. std::vector<VideoCodecSettings> negotiated_codecs_
  488. RTC_GUARDED_BY(thread_checker_);
  489. absl::optional<std::vector<webrtc::RtpExtension>> send_rtp_extensions_
  490. RTC_GUARDED_BY(thread_checker_);
  491. webrtc::VideoEncoderFactory* const encoder_factory_
  492. RTC_GUARDED_BY(thread_checker_);
  493. webrtc::VideoDecoderFactory* const decoder_factory_
  494. RTC_GUARDED_BY(thread_checker_);
  495. webrtc::VideoBitrateAllocatorFactory* const bitrate_allocator_factory_
  496. RTC_GUARDED_BY(thread_checker_);
  497. std::vector<VideoCodecSettings> recv_codecs_ RTC_GUARDED_BY(thread_checker_);
  498. std::vector<webrtc::RtpExtension> recv_rtp_extensions_
  499. RTC_GUARDED_BY(thread_checker_);
  500. // See reason for keeping track of the FlexFEC payload type separately in
  501. // comment in WebRtcVideoChannel::ChangedRecvParameters.
  502. int recv_flexfec_payload_type_ RTC_GUARDED_BY(thread_checker_);
  503. webrtc::BitrateConstraints bitrate_config_ RTC_GUARDED_BY(thread_checker_);
  504. // TODO(deadbeef): Don't duplicate information between
  505. // send_params/recv_params, rtp_extensions, options, etc.
  506. VideoSendParameters send_params_ RTC_GUARDED_BY(thread_checker_);
  507. VideoOptions default_send_options_ RTC_GUARDED_BY(thread_checker_);
  508. VideoRecvParameters recv_params_ RTC_GUARDED_BY(thread_checker_);
  509. int64_t last_stats_log_ms_ RTC_GUARDED_BY(thread_checker_);
  510. const bool discard_unknown_ssrc_packets_ RTC_GUARDED_BY(thread_checker_);
  511. // This is a stream param that comes from the remote description, but wasn't
  512. // signaled with any a=ssrc lines. It holds information that was signaled
  513. // before the unsignaled receive stream is created when the first packet is
  514. // received.
  515. StreamParams unsignaled_stream_params_ RTC_GUARDED_BY(thread_checker_);
  516. // Per peer connection crypto options that last for the lifetime of the peer
  517. // connection.
  518. const webrtc::CryptoOptions crypto_options_ RTC_GUARDED_BY(thread_checker_);
  519. // Optional frame transformer set on unsignaled streams.
  520. rtc::scoped_refptr<webrtc::FrameTransformerInterface>
  521. unsignaled_frame_transformer_ RTC_GUARDED_BY(thread_checker_);
  522. // Buffer for unhandled packets.
  523. std::unique_ptr<UnhandledPacketsBuffer> unknown_ssrc_packet_buffer_
  524. RTC_GUARDED_BY(thread_checker_);
  525. bool allow_codec_switching_ = false;
  526. absl::optional<EncoderSwitchRequestCallback::Config>
  527. requested_encoder_switch_;
  528. };
  529. class EncoderStreamFactory
  530. : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
  531. public:
  532. EncoderStreamFactory(std::string codec_name,
  533. int max_qp,
  534. bool is_screenshare,
  535. bool conference_mode)
  536. : EncoderStreamFactory(codec_name,
  537. max_qp,
  538. is_screenshare,
  539. conference_mode,
  540. nullptr) {}
  541. EncoderStreamFactory(std::string codec_name,
  542. int max_qp,
  543. bool is_screenshare,
  544. bool conference_mode,
  545. const webrtc::WebRtcKeyValueConfig* trials);
  546. private:
  547. std::vector<webrtc::VideoStream> CreateEncoderStreams(
  548. int width,
  549. int height,
  550. const webrtc::VideoEncoderConfig& encoder_config) override;
  551. std::vector<webrtc::VideoStream> CreateDefaultVideoStreams(
  552. int width,
  553. int height,
  554. const webrtc::VideoEncoderConfig& encoder_config,
  555. const absl::optional<webrtc::DataRate>& experimental_min_bitrate) const;
  556. std::vector<webrtc::VideoStream>
  557. CreateSimulcastOrConferenceModeScreenshareStreams(
  558. int width,
  559. int height,
  560. const webrtc::VideoEncoderConfig& encoder_config,
  561. const absl::optional<webrtc::DataRate>& experimental_min_bitrate) const;
  562. const std::string codec_name_;
  563. const int max_qp_;
  564. const bool is_screenshare_;
  565. // Allows a screenshare specific configuration, which enables temporal
  566. // layering and various settings.
  567. const bool conference_mode_;
  568. const webrtc::FieldTrialBasedConfig fallback_trials_;
  569. const webrtc::WebRtcKeyValueConfig& trials_;
  570. };
  571. } // namespace cricket
  572. #endif // MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_