• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //    __android_log_print(ANDROID_LOG_INFO, TAG, "push(numFrames:%d, numChannels:%d)",
69 //                        numFrames, numChannels);
70 
71     // Get the local JNI env
72     JNIEnv * env;
73     int getEnvStat = mJVM->GetEnv((void **)&env, JNI_VERSION_1_6);
74 
75     if (getEnvStat == JNI_EDETACHED) {
76         int rs = mJVM->AttachCurrentThread(&env, NULL);
77     }
78 
79     // put the float* into a jfloatarray
80     env->SetFloatArrayRegion(mAudioDataArray, 0, mAudioDataArrayLength, audioData);
81     env->CallVoidMethod(mCallbackObj, mMIDonDataReady, mAudioDataArray, numFrames);
82 
83     if (getEnvStat == JNI_EDETACHED) {
84         mJVM->DetachCurrentThread();
85     }
86 }
87 
releaseJNIResources(JNIEnv * env)88 void AppCallbackAudioSink::releaseJNIResources(JNIEnv *env) {
89     env->DeleteGlobalRef(mCallbackObj);
90 }
91 
92 extern "C" {
93 JNIEXPORT jlong JNICALL
Java_org_hyphonate_megaaudio_recorder_sinks_AppCallbackAudioSinkProvider_allocOboeSinkN(JNIEnv * env,jobject thiz,jobject callback_obj)94 Java_org_hyphonate_megaaudio_recorder_sinks_AppCallbackAudioSinkProvider_allocOboeSinkN(
95         JNIEnv *env, jobject thiz, jobject callback_obj) {
96     AppCallbackAudioSink* sink = new AppCallbackAudioSink(env, callback_obj);
97     return (jlong)sink;
98 }
99 
100 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_recorder_sinks_AppCallbackAudioSinkProvider_releaseJNIResourcesN(JNIEnv * env,jobject thiz,jlong oboe_sink)101 Java_org_hyphonate_megaaudio_recorder_sinks_AppCallbackAudioSinkProvider_releaseJNIResourcesN(
102         JNIEnv *env, jobject thiz, jlong oboe_sink) {
103     AppCallbackAudioSink* sink = (AppCallbackAudioSink*)oboe_sink;
104     sink->releaseJNIResources(env);
105 }
106 
107 } // extern "C"
108