| 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; | 
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 35 | Mutex AudioSystem::gLockAPS; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | sp<IAudioFlinger> AudioSystem::gAudioFlinger; | 
|  | 37 | sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient; | 
|  | 38 | audio_error_callback AudioSystem::gAudioErrorCallback = NULL; | 
| Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 39 | dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL; | 
| Glenn Kasten | 211eeaf | 2012-01-20 09:37:45 -0800 | [diff] [blame] | 40 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | // establish binder interface to AudioFlinger service | 
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 43 | const sp<IAudioFlinger> AudioSystem::get_audio_flinger() | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | { | 
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 45 | sp<IAudioFlinger> af; | 
|  | 46 | sp<AudioFlingerClient> afc; | 
|  | 47 | { | 
|  | 48 | Mutex::Autolock _l(gLock); | 
|  | 49 | if (gAudioFlinger == 0) { | 
|  | 50 | sp<IServiceManager> sm = defaultServiceManager(); | 
|  | 51 | sp<IBinder> binder; | 
|  | 52 | do { | 
|  | 53 | binder = sm->getService(String16("media.audio_flinger")); | 
|  | 54 | if (binder != 0) | 
|  | 55 | break; | 
|  | 56 | ALOGW("AudioFlinger not published, waiting..."); | 
|  | 57 | usleep(500000); // 0.5 s | 
|  | 58 | } while (true); | 
|  | 59 | if (gAudioFlingerClient == NULL) { | 
|  | 60 | gAudioFlingerClient = new AudioFlingerClient(); | 
|  | 61 | } else { | 
|  | 62 | if (gAudioErrorCallback) { | 
|  | 63 | gAudioErrorCallback(NO_ERROR); | 
|  | 64 | } | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | } | 
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 66 | binder->linkToDeath(gAudioFlingerClient); | 
|  | 67 | gAudioFlinger = interface_cast<IAudioFlinger>(binder); | 
|  | 68 | LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0); | 
|  | 69 | afc = gAudioFlingerClient; | 
| Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 70 | } | 
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 71 | af = gAudioFlinger; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } | 
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 73 | if (afc != 0) { | 
|  | 74 | af->registerClient(afc); | 
|  | 75 | } | 
|  | 76 | return af; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 79 | const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient() | 
|  | 80 | { | 
|  | 81 | // calling get_audio_flinger() will initialize gAudioFlingerClient if needed | 
|  | 82 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 83 | if (af == 0) return 0; | 
|  | 84 | Mutex::Autolock _l(gLock); | 
|  | 85 | return gAudioFlingerClient; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle) | 
|  | 89 | { | 
|  | 90 | sp<AudioIoDescriptor> desc; | 
|  | 91 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); | 
|  | 92 | if (afc != 0) { | 
|  | 93 | desc = afc->getIoDescriptor(ioHandle); | 
|  | 94 | } | 
|  | 95 | return desc; | 
|  | 96 | } | 
|  | 97 |  | 
| Eric Laurent | 4629161 | 2013-07-18 14:38:44 -0700 | [diff] [blame] | 98 | /* static */ status_t AudioSystem::checkAudioFlinger() | 
|  | 99 | { | 
|  | 100 | if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) { | 
|  | 101 | return NO_ERROR; | 
|  | 102 | } | 
|  | 103 | return DEAD_OBJECT; | 
|  | 104 | } | 
|  | 105 |  | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 106 | status_t AudioSystem::muteMicrophone(bool state) | 
|  | 107 | { | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 109 | if (af == 0) return PERMISSION_DENIED; | 
|  | 110 | return af->setMicMute(state); | 
|  | 111 | } | 
|  | 112 |  | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 113 | status_t AudioSystem::isMicrophoneMuted(bool* state) | 
|  | 114 | { | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 116 | if (af == 0) return PERMISSION_DENIED; | 
|  | 117 | *state = af->getMicMute(); | 
|  | 118 | return NO_ERROR; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | status_t AudioSystem::setMasterVolume(float value) | 
|  | 122 | { | 
|  | 123 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 124 | if (af == 0) return PERMISSION_DENIED; | 
|  | 125 | af->setMasterVolume(value); | 
|  | 126 | return NO_ERROR; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | status_t AudioSystem::setMasterMute(bool mute) | 
|  | 130 | { | 
|  | 131 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 132 | if (af == 0) return PERMISSION_DENIED; | 
|  | 133 | af->setMasterMute(mute); | 
|  | 134 | return NO_ERROR; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | status_t AudioSystem::getMasterVolume(float* volume) | 
|  | 138 | { | 
|  | 139 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 140 | if (af == 0) return PERMISSION_DENIED; | 
|  | 141 | *volume = af->masterVolume(); | 
|  | 142 | return NO_ERROR; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | status_t AudioSystem::getMasterMute(bool* mute) | 
|  | 146 | { | 
|  | 147 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 148 | if (af == 0) return PERMISSION_DENIED; | 
|  | 149 | *mute = af->masterMute(); | 
|  | 150 | return NO_ERROR; | 
|  | 151 | } | 
|  | 152 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 153 | status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value, | 
|  | 154 | audio_io_handle_t output) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 156 | 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] | 157 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 158 | if (af == 0) return PERMISSION_DENIED; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 159 | af->setStreamVolume(stream, value, output); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | return NO_ERROR; | 
|  | 161 | } | 
|  | 162 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 163 | 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] | 164 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 165 | 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] | 166 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 167 | if (af == 0) return PERMISSION_DENIED; | 
|  | 168 | af->setStreamMute(stream, mute); | 
|  | 169 | return NO_ERROR; | 
|  | 170 | } | 
|  | 171 |  | 
| Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 172 | status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume, | 
|  | 173 | audio_io_handle_t output) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 175 | 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] | 176 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 177 | if (af == 0) return PERMISSION_DENIED; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 178 | *volume = af->streamVolume(stream, output); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | return NO_ERROR; | 
|  | 180 | } | 
|  | 181 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 182 | 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] | 183 | { | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 184 | 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] | 185 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 186 | if (af == 0) return PERMISSION_DENIED; | 
|  | 187 | *mute = af->streamMute(stream); | 
|  | 188 | return NO_ERROR; | 
|  | 189 | } | 
|  | 190 |  | 
| Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 191 | status_t AudioSystem::setMode(audio_mode_t mode) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | { | 
| Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 193 | 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] | 194 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 195 | if (af == 0) return PERMISSION_DENIED; | 
|  | 196 | return af->setMode(mode); | 
|  | 197 | } | 
|  | 198 |  | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 199 | status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) | 
|  | 200 | { | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 202 | if (af == 0) return PERMISSION_DENIED; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 203 | return af->setParameters(ioHandle, keyValuePairs); | 
|  | 204 | } | 
|  | 205 |  | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 206 | String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) | 
|  | 207 | { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 208 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 209 | String8 result = String8(""); | 
|  | 210 | if (af == 0) return result; | 
|  | 211 |  | 
|  | 212 | result = af->getParameters(ioHandle, keys); | 
|  | 213 | return result; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | } | 
|  | 215 |  | 
| Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 216 | status_t AudioSystem::setParameters(const String8& keyValuePairs) | 
|  | 217 | { | 
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 218 | return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs); | 
| Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 219 | } | 
|  | 220 |  | 
|  | 221 | String8 AudioSystem::getParameters(const String8& keys) | 
|  | 222 | { | 
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 223 | return getParameters(AUDIO_IO_HANDLE_NONE, keys); | 
| Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 224 | } | 
|  | 225 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | // convert volume steps to natural log scale | 
|  | 227 |  | 
|  | 228 | // change this value to change volume scaling | 
|  | 229 | static const float dBPerStep = 0.5f; | 
|  | 230 | // shouldn't need to touch these | 
|  | 231 | static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f; | 
|  | 232 | static const float dBConvertInverse = 1.0f / dBConvert; | 
|  | 233 |  | 
|  | 234 | float AudioSystem::linearToLog(int volume) | 
|  | 235 | { | 
|  | 236 | // float v = volume ? exp(float(100 - volume) * dBConvert) : 0; | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 237 | // ALOGD("linearToLog(%d)=%f", volume, v); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | // return v; | 
|  | 239 | return volume ? exp(float(100 - volume) * dBConvert) : 0; | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | int AudioSystem::logToLinear(float volume) | 
|  | 243 | { | 
|  | 244 | // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; | 
| Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 245 | // ALOGD("logTolinear(%d)=%f", v, volume); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | // return v; | 
|  | 247 | return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; | 
|  | 248 | } | 
|  | 249 |  | 
| Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 250 | 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] | 251 | { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 252 | audio_io_handle_t output; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 254 | if (streamType == AUDIO_STREAM_DEFAULT) { | 
|  | 255 | streamType = AUDIO_STREAM_MUSIC; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 256 | } | 
|  | 257 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 258 | output = getOutput(streamType); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 259 | if (output == 0) { | 
|  | 260 | return PERMISSION_DENIED; | 
|  | 261 | } | 
|  | 262 |  | 
| Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 263 | return getSamplingRate(output, samplingRate); | 
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 264 | } | 
|  | 265 |  | 
|  | 266 | status_t AudioSystem::getSamplingRate(audio_io_handle_t output, | 
| Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 267 | uint32_t* samplingRate) | 
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 268 | { | 
| Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 269 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 270 | if (af == 0) return PERMISSION_DENIED; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 271 | sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 272 | if (outputDesc == 0) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 273 | ALOGV("getOutputSamplingRate() no output descriptor for output %d in gOutputs", output); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 274 | *samplingRate = af->sampleRate(output); | 
|  | 275 | } else { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 276 | ALOGV("getOutputSamplingRate() reading from output desc"); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 277 | *samplingRate = outputDesc->mSamplingRate; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 278 | } | 
| Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 279 | if (*samplingRate == 0) { | 
| Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 280 | ALOGE("AudioSystem::getSamplingRate failed for output %d", output); | 
| Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 281 | return BAD_VALUE; | 
|  | 282 | } | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 283 |  | 
| Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 284 | ALOGV("getSamplingRate() output %d, sampling rate %u", output, *samplingRate); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 285 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | return NO_ERROR; | 
|  | 287 | } | 
|  | 288 |  | 
| Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 289 | 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] | 290 | { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 291 | audio_io_handle_t output; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 293 | if (streamType == AUDIO_STREAM_DEFAULT) { | 
|  | 294 | streamType = AUDIO_STREAM_MUSIC; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 295 | } | 
| Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 296 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 297 | output = getOutput(streamType); | 
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 298 | if (output == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 299 | return PERMISSION_DENIED; | 
|  | 300 | } | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 |  | 
| Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 302 | return getFrameCount(output, frameCount); | 
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 303 | } | 
|  | 304 |  | 
|  | 305 | status_t AudioSystem::getFrameCount(audio_io_handle_t output, | 
| Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 306 | size_t* frameCount) | 
| 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 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 309 | if (af == 0) return PERMISSION_DENIED; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 310 | sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 311 | if (outputDesc == 0) { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 312 | *frameCount = af->frameCount(output); | 
|  | 313 | } else { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 314 | *frameCount = outputDesc->mFrameCount; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 315 | } | 
| Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 316 | if (*frameCount == 0) { | 
| Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 317 | ALOGE("AudioSystem::getFrameCount failed for output %d", output); | 
| Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 318 | return BAD_VALUE; | 
|  | 319 | } | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 320 |  | 
| Mark Salyzyn | 34fb296 | 2014-06-18 16:30:56 -0700 | [diff] [blame] | 321 | ALOGV("getFrameCount() output %d, frameCount %zu", output, *frameCount); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 322 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | return NO_ERROR; | 
|  | 324 | } | 
|  | 325 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 326 | 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] | 327 | { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 328 | audio_io_handle_t output; | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 330 | if (streamType == AUDIO_STREAM_DEFAULT) { | 
|  | 331 | streamType = AUDIO_STREAM_MUSIC; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 332 | } | 
| Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 333 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 334 | output = getOutput(streamType); | 
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 335 | if (output == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 336 | return PERMISSION_DENIED; | 
|  | 337 | } | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 |  | 
| Glenn Kasten | 241618f | 2014-03-25 17:48:57 -0700 | [diff] [blame] | 339 | return getLatency(output, latency); | 
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 340 | } | 
|  | 341 |  | 
|  | 342 | status_t AudioSystem::getLatency(audio_io_handle_t output, | 
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 343 | uint32_t* latency) | 
|  | 344 | { | 
| Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 345 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 346 | if (af == 0) return PERMISSION_DENIED; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 347 | sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output); | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 348 | if (outputDesc == 0) { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 349 | *latency = af->latency(output); | 
|  | 350 | } else { | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 351 | *latency = outputDesc->mLatency; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 352 | } | 
|  | 353 |  | 
| Glenn Kasten | 241618f | 2014-03-25 17:48:57 -0700 | [diff] [blame] | 354 | ALOGV("getLatency() output %d, latency %d", output, *latency); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 355 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | return NO_ERROR; | 
|  | 357 | } | 
|  | 358 |  | 
| Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 359 | status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format, | 
|  | 360 | audio_channel_mask_t channelMask, size_t* buffSize) | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | { | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 362 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); | 
|  | 363 | if (afc == 0) { | 
|  | 364 | return NO_INIT; | 
|  | 365 | } | 
|  | 366 | return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | } | 
|  | 368 |  | 
| Eric Laurent | f0ee6f4 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 369 | status_t AudioSystem::setVoiceVolume(float value) | 
|  | 370 | { | 
|  | 371 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 372 | if (af == 0) return PERMISSION_DENIED; | 
|  | 373 | return af->setVoiceVolume(value); | 
|  | 374 | } | 
|  | 375 |  | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 376 | status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t *halFrames, | 
| Glenn Kasten | 0ed1959 | 2014-03-26 07:50:05 -0700 | [diff] [blame] | 377 | uint32_t *dspFrames) | 
| Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 378 | { | 
|  | 379 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 380 | if (af == 0) return PERMISSION_DENIED; | 
|  | 381 |  | 
| Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 382 | return af->getRenderPosition(halFrames, dspFrames, output); | 
| Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 383 | } | 
|  | 384 |  | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 385 | uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) | 
|  | 386 | { | 
| Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 387 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
| Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 388 | uint32_t result = 0; | 
| Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 389 | if (af == 0) return result; | 
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 390 | if (ioHandle == AUDIO_IO_HANDLE_NONE) return result; | 
| Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 391 |  | 
|  | 392 | result = af->getInputFramesLost(ioHandle); | 
|  | 393 | return result; | 
|  | 394 | } | 
|  | 395 |  | 
| Eric Laurent | de3f839 | 2014-07-27 18:38:22 -0700 | [diff] [blame] | 396 | audio_unique_id_t AudioSystem::newAudioUniqueId() | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 397 | { | 
| Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 398 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
| Eric Laurent | de3f839 | 2014-07-27 18:38:22 -0700 | [diff] [blame] | 399 | if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE; | 
|  | 400 | return af->newAudioUniqueId(); | 
| Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 401 | } | 
|  | 402 |  | 
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 403 | void AudioSystem::acquireAudioSessionId(int audioSession, pid_t pid) | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 404 | { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 405 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 406 | if (af != 0) { | 
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 407 | af->acquireAudioSessionId(audioSession, pid); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 408 | } | 
|  | 409 | } | 
|  | 410 |  | 
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 411 | void AudioSystem::releaseAudioSessionId(int audioSession, pid_t pid) | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 412 | { | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 413 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 414 | if (af != 0) { | 
| Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 415 | af->releaseAudioSessionId(audioSession, pid); | 
| Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 416 | } | 
|  | 417 | } | 
|  | 418 |  | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 419 | audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) | 
|  | 420 | { | 
|  | 421 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 422 | if (af == 0) return AUDIO_HW_SYNC_INVALID; | 
|  | 423 | return af->getAudioHwSyncForSession(sessionId); | 
|  | 424 | } | 
|  | 425 |  | 
| Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 426 | status_t AudioSystem::systemReady() | 
|  | 427 | { | 
|  | 428 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 429 | if (af == 0) return NO_INIT; | 
|  | 430 | return af->systemReady(); | 
|  | 431 | } | 
|  | 432 |  | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | // --------------------------------------------------------------------------- | 
|  | 434 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 435 |  | 
|  | 436 | void AudioSystem::AudioFlingerClient::clearIoCache() | 
|  | 437 | { | 
|  | 438 | Mutex::Autolock _l(mLock); | 
|  | 439 | mIoDescriptors.clear(); | 
|  | 440 | mInBuffSize = 0; | 
|  | 441 | mInSamplingRate = 0; | 
|  | 442 | mInFormat = AUDIO_FORMAT_DEFAULT; | 
|  | 443 | mInChannelMask = AUDIO_CHANNEL_NONE; | 
|  | 444 | } | 
|  | 445 |  | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 446 | void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) | 
|  | 447 | { | 
| Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 448 | audio_error_callback cb = NULL; | 
|  | 449 | { | 
|  | 450 | Mutex::Autolock _l(AudioSystem::gLock); | 
|  | 451 | AudioSystem::gAudioFlinger.clear(); | 
|  | 452 | cb = gAudioErrorCallback; | 
|  | 453 | } | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 455 | // clear output handles and stream to output map caches | 
|  | 456 | clearIoCache(); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 |  | 
| Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 458 | if (cb) { | 
|  | 459 | cb(DEAD_OBJECT); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | } | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 461 | ALOGW("AudioFlinger server died!"); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | } | 
|  | 463 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 464 | void AudioSystem::AudioFlingerClient::ioConfigChanged(audio_io_config_event event, | 
|  | 465 | const sp<AudioIoDescriptor>& ioDesc) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 466 | ALOGV("ioConfigChanged() event %d", event); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 467 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 468 | if (ioDesc == 0 || ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 469 |  | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 470 | audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE; | 
|  | 471 | Vector < sp<AudioDeviceCallback> > callbacks; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 472 |  | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 473 | { | 
|  | 474 | Mutex::Autolock _l(mLock); | 
|  | 475 |  | 
|  | 476 | switch (event) { | 
|  | 477 | case AUDIO_OUTPUT_OPENED: | 
|  | 478 | case AUDIO_INPUT_OPENED: { | 
|  | 479 | if (getIoDescriptor(ioDesc->mIoHandle) != 0) { | 
|  | 480 | ALOGV("ioConfigChanged() opening already existing output! %d", ioDesc->mIoHandle); | 
|  | 481 | break; | 
|  | 482 | } | 
|  | 483 | mIoDescriptors.add(ioDesc->mIoHandle, ioDesc); | 
|  | 484 |  | 
|  | 485 | if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) { | 
|  | 486 | deviceId = ioDesc->getDeviceId(); | 
|  | 487 | ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle); | 
|  | 488 | if (ioIndex >= 0) { | 
|  | 489 | callbacks = mAudioDeviceCallbacks.valueAt(ioIndex); | 
|  | 490 | } | 
|  | 491 | } | 
|  | 492 | ALOGV("ioConfigChanged() new %s opened %d samplingRate %u, format %#x channel mask %#x " | 
|  | 493 | "frameCount %zu deviceId %d", event == AUDIO_OUTPUT_OPENED ? "output" : "input", | 
|  | 494 | ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, ioDesc->mChannelMask, | 
|  | 495 | ioDesc->mFrameCount, ioDesc->getDeviceId()); | 
|  | 496 | } break; | 
|  | 497 | case AUDIO_OUTPUT_CLOSED: | 
|  | 498 | case AUDIO_INPUT_CLOSED: { | 
|  | 499 | if (getIoDescriptor(ioDesc->mIoHandle) == 0) { | 
|  | 500 | ALOGW("ioConfigChanged() closing unknown %s %d", | 
|  | 501 | event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle); | 
|  | 502 | break; | 
|  | 503 | } | 
|  | 504 | ALOGV("ioConfigChanged() %s %d closed", | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 505 | event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 506 |  | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 507 | mIoDescriptors.removeItem(ioDesc->mIoHandle); | 
|  | 508 | mAudioDeviceCallbacks.removeItem(ioDesc->mIoHandle); | 
|  | 509 | } break; | 
|  | 510 |  | 
|  | 511 | case AUDIO_OUTPUT_CONFIG_CHANGED: | 
|  | 512 | case AUDIO_INPUT_CONFIG_CHANGED: { | 
|  | 513 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor(ioDesc->mIoHandle); | 
|  | 514 | if (oldDesc == 0) { | 
|  | 515 | ALOGW("ioConfigChanged() modifying unknown output! %d", ioDesc->mIoHandle); | 
|  | 516 | break; | 
|  | 517 | } | 
|  | 518 |  | 
|  | 519 | deviceId = oldDesc->getDeviceId(); | 
|  | 520 | mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc); | 
|  | 521 |  | 
|  | 522 | if (deviceId != ioDesc->getDeviceId()) { | 
|  | 523 | deviceId = ioDesc->getDeviceId(); | 
|  | 524 | ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(ioDesc->mIoHandle); | 
|  | 525 | if (ioIndex >= 0) { | 
|  | 526 | callbacks = mAudioDeviceCallbacks.valueAt(ioIndex); | 
|  | 527 | } | 
|  | 528 | } | 
|  | 529 | ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x " | 
|  | 530 | "channel mask %#x frameCount %zu deviceId %d", | 
|  | 531 | event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", | 
|  | 532 | ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, | 
|  | 533 | ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->getDeviceId()); | 
|  | 534 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 535 | } break; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 536 | } | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 537 | } | 
|  | 538 | // callbacks.size() != 0 =>  ioDesc->mIoHandle and deviceId are valid | 
|  | 539 | for (size_t i = 0; i < callbacks.size(); i++) { | 
|  | 540 | callbacks[i]->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 541 | } | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | } | 
|  | 543 |  | 
| Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 544 | status_t AudioSystem::AudioFlingerClient::getInputBufferSize( | 
|  | 545 | uint32_t sampleRate, audio_format_t format, | 
|  | 546 | audio_channel_mask_t channelMask, size_t* buffSize) | 
|  | 547 | { | 
|  | 548 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 549 | if (af == 0) { | 
|  | 550 | return PERMISSION_DENIED; | 
|  | 551 | } | 
|  | 552 | Mutex::Autolock _l(mLock); | 
|  | 553 | // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values | 
|  | 554 | if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat) | 
|  | 555 | || (channelMask != mInChannelMask)) { | 
|  | 556 | size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask); | 
|  | 557 | if (inBuffSize == 0) { | 
|  | 558 | ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %x", | 
|  | 559 | sampleRate, format, channelMask); | 
|  | 560 | return BAD_VALUE; | 
|  | 561 | } | 
|  | 562 | // A benign race is possible here: we could overwrite a fresher cache entry | 
|  | 563 | // save the request params | 
|  | 564 | mInSamplingRate = sampleRate; | 
|  | 565 | mInFormat = format; | 
|  | 566 | mInChannelMask = channelMask; | 
|  | 567 |  | 
|  | 568 | mInBuffSize = inBuffSize; | 
|  | 569 | } | 
|  | 570 |  | 
|  | 571 | *buffSize = mInBuffSize; | 
|  | 572 |  | 
|  | 573 | return NO_ERROR; | 
|  | 574 | } | 
|  | 575 |  | 
|  | 576 | sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) | 
|  | 577 | { | 
|  | 578 | sp<AudioIoDescriptor> desc; | 
|  | 579 | ssize_t index = mIoDescriptors.indexOfKey(ioHandle); | 
|  | 580 | if (index >= 0) { | 
|  | 581 | desc = mIoDescriptors.valueAt(index); | 
|  | 582 | } | 
|  | 583 | return desc; | 
|  | 584 | } | 
|  | 585 |  | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 586 | status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback( | 
|  | 587 | const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo) | 
|  | 588 | { | 
|  | 589 | Mutex::Autolock _l(mLock); | 
|  | 590 | Vector < sp<AudioDeviceCallback> > callbacks; | 
|  | 591 | ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo); | 
|  | 592 | if (ioIndex >= 0) { | 
|  | 593 | callbacks = mAudioDeviceCallbacks.valueAt(ioIndex); | 
|  | 594 | } | 
|  | 595 |  | 
|  | 596 | for (size_t cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) { | 
|  | 597 | if (callbacks[cbIndex] == callback) { | 
|  | 598 | return INVALID_OPERATION; | 
|  | 599 | } | 
|  | 600 | } | 
|  | 601 | callbacks.add(callback); | 
|  | 602 |  | 
|  | 603 | mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks); | 
|  | 604 | return NO_ERROR; | 
|  | 605 | } | 
|  | 606 |  | 
|  | 607 | status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback( | 
|  | 608 | const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo) | 
|  | 609 | { | 
|  | 610 | Mutex::Autolock _l(mLock); | 
|  | 611 | ssize_t ioIndex = mAudioDeviceCallbacks.indexOfKey(audioIo); | 
|  | 612 | if (ioIndex < 0) { | 
|  | 613 | return INVALID_OPERATION; | 
|  | 614 | } | 
|  | 615 | Vector < sp<AudioDeviceCallback> > callbacks = mAudioDeviceCallbacks.valueAt(ioIndex); | 
|  | 616 |  | 
|  | 617 | size_t cbIndex; | 
|  | 618 | for (cbIndex = 0; cbIndex < callbacks.size(); cbIndex++) { | 
|  | 619 | if (callbacks[cbIndex] == callback) { | 
|  | 620 | break; | 
|  | 621 | } | 
|  | 622 | } | 
|  | 623 | if (cbIndex == callbacks.size()) { | 
|  | 624 | return INVALID_OPERATION; | 
|  | 625 | } | 
|  | 626 | callbacks.removeAt(cbIndex); | 
|  | 627 | if (callbacks.size() != 0) { | 
|  | 628 | mAudioDeviceCallbacks.replaceValueFor(audioIo, callbacks); | 
|  | 629 | } else { | 
|  | 630 | mAudioDeviceCallbacks.removeItem(audioIo); | 
|  | 631 | } | 
|  | 632 | return NO_ERROR; | 
|  | 633 | } | 
|  | 634 |  | 
|  | 635 | /* static */ void AudioSystem::setErrorCallback(audio_error_callback cb) | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 636 | { | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 637 | Mutex::Autolock _l(gLock); | 
| The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | gAudioErrorCallback = cb; | 
|  | 639 | } | 
|  | 640 |  | 
| Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 641 | /*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) | 
|  | 642 | { | 
|  | 643 | Mutex::Autolock _l(gLock); | 
|  | 644 | gDynPolicyCallback = cb; | 
|  | 645 | } | 
|  | 646 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 647 | // client singleton for AudioPolicyService binder interface | 
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 648 | // protected by gLockAPS | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 649 | sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; | 
|  | 650 | sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; | 
|  | 651 |  | 
|  | 652 |  | 
| Glenn Kasten | 18a6d90 | 2012-09-24 11:27:56 -0700 | [diff] [blame] | 653 | // establish binder interface to AudioPolicy service | 
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 654 | const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 655 | { | 
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 656 | sp<IAudioPolicyService> ap; | 
|  | 657 | sp<AudioPolicyServiceClient> apc; | 
|  | 658 | { | 
|  | 659 | Mutex::Autolock _l(gLockAPS); | 
|  | 660 | if (gAudioPolicyService == 0) { | 
|  | 661 | sp<IServiceManager> sm = defaultServiceManager(); | 
|  | 662 | sp<IBinder> binder; | 
|  | 663 | do { | 
|  | 664 | binder = sm->getService(String16("media.audio_policy")); | 
|  | 665 | if (binder != 0) | 
|  | 666 | break; | 
|  | 667 | ALOGW("AudioPolicyService not published, waiting..."); | 
|  | 668 | usleep(500000); // 0.5 s | 
|  | 669 | } while (true); | 
|  | 670 | if (gAudioPolicyServiceClient == NULL) { | 
|  | 671 | gAudioPolicyServiceClient = new AudioPolicyServiceClient(); | 
|  | 672 | } | 
|  | 673 | binder->linkToDeath(gAudioPolicyServiceClient); | 
|  | 674 | gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); | 
|  | 675 | LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0); | 
|  | 676 | apc = gAudioPolicyServiceClient; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 677 | } | 
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 678 | ap = gAudioPolicyService; | 
|  | 679 | } | 
|  | 680 | if (apc != 0) { | 
|  | 681 | ap->registerClient(apc); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 682 | } | 
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 683 |  | 
| Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 684 | return ap; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 685 | } | 
|  | 686 |  | 
| Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 687 | // --------------------------------------------------------------------------- | 
|  | 688 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 689 | status_t AudioSystem::setDeviceConnectionState(audio_devices_t device, | 
|  | 690 | audio_policy_dev_state_t state, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 691 | const char *device_address, | 
|  | 692 | const char *device_name) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 693 | { | 
|  | 694 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
| Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 695 | const char *address = ""; | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 696 | const char *name = ""; | 
| Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 697 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 698 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 699 |  | 
| Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 700 | if (device_address != NULL) { | 
|  | 701 | address = device_address; | 
|  | 702 | } | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 703 | if (device_name != NULL) { | 
|  | 704 | name = device_name; | 
|  | 705 | } | 
|  | 706 | return aps->setDeviceConnectionState(device, state, address, name); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 707 | } | 
|  | 708 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 709 | audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device, | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 710 | const char *device_address) | 
|  | 711 | { | 
|  | 712 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 713 | if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 714 |  | 
|  | 715 | return aps->getDeviceConnectionState(device, device_address); | 
|  | 716 | } | 
|  | 717 |  | 
| Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 718 | status_t AudioSystem::setPhoneState(audio_mode_t state) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 719 | { | 
| Glenn Kasten | 347966c | 2012-01-18 14:58:32 -0800 | [diff] [blame] | 720 | if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 721 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 722 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 723 |  | 
|  | 724 | return aps->setPhoneState(state); | 
|  | 725 | } | 
|  | 726 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 727 | 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] | 728 | { | 
|  | 729 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 730 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 731 | return aps->setForceUse(usage, config); | 
|  | 732 | } | 
|  | 733 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 734 | 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] | 735 | { | 
|  | 736 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 737 | if (aps == 0) return AUDIO_POLICY_FORCE_NONE; | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 738 | return aps->getForceUse(usage); | 
|  | 739 | } | 
|  | 740 |  | 
|  | 741 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 742 | audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream, | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 743 | uint32_t samplingRate, | 
| Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 744 | audio_format_t format, | 
| Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 745 | audio_channel_mask_t channelMask, | 
| Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 746 | audio_output_flags_t flags, | 
|  | 747 | const audio_offload_info_t *offloadInfo) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 748 | { | 
| Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 749 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 750 | if (aps == 0) return 0; | 
| Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 751 | return aps->getOutput(stream, samplingRate, format, channelMask, flags, offloadInfo); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 752 | } | 
|  | 753 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 754 | status_t AudioSystem::getOutputForAttr(const audio_attributes_t *attr, | 
|  | 755 | audio_io_handle_t *output, | 
|  | 756 | audio_session_t session, | 
|  | 757 | audio_stream_type_t *stream, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 758 | uid_t uid, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 759 | uint32_t samplingRate, | 
|  | 760 | audio_format_t format, | 
|  | 761 | audio_channel_mask_t channelMask, | 
|  | 762 | audio_output_flags_t flags, | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 763 | audio_port_handle_t selectedDeviceId, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 764 | const audio_offload_info_t *offloadInfo) | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 765 | { | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 766 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 767 | if (aps == 0) return NO_INIT; | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 768 | return aps->getOutputForAttr(attr, output, session, stream, uid, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 769 | samplingRate, format, channelMask, | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 770 | flags, selectedDeviceId, offloadInfo); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 771 | } | 
|  | 772 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 773 | status_t AudioSystem::startOutput(audio_io_handle_t output, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 774 | audio_stream_type_t stream, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 775 | audio_session_t session) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 776 | { | 
|  | 777 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 778 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 779 | return aps->startOutput(output, stream, session); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 780 | } | 
|  | 781 |  | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 782 | status_t AudioSystem::stopOutput(audio_io_handle_t output, | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 783 | audio_stream_type_t stream, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 784 | audio_session_t session) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 785 | { | 
|  | 786 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 787 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 788 | return aps->stopOutput(output, stream, session); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 789 | } | 
|  | 790 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 791 | void AudioSystem::releaseOutput(audio_io_handle_t output, | 
|  | 792 | audio_stream_type_t stream, | 
|  | 793 | audio_session_t session) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 794 | { | 
|  | 795 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 796 | if (aps == 0) return; | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 797 | aps->releaseOutput(output, stream, session); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 798 | } | 
|  | 799 |  | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 800 | status_t AudioSystem::getInputForAttr(const audio_attributes_t *attr, | 
|  | 801 | audio_io_handle_t *input, | 
|  | 802 | audio_session_t session, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 803 | uid_t uid, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 804 | uint32_t samplingRate, | 
|  | 805 | audio_format_t format, | 
|  | 806 | audio_channel_mask_t channelMask, | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 807 | audio_input_flags_t flags, | 
|  | 808 | audio_port_handle_t selectedDeviceId) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 809 | { | 
|  | 810 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 811 | if (aps == 0) return NO_INIT; | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 812 | return aps->getInputForAttr( | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 813 | attr, input, session, uid, samplingRate, format, channelMask, flags, selectedDeviceId); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 814 | } | 
|  | 815 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 816 | status_t AudioSystem::startInput(audio_io_handle_t input, | 
|  | 817 | audio_session_t session) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 818 | { | 
|  | 819 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 820 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 821 | return aps->startInput(input, session); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 822 | } | 
|  | 823 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 824 | status_t AudioSystem::stopInput(audio_io_handle_t input, | 
|  | 825 | audio_session_t session) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 826 | { | 
|  | 827 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 828 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 829 | return aps->stopInput(input, session); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 830 | } | 
|  | 831 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 832 | void AudioSystem::releaseInput(audio_io_handle_t input, | 
|  | 833 | audio_session_t session) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 834 | { | 
|  | 835 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 836 | if (aps == 0) return; | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 837 | aps->releaseInput(input, session); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 838 | } | 
|  | 839 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 840 | status_t AudioSystem::initStreamVolume(audio_stream_type_t stream, | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 841 | int indexMin, | 
|  | 842 | int indexMax) | 
|  | 843 | { | 
|  | 844 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 845 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 846 | return aps->initStreamVolume(stream, indexMin, indexMax); | 
|  | 847 | } | 
|  | 848 |  | 
| Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 849 | status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, | 
|  | 850 | int index, | 
|  | 851 | audio_devices_t device) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 852 | { | 
|  | 853 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 854 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 855 | return aps->setStreamVolumeIndex(stream, index, device); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 856 | } | 
|  | 857 |  | 
| Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 858 | status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, | 
|  | 859 | int *index, | 
|  | 860 | audio_devices_t device) | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 861 | { | 
|  | 862 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 863 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 864 | return aps->getStreamVolumeIndex(stream, index, device); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 865 | } | 
|  | 866 |  | 
| Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 867 | uint32_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 868 | { | 
|  | 869 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 870 | if (aps == 0) return 0; | 
|  | 871 | return aps->getStrategyForStream(stream); | 
|  | 872 | } | 
|  | 873 |  | 
| Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 874 | audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream) | 
| Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 875 | { | 
|  | 876 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
| Glenn Kasten | 45faf7e | 2014-01-17 10:23:01 -0800 | [diff] [blame] | 877 | if (aps == 0) return AUDIO_DEVICE_NONE; | 
| Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 878 | return aps->getDevicesForStream(stream); | 
|  | 879 | } | 
|  | 880 |  | 
| Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 881 | audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t *desc) | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 882 | { | 
|  | 883 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
| Glenn Kasten | efa6ea9 | 2014-01-08 09:10:43 -0800 | [diff] [blame] | 884 | // FIXME change return type to status_t, and return PERMISSION_DENIED here | 
| Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 885 | if (aps == 0) return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 886 | return aps->getOutputForEffect(desc); | 
|  | 887 | } | 
|  | 888 |  | 
| Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 889 | status_t AudioSystem::registerEffect(const effect_descriptor_t *desc, | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 890 | audio_io_handle_t io, | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 891 | uint32_t strategy, | 
|  | 892 | int session, | 
|  | 893 | int id) | 
|  | 894 | { | 
|  | 895 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 896 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 897 | return aps->registerEffect(desc, io, strategy, session, id); | 
| Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 898 | } | 
|  | 899 |  | 
|  | 900 | status_t AudioSystem::unregisterEffect(int id) | 
|  | 901 | { | 
|  | 902 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 903 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 904 | return aps->unregisterEffect(id); | 
|  | 905 | } | 
|  | 906 |  | 
| Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 907 | status_t AudioSystem::setEffectEnabled(int id, bool enabled) | 
|  | 908 | { | 
|  | 909 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 910 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 911 | return aps->setEffectEnabled(id, enabled); | 
|  | 912 | } | 
|  | 913 |  | 
| Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 914 | 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] | 915 | { | 
| Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 916 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 917 | if (aps == 0) return PERMISSION_DENIED; | 
| Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 918 | if (state == NULL) return BAD_VALUE; | 
| Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 919 | *state = aps->isStreamActive(stream, inPastMs); | 
|  | 920 | return NO_ERROR; | 
|  | 921 | } | 
|  | 922 |  | 
| Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 923 | status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state, | 
|  | 924 | uint32_t inPastMs) | 
|  | 925 | { | 
|  | 926 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 927 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 928 | if (state == NULL) return BAD_VALUE; | 
|  | 929 | *state = aps->isStreamActiveRemotely(stream, inPastMs); | 
|  | 930 | return NO_ERROR; | 
|  | 931 | } | 
|  | 932 |  | 
| Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 933 | status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) | 
|  | 934 | { | 
|  | 935 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 936 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 937 | if (state == NULL) return BAD_VALUE; | 
|  | 938 | *state = aps->isSourceActive(stream); | 
|  | 939 | return NO_ERROR; | 
|  | 940 | } | 
|  | 941 |  | 
| Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 942 | uint32_t AudioSystem::getPrimaryOutputSamplingRate() | 
| Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 943 | { | 
|  | 944 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 945 | if (af == 0) return 0; | 
|  | 946 | return af->getPrimaryOutputSamplingRate(); | 
|  | 947 | } | 
|  | 948 |  | 
| Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 949 | size_t AudioSystem::getPrimaryOutputFrameCount() | 
| Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 950 | { | 
|  | 951 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 952 | if (af == 0) return 0; | 
|  | 953 | return af->getPrimaryOutputFrameCount(); | 
|  | 954 | } | 
| Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 955 |  | 
| Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 956 | status_t AudioSystem::setLowRamDevice(bool isLowRamDevice) | 
|  | 957 | { | 
|  | 958 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 959 | if (af == 0) return PERMISSION_DENIED; | 
|  | 960 | return af->setLowRamDevice(isLowRamDevice); | 
|  | 961 | } | 
|  | 962 |  | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 963 | void AudioSystem::clearAudioConfigCache() | 
|  | 964 | { | 
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 965 | // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 966 | ALOGV("clearAudioConfigCache()"); | 
| Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 967 | { | 
|  | 968 | Mutex::Autolock _l(gLock); | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 969 | if (gAudioFlingerClient != 0) { | 
|  | 970 | gAudioFlingerClient->clearIoCache(); | 
|  | 971 | } | 
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 972 | gAudioFlinger.clear(); | 
|  | 973 | } | 
|  | 974 | { | 
|  | 975 | Mutex::Autolock _l(gLockAPS); | 
|  | 976 | gAudioPolicyService.clear(); | 
|  | 977 | } | 
| Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 978 | } | 
|  | 979 |  | 
| Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 980 | bool AudioSystem::isOffloadSupported(const audio_offload_info_t& info) | 
|  | 981 | { | 
|  | 982 | ALOGV("isOffloadSupported()"); | 
|  | 983 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 984 | if (aps == 0) return false; | 
|  | 985 | return aps->isOffloadSupported(info); | 
|  | 986 | } | 
|  | 987 |  | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 988 | status_t AudioSystem::listAudioPorts(audio_port_role_t role, | 
|  | 989 | audio_port_type_t type, | 
|  | 990 | unsigned int *num_ports, | 
|  | 991 | struct audio_port *ports, | 
|  | 992 | unsigned int *generation) | 
|  | 993 | { | 
|  | 994 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 995 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 996 | return aps->listAudioPorts(role, type, num_ports, ports, generation); | 
|  | 997 | } | 
|  | 998 |  | 
|  | 999 | status_t AudioSystem::getAudioPort(struct audio_port *port) | 
|  | 1000 | { | 
|  | 1001 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 1002 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 1003 | return aps->getAudioPort(port); | 
|  | 1004 | } | 
|  | 1005 |  | 
|  | 1006 | status_t AudioSystem::createAudioPatch(const struct audio_patch *patch, | 
|  | 1007 | audio_patch_handle_t *handle) | 
|  | 1008 | { | 
|  | 1009 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 1010 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 1011 | return aps->createAudioPatch(patch, handle); | 
|  | 1012 | } | 
|  | 1013 |  | 
|  | 1014 | status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) | 
|  | 1015 | { | 
|  | 1016 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 1017 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 1018 | return aps->releaseAudioPatch(handle); | 
|  | 1019 | } | 
|  | 1020 |  | 
|  | 1021 | status_t AudioSystem::listAudioPatches(unsigned int *num_patches, | 
|  | 1022 | struct audio_patch *patches, | 
|  | 1023 | unsigned int *generation) | 
|  | 1024 | { | 
|  | 1025 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 1026 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 1027 | return aps->listAudioPatches(num_patches, patches, generation); | 
|  | 1028 | } | 
|  | 1029 |  | 
|  | 1030 | status_t AudioSystem::setAudioPortConfig(const struct audio_port_config *config) | 
|  | 1031 | { | 
|  | 1032 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 1033 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 1034 | return aps->setAudioPortConfig(config); | 
|  | 1035 | } | 
|  | 1036 |  | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1037 | status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1038 | { | 
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1039 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 1040 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 1041 |  | 
|  | 1042 | Mutex::Autolock _l(gLockAPS); | 
|  | 1043 | if (gAudioPolicyServiceClient == 0) { | 
|  | 1044 | return NO_INIT; | 
|  | 1045 | } | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame^] | 1046 | int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback); | 
|  | 1047 | if (ret == 1) { | 
|  | 1048 | aps->setAudioPortCallbacksEnabled(true); | 
|  | 1049 | } | 
|  | 1050 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1051 | } | 
|  | 1052 |  | 
| Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1053 | /*static*/ | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1054 | status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) | 
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1055 | { | 
|  | 1056 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 1057 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 1058 |  | 
|  | 1059 | Mutex::Autolock _l(gLockAPS); | 
|  | 1060 | if (gAudioPolicyServiceClient == 0) { | 
|  | 1061 | return NO_INIT; | 
|  | 1062 | } | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame^] | 1063 | int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback); | 
|  | 1064 | if (ret == 0) { | 
|  | 1065 | aps->setAudioPortCallbacksEnabled(false); | 
|  | 1066 | } | 
|  | 1067 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1068 | } | 
|  | 1069 |  | 
|  | 1070 | status_t AudioSystem::addAudioDeviceCallback( | 
|  | 1071 | const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo) | 
|  | 1072 | { | 
|  | 1073 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); | 
|  | 1074 | if (afc == 0) { | 
|  | 1075 | return NO_INIT; | 
|  | 1076 | } | 
|  | 1077 | return afc->addAudioDeviceCallback(callback, audioIo); | 
|  | 1078 | } | 
|  | 1079 |  | 
|  | 1080 | status_t AudioSystem::removeAudioDeviceCallback( | 
|  | 1081 | const sp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo) | 
|  | 1082 | { | 
|  | 1083 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); | 
|  | 1084 | if (afc == 0) { | 
|  | 1085 | return NO_INIT; | 
|  | 1086 | } | 
|  | 1087 | return afc->removeAudioDeviceCallback(callback, audioIo); | 
|  | 1088 | } | 
|  | 1089 |  | 
|  | 1090 | audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) | 
|  | 1091 | { | 
|  | 1092 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); | 
|  | 1093 | if (af == 0) return PERMISSION_DENIED; | 
|  | 1094 | const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo); | 
|  | 1095 | if (desc == 0) { | 
|  | 1096 | return AUDIO_PORT_HANDLE_NONE; | 
|  | 1097 | } | 
|  | 1098 | return desc->getDeviceId(); | 
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1099 | } | 
|  | 1100 |  | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1101 | status_t AudioSystem::acquireSoundTriggerSession(audio_session_t *session, | 
|  | 1102 | audio_io_handle_t *ioHandle, | 
|  | 1103 | audio_devices_t *device) | 
|  | 1104 | { | 
|  | 1105 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 1106 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 1107 | return aps->acquireSoundTriggerSession(session, ioHandle, device); | 
|  | 1108 | } | 
|  | 1109 |  | 
|  | 1110 | status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) | 
|  | 1111 | { | 
|  | 1112 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 1113 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 1114 | return aps->releaseSoundTriggerSession(session); | 
|  | 1115 | } | 
| Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1116 |  | 
|  | 1117 | audio_mode_t AudioSystem::getPhoneState() | 
|  | 1118 | { | 
|  | 1119 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 1120 | if (aps == 0) return AUDIO_MODE_INVALID; | 
|  | 1121 | return aps->getPhoneState(); | 
|  | 1122 | } | 
|  | 1123 |  | 
| Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 1124 | status_t AudioSystem::registerPolicyMixes(Vector<AudioMix> mixes, bool registration) | 
|  | 1125 | { | 
|  | 1126 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 1127 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 1128 | return aps->registerPolicyMixes(mixes, registration); | 
|  | 1129 | } | 
| Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1130 |  | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1131 | status_t AudioSystem::startAudioSource(const struct audio_port_config *source, | 
|  | 1132 | const audio_attributes_t *attributes, | 
|  | 1133 | audio_io_handle_t *handle) | 
|  | 1134 | { | 
|  | 1135 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 1136 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 1137 | return aps->startAudioSource(source, attributes, handle); | 
|  | 1138 | } | 
|  | 1139 |  | 
|  | 1140 | status_t AudioSystem::stopAudioSource(audio_io_handle_t handle) | 
|  | 1141 | { | 
|  | 1142 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); | 
|  | 1143 | if (aps == 0) return PERMISSION_DENIED; | 
|  | 1144 | return aps->stopAudioSource(handle); | 
|  | 1145 | } | 
|  | 1146 |  | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1147 | // --------------------------------------------------------------------------- | 
|  | 1148 |  | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame^] | 1149 | int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback( | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1150 | const sp<AudioPortCallback>& callback) | 
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1151 | { | 
|  | 1152 | Mutex::Autolock _l(mLock); | 
|  | 1153 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1154 | if (mAudioPortCallbacks[i] == callback) { | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame^] | 1155 | return -1; | 
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1156 | } | 
|  | 1157 | } | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1158 | mAudioPortCallbacks.add(callback); | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame^] | 1159 | return mAudioPortCallbacks.size(); | 
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1160 | } | 
|  | 1161 |  | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame^] | 1162 | int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback( | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1163 | const sp<AudioPortCallback>& callback) | 
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1164 | { | 
|  | 1165 | Mutex::Autolock _l(mLock); | 
|  | 1166 | size_t i; | 
|  | 1167 | for (i = 0; i < mAudioPortCallbacks.size(); i++) { | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1168 | if (mAudioPortCallbacks[i] == callback) { | 
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1169 | break; | 
|  | 1170 | } | 
|  | 1171 | } | 
|  | 1172 | if (i == mAudioPortCallbacks.size()) { | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame^] | 1173 | return -1; | 
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1174 | } | 
|  | 1175 | mAudioPortCallbacks.removeAt(i); | 
| Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame^] | 1176 | return mAudioPortCallbacks.size(); | 
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1177 | } | 
|  | 1178 |  | 
| Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1179 |  | 
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1180 | void AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() | 
|  | 1181 | { | 
|  | 1182 | Mutex::Autolock _l(mLock); | 
|  | 1183 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { | 
|  | 1184 | mAudioPortCallbacks[i]->onAudioPortListUpdate(); | 
|  | 1185 | } | 
|  | 1186 | } | 
|  | 1187 |  | 
|  | 1188 | void AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() | 
|  | 1189 | { | 
|  | 1190 | Mutex::Autolock _l(mLock); | 
|  | 1191 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { | 
|  | 1192 | mAudioPortCallbacks[i]->onAudioPatchListUpdate(); | 
|  | 1193 | } | 
|  | 1194 | } | 
|  | 1195 |  | 
| Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1196 | void AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate( | 
|  | 1197 | String8 regId, int32_t state) | 
|  | 1198 | { | 
| Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1199 | ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.string(), state); | 
|  | 1200 | dynamic_policy_callback cb = NULL; | 
|  | 1201 | { | 
|  | 1202 | Mutex::Autolock _l(AudioSystem::gLock); | 
|  | 1203 | cb = gDynPolicyCallback; | 
|  | 1204 | } | 
|  | 1205 |  | 
|  | 1206 | if (cb != NULL) { | 
|  | 1207 | cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regId, state); | 
|  | 1208 | } | 
| Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 1209 | } | 
|  | 1210 |  | 
| Glenn Kasten | 4944acb | 2013-08-19 08:39:20 -0700 | [diff] [blame] | 1211 | void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) | 
|  | 1212 | { | 
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1213 | { | 
| Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1214 | Mutex::Autolock _l(mLock); | 
|  | 1215 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { | 
|  | 1216 | mAudioPortCallbacks[i]->onServiceDied(); | 
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1217 | } | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1218 | } | 
| Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1219 | { | 
|  | 1220 | Mutex::Autolock _l(gLockAPS); | 
|  | 1221 | AudioSystem::gAudioPolicyService.clear(); | 
|  | 1222 | } | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1223 |  | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1224 | ALOGW("AudioPolicyService server died!"); | 
| Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1225 | } | 
|  | 1226 |  | 
| Glenn Kasten | 40bc906 | 2015-03-20 09:09:33 -0700 | [diff] [blame] | 1227 | } // namespace android |