diff options
Diffstat (limited to 'media/jni/audioeffect/android_media_AudioEffect.cpp')
-rw-r--r-- | media/jni/audioeffect/android_media_AudioEffect.cpp | 115 |
1 files changed, 60 insertions, 55 deletions
diff --git a/media/jni/audioeffect/android_media_AudioEffect.cpp b/media/jni/audioeffect/android_media_AudioEffect.cpp index 96b72a2..fdc586b 100644 --- a/media/jni/audioeffect/android_media_AudioEffect.cpp +++ b/media/jni/audioeffect/android_media_AudioEffect.cpp @@ -92,6 +92,7 @@ static jint translateError(int code) { } } +static Mutex sLock; // ---------------------------------------------------------------------------- static void effectCallback(int event, void* user, void *info) { @@ -182,6 +183,32 @@ effectCallback_Exit: } // ---------------------------------------------------------------------------- + +static sp<AudioEffect> getAudioEffect(JNIEnv* env, jobject thiz) +{ + Mutex::Autolock l(sLock); + AudioEffect* const ae = + (AudioEffect*)env->GetLongField(thiz, fields.fidNativeAudioEffect); + return sp<AudioEffect>(ae); +} + +static sp<AudioEffect> setAudioEffect(JNIEnv* env, jobject thiz, + const sp<AudioEffect>& ae) +{ + Mutex::Autolock l(sLock); + sp<AudioEffect> old = + (AudioEffect*)env->GetLongField(thiz, fields.fidNativeAudioEffect); + if (ae.get()) { + ae->incStrong((void*)setAudioEffect); + } + if (old != 0) { + old->decStrong((void*)setAudioEffect); + } + env->SetLongField(thiz, fields.fidNativeAudioEffect, (jlong)ae.get()); + return old; +} + +// ---------------------------------------------------------------------------- // This function gets some field IDs, which in turn causes class initialization. // It is called from a static block in AudioEffect, which won't run until the // first time an instance of this class is used. @@ -257,7 +284,7 @@ android_media_AudioEffect_native_setup(JNIEnv *env, jobject thiz, jobject weak_t ALOGV("android_media_AudioEffect_native_setup"); AudioEffectJniStorage* lpJniStorage = NULL; int lStatus = AUDIOEFFECT_ERROR_NO_MEMORY; - AudioEffect* lpAudioEffect = NULL; + sp<AudioEffect> lpAudioEffect; jint* nId = NULL; const char *typeStr = NULL; const char *uuidStr = NULL; @@ -272,6 +299,8 @@ android_media_AudioEffect_native_setup(JNIEnv *env, jobject thiz, jobject weak_t ScopedUtfChars opPackageNameStr(env, opPackageName); + setAudioEffect(env, thiz, 0); + if (type != NULL) { typeStr = env->GetStringUTFChars(type, NULL); if (typeStr == NULL) { // Out of memory @@ -324,7 +353,7 @@ android_media_AudioEffect_native_setup(JNIEnv *env, jobject thiz, jobject weak_t &lpJniStorage->mCallbackData, sessionId, 0); - if (lpAudioEffect == NULL) { + if (lpAudioEffect == 0) { ALOGE("Error creating AudioEffect"); goto setup_failure; } @@ -394,7 +423,7 @@ android_media_AudioEffect_native_setup(JNIEnv *env, jobject thiz, jobject weak_t env->SetObjectArrayElement(javadesc, 0, jdesc); - env->SetLongField(thiz, fields.fidNativeAudioEffect, (jlong)lpAudioEffect); + setAudioEffect(env, thiz, lpAudioEffect); env->SetLongField(thiz, fields.fidJniData, (jlong)lpJniStorage); @@ -407,12 +436,9 @@ setup_failure: env->ReleasePrimitiveArrayCritical(jId, nId, 0); } - if (lpAudioEffect) { - delete lpAudioEffect; - } - env->SetLongField(thiz, fields.fidNativeAudioEffect, 0); - if (lpJniStorage) { + env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioEffect_class); + env->DeleteGlobalRef(lpJniStorage->mCallbackData.audioEffect_ref); delete lpJniStorage; } env->SetLongField(thiz, fields.fidJniData, 0); @@ -430,20 +456,20 @@ setup_failure: // ---------------------------------------------------------------------------- -static void android_media_AudioEffect_native_finalize(JNIEnv *env, jobject thiz) { - ALOGV("android_media_AudioEffect_native_finalize jobject: %p\n", thiz); - - // delete the AudioEffect object - AudioEffect* lpAudioEffect = (AudioEffect *)env->GetLongField( - thiz, fields.fidNativeAudioEffect); - if (lpAudioEffect) { - ALOGV("deleting AudioEffect: %p\n", lpAudioEffect); - delete lpAudioEffect; +static void android_media_AudioEffect_native_release(JNIEnv *env, jobject thiz) { + sp<AudioEffect> lpAudioEffect = setAudioEffect(env, thiz, 0); + if (lpAudioEffect == 0) { + return; } // delete the JNI data - AudioEffectJniStorage* lpJniStorage = (AudioEffectJniStorage *)env->GetLongField( - thiz, fields.fidJniData); + AudioEffectJniStorage* lpJniStorage = + (AudioEffectJniStorage *)env->GetLongField(thiz, fields.fidJniData); + + // reset the native resources in the Java object so any attempt to access + // them after a call to release fails. + env->SetLongField(thiz, fields.fidJniData, 0); + if (lpJniStorage) { ALOGV("deleting pJniStorage: %p\n", lpJniStorage); delete lpJniStorage; @@ -451,24 +477,16 @@ static void android_media_AudioEffect_native_finalize(JNIEnv *env, jobject thiz } // ---------------------------------------------------------------------------- -static void android_media_AudioEffect_native_release(JNIEnv *env, jobject thiz) { - - // do everything a call to finalize would - android_media_AudioEffect_native_finalize(env, thiz); - // + reset the native resources in the Java object so any attempt to access - // them after a call to release fails. - env->SetLongField(thiz, fields.fidNativeAudioEffect, 0); - env->SetLongField(thiz, fields.fidJniData, 0); +static void android_media_AudioEffect_native_finalize(JNIEnv *env, jobject thiz) { + ALOGV("android_media_AudioEffect_native_finalize jobject: %p\n", thiz); + android_media_AudioEffect_native_release(env, thiz); } static jint android_media_AudioEffect_native_setEnabled(JNIEnv *env, jobject thiz, jboolean enabled) { - // retrieve the AudioEffect object - AudioEffect* lpAudioEffect = (AudioEffect *)env->GetLongField( - thiz, fields.fidNativeAudioEffect); - - if (lpAudioEffect == NULL) { + sp<AudioEffect> lpAudioEffect = getAudioEffect(env, thiz); + if (lpAudioEffect == 0) { jniThrowException(env, "java/lang/IllegalStateException", "Unable to retrieve AudioEffect pointer for enable()"); return AUDIOEFFECT_ERROR_NO_INIT; @@ -480,11 +498,8 @@ android_media_AudioEffect_native_setEnabled(JNIEnv *env, jobject thiz, jboolean static jboolean android_media_AudioEffect_native_getEnabled(JNIEnv *env, jobject thiz) { - // retrieve the AudioEffect object - AudioEffect* lpAudioEffect = (AudioEffect *)env->GetLongField( - thiz, fields.fidNativeAudioEffect); - - if (lpAudioEffect == NULL) { + sp<AudioEffect> lpAudioEffect = getAudioEffect(env, thiz); + if (lpAudioEffect == 0) { jniThrowException(env, "java/lang/IllegalStateException", "Unable to retrieve AudioEffect pointer for getEnabled()"); return JNI_FALSE; @@ -501,11 +516,8 @@ android_media_AudioEffect_native_getEnabled(JNIEnv *env, jobject thiz) static jboolean android_media_AudioEffect_native_hasControl(JNIEnv *env, jobject thiz) { - // retrieve the AudioEffect object - AudioEffect* lpAudioEffect = (AudioEffect *)env->GetLongField( - thiz, fields.fidNativeAudioEffect); - - if (lpAudioEffect == NULL) { + sp<AudioEffect> lpAudioEffect = getAudioEffect(env, thiz); + if (lpAudioEffect == 0) { jniThrowException(env, "java/lang/IllegalStateException", "Unable to retrieve AudioEffect pointer for hasControl()"); return JNI_FALSE; @@ -528,10 +540,8 @@ static jint android_media_AudioEffect_native_setParameter(JNIEnv *env, effect_param_t *p; int voffset; - AudioEffect* lpAudioEffect = (AudioEffect *) env->GetLongField(thiz, - fields.fidNativeAudioEffect); - - if (lpAudioEffect == NULL) { + sp<AudioEffect> lpAudioEffect = getAudioEffect(env, thiz); + if (lpAudioEffect == 0) { jniThrowException(env, "java/lang/IllegalStateException", "Unable to retrieve AudioEffect pointer for setParameter()"); return AUDIOEFFECT_ERROR_NO_INIT; @@ -591,10 +601,8 @@ android_media_AudioEffect_native_getParameter(JNIEnv *env, effect_param_t *p; int voffset; - AudioEffect* lpAudioEffect = (AudioEffect *) env->GetLongField(thiz, - fields.fidNativeAudioEffect); - - if (lpAudioEffect == NULL) { + sp<AudioEffect> lpAudioEffect = getAudioEffect(env, thiz); + if (lpAudioEffect == 0) { jniThrowException(env, "java/lang/IllegalStateException", "Unable to retrieve AudioEffect pointer for getParameter()"); return AUDIOEFFECT_ERROR_NO_INIT; @@ -657,11 +665,8 @@ static jint android_media_AudioEffect_native_command(JNIEnv *env, jobject thiz, jbyte* pReplyData = NULL; jint lStatus = AUDIOEFFECT_ERROR_BAD_VALUE; - // retrieve the AudioEffect object - AudioEffect* lpAudioEffect = (AudioEffect *) env->GetLongField(thiz, - fields.fidNativeAudioEffect); - - if (lpAudioEffect == NULL) { + sp<AudioEffect> lpAudioEffect = getAudioEffect(env, thiz); + if (lpAudioEffect == 0) { jniThrowException(env, "java/lang/IllegalStateException", "Unable to retrieve AudioEffect pointer for setParameter()"); return AUDIOEFFECT_ERROR_NO_INIT; |