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 * SourceDefaultEffect is a default effect that attaches automatically to all AudioRecord and 26 * MediaRecorder instances of a given source type. 27 * <p>see {@link android.media.audiofx.DefaultEffect} class for more details on default effects. 28 * @hide 29 */ 30 31 public class SourceDefaultEffect extends DefaultEffect { 32 static { 33 System.loadLibrary("audioeffect_jni"); 34 } 35 36 private final static String TAG = "SourceDefaultEffect-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 source a MediaRecorder.AudioSource.* constant from 54 * {@link android.media.MediaRecorder.AudioSource} indicating 55 * what sources the given effect should attach to by default. Note that similar 56 * sources may share defaults. 57 * 58 * @throws java.lang.IllegalArgumentException 59 * @throws java.lang.UnsupportedOperationException 60 * @throws java.lang.RuntimeException 61 */ 62 @RequiresPermission(value = android.Manifest.permission.MODIFY_DEFAULT_AUDIO_EFFECTS, 63 conditional = true) // Android Things uses an alternate permission. SourceDefaultEffect(UUID type, UUID uuid, int priority, int source)64 public SourceDefaultEffect(UUID type, UUID uuid, int priority, int source) { 65 int[] id = new int[1]; 66 int initResult = native_setup(type.toString(), 67 uuid.toString(), 68 priority, 69 source, 70 ActivityThread.currentOpPackageName(), 71 id); 72 if (initResult != AudioEffect.SUCCESS) { 73 Log.e(TAG, "Error code " + initResult + " when initializing SourceDefaultEffect"); 74 switch (initResult) { 75 case AudioEffect.ERROR_BAD_VALUE: 76 throw (new IllegalArgumentException( 77 "Source, type uuid, or implementation uuid not supported.")); 78 case AudioEffect.ERROR_INVALID_OPERATION: 79 throw (new UnsupportedOperationException( 80 "Effect library not loaded")); 81 default: 82 throw (new RuntimeException( 83 "Cannot initialize effect engine for type: " + type 84 + " Error: " + initResult)); 85 } 86 } 87 88 mId = id[0]; 89 } 90 91 92 /** 93 * Releases the native SourceDefaultEffect resources. It is a good practice to 94 * release the default effect when done with use as control can be returned to 95 * other applications or the native resources released. 96 */ release()97 public void release() { 98 native_release(mId); 99 } 100 101 @Override finalize()102 protected void finalize() { 103 release(); 104 } 105 106 // --------------------------------------------------------- 107 // Native methods called from the Java side 108 // -------------------- 109 native_setup(String type, String uuid, int priority, int source, String opPackageName, int[] id)110 private native final int native_setup(String type, 111 String uuid, 112 int priority, 113 int source, 114 String opPackageName, 115 int[] id); 116 native_release(int id)117 private native final void native_release(int id); 118 } 119