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

Side by Side Diff: media/filters/video_renderer_algorithm.cc

Issue 1021943002: Introduce cadence based VideoRendererAlgorithm. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improve EffectiveFramesQueued. Created 5 years, 8 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
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/filters/video_renderer_algorithm.h"
6
7 #include <algorithm>
8 #include <limits>
9
10 namespace media {
11
12 // The number of frames to store for moving average calculations. Value picked
13 // after experimenting with playback of various local media and YouTube clips.
14 static const int kMovingAverageSamples = 25;
15
16 VideoRendererAlgorithm::ReadyFrame::ReadyFrame(
17 const scoped_refptr<VideoFrame>& ready_frame)
18 : frame(ready_frame),
19 ideal_render_count(0),
20 render_count(0),
21 drop_count(0) {
22 }
23
24 VideoRendererAlgorithm::ReadyFrame::~ReadyFrame() {
25 }
26
27 bool VideoRendererAlgorithm::ReadyFrame::operator<(
28 const ReadyFrame& other) const {
29 return frame->timestamp() < other.frame->timestamp();
30 }
31
32 VideoRendererAlgorithm::VideoRendererAlgorithm(
33 const TimeConverterCB& time_converter_cb)
34 : cadence_estimator_(base::TimeDelta::FromSeconds(
35 kMinimumAcceptableTimeBetweenGlitchesSecs)),
36 time_converter_cb_(time_converter_cb),
37 frame_duration_calculator_(kMovingAverageSamples),
38 frame_dropping_disabled_(false) {
39 DCHECK(!time_converter_cb_.is_null());
40 Reset();
41 }
42
43 VideoRendererAlgorithm::~VideoRendererAlgorithm() {
44 }
45
46 scoped_refptr<VideoFrame> VideoRendererAlgorithm::Render(
47 base::TimeTicks deadline_min,
48 base::TimeTicks deadline_max,
49 size_t* frames_dropped) {
50 DCHECK(deadline_min < deadline_max);
51
52 if (frame_queue_.empty())
53 return nullptr;
54
55 if (frames_dropped)
56 *frames_dropped = 0;
57
58 // Once Render() is called |last_frame_index_| has meaning and should thus be
59 // preserved even if better frames come in before it due to out of order
60 // timestamps.
61 have_rendered_frames_ = true;
62
63 // Step 1: Update the current render interval for subroutines.
64 render_interval_ = deadline_max - deadline_min;
65
66 // Step 2: Figure out if any intervals have been skipped since the last call
67 // to Render(). If so, we assume the last frame provided was rendered during
68 // those intervals and adjust its render count appropriately.
69 AccountForMissedIntervals(deadline_min, deadline_max);
70 last_deadline_max_ = deadline_max;
71
72 // Step 3: Update the wall clock timestamps and frame duration estimates for
73 // all frames currently in the |frame_queue_|.
74 if (!UpdateFrameStatistics()) {
75 DVLOG(2) << "Failed to update frame statistics.";
76
77 ReadyFrame& ready_frame = frame_queue_[last_frame_index_];
78 DCHECK(ready_frame.frame);
79
80 // If duration is unknown, we don't have enough frames to make a good guess
81 // about which frame to use, so always choose the first.
82 if (average_frame_duration_ == base::TimeDelta() &&
83 !ready_frame.wall_clock_time.is_null()) {
84 ++ready_frame.render_count;
85 }
86
87 return ready_frame.frame;
88 }
89
90 DCHECK_GT(average_frame_duration_, base::TimeDelta());
91
92 base::TimeDelta selected_frame_drift;
93
94 // Step 4: Attempt to find the best frame by cadence.
95 int frame_to_render = FindBestFrameByCadence();
96 if (frame_to_render >= 0) {
97 selected_frame_drift =
98 CalculateAbsoluteDriftForFrame(deadline_min, frame_to_render);
99 }
100
101 // Step 5: If no frame could be found by cadence or the selected frame exceeds
102 // acceptable drift, try to find the best frame by coverage of the deadline.
103 if (frame_to_render < 0 || selected_frame_drift > max_acceptable_drift_) {
104 int second_best_by_coverage = -1;
105 const int best_by_coverage = FindBestFrameByCoverage(
106 deadline_min, deadline_max, &second_best_by_coverage);
107
108 // If the frame was previously selected based on cadence, we're only here
109 // because the drift is too large, so even if the cadence frame has the best
110 // coverage, fallback to the second best by coverage if it has better drift.
111 if (frame_to_render == best_by_coverage && second_best_by_coverage >= 0 &&
112 CalculateAbsoluteDriftForFrame(deadline_min, second_best_by_coverage) <=
113 selected_frame_drift) {
114 frame_to_render = second_best_by_coverage;
115 } else {
116 frame_to_render = best_by_coverage;
117 }
118
119 if (frame_to_render >= 0) {
120 selected_frame_drift =
121 CalculateAbsoluteDriftForFrame(deadline_min, frame_to_render);
122 }
123 }
124
125 // Step 6: If _still_ no frame could be found by coverage, try to choose the
126 // least crappy option based on the drift from the deadline. If we're here the
127 // selection is going to be bad because it means no suitable frame has any
128 // coverage of the deadline interval.
129 if (frame_to_render < 0 || selected_frame_drift > max_acceptable_drift_)
130 frame_to_render = FindBestFrameByDrift(deadline_min, &selected_frame_drift);
131
132 last_render_had_glitch_ = selected_frame_drift > max_acceptable_drift_;
133 DVLOG_IF(2, last_render_had_glitch_)
134 << "Frame drift is too far: " << selected_frame_drift.InMillisecondsF()
135 << "ms";
136
137 DCHECK_GE(frame_to_render, 0);
138
139 // Drop some debugging information if a frame had poor cadence.
140 if (cadence_estimator_.has_cadence()) {
141 const ReadyFrame& last_frame_info = frame_queue_[last_frame_index_];
142 if (static_cast<size_t>(frame_to_render) != last_frame_index_ &&
143 last_frame_info.render_count < last_frame_info.ideal_render_count) {
144 last_render_had_glitch_ = true;
145 DVLOG(2) << "Under-rendered frame " << last_frame_info.frame->timestamp()
146 << "; only " << last_frame_info.render_count
147 << " times instead of " << last_frame_info.ideal_render_count;
148 } else if (static_cast<size_t>(frame_to_render) == last_frame_index_ &&
149 last_frame_info.render_count >=
150 last_frame_info.ideal_render_count) {
151 DVLOG(2) << "Over-rendered frame " << last_frame_info.frame->timestamp()
152 << "; rendered " << last_frame_info.render_count + 1
153 << " times instead of " << last_frame_info.ideal_render_count;
154 last_render_had_glitch_ = true;
155 }
156 }
157
158 // Step 7: Drop frames which occur prior to the frame to be rendered. If any
159 // frame has a zero render count it should be reported as dropped.
160 if (frame_to_render > 0) {
161 if (frames_dropped) {
162 for (int i = 0; i < frame_to_render; ++i) {
163 const ReadyFrame& frame = frame_queue_[i];
164 if (frame.render_count != frame.drop_count)
165 continue;
166
167 // If frame dropping is disabled, ignore the results of the algorithm
168 // and return the earliest unrendered frame.
169 if (frame_dropping_disabled_) {
170 frame_to_render = i;
171 break;
172 }
173
174 DVLOG(2) << "Dropping unrendered (or always dropped) frame "
175 << frame.frame->timestamp()
176 << ", wall clock: " << frame.wall_clock_time.ToInternalValue()
177 << " (" << frame.render_count << ", " << frame.drop_count
178 << ")";
179 ++(*frames_dropped);
180 if (!cadence_estimator_.has_cadence() || frame.ideal_render_count)
181 last_render_had_glitch_ = true;
182 }
183 }
184
185 frame_queue_.erase(frame_queue_.begin(),
186 frame_queue_.begin() + frame_to_render);
187 }
188
189 if (last_render_had_glitch_) {
190 DVLOG(2) << "Deadline: [" << deadline_min.ToInternalValue() << ", "
191 << deadline_max.ToInternalValue()
192 << "], Interval: " << render_interval_.InMicroseconds()
193 << ", Duration: " << average_frame_duration_.InMicroseconds();
194 }
195
196 // Step 8: Congratulations, the frame selection guantlet has been passed!
197 last_frame_index_ = 0;
198 ++frame_queue_.front().render_count;
199 DCHECK(frame_queue_.front().frame);
200 return frame_queue_.front().frame;
201 }
202
203 size_t VideoRendererAlgorithm::RemoveExpiredFrames(
204 base::TimeTicks deadline_min) {
205 if (!UpdateFrameStatistics() || frame_queue_.size() < 2)
206 return 0;
207
208 DCHECK_GT(average_frame_duration_, base::TimeDelta());
209
210 // Finds and removes all frames which are too old to be used; I.e., the end of
211 // their render interval is further than |max_acceptable_drift_| from the
212 // given |deadline_min|.
213 size_t frames_to_expire = 0;
214 const base::TimeTicks mininum_frame_time =
215 deadline_min - max_acceptable_drift_ - average_frame_duration_;
216 for (; frames_to_expire < frame_queue_.size() - 1; ++frames_to_expire) {
217 if (frame_queue_[frames_to_expire].wall_clock_time >= mininum_frame_time)
218 break;
219 }
220
221 if (!frames_to_expire)
222 return 0;
223
224 frame_queue_.erase(frame_queue_.begin(),
225 frame_queue_.begin() + frames_to_expire);
226
227 last_frame_index_ = last_frame_index_ > frames_to_expire
228 ? last_frame_index_ - frames_to_expire
229 : 0;
230 return frames_to_expire;
231 }
232
233 void VideoRendererAlgorithm::OnLastFrameDropped() {
234 DCHECK(have_rendered_frames_);
235 DCHECK(!frame_queue_.empty());
236 // If frames were expired by RemoveExpiredFrames() this count may be zero when
237 // the OnLastFrameDropped() call comes in.
238 if (!frame_queue_[last_frame_index_].render_count)
239 return;
240
241 ++frame_queue_[last_frame_index_].drop_count;
242 DCHECK_LE(frame_queue_[last_frame_index_].drop_count,
243 frame_queue_[last_frame_index_].render_count);
244 }
245
246 void VideoRendererAlgorithm::Reset() {
247 last_frame_index_ = 0;
248 have_rendered_frames_ = last_render_had_glitch_ = false;
249 last_deadline_max_ = base::TimeTicks();
250 average_frame_duration_ = render_interval_ = base::TimeDelta();
251 frame_queue_.clear();
252 cadence_estimator_.Reset();
253 frame_duration_calculator_.Reset();
254
255 // Default to ATSC IS/191 recommendations for maximum acceptable drift before
256 // we have enough frames to base the the maximum on frame duration.
257 max_acceptable_drift_ = base::TimeDelta::FromMilliseconds(15);
258 }
259
260 size_t VideoRendererAlgorithm::EffectiveFramesQueued() const {
261 if (frame_queue_.empty() || !have_rendered_frames_ ||
262 average_frame_duration_ == base::TimeDelta()) {
263 return frame_queue_.size();
264 }
265
266 // If we don't have cadence, subtract off any frames which are before
267 // the last rendered frame or are past their expected rendering time.
268 if (!cadence_estimator_.has_cadence()) {
269 size_t expired_frames = last_frame_index_;
270 DCHECK_LT(last_frame_index_, frame_queue_.size());
271 for (; expired_frames < frame_queue_.size(); ++expired_frames) {
272 if (frame_queue_[expired_frames].wall_clock_time.is_null() ||
273 EndTimeForFrame(expired_frames) > last_deadline_max_) {
274 break;
275 }
276 }
277 return frame_queue_.size() - expired_frames;
278 }
279
280 // Find the first usable frame to start counting from.
281 const int start_index = FindBestFrameByCadenceInternal(nullptr);
282 if (start_index < 0)
283 return 0;
284
285 size_t renderable_frame_count = 0;
286 for (size_t i = start_index; i < frame_queue_.size(); ++i) {
287 if (frame_queue_[i].render_count < frame_queue_[i].ideal_render_count)
288 ++renderable_frame_count;
289 }
290
291 return renderable_frame_count;
292 }
293
294 void VideoRendererAlgorithm::EnqueueFrame(
295 const scoped_refptr<VideoFrame>& frame) {
296 DCHECK(frame);
297 DCHECK(!frame->end_of_stream());
298
299 ReadyFrame ready_frame(frame);
300 auto it = frame_queue_.empty() ? frame_queue_.end()
301 : std::lower_bound(frame_queue_.begin(),
302 frame_queue_.end(), frame);
303 DCHECK_GE(it - frame_queue_.begin(), 0);
304
305 // If a frame was inserted before the first frame, update the index. On the
306 // next call to Render() it will be dropped.
307 if (static_cast<size_t>(it - frame_queue_.begin()) <= last_frame_index_ &&
308 have_rendered_frames_) {
309 ++last_frame_index_;
310 }
311
312 // The vast majority of cases should always append to the back, but in rare
313 // circumstance we get out of order timestamps, http://crbug.com/386551.
314 it = frame_queue_.insert(it, ready_frame);
315
316 // Project the current cadence calculations to include the new frame. These
317 // may not be accurate until the next Render() call. These updates are done
318 // to ensure EffectiveFramesQueued() returns a semi-reliable result.
319 if (cadence_estimator_.has_cadence())
320 UpdateCadenceForFrames();
321
322 #ifndef NDEBUG
323 // Verify sorted order in debug mode.
324 for (size_t i = 0; i < frame_queue_.size() - 1; ++i) {
325 DCHECK(frame_queue_[i].frame->timestamp() <=
326 frame_queue_[i + 1].frame->timestamp());
327 }
328 #endif
329 }
330
331 void VideoRendererAlgorithm::AccountForMissedIntervals(
332 base::TimeTicks deadline_min,
333 base::TimeTicks deadline_max) {
334 if (last_deadline_max_.is_null() || deadline_min <= last_deadline_max_)
335 return;
336
337 const int64 render_cycle_count =
338 (deadline_min - last_deadline_max_) / render_interval_;
339
340 // In the ideal case this value will be zero.
341 if (!render_cycle_count)
342 return;
343
344 DVLOG(2) << "Missed " << render_cycle_count << " Render() intervals.";
345
346 // Only update render count if the frame was rendered at all; it may not have
347 // been if the frame is at the head because we haven't rendered anything yet
348 // or because previous frames were removed via RemoveExpiredFrames().
349 ReadyFrame& ready_frame = frame_queue_[last_frame_index_];
350 if (!ready_frame.render_count)
351 return;
352
353 // If the frame was never really rendered since it was dropped each attempt,
354 // we need to increase the drop count as well to match the new render count.
355 // Otherwise we won't properly count the frame as dropped when it's discarded.
356 // We always update the render count so FindBestFrameByCadenceInternal() can
357 // properly account for potentially over-rendered frames.
358 if (ready_frame.render_count == ready_frame.drop_count)
359 ready_frame.drop_count += render_cycle_count;
360 ready_frame.render_count += render_cycle_count;
361 }
362
363 bool VideoRendererAlgorithm::UpdateFrameStatistics() {
364 // Figure out all current ready frame times at once so we minimize the drift
365 // relative to real time as the code below executes.
366 for (size_t i = 0; i < frame_queue_.size(); ++i) {
367 ReadyFrame& frame = frame_queue_[i];
368 const bool new_frame = frame.wall_clock_time.is_null();
369 frame.wall_clock_time = time_converter_cb_.Run(frame.frame->timestamp());
370
371 // If time stops or never started, exit immediately.
372 if (frame.wall_clock_time.is_null())
373 return false;
374
375 // TODO(dalecurtis): An unlucky tick of a playback rate change could cause
376 // this to skew so much that time goes backwards between calls. Fix this by
377 // either converting all timestamps at once or with some retry logic.
378 if (i > 0) {
379 const base::TimeDelta delta =
380 frame.wall_clock_time - frame_queue_[i - 1].wall_clock_time;
381 CHECK_GT(delta, base::TimeDelta());
382 if (new_frame)
383 frame_duration_calculator_.AddSample(delta);
384 }
385 }
386
387 // Do we have enough frames to compute statistics?
388 const bool have_frame_duration = average_frame_duration_ != base::TimeDelta();
389 if (frame_queue_.size() < 2 && !have_frame_duration)
390 return false;
391
392 // Compute |average_frame_duration_|, a moving average of the last few frames;
393 // see kMovingAverageSamples for the exact number.
394 average_frame_duration_ = frame_duration_calculator_.Average();
395
396 // ITU-R BR.265 recommends a maximum acceptable drift of +/- half of the frame
397 // duration; there are other asymmetric, more lenient measures, that we're
398 // forgoing in favor of simplicity.
399 //
400 // We'll always allow at least 8.33ms of drift since literature suggests it's
401 // well below the floor of detection.
402 max_acceptable_drift_ = std::max(average_frame_duration_ / 2,
403 base::TimeDelta::FromSecondsD(1.0 / 120));
404
405 const bool cadence_changed = cadence_estimator_.UpdateCadenceEstimate(
406 render_interval_, average_frame_duration_, max_acceptable_drift_);
407
408 // No need to update cadence if there's been no change; cadence will be set
409 // as frames are added to the queue.
410 if (!cadence_changed)
411 return true;
412
413 UpdateCadenceForFrames();
414
415 // Thus far there appears to be no need for special 3:2 considerations, the
416 // smoothness scores seem to naturally fit that pattern based on maximizing
417 // frame coverage.
418 return true;
419 }
420
421 void VideoRendererAlgorithm::UpdateCadenceForFrames() {
422 for (size_t i = last_frame_index_; i < frame_queue_.size(); ++i) {
423 // It's always okay to adjust the ideal render count, since the cadence
424 // selection method will still count its current render count towards
425 // cadence selection.
426 frame_queue_[i].ideal_render_count =
427 cadence_estimator_.has_cadence()
428 ? cadence_estimator_.GetCadenceForFrame(i - last_frame_index_)
429 : 0;
430 }
431 }
432
433 int VideoRendererAlgorithm::FindBestFrameByCadence() {
434 DCHECK(!frame_queue_.empty());
435 if (!cadence_estimator_.has_cadence())
436 return -1;
437
438 int new_ideal_render_count = 0;
439 const int best_frame =
440 FindBestFrameByCadenceInternal(&new_ideal_render_count);
441 if (best_frame < 0)
442 return -1;
443
444 DCHECK_GT(new_ideal_render_count, 0);
445 frame_queue_[best_frame].ideal_render_count = new_ideal_render_count;
446 return best_frame;
447 }
448
449 int VideoRendererAlgorithm::FindBestFrameByCadenceInternal(
450 int* adjusted_ideal_render_count) const {
451 DCHECK(!frame_queue_.empty());
452 DCHECK(cadence_estimator_.has_cadence());
453 const ReadyFrame& current_frame = frame_queue_[last_frame_index_];
454
455 // If the current frame is below cadence, we should prefer it.
456 if (current_frame.render_count < current_frame.ideal_render_count) {
457 if (adjusted_ideal_render_count)
458 *adjusted_ideal_render_count = current_frame.ideal_render_count;
459 return last_frame_index_;
460 }
461
462 // For over-rendered frames we need to ensure we skip frames and subtract
463 // each skipped frame's ideal cadence from the over-render count until we
464 // find a frame which still has a positive ideal render count.
465 int render_count_overage = std::max(
466 0, current_frame.render_count - current_frame.ideal_render_count);
467
468 // If the current frame is on cadence or over cadence, find the next frame
469 // with a positive ideal render count.
470 for (size_t i = last_frame_index_ + 1; i < frame_queue_.size(); ++i) {
471 const ReadyFrame& frame = frame_queue_[i];
472 if (frame.ideal_render_count > render_count_overage) {
473 if (adjusted_ideal_render_count) {
474 *adjusted_ideal_render_count =
475 frame.ideal_render_count - render_count_overage;
476 }
477 return i;
478 } else {
479 // The ideal render count should always be zero or smaller than the
480 // over-render count.
481 render_count_overage -= frame.ideal_render_count;
482 DCHECK_GE(render_count_overage, 0);
483 }
484 }
485
486 // We don't have enough frames to find a better once by cadence.
487 return -1;
488 }
489
490 int VideoRendererAlgorithm::FindBestFrameByCoverage(
491 base::TimeTicks deadline_min,
492 base::TimeTicks deadline_max,
493 int* second_best) const {
494 DCHECK(!frame_queue_.empty());
495
496 // Find the frame which covers the most of the interval [deadline_min,
497 // deadline_max]. Frames outside of the interval are considered to have no
498 // coverage, while those which completely overlap the interval have complete
499 // coverage.
500 int best_frame_by_coverage = -1;
501 base::TimeDelta best_coverage;
502 std::vector<base::TimeDelta> coverage(frame_queue_.size(), base::TimeDelta());
503 for (size_t i = last_frame_index_; i < frame_queue_.size(); ++i) {
504 // Frames which start after the deadline interval have zero coverage.
505 if (frame_queue_[i].wall_clock_time > deadline_max)
506 break;
507
508 // Clamp frame end times to a maximum of |deadline_max|.
509 const base::TimeTicks frame_end_time =
510 std::min(deadline_max, EndTimeForFrame(i));
511
512 // Frames entirely before the deadline interval have zero coverage.
513 if (frame_end_time < deadline_min)
514 continue;
515
516 // If we're here, the current frame overlaps the deadline in some way; so
517 // compute the duration of the interval which is covered.
518 const base::TimeDelta duration =
519 frame_end_time -
520 std::max(deadline_min, frame_queue_[i].wall_clock_time);
521
522 coverage[i] = duration;
523 if (coverage[i] > best_coverage) {
524 best_frame_by_coverage = i;
525 best_coverage = coverage[i];
526 }
527 }
528
529 // Find the second best frame by coverage; done by zeroing the coverage for
530 // the previous best and recomputing the maximum.
531 *second_best = -1;
532 if (best_frame_by_coverage >= 0) {
533 coverage[best_frame_by_coverage] = base::TimeDelta();
534 auto it = std::max_element(coverage.begin(), coverage.end());
535 if (*it > base::TimeDelta())
536 *second_best = it - coverage.begin();
537 }
538
539 // If two frames have coverage within half a millisecond, prefer the earliest
540 // frame as having the best coverage. Value chosen via experimentation to
541 // ensure proper coverage calculation for 24fps in 60Hz where +/- 100us of
542 // jitter is present within the |render_interval_|. At 60Hz this works out to
543 // an allowed jitter of 3%.
544 const base::TimeDelta kAllowableJitter =
545 base::TimeDelta::FromMicroseconds(500);
546 if (*second_best >= 0 && best_frame_by_coverage > *second_best &&
547 (best_coverage - coverage[*second_best]).magnitude() <=
548 kAllowableJitter) {
549 std::swap(best_frame_by_coverage, *second_best);
550 }
551
552 // TODO(dalecurtis): We may want to make a better decision about what to do
553 // when multiple frames have equivalent coverage over an interval. Jitter in
554 // the render interval may result in irregular frame selection which may be
555 // visible to a viewer.
556 //
557 // 23.974fps and 24fps in 60Hz are the most common susceptible rates, so
558 // extensive tests have been added to ensure these cases work properly.
559
560 return best_frame_by_coverage;
561 }
562
563 int VideoRendererAlgorithm::FindBestFrameByDrift(
564 base::TimeTicks deadline_min,
565 base::TimeDelta* selected_frame_drift) const {
566 DCHECK(!frame_queue_.empty());
567
568 int best_frame_by_drift = -1;
569 *selected_frame_drift = base::TimeDelta::Max();
570
571 for (size_t i = last_frame_index_; i < frame_queue_.size(); ++i) {
572 const base::TimeDelta drift =
573 CalculateAbsoluteDriftForFrame(deadline_min, i);
574 // We use <= here to prefer the latest frame with minimum drift.
575 if (drift <= *selected_frame_drift) {
576 *selected_frame_drift = drift;
577 best_frame_by_drift = i;
578 }
579 }
580
581 return best_frame_by_drift;
582 }
583
584 base::TimeDelta VideoRendererAlgorithm::CalculateAbsoluteDriftForFrame(
585 base::TimeTicks deadline_min,
586 int frame_index) const {
587 // If the frame lies before the deadline, compute the delta against the end
588 // of the frame's duration.
589 const base::TimeTicks frame_end_time = EndTimeForFrame(frame_index);
590 if (frame_end_time < deadline_min)
591 return deadline_min - frame_end_time;
592
593 // If the frame lies after the deadline, compute the delta against the frame's
594 // wall clock time.
595 const ReadyFrame& frame = frame_queue_[frame_index];
596 if (frame.wall_clock_time > deadline_min)
597 return frame.wall_clock_time - deadline_min;
598
599 // Drift is zero for frames which overlap the deadline interval.
600 DCHECK_GE(deadline_min, frame.wall_clock_time);
601 DCHECK_GE(frame_end_time, deadline_min);
602 return base::TimeDelta();
603 }
604
605 base::TimeTicks VideoRendererAlgorithm::EndTimeForFrame(
606 size_t frame_index) const {
607 DCHECK_LT(frame_index, frame_queue_.size());
608 DCHECK_GT(average_frame_duration_, base::TimeDelta());
609 return frame_index + 1 < frame_queue_.size()
610 ? frame_queue_[frame_index + 1].wall_clock_time
611 : frame_queue_[frame_index].wall_clock_time +
612 average_frame_duration_;
613 }
614
615 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698