• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 <jni.h>
18 #include <oboe/Oboe.h>
19 #include "HelloOboeEngine.h"
20 #include "logging_macros.h"
21 
22 extern "C" {
23 
24 /**
25  * Creates the audio engine
26  *
27  * @return a pointer to the audio engine. This should be passed to other methods
28  */
29 JNIEXPORT jlong JNICALL
Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1createEngine(JNIEnv * env,jclass)30 Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1createEngine(
31         JNIEnv *env,
32         jclass /*unused*/) {
33     // We use std::nothrow so `new` returns a nullptr if the engine creation fails
34     HelloOboeEngine *engine = new(std::nothrow) HelloOboeEngine();
35     if (engine == nullptr) {
36         LOGE("Could not instantiate HelloOboeEngine");
37         return 0;
38     }
39     auto result = engine->start();
40     if (result != oboe::Result::OK) {
41         LOGE("Opening and starting stream failed. Returned %d", result);
42         engine->stop();
43         delete engine;
44         return 0;
45     }
46     return reinterpret_cast<jlong>(engine);
47 }
48 
49 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1deleteEngine(JNIEnv * env,jclass,jlong engineHandle)50 Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1deleteEngine(
51         JNIEnv *env,
52         jclass,
53         jlong engineHandle) {
54 
55     HelloOboeEngine *engine = reinterpret_cast<HelloOboeEngine *>(engineHandle);
56     engine->stop();
57     delete engine;
58 }
59 
60 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1setToneOn(JNIEnv * env,jclass,jlong engineHandle,jboolean isToneOn)61 Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1setToneOn(
62         JNIEnv *env,
63         jclass,
64         jlong engineHandle,
65         jboolean isToneOn) {
66 
67     HelloOboeEngine *engine = reinterpret_cast<HelloOboeEngine *>(engineHandle);
68     if (engine == nullptr) {
69         LOGE("Engine handle is invalid, call createHandle() to create a new one");
70         return;
71     }
72     engine->tap(isToneOn);
73 }
74 
75 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1setAudioApi(JNIEnv * env,jclass type,jlong engineHandle,jint audioApi)76 Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1setAudioApi(
77         JNIEnv *env,
78         jclass type,
79         jlong engineHandle,
80         jint audioApi) {
81 
82     HelloOboeEngine *engine = reinterpret_cast<HelloOboeEngine*>(engineHandle);
83     if (engine == nullptr) {
84         LOGE("Engine handle is invalid, call createHandle() to create a new one");
85         return;
86     }
87 
88     oboe::AudioApi api = static_cast<oboe::AudioApi>(audioApi);
89     engine->setAudioApi(api);
90 }
91 
92 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1setAudioDeviceId(JNIEnv * env,jclass,jlong engineHandle,jint deviceId)93 Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1setAudioDeviceId(
94         JNIEnv *env,
95         jclass,
96         jlong engineHandle,
97         jint deviceId) {
98 
99     HelloOboeEngine *engine = reinterpret_cast<HelloOboeEngine*>(engineHandle);
100     if (engine == nullptr) {
101         LOGE("Engine handle is invalid, call createHandle() to create a new one");
102         return;
103     }
104     engine->setDeviceId(deviceId);
105 }
106 
107 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1setChannelCount(JNIEnv * env,jclass type,jlong engineHandle,jint channelCount)108 Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1setChannelCount(
109         JNIEnv *env,
110         jclass type,
111         jlong engineHandle,
112         jint channelCount) {
113 
114     HelloOboeEngine *engine = reinterpret_cast<HelloOboeEngine*>(engineHandle);
115     if (engine == nullptr) {
116         LOGE("Engine handle is invalid, call createHandle() to create a new one");
117         return;
118     }
119     engine->setChannelCount(channelCount);
120 }
121 
122 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1setBufferSizeInBursts(JNIEnv * env,jclass,jlong engineHandle,jint bufferSizeInBursts)123 Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1setBufferSizeInBursts(
124         JNIEnv *env,
125         jclass,
126         jlong engineHandle,
127         jint bufferSizeInBursts) {
128 
129     HelloOboeEngine *engine = reinterpret_cast<HelloOboeEngine*>(engineHandle);
130     if (engine == nullptr) {
131         LOGE("Engine handle is invalid, call createHandle() to create a new one");
132         return;
133     }
134     engine->setBufferSizeInBursts(bufferSizeInBursts);
135 }
136 
137 
138 JNIEXPORT jdouble JNICALL
Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1getCurrentOutputLatencyMillis(JNIEnv * env,jclass,jlong engineHandle)139 Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1getCurrentOutputLatencyMillis(
140         JNIEnv *env,
141         jclass,
142         jlong engineHandle) {
143 
144     HelloOboeEngine *engine = reinterpret_cast<HelloOboeEngine*>(engineHandle);
145     if (engine == nullptr) {
146         LOGE("Engine is null, you must call createEngine before calling this method");
147         return static_cast<jdouble>(-1.0);
148     }
149     return static_cast<jdouble>(engine->getCurrentOutputLatencyMillis());
150 }
151 
152 JNIEXPORT jboolean JNICALL
Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1isLatencyDetectionSupported(JNIEnv * env,jclass type,jlong engineHandle)153 Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1isLatencyDetectionSupported(
154         JNIEnv *env,
155         jclass type,
156         jlong engineHandle) {
157 
158     HelloOboeEngine *engine = reinterpret_cast<HelloOboeEngine*>(engineHandle);
159     if (engine == nullptr) {
160         LOGE("Engine is null, you must call createEngine before calling this method");
161         return JNI_FALSE;
162     }
163     return (engine->isLatencyDetectionSupported() ? JNI_TRUE : JNI_FALSE);
164 }
165 
166 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1setDefaultStreamValues(JNIEnv * env,jclass type,jint sampleRate,jint framesPerBurst)167 Java_com_google_oboe_samples_hellooboe_PlaybackEngine_native_1setDefaultStreamValues(
168         JNIEnv *env,
169         jclass type,
170         jint sampleRate,
171         jint framesPerBurst) {
172     oboe::DefaultStreamValues::SampleRate = (int32_t) sampleRate;
173     oboe::DefaultStreamValues::FramesPerBurst = (int32_t) framesPerBurst;
174 }
175 } // extern "C"
176