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..97710e24d7eb538b34550f5b6ef02d2de3df0d23 |
| --- /dev/null |
| +++ b/content/browser/renderer_host/media/audio_stream_registry_impl.cc |
| @@ -0,0 +1,95 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// 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(output_streams_.empty()); |
| + |
| + 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(); |
| + } |
| +} |
| + |
| +bool AudioStreamRegistryImpl::HasActiveAudio() { |
| + return !base::AtomicRefCountIsZero(&num_playing_output_streams_); |
| +} |
| + |
| +void AudioStreamRegistryImpl::RegisterOutputStream(Stream* stream) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + output_streams_.insert(stream); |
|
o1ka
2016/12/16 09:48:16
DCheck that unique value is inserted?
Max Morin
2016/12/19 16:29:03
Done.
|
| + max_simultaneous_output_streams_ = |
| + std::max(max_simultaneous_output_streams_, output_streams_.size()); |
| + g_audio_output_streams_tracker.Get().IncreaseStreamCount(); |
| +} |
| + |
| +void AudioStreamRegistryImpl::DeregisterOutputStream(Stream* stream) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + size_t num_erased = output_streams_.erase(stream); |
| + DCHECK_EQ(num_erased, 1u); |
| + g_audio_output_streams_tracker.Get().DecreaseStreamCount(); |
| +} |
| + |
| +void AudioStreamRegistryImpl::OutputStreamStateChanged(bool playing) { |
|
o1ka
2016/12/16 09:48:16
Actually, to make it safer and more debuggable, we
Max Morin
2016/12/19 16:29:03
Done. (Debug only).
|
| + 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. |
| + if (!base::AtomicRefCountDec(&num_playing_output_streams_)) { |
|
o1ka
2016/12/16 09:48:16
Check that it's not below zero
Max Morin
2016/12/19 16:29:03
Done.
|
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&NotifyRenderProcessHostThatAudioStateChanged, |
| + render_process_id_)); |
| + } |
| + } |
| +} |
| + |
| +} // namespace content |