-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_renderer_sink_cache_impl.cc
277 lines (232 loc) · 9.33 KB
/
audio_renderer_sink_cache_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// 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 <algorithm>
#include "base/bind.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/synchronization/lock.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chromium_media_lib/audio_device_factory.h"
#include "chromium_media_lib/audio_renderer_sink_cache_impl.h"
#include "media/audio/audio_device_description.h"
#include "media/base/audio_renderer_sink.h"
#include "url/origin.h"
namespace media {
constexpr int kDeleteTimeoutMs = 5000;
namespace {
enum GetOutputDeviceInfoCacheUtilization {
// No cached sink found.
SINK_CACHE_MISS_NO_SINK = 0,
// If session id is used to specify a device, we always have to create and
// cache a new sink.
SINK_CACHE_MISS_CANNOT_LOOKUP_BY_SESSION_ID = 1,
// Output parmeters for an already-cached sink are requested.
SINK_CACHE_HIT = 2,
// For UMA.
SINK_CACHE_LAST_ENTRY
};
bool SinkIsHealthy(media::AudioRendererSink* sink) {
return sink->GetOutputDeviceInfo().device_status() ==
media::OUTPUT_DEVICE_STATUS_OK;
}
} // namespace
// Cached sink data.
struct AudioRendererSinkCacheImpl::CacheEntry {
int source_render_frame_id;
std::string device_id;
url::Origin security_origin;
scoped_refptr<media::AudioRendererSink> sink; // Sink instance
bool used; // True if in use by a client.
};
// static
std::unique_ptr<AudioRendererSinkCache> AudioRendererSinkCache::Create() {
return base::WrapUnique(new AudioRendererSinkCacheImpl(
base::ThreadTaskRunnerHandle::Get(),
base::Bind(&AudioDeviceFactory::NewAudioRendererMixerSink),
base::TimeDelta::FromMilliseconds(kDeleteTimeoutMs)));
}
AudioRendererSinkCacheImpl::AudioRendererSinkCacheImpl(
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
const CreateSinkCallback& create_sink_cb,
base::TimeDelta delete_timeout)
: task_runner_(std::move(task_runner)),
create_sink_cb_(create_sink_cb),
delete_timeout_(delete_timeout),
weak_ptr_factory_(this) {
weak_this_ = weak_ptr_factory_.GetWeakPtr();
}
AudioRendererSinkCacheImpl::~AudioRendererSinkCacheImpl() {
DCHECK(task_runner_->BelongsToCurrentThread());
// We just release all the cached sinks here. Stop them first.
// We can stop all the sinks, no matter they are used or not, since everything
// is being destroyed anyways.
for (auto& entry : cache_)
entry.sink->Stop();
}
media::OutputDeviceInfo AudioRendererSinkCacheImpl::GetSinkInfo(
int source_render_frame_id,
int session_id,
const std::string& device_id,
const url::Origin& security_origin) {
if (media::AudioDeviceDescription::UseSessionIdToSelectDevice(session_id,
device_id)) {
// We are provided with session id instead of device id. Session id is
// unique, so we can't find any matching sink. Creating a new one.
scoped_refptr<media::AudioRendererSink> sink = create_sink_cb_.Run(
source_render_frame_id, session_id, device_id, security_origin);
CacheUnusedSinkIfHealthy(source_render_frame_id,
sink->GetOutputDeviceInfo().device_id(),
security_origin, sink);
UMA_HISTOGRAM_ENUMERATION(
"Media.Audio.Render.SinkCache.GetOutputDeviceInfoCacheUtilization",
SINK_CACHE_MISS_CANNOT_LOOKUP_BY_SESSION_ID, SINK_CACHE_LAST_ENTRY);
return sink->GetOutputDeviceInfo();
}
// Ignore session id.
{
base::AutoLock auto_lock(cache_lock_);
auto cache_iter =
FindCacheEntry_Locked(source_render_frame_id, device_id,
security_origin, false /* unused_only */);
if (cache_iter != cache_.end()) {
// A matching cached sink is found.
UMA_HISTOGRAM_ENUMERATION(
"Media.Audio.Render.SinkCache.GetOutputDeviceInfoCacheUtilization",
SINK_CACHE_HIT, SINK_CACHE_LAST_ENTRY);
return cache_iter->sink->GetOutputDeviceInfo();
}
}
// No matching sink found, create a new one.
scoped_refptr<media::AudioRendererSink> sink = create_sink_cb_.Run(
source_render_frame_id, 0 /* session_id */, device_id, security_origin);
CacheUnusedSinkIfHealthy(source_render_frame_id, device_id, security_origin,
sink);
UMA_HISTOGRAM_ENUMERATION(
"Media.Audio.Render.SinkCache.GetOutputDeviceInfoCacheUtilization",
SINK_CACHE_MISS_NO_SINK, SINK_CACHE_LAST_ENTRY);
//|sink| is ref-counted, so it's ok if it is removed from cache before we get
// here.
return sink->GetOutputDeviceInfo();
}
scoped_refptr<media::AudioRendererSink> AudioRendererSinkCacheImpl::GetSink(
int source_render_frame_id,
const std::string& device_id,
const url::Origin& security_origin) {
UMA_HISTOGRAM_BOOLEAN("Media.Audio.Render.SinkCache.UsedForSinkCreation",
true);
base::AutoLock auto_lock(cache_lock_);
auto cache_iter =
FindCacheEntry_Locked(source_render_frame_id, device_id, security_origin,
true /* unused sink only */);
if (cache_iter != cache_.end()) {
// Found unused sink; mark it as used and return.
cache_iter->used = true;
UMA_HISTOGRAM_BOOLEAN(
"Media.Audio.Render.SinkCache.InfoSinkReusedForOutput", true);
return cache_iter->sink;
}
// No unused sink is found, create one, mark it used, cache it and return.
CacheEntry cache_entry = {
source_render_frame_id, device_id, security_origin,
create_sink_cb_.Run(source_render_frame_id, 0 /* session_id */, device_id,
security_origin),
true /* used */};
if (SinkIsHealthy(cache_entry.sink.get()))
cache_.push_back(cache_entry);
return cache_entry.sink;
}
void AudioRendererSinkCacheImpl::ReleaseSink(
const media::AudioRendererSink* sink_ptr) {
// We don't know the sink state, so won't reuse it. Delete it immediately.
DeleteSink(sink_ptr, true);
}
void AudioRendererSinkCacheImpl::DeleteLaterIfUnused(
const media::AudioRendererSink* sink_ptr) {
task_runner_->PostDelayedTask(
FROM_HERE,
base::Bind(&AudioRendererSinkCacheImpl::DeleteSink, weak_this_,
base::RetainedRef(sink_ptr),
false /*do not delete if used*/),
delete_timeout_);
}
void AudioRendererSinkCacheImpl::DeleteSink(
const media::AudioRendererSink* sink_ptr,
bool force_delete_used) {
DCHECK(sink_ptr);
scoped_refptr<media::AudioRendererSink> sink_to_stop;
{
base::AutoLock auto_lock(cache_lock_);
// Looking up the sink by its pointer.
auto cache_iter = std::find_if(cache_.begin(), cache_.end(),
[sink_ptr](const CacheEntry& val) {
return val.sink.get() == sink_ptr;
});
if (cache_iter == cache_.end())
return;
// When |force_delete_used| is set, it's expected that we are deleting a
// used sink.
DCHECK((!force_delete_used) || (force_delete_used && cache_iter->used))
<< "Attempt to delete a non-aquired sink.";
if (!force_delete_used && cache_iter->used)
return;
// To stop the sink before deletion if it's not used, we need to hold
// a ref to it.
if (!cache_iter->used) {
sink_to_stop = cache_iter->sink;
UMA_HISTOGRAM_BOOLEAN(
"Media.Audio.Render.SinkCache.InfoSinkReusedForOutput", false);
}
cache_.erase(cache_iter);
} // Lock scope;
// Stop the sink out of the lock scope.
if (sink_to_stop.get()) {
DCHECK_EQ(sink_ptr, sink_to_stop.get());
sink_to_stop->Stop();
}
}
AudioRendererSinkCacheImpl::CacheContainer::iterator
AudioRendererSinkCacheImpl::FindCacheEntry_Locked(
int source_render_frame_id,
const std::string& device_id,
const url::Origin& security_origin,
bool unused_only) {
return std::find_if(
cache_.begin(), cache_.end(),
[source_render_frame_id, &device_id, &security_origin,
unused_only](const CacheEntry& val) {
if (val.used && unused_only)
return false;
if (val.source_render_frame_id != source_render_frame_id)
return false;
if (media::AudioDeviceDescription::IsDefaultDevice(device_id) &&
media::AudioDeviceDescription::IsDefaultDevice(val.device_id)) {
// Both device IDs represent the same default device => do not compare
// them; the default device is always authorized => ignore security
// origin.
return true;
}
return val.device_id == device_id &&
val.security_origin == security_origin;
});
};
void AudioRendererSinkCacheImpl::CacheUnusedSinkIfHealthy(
int source_render_frame_id,
const std::string& device_id,
const url::Origin& security_origin,
scoped_refptr<media::AudioRendererSink> sink) {
if (!SinkIsHealthy(sink.get()))
return;
CacheEntry cache_entry = {source_render_frame_id, device_id, security_origin,
sink, false /* not used */};
{
base::AutoLock auto_lock(cache_lock_);
cache_.push_back(cache_entry);
}
DeleteLaterIfUnused(cache_entry.sink.get());
}
int AudioRendererSinkCacheImpl::GetCacheSizeForTesting() {
return cache_.size();
}
} // namespace media