1/*
2 * Copyright (C) 2010 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#include <stdio.h>
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "visualizers-JNI"
21
22#include <utils/Log.h>
23#include <nativehelper/jni.h>
24#include <nativehelper/JNIHelp.h>
25#include <android_runtime/AndroidRuntime.h>
26#include <utils/threads.h>
27#include "media/Visualizer.h"
28
29using namespace android;
30
31#define VISUALIZER_SUCCESS                      0
32#define VISUALIZER_ERROR                       -1
33#define VISUALIZER_ERROR_ALREADY_EXISTS        -2
34#define VISUALIZER_ERROR_NO_INIT               -3
35#define VISUALIZER_ERROR_BAD_VALUE             -4
36#define VISUALIZER_ERROR_INVALID_OPERATION     -5
37#define VISUALIZER_ERROR_NO_MEMORY             -6
38#define VISUALIZER_ERROR_DEAD_OBJECT           -7
39
40#define NATIVE_EVENT_PCM_CAPTURE                0
41#define NATIVE_EVENT_FFT_CAPTURE                1
42#define NATIVE_EVENT_SERVER_DIED                2
43
44// ----------------------------------------------------------------------------
45static const char* const kClassPathName = "android/media/audiofx/Visualizer";
46
47struct fields_t {
48    // these fields provide access from C++ to the...
49    jclass    clazzEffect;          // Visualizer class
50    jmethodID midPostNativeEvent;   // event post callback method
51    jfieldID  fidNativeVisualizer; // stores in Java the native Visualizer object
52    jfieldID  fidJniData;           // stores in Java additional resources used by the native Visualizer
53};
54static fields_t fields;
55
56struct visualizer_callback_cookie {
57    jclass      visualizer_class;  // Visualizer class
58    jobject     visualizer_ref;    // Visualizer object instance
59
60    // Lazily allocated arrays used to hold callback data provided to java
61    // applications.  These arrays are allocated during the first callback and
62    // reallocated when the size of the callback data changes.  Allocating on
63    // demand and saving the arrays means that applications cannot safely hold a
64    // reference to the provided data (they need to make a copy if they want to
65    // hold onto outside of the callback scope), but it avoids GC thrash caused
66    // by constantly allocating and releasing arrays to hold callback data.
67    Mutex       callback_data_lock;
68    jbyteArray  waveform_data;
69    jbyteArray  fft_data;
70
71    visualizer_callback_cookie() {
72        waveform_data = NULL;
73        fft_data = NULL;
74    }
75
76    ~visualizer_callback_cookie() {
77        cleanupBuffers();
78    }
79
80    void cleanupBuffers() {
81        AutoMutex lock(&callback_data_lock);
82        if (waveform_data || fft_data) {
83            JNIEnv *env = AndroidRuntime::getJNIEnv();
84
85            if (waveform_data) {
86                env->DeleteGlobalRef(waveform_data);
87                waveform_data = NULL;
88            }
89
90            if (fft_data) {
91                env->DeleteGlobalRef(fft_data);
92                fft_data = NULL;
93            }
94        }
95    }
96 };
97
98// ----------------------------------------------------------------------------
99class visualizerJniStorage {
100    public:
101        visualizer_callback_cookie mCallbackData;
102
103    visualizerJniStorage() {
104    }
105
106    ~visualizerJniStorage() {
107    }
108};
109
110
111static jint translateError(int code) {
112    switch(code) {
113    case NO_ERROR:
114        return VISUALIZER_SUCCESS;
115    case ALREADY_EXISTS:
116        return VISUALIZER_ERROR_ALREADY_EXISTS;
117    case NO_INIT:
118        return VISUALIZER_ERROR_NO_INIT;
119    case BAD_VALUE:
120        return VISUALIZER_ERROR_BAD_VALUE;
121    case INVALID_OPERATION:
122        return VISUALIZER_ERROR_INVALID_OPERATION;
123    case NO_MEMORY:
124        return VISUALIZER_ERROR_NO_MEMORY;
125    case DEAD_OBJECT:
126        return VISUALIZER_ERROR_DEAD_OBJECT;
127    default:
128        return VISUALIZER_ERROR;
129    }
130}
131
132
133// ----------------------------------------------------------------------------
134static void ensureArraySize(JNIEnv *env, jbyteArray *array, uint32_t size) {
135    if (NULL != *array) {
136        uint32_t len = env->GetArrayLength(*array);
137        if (len == size)
138            return;
139
140        env->DeleteGlobalRef(*array);
141        *array = NULL;
142    }
143
144    jbyteArray localRef = env->NewByteArray(size);
145    if (NULL != localRef) {
146        // Promote to global ref.
147        *array = (jbyteArray)env->NewGlobalRef(localRef);
148
149        // Release our (now pointless) local ref.
150        env->DeleteLocalRef(localRef);
151    }
152}
153
154static void captureCallback(void* user,
155        uint32_t waveformSize,
156        uint8_t *waveform,
157        uint32_t fftSize,
158        uint8_t *fft,
159        uint32_t samplingrate) {
160
161    int arg1 = 0;
162    int arg2 = 0;
163    size_t size;
164
165    visualizer_callback_cookie *callbackInfo = (visualizer_callback_cookie *)user;
166    JNIEnv *env = AndroidRuntime::getJNIEnv();
167    AutoMutex lock(&callbackInfo->callback_data_lock);
168
169    ALOGV("captureCallback: callbackInfo %p, visualizer_ref %p visualizer_class %p",
170            callbackInfo,
171            callbackInfo->visualizer_ref,
172            callbackInfo->visualizer_class);
173
174    if (!user || !env) {
175        ALOGW("captureCallback error user %p, env %p", user, env);
176        return;
177    }
178
179    if (waveformSize != 0 && waveform != NULL) {
180        jbyteArray jArray;
181
182        ensureArraySize(env, &callbackInfo->waveform_data, waveformSize);
183        jArray = callbackInfo->waveform_data;
184
185        if (jArray != NULL) {
186            jbyte *nArray = env->GetByteArrayElements(jArray, NULL);
187            memcpy(nArray, waveform, waveformSize);
188            env->ReleaseByteArrayElements(jArray, nArray, 0);
189            env->CallStaticVoidMethod(
190                callbackInfo->visualizer_class,
191                fields.midPostNativeEvent,
192                callbackInfo->visualizer_ref,
193                NATIVE_EVENT_PCM_CAPTURE,
194                samplingrate,
195                0,
196                jArray);
197        }
198    }
199
200    if (fftSize != 0 && fft != NULL) {
201        jbyteArray jArray;
202
203        ensureArraySize(env, &callbackInfo->fft_data, fftSize);
204        jArray = callbackInfo->fft_data;
205
206        if (jArray != NULL) {
207            jbyte *nArray = env->GetByteArrayElements(jArray, NULL);
208            memcpy(nArray, fft, fftSize);
209            env->ReleaseByteArrayElements(jArray, nArray, 0);
210            env->CallStaticVoidMethod(
211                callbackInfo->visualizer_class,
212                fields.midPostNativeEvent,
213                callbackInfo->visualizer_ref,
214                NATIVE_EVENT_FFT_CAPTURE,
215                samplingrate,
216                0,
217                jArray);
218        }
219    }
220
221    if (env->ExceptionCheck()) {
222        env->ExceptionDescribe();
223        env->ExceptionClear();
224    }
225}
226
227static Visualizer *getVisualizer(JNIEnv* env, jobject thiz)
228{
229    Visualizer *v = (Visualizer *)env->GetIntField(
230        thiz, fields.fidNativeVisualizer);
231    if (v == NULL) {
232        jniThrowException(env, "java/lang/IllegalStateException",
233            "Unable to retrieve Visualizer pointer");
234    }
235    return v;
236}
237
238// ----------------------------------------------------------------------------
239// This function gets some field IDs, which in turn causes class initialization.
240// It is called from a static block in Visualizer, which won't run until the
241// first time an instance of this class is used.
242static void
243android_media_visualizer_native_init(JNIEnv *env)
244{
245
246    ALOGV("android_media_visualizer_native_init");
247
248    fields.clazzEffect = NULL;
249
250    // Get the Visualizer class
251    jclass clazz = env->FindClass(kClassPathName);
252    if (clazz == NULL) {
253        ALOGE("Can't find %s", kClassPathName);
254        return;
255    }
256
257    fields.clazzEffect = (jclass)env->NewGlobalRef(clazz);
258
259    // Get the postEvent method
260    fields.midPostNativeEvent = env->GetStaticMethodID(
261            fields.clazzEffect,
262            "postEventFromNative", "(Ljava/lang/Object;IIILjava/lang/Object;)V");
263    if (fields.midPostNativeEvent == NULL) {
264        ALOGE("Can't find Visualizer.%s", "postEventFromNative");
265        return;
266    }
267
268    // Get the variables fields
269    //      nativeTrackInJavaObj
270    fields.fidNativeVisualizer = env->GetFieldID(
271            fields.clazzEffect,
272            "mNativeVisualizer", "I");
273    if (fields.fidNativeVisualizer == NULL) {
274        ALOGE("Can't find Visualizer.%s", "mNativeVisualizer");
275        return;
276    }
277    //      fidJniData;
278    fields.fidJniData = env->GetFieldID(
279            fields.clazzEffect,
280            "mJniData", "I");
281    if (fields.fidJniData == NULL) {
282        ALOGE("Can't find Visualizer.%s", "mJniData");
283        return;
284    }
285
286}
287
288static void android_media_visualizer_effect_callback(int32_t event,
289                                                     void *user,
290                                                     void *info) {
291    if ((event == AudioEffect::EVENT_ERROR) &&
292        (*((status_t*)info) == DEAD_OBJECT)) {
293        visualizerJniStorage* lpJniStorage = (visualizerJniStorage*)user;
294        visualizer_callback_cookie* callbackInfo = &lpJniStorage->mCallbackData;
295        JNIEnv *env = AndroidRuntime::getJNIEnv();
296
297        env->CallStaticVoidMethod(
298            callbackInfo->visualizer_class,
299            fields.midPostNativeEvent,
300            callbackInfo->visualizer_ref,
301            NATIVE_EVENT_SERVER_DIED,
302            0, 0, 0);
303    }
304}
305
306static jint
307android_media_visualizer_native_setup(JNIEnv *env, jobject thiz, jobject weak_this,
308        jint sessionId, jintArray jId)
309{
310    ALOGV("android_media_visualizer_native_setup");
311    visualizerJniStorage* lpJniStorage = NULL;
312    int lStatus = VISUALIZER_ERROR_NO_MEMORY;
313    Visualizer* lpVisualizer = NULL;
314    jint* nId = NULL;
315
316    lpJniStorage = new visualizerJniStorage();
317    if (lpJniStorage == NULL) {
318        ALOGE("setup: Error creating JNI Storage");
319        goto setup_failure;
320    }
321
322    lpJniStorage->mCallbackData.visualizer_class = (jclass)env->NewGlobalRef(fields.clazzEffect);
323    // we use a weak reference so the Visualizer object can be garbage collected.
324    lpJniStorage->mCallbackData.visualizer_ref = env->NewGlobalRef(weak_this);
325
326    ALOGV("setup: lpJniStorage: %p visualizer_ref %p visualizer_class %p, &mCallbackData %p",
327            lpJniStorage,
328            lpJniStorage->mCallbackData.visualizer_ref,
329            lpJniStorage->mCallbackData.visualizer_class,
330            &lpJniStorage->mCallbackData);
331
332    if (jId == NULL) {
333        ALOGE("setup: NULL java array for id pointer");
334        lStatus = VISUALIZER_ERROR_BAD_VALUE;
335        goto setup_failure;
336    }
337
338    // create the native Visualizer object
339    lpVisualizer = new Visualizer(0,
340                                  android_media_visualizer_effect_callback,
341                                  lpJniStorage,
342                                  sessionId);
343    if (lpVisualizer == NULL) {
344        ALOGE("Error creating Visualizer");
345        goto setup_failure;
346    }
347
348    lStatus = translateError(lpVisualizer->initCheck());
349    if (lStatus != VISUALIZER_SUCCESS && lStatus != VISUALIZER_ERROR_ALREADY_EXISTS) {
350        ALOGE("Visualizer initCheck failed %d", lStatus);
351        goto setup_failure;
352    }
353
354    nId = (jint *) env->GetPrimitiveArrayCritical(jId, NULL);
355    if (nId == NULL) {
356        ALOGE("setup: Error retrieving id pointer");
357        lStatus = VISUALIZER_ERROR_BAD_VALUE;
358        goto setup_failure;
359    }
360    nId[0] = lpVisualizer->id();
361    env->ReleasePrimitiveArrayCritical(jId, nId, 0);
362    nId = NULL;
363
364    env->SetIntField(thiz, fields.fidNativeVisualizer, (int)lpVisualizer);
365
366    env->SetIntField(thiz, fields.fidJniData, (int)lpJniStorage);
367
368    return VISUALIZER_SUCCESS;
369
370    // failures:
371setup_failure:
372
373    if (nId != NULL) {
374        env->ReleasePrimitiveArrayCritical(jId, nId, 0);
375    }
376
377    if (lpVisualizer) {
378        delete lpVisualizer;
379    }
380    env->SetIntField(thiz, fields.fidNativeVisualizer, 0);
381
382    if (lpJniStorage) {
383        delete lpJniStorage;
384    }
385    env->SetIntField(thiz, fields.fidJniData, 0);
386
387    return lStatus;
388}
389
390// ----------------------------------------------------------------------------
391static void android_media_visualizer_native_finalize(JNIEnv *env,  jobject thiz) {
392    ALOGV("android_media_visualizer_native_finalize jobject: %x\n", (int)thiz);
393
394    // delete the Visualizer object
395    Visualizer* lpVisualizer = (Visualizer *)env->GetIntField(
396        thiz, fields.fidNativeVisualizer);
397    if (lpVisualizer) {
398        ALOGV("deleting Visualizer: %x\n", (int)lpVisualizer);
399        delete lpVisualizer;
400    }
401
402    // delete the JNI data
403    visualizerJniStorage* lpJniStorage = (visualizerJniStorage *)env->GetIntField(
404        thiz, fields.fidJniData);
405    if (lpJniStorage) {
406        ALOGV("deleting pJniStorage: %x\n", (int)lpJniStorage);
407        delete lpJniStorage;
408    }
409}
410
411// ----------------------------------------------------------------------------
412static void android_media_visualizer_native_release(JNIEnv *env,  jobject thiz) {
413
414    // do everything a call to finalize would
415    android_media_visualizer_native_finalize(env, thiz);
416    // + reset the native resources in the Java object so any attempt to access
417    // them after a call to release fails.
418    env->SetIntField(thiz, fields.fidNativeVisualizer, 0);
419    env->SetIntField(thiz, fields.fidJniData, 0);
420}
421
422static jint
423android_media_visualizer_native_setEnabled(JNIEnv *env, jobject thiz, jboolean enabled)
424{
425    Visualizer* lpVisualizer = getVisualizer(env, thiz);
426    if (lpVisualizer == NULL) {
427        return VISUALIZER_ERROR_NO_INIT;
428    }
429
430    jint retVal = translateError(lpVisualizer->setEnabled(enabled));
431
432    if (!enabled) {
433        visualizerJniStorage* lpJniStorage = (visualizerJniStorage *)env->GetIntField(
434            thiz, fields.fidJniData);
435
436        if (NULL != lpJniStorage)
437            lpJniStorage->mCallbackData.cleanupBuffers();
438    }
439
440    return retVal;
441}
442
443static jboolean
444android_media_visualizer_native_getEnabled(JNIEnv *env, jobject thiz)
445{
446    Visualizer* lpVisualizer = getVisualizer(env, thiz);
447    if (lpVisualizer == NULL) {
448        return false;
449    }
450
451    return (jboolean)lpVisualizer->getEnabled();
452}
453
454static jintArray
455android_media_visualizer_native_getCaptureSizeRange(JNIEnv *env, jobject thiz)
456{
457    jintArray jRange = env->NewIntArray(2);
458    jint *nRange = env->GetIntArrayElements(jRange, NULL);
459    nRange[0] = Visualizer::getMinCaptureSize();
460    nRange[1] = Visualizer::getMaxCaptureSize();
461    ALOGV("getCaptureSizeRange() min %d max %d", nRange[0], nRange[1]);
462    env->ReleaseIntArrayElements(jRange, nRange, 0);
463    return jRange;
464}
465
466static jint
467android_media_visualizer_native_getMaxCaptureRate(JNIEnv *env, jobject thiz)
468{
469    return Visualizer::getMaxCaptureRate();
470}
471
472static jint
473android_media_visualizer_native_setCaptureSize(JNIEnv *env, jobject thiz, jint size)
474{
475    Visualizer* lpVisualizer = getVisualizer(env, thiz);
476    if (lpVisualizer == NULL) {
477        return VISUALIZER_ERROR_NO_INIT;
478    }
479
480    return translateError(lpVisualizer->setCaptureSize(size));
481}
482
483static jint
484android_media_visualizer_native_getCaptureSize(JNIEnv *env, jobject thiz)
485{
486    Visualizer* lpVisualizer = getVisualizer(env, thiz);
487    if (lpVisualizer == NULL) {
488        return -1;
489    }
490    return lpVisualizer->getCaptureSize();
491}
492
493static jint
494android_media_visualizer_native_setScalingMode(JNIEnv *env, jobject thiz, jint mode)
495{
496    Visualizer* lpVisualizer = getVisualizer(env, thiz);
497    if (lpVisualizer == NULL) {
498        return VISUALIZER_ERROR_NO_INIT;
499    }
500
501    return translateError(lpVisualizer->setScalingMode(mode));
502}
503
504static jint
505android_media_visualizer_native_getScalingMode(JNIEnv *env, jobject thiz)
506{
507    Visualizer* lpVisualizer = getVisualizer(env, thiz);
508    if (lpVisualizer == NULL) {
509        return -1;
510    }
511    return lpVisualizer->getScalingMode();
512}
513
514static jint
515android_media_visualizer_native_getSamplingRate(JNIEnv *env, jobject thiz)
516{
517    Visualizer* lpVisualizer = getVisualizer(env, thiz);
518    if (lpVisualizer == NULL) {
519        return -1;
520    }
521    return lpVisualizer->getSamplingRate();
522}
523
524static jint
525android_media_visualizer_native_getWaveForm(JNIEnv *env, jobject thiz, jbyteArray jWaveform)
526{
527    Visualizer* lpVisualizer = getVisualizer(env, thiz);
528    if (lpVisualizer == NULL) {
529        return VISUALIZER_ERROR_NO_INIT;
530    }
531
532    jbyte* nWaveform = (jbyte *) env->GetPrimitiveArrayCritical(jWaveform, NULL);
533    if (nWaveform == NULL) {
534        return VISUALIZER_ERROR_NO_MEMORY;
535    }
536    jint status = translateError(lpVisualizer->getWaveForm((uint8_t *)nWaveform));
537
538    env->ReleasePrimitiveArrayCritical(jWaveform, nWaveform, 0);
539    return status;
540}
541
542static jint
543android_media_visualizer_native_getFft(JNIEnv *env, jobject thiz, jbyteArray jFft)
544{
545    Visualizer* lpVisualizer = getVisualizer(env, thiz);
546    if (lpVisualizer == NULL) {
547        return VISUALIZER_ERROR_NO_INIT;
548    }
549
550    jbyte* nFft = (jbyte *) env->GetPrimitiveArrayCritical(jFft, NULL);
551    if (nFft == NULL) {
552        return VISUALIZER_ERROR_NO_MEMORY;
553    }
554    jint status = translateError(lpVisualizer->getFft((uint8_t *)nFft));
555
556    env->ReleasePrimitiveArrayCritical(jFft, nFft, 0);
557
558    return status;
559}
560
561static jint
562android_media_setPeriodicCapture(JNIEnv *env, jobject thiz, jint rate, jboolean jWaveform, jboolean jFft)
563{
564    Visualizer* lpVisualizer = getVisualizer(env, thiz);
565    if (lpVisualizer == NULL) {
566        return VISUALIZER_ERROR_NO_INIT;
567    }
568    visualizerJniStorage* lpJniStorage = (visualizerJniStorage *)env->GetIntField(thiz,
569            fields.fidJniData);
570    if (lpJniStorage == NULL) {
571        return VISUALIZER_ERROR_NO_INIT;
572    }
573
574    ALOGV("setPeriodicCapture: rate %d, jWaveform %d jFft %d",
575            rate,
576            jWaveform,
577            jFft);
578
579    uint32_t flags = Visualizer::CAPTURE_CALL_JAVA;
580    if (jWaveform) flags |= Visualizer::CAPTURE_WAVEFORM;
581    if (jFft) flags |= Visualizer::CAPTURE_FFT;
582    Visualizer::capture_cbk_t cbk = captureCallback;
583    if (!jWaveform && !jFft) cbk = NULL;
584
585    return translateError(lpVisualizer->setCaptureCallBack(cbk,
586                                                &lpJniStorage->mCallbackData,
587                                                flags,
588                                                rate));
589}
590
591// ----------------------------------------------------------------------------
592
593// Dalvik VM type signatures
594static JNINativeMethod gMethods[] = {
595    {"native_init",            "()V",     (void *)android_media_visualizer_native_init},
596    {"native_setup",           "(Ljava/lang/Object;I[I)I",
597                                          (void *)android_media_visualizer_native_setup},
598    {"native_finalize",          "()V",   (void *)android_media_visualizer_native_finalize},
599    {"native_release",           "()V",   (void *)android_media_visualizer_native_release},
600    {"native_setEnabled",        "(Z)I",  (void *)android_media_visualizer_native_setEnabled},
601    {"native_getEnabled",        "()Z",   (void *)android_media_visualizer_native_getEnabled},
602    {"getCaptureSizeRange",      "()[I",  (void *)android_media_visualizer_native_getCaptureSizeRange},
603    {"getMaxCaptureRate",        "()I",   (void *)android_media_visualizer_native_getMaxCaptureRate},
604    {"native_setCaptureSize",    "(I)I",  (void *)android_media_visualizer_native_setCaptureSize},
605    {"native_getCaptureSize",    "()I",   (void *)android_media_visualizer_native_getCaptureSize},
606    {"native_setScalingMode",    "(I)I",  (void *)android_media_visualizer_native_setScalingMode},
607    {"native_getScalingMode",    "()I",   (void *)android_media_visualizer_native_getScalingMode},
608    {"native_getSamplingRate",   "()I",   (void *)android_media_visualizer_native_getSamplingRate},
609    {"native_getWaveForm",       "([B)I", (void *)android_media_visualizer_native_getWaveForm},
610    {"native_getFft",            "([B)I", (void *)android_media_visualizer_native_getFft},
611    {"native_setPeriodicCapture","(IZZ)I",(void *)android_media_setPeriodicCapture},
612};
613
614// ----------------------------------------------------------------------------
615
616int register_android_media_visualizer(JNIEnv *env)
617{
618    return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
619}
620
621