• 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 
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 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getError(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)99 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getError
100   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
101     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
102     if (analyzer != nullptr) {
103         return (jint) analyzer->getError();
104     }
105     return (jint) AAUDIO_ERROR_NULL;
106 }
107 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_analyze(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)108 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_analyze
109   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
110     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
111     if (analyzer != nullptr) {
112         return analyzer->analyze();
113     }
114     return AAUDIO_ERROR_NULL;
115 }
116 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getLatencyMillis(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)117 JNIEXPORT jdouble JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getLatencyMillis
118   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
119     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
120     if (analyzer != nullptr) {
121         return analyzer->getLatencyMillis();
122     }
123     return -1.0;
124 }
125 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getConfidence(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)126 JNIEXPORT jdouble JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getConfidence
127   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
128     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
129     if (analyzer != nullptr) {
130         return analyzer->getConfidence();
131     }
132     return 0.0;
133 }
134 
Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getSampleRate(JNIEnv * env __unused,jobject obj __unused,jlong pAnalyzer)135 JNIEXPORT jint JNICALL Java_com_android_cts_verifier_audio_NativeAnalyzerThread_getSampleRate
136   (JNIEnv *env __unused, jobject obj __unused, jlong pAnalyzer) {
137     NativeAudioAnalyzer * analyzer = (NativeAudioAnalyzer *) pAnalyzer;
138     if (analyzer != nullptr) {
139         return analyzer->getSampleRate();
140     }
141     return 0;
142 }
143 
144 //
145 // com.android.cts.verifier.audio.audiolib.AudioUtils
146 //
147 JNIEXPORT jboolean JNICALL
Java_com_android_cts_verifier_audio_audiolib_AudioUtils_isMMapSupported(JNIEnv * env __unused)148     Java_com_android_cts_verifier_audio_audiolib_AudioUtils_isMMapSupported(JNIEnv *env __unused) {
149 
150     return oboe::AAudioExtensions().isMMapSupported();
151 }
152 
153 JNIEXPORT jboolean JNICALL
Java_com_android_cts_verifier_audio_audiolib_AudioUtils_isMMapExclusiveSupported(JNIEnv * env __unused)154     Java_com_android_cts_verifier_audio_audiolib_AudioUtils_isMMapExclusiveSupported(
155         JNIEnv *env __unused) {
156 
157     return oboe::AAudioExtensions().isMMapExclusiveSupported();
158 }
159 
160 }
161