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

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: Comments. Created 5 years, 7 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 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 gauntlet 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(base::TimeTicks deadline) {
204 if (!UpdateFrameStatistics() || frame_queue_.size() < 2)
205 return 0;
206
207 DCHECK_GT(average_frame_duration_, base::TimeDelta());
208
209 // Update |last_deadline_max_| if it's no longer accurate.
210 if (deadline > last_deadline_max_)
211 last_deadline_max_ = deadline;
212
213 // Finds and removes all frames which are too old to be used; I.e., the end of
214 // their render interval is further than |max_acceptable_drift_| from the
215 // given |deadline|.
216 size_t frames_to_expire = 0;
217 const base::TimeTicks minimum_frame_time =
218 deadline - max_acceptable_drift_ - average_frame_duration_;
219 for (; frames_to_expire < frame_queue_.size() - 1; ++frames_to_expire) {
220 if (frame_queue_[frames_to_expire].wall_clock_time >= minimum_frame_time)
221 break;
222 }
223
224 if (!frames_to_expire)
225 return 0;
226
227 frame_queue_.erase(frame_queue_.begin(),
228 frame_queue_.begin() + frames_to_expire);
229
230 last_frame_index_ = last_frame_index_ > frames_to_expire
231 ? last_frame_index_ - frames_to_expire
232 : 0;
233 return frames_to_expire;
234 }
235
236 void VideoRendererAlgorithm::OnLastFrameDropped() {
237 DCHECK(have_rendered_frames_);
238 DCHECK(!frame_queue_.empty());
239 // If frames were expired by RemoveExpiredFrames() this count may be zero when
240 // the OnLastFrameDropped() call comes in.
241 if (!frame_queue_[last_frame_index_].render_count)
242 return;
243
244 ++frame_queue_[last_frame_index_].drop_count;
245 DCHECK_LE(frame_queue_[last_frame_index_].drop_count,
246 frame_queue_[last_frame_index_].render_count);
247 }
248
249 void VideoRendererAlgorithm::Reset() {
250 last_frame_index_ = 0;
251 have_rendered_frames_ = last_render_had_glitch_ = false;
252 last_deadline_max_ = base::TimeTicks();
253 average_frame_duration_ = render_interval_ = base::TimeDelta();
254 frame_queue_.clear();
255 cadence_estimator_.Reset();
256 frame_duration_calculator_.Reset();
257
258 // Default to ATSC IS/191 recommendations for maximum acceptable drift before
259 // we have enough frames to base the maximum on frame duration.
260 max_acceptable_drift_ = base::TimeDelta::FromMilliseconds(15);
261 }
262
263 size_t VideoRendererAlgorithm::EffectiveFramesQueued() const {
264 if (frame_queue_.empty() || average_frame_duration_ == base::TimeDelta() ||
265 last_deadline_max_.is_null()) {
266 return frame_queue_.size();
267 }
268
269 // If we don't have cadence, subtract off any frames which are before
270 // the last rendered frame or are past their expected rendering time.
271 if (!cadence_estimator_.has_cadence()) {
272 size_t expired_frames = last_frame_index_;
273 DCHECK_LT(last_frame_index_, frame_queue_.size());
274 for (; expired_frames < frame_queue_.size(); ++expired_frames) {
275 if (frame_queue_[expired_frames].wall_clock_time.is_null() ||
276 EndTimeForFrame(expired_frames) > last_deadline_max_) {
277 break;
278 }
279 }
280 return frame_queue_.size() - expired_frames;
281 }
282
283 // Find the first usable frame to start counting from.
284 const int start_index = FindBestFrameByCadenceInternal(nullptr);
285 if (start_index < 0)
286 return 0;
287
288 size_t renderable_frame_count = 0;
289 for (size_t i = start_index; i < frame_queue_.size(); ++i) {
290 if (frame_queue_[i].render_count < frame_queue_[i].ideal_render_count)
291 ++renderable_frame_count;
292 }
293
294 return renderable_frame_count;
295 }
296
297 void VideoRendererAlgorithm::EnqueueFrame(
298 const scoped_refptr<VideoFrame>& frame) {
299 DCHECK(frame);
300 DCHECK(!frame->end_of_stream());
301
302 ReadyFrame ready_frame(frame);
303 auto it = frame_queue_.empty() ? frame_queue_.end()
304 : std::lower_bound(frame_queue_.begin(),
305 frame_queue_.end(), frame);
306 DCHECK_GE(it - frame_queue_.begin(), 0);
307
308 // If a frame was inserted before the first frame, update the index. On the
309 // next call to Render() it will be dropped.
310 if (static_cast<size_t>(it - frame_queue_.begin()) <= last_frame_index_ &&
311 have_rendered_frames_) {
312 ++last_frame_index_;
313 }
314
315 // The vast majority of cases should always append to the back, but in rare
316 // circumstance we get out of order timestamps, http://crbug.com/386551.
317 it = frame_queue_.insert(it, ready_frame);
318
319 // Project the current cadence calculations to include the new frame. These
320 // may not be accurate until the next Render() call. These updates are done
321 // to ensure EffectiveFramesQueued() returns a semi-reliable result.
322 if (cadence_estimator_.has_cadence())
323 UpdateCadenceForFrames();
324
325 #ifndef NDEBUG
326 // Verify sorted order in debug mode.
327 for (size_t i = 0; i < frame_queue_.size() - 1; ++i) {
328 DCHECK(frame_queue_[i].frame->timestamp() <=
329 frame_queue_[i + 1].frame->timestamp());
330 }
331 #endif
332 }
333
334 void VideoRendererAlgorithm::AccountForMissedIntervals(
335 base::TimeTicks deadline_min,
336 base::TimeTicks deadline_max) {
337 if (last_deadline_max_.is_null() || deadline_min <= last_deadline_max_ ||
338 !have_rendered_frames_) {
339 return;
340 }
341
342 DCHECK_GT(render_interval_, base::TimeDelta());
343 const int64 render_cycle_count =
344 (deadline_min - last_deadline_max_) / render_interval_;
345
346 // In the ideal case this value will be zero.
347 if (!render_cycle_count)
348 return;
349
350 DVLOG(2) << "Missed " << render_cycle_count << " Render() intervals.";
351
352 // Only update render count if the frame was rendered at all; it may not have
353 // been if the frame is at the head because we haven't rendered anything yet
354 // or because previous frames were removed via RemoveExpiredFrames().
355 ReadyFrame& ready_frame = frame_queue_[last_frame_index_];
356 if (!ready_frame.render_count)
357 return;
358
359 // If the frame was never really rendered since it was dropped each attempt,
360 // we need to increase the drop count as well to match the new render count.
361 // Otherwise we won't properly count the frame as dropped when it's discarded.
362 // We always update the render count so FindBestFrameByCadenceInternal() can
363 // properly account for potentially over-rendered frames.
364 if (ready_frame.render_count == ready_frame.drop_count)
365 ready_frame.drop_count += render_cycle_count;
366 ready_frame.render_count += render_cycle_count;
367 }
368
369 bool VideoRendererAlgorithm::UpdateFrameStatistics() {
370 // Figure out all current ready frame times at once so we minimize the drift
371 // relative to real time as the code below executes.
372 for (size_t i = 0; i < frame_queue_.size(); ++i) {
373 ReadyFrame& frame = frame_queue_[i];
374 const bool new_frame = frame.wall_clock_time.is_null();
375 frame.wall_clock_time = time_converter_cb_.Run(frame.frame->timestamp());
376
377 // If time stops or never started, exit immediately.
378 if (frame.wall_clock_time.is_null())
379 return false;
380
381 // TODO(dalecurtis): An unlucky tick of a playback rate change could cause
382 // this to skew so much that time goes backwards between calls. Fix this by
383 // either converting all timestamps at once or with some retry logic.
384 if (i > 0) {
385 const base::TimeDelta delta =
386 frame.wall_clock_time - frame_queue_[i - 1].wall_clock_time;
387 CHECK_GT(delta, base::TimeDelta());
388 if (new_frame)
389 frame_duration_calculator_.AddSample(delta);
390 }
391 }
392
393 // Do we have enough frames to compute statistics?
394 const bool have_frame_duration = average_frame_duration_ != base::TimeDelta();
395 if (frame_queue_.size() < 2 && !have_frame_duration)
396 return false;
397
398 // Compute |average_frame_duration_|, a moving average of the last few frames;
399 // see kMovingAverageSamples for the exact number.
400 average_frame_duration_ = frame_duration_calculator_.Average();
401
402 // ITU-R BR.265 recommends a maximum acceptable drift of +/- half of the frame
403 // duration; there are other asymmetric, more lenient measures, that we're
404 // forgoing in favor of simplicity.
405 //
406 // We'll always allow at least 8.33ms of drift since literature suggests it's
407 // well below the floor of detection.
408 max_acceptable_drift_ = std::max(average_frame_duration_ / 2,
409 base::TimeDelta::FromSecondsD(1.0 / 120));
410
411 // If we were called via RemoveExpiredFrames() and Render() was never called,
412 // we may not have a render interval yet.
413 if (render_interval_ == base::TimeDelta())
414 return true;
415
416 const bool cadence_changed = cadence_estimator_.UpdateCadenceEstimate(
417 render_interval_, average_frame_duration_, max_acceptable_drift_);
418
419 // No need to update cadence if there's been no change; cadence will be set
420 // as frames are added to the queue.
421 if (!cadence_changed)
422 return true;
423
424 UpdateCadenceForFrames();
425
426 // Thus far there appears to be no need for special 3:2 considerations, the
427 // smoothness scores seem to naturally fit that pattern based on maximizing
428 // frame coverage.
429 return true;
430 }
431
432 void VideoRendererAlgorithm::UpdateCadenceForFrames() {
433 for (size_t i = last_frame_index_; i < frame_queue_.size(); ++i) {
434 // It's always okay to adjust the ideal render count, since the cadence
435 // selection method will still count its current render count towards
436 // cadence selection.
437 frame_queue_[i].ideal_render_count =
438 cadence_estimator_.has_cadence()
439 ? cadence_estimator_.GetCadenceForFrame(i - last_frame_index_)
440 : 0;
441 }
442 }
443
444 int VideoRendererAlgorithm::FindBestFrameByCadence() {
445 DCHECK(!frame_queue_.empty());
446 if (!cadence_estimator_.has_cadence())
447 return -1;
448
449 int new_ideal_render_count = 0;
450 const int best_frame =
451 FindBestFrameByCadenceInternal(&new_ideal_render_count);
452 if (best_frame < 0)
453 return -1;
454
455 DCHECK_GT(new_ideal_render_count, 0);
456 frame_queue_[best_frame].ideal_render_count = new_ideal_render_count;
457 return best_frame;
458 }
459
460 int VideoRendererAlgorithm::FindBestFrameByCadenceInternal(
461 int* adjusted_ideal_render_count) const {
462 DCHECK(!frame_queue_.empty());
463 DCHECK(cadence_estimator_.has_cadence());
464 const ReadyFrame& current_frame = frame_queue_[last_frame_index_];
465
466 // If the current frame is below cadence, we should prefer it.
467 if (current_frame.render_count < current_frame.ideal_render_count) {
468 if (adjusted_ideal_render_count)
469 *adjusted_ideal_render_count = current_frame.ideal_render_count;
470 return last_frame_index_;
471 }
472
473 // For over-rendered frames we need to ensure we skip frames and subtract
474 // each skipped frame's ideal cadence from the over-render count until we
475 // find a frame which still has a positive ideal render count.
476 int render_count_overage = std::max(
477 0, current_frame.render_count - current_frame.ideal_render_count);
478
479 // If the current frame is on cadence or over cadence, find the next frame
480 // with a positive ideal render count.
481 for (size_t i = last_frame_index_ + 1; i < frame_queue_.size(); ++i) {
482 const ReadyFrame& frame = frame_queue_[i];
483 if (frame.ideal_render_count > render_count_overage) {
484 if (adjusted_ideal_render_count) {
485 *adjusted_ideal_render_count =
486 frame.ideal_render_count - render_count_overage;
487 }
488 return i;
489 } else {
490 // The ideal render count should always be zero or smaller than the
491 // over-render count.
492 render_count_overage -= frame.ideal_render_count;
493 DCHECK_GE(render_count_overage, 0);
494 }
495 }
496
497 // We don't have enough frames to find a better once by cadence.
498 return -1;
499 }
500
501 int VideoRendererAlgorithm::FindBestFrameByCoverage(
502 base::TimeTicks deadline_min,
503 base::TimeTicks deadline_max,
504 int* second_best) const {
505 DCHECK(!frame_queue_.empty());
506
507 // Find the frame which covers the most of the interval [deadline_min,
508 // deadline_max]. Frames outside of the interval are considered to have no
509 // coverage, while those which completely overlap the interval have complete
510 // coverage.
511 int best_frame_by_coverage = -1;
512 base::TimeDelta best_coverage;
513 std::vector<base::TimeDelta> coverage(frame_queue_.size(), base::TimeDelta());
514 for (size_t i = last_frame_index_; i < frame_queue_.size(); ++i) {
515 // Frames which start after the deadline interval have zero coverage.
516 if (frame_queue_[i].wall_clock_time > deadline_max)
517 break;
518
519 // Clamp frame end times to a maximum of |deadline_max|.
520 const base::TimeTicks frame_end_time =
521 std::min(deadline_max, EndTimeForFrame(i));
522
523 // Frames entirely before the deadline interval have zero coverage.
524 if (frame_end_time < deadline_min)
525 continue;
526
527 // If we're here, the current frame overlaps the deadline in some way; so
528 // compute the duration of the interval which is covered.
529 const base::TimeDelta duration =
530 frame_end_time -
531 std::max(deadline_min, frame_queue_[i].wall_clock_time);
532
533 coverage[i] = duration;
534 if (coverage[i] > best_coverage) {
535 best_frame_by_coverage = i;
536 best_coverage = coverage[i];
537 }
538 }
539
540 // Find the second best frame by coverage; done by zeroing the coverage for
541 // the previous best and recomputing the maximum.
542 *second_best = -1;
543 if (best_frame_by_coverage >= 0) {
544 coverage[best_frame_by_coverage] = base::TimeDelta();
545 auto it = std::max_element(coverage.begin(), coverage.end());
546 if (*it > base::TimeDelta())
547 *second_best = it - coverage.begin();
548 }
549
550 // If two frames have coverage within half a millisecond, prefer the earliest
551 // frame as having the best coverage. Value chosen via experimentation to
552 // ensure proper coverage calculation for 24fps in 60Hz where +/- 100us of
553 // jitter is present within the |render_interval_|. At 60Hz this works out to
554 // an allowed jitter of 3%.
555 const base::TimeDelta kAllowableJitter =
556 base::TimeDelta::FromMicroseconds(500);
557 if (*second_best >= 0 && best_frame_by_coverage > *second_best &&
558 (best_coverage - coverage[*second_best]).magnitude() <=
559 kAllowableJitter) {
560 std::swap(best_frame_by_coverage, *second_best);
561 }
562
563 // TODO(dalecurtis): We may want to make a better decision about what to do
564 // when multiple frames have equivalent coverage over an interval. Jitter in
565 // the render interval may result in irregular frame selection which may be
566 // visible to a viewer.
567 //
568 // 23.974fps and 24fps in 60Hz are the most common susceptible rates, so
569 // extensive tests have been added to ensure these cases work properly.
570
571 return best_frame_by_coverage;
572 }
573
574 int VideoRendererAlgorithm::FindBestFrameByDrift(
575 base::TimeTicks deadline_min,
576 base::TimeDelta* selected_frame_drift) const {
577 DCHECK(!frame_queue_.empty());
578
579 int best_frame_by_drift = -1;
580 *selected_frame_drift = base::TimeDelta::Max();
581
582 for (size_t i = last_frame_index_; i < frame_queue_.size(); ++i) {
583 const base::TimeDelta drift =
584 CalculateAbsoluteDriftForFrame(deadline_min, i);
585 // We use <= here to prefer the latest frame with minimum drift.
586 if (drift <= *selected_frame_drift) {
587 *selected_frame_drift = drift;
588 best_frame_by_drift = i;
589 }
590 }
591
592 return best_frame_by_drift;
593 }
594
595 base::TimeDelta VideoRendererAlgorithm::CalculateAbsoluteDriftForFrame(
596 base::TimeTicks deadline_min,
597 int frame_index) const {
598 // If the frame lies before the deadline, compute the delta against the end
599 // of the frame's duration.
600 const base::TimeTicks frame_end_time = EndTimeForFrame(frame_index);
601 if (frame_end_time < deadline_min)
602 return deadline_min - frame_end_time;
603
604 // If the frame lies after the deadline, compute the delta against the frame's
605 // wall clock time.
606 const ReadyFrame& frame = frame_queue_[frame_index];
607 if (frame.wall_clock_time > deadline_min)
608 return frame.wall_clock_time - deadline_min;
609
610 // Drift is zero for frames which overlap the deadline interval.
611 DCHECK_GE(deadline_min, frame.wall_clock_time);
612 DCHECK_GE(frame_end_time, deadline_min);
613 return base::TimeDelta();
614 }
615
616 base::TimeTicks VideoRendererAlgorithm::EndTimeForFrame(
617 size_t frame_index) const {
618 DCHECK_LT(frame_index, frame_queue_.size());
619 DCHECK_GT(average_frame_duration_, base::TimeDelta());
620 return frame_index + 1 < frame_queue_.size()
621 ? frame_queue_[frame_index + 1].wall_clock_time
622 : frame_queue_[frame_index].wall_clock_time +
623 average_frame_duration_;
624 }
625
626 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698