Chromium Code Reviews| 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/capture/audio_mirroring_manager.h" | 13 #include "content/browser/media/capture/audio_mirroring_manager.h" |
| 14 #include "content/browser/media/media_internals.h" | 14 #include "content/browser/media/media_internals.h" |
| 15 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 15 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| 16 #include "content/browser/renderer_host/media/audio_sync_reader.h" | 16 #include "content/browser/renderer_host/media/audio_sync_reader.h" |
| 17 #include "content/browser/renderer_host/media/media_stream_manager.h" | 17 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 18 #include "content/common/media/audio_messages.h" | 18 #include "content/common/media/audio_messages.h" |
| 19 #include "content/public/browser/content_browser_client.h" | 19 #include "content/public/browser/content_browser_client.h" |
| 20 #include "content/public/browser/media_observer.h" | 20 #include "content/public/browser/media_observer.h" |
| 21 #include "content/public/common/content_switches.h" | 21 #include "content/public/common/content_switches.h" |
| 22 #include "media/audio/audio_manager_base.h" | 22 #include "media/audio/audio_manager_base.h" |
| 23 #include "media/base/audio_bus.h" | 23 #include "media/base/audio_bus.h" |
| 24 #include "media/base/limits.h" | 24 #include "media/base/limits.h" |
| 25 | 25 |
| 26 // Only do audio stream monitoring for platforms that use it for the tab media | |
| 27 // indicator UI or the OOM killer. | |
| 28 #if !defined(OS_ANDROID) && !defined(OS_IOS) | |
| 29 #define AUDIO_STREAM_MONITORING | |
|
DaleCurtis
2014/08/29 23:56:47
It's unfortunate to define this everywhere. I'm op
| |
| 30 #include "content/browser/media/audio_stream_monitor.h" | |
| 31 #endif | |
| 32 | |
| 26 using media::AudioBus; | 33 using media::AudioBus; |
| 27 using media::AudioManager; | 34 using media::AudioManager; |
| 28 | 35 |
| 29 namespace content { | 36 namespace content { |
| 30 | 37 |
| 31 class AudioRendererHost::AudioEntry | 38 class AudioRendererHost::AudioEntry |
| 32 : public media::AudioOutputController::EventHandler { | 39 : public media::AudioOutputController::EventHandler { |
| 33 public: | 40 public: |
| 34 AudioEntry(AudioRendererHost* host, | 41 AudioEntry(AudioRendererHost* host, |
| 35 int stream_id, | 42 int stream_id, |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 | 272 |
| 266 AudioEntry* const entry = LookupById(stream_id); | 273 AudioEntry* const entry = LookupById(stream_id); |
| 267 if (!entry) | 274 if (!entry) |
| 268 return; | 275 return; |
| 269 | 276 |
| 270 Send(new AudioMsg_NotifyStreamStateChanged( | 277 Send(new AudioMsg_NotifyStreamStateChanged( |
| 271 stream_id, | 278 stream_id, |
| 272 is_playing ? media::AudioOutputIPCDelegate::kPlaying | 279 is_playing ? media::AudioOutputIPCDelegate::kPlaying |
| 273 : media::AudioOutputIPCDelegate::kPaused)); | 280 : media::AudioOutputIPCDelegate::kPaused)); |
| 274 | 281 |
| 275 MediaObserver* const media_observer = | 282 if (is_playing) { |
| 276 GetContentClient()->browser()->GetMediaObserver(); | 283 #if defined(AUDIO_STREAM_MONITORING) |
| 277 if (media_observer) { | 284 AudioStreamMonitor::StartMonitoringStream( |
| 278 if (is_playing) { | 285 render_process_id_, |
| 279 media_observer->OnAudioStreamPlaying( | 286 entry->render_frame_id(), |
| 280 render_process_id_, | 287 entry->stream_id(), |
| 281 entry->render_frame_id(), | 288 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, |
| 282 entry->stream_id(), | 289 entry->controller())); |
| 283 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, | 290 #endif |
| 284 entry->controller())); | 291 // TODO(dalecurtis): See about using AudioStreamMonitor instead. |
| 285 if (!entry->playing()) { | 292 if (!entry->playing()) { |
| 286 entry->set_playing(true); | 293 entry->set_playing(true); |
| 287 base::AtomicRefCountInc(&num_playing_streams_); | 294 base::AtomicRefCountInc(&num_playing_streams_); |
| 288 } | 295 } |
| 289 } else { | 296 } else { |
| 290 media_observer->OnAudioStreamStopped(render_process_id_, | 297 #if defined(AUDIO_STREAM_MONITORING) |
| 291 entry->render_frame_id(), | 298 AudioStreamMonitor::StopMonitoringStream( |
| 292 entry->stream_id()); | 299 render_process_id_, entry->render_frame_id(), entry->stream_id()); |
| 293 if (entry->playing()) { | 300 #endif |
| 294 entry->set_playing(false); | 301 // TODO(dalecurtis): See about using AudioStreamMonitor instead. |
| 295 base::AtomicRefCountDec(&num_playing_streams_); | 302 if (entry->playing()) { |
| 296 } | 303 entry->set_playing(false); |
| 304 base::AtomicRefCountDec(&num_playing_streams_); | |
| 297 } | 305 } |
| 298 } | 306 } |
| 299 } | 307 } |
| 300 | 308 |
| 301 RenderViewHost::AudioOutputControllerList | 309 RenderViewHost::AudioOutputControllerList |
| 302 AudioRendererHost::DoGetOutputControllers(int render_view_id) const { | 310 AudioRendererHost::DoGetOutputControllers(int render_view_id) const { |
| 303 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 311 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 304 | 312 |
| 305 RenderViewHost::AudioOutputControllerList controllers; | 313 RenderViewHost::AudioOutputControllerList controllers; |
| 306 AudioEntryMap::const_iterator it = audio_entries_.begin(); | 314 AudioEntryMap::const_iterator it = audio_entries_.begin(); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 456 mirroring_manager_->RemoveDiverter( | 464 mirroring_manager_->RemoveDiverter( |
| 457 render_process_id_, entry->render_view_id(), controller); | 465 render_process_id_, entry->render_view_id(), controller); |
| 458 } | 466 } |
| 459 controller->Close( | 467 controller->Close( |
| 460 base::Bind(&AudioRendererHost::DeleteEntry, this, base::Passed(&entry))); | 468 base::Bind(&AudioRendererHost::DeleteEntry, this, base::Passed(&entry))); |
| 461 audio_log_->OnClosed(stream_id); | 469 audio_log_->OnClosed(stream_id); |
| 462 } | 470 } |
| 463 | 471 |
| 464 void AudioRendererHost::DeleteEntry(scoped_ptr<AudioEntry> entry) { | 472 void AudioRendererHost::DeleteEntry(scoped_ptr<AudioEntry> entry) { |
| 465 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 473 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 466 | 474 #if defined(AUDIO_STREAM_MONITORING) |
| 467 // At this point, make the final "say" in audio playback state. | 475 AudioStreamMonitor::StopMonitoringStream( |
| 468 MediaObserver* const media_observer = | 476 render_process_id_, entry->render_frame_id(), entry->stream_id()); |
| 469 GetContentClient()->browser()->GetMediaObserver(); | 477 #endif |
| 470 if (media_observer) { | 478 if (entry->playing()) |
| 471 media_observer->OnAudioStreamStopped(render_process_id_, | 479 base::AtomicRefCountDec(&num_playing_streams_); |
| 472 entry->render_frame_id(), | |
| 473 entry->stream_id()); | |
| 474 if (entry->playing()) | |
| 475 base::AtomicRefCountDec(&num_playing_streams_); | |
| 476 } | |
| 477 } | 480 } |
| 478 | 481 |
| 479 void AudioRendererHost::ReportErrorAndClose(int stream_id) { | 482 void AudioRendererHost::ReportErrorAndClose(int stream_id) { |
| 480 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 483 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 481 | 484 |
| 482 // Make sure this isn't a stray callback executing after the stream has been | 485 // Make sure this isn't a stray callback executing after the stream has been |
| 483 // closed, so error notifications aren't sent after clients believe the stream | 486 // closed, so error notifications aren't sent after clients believe the stream |
| 484 // is closed. | 487 // is closed. |
| 485 if (!LookupById(stream_id)) | 488 if (!LookupById(stream_id)) |
| 486 return; | 489 return; |
| 487 | 490 |
| 488 SendErrorMessage(stream_id); | 491 SendErrorMessage(stream_id); |
| 489 | 492 |
| 490 audio_log_->OnError(stream_id); | 493 audio_log_->OnError(stream_id); |
| 491 OnCloseStream(stream_id); | 494 OnCloseStream(stream_id); |
| 492 } | 495 } |
| 493 | 496 |
| 494 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { | 497 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { |
| 495 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 498 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 496 | 499 |
| 497 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id); | 500 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id); |
| 498 return i != audio_entries_.end() ? i->second : NULL; | 501 return i != audio_entries_.end() ? i->second : NULL; |
| 499 } | 502 } |
| 500 | 503 |
| 501 bool AudioRendererHost::HasActiveAudio() { | 504 bool AudioRendererHost::HasActiveAudio() { |
| 502 return !base::AtomicRefCountIsZero(&num_playing_streams_); | 505 return !base::AtomicRefCountIsZero(&num_playing_streams_); |
| 503 } | 506 } |
| 504 | 507 |
| 505 } // namespace content | 508 } // namespace content |
| OLD | NEW |