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..96987ac728eecee40078a90b3eb4c2e3c307d7e0 |
| --- /dev/null |
| +++ b/content/browser/renderer_host/media/audio_stream_registry_impl.cc |
| @@ -0,0 +1,155 @@ |
| +// 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) { |
| + if (!debug_recording_path_.empty()) { |
| + // There is already a recording in progress. |
| + DisableDebugRecording(); |
| + } |
| + debug_recording_path_ = base_file_name; |
|
o1ka
2016/12/22 10:46:52
Thread safety?
Max Morin
2017/01/09 15:34:23
Done.
|
| + for (Stream* stream : output_streams_) |
|
o1ka
2016/12/22 10:46:52
Thread safety?
Max Morin
2017/01/09 15:34:23
Done.
|
| + stream->EnableDebugRecording(base_file_name); |
| +} |
| + |
| +void AudioStreamRegistryImpl::DisableDebugRecording() { |
| + 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) { |
| +#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 |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
|
o1ka
2016/12/22 10:46:52
Thread check first, then access stream_currently_p
Max Morin
2017/01/09 15:34:23
Done.
|
| + 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 |