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 <algorithm> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "media/base/buffers.h" | 10 #include "media/base/buffers.h" |
9 | 11 |
10 namespace media { | 12 namespace media { |
11 | 13 |
12 AudioClock::AudioClock(int sample_rate) | 14 AudioClock::AudioClock(base::TimeDelta start_timestamp, int sample_rate) |
13 : sample_rate_(sample_rate), last_endpoint_timestamp_(kNoTimestamp()) { | 15 : start_timestamp_(start_timestamp), |
| 16 sample_rate_(sample_rate), |
| 17 microseconds_per_frame_( |
| 18 static_cast<double>(base::Time::kMicrosecondsPerSecond) / |
| 19 sample_rate), |
| 20 total_buffered_frames_(0), |
| 21 current_media_timestamp_(start_timestamp), |
| 22 audio_data_buffered_(0) { |
14 } | 23 } |
15 | 24 |
16 AudioClock::~AudioClock() { | 25 AudioClock::~AudioClock() { |
17 } | 26 } |
18 | 27 |
19 void AudioClock::WroteAudio(int frames, | 28 void AudioClock::WroteAudio(int frames_written, |
| 29 int frames_requested, |
20 int delay_frames, | 30 int delay_frames, |
21 float playback_rate, | 31 float playback_rate) { |
22 base::TimeDelta timestamp) { | 32 DCHECK_GE(frames_written, 0); |
23 CHECK_GT(playback_rate, 0); | 33 DCHECK_LE(frames_written, frames_requested); |
24 CHECK(timestamp != kNoTimestamp()); | |
25 DCHECK_GE(frames, 0); | |
26 DCHECK_GE(delay_frames, 0); | 34 DCHECK_GE(delay_frames, 0); |
| 35 DCHECK_GE(playback_rate, 0); |
27 | 36 |
28 if (last_endpoint_timestamp_ == kNoTimestamp()) | 37 // First write: initialize buffer with silence. |
29 PushBufferedAudio(delay_frames, 0, kNoTimestamp()); | 38 if (start_timestamp_ == current_media_timestamp_ && buffered_.empty()) |
| 39 PushBufferedAudioData(delay_frames, 0.0f); |
30 | 40 |
31 TrimBufferedAudioToMatchDelay(delay_frames); | 41 // Move frames from |buffered_| into the computed timestamp based on |
32 PushBufferedAudio(frames, playback_rate, timestamp); | 42 // |delay_frames|. |
| 43 // |
| 44 // The ordering of compute -> push -> pop eliminates unnecessary memory |
| 45 // reallocations in cases where |buffered_| gets emptied. |
| 46 int64_t frames_played = |
| 47 std::max(INT64_C(0), total_buffered_frames_ - delay_frames); |
| 48 current_media_timestamp_ += ComputeBufferedMediaTime(frames_played); |
| 49 PushBufferedAudioData(frames_written, playback_rate); |
| 50 PushBufferedAudioData(frames_requested - frames_written, 0.0f); |
| 51 PopBufferedAudioData(frames_played); |
33 | 52 |
34 last_endpoint_timestamp_ = timestamp; | 53 // Update cached values. |
35 } | 54 double scaled_frames = 0; |
36 | 55 double scaled_frames_at_same_rate = 0; |
37 void AudioClock::WroteSilence(int frames, int delay_frames) { | 56 bool found_silence = false; |
38 DCHECK_GE(frames, 0); | 57 audio_data_buffered_ = false; |
39 DCHECK_GE(delay_frames, 0); | 58 for (size_t i = 0; i < buffered_.size(); ++i) { |
40 | 59 if (buffered_[i].playback_rate == 0) { |
41 if (last_endpoint_timestamp_ == kNoTimestamp()) | 60 found_silence = true; |
42 PushBufferedAudio(delay_frames, 0, kNoTimestamp()); | |
43 | |
44 TrimBufferedAudioToMatchDelay(delay_frames); | |
45 PushBufferedAudio(frames, 0, kNoTimestamp()); | |
46 } | |
47 | |
48 base::TimeDelta AudioClock::CurrentMediaTimestamp( | |
49 base::TimeDelta time_since_writing) const { | |
50 int frames_to_skip = | |
51 static_cast<int>(time_since_writing.InSecondsF() * sample_rate_); | |
52 int silence_frames = 0; | |
53 for (size_t i = 0; i < buffered_audio_.size(); ++i) { | |
54 int frames = buffered_audio_[i].frames; | |
55 if (frames_to_skip > 0) { | |
56 if (frames <= frames_to_skip) { | |
57 frames_to_skip -= frames; | |
58 continue; | |
59 } | |
60 frames -= frames_to_skip; | |
61 frames_to_skip = 0; | |
62 } | |
63 | |
64 // Account for silence ahead of the buffer closest to being played. | |
65 if (buffered_audio_[i].playback_rate == 0) { | |
66 silence_frames += frames; | |
67 continue; | 61 continue; |
68 } | 62 } |
69 | 63 |
70 // Multiply by playback rate as frames represent time-scaled audio. | 64 audio_data_buffered_ = true; |
71 return buffered_audio_[i].endpoint_timestamp - | 65 |
72 base::TimeDelta::FromMicroseconds( | 66 // Any buffered silence breaks our contiguous stretch of audio data. |
73 ((frames * buffered_audio_[i].playback_rate) + silence_frames) / | 67 if (found_silence) |
74 sample_rate_ * base::Time::kMicrosecondsPerSecond); | 68 break; |
| 69 |
| 70 scaled_frames += (buffered_[i].frames * buffered_[i].playback_rate); |
| 71 |
| 72 if (i == 0) |
| 73 scaled_frames_at_same_rate = scaled_frames; |
75 } | 74 } |
76 | 75 |
77 // Either: | 76 contiguous_audio_data_buffered_ = base::TimeDelta::FromMicroseconds( |
78 // 1) AudioClock is uninitialziated and we'll return kNoTimestamp() | 77 scaled_frames * microseconds_per_frame_); |
79 // 2) All previously buffered audio has been replaced by silence, | 78 contiguous_audio_data_buffered_at_same_rate_ = |
80 // meaning media time is now at the last endpoint | 79 base::TimeDelta::FromMicroseconds(scaled_frames_at_same_rate * |
81 return last_endpoint_timestamp_; | 80 microseconds_per_frame_); |
82 } | 81 } |
83 | 82 |
84 void AudioClock::TrimBufferedAudioToMatchDelay(int delay_frames) { | 83 base::TimeDelta AudioClock::CurrentMediaTimestampSinceWriting( |
85 if (buffered_audio_.empty()) | 84 base::TimeDelta time_since_writing) const { |
86 return; | 85 int64_t frames_played_since_writing = std::min( |
87 | 86 total_buffered_frames_, |
88 size_t i = buffered_audio_.size() - 1; | 87 static_cast<int64_t>(time_since_writing.InSecondsF() * sample_rate_)); |
89 while (true) { | 88 return current_media_timestamp_ + |
90 if (buffered_audio_[i].frames <= delay_frames) { | 89 ComputeBufferedMediaTime(frames_played_since_writing); |
91 // Reached the end before accounting for all of |delay_frames|. This | |
92 // means we haven't written enough audio data yet to account for hardware | |
93 // delay. In this case, do nothing. | |
94 if (i == 0) | |
95 return; | |
96 | |
97 // Keep accounting for |delay_frames|. | |
98 delay_frames -= buffered_audio_[i].frames; | |
99 --i; | |
100 continue; | |
101 } | |
102 | |
103 // All of |delay_frames| has been accounted for: adjust amount of frames | |
104 // left in current buffer. All preceeding elements with index < |i| should | |
105 // be considered played out and hence discarded. | |
106 buffered_audio_[i].frames = delay_frames; | |
107 break; | |
108 } | |
109 | |
110 // At this point |i| points at what will be the new head of |buffered_audio_| | |
111 // however if it contains no audio it should be removed as well. | |
112 if (buffered_audio_[i].frames == 0) | |
113 ++i; | |
114 | |
115 buffered_audio_.erase(buffered_audio_.begin(), buffered_audio_.begin() + i); | |
116 } | 90 } |
117 | 91 |
118 void AudioClock::PushBufferedAudio(int frames, | 92 AudioClock::AudioData::AudioData(int64_t frames, float playback_rate) |
119 float playback_rate, | 93 : frames(frames), playback_rate(playback_rate) { |
120 base::TimeDelta endpoint_timestamp) { | 94 } |
121 if (playback_rate == 0) | |
122 DCHECK(endpoint_timestamp == kNoTimestamp()); | |
123 | 95 |
| 96 void AudioClock::PushBufferedAudioData(int64_t frames, float playback_rate) { |
124 if (frames == 0) | 97 if (frames == 0) |
125 return; | 98 return; |
126 | 99 |
| 100 total_buffered_frames_ += frames; |
| 101 |
127 // Avoid creating extra elements where possible. | 102 // Avoid creating extra elements where possible. |
128 if (!buffered_audio_.empty() && | 103 if (!buffered_.empty() && buffered_.back().playback_rate == playback_rate) { |
129 buffered_audio_.back().playback_rate == playback_rate) { | 104 buffered_.back().frames += frames; |
130 buffered_audio_.back().frames += frames; | |
131 buffered_audio_.back().endpoint_timestamp = endpoint_timestamp; | |
132 return; | 105 return; |
133 } | 106 } |
134 | 107 |
135 buffered_audio_.push_back( | 108 buffered_.push_back(AudioData(frames, playback_rate)); |
136 BufferedAudio(frames, playback_rate, endpoint_timestamp)); | |
137 } | 109 } |
138 | 110 |
139 AudioClock::BufferedAudio::BufferedAudio(int frames, | 111 void AudioClock::PopBufferedAudioData(int64_t frames) { |
140 float playback_rate, | 112 DCHECK_LE(frames, total_buffered_frames_); |
141 base::TimeDelta endpoint_timestamp) | 113 |
142 : frames(frames), | 114 total_buffered_frames_ -= frames; |
143 playback_rate(playback_rate), | 115 |
144 endpoint_timestamp(endpoint_timestamp) { | 116 while (frames > 0) { |
| 117 int64_t frames_to_pop = std::min(buffered_.front().frames, frames); |
| 118 buffered_.front().frames -= frames_to_pop; |
| 119 if (buffered_.front().frames == 0) |
| 120 buffered_.pop_front(); |
| 121 |
| 122 frames -= frames_to_pop; |
| 123 } |
| 124 } |
| 125 |
| 126 base::TimeDelta AudioClock::ComputeBufferedMediaTime(int64_t frames) const { |
| 127 DCHECK_LE(frames, total_buffered_frames_); |
| 128 |
| 129 double scaled_frames = 0; |
| 130 for (size_t i = 0; i < buffered_.size() && frames > 0; ++i) { |
| 131 int64_t min_frames = std::min(buffered_[i].frames, frames); |
| 132 scaled_frames += min_frames * buffered_[i].playback_rate; |
| 133 frames -= min_frames; |
| 134 } |
| 135 |
| 136 return base::TimeDelta::FromMicroseconds(scaled_frames * |
| 137 microseconds_per_frame_); |
145 } | 138 } |
146 | 139 |
147 } // namespace media | 140 } // namespace media |
OLD | NEW |