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