1 /* 2 * Copyright (C) 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 package android.media.audiofx; 18 19 import android.annotation.RequiresPermission; 20 import android.app.ActivityThread; 21 import android.util.Log; 22 import java.util.UUID; 23 24 /** 25 * StreamDefaultEffect is a default effect that attaches automatically to all AudioTracks and 26 * MediaPlayer instances of a given stream type. 27 * <p>see {@link android.media.audiofx.DefaultEffect} class for more details on default effects. 28 * @hide 29 */ 30 31 public class StreamDefaultEffect extends DefaultEffect { 32 static { 33 System.loadLibrary("audioeffect_jni"); 34 } 35 36 private final static String TAG = "StreamDefaultEffect-JAVA"; 37 38 /** 39 * Class constructor. 40 * 41 * @param type type of effect engine to be default. This parameter is ignored if uuid is set, 42 * and can be set to {@link android.media.audiofx.AudioEffect#EFFECT_TYPE_NULL} 43 * in that case. 44 * @param uuid unique identifier of a particular effect implementation to be default. This 45 * parameter can be set to 46 * {@link android.media.audiofx.AudioEffect#EFFECT_TYPE_NULL}, in which case only 47 * the type will be used to select the effect. 48 * @param priority the priority level requested by the application for controlling the effect 49 * engine. As the same engine can be shared by several applications, this parameter 50 * indicates how much the requesting application needs control of effect parameters. 51 * The normal priority is 0, above normal is a positive number, below normal a 52 * negative number. 53 * @param streamUsage a USAGE_* constant from {@link android.media.AudioAttributes} indicating 54 * what streams the given effect should attach to by default. Note that similar 55 * usages may share defaults. 56 * 57 * @throws java.lang.IllegalArgumentException 58 * @throws java.lang.UnsupportedOperationException 59 * @throws java.lang.RuntimeException 60 */ 61 @RequiresPermission(value = android.Manifest.permission.MODIFY_DEFAULT_AUDIO_EFFECTS, 62 conditional = true) // Android Things uses an alternate permission. StreamDefaultEffect(UUID type, UUID uuid, int priority, int streamUsage)63 public StreamDefaultEffect(UUID type, UUID uuid, int priority, int streamUsage) { 64 int[] id = new int[1]; 65 int initResult = native_setup(type.toString(), 66 uuid.toString(), 67 priority, 68 streamUsage, 69 ActivityThread.currentOpPackageName(), 70 id); 71 if (initResult != AudioEffect.SUCCESS) { 72 Log.e(TAG, "Error code " + initResult + " when initializing StreamDefaultEffect"); 73 switch (initResult) { 74 case AudioEffect.ERROR_BAD_VALUE: 75 throw (new IllegalArgumentException( 76 "Stream usage, type uuid, or implementation uuid not supported.")); 77 case AudioEffect.ERROR_INVALID_OPERATION: 78 throw (new UnsupportedOperationException( 79 "Effect library not loaded")); 80 default: 81 throw (new RuntimeException( 82 "Cannot initialize effect engine for type: " + type 83 + " Error: " + initResult)); 84 } 85 } 86 87 mId = id[0]; 88 } 89 90 91 /** 92 * Releases the native StreamDefaultEffect resources. It is a good practice to 93 * release the default effect when done with use as control can be returned to 94 * other applications or the native resources released. 95 */ release()96 public void release() { 97 native_release(mId); 98 } 99 100 @Override finalize()101 protected void finalize() { 102 release(); 103 } 104 105 // --------------------------------------------------------- 106 // Native methods called from the Java side 107 // -------------------- 108 native_setup(String type, String uuid, int priority, int streamUsage, String opPackageName, int[] id)109 private native final int native_setup(String type, 110 String uuid, 111 int priority, 112 int streamUsage, 113 String opPackageName, 114 int[] id); 115 native_release(int id)116 private native final void native_release(int id); 117 } 118