xref: /aosp_15_r20/frameworks/base/core/jni/android_media_DeviceCallback.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
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 <nativehelper/JNIHelp.h>
23 #include "core_jni_helpers.h"
24 #include <media/AudioSystem.h>
25 
26 #include "android_media_DeviceCallback.h"
27 
28 
29 // ----------------------------------------------------------------------------
30 
31 using namespace android;
32 
JNIDeviceCallback(JNIEnv * env,jobject thiz,jobject weak_thiz,jmethodID postEventFromNative)33 JNIDeviceCallback::JNIDeviceCallback(JNIEnv* env, jobject thiz, jobject weak_thiz,
34                                      jmethodID postEventFromNative)
35 {
36 
37     // Hold onto the AudioTrack/AudioRecord class for use in calling the static method
38     // that posts events to the application thread.
39     jclass clazz = env->GetObjectClass(thiz);
40     if (clazz == NULL) {
41         return;
42     }
43     mClass = (jclass)env->NewGlobalRef(clazz);
44 
45     // We use a weak reference so the AudioTrack/AudioRecord object can be garbage collected.
46     // The reference is only used as a proxy for callbacks.
47     mObject  = env->NewGlobalRef(weak_thiz);
48 
49     mPostEventFromNative = postEventFromNative;
50 }
51 
~JNIDeviceCallback()52 JNIDeviceCallback::~JNIDeviceCallback()
53 {
54     // remove global references
55     JNIEnv *env = AndroidRuntime::getJNIEnv();
56     if (env == NULL) {
57         return;
58     }
59     env->DeleteGlobalRef(mObject);
60     env->DeleteGlobalRef(mClass);
61 }
62 
onAudioDeviceUpdate(audio_io_handle_t audioIo,const DeviceIdVector & deviceIds)63 void JNIDeviceCallback::onAudioDeviceUpdate(audio_io_handle_t audioIo,
64                                             const DeviceIdVector& deviceIds) {
65     JNIEnv *env = AndroidRuntime::getJNIEnv();
66     if (env == NULL) {
67         return;
68     }
69 
70     ALOGV("%s audioIo %d deviceIds %s", __FUNCTION__, audioIo, toString(deviceIds).c_str());
71     // Java should query the new device ids once it gets the event.
72     // TODO(b/378505346): Pass the deviceIds to Java to avoid race conditions.
73     env->CallStaticVoidMethod(mClass, mPostEventFromNative, mObject,
74                               AUDIO_NATIVE_EVENT_ROUTING_CHANGE, 0 /*arg1*/, 0 /*arg2*/,
75                               NULL /*obj*/);
76     if (env->ExceptionCheck()) {
77         ALOGW("An exception occurred while notifying an event.");
78         env->ExceptionClear();
79     }
80 }
81