blob: 0f3e24532631bf4f1a3c052ca15d0089aa63cfe9 [file] [log] [blame]
Eric Laurent801a1182010-06-09 00:17:29 -07001/*
2**
3** Copyright 2010, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioEffect"
21
22#include <stdint.h>
23#include <sys/types.h>
24#include <limits.h>
25
26#include <private/media/AudioEffectShared.h>
27#include <media/AudioEffect.h>
28
29#include <utils/Log.h>
30#include <cutils/atomic.h>
31#include <binder/IPCThreadState.h>
32
33
34
35namespace android {
36
37// ---------------------------------------------------------------------------
38
39AudioEffect::AudioEffect()
40 : mStatus(NO_INIT)
41{
42}
43
44
45AudioEffect::AudioEffect(const effect_uuid_t *type,
46 const effect_uuid_t *uuid,
47 int32_t priority,
48 effect_callback_t cbf,
49 void* user,
50 int sessionId,
51 audio_io_handle_t output
52 )
53 : mStatus(NO_INIT)
54{
Jean-Michel Trivi42a050f2010-07-09 12:11:49 -070055 mStatus = set(type, uuid, priority, cbf, user, sessionId, output);
Eric Laurent801a1182010-06-09 00:17:29 -070056}
57
58AudioEffect::AudioEffect(const char *typeStr,
59 const char *uuidStr,
60 int32_t priority,
61 effect_callback_t cbf,
62 void* user,
63 int sessionId,
64 audio_io_handle_t output
65 )
66 : mStatus(NO_INIT)
67{
68 effect_uuid_t type;
69 effect_uuid_t *pType = NULL;
70 effect_uuid_t uuid;
71 effect_uuid_t *pUuid = NULL;
72
73 LOGV("Constructor string\n - type: %s\n - uuid: %s", typeStr, uuidStr);
74
75 if (typeStr != NULL) {
76 if (stringToGuid(typeStr, &type) == NO_ERROR) {
77 pType = &type;
78 }
79 }
80
81 if (uuidStr != NULL) {
82 if (stringToGuid(uuidStr, &uuid) == NO_ERROR) {
83 pUuid = &uuid;
84 }
85 }
86
Jean-Michel Trivi42a050f2010-07-09 12:11:49 -070087 mStatus = set(pType, pUuid, priority, cbf, user, sessionId, output);
Eric Laurent801a1182010-06-09 00:17:29 -070088}
89
90status_t AudioEffect::set(const effect_uuid_t *type,
91 const effect_uuid_t *uuid,
92 int32_t priority,
93 effect_callback_t cbf,
94 void* user,
95 int sessionId,
96 audio_io_handle_t output)
97{
98 sp<IEffect> iEffect;
99 sp<IMemory> cblk;
100 int enabled;
101
102 LOGV("set %p mUserData: %p", this, user);
103
104 if (mIEffect != 0) {
105 LOGW("Effect already in use");
106 return INVALID_OPERATION;
107 }
108
109 const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
110 if (audioFlinger == 0) {
111 LOGE("set(): Could not get audioflinger");
112 return NO_INIT;
113 }
114
115 if (type == NULL && uuid == NULL) {
116 LOGW("Must specify at least type or uuid");
117 return BAD_VALUE;
118 }
119
120 mPriority = priority;
121 mCbf = cbf;
122 mUserData = user;
123 mSessionId = sessionId;
124
125 memset(&mDescriptor, 0, sizeof(effect_descriptor_t));
126 memcpy(&mDescriptor.type, EFFECT_UUID_NULL, sizeof(effect_uuid_t));
127 memcpy(&mDescriptor.uuid, EFFECT_UUID_NULL, sizeof(effect_uuid_t));
128
129 if (type != NULL) {
130 memcpy(&mDescriptor.type, type, sizeof(effect_uuid_t));
131 }
132 if (uuid != NULL) {
133 memcpy(&mDescriptor.uuid, uuid, sizeof(effect_uuid_t));
134 }
135
136 mIEffectClient = new EffectClient(this);
137
138 iEffect = audioFlinger->createEffect(getpid(), (effect_descriptor_t *)&mDescriptor,
139 mIEffectClient, priority, output, mSessionId, &mStatus, &mId, &enabled);
140
141 if (iEffect == 0 || (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS)) {
142 LOGE("set(): AudioFlinger could not create effect, status: %d", mStatus);
143 return mStatus;
144 }
145
146 mEnabled = (volatile int32_t)enabled;
147
148 mIEffect = iEffect;
149 cblk = iEffect->getCblk();
150 if (cblk == 0) {
151 mStatus = NO_INIT;
152 LOGE("Could not get control block");
153 return mStatus;
154 }
155
156 mIEffect = iEffect;
157 mCblkMemory = cblk;
158 mCblk = static_cast<effect_param_cblk_t*>(cblk->pointer());
159 int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
160 mCblk->buffer = (uint8_t *)mCblk + bufOffset;
161
162 iEffect->asBinder()->linkToDeath(mIEffectClient);
163 LOGV("set() %p OK effect: %s id: %d status %d enabled %d, ", this, mDescriptor.name, mId, mStatus, mEnabled);
164
165 return mStatus;
166}
167
168
169AudioEffect::~AudioEffect()
170{
171 LOGV("Destructor %p", this);
172
173 if (mStatus == NO_ERROR || mStatus == ALREADY_EXISTS) {
Eric Laurentda7581b2010-07-02 08:12:41 -0700174 setEnabled(false);
Eric Laurent801a1182010-06-09 00:17:29 -0700175 if (mIEffect != NULL) {
176 mIEffect->disconnect();
177 mIEffect->asBinder()->unlinkToDeath(mIEffectClient);
178 }
179 IPCThreadState::self()->flushCommands();
180 }
181 mIEffect.clear();
182 mIEffectClient.clear();
183 mCblkMemory.clear();
184}
185
186
187status_t AudioEffect::initCheck() const
188{
189 return mStatus;
190}
191
192// -------------------------------------------------------------------------
193
194effect_descriptor_t AudioEffect::descriptor() const
195{
196 return mDescriptor;
197}
198
Eric Laurentda7581b2010-07-02 08:12:41 -0700199bool AudioEffect::getEnabled() const
Eric Laurent801a1182010-06-09 00:17:29 -0700200{
201 return (mEnabled != 0);
202}
203
Eric Laurentda7581b2010-07-02 08:12:41 -0700204status_t AudioEffect::setEnabled(bool enabled)
Eric Laurent801a1182010-06-09 00:17:29 -0700205{
206 if (mStatus != NO_ERROR) {
207 return INVALID_OPERATION;
208 }
Eric Laurent801a1182010-06-09 00:17:29 -0700209
Eric Laurentda7581b2010-07-02 08:12:41 -0700210 if (enabled) {
211 LOGV("enable %p", this);
212 if (android_atomic_or(1, &mEnabled) == 0) {
213 return mIEffect->enable();
214 }
215 } else {
216 LOGV("disable %p", this);
217 if (android_atomic_and(~1, &mEnabled) == 1) {
218 return mIEffect->disable();
219 }
Eric Laurent801a1182010-06-09 00:17:29 -0700220 }
Eric Laurent8569f0d2010-07-29 23:43:43 -0700221 return NO_ERROR;
Eric Laurent801a1182010-06-09 00:17:29 -0700222}
223
Eric Laurent25f43952010-07-28 05:40:18 -0700224status_t AudioEffect::command(uint32_t cmdCode,
225 uint32_t cmdSize,
226 void *cmdData,
227 uint32_t *replySize,
228 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700229{
230 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
231 return INVALID_OPERATION;
232 }
233
Eric Laurent8569f0d2010-07-29 23:43:43 -0700234 status_t status = mIEffect->command(cmdCode, cmdSize, cmdData, replySize, replyData);
235 if (status != NO_ERROR) {
236 return status;
237 }
238 status = *(status_t *)replyData;
239 if (status != NO_ERROR) {
240 return status;
241 }
242
243 if (cmdCode == EFFECT_CMD_ENABLE) {
244 android_atomic_or(1, &mEnabled);
245 }
246 if (cmdCode == EFFECT_CMD_DISABLE) {
247 android_atomic_and(~1, &mEnabled);
248 }
249 return status;
Eric Laurent801a1182010-06-09 00:17:29 -0700250}
251
252
253status_t AudioEffect::setParameter(effect_param_t *param)
254{
255 if (mStatus != NO_ERROR) {
256 return INVALID_OPERATION;
257 }
258
259 if (param == NULL || param->psize == 0 || param->vsize == 0) {
260 return BAD_VALUE;
261 }
262
Eric Laurent25f43952010-07-28 05:40:18 -0700263 uint32_t size = sizeof(int);
264 uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700265
266 LOGV("setParameter: param: %d, param2: %d", *(int *)param->data, (param->psize == 8) ? *((int *)param->data + 1): -1);
267
268 return mIEffect->command(EFFECT_CMD_SET_PARAM, sizeof (effect_param_t) + psize, param, &size, &param->status);
269}
270
271status_t AudioEffect::setParameterDeferred(effect_param_t *param)
272{
273 if (mStatus != NO_ERROR) {
274 return INVALID_OPERATION;
275 }
276
277 if (param == NULL || param->psize == 0 || param->vsize == 0) {
278 return BAD_VALUE;
279 }
280
281 Mutex::Autolock _l(mCblk->lock);
282
283 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
284 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
285
286 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
287 return NO_MEMORY;
288 }
289 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
290 *p++ = size;
291 memcpy(p, param, sizeof(effect_param_t) + psize);
292 mCblk->clientIndex += size;
293
294 return NO_ERROR;
295}
296
297status_t AudioEffect::setParameterCommit()
298{
299 if (mStatus != NO_ERROR) {
300 return INVALID_OPERATION;
301 }
302
303 Mutex::Autolock _l(mCblk->lock);
304 if (mCblk->clientIndex == 0) {
305 return INVALID_OPERATION;
306 }
Eric Laurent25f43952010-07-28 05:40:18 -0700307 uint32_t size = 0;
Eric Laurent801a1182010-06-09 00:17:29 -0700308 return mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, 0, NULL, &size, NULL);
309}
310
311status_t AudioEffect::getParameter(effect_param_t *param)
312{
313 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
314 return INVALID_OPERATION;
315 }
316
317 if (param == NULL || param->psize == 0 || param->vsize == 0) {
318 return BAD_VALUE;
319 }
320
321 LOGV("getParameter: param: %d, param2: %d", *(int *)param->data, (param->psize == 8) ? *((int *)param->data + 1): -1);
322
Eric Laurent25f43952010-07-28 05:40:18 -0700323 uint32_t psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
Eric Laurent801a1182010-06-09 00:17:29 -0700324
325 return mIEffect->command(EFFECT_CMD_GET_PARAM, sizeof(effect_param_t) + param->psize, param, &psize, param);
326}
327
328
329// -------------------------------------------------------------------------
330
331void AudioEffect::binderDied()
332{
333 LOGW("IEffect died");
334 mStatus = NO_INIT;
335 if (mCbf) {
336 status_t status = DEAD_OBJECT;
337 mCbf(EVENT_ERROR, mUserData, &status);
338 }
339 mIEffect.clear();
340}
341
342// -------------------------------------------------------------------------
343
344void AudioEffect::controlStatusChanged(bool controlGranted)
345{
346 LOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf, mUserData);
347 if (controlGranted) {
348 if (mStatus == ALREADY_EXISTS) {
349 mStatus = NO_ERROR;
350 }
351 } else {
352 if (mStatus == NO_ERROR) {
353 mStatus = ALREADY_EXISTS;
354 }
355 }
356 if (mCbf) {
357 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
358 }
359}
360
361void AudioEffect::enableStatusChanged(bool enabled)
362{
Eric Laurentda7581b2010-07-02 08:12:41 -0700363 LOGV("enableStatusChanged %p enabled %d mCbf %p", this, enabled, mCbf);
Eric Laurent801a1182010-06-09 00:17:29 -0700364 if (mStatus == ALREADY_EXISTS) {
Eric Laurent8569f0d2010-07-29 23:43:43 -0700365 if (enabled) {
366 android_atomic_or(1, &mEnabled);
367 } else {
368 android_atomic_and(~1, &mEnabled);
369 }
Eric Laurent801a1182010-06-09 00:17:29 -0700370 if (mCbf) {
371 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
372 }
373 }
374}
375
Eric Laurent25f43952010-07-28 05:40:18 -0700376void AudioEffect::commandExecuted(uint32_t cmdCode,
377 uint32_t cmdSize,
378 void *cmdData,
379 uint32_t replySize,
380 void *replyData)
Eric Laurent801a1182010-06-09 00:17:29 -0700381{
382 if (cmdData == NULL || replyData == NULL) {
383 return;
384 }
385
386 if (mCbf && cmdCode == EFFECT_CMD_SET_PARAM) {
387 effect_param_t *cmd = (effect_param_t *)cmdData;
388 cmd->status = *(int32_t *)replyData;
389 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
390 }
391}
392
393// -------------------------------------------------------------------------
394
395status_t AudioEffect::loadEffectLibrary(const char *libPath, int *handle)
396{
397 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
398 if (af == 0) return PERMISSION_DENIED;
399 return af->loadEffectLibrary(libPath, handle);
400}
401
402status_t AudioEffect::unloadEffectLibrary(int handle)
403{
404 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
405 if (af == 0) return PERMISSION_DENIED;
406 return af->unloadEffectLibrary(handle);
407}
408
409status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
410{
411 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
412 if (af == 0) return PERMISSION_DENIED;
413 return af->queryNumberEffects(numEffects);
414}
415
Eric Laurentffe9c252010-06-23 17:38:20 -0700416status_t AudioEffect::queryEffect(uint32_t index, effect_descriptor_t *descriptor)
Eric Laurent801a1182010-06-09 00:17:29 -0700417{
418 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
419 if (af == 0) return PERMISSION_DENIED;
Eric Laurentffe9c252010-06-23 17:38:20 -0700420 return af->queryEffect(index, descriptor);
Eric Laurent801a1182010-06-09 00:17:29 -0700421}
422
423status_t AudioEffect::getEffectDescriptor(effect_uuid_t *uuid, effect_descriptor_t *descriptor)
424{
425 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
426 if (af == 0) return PERMISSION_DENIED;
427 return af->getEffectDescriptor(uuid, descriptor);
428}
429
430// -------------------------------------------------------------------------
431
432status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
433{
434 if (str == NULL || guid == NULL) {
435 return BAD_VALUE;
436 }
437
438 int tmp[10];
439
440 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
441 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
442 return BAD_VALUE;
443 }
444 guid->timeLow = (uint32_t)tmp[0];
445 guid->timeMid = (uint16_t)tmp[1];
446 guid->timeHiAndVersion = (uint16_t)tmp[2];
447 guid->clockSeq = (uint16_t)tmp[3];
448 guid->node[0] = (uint8_t)tmp[4];
449 guid->node[1] = (uint8_t)tmp[5];
450 guid->node[2] = (uint8_t)tmp[6];
451 guid->node[3] = (uint8_t)tmp[7];
452 guid->node[4] = (uint8_t)tmp[8];
453 guid->node[5] = (uint8_t)tmp[9];
454
455 return NO_ERROR;
456}
457
458status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
459{
460 if (guid == NULL || str == NULL) {
461 return BAD_VALUE;
462 }
463
464 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
465 guid->timeLow,
466 guid->timeMid,
467 guid->timeHiAndVersion,
468 guid->clockSeq,
469 guid->node[0],
470 guid->node[1],
471 guid->node[2],
472 guid->node[3],
473 guid->node[4],
474 guid->node[5]);
475
476 return NO_ERROR;
477}
478
479
480}; // namespace android
481