1 /* 2 * Copyright (C) 2015 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 //#define LOG_NDEBUG 0 18 19 #define LOG_TAG "AudioDeviceCallback-JNI" 20 21 #include <utils/Log.h> 22 #include <JNIHelp.h> 23 #include <JniConstants.h> 24 #include "core_jni_helpers.h" 25 #include <media/AudioSystem.h> 26 27 #include "android_media_DeviceCallback.h" 28 29 30 // ---------------------------------------------------------------------------- 31 32 using namespace android; 33 34 JNIDeviceCallback::JNIDeviceCallback(JNIEnv* env, jobject thiz, jobject weak_thiz, 35 jmethodID postEventFromNative) 36 { 37 38 // Hold onto the AudioTrack/AudioRecord class for use in calling the static method 39 // that posts events to the application thread. 40 jclass clazz = env->GetObjectClass(thiz); 41 if (clazz == NULL) { 42 return; 43 } 44 mClass = (jclass)env->NewGlobalRef(clazz); 45 46 // We use a weak reference so the AudioTrack/AudioRecord object can be garbage collected. 47 // The reference is only used as a proxy for callbacks. 48 mObject = env->NewGlobalRef(weak_thiz); 49 50 mPostEventFromNative = postEventFromNative; 51 } 52 53 JNIDeviceCallback::~JNIDeviceCallback() 54 { 55 // remove global references 56 JNIEnv *env = AndroidRuntime::getJNIEnv(); 57 if (env == NULL) { 58 return; 59 } 60 env->DeleteGlobalRef(mObject); 61 env->DeleteGlobalRef(mClass); 62 } 63 64 void JNIDeviceCallback::onAudioDeviceUpdate(audio_io_handle_t audioIo, 65 audio_port_handle_t deviceId) 66 { 67 JNIEnv *env = AndroidRuntime::getJNIEnv(); 68 if (env == NULL) { 69 return; 70 } 71 72 ALOGV("%s audioIo %d deviceId %d", __FUNCTION__, audioIo, deviceId); 73 env->CallStaticVoidMethod(mClass, 74 mPostEventFromNative, 75 mObject, 76 AUDIO_NATIVE_EVENT_ROUTING_CHANGE, deviceId, 0, NULL); 77 if (env->ExceptionCheck()) { 78 ALOGW("An exception occurred while notifying an event."); 79 env->ExceptionClear(); 80 } 81 } 82 83