1 /*
2  * Copyright (C) 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 #ifndef ANDROID_SL_UTILS_H
18 #define ANDROID_SL_UTILS_H
19 
20 #include <SLES/OpenSLES.h>
21 #include <SLES/OpenSLES_Android.h>
22 
23 #include <jni.h>
24 #include <mutex>
25 #include <utils/Log.h>
26 
27 #define CheckErr(res) LOG_ALWAYS_FATAL_IF( \
28         (res) != SL_RESULT_SUCCESS, "result error %s", android::getSLErrStr(res));
29 
30 namespace android {
31 
32 // FIXME: Move to common file.
33 template <typename T>
34 static inline
min(const T & a,const T & b)35 const T &min(const T &a, const T &b) {
36     return a < b ? a : b;
37 }
38 
39 /* Returns the error string for the OpenSL ES error code
40  */
41 const char *getSLErrStr(int code);
42 
43 /* Returns the OpenSL ES equivalent standard channel mask
44  * for a given channel count, 0 if no such mask is available.
45  */
46 SLuint32 channelCountToMask(unsigned channelCount);
47 
48 /* Returns an OpenSL ES Engine object interface.
49  * The engine created will be thread safe [3.2]
50  * The underlying implementation may not support more than one engine. [4.1.1]
51  *
52  * @param global if true, return and open the global engine instance or make
53  *   a local engine instance if false.
54  * @return NULL if unsuccessful or the Engine SLObjectItf.
55  */
56 SLObjectItf OpenSLEngine(bool global = true);
57 
58 /* Closes an OpenSL ES Engine object returned by OpenSLEngine().
59  */
60 void CloseSLEngine(SLObjectItf engine);
61 
62 // overloaded JNI array helper functions (same as in android_media_AudioRecord)
63 inline
envGetArrayElements(JNIEnv * env,jbyteArray array,jboolean * isCopy)64 jbyte *envGetArrayElements(JNIEnv *env, jbyteArray array, jboolean *isCopy) {
65     return env->GetByteArrayElements(array, isCopy);
66 }
67 
68 inline
envReleaseArrayElements(JNIEnv * env,jbyteArray array,jbyte * elems,jint mode)69 void envReleaseArrayElements(JNIEnv *env, jbyteArray array, jbyte *elems, jint mode) {
70     env->ReleaseByteArrayElements(array, elems, mode);
71 }
72 
73 inline
envGetArrayElements(JNIEnv * env,jshortArray array,jboolean * isCopy)74 jshort *envGetArrayElements(JNIEnv *env, jshortArray array, jboolean *isCopy) {
75     return env->GetShortArrayElements(array, isCopy);
76 }
77 
78 inline
envReleaseArrayElements(JNIEnv * env,jshortArray array,jshort * elems,jint mode)79 void envReleaseArrayElements(JNIEnv *env, jshortArray array, jshort *elems, jint mode) {
80     env->ReleaseShortArrayElements(array, elems, mode);
81 }
82 
83 inline
envGetArrayElements(JNIEnv * env,jfloatArray array,jboolean * isCopy)84 jfloat *envGetArrayElements(JNIEnv *env, jfloatArray array, jboolean *isCopy) {
85     return env->GetFloatArrayElements(array, isCopy);
86 }
87 
88 inline
envReleaseArrayElements(JNIEnv * env,jfloatArray array,jfloat * elems,jint mode)89 void envReleaseArrayElements(JNIEnv *env, jfloatArray array, jfloat *elems, jint mode) {
90     env->ReleaseFloatArrayElements(array, elems, mode);
91 }
92 
93 } // namespace android
94 
95 #endif // ANDROID_SL_UTILS_H
96