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 #ifndef ANDROID_JAUDIOFORMAT_H 18 #define ANDROID_JAUDIOFORMAT_H 19 20 #include <android_media_AudioFormat.h> 21 #include <jni.h> 22 23 namespace android { 24 25 class JAudioFormat { 26 public: 27 /* Creates a Java AudioFormat object. */ 28 static jobject createAudioFormatObj(JNIEnv *env, 29 uint32_t sampleRate, 30 audio_format_t format, 31 audio_channel_mask_t channelMask) { 32 33 jclass jBuilderCls = env->FindClass("android/media/AudioFormat$Builder"); 34 jmethodID jBuilderCtor = env->GetMethodID(jBuilderCls, "<init>", "()V"); 35 jobject jBuilderObj = env->NewObject(jBuilderCls, jBuilderCtor); 36 37 if (sampleRate == 0) { 38 jclass jAudioFormatCls = env->FindClass("android/media/AudioFormat"); 39 jfieldID jSampleRateUnspecified = 40 env->GetStaticFieldID(jAudioFormatCls, "SAMPLE_RATE_UNSPECIFIED", "I"); 41 sampleRate = env->GetStaticIntField(jAudioFormatCls, jSampleRateUnspecified); 42 } 43 44 jmethodID jSetEncoding = env->GetMethodID(jBuilderCls, "setEncoding", 45 "(I)Landroid/media/AudioFormat$Builder;"); 46 jBuilderObj = env->CallObjectMethod(jBuilderObj, jSetEncoding, 47 audioFormatFromNative(format)); 48 49 jmethodID jSetSampleRate = env->GetMethodID(jBuilderCls, "setSampleRate", 50 "(I)Landroid/media/AudioFormat$Builder;"); 51 jBuilderObj = env->CallObjectMethod(jBuilderObj, jSetSampleRate, sampleRate); 52 53 jmethodID jSetChannelMask = env->GetMethodID(jBuilderCls, "setChannelMask", 54 "(I)Landroid/media/AudioFormat$Builder;"); 55 jBuilderObj = env->CallObjectMethod(jBuilderObj, jSetChannelMask, 56 outChannelMaskFromNative(channelMask)); 57 58 jmethodID jBuild = env->GetMethodID(jBuilderCls, "build", "()Landroid/media/AudioFormat;"); 59 return env->CallObjectMethod(jBuilderObj, jBuild); 60 } 61 62 }; 63 64 } // namespace android 65 66 #endif // ANDROID_JAUDIOFORMAT_H 67