Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: webrtc/modules/video_coding/frame_buffer2.cc

Issue 2322263002: Frame continuity is now tested as soon as a frame is inserted into the FrameBuffer. (Closed)
Patch Set: Feedback Fixes. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "webrtc/modules/video_coding/frame_buffer2.h" 11 #include "webrtc/modules/video_coding/frame_buffer2.h"
12 12
13 #include <algorithm> 13 #include <algorithm>
14 #include <cstring>
15 #include <queue>
14 16
15 #include "webrtc/base/checks.h" 17 #include "webrtc/base/checks.h"
16 #include "webrtc/modules/video_coding/frame_object.h" 18 #include "webrtc/base/logging.h"
17 #include "webrtc/modules/video_coding/jitter_estimator.h" 19 #include "webrtc/modules/video_coding/jitter_estimator.h"
18 #include "webrtc/modules/video_coding/sequence_number_util.h"
19 #include "webrtc/modules/video_coding/timing.h" 20 #include "webrtc/modules/video_coding/timing.h"
20 #include "webrtc/system_wrappers/include/clock.h" 21 #include "webrtc/system_wrappers/include/clock.h"
21 22
22 namespace webrtc { 23 namespace webrtc {
23 namespace video_coding { 24 namespace video_coding {
24 25
25 namespace { 26 namespace {
26 // The maximum age of decoded frames tracked by frame buffer, compared to 27 // Max number of frames the buffer will hold.
27 // |newest_picture_id_|. 28 constexpr int kMaxFramesBuffered = 1000;
28 constexpr int kMaxFrameAge = 4096;
29 29
30 // The maximum number of decoded frames being tracked by the frame buffer. 30 // Max number of decoded frame info that will be saved.
31 constexpr int kMaxNumHistoryFrames = 256; 31 constexpr int kMaxFramesHistory = 20;
32 } // namespace 32 } // namespace
33 33
34 bool FrameBuffer::FrameComp::operator()(const FrameKey& f1,
35 const FrameKey& f2) const {
36 // first = picture id
37 // second = spatial layer
38 if (f1.first == f2.first)
39 return f1.second < f2.second;
40 return AheadOf(f2.first, f1.first);
41 }
42
43 FrameBuffer::FrameBuffer(Clock* clock, 34 FrameBuffer::FrameBuffer(Clock* clock,
44 VCMJitterEstimator* jitter_estimator, 35 VCMJitterEstimator* jitter_estimator,
45 VCMTiming* timing) 36 VCMTiming* timing)
46 : clock_(clock), 37 : clock_(clock),
47 frame_inserted_event_(false, false), 38 new_countinuous_frame_event_(false, false),
48 jitter_estimator_(jitter_estimator), 39 jitter_estimator_(jitter_estimator),
49 timing_(timing), 40 timing_(timing),
50 inter_frame_delay_(clock_->TimeInMilliseconds()), 41 inter_frame_delay_(clock_->TimeInMilliseconds()),
51 newest_picture_id_(-1), 42 last_decoded_frame_it_(frames_.end()),
43 last_continuous_frame_it_(frames_.end()),
44 num_frames_history_(0),
45 num_frames_buffered_(0),
52 stopped_(false), 46 stopped_(false),
53 protection_mode_(kProtectionNack) {} 47 protection_mode_(kProtectionNack) {}
54 48
55 FrameBuffer::ReturnReason FrameBuffer::NextFrame( 49 FrameBuffer::ReturnReason FrameBuffer::NextFrame(
56 int64_t max_wait_time_ms, 50 int64_t max_wait_time_ms,
57 std::unique_ptr<FrameObject>* frame_out) { 51 std::unique_ptr<FrameObject>* frame_out) {
58 int64_t latest_return_time = clock_->TimeInMilliseconds() + max_wait_time_ms; 52 int64_t latest_return_time = clock_->TimeInMilliseconds() + max_wait_time_ms;
59 int64_t now = clock_->TimeInMilliseconds(); 53 int64_t now_ms = clock_->TimeInMilliseconds();
danilchap 2016/09/19 17:44:48 now_ms is not used outside do loop, so might be be
philipel 2016/09/20 09:30:25 Done.
60 int64_t wait_ms = max_wait_time_ms; 54 int64_t wait_ms = max_wait_time_ms;
61 while (true) { 55 FrameMap::iterator next_frame_it;
62 std::map<FrameKey, std::unique_ptr<FrameObject>, FrameComp>::iterator 56
63 next_frame_it; 57 do {
64 { 58 {
65 rtc::CritScope lock(&crit_); 59 rtc::CritScope lock(&crit_);
66 frame_inserted_event_.Reset(); 60 new_countinuous_frame_event_.Reset();
67 if (stopped_) 61 if (stopped_)
68 return kStopped; 62 return kStopped;
69 63
70 now = clock_->TimeInMilliseconds(); 64 now_ms = clock_->TimeInMilliseconds();
71 wait_ms = max_wait_time_ms; 65 wait_ms = max_wait_time_ms;
66
67 // Need to hold |crit_| in order to use |frames_|, therefore we
68 // set it here in the loop instead of outside the loop in order to not
69 // acquire the lock unnecesserily.
72 next_frame_it = frames_.end(); 70 next_frame_it = frames_.end();
73 for (auto frame_it = frames_.begin(); frame_it != frames_.end();
74 ++frame_it) {
75 const FrameObject& frame = *frame_it->second;
76 if (IsContinuous(frame)) {
77 next_frame_it = frame_it;
78 int64_t render_time =
79 next_frame_it->second->RenderTime() == -1
80 ? timing_->RenderTimeMs(frame.timestamp, now)
81 : next_frame_it->second->RenderTime();
82 wait_ms = timing_->MaxWaitingTime(render_time, now);
83 frame_it->second->SetRenderTime(render_time);
84 71
85 // This will cause the frame buffer to prefer high framerate rather 72 // |frame_it| points to the first frame after the
86 // than high resolution in the case of the decoder not decoding fast 73 // |last_decoded_frame_it_|.
87 // enough and the stream has multiple spatial and temporal layers. 74 auto frame_it = frames_.end();
88 if (wait_ms == 0) 75 if (last_decoded_frame_it_ == frames_.end()) {
89 continue; 76 frame_it = frames_.begin();
77 } else {
78 frame_it = last_decoded_frame_it_;
79 ++frame_it;
80 }
90 81
91 break; 82 // |continuous_end_it| point to the first frame after the
92 } 83 // |last_continuous_frame_it_|.
84 auto continuous_end_it = last_continuous_frame_it_;
85 if (continuous_end_it != frames_.end())
86 ++continuous_end_it;
87
88 for (; frame_it != continuous_end_it; ++frame_it) {
89 if (frame_it->second.num_missing_decodable > 0)
90 continue;
91
92 FrameObject* frame = frame_it->second.frame.get();
93 next_frame_it = frame_it;
94 int64_t render_time =
95 frame->RenderTime() == -1
96 ? timing_->RenderTimeMs(frame->timestamp, now_ms)
97 : frame->RenderTime();
98 wait_ms = timing_->MaxWaitingTime(render_time, now_ms);
99 frame->SetRenderTime(render_time);
100
101 // This will cause the frame buffer to prefer high framerate rather
102 // than high resolution in the case of the decoder not decoding fast
103 // enough and the stream has multiple spatial and temporal layers.
104 if (wait_ms == 0)
105 continue;
106
107 break;
93 } 108 }
109 } // rtc::Critscope lock(&crit_);
110
111 wait_ms = std::min<int64_t>(wait_ms, latest_return_time - now_ms);
112 wait_ms = std::max<int64_t>(wait_ms, 0);
113 } while (new_countinuous_frame_event_.Wait(wait_ms));
114
115 rtc::CritScope lock(&crit_);
116 if (next_frame_it != frames_.end()) {
117 std::unique_ptr<FrameObject> frame = std::move(next_frame_it->second.frame);
118 int64_t received_timestamp = frame->ReceivedTime();
119 uint32_t timestamp = frame->Timestamp();
120
121 int64_t frame_delay;
122 if (inter_frame_delay_.CalculateDelay(timestamp, &frame_delay,
123 received_timestamp)) {
124 jitter_estimator_->UpdateEstimate(frame_delay, frame->size);
94 } 125 }
126 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
127 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
128 timing_->UpdateCurrentDelay(frame->RenderTime(),
129 clock_->TimeInMilliseconds());
95 130
96 wait_ms = std::min<int64_t>(wait_ms, latest_return_time - now); 131 PropagateDecodability(next_frame_it->second);
97 wait_ms = std::max<int64_t>(wait_ms, 0); 132 AdvanceLastDecodedFrame(next_frame_it);
98 // If the timeout occurs, return. Otherwise a new frame has been inserted 133 *frame_out = std::move(frame);
99 // and the best frame to decode next will be selected again. 134 return kFrameFound;
100 if (!frame_inserted_event_.Wait(wait_ms)) { 135 } else {
101 rtc::CritScope lock(&crit_); 136 return kTimeout;
102 if (next_frame_it != frames_.end()) {
103 int64_t received_timestamp = next_frame_it->second->ReceivedTime();
104 uint32_t timestamp = next_frame_it->second->Timestamp();
105
106 int64_t frame_delay;
107 if (inter_frame_delay_.CalculateDelay(timestamp, &frame_delay,
108 received_timestamp)) {
109 jitter_estimator_->UpdateEstimate(frame_delay,
110 next_frame_it->second->size);
111 }
112 float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0;
113 timing_->SetJitterDelay(jitter_estimator_->GetJitterEstimate(rtt_mult));
114 timing_->UpdateCurrentDelay(next_frame_it->second->RenderTime(),
115 clock_->TimeInMilliseconds());
116
117 decoded_frames_.insert(next_frame_it->first);
118 std::unique_ptr<FrameObject> frame = std::move(next_frame_it->second);
119 frames_.erase(frames_.begin(), ++next_frame_it);
120 *frame_out = std::move(frame);
121 return kFrameFound;
122 } else {
123 return kTimeout;
124 }
125 }
126 } 137 }
127 } 138 }
128 139
129 void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) { 140 void FrameBuffer::SetProtectionMode(VCMVideoProtection mode) {
130 rtc::CritScope lock(&crit_); 141 rtc::CritScope lock(&crit_);
131 protection_mode_ = mode; 142 protection_mode_ = mode;
132 } 143 }
133 144
134 void FrameBuffer::Start() { 145 void FrameBuffer::Start() {
135 rtc::CritScope lock(&crit_); 146 rtc::CritScope lock(&crit_);
136 stopped_ = false; 147 stopped_ = false;
137 } 148 }
138 149
139 void FrameBuffer::Stop() { 150 void FrameBuffer::Stop() {
140 rtc::CritScope lock(&crit_); 151 rtc::CritScope lock(&crit_);
141 stopped_ = true; 152 stopped_ = true;
142 frame_inserted_event_.Set(); 153 new_countinuous_frame_event_.Set();
143 } 154 }
144 155
145 void FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) { 156 int FrameBuffer::InsertFrame(std::unique_ptr<FrameObject> frame) {
146 rtc::CritScope lock(&crit_); 157 rtc::CritScope lock(&crit_);
147 // If |newest_picture_id_| is -1 then this is the first frame we received. 158 FrameKey key(frame->picture_id, frame->spatial_layer);
148 if (newest_picture_id_ == -1) 159 int last_continuous_picture_id =
149 newest_picture_id_ = frame->picture_id; 160 last_continuous_frame_it_ == frames_.end()
161 ? -1
162 : last_continuous_frame_it_->first.picture_id;
150 163
151 if (AheadOf<uint16_t>(frame->picture_id, newest_picture_id_)) 164 if (num_frames_buffered_ >= kMaxFramesBuffered) {
152 newest_picture_id_ = frame->picture_id; 165 LOG(LS_INFO) << "Frame with (picture_id:spatial_id) (" << key.picture_id
166 << ":" << int(key.spatial_layer)
167 << ") could not be inserted due to the frame "
168 << "buffer being full, dropping frame.";
169 return last_continuous_picture_id;
170 }
153 171
154 // Remove frames as long as we have too many, |kMaxNumHistoryFrames|. 172 if (frame->inter_layer_predicted && frame->spatial_layer == 0) {
155 while (decoded_frames_.size() > kMaxNumHistoryFrames) 173 LOG(LS_INFO) << "Frame with (picture_id:spatial_id) (" << key.picture_id
156 decoded_frames_.erase(decoded_frames_.begin()); 174 << ":" << int(key.spatial_layer)
175 << ") is marked as inter layer predicted, dropping frame.";
176 return last_continuous_picture_id;
177 }
157 178
158 // Remove frames that are too old. 179 if (last_decoded_frame_it_ != frames_.end() &&
159 uint16_t old_picture_id = Subtract<1 << 16>(newest_picture_id_, kMaxFrameAge); 180 key < last_decoded_frame_it_->first) {
160 auto old_decoded_it = 181 LOG(LS_INFO) << "Frame with (picture_id:spatial_id) (" << key.picture_id
161 decoded_frames_.lower_bound(FrameKey(old_picture_id, 0)); 182 << ":" << int(key.spatial_layer) << ") inserted after frame ("
162 decoded_frames_.erase(decoded_frames_.begin(), old_decoded_it); 183 << last_decoded_frame_it_->first.picture_id << ":"
184 << int(last_decoded_frame_it_->first.spatial_layer)
185 << ") was handed off for decoding, dropping frame.";
186 return last_continuous_picture_id;
187 }
163 188
164 FrameKey key(frame->picture_id, frame->spatial_layer); 189 auto info = frames_.insert(std::make_pair(key, FrameInfo())).first;
165 frames_[key] = std::move(frame); 190
166 frame_inserted_event_.Set(); 191 if (!UpdateFrameInfoWithIncomingFrame(*frame, info)) {
192 frames_.erase(info);
193 return last_continuous_picture_id;
194 }
195
196 info->second.frame = std::move(frame);
197 ++num_frames_buffered_;
198
199 if (info->second.num_missing_continuous == 0) {
200 info->second.continuous = true;
201 PropagateContinuity(info);
202 last_continuous_picture_id = last_continuous_frame_it_->first.picture_id;
203
204 // Since we now have new continuous frames there might be a better frame
205 // to return from NextFrame. Signal that thread so that it again can choose
206 // which frame to return.
207 new_countinuous_frame_event_.Set();
208 }
209
210 return last_continuous_picture_id;
167 } 211 }
168 212
169 bool FrameBuffer::IsContinuous(const FrameObject& frame) const { 213 void FrameBuffer::PropagateContinuity(FrameMap::iterator start) {
170 // If a frame with an earlier picture id was inserted compared to the last 214 RTC_DCHECK(start->second.continuous);
171 // decoded frames picture id then that frame arrived too late. 215 if (last_continuous_frame_it_ == frames_.end())
172 if (!decoded_frames_.empty() && 216 last_continuous_frame_it_ = start;
173 AheadOf(decoded_frames_.rbegin()->first, frame.picture_id)) { 217
174 return false; 218 std::queue<FrameMap::iterator> continuous_frames;
219 continuous_frames.push(start);
220
221 // A simple BFS to traverse continuous frames.
222 while (!continuous_frames.empty()) {
223 auto frame = continuous_frames.front();
224 continuous_frames.pop();
225
226 if (last_continuous_frame_it_->first < frame->first)
227 last_continuous_frame_it_ = frame;
228
229 // Loop through all dependent frames, and if that frame no longer has
230 // any unfulfilled dependencies then that frame is continuous as well.
231 for (size_t d = 0; d < frame->second.num_dependent_frames; ++d) {
232 auto frame_ref = frames_.find(frame->second.dependent_frames[d]);
233 --frame_ref->second.num_missing_continuous;
234
235 if (frame_ref->second.num_missing_continuous == 0) {
236 frame_ref->second.continuous = true;
237 continuous_frames.push(frame_ref);
238 }
239 }
240 }
241 }
242
243 void FrameBuffer::PropagateDecodability(const FrameInfo& info) {
244 for (size_t d = 0; d < info.num_dependent_frames; ++d) {
245 auto ref_info = frames_.find(info.dependent_frames[d]);
246 RTC_DCHECK_GT(ref_info->second.num_missing_decodable, 0U);
247 --ref_info->second.num_missing_decodable;
248 }
249 }
250
251 void FrameBuffer::AdvanceLastDecodedFrame(FrameMap::iterator decoded) {
danilchap 2016/09/19 17:44:48 Order [new] functions same as in header file, i.e.
philipel 2016/09/20 09:30:25 Done.
252 if (last_decoded_frame_it_ == frames_.end()) {
253 last_decoded_frame_it_ = frames_.begin();
254 } else {
255 RTC_DCHECK(last_decoded_frame_it_->first < decoded->first);
256 ++last_decoded_frame_it_;
257 }
258 --num_frames_buffered_;
259 ++num_frames_history_;
260
261 // First, delete non-decoded frames from the history.
262 while (last_decoded_frame_it_ != decoded) {
263 last_decoded_frame_it_ = frames_.erase(last_decoded_frame_it_);
264 --num_frames_buffered_;
danilchap 2016/09/19 17:44:48 what is num_frames_buffered? number of frame objec
philipel 2016/09/20 09:30:25 Suppose to be number of buffered frames, fixed
175 } 265 }
176 266
177 // Have we decoded all frames that this frame depend on? 267 // Then remove old history if we have too much history saved.
268 if (num_frames_history_ > kMaxFramesHistory) {
269 frames_.erase(frames_.begin());
270 --num_frames_history_;
271 }
272 }
273
274 bool FrameBuffer::UpdateFrameInfoWithIncomingFrame(const FrameObject& frame,
275 FrameMap::iterator info) {
276 FrameKey key(frame.picture_id, frame.spatial_layer);
277 info->second.num_missing_continuous = frame.num_references;
278 info->second.num_missing_decodable = frame.num_references;
279
280 RTC_DCHECK(last_decoded_frame_it_ == frames_.end() ||
281 last_decoded_frame_it_->first < info->first);
282
283 // Check how many dependencies that has already been fulfilled.
178 for (size_t r = 0; r < frame.num_references; ++r) { 284 for (size_t r = 0; r < frame.num_references; ++r) {
179 FrameKey ref_key(frame.references[r], frame.spatial_layer); 285 FrameKey ref_key(frame.references[r], frame.spatial_layer);
180 if (decoded_frames_.find(ref_key) == decoded_frames_.end()) 286 auto ref_info = frames_.find(ref_key);
181 return false; 287
288 // Does |frame| depend on a frame earlier than the last decoded frame?
289 if (last_decoded_frame_it_ != frames_.end() &&
290 ref_key <= last_decoded_frame_it_->first) {
291 // Does |frame| depend on a frame that was never decoded?
292 if (ref_info == frames_.end()) {
293 LOG(LS_INFO) << "Frame with (picture_id:spatial_id) (" << key.picture_id
294 << ":" << int(key.spatial_layer)
295 << " depends on a non-decoded frame more previous than "
296 << "the last decoded frame, dropping frame.";
297 return false;
298 }
299
300 --info->second.num_missing_continuous;
301 --info->second.num_missing_decodable;
302
303 } else {
304 if (ref_info == frames_.end())
305 ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
306
307 if (ref_info->second.continuous)
308 --info->second.num_missing_continuous;
309
310 // Add backwards reference so |frame| can be updated when new
311 // frames are inserted or decoded.
312 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
313 key;
314 ++ref_info->second.num_dependent_frames;
315 }
182 } 316 }
183 317
184 // If this is a layer frame, have we decoded the lower layer of this 318 // Check if we have the lower spatial layer frame.
185 // super frame.
186 if (frame.inter_layer_predicted) { 319 if (frame.inter_layer_predicted) {
187 RTC_DCHECK_GT(frame.spatial_layer, 0); 320 ++info->second.num_missing_continuous;
321 ++info->second.num_missing_decodable;
danilchap 2016/09/19 17:44:48 no check in this block if referenced frame was alr
philipel 2016/09/20 09:30:25 No need, the DCHECK on line 280 ensures us that we
danilchap 2016/09/20 11:15:08 What about situation ref_key == last_decoded_frame
philipel 2016/09/20 11:44:44 You are absolutely right, fixed + added unittest.
322
188 FrameKey ref_key(frame.picture_id, frame.spatial_layer - 1); 323 FrameKey ref_key(frame.picture_id, frame.spatial_layer - 1);
189 if (decoded_frames_.find(ref_key) == decoded_frames_.end()) 324 auto ref_info = frames_.insert(std::make_pair(ref_key, FrameInfo())).first;
190 return false; 325 if (ref_info->second.continuous)
326 --info->second.num_missing_continuous;
327
328 ref_info->second.dependent_frames[ref_info->second.num_dependent_frames] =
329 key;
330 ++ref_info->second.num_dependent_frames;
191 } 331 }
192 332
193 return true; 333 return true;
194 } 334 }
195 335
196 } // namespace video_coding 336 } // namespace video_coding
197 } // namespace webrtc 337 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698