The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame^] | 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 | |
| 28 | class AudioSystem |
| 29 | { |
| 30 | public: |
| 31 | |
| 32 | enum stream_type { |
| 33 | DEFAULT =-1, |
| 34 | VOICE_CALL = 0, |
| 35 | SYSTEM = 1, |
| 36 | RING = 2, |
| 37 | MUSIC = 3, |
| 38 | ALARM = 4, |
| 39 | NOTIFICATION = 5, |
| 40 | BLUETOOTH_SCO = 6, |
| 41 | NUM_STREAM_TYPES |
| 42 | }; |
| 43 | |
| 44 | enum audio_output_type { |
| 45 | AUDIO_OUTPUT_DEFAULT =-1, |
| 46 | AUDIO_OUTPUT_HARDWARE = 0, |
| 47 | AUDIO_OUTPUT_A2DP = 1, |
| 48 | NUM_AUDIO_OUTPUT_TYPES |
| 49 | }; |
| 50 | |
| 51 | enum audio_format { |
| 52 | FORMAT_DEFAULT = 0, |
| 53 | PCM_16_BIT, |
| 54 | PCM_8_BIT, |
| 55 | INVALID_FORMAT |
| 56 | }; |
| 57 | |
| 58 | enum audio_mode { |
| 59 | MODE_INVALID = -2, |
| 60 | MODE_CURRENT = -1, |
| 61 | MODE_NORMAL = 0, |
| 62 | MODE_RINGTONE, |
| 63 | MODE_IN_CALL, |
| 64 | NUM_MODES // not a valid entry, denotes end-of-list |
| 65 | }; |
| 66 | |
| 67 | enum audio_routes { |
| 68 | ROUTE_EARPIECE = (1 << 0), |
| 69 | ROUTE_SPEAKER = (1 << 1), |
| 70 | ROUTE_BLUETOOTH_SCO = (1 << 2), |
| 71 | ROUTE_HEADSET = (1 << 3), |
| 72 | ROUTE_BLUETOOTH_A2DP = (1 << 4), |
| 73 | ROUTE_ALL = -1UL, |
| 74 | }; |
| 75 | |
| 76 | enum audio_in_acoustics { |
| 77 | AGC_ENABLE = 0x0001, |
| 78 | AGC_DISABLE = 0, |
| 79 | NS_ENABLE = 0x0002, |
| 80 | NS_DISABLE = 0, |
| 81 | TX_IIR_ENABLE = 0x0004, |
| 82 | TX_DISABLE = 0 |
| 83 | }; |
| 84 | |
| 85 | /* These are static methods to control the system-wide AudioFlinger |
| 86 | * only privileged processes can have access to them |
| 87 | */ |
| 88 | |
| 89 | // routing helper functions |
| 90 | static status_t speakerphone(bool state); |
| 91 | static status_t isSpeakerphoneOn(bool* state); |
| 92 | static status_t bluetoothSco(bool state); |
| 93 | static status_t isBluetoothScoOn(bool* state); |
| 94 | static status_t muteMicrophone(bool state); |
| 95 | static status_t isMicrophoneMuted(bool *state); |
| 96 | |
| 97 | static status_t setMasterVolume(float value); |
| 98 | static status_t setMasterMute(bool mute); |
| 99 | static status_t getMasterVolume(float* volume); |
| 100 | static status_t getMasterMute(bool* mute); |
| 101 | |
| 102 | static status_t setStreamVolume(int stream, float value); |
| 103 | static status_t setStreamMute(int stream, bool mute); |
| 104 | static status_t getStreamVolume(int stream, float* volume); |
| 105 | static status_t getStreamMute(int stream, bool* mute); |
| 106 | |
| 107 | static status_t setMode(int mode); |
| 108 | static status_t getMode(int* mode); |
| 109 | |
| 110 | static status_t setRouting(int mode, uint32_t routes, uint32_t mask); |
| 111 | static status_t getRouting(int mode, uint32_t* routes); |
| 112 | |
| 113 | static status_t isMusicActive(bool *state); |
| 114 | |
| 115 | // Temporary interface, do not use |
| 116 | // TODO: Replace with a more generic key:value get/set mechanism |
| 117 | static status_t setParameter(const char* key, const char* value); |
| 118 | |
| 119 | static void setErrorCallback(audio_error_callback cb); |
| 120 | |
| 121 | // helper function to obtain AudioFlinger service handle |
| 122 | static const sp<IAudioFlinger>& get_audio_flinger(); |
| 123 | |
| 124 | static float linearToLog(int volume); |
| 125 | static int logToLinear(float volume); |
| 126 | |
| 127 | static status_t getOutputSamplingRate(int* samplingRate, int stream = DEFAULT); |
| 128 | static status_t getOutputFrameCount(int* frameCount, int stream = DEFAULT); |
| 129 | static status_t getOutputLatency(uint32_t* latency, int stream = DEFAULT); |
| 130 | |
| 131 | static bool routedToA2dpOutput(int streamType); |
| 132 | |
| 133 | static status_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount, |
| 134 | size_t* buffSize); |
| 135 | |
| 136 | // ---------------------------------------------------------------------------- |
| 137 | |
| 138 | private: |
| 139 | |
| 140 | class AudioFlingerClient: public IBinder::DeathRecipient, public BnAudioFlingerClient |
| 141 | { |
| 142 | public: |
| 143 | AudioFlingerClient() { |
| 144 | } |
| 145 | |
| 146 | // DeathRecipient |
| 147 | virtual void binderDied(const wp<IBinder>& who); |
| 148 | |
| 149 | // IAudioFlingerClient |
| 150 | virtual void a2dpEnabledChanged(bool enabled); |
| 151 | |
| 152 | }; |
| 153 | static int getOutput(int streamType); |
| 154 | |
| 155 | static sp<AudioFlingerClient> gAudioFlingerClient; |
| 156 | |
| 157 | friend class AudioFlingerClient; |
| 158 | |
| 159 | static Mutex gLock; |
| 160 | static sp<IAudioFlinger> gAudioFlinger; |
| 161 | static audio_error_callback gAudioErrorCallback; |
| 162 | static int gOutSamplingRate[NUM_AUDIO_OUTPUT_TYPES]; |
| 163 | static int gOutFrameCount[NUM_AUDIO_OUTPUT_TYPES]; |
| 164 | static uint32_t gOutLatency[NUM_AUDIO_OUTPUT_TYPES]; |
| 165 | static bool gA2dpEnabled; |
| 166 | |
| 167 | static size_t gInBuffSize; |
| 168 | // previous parameters for recording buffer size queries |
| 169 | static uint32_t gPrevInSamplingRate; |
| 170 | static int gPrevInFormat; |
| 171 | static int gPrevInChannelCount; |
| 172 | |
| 173 | }; |
| 174 | |
| 175 | }; // namespace android |
| 176 | |
| 177 | #endif /*ANDROID_AUDIOSYSTEM_H_*/ |