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/media/audio_messages.h" | 9 #include "content/common/media/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(RenderThread::current()); | |
jam
2011/06/23 18:59:55
nit: no need
henrika_dont_use
2011/06/27 15:05:44
Done.
| |
38 filter_ = RenderThread::current()->audio_message_filter(); | |
wjia(left Chromium)
2011/06/23 19:43:22
it's easier to test AudioRendererImpl if |filter_|
henrika_dont_use
2011/06/27 15:05:44
Should be OK without injecting since it is possibl
| |
39 DCHECK(filter_); | |
jam
2011/06/23 18:59:55
ditto
henrika_dont_use
2011/06/27 15:05:44
Done.
| |
40 io_loop_ = filter_->message_loop(); | |
jam
2011/06/23 18:59:55
don't need this, just use ChildProcess::current()-
henrika_dont_use
2011/06/27 15:05:44
Done.
| |
39 DCHECK(io_loop_); | 41 DCHECK(io_loop_); |
40 } | 42 } |
41 | 43 |
42 AudioRendererImpl::~AudioRendererImpl() { | 44 AudioRendererImpl::~AudioRendererImpl() { |
43 } | 45 } |
44 | 46 |
45 base::TimeDelta AudioRendererImpl::ConvertToDuration(int bytes) { | 47 base::TimeDelta AudioRendererImpl::ConvertToDuration(int bytes) { |
46 if (bytes_per_second_) { | 48 if (bytes_per_second_) { |
47 return base::TimeDelta::FromMicroseconds( | 49 return base::TimeDelta::FromMicroseconds( |
48 base::Time::kMicrosecondsPerSecond * bytes / bytes_per_second_); | 50 base::Time::kMicrosecondsPerSecond * bytes / bytes_per_second_); |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 | 242 |
241 // Make sure we don't call create more than once. | 243 // Make sure we don't call create more than once. |
242 DCHECK_EQ(0, stream_id_); | 244 DCHECK_EQ(0, stream_id_); |
243 stream_id_ = filter_->AddDelegate(this); | 245 stream_id_ = filter_->AddDelegate(this); |
244 io_loop_->AddDestructionObserver(this); | 246 io_loop_->AddDestructionObserver(this); |
245 | 247 |
246 AudioParameters params_to_send(audio_params); | 248 AudioParameters params_to_send(audio_params); |
247 // Let the browser choose packet size. | 249 // Let the browser choose packet size. |
248 params_to_send.samples_per_packet = 0; | 250 params_to_send.samples_per_packet = 0; |
249 | 251 |
250 filter_->Send(new AudioHostMsg_CreateStream( | 252 Send(new AudioHostMsg_CreateStream(stream_id_, params_to_send, false)); |
251 0, stream_id_, params_to_send, false)); | |
252 } | 253 } |
253 | 254 |
254 void AudioRendererImpl::PlayTask() { | 255 void AudioRendererImpl::PlayTask() { |
255 DCHECK(MessageLoop::current() == io_loop_); | 256 DCHECK(MessageLoop::current() == io_loop_); |
256 | 257 |
257 filter_->Send(new AudioHostMsg_PlayStream(0, stream_id_)); | 258 Send(new AudioHostMsg_PlayStream(stream_id_)); |
258 } | 259 } |
259 | 260 |
260 void AudioRendererImpl::PauseTask() { | 261 void AudioRendererImpl::PauseTask() { |
261 DCHECK(MessageLoop::current() == io_loop_); | 262 DCHECK(MessageLoop::current() == io_loop_); |
262 | 263 |
263 filter_->Send(new AudioHostMsg_PauseStream(0, stream_id_)); | 264 Send(new AudioHostMsg_PauseStream(stream_id_)); |
264 } | 265 } |
265 | 266 |
266 void AudioRendererImpl::SeekTask() { | 267 void AudioRendererImpl::SeekTask() { |
267 DCHECK(MessageLoop::current() == io_loop_); | 268 DCHECK(MessageLoop::current() == io_loop_); |
268 | 269 |
269 // We have to pause the audio stream before we can flush. | 270 // We have to pause the audio stream before we can flush. |
270 filter_->Send(new AudioHostMsg_PauseStream(0, stream_id_)); | 271 Send(new AudioHostMsg_PauseStream(stream_id_)); |
271 filter_->Send(new AudioHostMsg_FlushStream(0, stream_id_)); | 272 Send(new AudioHostMsg_FlushStream(stream_id_)); |
272 } | 273 } |
273 | 274 |
274 void AudioRendererImpl::DestroyTask() { | 275 void AudioRendererImpl::DestroyTask() { |
275 DCHECK(MessageLoop::current() == io_loop_); | 276 DCHECK(MessageLoop::current() == io_loop_); |
276 | 277 |
277 // Make sure we don't call destroy more than once. | 278 // Make sure we don't call destroy more than once. |
278 DCHECK_NE(0, stream_id_); | 279 DCHECK_NE(0, stream_id_); |
279 filter_->RemoveDelegate(stream_id_); | 280 filter_->RemoveDelegate(stream_id_); |
280 filter_->Send(new AudioHostMsg_CloseStream(0, stream_id_)); | 281 Send(new AudioHostMsg_CloseStream(stream_id_)); |
281 io_loop_->RemoveDestructionObserver(this); | 282 io_loop_->RemoveDestructionObserver(this); |
282 stream_id_ = 0; | 283 stream_id_ = 0; |
283 } | 284 } |
284 | 285 |
285 void AudioRendererImpl::SetVolumeTask(double volume) { | 286 void AudioRendererImpl::SetVolumeTask(double volume) { |
286 DCHECK(MessageLoop::current() == io_loop_); | 287 DCHECK(MessageLoop::current() == io_loop_); |
287 | 288 |
288 base::AutoLock auto_lock(lock_); | 289 base::AutoLock auto_lock(lock_); |
289 if (stopped_) | 290 if (stopped_) |
290 return; | 291 return; |
291 filter_->Send(new AudioHostMsg_SetVolume(0, stream_id_, volume)); | 292 Send(new AudioHostMsg_SetVolume(stream_id_, volume)); |
292 } | 293 } |
293 | 294 |
294 void AudioRendererImpl::NotifyPacketReadyTask() { | 295 void AudioRendererImpl::NotifyPacketReadyTask() { |
295 DCHECK(MessageLoop::current() == io_loop_); | 296 DCHECK(MessageLoop::current() == io_loop_); |
296 | 297 |
297 base::AutoLock auto_lock(lock_); | 298 base::AutoLock auto_lock(lock_); |
298 if (stopped_) | 299 if (stopped_) |
299 return; | 300 return; |
300 if (pending_request_ && GetPlaybackRate() > 0.0f) { | 301 if (pending_request_ && GetPlaybackRate() > 0.0f) { |
301 DCHECK(shared_memory_.get()); | 302 DCHECK(shared_memory_.get()); |
(...skipping 22 matching lines...) Expand all Loading... | |
324 request_delay = base::TimeDelta::FromMicroseconds( | 325 request_delay = base::TimeDelta::FromMicroseconds( |
325 static_cast<int64>(ceil(request_delay.InMicroseconds() * | 326 static_cast<int64>(ceil(request_delay.InMicroseconds() * |
326 GetPlaybackRate()))); | 327 GetPlaybackRate()))); |
327 } | 328 } |
328 | 329 |
329 uint32 filled = FillBuffer(static_cast<uint8*>(shared_memory_->memory()), | 330 uint32 filled = FillBuffer(static_cast<uint8*>(shared_memory_->memory()), |
330 shared_memory_size_, request_delay, | 331 shared_memory_size_, request_delay, |
331 request_buffers_state_.pending_bytes == 0); | 332 request_buffers_state_.pending_bytes == 0); |
332 pending_request_ = false; | 333 pending_request_ = false; |
333 // Then tell browser process we are done filling into the buffer. | 334 // Then tell browser process we are done filling into the buffer. |
334 filter_->Send(new AudioHostMsg_NotifyPacketReady(0, stream_id_, filled)); | 335 Send(new AudioHostMsg_NotifyPacketReady(stream_id_, filled)); |
335 } | 336 } |
336 } | 337 } |
337 | 338 |
338 void AudioRendererImpl::WillDestroyCurrentMessageLoop() { | 339 void AudioRendererImpl::WillDestroyCurrentMessageLoop() { |
339 DCHECK(MessageLoop::current() == io_loop_); | 340 DCHECK(MessageLoop::current() == io_loop_); |
340 | 341 |
341 // We treat the IO loop going away the same as stopping. | 342 // We treat the IO loop going away the same as stopping. |
342 base::AutoLock auto_lock(lock_); | 343 base::AutoLock auto_lock(lock_); |
343 if (stopped_) | 344 if (stopped_) |
344 return; | 345 return; |
345 | 346 |
346 stopped_ = true; | 347 stopped_ = true; |
347 DestroyTask(); | 348 DestroyTask(); |
348 } | 349 } |
350 | |
351 void AudioRendererImpl::Send(IPC::Message* message) { | |
352 DCHECK(MessageLoop::current() == io_loop_); | |
jam
2011/06/23 18:59:55
nit: this isn't needed since filter_ posttasks to
henrika_dont_use
2011/06/27 15:05:44
Done.
| |
353 filter_->Send(message); | |
354 } | |
OLD | NEW |