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