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/audio_input_device.h" | 5 #include "media/audio/audio_input_device.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
(...skipping 19 matching lines...) Expand all Loading... | |
30 // Called whenever we receive notifications about pending data. | 30 // Called whenever we receive notifications about pending data. |
31 virtual void Process(int pending_data) OVERRIDE; | 31 virtual void Process(int pending_data) OVERRIDE; |
32 | 32 |
33 private: | 33 private: |
34 CaptureCallback* capture_callback_; | 34 CaptureCallback* capture_callback_; |
35 scoped_ptr<AudioBus> audio_bus_; | 35 scoped_ptr<AudioBus> audio_bus_; |
36 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); | 36 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); |
37 }; | 37 }; |
38 | 38 |
39 AudioInputDevice::AudioInputDevice( | 39 AudioInputDevice::AudioInputDevice( |
40 AudioInputIPC* ipc, | 40 scoped_ptr<AudioInputIPC> ipc, |
41 const scoped_refptr<base::MessageLoopProxy>& io_loop) | 41 const scoped_refptr<base::MessageLoopProxy>& io_loop) |
42 : ScopedLoopObserver(io_loop), | 42 : ScopedLoopObserver(io_loop), |
43 callback_(NULL), | 43 callback_(NULL), |
44 event_handler_(NULL), | 44 event_handler_(NULL), |
45 ipc_(ipc), | 45 ipc_(ipc.Pass()), |
46 stream_id_(0), | 46 state_(IDLE), |
47 session_id_(0), | 47 session_id_(0), |
48 pending_device_ready_(false), | 48 agc_is_enabled_(false), |
49 agc_is_enabled_(false) { | 49 stopping_hack_(false) { |
50 CHECK(ipc_); | 50 CHECK(ipc_); |
51 } | 51 } |
52 | 52 |
53 void AudioInputDevice::Initialize(const AudioParameters& params, | 53 void AudioInputDevice::Initialize(const AudioParameters& params, |
54 CaptureCallback* callback, | 54 CaptureCallback* callback, |
55 CaptureEventHandler* event_handler) { | 55 CaptureEventHandler* event_handler) { |
56 DCHECK(!callback_); | 56 DCHECK(!callback_); |
57 DCHECK(!event_handler_); | 57 DCHECK(!event_handler_); |
58 DCHECK(params.IsValid()); | |
58 audio_parameters_ = params; | 59 audio_parameters_ = params; |
59 callback_ = callback; | 60 callback_ = callback; |
60 event_handler_ = event_handler; | 61 event_handler_ = event_handler; |
61 } | 62 } |
62 | 63 |
63 void AudioInputDevice::SetDevice(int session_id) { | 64 void AudioInputDevice::SetDevice(int session_id) { |
64 DVLOG(1) << "SetDevice (session_id=" << session_id << ")"; | 65 DVLOG(1) << "SetDevice (session_id=" << session_id << ")"; |
65 message_loop()->PostTask(FROM_HERE, | 66 message_loop()->PostTask(FROM_HERE, |
66 base::Bind(&AudioInputDevice::SetSessionIdOnIOThread, this, session_id)); | 67 base::Bind(&AudioInputDevice::SetSessionIdOnIOThread, this, session_id)); |
67 } | 68 } |
68 | 69 |
69 void AudioInputDevice::Start() { | 70 void AudioInputDevice::Start() { |
71 DCHECK(callback_) << "Initialize hasn't been called"; | |
70 DVLOG(1) << "Start()"; | 72 DVLOG(1) << "Start()"; |
71 message_loop()->PostTask(FROM_HERE, | 73 message_loop()->PostTask(FROM_HERE, |
72 base::Bind(&AudioInputDevice::InitializeOnIOThread, this)); | 74 base::Bind(&AudioInputDevice::StartUpOnIOThread, this)); |
73 } | 75 } |
74 | 76 |
75 void AudioInputDevice::Stop() { | 77 void AudioInputDevice::Stop() { |
76 DVLOG(1) << "Stop()"; | 78 DVLOG(1) << "Stop()"; |
77 | 79 |
78 { | 80 { |
79 base::AutoLock auto_lock(audio_thread_lock_); | 81 base::AutoLock auto_lock(audio_thread_lock_); |
80 audio_thread_.Stop(MessageLoop::current()); | 82 audio_thread_.Stop(MessageLoop::current()); |
83 stopping_hack_ = true; | |
81 } | 84 } |
82 | 85 |
83 message_loop()->PostTask(FROM_HERE, | 86 message_loop()->PostTask(FROM_HERE, |
84 base::Bind(&AudioInputDevice::ShutDownOnIOThread, this)); | 87 base::Bind(&AudioInputDevice::ShutDownOnIOThread, this)); |
85 } | 88 } |
86 | 89 |
87 void AudioInputDevice::SetVolume(double volume) { | 90 void AudioInputDevice::SetVolume(double volume) { |
88 if (volume < 0 || volume > 1.0) { | 91 if (volume < 0 || volume > 1.0) { |
89 DLOG(ERROR) << "Invalid volume value specified"; | 92 DLOG(ERROR) << "Invalid volume value specified"; |
90 return; | 93 return; |
(...skipping 15 matching lines...) Expand all Loading... | |
106 base::SyncSocket::Handle socket_handle, | 109 base::SyncSocket::Handle socket_handle, |
107 int length) { | 110 int length) { |
108 DCHECK(message_loop()->BelongsToCurrentThread()); | 111 DCHECK(message_loop()->BelongsToCurrentThread()); |
109 #if defined(OS_WIN) | 112 #if defined(OS_WIN) |
110 DCHECK(handle); | 113 DCHECK(handle); |
111 DCHECK(socket_handle); | 114 DCHECK(socket_handle); |
112 #else | 115 #else |
113 DCHECK_GE(handle.fd, 0); | 116 DCHECK_GE(handle.fd, 0); |
114 DCHECK_GE(socket_handle, 0); | 117 DCHECK_GE(socket_handle, 0); |
115 #endif | 118 #endif |
116 DCHECK(length); | 119 DCHECK_LT(0, length); |
DaleCurtis
2013/03/05 23:29:54
/fist-shake.
miu
2013/03/06 22:36:52
With a cabasa, maybe?
| |
117 DVLOG(1) << "OnStreamCreated (stream_id=" << stream_id_ << ")"; | |
118 | 120 |
119 // We should only get this callback if stream_id_ is valid. If it is not, | 121 if (state_ != CREATING_STREAM) |
120 // the IPC layer should have closed the shared memory and socket handles | 122 return; |
121 // for us and not invoked the callback. The basic assertion is that when | |
122 // stream_id_ is 0 the AudioInputDevice instance is not registered as a | |
123 // delegate and hence it should not receive callbacks. | |
124 DCHECK(stream_id_); | |
125 | 123 |
126 base::AutoLock auto_lock(audio_thread_lock_); | 124 base::AutoLock auto_lock(audio_thread_lock_); |
125 // TODO(miu): See TODO in OnStreamCreated method for AudioOutputDevice. Same | |
126 // shit applies here. | |
palmer
2013/03/05 21:09:32
This would be the first naughty word I've seen in
DaleCurtis
2013/03/05 23:29:54
Damn straight.
miu
2013/03/06 22:36:52
Whoops. I meant to, um, *elaborate* on this. ;-)
| |
127 if (stopping_hack_) | |
128 return; | |
127 | 129 |
128 DCHECK(audio_thread_.IsStopped()); | 130 DCHECK(audio_thread_.IsStopped()); |
129 audio_callback_.reset( | 131 audio_callback_.reset( |
130 new AudioInputDevice::AudioThreadCallback(audio_parameters_, handle, | 132 new AudioInputDevice::AudioThreadCallback(audio_parameters_, handle, |
131 length, callback_)); | 133 length, callback_)); |
132 audio_thread_.Start(audio_callback_.get(), socket_handle, "AudioInputDevice"); | 134 audio_thread_.Start(audio_callback_.get(), socket_handle, "AudioInputDevice"); |
133 | 135 |
134 MessageLoop::current()->PostTask(FROM_HERE, | 136 state_ = RECORDING; |
135 base::Bind(&AudioInputDevice::StartOnIOThread, this)); | 137 ipc_->RecordStream(); |
136 } | 138 } |
137 | 139 |
138 void AudioInputDevice::OnVolume(double volume) { | 140 void AudioInputDevice::OnVolume(double volume) { |
139 NOTIMPLEMENTED(); | 141 NOTIMPLEMENTED(); |
140 } | 142 } |
141 | 143 |
142 void AudioInputDevice::OnStateChanged( | 144 void AudioInputDevice::OnStateChanged( |
143 AudioInputIPCDelegate::State state) { | 145 AudioInputIPCDelegate::State state) { |
144 DCHECK(message_loop()->BelongsToCurrentThread()); | 146 DCHECK(message_loop()->BelongsToCurrentThread()); |
145 | 147 |
146 // Do nothing if the stream has been closed. | 148 // Do nothing if the stream has been closed. |
147 if (!stream_id_) | 149 if (state_ < STARTING_DEVICE) |
148 return; | 150 return; |
149 | 151 |
150 switch (state) { | 152 switch (state) { |
151 case AudioInputIPCDelegate::kStopped: | 153 case AudioInputIPCDelegate::kStopped: |
152 // TODO(xians): Should we just call ShutDownOnIOThread here instead? | |
153 ipc_->RemoveDelegate(stream_id_); | |
154 | |
155 audio_thread_.Stop(MessageLoop::current()); | |
156 audio_callback_.reset(); | |
157 | |
158 if (event_handler_) | 154 if (event_handler_) |
159 event_handler_->OnDeviceStopped(); | 155 event_handler_->OnDeviceStopped(); |
160 | 156 ShutDownOnIOThread(); |
161 stream_id_ = 0; | |
162 pending_device_ready_ = false; | |
163 break; | 157 break; |
164 case AudioInputIPCDelegate::kRecording: | 158 case AudioInputIPCDelegate::kRecording: |
165 NOTIMPLEMENTED(); | 159 NOTIMPLEMENTED(); |
166 break; | 160 break; |
167 case AudioInputIPCDelegate::kError: | 161 case AudioInputIPCDelegate::kError: |
no longer working on chromium
2013/03/05 18:33:10
What are we expected to do here when getting a err
miu
2013/03/06 19:25:41
Hmm...Yeah, these "OnStateChanged" IPCs (both inpu
| |
168 DLOG(WARNING) << "AudioInputDevice::OnStateChanged(kError)"; | 162 DLOG(WARNING) << "AudioInputDevice::OnStateChanged(kError)"; |
169 // Don't dereference the callback object if the audio thread | 163 // Don't dereference the callback object if the audio thread |
170 // is stopped or stopping. That could mean that the callback | 164 // is stopped or stopping. That could mean that the callback |
171 // object has been deleted. | 165 // object has been deleted. |
172 // TODO(tommi): Add an explicit contract for clearing the callback | 166 // TODO(tommi): Add an explicit contract for clearing the callback |
173 // object. Possibly require calling Initialize again or provide | 167 // object. Possibly require calling Initialize again or provide |
174 // a callback object via Start() and clear it in Stop(). | 168 // a callback object via Start() and clear it in Stop(). |
175 if (!audio_thread_.IsStopped()) | 169 if (!audio_thread_.IsStopped()) |
176 callback_->OnCaptureError(); | 170 callback_->OnCaptureError(); |
177 break; | 171 break; |
178 default: | 172 default: |
179 NOTREACHED(); | 173 NOTREACHED(); |
180 break; | 174 break; |
181 } | 175 } |
182 } | 176 } |
183 | 177 |
184 void AudioInputDevice::OnDeviceReady(const std::string& device_id) { | 178 void AudioInputDevice::OnDeviceReady(const std::string& device_id) { |
185 DCHECK(message_loop()->BelongsToCurrentThread()); | 179 DCHECK(message_loop()->BelongsToCurrentThread()); |
186 DVLOG(1) << "OnDeviceReady (device_id=" << device_id << ")"; | 180 DVLOG(1) << "OnDeviceReady (device_id=" << device_id << ")"; |
187 | 181 |
188 // Takes care of the case when Stop() is called before OnDeviceReady(). | 182 // Takes care of the case when Stop() is called before OnDeviceReady(). |
189 if (!pending_device_ready_) | 183 if (state_ != STARTING_DEVICE) |
190 return; | 184 return; |
191 | 185 |
192 // If AudioInputDeviceManager returns an empty string, it means no device | 186 // If AudioInputDeviceManager returns an empty string, it means no device |
193 // is ready for start. | 187 // is ready for start. |
194 if (device_id.empty()) { | 188 if (device_id.empty()) { |
195 ipc_->RemoveDelegate(stream_id_); | 189 ipc_->CloseStream(); |
196 stream_id_ = 0; | 190 state_ = IDLE; |
197 } else { | 191 } else { |
198 ipc_->CreateStream(stream_id_, audio_parameters_, device_id, | 192 state_ = CREATING_STREAM; |
199 agc_is_enabled_); | 193 ipc_->CreateStream(this, audio_parameters_, device_id, agc_is_enabled_); |
200 } | 194 } |
201 | 195 |
202 pending_device_ready_ = false; | |
203 // Notify the client that the device has been started. | 196 // Notify the client that the device has been started. |
204 if (event_handler_) | 197 if (event_handler_) |
205 event_handler_->OnDeviceStarted(device_id); | 198 event_handler_->OnDeviceStarted(device_id); |
206 } | 199 } |
207 | 200 |
208 void AudioInputDevice::OnIPCClosed() { | 201 void AudioInputDevice::OnIPCClosed() { |
209 ipc_ = NULL; | 202 DCHECK(message_loop()->BelongsToCurrentThread()); |
203 state_ = IPC_CLOSED; | |
no longer working on chromium
2013/03/05 18:33:10
why do we need this IPC_CLOSED state? Can we simpl
miu
2013/03/06 19:25:41
See header file comments for the enum. IDLE indic
| |
204 ipc_.reset(); | |
210 } | 205 } |
211 | 206 |
212 AudioInputDevice::~AudioInputDevice() { | 207 AudioInputDevice::~AudioInputDevice() { |
213 // TODO(henrika): The current design requires that the user calls | 208 // TODO(henrika): The current design requires that the user calls |
214 // Stop before deleting this class. | 209 // Stop before deleting this class. |
215 CHECK_EQ(0, stream_id_); | 210 DCHECK(audio_thread_.IsStopped()); |
216 } | 211 } |
217 | 212 |
218 void AudioInputDevice::InitializeOnIOThread() { | 213 void AudioInputDevice::StartUpOnIOThread() { |
219 DCHECK(message_loop()->BelongsToCurrentThread()); | 214 DCHECK(message_loop()->BelongsToCurrentThread()); |
215 | |
220 // Make sure we don't call Start() more than once. | 216 // Make sure we don't call Start() more than once. |
221 DCHECK_EQ(0, stream_id_); | 217 if (state_ != IDLE) |
222 if (stream_id_) | |
223 return; | 218 return; |
224 | 219 |
225 stream_id_ = ipc_->AddDelegate(this); | 220 // If |session_id_| is not specified, directly create the stream. Otherwise, |
226 // If |session_id_| is not specified, it will directly create the stream; | 221 // send a AudioInputHostMsg_StartDevice msg to the browser and create the |
227 // otherwise it will send a AudioInputHostMsg_StartDevice msg to the browser | 222 // stream after receiving a reply via the OnDeviceReady() callback. |
228 // and create the stream when getting a OnDeviceReady() callback. | |
229 if (!session_id_) { | 223 if (!session_id_) { |
230 ipc_->CreateStream(stream_id_, audio_parameters_, | 224 state_ = CREATING_STREAM; |
231 AudioManagerBase::kDefaultDeviceId, agc_is_enabled_); | 225 ipc_->CreateStream(this, audio_parameters_, |
226 AudioManagerBase::kDefaultDeviceId, agc_is_enabled_); | |
232 } else { | 227 } else { |
233 ipc_->StartDevice(stream_id_, session_id_); | 228 state_ = STARTING_DEVICE; |
234 pending_device_ready_ = true; | 229 ipc_->StartDevice(this, session_id_); |
235 } | 230 } |
236 } | 231 } |
237 | 232 |
238 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { | 233 void AudioInputDevice::SetSessionIdOnIOThread(int session_id) { |
239 DCHECK(message_loop()->BelongsToCurrentThread()); | 234 DCHECK(message_loop()->BelongsToCurrentThread()); |
235 DCHECK_EQ(IDLE, state_); | |
240 session_id_ = session_id; | 236 session_id_ = session_id; |
241 } | 237 } |
242 | 238 |
243 void AudioInputDevice::StartOnIOThread() { | |
244 DCHECK(message_loop()->BelongsToCurrentThread()); | |
245 if (stream_id_) | |
246 ipc_->RecordStream(stream_id_); | |
247 } | |
248 | |
249 void AudioInputDevice::ShutDownOnIOThread() { | 239 void AudioInputDevice::ShutDownOnIOThread() { |
250 DCHECK(message_loop()->BelongsToCurrentThread()); | 240 DCHECK(message_loop()->BelongsToCurrentThread()); |
251 // NOTE: |completion| may be NULL. | |
252 // Make sure we don't call shutdown more than once. | |
253 if (stream_id_) { | |
254 if (ipc_) { | |
255 ipc_->CloseStream(stream_id_); | |
256 ipc_->RemoveDelegate(stream_id_); | |
257 } | |
258 | 241 |
259 stream_id_ = 0; | 242 // Close the stream, if we haven't already. |
243 if (state_ >= STARTING_DEVICE) { | |
244 ipc_->CloseStream(); | |
245 state_ = IDLE; | |
260 session_id_ = 0; | 246 session_id_ = 0; |
261 pending_device_ready_ = false; | |
262 agc_is_enabled_ = false; | 247 agc_is_enabled_ = false; |
263 } | 248 } |
264 | 249 |
265 // We can run into an issue where ShutDownOnIOThread is called right after | 250 // We can run into an issue where ShutDownOnIOThread is called right after |
266 // OnStreamCreated is called in cases where Start/Stop are called before we | 251 // OnStreamCreated is called in cases where Start/Stop are called before we |
267 // get the OnStreamCreated callback. To handle that corner case, we call | 252 // get the OnStreamCreated callback. To handle that corner case, we call |
268 // Stop(). In most cases, the thread will already be stopped. | 253 // Stop(). In most cases, the thread will already be stopped. |
254 // | |
269 // Another situation is when the IO thread goes away before Stop() is called | 255 // Another situation is when the IO thread goes away before Stop() is called |
270 // in which case, we cannot use the message loop to close the thread handle | 256 // in which case, we cannot use the message loop to close the thread handle |
271 // and can't not rely on the main thread existing either. | 257 // and can't not rely on the main thread existing either. |
258 base::AutoLock auto_lock_(audio_thread_lock_); | |
272 base::ThreadRestrictions::ScopedAllowIO allow_io; | 259 base::ThreadRestrictions::ScopedAllowIO allow_io; |
273 audio_thread_.Stop(NULL); | 260 audio_thread_.Stop(NULL); |
274 audio_callback_.reset(); | 261 audio_callback_.reset(); |
262 stopping_hack_ = false; | |
275 } | 263 } |
276 | 264 |
277 void AudioInputDevice::SetVolumeOnIOThread(double volume) { | 265 void AudioInputDevice::SetVolumeOnIOThread(double volume) { |
278 DCHECK(message_loop()->BelongsToCurrentThread()); | 266 DCHECK(message_loop()->BelongsToCurrentThread()); |
279 if (stream_id_) | 267 if (state_ >= CREATING_STREAM) |
280 ipc_->SetVolume(stream_id_, volume); | 268 ipc_->SetVolume(volume); |
281 } | 269 } |
282 | 270 |
283 void AudioInputDevice::SetAutomaticGainControlOnIOThread(bool enabled) { | 271 void AudioInputDevice::SetAutomaticGainControlOnIOThread(bool enabled) { |
284 DCHECK(message_loop()->BelongsToCurrentThread()); | 272 DCHECK(message_loop()->BelongsToCurrentThread()); |
285 DCHECK_EQ(0, stream_id_) << | 273 |
286 "The AGC state can not be modified while capturing is active."; | 274 if (state_ >= STARTING_DEVICE) { |
287 if (stream_id_) | 275 DLOG(WARNING) << "The AGC state can not be modified after starting."; |
288 return; | 276 return; |
277 } | |
289 | 278 |
290 // We simply store the new AGC setting here. This value will be used when | 279 // We simply store the new AGC setting here. This value will be used when |
291 // a new stream is initialized and by GetAutomaticGainControl(). | 280 // a new stream is initialized and by GetAutomaticGainControl(). |
292 agc_is_enabled_ = enabled; | 281 agc_is_enabled_ = enabled; |
293 } | 282 } |
294 | 283 |
295 void AudioInputDevice::WillDestroyCurrentMessageLoop() { | 284 void AudioInputDevice::WillDestroyCurrentMessageLoop() { |
296 LOG(ERROR) << "IO loop going away before the input device has been stopped"; | 285 LOG(ERROR) << "IO loop going away before the input device has been stopped"; |
297 ShutDownOnIOThread(); | 286 ShutDownOnIOThread(); |
298 } | 287 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
333 // with nominal range -1.0 -> +1.0. | 322 // with nominal range -1.0 -> +1.0. |
334 audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample); | 323 audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample); |
335 | 324 |
336 // Deliver captured data to the client in floating point format | 325 // Deliver captured data to the client in floating point format |
337 // and update the audio-delay measurement. | 326 // and update the audio-delay measurement. |
338 capture_callback_->Capture(audio_bus_.get(), | 327 capture_callback_->Capture(audio_bus_.get(), |
339 audio_delay_milliseconds, volume); | 328 audio_delay_milliseconds, volume); |
340 } | 329 } |
341 | 330 |
342 } // namespace media | 331 } // namespace media |
OLD | NEW |