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_renderer_host.h" | 5 #include "content/browser/renderer_host/media/audio_input_renderer_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/process.h" | 9 #include "base/process.h" |
10 #include "base/shared_memory.h" | 10 #include "base/shared_memory.h" |
11 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 11 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
12 #include "content/browser/renderer_host/media/audio_input_sync_writer.h" | 12 #include "content/browser/renderer_host/media/audio_input_sync_writer.h" |
13 #include "content/browser/renderer_host/media/media_stream_manager.h" | 13 #include "content/browser/renderer_host/media/media_stream_manager.h" |
14 #include "content/browser/renderer_host/media/web_contents_audio_input_stream.h" | 14 #include "content/browser/renderer_host/media/web_contents_audio_input_stream.h" |
15 #include "content/browser/renderer_host/media/web_contents_capture_util.h" | 15 #include "content/browser/renderer_host/media/web_contents_capture_util.h" |
16 #include "content/common/media/audio_messages.h" | |
17 #include "media/audio/audio_manager_base.h" | 16 #include "media/audio/audio_manager_base.h" |
18 | 17 |
19 namespace content { | 18 namespace content { |
20 | 19 |
21 struct AudioInputRendererHost::AudioEntry { | 20 struct AudioInputRendererHost::AudioEntry { |
22 AudioEntry(); | 21 AudioEntry(); |
23 ~AudioEntry(); | 22 ~AudioEntry(); |
24 | 23 |
25 // The AudioInputController that manages the audio input stream. | 24 // The AudioInputController that manages the audio input stream. |
26 scoped_refptr<media::AudioInputController> controller; | 25 scoped_refptr<media::AudioInputController> controller; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 return; | 178 return; |
180 | 179 |
181 DeleteEntryOnError(entry); | 180 DeleteEntryOnError(entry); |
182 } | 181 } |
183 | 182 |
184 bool AudioInputRendererHost::OnMessageReceived(const IPC::Message& message, | 183 bool AudioInputRendererHost::OnMessageReceived(const IPC::Message& message, |
185 bool* message_was_ok) { | 184 bool* message_was_ok) { |
186 bool handled = true; | 185 bool handled = true; |
187 IPC_BEGIN_MESSAGE_MAP_EX(AudioInputRendererHost, message, *message_was_ok) | 186 IPC_BEGIN_MESSAGE_MAP_EX(AudioInputRendererHost, message, *message_was_ok) |
188 IPC_MESSAGE_HANDLER(AudioInputHostMsg_CreateStream, OnCreateStream) | 187 IPC_MESSAGE_HANDLER(AudioInputHostMsg_CreateStream, OnCreateStream) |
189 IPC_MESSAGE_HANDLER(AudioInputHostMsg_AssociateStreamWithConsumer, | |
190 OnAssociateStreamWithConsumer) | |
191 IPC_MESSAGE_HANDLER(AudioInputHostMsg_RecordStream, OnRecordStream) | 188 IPC_MESSAGE_HANDLER(AudioInputHostMsg_RecordStream, OnRecordStream) |
192 IPC_MESSAGE_HANDLER(AudioInputHostMsg_CloseStream, OnCloseStream) | 189 IPC_MESSAGE_HANDLER(AudioInputHostMsg_CloseStream, OnCloseStream) |
193 IPC_MESSAGE_HANDLER(AudioInputHostMsg_SetVolume, OnSetVolume) | 190 IPC_MESSAGE_HANDLER(AudioInputHostMsg_SetVolume, OnSetVolume) |
194 IPC_MESSAGE_UNHANDLED(handled = false) | 191 IPC_MESSAGE_UNHANDLED(handled = false) |
195 IPC_END_MESSAGE_MAP_EX() | 192 IPC_END_MESSAGE_MAP_EX() |
196 | 193 |
197 return handled; | 194 return handled; |
198 } | 195 } |
199 | 196 |
200 void AudioInputRendererHost::OnCreateStream( | 197 void AudioInputRendererHost::OnCreateStream( |
201 int stream_id, | 198 int stream_id, |
| 199 int render_view_id, |
202 int session_id, | 200 int session_id, |
203 const media::AudioParameters& params, | 201 const AudioInputHostMsg_CreateStream_Config& config) { |
204 bool automatic_gain_control, | |
205 int shared_memory_count) { | |
206 VLOG(1) << "AudioInputRendererHost::OnCreateStream(stream_id=" | |
207 << stream_id << ", session_id=" << session_id << ")"; | |
208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 203 |
| 204 DVLOG(1) << "AudioInputRendererHost@" << this |
| 205 << "::OnCreateStream(stream_id=" << stream_id |
| 206 << ", render_view_id=" << render_view_id |
| 207 << ", session_id=" << session_id << ")"; |
| 208 DCHECK_GT(render_view_id, 0); |
| 209 |
209 // media::AudioParameters is validated in the deserializer. | 210 // media::AudioParameters is validated in the deserializer. |
210 if (LookupById(stream_id) != NULL) { | 211 if (LookupById(stream_id) != NULL) { |
211 SendErrorMessage(stream_id); | 212 SendErrorMessage(stream_id); |
212 return; | 213 return; |
213 } | 214 } |
214 | 215 |
215 // Check if we have the permission to open the device and which device to use. | 216 // Check if we have the permission to open the device and which device to use. |
216 std::string device_id = media::AudioManagerBase::kDefaultDeviceId; | 217 std::string device_id = media::AudioManagerBase::kDefaultDeviceId; |
217 if (session_id != AudioInputDeviceManager::kFakeOpenSessionId) { | 218 if (session_id != AudioInputDeviceManager::kFakeOpenSessionId) { |
218 const StreamDeviceInfo* info = media_stream_manager_-> | 219 const StreamDeviceInfo* info = media_stream_manager_-> |
219 audio_input_device_manager()->GetOpenedDeviceInfoById(session_id); | 220 audio_input_device_manager()->GetOpenedDeviceInfoById(session_id); |
220 if (!info) { | 221 if (!info) { |
221 SendErrorMessage(stream_id); | 222 SendErrorMessage(stream_id); |
222 DLOG(WARNING) << "No permission has been granted to input stream with " | 223 DLOG(WARNING) << "No permission has been granted to input stream with " |
223 << "session_id=" << session_id; | 224 << "session_id=" << session_id; |
224 return; | 225 return; |
225 } | 226 } |
226 | 227 |
227 device_id = info->device.id; | 228 device_id = info->device.id; |
228 } | 229 } |
229 | 230 |
230 media::AudioParameters audio_params(params); | 231 media::AudioParameters audio_params(config.params); |
231 if (media_stream_manager_->audio_input_device_manager()-> | 232 if (media_stream_manager_->audio_input_device_manager()-> |
232 ShouldUseFakeDevice()) { | 233 ShouldUseFakeDevice()) { |
233 audio_params.Reset(media::AudioParameters::AUDIO_FAKE, | 234 audio_params.Reset( |
234 params.channel_layout(), params.channels(), 0, | 235 media::AudioParameters::AUDIO_FAKE, |
235 params.sample_rate(), | 236 config.params.channel_layout(), config.params.channels(), 0, |
236 params.bits_per_sample(), params.frames_per_buffer()); | 237 config.params.sample_rate(), config.params.bits_per_sample(), |
| 238 config.params.frames_per_buffer()); |
237 } | 239 } |
238 | 240 |
239 uint32 buffer_size = audio_params.GetBytesPerBuffer(); | |
240 | |
241 // Create a new AudioEntry structure. | 241 // Create a new AudioEntry structure. |
242 scoped_ptr<AudioEntry> entry(new AudioEntry()); | 242 scoped_ptr<AudioEntry> entry(new AudioEntry()); |
243 | 243 |
244 uint32 segment_size = | 244 const uint32 segment_size = (sizeof(media::AudioInputBufferParameters) + |
245 sizeof(media::AudioInputBufferParameters) + buffer_size; | 245 audio_params.GetBytesPerBuffer()); |
246 entry->shared_memory_segment_count = shared_memory_count; | 246 entry->shared_memory_segment_count = config.shared_memory_count; |
247 | 247 |
248 // Create the shared memory and share it with the renderer process | 248 // Create the shared memory and share it with the renderer process |
249 // using a new SyncWriter object. | 249 // using a new SyncWriter object. |
250 if (!entry->shared_memory.CreateAndMapAnonymous( | 250 if (!entry->shared_memory.CreateAndMapAnonymous( |
251 segment_size * entry->shared_memory_segment_count)) { | 251 segment_size * entry->shared_memory_segment_count)) { |
252 // If creation of shared memory failed then send an error message. | 252 // If creation of shared memory failed then send an error message. |
253 SendErrorMessage(stream_id); | 253 SendErrorMessage(stream_id); |
254 return; | 254 return; |
255 } | 255 } |
256 | 256 |
(...skipping 28 matching lines...) Expand all Loading... |
285 entry->writer.get()); | 285 entry->writer.get()); |
286 } | 286 } |
287 | 287 |
288 if (!entry->controller) { | 288 if (!entry->controller) { |
289 SendErrorMessage(stream_id); | 289 SendErrorMessage(stream_id); |
290 return; | 290 return; |
291 } | 291 } |
292 | 292 |
293 // Set the initial AGC state for the audio input stream. Note that, the AGC | 293 // Set the initial AGC state for the audio input stream. Note that, the AGC |
294 // is only supported in AUDIO_PCM_LOW_LATENCY mode. | 294 // is only supported in AUDIO_PCM_LOW_LATENCY mode. |
295 if (params.format() == media::AudioParameters::AUDIO_PCM_LOW_LATENCY) | 295 if (config.params.format() == media::AudioParameters::AUDIO_PCM_LOW_LATENCY) |
296 entry->controller->SetAutomaticGainControl(automatic_gain_control); | 296 entry->controller->SetAutomaticGainControl(config.automatic_gain_control); |
297 | 297 |
298 // If we have created the controller successfully create a entry and add it | 298 // Since the controller was created successfully, create an entry and add it |
299 // to the map. | 299 // to the map. |
300 entry->stream_id = stream_id; | 300 entry->stream_id = stream_id; |
301 | |
302 audio_entries_.insert(std::make_pair(stream_id, entry.release())); | 301 audio_entries_.insert(std::make_pair(stream_id, entry.release())); |
303 } | 302 } |
304 | 303 |
305 void AudioInputRendererHost::OnAssociateStreamWithConsumer(int stream_id, | |
306 int render_view_id) { | |
307 // TODO(miu): Will use render_view_id in upcoming change. | |
308 DVLOG(1) << "AudioInputRendererHost@" << this | |
309 << "::OnAssociateStreamWithConsumer(stream_id=" << stream_id | |
310 << ", render_view_id=" << render_view_id << ")"; | |
311 } | |
312 | |
313 void AudioInputRendererHost::OnRecordStream(int stream_id) { | 304 void AudioInputRendererHost::OnRecordStream(int stream_id) { |
314 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
315 | 306 |
316 AudioEntry* entry = LookupById(stream_id); | 307 AudioEntry* entry = LookupById(stream_id); |
317 if (!entry) { | 308 if (!entry) { |
318 SendErrorMessage(stream_id); | 309 SendErrorMessage(stream_id); |
319 return; | 310 return; |
320 } | 311 } |
321 | 312 |
322 entry->controller->Record(); | 313 entry->controller->Record(); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 // TODO(hclam): Implement a faster look up method. | 395 // TODO(hclam): Implement a faster look up method. |
405 for (AudioEntryMap::iterator i = audio_entries_.begin(); | 396 for (AudioEntryMap::iterator i = audio_entries_.begin(); |
406 i != audio_entries_.end(); ++i) { | 397 i != audio_entries_.end(); ++i) { |
407 if (controller == i->second->controller) | 398 if (controller == i->second->controller) |
408 return i->second; | 399 return i->second; |
409 } | 400 } |
410 return NULL; | 401 return NULL; |
411 } | 402 } |
412 | 403 |
413 } // namespace content | 404 } // namespace content |
OLD | NEW |