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/logging.h" | |
| 9 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
| 10 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 11 #include "base/process/process.h" | 12 #include "base/process/process.h" |
| 12 #include "content/browser/browser_main_loop.h" | 13 #include "content/browser/browser_main_loop.h" |
| 14 #include "content/browser/loader/resource_dispatcher_host_impl.h" | |
| 13 #include "content/browser/media/audio_stream_monitor.h" | 15 #include "content/browser/media/audio_stream_monitor.h" |
| 14 #include "content/browser/media/capture/audio_mirroring_manager.h" | 16 #include "content/browser/media/capture/audio_mirroring_manager.h" |
| 15 #include "content/browser/media/media_internals.h" | 17 #include "content/browser/media/media_internals.h" |
| 16 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 18 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| 17 #include "content/browser/renderer_host/media/audio_sync_reader.h" | 19 #include "content/browser/renderer_host/media/audio_sync_reader.h" |
| 18 #include "content/browser/renderer_host/media/media_stream_manager.h" | 20 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 19 #include "content/common/media/audio_messages.h" | 21 #include "content/common/media/audio_messages.h" |
| 20 #include "content/public/browser/content_browser_client.h" | 22 #include "content/public/browser/content_browser_client.h" |
| 21 #include "content/public/browser/media_observer.h" | 23 #include "content/public/browser/media_observer.h" |
| 22 #include "content/public/common/content_switches.h" | 24 #include "content/public/common/content_switches.h" |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 250 | 252 |
| 251 Send(new AudioMsg_NotifyStreamCreated( | 253 Send(new AudioMsg_NotifyStreamCreated( |
| 252 entry->stream_id(), foreign_memory_handle, socket_descriptor, | 254 entry->stream_id(), foreign_memory_handle, socket_descriptor, |
| 253 entry->shared_memory()->requested_size())); | 255 entry->shared_memory()->requested_size())); |
| 254 } | 256 } |
| 255 | 257 |
| 256 void AudioRendererHost::DoNotifyStreamStateChanged(int stream_id, | 258 void AudioRendererHost::DoNotifyStreamStateChanged(int stream_id, |
| 257 bool is_playing) { | 259 bool is_playing) { |
| 258 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 260 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 259 | 261 |
| 260 AudioEntry* const entry = LookupById(stream_id); | 262 AudioEntry* const entry(LookupById(stream_id)); |
| 261 if (!entry) | 263 if (!entry) |
| 262 return; | 264 return; |
| 263 | 265 |
| 264 Send(new AudioMsg_NotifyStreamStateChanged( | 266 Send(new AudioMsg_NotifyStreamStateChanged( |
| 265 stream_id, | 267 stream_id, |
| 266 is_playing ? media::AudioOutputIPCDelegate::kPlaying | 268 is_playing ? media::AudioOutputIPCDelegate::kPlaying |
| 267 : media::AudioOutputIPCDelegate::kPaused)); | 269 : media::AudioOutputIPCDelegate::kPaused)); |
| 268 | 270 |
| 269 if (is_playing) { | 271 if (is_playing) { |
| 270 AudioStreamMonitor::StartMonitoringStream( | 272 AudioStreamMonitor::StartMonitoringStream( |
| 271 render_process_id_, | 273 render_process_id_, |
| 272 entry->render_frame_id(), | 274 entry->render_frame_id(), |
| 273 entry->stream_id(), | 275 entry->stream_id(), |
| 274 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, | 276 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, |
| 275 entry->controller())); | 277 entry->controller())); |
| 276 // TODO(dalecurtis): See about using AudioStreamMonitor instead. | |
| 277 if (!entry->playing()) { | |
| 278 entry->set_playing(true); | |
| 279 base::AtomicRefCountInc(&num_playing_streams_); | |
| 280 } | |
| 281 } else { | 278 } else { |
| 282 AudioStreamMonitor::StopMonitoringStream( | 279 AudioStreamMonitor::StopMonitoringStream( |
| 283 render_process_id_, entry->render_frame_id(), entry->stream_id()); | 280 render_process_id_, entry->render_frame_id(), entry->stream_id()); |
| 284 // TODO(dalecurtis): See about using AudioStreamMonitor instead. | |
| 285 if (entry->playing()) { | |
| 286 entry->set_playing(false); | |
| 287 base::AtomicRefCountDec(&num_playing_streams_); | |
| 288 } | |
| 289 } | 281 } |
| 282 UpdateNumPlayingStreams(entry, is_playing); | |
| 290 } | 283 } |
| 291 | 284 |
| 292 RenderViewHost::AudioOutputControllerList | 285 RenderViewHost::AudioOutputControllerList |
| 293 AudioRendererHost::DoGetOutputControllers(int render_view_id) const { | 286 AudioRendererHost::DoGetOutputControllers(int render_view_id) const { |
| 294 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 287 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 295 | 288 |
| 296 RenderViewHost::AudioOutputControllerList controllers; | 289 RenderViewHost::AudioOutputControllerList controllers; |
| 297 AudioEntryMap::const_iterator it = audio_entries_.begin(); | 290 for (AudioEntryMap::const_iterator it = audio_entries_.begin(); |
| 298 for (; it != audio_entries_.end(); ++it) { | 291 it != audio_entries_.end(); |
| 292 ++it) { | |
| 299 AudioEntry* entry = it->second; | 293 AudioEntry* entry = it->second; |
| 300 if (entry->render_view_id() == render_view_id) | 294 if (entry->render_view_id() == render_view_id) |
| 301 controllers.push_back(entry->controller()); | 295 controllers.push_back(entry->controller()); |
| 302 } | 296 } |
| 303 | 297 |
| 304 return controllers; | 298 return controllers; |
| 305 } | 299 } |
| 306 | 300 |
| 307 /////////////////////////////////////////////////////////////////////////////// | 301 /////////////////////////////////////////////////////////////////////////////// |
| 308 // IPC Messages handler | 302 // IPC Messages handler |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 447 mirroring_manager_->RemoveDiverter(controller); | 441 mirroring_manager_->RemoveDiverter(controller); |
| 448 controller->Close( | 442 controller->Close( |
| 449 base::Bind(&AudioRendererHost::DeleteEntry, this, base::Passed(&entry))); | 443 base::Bind(&AudioRendererHost::DeleteEntry, this, base::Passed(&entry))); |
| 450 audio_log_->OnClosed(stream_id); | 444 audio_log_->OnClosed(stream_id); |
| 451 } | 445 } |
| 452 | 446 |
| 453 void AudioRendererHost::DeleteEntry(scoped_ptr<AudioEntry> entry) { | 447 void AudioRendererHost::DeleteEntry(scoped_ptr<AudioEntry> entry) { |
| 454 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 448 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 455 AudioStreamMonitor::StopMonitoringStream( | 449 AudioStreamMonitor::StopMonitoringStream( |
| 456 render_process_id_, entry->render_frame_id(), entry->stream_id()); | 450 render_process_id_, entry->render_frame_id(), entry->stream_id()); |
| 457 if (entry->playing()) | 451 UpdateNumPlayingStreams(entry.get(), false); |
|
aiolos (Not reviewing)
2014/09/19 17:33:56
It looks like entry.get() is sometimes failing her
DaleCurtis
2014/09/19 17:40:40
That doesn't make sense. It shouldn't go out of sc
aiolos (Not reviewing)
2014/09/19 18:59:18
It didn't make sense to me either, but this was th
DaleCurtis
2014/09/19 19:13:42
Wait, this crash is in content::ResourceDispatcher
| |
| 458 base::AtomicRefCountDec(&num_playing_streams_); | |
| 459 } | 452 } |
| 460 | 453 |
| 461 void AudioRendererHost::ReportErrorAndClose(int stream_id) { | 454 void AudioRendererHost::ReportErrorAndClose(int stream_id) { |
| 462 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 455 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 463 | 456 |
| 464 // Make sure this isn't a stray callback executing after the stream has been | 457 // 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 | 458 // closed, so error notifications aren't sent after clients believe the stream |
| 466 // is closed. | 459 // is closed. |
| 467 if (!LookupById(stream_id)) | 460 if (!LookupById(stream_id)) |
| 468 return; | 461 return; |
| 469 | 462 |
| 470 SendErrorMessage(stream_id); | 463 SendErrorMessage(stream_id); |
| 471 | 464 |
| 472 audio_log_->OnError(stream_id); | 465 audio_log_->OnError(stream_id); |
| 473 OnCloseStream(stream_id); | 466 OnCloseStream(stream_id); |
| 474 } | 467 } |
| 475 | 468 |
| 476 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { | 469 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { |
| 477 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 470 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 478 | 471 |
| 479 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id); | 472 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id); |
| 480 return i != audio_entries_.end() ? i->second : NULL; | 473 return i != audio_entries_.end() ? i->second : NULL; |
| 481 } | 474 } |
| 482 | 475 |
| 476 void AudioRendererHost::UpdateNumPlayingStreams(AudioEntry* entry, | |
| 477 bool is_playing) { | |
| 478 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 479 if (entry->playing() == is_playing) | |
| 480 return; | |
| 481 entry->set_playing(is_playing); | |
| 482 if (is_playing) { | |
|
DaleCurtis
2014/09/19 17:40:40
Remove {} from single line if.
aiolos (Not reviewing)
2014/09/19 18:59:18
Even though it's part of the if/else? You and Matt
mmenke
2014/09/19 20:32:40
I'm certainly not going to fight over this, but I'
DaleCurtis
2014/09/19 20:39:41
I didn't see Matt's previous disagreement. I don't
| |
| 483 base::AtomicRefCountInc(&num_playing_streams_); | |
| 484 } else { | |
| 485 base::AtomicRefCountDec(&num_playing_streams_); | |
| 486 } | |
| 487 if (is_playing || !RenderViewHasActiveAudio(entry->render_view_id())) | |
|
DaleCurtis
2014/09/19 17:40:40
Multiline if needs {}
aiolos (Not reviewing)
2014/09/19 18:59:18
Done.
| |
| 488 ResourceDispatcherHostImpl::Get()->OnAudioRenderHostStreamStateChanged( | |
|
DaleCurtis
2014/09/19 17:40:40
I take it your upstream code will just ignore repe
aiolos (Not reviewing)
2014/09/19 18:59:18
Close. It will quickly recompute whether the tab s
| |
| 489 render_process_id_, entry->render_view_id(), is_playing); | |
| 490 } | |
| 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 |
| 496 bool AudioRendererHost::RenderViewHasActiveAudio(int render_view_id) { | |
| 497 for (AudioEntryMap::const_iterator it = audio_entries_.begin(); | |
| 498 it != audio_entries_.end(); | |
| 499 ++it) { | |
| 500 AudioEntry* entry = it->second; | |
| 501 if (entry->render_view_id() == render_view_id && entry->playing()) | |
| 502 return true; | |
| 503 } | |
| 504 return false; | |
| 505 } | |
| 506 | |
| 487 } // namespace content | 507 } // namespace content |
| OLD | NEW |