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