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/mac/audio_low_latency_output_mac.h" | 5 #include "media/audio/mac/audio_low_latency_output_mac.h" |
6 | 6 |
7 #include <CoreServices/CoreServices.h> | 7 #include <CoreServices/CoreServices.h> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 // 6) The same thread that called stop will call Close() where we cleanup | 43 // 6) The same thread that called stop will call Close() where we cleanup |
44 // and notify the audio manager, which likely will destroy this object. | 44 // and notify the audio manager, which likely will destroy this object. |
45 | 45 |
46 AUAudioOutputStream::AUAudioOutputStream( | 46 AUAudioOutputStream::AUAudioOutputStream( |
47 AudioManagerMac* manager, const AudioParameters& params) | 47 AudioManagerMac* manager, const AudioParameters& params) |
48 : manager_(manager), | 48 : manager_(manager), |
49 source_(NULL), | 49 source_(NULL), |
50 output_unit_(0), | 50 output_unit_(0), |
51 output_device_id_(kAudioObjectUnknown), | 51 output_device_id_(kAudioObjectUnknown), |
52 volume_(1), | 52 volume_(1), |
53 hardware_latency_frames_(0) { | 53 hardware_latency_frames_(0), |
54 stopped_(false) { | |
54 // We must have a manager. | 55 // We must have a manager. |
55 DCHECK(manager_); | 56 DCHECK(manager_); |
56 // A frame is one sample across all channels. In interleaved audio the per | 57 // A frame is one sample across all channels. In interleaved audio the per |
57 // frame fields identify the set of n |channels|. In uncompressed audio, a | 58 // frame fields identify the set of n |channels|. In uncompressed audio, a |
58 // packet is always one frame. | 59 // packet is always one frame. |
59 format_.mSampleRate = params.sample_rate(); | 60 format_.mSampleRate = params.sample_rate(); |
60 format_.mFormatID = kAudioFormatLinearPCM; | 61 format_.mFormatID = kAudioFormatLinearPCM; |
61 format_.mFormatFlags = kLinearPCMFormatFlagIsPacked | | 62 format_.mFormatFlags = kLinearPCMFormatFlagIsPacked | |
62 kLinearPCMFormatFlagIsSignedInteger; | 63 kLinearPCMFormatFlagIsSignedInteger; |
63 format_.mBitsPerChannel = params.bits_per_sample(); | 64 format_.mBitsPerChannel = params.bits_per_sample(); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 // destruction. | 172 // destruction. |
172 manager_->ReleaseOutputStream(this); | 173 manager_->ReleaseOutputStream(this); |
173 } | 174 } |
174 | 175 |
175 void AUAudioOutputStream::Start(AudioSourceCallback* callback) { | 176 void AUAudioOutputStream::Start(AudioSourceCallback* callback) { |
176 DCHECK(callback); | 177 DCHECK(callback); |
177 DLOG_IF(ERROR, !output_unit_) << "Open() has not been called successfully"; | 178 DLOG_IF(ERROR, !output_unit_) << "Open() has not been called successfully"; |
178 if (!output_unit_) | 179 if (!output_unit_) |
179 return; | 180 return; |
180 | 181 |
182 stopped_ = false; | |
181 source_ = callback; | 183 source_ = callback; |
182 | 184 |
183 AudioOutputUnitStart(output_unit_); | 185 AudioOutputUnitStart(output_unit_); |
184 } | 186 } |
185 | 187 |
186 void AUAudioOutputStream::Stop() { | 188 void AUAudioOutputStream::Stop() { |
187 // We request a synchronous stop, so the next call can take some time. In | 189 // We request a synchronous stop, so the next call can take some time. In |
188 // the windows implementation we block here as well. | 190 // the windows implementation we block here as well. |
189 source_ = NULL; | 191 if (stopped_) |
192 return; | |
193 | |
194 stopped_ = true; | |
Chris Rogers
2012/03/28 18:55:10
Should this maybe be set to "true" only after Audi
no longer working on chromium
2012/03/28 19:25:32
Done.
| |
190 | 195 |
191 AudioOutputUnitStop(output_unit_); | 196 AudioOutputUnitStop(output_unit_); |
197 | |
198 source_ = NULL; | |
192 } | 199 } |
193 | 200 |
194 void AUAudioOutputStream::SetVolume(double volume) { | 201 void AUAudioOutputStream::SetVolume(double volume) { |
195 if (!output_unit_) | 202 if (!output_unit_) |
196 return; | 203 return; |
197 volume_ = static_cast<float>(volume); | 204 volume_ = static_cast<float>(volume); |
198 | 205 |
199 // TODO(crogers): set volume property | 206 // TODO(crogers): set volume property |
200 } | 207 } |
201 | 208 |
202 void AUAudioOutputStream::GetVolume(double* volume) { | 209 void AUAudioOutputStream::GetVolume(double* volume) { |
203 if (!output_unit_) | 210 if (!output_unit_) |
204 return; | 211 return; |
205 *volume = volume_; | 212 *volume = volume_; |
206 } | 213 } |
207 | 214 |
208 // Pulls on our provider to get rendered audio stream. | 215 // Pulls on our provider to get rendered audio stream. |
209 // Note to future hackers of this function: Do not add locks here because this | 216 // Note to future hackers of this function: Do not add locks here because this |
210 // is running on a real-time thread (for low-latency). | 217 // is running on a real-time thread (for low-latency). |
211 OSStatus AUAudioOutputStream::Render(UInt32 number_of_frames, | 218 OSStatus AUAudioOutputStream::Render(UInt32 number_of_frames, |
212 AudioBufferList* io_data, | 219 AudioBufferList* io_data, |
213 const AudioTimeStamp* output_time_stamp) { | 220 const AudioTimeStamp* output_time_stamp) { |
221 if (stopped_) | |
no longer working on chromium
2012/03/27 11:01:32
I don't use atomic operation here since there is c
Chris Rogers
2012/03/28 18:55:10
Strictly speaking, I don't think it's even necessa
no longer working on chromium
2012/03/28 19:25:32
True, thanks for pointing out. It just feels safer
| |
222 return -1; | |
223 | |
214 // Update the playout latency. | 224 // Update the playout latency. |
215 double playout_latency_frames = GetPlayoutLatency(output_time_stamp); | 225 double playout_latency_frames = GetPlayoutLatency(output_time_stamp); |
216 | 226 |
217 AudioBuffer& buffer = io_data->mBuffers[0]; | 227 AudioBuffer& buffer = io_data->mBuffers[0]; |
218 uint8* audio_data = reinterpret_cast<uint8*>(buffer.mData); | 228 uint8* audio_data = reinterpret_cast<uint8*>(buffer.mData); |
219 uint32 hardware_pending_bytes = static_cast<uint32> | 229 uint32 hardware_pending_bytes = static_cast<uint32> |
220 ((playout_latency_frames + 0.5) * format_.mBytesPerFrame); | 230 ((playout_latency_frames + 0.5) * format_.mBytesPerFrame); |
221 uint32 filled = source_->OnMoreData( | 231 uint32 filled = source_->OnMoreData( |
222 this, audio_data, buffer.mDataByteSize, | 232 this, audio_data, buffer.mDataByteSize, |
223 AudioBuffersState(0, hardware_pending_bytes)); | 233 AudioBuffersState(0, hardware_pending_bytes)); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
337 // Get the delay between the moment getting the callback and the scheduled | 347 // Get the delay between the moment getting the callback and the scheduled |
338 // time stamp that tells when the data is going to be played out. | 348 // time stamp that tells when the data is going to be played out. |
339 UInt64 output_time_ns = AudioConvertHostTimeToNanos( | 349 UInt64 output_time_ns = AudioConvertHostTimeToNanos( |
340 output_time_stamp->mHostTime); | 350 output_time_stamp->mHostTime); |
341 UInt64 now_ns = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()); | 351 UInt64 now_ns = AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()); |
342 double delay_frames = static_cast<double> | 352 double delay_frames = static_cast<double> |
343 (1e-9 * (output_time_ns - now_ns) * format_.mSampleRate); | 353 (1e-9 * (output_time_ns - now_ns) * format_.mSampleRate); |
344 | 354 |
345 return (delay_frames + hardware_latency_frames_); | 355 return (delay_frames + hardware_latency_frames_); |
346 } | 356 } |
OLD | NEW |