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& other) const { |
77 // on their spatial layer. | 79 if (picture_id == other.picture_id) |
78 struct FrameComp { | 80 return spatial_layer < other.spatial_layer; |
79 bool operator()(const FrameKey& f1, const FrameKey& f2) const; | 81 return AheadOf(other.picture_id, picture_id); |
82 } | |
83 | |
84 bool operator<=(const FrameKey& other) const { return !(other < *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 static constexpr size_t kMaxNumDependentFrames = 5; |
92 | |
93 // Which other frames that have unfulfilled dependencies on this frame. | |
94 FrameKey dependent_frames[kMaxNumDependentFrames]; | |
95 size_t num_dependent_frames = 0; | |
96 | |
97 // A frame is continiuous if have all its referenced/indirectly referenced | |
98 // frames. | |
99 // | |
100 // How many unfulfilled frames this frame have to become continuous. | |
101 size_t num_missing_continuous = 0; | |
102 | |
103 // A frame is decodable if all its referenced frames have been decoded. | |
104 // | |
105 // How many unfulfilled frames this frame have to become decodable. | |
106 size_t num_missing_decodable = 0; | |
107 | |
108 // If this frame is continuous or not. | |
109 bool continuous = false; | |
110 | |
111 // The actual FrameObject. | |
112 std::unique_ptr<FrameObject> frame; | |
113 }; | |
114 | |
115 using FrameMap = std::map<FrameKey, FrameInfo>; | |
116 | |
117 // Update all directly dependent and indirectly dependent frames and mark | |
118 // them as continuous if all their references has been fulfilled. | |
119 void PropagateContinuity(const FrameMap::iterator start) | |
danilchap
2016/09/19 17:44:48
remove const
philipel
2016/09/20 09:30:25
Done.
| |
84 EXCLUSIVE_LOCKS_REQUIRED(crit_); | 120 EXCLUSIVE_LOCKS_REQUIRED(crit_); |
85 | 121 |
86 // Keep track of decoded frames. | 122 // Mark the frame as decoded and updates all directly dependent frames. |
87 std::set<FrameKey, FrameComp> decoded_frames_ GUARDED_BY(crit_); | 123 void PropagateDecodability(const FrameInfo& info) |
124 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
88 | 125 |
89 // The actual buffer that holds the FrameObjects. | 126 // Update the corresponding FrameInfo of |frame| and all FrameInfos that |
90 std::map<FrameKey, std::unique_ptr<FrameObject>, FrameComp> frames_ | 127 // |frame| references. |
91 GUARDED_BY(crit_); | 128 // Return false if |frame| will never be decodable, true otherwise. |
129 bool UpdateFrameInfoWithIncomingFrame(const FrameObject& frame, | |
130 FrameMap::iterator info) | |
131 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
132 | |
133 // Advances |last_decoded_frame_it_| to |decoded| and remove old | |
134 // frame info. | |
135 void AdvanceLastDecodedFrame(const FrameMap::iterator decoded) | |
danilchap
2016/09/19 17:44:48
ditto
philipel
2016/09/20 09:30:25
Done.
| |
136 EXCLUSIVE_LOCKS_REQUIRED(crit_); | |
137 | |
138 FrameMap frames_ GUARDED_BY(crit_); | |
92 | 139 |
93 rtc::CriticalSection crit_; | 140 rtc::CriticalSection crit_; |
94 Clock* const clock_; | 141 Clock* const clock_; |
95 rtc::Event frame_inserted_event_; | 142 rtc::Event new_countinuous_frame_event_; |
96 VCMJitterEstimator* const jitter_estimator_ GUARDED_BY(crit_); | 143 VCMJitterEstimator* const jitter_estimator_ GUARDED_BY(crit_); |
97 VCMTiming* const timing_ GUARDED_BY(crit_); | 144 VCMTiming* const timing_ GUARDED_BY(crit_); |
98 VCMInterFrameDelay inter_frame_delay_ GUARDED_BY(crit_); | 145 VCMInterFrameDelay inter_frame_delay_ GUARDED_BY(crit_); |
99 int newest_picture_id_ GUARDED_BY(crit_); | 146 FrameMap::iterator last_decoded_frame_it_ GUARDED_BY(crit_); |
danilchap
2016/09/19 17:44:48
the logic might be simplified if instead of keepin
philipel
2016/09/20 09:30:25
Well, that is how it worked in PS3, and then I cha
danilchap
2016/09/20 11:15:08
no, PS3 did it differently than what I propose her
philipel
2016/09/20 11:44:44
I'm not sure I understand your suggestion.
philipel
2016/09/20 13:11:08
Unfortunately it required a few more changes since
danilchap
2016/09/20 13:45:24
Hm, yes, sorry, missed scenario last_continuous_fr
| |
147 FrameMap::iterator last_continuous_frame_it_ GUARDED_BY(crit_); | |
148 int num_frames_history_ GUARDED_BY(crit_); | |
149 int num_frames_buffered_ GUARDED_BY(crit_); | |
100 bool stopped_ GUARDED_BY(crit_); | 150 bool stopped_ GUARDED_BY(crit_); |
101 VCMVideoProtection protection_mode_ GUARDED_BY(crit_); | 151 VCMVideoProtection protection_mode_ GUARDED_BY(crit_); |
102 | 152 |
103 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer); | 153 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(FrameBuffer); |
104 }; | 154 }; |
105 | 155 |
106 } // namespace video_coding | 156 } // namespace video_coding |
107 } // namespace webrtc | 157 } // namespace webrtc |
108 | 158 |
109 #endif // WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ | 159 #endif // WEBRTC_MODULES_VIDEO_CODING_FRAME_BUFFER2_H_ |
OLD | NEW |