1 /*
2 * Copyright 2020 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 #include <jni.h>
17
18 #include <android/log.h>
19
20 #include "AppCallbackAudioSink.h"
21
22 static const char * const TAG = "AppCallbackAudioSink";
23
AppCallbackAudioSink(JNIEnv * env,jobject callbackObj)24 AppCallbackAudioSink::AppCallbackAudioSink(JNIEnv *env, jobject callbackObj) {
25 jint rs = env->GetJavaVM(&mJVM);
26 mCallbackObj = env->NewGlobalRef(callbackObj);
27
28 jclass callbackClass = env->GetObjectClass(mCallbackObj);
29 mMIDonDataReady = env->GetMethodID(callbackClass, "onDataReady", "([FI)V");
30 }
31
init(int numFrames,int numChannels)32 void AppCallbackAudioSink::init(int numFrames, int numChannels) {
33 JNIEnv * env;
34 int getEnvStat = mJVM->GetEnv((void **)&env, JNI_VERSION_1_6);
35
36 if (getEnvStat == JNI_EDETACHED) {
37 int rs = mJVM->AttachCurrentThread(&env, NULL);
38 }
39
40 mAudioDataArrayLength = numChannels * numFrames;
41 mAudioDataArray = env->NewFloatArray(mAudioDataArrayLength);
42 mAudioDataArray = (jfloatArray)env->NewGlobalRef(mAudioDataArray);
43
44 if (getEnvStat == JNI_EDETACHED) {
45 mJVM->DetachCurrentThread();
46 }
47 }
48
start()49 void AppCallbackAudioSink::start() {
50 }
51
stop()52 void AppCallbackAudioSink::stop() {
53 JNIEnv * env;
54 int getEnvStat = mJVM->GetEnv((void **)&env, JNI_VERSION_1_6);
55
56 if (getEnvStat == JNI_EDETACHED) {
57 int rs = mJVM->AttachCurrentThread(&env, NULL);
58 }
59
60 releaseJNIResources(env);
61
62 if (getEnvStat == JNI_EDETACHED) {
63 mJVM->DetachCurrentThread();
64 }
65 }
66
push(float * audioData,int numFrames,int numChannels)67 void AppCallbackAudioSink::push(float* audioData, int numFrames, int numChannels) {
68 // Get the local JNI env
69 JNIEnv * env;
70 int getEnvStat = mJVM->GetEnv((void **)&env, JNI_VERSION_1_6);
71
72 if (getEnvStat == JNI_EDETACHED) {
73 int rs = mJVM->AttachCurrentThread(&env, NULL);
74 }
75
76 // put the float* into a jfloatarray
77 env->SetFloatArrayRegion(mAudioDataArray, 0, numFrames * numChannels, audioData);
78 env->CallVoidMethod(mCallbackObj, mMIDonDataReady, mAudioDataArray, numFrames);
79
80 if (getEnvStat == JNI_EDETACHED) {
81 mJVM->DetachCurrentThread();
82 }
83 }
84
releaseJNIResources(JNIEnv * env)85 void AppCallbackAudioSink::releaseJNIResources(JNIEnv *env) {
86 env->DeleteGlobalRef(mCallbackObj);
87 }
88
89 extern "C" {
90 JNIEXPORT jlong JNICALL
Java_org_hyphonate_megaaudio_recorder_sinks_AppCallbackAudioSinkProvider_allocOboeSinkN(JNIEnv * env,jobject thiz,jobject callback_obj)91 Java_org_hyphonate_megaaudio_recorder_sinks_AppCallbackAudioSinkProvider_allocOboeSinkN(
92 JNIEnv *env, jobject thiz, jobject callback_obj) {
93 AppCallbackAudioSink* sink = new AppCallbackAudioSink(env, callback_obj);
94 return (jlong)sink;
95 }
96
97 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_recorder_sinks_AppCallbackAudioSinkProvider_releaseJNIResourcesN(JNIEnv * env,jobject thiz,jlong oboe_sink)98 Java_org_hyphonate_megaaudio_recorder_sinks_AppCallbackAudioSinkProvider_releaseJNIResourcesN(
99 JNIEnv *env, jobject thiz, jlong oboe_sink) {
100 AppCallbackAudioSink* sink = (AppCallbackAudioSink*)oboe_sink;
101 sink->releaseJNIResources(env);
102 }
103
104 } // extern "C"
105