OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_TIME_SOURCE_H_ | 5 #ifndef MEDIA_BASE_TIME_SOURCE_H_ |
6 #define MEDIA_BASE_TIME_SOURCE_H_ | 6 #define MEDIA_BASE_TIME_SOURCE_H_ |
7 | 7 |
| 8 #include "base/callback.h" |
8 #include "base/time/time.h" | 9 #include "base/time/time.h" |
9 #include "media/base/media_export.h" | 10 #include "media/base/media_export.h" |
10 | 11 |
11 namespace media { | 12 namespace media { |
12 | 13 |
13 // A TimeSource is capable of providing the current media time. | 14 // A TimeSource is capable of providing the current media time. |
14 class MEDIA_EXPORT TimeSource { | 15 class MEDIA_EXPORT TimeSource { |
15 public: | 16 public: |
| 17 // Used to convert a media timestamp into a wall clock timestamp. |
| 18 using WallClockTimeCB = base::Callback<base::TimeTicks(base::TimeDelta, int)>; |
| 19 |
16 TimeSource() {} | 20 TimeSource() {} |
17 virtual ~TimeSource() {} | 21 virtual ~TimeSource() {} |
18 | 22 |
19 // Signal the time source to start ticking. It is expected that values from | 23 // Signal the time source to start ticking. It is expected that values from |
20 // CurrentMediaTime() will start increasing. | 24 // CurrentMediaTime() will start increasing. |
21 virtual void StartTicking() = 0; | 25 virtual void StartTicking() = 0; |
22 | 26 |
23 // Signal the time source to stop ticking. It is expected that values from | 27 // Signal the time source to stop ticking. It is expected that values from |
24 // CurrentMediaTime() will remain constant. | 28 // CurrentMediaTime() will remain constant. |
25 virtual void StopTicking() = 0; | 29 virtual void StopTicking() = 0; |
26 | 30 |
27 // Updates the current playback rate. It is expected that values from | 31 // Updates the current playback rate. It is expected that values from |
28 // CurrentMediaTime() will eventually reflect the new playback rate (e.g., the | 32 // CurrentMediaTime() will eventually reflect the new playback rate (e.g., the |
29 // media time will advance at half speed if the rate was set to 0.5f). | 33 // media time will advance at half speed if the rate was set to 0.5). |
30 virtual void SetPlaybackRate(double playback_rate) = 0; | 34 virtual void SetPlaybackRate(double playback_rate) = 0; |
31 | 35 |
32 // Sets the media time to start ticking from. Only valid to call while the | 36 // Sets the media time to start ticking from. Only valid to call while the |
33 // time source is not ticking. | 37 // time source is not ticking. |
34 virtual void SetMediaTime(base::TimeDelta time) = 0; | 38 virtual void SetMediaTime(base::TimeDelta time) = 0; |
35 | 39 |
36 // Returns the current media timestamp relative to the timestamp set by | 40 // Returns the current media timestamp relative to the timestamp set by |
37 // SetMediaTime(). | 41 // SetMediaTime(). |
38 // | 42 // |
39 // Values returned are intended for informational purposes, such as displaying | 43 // Values returned are intended for informational purposes, such as displaying |
40 // UI with the current minute and second count. While it is guaranteed values | 44 // UI with the current minute and second count. While it is guaranteed values |
41 // will never go backwards, the frequency at which they update may be low. | 45 // will never go backwards, the frequency at which they update may be low. |
42 virtual base::TimeDelta CurrentMediaTime() = 0; | 46 virtual base::TimeDelta CurrentMediaTime() = 0; |
43 | 47 |
44 // Converts a media timestamp into a wall clock time. If the media time is | 48 // Converts a media timestamp into a wall clock time. If the media time is |
45 // stopped, returns a null TimeTicks. | 49 // stopped, returns a null TimeTicks. |
46 // | 50 // |
47 // |media_time| values too far ahead of the current media time will return an | 51 // |media_time| values too far ahead of the current media time will return an |
48 // estimated value; as such, these values may go backwards in time slightly. | 52 // estimated value; as such, these values may go backwards in time slightly. |
49 // | 53 // |
50 // |media_time| values behind the current media time may be significantly | 54 // |media_time| values behind the current media time may be significantly |
51 // incorrect if the playback rate has changed recently. The only guarantee is | 55 // incorrect if the playback rate has changed recently. The only guarantee is |
52 // that the returned time will be less than the current wall clock time. | 56 // that the returned time will be less than the current wall clock time. |
53 virtual base::TimeTicks GetWallClockTime(base::TimeDelta media_time) = 0; | 57 // |
| 58 // Clients which need to convert a single timestamp should set |request_flags| |
| 59 // to SINGLE_TIMESTAMP. |
| 60 // |
| 61 // Clients which need to convert multiple timestamps may run into issues of |
| 62 // time monotonicity between timestamps if clock advancement and playback rate |
| 63 // changes are happening on a separate thread. Such clients should set the |
| 64 // |request_flags| value to SUSPEND_TIME for the first timestamp converted and |
| 65 // RESUME_TIME for the last timestamp to convert. Timestamps in between must |
| 66 // set TIME_IS_SUSPENDED. These flags guarantee that the time state will not |
| 67 // change between calls. Time may not be suspended when stopped, so if a null |
| 68 // TimeTicks value is returned, it is not necessary to send RESUME_TIME. |
| 69 // |
| 70 // Warning: Suspending the time state may stall other threads, including the |
| 71 // audio thread, do not suspend time for beyond very brief intervals. Unless |
| 72 // the returned TimeTicks value is null, callers which send SUSPEND_TIME must |
| 73 // _always_ send RESUME_TIME at a later time. |
| 74 enum RequestFlags { |
| 75 TIME_IS_SUSPENDED = 0, |
| 76 SUSPEND_TIME = 0x1, |
| 77 RESUME_TIME = 0x2, |
| 78 SINGLE_TIMESTAMP = SUSPEND_TIME | RESUME_TIME |
| 79 }; |
| 80 virtual base::TimeTicks GetWallClockTime(base::TimeDelta media_time, |
| 81 int request_flags) = 0; |
54 }; | 82 }; |
55 | 83 |
56 } // namespace media | 84 } // namespace media |
57 | 85 |
58 #endif // MEDIA_BASE_TIME_SOURCE_H_ | 86 #endif // MEDIA_BASE_TIME_SOURCE_H_ |
OLD | NEW |