summaryrefslogtreecommitdiffstats
path: root/media/base/clock_impl.cc
blob: b8537423e11440138230593940f6705c0253a74c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/logging.h"
#include "media/base/buffers.h"
#include "media/base/clock_impl.h"

namespace media {

ClockImpl::ClockImpl(TimeProvider* time_provider)
    : time_provider_(time_provider),
      playing_(false),
      playback_rate_(1.0f) {
}

ClockImpl::~ClockImpl() {
}

base::TimeDelta ClockImpl::Play() {
  DCHECK(!playing_);
  reference_ = time_provider_();
  playing_ = true;
  return media_time_;
}

base::TimeDelta ClockImpl::Pause() {
  DCHECK(playing_);
  // Save our new accumulated amount of media time.
  media_time_ = Elapsed();
  playing_ = false;
  return media_time_;
}

void ClockImpl::SetTime(const base::TimeDelta& time) {
  if (time == StreamSample::kInvalidTimestamp) {
    NOTREACHED();
    return;
  }
  if (playing_) {
    reference_ = time_provider_();
  }
  media_time_ = time;
}

void ClockImpl::SetPlaybackRate(float playback_rate) {
  if (playing_) {
    base::Time time = time_provider_();
    media_time_ = ElapsedViaProvidedTime(time);
    reference_ = time;
  }
  playback_rate_ = playback_rate;
}

base::TimeDelta ClockImpl::Elapsed() const {
  if (!playing_) {
    return media_time_;
  }
  return ElapsedViaProvidedTime(time_provider_());
}

base::TimeDelta ClockImpl::ElapsedViaProvidedTime(
    const base::Time& time) const {
  // TODO(scherkus): floating point badness scaling time by playback rate.
  int64 now_us = (time - reference_).InMicroseconds();
  now_us = static_cast<int64>(now_us * playback_rate_);
  return media_time_ + base::TimeDelta::FromMicroseconds(now_us);
}

}  // namespace media