Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1559)

Unified Diff: content/browser/renderer_host/media/audio_stream_registry_impl.cc

Issue 2578983003: Add AudioStreamRegistry. Move stream counting logic (Closed)
Patch Set: Thread checks. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..f6e10c47158cb88a040b0a917bfa0b50201c4c1d
--- /dev/null
+++ b/content/browser/renderer_host/media/audio_stream_registry_impl.cc
@@ -0,0 +1,157 @@
+// 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 <utility>
+
+#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());
+ DCHECK(stream_currently_playing_.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();
+ }
+}
+
+// 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_);
+}
+
+#if BUILDFLAG(ENABLE_WEBRTC)
+void AudioStreamRegistryImpl::EnableDebugRecording(
+ const base::FilePath& base_file_name) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ if (!debug_recording_path_.empty()) {
+ // There is already a recording in progress.
+ DisableDebugRecording();
+ }
+ debug_recording_path_ = base_file_name;
+ for (Stream* stream : output_streams_)
+ stream->EnableDebugRecording(base_file_name);
+}
+
+void AudioStreamRegistryImpl::DisableDebugRecording() {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ debug_recording_path_.clear();
+ for (Stream* stream : output_streams_)
+ stream->DisableDebugRecording();
+}
+#endif // BUILDFLAG(ENABLE_WEBRTC)
+
+void AudioStreamRegistryImpl::RegisterOutputStream(Stream* stream) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ bool did_insert = output_streams_.insert(stream).second;
+ DCHECK(did_insert);
+
+#if BUILDFLAG(ENABLE_WEBRTC)
+ if (!debug_recording_path_.empty())
+ stream->EnableDebugRecording(debug_recording_path_);
+#endif // BUILDFLAG(ENABLE_WEBRTC)
+
+ max_simultaneous_output_streams_ =
+ std::max(max_simultaneous_output_streams_, output_streams_.size());
+ g_audio_output_streams_tracker.Get().IncreaseStreamCount();
+#if DCHECK_IS_ON()
+ stream_currently_playing_.insert(std::make_pair(stream, false));
+#endif
+}
+
+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();
+#if BUILDFLAG(ENABLE_WEBRTC)
+ if (!debug_recording_path_.empty())
+ stream->DisableDebugRecording();
+#endif // BUILDFLAG(ENABLE_WEBRTC)
+
+#if DCHECK_IS_ON()
+ DCHECK(!stream_currently_playing_.at(stream));
+ stream_currently_playing_.erase(stream);
+#endif
+}
+
+void AudioStreamRegistryImpl::OutputStreamStateChanged(Stream* stream,
+ bool playing) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+#if DCHECK_IS_ON()
+ auto it = stream_currently_playing_.find(stream);
+ DCHECK(it != stream_currently_playing_.end());
+ DCHECK(it->second != playing);
+ it->second = playing;
+#endif
+ 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_));
+ }
+ }
+}
+
+// static
+void AudioStreamRegistryImpl::DetachAudioStreamsTrackerFromThreadForTesting() {
+ g_audio_output_streams_tracker.Get().DetachFromThreadForTesting();
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698