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_renderer_host.h" | 5 #include "content/browser/renderer_host/media/audio_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/browser_main_loop.h" | 11 #include "content/browser/browser_main_loop.h" |
12 #include "content/browser/renderer_host/media/audio_sync_reader.h" | 12 #include "content/browser/renderer_host/media/audio_sync_reader.h" |
13 #include "content/common/media/audio_messages.h" | 13 #include "content/common/media/audio_messages.h" |
14 #include "content/public/browser/media_observer.h" | 14 #include "content/public/browser/media_observer.h" |
15 #include "media/audio/shared_memory_util.h" | 15 #include "media/audio/shared_memory_util.h" |
16 #include "media/base/audio_bus.h" | 16 #include "media/base/audio_bus.h" |
17 #include "media/base/limits.h" | 17 #include "media/base/limits.h" |
18 | 18 |
19 using content::BrowserMessageFilter; | 19 using content::BrowserMessageFilter; |
20 using content::BrowserThread; | 20 using content::BrowserThread; |
| 21 using media::AudioBus; |
21 | 22 |
22 AudioRendererHost::AudioEntry::AudioEntry() | 23 AudioRendererHost::AudioEntry::AudioEntry() |
23 : stream_id(0), | 24 : stream_id(0), |
24 pending_close(false) { | 25 pending_close(false) { |
25 } | 26 } |
26 | 27 |
27 AudioRendererHost::AudioEntry::~AudioEntry() {} | 28 AudioRendererHost::AudioEntry::~AudioEntry() {} |
28 | 29 |
29 /////////////////////////////////////////////////////////////////////////////// | 30 /////////////////////////////////////////////////////////////////////////////// |
30 // AudioRendererHost implementations. | 31 // AudioRendererHost implementations. |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 IPC_MESSAGE_HANDLER(AudioHostMsg_FlushStream, OnFlushStream) | 188 IPC_MESSAGE_HANDLER(AudioHostMsg_FlushStream, OnFlushStream) |
188 IPC_MESSAGE_HANDLER(AudioHostMsg_CloseStream, OnCloseStream) | 189 IPC_MESSAGE_HANDLER(AudioHostMsg_CloseStream, OnCloseStream) |
189 IPC_MESSAGE_HANDLER(AudioHostMsg_SetVolume, OnSetVolume) | 190 IPC_MESSAGE_HANDLER(AudioHostMsg_SetVolume, OnSetVolume) |
190 IPC_MESSAGE_UNHANDLED(handled = false) | 191 IPC_MESSAGE_UNHANDLED(handled = false) |
191 IPC_END_MESSAGE_MAP_EX() | 192 IPC_END_MESSAGE_MAP_EX() |
192 | 193 |
193 return handled; | 194 return handled; |
194 } | 195 } |
195 | 196 |
196 void AudioRendererHost::OnCreateStream( | 197 void AudioRendererHost::OnCreateStream( |
197 int stream_id, const media::AudioParameters& params) { | 198 int stream_id, const media::AudioParameters& params, int input_channels) { |
198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
199 DCHECK(LookupById(stream_id) == NULL); | 200 DCHECK(LookupById(stream_id) == NULL); |
200 | 201 |
201 media::AudioParameters audio_params(params); | 202 media::AudioParameters audio_params(params); |
202 uint32 buffer_size = media::AudioBus::CalculateMemorySize(audio_params); | 203 uint32 buffer_size = media::AudioBus::CalculateMemorySize(audio_params); |
203 DCHECK_GT(buffer_size, 0U); | 204 DCHECK_GT(buffer_size, 0U); |
204 DCHECK_LE(buffer_size, | 205 DCHECK_LE(buffer_size, |
205 static_cast<uint32>(media::limits::kMaxPacketSizeInBytes)); | 206 static_cast<uint32>(media::limits::kMaxPacketSizeInBytes)); |
206 | 207 |
| 208 DCHECK_GE(input_channels, 0); |
| 209 DCHECK_LT(input_channels, media::limits::kMaxChannels); |
| 210 |
| 211 // Calculate output and input memory size. |
| 212 int output_memory_size = AudioBus::CalculateMemorySize(audio_params); |
| 213 DCHECK_GT(output_memory_size, 0); |
| 214 |
| 215 int frames = audio_params.frames_per_buffer(); |
| 216 int input_memory_size = |
| 217 AudioBus::CalculateMemorySize(input_channels, frames); |
| 218 |
| 219 DCHECK_GE(input_memory_size, 0); |
| 220 |
207 scoped_ptr<AudioEntry> entry(new AudioEntry()); | 221 scoped_ptr<AudioEntry> entry(new AudioEntry()); |
208 | 222 |
209 // Create the shared memory and share with the renderer process. | 223 // Create the shared memory and share with the renderer process. |
| 224 // For synchronized I/O (if input_channels > 0) then we allocate |
| 225 // extra memory after the output data for the input data. |
| 226 uint32 io_buffer_size = output_memory_size + input_memory_size; |
| 227 |
210 uint32 shared_memory_size = | 228 uint32 shared_memory_size = |
211 media::TotalSharedMemorySizeInBytes(buffer_size); | 229 media::TotalSharedMemorySizeInBytes(io_buffer_size); |
212 if (!entry->shared_memory.CreateAndMapAnonymous(shared_memory_size)) { | 230 if (!entry->shared_memory.CreateAndMapAnonymous(shared_memory_size)) { |
213 // If creation of shared memory failed then send an error message. | 231 // If creation of shared memory failed then send an error message. |
214 SendErrorMessage(stream_id); | 232 SendErrorMessage(stream_id); |
215 return; | 233 return; |
216 } | 234 } |
217 | 235 |
218 // Create sync reader and try to initialize it. | 236 // Create sync reader and try to initialize it. |
219 scoped_ptr<AudioSyncReader> reader( | 237 scoped_ptr<AudioSyncReader> reader( |
220 new AudioSyncReader(&entry->shared_memory, params)); | 238 new AudioSyncReader(&entry->shared_memory, params, input_channels)); |
221 | 239 |
222 if (!reader->Init()) { | 240 if (!reader->Init()) { |
223 SendErrorMessage(stream_id); | 241 SendErrorMessage(stream_id); |
224 return; | 242 return; |
225 } | 243 } |
226 | 244 |
227 // If we have successfully created the SyncReader then assign it to the | 245 // If we have successfully created the SyncReader then assign it to the |
228 // entry and construct an AudioOutputController. | 246 // entry and construct an AudioOutputController. |
229 entry->reader.reset(reader.release()); | 247 entry->reader.reset(reader.release()); |
230 entry->controller = media::AudioOutputController::Create( | 248 entry->controller = media::AudioOutputController::Create( |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 | 397 |
380 // Iterate the map of entries. | 398 // Iterate the map of entries. |
381 // TODO(hclam): Implement a faster look up method. | 399 // TODO(hclam): Implement a faster look up method. |
382 for (AudioEntryMap::iterator i = audio_entries_.begin(); | 400 for (AudioEntryMap::iterator i = audio_entries_.begin(); |
383 i != audio_entries_.end(); ++i) { | 401 i != audio_entries_.end(); ++i) { |
384 if (!i->second->pending_close && controller == i->second->controller.get()) | 402 if (!i->second->pending_close && controller == i->second->controller.get()) |
385 return i->second; | 403 return i->second; |
386 } | 404 } |
387 return NULL; | 405 return NULL; |
388 } | 406 } |
OLD | NEW |