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