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