blob: 39dba112883f369f9a97ac10d19803e5e341d7e7 [file] [log] [blame]
Phil Burk0433d8f2018-11-21 16:41:25 -08001/*
2 * Copyright 2015 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#define MODULE_NAME "OboeTester"
18
19#include <cassert>
20#include <cstring>
21#include <jni.h>
22#include <stdint.h>
23#include <thread>
24
25#include "common/OboeDebug.h"
26#include "oboe/Oboe.h"
27
28#include "NativeAudioContext.h"
29
Phil Burk0af43bb2019-03-12 14:11:38 -070030NativeAudioContext engine;
Phil Burk0433d8f2018-11-21 16:41:25 -080031
32/*********************************************************************************/
33/********************** JNI Prototypes *****************************************/
34/*********************************************************************************/
35extern "C" {
36
Phil Burk0433d8f2018-11-21 16:41:25 -080037JNIEXPORT jint JNICALL
38Java_com_google_sample_oboe_manualtest_OboeAudioStream_openNative(JNIEnv *env, jobject,
Phil Burk6c16ce92019-03-05 16:07:38 -080039 jint nativeApi,
Phil Burk0433d8f2018-11-21 16:41:25 -080040 jint sampleRate,
41 jint channelCount,
42 jint format,
43 jint sharingMode,
44 jint performanceMode,
Phil Burkcd03b992019-09-20 10:28:20 +090045 jint inputPreset,
Phil Burk0433d8f2018-11-21 16:41:25 -080046 jint deviceId,
47 jint sessionId,
48 jint framesPerBurst,
Phil Burk301b0662019-06-06 11:36:30 -070049 jboolean channelConversionAllowed,
Phil Burk59273982019-08-29 09:10:37 -070050 jboolean formatConversionAllowed,
Phil Burk301b0662019-06-06 11:36:30 -070051 jint rateConversionQuality,
Phil Burk2beb2c72019-09-05 18:51:14 -070052 jboolean isMMap,
Phil Burk0433d8f2018-11-21 16:41:25 -080053 jboolean isInput);
54JNIEXPORT void JNICALL
Phil Burk6c16ce92019-03-05 16:07:38 -080055Java_com_google_sample_oboe_manualtest_OboeAudioStream_close(JNIEnv *env, jobject, jint);
Phil Burk0af43bb2019-03-12 14:11:38 -070056
Phil Burk0433d8f2018-11-21 16:41:25 -080057JNIEXPORT jint JNICALL
Phil Burk6c16ce92019-03-05 16:07:38 -080058Java_com_google_sample_oboe_manualtest_OboeAudioStream_setThresholdInFrames(JNIEnv *env, jobject, jint, jint);
Phil Burk0433d8f2018-11-21 16:41:25 -080059JNIEXPORT jint JNICALL
Phil Burk6c16ce92019-03-05 16:07:38 -080060Java_com_google_sample_oboe_manualtest_OboeAudioStream_getThresholdInFrames(JNIEnv *env, jobject, jint);
Phil Burk0433d8f2018-11-21 16:41:25 -080061JNIEXPORT jint JNICALL
Phil Burk6c16ce92019-03-05 16:07:38 -080062Java_com_google_sample_oboe_manualtest_OboeAudioStream_getBufferCapacityInFrames(JNIEnv *env, jobject, jint);
Phil Burk0433d8f2018-11-21 16:41:25 -080063JNIEXPORT jint JNICALL
Phil Burk6c16ce92019-03-05 16:07:38 -080064Java_com_google_sample_oboe_manualtest_OboeAudioStream_setNativeApi(JNIEnv *env, jobject, jint, jint);
Phil Burk0433d8f2018-11-21 16:41:25 -080065
66JNIEXPORT void JNICALL
67Java_com_google_sample_oboe_manualtest_OboeAudioStream_setUseCallback(JNIEnv *env, jclass type,
Phil Burk6fb1d802018-12-21 15:28:07 -080068 jboolean useCallback);
69JNIEXPORT void JNICALL
70Java_com_google_sample_oboe_manualtest_OboeAudioStream_setCallbackReturnStop(JNIEnv *env,
71 jclass type,
72 jboolean b);
Phil Burk0433d8f2018-11-21 16:41:25 -080073JNIEXPORT void JNICALL
74Java_com_google_sample_oboe_manualtest_OboeAudioStream_setCallbackSize(JNIEnv *env, jclass type,
75 jint callbackSize);
76
77// ================= OboeAudioOutputStream ================================
78
79JNIEXPORT void JNICALL
Phil Burk9102c2a2020-11-04 16:19:52 -080080Java_com_google_sample_oboe_manualtest_OboeAudioOutputStream_trigger(JNIEnv *env, jobject);
Phil Burk0433d8f2018-11-21 16:41:25 -080081JNIEXPORT void JNICALL
82Java_com_google_sample_oboe_manualtest_OboeAudioOutputStream_setToneType(JNIEnv *env, jobject, jint);
83JNIEXPORT void JNICALL
84Java_com_google_sample_oboe_manualtest_OboeAudioOutputStream_setAmplitude(JNIEnv *env, jobject, jdouble);
85
86/*********************************************************************************/
87/********************** JNI Implementations *************************************/
88/*********************************************************************************/
Phil Burk6c16ce92019-03-05 16:07:38 -080089
Phil Burk2beb2c72019-09-05 18:51:14 -070090JNIEXPORT jboolean JNICALL
91Java_com_google_sample_oboe_manualtest_NativeEngine_isMMapSupported(JNIEnv *env, jclass type) {
Haibo Huangc0d5f472021-02-09 16:44:15 -080092 return oboe::AAudioExtensions::getInstance().isMMapSupported();
Phil Burk2beb2c72019-09-05 18:51:14 -070093}
94
Phil Burk67f41f02019-12-10 08:31:44 -080095JNIEXPORT jboolean JNICALL
96Java_com_google_sample_oboe_manualtest_NativeEngine_isMMapExclusiveSupported(JNIEnv *env, jclass type) {
Haibo Huangc0d5f472021-02-09 16:44:15 -080097 return oboe::AAudioExtensions::getInstance().isMMapExclusiveSupported();
Phil Burk67f41f02019-12-10 08:31:44 -080098}
99
Phil Burk236521e2019-10-15 07:51:07 -0700100JNIEXPORT void JNICALL
101Java_com_google_sample_oboe_manualtest_NativeEngine_setWorkaroundsEnabled(JNIEnv *env, jclass type,
102 jboolean enabled) {
103 oboe::OboeGlobals::setWorkaroundsEnabled(enabled);
104}
105
Phil Burk1b08a722020-06-11 17:54:14 -0700106JNIEXPORT jboolean JNICALL
107Java_com_google_sample_oboe_manualtest_NativeEngine_areWorkaroundsEnabled(JNIEnv *env,
108 jclass type) {
109 return oboe::OboeGlobals::areWorkaroundsEnabled();
110}
111
Phil Burk0433d8f2018-11-21 16:41:25 -0800112JNIEXPORT jint JNICALL
113Java_com_google_sample_oboe_manualtest_OboeAudioStream_openNative(
114 JNIEnv *env, jobject synth,
Phil Burk6c16ce92019-03-05 16:07:38 -0800115 jint nativeApi,
Phil Burk0433d8f2018-11-21 16:41:25 -0800116 jint sampleRate,
117 jint channelCount,
118 jint format,
119 jint sharingMode,
120 jint performanceMode,
Phil Burkcd03b992019-09-20 10:28:20 +0900121 jint inputPreset,
Phil Burk0433d8f2018-11-21 16:41:25 -0800122 jint deviceId,
123 jint sessionId,
124 jint framesPerBurst,
Phil Burk301b0662019-06-06 11:36:30 -0700125 jboolean channelConversionAllowed,
Phil Burk59273982019-08-29 09:10:37 -0700126 jboolean formatConversionAllowed,
Phil Burk301b0662019-06-06 11:36:30 -0700127 jint rateConversionQuality,
Phil Burk2beb2c72019-09-05 18:51:14 -0700128 jboolean isMMap,
Phil Burk0433d8f2018-11-21 16:41:25 -0800129 jboolean isInput) {
130 LOGD("OboeAudioStream_openNative: sampleRate = %d, framesPerBurst = %d", sampleRate, framesPerBurst);
131
Phil Burk0af43bb2019-03-12 14:11:38 -0700132 return (jint) engine.getCurrentActivity()->open(nativeApi,
Phil Burk2beb2c72019-09-05 18:51:14 -0700133 sampleRate,
134 channelCount,
135 format,
136 sharingMode,
137 performanceMode,
Phil Burkcd03b992019-09-20 10:28:20 +0900138 inputPreset,
Phil Burk2beb2c72019-09-05 18:51:14 -0700139 deviceId,
140 sessionId,
141 framesPerBurst,
142 channelConversionAllowed,
143 formatConversionAllowed,
144 rateConversionQuality,
145 isMMap,
146 isInput);
Phil Burk0433d8f2018-11-21 16:41:25 -0800147}
148
149JNIEXPORT jint JNICALL
Phil Burk03bdc0a2019-03-06 15:56:25 -0800150Java_com_google_sample_oboe_manualtest_TestAudioActivity_startNative(JNIEnv *env, jobject) {
Phil Burk0af43bb2019-03-12 14:11:38 -0700151 return (jint) engine.getCurrentActivity()->start();
Phil Burk0433d8f2018-11-21 16:41:25 -0800152}
153
154JNIEXPORT jint JNICALL
Phil Burk03bdc0a2019-03-06 15:56:25 -0800155Java_com_google_sample_oboe_manualtest_TestAudioActivity_pauseNative(JNIEnv *env, jobject) {
Phil Burk0af43bb2019-03-12 14:11:38 -0700156 return (jint) engine.getCurrentActivity()->pause();
Phil Burk0433d8f2018-11-21 16:41:25 -0800157}
158
159JNIEXPORT jint JNICALL
Phil Burk03bdc0a2019-03-06 15:56:25 -0800160Java_com_google_sample_oboe_manualtest_TestAudioActivity_stopNative(JNIEnv *env, jobject) {
Phil Burk0af43bb2019-03-12 14:11:38 -0700161 return (jint) engine.getCurrentActivity()->stop();
Phil Burk0433d8f2018-11-21 16:41:25 -0800162}
163
164JNIEXPORT jint JNICALL
Phil Burk27c72712020-01-27 09:13:19 -0800165Java_com_google_sample_oboe_manualtest_TestAudioActivity_getFramesPerCallback(JNIEnv *env, jobject) {
166 return (jint) engine.getCurrentActivity()->getFramesPerCallback();
167}
168
169JNIEXPORT jint JNICALL
Phil Burk0433d8f2018-11-21 16:41:25 -0800170Java_com_google_sample_oboe_manualtest_OboeAudioStream_startPlaybackNative(JNIEnv *env, jobject) {
Phil Burk0af43bb2019-03-12 14:11:38 -0700171 return (jint) engine.getCurrentActivity()->startPlayback();
Phil Burk0433d8f2018-11-21 16:41:25 -0800172}
173
174JNIEXPORT void JNICALL
Phil Burk6c16ce92019-03-05 16:07:38 -0800175Java_com_google_sample_oboe_manualtest_OboeAudioStream_close(JNIEnv *env, jobject, jint streamIndex) {
Phil Burk0af43bb2019-03-12 14:11:38 -0700176 engine.getCurrentActivity()->close(streamIndex);
Phil Burk0433d8f2018-11-21 16:41:25 -0800177}
178
179JNIEXPORT jint JNICALL
180Java_com_google_sample_oboe_manualtest_OboeAudioStream_setBufferSizeInFrames(
Phil Burk6c16ce92019-03-05 16:07:38 -0800181 JNIEnv *env, jobject, jint streamIndex, jint threshold) {
Haibo Huangc0d5f472021-02-09 16:44:15 -0800182 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800183 if (oboeStream != nullptr) {
184 auto result = oboeStream->setBufferSizeInFrames(threshold);
Phil Burk0433d8f2018-11-21 16:41:25 -0800185 return (!result)
186 ? (jint) result.error()
187 : (jint) result.value();
188 }
189 return (jint) oboe::Result::ErrorNull;
190}
191
192JNIEXPORT jint JNICALL
193Java_com_google_sample_oboe_manualtest_OboeAudioStream_getBufferSizeInFrames(
Phil Burk6c16ce92019-03-05 16:07:38 -0800194 JNIEnv *env, jobject, jint streamIndex) {
Phil Burk0433d8f2018-11-21 16:41:25 -0800195 jint result = (jint) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800196 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800197 if (oboeStream != nullptr) {
198 result = oboeStream->getBufferSizeInFrames();
Phil Burk0433d8f2018-11-21 16:41:25 -0800199 }
200 return result;
201}
202
203JNIEXPORT jint JNICALL
204Java_com_google_sample_oboe_manualtest_OboeAudioStream_getBufferCapacityInFrames(
Phil Burk6c16ce92019-03-05 16:07:38 -0800205 JNIEnv *env, jobject, jint streamIndex) {
Phil Burk0433d8f2018-11-21 16:41:25 -0800206 jint result = (jint) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800207 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800208 if (oboeStream != nullptr) {
209 result = oboeStream->getBufferCapacityInFrames();
Phil Burk0433d8f2018-11-21 16:41:25 -0800210 }
211 return result;
212}
213
214static int convertAudioApiToNativeApi(oboe::AudioApi audioApi) {
215 switch(audioApi) {
216 case oboe::AudioApi::Unspecified:
217 return NATIVE_MODE_UNSPECIFIED;
218 case oboe::AudioApi::OpenSLES:
219 return NATIVE_MODE_OPENSLES;
220 case oboe::AudioApi::AAudio:
221 return NATIVE_MODE_AAUDIO;
222 default:
223 return -1;
224 }
225}
226
227JNIEXPORT jint JNICALL
228Java_com_google_sample_oboe_manualtest_OboeAudioStream_getNativeApi(
Phil Burk6c16ce92019-03-05 16:07:38 -0800229 JNIEnv *env, jobject, jint streamIndex) {
Phil Burk0433d8f2018-11-21 16:41:25 -0800230 jint result = (jint) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800231 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800232 if (oboeStream != nullptr) {
233 oboe::AudioApi audioApi = oboeStream->getAudioApi();
Phil Burk0433d8f2018-11-21 16:41:25 -0800234 result = convertAudioApiToNativeApi(audioApi);
235 LOGD("OboeAudioStream_getNativeApi got %d", result);
236 }
237 return result;
238}
239
240JNIEXPORT jint JNICALL
241Java_com_google_sample_oboe_manualtest_OboeAudioStream_getSampleRate(
Phil Burk6c16ce92019-03-05 16:07:38 -0800242 JNIEnv *env, jobject, jint streamIndex) {
Phil Burk0433d8f2018-11-21 16:41:25 -0800243 jint result = (jint) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800244 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800245 if (oboeStream != nullptr) {
246 result = oboeStream->getSampleRate();
Phil Burk0433d8f2018-11-21 16:41:25 -0800247 }
248 return result;
249}
250
251JNIEXPORT jint JNICALL
252Java_com_google_sample_oboe_manualtest_OboeAudioStream_getSharingMode(
Phil Burk6c16ce92019-03-05 16:07:38 -0800253 JNIEnv *env, jobject, jint streamIndex) {
Phil Burk0433d8f2018-11-21 16:41:25 -0800254 jint result = (jint) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800255 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800256 if (oboeStream != nullptr) {
257 result = (jint) oboeStream->getSharingMode();
Phil Burk0433d8f2018-11-21 16:41:25 -0800258 }
259 return result;
260}
261
262JNIEXPORT jint JNICALL
263Java_com_google_sample_oboe_manualtest_OboeAudioStream_getPerformanceMode(
Phil Burk6c16ce92019-03-05 16:07:38 -0800264 JNIEnv *env, jobject, jint streamIndex) {
Phil Burk0433d8f2018-11-21 16:41:25 -0800265 jint result = (jint) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800266 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800267 if (oboeStream != nullptr) {
268 result = (jint) oboeStream->getPerformanceMode();
Phil Burk0433d8f2018-11-21 16:41:25 -0800269 }
270 return result;
271}
272
273JNIEXPORT jint JNICALL
Phil Burkcd03b992019-09-20 10:28:20 +0900274Java_com_google_sample_oboe_manualtest_OboeAudioStream_getInputPreset(
275 JNIEnv *env, jobject, jint streamIndex) {
276 jint result = (jint) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800277 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burkcd03b992019-09-20 10:28:20 +0900278 if (oboeStream != nullptr) {
279 result = (jint) oboeStream->getInputPreset();
280 }
281 return result;
282}
283
284JNIEXPORT jint JNICALL
Phil Burk0433d8f2018-11-21 16:41:25 -0800285Java_com_google_sample_oboe_manualtest_OboeAudioStream_getFramesPerBurst(
Phil Burk6c16ce92019-03-05 16:07:38 -0800286 JNIEnv *env, jobject, jint streamIndex) {
Phil Burk0433d8f2018-11-21 16:41:25 -0800287 jint result = (jint) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800288 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800289 if (oboeStream != nullptr) {
290 result = oboeStream->getFramesPerBurst();
Phil Burk0433d8f2018-11-21 16:41:25 -0800291 }
292 return result;
293}
294
295JNIEXPORT jint JNICALL
296Java_com_google_sample_oboe_manualtest_OboeAudioStream_getChannelCount(
Phil Burk6c16ce92019-03-05 16:07:38 -0800297 JNIEnv *env, jobject, jint streamIndex) {
Phil Burk0433d8f2018-11-21 16:41:25 -0800298 jint result = (jint) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800299 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800300 if (oboeStream != nullptr) {
301 result = oboeStream->getChannelCount();
Phil Burk0433d8f2018-11-21 16:41:25 -0800302 }
303 return result;
304}
305
306JNIEXPORT jint JNICALL
Phil Burk6c16ce92019-03-05 16:07:38 -0800307Java_com_google_sample_oboe_manualtest_OboeAudioStream_getFormat(JNIEnv *env, jobject instance, jint streamIndex) {
Phil Burk0433d8f2018-11-21 16:41:25 -0800308 jint result = (jint) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800309 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800310 if (oboeStream != nullptr) {
311 result = (jint) oboeStream->getFormat();
312 }
313 return result;
Phil Burk0433d8f2018-11-21 16:41:25 -0800314}
315
316JNIEXPORT jint JNICALL
317Java_com_google_sample_oboe_manualtest_OboeAudioStream_getDeviceId(
Phil Burk6c16ce92019-03-05 16:07:38 -0800318 JNIEnv *env, jobject, jint streamIndex) {
Phil Burk0433d8f2018-11-21 16:41:25 -0800319 jint result = (jint) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800320 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800321 if (oboeStream != nullptr) {
322 result = oboeStream->getDeviceId();
Phil Burk0433d8f2018-11-21 16:41:25 -0800323 }
324 return result;
325}
326
327JNIEXPORT jint JNICALL
328Java_com_google_sample_oboe_manualtest_OboeAudioStream_getSessionId(
Phil Burk6c16ce92019-03-05 16:07:38 -0800329 JNIEnv *env, jobject, jint streamIndex) {
Phil Burk0433d8f2018-11-21 16:41:25 -0800330 jint result = (jint) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800331 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800332 if (oboeStream != nullptr) {
333 result = oboeStream->getSessionId();
Phil Burk0433d8f2018-11-21 16:41:25 -0800334 }
335 return result;
336}
337
338JNIEXPORT jlong JNICALL
339Java_com_google_sample_oboe_manualtest_OboeAudioStream_getFramesWritten(
Phil Burk6c16ce92019-03-05 16:07:38 -0800340 JNIEnv *env, jobject, jint streamIndex) {
Phil Burk0433d8f2018-11-21 16:41:25 -0800341 jlong result = (jint) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800342 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800343 if (oboeStream != nullptr) {
344 result = oboeStream->getFramesWritten();
Phil Burk0433d8f2018-11-21 16:41:25 -0800345 }
346 return result;
347}
348
349JNIEXPORT jlong JNICALL
350Java_com_google_sample_oboe_manualtest_OboeAudioStream_getFramesRead(
Phil Burk6c16ce92019-03-05 16:07:38 -0800351 JNIEnv *env, jobject, jint streamIndex) {
Phil Burk0433d8f2018-11-21 16:41:25 -0800352 jlong result = (jlong) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800353 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800354 if (oboeStream != nullptr) {
355 result = oboeStream->getFramesRead();
Phil Burk0433d8f2018-11-21 16:41:25 -0800356 }
357 return result;
358}
359
Phil Burk5cb621f2019-02-14 17:51:30 -0800360JNIEXPORT jint JNICALL
361Java_com_google_sample_oboe_manualtest_OboeAudioStream_getXRunCount(
Phil Burk6c16ce92019-03-05 16:07:38 -0800362 JNIEnv *env, jobject, jint streamIndex) {
Phil Burk5cb621f2019-02-14 17:51:30 -0800363 jint result = (jlong) oboe::Result::ErrorNull;
Haibo Huangc0d5f472021-02-09 16:44:15 -0800364 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800365 if (oboeStream != nullptr) {
366 auto oboeResult = oboeStream->getXRunCount();
Phil Burk5cb621f2019-02-14 17:51:30 -0800367 if (!oboeResult) {
368 result = (jint) oboeResult.error();
369 } else {
370 result = oboeResult.value();
371 }
372 }
373 return result;
374}
375
Phil Burk6fb1d802018-12-21 15:28:07 -0800376JNIEXPORT jlong JNICALL
377Java_com_google_sample_oboe_manualtest_OboeAudioStream_getCallbackCount(
378 JNIEnv *env, jobject) {
Phil Burk0af43bb2019-03-12 14:11:38 -0700379 return engine.getCurrentActivity()->getCallbackCount();
Phil Burk6fb1d802018-12-21 15:28:07 -0800380}
381
Phil Burk5617ca62020-10-12 07:14:06 -0700382JNIEXPORT jint JNICALL
383Java_com_google_sample_oboe_manualtest_OboeAudioStream_getLastErrorCallbackResult(
Phil Burk7b814ba2020-11-11 07:30:52 -0800384 JNIEnv *env, jobject, jint streamIndex) {
Haibo Huangc0d5f472021-02-09 16:44:15 -0800385 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk7b814ba2020-11-11 07:30:52 -0800386 if (oboeStream != nullptr) {
387 return (jint) oboeStream->getLastErrorCallbackResult();
388 }
389 return 0;
Phil Burk5617ca62020-10-12 07:14:06 -0700390}
391
Phil Burk0433d8f2018-11-21 16:41:25 -0800392JNIEXPORT jdouble JNICALL
Haibo Huangc0d5f472021-02-09 16:44:15 -0800393Java_com_google_sample_oboe_manualtest_OboeAudioStream_getTimestampLatency(JNIEnv *env,
394 jobject instance,
395 jint streamIndex) {
396 return engine.getCurrentActivity()->getTimestampLatency(streamIndex);
Phil Burk0433d8f2018-11-21 16:41:25 -0800397}
398
Phil Burk8e038822020-02-25 15:00:59 -0800399JNIEXPORT jdouble JNICALL
400Java_com_google_sample_oboe_manualtest_OboeAudioStream_getCpuLoad(JNIEnv *env, jobject instance, jint streamIndex) {
401 return engine.getCurrentActivity()->getCpuLoad();
402}
403
Phil Burk0f94ca12019-12-21 15:13:40 -0800404JNIEXPORT void JNICALL
405Java_com_google_sample_oboe_manualtest_OboeAudioStream_setWorkload(
406 JNIEnv *env, jobject, jdouble workload) {
407 engine.getCurrentActivity()->setWorkload(workload);
408}
409
Phil Burk0433d8f2018-11-21 16:41:25 -0800410JNIEXPORT jint JNICALL
Phil Burk6c16ce92019-03-05 16:07:38 -0800411Java_com_google_sample_oboe_manualtest_OboeAudioStream_getState(JNIEnv *env, jobject instance, jint streamIndex) {
Haibo Huangc0d5f472021-02-09 16:44:15 -0800412 std::shared_ptr<oboe::AudioStream> oboeStream = engine.getCurrentActivity()->getStream(streamIndex);
Phil Burk6c16ce92019-03-05 16:07:38 -0800413 if (oboeStream != nullptr) {
414 auto state = oboeStream->getState();
Phil Burk0433d8f2018-11-21 16:41:25 -0800415 if (state != oboe::StreamState::Starting && state != oboe::StreamState::Started) {
Phil Burk6c16ce92019-03-05 16:07:38 -0800416 oboe::Result result = oboeStream->waitForStateChange(
Phil Burk0433d8f2018-11-21 16:41:25 -0800417 oboe::StreamState::Uninitialized,
418 &state, 0);
Don Turner5c427472019-11-15 15:59:25 +0000419
420 if (result != oboe::Result::OK){
421 if (result == oboe::Result::ErrorClosed) {
422 state = oboe::StreamState::Closed;
423 } else if (result == oboe::Result::ErrorDisconnected){
424 state = oboe::StreamState::Disconnected;
425 } else {
426 state = oboe::StreamState::Unknown;
427 }
428 }
Phil Burk0433d8f2018-11-21 16:41:25 -0800429 }
430 return (jint) state;
431 }
432 return -1;
433}
434
435JNIEXPORT jdouble JNICALL
436Java_com_google_sample_oboe_manualtest_AudioInputTester_getPeakLevel(JNIEnv *env,
437 jobject instance,
438 jint index) {
Phil Burk0af43bb2019-03-12 14:11:38 -0700439 return engine.getCurrentActivity()->getPeakLevel(index);
Phil Burk0433d8f2018-11-21 16:41:25 -0800440}
441
442JNIEXPORT void JNICALL
443Java_com_google_sample_oboe_manualtest_OboeAudioStream_setUseCallback(JNIEnv *env, jclass type,
Phil Burk6fb1d802018-12-21 15:28:07 -0800444 jboolean useCallback) {
Phil Burkbd76d6a2019-06-01 08:48:03 -0700445 ActivityContext::mUseCallback = useCallback;
Phil Burk6fb1d802018-12-21 15:28:07 -0800446}
Phil Burk0433d8f2018-11-21 16:41:25 -0800447
Phil Burk6fb1d802018-12-21 15:28:07 -0800448JNIEXPORT void JNICALL
449Java_com_google_sample_oboe_manualtest_OboeAudioStream_setCallbackReturnStop(JNIEnv *env, jclass type,
450 jboolean b) {
Phil Burk6f1d5c12019-04-05 15:33:07 -0700451 OboeStreamCallbackProxy::setCallbackReturnStop(b);
Phil Burk0433d8f2018-11-21 16:41:25 -0800452}
453
454JNIEXPORT void JNICALL
455Java_com_google_sample_oboe_manualtest_OboeAudioStream_setCallbackSize(JNIEnv *env, jclass type,
456 jint callbackSize) {
Phil Burk0af43bb2019-03-12 14:11:38 -0700457 ActivityContext::callbackSize = callbackSize;
Phil Burk0433d8f2018-11-21 16:41:25 -0800458}
459
460JNIEXPORT jboolean JNICALL
Phil Burk6c16ce92019-03-05 16:07:38 -0800461Java_com_google_sample_oboe_manualtest_OboeAudioStream_isMMap(JNIEnv *env, jobject instance, jint streamIndex) {
Phil Burk0af43bb2019-03-12 14:11:38 -0700462 return engine.getCurrentActivity()->isMMapUsed(streamIndex);
Phil Burk0433d8f2018-11-21 16:41:25 -0800463}
464
465// ================= OboeAudioOutputStream ================================
466
467JNIEXPORT void JNICALL
Phil Burk9102c2a2020-11-04 16:19:52 -0800468Java_com_google_sample_oboe_manualtest_OboeAudioOutputStream_trigger(
469 JNIEnv *env, jobject) {
470 engine.getCurrentActivity()->trigger();
Phil Burk0433d8f2018-11-21 16:41:25 -0800471}
472
473JNIEXPORT void JNICALL
Phil Burke5985ed2018-12-06 11:38:30 -0800474Java_com_google_sample_oboe_manualtest_OboeAudioOutputStream_setChannelEnabled(
475 JNIEnv *env, jobject, jint channelIndex, jboolean enabled) {
Phil Burk0af43bb2019-03-12 14:11:38 -0700476 engine.getCurrentActivity()->setChannelEnabled(channelIndex, enabled);
Phil Burke5985ed2018-12-06 11:38:30 -0800477}
478
Phil Burk0a784e62019-07-20 12:43:15 -0700479JNIEXPORT void JNICALL
480Java_com_google_sample_oboe_manualtest_OboeAudioOutputStream_setSignalType(
481 JNIEnv *env, jobject, jint signalType) {
482 engine.getCurrentActivity()->setSignalType(signalType);
483}
484
Phil Burkb8356bb2019-11-06 11:11:07 -0800485JNIEXPORT jint JNICALL
486Java_com_google_sample_oboe_manualtest_OboeAudioStream_getOboeVersionNumber(JNIEnv *env,
487 jclass type) {
488 return OBOE_VERSION_NUMBER;
489}
490
Phil Burk7386a832019-03-21 16:07:27 -0700491// ==========================================================================
Phil Burk03bdc0a2019-03-06 15:56:25 -0800492JNIEXPORT void JNICALL
493Java_com_google_sample_oboe_manualtest_TestAudioActivity_setActivityType(JNIEnv *env,
494 jobject instance,
495 jint activityType) {
496 engine.setActivityType(activityType);
497}
498
Phil Burk7386a832019-03-21 16:07:27 -0700499// ==========================================================================
Phil Burk327361c2019-04-13 14:45:27 -0700500JNIEXPORT jint JNICALL
501Java_com_google_sample_oboe_manualtest_TestInputActivity_saveWaveFile(JNIEnv *env,
502 jobject instance,
503 jstring fileName) {
504 const char *str = env->GetStringUTFChars(fileName, nullptr);
505 LOGD("nativeSaveFile(%s)", str);
506 jint result = engine.getCurrentActivity()->saveWaveFile(str);
507 env->ReleaseStringUTFChars(fileName, str);
508 return result;
509}
510
511// ==========================================================================
Phil Burk1d17a602019-03-12 15:37:56 -0700512JNIEXPORT void JNICALL
Phil Burkbbde5bb2019-09-02 16:00:18 -0700513Java_com_google_sample_oboe_manualtest_TestInputActivity_setMinimumFramesBeforeRead(JNIEnv *env,
514 jobject instance,
515 jint numFrames) {
516 engine.getCurrentActivity()->setMinimumFramesBeforeRead(numFrames);
517}
518
519// ==========================================================================
520JNIEXPORT void JNICALL
Phil Burk1d17a602019-03-12 15:37:56 -0700521Java_com_google_sample_oboe_manualtest_EchoActivity_setDelayTime(JNIEnv *env,
522 jobject instance,
523 jdouble delayTimeSeconds) {
524 engine.setDelayTime(delayTimeSeconds);
525}
526
Haibo Huangc0d5f472021-02-09 16:44:15 -0800527JNIEXPORT int JNICALL
528Java_com_google_sample_oboe_manualtest_EchoActivity_getColdStartInputMillis(JNIEnv *env,
529 jobject instance) {
530 return engine.getCurrentActivity()->getColdStartInputMillis();
531}
532
533JNIEXPORT int JNICALL
534Java_com_google_sample_oboe_manualtest_EchoActivity_getColdStartOutputMillis(JNIEnv *env,
535 jobject instance) {
536 return engine.getCurrentActivity()->getColdStartOutputMillis();
537}
538
Phil Burk7386a832019-03-21 16:07:27 -0700539// ==========================================================================
Phil Burk80d83d82019-03-15 12:03:37 -0700540JNIEXPORT jint JNICALL
541Java_com_google_sample_oboe_manualtest_RoundTripLatencyActivity_getAnalyzerProgress(JNIEnv *env,
Phil Burk3873e882019-03-18 10:15:13 -0700542 jobject instance) {
Phil Burkd2dd5272019-03-18 16:05:25 -0700543 return engine.mActivityRoundTripLatency.getLatencyAnalyzer()->getProgress();
Phil Burk80d83d82019-03-15 12:03:37 -0700544}
545
Phil Burkcd12e2c2019-07-29 20:39:42 -0700546JNIEXPORT jint JNICALL
Phil Burk80d83d82019-03-15 12:03:37 -0700547Java_com_google_sample_oboe_manualtest_RoundTripLatencyActivity_getMeasuredLatency(JNIEnv *env,
548 jobject instance) {
Phil Burkd2dd5272019-03-18 16:05:25 -0700549 return engine.mActivityRoundTripLatency.getLatencyAnalyzer()->getMeasuredLatency();
Phil Burk80d83d82019-03-15 12:03:37 -0700550}
551
552JNIEXPORT jdouble JNICALL
553Java_com_google_sample_oboe_manualtest_RoundTripLatencyActivity_getMeasuredConfidence(JNIEnv *env,
Phil Burkd2dd5272019-03-18 16:05:25 -0700554 jobject instance) {
555 return engine.mActivityRoundTripLatency.getLatencyAnalyzer()->getMeasuredConfidence();
556}
557
Phil Burkcd12e2c2019-07-29 20:39:42 -0700558JNIEXPORT jdouble JNICALL
559Java_com_google_sample_oboe_manualtest_RoundTripLatencyActivity_getBackgroundRMS(JNIEnv *env,
560 jobject instance) {
561 return engine.mActivityRoundTripLatency.getLatencyAnalyzer()->getBackgroundRMS();
562}
563
564JNIEXPORT jdouble JNICALL
565Java_com_google_sample_oboe_manualtest_RoundTripLatencyActivity_getSignalRMS(JNIEnv *env,
566 jobject instance) {
567 return engine.mActivityRoundTripLatency.getLatencyAnalyzer()->getSignalRMS();
568}
569
570JNIEXPORT jint JNICALL
571Java_com_google_sample_oboe_manualtest_AnalyzerActivity_getMeasuredResult(JNIEnv *env,
572 jobject instance) {
573 return engine.mActivityRoundTripLatency.getLatencyAnalyzer()->getResult();
574}
575
Phil Burk7386a832019-03-21 16:07:27 -0700576// ==========================================================================
Phil Burkd2dd5272019-03-18 16:05:25 -0700577JNIEXPORT jint JNICALL
Phil Burk7386a832019-03-21 16:07:27 -0700578Java_com_google_sample_oboe_manualtest_AnalyzerActivity_getAnalyzerState(JNIEnv *env,
579 jobject instance) {
580 return ((ActivityFullDuplex *)engine.getCurrentActivity())->getState();
581}
582
583JNIEXPORT jboolean JNICALL
584Java_com_google_sample_oboe_manualtest_AnalyzerActivity_isAnalyzerDone(JNIEnv *env,
585 jobject instance) {
586 return ((ActivityFullDuplex *)engine.getCurrentActivity())->isAnalyzerDone();
587}
588
589JNIEXPORT jint JNICALL
Phil Burkcd818322019-03-22 16:51:40 -0700590Java_com_google_sample_oboe_manualtest_AnalyzerActivity_getResetCount(JNIEnv *env,
591 jobject instance) {
592 return ((ActivityFullDuplex *)engine.getCurrentActivity())->getResetCount();
593}
Phil Burk7386a832019-03-21 16:07:27 -0700594
595// ==========================================================================
596JNIEXPORT jint JNICALL
597Java_com_google_sample_oboe_manualtest_GlitchActivity_getGlitchCount(JNIEnv *env,
598 jobject instance) {
Phil Burk7386a832019-03-21 16:07:27 -0700599 return engine.mActivityGlitches.getGlitchAnalyzer()->getGlitchCount();
Phil Burk0433d8f2018-11-21 16:41:25 -0800600}
Phil Burk7386a832019-03-21 16:07:27 -0700601
Phil Burkbbde5bb2019-09-02 16:00:18 -0700602JNIEXPORT jint JNICALL
603Java_com_google_sample_oboe_manualtest_GlitchActivity_getStateFrameCount(JNIEnv *env,
604 jobject instance,
605 jint state) {
606 return engine.mActivityGlitches.getGlitchAnalyzer()->getStateFrameCount(state);
607}
608
Phil Burk7386a832019-03-21 16:07:27 -0700609JNIEXPORT jdouble JNICALL
610Java_com_google_sample_oboe_manualtest_GlitchActivity_getSignalToNoiseDB(JNIEnv *env,
611 jobject instance) {
Phil Burk7386a832019-03-21 16:07:27 -0700612 return engine.mActivityGlitches.getGlitchAnalyzer()->getSignalToNoiseDB();
613}
Haibo Huangc0d5f472021-02-09 16:44:15 -0800614
Phil Burke87667d2019-04-30 14:01:42 -0700615JNIEXPORT jdouble JNICALL
616Java_com_google_sample_oboe_manualtest_GlitchActivity_getPeakAmplitude(JNIEnv *env,
Phil Burkaf0c19f2019-09-22 17:18:23 +0800617 jobject instance) {
Phil Burke87667d2019-04-30 14:01:42 -0700618 return engine.mActivityGlitches.getGlitchAnalyzer()->getPeakAmplitude();
619}
Phil Burk7386a832019-03-21 16:07:27 -0700620
Haibo Huangc0d5f472021-02-09 16:44:15 -0800621JNIEXPORT jdouble JNICALL
622Java_com_google_sample_oboe_manualtest_TestDataPathsActivity_getMagnitude(JNIEnv *env,
623 jobject instance) {
624 return engine.mActivityDataPath.getDataPathAnalyzer()->getMagnitude();
625}
626
627JNIEXPORT jdouble JNICALL
628Java_com_google_sample_oboe_manualtest_TestDataPathsActivity_getMaxMagnitude(JNIEnv *env,
629 jobject instance) {
630 return engine.mActivityDataPath.getDataPathAnalyzer()->getMaxMagnitude();
631}
632
633JNIEXPORT jdouble JNICALL
634Java_com_google_sample_oboe_manualtest_TestDataPathsActivity_getPhase(JNIEnv *env,
635 jobject instance) {
636 return engine.mActivityDataPath.getDataPathAnalyzer()->getPhaseOffset();
637}
638
Phil Burkaf0c19f2019-09-22 17:18:23 +0800639JNIEXPORT void JNICALL
640Java_com_google_sample_oboe_manualtest_GlitchActivity_setTolerance(JNIEnv *env,
641 jobject instance,
642 jfloat tolerance) {
643 if (engine.mActivityGlitches.getGlitchAnalyzer()) {
644 engine.mActivityGlitches.getGlitchAnalyzer()->setTolerance(tolerance);
645 }
646}
647
Haibo Huangc0d5f472021-02-09 16:44:15 -0800648JNIEXPORT void JNICALL
649Java_com_google_sample_oboe_manualtest_GlitchActivity_setInputChannelNative(JNIEnv *env,
650 jobject instance,
651 jint channel) {
652 if (engine.mActivityGlitches.getGlitchAnalyzer()) {
653 engine.mActivityGlitches.getGlitchAnalyzer()->setInputChannel(channel);
654 }
655 if (engine.mActivityDataPath.getDataPathAnalyzer()) {
656 engine.mActivityDataPath.getDataPathAnalyzer()->setInputChannel(channel);
657 }
658}
659
660JNIEXPORT void JNICALL
661Java_com_google_sample_oboe_manualtest_GlitchActivity_setOutputChannelNative(JNIEnv *env,
662 jobject instance,
663 jint channel) {
664 if (engine.mActivityGlitches.getGlitchAnalyzer()) {
665 engine.mActivityGlitches.getGlitchAnalyzer()->setOutputChannel(channel);
666 }
667 if (engine.mActivityDataPath.getDataPathAnalyzer()) {
668 engine.mActivityDataPath.getDataPathAnalyzer()->setOutputChannel(channel);
669 }
670}
671
Phil Burk56868742019-11-21 20:19:53 +0000672JNIEXPORT jint JNICALL
673Java_com_google_sample_oboe_manualtest_ManualGlitchActivity_getGlitch(JNIEnv *env, jobject instance,
674 jfloatArray waveform_) {
Phil Burk6ff92662019-11-25 15:47:58 -0800675 float *waveform = env->GetFloatArrayElements(waveform_, nullptr);
Phil Burk56868742019-11-21 20:19:53 +0000676 jsize length = env->GetArrayLength(waveform_);
677 jsize numSamples = 0;
678 auto *analyzer = engine.mActivityGlitches.getGlitchAnalyzer();
679 if (analyzer) {
680 numSamples = analyzer->getLastGlitch(waveform, length);
681 }
682
683 env->ReleaseFloatArrayElements(waveform_, waveform, 0);
684 return numSamples;
685}
686
687}