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

Side by Side Diff: media/base/clock_impl.cc

Issue 159517: Implemented a proper clock for audio/video synchronization. (Closed)
Patch Set: Now with convenience! Created 11 years, 4 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
« no previous file with comments | « media/base/clock_impl.h ('k') | media/base/clock_impl_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2009 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 "base/logging.h"
6 #include "media/base/clock_impl.h"
7
8 namespace media {
9
10 ClockImpl::ClockImpl(TimeProvider* time_provider)
11 : time_provider_(time_provider),
12 playing_(false),
13 playback_rate_(1.0f) {
14 }
15
16 ClockImpl::~ClockImpl() {
17 }
18
19 base::TimeDelta ClockImpl::Play() {
20 DCHECK(!playing_);
21 reference_ = time_provider_();
22 playing_ = true;
23 return media_time_;
24 }
25
26 base::TimeDelta ClockImpl::Pause() {
27 DCHECK(playing_);
28 // Save our new accumulated amount of media time.
29 media_time_ = Elapsed();
30 playing_ = false;
31 return media_time_;
32 }
33
34 void ClockImpl::SetTime(const base::TimeDelta& time) {
35 if (playing_) {
36 reference_ = time_provider_();
37 }
38 media_time_ = time;
39 }
40
41 void ClockImpl::SetPlaybackRate(float playback_rate) {
42 if (playing_) {
43 base::Time time = time_provider_();
44 media_time_ = ElapsedViaProvidedTime(time);
45 reference_ = time;
46 }
47 playback_rate_ = playback_rate;
48 }
49
50 base::TimeDelta ClockImpl::Elapsed() const {
51 if (!playing_) {
52 return media_time_;
53 }
54 return ElapsedViaProvidedTime(time_provider_());
55 }
56
57 base::TimeDelta ClockImpl::ElapsedViaProvidedTime(
58 const base::Time& time) const {
59 // TODO(scherkus): floating point badness scaling time by playback rate.
60 int64 now_us = (time - reference_).InMicroseconds();
61 now_us = static_cast<int64>(now_us * playback_rate_);
62 return media_time_ + base::TimeDelta::FromMicroseconds(now_us);
63 }
64
65 } // namespace media
OLDNEW
« no previous file with comments | « media/base/clock_impl.h ('k') | media/base/clock_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698