OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_input_mac.h" | 5 #include "media/audio/mac/audio_input_mac.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "media/audio/audio_util.h" | 9 #include "media/audio/audio_util.h" |
10 #include "media/audio/mac/audio_manager_mac.h" | 10 #include "media/audio/mac/audio_manager_mac.h" |
11 | 11 |
12 #if !defined(MAC_OS_X_VERSION_10_6) || \ | 12 #if !defined(MAC_OS_X_VERSION_10_6) || \ |
13 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 | 13 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6 |
14 enum { | 14 enum { |
15 kAudioQueueErr_EnqueueDuringReset = -66632 | 15 kAudioQueueErr_EnqueueDuringReset = -66632 |
16 }; | 16 }; |
17 #endif | 17 #endif |
18 | 18 |
19 PCMQueueInAudioInputStream::PCMQueueInAudioInputStream( | 19 PCMQueueInAudioInputStream::PCMQueueInAudioInputStream( |
20 AudioManagerMac* manager, const AudioParameters& params) | 20 AudioManagerMac* manager, const AudioParameters& params) |
21 : manager_(manager), | 21 : manager_(manager), |
22 callback_(NULL), | 22 callback_(NULL), |
23 audio_queue_(NULL), | 23 audio_queue_(NULL), |
24 buffer_size_bytes_(0) { | 24 buffer_size_bytes_(0), |
| 25 started_(false) { |
25 // We must have a manager. | 26 // We must have a manager. |
26 DCHECK(manager_); | 27 DCHECK(manager_); |
27 // A frame is one sample across all channels. In interleaved audio the per | 28 // A frame is one sample across all channels. In interleaved audio the per |
28 // frame fields identify the set of n |channels|. In uncompressed audio, a | 29 // frame fields identify the set of n |channels|. In uncompressed audio, a |
29 // packet is always one frame. | 30 // packet is always one frame. |
30 format_.mSampleRate = params.sample_rate; | 31 format_.mSampleRate = params.sample_rate; |
31 format_.mFormatID = kAudioFormatLinearPCM; | 32 format_.mFormatID = kAudioFormatLinearPCM; |
32 format_.mFormatFlags = kLinearPCMFormatFlagIsPacked | | 33 format_.mFormatFlags = kLinearPCMFormatFlagIsPacked | |
33 kLinearPCMFormatFlagIsSignedInteger; | 34 kLinearPCMFormatFlagIsSignedInteger; |
34 format_.mBitsPerChannel = params.bits_per_sample; | 35 format_.mBitsPerChannel = params.bits_per_sample; |
(...skipping 26 matching lines...) Expand all Loading... |
61 return SetupBuffers(); | 62 return SetupBuffers(); |
62 } | 63 } |
63 | 64 |
64 void PCMQueueInAudioInputStream::Start(AudioInputCallback* callback) { | 65 void PCMQueueInAudioInputStream::Start(AudioInputCallback* callback) { |
65 DCHECK(callback); | 66 DCHECK(callback); |
66 DLOG_IF(ERROR, !audio_queue_) << "Open() has not been called successfully"; | 67 DLOG_IF(ERROR, !audio_queue_) << "Open() has not been called successfully"; |
67 if (callback_ || !audio_queue_) | 68 if (callback_ || !audio_queue_) |
68 return; | 69 return; |
69 callback_ = callback; | 70 callback_ = callback; |
70 OSStatus err = AudioQueueStart(audio_queue_, NULL); | 71 OSStatus err = AudioQueueStart(audio_queue_, NULL); |
71 if (err != noErr) | 72 if (err != noErr) { |
72 HandleError(err); | 73 HandleError(err); |
73 else | 74 } else { |
| 75 started_ = true; |
74 manager_->IncreaseActiveInputStreamCount(); | 76 manager_->IncreaseActiveInputStreamCount(); |
| 77 } |
75 } | 78 } |
76 | 79 |
77 void PCMQueueInAudioInputStream::Stop() { | 80 void PCMQueueInAudioInputStream::Stop() { |
78 if (!audio_queue_) | 81 if (!audio_queue_ || !started_) |
79 return; | 82 return; |
| 83 |
80 // Stop is always called before Close. In case of error, this will be | 84 // Stop is always called before Close. In case of error, this will be |
81 // also called when closing the input controller. | 85 // also called when closing the input controller. |
82 manager_->DecreaseActiveInputStreamCount(); | 86 manager_->DecreaseActiveInputStreamCount(); |
83 | 87 |
84 // We request a synchronous stop, so the next call can take some time. In | 88 // We request a synchronous stop, so the next call can take some time. In |
85 // the windows implementation we block here as well. | 89 // the windows implementation we block here as well. |
86 OSStatus err = AudioQueueStop(audio_queue_, true); | 90 OSStatus err = AudioQueueStop(audio_queue_, true); |
87 if (err != noErr) | 91 if (err != noErr) |
88 HandleError(err); | 92 HandleError(err); |
| 93 |
| 94 started_ = false; |
89 } | 95 } |
90 | 96 |
91 void PCMQueueInAudioInputStream::Close() { | 97 void PCMQueueInAudioInputStream::Close() { |
92 // It is valid to call Close() before calling Open() or Start(), thus | 98 // It is valid to call Close() before calling Open() or Start(), thus |
93 // |audio_queue_| and |callback_| might be NULL. | 99 // |audio_queue_| and |callback_| might be NULL. |
94 if (audio_queue_) { | 100 if (audio_queue_) { |
95 OSStatus err = AudioQueueDispose(audio_queue_, true); | 101 OSStatus err = AudioQueueDispose(audio_queue_, true); |
96 audio_queue_ = NULL; | 102 audio_queue_ = NULL; |
97 if (err != noErr) | 103 if (err != noErr) |
98 HandleError(err); | 104 HandleError(err); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an | 183 // TODO(joth): PCMQueueOutAudioOutputStream uses callback_ to provide an |
178 // extra guard for this situation, but it seems to introduce more | 184 // extra guard for this situation, but it seems to introduce more |
179 // complications than it solves (memory barrier issues accessing it from | 185 // complications than it solves (memory barrier issues accessing it from |
180 // multiple threads, looses the means to indicate OnClosed to client). | 186 // multiple threads, looses the means to indicate OnClosed to client). |
181 // Should determine if we need to do something equivalent here. | 187 // Should determine if we need to do something equivalent here. |
182 return; | 188 return; |
183 } | 189 } |
184 HandleError(err); | 190 HandleError(err); |
185 } | 191 } |
186 } | 192 } |
OLD | NEW |