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/common/media/audio_messages.h" | 14 #include "content/common/media/audio_messages.h" |
15 | 15 |
16 using content::BrowserMessageFilter; | 16 using content::BrowserMessageFilter; |
17 using content::BrowserThread; | 17 using content::BrowserThread; |
18 | 18 |
19 AudioInputRendererHost::AudioEntry::AudioEntry() | 19 AudioInputRendererHost::AudioEntry::AudioEntry() |
20 : stream_id(0), | 20 : stream_id(0), |
21 pending_close(false) { | 21 pending_close(false) { |
22 } | 22 } |
23 | 23 |
24 AudioInputRendererHost::AudioEntry::~AudioEntry() {} | 24 AudioInputRendererHost::AudioEntry::~AudioEntry() {} |
25 | 25 |
26 AudioInputRendererHost::AudioInputRendererHost( | 26 AudioInputRendererHost::AudioInputRendererHost( |
27 content::ResourceContext* resource_context, | 27 content::ResourceContext* resource_context, |
28 media::AudioManager* audio_manager) | 28 AudioManager* audio_manager) |
29 : resource_context_(resource_context), | 29 : resource_context_(resource_context), |
30 audio_manager_(audio_manager) { | 30 audio_manager_(audio_manager) { |
31 } | 31 } |
32 | 32 |
33 AudioInputRendererHost::~AudioInputRendererHost() { | 33 AudioInputRendererHost::~AudioInputRendererHost() { |
34 DCHECK(audio_entries_.empty()); | 34 DCHECK(audio_entries_.empty()); |
35 } | 35 } |
36 | 36 |
37 void AudioInputRendererHost::OnChannelClosing() { | 37 void AudioInputRendererHost::OnChannelClosing() { |
38 BrowserMessageFilter::OnChannelClosing(); | 38 BrowserMessageFilter::OnChannelClosing(); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // mapping shared memory and sharing with the renderer process. | 107 // mapping shared memory and sharing with the renderer process. |
108 base::SharedMemoryHandle foreign_memory_handle; | 108 base::SharedMemoryHandle foreign_memory_handle; |
109 if (!entry->shared_memory.ShareToProcess(peer_handle(), | 109 if (!entry->shared_memory.ShareToProcess(peer_handle(), |
110 &foreign_memory_handle)) { | 110 &foreign_memory_handle)) { |
111 // If we failed to map and share the shared memory then close the audio | 111 // If we failed to map and share the shared memory then close the audio |
112 // stream and send an error message. | 112 // stream and send an error message. |
113 DeleteEntryOnError(entry); | 113 DeleteEntryOnError(entry); |
114 return; | 114 return; |
115 } | 115 } |
116 | 116 |
117 media::AudioInputSyncWriter* writer = | 117 AudioInputSyncWriter* writer = |
118 static_cast<media::AudioInputSyncWriter*>(entry->writer.get()); | 118 static_cast<AudioInputSyncWriter*>(entry->writer.get()); |
119 | 119 |
120 #if defined(OS_WIN) | 120 #if defined(OS_WIN) |
121 base::SyncSocket::Handle foreign_socket_handle; | 121 base::SyncSocket::Handle foreign_socket_handle; |
122 #else | 122 #else |
123 base::FileDescriptor foreign_socket_handle; | 123 base::FileDescriptor foreign_socket_handle; |
124 #endif | 124 #endif |
125 | 125 |
126 // If we failed to prepare the sync socket for the renderer then we fail | 126 // If we failed to prepare the sync socket for the renderer then we fail |
127 // the construction of audio input stream. | 127 // the construction of audio input stream. |
128 if (!writer->PrepareForeignSocketHandle(peer_handle(), | 128 if (!writer->PrepareForeignSocketHandle(peer_handle(), |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 resource_context_, audio_manager_)->audio_input_device_manager(); | 190 resource_context_, audio_manager_)->audio_input_device_manager(); |
191 | 191 |
192 // Add the session entry to the map. | 192 // Add the session entry to the map. |
193 session_entries_[session_id] = stream_id; | 193 session_entries_[session_id] = stream_id; |
194 | 194 |
195 // Start the device with the session_id. If the device is started | 195 // Start the device with the session_id. If the device is started |
196 // successfully, OnDeviceStarted() callback will be triggered. | 196 // successfully, OnDeviceStarted() callback will be triggered. |
197 audio_input_man->Start(session_id, this); | 197 audio_input_man->Start(session_id, this); |
198 } | 198 } |
199 | 199 |
200 void AudioInputRendererHost::OnCreateStream( | 200 void AudioInputRendererHost::OnCreateStream(int stream_id, |
201 int stream_id, const media::AudioParameters& params, | 201 const AudioParameters& params, |
202 const std::string& device_id, bool automatic_gain_control) { | 202 const std::string& device_id, |
| 203 bool automatic_gain_control) { |
203 VLOG(1) << "AudioInputRendererHost::OnCreateStream(stream_id=" | 204 VLOG(1) << "AudioInputRendererHost::OnCreateStream(stream_id=" |
204 << stream_id << ")"; | 205 << stream_id << ")"; |
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
206 DCHECK(LookupById(stream_id) == NULL); | 207 DCHECK(LookupById(stream_id) == NULL); |
207 | 208 |
208 media::AudioParameters audio_params(params); | 209 AudioParameters audio_params(params); |
209 | 210 |
210 DCHECK_GT(audio_params.frames_per_buffer(), 0); | 211 DCHECK_GT(audio_params.frames_per_buffer(), 0); |
211 uint32 buffer_size = audio_params.GetBytesPerBuffer(); | 212 uint32 buffer_size = audio_params.GetBytesPerBuffer(); |
212 | 213 |
213 // Create a new AudioEntry structure. | 214 // Create a new AudioEntry structure. |
214 scoped_ptr<AudioEntry> entry(new AudioEntry()); | 215 scoped_ptr<AudioEntry> entry(new AudioEntry()); |
215 | 216 |
216 uint32 mem_size = sizeof(media::AudioInputBufferParameters) + buffer_size; | 217 uint32 mem_size = sizeof(AudioInputBufferParameters) + buffer_size; |
217 | 218 |
218 // Create the shared memory and share it with the renderer process | 219 // Create the shared memory and share it with the renderer process |
219 // using a new SyncWriter object. | 220 // using a new SyncWriter object. |
220 if (!entry->shared_memory.CreateAndMapAnonymous(mem_size)) { | 221 if (!entry->shared_memory.CreateAndMapAnonymous(mem_size)) { |
221 // If creation of shared memory failed then send an error message. | 222 // If creation of shared memory failed then send an error message. |
222 SendErrorMessage(stream_id); | 223 SendErrorMessage(stream_id); |
223 return; | 224 return; |
224 } | 225 } |
225 | 226 |
226 scoped_ptr<media::AudioInputSyncWriter> writer( | 227 scoped_ptr<AudioInputSyncWriter> writer( |
227 new media::AudioInputSyncWriter(&entry->shared_memory)); | 228 new AudioInputSyncWriter(&entry->shared_memory)); |
228 | 229 |
229 if (!writer->Init()) { | 230 if (!writer->Init()) { |
230 SendErrorMessage(stream_id); | 231 SendErrorMessage(stream_id); |
231 return; | 232 return; |
232 } | 233 } |
233 | 234 |
234 // If we have successfully created the SyncWriter then assign it to the | 235 // If we have successfully created the SyncWriter then assign it to the |
235 // entry and construct an AudioInputController. | 236 // entry and construct an AudioInputController. |
236 // TODO(henrika): replace CreateLowLatency() with Create() as soon | 237 // TODO(henrika): replace CreateLowLatency() with Create() as soon |
237 // as satish has ensured that Speech Input also uses the default low- | 238 // as satish has ensured that Speech Input also uses the default low- |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 418 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
418 | 419 |
419 for (SessionEntryMap::iterator it = session_entries_.begin(); | 420 for (SessionEntryMap::iterator it = session_entries_.begin(); |
420 it != session_entries_.end(); ++it) { | 421 it != session_entries_.end(); ++it) { |
421 if (stream_id == it->second) { | 422 if (stream_id == it->second) { |
422 return it->first; | 423 return it->first; |
423 } | 424 } |
424 } | 425 } |
425 return 0; | 426 return 0; |
426 } | 427 } |
OLD | NEW |