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_renderer_impl.h" | 5 #include "content/renderer/media/audio_renderer_impl.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include "content/common/audio_messages.h" | 9 #include "content/common/audio_messages.h" |
10 #include "content/renderer/render_thread.h" | 10 #include "content/renderer/render_thread.h" |
11 #include "content/renderer/render_view.h" | 11 #include "content/renderer/render_view.h" |
12 #include "media/base/filter_host.h" | 12 #include "media/base/filter_host.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 // We will try to fill 200 ms worth of audio samples in each packet. A round | 16 // We will try to fill 200 ms worth of audio samples in each packet. A round |
17 // trip latency for IPC messages are typically 10 ms, this should give us | 17 // trip latency for IPC messages are typically 10 ms, this should give us |
18 // plenty of time to avoid clicks. | 18 // plenty of time to avoid clicks. |
19 const int kMillisecondsPerPacket = 200; | 19 const int kMillisecondsPerPacket = 200; |
20 | 20 |
21 // We have at most 3 packets in browser, i.e. 600 ms. This is a reasonable | 21 // We have at most 3 packets in browser, i.e. 600 ms. This is a reasonable |
22 // amount to avoid clicks. | 22 // amount to avoid clicks. |
23 const int kPacketsInBuffer = 3; | 23 const int kPacketsInBuffer = 3; |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 AudioRendererImpl::AudioRendererImpl(AudioMessageFilter* filter) | 27 AudioRendererImpl::AudioRendererImpl() |
28 : AudioRendererBase(), | 28 : AudioRendererBase(), |
29 bytes_per_second_(0), | 29 bytes_per_second_(0), |
30 filter_(filter), | |
31 stream_id_(0), | 30 stream_id_(0), |
32 shared_memory_(NULL), | 31 shared_memory_(NULL), |
33 shared_memory_size_(0), | 32 shared_memory_size_(0), |
34 io_loop_(filter->message_loop()), | |
35 stopped_(false), | 33 stopped_(false), |
36 pending_request_(false), | 34 pending_request_(false), |
37 prerolling_(false), | 35 prerolling_(false), |
38 preroll_bytes_(0) { | 36 preroll_bytes_(0) { |
37 DCHECK(AudioMessageFilter::GetInstance()); | |
38 io_loop_ = AudioMessageFilter::GetInstance()->message_loop(); | |
39 DCHECK(io_loop_); | 39 DCHECK(io_loop_); |
40 } | 40 } |
41 | 41 |
42 AudioRendererImpl::~AudioRendererImpl() { | 42 AudioRendererImpl::~AudioRendererImpl() { |
43 } | 43 } |
44 | 44 |
45 base::TimeDelta AudioRendererImpl::ConvertToDuration(int bytes) { | 45 base::TimeDelta AudioRendererImpl::ConvertToDuration(int bytes) { |
46 if (bytes_per_second_) { | 46 if (bytes_per_second_) { |
47 return base::TimeDelta::FromMicroseconds( | 47 return base::TimeDelta::FromMicroseconds( |
48 base::Time::kMicrosecondsPerSecond * bytes / bytes_per_second_); | 48 base::Time::kMicrosecondsPerSecond * bytes / bytes_per_second_); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 | 233 |
234 void AudioRendererImpl::CreateStreamTask(const AudioParameters& audio_params) { | 234 void AudioRendererImpl::CreateStreamTask(const AudioParameters& audio_params) { |
235 DCHECK(MessageLoop::current() == io_loop_); | 235 DCHECK(MessageLoop::current() == io_loop_); |
236 | 236 |
237 base::AutoLock auto_lock(lock_); | 237 base::AutoLock auto_lock(lock_); |
238 if (stopped_) | 238 if (stopped_) |
239 return; | 239 return; |
240 | 240 |
241 // Make sure we don't call create more than once. | 241 // Make sure we don't call create more than once. |
242 DCHECK_EQ(0, stream_id_); | 242 DCHECK_EQ(0, stream_id_); |
243 stream_id_ = filter_->AddDelegate(this); | 243 stream_id_ = AudioMessageFilter::GetInstance()->AddDelegate(this); |
244 io_loop_->AddDestructionObserver(this); | 244 io_loop_->AddDestructionObserver(this); |
245 | 245 |
246 AudioParameters params_to_send(audio_params); | 246 AudioParameters params_to_send(audio_params); |
247 // Let the browser choose packet size. | 247 // Let the browser choose packet size. |
248 params_to_send.samples_per_packet = 0; | 248 params_to_send.samples_per_packet = 0; |
249 | 249 |
250 filter_->Send(new AudioHostMsg_CreateStream( | 250 Send(new AudioHostMsg_CreateStream(0, stream_id_, params_to_send, false)); |
251 0, stream_id_, params_to_send, false)); | |
252 } | 251 } |
253 | 252 |
254 void AudioRendererImpl::PlayTask() { | 253 void AudioRendererImpl::PlayTask() { |
255 DCHECK(MessageLoop::current() == io_loop_); | 254 DCHECK(MessageLoop::current() == io_loop_); |
256 | 255 |
257 filter_->Send(new AudioHostMsg_PlayStream(0, stream_id_)); | 256 Send(new AudioHostMsg_PlayStream(0, stream_id_)); |
258 } | 257 } |
259 | 258 |
260 void AudioRendererImpl::PauseTask() { | 259 void AudioRendererImpl::PauseTask() { |
261 DCHECK(MessageLoop::current() == io_loop_); | 260 DCHECK(MessageLoop::current() == io_loop_); |
262 | 261 |
263 filter_->Send(new AudioHostMsg_PauseStream(0, stream_id_)); | 262 Send(new AudioHostMsg_PauseStream(0, stream_id_)); |
264 } | 263 } |
265 | 264 |
266 void AudioRendererImpl::SeekTask() { | 265 void AudioRendererImpl::SeekTask() { |
267 DCHECK(MessageLoop::current() == io_loop_); | 266 DCHECK(MessageLoop::current() == io_loop_); |
268 | 267 |
269 // We have to pause the audio stream before we can flush. | 268 // We have to pause the audio stream before we can flush. |
270 filter_->Send(new AudioHostMsg_PauseStream(0, stream_id_)); | 269 Send(new AudioHostMsg_PauseStream(0, stream_id_)); |
271 filter_->Send(new AudioHostMsg_FlushStream(0, stream_id_)); | 270 Send(new AudioHostMsg_FlushStream(0, stream_id_)); |
272 } | 271 } |
273 | 272 |
274 void AudioRendererImpl::DestroyTask() { | 273 void AudioRendererImpl::DestroyTask() { |
275 DCHECK(MessageLoop::current() == io_loop_); | 274 DCHECK(MessageLoop::current() == io_loop_); |
276 | 275 |
277 // Make sure we don't call destroy more than once. | 276 // Make sure we don't call destroy more than once. |
278 DCHECK_NE(0, stream_id_); | 277 DCHECK_NE(0, stream_id_); |
279 filter_->RemoveDelegate(stream_id_); | 278 AudioMessageFilter::GetInstance()->RemoveDelegate(stream_id_); |
280 filter_->Send(new AudioHostMsg_CloseStream(0, stream_id_)); | 279 Send(new AudioHostMsg_CloseStream(0, stream_id_)); |
281 io_loop_->RemoveDestructionObserver(this); | 280 io_loop_->RemoveDestructionObserver(this); |
282 stream_id_ = 0; | 281 stream_id_ = 0; |
283 } | 282 } |
284 | 283 |
285 void AudioRendererImpl::SetVolumeTask(double volume) { | 284 void AudioRendererImpl::SetVolumeTask(double volume) { |
286 DCHECK(MessageLoop::current() == io_loop_); | 285 DCHECK(MessageLoop::current() == io_loop_); |
287 | 286 |
288 base::AutoLock auto_lock(lock_); | 287 base::AutoLock auto_lock(lock_); |
289 if (stopped_) | 288 if (stopped_) |
290 return; | 289 return; |
291 filter_->Send(new AudioHostMsg_SetVolume(0, stream_id_, volume)); | 290 Send(new AudioHostMsg_SetVolume(0, stream_id_, volume)); |
292 } | 291 } |
293 | 292 |
294 void AudioRendererImpl::NotifyPacketReadyTask() { | 293 void AudioRendererImpl::NotifyPacketReadyTask() { |
295 DCHECK(MessageLoop::current() == io_loop_); | 294 DCHECK(MessageLoop::current() == io_loop_); |
296 | 295 |
297 base::AutoLock auto_lock(lock_); | 296 base::AutoLock auto_lock(lock_); |
298 if (stopped_) | 297 if (stopped_) |
299 return; | 298 return; |
300 if (pending_request_ && GetPlaybackRate() > 0.0f) { | 299 if (pending_request_ && GetPlaybackRate() > 0.0f) { |
301 DCHECK(shared_memory_.get()); | 300 DCHECK(shared_memory_.get()); |
(...skipping 22 matching lines...) Expand all Loading... | |
324 request_delay = base::TimeDelta::FromMicroseconds( | 323 request_delay = base::TimeDelta::FromMicroseconds( |
325 static_cast<int64>(ceil(request_delay.InMicroseconds() * | 324 static_cast<int64>(ceil(request_delay.InMicroseconds() * |
326 GetPlaybackRate()))); | 325 GetPlaybackRate()))); |
327 } | 326 } |
328 | 327 |
329 uint32 filled = FillBuffer(static_cast<uint8*>(shared_memory_->memory()), | 328 uint32 filled = FillBuffer(static_cast<uint8*>(shared_memory_->memory()), |
330 shared_memory_size_, request_delay, | 329 shared_memory_size_, request_delay, |
331 request_buffers_state_.pending_bytes == 0); | 330 request_buffers_state_.pending_bytes == 0); |
332 pending_request_ = false; | 331 pending_request_ = false; |
333 // Then tell browser process we are done filling into the buffer. | 332 // Then tell browser process we are done filling into the buffer. |
334 filter_->Send(new AudioHostMsg_NotifyPacketReady(0, stream_id_, filled)); | 333 Send(new AudioHostMsg_NotifyPacketReady(0, stream_id_, filled)); |
335 } | 334 } |
336 } | 335 } |
337 | 336 |
338 void AudioRendererImpl::WillDestroyCurrentMessageLoop() { | 337 void AudioRendererImpl::WillDestroyCurrentMessageLoop() { |
339 DCHECK(MessageLoop::current() == io_loop_); | 338 DCHECK(MessageLoop::current() == io_loop_); |
340 | 339 |
341 // We treat the IO loop going away the same as stopping. | 340 // We treat the IO loop going away the same as stopping. |
342 base::AutoLock auto_lock(lock_); | 341 base::AutoLock auto_lock(lock_); |
343 if (stopped_) | 342 if (stopped_) |
344 return; | 343 return; |
345 | 344 |
346 stopped_ = true; | 345 stopped_ = true; |
347 DestroyTask(); | 346 DestroyTask(); |
348 } | 347 } |
348 | |
349 void AudioRendererImpl::Send(IPC::Message* message) { | |
350 AudioMessageFilter::GetInstance()->Send(message); | |
scherkus (not reviewing)
2011/06/17 17:50:10
nice!
might want to add a DCHECK for io_loop_
henrika_dont_use
2011/06/21 16:43:38
Done.
| |
351 } | |
OLD | NEW |