Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // 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
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/renderer_host/media/audio_stream_registry_impl.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/lazy_instance.h" | |
| 10 #include "base/metrics/histogram_macros.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 #include "content/public/browser/render_process_host.h" | |
| 13 #include "media/audio/audio_streams_tracker.h" | |
| 14 | |
| 15 namespace content { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 base::LazyInstance<media::AudioStreamsTracker> g_audio_output_streams_tracker = | |
| 20 LAZY_INSTANCE_INITIALIZER; | |
| 21 | |
| 22 void NotifyRenderProcessHostThatAudioStateChanged(int render_process_id) { | |
| 23 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 24 | |
| 25 RenderProcessHost* render_process_host = | |
| 26 RenderProcessHost::FromID(render_process_id); | |
| 27 | |
| 28 if (render_process_host) | |
| 29 render_process_host->AudioStateChanged(); | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 AudioStreamRegistryImpl::AudioStreamRegistryImpl(int render_process_id) | |
| 35 : render_process_id_(render_process_id) {} | |
| 36 | |
| 37 AudioStreamRegistryImpl::~AudioStreamRegistryImpl() { | |
| 38 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 39 DCHECK_EQ(num_output_streams_, 0); | |
| 40 DCHECK_EQ(num_playing_output_streams_, 0); | |
| 41 | |
| 42 if (max_simultaneous_output_streams_ > 0) { | |
| 43 UMA_HISTOGRAM_CUSTOM_COUNTS("Media.AudioRendererIpcStreams", | |
| 44 max_simultaneous_output_streams_, 1, 50, 51); | |
| 45 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
| 46 "Media.AudioRendererIpcStreamsTotal", | |
| 47 g_audio_output_streams_tracker.Get().max_stream_count(), 1, 100, 101); | |
| 48 g_audio_output_streams_tracker.Get().ResetMaxStreamCount(); | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 // static | |
| 53 AudioStreamRegistryImpl::UniquePtr AudioStreamRegistryImpl::Create( | |
| 54 int render_process_id) { | |
| 55 return {new AudioStreamRegistryImpl(render_process_id), {}}; | |
| 56 } | |
| 57 | |
| 58 bool AudioStreamRegistryImpl::HasActiveAudio() { | |
| 59 return !base::AtomicRefCountIsZero(&num_playing_output_streams_); | |
| 60 } | |
| 61 | |
| 62 void AudioStreamRegistryImpl::RegisterStream() { | |
| 63 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 64 ++num_output_streams_; | |
| 65 max_simultaneous_output_streams_ = | |
| 66 std::max(max_simultaneous_output_streams_, num_output_streams_); | |
| 67 g_audio_output_streams_tracker.Get().IncreaseStreamCount(); | |
| 68 } | |
| 69 | |
| 70 void AudioStreamRegistryImpl::DeregisterStream() { | |
| 71 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 72 DCHECK_GT(num_output_streams_, 0); | |
| 73 --num_output_streams_; | |
| 74 g_audio_output_streams_tracker.Get().DecreaseStreamCount(); | |
| 75 } | |
| 76 | |
| 77 void AudioStreamRegistryImpl::StreamStateChanged(bool playing) { | |
| 78 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 79 if (playing) { | |
| 80 base::AtomicRefCountInc(&num_playing_output_streams_); | |
| 81 | |
| 82 // Inform the RenderProcessHost when audio starts playing for the first | |
| 83 // time. The nonatomic increment-and-read is ok since this is the only | |
| 84 // thread that |num_playing_output_streams_| may be updated on. | |
| 85 if (base::AtomicRefCountIsOne(&num_playing_output_streams_)) { | |
| 86 BrowserThread::PostTask( | |
| 87 BrowserThread::UI, FROM_HERE, | |
| 88 base::Bind(&NotifyRenderProcessHostThatAudioStateChanged, | |
| 89 render_process_id_)); | |
| 90 } | |
| 91 } else { | |
| 92 // Inform the RenderProcessHost when there is no more audio playing. | |
| 93 DCHECK(!base::AtomicRefCountIsZero(&num_playing_output_streams_)); | |
| 94 if (!base::AtomicRefCountDec(&num_playing_output_streams_)) { | |
| 95 BrowserThread::PostTask( | |
| 96 BrowserThread::UI, FROM_HERE, | |
| 97 base::Bind(&NotifyRenderProcessHostThatAudioStateChanged, | |
| 98 render_process_id_)); | |
| 99 } | |
| 100 } | |
| 101 DCHECK_LE(num_playing_output_streams_, num_output_streams_); | |
| 102 } | |
| 103 | |
| 104 } // namespace content | |
| OLD | NEW |