1 package com.google.oboe.samples.hellooboe; 2 /* 3 * Copyright 2017 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 import android.content.Context; 19 import android.media.AudioManager; 20 import android.os.Build; 21 22 public class PlaybackEngine { 23 24 static long mEngineHandle = 0; 25 26 // Load native library 27 static { 28 System.loadLibrary("hello-oboe"); 29 } 30 create(Context context)31 static boolean create(Context context){ 32 33 if (mEngineHandle == 0){ 34 setDefaultStreamValues(context); 35 mEngineHandle = native_createEngine(); 36 } 37 return (mEngineHandle != 0); 38 } 39 setDefaultStreamValues(Context context)40 private static void setDefaultStreamValues(Context context) { 41 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){ 42 AudioManager myAudioMgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 43 String sampleRateStr = myAudioMgr.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE); 44 int defaultSampleRate = Integer.parseInt(sampleRateStr); 45 String framesPerBurstStr = myAudioMgr.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER); 46 int defaultFramesPerBurst = Integer.parseInt(framesPerBurstStr); 47 48 native_setDefaultStreamValues(defaultSampleRate, defaultFramesPerBurst); 49 } 50 } 51 delete()52 static void delete(){ 53 if (mEngineHandle != 0){ 54 native_deleteEngine(mEngineHandle); 55 } 56 mEngineHandle = 0; 57 } 58 setToneOn(boolean isToneOn)59 static void setToneOn(boolean isToneOn){ 60 if (mEngineHandle != 0) native_setToneOn(mEngineHandle, isToneOn); 61 } 62 setAudioApi(int audioApi)63 static void setAudioApi(int audioApi){ 64 if (mEngineHandle != 0) native_setAudioApi(mEngineHandle, audioApi); 65 } 66 setAudioDeviceId(int deviceId)67 static void setAudioDeviceId(int deviceId){ 68 if (mEngineHandle != 0) native_setAudioDeviceId(mEngineHandle, deviceId); 69 } 70 setChannelCount(int channelCount)71 static void setChannelCount(int channelCount) { 72 if (mEngineHandle != 0) native_setChannelCount(mEngineHandle, channelCount); 73 } 74 setBufferSizeInBursts(int bufferSizeInBursts)75 static void setBufferSizeInBursts(int bufferSizeInBursts){ 76 if (mEngineHandle != 0) native_setBufferSizeInBursts(mEngineHandle, bufferSizeInBursts); 77 } 78 getCurrentOutputLatencyMillis()79 static double getCurrentOutputLatencyMillis(){ 80 if (mEngineHandle == 0) return 0; 81 return native_getCurrentOutputLatencyMillis(mEngineHandle); 82 } 83 isLatencyDetectionSupported()84 static boolean isLatencyDetectionSupported() { 85 return mEngineHandle != 0 && native_isLatencyDetectionSupported(mEngineHandle); 86 } 87 88 // Native methods native_createEngine()89 private static native long native_createEngine(); native_deleteEngine(long engineHandle)90 private static native void native_deleteEngine(long engineHandle); native_setToneOn(long engineHandle, boolean isToneOn)91 private static native void native_setToneOn(long engineHandle, boolean isToneOn); native_setAudioApi(long engineHandle, int audioApi)92 private static native void native_setAudioApi(long engineHandle, int audioApi); native_setAudioDeviceId(long engineHandle, int deviceId)93 private static native void native_setAudioDeviceId(long engineHandle, int deviceId); native_setChannelCount(long mEngineHandle, int channelCount)94 private static native void native_setChannelCount(long mEngineHandle, int channelCount); native_setBufferSizeInBursts(long engineHandle, int bufferSizeInBursts)95 private static native void native_setBufferSizeInBursts(long engineHandle, int bufferSizeInBursts); native_getCurrentOutputLatencyMillis(long engineHandle)96 private static native double native_getCurrentOutputLatencyMillis(long engineHandle); native_isLatencyDetectionSupported(long engineHandle)97 private static native boolean native_isLatencyDetectionSupported(long engineHandle); native_setDefaultStreamValues(int sampleRate, int framesPerBurst)98 private static native void native_setDefaultStreamValues(int sampleRate, int framesPerBurst); 99 } 100