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 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 | 266 |
266 AudioEntry* const entry = LookupById(stream_id); | 267 AudioEntry* const entry = LookupById(stream_id); |
267 if (!entry) | 268 if (!entry) |
268 return; | 269 return; |
269 | 270 |
270 Send(new AudioMsg_NotifyStreamStateChanged( | 271 Send(new AudioMsg_NotifyStreamStateChanged( |
271 stream_id, | 272 stream_id, |
272 is_playing ? media::AudioOutputIPCDelegate::kPlaying | 273 is_playing ? media::AudioOutputIPCDelegate::kPlaying |
273 : media::AudioOutputIPCDelegate::kPaused)); | 274 : media::AudioOutputIPCDelegate::kPaused)); |
274 | 275 |
275 MediaObserver* const media_observer = | 276 if (is_playing) { |
276 GetContentClient()->browser()->GetMediaObserver(); | 277 AudioStreamMonitor::StartMonitoringStream( |
277 if (media_observer) { | 278 render_process_id_, |
278 if (is_playing) { | 279 entry->render_frame_id(), |
279 media_observer->OnAudioStreamPlaying( | 280 entry->stream_id(), |
280 render_process_id_, | 281 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, |
281 entry->render_frame_id(), | 282 entry->controller())); |
282 entry->stream_id(), | 283 // TODO(dalecurtis): See about using AudioStreamMonitor instead. |
283 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, | 284 if (!entry->playing()) { |
284 entry->controller())); | 285 entry->set_playing(true); |
285 if (!entry->playing()) { | 286 base::AtomicRefCountInc(&num_playing_streams_); |
286 entry->set_playing(true); | 287 } |
287 base::AtomicRefCountInc(&num_playing_streams_); | 288 } else { |
288 } | 289 AudioStreamMonitor::StopMonitoringStream( |
289 } else { | 290 render_process_id_, entry->render_frame_id(), entry->stream_id()); |
290 media_observer->OnAudioStreamStopped(render_process_id_, | 291 // TODO(dalecurtis): See about using AudioStreamMonitor instead. |
291 entry->render_frame_id(), | 292 if (entry->playing()) { |
292 entry->stream_id()); | 293 entry->set_playing(false); |
293 if (entry->playing()) { | 294 base::AtomicRefCountDec(&num_playing_streams_); |
294 entry->set_playing(false); | |
295 base::AtomicRefCountDec(&num_playing_streams_); | |
296 } | |
297 } | 295 } |
298 } | 296 } |
299 } | 297 } |
300 | 298 |
301 RenderViewHost::AudioOutputControllerList | 299 RenderViewHost::AudioOutputControllerList |
302 AudioRendererHost::DoGetOutputControllers(int render_view_id) const { | 300 AudioRendererHost::DoGetOutputControllers(int render_view_id) const { |
303 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 301 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
304 | 302 |
305 RenderViewHost::AudioOutputControllerList controllers; | 303 RenderViewHost::AudioOutputControllerList controllers; |
306 AudioEntryMap::const_iterator it = audio_entries_.begin(); | 304 AudioEntryMap::const_iterator it = audio_entries_.begin(); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 mirroring_manager_->RemoveDiverter( | 454 mirroring_manager_->RemoveDiverter( |
457 render_process_id_, entry->render_view_id(), controller); | 455 render_process_id_, entry->render_view_id(), controller); |
458 } | 456 } |
459 controller->Close( | 457 controller->Close( |
460 base::Bind(&AudioRendererHost::DeleteEntry, this, base::Passed(&entry))); | 458 base::Bind(&AudioRendererHost::DeleteEntry, this, base::Passed(&entry))); |
461 audio_log_->OnClosed(stream_id); | 459 audio_log_->OnClosed(stream_id); |
462 } | 460 } |
463 | 461 |
464 void AudioRendererHost::DeleteEntry(scoped_ptr<AudioEntry> entry) { | 462 void AudioRendererHost::DeleteEntry(scoped_ptr<AudioEntry> entry) { |
465 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 463 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
466 | 464 AudioStreamMonitor::StopMonitoringStream( |
467 // At this point, make the final "say" in audio playback state. | 465 render_process_id_, entry->render_frame_id(), entry->stream_id()); |
468 MediaObserver* const media_observer = | 466 if (entry->playing()) |
469 GetContentClient()->browser()->GetMediaObserver(); | 467 base::AtomicRefCountDec(&num_playing_streams_); |
470 if (media_observer) { | |
471 media_observer->OnAudioStreamStopped(render_process_id_, | |
472 entry->render_frame_id(), | |
473 entry->stream_id()); | |
474 if (entry->playing()) | |
475 base::AtomicRefCountDec(&num_playing_streams_); | |
476 } | |
477 } | 468 } |
478 | 469 |
479 void AudioRendererHost::ReportErrorAndClose(int stream_id) { | 470 void AudioRendererHost::ReportErrorAndClose(int stream_id) { |
480 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 471 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
481 | 472 |
482 // 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 |
483 // 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 |
484 // is closed. | 475 // is closed. |
485 if (!LookupById(stream_id)) | 476 if (!LookupById(stream_id)) |
486 return; | 477 return; |
487 | 478 |
488 SendErrorMessage(stream_id); | 479 SendErrorMessage(stream_id); |
489 | 480 |
490 audio_log_->OnError(stream_id); | 481 audio_log_->OnError(stream_id); |
491 OnCloseStream(stream_id); | 482 OnCloseStream(stream_id); |
492 } | 483 } |
493 | 484 |
494 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { | 485 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { |
495 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 486 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
496 | 487 |
497 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id); | 488 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id); |
498 return i != audio_entries_.end() ? i->second : NULL; | 489 return i != audio_entries_.end() ? i->second : NULL; |
499 } | 490 } |
500 | 491 |
501 bool AudioRendererHost::HasActiveAudio() { | 492 bool AudioRendererHost::HasActiveAudio() { |
502 return !base::AtomicRefCountIsZero(&num_playing_streams_); | 493 return !base::AtomicRefCountIsZero(&num_playing_streams_); |
503 } | 494 } |
504 | 495 |
505 } // namespace content | 496 } // namespace content |
OLD | NEW |