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; |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 35 | Mutex AudioSystem::gLockCache; |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 36 | Mutex AudioSystem::gLockAPS; |
| 37 | Mutex AudioSystem::gLockAPC; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | sp<IAudioFlinger> AudioSystem::gAudioFlinger; |
| 39 | sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient; |
| 40 | audio_error_callback AudioSystem::gAudioErrorCallback = NULL; |
Glenn Kasten | 211eeaf | 2012-01-20 09:37:45 -0800 | [diff] [blame] | 41 | |
Glenn Kasten | 2301acc | 2014-01-17 10:21:00 -0800 | [diff] [blame] | 42 | // Cached values for output handles |
Glenn Kasten | 9ea65d0 | 2014-01-17 10:21:24 -0800 | [diff] [blame] | 43 | DefaultKeyedVector<audio_io_handle_t, AudioSystem::OutputDescriptor *> AudioSystem::gOutputs(NULL); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 44 | |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 45 | // Cached values for recording queries, all protected by gLock |
Glenn Kasten | 5446e54 | 2014-01-08 08:58:53 -0800 | [diff] [blame] | 46 | uint32_t AudioSystem::gPrevInSamplingRate; |
| 47 | audio_format_t AudioSystem::gPrevInFormat; |
| 48 | audio_channel_mask_t AudioSystem::gPrevInChannelMask; |
| 49 | size_t AudioSystem::gInBuffSize = 0; // zero indicates cache is invalid |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 51 | sp<AudioSystem::AudioPortCallback> AudioSystem::gAudioPortCallback; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
| 53 | // establish binder interface to AudioFlinger service |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 54 | const sp<IAudioFlinger> AudioSystem::get_audio_flinger() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 56 | sp<IAudioFlinger> af; |
| 57 | sp<AudioFlingerClient> afc; |
| 58 | { |
| 59 | Mutex::Autolock _l(gLock); |
| 60 | if (gAudioFlinger == 0) { |
| 61 | sp<IServiceManager> sm = defaultServiceManager(); |
| 62 | sp<IBinder> binder; |
| 63 | do { |
| 64 | binder = sm->getService(String16("media.audio_flinger")); |
| 65 | if (binder != 0) |
| 66 | break; |
| 67 | ALOGW("AudioFlinger not published, waiting..."); |
| 68 | usleep(500000); // 0.5 s |
| 69 | } while (true); |
| 70 | if (gAudioFlingerClient == NULL) { |
| 71 | gAudioFlingerClient = new AudioFlingerClient(); |
| 72 | } else { |
| 73 | if (gAudioErrorCallback) { |
| 74 | gAudioErrorCallback(NO_ERROR); |
| 75 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 77 | binder->linkToDeath(gAudioFlingerClient); |
| 78 | gAudioFlinger = interface_cast<IAudioFlinger>(binder); |
| 79 | LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0); |
| 80 | afc = gAudioFlingerClient; |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 81 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 82 | af = gAudioFlinger; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 84 | if (afc != 0) { |
| 85 | af->registerClient(afc); |
| 86 | } |
| 87 | return af; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Eric Laurent | 4629161 | 2013-07-18 14:38:44 -0700 | [diff] [blame] | 90 | /* static */ status_t AudioSystem::checkAudioFlinger() |
| 91 | { |
| 92 | if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) { |
| 93 | return NO_ERROR; |
| 94 | } |
| 95 | return DEAD_OBJECT; |
| 96 | } |
| 97 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 98 | status_t AudioSystem::muteMicrophone(bool state) |
| 99 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 101 | if (af == 0) return PERMISSION_DENIED; |
| 102 | return af->setMicMute(state); |
| 103 | } |
| 104 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 105 | status_t AudioSystem::isMicrophoneMuted(bool* state) |
| 106 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 108 | if (af == 0) return PERMISSION_DENIED; |
| 109 | *state = af->getMicMute(); |
| 110 | return NO_ERROR; |
| 111 | } |
| 112 | |
| 113 | status_t AudioSystem::setMasterVolume(float value) |
| 114 | { |
| 115 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 116 | if (af == 0) return PERMISSION_DENIED; |
| 117 | af->setMasterVolume(value); |
| 118 | return NO_ERROR; |
| 119 | } |
| 120 | |
| 121 | status_t AudioSystem::setMasterMute(bool mute) |
| 122 | { |
| 123 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 124 | if (af == 0) return PERMISSION_DENIED; |
| 125 | af->setMasterMute(mute); |
| 126 | return NO_ERROR; |
| 127 | } |
| 128 | |
| 129 | status_t AudioSystem::getMasterVolume(float* volume) |
| 130 | { |
| 131 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 132 | if (af == 0) return PERMISSION_DENIED; |
| 133 | *volume = af->masterVolume(); |
| 134 | return NO_ERROR; |
| 135 | } |
| 136 | |
| 137 | status_t AudioSystem::getMasterMute(bool* mute) |
| 138 | { |
| 139 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 140 | if (af == 0) return PERMISSION_DENIED; |
| 141 | *mute = af->masterMute(); |
| 142 | return NO_ERROR; |
| 143 | } |
| 144 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 145 | status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value, |
| 146 | audio_io_handle_t output) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 148 | 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] | 149 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 150 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 151 | af->setStreamVolume(stream, value, output); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | return NO_ERROR; |
| 153 | } |
| 154 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 155 | 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] | 156 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 157 | 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] | 158 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 159 | if (af == 0) return PERMISSION_DENIED; |
| 160 | af->setStreamMute(stream, mute); |
| 161 | return NO_ERROR; |
| 162 | } |
| 163 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 164 | status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume, |
| 165 | audio_io_handle_t output) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 167 | 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] | 168 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 169 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 170 | *volume = af->streamVolume(stream, output); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | return NO_ERROR; |
| 172 | } |
| 173 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 174 | 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] | 175 | { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 176 | 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] | 177 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 178 | if (af == 0) return PERMISSION_DENIED; |
| 179 | *mute = af->streamMute(stream); |
| 180 | return NO_ERROR; |
| 181 | } |
| 182 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 183 | status_t AudioSystem::setMode(audio_mode_t mode) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | { |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 185 | 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] | 186 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 187 | if (af == 0) return PERMISSION_DENIED; |
| 188 | return af->setMode(mode); |
| 189 | } |
| 190 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 191 | status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) |
| 192 | { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 194 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 195 | return af->setParameters(ioHandle, keyValuePairs); |
| 196 | } |
| 197 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 198 | String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) |
| 199 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 200 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 201 | String8 result = String8(""); |
| 202 | if (af == 0) return result; |
| 203 | |
| 204 | result = af->getParameters(ioHandle, keys); |
| 205 | return result; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 208 | status_t AudioSystem::setParameters(const String8& keyValuePairs) |
| 209 | { |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 210 | return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs); |
Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | String8 AudioSystem::getParameters(const String8& keys) |
| 214 | { |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 215 | return getParameters(AUDIO_IO_HANDLE_NONE, keys); |
Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 216 | } |
| 217 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | // convert volume steps to natural log scale |
| 219 | |
| 220 | // change this value to change volume scaling |
| 221 | static const float dBPerStep = 0.5f; |
| 222 | // shouldn't need to touch these |
| 223 | static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f; |
| 224 | static const float dBConvertInverse = 1.0f / dBConvert; |
| 225 | |
| 226 | float AudioSystem::linearToLog(int volume) |
| 227 | { |
| 228 | // float v = volume ? exp(float(100 - volume) * dBConvert) : 0; |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 229 | // ALOGD("linearToLog(%d)=%f", volume, v); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | // return v; |
| 231 | return volume ? exp(float(100 - volume) * dBConvert) : 0; |
| 232 | } |
| 233 | |
| 234 | int AudioSystem::logToLinear(float volume) |
| 235 | { |
| 236 | // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 237 | // ALOGD("logTolinear(%d)=%f", v, volume); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | // return v; |
| 239 | return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; |
| 240 | } |
| 241 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 242 | 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] | 243 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 244 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 246 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 247 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 250 | output = getOutput(streamType); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 251 | if (output == 0) { |
| 252 | return PERMISSION_DENIED; |
| 253 | } |
| 254 | |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 255 | return getSamplingRate(output, samplingRate); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | status_t AudioSystem::getSamplingRate(audio_io_handle_t output, |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 259 | uint32_t* samplingRate) |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 260 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 261 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 262 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 263 | |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 264 | Mutex::Autolock _l(gLockCache); |
| 265 | |
| 266 | OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 267 | if (outputDesc == NULL) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 268 | ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output); |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 269 | gLockCache.unlock(); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 270 | *samplingRate = af->sampleRate(output); |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 271 | gLockCache.lock(); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 272 | } else { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 273 | ALOGV("getOutputSamplingRate() reading from output desc"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 274 | *samplingRate = outputDesc->samplingRate; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 275 | } |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 276 | if (*samplingRate == 0) { |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 277 | ALOGE("AudioSystem::getSamplingRate failed for output %d", output); |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 278 | return BAD_VALUE; |
| 279 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 280 | |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 281 | ALOGV("getSamplingRate() output %d, sampling rate %u", output, *samplingRate); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 282 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | return NO_ERROR; |
| 284 | } |
| 285 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 286 | 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] | 287 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 288 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 290 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 291 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 292 | } |
Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 293 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 294 | output = getOutput(streamType); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 295 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 296 | return PERMISSION_DENIED; |
| 297 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 299 | return getFrameCount(output, frameCount); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | status_t AudioSystem::getFrameCount(audio_io_handle_t output, |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 303 | size_t* frameCount) |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 304 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 305 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 306 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 307 | |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 308 | Mutex::Autolock _l(gLockCache); |
| 309 | |
| 310 | OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 311 | if (outputDesc == NULL) { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 312 | gLockCache.unlock(); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 313 | *frameCount = af->frameCount(output); |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 314 | gLockCache.lock(); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 315 | } else { |
| 316 | *frameCount = outputDesc->frameCount; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 317 | } |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 318 | if (*frameCount == 0) { |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 319 | ALOGE("AudioSystem::getFrameCount failed for output %d", output); |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 320 | return BAD_VALUE; |
| 321 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 322 | |
Mark Salyzyn | 34fb296 | 2014-06-18 16:30:56 -0700 | [diff] [blame] | 323 | ALOGV("getFrameCount() output %d, frameCount %zu", output, *frameCount); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 324 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | return NO_ERROR; |
| 326 | } |
| 327 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 328 | 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] | 329 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 330 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 332 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 333 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 334 | } |
Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 335 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 336 | output = getOutput(streamType); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 337 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 338 | return PERMISSION_DENIED; |
| 339 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | |
Glenn Kasten | 241618f | 2014-03-25 17:48:57 -0700 | [diff] [blame] | 341 | return getLatency(output, latency); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | status_t AudioSystem::getLatency(audio_io_handle_t output, |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 345 | uint32_t* latency) |
| 346 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 347 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 348 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 349 | |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 350 | Mutex::Autolock _l(gLockCache); |
| 351 | |
| 352 | OutputDescriptor *outputDesc = AudioSystem::gOutputs.valueFor(output); |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 353 | if (outputDesc == NULL) { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 354 | gLockCache.unlock(); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 355 | *latency = af->latency(output); |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 356 | gLockCache.lock(); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 357 | } else { |
| 358 | *latency = outputDesc->latency; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Glenn Kasten | 241618f | 2014-03-25 17:48:57 -0700 | [diff] [blame] | 361 | ALOGV("getLatency() output %d, latency %d", output, *latency); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 362 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | return NO_ERROR; |
| 364 | } |
| 365 | |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 366 | status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format, |
| 367 | audio_channel_mask_t channelMask, size_t* buffSize) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 369 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 370 | if (af == 0) { |
| 371 | return PERMISSION_DENIED; |
| 372 | } |
| 373 | Mutex::Autolock _l(gLockCache); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | // 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] | 375 | size_t inBuffSize = gInBuffSize; |
| 376 | if ((inBuffSize == 0) || (sampleRate != gPrevInSamplingRate) || (format != gPrevInFormat) |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 377 | || (channelMask != gPrevInChannelMask)) { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 378 | gLockCache.unlock(); |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 379 | inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask); |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 380 | gLockCache.lock(); |
Glenn Kasten | 5446e54 | 2014-01-08 08:58:53 -0800 | [diff] [blame] | 381 | if (inBuffSize == 0) { |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 382 | ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x", |
Glenn Kasten | 5446e54 | 2014-01-08 08:58:53 -0800 | [diff] [blame] | 383 | sampleRate, format, channelMask); |
| 384 | return BAD_VALUE; |
| 385 | } |
| 386 | // A benign race is possible here: we could overwrite a fresher cache entry |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | // save the request params |
| 388 | gPrevInSamplingRate = sampleRate; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 389 | gPrevInFormat = format; |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 390 | gPrevInChannelMask = channelMask; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 392 | gInBuffSize = inBuffSize; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 393 | } |
Glenn Kasten | f8c1a6f | 2012-01-10 09:01:19 -0800 | [diff] [blame] | 394 | *buffSize = inBuffSize; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 395 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | return NO_ERROR; |
| 397 | } |
| 398 | |
Eric Laurent | f0ee6f4 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 399 | status_t AudioSystem::setVoiceVolume(float value) |
| 400 | { |
| 401 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 402 | if (af == 0) return PERMISSION_DENIED; |
| 403 | return af->setVoiceVolume(value); |
| 404 | } |
| 405 | |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 406 | status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames, |
Glenn Kasten | 0ed1959 | 2014-03-26 07:50:05 -0700 | [diff] [blame] | 407 | uint32_t *dspFrames) |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 408 | { |
| 409 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 410 | if (af == 0) return PERMISSION_DENIED; |
| 411 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 412 | return af->getRenderPosition(halFrames, dspFrames, output); |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 415 | uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) |
| 416 | { |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 417 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 418 | uint32_t result = 0; |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 419 | if (af == 0) return result; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 420 | if (ioHandle == AUDIO_IO_HANDLE_NONE) return result; |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 421 | |
| 422 | result = af->getInputFramesLost(ioHandle); |
| 423 | return result; |
| 424 | } |
| 425 | |
Eric Laurent | de3f839 | 2014-07-27 18:38:22 -0700 | [diff] [blame] | 426 | audio_unique_id_t AudioSystem::newAudioUniqueId() |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 427 | { |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 428 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
Eric Laurent | de3f839 | 2014-07-27 18:38:22 -0700 | [diff] [blame] | 429 | if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE; |
| 430 | return af->newAudioUniqueId(); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 433 | void AudioSystem::acquireAudioSessionId(int audioSession, pid_t pid) |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 434 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 435 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 436 | if (af != 0) { |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 437 | af->acquireAudioSessionId(audioSession, pid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 441 | void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid) |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 442 | { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 443 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 444 | if (af != 0) { |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 445 | af->releaseAudioSessionId(audioSession, pid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 449 | audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) |
| 450 | { |
| 451 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 452 | if (af == 0) return AUDIO_HW_SYNC_INVALID; |
| 453 | return af->getAudioHwSyncForSession(sessionId); |
| 454 | } |
| 455 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | // --------------------------------------------------------------------------- |
| 457 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 458 | void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) |
| 459 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 460 | audio_error_callback cb = NULL; |
| 461 | { |
| 462 | Mutex::Autolock _l(AudioSystem::gLock); |
| 463 | AudioSystem::gAudioFlinger.clear(); |
| 464 | cb = gAudioErrorCallback; |
| 465 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 467 | { |
| 468 | // clear output handles and stream to output map caches |
| 469 | Mutex::Autolock _l(gLockCache); |
| 470 | AudioSystem::gOutputs.clear(); |
| 471 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 473 | if (cb) { |
| 474 | cb(DEAD_OBJECT); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 476 | ALOGW("AudioFlinger server died!"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 479 | void AudioSystem::AudioFlingerClient::ioConfigChanged(int event, audio_io_handle_t ioHandle, |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 480 | const void *param2) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 481 | ALOGV("ioConfigChanged() event %d", event); |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 482 | const OutputDescriptor *desc; |
Glenn Kasten | 211eeaf | 2012-01-20 09:37:45 -0800 | [diff] [blame] | 483 | audio_stream_type_t stream; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 484 | |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 485 | if (ioHandle == AUDIO_IO_HANDLE_NONE) return; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 486 | |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 487 | Mutex::Autolock _l(AudioSystem::gLockCache); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 488 | |
| 489 | switch (event) { |
| 490 | case STREAM_CONFIG_CHANGED: |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 491 | break; |
| 492 | case OUTPUT_OPENED: { |
| 493 | if (gOutputs.indexOfKey(ioHandle) >= 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 494 | ALOGV("ioConfigChanged() opening already existing output! %d", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 495 | break; |
| 496 | } |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 497 | if (param2 == NULL) break; |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 498 | desc = (const OutputDescriptor *)param2; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 499 | |
| 500 | OutputDescriptor *outputDesc = new OutputDescriptor(*desc); |
| 501 | gOutputs.add(ioHandle, outputDesc); |
Mark Salyzyn | 34fb296 | 2014-06-18 16:30:56 -0700 | [diff] [blame] | 502 | ALOGV("ioConfigChanged() new output samplingRate %u, format %#x channel mask %#x frameCount %zu " |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 503 | "latency %d", |
Glenn Kasten | fad226a | 2013-07-16 17:19:58 -0700 | [diff] [blame] | 504 | outputDesc->samplingRate, outputDesc->format, outputDesc->channelMask, |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 505 | outputDesc->frameCount, outputDesc->latency); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 506 | } break; |
| 507 | case OUTPUT_CLOSED: { |
| 508 | if (gOutputs.indexOfKey(ioHandle) < 0) { |
Glenn Kasten | 85007a9 | 2012-11-13 15:06:37 -0800 | [diff] [blame] | 509 | ALOGW("ioConfigChanged() closing unknown output! %d", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 510 | break; |
| 511 | } |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 512 | ALOGV("ioConfigChanged() output %d closed", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 513 | |
| 514 | gOutputs.removeItem(ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 515 | } break; |
| 516 | |
| 517 | case OUTPUT_CONFIG_CHANGED: { |
| 518 | int index = gOutputs.indexOfKey(ioHandle); |
| 519 | if (index < 0) { |
Glenn Kasten | 85007a9 | 2012-11-13 15:06:37 -0800 | [diff] [blame] | 520 | ALOGW("ioConfigChanged() modifying unknown output! %d", ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 521 | break; |
| 522 | } |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 523 | if (param2 == NULL) break; |
Glenn Kasten | b81cc8c | 2012-03-01 09:14:51 -0800 | [diff] [blame] | 524 | desc = (const OutputDescriptor *)param2; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 525 | |
Glenn Kasten | cac3daa | 2014-02-07 09:47:14 -0800 | [diff] [blame] | 526 | ALOGV("ioConfigChanged() new config for output %d samplingRate %u, format %#x channel mask %#x " |
Mark Salyzyn | 34fb296 | 2014-06-18 16:30:56 -0700 | [diff] [blame] | 527 | "frameCount %zu latency %d", |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 528 | ioHandle, desc->samplingRate, desc->format, |
Glenn Kasten | fad226a | 2013-07-16 17:19:58 -0700 | [diff] [blame] | 529 | desc->channelMask, desc->frameCount, desc->latency); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 530 | OutputDescriptor *outputDesc = gOutputs.valueAt(index); |
| 531 | delete outputDesc; |
| 532 | outputDesc = new OutputDescriptor(*desc); |
| 533 | gOutputs.replaceValueFor(ioHandle, outputDesc); |
| 534 | } break; |
| 535 | case INPUT_OPENED: |
| 536 | case INPUT_CLOSED: |
| 537 | case INPUT_CONFIG_CHANGED: |
| 538 | break; |
| 539 | |
| 540 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 543 | void AudioSystem::setErrorCallback(audio_error_callback cb) |
| 544 | { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 545 | Mutex::Autolock _l(gLock); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | gAudioErrorCallback = cb; |
| 547 | } |
| 548 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 549 | // client singleton for AudioPolicyService binder interface |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 550 | // protected by gLockAPS |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 551 | sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; |
| 552 | sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; |
| 553 | |
| 554 | |
Glenn Kasten | 18a6d90 | 2012-09-24 11:27:56 -0700 | [diff] [blame] | 555 | // establish binder interface to AudioPolicy service |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 556 | const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 557 | { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 558 | sp<IAudioPolicyService> ap; |
| 559 | sp<AudioPolicyServiceClient> apc; |
| 560 | { |
| 561 | Mutex::Autolock _l(gLockAPS); |
| 562 | if (gAudioPolicyService == 0) { |
| 563 | sp<IServiceManager> sm = defaultServiceManager(); |
| 564 | sp<IBinder> binder; |
| 565 | do { |
| 566 | binder = sm->getService(String16("media.audio_policy")); |
| 567 | if (binder != 0) |
| 568 | break; |
| 569 | ALOGW("AudioPolicyService not published, waiting..."); |
| 570 | usleep(500000); // 0.5 s |
| 571 | } while (true); |
| 572 | if (gAudioPolicyServiceClient == NULL) { |
| 573 | gAudioPolicyServiceClient = new AudioPolicyServiceClient(); |
| 574 | } |
| 575 | binder->linkToDeath(gAudioPolicyServiceClient); |
| 576 | gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); |
| 577 | LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0); |
| 578 | apc = gAudioPolicyServiceClient; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 579 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 580 | ap = gAudioPolicyService; |
| 581 | } |
| 582 | if (apc != 0) { |
| 583 | ap->registerClient(apc); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 584 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 585 | |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 586 | return ap; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 589 | // --------------------------------------------------------------------------- |
| 590 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 591 | status_t AudioSystem::setDeviceConnectionState(audio_devices_t device, |
| 592 | audio_policy_dev_state_t state, |
| 593 | const char *device_address) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 594 | { |
| 595 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 596 | const char *address = ""; |
| 597 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 598 | if (aps == 0) return PERMISSION_DENIED; |
| 599 | |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 600 | if (device_address != NULL) { |
| 601 | address = device_address; |
| 602 | } |
| 603 | |
| 604 | return aps->setDeviceConnectionState(device, state, address); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 607 | audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 608 | const char *device_address) |
| 609 | { |
| 610 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 611 | if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 612 | |
| 613 | return aps->getDeviceConnectionState(device, device_address); |
| 614 | } |
| 615 | |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 616 | status_t AudioSystem::setPhoneState(audio_mode_t state) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 617 | { |
Glenn Kasten | 347966c | 2012-01-18 14:58:32 -0800 | [diff] [blame] | 618 | if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 619 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 620 | if (aps == 0) return PERMISSION_DENIED; |
| 621 | |
| 622 | return aps->setPhoneState(state); |
| 623 | } |
| 624 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 625 | 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] | 626 | { |
| 627 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 628 | if (aps == 0) return PERMISSION_DENIED; |
| 629 | return aps->setForceUse(usage, config); |
| 630 | } |
| 631 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 632 | 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] | 633 | { |
| 634 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 635 | if (aps == 0) return AUDIO_POLICY_FORCE_NONE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 636 | return aps->getForceUse(usage); |
| 637 | } |
| 638 | |
| 639 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 640 | audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 641 | uint32_t samplingRate, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 642 | audio_format_t format, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 643 | audio_channel_mask_t channelMask, |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 644 | audio_output_flags_t flags, |
| 645 | const audio_offload_info_t *offloadInfo) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 646 | { |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 647 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 648 | if (aps == 0) return 0; |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 649 | return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 652 | status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr, |
| 653 | audio_io_handle_t *output, |
| 654 | audio_session_t session, |
| 655 | audio_stream_type_t *stream, |
| 656 | uint32_t samplingRate, |
| 657 | audio_format_t format, |
| 658 | audio_channel_mask_t channelMask, |
| 659 | audio_output_flags_t flags, |
| 660 | const audio_offload_info_t *offloadInfo) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 661 | { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 662 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 663 | if (aps == 0) return NO_INIT; |
| 664 | return aps->getOutputForAttr(attr, output, session, stream, |
| 665 | samplingRate, format, channelMask, |
| 666 | flags, offloadInfo); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 669 | status_t AudioSystem::startOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 670 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 671 | audio_session_t session) |
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 | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 675 | return aps->startOutput(output, stream, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 678 | status_t AudioSystem::stopOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 679 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 680 | audio_session_t session) |
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 | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 684 | return aps->stopOutput(output, stream, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 687 | void AudioSystem::releaseOutput(audio_io_handle_t output, |
| 688 | audio_stream_type_t stream, |
| 689 | audio_session_t session) |
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; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 693 | aps->releaseOutput(output, stream, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 696 | status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr, |
| 697 | audio_io_handle_t *input, |
| 698 | audio_session_t session, |
| 699 | uint32_t samplingRate, |
| 700 | audio_format_t format, |
| 701 | audio_channel_mask_t channelMask, |
| 702 | audio_input_flags_t flags) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 703 | { |
| 704 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 705 | if (aps == 0) return NO_INIT; |
| 706 | return aps->getInputForAttr(attr, input, session, samplingRate, format, channelMask, flags); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 709 | status_t AudioSystem::startInput(audio_io_handle_t input, |
| 710 | audio_session_t session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 711 | { |
| 712 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 713 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 714 | return aps->startInput(input, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 717 | status_t AudioSystem::stopInput(audio_io_handle_t input, |
| 718 | audio_session_t session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 719 | { |
| 720 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 721 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 722 | return aps->stopInput(input, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 725 | void AudioSystem::releaseInput(audio_io_handle_t input, |
| 726 | audio_session_t session) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 727 | { |
| 728 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 729 | if (aps == 0) return; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 730 | aps->releaseInput(input, session); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 733 | status_t AudioSystem::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 734 | int indexMin, |
| 735 | int indexMax) |
| 736 | { |
| 737 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 738 | if (aps == 0) return PERMISSION_DENIED; |
| 739 | return aps->initStreamVolume(stream, indexMin, indexMax); |
| 740 | } |
| 741 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 742 | status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, |
| 743 | int index, |
| 744 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 745 | { |
| 746 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 747 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 748 | return aps->setStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 751 | status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, |
| 752 | int *index, |
| 753 | audio_devices_t device) |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 754 | { |
| 755 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 756 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 757 | return aps->getStreamVolumeIndex(stream, index, device); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 760 | uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 761 | { |
| 762 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 763 | if (aps == 0) return 0; |
| 764 | return aps->getStrategyForStream(stream); |
| 765 | } |
| 766 | |
Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 767 | audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream) |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 768 | { |
| 769 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Glenn Kasten | 45faf7e | 2014-01-17 10:23:01 -0800 | [diff] [blame] | 770 | if (aps == 0) return AUDIO_DEVICE_NONE; |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 771 | return aps->getDevicesForStream(stream); |
| 772 | } |
| 773 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 774 | audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 775 | { |
| 776 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Glenn Kasten | efa6ea9 | 2014-01-08 09:10:43 -0800 | [diff] [blame] | 777 | // FIXME change return type to status_t, and return PERMISSION_DENIED here |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 778 | if (aps == 0) return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 779 | return aps->getOutputForEffect(desc); |
| 780 | } |
| 781 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 782 | status_t AudioSystem::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 783 | audio_io_handle_t io, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 784 | uint32_t strategy, |
| 785 | int session, |
| 786 | int id) |
| 787 | { |
| 788 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 789 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 790 | return aps->registerEffect(desc, io, strategy, session, id); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | status_t AudioSystem::unregisterEffect(int id) |
| 794 | { |
| 795 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 796 | if (aps == 0) return PERMISSION_DENIED; |
| 797 | return aps->unregisterEffect(id); |
| 798 | } |
| 799 | |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 800 | status_t AudioSystem::setEffectEnabled(int id, bool enabled) |
| 801 | { |
| 802 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 803 | if (aps == 0) return PERMISSION_DENIED; |
| 804 | return aps->setEffectEnabled(id, enabled); |
| 805 | } |
| 806 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 807 | 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] | 808 | { |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 809 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 810 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 811 | if (state == NULL) return BAD_VALUE; |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 812 | *state = aps->isStreamActive(stream, inPastMs); |
| 813 | return NO_ERROR; |
| 814 | } |
| 815 | |
Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 816 | status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state, |
| 817 | uint32_t inPastMs) |
| 818 | { |
| 819 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 820 | if (aps == 0) return PERMISSION_DENIED; |
| 821 | if (state == NULL) return BAD_VALUE; |
| 822 | *state = aps->isStreamActiveRemotely(stream, inPastMs); |
| 823 | return NO_ERROR; |
| 824 | } |
| 825 | |
Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 826 | status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) |
| 827 | { |
| 828 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 829 | if (aps == 0) return PERMISSION_DENIED; |
| 830 | if (state == NULL) return BAD_VALUE; |
| 831 | *state = aps->isSourceActive(stream); |
| 832 | return NO_ERROR; |
| 833 | } |
| 834 | |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 835 | uint32_t AudioSystem::getPrimaryOutputSamplingRate() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 836 | { |
| 837 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 838 | if (af == 0) return 0; |
| 839 | return af->getPrimaryOutputSamplingRate(); |
| 840 | } |
| 841 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 842 | size_t AudioSystem::getPrimaryOutputFrameCount() |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 843 | { |
| 844 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 845 | if (af == 0) return 0; |
| 846 | return af->getPrimaryOutputFrameCount(); |
| 847 | } |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 848 | |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 849 | status_t AudioSystem::setLowRamDevice(bool isLowRamDevice) |
| 850 | { |
| 851 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 852 | if (af == 0) return PERMISSION_DENIED; |
| 853 | return af->setLowRamDevice(isLowRamDevice); |
| 854 | } |
| 855 | |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 856 | void AudioSystem::clearAudioConfigCache() |
| 857 | { |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 858 | // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 859 | ALOGV("clearAudioConfigCache()"); |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 860 | { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 861 | Mutex::Autolock _l(gLockCache); |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 862 | gOutputs.clear(); |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 863 | } |
| 864 | { |
| 865 | Mutex::Autolock _l(gLock); |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 866 | gAudioFlinger.clear(); |
| 867 | } |
| 868 | { |
| 869 | Mutex::Autolock _l(gLockAPS); |
| 870 | gAudioPolicyService.clear(); |
| 871 | } |
| 872 | // Do not clear gAudioPortCallback |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 875 | bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info) |
| 876 | { |
| 877 | ALOGV("isOffloadSupported()"); |
| 878 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 879 | if (aps == 0) return false; |
| 880 | return aps->isOffloadSupported(info); |
| 881 | } |
| 882 | |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 883 | status_t AudioSystem::listAudioPorts(audio_port_role_t role, |
| 884 | audio_port_type_t type, |
| 885 | unsigned int *num_ports, |
| 886 | struct audio_port *ports, |
| 887 | unsigned int *generation) |
| 888 | { |
| 889 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 890 | if (aps == 0) return PERMISSION_DENIED; |
| 891 | return aps->listAudioPorts(role, type, num_ports, ports, generation); |
| 892 | } |
| 893 | |
| 894 | status_t AudioSystem::getAudioPort(struct audio_port *port) |
| 895 | { |
| 896 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 897 | if (aps == 0) return PERMISSION_DENIED; |
| 898 | return aps->getAudioPort(port); |
| 899 | } |
| 900 | |
| 901 | status_t AudioSystem::createAudioPatch(const struct audio_patch *patch, |
| 902 | audio_patch_handle_t *handle) |
| 903 | { |
| 904 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 905 | if (aps == 0) return PERMISSION_DENIED; |
| 906 | return aps->createAudioPatch(patch, handle); |
| 907 | } |
| 908 | |
| 909 | status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) |
| 910 | { |
| 911 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 912 | if (aps == 0) return PERMISSION_DENIED; |
| 913 | return aps->releaseAudioPatch(handle); |
| 914 | } |
| 915 | |
| 916 | status_t AudioSystem::listAudioPatches(unsigned int *num_patches, |
| 917 | struct audio_patch *patches, |
| 918 | unsigned int *generation) |
| 919 | { |
| 920 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 921 | if (aps == 0) return PERMISSION_DENIED; |
| 922 | return aps->listAudioPatches(num_patches, patches, generation); |
| 923 | } |
| 924 | |
| 925 | status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config) |
| 926 | { |
| 927 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 928 | if (aps == 0) return PERMISSION_DENIED; |
| 929 | return aps->setAudioPortConfig(config); |
| 930 | } |
| 931 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 932 | void AudioSystem::setAudioPortCallback(sp<AudioPortCallback> callBack) |
| 933 | { |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 934 | Mutex::Autolock _l(gLockAPC); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 935 | gAudioPortCallback = callBack; |
| 936 | } |
| 937 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 938 | status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session, |
| 939 | audio_io_handle_t *ioHandle, |
| 940 | audio_devices_t *device) |
| 941 | { |
| 942 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 943 | if (aps == 0) return PERMISSION_DENIED; |
| 944 | return aps->acquireSoundTriggerSession(session, ioHandle, device); |
| 945 | } |
| 946 | |
| 947 | status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) |
| 948 | { |
| 949 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 950 | if (aps == 0) return PERMISSION_DENIED; |
| 951 | return aps->releaseSoundTriggerSession(session); |
| 952 | } |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 953 | |
| 954 | audio_mode_t AudioSystem::getPhoneState() |
| 955 | { |
| 956 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 957 | if (aps == 0) return AUDIO_MODE_INVALID; |
| 958 | return aps->getPhoneState(); |
| 959 | } |
| 960 | |
Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame^] | 961 | status_t AudioSystem::registerPolicyMixes(Vector<AudioMix> mixes, bool registration) |
| 962 | { |
| 963 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 964 | if (aps == 0) return PERMISSION_DENIED; |
| 965 | return aps->registerPolicyMixes(mixes, registration); |
| 966 | } |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 967 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 968 | // --------------------------------------------------------------------------- |
| 969 | |
Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 970 | void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) |
| 971 | { |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 972 | { |
| 973 | Mutex::Autolock _l(gLockAPC); |
| 974 | if (gAudioPortCallback != 0) { |
| 975 | gAudioPortCallback->onServiceDied(); |
| 976 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 977 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 978 | { |
| 979 | Mutex::Autolock _l(gLockAPS); |
| 980 | AudioSystem::gAudioPolicyService.clear(); |
| 981 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 982 | |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 983 | ALOGW("AudioPolicyService server died!"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 986 | void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() |
| 987 | { |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 988 | Mutex::Autolock _l(gLockAPC); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 989 | if (gAudioPortCallback != 0) { |
| 990 | gAudioPortCallback->onAudioPortListUpdate(); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() |
| 995 | { |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 996 | Mutex::Autolock _l(gLockAPC); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 997 | if (gAudioPortCallback != 0) { |
| 998 | gAudioPortCallback->onAudioPatchListUpdate(); |
| 999 | } |
| 1000 | } |
| 1001 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | }; // namespace android |