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 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame^] | 364 | status_t AudioSystem::getRenderPosition(audio_io_handle_t output, size_t *halFrames, |
| 365 | size_t *dspFrames, 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 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame^] | 374 | if (output == 0) { |
| 375 | output = getOutput(stream); |
| 376 | } |
| 377 | |
| 378 | return af->getRenderPosition(halFrames, dspFrames, output); |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 381 | size_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) { |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 382 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 383 | unsigned int result = 0; |
| 384 | if (af == 0) return result; |
Eric Laurent | be55a2d | 2010-03-11 14:47:00 -0800 | [diff] [blame] | 385 | if (ioHandle == 0) return result; |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 386 | |
| 387 | result = af->getInputFramesLost(ioHandle); |
| 388 | return result; |
| 389 | } |
| 390 | |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 391 | int AudioSystem::newAudioSessionId() { |
| 392 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 393 | if (af == 0) return 0; |
| 394 | return af->newAudioSessionId(); |
| 395 | } |
| 396 | |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 397 | void AudioSystem::acquireAudioSessionId(int audioSession) { |
| 398 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 399 | if (af != 0) { |
| 400 | af->acquireAudioSessionId(audioSession); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | void AudioSystem::releaseAudioSessionId(int audioSession) { |
| 405 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 406 | if (af != 0) { |
| 407 | af->releaseAudioSessionId(audioSession); |
| 408 | } |
| 409 | } |
| 410 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | // --------------------------------------------------------------------------- |
| 412 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 413 | void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | Mutex::Autolock _l(AudioSystem::gLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 416 | AudioSystem::gAudioFlinger.clear(); |
Eric Laurent | 0ef583f | 2010-01-25 10:27:15 -0800 | [diff] [blame] | 417 | // clear output handles and stream to output map caches |
Eric Laurent | 0ef583f | 2010-01-25 10:27:15 -0800 | [diff] [blame] | 418 | AudioSystem::gOutputs.clear(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | |
| 420 | if (gAudioErrorCallback) { |
| 421 | gAudioErrorCallback(DEAD_OBJECT); |
| 422 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 423 | ALOGW("AudioFlinger server died!"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 426 | void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle, |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 427 | const void *param2) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 428 | ALOGV("ioConfigChanged() event %d", event); |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 429 | const OutputDescriptor *desc; |
Glenn Kasten | 211eeaf | 2012-01-20 09:37:45 -0800 | [diff] [blame] | 430 | audio_stream_type_t stream; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 431 | |
Eric Laurent | fa2877b | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 432 | if (ioHandle == 0) return; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 433 | |
| 434 | Mutex::Autolock _l(AudioSystem::gLock); |
| 435 | |
| 436 | switch (event) { |
| 437 | case STREAM_CONFIG_CHANGED: |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 438 | break; |
| 439 | case OUTPUT_OPENED: { |
| 440 | if (gOutputs.indexOfKey(ioHandle) >= 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 441 | ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 442 | break; |
| 443 | } |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 444 | if (param2 == NULL) break; |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 445 | desc = (const OutputDescriptor *)param2; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 446 | |
| 447 | OutputDescriptor *outputDesc = new OutputDescriptor(*desc); |
| 448 | gOutputs.add(ioHandle, outputDesc); |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 449 | ALOGV("ioConfigChanged() new output samplingRate %u, format %d channels %#x frameCount %u " |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 450 | "latency %d", |
| 451 | outputDesc->samplingRate, outputDesc->format, outputDesc->channels, |
| 452 | outputDesc->frameCount, outputDesc->latency); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 453 | } break; |
| 454 | case OUTPUT_CLOSED: { |
| 455 | if (gOutputs.indexOfKey(ioHandle) < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 456 | ALOGW("ioConfigChanged() closing unknow output! %d", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 457 | break; |
| 458 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 459 | ALOGV("ioConfigChanged() output %d closed", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 460 | |
| 461 | gOutputs.removeItem(ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 462 | } break; |
| 463 | |
| 464 | case OUTPUT_CONFIG_CHANGED: { |
| 465 | int index = gOutputs.indexOfKey(ioHandle); |
| 466 | if (index < 0) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 467 | ALOGW("ioConfigChanged() modifying unknow output! %d", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 468 | break; |
| 469 | } |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 470 | if (param2 == NULL) break; |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 471 | desc = (const OutputDescriptor *)param2; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 472 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 473 | 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] | 474 | "frameCount %d latency %d", |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 475 | ioHandle, desc->samplingRate, desc->format, |
| 476 | desc->channels, desc->frameCount, desc->latency); |
| 477 | OutputDescriptor *outputDesc = gOutputs.valueAt(index); |
| 478 | delete outputDesc; |
| 479 | outputDesc = new OutputDescriptor(*desc); |
| 480 | gOutputs.replaceValueFor(ioHandle, outputDesc); |
| 481 | } break; |
| 482 | case INPUT_OPENED: |
| 483 | case INPUT_CLOSED: |
| 484 | case INPUT_CONFIG_CHANGED: |
| 485 | break; |
| 486 | |
| 487 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | void AudioSystem::setErrorCallback(audio_error_callback cb) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 491 | Mutex::Autolock _l(gLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | gAudioErrorCallback = cb; |
| 493 | } |
| 494 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 495 | bool AudioSystem::routedToA2dpOutput(audio_stream_type_t streamType) { |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 496 | switch (streamType) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 497 | case AUDIO_STREAM_MUSIC: |
| 498 | case AUDIO_STREAM_VOICE_CALL: |
| 499 | case AUDIO_STREAM_BLUETOOTH_SCO: |
| 500 | case AUDIO_STREAM_SYSTEM: |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | return true; |
| 502 | default: |
| 503 | return false; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 508 | // client singleton for AudioPolicyService binder interface |
| 509 | sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; |
| 510 | sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; |
| 511 | |
| 512 | |
Glenn Kasten | 18a6d90 | 2012-09-24 11:27:56 -0700 | [diff] [blame] | 513 | // establish binder interface to AudioPolicy service |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 514 | const sp<IAudioPolicyService>& AudioSystem::get_audio_policy_service() |
| 515 | { |
| 516 | gLock.lock(); |
Glenn Kasten | 7fc9a6f | 2012-01-10 10:46:34 -0800 | [diff] [blame] | 517 | if (gAudioPolicyService == 0) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 518 | sp<IServiceManager> sm = defaultServiceManager(); |
| 519 | sp<IBinder> binder; |
| 520 | do { |
| 521 | binder = sm->getService(String16("media.audio_policy")); |
| 522 | if (binder != 0) |
| 523 | break; |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 524 | ALOGW("AudioPolicyService not published, waiting..."); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 525 | usleep(500000); // 0.5 s |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 526 | } while (true); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 527 | if (gAudioPolicyServiceClient == NULL) { |
| 528 | gAudioPolicyServiceClient = new AudioPolicyServiceClient(); |
| 529 | } |
| 530 | binder->linkToDeath(gAudioPolicyServiceClient); |
| 531 | gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); |
| 532 | gLock.unlock(); |
| 533 | } else { |
| 534 | gLock.unlock(); |
| 535 | } |
| 536 | return gAudioPolicyService; |
| 537 | } |
| 538 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 539 | status_t AudioSystem::setDeviceConnectionState(audio_devices_t device, |
| 540 | audio_policy_dev_state_t state, |
| 541 | const char *device_address) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 542 | { |
| 543 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 544 | const char *address = ""; |
| 545 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 546 | if (aps == 0) return PERMISSION_DENIED; |
| 547 | |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 548 | if (device_address != NULL) { |
| 549 | address = device_address; |
| 550 | } |
| 551 | |
| 552 | return aps->setDeviceConnectionState(device, state, address); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 555 | audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 556 | const char *device_address) |
| 557 | { |
| 558 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 559 | if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 560 | |
| 561 | return aps->getDeviceConnectionState(device, device_address); |
| 562 | } |
| 563 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 564 | status_t AudioSystem::setPhoneState(audio_mode_t state) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 565 | { |
Glenn Kasten | 347966c | 2012-01-18 14:58:32 -0800 | [diff] [blame] | 566 | if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 567 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 568 | if (aps == 0) return PERMISSION_DENIED; |
| 569 | |
| 570 | return aps->setPhoneState(state); |
| 571 | } |
| 572 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 573 | 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] | 574 | { |
| 575 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 576 | if (aps == 0) return PERMISSION_DENIED; |
| 577 | return aps->setForceUse(usage, config); |
| 578 | } |
| 579 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 580 | 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] | 581 | { |
| 582 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 583 | if (aps == 0) return AUDIO_POLICY_FORCE_NONE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 584 | return aps->getForceUse(usage); |
| 585 | } |
| 586 | |
| 587 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 588 | audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 589 | uint32_t samplingRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 590 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 591 | audio_channel_mask_t channelMask, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame^] | 592 | audio_output_flags_t flags, |
| 593 | const audio_offload_info_t *offloadInfo) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 594 | { |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 595 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 596 | if (aps == 0) return 0; |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame^] | 597 | return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 600 | status_t AudioSystem::startOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 601 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 602 | int session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 603 | { |
| 604 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 605 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 606 | return aps->startOutput(output, stream, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 609 | status_t AudioSystem::stopOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 610 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 611 | int session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 612 | { |
| 613 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 614 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 615 | return aps->stopOutput(output, stream, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | void AudioSystem::releaseOutput(audio_io_handle_t output) |
| 619 | { |
| 620 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 621 | if (aps == 0) return; |
| 622 | aps->releaseOutput(output); |
| 623 | } |
| 624 | |
Glenn Kasten | eba51fb | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 625 | audio_io_handle_t AudioSystem::getInput(audio_source_t inputSource, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 626 | uint32_t samplingRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 627 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 628 | audio_channel_mask_t channelMask, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 629 | int sessionId) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 630 | { |
| 631 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | fa2877b | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 632 | if (aps == 0) return 0; |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 633 | return aps->getInput(inputSource, samplingRate, format, channelMask, sessionId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | status_t AudioSystem::startInput(audio_io_handle_t input) |
| 637 | { |
| 638 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 639 | if (aps == 0) return PERMISSION_DENIED; |
| 640 | return aps->startInput(input); |
| 641 | } |
| 642 | |
| 643 | status_t AudioSystem::stopInput(audio_io_handle_t input) |
| 644 | { |
| 645 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 646 | if (aps == 0) return PERMISSION_DENIED; |
| 647 | return aps->stopInput(input); |
| 648 | } |
| 649 | |
| 650 | void AudioSystem::releaseInput(audio_io_handle_t input) |
| 651 | { |
| 652 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 653 | if (aps == 0) return; |
| 654 | aps->releaseInput(input); |
| 655 | } |
| 656 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 657 | status_t AudioSystem::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 658 | int indexMin, |
| 659 | int indexMax) |
| 660 | { |
| 661 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 662 | if (aps == 0) return PERMISSION_DENIED; |
| 663 | return aps->initStreamVolume(stream, indexMin, indexMax); |
| 664 | } |
| 665 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 666 | status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, |
| 667 | int index, |
| 668 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 669 | { |
| 670 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 671 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 672 | return aps->setStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 675 | status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, |
| 676 | int *index, |
| 677 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 678 | { |
| 679 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 680 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 681 | return aps->getStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 684 | uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 685 | { |
| 686 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 687 | if (aps == 0) return 0; |
| 688 | return aps->getStrategyForStream(stream); |
| 689 | } |
| 690 | |
Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 691 | audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream) |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 692 | { |
| 693 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 694 | if (aps == 0) return (audio_devices_t)0; |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 695 | return aps->getDevicesForStream(stream); |
| 696 | } |
| 697 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 698 | audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 699 | { |
| 700 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 701 | if (aps == 0) return PERMISSION_DENIED; |
| 702 | return aps->getOutputForEffect(desc); |
| 703 | } |
| 704 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 705 | status_t AudioSystem::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 706 | audio_io_handle_t io, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 707 | uint32_t strategy, |
| 708 | int session, |
| 709 | int id) |
| 710 | { |
| 711 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 712 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 713 | return aps->registerEffect(desc, io, strategy, session, id); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | status_t AudioSystem::unregisterEffect(int id) |
| 717 | { |
| 718 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 719 | if (aps == 0) return PERMISSION_DENIED; |
| 720 | return aps->unregisterEffect(id); |
| 721 | } |
| 722 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 723 | status_t AudioSystem::setEffectEnabled(int id, bool enabled) |
| 724 | { |
| 725 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 726 | if (aps == 0) return PERMISSION_DENIED; |
| 727 | return aps->setEffectEnabled(id, enabled); |
| 728 | } |
| 729 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 730 | 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] | 731 | { |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 732 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 733 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 734 | if (state == NULL) return BAD_VALUE; |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 735 | *state = aps->isStreamActive(stream, inPastMs); |
| 736 | return NO_ERROR; |
| 737 | } |
| 738 | |
Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 739 | status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state, |
| 740 | uint32_t inPastMs) |
| 741 | { |
| 742 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 743 | if (aps == 0) return PERMISSION_DENIED; |
| 744 | if (state == NULL) return BAD_VALUE; |
| 745 | *state = aps->isStreamActiveRemotely(stream, inPastMs); |
| 746 | return NO_ERROR; |
| 747 | } |
| 748 | |
Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 749 | status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) |
| 750 | { |
| 751 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 752 | if (aps == 0) return PERMISSION_DENIED; |
| 753 | if (state == NULL) return BAD_VALUE; |
| 754 | *state = aps->isSourceActive(stream); |
| 755 | return NO_ERROR; |
| 756 | } |
| 757 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 758 | uint32_t AudioSystem::getPrimaryOutputSamplingRate() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 759 | { |
| 760 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 761 | if (af == 0) return 0; |
| 762 | return af->getPrimaryOutputSamplingRate(); |
| 763 | } |
| 764 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 765 | size_t AudioSystem::getPrimaryOutputFrameCount() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 766 | { |
| 767 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 768 | if (af == 0) return 0; |
| 769 | return af->getPrimaryOutputFrameCount(); |
| 770 | } |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 771 | |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 772 | void AudioSystem::clearAudioConfigCache() |
| 773 | { |
| 774 | Mutex::Autolock _l(gLock); |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 775 | ALOGV("clearAudioConfigCache()"); |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 776 | gOutputs.clear(); |
| 777 | } |
| 778 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame^] | 779 | bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info) |
| 780 | { |
| 781 | ALOGV("isOffloadSupported()"); |
| 782 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 783 | if (aps == 0) return false; |
| 784 | return aps->isOffloadSupported(info); |
| 785 | } |
| 786 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 787 | // --------------------------------------------------------------------------- |
| 788 | |
| 789 | void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who) { |
| 790 | Mutex::Autolock _l(AudioSystem::gLock); |
| 791 | AudioSystem::gAudioPolicyService.clear(); |
| 792 | |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 793 | ALOGW("AudioPolicyService server died!"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 794 | } |
| 795 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | }; // namespace android |