You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
7.5 KiB

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