1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "APM::EffectDescriptor"
18 //#define LOG_NDEBUG 0
19
20 #include <android-base/stringprintf.h>
21
22 #include "AudioInputDescriptor.h"
23 #include "EffectDescriptor.h"
24 #include <system/audio_effects/audio_effects_utils.h>
25 #include <utils/String8.h>
26
27 #include <AudioPolicyInterface.h>
28 #include "AudioPolicyMix.h"
29 #include "HwModule.h"
30
31 namespace android {
32
dump(String8 * dst,int spaces) const33 void EffectDescriptor::dump(String8 *dst, int spaces) const
34 {
35 dst->appendFormat("Effect ID: %d; Attached to I/O handle: %d; Session: %d;\n",
36 mId, mIo, mSession);
37 dst->appendFormat("%*sMusic Effect? %s; \"%s\"; %s; %s\n", spaces, "",
38 isMusicEffect()? "yes" : "no", mDesc.name,
39 mEnabled ? "Enabled" : "Disabled", mSuspended ? "Suspended" : "Active");
40 }
41
EffectDescriptorCollection()42 EffectDescriptorCollection::EffectDescriptorCollection() :
43 mTotalEffectsCpuLoad(0),
44 mTotalEffectsMemory(0),
45 mTotalEffectsMemoryMaxUsed(0)
46 {
47
48 }
49
registerEffect(const effect_descriptor_t * desc,audio_io_handle_t io,int session,int id,bool isMusicEffect)50 status_t EffectDescriptorCollection::registerEffect(const effect_descriptor_t *desc,
51 audio_io_handle_t io,
52 int session,
53 int id, bool isMusicEffect)
54 {
55 if (getEffect(id) != nullptr) {
56 ALOGW("%s effect %s already registered", __FUNCTION__, desc->name);
57 return INVALID_OPERATION;
58 }
59
60 if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
61 ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
62 desc->name, desc->memoryUsage);
63 return INVALID_OPERATION;
64 }
65 mTotalEffectsMemory += desc->memoryUsage;
66 if (mTotalEffectsMemory > mTotalEffectsMemoryMaxUsed) {
67 mTotalEffectsMemoryMaxUsed = mTotalEffectsMemory;
68 }
69 ALOGV("registerEffect() effect %s, io %d, session %d id %d",
70 desc->name, io, session, id);
71 ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
72
73 sp<EffectDescriptor> effectDesc =
74 new EffectDescriptor(desc, isMusicEffect, id, io, (audio_session_t)session);
75 add(id, effectDesc);
76
77 return NO_ERROR;
78 }
79
getEffect(int id) const80 sp<EffectDescriptor> EffectDescriptorCollection::getEffect(int id) const
81 {
82 ssize_t index = indexOfKey(id);
83 if (index < 0) {
84 return nullptr;
85 }
86 return valueAt(index);
87 }
88
unregisterEffect(int id)89 status_t EffectDescriptorCollection::unregisterEffect(int id)
90 {
91 sp<EffectDescriptor> effectDesc = getEffect(id);
92 if (effectDesc == nullptr) {
93 ALOGW("%s unknown effect ID %d", __FUNCTION__, id);
94 return INVALID_OPERATION;
95 }
96
97 if (mTotalEffectsMemory < effectDesc->mDesc.memoryUsage) {
98 ALOGW("unregisterEffect() memory %d too big for total %d",
99 effectDesc->mDesc.memoryUsage, mTotalEffectsMemory);
100 effectDesc->mDesc.memoryUsage = mTotalEffectsMemory;
101 }
102 mTotalEffectsMemory -= effectDesc->mDesc.memoryUsage;
103 ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
104 effectDesc->mDesc.name, id, effectDesc->mDesc.memoryUsage, mTotalEffectsMemory);
105
106 removeItem(id);
107
108 return NO_ERROR;
109 }
110
setEffectEnabled(int id,bool enabled)111 status_t EffectDescriptorCollection::setEffectEnabled(int id, bool enabled)
112 {
113 ssize_t index = indexOfKey(id);
114 if (index < 0) {
115 ALOGW("unregisterEffect() unknown effect ID %d", id);
116 return INVALID_OPERATION;
117 }
118
119 return setEffectEnabled(valueAt(index), enabled);
120 }
121
isEffectEnabled(int id) const122 bool EffectDescriptorCollection::isEffectEnabled(int id) const
123 {
124 ssize_t index = indexOfKey(id);
125 if (index < 0) {
126 return false;
127 }
128 return valueAt(index)->mEnabled;
129 }
130
setEffectEnabled(const sp<EffectDescriptor> & effectDesc,bool enabled)131 status_t EffectDescriptorCollection::setEffectEnabled(const sp<EffectDescriptor> &effectDesc,
132 bool enabled)
133 {
134 if (enabled == effectDesc->mEnabled) {
135 ALOGV("setEffectEnabled(%s) effect already %s",
136 enabled?"true":"false", enabled?"enabled":"disabled");
137 return INVALID_OPERATION;
138 }
139
140 if (enabled) {
141 if (mTotalEffectsCpuLoad + effectDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
142 ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
143 effectDesc->mDesc.name, (float)effectDesc->mDesc.cpuLoad/10);
144 return INVALID_OPERATION;
145 }
146 mTotalEffectsCpuLoad += effectDesc->mDesc.cpuLoad;
147 ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
148 } else {
149 if (mTotalEffectsCpuLoad < effectDesc->mDesc.cpuLoad) {
150 ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
151 effectDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
152 effectDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
153 }
154 mTotalEffectsCpuLoad -= effectDesc->mDesc.cpuLoad;
155 ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
156 }
157 effectDesc->mEnabled = enabled;
158 return NO_ERROR;
159 }
160
isNonOffloadableEffectEnabled(const std::optional<const effect_uuid_t> & uuid) const161 bool EffectDescriptorCollection::isNonOffloadableEffectEnabled(
162 const std::optional<const effect_uuid_t>& uuid) const
163 {
164 using namespace android::effect::utils;
165 for (size_t i = 0; i < size(); i++) {
166 sp<EffectDescriptor> effectDesc = valueAt(i);
167 if ((effectDesc->mEnabled && (effectDesc->isMusicEffect()) &&
168 ((effectDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) &&
169 (uuid == std::nullopt || uuid.value() == effectDesc->mDesc.uuid)) {
170 ALOGE("%s: non offloadable effect %s, uuid %s, enabled on session %d", __func__,
171 effectDesc->mDesc.name, ToString(effectDesc->mDesc.uuid).c_str(),
172 effectDesc->mSession);
173 return true;
174 }
175 }
176 return false;
177 }
178
getMaxEffectsCpuLoad() const179 uint32_t EffectDescriptorCollection::getMaxEffectsCpuLoad() const
180 {
181 return MAX_EFFECTS_CPU_LOAD;
182 }
183
getMaxEffectsMemory() const184 uint32_t EffectDescriptorCollection::getMaxEffectsMemory() const
185 {
186 return MAX_EFFECTS_MEMORY;
187 }
188
moveEffects(audio_session_t sessionId,audio_io_handle_t srcIo,audio_io_handle_t dstIo,AudioPolicyClientInterface * clientInterface)189 void EffectDescriptorCollection::moveEffects(audio_session_t sessionId, audio_io_handle_t srcIo,
190 audio_io_handle_t dstIo,
191 AudioPolicyClientInterface *clientInterface)
192 {
193 ALOGV("%s session %d srcIo %d dstIo %d", __func__, sessionId, srcIo, dstIo);
194 for (size_t i = 0; i < size(); i++) {
195 sp<EffectDescriptor> effect = valueAt(i);
196 if (effect->mSession == sessionId && effect->mIo == srcIo) {
197 effect->mIo = dstIo;
198 // Backup enable state before any updatePolicyState call
199 effect->mIsOrphan = (dstIo == AUDIO_IO_HANDLE_NONE);
200 }
201 }
202 clientInterface->moveEffects(sessionId, srcIo, dstIo);
203 }
204
moveEffects(const std::vector<int> & ids,audio_io_handle_t dstIo)205 void EffectDescriptorCollection::moveEffects(const std::vector<int>& ids, audio_io_handle_t dstIo)
206 {
207 ALOGV("%s num effects %zu, first ID %d, dstIo %d",
208 __func__, ids.size(), ids.size() ? ids[0] : 0, dstIo);
209 for (size_t i = 0; i < size(); i++) {
210 sp<EffectDescriptor> effect = valueAt(i);
211 if (std::find(begin(ids), end(ids), effect->mId) != end(ids)) {
212 effect->mIo = dstIo;
213 effect->mIsOrphan = (dstIo == AUDIO_IO_HANDLE_NONE);
214 }
215 }
216 }
217
hasOrphansForSession(audio_session_t sessionId,const effect_uuid_t * effectType) const218 bool EffectDescriptorCollection::hasOrphansForSession(audio_session_t sessionId,
219 const effect_uuid_t* effectType) const {
220 for (size_t i = 0; i < size(); ++i) {
221 sp<EffectDescriptor> effect = valueAt(i);
222 if (effect->mSession == sessionId && effect->mIsOrphan &&
223 (effectType == nullptr ||
224 memcmp(&effect->mDesc.type, effectType, sizeof(effect_uuid_t)) == 0)) {
225 return true;
226 }
227 }
228 return false;
229 }
230
getOrphanEffectsForSession(audio_session_t sessionId) const231 EffectDescriptorCollection EffectDescriptorCollection::getOrphanEffectsForSession(
232 audio_session_t sessionId) const
233 {
234 EffectDescriptorCollection effects;
235 for (size_t i = 0; i < size(); i++) {
236 sp<EffectDescriptor> effect = valueAt(i);
237 if (effect->mSession == sessionId && effect->mIsOrphan) {
238 effects.add(keyAt(i), effect);
239 }
240 }
241 return effects;
242 }
243
getIoForSession(audio_session_t sessionId,const effect_uuid_t * effectType) const244 audio_io_handle_t EffectDescriptorCollection::getIoForSession(audio_session_t sessionId,
245 const effect_uuid_t *effectType) const
246 {
247 for (size_t i = 0; i < size(); ++i) {
248 sp<EffectDescriptor> effect = valueAt(i);
249 if (effect->mSession == sessionId && (effectType == nullptr ||
250 memcmp(&effect->mDesc.type, effectType, sizeof(effect_uuid_t)) == 0)) {
251 return effect->mIo;
252 }
253 }
254 return AUDIO_IO_HANDLE_NONE;
255 }
256
moveEffectsForIo(audio_session_t session,audio_io_handle_t dstIo,const AudioInputCollection * inputs,AudioPolicyClientInterface * clientInterface)257 void EffectDescriptorCollection::moveEffectsForIo(audio_session_t session,
258 audio_io_handle_t dstIo, const AudioInputCollection *inputs,
259 AudioPolicyClientInterface *clientInterface)
260 {
261 // No src io: try to find from effect session the src Io to move from
262 audio_io_handle_t srcIo = getIoForSession(session);
263 if (hasOrphansForSession(session) || (srcIo != AUDIO_IO_HANDLE_NONE && srcIo != dstIo)) {
264 moveEffects(session, srcIo, dstIo, inputs, clientInterface);
265 }
266 }
267
moveEffects(audio_session_t session,audio_io_handle_t srcIo,audio_io_handle_t dstIo,const AudioInputCollection * inputs,AudioPolicyClientInterface * clientInterface)268 void EffectDescriptorCollection::moveEffects(audio_session_t session,
269 audio_io_handle_t srcIo, audio_io_handle_t dstIo, const AudioInputCollection *inputs,
270 AudioPolicyClientInterface *clientInterface)
271 {
272 if ((srcIo != AUDIO_IO_HANDLE_NONE && srcIo == dstIo)
273 || (srcIo == AUDIO_IO_HANDLE_NONE && !hasOrphansForSession(session))) {
274 return;
275 }
276 // Either we may find orphan effects for given session or effects for this session might have
277 // been assigned first to another input (it may happen when an input is released or recreated
278 // after client sets its preferred device)
279 EffectDescriptorCollection effectsToMove;
280 if (srcIo == AUDIO_IO_HANDLE_NONE) {
281 ALOGV("%s: restoring effects for session %d from orphan park to io=%d", __func__,
282 session, dstIo);
283 effectsToMove = getOrphanEffectsForSession(session);
284 } else {
285 ALOGV("%s: moving effects for session %d from io=%d to io=%d", __func__, session, srcIo,
286 dstIo);
287 if (const sp<AudioInputDescriptor>& previousInputDesc = inputs->valueFor(srcIo)) {
288 effectsToMove = getEffectsForIo(srcIo);
289 for (size_t i = 0; i < effectsToMove.size(); ++i) {
290 const sp<EffectDescriptor>& effect = effectsToMove.valueAt(i);
291 effect->mEnabledWhenMoved = effect->mEnabled;
292 previousInputDesc->trackEffectEnabled(effect, false);
293 }
294 } else {
295 ALOGW("%s: no effect descriptor for srcIo %d", __func__, srcIo);
296 }
297 }
298 moveEffects(session, srcIo, dstIo, clientInterface);
299
300 if (dstIo != AUDIO_IO_HANDLE_NONE) {
301 if (const sp<AudioInputDescriptor>& inputDesc = inputs->valueFor(dstIo)) {
302 for (size_t i = 0; i < effectsToMove.size(); ++i) {
303 const sp<EffectDescriptor>& effect = effectsToMove.valueAt(i);
304 inputDesc->trackEffectEnabled(effect, effect->mEnabledWhenMoved);
305 }
306 } else {
307 ALOGW("%s: no effect descriptor for dstIo %d", __func__, dstIo);
308 }
309 }
310 }
311
putOrphanEffectsForIo(audio_io_handle_t srcIo)312 void EffectDescriptorCollection::putOrphanEffectsForIo(audio_io_handle_t srcIo)
313 {
314 for (size_t i = 0; i < size(); i++) {
315 sp<EffectDescriptor> effect = valueAt(i);
316 if (effect->mIo == srcIo) {
317 effect->mIo = AUDIO_IO_HANDLE_NONE;
318 effect->mIsOrphan = true;
319 }
320 }
321 }
322
putOrphanEffects(audio_session_t session,audio_io_handle_t srcIo,const AudioInputCollection * inputs,AudioPolicyClientInterface * clientInterface)323 void EffectDescriptorCollection::putOrphanEffects(audio_session_t session,
324 audio_io_handle_t srcIo, const AudioInputCollection *inputs,
325 AudioPolicyClientInterface *clientInterface)
326 {
327 if (getIoForSession(session) != srcIo) {
328 // Effect session not held by this client io handle
329 return;
330 }
331 ALOGV("%s: park effects for session %d and io=%d to orphans", __func__, session, srcIo);
332 moveEffects(session, srcIo, AUDIO_IO_HANDLE_NONE, inputs, clientInterface);
333 }
334
getEffectsForIo(audio_io_handle_t io) const335 EffectDescriptorCollection EffectDescriptorCollection::getEffectsForIo(audio_io_handle_t io) const
336 {
337 EffectDescriptorCollection effects;
338 for (size_t i = 0; i < size(); i++) {
339 if (valueAt(i)->mIo == io) {
340 effects.add(keyAt(i), valueAt(i));
341 }
342 }
343 return effects;
344 }
345
dump(String8 * dst,int spaces,bool verbose) const346 void EffectDescriptorCollection::dump(String8 *dst, int spaces, bool verbose) const
347 {
348 if (verbose) {
349 dst->appendFormat(
350 "\n%*sTotal Effects CPU: %f MIPS, "
351 "Total Effects memory: %d KB, Max memory used: %d KB\n",
352 spaces, "",
353 (float) mTotalEffectsCpuLoad / 10,
354 mTotalEffectsMemory,
355 mTotalEffectsMemoryMaxUsed);
356 }
357 if (size() > 0) {
358 if (spaces > 1) spaces -= 2;
359 dst->appendFormat("%*s- Effects (%zu):\n", spaces, "", size());
360 for (size_t i = 0; i < size(); i++) {
361 const std::string prefix = base::StringPrintf("%*s %zu. ", spaces, "", i + 1);
362 dst->appendFormat("%s", prefix.c_str());
363 valueAt(i)->dump(dst, prefix.size());
364 }
365 }
366 }
367
368 }; //namespace android
369