OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/audio_device.h" | 5 #include "content/renderer/media/audio_device.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "content/common/child_process.h" | 11 #include "content/common/child_process.h" |
12 #include "content/common/media/audio_messages.h" | 12 #include "content/common/media/audio_messages.h" |
13 #include "content/common/view_messages.h" | 13 #include "content/common/view_messages.h" |
14 #include "content/renderer/render_thread_impl.h" | 14 #include "content/renderer/render_thread_impl.h" |
15 #include "media/audio/audio_output_controller.h" | |
15 #include "media/audio/audio_util.h" | 16 #include "media/audio/audio_util.h" |
16 | 17 |
18 AudioDevice::AudioDevice() | |
19 : buffer_size_(0), | |
20 channels_(0), | |
21 bits_per_sample_(16), | |
22 sample_rate_(0), | |
23 latency_format_(AudioParameters::AUDIO_PCM_LOW_LATENCY), | |
24 callback_(0), | |
25 audio_delay_milliseconds_(0), | |
26 volume_(1.0), | |
27 stream_id_(0), | |
28 play_on_start_(true), | |
29 is_started_(false), | |
30 shared_memory_size_(0) { | |
31 filter_ = RenderThreadImpl::current()->audio_message_filter(); | |
32 } | |
33 | |
17 AudioDevice::AudioDevice(size_t buffer_size, | 34 AudioDevice::AudioDevice(size_t buffer_size, |
18 int channels, | 35 int channels, |
19 double sample_rate, | 36 double sample_rate, |
20 RenderCallback* callback) | 37 RenderCallback* callback) |
21 : buffer_size_(buffer_size), | 38 : buffer_size_(buffer_size), |
22 channels_(channels), | 39 channels_(channels), |
23 bits_per_sample_(16), | 40 bits_per_sample_(16), |
24 sample_rate_(sample_rate), | 41 sample_rate_(sample_rate), |
25 callback_(callback), | 42 callback_(callback), |
26 audio_delay_milliseconds_(0), | 43 audio_delay_milliseconds_(0), |
27 volume_(1.0), | 44 volume_(1.0), |
28 stream_id_(0) { | 45 stream_id_(0), |
46 play_on_start_(true), | |
47 is_started_(false), | |
48 shared_memory_size_(0) { | |
29 filter_ = RenderThreadImpl::current()->audio_message_filter(); | 49 filter_ = RenderThreadImpl::current()->audio_message_filter(); |
50 Initialize(buffer_size, | |
henrika (OOO until Aug 14)
2011/11/16 20:48:03
You assign values to members twice here. Both in t
Chris Rogers
2011/11/17 02:37:30
Done.
| |
51 channels, | |
52 sample_rate, | |
53 AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
54 callback); | |
55 } | |
56 | |
57 void AudioDevice::Initialize(size_t buffer_size, | |
58 int channels, | |
59 double sample_rate, | |
60 AudioParameters::Format latency_format, | |
61 RenderCallback* callback) { | |
62 CHECK_EQ(0, stream_id_) << | |
63 "AudioDevice::Initialize() must be called before Start()"; | |
64 | |
65 buffer_size_ = buffer_size; | |
66 channels_ = channels; | |
67 sample_rate_ = sample_rate; | |
68 latency_format_ = latency_format; | |
69 callback_ = callback; | |
70 | |
71 // Cleanup from any previous initialization. | |
72 for (size_t i = 0; i < audio_data_.size(); ++i) | |
73 delete [] audio_data_[i]; | |
74 | |
30 audio_data_.reserve(channels); | 75 audio_data_.reserve(channels); |
31 for (int i = 0; i < channels; ++i) { | 76 for (int i = 0; i < channels; ++i) { |
32 float* channel_data = new float[buffer_size]; | 77 float* channel_data = new float[buffer_size]; |
33 audio_data_.push_back(channel_data); | 78 audio_data_.push_back(channel_data); |
34 } | 79 } |
35 } | 80 } |
36 | 81 |
82 bool AudioDevice::IsInitialized() { | |
83 return audio_data_.size() > 0; | |
henrika (OOO until Aug 14)
2011/11/16 20:48:03
I would add an explicit state instead. These types
Chris Rogers
2011/11/17 02:37:30
Done.
| |
84 } | |
85 | |
37 AudioDevice::~AudioDevice() { | 86 AudioDevice::~AudioDevice() { |
38 // The current design requires that the user calls Stop() before deleting | 87 // The current design requires that the user calls Stop() before deleting |
39 // this class. | 88 // this class. |
40 CHECK_EQ(0, stream_id_); | 89 CHECK_EQ(0, stream_id_); |
41 for (int i = 0; i < channels_; ++i) | 90 for (int i = 0; i < channels_; ++i) |
42 delete [] audio_data_[i]; | 91 delete [] audio_data_[i]; |
43 } | 92 } |
44 | 93 |
45 void AudioDevice::Start() { | 94 void AudioDevice::Start() { |
46 AudioParameters params; | 95 AudioParameters params; |
47 params.format = AudioParameters::AUDIO_PCM_LOW_LATENCY; | 96 params.format = latency_format_; |
48 params.channels = channels_; | 97 params.channels = channels_; |
49 params.sample_rate = static_cast<int>(sample_rate_); | 98 params.sample_rate = static_cast<int>(sample_rate_); |
50 params.bits_per_sample = bits_per_sample_; | 99 params.bits_per_sample = bits_per_sample_; |
51 params.samples_per_packet = buffer_size_; | 100 params.samples_per_packet = buffer_size_; |
52 | 101 |
53 ChildProcess::current()->io_message_loop()->PostTask( | 102 ChildProcess::current()->io_message_loop()->PostTask( |
54 FROM_HERE, | 103 FROM_HERE, |
55 base::Bind(&AudioDevice::InitializeOnIOThread, this, params)); | 104 base::Bind(&AudioDevice::InitializeOnIOThread, this, params)); |
56 } | 105 } |
57 | 106 |
58 bool AudioDevice::Stop() { | 107 bool AudioDevice::Stop() { |
108 if (MessageLoop::current() == | |
henrika (OOO until Aug 14)
2011/11/16 20:48:03
Why is this added for Stop() only. Comments still
Chris Rogers
2011/11/17 02:37:30
FIXED
I've removed this added code in the latest
| |
109 ChildProcess::current()->io_message_loop()) { | |
110 // If we're already on the IO thread then simply shutdown right here. | |
111 ShutDownOnIOThread(NULL); | |
112 ShutDownAudioThread(); | |
113 return true; | |
114 } | |
115 | |
59 // Max waiting time for Stop() to complete. If this time limit is passed, | 116 // Max waiting time for Stop() to complete. If this time limit is passed, |
60 // we will stop waiting and return false. It ensures that Stop() can't block | 117 // we will stop waiting and return false. It ensures that Stop() can't block |
61 // the calling thread forever. | 118 // the calling thread forever. |
62 const base::TimeDelta kMaxTimeOut = base::TimeDelta::FromMilliseconds(1000); | 119 const base::TimeDelta kMaxTimeOut = base::TimeDelta::FromMilliseconds(1000); |
63 | 120 |
64 base::WaitableEvent completion(false, false); | 121 base::WaitableEvent completion(false, false); |
65 | 122 |
66 ChildProcess::current()->io_message_loop()->PostTask( | 123 ChildProcess::current()->io_message_loop()->PostTask( |
67 FROM_HERE, | 124 FROM_HERE, |
68 base::Bind(&AudioDevice::ShutDownOnIOThread, this, &completion)); | 125 base::Bind(&AudioDevice::ShutDownOnIOThread, this, &completion)); |
69 | 126 |
70 // We wait here for the IO task to be completed to remove race conflicts | 127 // We wait here for the IO task to be completed to remove race conflicts |
71 // with OnLowLatencyCreated() and to ensure that Stop() acts as a synchronous | 128 // with OnLowLatencyCreated() and to ensure that Stop() acts as a synchronous |
72 // function call. | 129 // function call. |
73 if (completion.TimedWait(kMaxTimeOut)) { | 130 if (completion.TimedWait(kMaxTimeOut)) { |
74 if (audio_thread_.get()) { | 131 ShutDownAudioThread(); |
75 socket_->Close(); | |
76 audio_thread_->Join(); | |
77 audio_thread_.reset(NULL); | |
78 } | |
79 } else { | 132 } else { |
80 LOG(ERROR) << "Failed to shut down audio output on IO thread"; | 133 LOG(ERROR) << "Failed to shut down audio output on IO thread"; |
81 return false; | 134 return false; |
82 } | 135 } |
83 | 136 |
84 return true; | 137 return true; |
85 } | 138 } |
86 | 139 |
140 void AudioDevice::Play() { | |
141 ChildProcess::current()->io_message_loop()->PostTask( | |
142 FROM_HERE, | |
143 base::Bind(&AudioDevice::PlayOnIOThread, this)); | |
144 } | |
145 | |
146 void AudioDevice::Pause(bool flush) { | |
147 ChildProcess::current()->io_message_loop()->PostTask( | |
148 FROM_HERE, | |
149 base::Bind(&AudioDevice::PauseOnIOThread, this, flush)); | |
150 } | |
151 | |
87 bool AudioDevice::SetVolume(double volume) { | 152 bool AudioDevice::SetVolume(double volume) { |
88 if (volume < 0 || volume > 1.0) | 153 if (volume < 0 || volume > 1.0) |
89 return false; | 154 return false; |
90 | 155 |
91 ChildProcess::current()->io_message_loop()->PostTask( | 156 ChildProcess::current()->io_message_loop()->PostTask( |
92 FROM_HERE, | 157 FROM_HERE, |
93 base::Bind(&AudioDevice::SetVolumeOnIOThread, this, volume)); | 158 base::Bind(&AudioDevice::SetVolumeOnIOThread, this, volume)); |
94 | 159 |
95 volume_ = volume; | 160 volume_ = volume; |
96 | 161 |
97 return true; | 162 return true; |
98 } | 163 } |
99 | 164 |
100 void AudioDevice::GetVolume(double* volume) { | 165 void AudioDevice::GetVolume(double* volume) { |
101 // Return a locally cached version of the current scaling factor. | 166 // Return a locally cached version of the current scaling factor. |
102 *volume = volume_; | 167 *volume = volume_; |
103 } | 168 } |
104 | 169 |
105 void AudioDevice::InitializeOnIOThread(const AudioParameters& params) { | 170 void AudioDevice::InitializeOnIOThread(const AudioParameters& params) { |
106 // Make sure we don't call Start() more than once. | 171 // Make sure we don't create the stream more than once. |
107 DCHECK_EQ(0, stream_id_); | 172 DCHECK_EQ(0, stream_id_); |
108 if (stream_id_) | 173 if (stream_id_) |
109 return; | 174 return; |
110 | 175 |
111 stream_id_ = filter_->AddDelegate(this); | 176 stream_id_ = filter_->AddDelegate(this); |
112 Send(new AudioHostMsg_CreateStream(stream_id_, params, true)); | 177 Send(new AudioHostMsg_CreateStream(stream_id_, params, true)); |
113 } | 178 } |
114 | 179 |
115 void AudioDevice::StartOnIOThread() { | 180 void AudioDevice::PlayOnIOThread() { |
116 if (stream_id_) | 181 if (stream_id_ && is_started_) |
117 Send(new AudioHostMsg_PlayStream(stream_id_)); | 182 Send(new AudioHostMsg_PlayStream(stream_id_)); |
183 else | |
184 play_on_start_ = true; | |
185 } | |
186 | |
187 void AudioDevice::PauseOnIOThread(bool flush) { | |
188 if (stream_id_ && is_started_) { | |
189 Send(new AudioHostMsg_PauseStream(stream_id_)); | |
190 if (flush) | |
191 Send(new AudioHostMsg_FlushStream(stream_id_)); | |
192 } else { | |
193 // Note that |flush| isn't relevant here since this is the case where | |
194 // the stream is first starting. | |
195 play_on_start_ = false; | |
196 } | |
118 } | 197 } |
119 | 198 |
120 void AudioDevice::ShutDownOnIOThread(base::WaitableEvent* completion) { | 199 void AudioDevice::ShutDownOnIOThread(base::WaitableEvent* completion) { |
200 is_started_ = false; | |
201 | |
121 // Make sure we don't call shutdown more than once. | 202 // Make sure we don't call shutdown more than once. |
122 if (!stream_id_) { | 203 if (!stream_id_) { |
123 completion->Signal(); | 204 if (completion) |
205 completion->Signal(); | |
124 return; | 206 return; |
125 } | 207 } |
126 | 208 |
127 filter_->RemoveDelegate(stream_id_); | 209 filter_->RemoveDelegate(stream_id_); |
128 Send(new AudioHostMsg_CloseStream(stream_id_)); | 210 Send(new AudioHostMsg_CloseStream(stream_id_)); |
129 stream_id_ = 0; | 211 stream_id_ = 0; |
130 | 212 |
131 completion->Signal(); | 213 if (completion) |
214 completion->Signal(); | |
132 } | 215 } |
133 | 216 |
134 void AudioDevice::SetVolumeOnIOThread(double volume) { | 217 void AudioDevice::SetVolumeOnIOThread(double volume) { |
135 if (stream_id_) | 218 if (stream_id_) |
136 Send(new AudioHostMsg_SetVolume(stream_id_, volume)); | 219 Send(new AudioHostMsg_SetVolume(stream_id_, volume)); |
137 } | 220 } |
138 | 221 |
139 void AudioDevice::OnRequestPacket(AudioBuffersState buffers_state) { | 222 void AudioDevice::OnRequestPacket(AudioBuffersState buffers_state) { |
140 // This method does not apply to the low-latency system. | 223 // This method does not apply to the low-latency system. |
141 NOTIMPLEMENTED(); | |
142 } | 224 } |
143 | 225 |
144 void AudioDevice::OnStateChanged(AudioStreamState state) { | 226 void AudioDevice::OnStateChanged(AudioStreamState state) { |
145 if (state == kAudioStreamError) { | 227 if (state == kAudioStreamError) { |
146 DLOG(WARNING) << "AudioDevice::OnStateChanged(kError)"; | 228 DLOG(WARNING) << "AudioDevice::OnStateChanged(kError)"; |
147 } | 229 } |
148 NOTIMPLEMENTED(); | |
149 } | 230 } |
150 | 231 |
151 void AudioDevice::OnCreated( | 232 void AudioDevice::OnCreated( |
152 base::SharedMemoryHandle handle, uint32 length) { | 233 base::SharedMemoryHandle handle, uint32 length) { |
153 // Not needed in this simple implementation. | 234 // Not needed in this simple implementation. |
154 NOTIMPLEMENTED(); | |
155 } | 235 } |
156 | 236 |
157 void AudioDevice::OnLowLatencyCreated( | 237 void AudioDevice::OnLowLatencyCreated( |
158 base::SharedMemoryHandle handle, | 238 base::SharedMemoryHandle handle, |
159 base::SyncSocket::Handle socket_handle, | 239 base::SyncSocket::Handle socket_handle, |
160 uint32 length) { | 240 uint32 length) { |
161 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); | 241 DCHECK(MessageLoop::current() == ChildProcess::current()->io_message_loop()); |
162 #if defined(OS_WIN) | 242 #if defined(OS_WIN) |
163 DCHECK(handle); | 243 DCHECK(handle); |
164 DCHECK(socket_handle); | 244 DCHECK(socket_handle); |
165 #else | 245 #else |
166 DCHECK_GE(handle.fd, 0); | 246 DCHECK_GE(handle.fd, 0); |
167 DCHECK_GE(socket_handle, 0); | 247 DCHECK_GE(socket_handle, 0); |
168 #endif | 248 #endif |
169 DCHECK(length); | 249 DCHECK(length); |
170 | 250 |
171 // Takes care of the case when Stop() is called before OnLowLatencyCreated(). | 251 // Takes care of the case when Stop() is called before OnLowLatencyCreated(). |
172 if (!stream_id_) { | 252 if (!stream_id_) { |
173 base::SharedMemory::CloseHandle(handle); | 253 base::SharedMemory::CloseHandle(handle); |
174 // Close the socket handler. | 254 // Close the socket handler. |
175 base::SyncSocket socket(socket_handle); | 255 base::SyncSocket socket(socket_handle); |
176 return; | 256 return; |
177 } | 257 } |
178 | 258 |
179 shared_memory_.reset(new base::SharedMemory(handle, false)); | 259 shared_memory_.reset(new base::SharedMemory(handle, false)); |
180 shared_memory_->Map(length); | 260 shared_memory_->Map(length); |
261 shared_memory_size_ = length; | |
181 | 262 |
182 DCHECK_GE(length, buffer_size_ * sizeof(int16) * channels_); | 263 DCHECK_GE(length, buffer_size_ * sizeof(int16) * channels_); |
183 | 264 |
184 socket_.reset(new base::SyncSocket(socket_handle)); | 265 { |
185 // Allow the client to pre-populate the buffer. | 266 // Synchronize with ShutDownAudioThread(). |
186 FireRenderCallback(); | 267 base::AutoLock auto_lock(lock_); |
187 | 268 |
188 audio_thread_.reset( | 269 socket_.reset(new base::SyncSocket(socket_handle)); |
189 new base::DelegateSimpleThread(this, "renderer_audio_thread")); | 270 // Allow the client to pre-populate the buffer. |
190 audio_thread_->Start(); | 271 FireRenderCallback(); |
191 | 272 |
192 MessageLoop::current()->PostTask( | 273 DCHECK(!audio_thread_.get()); |
193 FROM_HERE, | 274 audio_thread_.reset( |
194 base::Bind(&AudioDevice::StartOnIOThread, this)); | 275 new base::DelegateSimpleThread(this, "renderer_audio_thread")); |
276 audio_thread_->Start(); | |
277 } | |
278 | |
279 // We handle the case where Play() and/or Pause() may have been called | |
280 // multiple times before OnLowLatencyCreated() gets called. | |
281 is_started_ = true; | |
282 if (play_on_start_) { | |
283 MessageLoop::current()->PostTask( | |
284 FROM_HERE, | |
285 base::Bind(&AudioDevice::PlayOnIOThread, this)); | |
286 } | |
195 } | 287 } |
196 | 288 |
197 void AudioDevice::OnVolume(double volume) { | 289 void AudioDevice::OnVolume(double volume) { |
198 NOTIMPLEMENTED(); | 290 NOTIMPLEMENTED(); |
199 } | 291 } |
200 | 292 |
201 void AudioDevice::Send(IPC::Message* message) { | 293 void AudioDevice::Send(IPC::Message* message) { |
202 filter_->Send(message); | 294 filter_->Send(message); |
203 } | 295 } |
204 | 296 |
205 // Our audio thread runs here. | 297 // Our audio thread runs here. |
206 void AudioDevice::Run() { | 298 void AudioDevice::Run() { |
207 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); | 299 audio_thread_->SetThreadPriority(base::kThreadPriority_RealtimeAudio); |
208 | 300 |
209 int pending_data; | 301 int pending_data; |
210 const int samples_per_ms = static_cast<int>(sample_rate_) / 1000; | 302 const int samples_per_ms = static_cast<int>(sample_rate_) / 1000; |
211 const int bytes_per_ms = channels_ * (bits_per_sample_ / 8) * samples_per_ms; | 303 const int bytes_per_ms = channels_ * (bits_per_sample_ / 8) * samples_per_ms; |
212 | 304 |
213 while ((sizeof(pending_data) == socket_->Receive(&pending_data, | 305 while (sizeof(pending_data) == |
214 sizeof(pending_data))) && | 306 socket_->Receive(&pending_data, sizeof(pending_data))) { |
215 (pending_data >= 0)) { | 307 if (pending_data == media::AudioOutputController::kPauseMark) { |
308 memset(shared_memory_data(), 0, shared_memory_size_); | |
309 continue; | |
310 } else if (pending_data < 0) { | |
311 break; | |
312 } | |
216 | 313 |
217 // Convert the number of pending bytes in the render buffer | 314 // Convert the number of pending bytes in the render buffer |
218 // into milliseconds. | 315 // into milliseconds. |
219 audio_delay_milliseconds_ = pending_data / bytes_per_ms; | 316 audio_delay_milliseconds_ = pending_data / bytes_per_ms; |
220 FireRenderCallback(); | 317 FireRenderCallback(); |
221 } | 318 } |
222 } | 319 } |
223 | 320 |
224 void AudioDevice::FireRenderCallback() { | 321 void AudioDevice::FireRenderCallback() { |
225 TRACE_EVENT0("audio", "AudioDevice::FireRenderCallback"); | 322 TRACE_EVENT0("audio", "AudioDevice::FireRenderCallback"); |
226 | 323 |
227 if (callback_) { | 324 if (callback_) { |
228 // Update the audio-delay measurement then ask client to render audio. | 325 // Update the audio-delay measurement then ask client to render audio. |
229 callback_->Render(audio_data_, buffer_size_, audio_delay_milliseconds_); | 326 callback_->Render(audio_data_, buffer_size_, audio_delay_milliseconds_); |
230 | 327 |
231 // Interleave, scale, and clip to int16. | 328 // Interleave, scale, and clip to int16. |
329 // TODO(crogers): avoid converting to integer here, and pass the data | |
330 // to the browser process as float, so we don't lose precision for | |
331 // audio hardware which has better than 16bit precision. | |
232 media::InterleaveFloatToInt16(audio_data_, | 332 media::InterleaveFloatToInt16(audio_data_, |
233 static_cast<int16*>(shared_memory_data()), | 333 static_cast<int16*>(shared_memory_data()), |
234 buffer_size_); | 334 buffer_size_); |
235 } | 335 } |
236 } | 336 } |
237 | 337 |
338 void AudioDevice::ShutDownAudioThread() { | |
339 // Synchronize with OnLowLatencyCreated(). | |
340 base::AutoLock auto_lock(lock_); | |
341 | |
342 if (socket_.get()) { | |
343 socket_->Close(); | |
344 } | |
345 if (audio_thread_.get()) { | |
346 audio_thread_->Join(); | |
347 audio_thread_.reset(NULL); | |
348 } | |
349 // Note that the socket can't be reset until *after* joining the thread. | |
350 socket_.reset(NULL); | |
351 } | |
352 | |
238 double AudioDevice::GetAudioHardwareSampleRate() { | 353 double AudioDevice::GetAudioHardwareSampleRate() { |
239 // Uses cached value if possible. | 354 // Uses cached value if possible. |
240 static double hardware_sample_rate = 0; | 355 static double hardware_sample_rate = 0; |
241 if (!hardware_sample_rate) { | 356 if (!hardware_sample_rate) { |
242 RenderThreadImpl::current()->Send( | 357 RenderThreadImpl::current()->Send( |
243 new ViewHostMsg_GetHardwareSampleRate(&hardware_sample_rate)); | 358 new ViewHostMsg_GetHardwareSampleRate(&hardware_sample_rate)); |
244 } | 359 } |
245 return hardware_sample_rate; | 360 return hardware_sample_rate; |
246 } | 361 } |
247 | 362 |
248 size_t AudioDevice::GetAudioHardwareBufferSize() { | 363 size_t AudioDevice::GetAudioHardwareBufferSize() { |
249 // Uses cached value if possible. | 364 // Uses cached value if possible. |
250 static size_t buffer_size = 0; | 365 static size_t buffer_size = 0; |
251 | 366 |
252 if (!buffer_size) | 367 if (!buffer_size) |
253 buffer_size = media::GetAudioHardwareBufferSize(); | 368 buffer_size = media::GetAudioHardwareBufferSize(); |
254 | 369 |
255 return buffer_size; | 370 return buffer_size; |
256 } | 371 } |
OLD | NEW |