• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /* 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;
getNumStreamTypes()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      */
muteMicrophone(boolean on)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      */
isMicrophoneMuted()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      */
isStreamActive(int stream, int inPastMs)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      */
isStreamActiveRemotely(int stream, int inPastMs)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      */
isSourceActive(int source)151     public static native boolean isSourceActive(int source);
152 
153     /*
154      * Returns a new unused audio session ID
155      */
newAudioSessionId()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      */
setParameters(String keyValuePairs)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      */
getParameters(String keys)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          */
onError(int error)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      */
setErrorCallback(ErrorCallback cb)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 
errorCallbackFromNative(int error)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 from the audio policy manager about dynamic audio policies
231      * @see android.media.audiopolicy.AudioPolicy
232      */
233     public interface DynamicPolicyCallback
234     {
onDynamicPolicyMixStateUpdate(String regId, int state)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 
setDynamicPolicyCallback(DynamicPolicyCallback cb)243     public static void setDynamicPolicyCallback(DynamicPolicyCallback cb)
244     {
245         synchronized (AudioSystem.class) {
246             sDynPolicyCallback = cb;
247             native_register_dynamic_policy_callback();
248         }
249     }
250 
dynamicPolicyCallbackFromNative(int event, String regId, int val)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      * Handles events from the audio policy manager about recording events
272      * @see android.media.AudioManager.AudioRecordingCallback
273      */
274     public interface AudioRecordingCallback
275     {
276         /**
277          * Callback for recording activity notifications events
278          * @param event
279          * @param session
280          * @param source
281          * @param recordingFormat an array of ints containing respectively the client and device
282          *    recording configurations (2*3 ints), followed by the patch handle:
283          *    index 0: client format
284          *          1: client channel mask
285          *          2: client sample rate
286          *          3: device format
287          *          4: device channel mask
288          *          5: device sample rate
289          *          6: patch handle
290          */
onRecordingConfigurationChanged(int event, int session, int source, int[] recordingFormat)291         void onRecordingConfigurationChanged(int event, int session, int source,
292                 int[] recordingFormat);
293     }
294 
295     private static AudioRecordingCallback sRecordingCallback;
296 
setRecordingCallback(AudioRecordingCallback cb)297     public static void setRecordingCallback(AudioRecordingCallback cb) {
298         synchronized (AudioSystem.class) {
299             sRecordingCallback = cb;
300             native_register_recording_callback();
301         }
302     }
303 
304     /**
305      * Callback from native for recording configuration updates.
306      * @param event
307      * @param session
308      * @param source
309      * @param recordingFormat see
310      *     {@link AudioRecordingCallback#onRecordingConfigurationChanged(int, int, int, int[])} for
311      *     the description of the record format.
312      */
recordingCallbackFromNative(int event, int session, int source, int[] recordingFormat)313     private static void recordingCallbackFromNative(int event, int session, int source,
314             int[] recordingFormat) {
315         AudioRecordingCallback cb = null;
316         synchronized (AudioSystem.class) {
317             cb = sRecordingCallback;
318         }
319         if (cb != null) {
320             cb.onRecordingConfigurationChanged(event, session, source, recordingFormat);
321         }
322     }
323 
324     /*
325      * Error codes used by public APIs (AudioTrack, AudioRecord, AudioManager ...)
326      * Must be kept in sync with frameworks/base/core/jni/android_media_AudioErrors.h
327      */
328     public static final int SUCCESS            = 0;
329     public static final int ERROR              = -1;
330     public static final int BAD_VALUE          = -2;
331     public static final int INVALID_OPERATION  = -3;
332     public static final int PERMISSION_DENIED  = -4;
333     public static final int NO_INIT            = -5;
334     public static final int DEAD_OBJECT        = -6;
335     public static final int WOULD_BLOCK        = -7;
336 
337     /*
338      * AudioPolicyService methods
339      */
340 
341     //
342     // audio device definitions: must be kept in sync with values in system/core/audio.h
343     //
344 
345     public static final int DEVICE_NONE = 0x0;
346     // reserved bits
347     public static final int DEVICE_BIT_IN = 0x80000000;
348     public static final int DEVICE_BIT_DEFAULT = 0x40000000;
349     // output devices, be sure to update AudioManager.java also
350     public static final int DEVICE_OUT_EARPIECE = 0x1;
351     public static final int DEVICE_OUT_SPEAKER = 0x2;
352     public static final int DEVICE_OUT_WIRED_HEADSET = 0x4;
353     public static final int DEVICE_OUT_WIRED_HEADPHONE = 0x8;
354     public static final int DEVICE_OUT_BLUETOOTH_SCO = 0x10;
355     public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20;
356     public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40;
357     public static final int DEVICE_OUT_BLUETOOTH_A2DP = 0x80;
358     public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100;
359     public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200;
360     public static final int DEVICE_OUT_AUX_DIGITAL = 0x400;
361     public static final int DEVICE_OUT_HDMI = DEVICE_OUT_AUX_DIGITAL;
362     public static final int DEVICE_OUT_ANLG_DOCK_HEADSET = 0x800;
363     public static final int DEVICE_OUT_DGTL_DOCK_HEADSET = 0x1000;
364     public static final int DEVICE_OUT_USB_ACCESSORY = 0x2000;
365     public static final int DEVICE_OUT_USB_DEVICE = 0x4000;
366     public static final int DEVICE_OUT_REMOTE_SUBMIX = 0x8000;
367     public static final int DEVICE_OUT_TELEPHONY_TX = 0x10000;
368     public static final int DEVICE_OUT_LINE = 0x20000;
369     public static final int DEVICE_OUT_HDMI_ARC = 0x40000;
370     public static final int DEVICE_OUT_SPDIF = 0x80000;
371     public static final int DEVICE_OUT_FM = 0x100000;
372     public static final int DEVICE_OUT_AUX_LINE = 0x200000;
373     public static final int DEVICE_OUT_SPEAKER_SAFE = 0x400000;
374     public static final int DEVICE_OUT_IP = 0x800000;
375     public static final int DEVICE_OUT_BUS = 0x1000000;
376 
377     public static final int DEVICE_OUT_DEFAULT = DEVICE_BIT_DEFAULT;
378 
379     public static final int DEVICE_OUT_ALL = (DEVICE_OUT_EARPIECE |
380                                               DEVICE_OUT_SPEAKER |
381                                               DEVICE_OUT_WIRED_HEADSET |
382                                               DEVICE_OUT_WIRED_HEADPHONE |
383                                               DEVICE_OUT_BLUETOOTH_SCO |
384                                               DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
385                                               DEVICE_OUT_BLUETOOTH_SCO_CARKIT |
386                                               DEVICE_OUT_BLUETOOTH_A2DP |
387                                               DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
388                                               DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER |
389                                               DEVICE_OUT_HDMI |
390                                               DEVICE_OUT_ANLG_DOCK_HEADSET |
391                                               DEVICE_OUT_DGTL_DOCK_HEADSET |
392                                               DEVICE_OUT_USB_ACCESSORY |
393                                               DEVICE_OUT_USB_DEVICE |
394                                               DEVICE_OUT_REMOTE_SUBMIX |
395                                               DEVICE_OUT_TELEPHONY_TX |
396                                               DEVICE_OUT_LINE |
397                                               DEVICE_OUT_HDMI_ARC |
398                                               DEVICE_OUT_SPDIF |
399                                               DEVICE_OUT_FM |
400                                               DEVICE_OUT_AUX_LINE |
401                                               DEVICE_OUT_SPEAKER_SAFE |
402                                               DEVICE_OUT_IP |
403                                               DEVICE_OUT_BUS |
404                                               DEVICE_OUT_DEFAULT);
405     public static final int DEVICE_OUT_ALL_A2DP = (DEVICE_OUT_BLUETOOTH_A2DP |
406                                                    DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
407                                                    DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
408     public static final int DEVICE_OUT_ALL_SCO = (DEVICE_OUT_BLUETOOTH_SCO |
409                                                   DEVICE_OUT_BLUETOOTH_SCO_HEADSET |
410                                                   DEVICE_OUT_BLUETOOTH_SCO_CARKIT);
411     public static final int DEVICE_OUT_ALL_USB = (DEVICE_OUT_USB_ACCESSORY |
412                                                   DEVICE_OUT_USB_DEVICE);
413     public static final int DEVICE_OUT_ALL_HDMI_SYSTEM_AUDIO = (DEVICE_OUT_AUX_LINE |
414                                                                 DEVICE_OUT_HDMI_ARC |
415                                                                 DEVICE_OUT_SPDIF);
416     public static final int DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER =
417             (DEVICE_OUT_ALL_HDMI_SYSTEM_AUDIO |
418              DEVICE_OUT_SPEAKER);
419 
420     // input devices
421     public static final int DEVICE_IN_COMMUNICATION = DEVICE_BIT_IN | 0x1;
422     public static final int DEVICE_IN_AMBIENT = DEVICE_BIT_IN | 0x2;
423     public static final int DEVICE_IN_BUILTIN_MIC = DEVICE_BIT_IN | 0x4;
424     public static final int DEVICE_IN_BLUETOOTH_SCO_HEADSET = DEVICE_BIT_IN | 0x8;
425     public static final int DEVICE_IN_WIRED_HEADSET = DEVICE_BIT_IN | 0x10;
426     public static final int DEVICE_IN_AUX_DIGITAL = DEVICE_BIT_IN | 0x20;
427     public static final int DEVICE_IN_HDMI = DEVICE_IN_AUX_DIGITAL;
428     public static final int DEVICE_IN_VOICE_CALL = DEVICE_BIT_IN | 0x40;
429     public static final int DEVICE_IN_TELEPHONY_RX = DEVICE_IN_VOICE_CALL;
430     public static final int DEVICE_IN_BACK_MIC = DEVICE_BIT_IN | 0x80;
431     public static final int DEVICE_IN_REMOTE_SUBMIX = DEVICE_BIT_IN | 0x100;
432     public static final int DEVICE_IN_ANLG_DOCK_HEADSET = DEVICE_BIT_IN | 0x200;
433     public static final int DEVICE_IN_DGTL_DOCK_HEADSET = DEVICE_BIT_IN | 0x400;
434     public static final int DEVICE_IN_USB_ACCESSORY = DEVICE_BIT_IN | 0x800;
435     public static final int DEVICE_IN_USB_DEVICE = DEVICE_BIT_IN | 0x1000;
436     public static final int DEVICE_IN_FM_TUNER = DEVICE_BIT_IN | 0x2000;
437     public static final int DEVICE_IN_TV_TUNER = DEVICE_BIT_IN | 0x4000;
438     public static final int DEVICE_IN_LINE = DEVICE_BIT_IN | 0x8000;
439     public static final int DEVICE_IN_SPDIF = DEVICE_BIT_IN | 0x10000;
440     public static final int DEVICE_IN_BLUETOOTH_A2DP = DEVICE_BIT_IN | 0x20000;
441     public static final int DEVICE_IN_LOOPBACK = DEVICE_BIT_IN | 0x40000;
442     public static final int DEVICE_IN_IP = DEVICE_BIT_IN | 0x80000;
443     public static final int DEVICE_IN_BUS = DEVICE_BIT_IN | 0x100000;
444     public static final int DEVICE_IN_DEFAULT = DEVICE_BIT_IN | DEVICE_BIT_DEFAULT;
445 
446     public static final int DEVICE_IN_ALL = (DEVICE_IN_COMMUNICATION |
447                                              DEVICE_IN_AMBIENT |
448                                              DEVICE_IN_BUILTIN_MIC |
449                                              DEVICE_IN_BLUETOOTH_SCO_HEADSET |
450                                              DEVICE_IN_WIRED_HEADSET |
451                                              DEVICE_IN_HDMI |
452                                              DEVICE_IN_TELEPHONY_RX |
453                                              DEVICE_IN_BACK_MIC |
454                                              DEVICE_IN_REMOTE_SUBMIX |
455                                              DEVICE_IN_ANLG_DOCK_HEADSET |
456                                              DEVICE_IN_DGTL_DOCK_HEADSET |
457                                              DEVICE_IN_USB_ACCESSORY |
458                                              DEVICE_IN_USB_DEVICE |
459                                              DEVICE_IN_FM_TUNER |
460                                              DEVICE_IN_TV_TUNER |
461                                              DEVICE_IN_LINE |
462                                              DEVICE_IN_SPDIF |
463                                              DEVICE_IN_BLUETOOTH_A2DP |
464                                              DEVICE_IN_LOOPBACK |
465                                              DEVICE_IN_IP |
466                                              DEVICE_IN_BUS |
467                                              DEVICE_IN_DEFAULT);
468     public static final int DEVICE_IN_ALL_SCO = DEVICE_IN_BLUETOOTH_SCO_HEADSET;
469     public static final int DEVICE_IN_ALL_USB = (DEVICE_IN_USB_ACCESSORY |
470                                                  DEVICE_IN_USB_DEVICE);
471 
472     // device states, must match AudioSystem::device_connection_state
473     public static final int DEVICE_STATE_UNAVAILABLE = 0;
474     public static final int DEVICE_STATE_AVAILABLE = 1;
475     private static final int NUM_DEVICE_STATES = 1;
476 
477     public static final String DEVICE_OUT_EARPIECE_NAME = "earpiece";
478     public static final String DEVICE_OUT_SPEAKER_NAME = "speaker";
479     public static final String DEVICE_OUT_WIRED_HEADSET_NAME = "headset";
480     public static final String DEVICE_OUT_WIRED_HEADPHONE_NAME = "headphone";
481     public static final String DEVICE_OUT_BLUETOOTH_SCO_NAME = "bt_sco";
482     public static final String DEVICE_OUT_BLUETOOTH_SCO_HEADSET_NAME = "bt_sco_hs";
483     public static final String DEVICE_OUT_BLUETOOTH_SCO_CARKIT_NAME = "bt_sco_carkit";
484     public static final String DEVICE_OUT_BLUETOOTH_A2DP_NAME = "bt_a2dp";
485     public static final String DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES_NAME = "bt_a2dp_hp";
486     public static final String DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER_NAME = "bt_a2dp_spk";
487     public static final String DEVICE_OUT_AUX_DIGITAL_NAME = "aux_digital";
488     public static final String DEVICE_OUT_HDMI_NAME = "hdmi";
489     public static final String DEVICE_OUT_ANLG_DOCK_HEADSET_NAME = "analog_dock";
490     public static final String DEVICE_OUT_DGTL_DOCK_HEADSET_NAME = "digital_dock";
491     public static final String DEVICE_OUT_USB_ACCESSORY_NAME = "usb_accessory";
492     public static final String DEVICE_OUT_USB_DEVICE_NAME = "usb_device";
493     public static final String DEVICE_OUT_REMOTE_SUBMIX_NAME = "remote_submix";
494     public static final String DEVICE_OUT_TELEPHONY_TX_NAME = "telephony_tx";
495     public static final String DEVICE_OUT_LINE_NAME = "line";
496     public static final String DEVICE_OUT_HDMI_ARC_NAME = "hmdi_arc";
497     public static final String DEVICE_OUT_SPDIF_NAME = "spdif";
498     public static final String DEVICE_OUT_FM_NAME = "fm_transmitter";
499     public static final String DEVICE_OUT_AUX_LINE_NAME = "aux_line";
500     public static final String DEVICE_OUT_SPEAKER_SAFE_NAME = "speaker_safe";
501     public static final String DEVICE_OUT_IP_NAME = "ip";
502     public static final String DEVICE_OUT_BUS_NAME = "bus";
503 
504     public static final String DEVICE_IN_COMMUNICATION_NAME = "communication";
505     public static final String DEVICE_IN_AMBIENT_NAME = "ambient";
506     public static final String DEVICE_IN_BUILTIN_MIC_NAME = "mic";
507     public static final String DEVICE_IN_BLUETOOTH_SCO_HEADSET_NAME = "bt_sco_hs";
508     public static final String DEVICE_IN_WIRED_HEADSET_NAME = "headset";
509     public static final String DEVICE_IN_AUX_DIGITAL_NAME = "aux_digital";
510     public static final String DEVICE_IN_TELEPHONY_RX_NAME = "telephony_rx";
511     public static final String DEVICE_IN_BACK_MIC_NAME = "back_mic";
512     public static final String DEVICE_IN_REMOTE_SUBMIX_NAME = "remote_submix";
513     public static final String DEVICE_IN_ANLG_DOCK_HEADSET_NAME = "analog_dock";
514     public static final String DEVICE_IN_DGTL_DOCK_HEADSET_NAME = "digital_dock";
515     public static final String DEVICE_IN_USB_ACCESSORY_NAME = "usb_accessory";
516     public static final String DEVICE_IN_USB_DEVICE_NAME = "usb_device";
517     public static final String DEVICE_IN_FM_TUNER_NAME = "fm_tuner";
518     public static final String DEVICE_IN_TV_TUNER_NAME = "tv_tuner";
519     public static final String DEVICE_IN_LINE_NAME = "line";
520     public static final String DEVICE_IN_SPDIF_NAME = "spdif";
521     public static final String DEVICE_IN_BLUETOOTH_A2DP_NAME = "bt_a2dp";
522     public static final String DEVICE_IN_LOOPBACK_NAME = "loopback";
523     public static final String DEVICE_IN_IP_NAME = "ip";
524     public static final String DEVICE_IN_BUS_NAME = "bus";
525 
getOutputDeviceName(int device)526     public static String getOutputDeviceName(int device)
527     {
528         switch(device) {
529         case DEVICE_OUT_EARPIECE:
530             return DEVICE_OUT_EARPIECE_NAME;
531         case DEVICE_OUT_SPEAKER:
532             return DEVICE_OUT_SPEAKER_NAME;
533         case DEVICE_OUT_WIRED_HEADSET:
534             return DEVICE_OUT_WIRED_HEADSET_NAME;
535         case DEVICE_OUT_WIRED_HEADPHONE:
536             return DEVICE_OUT_WIRED_HEADPHONE_NAME;
537         case DEVICE_OUT_BLUETOOTH_SCO:
538             return DEVICE_OUT_BLUETOOTH_SCO_NAME;
539         case DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
540             return DEVICE_OUT_BLUETOOTH_SCO_HEADSET_NAME;
541         case DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
542             return DEVICE_OUT_BLUETOOTH_SCO_CARKIT_NAME;
543         case DEVICE_OUT_BLUETOOTH_A2DP:
544             return DEVICE_OUT_BLUETOOTH_A2DP_NAME;
545         case DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
546             return DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES_NAME;
547         case DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
548             return DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER_NAME;
549         case DEVICE_OUT_HDMI:
550             return DEVICE_OUT_HDMI_NAME;
551         case DEVICE_OUT_ANLG_DOCK_HEADSET:
552             return DEVICE_OUT_ANLG_DOCK_HEADSET_NAME;
553         case DEVICE_OUT_DGTL_DOCK_HEADSET:
554             return DEVICE_OUT_DGTL_DOCK_HEADSET_NAME;
555         case DEVICE_OUT_USB_ACCESSORY:
556             return DEVICE_OUT_USB_ACCESSORY_NAME;
557         case DEVICE_OUT_USB_DEVICE:
558             return DEVICE_OUT_USB_DEVICE_NAME;
559         case DEVICE_OUT_REMOTE_SUBMIX:
560             return DEVICE_OUT_REMOTE_SUBMIX_NAME;
561         case DEVICE_OUT_TELEPHONY_TX:
562             return DEVICE_OUT_TELEPHONY_TX_NAME;
563         case DEVICE_OUT_LINE:
564             return DEVICE_OUT_LINE_NAME;
565         case DEVICE_OUT_HDMI_ARC:
566             return DEVICE_OUT_HDMI_ARC_NAME;
567         case DEVICE_OUT_SPDIF:
568             return DEVICE_OUT_SPDIF_NAME;
569         case DEVICE_OUT_FM:
570             return DEVICE_OUT_FM_NAME;
571         case DEVICE_OUT_AUX_LINE:
572             return DEVICE_OUT_AUX_LINE_NAME;
573         case DEVICE_OUT_SPEAKER_SAFE:
574             return DEVICE_OUT_SPEAKER_SAFE_NAME;
575         case DEVICE_OUT_IP:
576             return DEVICE_OUT_IP_NAME;
577         case DEVICE_OUT_BUS:
578             return DEVICE_OUT_BUS_NAME;
579         case DEVICE_OUT_DEFAULT:
580         default:
581             return Integer.toString(device);
582         }
583     }
584 
getInputDeviceName(int device)585     public static String getInputDeviceName(int device)
586     {
587         switch(device) {
588         case DEVICE_IN_COMMUNICATION:
589             return DEVICE_IN_COMMUNICATION_NAME;
590         case DEVICE_IN_AMBIENT:
591             return DEVICE_IN_AMBIENT_NAME;
592         case DEVICE_IN_BUILTIN_MIC:
593             return DEVICE_IN_BUILTIN_MIC_NAME;
594         case DEVICE_IN_BLUETOOTH_SCO_HEADSET:
595             return DEVICE_IN_BLUETOOTH_SCO_HEADSET_NAME;
596         case DEVICE_IN_WIRED_HEADSET:
597             return DEVICE_IN_WIRED_HEADSET_NAME;
598         case DEVICE_IN_AUX_DIGITAL:
599             return DEVICE_IN_AUX_DIGITAL_NAME;
600         case DEVICE_IN_TELEPHONY_RX:
601             return DEVICE_IN_TELEPHONY_RX_NAME;
602         case DEVICE_IN_BACK_MIC:
603             return DEVICE_IN_BACK_MIC_NAME;
604         case DEVICE_IN_REMOTE_SUBMIX:
605             return DEVICE_IN_REMOTE_SUBMIX_NAME;
606         case DEVICE_IN_ANLG_DOCK_HEADSET:
607             return DEVICE_IN_ANLG_DOCK_HEADSET_NAME;
608         case DEVICE_IN_DGTL_DOCK_HEADSET:
609             return DEVICE_IN_DGTL_DOCK_HEADSET_NAME;
610         case DEVICE_IN_USB_ACCESSORY:
611             return DEVICE_IN_USB_ACCESSORY_NAME;
612         case DEVICE_IN_USB_DEVICE:
613             return DEVICE_IN_USB_DEVICE_NAME;
614         case DEVICE_IN_FM_TUNER:
615             return DEVICE_IN_FM_TUNER_NAME;
616         case DEVICE_IN_TV_TUNER:
617             return DEVICE_IN_TV_TUNER_NAME;
618         case DEVICE_IN_LINE:
619             return DEVICE_IN_LINE_NAME;
620         case DEVICE_IN_SPDIF:
621             return DEVICE_IN_SPDIF_NAME;
622         case DEVICE_IN_BLUETOOTH_A2DP:
623             return DEVICE_IN_BLUETOOTH_A2DP_NAME;
624         case DEVICE_IN_LOOPBACK:
625             return DEVICE_IN_LOOPBACK_NAME;
626         case DEVICE_IN_IP:
627             return DEVICE_IN_IP_NAME;
628         case DEVICE_IN_BUS:
629             return DEVICE_IN_BUS_NAME;
630         case DEVICE_IN_DEFAULT:
631         default:
632             return Integer.toString(device);
633         }
634     }
635 
636     // phone state, match audio_mode???
637     public static final int PHONE_STATE_OFFCALL = 0;
638     public static final int PHONE_STATE_RINGING = 1;
639     public static final int PHONE_STATE_INCALL = 2;
640 
641     // device categories config for setForceUse, must match audio_policy_forced_cfg_t
642     public static final int FORCE_NONE = 0;
643     public static final int FORCE_SPEAKER = 1;
644     public static final int FORCE_HEADPHONES = 2;
645     public static final int FORCE_BT_SCO = 3;
646     public static final int FORCE_BT_A2DP = 4;
647     public static final int FORCE_WIRED_ACCESSORY = 5;
648     public static final int FORCE_BT_CAR_DOCK = 6;
649     public static final int FORCE_BT_DESK_DOCK = 7;
650     public static final int FORCE_ANALOG_DOCK = 8;
651     public static final int FORCE_DIGITAL_DOCK = 9;
652     public static final int FORCE_NO_BT_A2DP = 10;
653     public static final int FORCE_SYSTEM_ENFORCED = 11;
654     public static final int FORCE_HDMI_SYSTEM_AUDIO_ENFORCED = 12;
655     public static final int FORCE_ENCODED_SURROUND_NEVER = 13;
656     public static final int FORCE_ENCODED_SURROUND_ALWAYS = 14;
657     public static final int NUM_FORCE_CONFIG = 15;
658     public static final int FORCE_DEFAULT = FORCE_NONE;
659 
660     // usage for setForceUse, must match audio_policy_force_use_t
661     public static final int FOR_COMMUNICATION = 0;
662     public static final int FOR_MEDIA = 1;
663     public static final int FOR_RECORD = 2;
664     public static final int FOR_DOCK = 3;
665     public static final int FOR_SYSTEM = 4;
666     public static final int FOR_HDMI_SYSTEM_AUDIO = 5;
667     public static final int FOR_ENCODED_SURROUND = 6;
668     private static final int NUM_FORCE_USE = 7;
669 
670     // usage for AudioRecord.startRecordingSync(), must match AudioSystem::sync_event_t
671     public static final int SYNC_EVENT_NONE = 0;
672     public static final int SYNC_EVENT_PRESENTATION_COMPLETE = 1;
673 
674     /**
675      * @return command completion status, one of {@link #AUDIO_STATUS_OK},
676      *     {@link #AUDIO_STATUS_ERROR} or {@link #AUDIO_STATUS_SERVER_DIED}
677      */
setDeviceConnectionState(int device, int state, String device_address, String device_name)678     public static native int setDeviceConnectionState(int device, int state,
679                                                       String device_address, String device_name);
getDeviceConnectionState(int device, String device_address)680     public static native int getDeviceConnectionState(int device, String device_address);
setPhoneState(int state)681     public static native int setPhoneState(int state);
setForceUse(int usage, int config)682     public static native int setForceUse(int usage, int config);
getForceUse(int usage)683     public static native int getForceUse(int usage);
initStreamVolume(int stream, int indexMin, int indexMax)684     public static native int initStreamVolume(int stream, int indexMin, int indexMax);
setStreamVolumeIndex(int stream, int index, int device)685     public static native int setStreamVolumeIndex(int stream, int index, int device);
getStreamVolumeIndex(int stream, int device)686     public static native int getStreamVolumeIndex(int stream, int device);
setMasterVolume(float value)687     public static native int setMasterVolume(float value);
getMasterVolume()688     public static native float getMasterVolume();
setMasterMute(boolean mute)689     public static native int setMasterMute(boolean mute);
getMasterMute()690     public static native boolean getMasterMute();
getDevicesForStream(int stream)691     public static native int getDevicesForStream(int stream);
692 
693     /** @hide returns true if master mono is enabled. */
getMasterMono()694     public static native boolean getMasterMono();
695     /** @hide enables or disables the master mono mode. */
setMasterMono(boolean mono)696     public static native int setMasterMono(boolean mono);
697 
698     // helpers for android.media.AudioManager.getProperty(), see description there for meaning
getPrimaryOutputSamplingRate()699     public static native int getPrimaryOutputSamplingRate();
getPrimaryOutputFrameCount()700     public static native int getPrimaryOutputFrameCount();
getOutputLatency(int stream)701     public static native int getOutputLatency(int stream);
702 
setLowRamDevice(boolean isLowRamDevice)703     public static native int setLowRamDevice(boolean isLowRamDevice);
checkAudioFlinger()704     public static native int checkAudioFlinger();
705 
listAudioPorts(ArrayList<AudioPort> ports, int[] generation)706     public static native int listAudioPorts(ArrayList<AudioPort> ports, int[] generation);
createAudioPatch(AudioPatch[] patch, AudioPortConfig[] sources, AudioPortConfig[] sinks)707     public static native int createAudioPatch(AudioPatch[] patch,
708                                             AudioPortConfig[] sources, AudioPortConfig[] sinks);
releaseAudioPatch(AudioPatch patch)709     public static native int releaseAudioPatch(AudioPatch patch);
listAudioPatches(ArrayList<AudioPatch> patches, int[] generation)710     public static native int listAudioPatches(ArrayList<AudioPatch> patches, int[] generation);
setAudioPortConfig(AudioPortConfig config)711     public static native int setAudioPortConfig(AudioPortConfig config);
712 
713     // declare this instance as having a dynamic policy callback handler
native_register_dynamic_policy_callback()714     private static native final void native_register_dynamic_policy_callback();
715     // declare this instance as having a recording configuration update callback handler
native_register_recording_callback()716     private static native final void native_register_recording_callback();
717 
718     // must be kept in sync with value in include/system/audio.h
719     public static final int AUDIO_HW_SYNC_INVALID = 0;
720 
getAudioHwSyncForSession(int sessionId)721     public static native int getAudioHwSyncForSession(int sessionId);
722 
registerPolicyMixes(ArrayList<AudioMix> mixes, boolean register)723     public static native int registerPolicyMixes(ArrayList<AudioMix> mixes, boolean register);
724 
systemReady()725     public static native int systemReady();
726 
727     // Items shared with audio service
728 
729     /**
730      * The delay before playing a sound. This small period exists so the user
731      * can press another key (non-volume keys, too) to have it NOT be audible.
732      * <p>
733      * PhoneWindow will implement this part.
734      */
735     public static final int PLAY_SOUND_DELAY = 300;
736 
737     /**
738      * Constant to identify a focus stack entry that is used to hold the focus while the phone
739      * is ringing or during a call. Used by com.android.internal.telephony.CallManager when
740      * entering and exiting calls.
741      */
742     public final static String IN_VOICE_COMM_FOCUS_ID = "AudioFocus_For_Phone_Ring_And_Calls";
743 
744     /**
745      * @see AudioManager#setVibrateSetting(int, int)
746      */
getValueForVibrateSetting(int existingValue, int vibrateType, int vibrateSetting)747     public static int getValueForVibrateSetting(int existingValue, int vibrateType,
748             int vibrateSetting) {
749 
750         // First clear the existing setting. Each vibrate type has two bits in
751         // the value. Note '3' is '11' in binary.
752         existingValue &= ~(3 << (vibrateType * 2));
753 
754         // Set into the old value
755         existingValue |= (vibrateSetting & 3) << (vibrateType * 2);
756 
757         return existingValue;
758     }
759 
getDefaultStreamVolume(int streamType)760     public static int getDefaultStreamVolume(int streamType) {
761         return DEFAULT_STREAM_VOLUME[streamType];
762     }
763 
764     public static int[] DEFAULT_STREAM_VOLUME = new int[] {
765         4,  // STREAM_VOICE_CALL
766         7,  // STREAM_SYSTEM
767         5,  // STREAM_RING
768         11, // STREAM_MUSIC
769         6,  // STREAM_ALARM
770         5,  // STREAM_NOTIFICATION
771         7,  // STREAM_BLUETOOTH_SCO
772         7,  // STREAM_SYSTEM_ENFORCED
773         11, // STREAM_DTMF
774         11  // STREAM_TTS
775     };
776 
streamToString(int stream)777     public static String streamToString(int stream) {
778         if (stream >= 0 && stream < STREAM_NAMES.length) return STREAM_NAMES[stream];
779         if (stream == AudioManager.USE_DEFAULT_STREAM_TYPE) return "USE_DEFAULT_STREAM_TYPE";
780         return "UNKNOWN_STREAM_" + stream;
781     }
782 
783     /** The platform has no specific capabilities */
784     public static final int PLATFORM_DEFAULT = 0;
785     /** The platform is voice call capable (a phone) */
786     public static final int PLATFORM_VOICE = 1;
787     /** The platform is a television or a set-top box */
788     public static final int PLATFORM_TELEVISION = 2;
789 
790     /**
791      * Return the platform type that this is running on. One of:
792      * <ul>
793      * <li>{@link #PLATFORM_VOICE}</li>
794      * <li>{@link #PLATFORM_TELEVISION}</li>
795      * <li>{@link #PLATFORM_DEFAULT}</li>
796      * </ul>
797      */
getPlatformType(Context context)798     public static int getPlatformType(Context context) {
799         if (context.getResources().getBoolean(com.android.internal.R.bool.config_voice_capable)) {
800             return PLATFORM_VOICE;
801         } else if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
802             return PLATFORM_TELEVISION;
803         } else {
804             return PLATFORM_DEFAULT;
805         }
806     }
807 
808     public static final int DEFAULT_MUTE_STREAMS_AFFECTED =
809             (1 << STREAM_MUSIC) |
810             (1 << STREAM_RING) |
811             (1 << STREAM_NOTIFICATION) |
812             (1 << STREAM_SYSTEM);
813 
814     /**
815      * Event posted by AudioTrack and AudioRecord JNI (JNIDeviceCallback) when routing changes.
816      * Keep in sync with core/jni/android_media_DeviceCallback.h.
817      */
818     final static int NATIVE_EVENT_ROUTING_CHANGE = 1000;
819 }
820 
821