AudioSystem.java revision 3af0fe0c9df2fc1e0a4d6bcd827a4d7aa28dbdeb
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    /* These values must be kept in sync with AudioSystem.h */
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    /* modes for setPhoneState, must match AudioSystem.h audio_mode */
81    public static final int MODE_INVALID            = -2;
82    public static final int MODE_CURRENT            = -1;
83    public static final int MODE_NORMAL             = 0;
84    public static final int MODE_RINGTONE           = 1;
85    public static final int MODE_IN_CALL            = 2;
86    public static final int MODE_IN_COMMUNICATION   = 3;
87    public static final int NUM_MODES               = 4;
88
89
90    /* Routing bits for the former setRouting/getRouting API */
91    /** @deprecated */
92    @Deprecated public static final int ROUTE_EARPIECE          = (1 << 0);
93    /** @deprecated */
94    @Deprecated public static final int ROUTE_SPEAKER           = (1 << 1);
95    /** @deprecated use {@link #ROUTE_BLUETOOTH_SCO} */
96    @Deprecated public static final int ROUTE_BLUETOOTH = (1 << 2);
97    /** @deprecated */
98    @Deprecated public static final int ROUTE_BLUETOOTH_SCO     = (1 << 2);
99    /** @deprecated */
100    @Deprecated public static final int ROUTE_HEADSET           = (1 << 3);
101    /** @deprecated */
102    @Deprecated public static final int ROUTE_BLUETOOTH_A2DP    = (1 << 4);
103    /** @deprecated */
104    @Deprecated public static final int ROUTE_ALL               = 0xFFFFFFFF;
105
106    // Keep in sync with system/core/include/system/audio.h
107    public static final int AUDIO_SESSION_ALLOCATE = 0;
108
109    /*
110     * Checks whether the specified stream type is active.
111     *
112     * return true if any track playing on this stream is active.
113     */
114    public static native boolean isStreamActive(int stream, int inPastMs);
115
116    /*
117     * Checks whether the specified stream type is active on a remotely connected device. The notion
118     * of what constitutes a remote device is enforced by the audio policy manager of the platform.
119     *
120     * return true if any track playing on this stream is active on a remote device.
121     */
122    public static native boolean isStreamActiveRemotely(int stream, int inPastMs);
123
124    /*
125     * Checks whether the specified audio source is active.
126     *
127     * return true if any recorder using this source is currently recording
128     */
129    public static native boolean isSourceActive(int source);
130
131    /*
132     * Sets a group generic audio configuration parameters. The use of these parameters
133     * are platform dependent, see libaudio
134     *
135     * param keyValuePairs  list of parameters key value pairs in the form:
136     *    key1=value1;key2=value2;...
137     */
138    public static native int setParameters(String keyValuePairs);
139
140    /*
141     * Gets a group generic audio configuration parameters. The use of these parameters
142     * are platform dependent, see libaudio
143     *
144     * param keys  list of parameters
145     * return value: list of parameters key value pairs in the form:
146     *    key1=value1;key2=value2;...
147     */
148    public static native String getParameters(String keys);
149
150    // These match the enum AudioError in frameworks/base/core/jni/android_media_AudioSystem.cpp
151    /* Command sucessful or Media server restarted. see ErrorCallback */
152    public static final int AUDIO_STATUS_OK = 0;
153    /* Command failed or unspecified audio error.  see ErrorCallback */
154    public static final int AUDIO_STATUS_ERROR = 1;
155    /* Media server died. see ErrorCallback */
156    public static final int AUDIO_STATUS_SERVER_DIED = 100;
157
158    private static ErrorCallback mErrorCallback;
159
160    /*
161     * Handles the audio error callback.
162     */
163    public interface ErrorCallback
164    {
165        /*
166         * Callback for audio server errors.
167         * param error   error code:
168         * - AUDIO_STATUS_OK
169         * - AUDIO_STATUS_SERVER_DIED
170         * - AUDIO_STATUS_ERROR
171         */
172        void onError(int error);
173    };
174
175    /*
176     * Registers a callback to be invoked when an error occurs.
177     * @param cb the callback to run
178     */
179    public static void setErrorCallback(ErrorCallback cb)
180    {
181        synchronized (AudioSystem.class) {
182            mErrorCallback = cb;
183            if (cb != null) {
184                cb.onError(checkAudioFlinger());
185            }
186        }
187    }
188
189    private static void errorCallbackFromNative(int error)
190    {
191        ErrorCallback errorCallback = null;
192        synchronized (AudioSystem.class) {
193            if (mErrorCallback != null) {
194                errorCallback = mErrorCallback;
195            }
196        }
197        if (errorCallback != null) {
198            errorCallback.onError(error);
199        }
200    }
201
202
203    /*
204     * AudioPolicyService methods
205     */
206
207    //
208    // audio device definitions: must be kept in sync with values in system/core/audio.h
209    //
210
211    // reserved bits
212    public static final int DEVICE_BIT_IN = 0x80000000;
213    public static final int DEVICE_BIT_DEFAULT = 0x40000000;
214    // output devices, be sure to update AudioManager.java also
215    public static final int DEVICE_OUT_EARPIECE = 0x1;
216    public static final int DEVICE_OUT_SPEAKER = 0x2;
217    public static final int DEVICE_OUT_WIRED_HEADSET = 0x4;
218    public static final int DEVICE_OUT_WIRED_HEADPHONE = 0x8;
219    public static final int DEVICE_OUT_BLUETOOTH_SCO = 0x10;
220    public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20;
221    public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40;
222    public static final int DEVICE_OUT_BLUETOOTH_A2DP = 0x80;
223    public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100;
224    public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200;
225    public static final int DEVICE_OUT_AUX_DIGITAL = 0x400;
226    public static final int DEVICE_OUT_ANLG_DOCK_HEADSET = 0x800;
227    public static final int DEVICE_OUT_DGTL_DOCK_HEADSET = 0x1000;
228    public static final int DEVICE_OUT_USB_ACCESSORY = 0x2000;
229    public static final int DEVICE_OUT_USB_DEVICE = 0x4000;
230    public static final int DEVICE_OUT_REMOTE_SUBMIX = 0x8000;
231
232    public static final int DEVICE_OUT_DEFAULT = DEVICE_BIT_DEFAULT;
233
234    public static final int DEVICE_OUT_ALL = (DEVICE_OUT_EARPIECE |
235                                              DEVICE_OUT_SPEAKER |
236                                              DEVICE_OUT_WIRED_HEADSET |
237                                              DEVICE_OUT_WIRED_HEADPHONE |
238                                              DEVICE_OUT_BLUETOOTH_SCO |
239                                              DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
240                                              DEVICE_OUT_BLUETOOTH_SCO_CARKIT |
241                                              DEVICE_OUT_BLUETOOTH_A2DP |
242                                              DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
243                                              DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER |
244                                              DEVICE_OUT_AUX_DIGITAL |
245                                              DEVICE_OUT_ANLG_DOCK_HEADSET |
246                                              DEVICE_OUT_DGTL_DOCK_HEADSET |
247                                              DEVICE_OUT_USB_ACCESSORY |
248                                              DEVICE_OUT_USB_DEVICE |
249                                              DEVICE_OUT_REMOTE_SUBMIX |
250                                              DEVICE_OUT_DEFAULT);
251    public static final int DEVICE_OUT_ALL_A2DP = (DEVICE_OUT_BLUETOOTH_A2DP |
252                                                   DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
253                                                   DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
254    public static final int DEVICE_OUT_ALL_SCO = (DEVICE_OUT_BLUETOOTH_SCO |
255                                                  DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
256                                                  DEVICE_OUT_BLUETOOTH_SCO_CARKIT);
257    public static final int DEVICE_OUT_ALL_USB = (DEVICE_OUT_USB_ACCESSORY |
258                                                  DEVICE_OUT_USB_DEVICE);
259
260    // input devices
261    public static final int DEVICE_IN_COMMUNICATION = DEVICE_BIT_IN | 0x1;
262    public static final int DEVICE_IN_AMBIENT = DEVICE_BIT_IN | 0x2;
263    public static final int DEVICE_IN_BUILTIN_MIC = DEVICE_BIT_IN | 0x4;
264    public static final int DEVICE_IN_BLUETOOTH_SCO_HEADSET = DEVICE_BIT_IN | 0x8;
265    public static final int DEVICE_IN_WIRED_HEADSET = DEVICE_BIT_IN | 0x10;
266    public static final int DEVICE_IN_AUX_DIGITAL = DEVICE_BIT_IN | 0x20;
267    public static final int DEVICE_IN_VOICE_CALL = DEVICE_BIT_IN | 0x40;
268    public static final int DEVICE_IN_BACK_MIC = DEVICE_BIT_IN | 0x80;
269    public static final int DEVICE_IN_REMOTE_SUBMIX = DEVICE_BIT_IN | 0x100;
270    public static final int DEVICE_IN_ANLG_DOCK_HEADSET = DEVICE_BIT_IN | 0x200;
271    public static final int DEVICE_IN_DGTL_DOCK_HEADSET = DEVICE_BIT_IN | 0x400;
272    public static final int DEVICE_IN_USB_ACCESSORY = DEVICE_BIT_IN | 0x800;
273    public static final int DEVICE_IN_USB_DEVICE = DEVICE_BIT_IN | 0x1000;
274    public static final int DEVICE_IN_DEFAULT = DEVICE_BIT_IN | DEVICE_BIT_DEFAULT;
275
276    public static final int DEVICE_IN_ALL = (DEVICE_IN_COMMUNICATION |
277                                             DEVICE_IN_AMBIENT |
278                                             DEVICE_IN_BUILTIN_MIC |
279                                             DEVICE_IN_BLUETOOTH_SCO_HEADSET |
280                                             DEVICE_IN_WIRED_HEADSET |
281                                             DEVICE_IN_AUX_DIGITAL |
282                                             DEVICE_IN_VOICE_CALL |
283                                             DEVICE_IN_BACK_MIC |
284                                             DEVICE_IN_REMOTE_SUBMIX |
285                                             DEVICE_IN_ANLG_DOCK_HEADSET |
286                                             DEVICE_IN_DGTL_DOCK_HEADSET |
287                                             DEVICE_IN_USB_ACCESSORY |
288                                             DEVICE_IN_USB_DEVICE |
289                                             DEVICE_IN_DEFAULT);
290    public static final int DEVICE_IN_ALL_SCO = DEVICE_IN_BLUETOOTH_SCO_HEADSET;
291    public static final int DEVICE_IN_ALL_USB = (DEVICE_IN_USB_ACCESSORY |
292                                                 DEVICE_IN_USB_DEVICE);
293
294    // device states, must match AudioSystem::device_connection_state
295    public static final int DEVICE_STATE_UNAVAILABLE = 0;
296    public static final int DEVICE_STATE_AVAILABLE = 1;
297    private static final int NUM_DEVICE_STATES = 1;
298
299    public static final String DEVICE_OUT_EARPIECE_NAME = "earpiece";
300    public static final String DEVICE_OUT_SPEAKER_NAME = "speaker";
301    public static final String DEVICE_OUT_WIRED_HEADSET_NAME = "headset";
302    public static final String DEVICE_OUT_WIRED_HEADPHONE_NAME = "headphone";
303    public static final String DEVICE_OUT_BLUETOOTH_SCO_NAME = "bt_sco";
304    public static final String DEVICE_OUT_BLUETOOTH_SCO_HEADSET_NAME = "bt_sco_hs";
305    public static final String DEVICE_OUT_BLUETOOTH_SCO_CARKIT_NAME = "bt_sco_carkit";
306    public static final String DEVICE_OUT_BLUETOOTH_A2DP_NAME = "bt_a2dp";
307    public static final String DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES_NAME = "bt_a2dp_hp";
308    public static final String DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER_NAME = "bt_a2dp_spk";
309    public static final String DEVICE_OUT_AUX_DIGITAL_NAME = "aux_digital";
310    public static final String DEVICE_OUT_ANLG_DOCK_HEADSET_NAME = "analog_dock";
311    public static final String DEVICE_OUT_DGTL_DOCK_HEADSET_NAME = "digital_dock";
312    public static final String DEVICE_OUT_USB_ACCESSORY_NAME = "usb_accessory";
313    public static final String DEVICE_OUT_USB_DEVICE_NAME = "usb_device";
314    public static final String DEVICE_OUT_REMOTE_SUBMIX_NAME = "remote_submix";
315
316    public static String getDeviceName(int device)
317    {
318        switch(device) {
319        case DEVICE_OUT_EARPIECE:
320            return DEVICE_OUT_EARPIECE_NAME;
321        case DEVICE_OUT_SPEAKER:
322            return DEVICE_OUT_SPEAKER_NAME;
323        case DEVICE_OUT_WIRED_HEADSET:
324            return DEVICE_OUT_WIRED_HEADSET_NAME;
325        case DEVICE_OUT_WIRED_HEADPHONE:
326            return DEVICE_OUT_WIRED_HEADPHONE_NAME;
327        case DEVICE_OUT_BLUETOOTH_SCO:
328            return DEVICE_OUT_BLUETOOTH_SCO_NAME;
329        case DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
330            return DEVICE_OUT_BLUETOOTH_SCO_HEADSET_NAME;
331        case DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
332            return DEVICE_OUT_BLUETOOTH_SCO_CARKIT_NAME;
333        case DEVICE_OUT_BLUETOOTH_A2DP:
334            return DEVICE_OUT_BLUETOOTH_A2DP_NAME;
335        case DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
336            return DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES_NAME;
337        case DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
338            return DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER_NAME;
339        case DEVICE_OUT_AUX_DIGITAL:
340            return DEVICE_OUT_AUX_DIGITAL_NAME;
341        case DEVICE_OUT_ANLG_DOCK_HEADSET:
342            return DEVICE_OUT_ANLG_DOCK_HEADSET_NAME;
343        case DEVICE_OUT_DGTL_DOCK_HEADSET:
344            return DEVICE_OUT_DGTL_DOCK_HEADSET_NAME;
345        case DEVICE_OUT_USB_ACCESSORY:
346            return DEVICE_OUT_USB_ACCESSORY_NAME;
347        case DEVICE_OUT_USB_DEVICE:
348            return DEVICE_OUT_USB_DEVICE_NAME;
349        case DEVICE_OUT_REMOTE_SUBMIX:
350            return DEVICE_OUT_REMOTE_SUBMIX_NAME;
351        case DEVICE_OUT_DEFAULT:
352        default:
353            return "";
354        }
355    }
356
357    // phone state, match audio_mode???
358    public static final int PHONE_STATE_OFFCALL = 0;
359    public static final int PHONE_STATE_RINGING = 1;
360    public static final int PHONE_STATE_INCALL = 2;
361
362    // device categories config for setForceUse, must match AudioSystem::forced_config
363    public static final int FORCE_NONE = 0;
364    public static final int FORCE_SPEAKER = 1;
365    public static final int FORCE_HEADPHONES = 2;
366    public static final int FORCE_BT_SCO = 3;
367    public static final int FORCE_BT_A2DP = 4;
368    public static final int FORCE_WIRED_ACCESSORY = 5;
369    public static final int FORCE_BT_CAR_DOCK = 6;
370    public static final int FORCE_BT_DESK_DOCK = 7;
371    public static final int FORCE_ANALOG_DOCK = 8;
372    public static final int FORCE_DIGITAL_DOCK = 9;
373    public static final int FORCE_NO_BT_A2DP = 10;
374    public static final int FORCE_SYSTEM_ENFORCED = 11;
375    private static final int NUM_FORCE_CONFIG = 12;
376    public static final int FORCE_DEFAULT = FORCE_NONE;
377
378    // usage for setForceUse, must match AudioSystem::force_use
379    public static final int FOR_COMMUNICATION = 0;
380    public static final int FOR_MEDIA = 1;
381    public static final int FOR_RECORD = 2;
382    public static final int FOR_DOCK = 3;
383    public static final int FOR_SYSTEM = 4;
384    private static final int NUM_FORCE_USE = 5;
385
386    // usage for AudioRecord.startRecordingSync(), must match AudioSystem::sync_event_t
387    public static final int SYNC_EVENT_NONE = 0;
388    public static final int SYNC_EVENT_PRESENTATION_COMPLETE = 1;
389
390    public static native int setDeviceConnectionState(int device, int state, String device_address);
391    public static native int getDeviceConnectionState(int device, String device_address);
392    public static native int setPhoneState(int state);
393    public static native int setForceUse(int usage, int config);
394    public static native int getForceUse(int usage);
395    public static native int initStreamVolume(int stream, int indexMin, int indexMax);
396    public static native int setStreamVolumeIndex(int stream, int index, int device);
397    public static native int getStreamVolumeIndex(int stream, int device);
398    public static native int setMasterVolume(float value);
399    public static native float getMasterVolume();
400    public static native int setMasterMute(boolean mute);
401    public static native boolean getMasterMute();
402    public static native int getDevicesForStream(int stream);
403
404    // helpers for android.media.AudioManager.getProperty(), see description there for meaning
405    public static native int getPrimaryOutputSamplingRate();
406    public static native int getPrimaryOutputFrameCount();
407    public static native int getOutputLatency(int stream);
408
409    public static native int setLowRamDevice(boolean isLowRamDevice);
410    public static native int checkAudioFlinger();
411}
412