OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BASE_CLOCK_H_ | 5 #ifndef MEDIA_BASE_CLOCK_H_ |
6 #define MEDIA_BASE_CLOCK_H_ | 6 #define MEDIA_BASE_CLOCK_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/time.h" | 9 #include "base/time.h" |
10 #include "media/base/media_export.h" | 10 #include "media/base/media_export.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 class Clock; | 13 class TickClock; |
14 } // namespace base | 14 } // namespace base |
15 | 15 |
16 namespace media { | 16 namespace media { |
17 | 17 |
18 // A clock represents a single source of time to allow audio and video streams | 18 // A clock represents a single source of time to allow audio and video streams |
19 // to synchronize with each other. Clock essentially tracks the media time with | 19 // to synchronize with each other. Clock essentially tracks the media time with |
20 // respect to some other source of time, whether that may be the system clock or | 20 // respect to some other source of time, whether that may be the monotonic |
21 // updates via SetTime(). Clock uses linear interpolation to calculate the | 21 // system clock or updates via SetTime(). Clock uses linear interpolation to |
22 // current media time since the last time SetTime() was called. | 22 // calculate the current media time since the last time SetTime() was called. |
23 // | 23 // |
24 // Clocks start off paused with a playback rate of 1.0f and a media time of 0. | 24 // Clocks start off paused with a playback rate of 1.0f and a media time of 0. |
25 // | 25 // |
26 // Clock is not thread-safe and must be externally locked. | 26 // Clock is not thread-safe and must be externally locked. |
27 // | 27 // |
28 // TODO(scherkus): Clock will some day be responsible for executing callbacks | 28 // TODO(scherkus): Clock will some day be responsible for executing callbacks |
29 // given a media time. This will be used primarily by video renderers. For now | 29 // given a media time. This will be used primarily by video renderers. For now |
30 // we'll keep using a poll-and-sleep solution. | 30 // we'll keep using a poll-and-sleep solution. |
| 31 // |
| 32 // TODO(miu): Rename media::Clock to avoid confusion (and tripping up the media |
| 33 // PRESUBMIT script on future changes). |
31 class MEDIA_EXPORT Clock { | 34 class MEDIA_EXPORT Clock { |
32 public: | 35 public: |
33 explicit Clock(base::Clock* clock); | 36 explicit Clock(base::TickClock* clock); |
34 ~Clock(); | 37 ~Clock(); |
35 | 38 |
36 // Returns true if the clock is running. | 39 // Returns true if the clock is running. |
37 bool IsPlaying() const; | 40 bool IsPlaying() const; |
38 | 41 |
39 // Starts the clock and returns the current media time, which will increase | 42 // Starts the clock and returns the current media time, which will increase |
40 // with respect to the current playback rate. | 43 // with respect to the current playback rate. |
41 base::TimeDelta Play(); | 44 base::TimeDelta Play(); |
42 | 45 |
43 // Stops the clock and returns the current media time, which will remain | 46 // Stops the clock and returns the current media time, which will remain |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 // Updates the reference points based on the current calculated time. | 84 // Updates the reference points based on the current calculated time. |
82 void UpdateReferencePoints(); | 85 void UpdateReferencePoints(); |
83 | 86 |
84 // Updates the reference points based on the given |current_time|. | 87 // Updates the reference points based on the given |current_time|. |
85 void UpdateReferencePoints(base::TimeDelta current_time); | 88 void UpdateReferencePoints(base::TimeDelta current_time); |
86 | 89 |
87 // Returns the time elapsed based on the current reference points, ignoring | 90 // Returns the time elapsed based on the current reference points, ignoring |
88 // the |max_time_| cap. | 91 // the |max_time_| cap. |
89 base::TimeDelta EstimatedElapsedTime(); | 92 base::TimeDelta EstimatedElapsedTime(); |
90 | 93 |
91 // Returns the current media time treating the given time as the latest | 94 // Translates |time| into the current media time, based on the perspective of |
92 // value as returned by |time_provider_|. | 95 // the monotonically-increasing system clock. |
93 base::TimeDelta ElapsedViaProvidedTime(const base::Time& time) const; | 96 base::TimeDelta ElapsedViaProvidedTime(const base::TimeTicks& time) const; |
94 | 97 |
95 base::TimeDelta ClampToValidTimeRange(base::TimeDelta time) const; | 98 base::TimeDelta ClampToValidTimeRange(base::TimeDelta time) const; |
96 | 99 |
97 base::Clock* const clock_; | 100 base::TickClock* const clock_; |
98 | 101 |
99 // Whether the clock is running. | 102 // Whether the clock is running. |
100 bool playing_; | 103 bool playing_; |
101 | 104 |
102 // Whether the clock is stalled because it has reached the |max_time_| | 105 // Whether the clock is stalled because it has reached the |max_time_| |
103 // allowed. | 106 // allowed. |
104 bool underflow_; | 107 bool underflow_; |
105 | 108 |
106 // The system clock time when this clock last starting playing or had its | 109 // The monotonic system clock time when this Clock last started playing or had |
107 // time set via SetTime(). | 110 // its time set via SetTime(). |
108 base::Time reference_; | 111 base::TimeTicks reference_; |
109 | 112 |
110 // Current accumulated amount of media time. The remaining portion must be | 113 // Current accumulated amount of media time. The remaining portion must be |
111 // calculated by comparing the system time to the reference time. | 114 // calculated by comparing the system time to the reference time. |
112 base::TimeDelta media_time_; | 115 base::TimeDelta media_time_; |
113 | 116 |
114 // Current playback rate. | 117 // Current playback rate. |
115 float playback_rate_; | 118 float playback_rate_; |
116 | 119 |
117 // The maximum time that can be returned by calls to Elapsed(). | 120 // The maximum time that can be returned by calls to Elapsed(). |
118 base::TimeDelta max_time_; | 121 base::TimeDelta max_time_; |
119 | 122 |
120 // Duration of the media. | 123 // Duration of the media. |
121 base::TimeDelta duration_; | 124 base::TimeDelta duration_; |
122 | 125 |
123 DISALLOW_COPY_AND_ASSIGN(Clock); | 126 DISALLOW_COPY_AND_ASSIGN(Clock); |
124 }; | 127 }; |
125 | 128 |
126 } // namespace media | 129 } // namespace media |
127 | 130 |
128 #endif // MEDIA_BASE_CLOCK_H_ | 131 #endif // MEDIA_BASE_CLOCK_H_ |
OLD | NEW |