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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
17 #include "content/renderer/media/audio_device_factory.h" | 17 #include "content/renderer/media/audio_device_factory.h" |
18 #include "content/renderer/render_frame_impl.h" | 18 #include "content/renderer/render_frame_impl.h" |
19 #include "media/audio/audio_output_device.h" | |
20 #include "media/audio/null_audio_sink.h" | 19 #include "media/audio/null_audio_sink.h" |
21 #include "media/base/media_switches.h" | 20 #include "media/base/media_switches.h" |
22 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 21 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
23 #include "third_party/WebKit/public/web/WebView.h" | 22 #include "third_party/WebKit/public/web/WebView.h" |
24 | 23 |
25 using blink::WebAudioDevice; | 24 using blink::WebAudioDevice; |
26 using blink::WebLocalFrame; | 25 using blink::WebLocalFrame; |
27 using blink::WebVector; | 26 using blink::WebVector; |
28 using blink::WebView; | 27 using blink::WebView; |
29 | 28 |
(...skipping 16 matching lines...) Expand all Loading... |
46 is_using_null_audio_sink_(false), | 45 is_using_null_audio_sink_(false), |
47 first_buffer_after_silence_(media::AudioBus::Create(params_)), | 46 first_buffer_after_silence_(media::AudioBus::Create(params_)), |
48 is_first_buffer_after_silence_(false), | 47 is_first_buffer_after_silence_(false), |
49 security_origin_(security_origin) { | 48 security_origin_(security_origin) { |
50 DCHECK(client_callback_); | 49 DCHECK(client_callback_); |
51 null_audio_sink_->Initialize(params_, this); | 50 null_audio_sink_->Initialize(params_, this); |
52 null_audio_sink_->Start(); | 51 null_audio_sink_->Start(); |
53 } | 52 } |
54 | 53 |
55 RendererWebAudioDeviceImpl::~RendererWebAudioDeviceImpl() { | 54 RendererWebAudioDeviceImpl::~RendererWebAudioDeviceImpl() { |
56 DCHECK(!output_device_); | 55 DCHECK(!sink_); |
57 } | 56 } |
58 | 57 |
59 void RendererWebAudioDeviceImpl::start() { | 58 void RendererWebAudioDeviceImpl::start() { |
60 DCHECK(thread_checker_.CalledOnValidThread()); | 59 DCHECK(thread_checker_.CalledOnValidThread()); |
61 | 60 |
62 if (output_device_) | 61 if (sink_) |
63 return; // Already started. | 62 return; // Already started. |
64 | 63 |
65 // Assumption: This method is being invoked within a V8 call stack. CHECKs | 64 // Assumption: This method is being invoked within a V8 call stack. CHECKs |
66 // will fail in the call to frameForCurrentContext() otherwise. | 65 // will fail in the call to frameForCurrentContext() otherwise. |
67 // | 66 // |
68 // Therefore, we can perform look-ups to determine which RenderView is | 67 // Therefore, we can perform look-ups to determine which RenderView is |
69 // starting the audio device. The reason for all this is because the creator | 68 // starting the audio device. The reason for all this is because the creator |
70 // of the WebAudio objects might not be the actual source of the audio (e.g., | 69 // of the WebAudio objects might not be the actual source of the audio (e.g., |
71 // an extension creates a object that is passed and used within a page). | 70 // an extension creates a object that is passed and used within a page). |
72 WebLocalFrame* const web_frame = WebLocalFrame::frameForCurrentContext(); | 71 WebLocalFrame* const web_frame = WebLocalFrame::frameForCurrentContext(); |
73 RenderFrame* const render_frame = | 72 RenderFrame* const render_frame = |
74 web_frame ? RenderFrame::FromWebFrame(web_frame) : NULL; | 73 web_frame ? RenderFrame::FromWebFrame(web_frame) : NULL; |
75 output_device_ = AudioDeviceFactory::NewOutputDevice( | 74 sink_ = AudioDeviceFactory::NewAudioRendererSink( |
| 75 AudioDeviceFactory::kSourceWebAudio, |
76 render_frame ? render_frame->GetRoutingID() : MSG_ROUTING_NONE, | 76 render_frame ? render_frame->GetRoutingID() : MSG_ROUTING_NONE, |
77 session_id_, std::string(), security_origin_); | 77 session_id_, std::string(), security_origin_); |
78 output_device_->Initialize(params_, this); | 78 sink_->Initialize(params_, this); |
79 output_device_->Start(); | 79 sink_->Start(); |
| 80 sink_->Play(); |
80 start_null_audio_sink_callback_.Reset( | 81 start_null_audio_sink_callback_.Reset( |
81 base::Bind(&media::NullAudioSink::Play, null_audio_sink_)); | 82 base::Bind(&media::NullAudioSink::Play, null_audio_sink_)); |
82 // Note: Default behavior is to auto-play on start. | 83 // Note: Default behavior is to auto-play on start. |
83 } | 84 } |
84 | 85 |
85 void RendererWebAudioDeviceImpl::stop() { | 86 void RendererWebAudioDeviceImpl::stop() { |
86 DCHECK(thread_checker_.CalledOnValidThread()); | 87 DCHECK(thread_checker_.CalledOnValidThread()); |
87 | 88 |
88 if (output_device_) { | 89 if (sink_) { |
89 output_device_->Stop(); | 90 sink_->Stop(); |
90 output_device_ = NULL; | 91 sink_ = NULL; |
91 } | 92 } |
92 null_audio_sink_->Stop(); | 93 null_audio_sink_->Stop(); |
93 is_using_null_audio_sink_ = false; | 94 is_using_null_audio_sink_ = false; |
94 is_first_buffer_after_silence_ = false; | 95 is_first_buffer_after_silence_ = false; |
95 start_null_audio_sink_callback_.Cancel(); | 96 start_null_audio_sink_callback_.Cancel(); |
96 } | 97 } |
97 | 98 |
98 double RendererWebAudioDeviceImpl::sampleRate() { | 99 double RendererWebAudioDeviceImpl::sampleRate() { |
99 return params_.sample_rate(); | 100 return params_.sample_rate(); |
100 } | 101 } |
(...skipping 28 matching lines...) Expand all Loading... |
129 first_silence_time_ = base::TimeTicks(); | 130 first_silence_time_ = base::TimeTicks(); |
130 if (is_using_null_audio_sink_) { | 131 if (is_using_null_audio_sink_) { |
131 // This is called on the main render thread when audio is detected. | 132 // This is called on the main render thread when audio is detected. |
132 DCHECK(thread_checker_.CalledOnValidThread()); | 133 DCHECK(thread_checker_.CalledOnValidThread()); |
133 is_using_null_audio_sink_ = false; | 134 is_using_null_audio_sink_ = false; |
134 is_first_buffer_after_silence_ = true; | 135 is_first_buffer_after_silence_ = true; |
135 dest->CopyTo(first_buffer_after_silence_.get()); | 136 dest->CopyTo(first_buffer_after_silence_.get()); |
136 task_runner_->PostTask( | 137 task_runner_->PostTask( |
137 FROM_HERE, | 138 FROM_HERE, |
138 base::Bind(&media::NullAudioSink::Pause, null_audio_sink_)); | 139 base::Bind(&media::NullAudioSink::Pause, null_audio_sink_)); |
139 // Calling output_device_->Play() may trigger reentrancy into this | 140 // Calling sink_->Play() may trigger reentrancy into this |
140 // function, so this should be called at the end. | 141 // function, so this should be called at the end. |
141 output_device_->Play(); | 142 sink_->Play(); |
142 return dest->frames(); | 143 return dest->frames(); |
143 } | 144 } |
144 } else if (!is_using_null_audio_sink_) { | 145 } else if (!is_using_null_audio_sink_) { |
145 // Called on the audio device thread. | 146 // Called on the audio device thread. |
146 const base::TimeTicks now = base::TimeTicks::Now(); | 147 const base::TimeTicks now = base::TimeTicks::Now(); |
147 if (first_silence_time_.is_null()) | 148 if (first_silence_time_.is_null()) |
148 first_silence_time_ = now; | 149 first_silence_time_ = now; |
149 if (now - first_silence_time_ | 150 if (now - first_silence_time_ |
150 > base::TimeDelta::FromSeconds(kSilenceInSecondsToEnterIdleMode)) { | 151 > base::TimeDelta::FromSeconds(kSilenceInSecondsToEnterIdleMode)) { |
151 output_device_->Pause(); | 152 sink_->Pause(); |
152 is_using_null_audio_sink_ = true; | 153 is_using_null_audio_sink_ = true; |
153 // If Stop() is called right after the task is posted, need to cancel | 154 // If Stop() is called right after the task is posted, need to cancel |
154 // this task. | 155 // this task. |
155 task_runner_->PostDelayedTask( | 156 task_runner_->PostDelayedTask( |
156 FROM_HERE, | 157 FROM_HERE, |
157 start_null_audio_sink_callback_.callback(), | 158 start_null_audio_sink_callback_.callback(), |
158 params_.GetBufferDuration()); | 159 params_.GetBufferDuration()); |
159 } | 160 } |
160 } | 161 } |
161 #endif | 162 #endif |
162 return dest->frames(); | 163 return dest->frames(); |
163 } | 164 } |
164 | 165 |
165 void RendererWebAudioDeviceImpl::OnRenderError() { | 166 void RendererWebAudioDeviceImpl::OnRenderError() { |
166 // TODO(crogers): implement error handling. | 167 // TODO(crogers): implement error handling. |
167 } | 168 } |
168 | 169 |
169 } // namespace content | 170 } // namespace content |
OLD | NEW |