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 "base/single_thread_task_runner.h" | |
10 #include "base/thread_task_runner_handle.h" | |
11 #include "base/time/time.h" | |
9 #include "content/renderer/media/audio_device_factory.h" | 12 #include "content/renderer/media/audio_device_factory.h" |
10 #include "content/renderer/render_frame_impl.h" | 13 #include "content/renderer/render_frame_impl.h" |
11 #include "media/audio/audio_output_device.h" | 14 #include "media/audio/audio_output_device.h" |
15 #include "media/audio/null_audio_sink.h" | |
16 #include "media/base/audio_timestamp_helper.h" | |
DaleCurtis
2015/06/22 22:49:42
Remove?
qinmin
2015/06/23 00:01:54
Done.
| |
12 #include "media/base/media_switches.h" | 17 #include "media/base/media_switches.h" |
13 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 18 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
14 #include "third_party/WebKit/public/web/WebView.h" | 19 #include "third_party/WebKit/public/web/WebView.h" |
15 | 20 |
16 using blink::WebAudioDevice; | 21 using blink::WebAudioDevice; |
17 using blink::WebLocalFrame; | 22 using blink::WebLocalFrame; |
18 using blink::WebVector; | 23 using blink::WebVector; |
19 using blink::WebView; | 24 using blink::WebView; |
20 | 25 |
21 namespace content { | 26 namespace content { |
22 | 27 |
28 static const int kSilenceInSecondsToEnterIdleMode = 30.0; | |
DaleCurtis
2015/06/22 22:49:42
Remove .0
qinmin
2015/06/23 00:01:54
Done.
| |
29 | |
23 RendererWebAudioDeviceImpl::RendererWebAudioDeviceImpl( | 30 RendererWebAudioDeviceImpl::RendererWebAudioDeviceImpl( |
24 const media::AudioParameters& params, | 31 const media::AudioParameters& params, |
25 WebAudioDevice::RenderCallback* callback, | 32 WebAudioDevice::RenderCallback* callback, |
26 int session_id) | 33 int session_id) |
27 : params_(params), | 34 : params_(params), |
28 client_callback_(callback), | 35 client_callback_(callback), |
29 session_id_(session_id) { | 36 session_id_(session_id), |
37 task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
38 null_audio_sink_(new media::NullAudioSink(task_runner_)), | |
39 is_using_null_audio_sink_(false), | |
40 weak_factory_(this) { | |
DaleCurtis
2015/06/22 22:49:42
Remove.
qinmin
2015/06/23 00:01:54
Done.
| |
30 DCHECK(client_callback_); | 41 DCHECK(client_callback_); |
42 null_audio_sink_->Initialize(params_, this); | |
43 weak_this_ = weak_factory_.GetWeakPtr(); | |
DaleCurtis
2015/06/22 22:49:42
Remove.
qinmin
2015/06/23 00:01:54
Done.
| |
31 } | 44 } |
32 | 45 |
33 RendererWebAudioDeviceImpl::~RendererWebAudioDeviceImpl() { | 46 RendererWebAudioDeviceImpl::~RendererWebAudioDeviceImpl() { |
34 DCHECK(!output_device_.get()); | 47 DCHECK(!output_device_); |
35 } | 48 } |
36 | 49 |
37 void RendererWebAudioDeviceImpl::start() { | 50 void RendererWebAudioDeviceImpl::start() { |
38 DCHECK(thread_checker_.CalledOnValidThread()); | 51 DCHECK(thread_checker_.CalledOnValidThread()); |
39 | 52 |
40 if (output_device_.get()) | 53 if (output_device_) |
41 return; // Already started. | 54 return; // Already started. |
42 | 55 |
43 // Assumption: This method is being invoked within a V8 call stack. CHECKs | 56 // Assumption: This method is being invoked within a V8 call stack. CHECKs |
44 // will fail in the call to frameForCurrentContext() otherwise. | 57 // will fail in the call to frameForCurrentContext() otherwise. |
45 // | 58 // |
46 // Therefore, we can perform look-ups to determine which RenderView is | 59 // Therefore, we can perform look-ups to determine which RenderView is |
47 // starting the audio device. The reason for all this is because the creator | 60 // starting the audio device. The reason for all this is because the creator |
48 // of the WebAudio objects might not be the actual source of the audio (e.g., | 61 // of the WebAudio objects might not be the actual source of the audio (e.g., |
49 // an extension creates a object that is passed and used within a page). | 62 // an extension creates a object that is passed and used within a page). |
50 WebLocalFrame* const web_frame = WebLocalFrame::frameForCurrentContext(); | 63 WebLocalFrame* const web_frame = WebLocalFrame::frameForCurrentContext(); |
51 RenderFrame* const render_frame = | 64 RenderFrame* const render_frame = |
52 web_frame ? RenderFrame::FromWebFrame(web_frame) : NULL; | 65 web_frame ? RenderFrame::FromWebFrame(web_frame) : NULL; |
53 output_device_ = AudioDeviceFactory::NewOutputDevice( | 66 output_device_ = AudioDeviceFactory::NewOutputDevice( |
54 render_frame ? render_frame->GetRoutingID(): MSG_ROUTING_NONE); | 67 render_frame ? render_frame->GetRoutingID(): MSG_ROUTING_NONE); |
55 output_device_->InitializeWithSessionId(params_, this, session_id_); | 68 output_device_->InitializeWithSessionId(params_, this, session_id_); |
56 output_device_->Start(); | 69 output_device_->Start(); |
57 // Note: Default behavior is to auto-play on start. | 70 // Note: Default behavior is to auto-play on start. |
DaleCurtis
2015/06/22 22:49:42
Technically you should call NullAudioSink::Start()
| |
58 } | 71 } |
59 | 72 |
60 void RendererWebAudioDeviceImpl::stop() { | 73 void RendererWebAudioDeviceImpl::stop() { |
61 DCHECK(thread_checker_.CalledOnValidThread()); | 74 DCHECK(thread_checker_.CalledOnValidThread()); |
62 | 75 |
63 if (output_device_.get()) { | 76 if (output_device_) { |
64 output_device_->Stop(); | 77 output_device_->Stop(); |
65 output_device_ = NULL; | 78 output_device_ = NULL; |
66 } | 79 } |
80 is_using_null_audio_sink_ = false; | |
81 null_audio_sink_->Stop(); | |
82 first_buffer_after_silence_.reset(); | |
67 } | 83 } |
68 | 84 |
69 double RendererWebAudioDeviceImpl::sampleRate() { | 85 double RendererWebAudioDeviceImpl::sampleRate() { |
70 return params_.sample_rate(); | 86 return params_.sample_rate(); |
71 } | 87 } |
72 | 88 |
73 int RendererWebAudioDeviceImpl::Render(media::AudioBus* dest, | 89 int RendererWebAudioDeviceImpl::Render(media::AudioBus* dest, |
74 int audio_delay_milliseconds) { | 90 int audio_delay_milliseconds) { |
75 if (client_callback_) { | 91 if (client_callback_) { |
92 #if defined(OS_ANDROID) | |
93 if (first_buffer_after_silence_) { | |
94 DCHECK(!is_using_null_audio_sink_); | |
95 first_buffer_after_silence_->CopyTo(dest); | |
96 first_buffer_after_silence_.reset(); | |
97 return dest->frames(); | |
98 } | |
99 #endif | |
76 // Wrap the output pointers using WebVector. | 100 // Wrap the output pointers using WebVector. |
77 WebVector<float*> web_audio_dest_data( | 101 WebVector<float*> web_audio_dest_data( |
78 static_cast<size_t>(dest->channels())); | 102 static_cast<size_t>(dest->channels())); |
79 for (int i = 0; i < dest->channels(); ++i) | 103 for (int i = 0; i < dest->channels(); ++i) |
80 web_audio_dest_data[i] = dest->channel(i); | 104 web_audio_dest_data[i] = dest->channel(i); |
81 | 105 |
82 // TODO(xians): Remove the following |web_audio_source_data| after | 106 // TODO(xians): Remove the following |web_audio_source_data| after |
83 // changing the blink interface. | 107 // changing the blink interface. |
84 WebVector<float*> web_audio_source_data(static_cast<size_t>(0)); | 108 WebVector<float*> web_audio_source_data(static_cast<size_t>(0)); |
85 client_callback_->render(web_audio_source_data, | 109 client_callback_->render(web_audio_source_data, |
86 web_audio_dest_data, | 110 web_audio_dest_data, |
87 dest->frames()); | 111 dest->frames()); |
88 } | 112 } |
89 | 113 |
114 #if defined(OS_ANDROID) | |
115 const bool is_zero = dest->AreFramesZero(); | |
116 if (!is_zero) { | |
117 first_silence_time_ = base::TimeTicks(); | |
118 if (is_using_null_audio_sink_) { | |
119 // This is called on the main render thread when audio is detected. | |
120 output_device_->Play(); | |
121 is_using_null_audio_sink_ = false; | |
122 first_buffer_after_silence_ = media::AudioBus::Create(params_); | |
DaleCurtis
2015/06/22 22:49:42
If we ever lower the silence threshold it might be
qinmin
2015/06/23 00:01:54
Done.
| |
123 dest->CopyTo(first_buffer_after_silence_.get()); | |
124 task_runner_->PostTask( | |
125 FROM_HERE, | |
126 base::Bind(&media::NullAudioSink::Stop, null_audio_sink_)); | |
127 } | |
128 } else if (!is_using_null_audio_sink_) { | |
129 // Called on the audio device thread. | |
130 const base::TimeTicks now = base::TimeTicks::Now(); | |
131 if (first_silence_time_.is_null()) | |
132 first_silence_time_ = now; | |
133 if ((now - first_silence_time_).InSecondsF() | |
DaleCurtis
2015/06/22 22:49:42
Instead of doing this, compare against base::TimeD
qinmin
2015/06/23 00:01:54
Done.
| |
134 > kSilenceInSecondsToEnterIdleMode) { | |
135 output_device_->Pause(); | |
136 is_using_null_audio_sink_ = true; | |
137 // If Stop() is called right after the task is posted, | |
DaleCurtis
2015/06/22 22:49:42
This isn't true, only the Stop() above is safe, yo
qinmin
2015/06/23 00:01:54
That's why I kept the weak_ptr_ here. If StartNull
| |
138 // StartNullAudioSink() should do nothing. | |
139 double delay = | |
DaleCurtis
2015/06/22 22:49:42
Just use params_.GetBufferDuration()
qinmin
2015/06/23 00:01:54
Thanks, didn't notice that method.
| |
140 static_cast<double>(dest->frames() * | |
141 base::Time::kMicrosecondsPerSecond) / params_.sample_rate(); | |
142 task_runner_->PostDelayedTask( | |
143 FROM_HERE, | |
144 base::Bind(&RendererWebAudioDeviceImpl::StartNullAudioSink, | |
DaleCurtis
2015/06/22 22:49:42
Bind directly to Play() here.
qinmin
2015/06/23 00:01:54
Done.
| |
145 weak_this_), | |
146 base::TimeDelta::FromMicroseconds(delay)); | |
147 } | |
148 } | |
149 #endif | |
90 return dest->frames(); | 150 return dest->frames(); |
91 } | 151 } |
92 | 152 |
93 void RendererWebAudioDeviceImpl::OnRenderError() { | 153 void RendererWebAudioDeviceImpl::OnRenderError() { |
94 // TODO(crogers): implement error handling. | 154 // TODO(crogers): implement error handling. |
95 } | 155 } |
96 | 156 |
157 void RendererWebAudioDeviceImpl::StartNullAudioSink() { | |
DaleCurtis
2015/06/22 22:49:42
Delete?
qinmin
2015/06/23 00:01:54
Done.
| |
158 if (is_using_null_audio_sink_) | |
159 null_audio_sink_->Play(); | |
160 } | |
161 | |
97 } // namespace content | 162 } // namespace content |
OLD | NEW |