OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/base/time_delta_interpolator.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/time/tick_clock.h" |
| 11 #include "media/base/buffers.h" |
| 12 |
| 13 namespace media { |
| 14 |
| 15 TimeDeltaInterpolator::TimeDeltaInterpolator(base::TickClock* tick_clock) |
| 16 : tick_clock_(tick_clock), |
| 17 interpolating_(false), |
| 18 upper_bound_(kNoTimestamp()), |
| 19 playback_rate_(1.0f) { |
| 20 DCHECK(tick_clock_); |
| 21 } |
| 22 |
| 23 TimeDeltaInterpolator::~TimeDeltaInterpolator() {} |
| 24 |
| 25 base::TimeDelta TimeDeltaInterpolator::StartInterpolating() { |
| 26 DCHECK(!interpolating_); |
| 27 reference_ = tick_clock_->NowTicks(); |
| 28 interpolating_ = true; |
| 29 return lower_bound_; |
| 30 } |
| 31 |
| 32 base::TimeDelta TimeDeltaInterpolator::StopInterpolating() { |
| 33 DCHECK(interpolating_); |
| 34 lower_bound_ = GetInterpolatedTime(); |
| 35 interpolating_ = false; |
| 36 return lower_bound_; |
| 37 } |
| 38 |
| 39 void TimeDeltaInterpolator::SetPlaybackRate(float playback_rate) { |
| 40 lower_bound_ = GetInterpolatedTime(); |
| 41 reference_ = tick_clock_->NowTicks(); |
| 42 playback_rate_ = playback_rate; |
| 43 } |
| 44 |
| 45 void TimeDeltaInterpolator::SetInterpolationRange(base::TimeDelta lower_bound, |
| 46 base::TimeDelta upper_bound) { |
| 47 DCHECK(lower_bound <= upper_bound); |
| 48 DCHECK(lower_bound != kNoTimestamp()); |
| 49 |
| 50 lower_bound_ = std::max(base::TimeDelta(), lower_bound); |
| 51 upper_bound_ = std::max(base::TimeDelta(), upper_bound); |
| 52 reference_ = tick_clock_->NowTicks(); |
| 53 } |
| 54 |
| 55 void TimeDeltaInterpolator::SetUpperBound(base::TimeDelta upper_bound) { |
| 56 DCHECK(upper_bound != kNoTimestamp()); |
| 57 |
| 58 lower_bound_ = GetInterpolatedTime(); |
| 59 reference_ = tick_clock_->NowTicks(); |
| 60 upper_bound_ = upper_bound; |
| 61 } |
| 62 |
| 63 base::TimeDelta TimeDeltaInterpolator::GetInterpolatedTime() { |
| 64 if (!interpolating_) |
| 65 return lower_bound_; |
| 66 |
| 67 int64 now_us = (tick_clock_->NowTicks() - reference_).InMicroseconds(); |
| 68 now_us = static_cast<int64>(now_us * playback_rate_); |
| 69 base::TimeDelta interpolated_time = |
| 70 lower_bound_ + base::TimeDelta::FromMicroseconds(now_us); |
| 71 |
| 72 if (upper_bound_ == kNoTimestamp()) |
| 73 return interpolated_time; |
| 74 |
| 75 return std::min(interpolated_time, upper_bound_); |
| 76 } |
| 77 |
| 78 } // namespace media |
OLD | NEW |