webrtc_video_engine.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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/video/video_bitrate_allocator_factory.h"
  20. #include "api/video/video_frame.h"
  21. #include "api/video/video_sink_interface.h"
  22. #include "api/video/video_source_interface.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/media_engine.h"
  29. #include "media/engine/constants.h"
  30. #include "media/engine/unhandled_packets_buffer.h"
  31. #include "rtc_base/async_invoker.h"
  32. #include "rtc_base/critical_section.h"
  33. #include "rtc_base/network_route.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. ~WebRtcVideoEngine() override;
  89. VideoMediaChannel* CreateMediaChannel(
  90. webrtc::Call* call,
  91. const MediaConfig& config,
  92. const VideoOptions& options,
  93. const webrtc::CryptoOptions& crypto_options,
  94. webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory)
  95. override;
  96. std::vector<VideoCodec> send_codecs() const override;
  97. std::vector<VideoCodec> recv_codecs() const override;
  98. std::vector<webrtc::RtpHeaderExtensionCapability> GetRtpHeaderExtensions()
  99. const override;
  100. private:
  101. const std::unique_ptr<webrtc::VideoDecoderFactory> decoder_factory_;
  102. const std::unique_ptr<webrtc::VideoEncoderFactory> encoder_factory_;
  103. const std::unique_ptr<webrtc::VideoBitrateAllocatorFactory>
  104. bitrate_allocator_factory_;
  105. };
  106. class WebRtcVideoChannel : public VideoMediaChannel,
  107. public webrtc::Transport,
  108. public webrtc::EncoderSwitchRequestCallback {
  109. public:
  110. WebRtcVideoChannel(
  111. webrtc::Call* call,
  112. const MediaConfig& config,
  113. const VideoOptions& options,
  114. const webrtc::CryptoOptions& crypto_options,
  115. webrtc::VideoEncoderFactory* encoder_factory,
  116. webrtc::VideoDecoderFactory* decoder_factory,
  117. webrtc::VideoBitrateAllocatorFactory* bitrate_allocator_factory);
  118. ~WebRtcVideoChannel() override;
  119. // VideoMediaChannel implementation
  120. bool SetSendParameters(const VideoSendParameters& params) override;
  121. bool SetRecvParameters(const VideoRecvParameters& params) override;
  122. webrtc::RtpParameters GetRtpSendParameters(uint32_t ssrc) const override;
  123. webrtc::RTCError SetRtpSendParameters(
  124. uint32_t ssrc,
  125. const webrtc::RtpParameters& parameters) override;
  126. webrtc::RtpParameters GetRtpReceiveParameters(uint32_t ssrc) const override;
  127. webrtc::RtpParameters GetDefaultRtpReceiveParameters() const override;
  128. bool GetSendCodec(VideoCodec* send_codec) override;
  129. bool SetSend(bool send) override;
  130. bool SetVideoSend(
  131. uint32_t ssrc,
  132. const VideoOptions* options,
  133. rtc::VideoSourceInterface<webrtc::VideoFrame>* source) override;
  134. bool AddSendStream(const StreamParams& sp) override;
  135. bool RemoveSendStream(uint32_t ssrc) override;
  136. bool AddRecvStream(const StreamParams& sp) override;
  137. bool AddRecvStream(const StreamParams& sp, bool default_stream);
  138. bool RemoveRecvStream(uint32_t ssrc) override;
  139. void ResetUnsignaledRecvStream() override;
  140. bool SetSink(uint32_t ssrc,
  141. rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
  142. void SetDefaultSink(
  143. rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
  144. void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) override;
  145. bool GetStats(VideoMediaInfo* info) override;
  146. void OnPacketReceived(rtc::CopyOnWriteBuffer packet,
  147. int64_t packet_time_us) override;
  148. void OnReadyToSend(bool ready) override;
  149. void OnNetworkRouteChanged(const std::string& transport_name,
  150. const rtc::NetworkRoute& network_route) override;
  151. void SetInterface(
  152. NetworkInterface* iface,
  153. const webrtc::MediaTransportConfig& media_transport_config) 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 rtc::VideoSourceInterface<webrtc::VideoFrame> {
  278. public:
  279. WebRtcVideoSendStream(
  280. webrtc::Call* call,
  281. const StreamParams& sp,
  282. webrtc::VideoSendStream::Config config,
  283. const VideoOptions& options,
  284. bool enable_cpu_overuse_detection,
  285. int max_bitrate_bps,
  286. const absl::optional<VideoCodecSettings>& codec_settings,
  287. const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions,
  288. const VideoSendParameters& send_params);
  289. virtual ~WebRtcVideoSendStream();
  290. void SetSendParameters(const ChangedSendParameters& send_params);
  291. webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters);
  292. webrtc::RtpParameters GetRtpParameters() const;
  293. void SetFrameEncryptor(
  294. rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor);
  295. // Implements rtc::VideoSourceInterface<webrtc::VideoFrame>.
  296. // WebRtcVideoSendStream acts as a source to the webrtc::VideoSendStream
  297. // in |stream_|. This is done to proxy VideoSinkWants from the encoder to
  298. // the worker thread.
  299. void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
  300. const rtc::VideoSinkWants& wants) override;
  301. void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
  302. bool SetVideoSend(const VideoOptions* options,
  303. rtc::VideoSourceInterface<webrtc::VideoFrame>* source);
  304. void SetSend(bool send);
  305. const std::vector<uint32_t>& GetSsrcs() const;
  306. // Returns per ssrc VideoSenderInfos. Useful for simulcast scenario.
  307. std::vector<VideoSenderInfo> GetPerLayerVideoSenderInfos(bool log_stats);
  308. // Aggregates per ssrc VideoSenderInfos to single VideoSenderInfo for
  309. // legacy reasons. Used in old GetStats API and track stats.
  310. VideoSenderInfo GetAggregatedVideoSenderInfo(
  311. const std::vector<VideoSenderInfo>& infos) const;
  312. void FillBitrateInfo(BandwidthEstimationInfo* bwe_info);
  313. void SetEncoderToPacketizerFrameTransformer(
  314. rtc::scoped_refptr<webrtc::FrameTransformerInterface>
  315. frame_transformer);
  316. private:
  317. // Parameters needed to reconstruct the underlying stream.
  318. // webrtc::VideoSendStream doesn't support setting a lot of options on the
  319. // fly, so when those need to be changed we tear down and reconstruct with
  320. // similar parameters depending on which options changed etc.
  321. struct VideoSendStreamParameters {
  322. VideoSendStreamParameters(
  323. webrtc::VideoSendStream::Config config,
  324. const VideoOptions& options,
  325. int max_bitrate_bps,
  326. const absl::optional<VideoCodecSettings>& codec_settings);
  327. webrtc::VideoSendStream::Config config;
  328. VideoOptions options;
  329. int max_bitrate_bps;
  330. bool conference_mode;
  331. absl::optional<VideoCodecSettings> codec_settings;
  332. // Sent resolutions + bitrates etc. by the underlying VideoSendStream,
  333. // typically changes when setting a new resolution or reconfiguring
  334. // bitrates.
  335. webrtc::VideoEncoderConfig encoder_config;
  336. };
  337. rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings>
  338. ConfigureVideoEncoderSettings(const VideoCodec& codec);
  339. void SetCodec(const VideoCodecSettings& codec);
  340. void RecreateWebRtcStream();
  341. webrtc::VideoEncoderConfig CreateVideoEncoderConfig(
  342. const VideoCodec& codec) const;
  343. void ReconfigureEncoder();
  344. // Calls Start or Stop according to whether or not |sending_| is true,
  345. // and whether or not the encoding in |rtp_parameters_| is active.
  346. void UpdateSendState();
  347. webrtc::DegradationPreference GetDegradationPreference() const
  348. RTC_EXCLUSIVE_LOCKS_REQUIRED(&thread_checker_);
  349. rtc::ThreadChecker thread_checker_;
  350. rtc::Thread* worker_thread_;
  351. const std::vector<uint32_t> ssrcs_ RTC_GUARDED_BY(&thread_checker_);
  352. const std::vector<SsrcGroup> ssrc_groups_ RTC_GUARDED_BY(&thread_checker_);
  353. webrtc::Call* const call_;
  354. const bool enable_cpu_overuse_detection_;
  355. rtc::VideoSourceInterface<webrtc::VideoFrame>* source_
  356. RTC_GUARDED_BY(&thread_checker_);
  357. webrtc::VideoSendStream* stream_ RTC_GUARDED_BY(&thread_checker_);
  358. rtc::VideoSinkInterface<webrtc::VideoFrame>* encoder_sink_
  359. RTC_GUARDED_BY(&thread_checker_);
  360. // Contains settings that are the same for all streams in the MediaChannel,
  361. // such as codecs, header extensions, and the global bitrate limit for the
  362. // entire channel.
  363. VideoSendStreamParameters parameters_ RTC_GUARDED_BY(&thread_checker_);
  364. // Contains settings that are unique for each stream, such as max_bitrate.
  365. // Does *not* contain codecs, however.
  366. // TODO(skvlad): Move ssrcs_ and ssrc_groups_ into rtp_parameters_.
  367. // TODO(skvlad): Combine parameters_ and rtp_parameters_ once we have only
  368. // one stream per MediaChannel.
  369. webrtc::RtpParameters rtp_parameters_ RTC_GUARDED_BY(&thread_checker_);
  370. bool sending_ RTC_GUARDED_BY(&thread_checker_);
  371. // In order for the |invoker_| to protect other members from being
  372. // destructed as they are used in asynchronous tasks it has to be destructed
  373. // first.
  374. rtc::AsyncInvoker invoker_;
  375. };
  376. // Wrapper for the receiver part, contains configs etc. that are needed to
  377. // reconstruct the underlying VideoReceiveStream.
  378. class WebRtcVideoReceiveStream
  379. : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
  380. public:
  381. WebRtcVideoReceiveStream(
  382. WebRtcVideoChannel* channel,
  383. webrtc::Call* call,
  384. const StreamParams& sp,
  385. webrtc::VideoReceiveStream::Config config,
  386. webrtc::VideoDecoderFactory* decoder_factory,
  387. bool default_stream,
  388. const std::vector<VideoCodecSettings>& recv_codecs,
  389. const webrtc::FlexfecReceiveStream::Config& flexfec_config);
  390. ~WebRtcVideoReceiveStream();
  391. const std::vector<uint32_t>& GetSsrcs() const;
  392. std::vector<webrtc::RtpSource> GetSources();
  393. // Does not return codecs, they are filled by the owning WebRtcVideoChannel.
  394. webrtc::RtpParameters GetRtpParameters() const;
  395. void SetLocalSsrc(uint32_t local_ssrc);
  396. // TODO(deadbeef): Move these feedback parameters into the recv parameters.
  397. void SetFeedbackParameters(bool lntf_enabled,
  398. bool nack_enabled,
  399. bool transport_cc_enabled,
  400. webrtc::RtcpMode rtcp_mode);
  401. void SetRecvParameters(const ChangedRecvParameters& recv_params);
  402. void OnFrame(const webrtc::VideoFrame& frame) override;
  403. bool IsDefaultStream() const;
  404. void SetFrameDecryptor(
  405. rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor);
  406. bool SetBaseMinimumPlayoutDelayMs(int delay_ms);
  407. int GetBaseMinimumPlayoutDelayMs() const;
  408. void SetSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink);
  409. VideoReceiverInfo GetVideoReceiverInfo(bool log_stats);
  410. void SetRecordableEncodedFrameCallback(
  411. std::function<void(const webrtc::RecordableEncodedFrame&)> callback);
  412. void ClearRecordableEncodedFrameCallback();
  413. void GenerateKeyFrame();
  414. void SetDepacketizerToDecoderFrameTransformer(
  415. rtc::scoped_refptr<webrtc::FrameTransformerInterface>
  416. frame_transformer);
  417. private:
  418. void RecreateWebRtcVideoStream();
  419. void MaybeRecreateWebRtcFlexfecStream();
  420. void MaybeAssociateFlexfecWithVideo();
  421. void MaybeDissociateFlexfecFromVideo();
  422. void ConfigureCodecs(const std::vector<VideoCodecSettings>& recv_codecs);
  423. void ConfigureFlexfecCodec(int flexfec_payload_type);
  424. std::string GetCodecNameFromPayloadType(int payload_type);
  425. WebRtcVideoChannel* const channel_;
  426. webrtc::Call* const call_;
  427. const StreamParams stream_params_;
  428. // Both |stream_| and |flexfec_stream_| are managed by |this|. They are
  429. // destroyed by calling call_->DestroyVideoReceiveStream and
  430. // call_->DestroyFlexfecReceiveStream, respectively.
  431. webrtc::VideoReceiveStream* stream_;
  432. const bool default_stream_;
  433. webrtc::VideoReceiveStream::Config config_;
  434. webrtc::FlexfecReceiveStream::Config flexfec_config_;
  435. webrtc::FlexfecReceiveStream* flexfec_stream_;
  436. webrtc::VideoDecoderFactory* const decoder_factory_;
  437. rtc::CriticalSection sink_lock_;
  438. rtc::VideoSinkInterface<webrtc::VideoFrame>* sink_
  439. RTC_GUARDED_BY(sink_lock_);
  440. // Expands remote RTP timestamps to int64_t to be able to estimate how long
  441. // the stream has been running.
  442. rtc::TimestampWrapAroundHandler timestamp_wraparound_handler_
  443. RTC_GUARDED_BY(sink_lock_);
  444. int64_t first_frame_timestamp_ RTC_GUARDED_BY(sink_lock_);
  445. // Start NTP time is estimated as current remote NTP time (estimated from
  446. // RTCP) minus the elapsed time, as soon as remote NTP time is available.
  447. int64_t estimated_remote_start_ntp_time_ms_ RTC_GUARDED_BY(sink_lock_);
  448. };
  449. void Construct(webrtc::Call* call, WebRtcVideoEngine* engine);
  450. bool SendRtp(const uint8_t* data,
  451. size_t len,
  452. const webrtc::PacketOptions& options) override;
  453. bool SendRtcp(const uint8_t* data, size_t len) override;
  454. // Generate the list of codec parameters to pass down based on the negotiated
  455. // "codecs". Note that VideoCodecSettings correspond to concrete codecs like
  456. // VP8, VP9, H264 while VideoCodecs correspond also to "virtual" codecs like
  457. // RTX, ULPFEC, FLEXFEC.
  458. static std::vector<VideoCodecSettings> MapCodecs(
  459. const std::vector<VideoCodec>& codecs);
  460. // Get all codecs that are compatible with the receiver.
  461. std::vector<VideoCodecSettings> SelectSendVideoCodecs(
  462. const std::vector<VideoCodecSettings>& remote_mapped_codecs) const
  463. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  464. static bool NonFlexfecReceiveCodecsHaveChanged(
  465. std::vector<VideoCodecSettings> before,
  466. std::vector<VideoCodecSettings> after);
  467. void FillSenderStats(VideoMediaInfo* info, bool log_stats)
  468. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  469. void FillReceiverStats(VideoMediaInfo* info, bool log_stats)
  470. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  471. void FillBandwidthEstimationStats(const webrtc::Call::Stats& stats,
  472. VideoMediaInfo* info)
  473. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  474. void FillSendAndReceiveCodecStats(VideoMediaInfo* video_media_info)
  475. RTC_EXCLUSIVE_LOCKS_REQUIRED(thread_checker_);
  476. rtc::Thread* worker_thread_;
  477. rtc::ThreadChecker thread_checker_;
  478. uint32_t rtcp_receiver_report_ssrc_ RTC_GUARDED_BY(thread_checker_);
  479. bool sending_ RTC_GUARDED_BY(thread_checker_);
  480. webrtc::Call* const call_ RTC_GUARDED_BY(thread_checker_);
  481. DefaultUnsignalledSsrcHandler default_unsignalled_ssrc_handler_
  482. RTC_GUARDED_BY(thread_checker_);
  483. UnsignalledSsrcHandler* const unsignalled_ssrc_handler_
  484. RTC_GUARDED_BY(thread_checker_);
  485. // Delay for unsignaled streams, which may be set before the stream exists.
  486. int default_recv_base_minimum_delay_ms_ RTC_GUARDED_BY(thread_checker_) = 0;
  487. const MediaConfig::Video video_config_ RTC_GUARDED_BY(thread_checker_);
  488. // Using primary-ssrc (first ssrc) as key.
  489. std::map<uint32_t, WebRtcVideoSendStream*> send_streams_
  490. RTC_GUARDED_BY(thread_checker_);
  491. std::map<uint32_t, WebRtcVideoReceiveStream*> receive_streams_
  492. RTC_GUARDED_BY(thread_checker_);
  493. std::set<uint32_t> send_ssrcs_ RTC_GUARDED_BY(thread_checker_);
  494. std::set<uint32_t> receive_ssrcs_ RTC_GUARDED_BY(thread_checker_);
  495. absl::optional<VideoCodecSettings> send_codec_
  496. RTC_GUARDED_BY(thread_checker_);
  497. std::vector<VideoCodecSettings> negotiated_codecs_
  498. RTC_GUARDED_BY(thread_checker_);
  499. absl::optional<std::vector<webrtc::RtpExtension>> send_rtp_extensions_
  500. RTC_GUARDED_BY(thread_checker_);
  501. webrtc::VideoEncoderFactory* const encoder_factory_
  502. RTC_GUARDED_BY(thread_checker_);
  503. webrtc::VideoDecoderFactory* const decoder_factory_
  504. RTC_GUARDED_BY(thread_checker_);
  505. webrtc::VideoBitrateAllocatorFactory* const bitrate_allocator_factory_
  506. RTC_GUARDED_BY(thread_checker_);
  507. std::vector<VideoCodecSettings> recv_codecs_ RTC_GUARDED_BY(thread_checker_);
  508. std::vector<webrtc::RtpExtension> recv_rtp_extensions_
  509. RTC_GUARDED_BY(thread_checker_);
  510. // See reason for keeping track of the FlexFEC payload type separately in
  511. // comment in WebRtcVideoChannel::ChangedRecvParameters.
  512. int recv_flexfec_payload_type_ RTC_GUARDED_BY(thread_checker_);
  513. webrtc::BitrateConstraints bitrate_config_ RTC_GUARDED_BY(thread_checker_);
  514. // TODO(deadbeef): Don't duplicate information between
  515. // send_params/recv_params, rtp_extensions, options, etc.
  516. VideoSendParameters send_params_ RTC_GUARDED_BY(thread_checker_);
  517. VideoOptions default_send_options_ RTC_GUARDED_BY(thread_checker_);
  518. VideoRecvParameters recv_params_ RTC_GUARDED_BY(thread_checker_);
  519. int64_t last_stats_log_ms_ RTC_GUARDED_BY(thread_checker_);
  520. const bool discard_unknown_ssrc_packets_ RTC_GUARDED_BY(thread_checker_);
  521. // This is a stream param that comes from the remote description, but wasn't
  522. // signaled with any a=ssrc lines. It holds information that was signaled
  523. // before the unsignaled receive stream is created when the first packet is
  524. // received.
  525. StreamParams unsignaled_stream_params_ RTC_GUARDED_BY(thread_checker_);
  526. // Per peer connection crypto options that last for the lifetime of the peer
  527. // connection.
  528. const webrtc::CryptoOptions crypto_options_ RTC_GUARDED_BY(thread_checker_);
  529. // Optional frame transformer set on unsignaled streams.
  530. rtc::scoped_refptr<webrtc::FrameTransformerInterface>
  531. unsignaled_frame_transformer_ RTC_GUARDED_BY(thread_checker_);
  532. // Buffer for unhandled packets.
  533. std::unique_ptr<UnhandledPacketsBuffer> unknown_ssrc_packet_buffer_
  534. RTC_GUARDED_BY(thread_checker_);
  535. bool allow_codec_switching_ = false;
  536. absl::optional<EncoderSwitchRequestCallback::Config>
  537. requested_encoder_switch_;
  538. // In order for the |invoker_| to protect other members from being destructed
  539. // as they are used in asynchronous tasks it has to be destructed first.
  540. rtc::AsyncInvoker invoker_;
  541. };
  542. class EncoderStreamFactory
  543. : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface {
  544. public:
  545. EncoderStreamFactory(std::string codec_name,
  546. int max_qp,
  547. bool is_screenshare,
  548. bool conference_mode);
  549. private:
  550. std::vector<webrtc::VideoStream> CreateEncoderStreams(
  551. int width,
  552. int height,
  553. const webrtc::VideoEncoderConfig& encoder_config) override;
  554. std::vector<webrtc::VideoStream> CreateDefaultVideoStreams(
  555. int width,
  556. int height,
  557. const webrtc::VideoEncoderConfig& encoder_config,
  558. const absl::optional<webrtc::DataRate>& experimental_min_bitrate) const;
  559. std::vector<webrtc::VideoStream>
  560. CreateSimulcastOrConfereceModeScreenshareStreams(
  561. int width,
  562. int height,
  563. const webrtc::VideoEncoderConfig& encoder_config,
  564. const absl::optional<webrtc::DataRate>& experimental_min_bitrate) const;
  565. const std::string codec_name_;
  566. const int max_qp_;
  567. const bool is_screenshare_;
  568. // Allows a screenshare specific configuration, which enables temporal
  569. // layering and various settings.
  570. const bool conference_mode_;
  571. };
  572. } // namespace cricket
  573. #endif // MEDIA_ENGINE_WEBRTC_VIDEO_ENGINE_H_