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/audio/virtual_audio_input_stream.h" | 5 #include "media/audio/virtual_audio_input_stream.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 } | 78 } |
79 | 79 |
80 bool VirtualAudioInputStream::Open() { | 80 bool VirtualAudioInputStream::Open() { |
81 memset(buffer_.get(), 0, params_.GetBytesPerBuffer()); | 81 memset(buffer_.get(), 0, params_.GetBytesPerBuffer()); |
82 return true; | 82 return true; |
83 } | 83 } |
84 | 84 |
85 void VirtualAudioInputStream::Start(AudioInputCallback* callback) { | 85 void VirtualAudioInputStream::Start(AudioInputCallback* callback) { |
86 DCHECK(message_loop_->BelongsToCurrentThread()); | 86 DCHECK(message_loop_->BelongsToCurrentThread()); |
87 callback_ = callback; | 87 callback_ = callback; |
88 next_read_time_ = base::Time::Now(); | |
88 on_more_data_cb_.Reset(base::Bind(&VirtualAudioInputStream::ReadAudio, | 89 on_more_data_cb_.Reset(base::Bind(&VirtualAudioInputStream::ReadAudio, |
89 base::Unretained(this))); | 90 base::Unretained(this))); |
90 audio_manager_->GetMessageLoop()->PostTask(FROM_HERE, | 91 audio_manager_->GetMessageLoop()->PostTask(FROM_HERE, |
91 on_more_data_cb_.callback()); | 92 on_more_data_cb_.callback()); |
92 } | 93 } |
93 | 94 |
94 void VirtualAudioInputStream::Stop() { | 95 void VirtualAudioInputStream::Stop() { |
95 DCHECK(message_loop_->BelongsToCurrentThread()); | 96 DCHECK(message_loop_->BelongsToCurrentThread()); |
96 on_more_data_cb_.Cancel(); | 97 on_more_data_cb_.Cancel(); |
97 } | 98 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 audio_bus_->ToInterleaved(params_.frames_per_buffer(), | 133 audio_bus_->ToInterleaved(params_.frames_per_buffer(), |
133 params_.bits_per_sample() / 8, | 134 params_.bits_per_sample() / 8, |
134 buffer_.get()); | 135 buffer_.get()); |
135 | 136 |
136 callback_->OnData(this, | 137 callback_->OnData(this, |
137 buffer_.get(), | 138 buffer_.get(), |
138 params_.GetBytesPerBuffer(), | 139 params_.GetBytesPerBuffer(), |
139 params_.GetBytesPerBuffer(), | 140 params_.GetBytesPerBuffer(), |
140 1.0); | 141 1.0); |
141 | 142 |
143 // Need to account for time spent here due to renderer side mixing as well as | |
144 // the imprecision of PostDelayedTask. | |
145 next_read_time_ += buffer_duration_ms_; | |
146 base::TimeDelta delay = next_read_time_ - base::Time::Now(); | |
147 if (delay < base::TimeDelta()) { | |
148 // Reset the next read time if we end up getting too far behind. We'll just | |
149 // slow down playback to avoid using up all the CPU. | |
150 delay = base::TimeDelta(); | |
151 next_read_time_ = base::Time::Now(); | |
DaleCurtis
2013/01/17 00:10:51
I'd save base::Time::Now() from the first call ins
justinlin
2013/01/17 22:21:05
Done. Updated this: delay should be set to the buf
| |
152 } | |
153 | |
142 message_loop_->PostDelayedTask(FROM_HERE, | 154 message_loop_->PostDelayedTask(FROM_HERE, |
143 on_more_data_cb_.callback(), | 155 on_more_data_cb_.callback(), |
144 buffer_duration_ms_); | 156 delay); |
145 } | 157 } |
146 | 158 |
147 void VirtualAudioInputStream::Close() { | 159 void VirtualAudioInputStream::Close() { |
148 DCHECK(message_loop_->BelongsToCurrentThread()); | 160 DCHECK(message_loop_->BelongsToCurrentThread()); |
149 if (callback_) { | 161 if (callback_) { |
150 DCHECK(on_more_data_cb_.IsCancelled()); | 162 DCHECK(on_more_data_cb_.IsCancelled()); |
151 callback_->OnClose(this); | 163 callback_->OnClose(this); |
152 callback_ = NULL; | 164 callback_ = NULL; |
153 } | 165 } |
154 audio_manager_->ReleaseInputStream(this); | 166 audio_manager_->ReleaseInputStream(this); |
155 } | 167 } |
156 | 168 |
157 double VirtualAudioInputStream::GetMaxVolume() { | 169 double VirtualAudioInputStream::GetMaxVolume() { |
158 return 1.0; | 170 return 1.0; |
159 } | 171 } |
160 | 172 |
161 void VirtualAudioInputStream::SetVolume(double volume) {} | 173 void VirtualAudioInputStream::SetVolume(double volume) {} |
162 | 174 |
163 double VirtualAudioInputStream::GetVolume() { | 175 double VirtualAudioInputStream::GetVolume() { |
164 return 1.0; | 176 return 1.0; |
165 } | 177 } |
166 | 178 |
167 void VirtualAudioInputStream::SetAutomaticGainControl(bool enabled) {} | 179 void VirtualAudioInputStream::SetAutomaticGainControl(bool enabled) {} |
168 | 180 |
169 bool VirtualAudioInputStream::GetAutomaticGainControl() { | 181 bool VirtualAudioInputStream::GetAutomaticGainControl() { |
170 return false; | 182 return false; |
171 } | 183 } |
172 | 184 |
173 } // namespace media | 185 } // namespace media |
OLD | NEW |