video_receive_stream2.h 10 KB

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