frame_buffer2.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2016 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 MODULES_VIDEO_CODING_FRAME_BUFFER2_H_
  11. #define MODULES_VIDEO_CODING_FRAME_BUFFER2_H_
  12. #include <array>
  13. #include <map>
  14. #include <memory>
  15. #include <utility>
  16. #include <vector>
  17. #include "absl/container/inlined_vector.h"
  18. #include "api/video/encoded_frame.h"
  19. #include "modules/video_coding/include/video_coding_defines.h"
  20. #include "modules/video_coding/inter_frame_delay.h"
  21. #include "modules/video_coding/jitter_estimator.h"
  22. #include "modules/video_coding/utility/decoded_frames_history.h"
  23. #include "rtc_base/event.h"
  24. #include "rtc_base/experiments/rtt_mult_experiment.h"
  25. #include "rtc_base/numerics/sequence_number_util.h"
  26. #include "rtc_base/synchronization/mutex.h"
  27. #include "rtc_base/synchronization/sequence_checker.h"
  28. #include "rtc_base/system/no_unique_address.h"
  29. #include "rtc_base/task_queue.h"
  30. #include "rtc_base/task_utils/repeating_task.h"
  31. #include "rtc_base/thread_annotations.h"
  32. namespace webrtc {
  33. class Clock;
  34. class VCMReceiveStatisticsCallback;
  35. class VCMJitterEstimator;
  36. class VCMTiming;
  37. namespace video_coding {
  38. class FrameBuffer {
  39. public:
  40. enum ReturnReason { kFrameFound, kTimeout, kStopped };
  41. FrameBuffer(Clock* clock,
  42. VCMTiming* timing,
  43. VCMReceiveStatisticsCallback* stats_callback);
  44. FrameBuffer() = delete;
  45. FrameBuffer(const FrameBuffer&) = delete;
  46. FrameBuffer& operator=(const FrameBuffer&) = delete;
  47. virtual ~FrameBuffer();
  48. // Insert a frame into the frame buffer. Returns the picture id
  49. // of the last continuous frame or -1 if there is no continuous frame.
  50. // TODO(philipel): Return a VideoLayerFrameId and not only the picture id.
  51. int64_t InsertFrame(std::unique_ptr<EncodedFrame> frame);
  52. // Get the next frame for decoding. Will return at latest after
  53. // |max_wait_time_ms|.
  54. void NextFrame(
  55. int64_t max_wait_time_ms,
  56. bool keyframe_required,
  57. rtc::TaskQueue* callback_queue,
  58. std::function<void(std::unique_ptr<EncodedFrame>, ReturnReason)> handler);
  59. // Tells the FrameBuffer which protection mode that is in use. Affects
  60. // the frame timing.
  61. // TODO(philipel): Remove this when new timing calculations has been
  62. // implemented.
  63. void SetProtectionMode(VCMVideoProtection mode);
  64. // Stop the frame buffer, causing any sleeping thread in NextFrame to
  65. // return immediately.
  66. void Stop();
  67. // Updates the RTT for jitter buffer estimation.
  68. void UpdateRtt(int64_t rtt_ms);
  69. // Clears the FrameBuffer, removing all the buffered frames.
  70. void Clear();
  71. private:
  72. struct FrameInfo {
  73. FrameInfo();
  74. FrameInfo(FrameInfo&&);
  75. ~FrameInfo();
  76. // Which other frames that have direct unfulfilled dependencies
  77. // on this frame.
  78. absl::InlinedVector<VideoLayerFrameId, 8> dependent_frames;
  79. // A frame is continiuous if it has all its referenced/indirectly
  80. // referenced frames.
  81. //
  82. // How many unfulfilled frames this frame have until it becomes continuous.
  83. size_t num_missing_continuous = 0;
  84. // A frame is decodable if all its referenced frames have been decoded.
  85. //
  86. // How many unfulfilled frames this frame have until it becomes decodable.
  87. size_t num_missing_decodable = 0;
  88. // If this frame is continuous or not.
  89. bool continuous = false;
  90. // The actual EncodedFrame.
  91. std::unique_ptr<EncodedFrame> frame;
  92. };
  93. using FrameMap = std::map<VideoLayerFrameId, FrameInfo>;
  94. // Check that the references of |frame| are valid.
  95. bool ValidReferences(const EncodedFrame& frame) const;
  96. int64_t FindNextFrame(int64_t now_ms) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  97. EncodedFrame* GetNextFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  98. void StartWaitForNextFrameOnQueue() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  99. void CancelCallback() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  100. // Update all directly dependent and indirectly dependent frames and mark
  101. // them as continuous if all their references has been fulfilled.
  102. void PropagateContinuity(FrameMap::iterator start)
  103. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  104. // Marks the frame as decoded and updates all directly dependent frames.
  105. void PropagateDecodability(const FrameInfo& info)
  106. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  107. // Update the corresponding FrameInfo of |frame| and all FrameInfos that
  108. // |frame| references.
  109. // Return false if |frame| will never be decodable, true otherwise.
  110. bool UpdateFrameInfoWithIncomingFrame(const EncodedFrame& frame,
  111. FrameMap::iterator info)
  112. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  113. void UpdateJitterDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  114. void UpdateTimingFrameInfo() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  115. void ClearFramesAndHistory() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  116. // Checks if the superframe, which current frame belongs to, is complete.
  117. bool IsCompleteSuperFrame(const EncodedFrame& frame)
  118. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  119. bool HasBadRenderTiming(const EncodedFrame& frame, int64_t now_ms)
  120. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  121. // The cleaner solution would be to have the NextFrame function return a
  122. // vector of frames, but until the decoding pipeline can support decoding
  123. // multiple frames at the same time we combine all frames to one frame and
  124. // return it. See bugs.webrtc.org/10064
  125. EncodedFrame* CombineAndDeleteFrames(
  126. const std::vector<EncodedFrame*>& frames) const;
  127. RTC_NO_UNIQUE_ADDRESS SequenceChecker construction_checker_;
  128. RTC_NO_UNIQUE_ADDRESS SequenceChecker callback_checker_;
  129. // Stores only undecoded frames.
  130. FrameMap frames_ RTC_GUARDED_BY(mutex_);
  131. DecodedFramesHistory decoded_frames_history_ RTC_GUARDED_BY(mutex_);
  132. Mutex mutex_;
  133. Clock* const clock_;
  134. rtc::TaskQueue* callback_queue_ RTC_GUARDED_BY(mutex_);
  135. RepeatingTaskHandle callback_task_ RTC_GUARDED_BY(mutex_);
  136. std::function<void(std::unique_ptr<EncodedFrame>, ReturnReason)>
  137. frame_handler_ RTC_GUARDED_BY(mutex_);
  138. int64_t latest_return_time_ms_ RTC_GUARDED_BY(mutex_);
  139. bool keyframe_required_ RTC_GUARDED_BY(mutex_);
  140. VCMJitterEstimator jitter_estimator_ RTC_GUARDED_BY(mutex_);
  141. VCMTiming* const timing_ RTC_GUARDED_BY(mutex_);
  142. VCMInterFrameDelay inter_frame_delay_ RTC_GUARDED_BY(mutex_);
  143. absl::optional<VideoLayerFrameId> last_continuous_frame_
  144. RTC_GUARDED_BY(mutex_);
  145. std::vector<FrameMap::iterator> frames_to_decode_ RTC_GUARDED_BY(mutex_);
  146. bool stopped_ RTC_GUARDED_BY(mutex_);
  147. VCMVideoProtection protection_mode_ RTC_GUARDED_BY(mutex_);
  148. VCMReceiveStatisticsCallback* const stats_callback_;
  149. int64_t last_log_non_decoded_ms_ RTC_GUARDED_BY(mutex_);
  150. const bool add_rtt_to_playout_delay_;
  151. // rtt_mult experiment settings.
  152. const absl::optional<RttMultExperiment::Settings> rtt_mult_settings_;
  153. };
  154. } // namespace video_coding
  155. } // namespace webrtc
  156. #endif // MODULES_VIDEO_CODING_FRAME_BUFFER2_H_