AudioSystem.h revision db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903
1/* 2 * Copyright (C) 2008 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#ifndef ANDROID_AUDIOSYSTEM_H_ 18#define ANDROID_AUDIOSYSTEM_H_ 19 20#include <utils/RefBase.h> 21#include <utils/threads.h> 22#include <media/IAudioFlinger.h> 23 24/* XXX: Should be include by all the users instead */ 25#include <media/AudioParameter.h> 26 27namespace android { 28 29typedef void (*audio_error_callback)(status_t err); 30typedef int audio_io_handle_t; 31 32class IAudioPolicyService; 33class String8; 34 35class AudioSystem 36{ 37public: 38 39 // must match android/media/AudioSystem.java STREAM_* constants 40 enum stream_type { 41 DEFAULT =-1, 42 VOICE_CALL = 0, 43 SYSTEM = 1, 44 RING = 2, 45 MUSIC = 3, 46 ALARM = 4, 47 NOTIFICATION = 5, 48 BLUETOOTH_SCO = 6, 49 ENFORCED_AUDIBLE = 7, // Sounds that cannot be muted by user and must be routed to speaker 50 DTMF = 8, 51 TTS = 9, 52 NUM_STREAM_TYPES 53 }; 54 55 // Audio sub formats (see AudioSystem::audio_format). 56 enum pcm_sub_format { 57 PCM_SUB_16_BIT = 0x1, // must be 1 for backward compatibility 58 PCM_SUB_8_BIT = 0x2, // must be 2 for backward compatibility 59 }; 60 61 // FIXME These sub_format enums are currently unused 62 63 // MP3 sub format field definition : can use 11 LSBs in the same way as MP3 frame header to specify 64 // bit rate, stereo mode, version... 65 enum mp3_sub_format { 66 //TODO 67 }; 68 69 // AMR NB/WB sub format field definition: specify frame block interleaving, bandwidth efficient or octet aligned, 70 // encoding mode for recording... 71 enum amr_sub_format { 72 //TODO 73 }; 74 75 // AAC sub format field definition: specify profile or bitrate for recording... 76 enum aac_sub_format { 77 //TODO 78 }; 79 80 // VORBIS sub format field definition: specify quality for recording... 81 enum vorbis_sub_format { 82 //TODO 83 }; 84 85 // Audio format consists in a main format field (upper 8 bits) and a sub format field (lower 24 bits). 86 // The main format indicates the main codec type. The sub format field indicates options and parameters 87 // for each format. The sub format is mainly used for record to indicate for instance the requested bitrate 88 // or profile. It can also be used for certain formats to give informations not present in the encoded 89 // audio stream (e.g. octet alignement for AMR). 90 enum audio_format { 91 INVALID_FORMAT = -1, 92 FORMAT_DEFAULT = 0, 93 PCM = 0x00000000, // must be 0 for backward compatibility 94 MP3 = 0x01000000, 95 AMR_NB = 0x02000000, 96 AMR_WB = 0x03000000, 97 AAC = 0x04000000, 98 HE_AAC_V1 = 0x05000000, 99 HE_AAC_V2 = 0x06000000, 100 VORBIS = 0x07000000, 101 MAIN_FORMAT_MASK = 0xFF000000, 102 SUB_FORMAT_MASK = 0x00FFFFFF, 103 // Aliases 104 PCM_16_BIT = (PCM|PCM_SUB_16_BIT), 105 PCM_8_BIT = (PCM|PCM_SUB_8_BIT) 106 }; 107 108 109 // Channel mask definitions must be kept in sync with values in /media/java/android/media/AudioFormat.java 110 enum audio_channels { 111 // output channels 112 CHANNEL_OUT_FRONT_LEFT = 0x4, 113 CHANNEL_OUT_FRONT_RIGHT = 0x8, 114 CHANNEL_OUT_FRONT_CENTER = 0x10, 115 CHANNEL_OUT_LOW_FREQUENCY = 0x20, 116 CHANNEL_OUT_BACK_LEFT = 0x40, 117 CHANNEL_OUT_BACK_RIGHT = 0x80, 118 CHANNEL_OUT_FRONT_LEFT_OF_CENTER = 0x100, 119 CHANNEL_OUT_FRONT_RIGHT_OF_CENTER = 0x200, 120 CHANNEL_OUT_BACK_CENTER = 0x400, 121 CHANNEL_OUT_MONO = CHANNEL_OUT_FRONT_LEFT, 122 CHANNEL_OUT_STEREO = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT), 123 CHANNEL_OUT_QUAD = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT | 124 CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT), 125 CHANNEL_OUT_SURROUND = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT | 126 CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_BACK_CENTER), 127 CHANNEL_OUT_5POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT | 128 CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT), 129 CHANNEL_OUT_7POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT | 130 CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT | 131 CHANNEL_OUT_FRONT_LEFT_OF_CENTER | CHANNEL_OUT_FRONT_RIGHT_OF_CENTER), 132 CHANNEL_OUT_ALL = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT | 133 CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT | 134 CHANNEL_OUT_FRONT_LEFT_OF_CENTER | CHANNEL_OUT_FRONT_RIGHT_OF_CENTER | CHANNEL_OUT_BACK_CENTER), 135 136 // input channels 137 CHANNEL_IN_LEFT = 0x4, 138 CHANNEL_IN_RIGHT = 0x8, 139 CHANNEL_IN_FRONT = 0x10, 140 CHANNEL_IN_BACK = 0x20, 141 CHANNEL_IN_LEFT_PROCESSED = 0x40, 142 CHANNEL_IN_RIGHT_PROCESSED = 0x80, 143 CHANNEL_IN_FRONT_PROCESSED = 0x100, 144 CHANNEL_IN_BACK_PROCESSED = 0x200, 145 CHANNEL_IN_PRESSURE = 0x400, 146 CHANNEL_IN_X_AXIS = 0x800, 147 CHANNEL_IN_Y_AXIS = 0x1000, 148 CHANNEL_IN_Z_AXIS = 0x2000, 149 CHANNEL_IN_VOICE_UPLINK = 0x4000, 150 CHANNEL_IN_VOICE_DNLINK = 0x8000, 151 CHANNEL_IN_MONO = CHANNEL_IN_FRONT, 152 CHANNEL_IN_STEREO = (CHANNEL_IN_LEFT | CHANNEL_IN_RIGHT), 153 CHANNEL_IN_ALL = (CHANNEL_IN_LEFT | CHANNEL_IN_RIGHT | CHANNEL_IN_FRONT | CHANNEL_IN_BACK| 154 CHANNEL_IN_LEFT_PROCESSED | CHANNEL_IN_RIGHT_PROCESSED | CHANNEL_IN_FRONT_PROCESSED | CHANNEL_IN_BACK_PROCESSED| 155 CHANNEL_IN_PRESSURE | CHANNEL_IN_X_AXIS | CHANNEL_IN_Y_AXIS | CHANNEL_IN_Z_AXIS | 156 CHANNEL_IN_VOICE_UPLINK | CHANNEL_IN_VOICE_DNLINK) 157 }; 158 159 // must match android/media/AudioSystem.java MODE_* values 160 enum audio_mode { 161 MODE_INVALID = -2, 162 MODE_CURRENT = -1, 163 MODE_NORMAL = 0, 164 MODE_RINGTONE, 165 MODE_IN_CALL, 166 MODE_IN_COMMUNICATION, 167 NUM_MODES // not a valid entry, denotes end-of-list 168 }; 169 170 enum audio_in_acoustics { 171 AGC_ENABLE = 0x0001, 172 AGC_DISABLE = 0, 173 NS_ENABLE = 0x0002, 174 NS_DISABLE = 0, 175 TX_IIR_ENABLE = 0x0004, 176 TX_DISABLE = 0 177 }; 178 179 // special audio session values 180 enum audio_sessions { 181 SESSION_OUTPUT_STAGE = -1, // session for effects attached to a particular output stream 182 // (value must be less than 0) 183 SESSION_OUTPUT_MIX = 0, // session for effects applied to output mix. These effects can 184 // be moved by audio policy manager to another output stream 185 // (value must be 0) 186 }; 187 188 /* These are static methods to control the system-wide AudioFlinger 189 * only privileged processes can have access to them 190 */ 191 192 // mute/unmute microphone 193 static status_t muteMicrophone(bool state); 194 static status_t isMicrophoneMuted(bool *state); 195 196 // set/get master volume 197 static status_t setMasterVolume(float value); 198 static status_t getMasterVolume(float* volume); 199 200 // mute/unmute audio outputs 201 static status_t setMasterMute(bool mute); 202 static status_t getMasterMute(bool* mute); 203 204 // set/get stream volume on specified output 205 static status_t setStreamVolume(int stream, float value, int output); 206 static status_t getStreamVolume(int stream, float* volume, int output); 207 208 // mute/unmute stream 209 static status_t setStreamMute(int stream, bool mute); 210 static status_t getStreamMute(int stream, bool* mute); 211 212 // set audio mode in audio hardware (see AudioSystem::audio_mode) 213 static status_t setMode(int mode); 214 215 // returns true in *state if tracks are active on the specified stream or has been active 216 // in the past inPastMs milliseconds 217 static status_t isStreamActive(int stream, bool *state, uint32_t inPastMs = 0); 218 219 // set/get audio hardware parameters. The function accepts a list of parameters 220 // key value pairs in the form: key1=value1;key2=value2;... 221 // Some keys are reserved for standard parameters (See AudioParameter class). 222 static status_t setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs); 223 static String8 getParameters(audio_io_handle_t ioHandle, const String8& keys); 224 225 static void setErrorCallback(audio_error_callback cb); 226 227 // helper function to obtain AudioFlinger service handle 228 static const sp<IAudioFlinger>& get_audio_flinger(); 229 230 static float linearToLog(int volume); 231 static int logToLinear(float volume); 232 233 static status_t getOutputSamplingRate(int* samplingRate, int stream = DEFAULT); 234 static status_t getOutputFrameCount(int* frameCount, int stream = DEFAULT); 235 static status_t getOutputLatency(uint32_t* latency, int stream = DEFAULT); 236 237 static bool routedToA2dpOutput(int streamType); 238 239 static status_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount, 240 size_t* buffSize); 241 242 static status_t setVoiceVolume(float volume); 243 244 // return the number of audio frames written by AudioFlinger to audio HAL and 245 // audio dsp to DAC since the output on which the specified stream is playing 246 // has exited standby. 247 // returned status (from utils/Errors.h) can be: 248 // - NO_ERROR: successful operation, halFrames and dspFrames point to valid data 249 // - INVALID_OPERATION: Not supported on current hardware platform 250 // - BAD_VALUE: invalid parameter 251 // NOTE: this feature is not supported on all hardware platforms and it is 252 // necessary to check returned status before using the returned values. 253 static status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, int stream = DEFAULT); 254 255 static unsigned int getInputFramesLost(audio_io_handle_t ioHandle); 256 257 static int newAudioSessionId(); 258 // 259 // AudioPolicyService interface 260 // 261 262 enum audio_devices { 263 // output devices 264 DEVICE_OUT_EARPIECE = 0x1, 265 DEVICE_OUT_SPEAKER = 0x2, 266 DEVICE_OUT_WIRED_HEADSET = 0x4, 267 DEVICE_OUT_WIRED_HEADPHONE = 0x8, 268 DEVICE_OUT_BLUETOOTH_SCO = 0x10, 269 DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20, 270 DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40, 271 DEVICE_OUT_BLUETOOTH_A2DP = 0x80, 272 DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100, 273 DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200, 274 DEVICE_OUT_AUX_DIGITAL = 0x400, 275 DEVICE_OUT_ANLG_DOCK_HEADSET = 0x800, 276 DEVICE_OUT_DGTL_DOCK_HEADSET = 0x1000, 277 DEVICE_OUT_DEFAULT = 0x8000, 278 DEVICE_OUT_ALL = (DEVICE_OUT_EARPIECE | DEVICE_OUT_SPEAKER | DEVICE_OUT_WIRED_HEADSET | 279 DEVICE_OUT_WIRED_HEADPHONE | DEVICE_OUT_BLUETOOTH_SCO | DEVICE_OUT_BLUETOOTH_SCO_HEADSET | 280 DEVICE_OUT_BLUETOOTH_SCO_CARKIT | DEVICE_OUT_BLUETOOTH_A2DP | DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | 281 DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER | DEVICE_OUT_AUX_DIGITAL | 282 DEVICE_OUT_ANLG_DOCK_HEADSET | DEVICE_OUT_DGTL_DOCK_HEADSET | 283 DEVICE_OUT_DEFAULT), 284 DEVICE_OUT_ALL_A2DP = (DEVICE_OUT_BLUETOOTH_A2DP | DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | 285 DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER), 286 287 // input devices 288 DEVICE_IN_COMMUNICATION = 0x10000, 289 DEVICE_IN_AMBIENT = 0x20000, 290 DEVICE_IN_BUILTIN_MIC = 0x40000, 291 DEVICE_IN_BLUETOOTH_SCO_HEADSET = 0x80000, 292 DEVICE_IN_WIRED_HEADSET = 0x100000, 293 DEVICE_IN_AUX_DIGITAL = 0x200000, 294 DEVICE_IN_VOICE_CALL = 0x400000, 295 DEVICE_IN_BACK_MIC = 0x800000, 296 DEVICE_IN_DEFAULT = 0x80000000, 297 298 DEVICE_IN_ALL = (DEVICE_IN_COMMUNICATION | DEVICE_IN_AMBIENT | DEVICE_IN_BUILTIN_MIC | 299 DEVICE_IN_BLUETOOTH_SCO_HEADSET | DEVICE_IN_WIRED_HEADSET | DEVICE_IN_AUX_DIGITAL | 300 DEVICE_IN_VOICE_CALL | DEVICE_IN_BACK_MIC | DEVICE_IN_DEFAULT) 301 }; 302 303 // device connection states used for setDeviceConnectionState() 304 enum device_connection_state { 305 DEVICE_STATE_UNAVAILABLE, 306 DEVICE_STATE_AVAILABLE, 307 NUM_DEVICE_STATES 308 }; 309 310 // request to open a direct output with getOutput() (by opposition to sharing an output with other AudioTracks) 311 enum output_flags { 312 OUTPUT_FLAG_INDIRECT = 0x0, 313 OUTPUT_FLAG_DIRECT = 0x1 314 }; 315 316 // device categories used for setForceUse() 317 enum forced_config { 318 FORCE_NONE, 319 FORCE_SPEAKER, 320 FORCE_HEADPHONES, 321 FORCE_BT_SCO, 322 FORCE_BT_A2DP, 323 FORCE_WIRED_ACCESSORY, 324 FORCE_BT_CAR_DOCK, 325 FORCE_BT_DESK_DOCK, 326 FORCE_ANALOG_DOCK, 327 FORCE_DIGITAL_DOCK, 328 NUM_FORCE_CONFIG, 329 FORCE_DEFAULT = FORCE_NONE 330 }; 331 332 // usages used for setForceUse(), must match AudioSystem.java 333 enum force_use { 334 FOR_COMMUNICATION, 335 FOR_MEDIA, 336 FOR_RECORD, 337 FOR_DOCK, 338 NUM_FORCE_USE 339 }; 340 341 // types of io configuration change events received with ioConfigChanged() 342 enum io_config_event { 343 OUTPUT_OPENED, 344 OUTPUT_CLOSED, 345 OUTPUT_CONFIG_CHANGED, 346 INPUT_OPENED, 347 INPUT_CLOSED, 348 INPUT_CONFIG_CHANGED, 349 STREAM_CONFIG_CHANGED, 350 NUM_CONFIG_EVENTS 351 }; 352 353 // audio output descritor used to cache output configurations in client process to avoid frequent calls 354 // through IAudioFlinger 355 class OutputDescriptor { 356 public: 357 OutputDescriptor() 358 : samplingRate(0), format(0), channels(0), frameCount(0), latency(0) {} 359 360 uint32_t samplingRate; 361 int32_t format; 362 int32_t channels; 363 size_t frameCount; 364 uint32_t latency; 365 }; 366 367 // 368 // IAudioPolicyService interface (see AudioPolicyInterface for method descriptions) 369 // 370 static status_t setDeviceConnectionState(audio_devices device, device_connection_state state, const char *device_address); 371 static device_connection_state getDeviceConnectionState(audio_devices device, const char *device_address); 372 static status_t setPhoneState(int state); 373 static status_t setRingerMode(uint32_t mode, uint32_t mask); 374 static status_t setForceUse(force_use usage, forced_config config); 375 static forced_config getForceUse(force_use usage); 376 static audio_io_handle_t getOutput(stream_type stream, 377 uint32_t samplingRate = 0, 378 uint32_t format = FORMAT_DEFAULT, 379 uint32_t channels = CHANNEL_OUT_STEREO, 380 output_flags flags = OUTPUT_FLAG_INDIRECT); 381 static status_t startOutput(audio_io_handle_t output, 382 AudioSystem::stream_type stream, 383 int session = 0); 384 static status_t stopOutput(audio_io_handle_t output, 385 AudioSystem::stream_type stream, 386 int session = 0); 387 static void releaseOutput(audio_io_handle_t output); 388 static audio_io_handle_t getInput(int inputSource, 389 uint32_t samplingRate = 0, 390 uint32_t format = FORMAT_DEFAULT, 391 uint32_t channels = CHANNEL_IN_MONO, 392 audio_in_acoustics acoustics = (audio_in_acoustics)0); 393 static status_t startInput(audio_io_handle_t input); 394 static status_t stopInput(audio_io_handle_t input); 395 static void releaseInput(audio_io_handle_t input); 396 static status_t initStreamVolume(stream_type stream, 397 int indexMin, 398 int indexMax); 399 static status_t setStreamVolumeIndex(stream_type stream, int index); 400 static status_t getStreamVolumeIndex(stream_type stream, int *index); 401 402 static uint32_t getStrategyForStream(stream_type stream); 403 static uint32_t getDevicesForStream(stream_type stream); 404 405 static audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc); 406 static status_t registerEffect(effect_descriptor_t *desc, 407 audio_io_handle_t output, 408 uint32_t strategy, 409 int session, 410 int id); 411 static status_t unregisterEffect(int id); 412 413 static const sp<IAudioPolicyService>& get_audio_policy_service(); 414 415 // ---------------------------------------------------------------------------- 416 417 static uint32_t popCount(uint32_t u); 418 static bool isOutputDevice(audio_devices device); 419 static bool isInputDevice(audio_devices device); 420 static bool isA2dpDevice(audio_devices device); 421 static bool isBluetoothScoDevice(audio_devices device); 422 static bool isLowVisibility(stream_type stream); 423 static bool isOutputChannel(uint32_t channel); 424 static bool isInputChannel(uint32_t channel); 425 static bool isValidFormat(uint32_t format); 426 static bool isLinearPCM(uint32_t format); 427 428private: 429 430 class AudioFlingerClient: public IBinder::DeathRecipient, public BnAudioFlingerClient 431 { 432 public: 433 AudioFlingerClient() { 434 } 435 436 // DeathRecipient 437 virtual void binderDied(const wp<IBinder>& who); 438 439 // IAudioFlingerClient 440 441 // indicate a change in the configuration of an output or input: keeps the cached 442 // values for output/input parameters upto date in client process 443 virtual void ioConfigChanged(int event, int ioHandle, void *param2); 444 }; 445 446 class AudioPolicyServiceClient: public IBinder::DeathRecipient 447 { 448 public: 449 AudioPolicyServiceClient() { 450 } 451 452 // DeathRecipient 453 virtual void binderDied(const wp<IBinder>& who); 454 }; 455 456 static sp<AudioFlingerClient> gAudioFlingerClient; 457 static sp<AudioPolicyServiceClient> gAudioPolicyServiceClient; 458 friend class AudioFlingerClient; 459 friend class AudioPolicyServiceClient; 460 461 static Mutex gLock; 462 static sp<IAudioFlinger> gAudioFlinger; 463 static audio_error_callback gAudioErrorCallback; 464 465 static size_t gInBuffSize; 466 // previous parameters for recording buffer size queries 467 static uint32_t gPrevInSamplingRate; 468 static int gPrevInFormat; 469 static int gPrevInChannelCount; 470 471 static sp<IAudioPolicyService> gAudioPolicyService; 472 473 // mapping between stream types and outputs 474 static DefaultKeyedVector<int, audio_io_handle_t> gStreamOutputMap; 475 // list of output descritor containing cached parameters (sampling rate, framecount, channel count...) 476 static DefaultKeyedVector<audio_io_handle_t, OutputDescriptor *> gOutputs; 477}; 478 479}; // namespace android 480 481#endif /*ANDROID_AUDIOSYSTEM_H_*/ 482