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