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