video_receive_stream.h 9.3 KB

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