| 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/browser/renderer_host/media/audio_renderer_host.h" | 5 #include "content/browser/renderer_host/media/audio_renderer_host.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/memory/shared_memory.h" | 9 #include "base/memory/shared_memory.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 | 372 |
| 373 scoped_ptr<AudioEntry> entry(new AudioEntry( | 373 scoped_ptr<AudioEntry> entry(new AudioEntry( |
| 374 this, stream_id, render_view_id, params, output_device_id, | 374 this, stream_id, render_view_id, params, output_device_id, |
| 375 input_device_id, shared_memory.Pass(), | 375 input_device_id, shared_memory.Pass(), |
| 376 reader.PassAs<media::AudioOutputController::SyncReader>())); | 376 reader.PassAs<media::AudioOutputController::SyncReader>())); |
| 377 if (mirroring_manager_) { | 377 if (mirroring_manager_) { |
| 378 mirroring_manager_->AddDiverter( | 378 mirroring_manager_->AddDiverter( |
| 379 render_process_id_, entry->render_view_id(), entry->controller()); | 379 render_process_id_, entry->render_view_id(), entry->controller()); |
| 380 } | 380 } |
| 381 audio_entries_.insert(std::make_pair(stream_id, entry.release())); | 381 audio_entries_.insert(std::make_pair(stream_id, entry.release())); |
| 382 if (media_internals_) { | 382 // if (media_internals_) { |
| 383 media_internals_->OnAudioStreamCreated( | 383 // media_internals_->OnAudioStreamCreated( |
| 384 this, stream_id, params, input_device_id); | 384 // this, stream_id, params, input_device_id); |
| 385 } | 385 // } |
| 386 } | 386 } |
| 387 | 387 |
| 388 void AudioRendererHost::OnPlayStream(int stream_id) { | 388 void AudioRendererHost::OnPlayStream(int stream_id) { |
| 389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 390 | 390 |
| 391 AudioEntry* entry = LookupById(stream_id); | 391 AudioEntry* entry = LookupById(stream_id); |
| 392 if (!entry) { | 392 if (!entry) { |
| 393 SendErrorMessage(stream_id); | 393 SendErrorMessage(stream_id); |
| 394 return; | 394 return; |
| 395 } | 395 } |
| 396 | 396 |
| 397 entry->controller()->Play(); | 397 entry->controller()->Play(); |
| 398 if (media_internals_) | 398 // if (media_internals_) |
| 399 media_internals_->OnSetAudioStreamPlaying(this, stream_id, true); | 399 // media_internals_->OnSetAudioStreamPlaying(this, stream_id, true); |
| 400 } | 400 } |
| 401 | 401 |
| 402 void AudioRendererHost::OnPauseStream(int stream_id) { | 402 void AudioRendererHost::OnPauseStream(int stream_id) { |
| 403 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 403 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 404 | 404 |
| 405 AudioEntry* entry = LookupById(stream_id); | 405 AudioEntry* entry = LookupById(stream_id); |
| 406 if (!entry) { | 406 if (!entry) { |
| 407 SendErrorMessage(stream_id); | 407 SendErrorMessage(stream_id); |
| 408 return; | 408 return; |
| 409 } | 409 } |
| 410 | 410 |
| 411 entry->controller()->Pause(); | 411 entry->controller()->Pause(); |
| 412 if (media_internals_) | 412 // if (media_internals_) |
| 413 media_internals_->OnSetAudioStreamPlaying(this, stream_id, false); | 413 // media_internals_->OnSetAudioStreamPlaying(this, stream_id, false); |
| 414 } | 414 } |
| 415 | 415 |
| 416 void AudioRendererHost::OnSetVolume(int stream_id, double volume) { | 416 void AudioRendererHost::OnSetVolume(int stream_id, double volume) { |
| 417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 418 | 418 |
| 419 AudioEntry* entry = LookupById(stream_id); | 419 AudioEntry* entry = LookupById(stream_id); |
| 420 if (!entry) { | 420 if (!entry) { |
| 421 SendErrorMessage(stream_id); | 421 SendErrorMessage(stream_id); |
| 422 return; | 422 return; |
| 423 } | 423 } |
| 424 | 424 |
| 425 // Make sure the volume is valid. | 425 // Make sure the volume is valid. |
| 426 if (volume < 0 || volume > 1.0) | 426 if (volume < 0 || volume > 1.0) |
| 427 return; | 427 return; |
| 428 entry->controller()->SetVolume(volume); | 428 entry->controller()->SetVolume(volume); |
| 429 if (media_internals_) | 429 // if (media_internals_) |
| 430 media_internals_->OnSetAudioStreamVolume(this, stream_id, volume); | 430 // media_internals_->OnSetAudioStreamVolume(this, stream_id, volume); |
| 431 } | 431 } |
| 432 | 432 |
| 433 void AudioRendererHost::SendErrorMessage(int stream_id) { | 433 void AudioRendererHost::SendErrorMessage(int stream_id) { |
| 434 Send(new AudioMsg_NotifyStreamStateChanged( | 434 Send(new AudioMsg_NotifyStreamStateChanged( |
| 435 stream_id, media::AudioOutputIPCDelegate::kError)); | 435 stream_id, media::AudioOutputIPCDelegate::kError)); |
| 436 } | 436 } |
| 437 | 437 |
| 438 void AudioRendererHost::OnCloseStream(int stream_id) { | 438 void AudioRendererHost::OnCloseStream(int stream_id) { |
| 439 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 439 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 440 | 440 |
| 441 // Prevent oustanding callbacks from attempting to close/delete the same | 441 // Prevent oustanding callbacks from attempting to close/delete the same |
| 442 // AudioEntry twice. | 442 // AudioEntry twice. |
| 443 AudioEntryMap::iterator i = audio_entries_.find(stream_id); | 443 AudioEntryMap::iterator i = audio_entries_.find(stream_id); |
| 444 if (i == audio_entries_.end()) | 444 if (i == audio_entries_.end()) |
| 445 return; | 445 return; |
| 446 scoped_ptr<AudioEntry> entry(i->second); | 446 scoped_ptr<AudioEntry> entry(i->second); |
| 447 audio_entries_.erase(i); | 447 audio_entries_.erase(i); |
| 448 | 448 |
| 449 media::AudioOutputController* const controller = entry->controller(); | 449 media::AudioOutputController* const controller = entry->controller(); |
| 450 if (mirroring_manager_) { | 450 if (mirroring_manager_) { |
| 451 mirroring_manager_->RemoveDiverter( | 451 mirroring_manager_->RemoveDiverter( |
| 452 render_process_id_, entry->render_view_id(), controller); | 452 render_process_id_, entry->render_view_id(), controller); |
| 453 } | 453 } |
| 454 controller->Close( | 454 controller->Close( |
| 455 base::Bind(&AudioRendererHost::DeleteEntry, this, base::Passed(&entry))); | 455 base::Bind(&AudioRendererHost::DeleteEntry, this, base::Passed(&entry))); |
| 456 | 456 |
| 457 if (media_internals_) | 457 // if (media_internals_) |
| 458 media_internals_->OnSetAudioStreamStatus(this, stream_id, "closed"); | 458 // media_internals_->OnSetAudioStreamStatus(this, stream_id, "closed"); |
| 459 } | 459 } |
| 460 | 460 |
| 461 void AudioRendererHost::DeleteEntry(scoped_ptr<AudioEntry> entry) { | 461 void AudioRendererHost::DeleteEntry(scoped_ptr<AudioEntry> entry) { |
| 462 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 462 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 463 | 463 |
| 464 // At this point, make the final "say" in audio playback state. | 464 // At this point, make the final "say" in audio playback state. |
| 465 MediaObserver* const media_observer = | 465 MediaObserver* const media_observer = |
| 466 GetContentClient()->browser()->GetMediaObserver(); | 466 GetContentClient()->browser()->GetMediaObserver(); |
| 467 if (media_observer) { | 467 if (media_observer) { |
| 468 media_observer->OnAudioStreamPlayingChanged( | 468 media_observer->OnAudioStreamPlayingChanged( |
| 469 render_process_id_, entry->render_view_id(), entry->stream_id(), | 469 render_process_id_, entry->render_view_id(), entry->stream_id(), |
| 470 false, -std::numeric_limits<float>::infinity(), false); | 470 false, -std::numeric_limits<float>::infinity(), false); |
| 471 } | 471 } |
| 472 | 472 |
| 473 // Notify the media observer. | 473 // Notify the media observer. |
| 474 if (media_internals_) | 474 // if (media_internals_) |
| 475 media_internals_->OnDeleteAudioStream(this, entry->stream_id()); | 475 // media_internals_->OnDeleteAudioStream(this, entry->stream_id()); |
| 476 | 476 |
| 477 // Note: |entry| will be deleted upon leaving this scope. | 477 // Note: |entry| will be deleted upon leaving this scope. |
| 478 } | 478 } |
| 479 | 479 |
| 480 void AudioRendererHost::ReportErrorAndClose(int stream_id) { | 480 void AudioRendererHost::ReportErrorAndClose(int stream_id) { |
| 481 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 481 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 482 | 482 |
| 483 // Make sure this isn't a stray callback executing after the stream has been | 483 // Make sure this isn't a stray callback executing after the stream has been |
| 484 // closed, so error notifications aren't sent after clients believe the stream | 484 // closed, so error notifications aren't sent after clients believe the stream |
| 485 // is closed. | 485 // is closed. |
| 486 if (!LookupById(stream_id)) | 486 if (!LookupById(stream_id)) |
| 487 return; | 487 return; |
| 488 | 488 |
| 489 SendErrorMessage(stream_id); | 489 SendErrorMessage(stream_id); |
| 490 | 490 |
| 491 if (media_internals_) | 491 // if (media_internals_) |
| 492 media_internals_->OnSetAudioStreamStatus(this, stream_id, "error"); | 492 // media_internals_->OnSetAudioStreamStatus(this, stream_id, "error"); |
| 493 | 493 |
| 494 OnCloseStream(stream_id); | 494 OnCloseStream(stream_id); |
| 495 } | 495 } |
| 496 | 496 |
| 497 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { | 497 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { |
| 498 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 498 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 499 | 499 |
| 500 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id); | 500 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id); |
| 501 return i != audio_entries_.end() ? i->second : NULL; | 501 return i != audio_entries_.end() ? i->second : NULL; |
| 502 } | 502 } |
| 503 | 503 |
| 504 } // namespace content | 504 } // namespace content |
| OLD | NEW |