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
19import android.content.Context;
20import android.content.pm.PackageManager;
21import android.media.audiopolicy.AudioMix;
22import android.util.Log;
23
24import java.util.ArrayList;
25
26/* IF YOU CHANGE ANY OF THE CONSTANTS IN THIS FILE, DO NOT FORGET
27 * TO UPDATE THE CORRESPONDING NATIVE GLUE AND AudioManager.java.
28 * THANK YOU FOR YOUR COOPERATION.
29 */
30
31/**
32 * @hide
33 */
34public class AudioSystem
35{
36    private static final String TAG = "AudioSystem";
37    /* These values must be kept in sync with system/audio.h */
38    /*
39     * If these are modified, please also update Settings.System.VOLUME_SETTINGS
40     * and attrs.xml and AudioManager.java.
41     */
42    /* The default audio stream */
43    public static final int STREAM_DEFAULT = -1;
44    /* The audio stream for phone calls */
45    public static final int STREAM_VOICE_CALL = 0;
46    /* The audio stream for system sounds */
47    public static final int STREAM_SYSTEM = 1;
48    /* The audio stream for the phone ring and message alerts */
49    public static final int STREAM_RING = 2;
50    /* The audio stream for music playback */
51    public static final int STREAM_MUSIC = 3;
52    /* The audio stream for alarms */
53    public static final int STREAM_ALARM = 4;
54    /* The audio stream for notifications */
55    public static final int STREAM_NOTIFICATION = 5;
56    /* @hide The audio stream for phone calls when connected on bluetooth */
57    public static final int STREAM_BLUETOOTH_SCO = 6;
58    /* @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */
59    public static final int STREAM_SYSTEM_ENFORCED = 7;
60    /* @hide The audio stream for DTMF tones */
61    public static final int STREAM_DTMF = 8;
62    /* @hide The audio stream for text to speech (TTS) */
63    public static final int STREAM_TTS = 9;
64    /**
65     * @deprecated Use {@link #numStreamTypes() instead}
66     */
67    public static final int NUM_STREAMS = 5;
68
69    // Expose only the getter method publicly so we can change it in the future
70    private static final int NUM_STREAM_TYPES = 10;
71    public static final int getNumStreamTypes() { return NUM_STREAM_TYPES; }
72
73    public static final String[] STREAM_NAMES = new String[] {
74        "STREAM_VOICE_CALL",
75        "STREAM_SYSTEM",
76        "STREAM_RING",
77        "STREAM_MUSIC",
78        "STREAM_ALARM",
79        "STREAM_NOTIFICATION",
80        "STREAM_BLUETOOTH_SCO",
81        "STREAM_SYSTEM_ENFORCED",
82        "STREAM_DTMF",
83        "STREAM_TTS"
84    };
85
86    /*
87     * Sets the microphone mute on or off.
88     *
89     * @param on set <var>true</var> to mute the microphone;
90     *           <var>false</var> to turn mute off
91     * @return command completion status see AUDIO_STATUS_OK, see AUDIO_STATUS_ERROR
92     */
93    public static native int muteMicrophone(boolean on);
94
95    /*
96     * Checks whether the microphone mute is on or off.
97     *
98     * @return true if microphone is muted, false if it's not
99     */
100    public static native boolean isMicrophoneMuted();
101
102    /* modes for setPhoneState, must match AudioSystem.h audio_mode */
103    public static final int MODE_INVALID            = -2;
104    public static final int MODE_CURRENT            = -1;
105    public static final int MODE_NORMAL             = 0;
106    public static final int MODE_RINGTONE           = 1;
107    public static final int MODE_IN_CALL            = 2;
108    public static final int MODE_IN_COMMUNICATION   = 3;
109    public static final int NUM_MODES               = 4;
110
111
112    /* Routing bits for the former setRouting/getRouting API */
113    /** @deprecated */
114    @Deprecated public static final int ROUTE_EARPIECE          = (1 << 0);
115    /** @deprecated */
116    @Deprecated public static final int ROUTE_SPEAKER           = (1 << 1);
117    /** @deprecated use {@link #ROUTE_BLUETOOTH_SCO} */
118    @Deprecated public static final int ROUTE_BLUETOOTH = (1 << 2);
119    /** @deprecated */
120    @Deprecated public static final int ROUTE_BLUETOOTH_SCO     = (1 << 2);
121    /** @deprecated */
122    @Deprecated public static final int ROUTE_HEADSET           = (1 << 3);
123    /** @deprecated */
124    @Deprecated public static final int ROUTE_BLUETOOTH_A2DP    = (1 << 4);
125    /** @deprecated */
126    @Deprecated public static final int ROUTE_ALL               = 0xFFFFFFFF;
127
128    // Keep in sync with system/media/audio/include/system/audio.h
129    public static final int AUDIO_SESSION_ALLOCATE = 0;
130
131    /*
132     * Checks whether the specified stream type is active.
133     *
134     * return true if any track playing on this stream is active.
135     */
136    public static native boolean isStreamActive(int stream, int inPastMs);
137
138    /*
139     * Checks whether the specified stream type is active on a remotely connected device. The notion
140     * of what constitutes a remote device is enforced by the audio policy manager of the platform.
141     *
142     * return true if any track playing on this stream is active on a remote device.
143     */
144    public static native boolean isStreamActiveRemotely(int stream, int inPastMs);
145
146    /*
147     * Checks whether the specified audio source is active.
148     *
149     * return true if any recorder using this source is currently recording
150     */
151    public static native boolean isSourceActive(int source);
152
153    /*
154     * Returns a new unused audio session ID
155     */
156    public static native int newAudioSessionId();
157
158    /*
159     * Sets a group generic audio configuration parameters. The use of these parameters
160     * are platform dependent, see libaudio
161     *
162     * param keyValuePairs  list of parameters key value pairs in the form:
163     *    key1=value1;key2=value2;...
164     */
165    public static native int setParameters(String keyValuePairs);
166
167    /*
168     * Gets a group generic audio configuration parameters. The use of these parameters
169     * are platform dependent, see libaudio
170     *
171     * param keys  list of parameters
172     * return value: list of parameters key value pairs in the form:
173     *    key1=value1;key2=value2;...
174     */
175    public static native String getParameters(String keys);
176
177    // These match the enum AudioError in frameworks/base/core/jni/android_media_AudioSystem.cpp
178    /* Command sucessful or Media server restarted. see ErrorCallback */
179    public static final int AUDIO_STATUS_OK = 0;
180    /* Command failed or unspecified audio error.  see ErrorCallback */
181    public static final int AUDIO_STATUS_ERROR = 1;
182    /* Media server died. see ErrorCallback */
183    public static final int AUDIO_STATUS_SERVER_DIED = 100;
184
185    private static ErrorCallback mErrorCallback;
186
187    /*
188     * Handles the audio error callback.
189     */
190    public interface ErrorCallback
191    {
192        /*
193         * Callback for audio server errors.
194         * param error   error code:
195         * - AUDIO_STATUS_OK
196         * - AUDIO_STATUS_SERVER_DIED
197         * - AUDIO_STATUS_ERROR
198         */
199        void onError(int error);
200    };
201
202    /*
203     * Registers a callback to be invoked when an error occurs.
204     * @param cb the callback to run
205     */
206    public static void setErrorCallback(ErrorCallback cb)
207    {
208        synchronized (AudioSystem.class) {
209            mErrorCallback = cb;
210            if (cb != null) {
211                cb.onError(checkAudioFlinger());
212            }
213        }
214    }
215
216    private static void errorCallbackFromNative(int error)
217    {
218        ErrorCallback errorCallback = null;
219        synchronized (AudioSystem.class) {
220            if (mErrorCallback != null) {
221                errorCallback = mErrorCallback;
222            }
223        }
224        if (errorCallback != null) {
225            errorCallback.onError(error);
226        }
227    }
228
229    /**
230     * Handles events for the audio policy manager about dynamic audio policies
231     * @see android.media.audiopolicy.AudioPolicy
232     */
233    public interface DynamicPolicyCallback
234    {
235        void onDynamicPolicyMixStateUpdate(String regId, int state);
236    }
237
238    //keep in sync with include/media/AudioPolicy.h
239    private final static int DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE = 0;
240
241    private static DynamicPolicyCallback sDynPolicyCallback;
242
243    public static void setDynamicPolicyCallback(DynamicPolicyCallback cb)
244    {
245        synchronized (AudioSystem.class) {
246            sDynPolicyCallback = cb;
247            native_register_dynamic_policy_callback();
248        }
249    }
250
251    private static void dynamicPolicyCallbackFromNative(int event, String regId, int val)
252    {
253        DynamicPolicyCallback cb = null;
254        synchronized (AudioSystem.class) {
255            if (sDynPolicyCallback != null) {
256                cb = sDynPolicyCallback;
257            }
258        }
259        if (cb != null) {
260            switch(event) {
261                case DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE:
262                    cb.onDynamicPolicyMixStateUpdate(regId, val);
263                    break;
264                default:
265                    Log.e(TAG, "dynamicPolicyCallbackFromNative: unknown event " + event);
266            }
267        }
268    }
269
270
271    /*
272     * Error codes used by public APIs (AudioTrack, AudioRecord, AudioManager ...)
273     * Must be kept in sync with frameworks/base/core/jni/android_media_AudioErrors.h
274     */
275    public static final int SUCCESS            = 0;
276    public static final int ERROR              = -1;
277    public static final int BAD_VALUE          = -2;
278    public static final int INVALID_OPERATION  = -3;
279    public static final int PERMISSION_DENIED  = -4;
280    public static final int NO_INIT            = -5;
281    public static final int DEAD_OBJECT        = -6;
282    public static final int WOULD_BLOCK        = -7;
283
284    /*
285     * AudioPolicyService methods
286     */
287
288    //
289    // audio device definitions: must be kept in sync with values in system/core/audio.h
290    //
291
292    public static final int DEVICE_NONE = 0x0;
293    // reserved bits
294    public static final int DEVICE_BIT_IN = 0x80000000;
295    public static final int DEVICE_BIT_DEFAULT = 0x40000000;
296    // output devices, be sure to update AudioManager.java also
297    public static final int DEVICE_OUT_EARPIECE = 0x1;
298    public static final int DEVICE_OUT_SPEAKER = 0x2;
299    public static final int DEVICE_OUT_WIRED_HEADSET = 0x4;
300    public static final int DEVICE_OUT_WIRED_HEADPHONE = 0x8;
301    public static final int DEVICE_OUT_BLUETOOTH_SCO = 0x10;
302    public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20;
303    public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40;
304    public static final int DEVICE_OUT_BLUETOOTH_A2DP = 0x80;
305    public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100;
306    public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200;
307    public static final int DEVICE_OUT_AUX_DIGITAL = 0x400;
308    public static final int DEVICE_OUT_HDMI = DEVICE_OUT_AUX_DIGITAL;
309    public static final int DEVICE_OUT_ANLG_DOCK_HEADSET = 0x800;
310    public static final int DEVICE_OUT_DGTL_DOCK_HEADSET = 0x1000;
311    public static final int DEVICE_OUT_USB_ACCESSORY = 0x2000;
312    public static final int DEVICE_OUT_USB_DEVICE = 0x4000;
313    public static final int DEVICE_OUT_REMOTE_SUBMIX = 0x8000;
314    public static final int DEVICE_OUT_TELEPHONY_TX = 0x10000;
315    public static final int DEVICE_OUT_LINE = 0x20000;
316    public static final int DEVICE_OUT_HDMI_ARC = 0x40000;
317    public static final int DEVICE_OUT_SPDIF = 0x80000;
318    public static final int DEVICE_OUT_FM = 0x100000;
319    public static final int DEVICE_OUT_AUX_LINE = 0x200000;
320    public static final int DEVICE_OUT_SPEAKER_SAFE = 0x400000;
321    public static final int DEVICE_OUT_IP = 0x800000;
322
323    public static final int DEVICE_OUT_DEFAULT = DEVICE_BIT_DEFAULT;
324
325    public static final int DEVICE_OUT_ALL = (DEVICE_OUT_EARPIECE |
326                                              DEVICE_OUT_SPEAKER |
327                                              DEVICE_OUT_WIRED_HEADSET |
328                                              DEVICE_OUT_WIRED_HEADPHONE |
329                                              DEVICE_OUT_BLUETOOTH_SCO |
330                                              DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
331                                              DEVICE_OUT_BLUETOOTH_SCO_CARKIT |
332                                              DEVICE_OUT_BLUETOOTH_A2DP |
333                                              DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
334                                              DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER |
335                                              DEVICE_OUT_HDMI |
336                                              DEVICE_OUT_ANLG_DOCK_HEADSET |
337                                              DEVICE_OUT_DGTL_DOCK_HEADSET |
338                                              DEVICE_OUT_USB_ACCESSORY |
339                                              DEVICE_OUT_USB_DEVICE |
340                                              DEVICE_OUT_REMOTE_SUBMIX |
341                                              DEVICE_OUT_TELEPHONY_TX |
342                                              DEVICE_OUT_LINE |
343                                              DEVICE_OUT_HDMI_ARC |
344                                              DEVICE_OUT_SPDIF |
345                                              DEVICE_OUT_FM |
346                                              DEVICE_OUT_AUX_LINE |
347                                              DEVICE_OUT_SPEAKER_SAFE |
348                                              DEVICE_OUT_IP |
349                                              DEVICE_OUT_DEFAULT);
350    public static final int DEVICE_OUT_ALL_A2DP = (DEVICE_OUT_BLUETOOTH_A2DP |
351                                                   DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
352                                                   DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
353    public static final int DEVICE_OUT_ALL_SCO = (DEVICE_OUT_BLUETOOTH_SCO |
354                                                  DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
355                                                  DEVICE_OUT_BLUETOOTH_SCO_CARKIT);
356    public static final int DEVICE_OUT_ALL_USB = (DEVICE_OUT_USB_ACCESSORY |
357                                                  DEVICE_OUT_USB_DEVICE);
358    public static final int DEVICE_OUT_ALL_HDMI_SYSTEM_AUDIO = (DEVICE_OUT_AUX_LINE |
359                                                                DEVICE_OUT_HDMI_ARC |
360                                                                DEVICE_OUT_SPDIF);
361    public static final int DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER =
362            (DEVICE_OUT_ALL_HDMI_SYSTEM_AUDIO |
363             DEVICE_OUT_SPEAKER);
364
365    // input devices
366    public static final int DEVICE_IN_COMMUNICATION = DEVICE_BIT_IN | 0x1;
367    public static final int DEVICE_IN_AMBIENT = DEVICE_BIT_IN | 0x2;
368    public static final int DEVICE_IN_BUILTIN_MIC = DEVICE_BIT_IN | 0x4;
369    public static final int DEVICE_IN_BLUETOOTH_SCO_HEADSET = DEVICE_BIT_IN | 0x8;
370    public static final int DEVICE_IN_WIRED_HEADSET = DEVICE_BIT_IN | 0x10;
371    public static final int DEVICE_IN_AUX_DIGITAL = DEVICE_BIT_IN | 0x20;
372    public static final int DEVICE_IN_HDMI = DEVICE_IN_AUX_DIGITAL;
373    public static final int DEVICE_IN_VOICE_CALL = DEVICE_BIT_IN | 0x40;
374    public static final int DEVICE_IN_TELEPHONY_RX = DEVICE_IN_VOICE_CALL;
375    public static final int DEVICE_IN_BACK_MIC = DEVICE_BIT_IN | 0x80;
376    public static final int DEVICE_IN_REMOTE_SUBMIX = DEVICE_BIT_IN | 0x100;
377    public static final int DEVICE_IN_ANLG_DOCK_HEADSET = DEVICE_BIT_IN | 0x200;
378    public static final int DEVICE_IN_DGTL_DOCK_HEADSET = DEVICE_BIT_IN | 0x400;
379    public static final int DEVICE_IN_USB_ACCESSORY = DEVICE_BIT_IN | 0x800;
380    public static final int DEVICE_IN_USB_DEVICE = DEVICE_BIT_IN | 0x1000;
381    public static final int DEVICE_IN_FM_TUNER = DEVICE_BIT_IN | 0x2000;
382    public static final int DEVICE_IN_TV_TUNER = DEVICE_BIT_IN | 0x4000;
383    public static final int DEVICE_IN_LINE = DEVICE_BIT_IN | 0x8000;
384    public static final int DEVICE_IN_SPDIF = DEVICE_BIT_IN | 0x10000;
385    public static final int DEVICE_IN_BLUETOOTH_A2DP = DEVICE_BIT_IN | 0x20000;
386    public static final int DEVICE_IN_LOOPBACK = DEVICE_BIT_IN | 0x40000;
387    public static final int DEVICE_IN_IP = DEVICE_BIT_IN | 0x80000;
388    public static final int DEVICE_IN_DEFAULT = DEVICE_BIT_IN | DEVICE_BIT_DEFAULT;
389
390    public static final int DEVICE_IN_ALL = (DEVICE_IN_COMMUNICATION |
391                                             DEVICE_IN_AMBIENT |
392                                             DEVICE_IN_BUILTIN_MIC |
393                                             DEVICE_IN_BLUETOOTH_SCO_HEADSET |
394                                             DEVICE_IN_WIRED_HEADSET |
395                                             DEVICE_IN_HDMI |
396                                             DEVICE_IN_TELEPHONY_RX |
397                                             DEVICE_IN_BACK_MIC |
398                                             DEVICE_IN_REMOTE_SUBMIX |
399                                             DEVICE_IN_ANLG_DOCK_HEADSET |
400                                             DEVICE_IN_DGTL_DOCK_HEADSET |
401                                             DEVICE_IN_USB_ACCESSORY |
402                                             DEVICE_IN_USB_DEVICE |
403                                             DEVICE_IN_FM_TUNER |
404                                             DEVICE_IN_TV_TUNER |
405                                             DEVICE_IN_LINE |
406                                             DEVICE_IN_SPDIF |
407                                             DEVICE_IN_BLUETOOTH_A2DP |
408                                             DEVICE_IN_LOOPBACK |
409                                             DEVICE_IN_IP |
410                                             DEVICE_IN_DEFAULT);
411    public static final int DEVICE_IN_ALL_SCO = DEVICE_IN_BLUETOOTH_SCO_HEADSET;
412    public static final int DEVICE_IN_ALL_USB = (DEVICE_IN_USB_ACCESSORY |
413                                                 DEVICE_IN_USB_DEVICE);
414
415    // device states, must match AudioSystem::device_connection_state
416    public static final int DEVICE_STATE_UNAVAILABLE = 0;
417    public static final int DEVICE_STATE_AVAILABLE = 1;
418    private static final int NUM_DEVICE_STATES = 1;
419
420    public static final String DEVICE_OUT_EARPIECE_NAME = "earpiece";
421    public static final String DEVICE_OUT_SPEAKER_NAME = "speaker";
422    public static final String DEVICE_OUT_WIRED_HEADSET_NAME = "headset";
423    public static final String DEVICE_OUT_WIRED_HEADPHONE_NAME = "headphone";
424    public static final String DEVICE_OUT_BLUETOOTH_SCO_NAME = "bt_sco";
425    public static final String DEVICE_OUT_BLUETOOTH_SCO_HEADSET_NAME = "bt_sco_hs";
426    public static final String DEVICE_OUT_BLUETOOTH_SCO_CARKIT_NAME = "bt_sco_carkit";
427    public static final String DEVICE_OUT_BLUETOOTH_A2DP_NAME = "bt_a2dp";
428    public static final String DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES_NAME = "bt_a2dp_hp";
429    public static final String DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER_NAME = "bt_a2dp_spk";
430    public static final String DEVICE_OUT_AUX_DIGITAL_NAME = "aux_digital";
431    public static final String DEVICE_OUT_HDMI_NAME = "hdmi";
432    public static final String DEVICE_OUT_ANLG_DOCK_HEADSET_NAME = "analog_dock";
433    public static final String DEVICE_OUT_DGTL_DOCK_HEADSET_NAME = "digital_dock";
434    public static final String DEVICE_OUT_USB_ACCESSORY_NAME = "usb_accessory";
435    public static final String DEVICE_OUT_USB_DEVICE_NAME = "usb_device";
436    public static final String DEVICE_OUT_REMOTE_SUBMIX_NAME = "remote_submix";
437    public static final String DEVICE_OUT_TELEPHONY_TX_NAME = "telephony_tx";
438    public static final String DEVICE_OUT_LINE_NAME = "line";
439    public static final String DEVICE_OUT_HDMI_ARC_NAME = "hmdi_arc";
440    public static final String DEVICE_OUT_SPDIF_NAME = "spdif";
441    public static final String DEVICE_OUT_FM_NAME = "fm_transmitter";
442    public static final String DEVICE_OUT_AUX_LINE_NAME = "aux_line";
443    public static final String DEVICE_OUT_SPEAKER_SAFE_NAME = "speaker_safe";
444    public static final String DEVICE_OUT_IP_NAME = "ip";
445
446    public static final String DEVICE_IN_COMMUNICATION_NAME = "communication";
447    public static final String DEVICE_IN_AMBIENT_NAME = "ambient";
448    public static final String DEVICE_IN_BUILTIN_MIC_NAME = "mic";
449    public static final String DEVICE_IN_BLUETOOTH_SCO_HEADSET_NAME = "bt_sco_hs";
450    public static final String DEVICE_IN_WIRED_HEADSET_NAME = "headset";
451    public static final String DEVICE_IN_AUX_DIGITAL_NAME = "aux_digital";
452    public static final String DEVICE_IN_TELEPHONY_RX_NAME = "telephony_rx";
453    public static final String DEVICE_IN_BACK_MIC_NAME = "back_mic";
454    public static final String DEVICE_IN_REMOTE_SUBMIX_NAME = "remote_submix";
455    public static final String DEVICE_IN_ANLG_DOCK_HEADSET_NAME = "analog_dock";
456    public static final String DEVICE_IN_DGTL_DOCK_HEADSET_NAME = "digital_dock";
457    public static final String DEVICE_IN_USB_ACCESSORY_NAME = "usb_accessory";
458    public static final String DEVICE_IN_USB_DEVICE_NAME = "usb_device";
459    public static final String DEVICE_IN_FM_TUNER_NAME = "fm_tuner";
460    public static final String DEVICE_IN_TV_TUNER_NAME = "tv_tuner";
461    public static final String DEVICE_IN_LINE_NAME = "line";
462    public static final String DEVICE_IN_SPDIF_NAME = "spdif";
463    public static final String DEVICE_IN_BLUETOOTH_A2DP_NAME = "bt_a2dp";
464    public static final String DEVICE_IN_LOOPBACK_NAME = "loopback";
465    public static final String DEVICE_IN_IP_NAME = "ip";
466
467    public static String getOutputDeviceName(int device)
468    {
469        switch(device) {
470        case DEVICE_OUT_EARPIECE:
471            return DEVICE_OUT_EARPIECE_NAME;
472        case DEVICE_OUT_SPEAKER:
473            return DEVICE_OUT_SPEAKER_NAME;
474        case DEVICE_OUT_WIRED_HEADSET:
475            return DEVICE_OUT_WIRED_HEADSET_NAME;
476        case DEVICE_OUT_WIRED_HEADPHONE:
477            return DEVICE_OUT_WIRED_HEADPHONE_NAME;
478        case DEVICE_OUT_BLUETOOTH_SCO:
479            return DEVICE_OUT_BLUETOOTH_SCO_NAME;
480        case DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
481            return DEVICE_OUT_BLUETOOTH_SCO_HEADSET_NAME;
482        case DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
483            return DEVICE_OUT_BLUETOOTH_SCO_CARKIT_NAME;
484        case DEVICE_OUT_BLUETOOTH_A2DP:
485            return DEVICE_OUT_BLUETOOTH_A2DP_NAME;
486        case DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
487            return DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES_NAME;
488        case DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
489            return DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER_NAME;
490        case DEVICE_OUT_HDMI:
491            return DEVICE_OUT_HDMI_NAME;
492        case DEVICE_OUT_ANLG_DOCK_HEADSET:
493            return DEVICE_OUT_ANLG_DOCK_HEADSET_NAME;
494        case DEVICE_OUT_DGTL_DOCK_HEADSET:
495            return DEVICE_OUT_DGTL_DOCK_HEADSET_NAME;
496        case DEVICE_OUT_USB_ACCESSORY:
497            return DEVICE_OUT_USB_ACCESSORY_NAME;
498        case DEVICE_OUT_USB_DEVICE:
499            return DEVICE_OUT_USB_DEVICE_NAME;
500        case DEVICE_OUT_REMOTE_SUBMIX:
501            return DEVICE_OUT_REMOTE_SUBMIX_NAME;
502        case DEVICE_OUT_TELEPHONY_TX:
503            return DEVICE_OUT_TELEPHONY_TX_NAME;
504        case DEVICE_OUT_LINE:
505            return DEVICE_OUT_LINE_NAME;
506        case DEVICE_OUT_HDMI_ARC:
507            return DEVICE_OUT_HDMI_ARC_NAME;
508        case DEVICE_OUT_SPDIF:
509            return DEVICE_OUT_SPDIF_NAME;
510        case DEVICE_OUT_FM:
511            return DEVICE_OUT_FM_NAME;
512        case DEVICE_OUT_AUX_LINE:
513            return DEVICE_OUT_AUX_LINE_NAME;
514        case DEVICE_OUT_SPEAKER_SAFE:
515            return DEVICE_OUT_SPEAKER_SAFE_NAME;
516        case DEVICE_OUT_IP:
517            return DEVICE_OUT_IP_NAME;
518        case DEVICE_OUT_DEFAULT:
519        default:
520            return Integer.toString(device);
521        }
522    }
523
524    public static String getInputDeviceName(int device)
525    {
526        switch(device) {
527        case DEVICE_IN_COMMUNICATION:
528            return DEVICE_IN_COMMUNICATION_NAME;
529        case DEVICE_IN_AMBIENT:
530            return DEVICE_IN_AMBIENT_NAME;
531        case DEVICE_IN_BUILTIN_MIC:
532            return DEVICE_IN_BUILTIN_MIC_NAME;
533        case DEVICE_IN_BLUETOOTH_SCO_HEADSET:
534            return DEVICE_IN_BLUETOOTH_SCO_HEADSET_NAME;
535        case DEVICE_IN_WIRED_HEADSET:
536            return DEVICE_IN_WIRED_HEADSET_NAME;
537        case DEVICE_IN_AUX_DIGITAL:
538            return DEVICE_IN_AUX_DIGITAL_NAME;
539        case DEVICE_IN_TELEPHONY_RX:
540            return DEVICE_IN_TELEPHONY_RX_NAME;
541        case DEVICE_IN_BACK_MIC:
542            return DEVICE_IN_BACK_MIC_NAME;
543        case DEVICE_IN_REMOTE_SUBMIX:
544            return DEVICE_IN_REMOTE_SUBMIX_NAME;
545        case DEVICE_IN_ANLG_DOCK_HEADSET:
546            return DEVICE_IN_ANLG_DOCK_HEADSET_NAME;
547        case DEVICE_IN_DGTL_DOCK_HEADSET:
548            return DEVICE_IN_DGTL_DOCK_HEADSET_NAME;
549        case DEVICE_IN_USB_ACCESSORY:
550            return DEVICE_IN_USB_ACCESSORY_NAME;
551        case DEVICE_IN_USB_DEVICE:
552            return DEVICE_IN_USB_DEVICE_NAME;
553        case DEVICE_IN_FM_TUNER:
554            return DEVICE_IN_FM_TUNER_NAME;
555        case DEVICE_IN_TV_TUNER:
556            return DEVICE_IN_TV_TUNER_NAME;
557        case DEVICE_IN_LINE:
558            return DEVICE_IN_LINE_NAME;
559        case DEVICE_IN_SPDIF:
560            return DEVICE_IN_SPDIF_NAME;
561        case DEVICE_IN_BLUETOOTH_A2DP:
562            return DEVICE_IN_BLUETOOTH_A2DP_NAME;
563        case DEVICE_IN_LOOPBACK:
564            return DEVICE_IN_LOOPBACK_NAME;
565        case DEVICE_IN_IP:
566            return DEVICE_IN_IP_NAME;
567        case DEVICE_IN_DEFAULT:
568        default:
569            return Integer.toString(device);
570        }
571    }
572
573    // phone state, match audio_mode???
574    public static final int PHONE_STATE_OFFCALL = 0;
575    public static final int PHONE_STATE_RINGING = 1;
576    public static final int PHONE_STATE_INCALL = 2;
577
578    // device categories config for setForceUse, must match AudioSystem::forced_config
579    public static final int FORCE_NONE = 0;
580    public static final int FORCE_SPEAKER = 1;
581    public static final int FORCE_HEADPHONES = 2;
582    public static final int FORCE_BT_SCO = 3;
583    public static final int FORCE_BT_A2DP = 4;
584    public static final int FORCE_WIRED_ACCESSORY = 5;
585    public static final int FORCE_BT_CAR_DOCK = 6;
586    public static final int FORCE_BT_DESK_DOCK = 7;
587    public static final int FORCE_ANALOG_DOCK = 8;
588    public static final int FORCE_DIGITAL_DOCK = 9;
589    public static final int FORCE_NO_BT_A2DP = 10;
590    public static final int FORCE_SYSTEM_ENFORCED = 11;
591    public static final int FORCE_HDMI_SYSTEM_AUDIO_ENFORCED = 12;
592    private static final int NUM_FORCE_CONFIG = 13;
593    public static final int FORCE_DEFAULT = FORCE_NONE;
594
595    // usage for setForceUse, must match AudioSystem::force_use
596    public static final int FOR_COMMUNICATION = 0;
597    public static final int FOR_MEDIA = 1;
598    public static final int FOR_RECORD = 2;
599    public static final int FOR_DOCK = 3;
600    public static final int FOR_SYSTEM = 4;
601    public static final int FOR_HDMI_SYSTEM_AUDIO = 5;
602    private static final int NUM_FORCE_USE = 6;
603
604    // usage for AudioRecord.startRecordingSync(), must match AudioSystem::sync_event_t
605    public static final int SYNC_EVENT_NONE = 0;
606    public static final int SYNC_EVENT_PRESENTATION_COMPLETE = 1;
607
608    /**
609     * @return command completion status, one of {@link #AUDIO_STATUS_OK},
610     *     {@link #AUDIO_STATUS_ERROR} or {@link #AUDIO_STATUS_SERVER_DIED}
611     */
612    public static native int setDeviceConnectionState(int device, int state,
613                                                      String device_address, String device_name);
614    public static native int getDeviceConnectionState(int device, String device_address);
615    public static native int setPhoneState(int state);
616    public static native int setForceUse(int usage, int config);
617    public static native int getForceUse(int usage);
618    public static native int initStreamVolume(int stream, int indexMin, int indexMax);
619    public static native int setStreamVolumeIndex(int stream, int index, int device);
620    public static native int getStreamVolumeIndex(int stream, int device);
621    public static native int setMasterVolume(float value);
622    public static native float getMasterVolume();
623    public static native int setMasterMute(boolean mute);
624    public static native boolean getMasterMute();
625    public static native int getDevicesForStream(int stream);
626
627    // helpers for android.media.AudioManager.getProperty(), see description there for meaning
628    public static native int getPrimaryOutputSamplingRate();
629    public static native int getPrimaryOutputFrameCount();
630    public static native int getOutputLatency(int stream);
631
632    public static native int setLowRamDevice(boolean isLowRamDevice);
633    public static native int checkAudioFlinger();
634
635    public static native int listAudioPorts(ArrayList<AudioPort> ports, int[] generation);
636    public static native int createAudioPatch(AudioPatch[] patch,
637                                            AudioPortConfig[] sources, AudioPortConfig[] sinks);
638    public static native int releaseAudioPatch(AudioPatch patch);
639    public static native int listAudioPatches(ArrayList<AudioPatch> patches, int[] generation);
640    public static native int setAudioPortConfig(AudioPortConfig config);
641
642    // declare this instance as having a dynamic policy callback handler
643    private static native final void native_register_dynamic_policy_callback();
644
645    // must be kept in sync with value in include/system/audio.h
646    public static final int AUDIO_HW_SYNC_INVALID = 0;
647
648    public static native int getAudioHwSyncForSession(int sessionId);
649
650    public static native int registerPolicyMixes(ArrayList<AudioMix> mixes, boolean register);
651
652    public static native int systemReady();
653
654    // Items shared with audio service
655
656    /**
657     * The delay before playing a sound. This small period exists so the user
658     * can press another key (non-volume keys, too) to have it NOT be audible.
659     * <p>
660     * PhoneWindow will implement this part.
661     */
662    public static final int PLAY_SOUND_DELAY = 300;
663
664    /**
665     * Constant to identify a focus stack entry that is used to hold the focus while the phone
666     * is ringing or during a call. Used by com.android.internal.telephony.CallManager when
667     * entering and exiting calls.
668     */
669    public final static String IN_VOICE_COMM_FOCUS_ID = "AudioFocus_For_Phone_Ring_And_Calls";
670
671    /**
672     * @see AudioManager#setVibrateSetting(int, int)
673     */
674    public static int getValueForVibrateSetting(int existingValue, int vibrateType,
675            int vibrateSetting) {
676
677        // First clear the existing setting. Each vibrate type has two bits in
678        // the value. Note '3' is '11' in binary.
679        existingValue &= ~(3 << (vibrateType * 2));
680
681        // Set into the old value
682        existingValue |= (vibrateSetting & 3) << (vibrateType * 2);
683
684        return existingValue;
685    }
686
687    public static int getDefaultStreamVolume(int streamType) {
688        return DEFAULT_STREAM_VOLUME[streamType];
689    }
690
691    public static int[] DEFAULT_STREAM_VOLUME = new int[] {
692        4,  // STREAM_VOICE_CALL
693        7,  // STREAM_SYSTEM
694        5,  // STREAM_RING
695        11, // STREAM_MUSIC
696        6,  // STREAM_ALARM
697        5,  // STREAM_NOTIFICATION
698        7,  // STREAM_BLUETOOTH_SCO
699        7,  // STREAM_SYSTEM_ENFORCED
700        11, // STREAM_DTMF
701        11  // STREAM_TTS
702    };
703
704    public static String streamToString(int stream) {
705        if (stream >= 0 && stream < STREAM_NAMES.length) return STREAM_NAMES[stream];
706        if (stream == AudioManager.USE_DEFAULT_STREAM_TYPE) return "USE_DEFAULT_STREAM_TYPE";
707        return "UNKNOWN_STREAM_" + stream;
708    }
709
710    /** The platform has no specific capabilities */
711    public static final int PLATFORM_DEFAULT = 0;
712    /** The platform is voice call capable (a phone) */
713    public static final int PLATFORM_VOICE = 1;
714    /** The platform is a television or a set-top box */
715    public static final int PLATFORM_TELEVISION = 2;
716
717    /**
718     * Return the platform type that this is running on. One of:
719     * <ul>
720     * <li>{@link #PLATFORM_VOICE}</li>
721     * <li>{@link #PLATFORM_TELEVISION}</li>
722     * <li>{@link #PLATFORM_DEFAULT}</li>
723     * </ul>
724     */
725    public static int getPlatformType(Context context) {
726        if (context.getResources().getBoolean(com.android.internal.R.bool.config_voice_capable)) {
727            return PLATFORM_VOICE;
728        } else if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
729            return PLATFORM_TELEVISION;
730        } else {
731            return PLATFORM_DEFAULT;
732        }
733    }
734
735    public static final int DEFAULT_MUTE_STREAMS_AFFECTED =
736            (1 << STREAM_MUSIC) |
737            (1 << STREAM_RING) |
738            (1 << STREAM_NOTIFICATION) |
739            (1 << STREAM_SYSTEM);
740
741    /**
742     * Event posted by AudioTrack and AudioRecord JNI (JNIDeviceCallback) when routing changes.
743     * Keep in sync with core/jni/android_media_DeviceCallback.h.
744     */
745    final static int NATIVE_EVENT_ROUTING_CHANGE = 1000;
746}
747
748