Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / media / renderer_webaudiodevice_impl.cc
blobd816db3a418d1a6c35bd6805a3bb65313045262e
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 <string>
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/time/time.h"
14 #include "content/renderer/media/audio_device_factory.h"
15 #include "content/renderer/render_frame_impl.h"
16 #include "media/audio/audio_output_device.h"
17 #include "media/audio/null_audio_sink.h"
18 #include "media/base/media_switches.h"
19 #include "third_party/WebKit/public/web/WebLocalFrame.h"
20 #include "third_party/WebKit/public/web/WebView.h"
22 using blink::WebAudioDevice;
23 using blink::WebLocalFrame;
24 using blink::WebVector;
25 using blink::WebView;
27 namespace content {
29 #if defined(OS_ANDROID)
30 static const int kSilenceInSecondsToEnterIdleMode = 30;
31 #endif
33 RendererWebAudioDeviceImpl::RendererWebAudioDeviceImpl(
34 const media::AudioParameters& params,
35 WebAudioDevice::RenderCallback* callback,
36 int session_id)
37 : params_(params),
38 client_callback_(callback),
39 session_id_(session_id),
40 task_runner_(base::ThreadTaskRunnerHandle::Get()),
41 null_audio_sink_(new media::NullAudioSink(task_runner_)),
42 is_using_null_audio_sink_(false),
43 first_buffer_after_silence_(media::AudioBus::Create(params_)),
44 is_first_buffer_after_silence_(false) {
45 DCHECK(client_callback_);
46 null_audio_sink_->Initialize(params_, this);
49 RendererWebAudioDeviceImpl::~RendererWebAudioDeviceImpl() {
50 DCHECK(!output_device_);
53 void RendererWebAudioDeviceImpl::start() {
54 DCHECK(thread_checker_.CalledOnValidThread());
56 if (output_device_)
57 return; // Already started.
59 // Assumption: This method is being invoked within a V8 call stack. CHECKs
60 // will fail in the call to frameForCurrentContext() otherwise.
62 // Therefore, we can perform look-ups to determine which RenderView is
63 // starting the audio device. The reason for all this is because the creator
64 // of the WebAudio objects might not be the actual source of the audio (e.g.,
65 // an extension creates a object that is passed and used within a page).
66 WebLocalFrame* const web_frame = WebLocalFrame::frameForCurrentContext();
67 RenderFrame* const render_frame =
68 web_frame ? RenderFrame::FromWebFrame(web_frame) : NULL;
69 output_device_ = AudioDeviceFactory::NewOutputDevice(
70 render_frame ? render_frame->GetRoutingID() : MSG_ROUTING_NONE,
71 session_id_, std::string(), url::Origin());
72 output_device_->Initialize(params_, this);
73 output_device_->Start();
74 start_null_audio_sink_callback_.Reset(
75 base::Bind(&media::NullAudioSink::Play, null_audio_sink_));
76 // Note: Default behavior is to auto-play on start.
79 void RendererWebAudioDeviceImpl::stop() {
80 DCHECK(thread_checker_.CalledOnValidThread());
82 if (output_device_) {
83 output_device_->Stop();
84 output_device_ = NULL;
86 null_audio_sink_->Stop();
87 is_using_null_audio_sink_ = false;
88 is_first_buffer_after_silence_ = false;
89 start_null_audio_sink_callback_.Cancel();
92 double RendererWebAudioDeviceImpl::sampleRate() {
93 return params_.sample_rate();
96 int RendererWebAudioDeviceImpl::Render(media::AudioBus* dest,
97 int audio_delay_milliseconds) {
98 #if defined(OS_ANDROID)
99 if (is_first_buffer_after_silence_) {
100 DCHECK(!is_using_null_audio_sink_);
101 first_buffer_after_silence_->CopyTo(dest);
102 is_first_buffer_after_silence_ = false;
103 return dest->frames();
105 #endif
106 // Wrap the output pointers using WebVector.
107 WebVector<float*> web_audio_dest_data(
108 static_cast<size_t>(dest->channels()));
109 for (int i = 0; i < dest->channels(); ++i)
110 web_audio_dest_data[i] = dest->channel(i);
112 // TODO(xians): Remove the following |web_audio_source_data| after
113 // changing the blink interface.
114 WebVector<float*> web_audio_source_data(static_cast<size_t>(0));
115 client_callback_->render(web_audio_source_data,
116 web_audio_dest_data,
117 dest->frames());
119 #if defined(OS_ANDROID)
120 const bool is_zero = dest->AreFramesZero();
121 if (!is_zero) {
122 first_silence_time_ = base::TimeTicks();
123 if (is_using_null_audio_sink_) {
124 // This is called on the main render thread when audio is detected.
125 output_device_->Play();
126 is_using_null_audio_sink_ = false;
127 is_first_buffer_after_silence_ = true;
128 dest->CopyTo(first_buffer_after_silence_.get());
129 task_runner_->PostTask(
130 FROM_HERE,
131 base::Bind(&media::NullAudioSink::Stop, null_audio_sink_));
133 } else if (!is_using_null_audio_sink_) {
134 // Called on the audio device thread.
135 const base::TimeTicks now = base::TimeTicks::Now();
136 if (first_silence_time_.is_null())
137 first_silence_time_ = now;
138 if (now - first_silence_time_
139 > base::TimeDelta::FromSeconds(kSilenceInSecondsToEnterIdleMode)) {
140 output_device_->Pause();
141 is_using_null_audio_sink_ = true;
142 // If Stop() is called right after the task is posted, need to cancel
143 // this task.
144 task_runner_->PostDelayedTask(
145 FROM_HERE,
146 start_null_audio_sink_callback_.callback(),
147 params_.GetBufferDuration());
150 #endif
151 return dest->frames();
154 void RendererWebAudioDeviceImpl::OnRenderError() {
155 // TODO(crogers): implement error handling.
158 } // namespace content