OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
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(output_streams_.empty()); | |
40 | |
41 if (max_simultaneous_output_streams_ > 0) { | |
42 UMA_HISTOGRAM_CUSTOM_COUNTS("Media.AudioRendererIpcStreams", | |
43 max_simultaneous_output_streams_, 1, 50, 51); | |
44 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
45 "Media.AudioRendererIpcStreamsTotal", | |
46 g_audio_output_streams_tracker.Get().max_stream_count(), 1, 100, 101); | |
47 g_audio_output_streams_tracker.Get().ResetMaxStreamCount(); | |
48 } | |
49 } | |
50 | |
51 bool AudioStreamRegistryImpl::HasActiveAudio() { | |
52 return !base::AtomicRefCountIsZero(&num_playing_output_streams_); | |
53 } | |
54 | |
55 void AudioStreamRegistryImpl::RegisterOutputStream(Stream* stream) { | |
56 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
57 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.
| |
58 max_simultaneous_output_streams_ = | |
59 std::max(max_simultaneous_output_streams_, output_streams_.size()); | |
60 g_audio_output_streams_tracker.Get().IncreaseStreamCount(); | |
61 } | |
62 | |
63 void AudioStreamRegistryImpl::DeregisterOutputStream(Stream* stream) { | |
64 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
65 size_t num_erased = output_streams_.erase(stream); | |
66 DCHECK_EQ(num_erased, 1u); | |
67 g_audio_output_streams_tracker.Get().DecreaseStreamCount(); | |
68 } | |
69 | |
70 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).
| |
71 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
72 if (playing) { | |
73 base::AtomicRefCountInc(&num_playing_output_streams_); | |
74 | |
75 // Inform the RenderProcessHost when audio starts playing for the first | |
76 // time. The nonatomic increment-and-read is ok since this is the only | |
77 // thread that |num_playing_output_streams_| may be updated on. | |
78 if (base::AtomicRefCountIsOne(&num_playing_output_streams_)) { | |
79 BrowserThread::PostTask( | |
80 BrowserThread::UI, FROM_HERE, | |
81 base::Bind(&NotifyRenderProcessHostThatAudioStateChanged, | |
82 render_process_id_)); | |
83 } | |
84 } else { | |
85 // Inform the RenderProcessHost when there is no more audio playing. | |
86 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.
| |
87 BrowserThread::PostTask( | |
88 BrowserThread::UI, FROM_HERE, | |
89 base::Bind(&NotifyRenderProcessHostThatAudioStateChanged, | |
90 render_process_id_)); | |
91 } | |
92 } | |
93 } | |
94 | |
95 } // namespace content | |
OLD | NEW |