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 
17 #include <cassert>
18 #include <cstring>
19 #include <jni.h>
20 #include <stdint.h>
21 
22 // If the NDK is before O then define this in your build
23 // so that AAudio.h will not be included.
24 // #define OBOE_NO_INCLUDE_AAUDIO
25 
26 // Oboe Includes
27 //#include <oboe/Oboe.h>
28 #include <AAudioExtensions.h>
29 
30 #include "NativeAudioAnalyzer.h"
31 
32 extern "C" {
33 
34 //
35 // com.android.cts.verifier.audio.NativeAnalyzerThread
36 //
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_openAudio(JNIEnv *,jobject,jint)37 JNIEXPORT jlong JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_openAudio
38   (JNIEnv * /*env */, jobject /* obj */,
39           jint /* micSource */) {
40     // It is OK to use a raw pointer here because the pointer will be passed back
41     // to Java and only used from one thread.
42     // Java then deletes it from that same thread by calling _closeAudio() below.
43     NativeAudioAnalyzer * analyzer = new NativeAudioAnalyzer();
44     aaudio_result_t result = analyzer->openAudio();
45     if (result != AAUDIO_OK) {
46         delete analyzer;
47         analyzer = nullptr;
48     }
49     return (jlong) analyzer;
50 }
51 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_startAudio(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)52 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_startAudio
53   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
54     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
55     int result = AAUDIO_ERROR_NULL;
56     if (analyzer != nullptr) {
57         result = analyzer->startAudio();
58     }
59     return result;
60 }
61 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_stopAudio(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)62 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_stopAudio
63   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
64     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
65     if (analyzer != nullptr) {
66         return analyzer->stopAudio();
67     }
68     return AAUDIO_ERROR_NULL;
69 }
70 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_closeAudio(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)71 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_closeAudio
72   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
73     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
74     int result = AAUDIO_ERROR_NULL;
75     if (analyzer != nullptr) {
76         result = analyzer->closeAudio();
77         delete analyzer;
78     }
79     return result;
80 }
81 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_isRecordingComplete(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)82 JNIEXPORT jboolean JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_isRecordingComplete
83   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
84     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
85     if (analyzer != nullptr) {
86         return analyzer->isRecordingComplete();
87     }
88     return false;
89 }
90 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_isLowlatency(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)91 JNIEXPORT jboolean JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_isLowlatency
92   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
93     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
94     if (analyzer != nullptr) {
95         return analyzer->isLowLatencyStream();
96     }
97     return false;
98 }
99 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getError(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)100 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getError
101   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
102     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
103     if (analyzer != nullptr) {
104         return (jint) analyzer->getError();
105     }
106     return (jint) AAUDIO_ERROR_NULL;
107 }
108 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_analyze(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)109 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_analyze
110   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
111     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
112     if (analyzer != nullptr) {
113         return analyzer->analyze();
114     }
115     return AAUDIO_ERROR_NULL;
116 }
117 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getLatencyMillis(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)118 JNIEXPORT jdouble JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getLatencyMillis
119   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
120     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
121     if (analyzer != nullptr) {
122         return analyzer->getLatencyMillis();
123     }
124     return -1.0;
125 }
126 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getConfidence(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)127 JNIEXPORT jdouble JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getConfidence
128   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
129     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
130     if (analyzer != nullptr) {
131         return analyzer->getConfidence();
132     }
133     return 0.0;
134 }
135 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getSampleRate(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)136 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getSampleRate
137   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
138     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
139     if (analyzer != nullptr) {
140         return analyzer->getSampleRate();
141     }
142     return 0;
143 }
144 
145 //
146 // com.android.cts.verifier.audio.audiolib.AudioUtils
147 //
148 JNIEXPORT jboolean JNICALL
Java_com_android_cts_verifier_audio_audiolib_AudioUtils_isMMapSupported(JNIEnv * env __unused)149     Java_com_android_cts_verifier_audio_audiolib_AudioUtils_isMMapSupported(JNIEnv *env __unused) {
150 
151     return oboe::AAudioExtensions().isMMapSupported();
152 }
153 
154 JNIEXPORT jboolean JNICALL
Java_com_android_cts_verifier_audio_audiolib_AudioUtils_isMMapExclusiveSupported(JNIEnv * env __unused)155     Java_com_android_cts_verifier_audio_audiolib_AudioUtils_isMMapExclusiveSupported(
156         JNIEnv *env __unused) {
157 
158     return oboe::AAudioExtensions().isMMapExclusiveSupported();
159 }
160 
161 }
162