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