| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ | 11 #ifndef WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ |
| 12 #define WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ | 12 #define WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ |
| 13 | 13 |
| 14 #include <array> | 14 #include <array> |
| 15 #include <map> | 15 #include <map> |
| 16 #include <memory> | 16 #include <memory> |
| 17 #include <set> | |
| 18 #include <utility> | 17 #include <utility> |
| 19 | 18 |
| 20 #include "webrtc/base/constructormagic.h" | 19 #include "webrtc/base/constructormagic.h" |
| 21 #include "webrtc/base/criticalsection.h" | 20 #include "webrtc/base/criticalsection.h" |
| 22 #include "webrtc/base/event.h" | 21 #include "webrtc/base/event.h" |
| 23 #include "webrtc/base/thread_annotations.h" | 22 #include "webrtc/base/thread_annotations.h" |
| 23 #include "webrtc/modules/video_coding/frame_object.h" |
| 24 #include "webrtc/modules/video_coding/include/video_coding_defines.h" | 24 #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
| 25 #include "webrtc/modules/video_coding/inter_frame_delay.h" | 25 #include "webrtc/modules/video_coding/inter_frame_delay.h" |
| 26 #include "webrtc/modules/video_coding/sequence_number_util.h" |
| 26 | 27 |
| 27 namespace webrtc { | 28 namespace webrtc { |
| 28 | 29 |
| 29 class Clock; | 30 class Clock; |
| 30 class VCMJitterEstimator; | 31 class VCMJitterEstimator; |
| 31 class VCMTiming; | 32 class VCMTiming; |
| 32 | 33 |
| 33 namespace video_coding { | 34 namespace video_coding { |
| 34 | 35 |
| 35 class FrameObject; | |
| 36 | |
| 37 class FrameBuffer { | 36 class FrameBuffer { |
| 38 public: | 37 public: |
| 39 enum ReturnReason { kFrameFound, kTimeout, kStopped }; | 38 enum ReturnReason { kFrameFound, kTimeout, kStopped }; |
| 40 | 39 |
| 41 FrameBuffer(Clock* clock, | 40 FrameBuffer(Clock* clock, |
| 42 VCMJitterEstimator* jitter_estimator, | 41 VCMJitterEstimator* jitter_estimator, |
| 43 VCMTiming* timing); | 42 VCMTiming* timing); |
| 44 | 43 |
| 45 // Insert a frame into the frame buffer. | 44 // Insert a frame into the frame buffer. Returns the picture id |
| 46 void InsertFrame(std::unique_ptr<FrameObject> frame); | 45 // of the last continuous frame or -1 if there is no continuous frame. |
| 46 int InsertFrame(std::unique_ptr<FrameObject> frame); |
| 47 | 47 |
| 48 // Get the next frame for decoding. Will return at latest after | 48 // Get the next frame for decoding. Will return at latest after |
| 49 // |max_wait_time_ms|. | 49 // |max_wait_time_ms|. |
| 50 // - If a frame is availiable within |max_wait_time_ms| it will return | 50 // - If a frame is available within |max_wait_time_ms| it will return |
| 51 // kFrameFound and set |frame_out| to the resulting frame. | 51 // kFrameFound and set |frame_out| to the resulting frame. |
| 52 // - If no frame is available after |max_wait_time_ms| it will return | 52 // - If no frame is available after |max_wait_time_ms| it will return |
| 53 // kTimeout. | 53 // kTimeout. |
| 54 // - If the FrameBuffer is stopped then it will return kStopped. | 54 // - If the FrameBuffer is stopped then it will return kStopped. |
| 55 ReturnReason NextFrame(int64_t max_wait_time_ms, | 55 ReturnReason NextFrame(int64_t max_wait_time_ms, |
| 56 std::unique_ptr<FrameObject>* frame_out); | 56 std::unique_ptr<FrameObject>* frame_out); |
| 57 | 57 |
| 58 // Tells the FrameBuffer which protection mode that is in use. Affects | 58 // Tells the FrameBuffer which protection mode that is in use. Affects |
| 59 // the frame timing. | 59 // the frame timing. |
| 60 // TODO(philipel): Remove this when new timing calculations has been | 60 // TODO(philipel): Remove this when new timing calculations has been |
| 61 // implemented. | 61 // implemented. |
| 62 void SetProtectionMode(VCMVideoProtection mode); | 62 void SetProtectionMode(VCMVideoProtection mode); |
| 63 | 63 |
| 64 // Start the frame buffer, has no effect if the frame buffer is started. | 64 // Start the frame buffer, has no effect if the frame buffer is started. |
| 65 // The frame buffer is started upon construction. | 65 // The frame buffer is started upon construction. |
| 66 void Start(); | 66 void Start(); |
| 67 | 67 |
| 68 // Stop the frame buffer, causing any sleeping thread in NextFrame to | 68 // Stop the frame buffer, causing any sleeping thread in NextFrame to |
| 69 // return immediately. | 69 // return immediately. |
| 70 void Stop(); | 70 void Stop(); |
| 71 | 71 |
| 72 private: | 72 private: |
| 73 // FrameKey is a pair of (picture id, spatial layer). | 73 struct FrameKey { |
| 74 using FrameKey = std::pair<uint16_t, uint8_t>; | 74 FrameKey() : picture_id(0), spatial_layer(0) {} |
| 75 FrameKey(uint16_t picture_id, uint8_t spatial_layer) |
| 76 : picture_id(picture_id), spatial_layer(spatial_layer) {} |
| 75 | 77 |
| 76 // Comparator used to sort frames, first on their picture id, and second | 78 bool operator<(const FrameKey& rhs) const { |
| 77 // on their spatial layer. | 79 if (picture_id == rhs.picture_id) |
| 78 struct FrameComp { | 80 return spatial_layer < rhs.spatial_layer; |
| 79 bool operator()(const FrameKey& f1, const FrameKey& f2) const; | 81 return AheadOf(rhs.picture_id, picture_id); |
| 82 } |
| 83 |
| 84 bool operator<=(const FrameKey& rhs) const { return !(rhs < *this); } |
| 85 |
| 86 uint16_t picture_id; |
| 87 uint8_t spatial_layer; |
| 80 }; | 88 }; |
| 81 | 89 |
| 82 // Determines whether a frame is continuous. | 90 struct FrameInfo { |
| 83 bool IsContinuous(const FrameObject& frame) const | 91 // The maximum number of frames that can depend on this frame. |
| 92 static constexpr size_t kMaxNumDependentFrames = 8; |
| 93 |
| 94 // Which other frames that have direct unfulfilled dependencies |
| 95 // on this frame. |
| 96 FrameKey dependent_frames[kMaxNumDependentFrames]; |
| 97 size_t num_dependent_frames = 0; |
| 98 |
| 99 // A frame is continiuous if it has all its referenced/indirectly |
| 100 // referenced frames. |
| 101 // |
| 102 // How many unfulfilled frames this frame have until it becomes continuous. |
| 103 size_t num_missing_continuous = 0; |
| 104 |
| 105 // A frame is decodable if all its referenced frames have been decoded. |
| 106 // |
| 107 // How many unfulfilled frames this frame have until it becomes decodable. |
| 108 size_t num_missing_decodable = 0; |
| 109 |
| 110 // If this frame is continuous or not. |
| 111 bool continuous = false; |
| 112 |
| 113 // The actual FrameObject. |
| 114 std::unique_ptr<FrameObject> frame; |
| 115 }; |
| 116 |
| 117 using FrameMap = std::map<FrameKey, FrameInfo>; |
| 118 |
| 119 // Update all directly dependent and indirectly dependent frames and mark |
| 120 // them as continuous if all their references has been fulfilled. |
| 121 void PropagateContinuity(FrameMap::iterator start) |
| 84 EXCLUSIVE_LOCKS_REQUIRED(crit_); | 122 EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 85 | 123 |
| 86 // Keep track of decoded frames. | 124 // Marks the frame as decoded and updates all directly dependent frames. |
| 87 std::set<FrameKey, FrameComp> decoded_frames_ GUARDED_BY(crit_); | 125 void PropagateDecodability(const FrameInfo& info) |
| 126 EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 88 | 127 |
| 89 // The actual buffer that holds the FrameObjects. | 128 // Advances |last_decoded_frame_it_| to |decoded| and removes old |
| 90 std::map<FrameKey, std::unique_ptr<FrameObject>, FrameComp> frames_ | 129 // frame info. |
| 91 GUARDED_BY(crit_); | 130 void AdvanceLastDecodedFrame(FrameMap::iterator decoded) |
| 131 EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 132 |
| 133 // Update the corresponding FrameInfo of |frame| and all FrameInfos that |
| 134 // |frame| references. |
| 135 // Return false if |frame| will never be decodable, true otherwise. |
| 136 bool UpdateFrameInfoWithIncomingFrame(const FrameObject& frame, |
| 137 FrameMap::iterator info) |
| 138 EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 139 |
| 140 FrameMap frames_ GUARDED_BY(crit_); |
| 92 | 141 |
| 93 rtc::CriticalSection crit_; | 142 rtc::CriticalSection crit_; |
| 94 Clock* const clock_; | 143 Clock* const clock_; |
| 95 rtc::Event frame_inserted_event_; | 144 rtc::Event new_countinuous_frame_event_; |
| 96 VCMJitterEstimator* const jitter_estimator_ GUARDED_BY(crit_); | 145 VCMJitterEstimator* const jitter_estimator_ GUARDED_BY(crit_); |
| 97 VCMTiming* const timing_ GUARDED_BY(crit_); | 146 VCMTiming* const timing_ GUARDED_BY(crit_); |
| 98 VCMInterFrameDelay inter_frame_delay_ GUARDED_BY(crit_); | 147 VCMInterFrameDelay inter_frame_delay_ GUARDED_BY(crit_); |
| 99 int newest_picture_id_ GUARDED_BY(crit_); | 148 FrameMap::iterator last_decoded_frame_it_ GUARDED_BY(crit_); |
| 149 FrameMap::iterator last_continuous_frame_it_ GUARDED_BY(crit_); |
| 150 int num_frames_history_ GUARDED_BY(crit_); |
| 151 int num_frames_buffered_ GUARDED_BY(crit_); |
| 100 bool stopped_ GUARDED_BY(crit_); | 152 bool stopped_ GUARDED_BY(crit_); |
| 101 VCMVideoProtection protection_mode_ GUARDED_BY(crit_); | 153 VCMVideoProtection protection_mode_ GUARDED_BY(crit_); |
| 102 | 154 |
| 103 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer); | 155 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer); |
| 104 }; | 156 }; |
| 105 | 157 |
| 106 } // namespace video_coding | 158 } // namespace video_coding |
| 107 } // namespace webrtc | 159 } // namespace webrtc |
| 108 | 160 |
| 109 #endif // WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ | 161 #endif // WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ |
| OLD | NEW |