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