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