video_receive_stream2.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2020 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 VIDEO_VIDEO_RECEIVE_STREAM2_H_
  11. #define VIDEO_VIDEO_RECEIVE_STREAM2_H_
  12. #include <memory>
  13. #include <vector>
  14. #include "api/task_queue/task_queue_factory.h"
  15. #include "api/transport/media/media_transport_interface.h"
  16. #include "api/units/timestamp.h"
  17. #include "api/video/recordable_encoded_frame.h"
  18. #include "call/rtp_packet_sink_interface.h"
  19. #include "call/syncable.h"
  20. #include "call/video_receive_stream.h"
  21. #include "modules/rtp_rtcp/include/flexfec_receiver.h"
  22. #include "modules/rtp_rtcp/source/source_tracker.h"
  23. #include "modules/video_coding/frame_buffer2.h"
  24. #include "modules/video_coding/video_receiver2.h"
  25. #include "rtc_base/synchronization/sequence_checker.h"
  26. #include "rtc_base/task_queue.h"
  27. #include "rtc_base/task_utils/pending_task_safety_flag.h"
  28. #include "system_wrappers/include/clock.h"
  29. #include "video/receive_statistics_proxy2.h"
  30. #include "video/rtp_streams_synchronizer2.h"
  31. #include "video/rtp_video_stream_receiver2.h"
  32. #include "video/transport_adapter.h"
  33. #include "video/video_stream_decoder2.h"
  34. namespace webrtc {
  35. class ProcessThread;
  36. class RTPFragmentationHeader;
  37. class RtpStreamReceiverInterface;
  38. class RtpStreamReceiverControllerInterface;
  39. class RtxReceiveStream;
  40. class VCMTiming;
  41. namespace internal {
  42. class CallStats;
  43. // Utility struct for grabbing metadata from a VideoFrame and processing it
  44. // asynchronously without needing the actual frame data.
  45. // Additionally the caller can bundle information from the current clock
  46. // when the metadata is captured, for accurate reporting and not needeing
  47. // multiple calls to clock->Now().
  48. struct VideoFrameMetaData {
  49. VideoFrameMetaData(const webrtc::VideoFrame& frame, Timestamp now)
  50. : rtp_timestamp(frame.timestamp()),
  51. timestamp_us(frame.timestamp_us()),
  52. ntp_time_ms(frame.ntp_time_ms()),
  53. width(frame.width()),
  54. height(frame.height()),
  55. decode_timestamp(now) {}
  56. int64_t render_time_ms() const {
  57. return timestamp_us / rtc::kNumMicrosecsPerMillisec;
  58. }
  59. const uint32_t rtp_timestamp;
  60. const int64_t timestamp_us;
  61. const int64_t ntp_time_ms;
  62. const int width;
  63. const int height;
  64. const Timestamp decode_timestamp;
  65. };
  66. class VideoReceiveStream2 : public webrtc::VideoReceiveStream,
  67. public rtc::VideoSinkInterface<VideoFrame>,
  68. public NackSender,
  69. public video_coding::OnCompleteFrameCallback,
  70. public Syncable,
  71. public CallStatsObserver {
  72. public:
  73. // The default number of milliseconds to pass before re-requesting a key frame
  74. // to be sent.
  75. static constexpr int kMaxWaitForKeyFrameMs = 200;
  76. VideoReceiveStream2(TaskQueueFactory* task_queue_factory,
  77. TaskQueueBase* current_queue,
  78. RtpStreamReceiverControllerInterface* receiver_controller,
  79. int num_cpu_cores,
  80. PacketRouter* packet_router,
  81. VideoReceiveStream::Config config,
  82. ProcessThread* process_thread,
  83. CallStats* call_stats,
  84. Clock* clock,
  85. VCMTiming* timing);
  86. ~VideoReceiveStream2() override;
  87. const Config& config() const { return config_; }
  88. void SignalNetworkState(NetworkState state);
  89. bool DeliverRtcp(const uint8_t* packet, size_t length);
  90. void SetSync(Syncable* audio_syncable);
  91. // Implements webrtc::VideoReceiveStream.
  92. void Start() override;
  93. void Stop() override;
  94. webrtc::VideoReceiveStream::Stats GetStats() const override;
  95. void AddSecondarySink(RtpPacketSinkInterface* sink) override;
  96. void RemoveSecondarySink(const RtpPacketSinkInterface* sink) override;
  97. // SetBaseMinimumPlayoutDelayMs and GetBaseMinimumPlayoutDelayMs are called
  98. // from webrtc/api level and requested by user code. For e.g. blink/js layer
  99. // in Chromium.
  100. bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override;
  101. int GetBaseMinimumPlayoutDelayMs() const override;
  102. void SetFrameDecryptor(
  103. rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) override;
  104. void SetDepacketizerToDecoderFrameTransformer(
  105. rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) override;
  106. // Implements rtc::VideoSinkInterface<VideoFrame>.
  107. void OnFrame(const VideoFrame& video_frame) override;
  108. // Implements NackSender.
  109. // For this particular override of the interface,
  110. // only (buffering_allowed == true) is acceptable.
  111. void SendNack(const std::vector<uint16_t>& sequence_numbers,
  112. bool buffering_allowed) override;
  113. // Implements video_coding::OnCompleteFrameCallback.
  114. void OnCompleteFrame(
  115. std::unique_ptr<video_coding::EncodedFrame> frame) override;
  116. // Implements CallStatsObserver::OnRttUpdate
  117. void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
  118. // Implements Syncable.
  119. uint32_t id() const override;
  120. absl::optional<Syncable::Info> GetInfo() const override;
  121. bool GetPlayoutRtpTimestamp(uint32_t* rtp_timestamp,
  122. int64_t* time_ms) const override;
  123. void SetEstimatedPlayoutNtpTimestampMs(int64_t ntp_timestamp_ms,
  124. int64_t time_ms) override;
  125. // SetMinimumPlayoutDelay is only called by A/V sync.
  126. void SetMinimumPlayoutDelay(int delay_ms) override;
  127. std::vector<webrtc::RtpSource> GetSources() const override;
  128. RecordingState SetAndGetRecordingState(RecordingState state,
  129. bool generate_key_frame) override;
  130. void GenerateKeyFrame() override;
  131. private:
  132. int64_t GetMaxWaitMs() const RTC_RUN_ON(decode_queue_);
  133. void StartNextDecode() RTC_RUN_ON(decode_queue_);
  134. void HandleEncodedFrame(std::unique_ptr<video_coding::EncodedFrame> frame)
  135. RTC_RUN_ON(decode_queue_);
  136. void HandleFrameBufferTimeout(int64_t now_ms, int64_t wait_ms)
  137. RTC_RUN_ON(worker_sequence_checker_);
  138. void UpdatePlayoutDelays() const
  139. RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_sequence_checker_);
  140. void RequestKeyFrame(int64_t timestamp_ms)
  141. RTC_RUN_ON(worker_sequence_checker_);
  142. void HandleKeyFrameGeneration(bool received_frame_is_keyframe,
  143. int64_t now_ms,
  144. bool always_request_key_frame,
  145. bool keyframe_request_is_due)
  146. RTC_RUN_ON(worker_sequence_checker_);
  147. bool IsReceivingKeyFrame(int64_t timestamp_ms) const
  148. RTC_RUN_ON(worker_sequence_checker_);
  149. void UpdateHistograms();
  150. SequenceChecker worker_sequence_checker_;
  151. SequenceChecker module_process_sequence_checker_;
  152. TaskQueueFactory* const task_queue_factory_;
  153. TransportAdapter transport_adapter_;
  154. const VideoReceiveStream::Config config_;
  155. const int num_cpu_cores_;
  156. TaskQueueBase* const worker_thread_;
  157. Clock* const clock_;
  158. CallStats* const call_stats_;
  159. bool decoder_running_ RTC_GUARDED_BY(worker_sequence_checker_) = false;
  160. bool decoder_stopped_ RTC_GUARDED_BY(decode_queue_) = true;
  161. SourceTracker source_tracker_;
  162. ReceiveStatisticsProxy stats_proxy_;
  163. // Shared by media and rtx stream receivers, since the latter has no RtpRtcp
  164. // module of its own.
  165. const std::unique_ptr<ReceiveStatistics> rtp_receive_statistics_;
  166. std::unique_ptr<VCMTiming> timing_; // Jitter buffer experiment.
  167. VideoReceiver2 video_receiver_;
  168. std::unique_ptr<rtc::VideoSinkInterface<VideoFrame>> incoming_video_stream_;
  169. RtpVideoStreamReceiver2 rtp_video_stream_receiver_;
  170. std::unique_ptr<VideoStreamDecoder> video_stream_decoder_;
  171. RtpStreamsSynchronizer rtp_stream_sync_;
  172. // TODO(nisse, philipel): Creation and ownership of video encoders should be
  173. // moved to the new VideoStreamDecoder.
  174. std::vector<std::unique_ptr<VideoDecoder>> video_decoders_;
  175. // Members for the new jitter buffer experiment.
  176. std::unique_ptr<video_coding::FrameBuffer> frame_buffer_;
  177. std::unique_ptr<RtpStreamReceiverInterface> media_receiver_;
  178. std::unique_ptr<RtxReceiveStream> rtx_receive_stream_;
  179. std::unique_ptr<RtpStreamReceiverInterface> rtx_receiver_;
  180. // Whenever we are in an undecodable state (stream has just started or due to
  181. // a decoding error) we require a keyframe to restart the stream.
  182. bool keyframe_required_ RTC_GUARDED_BY(decode_queue_) = true;
  183. // If we have successfully decoded any frame.
  184. bool frame_decoded_ RTC_GUARDED_BY(decode_queue_) = false;
  185. int64_t last_keyframe_request_ms_ RTC_GUARDED_BY(decode_queue_) = 0;
  186. int64_t last_complete_frame_time_ms_
  187. RTC_GUARDED_BY(worker_sequence_checker_) = 0;
  188. // Keyframe request intervals are configurable through field trials.
  189. const int max_wait_for_keyframe_ms_;
  190. const int max_wait_for_frame_ms_;
  191. // All of them tries to change current min_playout_delay on |timing_| but
  192. // source of the change request is different in each case. Among them the
  193. // biggest delay is used. -1 means use default value from the |timing_|.
  194. //
  195. // Minimum delay as decided by the RTP playout delay extension.
  196. int frame_minimum_playout_delay_ms_ RTC_GUARDED_BY(worker_sequence_checker_) =
  197. -1;
  198. // Minimum delay as decided by the setLatency function in "webrtc/api".
  199. int base_minimum_playout_delay_ms_ RTC_GUARDED_BY(worker_sequence_checker_) =
  200. -1;
  201. // Minimum delay as decided by the A/V synchronization feature.
  202. int syncable_minimum_playout_delay_ms_
  203. RTC_GUARDED_BY(worker_sequence_checker_) = -1;
  204. // Maximum delay as decided by the RTP playout delay extension.
  205. int frame_maximum_playout_delay_ms_ RTC_GUARDED_BY(worker_sequence_checker_) =
  206. -1;
  207. // Function that is triggered with encoded frames, if not empty.
  208. std::function<void(const RecordableEncodedFrame&)>
  209. encoded_frame_buffer_function_ RTC_GUARDED_BY(decode_queue_);
  210. // Set to true while we're requesting keyframes but not yet received one.
  211. bool keyframe_generation_requested_ RTC_GUARDED_BY(worker_sequence_checker_) =
  212. false;
  213. // Defined last so they are destroyed before all other members.
  214. rtc::TaskQueue decode_queue_;
  215. // Used to signal destruction to potentially pending tasks.
  216. ScopedTaskSafety task_safety_;
  217. };
  218. } // namespace internal
  219. } // namespace webrtc
  220. #endif // VIDEO_VIDEO_RECEIVE_STREAM2_H_