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 | |
| 19 | #include "webrtc/base/constructormagic.h" |
| 20 | #include "webrtc/base/criticalsection.h" |
| 21 | #include "webrtc/base/event.h" |
| 22 | #include "webrtc/base/thread_annotations.h" |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 23 | #include "webrtc/modules/video_coding/frame_object.h" |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 24 | #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
| 25 | #include "webrtc/modules/video_coding/inter_frame_delay.h" |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 26 | #include "webrtc/modules/video_coding/sequence_number_util.h" |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | class Clock; |
| 31 | class VCMJitterEstimator; |
| 32 | class VCMTiming; |
| 33 | |
| 34 | namespace video_coding { |
| 35 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 36 | class FrameBuffer { |
| 37 | public: |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 38 | enum ReturnReason { kFrameFound, kTimeout, kStopped }; |
| 39 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 40 | FrameBuffer(Clock* clock, |
| 41 | VCMJitterEstimator* jitter_estimator, |
philipel | c08c191 | 2017-01-17 04:03:53 -0800 | [diff] [blame] | 42 | VCMTiming* timing); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 43 | |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 44 | virtual ~FrameBuffer(); |
| 45 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 46 | // Insert a frame into the frame buffer. Returns the picture id |
| 47 | // of the last continuous frame or -1 if there is no continuous frame. |
| 48 | int InsertFrame(std::unique_ptr<FrameObject> frame); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 49 | |
| 50 | // Get the next frame for decoding. Will return at latest after |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 51 | // |max_wait_time_ms|. |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 52 | // - If a frame is available within |max_wait_time_ms| it will return |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 53 | // kFrameFound and set |frame_out| to the resulting frame. |
| 54 | // - If no frame is available after |max_wait_time_ms| it will return |
| 55 | // kTimeout. |
| 56 | // - If the FrameBuffer is stopped then it will return kStopped. |
| 57 | ReturnReason NextFrame(int64_t max_wait_time_ms, |
| 58 | std::unique_ptr<FrameObject>* frame_out); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 59 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 60 | // Tells the FrameBuffer which protection mode that is in use. Affects |
| 61 | // the frame timing. |
| 62 | // TODO(philipel): Remove this when new timing calculations has been |
| 63 | // implemented. |
| 64 | void SetProtectionMode(VCMVideoProtection mode); |
| 65 | |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 66 | // Start the frame buffer, has no effect if the frame buffer is started. |
| 67 | // The frame buffer is started upon construction. |
| 68 | void Start(); |
| 69 | |
| 70 | // Stop the frame buffer, causing any sleeping thread in NextFrame to |
| 71 | // return immediately. |
| 72 | void Stop(); |
| 73 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 74 | private: |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 75 | struct FrameKey { |
| 76 | FrameKey() : picture_id(0), spatial_layer(0) {} |
| 77 | FrameKey(uint16_t picture_id, uint8_t spatial_layer) |
| 78 | : picture_id(picture_id), spatial_layer(spatial_layer) {} |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 79 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 80 | bool operator<(const FrameKey& rhs) const { |
| 81 | if (picture_id == rhs.picture_id) |
| 82 | return spatial_layer < rhs.spatial_layer; |
| 83 | return AheadOf(rhs.picture_id, picture_id); |
| 84 | } |
| 85 | |
| 86 | bool operator<=(const FrameKey& rhs) const { return !(rhs < *this); } |
| 87 | |
| 88 | uint16_t picture_id; |
| 89 | uint8_t spatial_layer; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 92 | struct FrameInfo { |
| 93 | // The maximum number of frames that can depend on this frame. |
| 94 | static constexpr size_t kMaxNumDependentFrames = 8; |
| 95 | |
| 96 | // Which other frames that have direct unfulfilled dependencies |
| 97 | // on this frame. |
| 98 | FrameKey dependent_frames[kMaxNumDependentFrames]; |
| 99 | size_t num_dependent_frames = 0; |
| 100 | |
| 101 | // A frame is continiuous if it has all its referenced/indirectly |
| 102 | // referenced frames. |
| 103 | // |
| 104 | // How many unfulfilled frames this frame have until it becomes continuous. |
| 105 | size_t num_missing_continuous = 0; |
| 106 | |
| 107 | // A frame is decodable if all its referenced frames have been decoded. |
| 108 | // |
| 109 | // How many unfulfilled frames this frame have until it becomes decodable. |
| 110 | size_t num_missing_decodable = 0; |
| 111 | |
| 112 | // If this frame is continuous or not. |
| 113 | bool continuous = false; |
| 114 | |
| 115 | // The actual FrameObject. |
| 116 | std::unique_ptr<FrameObject> frame; |
| 117 | }; |
| 118 | |
| 119 | using FrameMap = std::map<FrameKey, FrameInfo>; |
| 120 | |
| 121 | // Update all directly dependent and indirectly dependent frames and mark |
| 122 | // them as continuous if all their references has been fulfilled. |
| 123 | void PropagateContinuity(FrameMap::iterator start) |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 124 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 125 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 126 | // Marks the frame as decoded and updates all directly dependent frames. |
| 127 | void PropagateDecodability(const FrameInfo& info) |
| 128 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 129 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 130 | // Advances |last_decoded_frame_it_| to |decoded| and removes old |
| 131 | // frame info. |
| 132 | void AdvanceLastDecodedFrame(FrameMap::iterator decoded) |
| 133 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 134 | |
| 135 | // Update the corresponding FrameInfo of |frame| and all FrameInfos that |
| 136 | // |frame| references. |
| 137 | // Return false if |frame| will never be decodable, true otherwise. |
| 138 | bool UpdateFrameInfoWithIncomingFrame(const FrameObject& frame, |
| 139 | FrameMap::iterator info) |
| 140 | EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 141 | |
philipel | be74270 | 2016-11-30 01:31:40 -0800 | [diff] [blame] | 142 | void UpdateJitterDelay() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 143 | |
philipel | c08c191 | 2017-01-17 04:03:53 -0800 | [diff] [blame] | 144 | void UpdateHistograms() const; |
| 145 | |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame^] | 146 | void ClearFramesAndHistory() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 147 | |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 148 | FrameMap frames_ GUARDED_BY(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 149 | |
| 150 | rtc::CriticalSection crit_; |
| 151 | Clock* const clock_; |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 152 | rtc::Event new_countinuous_frame_event_; |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 153 | VCMJitterEstimator* const jitter_estimator_ GUARDED_BY(crit_); |
| 154 | VCMTiming* const timing_ GUARDED_BY(crit_); |
| 155 | VCMInterFrameDelay inter_frame_delay_ GUARDED_BY(crit_); |
philipel | fcc6006 | 2017-01-18 05:35:20 -0800 | [diff] [blame^] | 156 | uint32_t last_decoded_frame_timestamp_ GUARDED_BY(crit_); |
philipel | e0b2f15 | 2016-09-28 10:23:49 +0200 | [diff] [blame] | 157 | FrameMap::iterator last_decoded_frame_it_ GUARDED_BY(crit_); |
| 158 | FrameMap::iterator last_continuous_frame_it_ GUARDED_BY(crit_); |
| 159 | int num_frames_history_ GUARDED_BY(crit_); |
| 160 | int num_frames_buffered_ GUARDED_BY(crit_); |
philipel | 504c47d | 2016-06-30 17:33:02 +0200 | [diff] [blame] | 161 | bool stopped_ GUARDED_BY(crit_); |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 162 | VCMVideoProtection protection_mode_ GUARDED_BY(crit_); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 163 | |
| 164 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer); |
philipel | c08c191 | 2017-01-17 04:03:53 -0800 | [diff] [blame] | 165 | |
| 166 | // For WebRTC.Video.JitterBufferDelayInMs metric. |
| 167 | int64_t accumulated_delay_ = 0; |
| 168 | int64_t accumulated_delay_samples_ = 0; |
| 169 | |
| 170 | // For WebRTC.Video.KeyFramesReceivedInPermille metric. |
| 171 | int64_t num_total_frames_ = 0; |
| 172 | int64_t num_key_frames_ = 0; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | } // namespace video_coding |
| 176 | } // namespace webrtc |
| 177 | |
| 178 | #endif // WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ |