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