blob: a1a035110cafef64b750e26e5699f776361eb6b9 [file] [log] [blame]
Eric Laurent4bcdba82015-05-01 11:37:49 -07001/*
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>
Steven Moreland2279b252017-07-19 09:50:45 -070022#include <nativehelper/JNIHelp.h>
Eric Laurent4bcdba82015-05-01 11:37:49 -070023#include "core_jni_helpers.h"
24#include <media/AudioSystem.h>
25
26#include "android_media_DeviceCallback.h"
27
28
29// ----------------------------------------------------------------------------
30
31using namespace android;
32
33JNIDeviceCallback::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
52JNIDeviceCallback::~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
63void JNIDeviceCallback::onAudioDeviceUpdate(audio_io_handle_t audioIo,
64 audio_port_handle_t deviceId)
65{
66 JNIEnv *env = AndroidRuntime::getJNIEnv();
67 if (env == NULL) {
68 return;
69 }
70
71 ALOGV("%s audioIo %d deviceId %d", __FUNCTION__, audioIo, deviceId);
72 env->CallStaticVoidMethod(mClass,
73 mPostEventFromNative,
74 mObject,
75 AUDIO_NATIVE_EVENT_ROUTING_CHANGE, deviceId, 0, NULL);
76 if (env->ExceptionCheck()) {
77 ALOGW("An exception occurred while notifying an event.");
78 env->ExceptionClear();
79 }
80}
81