AudioSystem.java revision 8b4b97a14ad9b5b982d8fe92755efabec8ad0076
1/*
2 * Copyright (C) 2006 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
17package android.media;
18
19
20/* IF YOU CHANGE ANY OF THE CONSTANTS IN THIS FILE, DO NOT FORGET
21 * TO UPDATE THE CORRESPONDING NATIVE GLUE AND AudioManager.java.
22 * THANK YOU FOR YOUR COOPERATION.
23 */
24
25/**
26 * @hide
27 */
28public class AudioSystem
29{
30    /* FIXME: Need to finalize this and correlate with native layer */
31    /*
32     * If these are modified, please also update Settings.System.VOLUME_SETTINGS
33     * and attrs.xml and AudioManager.java.
34     */
35    /* The audio stream for phone calls */
36    public static final int STREAM_VOICE_CALL = 0;
37    /* The audio stream for system sounds */
38    public static final int STREAM_SYSTEM = 1;
39    /* The audio stream for the phone ring and message alerts */
40    public static final int STREAM_RING = 2;
41    /* The audio stream for music playback */
42    public static final int STREAM_MUSIC = 3;
43    /* The audio stream for alarms */
44    public static final int STREAM_ALARM = 4;
45    /* The audio stream for notifications */
46    public static final int STREAM_NOTIFICATION = 5;
47    /* @hide The audio stream for phone calls when connected on bluetooth */
48    public static final int STREAM_BLUETOOTH_SCO = 6;
49    /* @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */
50    public static final int STREAM_SYSTEM_ENFORCED = 7;
51    /* @hide The audio stream for DTMF tones */
52    public static final int STREAM_DTMF = 8;
53    /* @hide The audio stream for text to speech (TTS) */
54    public static final int STREAM_TTS = 9;
55    /**
56     * @deprecated Use {@link #numStreamTypes() instead}
57     */
58    public static final int NUM_STREAMS = 5;
59
60    // Expose only the getter method publicly so we can change it in the future
61    private static final int NUM_STREAM_TYPES = 10;
62    public static final int getNumStreamTypes() { return NUM_STREAM_TYPES; }
63
64    /*
65     * Sets the microphone mute on or off.
66     *
67     * param on set <var>true</var> to mute the microphone;
68     *           <var>false</var> to turn mute off
69     * return command completion status see AUDIO_STATUS_OK, see AUDIO_STATUS_ERROR
70     */
71    public static native int muteMicrophone(boolean on);
72
73    /*
74     * Checks whether the microphone mute is on or off.
75     *
76     * return true if microphone is muted, false if it's not
77     */
78    public static native boolean isMicrophoneMuted();
79
80    /*
81     * Sets the audio mode.
82     *
83     * param mode  the requested audio mode (NORMAL, RINGTONE, or IN_CALL).
84     *              Informs the HAL about the current audio state so that
85     *              it can route the audio appropriately.
86     * return command completion status see AUDIO_STATUS_OK, see AUDIO_STATUS_ERROR
87     */
88    /** @deprecated use {@link #setPhoneState(int)} */
89    public static int setMode(int mode) {
90        return AUDIO_STATUS_ERROR;
91    }
92    /*
93     * Returns the current audio mode.
94     *
95     * return      the current audio mode (NORMAL, RINGTONE, or IN_CALL).
96     *              Returns the current current audio state from the HAL.
97     *
98     */
99    /** @deprecated Do not use. */
100    public static int getMode() {
101        return MODE_INVALID;
102    }
103
104    /* modes for setPhoneState */
105    public static final int MODE_INVALID            = -2;
106    public static final int MODE_CURRENT            = -1;
107    public static final int MODE_NORMAL             = 0;
108    public static final int MODE_RINGTONE           = 1;
109    public static final int MODE_IN_CALL            = 2;
110    public static final int MODE_IN_COMMUNICATION   = 3;
111    public static final int NUM_MODES               = 4;
112
113
114    /* Routing bits for setRouting/getRouting API */
115    /** @deprecated */
116    @Deprecated public static final int ROUTE_EARPIECE          = (1 << 0);
117    /** @deprecated */
118    @Deprecated public static final int ROUTE_SPEAKER           = (1 << 1);
119    /** @deprecated use {@link #ROUTE_BLUETOOTH_SCO} */
120    @Deprecated public static final int ROUTE_BLUETOOTH = (1 << 2);
121    /** @deprecated */
122    @Deprecated public static final int ROUTE_BLUETOOTH_SCO     = (1 << 2);
123    /** @deprecated */
124    @Deprecated public static final int ROUTE_HEADSET           = (1 << 3);
125    /** @deprecated */
126    @Deprecated public static final int ROUTE_BLUETOOTH_A2DP    = (1 << 4);
127    /** @deprecated */
128    @Deprecated public static final int ROUTE_ALL               = 0xFFFFFFFF;
129
130    /*
131     * Sets the audio routing for a specified mode
132     *
133     * param mode   audio mode to change route. E.g., MODE_RINGTONE.
134     * param routes bit vector of routes requested, created from one or
135     *               more of ROUTE_xxx types. Set bits indicate that route should be on
136     * param mask   bit vector of routes to change, created from one or more of
137     * ROUTE_xxx types. Unset bits indicate the route should be left unchanged
138     * return command completion status see AUDIO_STATUS_OK, see AUDIO_STATUS_ERROR
139     */
140    /** @deprecated use {@link #setDeviceConnectionState(int,int,String)} */
141    public static int setRouting(int mode, int routes, int mask) {
142        return AUDIO_STATUS_ERROR;
143    }
144
145    /*
146     * Returns the current audio routing bit vector for a specified mode.
147     *
148     * param mode audio mode to change route (e.g., MODE_RINGTONE)
149     * return an audio route bit vector that can be compared with ROUTE_xxx
150     * bits
151     */
152    /** @deprecated use {@link #getDeviceConnectionState(int,String)} */
153    public static int getRouting(int mode) {
154        return 0;
155    }
156
157    /*
158     * Checks whether the specified stream type is active.
159     *
160     * return true if any track playing on this stream is active.
161     */
162    public static native boolean isStreamActive(int stream, int inPastMs);
163
164    /*
165     * Sets a group generic audio configuration parameters. The use of these parameters
166     * are platform dependant, see libaudio
167     *
168     * param keyValuePairs  list of parameters key value pairs in the form:
169     *    key1=value1;key2=value2;...
170     */
171    public static native int setParameters(String keyValuePairs);
172
173    /*
174     * Gets a group generic audio configuration parameters. The use of these parameters
175     * are platform dependant, see libaudio
176     *
177     * param keys  list of parameters
178     * return value: list of parameters key value pairs in the form:
179     *    key1=value1;key2=value2;...
180     */
181    public static native String getParameters(String keys);
182
183    /*
184    private final static String TAG = "audio";
185
186    private void log(String msg) {
187        Log.d(TAG, "[AudioSystem] " + msg);
188    }
189    */
190
191    // These match the enum in libs/android_runtime/android_media_AudioSystem.cpp
192    /* Command sucessful or Media server restarted. see ErrorCallback */
193    public static final int AUDIO_STATUS_OK = 0;
194    /* Command failed or unspecified audio error.  see ErrorCallback */
195    public static final int AUDIO_STATUS_ERROR = 1;
196    /* Media server died. see ErrorCallback */
197    public static final int AUDIO_STATUS_SERVER_DIED = 100;
198
199    private static ErrorCallback mErrorCallback;
200
201    /*
202     * Handles the audio error callback.
203     */
204    public interface ErrorCallback
205    {
206        /*
207         * Callback for audio server errors.
208         * param error   error code:
209         * - AUDIO_STATUS_OK
210         * - AUDIO_STATUS_SERVER_DIED
211         * - AUDIO_STATUS_ERROR
212         */
213        void onError(int error);
214    };
215
216    /*
217     * Registers a callback to be invoked when an error occurs.
218     * param cb the callback to run
219     */
220    public static void setErrorCallback(ErrorCallback cb)
221    {
222        mErrorCallback = cb;
223    }
224
225    private static void errorCallbackFromNative(int error)
226    {
227        if (mErrorCallback != null) {
228            mErrorCallback.onError(error);
229        }
230    }
231
232    /*
233     * AudioPolicyService methods
234     */
235
236    // output devices, be sure to update AudioManager.java also
237    public static final int DEVICE_OUT_EARPIECE = 0x1;
238    public static final int DEVICE_OUT_SPEAKER = 0x2;
239    public static final int DEVICE_OUT_WIRED_HEADSET = 0x4;
240    public static final int DEVICE_OUT_WIRED_HEADPHONE = 0x8;
241    public static final int DEVICE_OUT_BLUETOOTH_SCO = 0x10;
242    public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20;
243    public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40;
244    public static final int DEVICE_OUT_BLUETOOTH_A2DP = 0x80;
245    public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100;
246    public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200;
247    public static final int DEVICE_OUT_AUX_DIGITAL = 0x400;
248    public static final int DEVICE_OUT_ANLG_DOCK_HEADSET = 0x800;
249    public static final int DEVICE_OUT_DGTL_DOCK_HEADSET = 0x1000;
250    public static final int DEVICE_OUT_DEFAULT = 0x8000;
251    // input devices
252    public static final int DEVICE_IN_COMMUNICATION = 0x10000;
253    public static final int DEVICE_IN_AMBIENT = 0x20000;
254    public static final int DEVICE_IN_BUILTIN_MIC1 = 0x40000;
255    public static final int DEVICE_IN_BUILTIN_MIC2 = 0x80000;
256    public static final int DEVICE_IN_MIC_ARRAY = 0x100000;
257    public static final int DEVICE_IN_BLUETOOTH_SCO_HEADSET = 0x200000;
258    public static final int DEVICE_IN_WIRED_HEADSET = 0x400000;
259    public static final int DEVICE_IN_AUX_DIGITAL = 0x800000;
260    public static final int DEVICE_IN_DEFAULT = 0x80000000;
261
262    // device states
263    public static final int DEVICE_STATE_UNAVAILABLE = 0;
264    public static final int DEVICE_STATE_AVAILABLE = 1;
265
266    // phone state
267    public static final int PHONE_STATE_OFFCALL = 0;
268    public static final int PHONE_STATE_RINGING = 1;
269    public static final int PHONE_STATE_INCALL = 2;
270
271    // config for setForceUse
272    public static final int FORCE_NONE = 0;
273    public static final int FORCE_SPEAKER = 1;
274    public static final int FORCE_HEADPHONES = 2;
275    public static final int FORCE_BT_SCO = 3;
276    public static final int FORCE_BT_A2DP = 4;
277    public static final int FORCE_WIRED_ACCESSORY = 5;
278    public static final int FORCE_BT_CAR_DOCK = 6;
279    public static final int FORCE_BT_DESK_DOCK = 7;
280    public static final int FORCE_ANALOG_DOCK = 8;
281    public static final int FORCE_DIGITAL_DOCK = 9;
282    public static final int FORCE_DEFAULT = FORCE_NONE;
283
284    // usage for serForceUse
285    public static final int FOR_COMMUNICATION = 0;
286    public static final int FOR_MEDIA = 1;
287    public static final int FOR_RECORD = 2;
288    public static final int FOR_DOCK = 3;
289
290    public static native int setDeviceConnectionState(int device, int state, String device_address);
291    public static native int getDeviceConnectionState(int device, String device_address);
292    public static native int setPhoneState(int state);
293    public static native int setRingerMode(int mode, int mask);
294    public static native int setForceUse(int usage, int config);
295    public static native int getForceUse(int usage);
296    public static native int initStreamVolume(int stream, int indexMin, int indexMax);
297    public static native int setStreamVolumeIndex(int stream, int index);
298    public static native int getStreamVolumeIndex(int stream);
299    public static native int getDevicesForStream(int stream);
300}
301