Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / renderer_webaudiodevice_impl.cc
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.
4
5 #include "content/renderer/media/renderer_webaudiodevice_impl.h"
6
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"
16
17 using blink::WebAudioDevice;
18 using blink::WebLocalFrame;
19 using blink::WebVector;
20 using blink::WebView;
21
22 namespace content {
23
24 RendererWebAudioDeviceImpl::RendererWebAudioDeviceImpl(
25     const media::AudioParameters& params,
26     WebAudioDevice::RenderCallback* callback,
27     int session_id)
28     : params_(params),
29       client_callback_(callback),
30       session_id_(session_id) {
31   DCHECK(client_callback_);
32 }
33
34 RendererWebAudioDeviceImpl::~RendererWebAudioDeviceImpl() {
35   DCHECK(!output_device_.get());
36 }
37
38 void RendererWebAudioDeviceImpl::start() {
39   DCHECK(thread_checker_.CalledOnValidThread());
40
41   if (output_device_.get())
42     return;  // Already started.
43
44   // Assumption: This method is being invoked within a V8 call stack.  CHECKs
45   // will fail in the call to frameForCurrentContext() otherwise.
46   //
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.
63 }
64
65 void RendererWebAudioDeviceImpl::stop() {
66   DCHECK(thread_checker_.CalledOnValidThread());
67
68   if (output_device_.get()) {
69     output_device_->Stop();
70     output_device_ = NULL;
71   }
72 }
73
74 double RendererWebAudioDeviceImpl::sampleRate() {
75   return params_.sample_rate();
76 }
77
78 int RendererWebAudioDeviceImpl::Render(media::AudioBus* dest,
79                                        int audio_delay_milliseconds) {
80   RenderIO(NULL, dest, audio_delay_milliseconds);
81   return dest->frames();
82 }
83
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);
95
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);
101
102     client_callback_->render(web_audio_source_data,
103                              web_audio_dest_data,
104                              dest->frames());
105   }
106 }
107
108 void RendererWebAudioDeviceImpl::OnRenderError() {
109   // TODO(crogers): implement error handling.
110 }
111
112 }  // namespace content