philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 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 | |
| 11 | #ifndef WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ |
| 13 | |
| 14 | #include <array> |
| 15 | #include <map> |
| 16 | #include <memory> |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 17 | #include <utility> |
| 18 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 19 | #include "webrtc/modules/video_coding/frame_object.h" |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 20 | #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
| 21 | #include "webrtc/modules/video_coding/inter_frame_delay.h" |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 22 | #include "webrtc/modules/video_coding/sequence_number_util.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 23 | #include "webrtc/rtc_base/constructormagic.h" |
| 24 | #include "webrtc/rtc_base/criticalsection.h" |
| 25 | #include "webrtc/rtc_base/event.h" |
| 26 | #include "webrtc/rtc_base/thread_annotations.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | class Clock; |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 31 | class VCMReceiveStatisticsCallback; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 32 | class VCMJitterEstimator; |
| 33 | class VCMTiming; |
| 34 | |
| 35 | namespace video_coding { |
| 36 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 37 | class FrameBuffer { |
| 38 | public: |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 39 | enum ReturnReason { kFrameFound, kTimeout, kStopped }; |
| 40 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 41 | FrameBuffer(Clock* clock, |
| 42 | VCMJitterEstimator* jitter_estimator, |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 43 | VCMTiming* timing, |
| 44 | VCMReceiveStatisticsCallback* stats_proxy); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 45 | |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 46 | virtual ~FrameBuffer(); |
| 47 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 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 | int InsertFrame(std::unique_ptr<FrameObject> frame); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 51 | |
| 52 | // Get the next frame for decoding. Will return at latest after |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 53 | // |max_wait_time_ms|. |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 54 | // - If a frame is available within |max_wait_time_ms| it will return |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 55 | // kFrameFound and set |frame_out| to the resulting frame. |
| 56 | // - If no frame is available after |max_wait_time_ms| it will return |
| 57 | // kTimeout. |
| 58 | // - If the FrameBuffer is stopped then it will return kStopped. |
| 59 | ReturnReason NextFrame(int64_t max_wait_time_ms, |
philipel | 3042c2d | 2017-08-18 04:55:02 -0700 | [diff] [blame^] | 60 | std::unique_ptr<FrameObject>* frame_out, |
| 61 | bool keyframe_required = false); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 62 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 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 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 69 | // Start the frame buffer, has no effect if the frame buffer is started. |
| 70 | // The frame buffer is started upon construction. |
| 71 | void Start(); |
| 72 | |
| 73 | // Stop the frame buffer, causing any sleeping thread in NextFrame to |
| 74 | // return immediately. |
| 75 | void Stop(); |
| 76 | |
gustavogb | f1e08d0 | 2017-08-09 05:43:08 -0700 | [diff] [blame] | 77 | // Updates the RTT for jitter buffer estimation. |
| 78 | void UpdateRtt(int64_t rtt_ms); |
| 79 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 80 | private: |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 81 | struct FrameKey { |
| 82 | FrameKey() : picture_id(0), spatial_layer(0) {} |
| 83 | FrameKey(uint16_t picture_id, uint8_t spatial_layer) |
| 84 | : picture_id(picture_id), spatial_layer(spatial_layer) {} |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 85 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 86 | bool operator<(const FrameKey& rhs) const { |
| 87 | if (picture_id == rhs.picture_id) |
| 88 | return spatial_layer < rhs.spatial_layer; |
| 89 | return AheadOf(rhs.picture_id, picture_id); |
| 90 | } |
| 91 | |
| 92 | bool operator<=(const FrameKey& rhs) const { return !(rhs < *this); } |
| 93 | |
| 94 | uint16_t picture_id; |
| 95 | uint8_t spatial_layer; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 98 | struct FrameInfo { |
| 99 | // The maximum number of frames that can depend on this frame. |
| 100 | static constexpr size_t kMaxNumDependentFrames = 8; |
| 101 | |
| 102 | // Which other frames that have direct unfulfilled dependencies |
| 103 | // on this frame. |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 104 | // TODO(philipel): Add simple modify/access functions to prevent adding too |
| 105 | // many |dependent_frames|. |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 106 | FrameKey dependent_frames[kMaxNumDependentFrames]; |
| 107 | size_t num_dependent_frames = 0; |
| 108 | |
| 109 | // A frame is continiuous if it has all its referenced/indirectly |
| 110 | // referenced frames. |
| 111 | // |
| 112 | // How many unfulfilled frames this frame have until it becomes continuous. |
| 113 | size_t num_missing_continuous = 0; |
| 114 | |
| 115 | // A frame is decodable if all its referenced frames have been decoded. |
| 116 | // |
| 117 | // How many unfulfilled frames this frame have until it becomes decodable. |
| 118 | size_t num_missing_decodable = 0; |
| 119 | |
| 120 | // If this frame is continuous or not. |
| 121 | bool continuous = false; |
| 122 | |
| 123 | // The actual FrameObject. |
| 124 | std::unique_ptr<FrameObject> frame; |
| 125 | }; |
| 126 | |
| 127 | using FrameMap = std::map<FrameKey, FrameInfo>; |
| 128 | |
philipel | 112adf9 | 2017-06-15 09:06:21 -0700 | [diff] [blame] | 129 | // Check that the references of |frame| are valid. |
| 130 | bool ValidReferences(const FrameObject& frame) const; |
| 131 | |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 132 | // Updates the minimal and maximal playout delays |
| 133 | // depending on the frame. |
| 134 | void UpdatePlayoutDelays(const FrameObject& frame) |
| 135 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 136 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 137 | // Update all directly dependent and indirectly dependent frames and mark |
| 138 | // them as continuous if all their references has been fulfilled. |
| 139 | void PropagateContinuity(FrameMap::iterator start) |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 140 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 141 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 142 | // Marks the frame as decoded and updates all directly dependent frames. |
| 143 | void PropagateDecodability(const FrameInfo& info) |
| 144 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 145 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 146 | // Advances |last_decoded_frame_it_| to |decoded| and removes old |
| 147 | // frame info. |
| 148 | void AdvanceLastDecodedFrame(FrameMap::iterator decoded) |
| 149 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 150 | |
| 151 | // Update the corresponding FrameInfo of |frame| and all FrameInfos that |
| 152 | // |frame| references. |
| 153 | // Return false if |frame| will never be decodable, true otherwise. |
| 154 | bool UpdateFrameInfoWithIncomingFrame(const FrameObject& frame, |
| 155 | FrameMap::iterator info) |
| 156 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 157 | |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 158 | void UpdateJitterDelay() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 159 | |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 160 | void UpdateTimingFrameInfo() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 161 | |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 162 | void ClearFramesAndHistory() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 163 | |
stefan | 95e9754 | 2017-05-23 09:52:18 -0700 | [diff] [blame] | 164 | bool HasBadRenderTiming(const FrameObject& frame, int64_t now_ms) |
| 165 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 166 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 167 | FrameMap frames_ GUARDED_BY(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 168 | |
| 169 | rtc::CriticalSection crit_; |
| 170 | Clock* const clock_; |
tommi | 0a73564 | 2017-03-14 06:23:57 -0700 | [diff] [blame] | 171 | rtc::Event new_continuous_frame_event_; |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 172 | VCMJitterEstimator* const jitter_estimator_ GUARDED_BY(crit_); |
| 173 | VCMTiming* const timing_ GUARDED_BY(crit_); |
| 174 | VCMInterFrameDelay inter_frame_delay_ GUARDED_BY(crit_); |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame] | 175 | uint32_t last_decoded_frame_timestamp_ GUARDED_BY(crit_); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 176 | FrameMap::iterator last_decoded_frame_it_ GUARDED_BY(crit_); |
| 177 | FrameMap::iterator last_continuous_frame_it_ GUARDED_BY(crit_); |
philipel | 1c05625 | 2017-01-31 09:53:12 -0800 | [diff] [blame] | 178 | FrameMap::iterator next_frame_it_ GUARDED_BY(crit_); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 179 | int num_frames_history_ GUARDED_BY(crit_); |
| 180 | int num_frames_buffered_ GUARDED_BY(crit_); |
philipel | 29f730e | 2017-03-15 08:10:08 -0700 | [diff] [blame] | 181 | bool stopped_ GUARDED_BY(crit_); |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 182 | VCMVideoProtection protection_mode_ GUARDED_BY(crit_); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 183 | VCMReceiveStatisticsCallback* const stats_callback_; |
philipel | 65e1f94 | 2017-07-24 08:26:53 -0700 | [diff] [blame] | 184 | int64_t last_log_non_decoded_ms_ GUARDED_BY(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 185 | |
| 186 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | } // namespace video_coding |
| 190 | } // namespace webrtc |
| 191 | |
| 192 | #endif // WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ |