PageRenderTime 27ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/PlaceHolderTTSEngine/native/project/jni/media/AudioSystem.h

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