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 #include "media/filters/audio_clock.h" | 5 #include "media/filters/audio_clock.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <cmath> | |
12 | 11 |
13 #include "base/logging.h" | 12 #include "base/logging.h" |
14 | 13 |
15 namespace media { | 14 namespace media { |
16 | 15 |
17 AudioClock::AudioClock(base::TimeDelta start_timestamp, int sample_rate) | 16 AudioClock::AudioClock(base::TimeDelta start_timestamp, int sample_rate) |
18 : start_timestamp_(start_timestamp), | 17 : start_timestamp_(start_timestamp), |
19 microseconds_per_frame_( | 18 microseconds_per_frame_( |
20 static_cast<double>(base::Time::kMicrosecondsPerSecond) / | 19 static_cast<double>(base::Time::kMicrosecondsPerSecond) / |
21 sample_rate), | 20 sample_rate), |
22 total_buffered_frames_(0), | 21 total_buffered_frames_(0), |
23 front_timestamp_micros_(start_timestamp.InMicroseconds()), | 22 front_timestamp_(start_timestamp), |
24 back_timestamp_micros_(start_timestamp.InMicroseconds()) {} | 23 back_timestamp_(start_timestamp) { |
| 24 } |
25 | 25 |
26 AudioClock::~AudioClock() { | 26 AudioClock::~AudioClock() { |
27 } | 27 } |
28 | 28 |
29 void AudioClock::WroteAudio(int frames_written, | 29 void AudioClock::WroteAudio(int frames_written, |
30 int frames_requested, | 30 int frames_requested, |
31 int delay_frames, | 31 int delay_frames, |
32 double playback_rate) { | 32 double playback_rate) { |
33 DCHECK_GE(frames_written, 0); | 33 DCHECK_GE(frames_written, 0); |
34 DCHECK_LE(frames_written, frames_requested); | 34 DCHECK_LE(frames_written, frames_requested); |
35 DCHECK_GE(delay_frames, 0); | 35 DCHECK_GE(delay_frames, 0); |
36 DCHECK_GE(playback_rate, 0); | 36 DCHECK_GE(playback_rate, 0); |
37 | 37 |
38 // First write: initialize buffer with silence. | 38 // First write: initialize buffer with silence. |
39 if (start_timestamp_.InMicroseconds() == front_timestamp_micros_ && | 39 if (start_timestamp_ == front_timestamp_ && buffered_.empty()) |
40 buffered_.empty()) { | |
41 PushBufferedAudioData(delay_frames, 0.0); | 40 PushBufferedAudioData(delay_frames, 0.0); |
42 } | |
43 | 41 |
44 // Move frames from |buffered_| into the computed timestamp based on | 42 // Move frames from |buffered_| into the computed timestamp based on |
45 // |delay_frames|. | 43 // |delay_frames|. |
46 // | 44 // |
47 // The ordering of compute -> push -> pop eliminates unnecessary memory | 45 // The ordering of compute -> push -> pop eliminates unnecessary memory |
48 // reallocations in cases where |buffered_| gets emptied. | 46 // reallocations in cases where |buffered_| gets emptied. |
49 int64_t frames_played = | 47 int64_t frames_played = |
50 std::max(INT64_C(0), total_buffered_frames_ - delay_frames); | 48 std::max(INT64_C(0), total_buffered_frames_ - delay_frames); |
51 PushBufferedAudioData(frames_written, playback_rate); | 49 PushBufferedAudioData(frames_written, playback_rate); |
52 PushBufferedAudioData(frames_requested - frames_written, 0.0); | 50 PushBufferedAudioData(frames_requested - frames_written, 0.0); |
53 PopBufferedAudioData(frames_played); | 51 PopBufferedAudioData(frames_played); |
54 | 52 |
55 // Update our front and back timestamps. The back timestamp is considered the | 53 // Update our front and back timestamps. The back timestamp is considered the |
56 // authoritative source of truth, so base the front timestamp on range of data | 54 // authoritative source of truth, so base the front timestamp on range of data |
57 // buffered. Doing so avoids accumulation errors on the front timestamp. | 55 // buffered. Doing so avoids accumulation errors on the front timestamp. |
58 back_timestamp_micros_ += | 56 back_timestamp_ += base::TimeDelta::FromMicroseconds( |
59 frames_written * playback_rate * microseconds_per_frame_; | 57 frames_written * playback_rate * microseconds_per_frame_); |
60 | |
61 // Don't let front timestamp move earlier in time, as could occur due to delay | 58 // Don't let front timestamp move earlier in time, as could occur due to delay |
62 // frames pushed in the first write, above. | 59 // frames pushed in the first write, above. |
63 front_timestamp_micros_ = | 60 front_timestamp_ = std::max(front_timestamp_, |
64 std::max(front_timestamp_micros_, | 61 back_timestamp_ - ComputeBufferedMediaDuration()); |
65 back_timestamp_micros_ - ComputeBufferedMediaDurationMicros()); | 62 DCHECK_GE(front_timestamp_, start_timestamp_); |
66 DCHECK_GE(front_timestamp_micros_, start_timestamp_.InMicroseconds()); | 63 DCHECK_LE(front_timestamp_, back_timestamp_); |
67 DCHECK_LE(front_timestamp_micros_, back_timestamp_micros_); | |
68 } | 64 } |
69 | 65 |
70 void AudioClock::CompensateForSuspendedWrites(base::TimeDelta elapsed, | 66 void AudioClock::CompensateForSuspendedWrites(base::TimeDelta elapsed, |
71 int delay_frames) { | 67 int delay_frames) { |
72 const int64_t frames_elapsed = | 68 const int64_t frames_elapsed = |
73 elapsed.InMicroseconds() / microseconds_per_frame_ + 0.5; | 69 elapsed.InMicroseconds() / microseconds_per_frame_ + 0.5; |
74 | 70 |
75 // No need to do anything if we're within the limits of our played out audio | 71 // No need to do anything if we're within the limits of our played out audio |
76 // or there are no delay frames, the next WroteAudio() call will expire | 72 // or there are no delay frames, the next WroteAudio() call will expire |
77 // everything correctly. | 73 // everything correctly. |
78 if (frames_elapsed < total_buffered_frames_ || !delay_frames) | 74 if (frames_elapsed < total_buffered_frames_ || !delay_frames) |
79 return; | 75 return; |
80 | 76 |
81 // Otherwise, flush everything and prime with the delay frames. | 77 // Otherwise, flush everything and prime with the delay frames. |
82 WroteAudio(0, 0, 0, 0); | 78 WroteAudio(0, 0, 0, 0); |
83 DCHECK(buffered_.empty()); | 79 DCHECK(buffered_.empty()); |
84 PushBufferedAudioData(delay_frames, 0.0); | 80 PushBufferedAudioData(delay_frames, 0.0); |
85 } | 81 } |
86 | 82 |
87 base::TimeDelta AudioClock::TimeUntilPlayback(base::TimeDelta timestamp) const { | 83 base::TimeDelta AudioClock::TimeUntilPlayback(base::TimeDelta timestamp) const { |
88 // Use front/back_timestamp() methods rather than internal members. The public | 84 DCHECK_GE(timestamp, front_timestamp_); |
89 // methods round to the nearest microsecond for conversion to TimeDelta and | 85 DCHECK_LE(timestamp, back_timestamp_); |
90 // the rounded value will likely be used by the caller. | |
91 DCHECK_GE(timestamp, front_timestamp()); | |
92 DCHECK_LE(timestamp, back_timestamp()); | |
93 | 86 |
94 int64_t frames_until_timestamp = 0; | 87 int64_t frames_until_timestamp = 0; |
95 double timestamp_us = timestamp.InMicroseconds(); | 88 double timestamp_us = timestamp.InMicroseconds(); |
96 double media_time_us = front_timestamp().InMicroseconds(); | 89 double media_time_us = front_timestamp_.InMicroseconds(); |
97 | 90 |
98 for (size_t i = 0; i < buffered_.size(); ++i) { | 91 for (size_t i = 0; i < buffered_.size(); ++i) { |
99 // Leading silence is always accounted prior to anything else. | 92 // Leading silence is always accounted prior to anything else. |
100 if (buffered_[i].playback_rate == 0) { | 93 if (buffered_[i].playback_rate == 0) { |
101 frames_until_timestamp += buffered_[i].frames; | 94 frames_until_timestamp += buffered_[i].frames; |
102 continue; | 95 continue; |
103 } | 96 } |
104 | 97 |
105 // Calculate upper bound on media time for current block of buffered frames. | 98 // Calculate upper bound on media time for current block of buffered frames. |
106 double delta_us = buffered_[i].frames * buffered_[i].playback_rate * | 99 double delta_us = buffered_[i].frames * buffered_[i].playback_rate * |
107 microseconds_per_frame_; | 100 microseconds_per_frame_; |
108 double max_media_time_us = media_time_us + delta_us; | 101 double max_media_time_us = media_time_us + delta_us; |
109 | 102 |
110 // Determine amount of media time to convert to frames for current block. If | 103 // Determine amount of media time to convert to frames for current block. If |
111 // target timestamp falls within current block, scale the amount of frames | 104 // target timestamp falls within current block, scale the amount of frames |
112 // based on remaining amount of media time. | 105 // based on remaining amount of media time. |
113 if (timestamp_us <= max_media_time_us) { | 106 if (timestamp_us <= max_media_time_us) { |
114 frames_until_timestamp += | 107 frames_until_timestamp += |
115 buffered_[i].frames * (timestamp_us - media_time_us) / delta_us; | 108 buffered_[i].frames * (timestamp_us - media_time_us) / delta_us; |
116 break; | 109 break; |
117 } | 110 } |
118 | 111 |
119 media_time_us = max_media_time_us; | 112 media_time_us = max_media_time_us; |
120 frames_until_timestamp += buffered_[i].frames; | 113 frames_until_timestamp += buffered_[i].frames; |
121 } | 114 } |
122 | 115 |
123 return base::TimeDelta::FromMicroseconds( | 116 return base::TimeDelta::FromMicroseconds(frames_until_timestamp * |
124 std::round(frames_until_timestamp * microseconds_per_frame_)); | 117 microseconds_per_frame_); |
125 } | 118 } |
126 | 119 |
127 void AudioClock::ContiguousAudioDataBufferedForTesting( | 120 void AudioClock::ContiguousAudioDataBufferedForTesting( |
128 base::TimeDelta* total, | 121 base::TimeDelta* total, |
129 base::TimeDelta* same_rate_total) const { | 122 base::TimeDelta* same_rate_total) const { |
130 double scaled_frames = 0; | 123 double scaled_frames = 0; |
131 double scaled_frames_at_same_rate = 0; | 124 double scaled_frames_at_same_rate = 0; |
132 bool found_silence = false; | 125 bool found_silence = false; |
133 for (size_t i = 0; i < buffered_.size(); ++i) { | 126 for (size_t i = 0; i < buffered_.size(); ++i) { |
134 if (buffered_[i].playback_rate == 0) { | 127 if (buffered_[i].playback_rate == 0) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 while (frames > 0) { | 172 while (frames > 0) { |
180 int64_t frames_to_pop = std::min(buffered_.front().frames, frames); | 173 int64_t frames_to_pop = std::min(buffered_.front().frames, frames); |
181 buffered_.front().frames -= frames_to_pop; | 174 buffered_.front().frames -= frames_to_pop; |
182 if (buffered_.front().frames == 0) | 175 if (buffered_.front().frames == 0) |
183 buffered_.pop_front(); | 176 buffered_.pop_front(); |
184 | 177 |
185 frames -= frames_to_pop; | 178 frames -= frames_to_pop; |
186 } | 179 } |
187 } | 180 } |
188 | 181 |
189 double AudioClock::ComputeBufferedMediaDurationMicros() const { | 182 base::TimeDelta AudioClock::ComputeBufferedMediaDuration() const { |
190 double scaled_frames = 0; | 183 double scaled_frames = 0; |
191 for (const auto& buffer : buffered_) | 184 for (const auto& buffer : buffered_) |
192 scaled_frames += buffer.frames * buffer.playback_rate; | 185 scaled_frames += buffer.frames * buffer.playback_rate; |
193 return scaled_frames * microseconds_per_frame_; | 186 return base::TimeDelta::FromMicroseconds(scaled_frames * |
| 187 microseconds_per_frame_); |
194 } | 188 } |
195 | 189 |
196 } // namespace media | 190 } // namespace media |
OLD | NEW |