| 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" |
| 11 #include "base/process/process.h" | 11 #include "base/process/process.h" |
| 12 #include "content/browser/browser_main_loop.h" | 12 #include "content/browser/browser_main_loop.h" |
| 13 #include "content/browser/media/audio_stream_monitor.h" | |
| 14 #include "content/browser/media/capture/audio_mirroring_manager.h" | 13 #include "content/browser/media/capture/audio_mirroring_manager.h" |
| 15 #include "content/browser/media/media_internals.h" | 14 #include "content/browser/media/media_internals.h" |
| 16 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 15 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| 17 #include "content/browser/renderer_host/media/audio_sync_reader.h" | 16 #include "content/browser/renderer_host/media/audio_sync_reader.h" |
| 18 #include "content/browser/renderer_host/media/media_stream_manager.h" | 17 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 19 #include "content/common/media/audio_messages.h" | 18 #include "content/common/media/audio_messages.h" |
| 20 #include "content/public/browser/content_browser_client.h" | 19 #include "content/public/browser/content_browser_client.h" |
| 21 #include "content/public/browser/media_observer.h" | 20 #include "content/public/browser/media_observer.h" |
| 22 #include "content/public/common/content_switches.h" | 21 #include "content/public/common/content_switches.h" |
| 23 #include "media/audio/audio_manager_base.h" | 22 #include "media/audio/audio_manager_base.h" |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 | 258 |
| 260 AudioEntry* const entry = LookupById(stream_id); | 259 AudioEntry* const entry = LookupById(stream_id); |
| 261 if (!entry) | 260 if (!entry) |
| 262 return; | 261 return; |
| 263 | 262 |
| 264 Send(new AudioMsg_NotifyStreamStateChanged( | 263 Send(new AudioMsg_NotifyStreamStateChanged( |
| 265 stream_id, | 264 stream_id, |
| 266 is_playing ? media::AudioOutputIPCDelegate::kPlaying | 265 is_playing ? media::AudioOutputIPCDelegate::kPlaying |
| 267 : media::AudioOutputIPCDelegate::kPaused)); | 266 : media::AudioOutputIPCDelegate::kPaused)); |
| 268 | 267 |
| 269 if (is_playing) { | 268 MediaObserver* const media_observer = |
| 270 AudioStreamMonitor::StartMonitoringStream( | 269 GetContentClient()->browser()->GetMediaObserver(); |
| 271 render_process_id_, | 270 if (media_observer) { |
| 272 entry->render_frame_id(), | 271 if (is_playing) { |
| 273 entry->stream_id(), | 272 media_observer->OnAudioStreamPlaying( |
| 274 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, | 273 render_process_id_, |
| 275 entry->controller())); | 274 entry->render_frame_id(), |
| 276 // TODO(dalecurtis): See about using AudioStreamMonitor instead. | 275 entry->stream_id(), |
| 277 if (!entry->playing()) { | 276 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, |
| 278 entry->set_playing(true); | 277 entry->controller())); |
| 279 base::AtomicRefCountInc(&num_playing_streams_); | 278 if (!entry->playing()) { |
| 280 } | 279 entry->set_playing(true); |
| 281 } else { | 280 base::AtomicRefCountInc(&num_playing_streams_); |
| 282 AudioStreamMonitor::StopMonitoringStream( | 281 } |
| 283 render_process_id_, entry->render_frame_id(), entry->stream_id()); | 282 } else { |
| 284 // TODO(dalecurtis): See about using AudioStreamMonitor instead. | 283 media_observer->OnAudioStreamStopped(render_process_id_, |
| 285 if (entry->playing()) { | 284 entry->render_frame_id(), |
| 286 entry->set_playing(false); | 285 entry->stream_id()); |
| 287 base::AtomicRefCountDec(&num_playing_streams_); | 286 if (entry->playing()) { |
| 287 entry->set_playing(false); |
| 288 base::AtomicRefCountDec(&num_playing_streams_); |
| 289 } |
| 288 } | 290 } |
| 289 } | 291 } |
| 290 } | 292 } |
| 291 | 293 |
| 292 RenderViewHost::AudioOutputControllerList | 294 RenderViewHost::AudioOutputControllerList |
| 293 AudioRendererHost::DoGetOutputControllers(int render_view_id) const { | 295 AudioRendererHost::DoGetOutputControllers(int render_view_id) const { |
| 294 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 296 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 295 | 297 |
| 296 RenderViewHost::AudioOutputControllerList controllers; | 298 RenderViewHost::AudioOutputControllerList controllers; |
| 297 AudioEntryMap::const_iterator it = audio_entries_.begin(); | 299 AudioEntryMap::const_iterator it = audio_entries_.begin(); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 media::AudioOutputController* const controller = entry->controller(); | 447 media::AudioOutputController* const controller = entry->controller(); |
| 446 if (mirroring_manager_) | 448 if (mirroring_manager_) |
| 447 mirroring_manager_->RemoveDiverter(controller); | 449 mirroring_manager_->RemoveDiverter(controller); |
| 448 controller->Close( | 450 controller->Close( |
| 449 base::Bind(&AudioRendererHost::DeleteEntry, this, base::Passed(&entry))); | 451 base::Bind(&AudioRendererHost::DeleteEntry, this, base::Passed(&entry))); |
| 450 audio_log_->OnClosed(stream_id); | 452 audio_log_->OnClosed(stream_id); |
| 451 } | 453 } |
| 452 | 454 |
| 453 void AudioRendererHost::DeleteEntry(scoped_ptr<AudioEntry> entry) { | 455 void AudioRendererHost::DeleteEntry(scoped_ptr<AudioEntry> entry) { |
| 454 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 456 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 455 AudioStreamMonitor::StopMonitoringStream( | 457 |
| 456 render_process_id_, entry->render_frame_id(), entry->stream_id()); | 458 // At this point, make the final "say" in audio playback state. |
| 457 if (entry->playing()) | 459 MediaObserver* const media_observer = |
| 458 base::AtomicRefCountDec(&num_playing_streams_); | 460 GetContentClient()->browser()->GetMediaObserver(); |
| 461 if (media_observer) { |
| 462 media_observer->OnAudioStreamStopped(render_process_id_, |
| 463 entry->render_frame_id(), |
| 464 entry->stream_id()); |
| 465 if (entry->playing()) |
| 466 base::AtomicRefCountDec(&num_playing_streams_); |
| 467 } |
| 459 } | 468 } |
| 460 | 469 |
| 461 void AudioRendererHost::ReportErrorAndClose(int stream_id) { | 470 void AudioRendererHost::ReportErrorAndClose(int stream_id) { |
| 462 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 471 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 463 | 472 |
| 464 // Make sure this isn't a stray callback executing after the stream has been | 473 // Make sure this isn't a stray callback executing after the stream has been |
| 465 // closed, so error notifications aren't sent after clients believe the stream | 474 // closed, so error notifications aren't sent after clients believe the stream |
| 466 // is closed. | 475 // is closed. |
| 467 if (!LookupById(stream_id)) | 476 if (!LookupById(stream_id)) |
| 468 return; | 477 return; |
| 469 | 478 |
| 470 SendErrorMessage(stream_id); | 479 SendErrorMessage(stream_id); |
| 471 | 480 |
| 472 audio_log_->OnError(stream_id); | 481 audio_log_->OnError(stream_id); |
| 473 OnCloseStream(stream_id); | 482 OnCloseStream(stream_id); |
| 474 } | 483 } |
| 475 | 484 |
| 476 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { | 485 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { |
| 477 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 486 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 478 | 487 |
| 479 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id); | 488 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id); |
| 480 return i != audio_entries_.end() ? i->second : NULL; | 489 return i != audio_entries_.end() ? i->second : NULL; |
| 481 } | 490 } |
| 482 | 491 |
| 483 bool AudioRendererHost::HasActiveAudio() { | 492 bool AudioRendererHost::HasActiveAudio() { |
| 484 return !base::AtomicRefCountIsZero(&num_playing_streams_); | 493 return !base::AtomicRefCountIsZero(&num_playing_streams_); |
| 485 } | 494 } |
| 486 | 495 |
| 487 } // namespace content | 496 } // namespace content |
| OLD | NEW |