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/renderer/media/renderer_webaudiodevice_impl.h" | 5 #include "content/renderer/media/renderer_webaudiodevice_impl.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "content/renderer/media/audio_device_factory.h" | 9 #include "content/renderer/media/audio_device_factory.h" |
10 #include "content/renderer/media/renderer_audio_output_device.h" | |
11 #include "content/renderer/render_view_impl.h" | |
10 #include "media/base/media_switches.h" | 12 #include "media/base/media_switches.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
11 | 15 |
12 using WebKit::WebAudioDevice; | 16 using WebKit::WebAudioDevice; |
17 using WebKit::WebFrame; | |
13 using WebKit::WebVector; | 18 using WebKit::WebVector; |
19 using WebKit::WebView; | |
14 | 20 |
15 namespace content { | 21 namespace content { |
16 | 22 |
17 RendererWebAudioDeviceImpl::RendererWebAudioDeviceImpl( | 23 RendererWebAudioDeviceImpl::RendererWebAudioDeviceImpl( |
18 const media::AudioParameters& params, | 24 const media::AudioParameters& params, |
19 WebAudioDevice::RenderCallback* callback) | 25 WebAudioDevice::RenderCallback* callback) |
20 : is_running_(false), | 26 : params_(params), |
21 client_callback_(callback) { | 27 client_callback_(callback) { |
22 audio_device_ = AudioDeviceFactory::NewOutputDevice(); | 28 DCHECK(client_callback_); |
29 } | |
30 | |
31 RendererWebAudioDeviceImpl::~RendererWebAudioDeviceImpl() { | |
32 DCHECK(!output_device_); | |
33 } | |
34 | |
35 void RendererWebAudioDeviceImpl::start() { | |
36 DCHECK(thread_checker_.CalledOnValidThread()); | |
37 | |
38 if (output_device_) | |
39 return; // Already started. | |
40 | |
41 output_device_ = AudioDeviceFactory::NewOutputDevice(); | |
23 | 42 |
24 // TODO(crogers): remove once we properly handle input device selection. | 43 // TODO(crogers): remove once we properly handle input device selection. |
25 // https://code.google.com/p/chromium/issues/detail?id=147327 | 44 // https://code.google.com/p/chromium/issues/detail?id=147327 |
26 if (CommandLine::ForCurrentProcess()->HasSwitch( | 45 if (CommandLine::ForCurrentProcess()->HasSwitch( |
27 switches::kEnableWebAudioInput)) { | 46 switches::kEnableWebAudioInput)) { |
28 // TODO(crogers): support more than hard-coded stereo: | 47 // TODO(crogers): support more than hard-coded stereo: |
29 // https://code.google.com/p/chromium/issues/detail?id=147326 | 48 // https://code.google.com/p/chromium/issues/detail?id=147326 |
30 audio_device_->InitializeIO(params, 2, this); | 49 output_device_->InitializeIO(params_, 2, this); |
31 } else { | 50 } else { |
32 audio_device_->Initialize(params, this); | 51 output_device_->Initialize(params_, this); |
33 } | 52 } |
34 } | |
35 | 53 |
36 RendererWebAudioDeviceImpl::~RendererWebAudioDeviceImpl() { | 54 // Assumption: This method is being invoked within a V8 call stack. CHECKs |
37 stop(); | 55 // will fail in the call to frameForCurrentContext() otherwise. |
38 } | 56 // |
57 // Therefore, we can perform look-ups to determine which RenderView is | |
58 // starting the audio device. The reason for all this is because the creator | |
59 // of the WebAudio objects might not be the actual source of the audio (e.g., | |
60 // an extension creates a object that is passed and used within a page). | |
61 WebFrame* const web_frame = WebFrame::frameForCurrentContext(); | |
62 WebView* const web_view = web_frame ? web_frame->view() : NULL; | |
63 RenderViewImpl* const render_view = | |
64 web_view ? RenderViewImpl::FromWebView(web_view) : NULL; | |
65 if (render_view) { | |
66 output_device_->SetSourceRenderView(render_view->routing_id()); | |
67 } | |
39 | 68 |
40 void RendererWebAudioDeviceImpl::start() { | 69 output_device_->Start(); |
41 if (!is_running_) { | 70 // Note: Default behavior is to auto-play on start. |
42 audio_device_->Start(); | |
43 is_running_ = true; | |
44 } | |
45 } | 71 } |
46 | 72 |
47 void RendererWebAudioDeviceImpl::stop() { | 73 void RendererWebAudioDeviceImpl::stop() { |
48 if (is_running_) { | 74 DCHECK(thread_checker_.CalledOnValidThread()); |
49 audio_device_->Stop(); | 75 |
50 is_running_ = false; | 76 if (output_device_) { |
77 output_device_->Stop(); | |
78 output_device_ = NULL; | |
51 } | 79 } |
52 } | 80 } |
53 | 81 |
54 double RendererWebAudioDeviceImpl::sampleRate() { | 82 double RendererWebAudioDeviceImpl::sampleRate() { |
55 return 44100.0; | 83 DCHECK(thread_checker_.CalledOnValidThread()); |
Chris Rogers
2012/12/03 20:50:11
I would remove this DCHECK since params_ should be
miu
2012/12/03 22:39:14
Done.
| |
84 | |
85 return params_.sample_rate(); | |
56 } | 86 } |
57 | 87 |
58 int RendererWebAudioDeviceImpl::Render(media::AudioBus* dest, | 88 int RendererWebAudioDeviceImpl::Render(media::AudioBus* dest, |
59 int audio_delay_milliseconds) { | 89 int audio_delay_milliseconds) { |
60 RenderIO(NULL, dest, audio_delay_milliseconds); | 90 RenderIO(NULL, dest, audio_delay_milliseconds); |
61 return dest->frames(); | 91 return dest->frames(); |
62 } | 92 } |
63 | 93 |
64 void RendererWebAudioDeviceImpl::RenderIO(media::AudioBus* source, | 94 void RendererWebAudioDeviceImpl::RenderIO(media::AudioBus* source, |
65 media::AudioBus* dest, | 95 media::AudioBus* dest, |
66 int audio_delay_milliseconds) { | 96 int audio_delay_milliseconds) { |
67 // Make the client callback for an I/O cycle. | 97 // Make the client callback for an I/O cycle. |
68 DCHECK(client_callback_); | |
69 if (client_callback_) { | 98 if (client_callback_) { |
70 // Wrap the input pointers using WebVector. | 99 // Wrap the input pointers using WebVector. |
71 size_t input_channels = | 100 size_t input_channels = |
72 source ? static_cast<size_t>(source->channels()) : 0; | 101 source ? static_cast<size_t>(source->channels()) : 0; |
73 WebVector<float*> web_audio_input_data(input_channels); | 102 WebVector<float*> web_audio_input_data(input_channels); |
74 for (size_t i = 0; i < input_channels; ++i) | 103 for (size_t i = 0; i < input_channels; ++i) |
75 web_audio_input_data[i] = source->channel(i); | 104 web_audio_input_data[i] = source->channel(i); |
76 | 105 |
77 // Wrap the output pointers using WebVector. | 106 // Wrap the output pointers using WebVector. |
78 WebVector<float*> web_audio_data( | 107 WebVector<float*> web_audio_data( |
79 static_cast<size_t>(dest->channels())); | 108 static_cast<size_t>(dest->channels())); |
80 for (int i = 0; i < dest->channels(); ++i) | 109 for (int i = 0; i < dest->channels(); ++i) |
81 web_audio_data[i] = dest->channel(i); | 110 web_audio_data[i] = dest->channel(i); |
82 | 111 |
83 client_callback_->render(web_audio_input_data, | 112 client_callback_->render(web_audio_input_data, |
84 web_audio_data, | 113 web_audio_data, |
85 dest->frames()); | 114 dest->frames()); |
86 } | 115 } |
87 } | 116 } |
88 | 117 |
89 void RendererWebAudioDeviceImpl::OnRenderError() { | 118 void RendererWebAudioDeviceImpl::OnRenderError() { |
90 // TODO(crogers): implement error handling. | 119 // TODO(crogers): implement error handling. |
91 } | 120 } |
92 | 121 |
93 } // namespace content | 122 } // namespace content |
OLD | NEW |