| 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 "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/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/common/media_stream_request.h" | 10 #include "content/public/common/media_stream_request.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 0); | 38 0); |
| 39 fake_device.session_id = kFakeOpenSessionId; | 39 fake_device.session_id = kFakeOpenSessionId; |
| 40 devices_.push_back(fake_device); | 40 devices_.push_back(fake_device); |
| 41 } | 41 } |
| 42 | 42 |
| 43 AudioInputDeviceManager::~AudioInputDeviceManager() { | 43 AudioInputDeviceManager::~AudioInputDeviceManager() { |
| 44 } | 44 } |
| 45 | 45 |
| 46 const StreamDeviceInfo* AudioInputDeviceManager::GetOpenedDeviceInfoById( | 46 const StreamDeviceInfo* AudioInputDeviceManager::GetOpenedDeviceInfoById( |
| 47 int session_id) { | 47 int session_id) { |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 48 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 49 StreamDeviceList::iterator device = GetDevice(session_id); | 49 StreamDeviceList::iterator device = GetDevice(session_id); |
| 50 if (device == devices_.end()) | 50 if (device == devices_.end()) |
| 51 return NULL; | 51 return NULL; |
| 52 | 52 |
| 53 return &(*device); | 53 return &(*device); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void AudioInputDeviceManager::Register( | 56 void AudioInputDeviceManager::Register( |
| 57 MediaStreamProviderListener* listener, | 57 MediaStreamProviderListener* listener, |
| 58 const scoped_refptr<base::SingleThreadTaskRunner>& device_task_runner) { | 58 const scoped_refptr<base::SingleThreadTaskRunner>& device_task_runner) { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 59 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 60 DCHECK(!listener_); | 60 DCHECK(!listener_); |
| 61 DCHECK(!device_task_runner_); | 61 DCHECK(!device_task_runner_); |
| 62 listener_ = listener; | 62 listener_ = listener; |
| 63 device_task_runner_ = device_task_runner; | 63 device_task_runner_ = device_task_runner; |
| 64 } | 64 } |
| 65 | 65 |
| 66 void AudioInputDeviceManager::Unregister() { | 66 void AudioInputDeviceManager::Unregister() { |
| 67 DCHECK(listener_); | 67 DCHECK(listener_); |
| 68 listener_ = NULL; | 68 listener_ = NULL; |
| 69 } | 69 } |
| 70 | 70 |
| 71 void AudioInputDeviceManager::EnumerateDevices(MediaStreamType stream_type) { | 71 void AudioInputDeviceManager::EnumerateDevices(MediaStreamType stream_type) { |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 72 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 73 DCHECK(listener_); | 73 DCHECK(listener_); |
| 74 | 74 |
| 75 device_task_runner_->PostTask( | 75 device_task_runner_->PostTask( |
| 76 FROM_HERE, | 76 FROM_HERE, |
| 77 base::Bind(&AudioInputDeviceManager::EnumerateOnDeviceThread, | 77 base::Bind(&AudioInputDeviceManager::EnumerateOnDeviceThread, |
| 78 this, stream_type)); | 78 this, stream_type)); |
| 79 } | 79 } |
| 80 | 80 |
| 81 int AudioInputDeviceManager::Open(const StreamDeviceInfo& device) { | 81 int AudioInputDeviceManager::Open(const StreamDeviceInfo& device) { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 82 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 83 // Generate a new id for this device. | 83 // Generate a new id for this device. |
| 84 int session_id = next_capture_session_id_++; | 84 int session_id = next_capture_session_id_++; |
| 85 device_task_runner_->PostTask( | 85 device_task_runner_->PostTask( |
| 86 FROM_HERE, | 86 FROM_HERE, |
| 87 base::Bind(&AudioInputDeviceManager::OpenOnDeviceThread, | 87 base::Bind(&AudioInputDeviceManager::OpenOnDeviceThread, |
| 88 this, session_id, device)); | 88 this, session_id, device)); |
| 89 | 89 |
| 90 return session_id; | 90 return session_id; |
| 91 } | 91 } |
| 92 | 92 |
| 93 void AudioInputDeviceManager::Close(int session_id) { | 93 void AudioInputDeviceManager::Close(int session_id) { |
| 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 94 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 95 DCHECK(listener_); | 95 DCHECK(listener_); |
| 96 StreamDeviceList::iterator device = GetDevice(session_id); | 96 StreamDeviceList::iterator device = GetDevice(session_id); |
| 97 if (device == devices_.end()) | 97 if (device == devices_.end()) |
| 98 return; | 98 return; |
| 99 const MediaStreamType stream_type = device->device.type; | 99 const MediaStreamType stream_type = device->device.type; |
| 100 if (session_id != kFakeOpenSessionId) | 100 if (session_id != kFakeOpenSessionId) |
| 101 devices_.erase(device); | 101 devices_.erase(device); |
| 102 | 102 |
| 103 // Post a callback through the listener on IO thread since | 103 // Post a callback through the listener on IO thread since |
| 104 // MediaStreamManager is expecting the callback asynchronously. | 104 // MediaStreamManager is expecting the callback asynchronously. |
| 105 BrowserThread::PostTask(BrowserThread::IO, | 105 BrowserThread::PostTask(BrowserThread::IO, |
| 106 FROM_HERE, | 106 FROM_HERE, |
| 107 base::Bind(&AudioInputDeviceManager::ClosedOnIOThread, | 107 base::Bind(&AudioInputDeviceManager::ClosedOnIOThread, |
| 108 this, stream_type, session_id)); | 108 this, stream_type, session_id)); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void AudioInputDeviceManager::UseFakeDevice() { | 111 void AudioInputDeviceManager::UseFakeDevice() { |
| 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 112 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 113 use_fake_device_ = true; | 113 use_fake_device_ = true; |
| 114 } | 114 } |
| 115 | 115 |
| 116 bool AudioInputDeviceManager::ShouldUseFakeDevice() const { | 116 bool AudioInputDeviceManager::ShouldUseFakeDevice() const { |
| 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 117 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 118 return use_fake_device_; | 118 return use_fake_device_; |
| 119 } | 119 } |
| 120 | 120 |
| 121 void AudioInputDeviceManager::EnumerateOnDeviceThread( | 121 void AudioInputDeviceManager::EnumerateOnDeviceThread( |
| 122 MediaStreamType stream_type) { | 122 MediaStreamType stream_type) { |
| 123 SCOPED_UMA_HISTOGRAM_TIMER( | 123 SCOPED_UMA_HISTOGRAM_TIMER( |
| 124 "Media.AudioInputDeviceManager.EnumerateOnDeviceThreadTime"); | 124 "Media.AudioInputDeviceManager.EnumerateOnDeviceThreadTime"); |
| 125 DCHECK(IsOnDeviceThread()); | 125 DCHECK(IsOnDeviceThread()); |
| 126 | 126 |
| 127 media::AudioDeviceNames device_names; | 127 media::AudioDeviceNames device_names; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 // IO thread since MediaStreamManager handles the callback asynchronously. | 208 // IO thread since MediaStreamManager handles the callback asynchronously. |
| 209 BrowserThread::PostTask(BrowserThread::IO, | 209 BrowserThread::PostTask(BrowserThread::IO, |
| 210 FROM_HERE, | 210 FROM_HERE, |
| 211 base::Bind(&AudioInputDeviceManager::OpenedOnIOThread, | 211 base::Bind(&AudioInputDeviceManager::OpenedOnIOThread, |
| 212 this, session_id, out)); | 212 this, session_id, out)); |
| 213 } | 213 } |
| 214 | 214 |
| 215 void AudioInputDeviceManager::DevicesEnumeratedOnIOThread( | 215 void AudioInputDeviceManager::DevicesEnumeratedOnIOThread( |
| 216 MediaStreamType stream_type, | 216 MediaStreamType stream_type, |
| 217 scoped_ptr<StreamDeviceInfoArray> devices) { | 217 scoped_ptr<StreamDeviceInfoArray> devices) { |
| 218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 218 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 219 // Ensure that |devices| gets deleted on exit. | 219 // Ensure that |devices| gets deleted on exit. |
| 220 if (listener_) | 220 if (listener_) |
| 221 listener_->DevicesEnumerated(stream_type, *devices); | 221 listener_->DevicesEnumerated(stream_type, *devices); |
| 222 } | 222 } |
| 223 | 223 |
| 224 void AudioInputDeviceManager::OpenedOnIOThread(int session_id, | 224 void AudioInputDeviceManager::OpenedOnIOThread(int session_id, |
| 225 const StreamDeviceInfo& info) { | 225 const StreamDeviceInfo& info) { |
| 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 226 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 227 DCHECK_EQ(session_id, info.session_id); | 227 DCHECK_EQ(session_id, info.session_id); |
| 228 DCHECK(GetDevice(session_id) == devices_.end()); | 228 DCHECK(GetDevice(session_id) == devices_.end()); |
| 229 | 229 |
| 230 devices_.push_back(info); | 230 devices_.push_back(info); |
| 231 | 231 |
| 232 if (listener_) | 232 if (listener_) |
| 233 listener_->Opened(info.device.type, session_id); | 233 listener_->Opened(info.device.type, session_id); |
| 234 } | 234 } |
| 235 | 235 |
| 236 void AudioInputDeviceManager::ClosedOnIOThread(MediaStreamType stream_type, | 236 void AudioInputDeviceManager::ClosedOnIOThread(MediaStreamType stream_type, |
| 237 int session_id) { | 237 int session_id) { |
| 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 238 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 239 if (listener_) | 239 if (listener_) |
| 240 listener_->Closed(stream_type, session_id); | 240 listener_->Closed(stream_type, session_id); |
| 241 } | 241 } |
| 242 | 242 |
| 243 bool AudioInputDeviceManager::IsOnDeviceThread() const { | 243 bool AudioInputDeviceManager::IsOnDeviceThread() const { |
| 244 return device_task_runner_->BelongsToCurrentThread(); | 244 return device_task_runner_->BelongsToCurrentThread(); |
| 245 } | 245 } |
| 246 | 246 |
| 247 AudioInputDeviceManager::StreamDeviceList::iterator | 247 AudioInputDeviceManager::StreamDeviceList::iterator |
| 248 AudioInputDeviceManager::GetDevice(int session_id) { | 248 AudioInputDeviceManager::GetDevice(int session_id) { |
| 249 for (StreamDeviceList::iterator i(devices_.begin()); i != devices_.end(); | 249 for (StreamDeviceList::iterator i(devices_.begin()); i != devices_.end(); |
| 250 ++i) { | 250 ++i) { |
| 251 if (i->session_id == session_id) | 251 if (i->session_id == session_id) |
| 252 return i; | 252 return i; |
| 253 } | 253 } |
| 254 | 254 |
| 255 return devices_.end(); | 255 return devices_.end(); |
| 256 } | 256 } |
| 257 | 257 |
| 258 } // namespace content | 258 } // namespace content |
| OLD | NEW |