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 "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/time/time.h"
12 #include "content/renderer/media/audio_device_factory.h"
13 #include "content/renderer/render_frame_impl.h"
14 #include "media/audio/audio_output_device.h"
15 #include "media/audio/null_audio_sink.h"
16 #include "media/base/media_switches.h"
17 #include "third_party/WebKit/public/web/WebLocalFrame.h"
18 #include "third_party/WebKit/public/web/WebView.h"
20 using blink::WebAudioDevice
;
21 using blink::WebLocalFrame
;
22 using blink::WebVector
;
27 #if defined(OS_ANDROID)
28 static const int kSilenceInSecondsToEnterIdleMode
= 30;
31 RendererWebAudioDeviceImpl::RendererWebAudioDeviceImpl(
32 const media::AudioParameters
& params
,
33 WebAudioDevice::RenderCallback
* callback
,
36 client_callback_(callback
),
37 session_id_(session_id
),
38 task_runner_(base::ThreadTaskRunnerHandle::Get()),
39 null_audio_sink_(new media::NullAudioSink(task_runner_
)),
40 is_using_null_audio_sink_(false),
41 first_buffer_after_silence_(media::AudioBus::Create(params_
)),
42 is_first_buffer_after_silence_(false) {
43 DCHECK(client_callback_
);
44 null_audio_sink_
->Initialize(params_
, this);
47 RendererWebAudioDeviceImpl::~RendererWebAudioDeviceImpl() {
48 DCHECK(!output_device_
);
51 void RendererWebAudioDeviceImpl::start() {
52 DCHECK(thread_checker_
.CalledOnValidThread());
55 return; // Already started.
57 // Assumption: This method is being invoked within a V8 call stack. CHECKs
58 // will fail in the call to frameForCurrentContext() otherwise.
60 // Therefore, we can perform look-ups to determine which RenderView is
61 // starting the audio device. The reason for all this is because the creator
62 // of the WebAudio objects might not be the actual source of the audio (e.g.,
63 // an extension creates a object that is passed and used within a page).
64 WebLocalFrame
* const web_frame
= WebLocalFrame::frameForCurrentContext();
65 RenderFrame
* const render_frame
=
66 web_frame
? RenderFrame::FromWebFrame(web_frame
) : NULL
;
67 output_device_
= AudioDeviceFactory::NewOutputDevice(
68 render_frame
? render_frame
->GetRoutingID(): MSG_ROUTING_NONE
);
69 output_device_
->InitializeWithSessionId(params_
, this, session_id_
);
70 output_device_
->Start();
71 start_null_audio_sink_callback_
.Reset(
72 base::Bind(&media::NullAudioSink::Play
, null_audio_sink_
));
73 // Note: Default behavior is to auto-play on start.
76 void RendererWebAudioDeviceImpl::stop() {
77 DCHECK(thread_checker_
.CalledOnValidThread());
80 output_device_
->Stop();
81 output_device_
= NULL
;
83 null_audio_sink_
->Stop();
84 is_using_null_audio_sink_
= false;
85 is_first_buffer_after_silence_
= false;
86 start_null_audio_sink_callback_
.Cancel();
89 double RendererWebAudioDeviceImpl::sampleRate() {
90 return params_
.sample_rate();
93 int RendererWebAudioDeviceImpl::Render(media::AudioBus
* dest
,
94 int audio_delay_milliseconds
) {
95 #if defined(OS_ANDROID)
96 if (is_first_buffer_after_silence_
) {
97 DCHECK(!is_using_null_audio_sink_
);
98 first_buffer_after_silence_
->CopyTo(dest
);
99 is_first_buffer_after_silence_
= false;
100 return dest
->frames();
103 // Wrap the output pointers using WebVector.
104 WebVector
<float*> web_audio_dest_data(
105 static_cast<size_t>(dest
->channels()));
106 for (int i
= 0; i
< dest
->channels(); ++i
)
107 web_audio_dest_data
[i
] = dest
->channel(i
);
109 // TODO(xians): Remove the following |web_audio_source_data| after
110 // changing the blink interface.
111 WebVector
<float*> web_audio_source_data(static_cast<size_t>(0));
112 client_callback_
->render(web_audio_source_data
,
116 #if defined(OS_ANDROID)
117 const bool is_zero
= dest
->AreFramesZero();
119 first_silence_time_
= base::TimeTicks();
120 if (is_using_null_audio_sink_
) {
121 // This is called on the main render thread when audio is detected.
122 output_device_
->Play();
123 is_using_null_audio_sink_
= false;
124 is_first_buffer_after_silence_
= true;
125 dest
->CopyTo(first_buffer_after_silence_
.get());
126 task_runner_
->PostTask(
128 base::Bind(&media::NullAudioSink::Stop
, null_audio_sink_
));
130 } else if (!is_using_null_audio_sink_
) {
131 // Called on the audio device thread.
132 const base::TimeTicks now
= base::TimeTicks::Now();
133 if (first_silence_time_
.is_null())
134 first_silence_time_
= now
;
135 if (now
- first_silence_time_
136 > base::TimeDelta::FromSeconds(kSilenceInSecondsToEnterIdleMode
)) {
137 output_device_
->Pause();
138 is_using_null_audio_sink_
= true;
139 // If Stop() is called right after the task is posted, need to cancel
141 task_runner_
->PostDelayedTask(
143 start_null_audio_sink_callback_
.callback(),
144 params_
.GetBufferDuration());
148 return dest
->frames();
151 void RendererWebAudioDeviceImpl::OnRenderError() {
152 // TODO(crogers): implement error handling.
155 } // namespace content