| 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(int routing_id) |
| 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 AudioMessageFilter* audio_message_filter = AudioMessageFilter::GetInstance(); |
| 38 DCHECK(audio_message_filter); |
| 39 if (!audio_message_filter->RoutingIDHasBeenSet()) { |
| 40 // Ensure that first user sets a valid routing ID. |
| 41 // This routing ID will remain during the lifetime of the audio message |
| 42 // filter (which is a singleton). |
| 43 audio_message_filter->SetRoutingID(routing_id); |
| 44 } |
| 45 io_loop_ = audio_message_filter->message_loop(); |
| 39 DCHECK(io_loop_); | 46 DCHECK(io_loop_); |
| 40 } | 47 } |
| 41 | 48 |
| 42 AudioRendererImpl::~AudioRendererImpl() { | 49 AudioRendererImpl::~AudioRendererImpl() { |
| 43 } | 50 } |
| 44 | 51 |
| 45 base::TimeDelta AudioRendererImpl::ConvertToDuration(int bytes) { | 52 base::TimeDelta AudioRendererImpl::ConvertToDuration(int bytes) { |
| 46 if (bytes_per_second_) { | 53 if (bytes_per_second_) { |
| 47 return base::TimeDelta::FromMicroseconds( | 54 return base::TimeDelta::FromMicroseconds( |
| 48 base::Time::kMicrosecondsPerSecond * bytes / bytes_per_second_); | 55 base::Time::kMicrosecondsPerSecond * bytes / bytes_per_second_); |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 240 |
| 234 void AudioRendererImpl::CreateStreamTask(const AudioParameters& audio_params) { | 241 void AudioRendererImpl::CreateStreamTask(const AudioParameters& audio_params) { |
| 235 DCHECK(MessageLoop::current() == io_loop_); | 242 DCHECK(MessageLoop::current() == io_loop_); |
| 236 | 243 |
| 237 base::AutoLock auto_lock(lock_); | 244 base::AutoLock auto_lock(lock_); |
| 238 if (stopped_) | 245 if (stopped_) |
| 239 return; | 246 return; |
| 240 | 247 |
| 241 // Make sure we don't call create more than once. | 248 // Make sure we don't call create more than once. |
| 242 DCHECK_EQ(0, stream_id_); | 249 DCHECK_EQ(0, stream_id_); |
| 243 stream_id_ = filter_->AddDelegate(this); | 250 stream_id_ = AudioMessageFilter::GetInstance()->AddDelegate(this); |
| 244 io_loop_->AddDestructionObserver(this); | 251 io_loop_->AddDestructionObserver(this); |
| 245 | 252 |
| 246 AudioParameters params_to_send(audio_params); | 253 AudioParameters params_to_send(audio_params); |
| 247 // Let the browser choose packet size. | 254 // Let the browser choose packet size. |
| 248 params_to_send.samples_per_packet = 0; | 255 params_to_send.samples_per_packet = 0; |
| 249 | 256 |
| 250 filter_->Send(new AudioHostMsg_CreateStream( | 257 Send(new AudioHostMsg_CreateStream(0, stream_id_, params_to_send, false)); |
| 251 0, stream_id_, params_to_send, false)); | |
| 252 } | 258 } |
| 253 | 259 |
| 254 void AudioRendererImpl::PlayTask() { | 260 void AudioRendererImpl::PlayTask() { |
| 255 DCHECK(MessageLoop::current() == io_loop_); | 261 DCHECK(MessageLoop::current() == io_loop_); |
| 256 | 262 |
| 257 filter_->Send(new AudioHostMsg_PlayStream(0, stream_id_)); | 263 Send(new AudioHostMsg_PlayStream(0, stream_id_)); |
| 258 } | 264 } |
| 259 | 265 |
| 260 void AudioRendererImpl::PauseTask() { | 266 void AudioRendererImpl::PauseTask() { |
| 261 DCHECK(MessageLoop::current() == io_loop_); | 267 DCHECK(MessageLoop::current() == io_loop_); |
| 262 | 268 |
| 263 filter_->Send(new AudioHostMsg_PauseStream(0, stream_id_)); | 269 Send(new AudioHostMsg_PauseStream(0, stream_id_)); |
| 264 } | 270 } |
| 265 | 271 |
| 266 void AudioRendererImpl::SeekTask() { | 272 void AudioRendererImpl::SeekTask() { |
| 267 DCHECK(MessageLoop::current() == io_loop_); | 273 DCHECK(MessageLoop::current() == io_loop_); |
| 268 | 274 |
| 269 // We have to pause the audio stream before we can flush. | 275 // We have to pause the audio stream before we can flush. |
| 270 filter_->Send(new AudioHostMsg_PauseStream(0, stream_id_)); | 276 Send(new AudioHostMsg_PauseStream(0, stream_id_)); |
| 271 filter_->Send(new AudioHostMsg_FlushStream(0, stream_id_)); | 277 Send(new AudioHostMsg_FlushStream(0, stream_id_)); |
| 272 } | 278 } |
| 273 | 279 |
| 274 void AudioRendererImpl::DestroyTask() { | 280 void AudioRendererImpl::DestroyTask() { |
| 275 DCHECK(MessageLoop::current() == io_loop_); | 281 DCHECK(MessageLoop::current() == io_loop_); |
| 276 | 282 |
| 277 // Make sure we don't call destroy more than once. | 283 // Make sure we don't call destroy more than once. |
| 278 DCHECK_NE(0, stream_id_); | 284 DCHECK_NE(0, stream_id_); |
| 279 filter_->RemoveDelegate(stream_id_); | 285 AudioMessageFilter::GetInstance()->RemoveDelegate(stream_id_); |
| 280 filter_->Send(new AudioHostMsg_CloseStream(0, stream_id_)); | 286 Send(new AudioHostMsg_CloseStream(0, stream_id_)); |
| 281 io_loop_->RemoveDestructionObserver(this); | 287 io_loop_->RemoveDestructionObserver(this); |
| 282 stream_id_ = 0; | 288 stream_id_ = 0; |
| 283 } | 289 } |
| 284 | 290 |
| 285 void AudioRendererImpl::SetVolumeTask(double volume) { | 291 void AudioRendererImpl::SetVolumeTask(double volume) { |
| 286 DCHECK(MessageLoop::current() == io_loop_); | 292 DCHECK(MessageLoop::current() == io_loop_); |
| 287 | 293 |
| 288 base::AutoLock auto_lock(lock_); | 294 base::AutoLock auto_lock(lock_); |
| 289 if (stopped_) | 295 if (stopped_) |
| 290 return; | 296 return; |
| 291 filter_->Send(new AudioHostMsg_SetVolume(0, stream_id_, volume)); | 297 Send(new AudioHostMsg_SetVolume(0, stream_id_, volume)); |
| 292 } | 298 } |
| 293 | 299 |
| 294 void AudioRendererImpl::NotifyPacketReadyTask() { | 300 void AudioRendererImpl::NotifyPacketReadyTask() { |
| 295 DCHECK(MessageLoop::current() == io_loop_); | 301 DCHECK(MessageLoop::current() == io_loop_); |
| 296 | 302 |
| 297 base::AutoLock auto_lock(lock_); | 303 base::AutoLock auto_lock(lock_); |
| 298 if (stopped_) | 304 if (stopped_) |
| 299 return; | 305 return; |
| 300 if (pending_request_ && GetPlaybackRate() > 0.0f) { | 306 if (pending_request_ && GetPlaybackRate() > 0.0f) { |
| 301 DCHECK(shared_memory_.get()); | 307 DCHECK(shared_memory_.get()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 324 request_delay = base::TimeDelta::FromMicroseconds( | 330 request_delay = base::TimeDelta::FromMicroseconds( |
| 325 static_cast<int64>(ceil(request_delay.InMicroseconds() * | 331 static_cast<int64>(ceil(request_delay.InMicroseconds() * |
| 326 GetPlaybackRate()))); | 332 GetPlaybackRate()))); |
| 327 } | 333 } |
| 328 | 334 |
| 329 uint32 filled = FillBuffer(static_cast<uint8*>(shared_memory_->memory()), | 335 uint32 filled = FillBuffer(static_cast<uint8*>(shared_memory_->memory()), |
| 330 shared_memory_size_, request_delay, | 336 shared_memory_size_, request_delay, |
| 331 request_buffers_state_.pending_bytes == 0); | 337 request_buffers_state_.pending_bytes == 0); |
| 332 pending_request_ = false; | 338 pending_request_ = false; |
| 333 // Then tell browser process we are done filling into the buffer. | 339 // Then tell browser process we are done filling into the buffer. |
| 334 filter_->Send(new AudioHostMsg_NotifyPacketReady(0, stream_id_, filled)); | 340 Send(new AudioHostMsg_NotifyPacketReady(0, stream_id_, filled)); |
| 335 } | 341 } |
| 336 } | 342 } |
| 337 | 343 |
| 338 void AudioRendererImpl::WillDestroyCurrentMessageLoop() { | 344 void AudioRendererImpl::WillDestroyCurrentMessageLoop() { |
| 339 DCHECK(MessageLoop::current() == io_loop_); | 345 DCHECK(MessageLoop::current() == io_loop_); |
| 340 | 346 |
| 341 // We treat the IO loop going away the same as stopping. | 347 // We treat the IO loop going away the same as stopping. |
| 342 base::AutoLock auto_lock(lock_); | 348 base::AutoLock auto_lock(lock_); |
| 343 if (stopped_) | 349 if (stopped_) |
| 344 return; | 350 return; |
| 345 | 351 |
| 346 stopped_ = true; | 352 stopped_ = true; |
| 347 DestroyTask(); | 353 DestroyTask(); |
| 348 } | 354 } |
| 355 |
| 356 void AudioRendererImpl::Send(IPC::Message* message) { |
| 357 DCHECK(MessageLoop::current() == io_loop_); |
| 358 AudioMessageFilter::GetInstance()->Send(message); |
| 359 } |
| OLD | NEW |