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_input_mac.h" | 5 #include "media/audio/mac/audio_input_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" |
11 #include "base/mac/mac_logging.h" | 11 #include "base/mac/mac_logging.h" |
12 #include "media/audio/audio_manager_base.h" | 12 #include "media/audio/mac/audio_manager_mac.h" |
13 | |
14 | 13 |
15 namespace media { | 14 namespace media { |
16 | 15 |
17 PCMQueueInAudioInputStream::PCMQueueInAudioInputStream( | 16 PCMQueueInAudioInputStream::PCMQueueInAudioInputStream( |
18 AudioManagerBase* manager, const AudioParameters& params) | 17 AudioManagerMac* manager, const AudioParameters& params) |
19 : manager_(manager), | 18 : manager_(manager), |
20 callback_(NULL), | 19 callback_(NULL), |
21 audio_queue_(NULL), | 20 audio_queue_(NULL), |
22 buffer_size_bytes_(0), | 21 buffer_size_bytes_(0), |
23 started_(false) { | 22 started_(false) { |
24 // We must have a manager. | 23 // We must have a manager. |
25 DCHECK(manager_); | 24 DCHECK(manager_); |
26 // A frame is one sample across all channels. In interleaved audio the per | 25 // A frame is one sample across all channels. In interleaved audio the per |
27 // frame fields identify the set of n |channels|. In uncompressed audio, a | 26 // frame fields identify the set of n |channels|. In uncompressed audio, a |
28 // packet is always one frame. | 27 // packet is always one frame. |
(...skipping 29 matching lines...) Expand all Loading... |
58 return false; | 57 return false; |
59 } | 58 } |
60 return SetupBuffers(); | 59 return SetupBuffers(); |
61 } | 60 } |
62 | 61 |
63 void PCMQueueInAudioInputStream::Start(AudioInputCallback* callback) { | 62 void PCMQueueInAudioInputStream::Start(AudioInputCallback* callback) { |
64 DCHECK(callback); | 63 DCHECK(callback); |
65 DLOG_IF(ERROR, !audio_queue_) << "Open() has not been called successfully"; | 64 DLOG_IF(ERROR, !audio_queue_) << "Open() has not been called successfully"; |
66 if (callback_ || !audio_queue_) | 65 if (callback_ || !audio_queue_) |
67 return; | 66 return; |
| 67 |
| 68 // Check if we should defer Start() for http://crbug.com/160920. |
| 69 if (manager_->ShouldDeferStreamStart()) { |
| 70 // Use a cancellable closure so that if Stop() is called before Start() |
| 71 // actually runs, we can cancel the pending start. |
| 72 DCHECK(deferred_start_cb_.IsCancelled()); |
| 73 deferred_start_cb_.Reset(base::Bind( |
| 74 &PCMQueueInAudioInputStream::Start, base::Unretained(this), callback)); |
| 75 manager_->GetTaskRunner()->PostDelayedTask( |
| 76 FROM_HERE, |
| 77 deferred_start_cb_.callback(), |
| 78 base::TimeDelta::FromSeconds( |
| 79 AudioManagerMac::kStartDelayInSecsForPowerEvents)); |
| 80 return; |
| 81 } |
| 82 |
68 callback_ = callback; | 83 callback_ = callback; |
69 OSStatus err = AudioQueueStart(audio_queue_, NULL); | 84 OSStatus err = AudioQueueStart(audio_queue_, NULL); |
70 if (err != noErr) { | 85 if (err != noErr) { |
71 HandleError(err); | 86 HandleError(err); |
72 } else { | 87 } else { |
73 started_ = true; | 88 started_ = true; |
74 } | 89 } |
75 } | 90 } |
76 | 91 |
77 void PCMQueueInAudioInputStream::Stop() { | 92 void PCMQueueInAudioInputStream::Stop() { |
| 93 deferred_start_cb_.Cancel(); |
78 if (!audio_queue_ || !started_) | 94 if (!audio_queue_ || !started_) |
79 return; | 95 return; |
80 | 96 |
81 // We request a synchronous stop, so the next call can take some time. In | 97 // We request a synchronous stop, so the next call can take some time. In |
82 // the windows implementation we block here as well. | 98 // the windows implementation we block here as well. |
83 OSStatus err = AudioQueueStop(audio_queue_, true); | 99 OSStatus err = AudioQueueStop(audio_queue_, true); |
84 if (err != noErr) | 100 if (err != noErr) |
85 HandleError(err); | 101 HandleError(err); |
86 | 102 |
87 started_ = false; | 103 started_ = false; |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 // complications than it solves (memory barrier issues accessing it from | 236 // complications than it solves (memory barrier issues accessing it from |
221 // multiple threads, looses the means to indicate OnClosed to client). | 237 // multiple threads, looses the means to indicate OnClosed to client). |
222 // Should determine if we need to do something equivalent here. | 238 // Should determine if we need to do something equivalent here. |
223 return; | 239 return; |
224 } | 240 } |
225 HandleError(err); | 241 HandleError(err); |
226 } | 242 } |
227 } | 243 } |
228 | 244 |
229 } // namespace media | 245 } // namespace media |
OLD | NEW |