blob: 864821171fba4733ef7fe2eadfd77a7de84858d1 [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{
55 mStatus = set(type, uuid, priority, cbf, user, output, sessionId);
56}
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
87 mStatus = set(pType, pUuid, priority, cbf, user, output, sessionId);
88}
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) {
174 disable();
175 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
199bool AudioEffect::isEnabled() const
200{
201 return (mEnabled != 0);
202}
203
204status_t AudioEffect::enable()
205{
206 if (mStatus != NO_ERROR) {
207 return INVALID_OPERATION;
208 }
209 LOGV("enable %p", this);
210
211 if (android_atomic_or(1, &mEnabled) == 0) {
212 return mIEffect->enable();
213 }
214
215 return INVALID_OPERATION;
216}
217
218status_t AudioEffect::disable()
219{
220 if (mStatus != NO_ERROR) {
221 return INVALID_OPERATION;
222 }
223 LOGV("disable %p", this);
224
225 if (android_atomic_and(~1, &mEnabled) == 1) {
226 return mIEffect->disable();
227 }
228
229 return INVALID_OPERATION;
230}
231
232status_t AudioEffect::command(int32_t cmdCode, int32_t cmdSize, void *cmdData, int32_t *replySize, void *replyData)
233{
234 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
235 return INVALID_OPERATION;
236 }
237
238 return mIEffect->command(cmdCode, cmdSize, cmdData, replySize, replyData);
239}
240
241
242status_t AudioEffect::setParameter(effect_param_t *param)
243{
244 if (mStatus != NO_ERROR) {
245 return INVALID_OPERATION;
246 }
247
248 if (param == NULL || param->psize == 0 || param->vsize == 0) {
249 return BAD_VALUE;
250 }
251
252 int size = sizeof(int);
253 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
254
255 LOGV("setParameter: param: %d, param2: %d", *(int *)param->data, (param->psize == 8) ? *((int *)param->data + 1): -1);
256
257 return mIEffect->command(EFFECT_CMD_SET_PARAM, sizeof (effect_param_t) + psize, param, &size, &param->status);
258}
259
260status_t AudioEffect::setParameterDeferred(effect_param_t *param)
261{
262 if (mStatus != NO_ERROR) {
263 return INVALID_OPERATION;
264 }
265
266 if (param == NULL || param->psize == 0 || param->vsize == 0) {
267 return BAD_VALUE;
268 }
269
270 Mutex::Autolock _l(mCblk->lock);
271
272 int psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
273 int size = ((sizeof(effect_param_t) + psize - 1) / sizeof(int) + 1) * sizeof(int);
274
275 if (mCblk->clientIndex + size > EFFECT_PARAM_BUFFER_SIZE) {
276 return NO_MEMORY;
277 }
278 int *p = (int *)(mCblk->buffer + mCblk->clientIndex);
279 *p++ = size;
280 memcpy(p, param, sizeof(effect_param_t) + psize);
281 mCblk->clientIndex += size;
282
283 return NO_ERROR;
284}
285
286status_t AudioEffect::setParameterCommit()
287{
288 if (mStatus != NO_ERROR) {
289 return INVALID_OPERATION;
290 }
291
292 Mutex::Autolock _l(mCblk->lock);
293 if (mCblk->clientIndex == 0) {
294 return INVALID_OPERATION;
295 }
296 int size = 0;
297 return mIEffect->command(EFFECT_CMD_SET_PARAM_COMMIT, 0, NULL, &size, NULL);
298}
299
300status_t AudioEffect::getParameter(effect_param_t *param)
301{
302 if (mStatus != NO_ERROR && mStatus != ALREADY_EXISTS) {
303 return INVALID_OPERATION;
304 }
305
306 if (param == NULL || param->psize == 0 || param->vsize == 0) {
307 return BAD_VALUE;
308 }
309
310 LOGV("getParameter: param: %d, param2: %d", *(int *)param->data, (param->psize == 8) ? *((int *)param->data + 1): -1);
311
312 int psize = sizeof(effect_param_t) + ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + param->vsize;
313
314 return mIEffect->command(EFFECT_CMD_GET_PARAM, sizeof(effect_param_t) + param->psize, param, &psize, param);
315}
316
317
318// -------------------------------------------------------------------------
319
320void AudioEffect::binderDied()
321{
322 LOGW("IEffect died");
323 mStatus = NO_INIT;
324 if (mCbf) {
325 status_t status = DEAD_OBJECT;
326 mCbf(EVENT_ERROR, mUserData, &status);
327 }
328 mIEffect.clear();
329}
330
331// -------------------------------------------------------------------------
332
333void AudioEffect::controlStatusChanged(bool controlGranted)
334{
335 LOGV("controlStatusChanged %p control %d callback %p mUserData %p", this, controlGranted, mCbf, mUserData);
336 if (controlGranted) {
337 if (mStatus == ALREADY_EXISTS) {
338 mStatus = NO_ERROR;
339 }
340 } else {
341 if (mStatus == NO_ERROR) {
342 mStatus = ALREADY_EXISTS;
343 }
344 }
345 if (mCbf) {
346 mCbf(EVENT_CONTROL_STATUS_CHANGED, mUserData, &controlGranted);
347 }
348}
349
350void AudioEffect::enableStatusChanged(bool enabled)
351{
352 LOGV("enableStatusChanged %p enabled %d", this, enabled);
353 if (mStatus == ALREADY_EXISTS) {
354 mEnabled = enabled;
355 if (mCbf) {
356 mCbf(EVENT_ENABLE_STATUS_CHANGED, mUserData, &enabled);
357 }
358 }
359}
360
361void AudioEffect::commandExecuted(int cmdCode, int cmdSize, void *cmdData, int replySize, void *replyData)
362{
363 if (cmdData == NULL || replyData == NULL) {
364 return;
365 }
366
367 if (mCbf && cmdCode == EFFECT_CMD_SET_PARAM) {
368 effect_param_t *cmd = (effect_param_t *)cmdData;
369 cmd->status = *(int32_t *)replyData;
370 mCbf(EVENT_PARAMETER_CHANGED, mUserData, cmd);
371 }
372}
373
374// -------------------------------------------------------------------------
375
376status_t AudioEffect::loadEffectLibrary(const char *libPath, int *handle)
377{
378 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
379 if (af == 0) return PERMISSION_DENIED;
380 return af->loadEffectLibrary(libPath, handle);
381}
382
383status_t AudioEffect::unloadEffectLibrary(int handle)
384{
385 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
386 if (af == 0) return PERMISSION_DENIED;
387 return af->unloadEffectLibrary(handle);
388}
389
390status_t AudioEffect::queryNumberEffects(uint32_t *numEffects)
391{
392 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
393 if (af == 0) return PERMISSION_DENIED;
394 return af->queryNumberEffects(numEffects);
395}
396
397status_t AudioEffect::queryNextEffect(effect_descriptor_t *descriptor)
398{
399 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
400 if (af == 0) return PERMISSION_DENIED;
401 return af->queryNextEffect(descriptor);
402}
403
404status_t AudioEffect::getEffectDescriptor(effect_uuid_t *uuid, effect_descriptor_t *descriptor)
405{
406 const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger();
407 if (af == 0) return PERMISSION_DENIED;
408 return af->getEffectDescriptor(uuid, descriptor);
409}
410
411// -------------------------------------------------------------------------
412
413status_t AudioEffect::stringToGuid(const char *str, effect_uuid_t *guid)
414{
415 if (str == NULL || guid == NULL) {
416 return BAD_VALUE;
417 }
418
419 int tmp[10];
420
421 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
422 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
423 return BAD_VALUE;
424 }
425 guid->timeLow = (uint32_t)tmp[0];
426 guid->timeMid = (uint16_t)tmp[1];
427 guid->timeHiAndVersion = (uint16_t)tmp[2];
428 guid->clockSeq = (uint16_t)tmp[3];
429 guid->node[0] = (uint8_t)tmp[4];
430 guid->node[1] = (uint8_t)tmp[5];
431 guid->node[2] = (uint8_t)tmp[6];
432 guid->node[3] = (uint8_t)tmp[7];
433 guid->node[4] = (uint8_t)tmp[8];
434 guid->node[5] = (uint8_t)tmp[9];
435
436 return NO_ERROR;
437}
438
439status_t AudioEffect::guidToString(const effect_uuid_t *guid, char *str, size_t maxLen)
440{
441 if (guid == NULL || str == NULL) {
442 return BAD_VALUE;
443 }
444
445 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
446 guid->timeLow,
447 guid->timeMid,
448 guid->timeHiAndVersion,
449 guid->clockSeq,
450 guid->node[0],
451 guid->node[1],
452 guid->node[2],
453 guid->node[3],
454 guid->node[4],
455 guid->node[5]);
456
457 return NO_ERROR;
458}
459
460
461}; // namespace android
462