Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(836)

Side by Side Diff: content/browser/renderer_host/media/audio_renderer_host.cc

Issue 15979015: Reland 15721002: Hook up the device selection to the WebAudio live audio (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed the comments. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
11 #include "base/process.h" 11 #include "base/process.h"
12 #include "base/shared_memory.h" 12 #include "base/shared_memory.h"
13 #include "content/browser/browser_main_loop.h" 13 #include "content/browser/browser_main_loop.h"
14 #include "content/browser/media/media_internals.h" 14 #include "content/browser/media/media_internals.h"
15 #include "content/browser/renderer_host/media/audio_input_device_manager.h"
15 #include "content/browser/renderer_host/media/audio_mirroring_manager.h" 16 #include "content/browser/renderer_host/media/audio_mirroring_manager.h"
16 #include "content/browser/renderer_host/media/audio_sync_reader.h" 17 #include "content/browser/renderer_host/media/audio_sync_reader.h"
18 #include "content/browser/renderer_host/media/media_stream_manager.h"
17 #include "content/common/media/audio_messages.h" 19 #include "content/common/media/audio_messages.h"
18 #include "content/public/browser/content_browser_client.h" 20 #include "content/public/browser/content_browser_client.h"
19 #include "content/public/browser/media_observer.h" 21 #include "content/public/browser/media_observer.h"
20 #include "content/public/common/content_switches.h" 22 #include "content/public/common/content_switches.h"
23 #include "media/audio/audio_manager_base.h"
21 #include "media/audio/shared_memory_util.h" 24 #include "media/audio/shared_memory_util.h"
22 #include "media/base/audio_bus.h" 25 #include "media/base/audio_bus.h"
23 #include "media/base/limits.h" 26 #include "media/base/limits.h"
24 27
25 using media::AudioBus; 28 using media::AudioBus;
26 29
27 namespace content { 30 namespace content {
28 31
29 class AudioRendererHost::AudioEntry 32 class AudioRendererHost::AudioEntry
30 : public media::AudioOutputController::EventHandler { 33 : public media::AudioOutputController::EventHandler {
31 public: 34 public:
32 AudioEntry(AudioRendererHost* host, 35 AudioEntry(AudioRendererHost* host,
33 int stream_id, 36 int stream_id,
34 int render_view_id, 37 int render_view_id,
35 const media::AudioParameters& params, 38 const media::AudioParameters& params,
39 const std::string& input_device_id,
36 scoped_ptr<base::SharedMemory> shared_memory, 40 scoped_ptr<base::SharedMemory> shared_memory,
37 scoped_ptr<media::AudioOutputController::SyncReader> reader); 41 scoped_ptr<media::AudioOutputController::SyncReader> reader);
38 virtual ~AudioEntry(); 42 virtual ~AudioEntry();
39 43
40 int stream_id() const { 44 int stream_id() const {
41 return stream_id_; 45 return stream_id_;
42 } 46 }
43 47
44 int render_view_id() const { 48 int render_view_id() const {
45 return render_view_id_; 49 return render_view_id_;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 // Shared memory for transmission of the audio data. 81 // Shared memory for transmission of the audio data.
78 const scoped_ptr<base::SharedMemory> shared_memory_; 82 const scoped_ptr<base::SharedMemory> shared_memory_;
79 83
80 // The synchronous reader to be used by the controller. 84 // The synchronous reader to be used by the controller.
81 const scoped_ptr<media::AudioOutputController::SyncReader> reader_; 85 const scoped_ptr<media::AudioOutputController::SyncReader> reader_;
82 }; 86 };
83 87
84 AudioRendererHost::AudioEntry::AudioEntry( 88 AudioRendererHost::AudioEntry::AudioEntry(
85 AudioRendererHost* host, int stream_id, int render_view_id, 89 AudioRendererHost* host, int stream_id, int render_view_id,
86 const media::AudioParameters& params, 90 const media::AudioParameters& params,
91 const std::string& input_device_id,
87 scoped_ptr<base::SharedMemory> shared_memory, 92 scoped_ptr<base::SharedMemory> shared_memory,
88 scoped_ptr<media::AudioOutputController::SyncReader> reader) 93 scoped_ptr<media::AudioOutputController::SyncReader> reader)
89 : host_(host), 94 : host_(host),
90 stream_id_(stream_id), 95 stream_id_(stream_id),
91 render_view_id_(render_view_id), 96 render_view_id_(render_view_id),
92 controller_(media::AudioOutputController::Create( 97 controller_(media::AudioOutputController::Create(
93 host->audio_manager_, this, params, reader.get())), 98 host->audio_manager_, this, params, input_device_id, reader.get())),
94 shared_memory_(shared_memory.Pass()), 99 shared_memory_(shared_memory.Pass()),
95 reader_(reader.Pass()) { 100 reader_(reader.Pass()) {
96 DCHECK(controller_.get()); 101 DCHECK(controller_.get());
97 } 102 }
98 103
99 AudioRendererHost::AudioEntry::~AudioEntry() {} 104 AudioRendererHost::AudioEntry::~AudioEntry() {}
100 105
101 /////////////////////////////////////////////////////////////////////////////// 106 ///////////////////////////////////////////////////////////////////////////////
102 // AudioRendererHost implementations. 107 // AudioRendererHost implementations.
103 AudioRendererHost::AudioRendererHost( 108 AudioRendererHost::AudioRendererHost(
104 int render_process_id, 109 int render_process_id,
105 media::AudioManager* audio_manager, 110 media::AudioManager* audio_manager,
106 AudioMirroringManager* mirroring_manager, 111 AudioMirroringManager* mirroring_manager,
107 MediaInternals* media_internals) 112 MediaInternals* media_internals,
113 MediaStreamManager* media_stream_manager)
108 : render_process_id_(render_process_id), 114 : render_process_id_(render_process_id),
109 audio_manager_(audio_manager), 115 audio_manager_(audio_manager),
110 mirroring_manager_(mirroring_manager), 116 mirroring_manager_(mirroring_manager),
111 media_internals_(media_internals) { 117 media_internals_(media_internals),
118 media_stream_manager_(media_stream_manager) {
112 DCHECK(audio_manager_); 119 DCHECK(audio_manager_);
120 DCHECK(media_stream_manager_);
113 } 121 }
114 122
115 AudioRendererHost::~AudioRendererHost() { 123 AudioRendererHost::~AudioRendererHost() {
116 DCHECK(audio_entries_.empty()); 124 DCHECK(audio_entries_.empty());
117 } 125 }
118 126
119 void AudioRendererHost::OnChannelClosing() { 127 void AudioRendererHost::OnChannelClosing() {
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
121 129
122 BrowserMessageFilter::OnChannelClosing(); 130 BrowserMessageFilter::OnChannelClosing();
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 IPC_MESSAGE_HANDLER(AudioHostMsg_PauseStream, OnPauseStream) 266 IPC_MESSAGE_HANDLER(AudioHostMsg_PauseStream, OnPauseStream)
259 IPC_MESSAGE_HANDLER(AudioHostMsg_CloseStream, OnCloseStream) 267 IPC_MESSAGE_HANDLER(AudioHostMsg_CloseStream, OnCloseStream)
260 IPC_MESSAGE_HANDLER(AudioHostMsg_SetVolume, OnSetVolume) 268 IPC_MESSAGE_HANDLER(AudioHostMsg_SetVolume, OnSetVolume)
261 IPC_MESSAGE_UNHANDLED(handled = false) 269 IPC_MESSAGE_UNHANDLED(handled = false)
262 IPC_END_MESSAGE_MAP_EX() 270 IPC_END_MESSAGE_MAP_EX()
263 271
264 return handled; 272 return handled;
265 } 273 }
266 274
267 void AudioRendererHost::OnCreateStream( 275 void AudioRendererHost::OnCreateStream(
268 int stream_id, int render_view_id, const media::AudioParameters& params) { 276 int stream_id, int render_view_id, int session_id,
277 const media::AudioParameters& params) {
269 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
270 279
271 DVLOG(1) << "AudioRendererHost@" << this 280 DVLOG(1) << "AudioRendererHost@" << this
272 << "::OnCreateStream(stream_id=" << stream_id 281 << "::OnCreateStream(stream_id=" << stream_id
273 << ", render_view_id=" << render_view_id << ")"; 282 << ", render_view_id=" << render_view_id
283 << ", session_id=" << session_id << ")";
274 DCHECK_GT(render_view_id, 0); 284 DCHECK_GT(render_view_id, 0);
275 285
276 // media::AudioParameters is validated in the deserializer. 286 // media::AudioParameters is validated in the deserializer.
277 int input_channels = params.input_channels(); 287 int input_channels = params.input_channels();
278 if (input_channels < 0 || 288 if (input_channels < 0 ||
279 input_channels > media::limits::kMaxChannels || 289 input_channels > media::limits::kMaxChannels ||
280 LookupById(stream_id) != NULL) { 290 LookupById(stream_id) != NULL) {
281 SendErrorMessage(stream_id); 291 SendErrorMessage(stream_id);
282 return; 292 return;
283 } 293 }
284 294
295 // When the |input_channels| is valid, clients are trying to create a unified
296 // IO stream which opens an input device mapping to the |session_id|.
297 std::string input_device_id;
298 if (input_channels > 0) {
299 const StreamDeviceInfo* info = media_stream_manager_->
300 audio_input_device_manager()->GetOpenedDeviceInfoById(session_id);
301 if (!info) {
302 SendErrorMessage(stream_id);
303 DLOG(WARNING) << "No permission has been granted to input stream with "
304 << "session_id=" << session_id;
305 return;
306 }
307
308 input_device_id = info->device.id;
309 }
310
285 // Calculate output and input memory size. 311 // Calculate output and input memory size.
286 int output_memory_size = AudioBus::CalculateMemorySize(params); 312 int output_memory_size = AudioBus::CalculateMemorySize(params);
287 int frames = params.frames_per_buffer(); 313 int frames = params.frames_per_buffer();
288 int input_memory_size = 314 int input_memory_size =
289 AudioBus::CalculateMemorySize(input_channels, frames); 315 AudioBus::CalculateMemorySize(input_channels, frames);
290 316
291 // Create the shared memory and share with the renderer process. 317 // Create the shared memory and share with the renderer process.
292 // For synchronized I/O (if input_channels > 0) then we allocate 318 // For synchronized I/O (if input_channels > 0) then we allocate
293 // extra memory after the output data for the input data. 319 // extra memory after the output data for the input data.
294 uint32 io_buffer_size = output_memory_size + input_memory_size; 320 uint32 io_buffer_size = output_memory_size + input_memory_size;
295 uint32 shared_memory_size = 321 uint32 shared_memory_size =
296 media::TotalSharedMemorySizeInBytes(io_buffer_size); 322 media::TotalSharedMemorySizeInBytes(io_buffer_size);
297 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); 323 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory());
298 if (!shared_memory->CreateAndMapAnonymous(shared_memory_size)) { 324 if (!shared_memory->CreateAndMapAnonymous(shared_memory_size)) {
299 SendErrorMessage(stream_id); 325 SendErrorMessage(stream_id);
300 return; 326 return;
301 } 327 }
302 328
303 scoped_ptr<AudioSyncReader> reader( 329 scoped_ptr<AudioSyncReader> reader(
304 new AudioSyncReader(shared_memory.get(), params, input_channels)); 330 new AudioSyncReader(shared_memory.get(), params, input_channels));
305 if (!reader->Init()) { 331 if (!reader->Init()) {
306 SendErrorMessage(stream_id); 332 SendErrorMessage(stream_id);
307 return; 333 return;
308 } 334 }
309 335
310 scoped_ptr<AudioEntry> entry(new AudioEntry( 336 scoped_ptr<AudioEntry> entry(new AudioEntry(
311 this, stream_id, render_view_id, params, shared_memory.Pass(), 337 this, stream_id, render_view_id, params, input_device_id,
338 shared_memory.Pass(),
312 reader.PassAs<media::AudioOutputController::SyncReader>())); 339 reader.PassAs<media::AudioOutputController::SyncReader>()));
313 if (mirroring_manager_) { 340 if (mirroring_manager_) {
314 mirroring_manager_->AddDiverter( 341 mirroring_manager_->AddDiverter(
315 render_process_id_, entry->render_view_id(), entry->controller()); 342 render_process_id_, entry->render_view_id(), entry->controller());
316 } 343 }
317 audio_entries_.insert(std::make_pair(stream_id, entry.release())); 344 audio_entries_.insert(std::make_pair(stream_id, entry.release()));
318 if (media_internals_) 345 if (media_internals_)
319 media_internals_->OnSetAudioStreamStatus(this, stream_id, "created"); 346 media_internals_->OnSetAudioStreamStatus(this, stream_id, "created");
320 } 347 }
321 348
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 } 455 }
429 456
430 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { 457 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) {
431 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 458 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
432 459
433 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id); 460 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id);
434 return i != audio_entries_.end() ? i->second : NULL; 461 return i != audio_entries_.end() ? i->second : NULL;
435 } 462 }
436 463
437 } // namespace content 464 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698