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 inputDeviceId,jint outputDeviceId)37 JNIEXPORT jlong JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_openAudio
38   (JNIEnv * /*env */, jobject /* obj */, jint inputDeviceId, jint outputDeviceId) {
39     // It is OK to use a raw pointer here because the pointer will be passed back
40     // to Java and only used from one thread.
41     // Java then deletes it from that same thread by calling _closeAudio() below.
42     NativeAudioAnalyzer * analyzer = new NativeAudioAnalyzer();
43     aaudio_result_t result = analyzer->openAudio(inputDeviceId, outputDeviceId);
44     if (result != AAUDIO_OK) {
45         delete analyzer;
46         analyzer = nullptr;
47     }
48     return (jlong) analyzer;
49 }
50 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_startAudio(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)51 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_startAudio
52   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
53     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
54     int result = AAUDIO_ERROR_NULL;
55     if (analyzer != nullptr) {
56         result = analyzer->startAudio();
57     }
58     return result;
59 }
60 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_stopAudio(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)61 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_stopAudio
62   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
63     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
64     if (analyzer != nullptr) {
65         return analyzer->stopAudio();
66     }
67     return AAUDIO_ERROR_NULL;
68 }
69 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_closeAudio(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)70 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_closeAudio
71   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
72     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
73     int result = AAUDIO_ERROR_NULL;
74     if (analyzer != nullptr) {
75         result = analyzer->closeAudio();
76         delete analyzer;
77     }
78     return result;
79 }
80 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_isRecordingComplete(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)81 JNIEXPORT jboolean JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_isRecordingComplete
82   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
83     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
84     if (analyzer != nullptr) {
85         return analyzer->isRecordingComplete();
86     }
87     return false;
88 }
89 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_isLowlatency(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)90 JNIEXPORT jboolean JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_isLowlatency
91   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
92     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
93     if (analyzer != nullptr) {
94         return analyzer->isLowLatencyStream();
95     }
96     return false;
97 }
98 
99 JNIEXPORT jboolean JNICALL
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_has24BitHardwareSupport(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)100     Java_com_android_cts_verifier_audio_NativeAnalyzerThread_has24BitHardwareSupport
101         (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
102     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
103     if (analyzer != nullptr) {
104         return analyzer->has24BitHardwareSupport();
105     }
106     return false;
107 }
108 
109 JNIEXPORT jint JNICALL
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getHardwareFormat(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)110 Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getHardwareFormat
111         (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
112     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
113     if (analyzer != nullptr) {
114         return analyzer->getHardwareFormat();
115     }
116     return (jint) AAUDIO_FORMAT_INVALID;
117 }
118 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getError(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)119 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getError
120   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
121     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
122     if (analyzer != nullptr) {
123         return (jint) analyzer->getError();
124     }
125     return (jint) AAUDIO_ERROR_NULL;
126 }
127 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_analyze(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)128 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_analyze
129   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
130     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
131     if (analyzer != nullptr) {
132         return analyzer->analyze();
133     }
134     return AAUDIO_ERROR_NULL;
135 }
136 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getLatencyMillis(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)137 JNIEXPORT jdouble JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getLatencyMillis
138   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
139     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
140     if (analyzer != nullptr) {
141         return analyzer->getLatencyMillis();
142     }
143     return -1.0;
144 }
145 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getConfidence(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)146 JNIEXPORT jdouble JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getConfidence
147   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
148     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
149     if (analyzer != nullptr) {
150         return analyzer->getConfidence();
151     }
152     return 0.0;
153 }
154 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getSampleRate(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)155 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getSampleRate
156   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
157     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
158     if (analyzer != nullptr) {
159         return analyzer->getSampleRate();
160     }
161     return 0;
162 }
163 
164 JNIEXPORT jdouble JNICALL
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_measureTimestampLatencyMillis(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)165         Java_com_android_cts_verifier_audio_NativeAnalyzerThread_measureTimestampLatencyMillis
166         (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
167     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
168     if (analyzer != nullptr) {
169         return analyzer->measureTimestampLatencyMillis();
170     }
171     return 0;
172 }
173 
174 //
175 // com.android.cts.verifier.audio.audiolib.AudioUtils
176 //
177 JNIEXPORT jboolean JNICALL
Java_com_android_cts_verifier_audio_audiolib_AudioUtils_isMMapSupported(JNIEnv * env __unused)178     Java_com_android_cts_verifier_audio_audiolib_AudioUtils_isMMapSupported(JNIEnv *env __unused) {
179 
180     return oboe::AAudioExtensions::getInstance().isMMapSupported();
181 }
182 
183 JNIEXPORT jboolean JNICALL
Java_com_android_cts_verifier_audio_audiolib_AudioUtils_isMMapExclusiveSupported(JNIEnv * env __unused)184     Java_com_android_cts_verifier_audio_audiolib_AudioUtils_isMMapExclusiveSupported(
185         JNIEnv *env __unused) {
186 
187     return oboe::AAudioExtensions::getInstance().isMMapExclusiveSupported();
188 }
189 
190 }
191