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 #include "media/base/audio_timestamp_helper.h" | 5 #include "media/base/audio_timestamp_helper.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "media/base/timestamp_constants.h" | 8 #include "media/base/timestamp_constants.h" |
9 | 9 |
10 namespace media { | 10 namespace media { |
11 | 11 |
12 AudioTimestampHelper::AudioTimestampHelper(int samples_per_second) | 12 AudioTimestampHelper::AudioTimestampHelper(int samples_per_second) |
13 : base_timestamp_(kNoTimestamp()), | 13 : base_timestamp_(kNoTimestamp), frame_count_(0) { |
14 frame_count_(0) { | |
15 DCHECK_GT(samples_per_second, 0); | 14 DCHECK_GT(samples_per_second, 0); |
16 double fps = samples_per_second; | 15 double fps = samples_per_second; |
17 microseconds_per_frame_ = base::Time::kMicrosecondsPerSecond / fps; | 16 microseconds_per_frame_ = base::Time::kMicrosecondsPerSecond / fps; |
18 } | 17 } |
19 | 18 |
20 void AudioTimestampHelper::SetBaseTimestamp(base::TimeDelta base_timestamp) { | 19 void AudioTimestampHelper::SetBaseTimestamp(base::TimeDelta base_timestamp) { |
21 base_timestamp_ = base_timestamp; | 20 base_timestamp_ = base_timestamp; |
22 frame_count_ = 0; | 21 frame_count_ = 0; |
23 } | 22 } |
24 | 23 |
25 base::TimeDelta AudioTimestampHelper::base_timestamp() const { | 24 base::TimeDelta AudioTimestampHelper::base_timestamp() const { |
26 return base_timestamp_; | 25 return base_timestamp_; |
27 } | 26 } |
28 | 27 |
29 void AudioTimestampHelper::AddFrames(int frame_count) { | 28 void AudioTimestampHelper::AddFrames(int frame_count) { |
30 DCHECK_GE(frame_count, 0); | 29 DCHECK_GE(frame_count, 0); |
31 DCHECK(base_timestamp_ != kNoTimestamp()); | 30 DCHECK(base_timestamp_ != kNoTimestamp); |
32 frame_count_ += frame_count; | 31 frame_count_ += frame_count; |
33 } | 32 } |
34 | 33 |
35 base::TimeDelta AudioTimestampHelper::GetTimestamp() const { | 34 base::TimeDelta AudioTimestampHelper::GetTimestamp() const { |
36 return ComputeTimestamp(frame_count_); | 35 return ComputeTimestamp(frame_count_); |
37 } | 36 } |
38 | 37 |
39 base::TimeDelta AudioTimestampHelper::GetFrameDuration(int frame_count) const { | 38 base::TimeDelta AudioTimestampHelper::GetFrameDuration(int frame_count) const { |
40 DCHECK_GE(frame_count, 0); | 39 DCHECK_GE(frame_count, 0); |
41 base::TimeDelta end_timestamp = ComputeTimestamp(frame_count_ + frame_count); | 40 base::TimeDelta end_timestamp = ComputeTimestamp(frame_count_ + frame_count); |
42 return end_timestamp - GetTimestamp(); | 41 return end_timestamp - GetTimestamp(); |
43 } | 42 } |
44 | 43 |
45 int64_t AudioTimestampHelper::GetFramesToTarget(base::TimeDelta target) const { | 44 int64_t AudioTimestampHelper::GetFramesToTarget(base::TimeDelta target) const { |
46 DCHECK(base_timestamp_ != kNoTimestamp()); | 45 DCHECK(base_timestamp_ != kNoTimestamp); |
47 DCHECK(target >= base_timestamp_); | 46 DCHECK(target >= base_timestamp_); |
48 | 47 |
49 int64_t delta_in_us = (target - GetTimestamp()).InMicroseconds(); | 48 int64_t delta_in_us = (target - GetTimestamp()).InMicroseconds(); |
50 if (delta_in_us == 0) | 49 if (delta_in_us == 0) |
51 return 0; | 50 return 0; |
52 | 51 |
53 // Compute a timestamp relative to |base_timestamp_| since timestamps | 52 // Compute a timestamp relative to |base_timestamp_| since timestamps |
54 // created from |frame_count_| are computed relative to this base. | 53 // created from |frame_count_| are computed relative to this base. |
55 // This ensures that the time to frame computation here is the proper inverse | 54 // This ensures that the time to frame computation here is the proper inverse |
56 // of the frame to time computation in ComputeTimestamp(). | 55 // of the frame to time computation in ComputeTimestamp(). |
57 base::TimeDelta delta_from_base = target - base_timestamp_; | 56 base::TimeDelta delta_from_base = target - base_timestamp_; |
58 | 57 |
59 // Compute frame count for the time delta. This computation rounds to | 58 // Compute frame count for the time delta. This computation rounds to |
60 // the nearest whole number of frames. | 59 // the nearest whole number of frames. |
61 double threshold = microseconds_per_frame_ / 2; | 60 double threshold = microseconds_per_frame_ / 2; |
62 int64_t target_frame_count = | 61 int64_t target_frame_count = |
63 (delta_from_base.InMicroseconds() + threshold) / microseconds_per_frame_; | 62 (delta_from_base.InMicroseconds() + threshold) / microseconds_per_frame_; |
64 return target_frame_count - frame_count_; | 63 return target_frame_count - frame_count_; |
65 } | 64 } |
66 | 65 |
67 base::TimeDelta AudioTimestampHelper::ComputeTimestamp( | 66 base::TimeDelta AudioTimestampHelper::ComputeTimestamp( |
68 int64_t frame_count) const { | 67 int64_t frame_count) const { |
69 DCHECK_GE(frame_count, 0); | 68 DCHECK_GE(frame_count, 0); |
70 DCHECK(base_timestamp_ != kNoTimestamp()); | 69 DCHECK(base_timestamp_ != kNoTimestamp); |
71 double frames_us = microseconds_per_frame_ * frame_count; | 70 double frames_us = microseconds_per_frame_ * frame_count; |
72 return base_timestamp_ + base::TimeDelta::FromMicroseconds(frames_us); | 71 return base_timestamp_ + base::TimeDelta::FromMicroseconds(frames_us); |
73 } | 72 } |
74 | 73 |
75 } // namespace media | 74 } // namespace media |
OLD | NEW |