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 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 208 | status_t AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 210 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 212 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 213 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 216 | output = getOutput(streamType); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 217 | if (output == 0) { |
| 218 | return PERMISSION_DENIED; |
| 219 | } |
| 220 | |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 221 | return getSamplingRate(output, streamType, samplingRate); |
| 222 | } |
| 223 | |
| 224 | status_t AudioSystem::getSamplingRate(audio_io_handle_t output, |
| 225 | audio_stream_type_t streamType, |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 226 | uint32_t* samplingRate) |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 227 | { |
| 228 | OutputDescriptor *outputDesc; |
| 229 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 230 | gLock.lock(); |
| 231 | outputDesc = AudioSystem::gOutputs.valueFor(output); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 232 | if (outputDesc == NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 233 | ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 234 | gLock.unlock(); |
| 235 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 236 | if (af == 0) return PERMISSION_DENIED; |
| 237 | *samplingRate = af->sampleRate(output); |
| 238 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 239 | ALOGV("getOutputSamplingRate() reading from output desc"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 240 | *samplingRate = outputDesc->samplingRate; |
| 241 | gLock.unlock(); |
| 242 | } |
| 243 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 244 | ALOGV("getSamplingRate() streamType %d, output %d, sampling rate %u", streamType, output, |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 245 | *samplingRate); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 246 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | return NO_ERROR; |
| 248 | } |
| 249 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame^] | 250 | status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 252 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 254 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 255 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 256 | } |
Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 257 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 258 | output = getOutput(streamType); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 259 | if (output == 0) { |
| 260 | return PERMISSION_DENIED; |
| 261 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 263 | return getFrameCount(output, streamType, frameCount); |
| 264 | } |
| 265 | |
| 266 | status_t AudioSystem::getFrameCount(audio_io_handle_t output, |
| 267 | audio_stream_type_t streamType, |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame^] | 268 | size_t* frameCount) |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 269 | { |
| 270 | OutputDescriptor *outputDesc; |
| 271 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 272 | gLock.lock(); |
| 273 | outputDesc = AudioSystem::gOutputs.valueFor(output); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 274 | if (outputDesc == NULL) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 275 | gLock.unlock(); |
| 276 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 277 | if (af == 0) return PERMISSION_DENIED; |
| 278 | *frameCount = af->frameCount(output); |
| 279 | } else { |
| 280 | *frameCount = outputDesc->frameCount; |
| 281 | gLock.unlock(); |
| 282 | } |
| 283 | |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 284 | ALOGV("getFrameCount() streamType %d, output %d, frameCount %d", streamType, output, |
| 285 | *frameCount); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 286 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | return NO_ERROR; |
| 288 | } |
| 289 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 290 | 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] | 291 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 292 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 294 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 295 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 296 | } |
Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 297 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 298 | output = getOutput(streamType); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 299 | if (output == 0) { |
| 300 | return PERMISSION_DENIED; |
| 301 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 303 | return getLatency(output, streamType, latency); |
| 304 | } |
| 305 | |
| 306 | status_t AudioSystem::getLatency(audio_io_handle_t output, |
| 307 | audio_stream_type_t streamType, |
| 308 | uint32_t* latency) |
| 309 | { |
| 310 | OutputDescriptor *outputDesc; |
| 311 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 312 | gLock.lock(); |
| 313 | outputDesc = AudioSystem::gOutputs.valueFor(output); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 314 | if (outputDesc == NULL) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 315 | gLock.unlock(); |
| 316 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 317 | if (af == 0) return PERMISSION_DENIED; |
| 318 | *latency = af->latency(output); |
| 319 | } else { |
| 320 | *latency = outputDesc->latency; |
| 321 | gLock.unlock(); |
| 322 | } |
| 323 | |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 324 | ALOGV("getLatency() streamType %d, output %d, latency %d", streamType, output, *latency); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 325 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | return NO_ERROR; |
| 327 | } |
| 328 | |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 329 | status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format, |
| 330 | audio_channel_mask_t channelMask, size_t* buffSize) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | { |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 332 | gLock.lock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | // 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] | 334 | size_t inBuffSize = gInBuffSize; |
| 335 | if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat) |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 336 | || (channelMask != gPrevInChannelMask)) { |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 337 | gLock.unlock(); |
| 338 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 339 | if (af == 0) { |
| 340 | return PERMISSION_DENIED; |
| 341 | } |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 342 | inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask); |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 343 | gLock.lock(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | // save the request params |
| 345 | gPrevInSamplingRate = sampleRate; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 346 | gPrevInFormat = format; |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 347 | gPrevInChannelMask = channelMask; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 349 | gInBuffSize = inBuffSize; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 350 | } |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 351 | gLock.unlock(); |
| 352 | *buffSize = inBuffSize; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 353 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | return NO_ERROR; |
| 355 | } |
| 356 | |
Eric Laurent | f0ee6f4 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 357 | status_t AudioSystem::setVoiceVolume(float value) |
| 358 | { |
| 359 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 360 | if (af == 0) return PERMISSION_DENIED; |
| 361 | return af->setVoiceVolume(value); |
| 362 | } |
| 363 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame^] | 364 | status_t AudioSystem::getRenderPosition(size_t *halFrames, size_t *dspFrames, |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 365 | audio_stream_type_t stream) |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 366 | { |
| 367 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 368 | if (af == 0) return PERMISSION_DENIED; |
| 369 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 370 | if (stream == AUDIO_STREAM_DEFAULT) { |
| 371 | stream = AUDIO_STREAM_MUSIC; |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 374 | return af->getRenderPosition(halFrames, dspFrames, getOutput(stream)); |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame^] | 377 | size_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) { |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 378 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 379 | unsigned int result = 0; |
| 380 | if (af == 0) return result; |
Eric Laurent | be55a2d | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 381 | if (ioHandle == 0) return result; |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 382 | |
| 383 | result = af->getInputFramesLost(ioHandle); |
| 384 | return result; |
| 385 | } |
| 386 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 387 | int AudioSystem::newAudioSessionId() { |
| 388 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 389 | if (af == 0) return 0; |
| 390 | return af->newAudioSessionId(); |
| 391 | } |
| 392 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 393 | void AudioSystem::acquireAudioSessionId(int audioSession) { |
| 394 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 395 | if (af != 0) { |
| 396 | af->acquireAudioSessionId(audioSession); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | void AudioSystem::releaseAudioSessionId(int audioSession) { |
| 401 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 402 | if (af != 0) { |
| 403 | af->releaseAudioSessionId(audioSession); |
| 404 | } |
| 405 | } |
| 406 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | // --------------------------------------------------------------------------- |
| 408 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 409 | void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | Mutex::Autolock _l(AudioSystem::gLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 412 | AudioSystem::gAudioFlinger.clear(); |
Eric Laurent | 0ef583f | 2010-01-25 10:27:15 -0800 | [diff] [blame] | 413 | // clear output handles and stream to output map caches |
Eric Laurent | 0ef583f | 2010-01-25 10:27:15 -0800 | [diff] [blame] | 414 | AudioSystem::gOutputs.clear(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | |
| 416 | if (gAudioErrorCallback) { |
| 417 | gAudioErrorCallback(DEAD_OBJECT); |
| 418 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 419 | ALOGW("AudioFlinger server died!"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 422 | void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle, |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 423 | const void *param2) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 424 | ALOGV("ioConfigChanged() event %d", event); |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 425 | const OutputDescriptor *desc; |
Glenn Kasten | 211eeaf | 2012-01-20 09:37:45 -0800 | [diff] [blame] | 426 | audio_stream_type_t stream; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 427 | |
Eric Laurent | fa2877b | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 428 | if (ioHandle == 0) return; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 429 | |
| 430 | Mutex::Autolock _l(AudioSystem::gLock); |
| 431 | |
| 432 | switch (event) { |
| 433 | case STREAM_CONFIG_CHANGED: |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 434 | break; |
| 435 | case OUTPUT_OPENED: { |
| 436 | if (gOutputs.indexOfKey(ioHandle) >= 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 437 | ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 438 | break; |
| 439 | } |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 440 | if (param2 == NULL) break; |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 441 | desc = (const OutputDescriptor *)param2; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 442 | |
| 443 | OutputDescriptor *outputDesc = new OutputDescriptor(*desc); |
| 444 | gOutputs.add(ioHandle, outputDesc); |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame^] | 445 | ALOGV("ioConfigChanged() new output samplingRate %u, format %d channels %#x frameCount %u " |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 446 | "latency %d", |
| 447 | outputDesc->samplingRate, outputDesc->format, outputDesc->channels, |
| 448 | outputDesc->frameCount, outputDesc->latency); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 449 | } break; |
| 450 | case OUTPUT_CLOSED: { |
| 451 | if (gOutputs.indexOfKey(ioHandle) < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 452 | ALOGW("ioConfigChanged() closing unknow output! %d", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 453 | break; |
| 454 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 455 | ALOGV("ioConfigChanged() output %d closed", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 456 | |
| 457 | gOutputs.removeItem(ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 458 | } break; |
| 459 | |
| 460 | case OUTPUT_CONFIG_CHANGED: { |
| 461 | int index = gOutputs.indexOfKey(ioHandle); |
| 462 | if (index < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 463 | ALOGW("ioConfigChanged() modifying unknow output! %d", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 464 | break; |
| 465 | } |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 466 | if (param2 == NULL) break; |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 467 | desc = (const OutputDescriptor *)param2; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 468 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 469 | ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %d channels %#x " |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 470 | "frameCount %d latency %d", |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 471 | ioHandle, desc->samplingRate, desc->format, |
| 472 | desc->channels, desc->frameCount, desc->latency); |
| 473 | OutputDescriptor *outputDesc = gOutputs.valueAt(index); |
| 474 | delete outputDesc; |
| 475 | outputDesc = new OutputDescriptor(*desc); |
| 476 | gOutputs.replaceValueFor(ioHandle, outputDesc); |
| 477 | } break; |
| 478 | case INPUT_OPENED: |
| 479 | case INPUT_CLOSED: |
| 480 | case INPUT_CONFIG_CHANGED: |
| 481 | break; |
| 482 | |
| 483 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | void AudioSystem::setErrorCallback(audio_error_callback cb) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 487 | Mutex::Autolock _l(gLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | gAudioErrorCallback = cb; |
| 489 | } |
| 490 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 491 | bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 492 | switch (streamType) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 493 | case AUDIO_STREAM_MUSIC: |
| 494 | case AUDIO_STREAM_VOICE_CALL: |
| 495 | case AUDIO_STREAM_BLUETOOTH_SCO: |
| 496 | case AUDIO_STREAM_SYSTEM: |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | return true; |
| 498 | default: |
| 499 | return false; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 504 | // client singleton for AudioPolicyService binder interface |
| 505 | sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; |
| 506 | sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; |
| 507 | |
| 508 | |
Glenn Kasten | 18a6d90 | 2012-09-24 11:27:56 -0700 | [diff] [blame] | 509 | // establish binder interface to AudioPolicy service |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 510 | const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service() |
| 511 | { |
| 512 | gLock.lock(); |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 513 | if (gAudioPolicyService == 0) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 514 | sp<IServiceManager> sm = defaultServiceManager(); |
| 515 | sp<IBinder> binder; |
| 516 | do { |
| 517 | binder = sm->getService(String16("media.audio_policy")); |
| 518 | if (binder != 0) |
| 519 | break; |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 520 | ALOGW("AudioPolicyService not published, waiting..."); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 521 | usleep(500000); // 0.5 s |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 522 | } while (true); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 523 | if (gAudioPolicyServiceClient == NULL) { |
| 524 | gAudioPolicyServiceClient = new AudioPolicyServiceClient(); |
| 525 | } |
| 526 | binder->linkToDeath(gAudioPolicyServiceClient); |
| 527 | gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); |
| 528 | gLock.unlock(); |
| 529 | } else { |
| 530 | gLock.unlock(); |
| 531 | } |
| 532 | return gAudioPolicyService; |
| 533 | } |
| 534 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 535 | status_t AudioSystem::setDeviceConnectionState(audio_devices_t device, |
| 536 | audio_policy_dev_state_t state, |
| 537 | const char *device_address) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 538 | { |
| 539 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 540 | const char *address = ""; |
| 541 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 542 | if (aps == 0) return PERMISSION_DENIED; |
| 543 | |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 544 | if (device_address != NULL) { |
| 545 | address = device_address; |
| 546 | } |
| 547 | |
| 548 | return aps->setDeviceConnectionState(device, state, address); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 551 | audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 552 | const char *device_address) |
| 553 | { |
| 554 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 555 | if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 556 | |
| 557 | return aps->getDeviceConnectionState(device, device_address); |
| 558 | } |
| 559 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 560 | status_t AudioSystem::setPhoneState(audio_mode_t state) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 561 | { |
Glenn Kasten | 347966c | 2012-01-18 14:58:32 -0800 | [diff] [blame] | 562 | if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 563 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 564 | if (aps == 0) return PERMISSION_DENIED; |
| 565 | |
| 566 | return aps->setPhoneState(state); |
| 567 | } |
| 568 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 569 | 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] | 570 | { |
| 571 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 572 | if (aps == 0) return PERMISSION_DENIED; |
| 573 | return aps->setForceUse(usage, config); |
| 574 | } |
| 575 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 576 | 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] | 577 | { |
| 578 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 579 | if (aps == 0) return AUDIO_POLICY_FORCE_NONE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 580 | return aps->getForceUse(usage); |
| 581 | } |
| 582 | |
| 583 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 584 | audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 585 | uint32_t samplingRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 586 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 587 | audio_channel_mask_t channelMask, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 588 | audio_output_flags_t flags) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 589 | { |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 590 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 591 | if (aps == 0) return 0; |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 592 | return aps->getOutput(stream, samplingRate, format, channelMask, flags); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 595 | status_t AudioSystem::startOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 596 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 597 | int session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 598 | { |
| 599 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 600 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 601 | return aps->startOutput(output, stream, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 604 | status_t AudioSystem::stopOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 605 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 606 | int session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 607 | { |
| 608 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 609 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 610 | return aps->stopOutput(output, stream, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void AudioSystem::releaseOutput(audio_io_handle_t output) |
| 614 | { |
| 615 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 616 | if (aps == 0) return; |
| 617 | aps->releaseOutput(output); |
| 618 | } |
| 619 | |
Glenn Kasten | eba51fb | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 620 | audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 621 | uint32_t samplingRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 622 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 623 | audio_channel_mask_t channelMask, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 624 | int sessionId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 625 | { |
| 626 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | fa2877b | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 627 | if (aps == 0) return 0; |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 628 | return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | status_t AudioSystem::startInput(audio_io_handle_t input) |
| 632 | { |
| 633 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 634 | if (aps == 0) return PERMISSION_DENIED; |
| 635 | return aps->startInput(input); |
| 636 | } |
| 637 | |
| 638 | status_t AudioSystem::stopInput(audio_io_handle_t input) |
| 639 | { |
| 640 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 641 | if (aps == 0) return PERMISSION_DENIED; |
| 642 | return aps->stopInput(input); |
| 643 | } |
| 644 | |
| 645 | void AudioSystem::releaseInput(audio_io_handle_t input) |
| 646 | { |
| 647 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 648 | if (aps == 0) return; |
| 649 | aps->releaseInput(input); |
| 650 | } |
| 651 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 652 | status_t AudioSystem::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 653 | int indexMin, |
| 654 | int indexMax) |
| 655 | { |
| 656 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 657 | if (aps == 0) return PERMISSION_DENIED; |
| 658 | return aps->initStreamVolume(stream, indexMin, indexMax); |
| 659 | } |
| 660 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 661 | status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, |
| 662 | int index, |
| 663 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 664 | { |
| 665 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 666 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 667 | return aps->setStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 670 | status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, |
| 671 | int *index, |
| 672 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 673 | { |
| 674 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 675 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 676 | return aps->getStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 679 | uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 680 | { |
| 681 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 682 | if (aps == 0) return 0; |
| 683 | return aps->getStrategyForStream(stream); |
| 684 | } |
| 685 | |
Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 686 | audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream) |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 687 | { |
| 688 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 689 | if (aps == 0) return (audio_devices_t)0; |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 690 | return aps->getDevicesForStream(stream); |
| 691 | } |
| 692 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 693 | audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 694 | { |
| 695 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 696 | if (aps == 0) return PERMISSION_DENIED; |
| 697 | return aps->getOutputForEffect(desc); |
| 698 | } |
| 699 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 700 | status_t AudioSystem::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 701 | audio_io_handle_t io, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 702 | uint32_t strategy, |
| 703 | int session, |
| 704 | int id) |
| 705 | { |
| 706 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 707 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 708 | return aps->registerEffect(desc, io, strategy, session, id); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | status_t AudioSystem::unregisterEffect(int id) |
| 712 | { |
| 713 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 714 | if (aps == 0) return PERMISSION_DENIED; |
| 715 | return aps->unregisterEffect(id); |
| 716 | } |
| 717 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 718 | status_t AudioSystem::setEffectEnabled(int id, bool enabled) |
| 719 | { |
| 720 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 721 | if (aps == 0) return PERMISSION_DENIED; |
| 722 | return aps->setEffectEnabled(id, enabled); |
| 723 | } |
| 724 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 725 | 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] | 726 | { |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 727 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 728 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 729 | if (state == NULL) return BAD_VALUE; |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 730 | *state = aps->isStreamActive(stream, inPastMs); |
| 731 | return NO_ERROR; |
| 732 | } |
| 733 | |
Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 734 | status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) |
| 735 | { |
| 736 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 737 | if (aps == 0) return PERMISSION_DENIED; |
| 738 | if (state == NULL) return BAD_VALUE; |
| 739 | *state = aps->isSourceActive(stream); |
| 740 | return NO_ERROR; |
| 741 | } |
| 742 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 743 | uint32_t AudioSystem::getPrimaryOutputSamplingRate() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 744 | { |
| 745 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 746 | if (af == 0) return 0; |
| 747 | return af->getPrimaryOutputSamplingRate(); |
| 748 | } |
| 749 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame^] | 750 | size_t AudioSystem::getPrimaryOutputFrameCount() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 751 | { |
| 752 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 753 | if (af == 0) return 0; |
| 754 | return af->getPrimaryOutputFrameCount(); |
| 755 | } |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 756 | |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 757 | void AudioSystem::clearAudioConfigCache() |
| 758 | { |
| 759 | Mutex::Autolock _l(gLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 760 | ALOGV("clearAudioConfigCache()"); |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 761 | gOutputs.clear(); |
| 762 | } |
| 763 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 764 | // --------------------------------------------------------------------------- |
| 765 | |
| 766 | void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) { |
| 767 | Mutex::Autolock _l(AudioSystem::gLock); |
| 768 | AudioSystem::gAudioPolicyService.clear(); |
| 769 | |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 770 | ALOGW("AudioPolicyService server died!"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 771 | } |
| 772 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | }; // namespace android |