| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2006-2007 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 | #define LOG_TAG "AudioSystem" | 
|  | 18 | //#define LOG_NDEBUG 0 | 
|  | 19 |  | 
|  | 20 | #include <utils/Log.h> | 
| Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <binder/IServiceManager.h> | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <media/AudioSystem.h> | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 23 | #include <media/IAudioPolicyService.h> | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <math.h> | 
|  | 25 |  | 
| Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 26 | #include <system/audio.h> | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 27 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 28 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 29 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | namespace android { | 
|  | 31 |  | 
|  | 32 | // client singleton for AudioFlinger binder interface | 
|  | 33 | Mutex AudioSystem::gLock; | 
|  | 34 | sp<IAudioFlinger> AudioSystem::gAudioFlinger; | 
|  | 35 | sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient; | 
|  | 36 | audio_error_callback AudioSystem::gAudioErrorCallback = NULL; | 
|  | 37 | // Cached values | 
| Glenn Kasten | 211eeaf | 2012-01-20 09:37:45 -0800 | [diff] [blame] | 38 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 39 | DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(0); | 
|  | 40 |  | 
| Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 41 | // Cached values for recording queries, all protected by gLock | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | uint32_t AudioSystem::gPrevInSamplingRate = 16000; | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 43 | audio_format_t AudioSystem::gPrevInFormat = AUDIO_FORMAT_PCM_16_BIT; | 
| Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 44 | audio_channel_mask_t AudioSystem::gPrevInChannelMask = AUDIO_CHANNEL_IN_MONO; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | size_t AudioSystem::gInBuffSize = 0; | 
|  | 46 |  | 
|  | 47 |  | 
|  | 48 | // establish binder interface to AudioFlinger service | 
|  | 49 | const sp<IAudioFlinger>& AudioSystem::get_audio_flinger() | 
|  | 50 | { | 
|  | 51 | Mutex::Autolock _l(gLock); | 
| Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 52 | if (gAudioFlinger == 0) { | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | sp<IServiceManager> sm = defaultServiceManager(); | 
|  | 54 | sp<IBinder> binder; | 
|  | 55 | do { | 
|  | 56 | binder = sm->getService(String16("media.audio_flinger")); | 
|  | 57 | if (binder != 0) | 
|  | 58 | break; | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 59 | ALOGW("AudioFlinger not published, waiting..."); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | usleep(500000); // 0.5 s | 
| Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 61 | } while (true); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | if (gAudioFlingerClient == NULL) { | 
|  | 63 | gAudioFlingerClient = new AudioFlingerClient(); | 
|  | 64 | } else { | 
|  | 65 | if (gAudioErrorCallback) { | 
|  | 66 | gAudioErrorCallback(NO_ERROR); | 
|  | 67 | } | 
| Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 68 | } | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | binder->linkToDeath(gAudioFlingerClient); | 
|  | 70 | gAudioFlinger = interface_cast<IAudioFlinger>(binder); | 
|  | 71 | gAudioFlinger->registerClient(gAudioFlingerClient); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 73 | ALOGE_IF(gAudioFlinger==0, "no AudioFlinger!?"); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 74 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | return gAudioFlinger; | 
|  | 76 | } | 
|  | 77 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | status_t AudioSystem::muteMicrophone(bool state) { | 
|  | 79 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 80 | if (af == 0) return PERMISSION_DENIED; | 
|  | 81 | return af->setMicMute(state); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | status_t AudioSystem::isMicrophoneMuted(bool* state) { | 
|  | 85 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 86 | if (af == 0) return PERMISSION_DENIED; | 
|  | 87 | *state = af->getMicMute(); | 
|  | 88 | return NO_ERROR; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | status_t AudioSystem::setMasterVolume(float value) | 
|  | 92 | { | 
|  | 93 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 94 | if (af == 0) return PERMISSION_DENIED; | 
|  | 95 | af->setMasterVolume(value); | 
|  | 96 | return NO_ERROR; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | status_t AudioSystem::setMasterMute(bool mute) | 
|  | 100 | { | 
|  | 101 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 102 | if (af == 0) return PERMISSION_DENIED; | 
|  | 103 | af->setMasterMute(mute); | 
|  | 104 | return NO_ERROR; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | status_t AudioSystem::getMasterVolume(float* volume) | 
|  | 108 | { | 
|  | 109 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 110 | if (af == 0) return PERMISSION_DENIED; | 
|  | 111 | *volume = af->masterVolume(); | 
|  | 112 | return NO_ERROR; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | status_t AudioSystem::getMasterMute(bool* mute) | 
|  | 116 | { | 
|  | 117 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 118 | if (af == 0) return PERMISSION_DENIED; | 
|  | 119 | *mute = af->masterMute(); | 
|  | 120 | return NO_ERROR; | 
|  | 121 | } | 
|  | 122 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 123 | status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value, | 
|  | 124 | audio_io_handle_t output) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 126 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 128 | if (af == 0) return PERMISSION_DENIED; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 129 | af->setStreamVolume(stream, value, output); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | return NO_ERROR; | 
|  | 131 | } | 
|  | 132 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 133 | status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 135 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 137 | if (af == 0) return PERMISSION_DENIED; | 
|  | 138 | af->setStreamMute(stream, mute); | 
|  | 139 | return NO_ERROR; | 
|  | 140 | } | 
|  | 141 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 142 | status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume, | 
|  | 143 | audio_io_handle_t output) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 145 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 147 | if (af == 0) return PERMISSION_DENIED; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 148 | *volume = af->streamVolume(stream, output); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | return NO_ERROR; | 
|  | 150 | } | 
|  | 151 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 152 | status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 154 | if (uint32_t(stream) >= AUDIO_STREAM_CNT) return BAD_VALUE; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 156 | if (af == 0) return PERMISSION_DENIED; | 
|  | 157 | *mute = af->streamMute(stream); | 
|  | 158 | return NO_ERROR; | 
|  | 159 | } | 
|  | 160 |  | 
| Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 161 | status_t AudioSystem::setMode(audio_mode_t mode) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | { | 
| Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 163 | if (uint32_t(mode) >= AUDIO_MODE_CNT) return BAD_VALUE; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 165 | if (af == 0) return PERMISSION_DENIED; | 
|  | 166 | return af->setMode(mode); | 
|  | 167 | } | 
|  | 168 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 169 | status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) { | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 171 | if (af == 0) return PERMISSION_DENIED; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 172 | return af->setParameters(ioHandle, keyValuePairs); | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) { | 
|  | 176 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 177 | String8 result = String8(""); | 
|  | 178 | if (af == 0) return result; | 
|  | 179 |  | 
|  | 180 | result = af->getParameters(ioHandle, keys); | 
|  | 181 | return result; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | } | 
|  | 183 |  | 
|  | 184 | // convert volume steps to natural log scale | 
|  | 185 |  | 
|  | 186 | // change this value to change volume scaling | 
|  | 187 | static const float dBPerStep = 0.5f; | 
|  | 188 | // shouldn't need to touch these | 
|  | 189 | static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f; | 
|  | 190 | static const float dBConvertInverse = 1.0f / dBConvert; | 
|  | 191 |  | 
|  | 192 | float AudioSystem::linearToLog(int volume) | 
|  | 193 | { | 
|  | 194 | // float v = volume ? exp(float(100 - volume) * dBConvert) : 0; | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 195 | // ALOGD("linearToLog(%d)=%f", volume, v); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | // return v; | 
|  | 197 | return volume ? exp(float(100 - volume) * dBConvert) : 0; | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | int AudioSystem::logToLinear(float volume) | 
|  | 201 | { | 
|  | 202 | // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 203 | // ALOGD("logTolinear(%d)=%f", v, volume); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | // return v; | 
|  | 205 | return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; | 
|  | 206 | } | 
|  | 207 |  | 
| Andreas Huber | c813985 | 2012-01-18 10:51:55 -0800 | [diff] [blame] | 208 | // DEPRECATED | 
|  | 209 | status_t AudioSystem::getOutputSamplingRate(int* samplingRate, int streamType) { | 
|  | 210 | return getOutputSamplingRate(samplingRate, (audio_stream_type_t)streamType); | 
|  | 211 | } | 
|  | 212 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 213 | status_t AudioSystem::getOutputSamplingRate(int* samplingRate, audio_stream_type_t streamType) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 215 | audio_io_handle_t output; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 217 | if (streamType == AUDIO_STREAM_DEFAULT) { | 
|  | 218 | streamType = AUDIO_STREAM_MUSIC; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 219 | } | 
|  | 220 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 221 | output = getOutput(streamType); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 222 | if (output == 0) { | 
|  | 223 | return PERMISSION_DENIED; | 
|  | 224 | } | 
|  | 225 |  | 
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 226 | return getSamplingRate(output, streamType, samplingRate); | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | status_t AudioSystem::getSamplingRate(audio_io_handle_t output, | 
|  | 230 | audio_stream_type_t streamType, | 
|  | 231 | int* samplingRate) | 
|  | 232 | { | 
|  | 233 | OutputDescriptor *outputDesc; | 
|  | 234 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 235 | gLock.lock(); | 
|  | 236 | outputDesc = AudioSystem::gOutputs.valueFor(output); | 
| Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 237 | if (outputDesc == NULL) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 238 | ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 239 | gLock.unlock(); | 
|  | 240 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 241 | if (af == 0) return PERMISSION_DENIED; | 
|  | 242 | *samplingRate = af->sampleRate(output); | 
|  | 243 | } else { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 244 | ALOGV("getOutputSamplingRate() reading from output desc"); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 245 | *samplingRate = outputDesc->samplingRate; | 
|  | 246 | gLock.unlock(); | 
|  | 247 | } | 
|  | 248 |  | 
| Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame^] | 249 | ALOGV("getSamplingRate() streamType %d, output %d, sampling rate %d", streamType, output, | 
|  | 250 | *samplingRate); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 251 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | return NO_ERROR; | 
|  | 253 | } | 
|  | 254 |  | 
| Andreas Huber | c813985 | 2012-01-18 10:51:55 -0800 | [diff] [blame] | 255 | // DEPRECATED | 
|  | 256 | status_t AudioSystem::getOutputFrameCount(int* frameCount, int streamType) { | 
|  | 257 | return getOutputFrameCount(frameCount, (audio_stream_type_t)streamType); | 
|  | 258 | } | 
|  | 259 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 260 | status_t AudioSystem::getOutputFrameCount(int* frameCount, audio_stream_type_t streamType) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 262 | audio_io_handle_t output; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 264 | if (streamType == AUDIO_STREAM_DEFAULT) { | 
|  | 265 | streamType = AUDIO_STREAM_MUSIC; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 266 | } | 
| Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 267 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 268 | output = getOutput(streamType); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 269 | if (output == 0) { | 
|  | 270 | return PERMISSION_DENIED; | 
|  | 271 | } | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 |  | 
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 273 | return getFrameCount(output, streamType, frameCount); | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | status_t AudioSystem::getFrameCount(audio_io_handle_t output, | 
|  | 277 | audio_stream_type_t streamType, | 
|  | 278 | int* frameCount) | 
|  | 279 | { | 
|  | 280 | OutputDescriptor *outputDesc; | 
|  | 281 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 282 | gLock.lock(); | 
|  | 283 | outputDesc = AudioSystem::gOutputs.valueFor(output); | 
| Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 284 | if (outputDesc == NULL) { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 285 | gLock.unlock(); | 
|  | 286 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 287 | if (af == 0) return PERMISSION_DENIED; | 
|  | 288 | *frameCount = af->frameCount(output); | 
|  | 289 | } else { | 
|  | 290 | *frameCount = outputDesc->frameCount; | 
|  | 291 | gLock.unlock(); | 
|  | 292 | } | 
|  | 293 |  | 
| Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame^] | 294 | ALOGV("getFrameCount() streamType %d, output %d, frameCount %d", streamType, output, | 
|  | 295 | *frameCount); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 296 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | return NO_ERROR; | 
|  | 298 | } | 
|  | 299 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 300 | status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 302 | audio_io_handle_t output; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 304 | if (streamType == AUDIO_STREAM_DEFAULT) { | 
|  | 305 | streamType = AUDIO_STREAM_MUSIC; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 306 | } | 
| Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 307 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 308 | output = getOutput(streamType); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 309 | if (output == 0) { | 
|  | 310 | return PERMISSION_DENIED; | 
|  | 311 | } | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 |  | 
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 313 | return getLatency(output, streamType, latency); | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | status_t AudioSystem::getLatency(audio_io_handle_t output, | 
|  | 317 | audio_stream_type_t streamType, | 
|  | 318 | uint32_t* latency) | 
|  | 319 | { | 
|  | 320 | OutputDescriptor *outputDesc; | 
|  | 321 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 322 | gLock.lock(); | 
|  | 323 | outputDesc = AudioSystem::gOutputs.valueFor(output); | 
| Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 324 | if (outputDesc == NULL) { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 325 | gLock.unlock(); | 
|  | 326 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 327 | if (af == 0) return PERMISSION_DENIED; | 
|  | 328 | *latency = af->latency(output); | 
|  | 329 | } else { | 
|  | 330 | *latency = outputDesc->latency; | 
|  | 331 | gLock.unlock(); | 
|  | 332 | } | 
|  | 333 |  | 
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 334 | ALOGV("getLatency() streamType %d, output %d, latency %d", streamType, output, *latency); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 335 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | return NO_ERROR; | 
|  | 337 | } | 
|  | 338 |  | 
| Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 339 | status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format, | 
|  | 340 | audio_channel_mask_t channelMask, size_t* buffSize) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | { | 
| Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 342 | gLock.lock(); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | // Do we have a stale gInBufferSize or are we requesting the input buffer size for new values | 
| Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 344 | size_t inBuffSize = gInBuffSize; | 
|  | 345 | if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat) | 
| Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 346 | || (channelMask != gPrevInChannelMask)) { | 
| Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 347 | gLock.unlock(); | 
|  | 348 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 349 | if (af == 0) { | 
|  | 350 | return PERMISSION_DENIED; | 
|  | 351 | } | 
| Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 352 | inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask); | 
| Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 353 | gLock.lock(); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | // save the request params | 
|  | 355 | gPrevInSamplingRate = sampleRate; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 356 | gPrevInFormat = format; | 
| Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 357 | gPrevInChannelMask = channelMask; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 |  | 
| Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 359 | gInBuffSize = inBuffSize; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 360 | } | 
| Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 361 | gLock.unlock(); | 
|  | 362 | *buffSize = inBuffSize; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 363 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | return NO_ERROR; | 
|  | 365 | } | 
|  | 366 |  | 
| Eric Laurent | f0ee6f4 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 367 | status_t AudioSystem::setVoiceVolume(float value) | 
|  | 368 | { | 
|  | 369 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 370 | if (af == 0) return PERMISSION_DENIED; | 
|  | 371 | return af->setVoiceVolume(value); | 
|  | 372 | } | 
|  | 373 |  | 
| Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame^] | 374 | status_t AudioSystem::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames, | 
|  | 375 | audio_stream_type_t stream) | 
| Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 376 | { | 
|  | 377 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 378 | if (af == 0) return PERMISSION_DENIED; | 
|  | 379 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 380 | if (stream == AUDIO_STREAM_DEFAULT) { | 
|  | 381 | stream = AUDIO_STREAM_MUSIC; | 
| Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 382 | } | 
|  | 383 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 384 | return af->getRenderPosition(halFrames, dspFrames, getOutput(stream)); | 
| Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 385 | } | 
|  | 386 |  | 
| Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 387 | unsigned int AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) { | 
|  | 388 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 389 | unsigned int result = 0; | 
|  | 390 | if (af == 0) return result; | 
| Eric Laurent | be55a2d | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 391 | if (ioHandle == 0) return result; | 
| Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 392 |  | 
|  | 393 | result = af->getInputFramesLost(ioHandle); | 
|  | 394 | return result; | 
|  | 395 | } | 
|  | 396 |  | 
| Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 397 | int AudioSystem::newAudioSessionId() { | 
|  | 398 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 399 | if (af == 0) return 0; | 
|  | 400 | return af->newAudioSessionId(); | 
|  | 401 | } | 
|  | 402 |  | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 403 | void AudioSystem::acquireAudioSessionId(int audioSession) { | 
|  | 404 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 405 | if (af != 0) { | 
|  | 406 | af->acquireAudioSessionId(audioSession); | 
|  | 407 | } | 
|  | 408 | } | 
|  | 409 |  | 
|  | 410 | void AudioSystem::releaseAudioSessionId(int audioSession) { | 
|  | 411 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 412 | if (af != 0) { | 
|  | 413 | af->releaseAudioSessionId(audioSession); | 
|  | 414 | } | 
|  | 415 | } | 
|  | 416 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | // --------------------------------------------------------------------------- | 
|  | 418 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 419 | void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) { | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | Mutex::Autolock _l(AudioSystem::gLock); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 422 | AudioSystem::gAudioFlinger.clear(); | 
| Eric Laurent | 0ef583f | 2010-01-25 10:27:15 -0800 | [diff] [blame] | 423 | // clear output handles and stream to output map caches | 
| Eric Laurent | 0ef583f | 2010-01-25 10:27:15 -0800 | [diff] [blame] | 424 | AudioSystem::gOutputs.clear(); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 |  | 
|  | 426 | if (gAudioErrorCallback) { | 
|  | 427 | gAudioErrorCallback(DEAD_OBJECT); | 
|  | 428 | } | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 429 | ALOGW("AudioFlinger server died!"); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | } | 
|  | 431 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 432 | void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle, | 
| Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 433 | const void *param2) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 434 | ALOGV("ioConfigChanged() event %d", event); | 
| Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 435 | const OutputDescriptor *desc; | 
| Glenn Kasten | 211eeaf | 2012-01-20 09:37:45 -0800 | [diff] [blame] | 436 | audio_stream_type_t stream; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 437 |  | 
| Eric Laurent | fa2877b | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 438 | if (ioHandle == 0) return; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 439 |  | 
|  | 440 | Mutex::Autolock _l(AudioSystem::gLock); | 
|  | 441 |  | 
|  | 442 | switch (event) { | 
|  | 443 | case STREAM_CONFIG_CHANGED: | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 444 | break; | 
|  | 445 | case OUTPUT_OPENED: { | 
|  | 446 | if (gOutputs.indexOfKey(ioHandle) >= 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 447 | ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 448 | break; | 
|  | 449 | } | 
| Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 450 | if (param2 == NULL) break; | 
| Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 451 | desc = (const OutputDescriptor *)param2; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 452 |  | 
|  | 453 | OutputDescriptor *outputDesc =  new OutputDescriptor(*desc); | 
|  | 454 | gOutputs.add(ioHandle, outputDesc); | 
| Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame^] | 455 | ALOGV("ioConfigChanged() new output samplingRate %d, format %d channels %#x frameCount %d " | 
|  | 456 | "latency %d", | 
|  | 457 | outputDesc->samplingRate, outputDesc->format, outputDesc->channels, | 
|  | 458 | outputDesc->frameCount, outputDesc->latency); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 459 | } break; | 
|  | 460 | case OUTPUT_CLOSED: { | 
|  | 461 | if (gOutputs.indexOfKey(ioHandle) < 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 462 | ALOGW("ioConfigChanged() closing unknow output! %d", ioHandle); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 463 | break; | 
|  | 464 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 465 | ALOGV("ioConfigChanged() output %d closed", ioHandle); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 466 |  | 
|  | 467 | gOutputs.removeItem(ioHandle); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 468 | } break; | 
|  | 469 |  | 
|  | 470 | case OUTPUT_CONFIG_CHANGED: { | 
|  | 471 | int index = gOutputs.indexOfKey(ioHandle); | 
|  | 472 | if (index < 0) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 473 | ALOGW("ioConfigChanged() modifying unknow output! %d", ioHandle); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 474 | break; | 
|  | 475 | } | 
| Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 476 | if (param2 == NULL) break; | 
| Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 477 | desc = (const OutputDescriptor *)param2; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 478 |  | 
| Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame^] | 479 | ALOGV("ioConfigChanged() new config for output %d samplingRate %d, format %d channels %#x " | 
|  | 480 | "frameCount %d latency %d", | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 481 | ioHandle, desc->samplingRate, desc->format, | 
|  | 482 | desc->channels, desc->frameCount, desc->latency); | 
|  | 483 | OutputDescriptor *outputDesc = gOutputs.valueAt(index); | 
|  | 484 | delete outputDesc; | 
|  | 485 | outputDesc =  new OutputDescriptor(*desc); | 
|  | 486 | gOutputs.replaceValueFor(ioHandle, outputDesc); | 
|  | 487 | } break; | 
|  | 488 | case INPUT_OPENED: | 
|  | 489 | case INPUT_CLOSED: | 
|  | 490 | case INPUT_CONFIG_CHANGED: | 
|  | 491 | break; | 
|  | 492 |  | 
|  | 493 | } | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | } | 
|  | 495 |  | 
|  | 496 | void AudioSystem::setErrorCallback(audio_error_callback cb) { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 497 | Mutex::Autolock _l(gLock); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | gAudioErrorCallback = cb; | 
|  | 499 | } | 
|  | 500 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 501 | bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType) { | 
| Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 502 | switch (streamType) { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 503 | case AUDIO_STREAM_MUSIC: | 
|  | 504 | case AUDIO_STREAM_VOICE_CALL: | 
|  | 505 | case AUDIO_STREAM_BLUETOOTH_SCO: | 
|  | 506 | case AUDIO_STREAM_SYSTEM: | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | return true; | 
|  | 508 | default: | 
|  | 509 | return false; | 
|  | 510 | } | 
|  | 511 | } | 
|  | 512 |  | 
|  | 513 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 514 | // client singleton for AudioPolicyService binder interface | 
|  | 515 | sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; | 
|  | 516 | sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; | 
|  | 517 |  | 
|  | 518 |  | 
| Glenn Kasten | 18a6d90 | 2012-09-24 11:27:56 -0700 | [diff] [blame] | 519 | // establish binder interface to AudioPolicy service | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 520 | const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service() | 
|  | 521 | { | 
|  | 522 | gLock.lock(); | 
| Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 523 | if (gAudioPolicyService == 0) { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 524 | sp<IServiceManager> sm = defaultServiceManager(); | 
|  | 525 | sp<IBinder> binder; | 
|  | 526 | do { | 
|  | 527 | binder = sm->getService(String16("media.audio_policy")); | 
|  | 528 | if (binder != 0) | 
|  | 529 | break; | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 530 | ALOGW("AudioPolicyService not published, waiting..."); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 531 | usleep(500000); // 0.5 s | 
| Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 532 | } while (true); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 533 | if (gAudioPolicyServiceClient == NULL) { | 
|  | 534 | gAudioPolicyServiceClient = new AudioPolicyServiceClient(); | 
|  | 535 | } | 
|  | 536 | binder->linkToDeath(gAudioPolicyServiceClient); | 
|  | 537 | gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); | 
|  | 538 | gLock.unlock(); | 
|  | 539 | } else { | 
|  | 540 | gLock.unlock(); | 
|  | 541 | } | 
|  | 542 | return gAudioPolicyService; | 
|  | 543 | } | 
|  | 544 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 545 | status_t AudioSystem::setDeviceConnectionState(audio_devices_t device, | 
|  | 546 | audio_policy_dev_state_t state, | 
|  | 547 | const char *device_address) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 548 | { | 
|  | 549 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
| Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 550 | const char *address = ""; | 
|  | 551 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 552 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 553 |  | 
| Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 554 | if (device_address != NULL) { | 
|  | 555 | address = device_address; | 
|  | 556 | } | 
|  | 557 |  | 
|  | 558 | return aps->setDeviceConnectionState(device, state, address); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 559 | } | 
|  | 560 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 561 | audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device, | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 562 | const char *device_address) | 
|  | 563 | { | 
|  | 564 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 565 | if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 566 |  | 
|  | 567 | return aps->getDeviceConnectionState(device, device_address); | 
|  | 568 | } | 
|  | 569 |  | 
| Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 570 | status_t AudioSystem::setPhoneState(audio_mode_t state) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 571 | { | 
| Glenn Kasten | 347966c | 2012-01-18 14:58:32 -0800 | [diff] [blame] | 572 | if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 573 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 574 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 575 |  | 
|  | 576 | return aps->setPhoneState(state); | 
|  | 577 | } | 
|  | 578 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 579 | status_t AudioSystem::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 580 | { | 
|  | 581 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 582 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 583 | return aps->setForceUse(usage, config); | 
|  | 584 | } | 
|  | 585 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 586 | audio_policy_forced_cfg_t AudioSystem::getForceUse(audio_policy_force_use_t usage) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 587 | { | 
|  | 588 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 589 | if (aps == 0) return AUDIO_POLICY_FORCE_NONE; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 590 | return aps->getForceUse(usage); | 
|  | 591 | } | 
|  | 592 |  | 
|  | 593 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 594 | audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream, | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 595 | uint32_t samplingRate, | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 596 | audio_format_t format, | 
| Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 597 | audio_channel_mask_t channelMask, | 
| Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 598 | audio_output_flags_t flags) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 599 | { | 
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 600 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 601 | if (aps == 0) return 0; | 
| Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 602 | return aps->getOutput(stream, samplingRate, format, channelMask, flags); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 603 | } | 
|  | 604 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 605 | status_t AudioSystem::startOutput(audio_io_handle_t output, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 606 | audio_stream_type_t stream, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 607 | int session) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 608 | { | 
|  | 609 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 610 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 611 | return aps->startOutput(output, stream, session); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 612 | } | 
|  | 613 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 614 | status_t AudioSystem::stopOutput(audio_io_handle_t output, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 615 | audio_stream_type_t stream, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 616 | int session) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 617 | { | 
|  | 618 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 619 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 620 | return aps->stopOutput(output, stream, session); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 621 | } | 
|  | 622 |  | 
|  | 623 | void AudioSystem::releaseOutput(audio_io_handle_t output) | 
|  | 624 | { | 
|  | 625 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 626 | if (aps == 0) return; | 
|  | 627 | aps->releaseOutput(output); | 
|  | 628 | } | 
|  | 629 |  | 
| Glenn Kasten | eba51fb | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 630 | audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource, | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 631 | uint32_t samplingRate, | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 632 | audio_format_t format, | 
| Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 633 | audio_channel_mask_t channelMask, | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 634 | int sessionId) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 635 | { | 
|  | 636 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
| Eric Laurent | fa2877b | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 637 | if (aps == 0) return 0; | 
| Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 638 | return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 639 | } | 
|  | 640 |  | 
|  | 641 | status_t AudioSystem::startInput(audio_io_handle_t input) | 
|  | 642 | { | 
|  | 643 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 644 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 645 | return aps->startInput(input); | 
|  | 646 | } | 
|  | 647 |  | 
|  | 648 | status_t AudioSystem::stopInput(audio_io_handle_t input) | 
|  | 649 | { | 
|  | 650 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 651 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 652 | return aps->stopInput(input); | 
|  | 653 | } | 
|  | 654 |  | 
|  | 655 | void AudioSystem::releaseInput(audio_io_handle_t input) | 
|  | 656 | { | 
|  | 657 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 658 | if (aps == 0) return; | 
|  | 659 | aps->releaseInput(input); | 
|  | 660 | } | 
|  | 661 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 662 | status_t AudioSystem::initStreamVolume(audio_stream_type_t stream, | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 663 | int indexMin, | 
|  | 664 | int indexMax) | 
|  | 665 | { | 
|  | 666 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 667 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 668 | return aps->initStreamVolume(stream, indexMin, indexMax); | 
|  | 669 | } | 
|  | 670 |  | 
| Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 671 | status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, | 
|  | 672 | int index, | 
|  | 673 | audio_devices_t device) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 674 | { | 
|  | 675 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 676 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 677 | return aps->setStreamVolumeIndex(stream, index, device); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 678 | } | 
|  | 679 |  | 
| Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 680 | status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, | 
|  | 681 | int *index, | 
|  | 682 | audio_devices_t device) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 683 | { | 
|  | 684 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 685 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 686 | return aps->getStreamVolumeIndex(stream, index, device); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 687 | } | 
|  | 688 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 689 | uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 690 | { | 
|  | 691 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 692 | if (aps == 0) return 0; | 
|  | 693 | return aps->getStrategyForStream(stream); | 
|  | 694 | } | 
|  | 695 |  | 
| Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 696 | audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream) | 
| Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 697 | { | 
|  | 698 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
| Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 699 | if (aps == 0) return (audio_devices_t)0; | 
| Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 700 | return aps->getDevicesForStream(stream); | 
|  | 701 | } | 
|  | 702 |  | 
| Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 703 | audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc) | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 704 | { | 
|  | 705 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 706 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 707 | return aps->getOutputForEffect(desc); | 
|  | 708 | } | 
|  | 709 |  | 
| Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 710 | status_t AudioSystem::registerEffect(const effect_descriptor_t *desc, | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 711 | audio_io_handle_t io, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 712 | uint32_t strategy, | 
|  | 713 | int session, | 
|  | 714 | int id) | 
|  | 715 | { | 
|  | 716 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 717 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 718 | return aps->registerEffect(desc, io, strategy, session, id); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 719 | } | 
|  | 720 |  | 
|  | 721 | status_t AudioSystem::unregisterEffect(int id) | 
|  | 722 | { | 
|  | 723 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 724 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 725 | return aps->unregisterEffect(id); | 
|  | 726 | } | 
|  | 727 |  | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 728 | status_t AudioSystem::setEffectEnabled(int id, bool enabled) | 
|  | 729 | { | 
|  | 730 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 731 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 732 | return aps->setEffectEnabled(id, enabled); | 
|  | 733 | } | 
|  | 734 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 735 | status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 736 | { | 
| Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 737 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 738 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 739 | if (state == NULL) return BAD_VALUE; | 
| Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 740 | *state = aps->isStreamActive(stream, inPastMs); | 
|  | 741 | return NO_ERROR; | 
|  | 742 | } | 
|  | 743 |  | 
| Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 744 | status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) | 
|  | 745 | { | 
|  | 746 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 747 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 748 | if (state == NULL) return BAD_VALUE; | 
|  | 749 | *state = aps->isSourceActive(stream); | 
|  | 750 | return NO_ERROR; | 
|  | 751 | } | 
|  | 752 |  | 
| Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 753 | int32_t AudioSystem::getPrimaryOutputSamplingRate() | 
|  | 754 | { | 
|  | 755 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 756 | if (af == 0) return 0; | 
|  | 757 | return af->getPrimaryOutputSamplingRate(); | 
|  | 758 | } | 
|  | 759 |  | 
|  | 760 | int32_t AudioSystem::getPrimaryOutputFrameCount() | 
|  | 761 | { | 
|  | 762 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 763 | if (af == 0) return 0; | 
|  | 764 | return af->getPrimaryOutputFrameCount(); | 
|  | 765 | } | 
| Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 766 |  | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 767 | void AudioSystem::clearAudioConfigCache() | 
|  | 768 | { | 
|  | 769 | Mutex::Autolock _l(gLock); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 770 | ALOGV("clearAudioConfigCache()"); | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 771 | gOutputs.clear(); | 
|  | 772 | } | 
|  | 773 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 774 | // --------------------------------------------------------------------------- | 
|  | 775 |  | 
|  | 776 | void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) { | 
|  | 777 | Mutex::Autolock _l(AudioSystem::gLock); | 
|  | 778 | AudioSystem::gAudioPolicyService.clear(); | 
|  | 779 |  | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 780 | ALOGW("AudioPolicyService server died!"); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 781 | } | 
|  | 782 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | }; // namespace android |