| 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/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 "content/public/common/media_stream_request.h" |
| 12 #include "media/audio/audio_device_name.h" |
| 11 #include "media/audio/audio_input_ipc.h" | 13 #include "media/audio/audio_input_ipc.h" |
| 12 #include "media/audio/audio_manager_base.h" | 14 #include "media/audio/audio_manager_base.h" |
| 13 | 15 |
| 14 using content::BrowserThread; | 16 using content::BrowserThread; |
| 15 | 17 |
| 16 namespace media_stream { | 18 namespace media_stream { |
| 17 | 19 |
| 18 const int AudioInputDeviceManager::kFakeOpenSessionId = 1; | 20 const int AudioInputDeviceManager::kFakeOpenSessionId = 1; |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 handler->OnDeviceStarted(session_id, | 99 handler->OnDeviceStarted(session_id, |
| 98 media::AudioManagerBase::kDefaultDeviceId); | 100 media::AudioManagerBase::kDefaultDeviceId); |
| 99 return; | 101 return; |
| 100 } | 102 } |
| 101 | 103 |
| 102 // Look up the device_id of the session so we can notify the renderer that | 104 // Look up the device_id of the session so we can notify the renderer that |
| 103 // the device has started. If the session has not been started, | 105 // the device has started. If the session has not been started, |
| 104 // use the empty device_id to indicate that Start() failed. | 106 // use the empty device_id to indicate that Start() failed. |
| 105 std::string device_id; | 107 std::string device_id; |
| 106 if (event_handlers_.insert(std::make_pair(session_id, handler)).second) { | 108 if (event_handlers_.insert(std::make_pair(session_id, handler)).second) { |
| 107 AudioInputDeviceMap::iterator it = devices_.find(session_id); | 109 StreamDeviceMap::const_iterator it = devices_.find(session_id); |
| 108 if (it != devices_.end()) | 110 if (it != devices_.end()) |
| 109 device_id = it->second.unique_id; | 111 device_id = it->second.device_id; |
| 110 } | 112 } |
| 111 | 113 |
| 112 // Post a callback through the AudioInputRendererHost to notify the renderer | 114 // Post a callback through the AudioInputRendererHost to notify the renderer |
| 113 // that the device has started. | 115 // that the device has started. |
| 114 handler->OnDeviceStarted(session_id, device_id); | 116 handler->OnDeviceStarted(session_id, device_id); |
| 115 } | 117 } |
| 116 | 118 |
| 117 void AudioInputDeviceManager::Stop(int session_id) { | 119 void AudioInputDeviceManager::Stop(int session_id) { |
| 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 119 | 121 |
| 120 // Erase the event handler referenced by the session_id. | 122 // Erase the event handler referenced by the session_id. |
| 121 event_handlers_.erase(session_id); | 123 event_handlers_.erase(session_id); |
| 122 } | 124 } |
| 123 | 125 |
| 124 void AudioInputDeviceManager::EnumerateOnDeviceThread() { | 126 void AudioInputDeviceManager::EnumerateOnDeviceThread() { |
| 125 DCHECK(IsOnDeviceThread()); | 127 DCHECK(IsOnDeviceThread()); |
| 126 // AudioManager is guaranteed to outlive MediaStreamManager in | 128 // AudioManager is guaranteed to outlive MediaStreamManager in |
| 127 // BrowserMainloop. | 129 // BrowserMainloop. |
| 128 media::AudioDeviceNames device_names; | 130 media::AudioDeviceNames device_names; |
| 129 audio_manager_->GetAudioInputDeviceNames(&device_names); | 131 audio_manager_->GetAudioInputDeviceNames(&device_names); |
| 130 | 132 |
| 131 StreamDeviceInfoArray* devices = new StreamDeviceInfoArray; | 133 StreamDeviceInfoArray* devices = new StreamDeviceInfoArray; |
| 132 for (media::AudioDeviceNames::iterator it = device_names.begin(); | 134 for (media::AudioDeviceNames::iterator it = device_names.begin(); |
| 133 it != device_names.end(); | 135 it != device_names.end(); |
| 134 ++it) { | 136 ++it) { |
| 135 devices->push_back( | 137 // NOTE: Only support enumeration of the MEDIA_DEVICE_AUDIO_CAPTURE type. |
| 136 StreamDeviceInfo(content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE, | 138 devices->push_back(StreamDeviceInfo( |
| 137 it->device_name, | 139 content::MEDIA_DEVICE_AUDIO_CAPTURE, it->device_name, |
| 138 it->unique_id, false)); | 140 it->unique_id, false)); |
| 139 } | 141 } |
| 140 | 142 |
| 141 // Return the device list through the listener by posting a task on | 143 // Return the device list through the listener by posting a task on |
| 142 // IO thread since MediaStreamManager handles the callback asynchronously. | 144 // IO thread since MediaStreamManager handles the callback asynchronously. |
| 143 BrowserThread::PostTask( | 145 BrowserThread::PostTask( |
| 144 BrowserThread::IO, | 146 BrowserThread::IO, |
| 145 FROM_HERE, | 147 FROM_HERE, |
| 146 base::Bind(&AudioInputDeviceManager::DevicesEnumeratedOnIOThread, | 148 base::Bind(&AudioInputDeviceManager::DevicesEnumeratedOnIOThread, |
| 147 this, | 149 this, |
| 148 devices)); | 150 devices)); |
| 149 } | 151 } |
| 150 | 152 |
| 151 void AudioInputDeviceManager::OpenOnDeviceThread( | 153 void AudioInputDeviceManager::OpenOnDeviceThread( |
| 152 int session_id, const StreamDeviceInfo& device) { | 154 int session_id, const StreamDeviceInfo& device) { |
| 153 DCHECK(IsOnDeviceThread()); | 155 DCHECK(IsOnDeviceThread()); |
| 154 | 156 |
| 155 // Add the session_id and device to the list. | 157 // Add the session_id and device to the map. |
| 156 media::AudioDeviceName target_device(device.name, device.device_id); | 158 if (!devices_.insert(std::make_pair(session_id, device)).second) { |
| 157 if (!devices_.insert(std::make_pair(session_id, target_device)).second) { | |
| 158 NOTREACHED(); | 159 NOTREACHED(); |
| 159 devices_[session_id] = target_device; | 160 devices_[session_id] = device; |
| 160 } | 161 } |
| 161 | 162 |
| 162 // Return the |session_id| through the listener by posting a task on | 163 // Return the |session_id| through the listener by posting a task on |
| 163 // IO thread since MediaStreamManager handles the callback asynchronously. | 164 // IO thread since MediaStreamManager handles the callback asynchronously. |
| 164 BrowserThread::PostTask(BrowserThread::IO, | 165 BrowserThread::PostTask(BrowserThread::IO, |
| 165 FROM_HERE, | 166 FROM_HERE, |
| 166 base::Bind(&AudioInputDeviceManager::OpenedOnIOThread, | 167 base::Bind(&AudioInputDeviceManager::OpenedOnIOThread, |
| 167 this, | 168 this, |
| 168 session_id)); | 169 device.stream_type, session_id)); |
| 169 } | 170 } |
| 170 | 171 |
| 171 void AudioInputDeviceManager::CloseOnDeviceThread(int session_id) { | 172 void AudioInputDeviceManager::CloseOnDeviceThread(int session_id) { |
| 172 DCHECK(IsOnDeviceThread()); | 173 DCHECK(IsOnDeviceThread()); |
| 173 | 174 |
| 174 AudioInputDeviceMap::iterator it = devices_.find(session_id); | 175 StreamDeviceMap::iterator it = devices_.find(session_id); |
| 175 if (it != devices_.end()) | 176 if (it == devices_.end()) |
| 176 devices_.erase(it); | 177 return; |
| 178 const content::MediaStreamDeviceType stream_type = it->second.stream_type; |
| 179 devices_.erase(it); |
| 177 | 180 |
| 178 // Post a callback through the listener on IO thread since | 181 // Post a callback through the listener on IO thread since |
| 179 // MediaStreamManager handles the callback asynchronously. | 182 // MediaStreamManager handles the callback asynchronously. |
| 180 BrowserThread::PostTask(BrowserThread::IO, | 183 BrowserThread::PostTask(BrowserThread::IO, |
| 181 FROM_HERE, | 184 FROM_HERE, |
| 182 base::Bind(&AudioInputDeviceManager::ClosedOnIOThread, | 185 base::Bind(&AudioInputDeviceManager::ClosedOnIOThread, |
| 183 this, | 186 this, |
| 184 session_id)); | 187 stream_type, session_id)); |
| 185 } | 188 } |
| 186 | 189 |
| 187 void AudioInputDeviceManager::DevicesEnumeratedOnIOThread( | 190 void AudioInputDeviceManager::DevicesEnumeratedOnIOThread( |
| 188 StreamDeviceInfoArray* devices) { | 191 StreamDeviceInfoArray* devices) { |
| 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 190 // Ensure that |devices| gets deleted on exit. | 193 // Ensure that |devices| gets deleted on exit. |
| 191 scoped_ptr<StreamDeviceInfoArray> devices_array(devices); | 194 scoped_ptr<StreamDeviceInfoArray> devices_array(devices); |
| 192 if (listener_) { | 195 if (listener_) { |
| 193 listener_->DevicesEnumerated( | 196 // NOTE: Only support enumeration of the MEDIA_DEVICE_AUDIO_CAPTURE type. |
| 194 content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE, | 197 listener_->DevicesEnumerated(content::MEDIA_DEVICE_AUDIO_CAPTURE, |
| 195 *devices_array); | 198 *devices_array); |
| 196 } | 199 } |
| 197 } | 200 } |
| 198 | 201 |
| 199 void AudioInputDeviceManager::OpenedOnIOThread(int session_id) { | 202 void AudioInputDeviceManager::OpenedOnIOThread( |
| 203 content::MediaStreamDeviceType stream_type, int session_id) { |
| 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 201 if (listener_) | 205 if (listener_) |
| 202 listener_->Opened(content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE, | 206 listener_->Opened(stream_type, session_id); |
| 203 session_id); | |
| 204 } | 207 } |
| 205 | 208 |
| 206 void AudioInputDeviceManager::ClosedOnIOThread(int session_id) { | 209 void AudioInputDeviceManager::ClosedOnIOThread( |
| 210 content::MediaStreamDeviceType stream_type, int session_id) { |
| 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 208 if (listener_) | 212 if (listener_) |
| 209 listener_->Closed(content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE, | 213 listener_->Closed(stream_type, session_id); |
| 210 session_id); | |
| 211 } | 214 } |
| 212 | 215 |
| 213 bool AudioInputDeviceManager::IsOnDeviceThread() const { | 216 bool AudioInputDeviceManager::IsOnDeviceThread() const { |
| 214 return device_loop_->BelongsToCurrentThread(); | 217 return device_loop_->BelongsToCurrentThread(); |
| 215 } | 218 } |
| 216 | 219 |
| 217 } // namespace media_stream | 220 } // namespace media_stream |
| OLD | NEW |