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