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
3 // found in the LICENSE file.
5 #include "content/renderer/media/renderer_webaudiodevice_impl.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "content/renderer/media/audio_device_factory.h"
10 #include "content/renderer/render_frame_impl.h"
11 #include "content/renderer/render_view_impl.h"
12 #include "media/audio/audio_output_device.h"
13 #include "media/base/media_switches.h"
14 #include "third_party/WebKit/public/web/WebLocalFrame.h"
15 #include "third_party/WebKit/public/web/WebView.h"
17 using blink::WebAudioDevice;
18 using blink::WebLocalFrame;
19 using blink::WebVector;
24 RendererWebAudioDeviceImpl::RendererWebAudioDeviceImpl(
25 const media::AudioParameters& params,
26 WebAudioDevice::RenderCallback* callback,
29 client_callback_(callback),
30 session_id_(session_id) {
31 DCHECK(client_callback_);
34 RendererWebAudioDeviceImpl::~RendererWebAudioDeviceImpl() {
35 DCHECK(!output_device_.get());
38 void RendererWebAudioDeviceImpl::start() {
39 DCHECK(thread_checker_.CalledOnValidThread());
41 if (output_device_.get())
42 return; // Already started.
44 // Assumption: This method is being invoked within a V8 call stack. CHECKs
45 // will fail in the call to frameForCurrentContext() otherwise.
47 // Therefore, we can perform look-ups to determine which RenderView is
48 // starting the audio device. The reason for all this is because the creator
49 // of the WebAudio objects might not be the actual source of the audio (e.g.,
50 // an extension creates a object that is passed and used within a page).
51 WebLocalFrame* const web_frame = WebLocalFrame::frameForCurrentContext();
52 WebView* const web_view = web_frame ? web_frame->view() : NULL;
53 RenderFrame* const render_frame =
54 web_frame ? RenderFrame::FromWebFrame(web_frame) : NULL;
55 RenderViewImpl* const render_view =
56 web_view ? RenderViewImpl::FromWebView(web_view) : NULL;
57 output_device_ = AudioDeviceFactory::NewOutputDevice(
58 render_view ? render_view->routing_id() : MSG_ROUTING_NONE,
59 render_frame ? render_frame->GetRoutingID(): MSG_ROUTING_NONE);
60 output_device_->InitializeUnifiedStream(params_, this, session_id_);
61 output_device_->Start();
62 // Note: Default behavior is to auto-play on start.
65 void RendererWebAudioDeviceImpl::stop() {
66 DCHECK(thread_checker_.CalledOnValidThread());
68 if (output_device_.get()) {
69 output_device_->Stop();
70 output_device_ = NULL;
74 double RendererWebAudioDeviceImpl::sampleRate() {
75 return params_.sample_rate();
78 int RendererWebAudioDeviceImpl::Render(media::AudioBus* dest,
79 int audio_delay_milliseconds) {
80 RenderIO(NULL, dest, audio_delay_milliseconds);
81 return dest->frames();
84 void RendererWebAudioDeviceImpl::RenderIO(media::AudioBus* source,
85 media::AudioBus* dest,
86 int audio_delay_milliseconds) {
87 // Make the client callback for an I/O cycle.
88 if (client_callback_) {
89 // Wrap the input pointers using WebVector.
90 size_t source_channels =
91 source ? static_cast<size_t>(source->channels()) : 0;
92 WebVector<float*> web_audio_source_data(source_channels);
93 for (size_t i = 0; i < source_channels; ++i)
94 web_audio_source_data[i] = source->channel(i);
96 // Wrap the output pointers using WebVector.
97 WebVector<float*> web_audio_dest_data(
98 static_cast<size_t>(dest->channels()));
99 for (int i = 0; i < dest->channels(); ++i)
100 web_audio_dest_data[i] = dest->channel(i);
102 client_callback_->render(web_audio_source_data,
108 void RendererWebAudioDeviceImpl::OnRenderError() {
109 // TODO(crogers): implement error handling.
112 } // namespace content