Chromium Code Reviews| Index: content/browser/renderer_host/media/audio_stream_registry_impl.cc |
| diff --git a/content/browser/renderer_host/media/audio_stream_registry_impl.cc b/content/browser/renderer_host/media/audio_stream_registry_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..89ac580a85e99c822b617c37ea6047a0a0ecee4b |
| --- /dev/null |
| +++ b/content/browser/renderer_host/media/audio_stream_registry_impl.cc |
| @@ -0,0 +1,104 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
|
DaleCurtis
2017/01/12 19:22:12
I think instead all of this can be deleted. AudioS
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/renderer_host/media/audio_stream_registry_impl.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/lazy_instance.h" |
| +#include "base/metrics/histogram_macros.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "media/audio/audio_streams_tracker.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +base::LazyInstance<media::AudioStreamsTracker> g_audio_output_streams_tracker = |
| + LAZY_INSTANCE_INITIALIZER; |
| + |
| +void NotifyRenderProcessHostThatAudioStateChanged(int render_process_id) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + |
| + RenderProcessHost* render_process_host = |
| + RenderProcessHost::FromID(render_process_id); |
| + |
| + if (render_process_host) |
| + render_process_host->AudioStateChanged(); |
| +} |
| + |
| +} // namespace |
| + |
| +AudioStreamRegistryImpl::AudioStreamRegistryImpl(int render_process_id) |
| + : render_process_id_(render_process_id) {} |
| + |
| +AudioStreamRegistryImpl::~AudioStreamRegistryImpl() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + DCHECK_EQ(num_output_streams_, 0); |
| + DCHECK_EQ(num_playing_output_streams_, 0); |
| + |
| + if (max_simultaneous_output_streams_ > 0) { |
| + UMA_HISTOGRAM_CUSTOM_COUNTS("Media.AudioRendererIpcStreams", |
| + max_simultaneous_output_streams_, 1, 50, 51); |
| + UMA_HISTOGRAM_CUSTOM_COUNTS( |
| + "Media.AudioRendererIpcStreamsTotal", |
| + g_audio_output_streams_tracker.Get().max_stream_count(), 1, 100, 101); |
| + g_audio_output_streams_tracker.Get().ResetMaxStreamCount(); |
| + } |
| +} |
| + |
| +// static |
| +AudioStreamRegistryImpl::UniquePtr AudioStreamRegistryImpl::Create( |
| + int render_process_id) { |
| + return {new AudioStreamRegistryImpl(render_process_id), {}}; |
| +} |
| + |
| +bool AudioStreamRegistryImpl::HasActiveAudio() { |
| + return !base::AtomicRefCountIsZero(&num_playing_output_streams_); |
| +} |
| + |
| +void AudioStreamRegistryImpl::RegisterStream() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + ++num_output_streams_; |
| + max_simultaneous_output_streams_ = |
| + std::max(max_simultaneous_output_streams_, num_output_streams_); |
| + g_audio_output_streams_tracker.Get().IncreaseStreamCount(); |
| +} |
| + |
| +void AudioStreamRegistryImpl::DeregisterStream() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + DCHECK_GT(num_output_streams_, 0); |
| + --num_output_streams_; |
| + g_audio_output_streams_tracker.Get().DecreaseStreamCount(); |
| +} |
| + |
| +void AudioStreamRegistryImpl::StreamStateChanged(bool playing) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + if (playing) { |
| + base::AtomicRefCountInc(&num_playing_output_streams_); |
| + |
| + // Inform the RenderProcessHost when audio starts playing for the first |
| + // time. The nonatomic increment-and-read is ok since this is the only |
| + // thread that |num_playing_output_streams_| may be updated on. |
| + if (base::AtomicRefCountIsOne(&num_playing_output_streams_)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&NotifyRenderProcessHostThatAudioStateChanged, |
| + render_process_id_)); |
| + } |
| + } else { |
| + // Inform the RenderProcessHost when there is no more audio playing. |
| + DCHECK(!base::AtomicRefCountIsZero(&num_playing_output_streams_)); |
| + if (!base::AtomicRefCountDec(&num_playing_output_streams_)) { |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&NotifyRenderProcessHostThatAudioStateChanged, |
| + render_process_id_)); |
| + } |
| + } |
| + DCHECK_LE(num_playing_output_streams_, num_output_streams_); |
| +} |
| + |
| +} // namespace content |