Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 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> |
Steven Moreland | 889f995 | 2017-07-17 12:08:45 -0700 | [diff] [blame] | 23 | #include <jni.h> |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 24 | #include <nativehelper/JNIHelp.h> |
| 25 | #include <android_runtime/AndroidRuntime.h> |
John Grossman | 449725f | 2012-01-10 12:17:03 -0800 | [diff] [blame] | 26 | #include <utils/threads.h> |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 27 | #include "media/Visualizer.h" |
| 28 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 29 | #include <nativehelper/ScopedUtfChars.h> |
Svet Ganov | fa5ecdc | 2015-04-28 12:03:28 -0700 | [diff] [blame] | 30 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 31 | using namespace android; |
| 32 | |
| 33 | #define VISUALIZER_SUCCESS 0 |
Chih-Hung Hsieh | 0ca16ef | 2016-05-19 15:14:54 -0700 | [diff] [blame] | 34 | #define VISUALIZER_ERROR (-1) |
| 35 | #define VISUALIZER_ERROR_ALREADY_EXISTS (-2) |
| 36 | #define VISUALIZER_ERROR_NO_INIT (-3) |
| 37 | #define VISUALIZER_ERROR_BAD_VALUE (-4) |
| 38 | #define VISUALIZER_ERROR_INVALID_OPERATION (-5) |
| 39 | #define VISUALIZER_ERROR_NO_MEMORY (-6) |
| 40 | #define VISUALIZER_ERROR_DEAD_OBJECT (-7) |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 41 | |
| 42 | #define NATIVE_EVENT_PCM_CAPTURE 0 |
| 43 | #define NATIVE_EVENT_FFT_CAPTURE 1 |
John Grossman | 3540a01 | 2012-01-11 12:23:42 -0800 | [diff] [blame] | 44 | #define NATIVE_EVENT_SERVER_DIED 2 |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 45 | |
| 46 | // ---------------------------------------------------------------------------- |
Eric Laurent | 1a5149e | 2010-09-21 18:18:20 -0700 | [diff] [blame] | 47 | static const char* const kClassPathName = "android/media/audiofx/Visualizer"; |
Jean-Michel Trivi | badca26 | 2013-09-20 10:48:55 -0700 | [diff] [blame] | 48 | static const char* const kClassPeakRmsPathName = |
| 49 | "android/media/audiofx/Visualizer$MeasurementPeakRms"; |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 50 | |
| 51 | struct fields_t { |
| 52 | // these fields provide access from C++ to the... |
| 53 | jclass clazzEffect; // Visualizer class |
| 54 | jmethodID midPostNativeEvent; // event post callback method |
| 55 | jfieldID fidNativeVisualizer; // stores in Java the native Visualizer object |
| 56 | jfieldID fidJniData; // stores in Java additional resources used by the native Visualizer |
Jean-Michel Trivi | badca26 | 2013-09-20 10:48:55 -0700 | [diff] [blame] | 57 | jfieldID fidPeak; // to access Visualizer.MeasurementPeakRms.mPeak |
| 58 | jfieldID fidRms; // to access Visualizer.MeasurementPeakRms.mRms |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 59 | }; |
| 60 | static fields_t fields; |
| 61 | |
| 62 | struct visualizer_callback_cookie { |
| 63 | jclass visualizer_class; // Visualizer class |
| 64 | jobject visualizer_ref; // Visualizer object instance |
John Grossman | 449725f | 2012-01-10 12:17:03 -0800 | [diff] [blame] | 65 | |
| 66 | // Lazily allocated arrays used to hold callback data provided to java |
| 67 | // applications. These arrays are allocated during the first callback and |
| 68 | // reallocated when the size of the callback data changes. Allocating on |
| 69 | // demand and saving the arrays means that applications cannot safely hold a |
| 70 | // reference to the provided data (they need to make a copy if they want to |
| 71 | // hold onto outside of the callback scope), but it avoids GC thrash caused |
| 72 | // by constantly allocating and releasing arrays to hold callback data. |
| 73 | Mutex callback_data_lock; |
| 74 | jbyteArray waveform_data; |
| 75 | jbyteArray fft_data; |
| 76 | |
| 77 | visualizer_callback_cookie() { |
| 78 | waveform_data = NULL; |
| 79 | fft_data = NULL; |
| 80 | } |
| 81 | |
| 82 | ~visualizer_callback_cookie() { |
| 83 | cleanupBuffers(); |
| 84 | } |
| 85 | |
| 86 | void cleanupBuffers() { |
| 87 | AutoMutex lock(&callback_data_lock); |
| 88 | if (waveform_data || fft_data) { |
| 89 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 90 | |
| 91 | if (waveform_data) { |
| 92 | env->DeleteGlobalRef(waveform_data); |
| 93 | waveform_data = NULL; |
| 94 | } |
| 95 | |
| 96 | if (fft_data) { |
| 97 | env->DeleteGlobalRef(fft_data); |
| 98 | fft_data = NULL; |
| 99 | } |
| 100 | } |
| 101 | } |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | // ---------------------------------------------------------------------------- |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 105 | class VisualizerJniStorage { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 106 | public: |
| 107 | visualizer_callback_cookie mCallbackData; |
| 108 | |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 109 | VisualizerJniStorage() { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 112 | ~VisualizerJniStorage() { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 113 | } |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | |
| 117 | static jint translateError(int code) { |
| 118 | switch(code) { |
| 119 | case NO_ERROR: |
| 120 | return VISUALIZER_SUCCESS; |
| 121 | case ALREADY_EXISTS: |
| 122 | return VISUALIZER_ERROR_ALREADY_EXISTS; |
| 123 | case NO_INIT: |
| 124 | return VISUALIZER_ERROR_NO_INIT; |
| 125 | case BAD_VALUE: |
| 126 | return VISUALIZER_ERROR_BAD_VALUE; |
| 127 | case INVALID_OPERATION: |
| 128 | return VISUALIZER_ERROR_INVALID_OPERATION; |
| 129 | case NO_MEMORY: |
| 130 | return VISUALIZER_ERROR_NO_MEMORY; |
| 131 | case DEAD_OBJECT: |
| 132 | return VISUALIZER_ERROR_DEAD_OBJECT; |
| 133 | default: |
| 134 | return VISUALIZER_ERROR; |
| 135 | } |
| 136 | } |
| 137 | |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 138 | static Mutex sLock; |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 139 | |
| 140 | // ---------------------------------------------------------------------------- |
John Grossman | 449725f | 2012-01-10 12:17:03 -0800 | [diff] [blame] | 141 | static void ensureArraySize(JNIEnv *env, jbyteArray *array, uint32_t size) { |
| 142 | if (NULL != *array) { |
| 143 | uint32_t len = env->GetArrayLength(*array); |
| 144 | if (len == size) |
| 145 | return; |
| 146 | |
| 147 | env->DeleteGlobalRef(*array); |
| 148 | *array = NULL; |
| 149 | } |
| 150 | |
| 151 | jbyteArray localRef = env->NewByteArray(size); |
| 152 | if (NULL != localRef) { |
| 153 | // Promote to global ref. |
| 154 | *array = (jbyteArray)env->NewGlobalRef(localRef); |
| 155 | |
| 156 | // Release our (now pointless) local ref. |
| 157 | env->DeleteLocalRef(localRef); |
| 158 | } |
| 159 | } |
| 160 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 161 | static void captureCallback(void* user, |
| 162 | uint32_t waveformSize, |
| 163 | uint8_t *waveform, |
| 164 | uint32_t fftSize, |
| 165 | uint8_t *fft, |
| 166 | uint32_t samplingrate) { |
| 167 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 168 | visualizer_callback_cookie *callbackInfo = (visualizer_callback_cookie *)user; |
| 169 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Mike J. Chen | 67a1218 | 2013-04-26 12:34:52 -0700 | [diff] [blame] | 170 | |
| 171 | if (!user || !env) { |
| 172 | ALOGW("captureCallback error user %p, env %p", user, env); |
| 173 | return; |
| 174 | } |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 175 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 176 | ALOGV("captureCallback: callbackInfo %p, visualizer_ref %p visualizer_class %p", |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 177 | callbackInfo, |
| 178 | callbackInfo->visualizer_ref, |
| 179 | callbackInfo->visualizer_class); |
| 180 | |
Mike J. Chen | 67a1218 | 2013-04-26 12:34:52 -0700 | [diff] [blame] | 181 | AutoMutex lock(&callbackInfo->callback_data_lock); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 182 | |
| 183 | if (waveformSize != 0 && waveform != NULL) { |
John Grossman | 449725f | 2012-01-10 12:17:03 -0800 | [diff] [blame] | 184 | jbyteArray jArray; |
| 185 | |
| 186 | ensureArraySize(env, &callbackInfo->waveform_data, waveformSize); |
| 187 | jArray = callbackInfo->waveform_data; |
| 188 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 189 | if (jArray != NULL) { |
| 190 | jbyte *nArray = env->GetByteArrayElements(jArray, NULL); |
| 191 | memcpy(nArray, waveform, waveformSize); |
| 192 | env->ReleaseByteArrayElements(jArray, nArray, 0); |
| 193 | env->CallStaticVoidMethod( |
| 194 | callbackInfo->visualizer_class, |
| 195 | fields.midPostNativeEvent, |
| 196 | callbackInfo->visualizer_ref, |
| 197 | NATIVE_EVENT_PCM_CAPTURE, |
| 198 | samplingrate, |
| 199 | 0, |
| 200 | jArray); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if (fftSize != 0 && fft != NULL) { |
John Grossman | 449725f | 2012-01-10 12:17:03 -0800 | [diff] [blame] | 205 | jbyteArray jArray; |
| 206 | |
| 207 | ensureArraySize(env, &callbackInfo->fft_data, fftSize); |
| 208 | jArray = callbackInfo->fft_data; |
| 209 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 210 | if (jArray != NULL) { |
| 211 | jbyte *nArray = env->GetByteArrayElements(jArray, NULL); |
| 212 | memcpy(nArray, fft, fftSize); |
| 213 | env->ReleaseByteArrayElements(jArray, nArray, 0); |
| 214 | env->CallStaticVoidMethod( |
| 215 | callbackInfo->visualizer_class, |
| 216 | fields.midPostNativeEvent, |
| 217 | callbackInfo->visualizer_ref, |
| 218 | NATIVE_EVENT_FFT_CAPTURE, |
| 219 | samplingrate, |
| 220 | 0, |
| 221 | jArray); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | if (env->ExceptionCheck()) { |
| 226 | env->ExceptionDescribe(); |
| 227 | env->ExceptionClear(); |
| 228 | } |
| 229 | } |
| 230 | |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 231 | // ---------------------------------------------------------------------------- |
| 232 | |
| 233 | static sp<Visualizer> getVisualizer(JNIEnv* env, jobject thiz) |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 234 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 235 | Mutex::Autolock l(sLock); |
| 236 | Visualizer* const v = |
| 237 | (Visualizer*)env->GetLongField(thiz, fields.fidNativeVisualizer); |
| 238 | return sp<Visualizer>(v); |
| 239 | } |
| 240 | |
| 241 | static sp<Visualizer> setVisualizer(JNIEnv* env, jobject thiz, |
| 242 | const sp<Visualizer>& v) |
| 243 | { |
| 244 | Mutex::Autolock l(sLock); |
| 245 | sp<Visualizer> old = |
| 246 | (Visualizer*)env->GetLongField(thiz, fields.fidNativeVisualizer); |
| 247 | if (v.get()) { |
| 248 | v->incStrong((void*)setVisualizer); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 249 | } |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 250 | if (old != 0) { |
| 251 | old->decStrong((void*)setVisualizer); |
| 252 | } |
| 253 | env->SetLongField(thiz, fields.fidNativeVisualizer, (jlong)v.get()); |
| 254 | return old; |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // ---------------------------------------------------------------------------- |
| 258 | // This function gets some field IDs, which in turn causes class initialization. |
| 259 | // It is called from a static block in Visualizer, which won't run until the |
| 260 | // first time an instance of this class is used. |
| 261 | static void |
| 262 | android_media_visualizer_native_init(JNIEnv *env) |
| 263 | { |
| 264 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 265 | ALOGV("android_media_visualizer_native_init"); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 266 | |
| 267 | fields.clazzEffect = NULL; |
| 268 | |
| 269 | // Get the Visualizer class |
| 270 | jclass clazz = env->FindClass(kClassPathName); |
| 271 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 272 | ALOGE("Can't find %s", kClassPathName); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 273 | return; |
| 274 | } |
| 275 | |
| 276 | fields.clazzEffect = (jclass)env->NewGlobalRef(clazz); |
| 277 | |
Jean-Michel Trivi | badca26 | 2013-09-20 10:48:55 -0700 | [diff] [blame] | 278 | // Get the Visualizer.MeasurementPeakRms class |
| 279 | clazz = env->FindClass(kClassPeakRmsPathName); |
| 280 | if (clazz == NULL) { |
| 281 | ALOGE("Can't find %s", kClassPeakRmsPathName); |
| 282 | return; |
| 283 | } |
| 284 | jclass clazzMeasurementPeakRms = (jclass)env->NewGlobalRef(clazz); |
| 285 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 286 | // Get the postEvent method |
| 287 | fields.midPostNativeEvent = env->GetStaticMethodID( |
| 288 | fields.clazzEffect, |
| 289 | "postEventFromNative", "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
| 290 | if (fields.midPostNativeEvent == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 291 | ALOGE("Can't find Visualizer.%s", "postEventFromNative"); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | |
| 295 | // Get the variables fields |
| 296 | // nativeTrackInJavaObj |
| 297 | fields.fidNativeVisualizer = env->GetFieldID( |
| 298 | fields.clazzEffect, |
Ashok Bhat | ea7861c | 2013-12-17 12:42:00 +0000 | [diff] [blame] | 299 | "mNativeVisualizer", "J"); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 300 | if (fields.fidNativeVisualizer == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 301 | ALOGE("Can't find Visualizer.%s", "mNativeVisualizer"); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 302 | return; |
| 303 | } |
| 304 | // fidJniData; |
| 305 | fields.fidJniData = env->GetFieldID( |
| 306 | fields.clazzEffect, |
Ashok Bhat | ea7861c | 2013-12-17 12:42:00 +0000 | [diff] [blame] | 307 | "mJniData", "J"); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 308 | if (fields.fidJniData == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 309 | ALOGE("Can't find Visualizer.%s", "mJniData"); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 310 | return; |
| 311 | } |
Jean-Michel Trivi | badca26 | 2013-09-20 10:48:55 -0700 | [diff] [blame] | 312 | // fidPeak |
| 313 | fields.fidPeak = env->GetFieldID( |
| 314 | clazzMeasurementPeakRms, |
| 315 | "mPeak", "I"); |
| 316 | if (fields.fidPeak == NULL) { |
| 317 | ALOGE("Can't find Visualizer.MeasurementPeakRms.%s", "mPeak"); |
| 318 | return; |
| 319 | } |
| 320 | // fidRms |
| 321 | fields.fidRms = env->GetFieldID( |
| 322 | clazzMeasurementPeakRms, |
| 323 | "mRms", "I"); |
| 324 | if (fields.fidRms == NULL) { |
| 325 | ALOGE("Can't find Visualizer.MeasurementPeakRms.%s", "mPeak"); |
| 326 | return; |
| 327 | } |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 328 | |
Jean-Michel Trivi | badca26 | 2013-09-20 10:48:55 -0700 | [diff] [blame] | 329 | env->DeleteGlobalRef(clazzMeasurementPeakRms); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 330 | } |
| 331 | |
John Grossman | 3540a01 | 2012-01-11 12:23:42 -0800 | [diff] [blame] | 332 | static void android_media_visualizer_effect_callback(int32_t event, |
| 333 | void *user, |
| 334 | void *info) { |
| 335 | if ((event == AudioEffect::EVENT_ERROR) && |
| 336 | (*((status_t*)info) == DEAD_OBJECT)) { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 337 | VisualizerJniStorage* lpJniStorage = (VisualizerJniStorage*)user; |
John Grossman | 3540a01 | 2012-01-11 12:23:42 -0800 | [diff] [blame] | 338 | visualizer_callback_cookie* callbackInfo = &lpJniStorage->mCallbackData; |
| 339 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 340 | |
| 341 | env->CallStaticVoidMethod( |
| 342 | callbackInfo->visualizer_class, |
| 343 | fields.midPostNativeEvent, |
| 344 | callbackInfo->visualizer_ref, |
| 345 | NATIVE_EVENT_SERVER_DIED, |
Haitao Feng | 8bd6a20 | 2014-12-31 13:48:08 +0800 | [diff] [blame] | 346 | 0, 0, NULL); |
John Grossman | 3540a01 | 2012-01-11 12:23:42 -0800 | [diff] [blame] | 347 | } |
| 348 | } |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 349 | |
| 350 | static jint |
| 351 | android_media_visualizer_native_setup(JNIEnv *env, jobject thiz, jobject weak_this, |
Svet Ganov | fa5ecdc | 2015-04-28 12:03:28 -0700 | [diff] [blame] | 352 | jint sessionId, jintArray jId, jstring opPackageName) |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 353 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 354 | ALOGV("android_media_visualizer_native_setup"); |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 355 | VisualizerJniStorage* lpJniStorage = NULL; |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 356 | int lStatus = VISUALIZER_ERROR_NO_MEMORY; |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 357 | sp<Visualizer> lpVisualizer; |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 358 | jint* nId = NULL; |
| 359 | |
Svet Ganov | fa5ecdc | 2015-04-28 12:03:28 -0700 | [diff] [blame] | 360 | ScopedUtfChars opPackageNameStr(env, opPackageName); |
| 361 | |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 362 | setVisualizer(env, thiz, 0); |
| 363 | |
| 364 | lpJniStorage = new VisualizerJniStorage(); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 365 | if (lpJniStorage == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 366 | ALOGE("setup: Error creating JNI Storage"); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 367 | goto setup_failure; |
| 368 | } |
| 369 | |
| 370 | lpJniStorage->mCallbackData.visualizer_class = (jclass)env->NewGlobalRef(fields.clazzEffect); |
| 371 | // we use a weak reference so the Visualizer object can be garbage collected. |
| 372 | lpJniStorage->mCallbackData.visualizer_ref = env->NewGlobalRef(weak_this); |
| 373 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 374 | ALOGV("setup: lpJniStorage: %p visualizer_ref %p visualizer_class %p, &mCallbackData %p", |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 375 | lpJniStorage, |
| 376 | lpJniStorage->mCallbackData.visualizer_ref, |
| 377 | lpJniStorage->mCallbackData.visualizer_class, |
| 378 | &lpJniStorage->mCallbackData); |
| 379 | |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 380 | if (jId == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 381 | ALOGE("setup: NULL java array for id pointer"); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 382 | lStatus = VISUALIZER_ERROR_BAD_VALUE; |
| 383 | goto setup_failure; |
| 384 | } |
| 385 | |
| 386 | // create the native Visualizer object |
Svet Ganov | fa5ecdc | 2015-04-28 12:03:28 -0700 | [diff] [blame] | 387 | lpVisualizer = new Visualizer(String16(opPackageNameStr.c_str()), |
| 388 | 0, |
John Grossman | 3540a01 | 2012-01-11 12:23:42 -0800 | [diff] [blame] | 389 | android_media_visualizer_effect_callback, |
| 390 | lpJniStorage, |
Glenn Kasten | 33b8404 | 2016-03-08 12:02:55 -0800 | [diff] [blame] | 391 | (audio_session_t) sessionId); |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 392 | if (lpVisualizer == 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 393 | ALOGE("Error creating Visualizer"); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 394 | goto setup_failure; |
| 395 | } |
| 396 | |
| 397 | lStatus = translateError(lpVisualizer->initCheck()); |
| 398 | if (lStatus != VISUALIZER_SUCCESS && lStatus != VISUALIZER_ERROR_ALREADY_EXISTS) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 399 | ALOGE("Visualizer initCheck failed %d", lStatus); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 400 | goto setup_failure; |
| 401 | } |
| 402 | |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 403 | nId = (jint *) env->GetPrimitiveArrayCritical(jId, NULL); |
| 404 | if (nId == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 405 | ALOGE("setup: Error retrieving id pointer"); |
Eric Laurent | 2fb43ef | 2010-09-24 12:03:36 -0700 | [diff] [blame] | 406 | lStatus = VISUALIZER_ERROR_BAD_VALUE; |
| 407 | goto setup_failure; |
| 408 | } |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 409 | nId[0] = lpVisualizer->id(); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 410 | env->ReleasePrimitiveArrayCritical(jId, nId, 0); |
| 411 | nId = NULL; |
| 412 | |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 413 | setVisualizer(env, thiz, lpVisualizer); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 414 | |
Ashok Bhat | ea7861c | 2013-12-17 12:42:00 +0000 | [diff] [blame] | 415 | env->SetLongField(thiz, fields.fidJniData, (jlong)lpJniStorage); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 416 | |
| 417 | return VISUALIZER_SUCCESS; |
| 418 | |
| 419 | // failures: |
| 420 | setup_failure: |
| 421 | |
| 422 | if (nId != NULL) { |
| 423 | env->ReleasePrimitiveArrayCritical(jId, nId, 0); |
| 424 | } |
| 425 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 426 | if (lpJniStorage) { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 427 | env->DeleteGlobalRef(lpJniStorage->mCallbackData.visualizer_class); |
| 428 | env->DeleteGlobalRef(lpJniStorage->mCallbackData.visualizer_ref); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 429 | delete lpJniStorage; |
| 430 | } |
Ashok Bhat | ea7861c | 2013-12-17 12:42:00 +0000 | [diff] [blame] | 431 | env->SetLongField(thiz, fields.fidJniData, 0); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 432 | |
Ashok Bhat | ea7861c | 2013-12-17 12:42:00 +0000 | [diff] [blame] | 433 | return (jint) lStatus; |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | // ---------------------------------------------------------------------------- |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 437 | static void android_media_visualizer_native_release(JNIEnv *env, jobject thiz) { |
rago | 8dc9afa | 2016-09-14 17:33:45 -0700 | [diff] [blame] | 438 | { //limit scope so that lpVisualizer is deleted before JNI storage data. |
ganxiaolin | 7b9f5be | 2016-06-06 11:09:05 +0800 | [diff] [blame] | 439 | sp<Visualizer> lpVisualizer = setVisualizer(env, thiz, 0); |
| 440 | if (lpVisualizer == 0) { |
| 441 | return; |
| 442 | } |
zengjing | 5a56b61 | 2018-09-29 13:19:15 +0800 | [diff] [blame] | 443 | lpVisualizer->release(); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 444 | } |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 445 | // delete the JNI data |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 446 | VisualizerJniStorage* lpJniStorage = |
| 447 | (VisualizerJniStorage *)env->GetLongField(thiz, fields.fidJniData); |
| 448 | |
| 449 | // reset the native resources in the Java object so any attempt to access |
| 450 | // them after a call to release fails. |
| 451 | env->SetLongField(thiz, fields.fidJniData, 0); |
| 452 | |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 453 | if (lpJniStorage) { |
Ashok Bhat | ea7861c | 2013-12-17 12:42:00 +0000 | [diff] [blame] | 454 | ALOGV("deleting pJniStorage: %p\n", lpJniStorage); |
Eric Laurent | 0779998 | 2015-06-19 09:13:02 -0700 | [diff] [blame] | 455 | env->DeleteGlobalRef(lpJniStorage->mCallbackData.visualizer_class); |
| 456 | env->DeleteGlobalRef(lpJniStorage->mCallbackData.visualizer_ref); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 457 | delete lpJniStorage; |
| 458 | } |
| 459 | } |
| 460 | |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 461 | static void android_media_visualizer_native_finalize(JNIEnv *env, jobject thiz) { |
| 462 | ALOGV("android_media_visualizer_native_finalize jobject: %p\n", thiz); |
| 463 | android_media_visualizer_native_release(env, thiz); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 466 | // ---------------------------------------------------------------------------- |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 467 | static jint |
| 468 | android_media_visualizer_native_setEnabled(JNIEnv *env, jobject thiz, jboolean enabled) |
| 469 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 470 | sp<Visualizer> lpVisualizer = getVisualizer(env, thiz); |
| 471 | if (lpVisualizer == 0) { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 472 | return VISUALIZER_ERROR_NO_INIT; |
| 473 | } |
| 474 | |
John Grossman | 449725f | 2012-01-10 12:17:03 -0800 | [diff] [blame] | 475 | jint retVal = translateError(lpVisualizer->setEnabled(enabled)); |
| 476 | |
| 477 | if (!enabled) { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 478 | VisualizerJniStorage* lpJniStorage = (VisualizerJniStorage *)env->GetLongField( |
John Grossman | 449725f | 2012-01-10 12:17:03 -0800 | [diff] [blame] | 479 | thiz, fields.fidJniData); |
| 480 | |
| 481 | if (NULL != lpJniStorage) |
| 482 | lpJniStorage->mCallbackData.cleanupBuffers(); |
| 483 | } |
| 484 | |
| 485 | return retVal; |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | static jboolean |
| 489 | android_media_visualizer_native_getEnabled(JNIEnv *env, jobject thiz) |
| 490 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 491 | sp<Visualizer> lpVisualizer = getVisualizer(env, thiz); |
| 492 | if (lpVisualizer == 0) { |
Ashok Bhat | ea7861c | 2013-12-17 12:42:00 +0000 | [diff] [blame] | 493 | return JNI_FALSE; |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Ashok Bhat | ea7861c | 2013-12-17 12:42:00 +0000 | [diff] [blame] | 496 | if (lpVisualizer->getEnabled()) { |
| 497 | return JNI_TRUE; |
| 498 | } else { |
| 499 | return JNI_FALSE; |
| 500 | } |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static jintArray |
Andreas Gampe | 5a15d0d | 2014-11-10 18:19:40 -0800 | [diff] [blame] | 504 | android_media_visualizer_native_getCaptureSizeRange(JNIEnv *env, jobject /* thiz */) |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 505 | { |
| 506 | jintArray jRange = env->NewIntArray(2); |
| 507 | jint *nRange = env->GetIntArrayElements(jRange, NULL); |
| 508 | nRange[0] = Visualizer::getMinCaptureSize(); |
| 509 | nRange[1] = Visualizer::getMaxCaptureSize(); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 510 | ALOGV("getCaptureSizeRange() min %d max %d", nRange[0], nRange[1]); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 511 | env->ReleaseIntArrayElements(jRange, nRange, 0); |
| 512 | return jRange; |
| 513 | } |
| 514 | |
| 515 | static jint |
Andreas Gampe | 5a15d0d | 2014-11-10 18:19:40 -0800 | [diff] [blame] | 516 | android_media_visualizer_native_getMaxCaptureRate(JNIEnv* /* env */, jobject /* thiz */) |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 517 | { |
Ashok Bhat | ea7861c | 2013-12-17 12:42:00 +0000 | [diff] [blame] | 518 | return (jint) Visualizer::getMaxCaptureRate(); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static jint |
| 522 | android_media_visualizer_native_setCaptureSize(JNIEnv *env, jobject thiz, jint size) |
| 523 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 524 | sp<Visualizer> lpVisualizer = getVisualizer(env, thiz); |
| 525 | if (lpVisualizer == 0) { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 526 | return VISUALIZER_ERROR_NO_INIT; |
| 527 | } |
| 528 | |
| 529 | return translateError(lpVisualizer->setCaptureSize(size)); |
| 530 | } |
| 531 | |
| 532 | static jint |
| 533 | android_media_visualizer_native_getCaptureSize(JNIEnv *env, jobject thiz) |
| 534 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 535 | sp<Visualizer> lpVisualizer = getVisualizer(env, thiz); |
| 536 | if (lpVisualizer == 0) { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 537 | return -1; |
| 538 | } |
Ashok Bhat | ea7861c | 2013-12-17 12:42:00 +0000 | [diff] [blame] | 539 | return (jint) lpVisualizer->getCaptureSize(); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static jint |
Jean-Michel Trivi | e1123e7 | 2012-04-15 17:22:01 -0700 | [diff] [blame] | 543 | android_media_visualizer_native_setScalingMode(JNIEnv *env, jobject thiz, jint mode) |
| 544 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 545 | sp<Visualizer> lpVisualizer = getVisualizer(env, thiz); |
| 546 | if (lpVisualizer == 0) { |
Jean-Michel Trivi | e1123e7 | 2012-04-15 17:22:01 -0700 | [diff] [blame] | 547 | return VISUALIZER_ERROR_NO_INIT; |
| 548 | } |
| 549 | |
| 550 | return translateError(lpVisualizer->setScalingMode(mode)); |
| 551 | } |
| 552 | |
| 553 | static jint |
| 554 | android_media_visualizer_native_getScalingMode(JNIEnv *env, jobject thiz) |
| 555 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 556 | sp<Visualizer> lpVisualizer = getVisualizer(env, thiz); |
| 557 | if (lpVisualizer == 0) { |
Jean-Michel Trivi | e1123e7 | 2012-04-15 17:22:01 -0700 | [diff] [blame] | 558 | return -1; |
| 559 | } |
Ashok Bhat | ea7861c | 2013-12-17 12:42:00 +0000 | [diff] [blame] | 560 | return (jint)lpVisualizer->getScalingMode(); |
Jean-Michel Trivi | e1123e7 | 2012-04-15 17:22:01 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static jint |
Jean-Michel Trivi | badca26 | 2013-09-20 10:48:55 -0700 | [diff] [blame] | 564 | android_media_visualizer_native_setMeasurementMode(JNIEnv *env, jobject thiz, jint mode) |
| 565 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 566 | sp<Visualizer> lpVisualizer = getVisualizer(env, thiz); |
| 567 | if (lpVisualizer == 0) { |
Jean-Michel Trivi | badca26 | 2013-09-20 10:48:55 -0700 | [diff] [blame] | 568 | return VISUALIZER_ERROR_NO_INIT; |
| 569 | } |
| 570 | return translateError(lpVisualizer->setMeasurementMode(mode)); |
| 571 | } |
| 572 | |
| 573 | static jint |
| 574 | android_media_visualizer_native_getMeasurementMode(JNIEnv *env, jobject thiz) |
| 575 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 576 | sp<Visualizer> lpVisualizer = getVisualizer(env, thiz); |
| 577 | if (lpVisualizer == 0) { |
Jean-Michel Trivi | badca26 | 2013-09-20 10:48:55 -0700 | [diff] [blame] | 578 | return MEASUREMENT_MODE_NONE; |
| 579 | } |
| 580 | return lpVisualizer->getMeasurementMode(); |
| 581 | } |
| 582 | |
| 583 | static jint |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 584 | android_media_visualizer_native_getSamplingRate(JNIEnv *env, jobject thiz) |
| 585 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 586 | sp<Visualizer> lpVisualizer = getVisualizer(env, thiz); |
| 587 | if (lpVisualizer == 0) { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 588 | return -1; |
| 589 | } |
Ashok Bhat | ea7861c | 2013-12-17 12:42:00 +0000 | [diff] [blame] | 590 | return (jint) lpVisualizer->getSamplingRate(); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | static jint |
| 594 | android_media_visualizer_native_getWaveForm(JNIEnv *env, jobject thiz, jbyteArray jWaveform) |
| 595 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 596 | sp<Visualizer> lpVisualizer = getVisualizer(env, thiz); |
| 597 | if (lpVisualizer == 0) { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 598 | return VISUALIZER_ERROR_NO_INIT; |
| 599 | } |
| 600 | |
| 601 | jbyte* nWaveform = (jbyte *) env->GetPrimitiveArrayCritical(jWaveform, NULL); |
| 602 | if (nWaveform == NULL) { |
| 603 | return VISUALIZER_ERROR_NO_MEMORY; |
| 604 | } |
| 605 | jint status = translateError(lpVisualizer->getWaveForm((uint8_t *)nWaveform)); |
| 606 | |
| 607 | env->ReleasePrimitiveArrayCritical(jWaveform, nWaveform, 0); |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 608 | return status; |
| 609 | } |
| 610 | |
| 611 | static jint |
| 612 | android_media_visualizer_native_getFft(JNIEnv *env, jobject thiz, jbyteArray jFft) |
| 613 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 614 | sp<Visualizer> lpVisualizer = getVisualizer(env, thiz); |
| 615 | if (lpVisualizer == 0) { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 616 | return VISUALIZER_ERROR_NO_INIT; |
| 617 | } |
| 618 | |
| 619 | jbyte* nFft = (jbyte *) env->GetPrimitiveArrayCritical(jFft, NULL); |
| 620 | if (nFft == NULL) { |
| 621 | return VISUALIZER_ERROR_NO_MEMORY; |
| 622 | } |
| 623 | jint status = translateError(lpVisualizer->getFft((uint8_t *)nFft)); |
| 624 | |
| 625 | env->ReleasePrimitiveArrayCritical(jFft, nFft, 0); |
| 626 | |
| 627 | return status; |
| 628 | } |
| 629 | |
| 630 | static jint |
Jean-Michel Trivi | badca26 | 2013-09-20 10:48:55 -0700 | [diff] [blame] | 631 | android_media_visualizer_native_getPeakRms(JNIEnv *env, jobject thiz, jobject jPeakRmsObj) |
| 632 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 633 | sp<Visualizer> lpVisualizer = getVisualizer(env, thiz); |
| 634 | if (lpVisualizer == 0) { |
Jean-Michel Trivi | badca26 | 2013-09-20 10:48:55 -0700 | [diff] [blame] | 635 | return VISUALIZER_ERROR_NO_INIT; |
| 636 | } |
| 637 | int32_t measurements[2]; |
| 638 | jint status = translateError( |
| 639 | lpVisualizer->getIntMeasurements(MEASUREMENT_MODE_PEAK_RMS, |
| 640 | 2, measurements)); |
| 641 | if (status == VISUALIZER_SUCCESS) { |
| 642 | // measurement worked, write the values to the java object |
| 643 | env->SetIntField(jPeakRmsObj, fields.fidPeak, measurements[MEASUREMENT_IDX_PEAK]); |
| 644 | env->SetIntField(jPeakRmsObj, fields.fidRms, measurements[MEASUREMENT_IDX_RMS]); |
| 645 | } |
| 646 | return status; |
| 647 | } |
| 648 | |
| 649 | static jint |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 650 | android_media_setPeriodicCapture(JNIEnv *env, jobject thiz, jint rate, jboolean jWaveform, jboolean jFft) |
| 651 | { |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 652 | sp<Visualizer> lpVisualizer = getVisualizer(env, thiz); |
| 653 | if (lpVisualizer == 0) { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 654 | return VISUALIZER_ERROR_NO_INIT; |
| 655 | } |
Eric Laurent | 76f8133 | 2015-06-04 16:20:47 -0700 | [diff] [blame] | 656 | VisualizerJniStorage* lpJniStorage = (VisualizerJniStorage *)env->GetLongField(thiz, |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 657 | fields.fidJniData); |
| 658 | if (lpJniStorage == NULL) { |
| 659 | return VISUALIZER_ERROR_NO_INIT; |
| 660 | } |
| 661 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 662 | ALOGV("setPeriodicCapture: rate %d, jWaveform %d jFft %d", |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 663 | rate, |
| 664 | jWaveform, |
| 665 | jFft); |
| 666 | |
| 667 | uint32_t flags = Visualizer::CAPTURE_CALL_JAVA; |
| 668 | if (jWaveform) flags |= Visualizer::CAPTURE_WAVEFORM; |
| 669 | if (jFft) flags |= Visualizer::CAPTURE_FFT; |
| 670 | Visualizer::capture_cbk_t cbk = captureCallback; |
| 671 | if (!jWaveform && !jFft) cbk = NULL; |
| 672 | |
| 673 | return translateError(lpVisualizer->setCaptureCallBack(cbk, |
| 674 | &lpJniStorage->mCallbackData, |
| 675 | flags, |
| 676 | rate)); |
| 677 | } |
| 678 | |
| 679 | // ---------------------------------------------------------------------------- |
| 680 | |
| 681 | // Dalvik VM type signatures |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 682 | static const JNINativeMethod gMethods[] = { |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 683 | {"native_init", "()V", (void *)android_media_visualizer_native_init}, |
Svet Ganov | fa5ecdc | 2015-04-28 12:03:28 -0700 | [diff] [blame] | 684 | {"native_setup", "(Ljava/lang/Object;I[ILjava/lang/String;)I", |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 685 | (void *)android_media_visualizer_native_setup}, |
| 686 | {"native_finalize", "()V", (void *)android_media_visualizer_native_finalize}, |
| 687 | {"native_release", "()V", (void *)android_media_visualizer_native_release}, |
| 688 | {"native_setEnabled", "(Z)I", (void *)android_media_visualizer_native_setEnabled}, |
| 689 | {"native_getEnabled", "()Z", (void *)android_media_visualizer_native_getEnabled}, |
| 690 | {"getCaptureSizeRange", "()[I", (void *)android_media_visualizer_native_getCaptureSizeRange}, |
| 691 | {"getMaxCaptureRate", "()I", (void *)android_media_visualizer_native_getMaxCaptureRate}, |
| 692 | {"native_setCaptureSize", "(I)I", (void *)android_media_visualizer_native_setCaptureSize}, |
| 693 | {"native_getCaptureSize", "()I", (void *)android_media_visualizer_native_getCaptureSize}, |
Jean-Michel Trivi | e1123e7 | 2012-04-15 17:22:01 -0700 | [diff] [blame] | 694 | {"native_setScalingMode", "(I)I", (void *)android_media_visualizer_native_setScalingMode}, |
| 695 | {"native_getScalingMode", "()I", (void *)android_media_visualizer_native_getScalingMode}, |
Jean-Michel Trivi | badca26 | 2013-09-20 10:48:55 -0700 | [diff] [blame] | 696 | {"native_setMeasurementMode","(I)I", (void *)android_media_visualizer_native_setMeasurementMode}, |
| 697 | {"native_getMeasurementMode","()I", (void *)android_media_visualizer_native_getMeasurementMode}, |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 698 | {"native_getSamplingRate", "()I", (void *)android_media_visualizer_native_getSamplingRate}, |
| 699 | {"native_getWaveForm", "([B)I", (void *)android_media_visualizer_native_getWaveForm}, |
| 700 | {"native_getFft", "([B)I", (void *)android_media_visualizer_native_getFft}, |
Jean-Michel Trivi | badca26 | 2013-09-20 10:48:55 -0700 | [diff] [blame] | 701 | {"native_getPeakRms", "(Landroid/media/audiofx/Visualizer$MeasurementPeakRms;)I", |
| 702 | (void *)android_media_visualizer_native_getPeakRms}, |
Eric Laurent | df9b81c | 2010-07-02 08:12:41 -0700 | [diff] [blame] | 703 | {"native_setPeriodicCapture","(IZZ)I",(void *)android_media_setPeriodicCapture}, |
| 704 | }; |
| 705 | |
| 706 | // ---------------------------------------------------------------------------- |
| 707 | |
| 708 | int register_android_media_visualizer(JNIEnv *env) |
| 709 | { |
| 710 | return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 711 | } |
| 712 | |