| 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::kInvalidDeviceUId[] = ""; |
| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 0(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, kInvalidDeviceUId); |
| 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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 system and find out |
| 207 // the index of the device. | 207 // the index of the device. |
| 208 int device_index = kInvalidDevice; | 208 std::string device_uid = kInvalidDeviceUId; |
| 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_uid = iter->unique_id; |
| 222 break; | 222 break; |
| 223 } | 223 } |
| 224 } | 224 } |
| 225 } | 225 } |
| 226 } | 226 } |
| 227 // Posts the index to AudioInputRenderHost through the event handler. | 227 // Posts the index to AudioInputRenderHost through the event handler. |
| 228 BrowserThread::PostTask(BrowserThread::IO, | 228 BrowserThread::PostTask(BrowserThread::IO, |
| 229 FROM_HERE, | 229 FROM_HERE, |
| 230 base::Bind( | 230 base::Bind( |
| 231 &AudioInputDeviceManager::StartedOnIOThread, | 231 &AudioInputDeviceManager::StartedOnIOThread, |
| 232 base::Unretained(this), | 232 base::Unretained(this), |
| 233 session_id, | 233 session_id, |
| 234 device_index)); | 234 device_uid)); |
| 235 } | 235 } |
| 236 | 236 |
| 237 void AudioInputDeviceManager::StopOnDeviceThread(int session_id) { | 237 void AudioInputDeviceManager::StopOnDeviceThread(int session_id) { |
| 238 DCHECK(IsOnCaptureDeviceThread()); | 238 DCHECK(IsOnCaptureDeviceThread()); |
| 239 BrowserThread::PostTask(BrowserThread::IO, | 239 BrowserThread::PostTask(BrowserThread::IO, |
| 240 FROM_HERE, | 240 FROM_HERE, |
| 241 base::Bind( | 241 base::Bind( |
| 242 &AudioInputDeviceManager::StoppedOnIOThread, | 242 &AudioInputDeviceManager::StoppedOnIOThread, |
| 243 base::Unretained(this), | 243 base::Unretained(this), |
| 244 session_id)); | 244 session_id)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 269 } | 269 } |
| 270 | 270 |
| 271 | 271 |
| 272 void AudioInputDeviceManager::ErrorOnIOThread(int session_id, | 272 void AudioInputDeviceManager::ErrorOnIOThread(int session_id, |
| 273 MediaStreamProviderError error) { | 273 MediaStreamProviderError error) { |
| 274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 275 if (listener_) | 275 if (listener_) |
| 276 listener_->Error(kAudioCapture, session_id, error); | 276 listener_->Error(kAudioCapture, session_id, error); |
| 277 } | 277 } |
| 278 | 278 |
| 279 void AudioInputDeviceManager::StartedOnIOThread(int session_id, int index) { | 279 void AudioInputDeviceManager::StartedOnIOThread( |
| 280 int session_id, const std::string& device_uid) { |
| 280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 281 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 281 | 282 |
| 282 EventHandlerMap::iterator it = event_handlers_.find(session_id); | 283 EventHandlerMap::iterator it = event_handlers_.find(session_id); |
| 283 if (it == event_handlers_.end()) | 284 if (it == event_handlers_.end()) |
| 284 return; | 285 return; |
| 285 | 286 |
| 286 // Post a callback through the event handler to create an audio stream. | 287 // Post a callback through the event handler to create an audio stream. |
| 287 it->second->OnDeviceStarted(session_id, index); | 288 it->second->OnDeviceStarted(session_id, device_uid); |
| 288 } | 289 } |
| 289 | 290 |
| 290 void AudioInputDeviceManager::StoppedOnIOThread(int session_id) { | 291 void AudioInputDeviceManager::StoppedOnIOThread(int session_id) { |
| 291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 292 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 292 // Erase the event handler referenced by the session_id. | 293 // Erase the event handler referenced by the session_id. |
| 293 event_handlers_.erase(session_id); | 294 event_handlers_.erase(session_id); |
| 294 } | 295 } |
| 295 | 296 |
| 296 void AudioInputDeviceManager::SignalError(int session_id, | 297 void AudioInputDeviceManager::SignalError(int session_id, |
| 297 MediaStreamProviderError error) { | 298 MediaStreamProviderError error) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 316 bool AudioInputDeviceManager::HasEventHandler(int session_id) { | 317 bool AudioInputDeviceManager::HasEventHandler(int session_id) { |
| 317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 318 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 318 return event_handlers_.find(session_id) != event_handlers_.end(); | 319 return event_handlers_.find(session_id) != event_handlers_.end(); |
| 319 } | 320 } |
| 320 | 321 |
| 321 MessageLoop* AudioInputDeviceManager::message_loop() { | 322 MessageLoop* AudioInputDeviceManager::message_loop() { |
| 322 return audio_input_device_thread_.message_loop(); | 323 return audio_input_device_thread_.message_loop(); |
| 323 } | 324 } |
| 324 | 325 |
| 325 } // namespace media_stream | 326 } // namespace media_stream |
| OLD | NEW |