1 /*
2  * Copyright 2018 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 <string>
19 #include <vector>
20 
21 #include "MegaDroneEngine.h"
22 
23 
convertJavaArrayToVector(JNIEnv * env,jintArray intArray)24 std::vector<int> convertJavaArrayToVector(JNIEnv *env, jintArray intArray) {
25     std::vector<int> v;
26     jsize length = env->GetArrayLength(intArray);
27     if (length > 0) {
28         jint *elements = env->GetIntArrayElements(intArray, nullptr);
29         v.insert(v.end(), &elements[0], &elements[length]);
30         // Unpin the memory for the array, or free the copy.
31         env->ReleaseIntArrayElements(intArray, elements, 0);
32     }
33     return v;
34 }
35 
36 extern "C" {
37 /**
38  * Start the audio engine
39  *
40  * @param env
41  * @param instance
42  * @param jCpuIds - CPU core IDs which the audio process should affine to
43  * @return a pointer to the audio engine. This should be passed to other methods
44  */
45 JNIEXPORT jlong JNICALL
Java_com_google_oboe_samples_megadrone_MainActivity_startEngine(JNIEnv * env,jobject,jintArray jCpuIds)46 Java_com_google_oboe_samples_megadrone_MainActivity_startEngine(JNIEnv *env, jobject /*unused*/,
47                                                          jintArray jCpuIds) {
48     std::vector<int> cpuIds = convertJavaArrayToVector(env, jCpuIds);
49     LOGD("cpu ids size: %d", static_cast<int>(cpuIds.size()));
50     MegaDroneEngine  *engine = new MegaDroneEngine(std::move(cpuIds));
51 
52     if (!engine->start()) {
53         LOGE("Failed to start MegaDrone Engine");
54         delete engine;
55         engine = nullptr;
56     } else  {
57         LOGD("Engine Started");
58     }
59     return reinterpret_cast<jlong>(engine);
60 }
61 
62 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_megadrone_MainActivity_stopEngine(JNIEnv * env,jobject instance,jlong jEngineHandle)63 Java_com_google_oboe_samples_megadrone_MainActivity_stopEngine(JNIEnv *env, jobject instance,
64         jlong jEngineHandle) {
65     auto engine = reinterpret_cast<MegaDroneEngine*>(jEngineHandle);
66     if (engine) {
67         engine->stop();
68         delete engine;
69     } else {
70         LOGD("Engine invalid, call startEngine() to create");
71     }
72 }
73 
74 
75 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_megadrone_MainActivity_tap(JNIEnv * env,jobject instance,jlong jEngineHandle,jboolean isDown)76 Java_com_google_oboe_samples_megadrone_MainActivity_tap(JNIEnv *env, jobject instance,
77         jlong jEngineHandle, jboolean isDown) {
78 
79     auto *engine = reinterpret_cast<MegaDroneEngine*>(jEngineHandle);
80     if (engine) {
81         engine->tap(isDown);
82     } else {
83         LOGE("Engine handle is invalid, call createEngine() to create a new one");
84     }
85 }
86 
87 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_megadrone_MainActivity_native_1setDefaultStreamValues(JNIEnv * env,jclass type,jint sampleRate,jint framesPerBurst)88 Java_com_google_oboe_samples_megadrone_MainActivity_native_1setDefaultStreamValues(JNIEnv *env,
89                                                                             jclass type,
90                                                                             jint sampleRate,
91                                                                             jint framesPerBurst) {
92     oboe::DefaultStreamValues::SampleRate = (int32_t) sampleRate;
93     oboe::DefaultStreamValues::FramesPerBurst = (int32_t) framesPerBurst;
94 }
95 
96 } // extern "C"
97