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

Side by Side Diff: content/browser/renderer_host/media/audio_stream_registry_impl.cc

Issue 2578983003: Add AudioStreamRegistry. Move stream counting logic (Closed)
Patch Set: Add missing EXPECT_TRUE. Created 4 years 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 unified diff | Download patch
OLDNEW
(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 #include <utility>
9
10 #include "base/lazy_instance.h"
11 #include "base/metrics/histogram_macros.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "media/audio/audio_streams_tracker.h"
15
16 namespace content {
17
18 namespace {
19
20 base::LazyInstance<media::AudioStreamsTracker> g_audio_output_streams_tracker =
21 LAZY_INSTANCE_INITIALIZER;
22
23 void NotifyRenderProcessHostThatAudioStateChanged(int render_process_id) {
24 DCHECK_CURRENTLY_ON(BrowserThread::UI);
25
26 RenderProcessHost* render_process_host =
27 RenderProcessHost::FromID(render_process_id);
28
29 if (render_process_host)
30 render_process_host->AudioStateChanged();
31 }
32
33 } // namespace
34
35 AudioStreamRegistryImpl::AudioStreamRegistryImpl(int render_process_id)
36 : render_process_id_(render_process_id) {}
37
38 AudioStreamRegistryImpl::~AudioStreamRegistryImpl() {
39 DCHECK_CURRENTLY_ON(BrowserThread::IO);
40 DCHECK(output_streams_.empty());
41 DCHECK(stream_currently_playing_.empty());
42
43 if (max_simultaneous_output_streams_ > 0) {
44 UMA_HISTOGRAM_CUSTOM_COUNTS("Media.AudioRendererIpcStreams",
45 max_simultaneous_output_streams_, 1, 50, 51);
46 UMA_HISTOGRAM_CUSTOM_COUNTS(
47 "Media.AudioRendererIpcStreamsTotal",
48 g_audio_output_streams_tracker.Get().max_stream_count(), 1, 100, 101);
49 g_audio_output_streams_tracker.Get().ResetMaxStreamCount();
50 }
51 }
52
53 // static
54 AudioStreamRegistryImpl::UniquePtr AudioStreamRegistryImpl::Create(
55 int render_process_id) {
56 return {new AudioStreamRegistryImpl(render_process_id), {}};
57 }
58
59 bool AudioStreamRegistryImpl::HasActiveAudio() {
60 return !base::AtomicRefCountIsZero(&num_playing_output_streams_);
61 }
62
63 #if BUILDFLAG(ENABLE_WEBRTC)
64 void AudioStreamRegistryImpl::EnableDebugRecording(
65 const base::FilePath& base_file_name) {
66 if (!debug_recording_path_.empty()) {
67 // There is already a recording in progress.
68 DisableDebugRecording();
69 }
70 debug_recording_path_ = base_file_name;
o1ka 2016/12/22 10:46:52 Thread safety?
Max Morin 2017/01/09 15:34:23 Done.
71 for (Stream* stream : output_streams_)
o1ka 2016/12/22 10:46:52 Thread safety?
Max Morin 2017/01/09 15:34:23 Done.
72 stream->EnableDebugRecording(base_file_name);
73 }
74
75 void AudioStreamRegistryImpl::DisableDebugRecording() {
76 debug_recording_path_.clear();
77 for (Stream* stream : output_streams_)
78 stream->DisableDebugRecording();
79 }
80 #endif // BUILDFLAG(ENABLE_WEBRTC)
81
82 void AudioStreamRegistryImpl::RegisterOutputStream(Stream* stream) {
83 DCHECK_CURRENTLY_ON(BrowserThread::IO);
84 bool did_insert = output_streams_.insert(stream).second;
85 DCHECK(did_insert);
86
87 #if BUILDFLAG(ENABLE_WEBRTC)
88 if (!debug_recording_path_.empty())
89 stream->EnableDebugRecording(debug_recording_path_);
90 #endif // BUILDFLAG(ENABLE_WEBRTC)
91
92 max_simultaneous_output_streams_ =
93 std::max(max_simultaneous_output_streams_, output_streams_.size());
94 g_audio_output_streams_tracker.Get().IncreaseStreamCount();
95 #if DCHECK_IS_ON()
96 stream_currently_playing_.insert(std::make_pair(stream, false));
97 #endif
98 }
99
100 void AudioStreamRegistryImpl::DeregisterOutputStream(Stream* stream) {
101 DCHECK_CURRENTLY_ON(BrowserThread::IO);
102 size_t num_erased = output_streams_.erase(stream);
103 DCHECK_EQ(num_erased, 1u);
104
105 g_audio_output_streams_tracker.Get().DecreaseStreamCount();
106 #if BUILDFLAG(ENABLE_WEBRTC)
107 if (!debug_recording_path_.empty())
108 stream->DisableDebugRecording();
109 #endif // BUILDFLAG(ENABLE_WEBRTC)
110
111 #if DCHECK_IS_ON()
112 DCHECK(!stream_currently_playing_.at(stream));
113 stream_currently_playing_.erase(stream);
114 #endif
115 }
116
117 void AudioStreamRegistryImpl::OutputStreamStateChanged(Stream* stream,
118 bool playing) {
119 #if DCHECK_IS_ON()
120 auto it = stream_currently_playing_.find(stream);
121 DCHECK(it != stream_currently_playing_.end());
122 DCHECK(it->second != playing);
123 it->second = playing;
124 #endif
125 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.
126 if (playing) {
127 base::AtomicRefCountInc(&num_playing_output_streams_);
128
129 // Inform the RenderProcessHost when audio starts playing for the first
130 // time. The nonatomic increment-and-read is ok since this is the only
131 // thread that |num_playing_output_streams_| may be updated on.
132 if (base::AtomicRefCountIsOne(&num_playing_output_streams_)) {
133 BrowserThread::PostTask(
134 BrowserThread::UI, FROM_HERE,
135 base::Bind(&NotifyRenderProcessHostThatAudioStateChanged,
136 render_process_id_));
137 }
138 } else {
139 // Inform the RenderProcessHost when there is no more audio playing.
140 DCHECK(!base::AtomicRefCountIsZero(&num_playing_output_streams_));
141 if (!base::AtomicRefCountDec(&num_playing_output_streams_)) {
142 BrowserThread::PostTask(
143 BrowserThread::UI, FROM_HERE,
144 base::Bind(&NotifyRenderProcessHostThatAudioStateChanged,
145 render_process_id_));
146 }
147 }
148 }
149
150 // static
151 void AudioStreamRegistryImpl::DetachAudioStreamsTrackerFromThreadForTesting() {
152 g_audio_output_streams_tracker.Get().DetachFromThreadForTesting();
153 }
154
155 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698