AudioSystem.java revision 948d32748caaac5be06c991ebf00f74265a7849f
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     * Error codes used by public APIs (AudioTrack, AudioRecord, AudioManager ...)
204     * Must be kept in sync with frameworks/base/core/jni/android_media_AudioErrors.h
205     */
206    public static final int SUCCESS            = 0;
207    public static final int ERROR              = -1;
208    public static final int BAD_VALUE          = -2;
209    public static final int INVALID_OPERATION  = -3;
210    public static final int PERMISSION_DENIED  = -4;
211    public static final int NO_INIT            = -5;
212    public static final int DEAD_OBJECT        = -6;
213
214    /*
215     * AudioPolicyService methods
216     */
217
218    //
219    // audio device definitions: must be kept in sync with values in system/core/audio.h
220    //
221
222    // reserved bits
223    public static final int DEVICE_BIT_IN = 0x80000000;
224    public static final int DEVICE_BIT_DEFAULT = 0x40000000;
225    // output devices, be sure to update AudioManager.java also
226    public static final int DEVICE_OUT_EARPIECE = 0x1;
227    public static final int DEVICE_OUT_SPEAKER = 0x2;
228    public static final int DEVICE_OUT_WIRED_HEADSET = 0x4;
229    public static final int DEVICE_OUT_WIRED_HEADPHONE = 0x8;
230    public static final int DEVICE_OUT_BLUETOOTH_SCO = 0x10;
231    public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20;
232    public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40;
233    public static final int DEVICE_OUT_BLUETOOTH_A2DP = 0x80;
234    public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100;
235    public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200;
236    public static final int DEVICE_OUT_AUX_DIGITAL = 0x400;
237    public static final int DEVICE_OUT_HDMI = DEVICE_OUT_AUX_DIGITAL;
238    public static final int DEVICE_OUT_ANLG_DOCK_HEADSET = 0x800;
239    public static final int DEVICE_OUT_DGTL_DOCK_HEADSET = 0x1000;
240    public static final int DEVICE_OUT_USB_ACCESSORY = 0x2000;
241    public static final int DEVICE_OUT_USB_DEVICE = 0x4000;
242    public static final int DEVICE_OUT_REMOTE_SUBMIX = 0x8000;
243    public static final int DEVICE_OUT_TELEPHONY_TX = 0x10000;
244    public static final int DEVICE_OUT_LINE = 0x20000;
245    public static final int DEVICE_OUT_HDMI_ARC = 0x40000;
246    public static final int DEVICE_OUT_SPDIF = 0x80000;
247    public static final int DEVICE_OUT_FM = 0x100000;
248
249    public static final int DEVICE_OUT_DEFAULT = DEVICE_BIT_DEFAULT;
250
251    public static final int DEVICE_OUT_ALL = (DEVICE_OUT_EARPIECE |
252                                              DEVICE_OUT_SPEAKER |
253                                              DEVICE_OUT_WIRED_HEADSET |
254                                              DEVICE_OUT_WIRED_HEADPHONE |
255                                              DEVICE_OUT_BLUETOOTH_SCO |
256                                              DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
257                                              DEVICE_OUT_BLUETOOTH_SCO_CARKIT |
258                                              DEVICE_OUT_BLUETOOTH_A2DP |
259                                              DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
260                                              DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER |
261                                              DEVICE_OUT_HDMI |
262                                              DEVICE_OUT_ANLG_DOCK_HEADSET |
263                                              DEVICE_OUT_DGTL_DOCK_HEADSET |
264                                              DEVICE_OUT_USB_ACCESSORY |
265                                              DEVICE_OUT_USB_DEVICE |
266                                              DEVICE_OUT_REMOTE_SUBMIX |
267                                              DEVICE_OUT_TELEPHONY_TX |
268                                              DEVICE_OUT_LINE |
269                                              DEVICE_OUT_HDMI_ARC |
270                                              DEVICE_OUT_SPDIF |
271                                              DEVICE_OUT_FM |
272                                              DEVICE_OUT_DEFAULT);
273    public static final int DEVICE_OUT_ALL_A2DP = (DEVICE_OUT_BLUETOOTH_A2DP |
274                                                   DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
275                                                   DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
276    public static final int DEVICE_OUT_ALL_SCO = (DEVICE_OUT_BLUETOOTH_SCO |
277                                                  DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
278                                                  DEVICE_OUT_BLUETOOTH_SCO_CARKIT);
279    public static final int DEVICE_OUT_ALL_USB = (DEVICE_OUT_USB_ACCESSORY |
280                                                  DEVICE_OUT_USB_DEVICE);
281
282    // input devices
283    public static final int DEVICE_IN_COMMUNICATION = DEVICE_BIT_IN | 0x1;
284    public static final int DEVICE_IN_AMBIENT = DEVICE_BIT_IN | 0x2;
285    public static final int DEVICE_IN_BUILTIN_MIC = DEVICE_BIT_IN | 0x4;
286    public static final int DEVICE_IN_BLUETOOTH_SCO_HEADSET = DEVICE_BIT_IN | 0x8;
287    public static final int DEVICE_IN_WIRED_HEADSET = DEVICE_BIT_IN | 0x10;
288    public static final int DEVICE_IN_AUX_DIGITAL = DEVICE_BIT_IN | 0x20;
289    public static final int DEVICE_IN_HDMI = DEVICE_IN_AUX_DIGITAL;
290    public static final int DEVICE_IN_VOICE_CALL = DEVICE_BIT_IN | 0x40;
291    public static final int DEVICE_IN_TELEPHONY_RX = DEVICE_IN_VOICE_CALL;
292    public static final int DEVICE_IN_BACK_MIC = DEVICE_BIT_IN | 0x80;
293    public static final int DEVICE_IN_REMOTE_SUBMIX = DEVICE_BIT_IN | 0x100;
294    public static final int DEVICE_IN_ANLG_DOCK_HEADSET = DEVICE_BIT_IN | 0x200;
295    public static final int DEVICE_IN_DGTL_DOCK_HEADSET = DEVICE_BIT_IN | 0x400;
296    public static final int DEVICE_IN_USB_ACCESSORY = DEVICE_BIT_IN | 0x800;
297    public static final int DEVICE_IN_USB_DEVICE = DEVICE_BIT_IN | 0x1000;
298    public static final int DEVICE_IN_FM_TUNER = DEVICE_BIT_IN | 0x2000;
299    public static final int DEVICE_IN_TV_TUNER = DEVICE_BIT_IN | 0x4000;
300    public static final int DEVICE_IN_LINE = DEVICE_BIT_IN | 0x8000;
301    public static final int DEVICE_IN_SPDIF = DEVICE_BIT_IN | 0x10000;
302
303    public static final int DEVICE_IN_DEFAULT = DEVICE_BIT_IN | DEVICE_BIT_DEFAULT;
304
305    public static final int DEVICE_IN_ALL = (DEVICE_IN_COMMUNICATION |
306                                             DEVICE_IN_AMBIENT |
307                                             DEVICE_IN_BUILTIN_MIC |
308                                             DEVICE_IN_BLUETOOTH_SCO_HEADSET |
309                                             DEVICE_IN_WIRED_HEADSET |
310                                             DEVICE_IN_HDMI |
311                                             DEVICE_IN_TELEPHONY_RX |
312                                             DEVICE_IN_BACK_MIC |
313                                             DEVICE_IN_REMOTE_SUBMIX |
314                                             DEVICE_IN_ANLG_DOCK_HEADSET |
315                                             DEVICE_IN_DGTL_DOCK_HEADSET |
316                                             DEVICE_IN_USB_ACCESSORY |
317                                             DEVICE_IN_USB_DEVICE |
318                                             DEVICE_IN_FM_TUNER |
319                                             DEVICE_IN_TV_TUNER |
320                                             DEVICE_IN_LINE |
321                                             DEVICE_IN_SPDIF |
322                                             DEVICE_IN_DEFAULT);
323    public static final int DEVICE_IN_ALL_SCO = DEVICE_IN_BLUETOOTH_SCO_HEADSET;
324    public static final int DEVICE_IN_ALL_USB = (DEVICE_IN_USB_ACCESSORY |
325                                                 DEVICE_IN_USB_DEVICE);
326
327    // device states, must match AudioSystem::device_connection_state
328    public static final int DEVICE_STATE_UNAVAILABLE = 0;
329    public static final int DEVICE_STATE_AVAILABLE = 1;
330    private static final int NUM_DEVICE_STATES = 1;
331
332    public static final String DEVICE_OUT_EARPIECE_NAME = "earpiece";
333    public static final String DEVICE_OUT_SPEAKER_NAME = "speaker";
334    public static final String DEVICE_OUT_WIRED_HEADSET_NAME = "headset";
335    public static final String DEVICE_OUT_WIRED_HEADPHONE_NAME = "headphone";
336    public static final String DEVICE_OUT_BLUETOOTH_SCO_NAME = "bt_sco";
337    public static final String DEVICE_OUT_BLUETOOTH_SCO_HEADSET_NAME = "bt_sco_hs";
338    public static final String DEVICE_OUT_BLUETOOTH_SCO_CARKIT_NAME = "bt_sco_carkit";
339    public static final String DEVICE_OUT_BLUETOOTH_A2DP_NAME = "bt_a2dp";
340    public static final String DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES_NAME = "bt_a2dp_hp";
341    public static final String DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER_NAME = "bt_a2dp_spk";
342    public static final String DEVICE_OUT_AUX_DIGITAL_NAME = "aux_digital";
343    public static final String DEVICE_OUT_HDMI_NAME = "hdmi";
344    public static final String DEVICE_OUT_ANLG_DOCK_HEADSET_NAME = "analog_dock";
345    public static final String DEVICE_OUT_DGTL_DOCK_HEADSET_NAME = "digital_dock";
346    public static final String DEVICE_OUT_USB_ACCESSORY_NAME = "usb_accessory";
347    public static final String DEVICE_OUT_USB_DEVICE_NAME = "usb_device";
348    public static final String DEVICE_OUT_REMOTE_SUBMIX_NAME = "remote_submix";
349    public static final String DEVICE_OUT_TELEPHONY_TX_NAME = "telephony_tx";
350    public static final String DEVICE_OUT_LINE_NAME = "line";
351    public static final String DEVICE_OUT_HDMI_ARC_NAME = "hmdi_arc";
352    public static final String DEVICE_OUT_SPDIF_NAME = "spdif";
353    public static final String DEVICE_OUT_FM_NAME = "fm_transmitter";
354
355    public static String getOutputDeviceName(int device)
356    {
357        switch(device) {
358        case DEVICE_OUT_EARPIECE:
359            return DEVICE_OUT_EARPIECE_NAME;
360        case DEVICE_OUT_SPEAKER:
361            return DEVICE_OUT_SPEAKER_NAME;
362        case DEVICE_OUT_WIRED_HEADSET:
363            return DEVICE_OUT_WIRED_HEADSET_NAME;
364        case DEVICE_OUT_WIRED_HEADPHONE:
365            return DEVICE_OUT_WIRED_HEADPHONE_NAME;
366        case DEVICE_OUT_BLUETOOTH_SCO:
367            return DEVICE_OUT_BLUETOOTH_SCO_NAME;
368        case DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
369            return DEVICE_OUT_BLUETOOTH_SCO_HEADSET_NAME;
370        case DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
371            return DEVICE_OUT_BLUETOOTH_SCO_CARKIT_NAME;
372        case DEVICE_OUT_BLUETOOTH_A2DP:
373            return DEVICE_OUT_BLUETOOTH_A2DP_NAME;
374        case DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
375            return DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES_NAME;
376        case DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
377            return DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER_NAME;
378        case DEVICE_OUT_HDMI:
379            return DEVICE_OUT_HDMI_NAME;
380        case DEVICE_OUT_ANLG_DOCK_HEADSET:
381            return DEVICE_OUT_ANLG_DOCK_HEADSET_NAME;
382        case DEVICE_OUT_DGTL_DOCK_HEADSET:
383            return DEVICE_OUT_DGTL_DOCK_HEADSET_NAME;
384        case DEVICE_OUT_USB_ACCESSORY:
385            return DEVICE_OUT_USB_ACCESSORY_NAME;
386        case DEVICE_OUT_USB_DEVICE:
387            return DEVICE_OUT_USB_DEVICE_NAME;
388        case DEVICE_OUT_REMOTE_SUBMIX:
389            return DEVICE_OUT_REMOTE_SUBMIX_NAME;
390        case DEVICE_OUT_TELEPHONY_TX:
391            return DEVICE_OUT_TELEPHONY_TX_NAME;
392        case DEVICE_OUT_LINE:
393            return DEVICE_OUT_LINE_NAME;
394        case DEVICE_OUT_HDMI_ARC:
395            return DEVICE_OUT_HDMI_ARC_NAME;
396        case DEVICE_OUT_SPDIF:
397            return DEVICE_OUT_SPDIF_NAME;
398        case DEVICE_OUT_FM:
399            return DEVICE_OUT_FM_NAME;
400        case DEVICE_OUT_DEFAULT:
401        default:
402            return "";
403        }
404    }
405
406
407    // phone state, match audio_mode???
408    public static final int PHONE_STATE_OFFCALL = 0;
409    public static final int PHONE_STATE_RINGING = 1;
410    public static final int PHONE_STATE_INCALL = 2;
411
412    // device categories config for setForceUse, must match AudioSystem::forced_config
413    public static final int FORCE_NONE = 0;
414    public static final int FORCE_SPEAKER = 1;
415    public static final int FORCE_HEADPHONES = 2;
416    public static final int FORCE_BT_SCO = 3;
417    public static final int FORCE_BT_A2DP = 4;
418    public static final int FORCE_WIRED_ACCESSORY = 5;
419    public static final int FORCE_BT_CAR_DOCK = 6;
420    public static final int FORCE_BT_DESK_DOCK = 7;
421    public static final int FORCE_ANALOG_DOCK = 8;
422    public static final int FORCE_DIGITAL_DOCK = 9;
423    public static final int FORCE_NO_BT_A2DP = 10;
424    public static final int FORCE_SYSTEM_ENFORCED = 11;
425    private static final int NUM_FORCE_CONFIG = 12;
426    public static final int FORCE_DEFAULT = FORCE_NONE;
427
428    // usage for setForceUse, must match AudioSystem::force_use
429    public static final int FOR_COMMUNICATION = 0;
430    public static final int FOR_MEDIA = 1;
431    public static final int FOR_RECORD = 2;
432    public static final int FOR_DOCK = 3;
433    public static final int FOR_SYSTEM = 4;
434    private static final int NUM_FORCE_USE = 5;
435
436    // usage for AudioRecord.startRecordingSync(), must match AudioSystem::sync_event_t
437    public static final int SYNC_EVENT_NONE = 0;
438    public static final int SYNC_EVENT_PRESENTATION_COMPLETE = 1;
439
440    public static native int setDeviceConnectionState(int device, int state, String device_address);
441    public static native int getDeviceConnectionState(int device, String device_address);
442    public static native int setPhoneState(int state);
443    public static native int setForceUse(int usage, int config);
444    public static native int getForceUse(int usage);
445    public static native int initStreamVolume(int stream, int indexMin, int indexMax);
446    public static native int setStreamVolumeIndex(int stream, int index, int device);
447    public static native int getStreamVolumeIndex(int stream, int device);
448    public static native int setMasterVolume(float value);
449    public static native float getMasterVolume();
450    public static native int setMasterMute(boolean mute);
451    public static native boolean getMasterMute();
452    public static native int getDevicesForStream(int stream);
453
454    // helpers for android.media.AudioManager.getProperty(), see description there for meaning
455    public static native int getPrimaryOutputSamplingRate();
456    public static native int getPrimaryOutputFrameCount();
457    public static native int getOutputLatency(int stream);
458
459    public static native int setLowRamDevice(boolean isLowRamDevice);
460    public static native int checkAudioFlinger();
461}
462