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