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