| 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 "content/browser/renderer_host/media/audio_input_device_manager.h" | 5 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "content/browser/renderer_host/media/audio_input_device_manager_event_h
andler.h" | 9 #include "content/browser/renderer_host/media/audio_input_device_manager_event_h
andler.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "media/audio/audio_manager.h" | 11 #include "media/audio/audio_manager_base.h" |
| 12 | 12 |
| 13 using content::BrowserThread; | 13 using content::BrowserThread; |
| 14 | 14 |
| 15 namespace media_stream { | 15 namespace media_stream { |
| 16 | 16 |
| 17 const int AudioInputDeviceManager::kFakeOpenSessionId = 1; | 17 const int AudioInputDeviceManager::kFakeOpenSessionId = 1; |
| 18 const int AudioInputDeviceManager::kInvalidSessionId = 0; | 18 const int AudioInputDeviceManager::kInvalidSessionId = 0; |
| 19 const int AudioInputDeviceManager::kInvalidDevice = -1; | 19 const char AudioInputDeviceManager::kInvalidDeviceId[] = ""; |
| 20 const int AudioInputDeviceManager::kDefaultDeviceIndex = 0; | |
| 21 | 20 |
| 22 // Starting id for the first capture session. | 21 // Starting id for the first capture session. |
| 23 const int kFirstSessionId = AudioInputDeviceManager::kFakeOpenSessionId + 1; | 22 const int kFirstSessionId = AudioInputDeviceManager::kFakeOpenSessionId + 1; |
| 24 | 23 |
| 25 // Helper function. | 24 // Helper function. |
| 26 static bool IsValidAudioInputDevice(const media::AudioDeviceName& device) { | 25 static bool IsValidAudioInputDevice(const media::AudioDeviceName& device) { |
| 27 AudioManager* audio_manager = AudioManager::GetAudioManager(); | 26 AudioManager* audio_manager = AudioManager::GetAudioManager(); |
| 28 if (!audio_manager) | 27 if (!audio_manager) |
| 29 return false; | 28 return false; |
| 30 | 29 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 base::Bind(&AudioInputDeviceManager::CloseOnDeviceThread, | 99 base::Bind(&AudioInputDeviceManager::CloseOnDeviceThread, |
| 101 base::Unretained(this), session_id)); | 100 base::Unretained(this), session_id)); |
| 102 } | 101 } |
| 103 | 102 |
| 104 void AudioInputDeviceManager::Start( | 103 void AudioInputDeviceManager::Start( |
| 105 int session_id, AudioInputDeviceManagerEventHandler* event_handler) { | 104 int session_id, AudioInputDeviceManagerEventHandler* event_handler) { |
| 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 107 DCHECK(event_handler); | 106 DCHECK(event_handler); |
| 108 | 107 |
| 109 // Solution for not using MediaStreamManager. This is needed when Start() is | 108 // Solution for not using MediaStreamManager. This is needed when Start() is |
| 110 // called without using Open(), we post 0(default device) for test purpose. | 109 // called without using Open(), we post default device for test purpose. |
| 111 // And we do not store the info for the kFakeOpenSessionId but return | 110 // And we do not store the info for the kFakeOpenSessionId but return |
| 112 // the callback immediately. | 111 // the callback immediately. |
| 113 if (session_id == kFakeOpenSessionId) { | 112 if (session_id == kFakeOpenSessionId) { |
| 114 event_handler->OnDeviceStarted(session_id, kDefaultDeviceIndex); | 113 event_handler->OnDeviceStarted(session_id, |
| 114 AudioManagerBase::kDefaultDeviceId); |
| 115 return; | 115 return; |
| 116 } | 116 } |
| 117 | 117 |
| 118 // If session has been started, post a callback with an error. | 118 // If session has been started, post a callback with an error. |
| 119 if (event_handlers_.find(session_id) != event_handlers_.end()) { | 119 if (event_handlers_.find(session_id) != event_handlers_.end()) { |
| 120 // Session has been started, post a callback with error. | 120 // Session has been started, post a callback with error. |
| 121 event_handler->OnDeviceStarted(session_id, kInvalidDevice); | 121 event_handler->OnDeviceStarted(session_id, kInvalidDeviceId); |
| 122 return; | 122 return; |
| 123 } | 123 } |
| 124 | 124 |
| 125 // Add the event handler to the session. | 125 // Add the event handler to the session. |
| 126 event_handlers_.insert(std::make_pair(session_id, event_handler)); | 126 event_handlers_.insert(std::make_pair(session_id, event_handler)); |
| 127 | 127 |
| 128 audio_input_device_thread_.message_loop()->PostTask( | 128 audio_input_device_thread_.message_loop()->PostTask( |
| 129 FROM_HERE, | 129 FROM_HERE, |
| 130 base::Bind(&AudioInputDeviceManager::StartOnDeviceThread, | 130 base::Bind(&AudioInputDeviceManager::StartOnDeviceThread, |
| 131 base::Unretained(this), session_id)); | 131 base::Unretained(this), session_id)); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 FROM_HERE, | 196 FROM_HERE, |
| 197 base::Bind( | 197 base::Bind( |
| 198 &AudioInputDeviceManager::ClosedOnIOThread, | 198 &AudioInputDeviceManager::ClosedOnIOThread, |
| 199 base::Unretained(this), | 199 base::Unretained(this), |
| 200 session_id)); | 200 session_id)); |
| 201 } | 201 } |
| 202 | 202 |
| 203 void AudioInputDeviceManager::StartOnDeviceThread(const int session_id) { | 203 void AudioInputDeviceManager::StartOnDeviceThread(const int session_id) { |
| 204 DCHECK(IsOnCaptureDeviceThread()); | 204 DCHECK(IsOnCaptureDeviceThread()); |
| 205 | 205 |
| 206 // Get the up-to-date device enumeration list from the system and find out | 206 // Get the up-to-date device enumeration list from the OS and find out |
| 207 // the index of the device. | 207 // the unique id of the device. |
| 208 int device_index = kInvalidDevice; | 208 std::string device_id = kInvalidDeviceId; |
| 209 AudioInputDeviceMap::const_iterator it = devices_.find(session_id); | 209 AudioInputDeviceMap::const_iterator it = devices_.find(session_id); |
| 210 if (it != devices_.end()) { | 210 if (it != devices_.end()) { |
| 211 media::AudioDeviceNames device_names; | 211 media::AudioDeviceNames device_names; |
| 212 AudioManager::GetAudioManager()->GetAudioInputDeviceNames(&device_names); | 212 AudioManager::GetAudioManager()->GetAudioInputDeviceNames(&device_names); |
| 213 if (!device_names.empty()) { | 213 if (!device_names.empty()) { |
| 214 int index = 0; | 214 int index = 0; |
| 215 for (media::AudioDeviceNames::iterator iter = device_names.begin(); | 215 for (media::AudioDeviceNames::iterator iter = device_names.begin(); |
| 216 iter != device_names.end(); | 216 iter != device_names.end(); |
| 217 ++iter, ++index) { | 217 ++iter, ++index) { |
| 218 if (iter->device_name == it->second.device_name && | 218 if (iter->device_name == it->second.device_name && |
| 219 iter->unique_id == it->second.unique_id) { | 219 iter->unique_id == it->second.unique_id) { |
| 220 // Found the device. | 220 // Found the device. |
| 221 device_index = index; | 221 device_id = iter->unique_id; |
| 222 break; | 222 break; |
| 223 } | 223 } |
| 224 } | 224 } |
| 225 } | 225 } |
| 226 } | 226 } |
| 227 |
| 227 // Posts the index to AudioInputRenderHost through the event handler. | 228 // Posts the index to AudioInputRenderHost through the event handler. |
| 228 BrowserThread::PostTask(BrowserThread::IO, | 229 BrowserThread::PostTask(BrowserThread::IO, |
| 229 FROM_HERE, | 230 FROM_HERE, |
| 230 base::Bind( | 231 base::Bind( |
| 231 &AudioInputDeviceManager::StartedOnIOThread, | 232 &AudioInputDeviceManager::StartedOnIOThread, |
| 232 base::Unretained(this), | 233 base::Unretained(this), |
| 233 session_id, | 234 session_id, |
| 234 device_index)); | 235 device_id)); |
| 235 } | 236 } |
| 236 | 237 |
| 237 void AudioInputDeviceManager::StopOnDeviceThread(int session_id) { | 238 void AudioInputDeviceManager::StopOnDeviceThread(int session_id) { |
| 238 DCHECK(IsOnCaptureDeviceThread()); | 239 DCHECK(IsOnCaptureDeviceThread()); |
| 239 BrowserThread::PostTask(BrowserThread::IO, | 240 BrowserThread::PostTask(BrowserThread::IO, |
| 240 FROM_HERE, | 241 FROM_HERE, |
| 241 base::Bind( | 242 base::Bind( |
| 242 &AudioInputDeviceManager::StoppedOnIOThread, | 243 &AudioInputDeviceManager::StoppedOnIOThread, |
| 243 base::Unretained(this), | 244 base::Unretained(this), |
| 244 session_id)); | 245 session_id)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 269 } | 270 } |
| 270 | 271 |
| 271 | 272 |
| 272 void AudioInputDeviceManager::ErrorOnIOThread(int session_id, | 273 void AudioInputDeviceManager::ErrorOnIOThread(int session_id, |
| 273 MediaStreamProviderError error) { | 274 MediaStreamProviderError error) { |
| 274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 275 if (listener_) | 276 if (listener_) |
| 276 listener_->Error(kAudioCapture, session_id, error); | 277 listener_->Error(kAudioCapture, session_id, error); |
| 277 } | 278 } |
| 278 | 279 |
| 279 void AudioInputDeviceManager::StartedOnIOThread(int session_id, int index) { | 280 void AudioInputDeviceManager::StartedOnIOThread( |
| 281 int session_id, const std::string& device_id) { |
| 280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 281 | 283 |
| 282 EventHandlerMap::iterator it = event_handlers_.find(session_id); | 284 EventHandlerMap::iterator it = event_handlers_.find(session_id); |
| 283 if (it == event_handlers_.end()) | 285 if (it == event_handlers_.end()) |
| 284 return; | 286 return; |
| 285 | 287 |
| 286 // Post a callback through the event handler to create an audio stream. | 288 // Post a callback through the AudioInputRendererHost to notify the renderer |
| 287 it->second->OnDeviceStarted(session_id, index); | 289 // device has been started. |
| 290 it->second->OnDeviceStarted(session_id, device_id); |
| 288 } | 291 } |
| 289 | 292 |
| 290 void AudioInputDeviceManager::StoppedOnIOThread(int session_id) { | 293 void AudioInputDeviceManager::StoppedOnIOThread(int session_id) { |
| 291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 294 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 292 // Erase the event handler referenced by the session_id. | 295 // Erase the event handler referenced by the session_id. |
| 293 event_handlers_.erase(session_id); | 296 event_handlers_.erase(session_id); |
| 294 } | 297 } |
| 295 | 298 |
| 296 void AudioInputDeviceManager::SignalError(int session_id, | 299 void AudioInputDeviceManager::SignalError(int session_id, |
| 297 MediaStreamProviderError error) { | 300 MediaStreamProviderError error) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 316 bool AudioInputDeviceManager::HasEventHandler(int session_id) { | 319 bool AudioInputDeviceManager::HasEventHandler(int session_id) { |
| 317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 318 return event_handlers_.find(session_id) != event_handlers_.end(); | 321 return event_handlers_.find(session_id) != event_handlers_.end(); |
| 319 } | 322 } |
| 320 | 323 |
| 321 MessageLoop* AudioInputDeviceManager::message_loop() { | 324 MessageLoop* AudioInputDeviceManager::message_loop() { |
| 322 return audio_input_device_thread_.message_loop(); | 325 return audio_input_device_thread_.message_loop(); |
| 323 } | 326 } |
| 324 | 327 |
| 325 } // namespace media_stream | 328 } // namespace media_stream |
| OLD | NEW |