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> |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 21 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 22 | #include <android/media/IAudioPolicyService.h> |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 23 | #include <android/media/BnCaptureStateListener.h> |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 24 | #include <binder/IServiceManager.h> |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 25 | #include <binder/ProcessState.h> |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 26 | #include <binder/IPCThreadState.h> |
Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 27 | #include <media/AidlConversion.h> |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 28 | #include <media/AudioResamplerPublic.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include <media/AudioSystem.h> |
Glenn Kasten | 1ab85ec | 2013-05-31 09:18:43 -0700 | [diff] [blame] | 30 | #include <media/IAudioFlinger.h> |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 31 | #include <media/PolicyAidlConversion.h> |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 32 | #include <media/TypeConverter.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | #include <math.h> |
| 34 | |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 35 | #include <system/audio.h> |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 36 | #include <android/media/GetInputForAttrResponse.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 37 | |
Ytai Ben-Tsvi | 1ff7569 | 2020-11-06 12:16:12 -0800 | [diff] [blame] | 38 | #define VALUE_OR_RETURN_BINDER_STATUS(x) \ |
Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 39 | ({ auto _tmp = (x); \ |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 40 | if (!_tmp.ok()) return aidl_utils::binderStatusFromStatusT(_tmp.error()); \ |
Ytai Ben-Tsvi | a381520 | 2020-10-28 14:58:08 -0700 | [diff] [blame] | 41 | std::move(_tmp.value()); }) |
Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 42 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 43 | #define RETURN_STATUS_IF_ERROR(x) \ |
| 44 | { \ |
| 45 | auto _tmp = (x); \ |
| 46 | if (_tmp != OK) return _tmp; \ |
| 47 | } |
| 48 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 49 | // ---------------------------------------------------------------------------- |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 50 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | namespace android { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 52 | using aidl_utils::statusTFromBinderStatus; |
Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 53 | using binder::Status; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 54 | using media::IAudioPolicyService; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame^] | 55 | using media::permission::Identity; |
Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 56 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | // client singleton for AudioFlinger binder interface |
| 58 | Mutex AudioSystem::gLock; |
Ytai Ben-Tsvi | 000c3e4 | 2020-01-09 15:26:40 -0800 | [diff] [blame] | 59 | Mutex AudioSystem::gLockErrorCallbacks; |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 60 | Mutex AudioSystem::gLockAPS; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | sp<IAudioFlinger> AudioSystem::gAudioFlinger; |
| 62 | sp<AudioSystem::AudioFlingerClient> AudioSystem::gAudioFlingerClient; |
Ytai Ben-Tsvi | 000c3e4 | 2020-01-09 15:26:40 -0800 | [diff] [blame] | 63 | std::set<audio_error_callback> AudioSystem::gAudioErrorCallbacks; |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 64 | dynamic_policy_callback AudioSystem::gDynPolicyCallback = NULL; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 65 | record_config_callback AudioSystem::gRecordConfigCallback = NULL; |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 66 | routing_callback AudioSystem::gRoutingCallback = NULL; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 68 | // Required to be held while calling into gSoundTriggerCaptureStateListener. |
Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 69 | class CaptureStateListenerImpl; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 70 | |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 71 | Mutex gSoundTriggerCaptureStateListenerLock; |
Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 72 | sp<CaptureStateListenerImpl> gSoundTriggerCaptureStateListener = nullptr; |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 73 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | // establish binder interface to AudioFlinger service |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 75 | const sp<IAudioFlinger> AudioSystem::get_audio_flinger() { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 76 | sp<IAudioFlinger> af; |
| 77 | sp<AudioFlingerClient> afc; |
Mikhail Naganov | 69330d4 | 2020-04-08 19:29:50 +0000 | [diff] [blame] | 78 | bool reportNoError = false; |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 79 | { |
| 80 | Mutex::Autolock _l(gLock); |
| 81 | if (gAudioFlinger == 0) { |
| 82 | sp<IServiceManager> sm = defaultServiceManager(); |
| 83 | sp<IBinder> binder; |
| 84 | do { |
Ytai Ben-Tsvi | 50b8ccb | 2020-11-24 13:47:54 -0800 | [diff] [blame] | 85 | binder = sm->getService(String16(IAudioFlinger::DEFAULT_SERVICE_NAME)); |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 86 | if (binder != 0) |
| 87 | break; |
| 88 | ALOGW("AudioFlinger not published, waiting..."); |
| 89 | usleep(500000); // 0.5 s |
| 90 | } while (true); |
| 91 | if (gAudioFlingerClient == NULL) { |
| 92 | gAudioFlingerClient = new AudioFlingerClient(); |
| 93 | } else { |
Mikhail Naganov | 69330d4 | 2020-04-08 19:29:50 +0000 | [diff] [blame] | 94 | reportNoError = true; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 96 | binder->linkToDeath(gAudioFlingerClient); |
Ytai Ben-Tsvi | 50b8ccb | 2020-11-24 13:47:54 -0800 | [diff] [blame] | 97 | gAudioFlinger = new AudioFlingerClientAdapter( |
| 98 | interface_cast<media::IAudioFlingerService>(binder)); |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 99 | LOG_ALWAYS_FATAL_IF(gAudioFlinger == 0); |
| 100 | afc = gAudioFlingerClient; |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 101 | // Make sure callbacks can be received by gAudioFlingerClient |
| 102 | ProcessState::self()->startThreadPool(); |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 103 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 104 | af = gAudioFlinger; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 106 | if (afc != 0) { |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 107 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 108 | af->registerClient(afc); |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 109 | IPCThreadState::self()->restoreCallingIdentity(token); |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 110 | } |
Mikhail Naganov | 69330d4 | 2020-04-08 19:29:50 +0000 | [diff] [blame] | 111 | if (reportNoError) reportError(NO_ERROR); |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 112 | return af; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 115 | const sp<AudioSystem::AudioFlingerClient> AudioSystem::getAudioFlingerClient() { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 116 | // calling get_audio_flinger() will initialize gAudioFlingerClient if needed |
| 117 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 118 | if (af == 0) return 0; |
| 119 | Mutex::Autolock _l(gLock); |
| 120 | return gAudioFlingerClient; |
| 121 | } |
| 122 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 123 | sp<AudioIoDescriptor> AudioSystem::getIoDescriptor(audio_io_handle_t ioHandle) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 124 | sp<AudioIoDescriptor> desc; |
| 125 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 126 | if (afc != 0) { |
| 127 | desc = afc->getIoDescriptor(ioHandle); |
| 128 | } |
| 129 | return desc; |
| 130 | } |
| 131 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 132 | /* static */ status_t AudioSystem::checkAudioFlinger() { |
Eric Laurent | 4629161 | 2013-07-18 14:38:44 -0700 | [diff] [blame] | 133 | if (defaultServiceManager()->checkService(String16("media.audio_flinger")) != 0) { |
| 134 | return NO_ERROR; |
| 135 | } |
| 136 | return DEAD_OBJECT; |
| 137 | } |
| 138 | |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 139 | // FIXME Declare in binder opcode order, similarly to IAudioFlinger.h and IAudioFlinger.cpp |
| 140 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 141 | status_t AudioSystem::muteMicrophone(bool state) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 143 | if (af == 0) return PERMISSION_DENIED; |
| 144 | return af->setMicMute(state); |
| 145 | } |
| 146 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 147 | status_t AudioSystem::isMicrophoneMuted(bool* state) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 149 | if (af == 0) return PERMISSION_DENIED; |
| 150 | *state = af->getMicMute(); |
| 151 | return NO_ERROR; |
| 152 | } |
| 153 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 154 | status_t AudioSystem::setMasterVolume(float value) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 156 | if (af == 0) return PERMISSION_DENIED; |
| 157 | af->setMasterVolume(value); |
| 158 | return NO_ERROR; |
| 159 | } |
| 160 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 161 | status_t AudioSystem::setMasterMute(bool mute) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 163 | if (af == 0) return PERMISSION_DENIED; |
| 164 | af->setMasterMute(mute); |
| 165 | return NO_ERROR; |
| 166 | } |
| 167 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 168 | status_t AudioSystem::getMasterVolume(float* volume) { |
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 | *volume = af->masterVolume(); |
| 172 | return NO_ERROR; |
| 173 | } |
| 174 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 175 | status_t AudioSystem::getMasterMute(bool* mute) { |
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; |
| 178 | *mute = af->masterMute(); |
| 179 | return NO_ERROR; |
| 180 | } |
| 181 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 182 | status_t AudioSystem::setStreamVolume(audio_stream_type_t stream, float value, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 183 | audio_io_handle_t output) { |
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; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 187 | af->setStreamVolume(stream, value, output); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | return NO_ERROR; |
| 189 | } |
| 190 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 191 | status_t AudioSystem::setStreamMute(audio_stream_type_t stream, bool mute) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 192 | 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] | 193 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 194 | if (af == 0) return PERMISSION_DENIED; |
| 195 | af->setStreamMute(stream, mute); |
| 196 | return NO_ERROR; |
| 197 | } |
| 198 | |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 199 | status_t AudioSystem::getStreamVolume(audio_stream_type_t stream, float* volume, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 200 | audio_io_handle_t output) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 201 | 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] | 202 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 203 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 204 | *volume = af->streamVolume(stream, output); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | return NO_ERROR; |
| 206 | } |
| 207 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 208 | status_t AudioSystem::getStreamMute(audio_stream_type_t stream, bool* mute) { |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 209 | 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] | 210 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 211 | if (af == 0) return PERMISSION_DENIED; |
| 212 | *mute = af->streamMute(stream); |
| 213 | return NO_ERROR; |
| 214 | } |
| 215 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 216 | status_t AudioSystem::setMode(audio_mode_t mode) { |
Glenn Kasten | 930f4ca | 2012-01-06 16:47:31 -0800 | [diff] [blame] | 217 | 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] | 218 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 219 | if (af == 0) return PERMISSION_DENIED; |
| 220 | return af->setMode(mode); |
| 221 | } |
| 222 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 223 | status_t AudioSystem::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 225 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 226 | return af->setParameters(ioHandle, keyValuePairs); |
| 227 | } |
| 228 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 229 | String8 AudioSystem::getParameters(audio_io_handle_t ioHandle, const String8& keys) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 230 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 231 | String8 result = String8(""); |
| 232 | if (af == 0) return result; |
| 233 | |
| 234 | result = af->getParameters(ioHandle, keys); |
| 235 | return result; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 238 | status_t AudioSystem::setParameters(const String8& keyValuePairs) { |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 239 | return setParameters(AUDIO_IO_HANDLE_NONE, keyValuePairs); |
Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 242 | String8 AudioSystem::getParameters(const String8& keys) { |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 243 | return getParameters(AUDIO_IO_HANDLE_NONE, keys); |
Glenn Kasten | c23885e | 2013-12-19 16:35:18 -0800 | [diff] [blame] | 244 | } |
| 245 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | // convert volume steps to natural log scale |
| 247 | |
| 248 | // change this value to change volume scaling |
| 249 | static const float dBPerStep = 0.5f; |
| 250 | // shouldn't need to touch these |
| 251 | static const float dBConvert = -dBPerStep * 2.302585093f / 20.0f; |
| 252 | static const float dBConvertInverse = 1.0f / dBConvert; |
| 253 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 254 | float AudioSystem::linearToLog(int volume) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | // float v = volume ? exp(float(100 - volume) * dBConvert) : 0; |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 256 | // ALOGD("linearToLog(%d)=%f", volume, v); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | // return v; |
| 258 | return volume ? exp(float(100 - volume) * dBConvert) : 0; |
| 259 | } |
| 260 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 261 | int AudioSystem::logToLinear(float volume) { |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | // int v = volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 263 | // ALOGD("logTolinear(%d)=%f", v, volume); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | // return v; |
| 265 | return volume ? 100 - int(dBConvertInverse * log(volume) + 0.5) : 0; |
| 266 | } |
| 267 | |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 268 | /* static */ size_t AudioSystem::calculateMinFrameCount( |
| 269 | uint32_t afLatencyMs, uint32_t afFrameCount, uint32_t afSampleRate, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 270 | uint32_t sampleRate, float speed /*, uint32_t notificationsPerBufferReq*/) { |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 271 | // Ensure that buffer depth covers at least audio hardware latency |
| 272 | uint32_t minBufCount = afLatencyMs / ((1000 * afFrameCount) / afSampleRate); |
| 273 | if (minBufCount < 2) { |
| 274 | minBufCount = 2; |
| 275 | } |
| 276 | #if 0 |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 277 | // The notificationsPerBufferReq parameter is not yet used for non-fast tracks, |
| 278 | // but keeping the code here to make it easier to add later. |
| 279 | if (minBufCount < notificationsPerBufferReq) { |
| 280 | minBufCount = notificationsPerBufferReq; |
| 281 | } |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 282 | #endif |
| 283 | ALOGV("calculateMinFrameCount afLatency %u afFrameCount %u afSampleRate %u " |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 284 | "sampleRate %u speed %f minBufCount: %u" /*" notificationsPerBufferReq %u"*/, |
| 285 | afLatencyMs, afFrameCount, afSampleRate, sampleRate, speed, minBufCount |
| 286 | /*, notificationsPerBufferReq*/); |
Eric Laurent | 21da647 | 2017-11-09 16:29:26 -0800 | [diff] [blame] | 287 | return minBufCount * sourceFramesNeededWithTimestretch( |
| 288 | sampleRate, afFrameCount, afSampleRate, speed); |
| 289 | } |
| 290 | |
| 291 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 292 | status_t |
| 293 | AudioSystem::getOutputSamplingRate(uint32_t* samplingRate, audio_stream_type_t streamType) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 294 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 296 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 297 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 300 | output = getOutput(streamType); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 301 | if (output == 0) { |
| 302 | return PERMISSION_DENIED; |
| 303 | } |
| 304 | |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 305 | return getSamplingRate(output, samplingRate); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 308 | status_t AudioSystem::getSamplingRate(audio_io_handle_t ioHandle, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 309 | uint32_t* samplingRate) { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 310 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 311 | if (af == 0) return PERMISSION_DENIED; |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 312 | sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle); |
| 313 | if (desc == 0) { |
| 314 | *samplingRate = af->sampleRate(ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 315 | } else { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 316 | *samplingRate = desc->mSamplingRate; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 317 | } |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 318 | if (*samplingRate == 0) { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 319 | ALOGE("AudioSystem::getSamplingRate failed for ioHandle %d", ioHandle); |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 320 | return BAD_VALUE; |
| 321 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 322 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 323 | ALOGV("getSamplingRate() ioHandle %d, sampling rate %u", ioHandle, *samplingRate); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 324 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | return NO_ERROR; |
| 326 | } |
| 327 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 328 | status_t AudioSystem::getOutputFrameCount(size_t* frameCount, audio_stream_type_t streamType) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 329 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 331 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 332 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 333 | } |
Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 334 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 335 | output = getOutput(streamType); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 336 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 337 | return PERMISSION_DENIED; |
| 338 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | |
Jean-Michel Trivi | b7f24b1 | 2014-06-11 10:05:30 -0700 | [diff] [blame] | 340 | return getFrameCount(output, frameCount); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 343 | status_t AudioSystem::getFrameCount(audio_io_handle_t ioHandle, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 344 | size_t* frameCount) { |
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; |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 347 | sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle); |
| 348 | if (desc == 0) { |
| 349 | *frameCount = af->frameCount(ioHandle); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 350 | } else { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 351 | *frameCount = desc->mFrameCount; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 352 | } |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 353 | if (*frameCount == 0) { |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 354 | ALOGE("AudioSystem::getFrameCount failed for ioHandle %d", ioHandle); |
Glenn Kasten | f94006c | 2014-01-08 08:56:06 -0800 | [diff] [blame] | 355 | return BAD_VALUE; |
| 356 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 357 | |
Glenn Kasten | 2c073da | 2016-02-26 09:14:08 -0800 | [diff] [blame] | 358 | ALOGV("getFrameCount() ioHandle %d, frameCount %zu", ioHandle, *frameCount); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 359 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | return NO_ERROR; |
| 361 | } |
| 362 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 363 | status_t AudioSystem::getOutputLatency(uint32_t* latency, audio_stream_type_t streamType) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 364 | audio_io_handle_t output; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 366 | if (streamType == AUDIO_STREAM_DEFAULT) { |
| 367 | streamType = AUDIO_STREAM_MUSIC; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 368 | } |
Eric Laurent | 48f7f5e | 2009-04-02 09:32:43 -0700 | [diff] [blame] | 369 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 370 | output = getOutput(streamType); |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 371 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 372 | return PERMISSION_DENIED; |
| 373 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | |
Glenn Kasten | 241618f | 2014-03-25 17:48:57 -0700 | [diff] [blame] | 375 | return getLatency(output, latency); |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | status_t AudioSystem::getLatency(audio_io_handle_t output, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 379 | uint32_t* latency) { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 380 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 381 | if (af == 0) return PERMISSION_DENIED; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 382 | sp<AudioIoDescriptor> outputDesc = getIoDescriptor(output); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 383 | if (outputDesc == 0) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 384 | *latency = af->latency(output); |
| 385 | } else { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 386 | *latency = outputDesc->mLatency; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Glenn Kasten | 241618f | 2014-03-25 17:48:57 -0700 | [diff] [blame] | 389 | ALOGV("getLatency() output %d, latency %d", output, *latency); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 390 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | return NO_ERROR; |
| 392 | } |
| 393 | |
Glenn Kasten | dd8104c | 2012-07-02 12:42:44 -0700 | [diff] [blame] | 394 | status_t AudioSystem::getInputBufferSize(uint32_t sampleRate, audio_format_t format, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 395 | audio_channel_mask_t channelMask, size_t* buffSize) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 396 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 397 | if (afc == 0) { |
| 398 | return NO_INIT; |
| 399 | } |
| 400 | return afc->getInputBufferSize(sampleRate, format, channelMask, buffSize); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 403 | status_t AudioSystem::setVoiceVolume(float value) { |
Eric Laurent | f0ee6f4 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 404 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 405 | if (af == 0) return PERMISSION_DENIED; |
| 406 | return af->setVoiceVolume(value); |
| 407 | } |
| 408 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 409 | status_t AudioSystem::getRenderPosition(audio_io_handle_t output, uint32_t* halFrames, |
| 410 | uint32_t* dspFrames) { |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 411 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 412 | if (af == 0) return PERMISSION_DENIED; |
| 413 | |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 414 | return af->getRenderPosition(halFrames, dspFrames, output); |
Eric Laurent | 342e9cf | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 417 | uint32_t AudioSystem::getInputFramesLost(audio_io_handle_t ioHandle) { |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 418 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
Glenn Kasten | 5f972c0 | 2014-01-13 09:59:31 -0800 | [diff] [blame] | 419 | uint32_t result = 0; |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 420 | if (af == 0) return result; |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 421 | if (ioHandle == AUDIO_IO_HANDLE_NONE) return result; |
Eric Laurent | 05bca2f | 2010-02-26 02:47:27 -0800 | [diff] [blame] | 422 | |
| 423 | result = af->getInputFramesLost(ioHandle); |
| 424 | return result; |
| 425 | } |
| 426 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 427 | audio_unique_id_t AudioSystem::newAudioUniqueId(audio_unique_id_use_t use) { |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 428 | // Must not use AF as IDs will re-roll on audioserver restart, b/130369529. |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 429 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
Eric Laurent | de3f839 | 2014-07-27 18:38:22 -0700 | [diff] [blame] | 430 | if (af == 0) return AUDIO_UNIQUE_ID_ALLOCATE; |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 431 | return af->newAudioUniqueId(use); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 434 | void AudioSystem::acquireAudioSessionId(audio_session_t audioSession, pid_t pid, uid_t uid) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 435 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 436 | if (af != 0) { |
Andy Hung | 8b0bfd9 | 2019-12-23 13:11:11 -0800 | [diff] [blame] | 437 | af->acquireAudioSessionId(audioSession, pid, uid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 441 | void AudioSystem::releaseAudioSessionId(audio_session_t audioSession, pid_t pid) { |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 442 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 443 | if (af != 0) { |
Marco Nelissen | d457c97 | 2014-02-11 08:47:07 -0800 | [diff] [blame] | 444 | af->releaseAudioSessionId(audioSession, pid); |
Marco Nelissen | 3a34bef | 2011-08-02 13:33:41 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 448 | audio_hw_sync_t AudioSystem::getAudioHwSyncForSession(audio_session_t sessionId) { |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 449 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 450 | if (af == 0) return AUDIO_HW_SYNC_INVALID; |
| 451 | return af->getAudioHwSyncForSession(sessionId); |
| 452 | } |
| 453 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 454 | status_t AudioSystem::systemReady() { |
Eric Laurent | 72e3f39 | 2015-05-20 14:43:50 -0700 | [diff] [blame] | 455 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 456 | if (af == 0) return NO_INIT; |
| 457 | return af->systemReady(); |
| 458 | } |
| 459 | |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 460 | status_t AudioSystem::getFrameCountHAL(audio_io_handle_t ioHandle, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 461 | size_t* frameCount) { |
Glenn Kasten | 4a8308b | 2016-04-18 14:10:01 -0700 | [diff] [blame] | 462 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 463 | if (af == 0) return PERMISSION_DENIED; |
| 464 | sp<AudioIoDescriptor> desc = getIoDescriptor(ioHandle); |
| 465 | if (desc == 0) { |
| 466 | *frameCount = af->frameCountHAL(ioHandle); |
| 467 | } else { |
| 468 | *frameCount = desc->mFrameCountHAL; |
| 469 | } |
| 470 | if (*frameCount == 0) { |
| 471 | ALOGE("AudioSystem::getFrameCountHAL failed for ioHandle %d", ioHandle); |
| 472 | return BAD_VALUE; |
| 473 | } |
| 474 | |
| 475 | ALOGV("getFrameCountHAL() ioHandle %d, frameCount %zu", ioHandle, *frameCount); |
| 476 | |
| 477 | return NO_ERROR; |
| 478 | } |
| 479 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | // --------------------------------------------------------------------------- |
| 481 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 482 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 483 | void AudioSystem::AudioFlingerClient::clearIoCache() { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 484 | Mutex::Autolock _l(mLock); |
| 485 | mIoDescriptors.clear(); |
| 486 | mInBuffSize = 0; |
| 487 | mInSamplingRate = 0; |
| 488 | mInFormat = AUDIO_FORMAT_DEFAULT; |
| 489 | mInChannelMask = AUDIO_CHANNEL_NONE; |
| 490 | } |
| 491 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 492 | void AudioSystem::AudioFlingerClient::binderDied(const wp<IBinder>& who __unused) { |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 493 | { |
| 494 | Mutex::Autolock _l(AudioSystem::gLock); |
| 495 | AudioSystem::gAudioFlinger.clear(); |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 496 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 498 | // clear output handles and stream to output map caches |
| 499 | clearIoCache(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | |
Ytai Ben-Tsvi | 000c3e4 | 2020-01-09 15:26:40 -0800 | [diff] [blame] | 501 | reportError(DEAD_OBJECT); |
| 502 | |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 503 | ALOGW("AudioFlinger server died!"); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 506 | Status AudioSystem::AudioFlingerClient::ioConfigChanged( |
| 507 | media::AudioIoConfigEvent _event, |
| 508 | const media::AudioIoDescriptor& _ioDesc) { |
Ytai Ben-Tsvi | 1ff7569 | 2020-11-06 12:16:12 -0800 | [diff] [blame] | 509 | audio_io_config_event event = VALUE_OR_RETURN_BINDER_STATUS( |
Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 510 | aidl2legacy_AudioIoConfigEvent_audio_io_config_event(_event)); |
| 511 | sp<AudioIoDescriptor> ioDesc( |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 512 | VALUE_OR_RETURN_BINDER_STATUS( |
| 513 | aidl2legacy_AudioIoDescriptor_AudioIoDescriptor(_ioDesc))); |
Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 514 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 515 | ALOGV("ioConfigChanged() event %d", event); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 516 | |
Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 517 | if (ioDesc->mIoHandle == AUDIO_IO_HANDLE_NONE) return Status::ok(); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 518 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 519 | audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 520 | std::vector<sp<AudioDeviceCallback>> callbacksToCall; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 521 | { |
| 522 | Mutex::Autolock _l(mLock); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 523 | auto callbacks = std::map<audio_port_handle_t, wp<AudioDeviceCallback>>(); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 524 | |
| 525 | switch (event) { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 526 | case AUDIO_OUTPUT_OPENED: |
| 527 | case AUDIO_OUTPUT_REGISTERED: |
| 528 | case AUDIO_INPUT_OPENED: |
| 529 | case AUDIO_INPUT_REGISTERED: { |
| 530 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle); |
| 531 | if (oldDesc == 0) { |
| 532 | mIoDescriptors.add(ioDesc->mIoHandle, ioDesc); |
| 533 | } else { |
| 534 | deviceId = oldDesc->getDeviceId(); |
| 535 | mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc); |
| 536 | } |
| 537 | |
| 538 | if (ioDesc->getDeviceId() != AUDIO_PORT_HANDLE_NONE) { |
| 539 | deviceId = ioDesc->getDeviceId(); |
| 540 | if (event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED) { |
| 541 | auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle); |
| 542 | if (it != mAudioDeviceCallbacks.end()) { |
| 543 | callbacks = it->second; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | ALOGV("ioConfigChanged() new %s %s %d samplingRate %u, format %#x channel mask %#x " |
| 548 | "frameCount %zu deviceId %d", |
| 549 | event == AUDIO_OUTPUT_OPENED || event == AUDIO_OUTPUT_REGISTERED ? |
| 550 | "output" : "input", |
| 551 | event == AUDIO_OUTPUT_OPENED || event == AUDIO_INPUT_OPENED ? |
| 552 | "opened" : "registered", |
| 553 | ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, |
| 554 | ioDesc->mChannelMask, |
| 555 | ioDesc->mFrameCount, ioDesc->getDeviceId()); |
| 556 | } |
| 557 | break; |
| 558 | case AUDIO_OUTPUT_CLOSED: |
| 559 | case AUDIO_INPUT_CLOSED: { |
| 560 | if (getIoDescriptor_l(ioDesc->mIoHandle) == 0) { |
| 561 | ALOGW("ioConfigChanged() closing unknown %s %d", |
| 562 | event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle); |
| 563 | break; |
| 564 | } |
| 565 | ALOGV("ioConfigChanged() %s %d closed", |
| 566 | event == AUDIO_OUTPUT_CLOSED ? "output" : "input", ioDesc->mIoHandle); |
| 567 | |
| 568 | mIoDescriptors.removeItem(ioDesc->mIoHandle); |
| 569 | mAudioDeviceCallbacks.erase(ioDesc->mIoHandle); |
| 570 | } |
| 571 | break; |
| 572 | |
| 573 | case AUDIO_OUTPUT_CONFIG_CHANGED: |
| 574 | case AUDIO_INPUT_CONFIG_CHANGED: { |
| 575 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle); |
| 576 | if (oldDesc == 0) { |
| 577 | ALOGW("ioConfigChanged() modifying unknown %s! %d", |
| 578 | event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", |
| 579 | ioDesc->mIoHandle); |
| 580 | break; |
| 581 | } |
| 582 | |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 583 | deviceId = oldDesc->getDeviceId(); |
| 584 | mIoDescriptors.replaceValueFor(ioDesc->mIoHandle, ioDesc); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 585 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 586 | if (deviceId != ioDesc->getDeviceId()) { |
| 587 | deviceId = ioDesc->getDeviceId(); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 588 | auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle); |
| 589 | if (it != mAudioDeviceCallbacks.end()) { |
| 590 | callbacks = it->second; |
Francois Gaffie | 24a9fb0 | 2019-01-18 17:51:34 +0100 | [diff] [blame] | 591 | } |
| 592 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 593 | ALOGV("ioConfigChanged() new config for %s %d samplingRate %u, format %#x " |
| 594 | "channel mask %#x frameCount %zu frameCountHAL %zu deviceId %d", |
| 595 | event == AUDIO_OUTPUT_CONFIG_CHANGED ? "output" : "input", |
| 596 | ioDesc->mIoHandle, ioDesc->mSamplingRate, ioDesc->mFormat, |
| 597 | ioDesc->mChannelMask, ioDesc->mFrameCount, ioDesc->mFrameCountHAL, |
| 598 | ioDesc->getDeviceId()); |
| 599 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 600 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 601 | break; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 602 | case AUDIO_CLIENT_STARTED: { |
| 603 | sp<AudioIoDescriptor> oldDesc = getIoDescriptor_l(ioDesc->mIoHandle); |
| 604 | if (oldDesc == 0) { |
| 605 | ALOGW("ioConfigChanged() start client on unknown io! %d", ioDesc->mIoHandle); |
| 606 | break; |
| 607 | } |
| 608 | ALOGV("ioConfigChanged() AUDIO_CLIENT_STARTED io %d port %d num callbacks %zu", |
| 609 | ioDesc->mIoHandle, ioDesc->mPortId, mAudioDeviceCallbacks.size()); |
| 610 | oldDesc->mPatch = ioDesc->mPatch; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 611 | auto it = mAudioDeviceCallbacks.find(ioDesc->mIoHandle); |
| 612 | if (it != mAudioDeviceCallbacks.end()) { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 613 | auto cbks = it->second; |
| 614 | auto it2 = cbks.find(ioDesc->mPortId); |
| 615 | if (it2 != cbks.end()) { |
| 616 | callbacks.emplace(ioDesc->mPortId, it2->second); |
| 617 | deviceId = oldDesc->getDeviceId(); |
| 618 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 619 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 620 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 621 | break; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | for (auto wpCbk : callbacks) { |
| 625 | sp<AudioDeviceCallback> spCbk = wpCbk.second.promote(); |
| 626 | if (spCbk != nullptr) { |
| 627 | callbacksToCall.push_back(spCbk); |
| 628 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 629 | } |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | // Callbacks must be called without mLock held. May lead to dead lock if calling for |
| 633 | // example getRoutedDevice that updates the device and tries to acquire mLock. |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 634 | for (auto cb : callbacksToCall) { |
| 635 | // If callbacksToCall is not empty, it implies ioDesc->mIoHandle and deviceId are valid |
| 636 | cb->onAudioDeviceUpdate(ioDesc->mIoHandle, deviceId); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 637 | } |
Ytai Ben-Tsvi | 10dc0a6 | 2020-09-18 11:31:55 -0700 | [diff] [blame] | 638 | |
| 639 | return Status::ok(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 642 | status_t AudioSystem::AudioFlingerClient::getInputBufferSize( |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 643 | uint32_t sampleRate, audio_format_t format, |
| 644 | audio_channel_mask_t channelMask, size_t* buffSize) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 645 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 646 | if (af == 0) { |
| 647 | return PERMISSION_DENIED; |
| 648 | } |
| 649 | Mutex::Autolock _l(mLock); |
| 650 | // Do we have a stale mInBuffSize or are we requesting the input buffer size for new values |
| 651 | if ((mInBuffSize == 0) || (sampleRate != mInSamplingRate) || (format != mInFormat) |
| 652 | || (channelMask != mInChannelMask)) { |
| 653 | size_t inBuffSize = af->getInputBufferSize(sampleRate, format, channelMask); |
| 654 | if (inBuffSize == 0) { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 655 | ALOGE("AudioSystem::getInputBufferSize failed sampleRate %d format %#x channelMask %#x", |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 656 | sampleRate, format, channelMask); |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 657 | return BAD_VALUE; |
| 658 | } |
| 659 | // A benign race is possible here: we could overwrite a fresher cache entry |
| 660 | // save the request params |
| 661 | mInSamplingRate = sampleRate; |
| 662 | mInFormat = format; |
| 663 | mInChannelMask = channelMask; |
| 664 | |
| 665 | mInBuffSize = inBuffSize; |
| 666 | } |
| 667 | |
| 668 | *buffSize = mInBuffSize; |
| 669 | |
| 670 | return NO_ERROR; |
| 671 | } |
| 672 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 673 | sp<AudioIoDescriptor> |
| 674 | AudioSystem::AudioFlingerClient::getIoDescriptor_l(audio_io_handle_t ioHandle) { |
Eric Laurent | 73e26b6 | 2015-04-27 16:55:58 -0700 | [diff] [blame] | 675 | sp<AudioIoDescriptor> desc; |
| 676 | ssize_t index = mIoDescriptors.indexOfKey(ioHandle); |
| 677 | if (index >= 0) { |
| 678 | desc = mIoDescriptors.valueAt(index); |
| 679 | } |
| 680 | return desc; |
| 681 | } |
| 682 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 683 | sp<AudioIoDescriptor> AudioSystem::AudioFlingerClient::getIoDescriptor(audio_io_handle_t ioHandle) { |
Praveen Chavan | 49fdeaf | 2015-09-29 02:25:47 -0700 | [diff] [blame] | 684 | Mutex::Autolock _l(mLock); |
| 685 | return getIoDescriptor_l(ioHandle); |
| 686 | } |
| 687 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 688 | status_t AudioSystem::AudioFlingerClient::addAudioDeviceCallback( |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 689 | const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 690 | audio_port_handle_t portId) { |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 691 | ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId); |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame] | 692 | Mutex::Autolock _l(mLock); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 693 | auto& callbacks = mAudioDeviceCallbacks.emplace( |
| 694 | audioIo, |
| 695 | std::map<audio_port_handle_t, wp<AudioDeviceCallback>>()).first->second; |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 696 | auto result = callbacks.try_emplace(portId, callback); |
| 697 | if (!result.second) { |
| 698 | return INVALID_OPERATION; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 699 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 700 | return NO_ERROR; |
| 701 | } |
| 702 | |
| 703 | status_t AudioSystem::AudioFlingerClient::removeAudioDeviceCallback( |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 704 | const wp<AudioDeviceCallback>& callback __unused, audio_io_handle_t audioIo, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 705 | audio_port_handle_t portId) { |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 706 | ALOGV("%s audioIo %d portId %d", __func__, audioIo, portId); |
Eric Laurent | 4463ff5 | 2019-02-07 13:56:09 -0800 | [diff] [blame] | 707 | Mutex::Autolock _l(mLock); |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 708 | auto it = mAudioDeviceCallbacks.find(audioIo); |
| 709 | if (it == mAudioDeviceCallbacks.end()) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 710 | return INVALID_OPERATION; |
| 711 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 712 | if (it->second.erase(portId) == 0) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 713 | return INVALID_OPERATION; |
| 714 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 715 | if (it->second.size() == 0) { |
| 716 | mAudioDeviceCallbacks.erase(audioIo); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 717 | } |
| 718 | return NO_ERROR; |
| 719 | } |
| 720 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 721 | /* static */ uintptr_t AudioSystem::addErrorCallback(audio_error_callback cb) { |
Ytai Ben-Tsvi | 000c3e4 | 2020-01-09 15:26:40 -0800 | [diff] [blame] | 722 | Mutex::Autolock _l(gLockErrorCallbacks); |
| 723 | gAudioErrorCallbacks.insert(cb); |
| 724 | return reinterpret_cast<uintptr_t>(cb); |
| 725 | } |
| 726 | |
| 727 | /* static */ void AudioSystem::removeErrorCallback(uintptr_t cb) { |
| 728 | Mutex::Autolock _l(gLockErrorCallbacks); |
| 729 | gAudioErrorCallbacks.erase(reinterpret_cast<audio_error_callback>(cb)); |
| 730 | } |
| 731 | |
| 732 | /* static */ void AudioSystem::reportError(status_t err) { |
| 733 | Mutex::Autolock _l(gLockErrorCallbacks); |
| 734 | for (auto callback : gAudioErrorCallbacks) { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 735 | callback(err); |
Ytai Ben-Tsvi | 000c3e4 | 2020-01-09 15:26:40 -0800 | [diff] [blame] | 736 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 739 | /*static*/ void AudioSystem::setDynPolicyCallback(dynamic_policy_callback cb) { |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 740 | Mutex::Autolock _l(gLock); |
| 741 | gDynPolicyCallback = cb; |
| 742 | } |
| 743 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 744 | /*static*/ void AudioSystem::setRecordConfigCallback(record_config_callback cb) { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 745 | Mutex::Autolock _l(gLock); |
| 746 | gRecordConfigCallback = cb; |
| 747 | } |
| 748 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 749 | /*static*/ void AudioSystem::setRoutingCallback(routing_callback cb) { |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 750 | Mutex::Autolock _l(gLock); |
| 751 | gRoutingCallback = cb; |
| 752 | } |
| 753 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 754 | // client singleton for AudioPolicyService binder interface |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 755 | // protected by gLockAPS |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 756 | sp<IAudioPolicyService> AudioSystem::gAudioPolicyService; |
| 757 | sp<AudioSystem::AudioPolicyServiceClient> AudioSystem::gAudioPolicyServiceClient; |
| 758 | |
| 759 | |
Glenn Kasten | 18a6d90 | 2012-09-24 11:27:56 -0700 | [diff] [blame] | 760 | // establish binder interface to AudioPolicy service |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 761 | const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service() { |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 762 | sp<IAudioPolicyService> ap; |
| 763 | sp<AudioPolicyServiceClient> apc; |
| 764 | { |
| 765 | Mutex::Autolock _l(gLockAPS); |
| 766 | if (gAudioPolicyService == 0) { |
| 767 | sp<IServiceManager> sm = defaultServiceManager(); |
| 768 | sp<IBinder> binder; |
| 769 | do { |
| 770 | binder = sm->getService(String16("media.audio_policy")); |
| 771 | if (binder != 0) |
| 772 | break; |
| 773 | ALOGW("AudioPolicyService not published, waiting..."); |
| 774 | usleep(500000); // 0.5 s |
| 775 | } while (true); |
| 776 | if (gAudioPolicyServiceClient == NULL) { |
| 777 | gAudioPolicyServiceClient = new AudioPolicyServiceClient(); |
| 778 | } |
| 779 | binder->linkToDeath(gAudioPolicyServiceClient); |
| 780 | gAudioPolicyService = interface_cast<IAudioPolicyService>(binder); |
| 781 | LOG_ALWAYS_FATAL_IF(gAudioPolicyService == 0); |
| 782 | apc = gAudioPolicyServiceClient; |
Eric Laurent | fb00fc7 | 2017-05-25 18:17:12 -0700 | [diff] [blame] | 783 | // Make sure callbacks can be received by gAudioPolicyServiceClient |
| 784 | ProcessState::self()->startThreadPool(); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 785 | } |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 786 | ap = gAudioPolicyService; |
| 787 | } |
| 788 | if (apc != 0) { |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 789 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 790 | ap->registerClient(apc); |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 791 | ap->setAudioPortCallbacksEnabled(apc->isAudioPortCbEnabled()); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 792 | ap->setAudioVolumeGroupCallbacksEnabled(apc->isAudioVolumeGroupCbEnabled()); |
François Gaffie | 2443760 | 2018-04-23 15:08:59 +0200 | [diff] [blame] | 793 | IPCThreadState::self()->restoreCallingIdentity(token); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 794 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 795 | |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 796 | return ap; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Glenn Kasten | fb1fdc9 | 2013-07-10 17:03:19 -0700 | [diff] [blame] | 799 | // --------------------------------------------------------------------------- |
| 800 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 801 | void AudioSystem::onNewAudioModulesAvailable() { |
Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 802 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 803 | if (aps == 0) return; |
| 804 | aps->onNewAudioModulesAvailable(); |
| 805 | } |
| 806 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 807 | status_t AudioSystem::setDeviceConnectionState(audio_devices_t device, |
| 808 | audio_policy_dev_state_t state, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 809 | const char* device_address, |
| 810 | const char* device_name, |
| 811 | audio_format_t encodedFormat) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 812 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 813 | const char* address = ""; |
| 814 | const char* name = ""; |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 815 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 816 | if (aps == 0) return PERMISSION_DENIED; |
| 817 | |
Eric Laurent | 71b63e3 | 2011-09-02 14:20:56 -0700 | [diff] [blame] | 818 | if (device_address != NULL) { |
| 819 | address = device_address; |
| 820 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 821 | if (device_name != NULL) { |
| 822 | name = device_name; |
| 823 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 824 | |
| 825 | media::AudioDevice deviceAidl; |
| 826 | deviceAidl.type = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_devices_t_int32_t(device)); |
| 827 | deviceAidl.address = address; |
| 828 | |
| 829 | return statusTFromBinderStatus( |
| 830 | aps->setDeviceConnectionState( |
| 831 | deviceAidl, |
| 832 | VALUE_OR_RETURN_STATUS( |
| 833 | legacy2aidl_audio_policy_dev_state_t_AudioPolicyDeviceState(state)), |
| 834 | name, |
| 835 | VALUE_OR_RETURN_STATUS(legacy2aidl_audio_format_t_AudioFormat(encodedFormat)))); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 838 | audio_policy_dev_state_t AudioSystem::getDeviceConnectionState(audio_devices_t device, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 839 | const char* device_address) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 840 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 841 | if (aps == 0) return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 842 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 843 | auto result = [&]() -> ConversionResult<audio_policy_dev_state_t> { |
| 844 | media::AudioDevice deviceAidl; |
| 845 | deviceAidl.type = VALUE_OR_RETURN(legacy2aidl_audio_devices_t_int32_t(device)); |
| 846 | deviceAidl.address = device_address; |
| 847 | |
| 848 | media::AudioPolicyDeviceState result; |
| 849 | RETURN_IF_ERROR(statusTFromBinderStatus( |
| 850 | aps->getDeviceConnectionState(deviceAidl, &result))); |
| 851 | |
| 852 | return aidl2legacy_AudioPolicyDeviceState_audio_policy_dev_state_t(result); |
| 853 | }(); |
| 854 | return result.value_or(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 857 | status_t AudioSystem::handleDeviceConfigChange(audio_devices_t device, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 858 | const char* device_address, |
| 859 | const char* device_name, |
| 860 | audio_format_t encodedFormat) { |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 861 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 862 | const char* address = ""; |
| 863 | const char* name = ""; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 864 | |
| 865 | if (aps == 0) return PERMISSION_DENIED; |
| 866 | |
| 867 | if (device_address != NULL) { |
| 868 | address = device_address; |
| 869 | } |
| 870 | if (device_name != NULL) { |
| 871 | name = device_name; |
| 872 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 873 | |
| 874 | media::AudioDevice deviceAidl; |
| 875 | deviceAidl.type = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_devices_t_int32_t(device)); |
| 876 | deviceAidl.address = address; |
| 877 | |
| 878 | return statusTFromBinderStatus( |
| 879 | aps->handleDeviceConfigChange(deviceAidl, name, VALUE_OR_RETURN_STATUS( |
| 880 | legacy2aidl_audio_format_t_AudioFormat(encodedFormat)))); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 881 | } |
| 882 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 883 | status_t AudioSystem::setPhoneState(audio_mode_t state, uid_t uid) { |
Glenn Kasten | 347966c | 2012-01-18 14:58:32 -0800 | [diff] [blame] | 884 | if (uint32_t(state) >= AUDIO_MODE_CNT) return BAD_VALUE; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 885 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 886 | if (aps == 0) return PERMISSION_DENIED; |
| 887 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 888 | return statusTFromBinderStatus(aps->setPhoneState( |
| 889 | VALUE_OR_RETURN_STATUS(legacy2aidl_audio_mode_t_AudioMode(state)), |
| 890 | VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid)))); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 893 | status_t |
| 894 | 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] | 895 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 896 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 897 | |
| 898 | return statusTFromBinderStatus( |
| 899 | aps->setForceUse( |
| 900 | VALUE_OR_RETURN_STATUS( |
| 901 | legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage)), |
| 902 | VALUE_OR_RETURN_STATUS( |
| 903 | legacy2aidl_audio_policy_forced_cfg_t_AudioPolicyForcedConfig( |
| 904 | config)))); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 907 | 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] | 908 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 909 | if (aps == 0) return AUDIO_POLICY_FORCE_NONE; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 910 | |
| 911 | auto result = [&]() -> ConversionResult<audio_policy_forced_cfg_t> { |
| 912 | media::AudioPolicyForceUse usageAidl = VALUE_OR_RETURN( |
| 913 | legacy2aidl_audio_policy_force_use_t_AudioPolicyForceUse(usage)); |
| 914 | media::AudioPolicyForcedConfig configAidl; |
| 915 | RETURN_IF_ERROR(statusTFromBinderStatus( |
| 916 | aps->getForceUse(usageAidl, &configAidl))); |
| 917 | return aidl2legacy_AudioPolicyForcedConfig_audio_policy_forced_cfg_t(configAidl); |
| 918 | }(); |
| 919 | |
| 920 | return result.value_or(AUDIO_POLICY_FORCE_NONE); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 924 | audio_io_handle_t AudioSystem::getOutput(audio_stream_type_t stream) { |
Eric Laurent | 1a9ed11 | 2012-03-20 18:36:01 -0700 | [diff] [blame] | 925 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 926 | if (aps == 0) return AUDIO_IO_HANDLE_NONE; |
| 927 | |
| 928 | auto result = [&]() -> ConversionResult<audio_io_handle_t> { |
| 929 | media::AudioStreamType streamAidl = VALUE_OR_RETURN( |
| 930 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 931 | int32_t outputAidl; |
| 932 | RETURN_IF_ERROR( |
| 933 | statusTFromBinderStatus(aps->getOutput(streamAidl, &outputAidl))); |
| 934 | return aidl2legacy_int32_t_audio_io_handle_t(outputAidl); |
| 935 | }(); |
| 936 | |
| 937 | return result.value_or(AUDIO_IO_HANDLE_NONE); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 940 | status_t AudioSystem::getOutputForAttr(audio_attributes_t* attr, |
| 941 | audio_io_handle_t* output, |
| 942 | audio_session_t session, |
| 943 | audio_stream_type_t* stream, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame^] | 944 | const Identity& identity, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 945 | const audio_config_t* config, |
| 946 | audio_output_flags_t flags, |
| 947 | audio_port_handle_t* selectedDeviceId, |
| 948 | audio_port_handle_t* portId, |
| 949 | std::vector<audio_io_handle_t>* secondaryOutputs) { |
| 950 | if (attr == nullptr) { |
| 951 | ALOGE("%s NULL audio attributes", __func__); |
| 952 | return BAD_VALUE; |
| 953 | } |
| 954 | if (output == nullptr) { |
| 955 | ALOGE("%s NULL output - shouldn't happen", __func__); |
| 956 | return BAD_VALUE; |
| 957 | } |
| 958 | if (selectedDeviceId == nullptr) { |
| 959 | ALOGE("%s NULL selectedDeviceId - shouldn't happen", __func__); |
| 960 | return BAD_VALUE; |
| 961 | } |
| 962 | if (portId == nullptr) { |
| 963 | ALOGE("%s NULL portId - shouldn't happen", __func__); |
| 964 | return BAD_VALUE; |
| 965 | } |
| 966 | if (secondaryOutputs == nullptr) { |
| 967 | ALOGE("%s NULL secondaryOutputs - shouldn't happen", __func__); |
| 968 | return BAD_VALUE; |
| 969 | } |
| 970 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 971 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 972 | if (aps == 0) return NO_INIT; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 973 | |
| 974 | media::AudioAttributesInternal attrAidl = VALUE_OR_RETURN_STATUS( |
| 975 | legacy2aidl_audio_attributes_t_AudioAttributesInternal(*attr)); |
| 976 | int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session)); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 977 | media::AudioConfig configAidl = VALUE_OR_RETURN_STATUS( |
| 978 | legacy2aidl_audio_config_t_AudioConfig(*config)); |
| 979 | int32_t flagsAidl = VALUE_OR_RETURN_STATUS( |
| 980 | legacy2aidl_audio_output_flags_t_int32_t_mask(flags)); |
Eric Laurent | f99edd3 | 2021-02-01 15:57:33 +0100 | [diff] [blame] | 981 | int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS( |
| 982 | legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId)); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 983 | |
| 984 | media::GetOutputForAttrResponse responseAidl; |
| 985 | |
| 986 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame^] | 987 | aps->getOutputForAttr(attrAidl, sessionAidl, identity, configAidl, flagsAidl, |
Eric Laurent | f99edd3 | 2021-02-01 15:57:33 +0100 | [diff] [blame] | 988 | selectedDeviceIdAidl, &responseAidl))); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 989 | |
| 990 | *output = VALUE_OR_RETURN_STATUS( |
| 991 | aidl2legacy_int32_t_audio_io_handle_t(responseAidl.output)); |
| 992 | |
| 993 | if (stream != nullptr) { |
| 994 | *stream = VALUE_OR_RETURN_STATUS( |
| 995 | aidl2legacy_AudioStreamType_audio_stream_type_t(responseAidl.stream)); |
| 996 | } |
| 997 | *selectedDeviceId = VALUE_OR_RETURN_STATUS( |
| 998 | aidl2legacy_int32_t_audio_port_handle_t(responseAidl.selectedDeviceId)); |
| 999 | *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(responseAidl.portId)); |
| 1000 | *secondaryOutputs = VALUE_OR_RETURN_STATUS(convertContainer<std::vector<audio_io_handle_t>>( |
| 1001 | responseAidl.secondaryOutputs, aidl2legacy_int32_t_audio_io_handle_t)); |
| 1002 | |
| 1003 | return OK; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1006 | status_t AudioSystem::startOutput(audio_port_handle_t portId) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1007 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1008 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1009 | |
| 1010 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1011 | return statusTFromBinderStatus(aps->startOutput(portIdAidl)); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1014 | status_t AudioSystem::stopOutput(audio_port_handle_t portId) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1015 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1016 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1017 | |
| 1018 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1019 | return statusTFromBinderStatus(aps->stopOutput(portIdAidl)); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1022 | void AudioSystem::releaseOutput(audio_port_handle_t portId) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1023 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1024 | if (aps == 0) return; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1025 | |
| 1026 | auto status = [&]() -> status_t { |
| 1027 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS( |
| 1028 | legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1029 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseOutput(portIdAidl))); |
| 1030 | return OK; |
| 1031 | }(); |
| 1032 | |
| 1033 | // Ignore status. |
| 1034 | (void) status; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1037 | status_t AudioSystem::getInputForAttr(const audio_attributes_t* attr, |
| 1038 | audio_io_handle_t* input, |
| 1039 | audio_unique_id_t riid, |
| 1040 | audio_session_t session, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame^] | 1041 | const Identity &identity, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1042 | const audio_config_base_t* config, |
| 1043 | audio_input_flags_t flags, |
| 1044 | audio_port_handle_t* selectedDeviceId, |
| 1045 | audio_port_handle_t* portId) { |
| 1046 | if (attr == NULL) { |
| 1047 | ALOGE("getInputForAttr NULL attr - shouldn't happen"); |
| 1048 | return BAD_VALUE; |
| 1049 | } |
| 1050 | if (input == NULL) { |
| 1051 | ALOGE("getInputForAttr NULL input - shouldn't happen"); |
| 1052 | return BAD_VALUE; |
| 1053 | } |
| 1054 | if (selectedDeviceId == NULL) { |
| 1055 | ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen"); |
| 1056 | return BAD_VALUE; |
| 1057 | } |
| 1058 | if (portId == NULL) { |
| 1059 | ALOGE("getInputForAttr NULL portId - shouldn't happen"); |
| 1060 | return BAD_VALUE; |
| 1061 | } |
| 1062 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1063 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1064 | if (aps == 0) return NO_INIT; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1065 | |
| 1066 | media::AudioAttributesInternal attrAidl = VALUE_OR_RETURN_STATUS( |
| 1067 | legacy2aidl_audio_attributes_t_AudioAttributesInternal(*attr)); |
| 1068 | int32_t inputAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(*input)); |
| 1069 | int32_t riidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_unique_id_t_int32_t(riid)); |
| 1070 | int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session)); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1071 | media::AudioConfigBase configAidl = VALUE_OR_RETURN_STATUS( |
| 1072 | legacy2aidl_audio_config_base_t_AudioConfigBase(*config)); |
| 1073 | int32_t flagsAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_input_flags_t_int32_t_mask(flags)); |
Eric Laurent | f99edd3 | 2021-02-01 15:57:33 +0100 | [diff] [blame] | 1074 | int32_t selectedDeviceIdAidl = VALUE_OR_RETURN_STATUS( |
| 1075 | legacy2aidl_audio_port_handle_t_int32_t(*selectedDeviceId)); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1076 | |
| 1077 | media::GetInputForAttrResponse response; |
| 1078 | |
| 1079 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame^] | 1080 | aps->getInputForAttr(attrAidl, inputAidl, riidAidl, sessionAidl, identity, |
| 1081 | configAidl, flagsAidl, selectedDeviceIdAidl, &response))); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1082 | |
| 1083 | *input = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(response.input)); |
| 1084 | *selectedDeviceId = VALUE_OR_RETURN_STATUS( |
| 1085 | aidl2legacy_int32_t_audio_port_handle_t(response.selectedDeviceId)); |
| 1086 | *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(response.portId)); |
| 1087 | |
| 1088 | return OK; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1091 | status_t AudioSystem::startInput(audio_port_handle_t portId) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1092 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1093 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1094 | |
| 1095 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1096 | return statusTFromBinderStatus(aps->startInput(portIdAidl)); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1099 | status_t AudioSystem::stopInput(audio_port_handle_t portId) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1100 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1101 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1102 | |
| 1103 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1104 | return statusTFromBinderStatus(aps->stopInput(portIdAidl)); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1107 | void AudioSystem::releaseInput(audio_port_handle_t portId) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1108 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1109 | if (aps == 0) return; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1110 | |
| 1111 | auto status = [&]() -> status_t { |
| 1112 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS( |
| 1113 | legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1114 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(aps->releaseInput(portIdAidl))); |
| 1115 | return OK; |
| 1116 | }(); |
| 1117 | |
| 1118 | // Ignore status. |
| 1119 | (void) status; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 1122 | status_t AudioSystem::initStreamVolume(audio_stream_type_t stream, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1123 | int indexMin, |
| 1124 | int indexMax) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1125 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1126 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1127 | |
| 1128 | media::AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS( |
| 1129 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1130 | int32_t indexMinAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMin)); |
| 1131 | int32_t indexMaxAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(indexMax)); |
| 1132 | return statusTFromBinderStatus( |
| 1133 | aps->initStreamVolume(streamAidl, indexMinAidl, indexMaxAidl)); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1136 | status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, |
| 1137 | int index, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1138 | audio_devices_t device) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1139 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1140 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1141 | |
| 1142 | media::AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS( |
| 1143 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1144 | int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index)); |
| 1145 | int32_t deviceAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_devices_t_int32_t(device)); |
| 1146 | return statusTFromBinderStatus( |
| 1147 | aps->setStreamVolumeIndex(streamAidl, deviceAidl, indexAidl)); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 1150 | status_t AudioSystem::getStreamVolumeIndex(audio_stream_type_t stream, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1151 | int* index, |
| 1152 | audio_devices_t device) { |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1153 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1154 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1155 | |
| 1156 | media::AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS( |
| 1157 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1158 | int32_t deviceAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_devices_t_int32_t(device)); |
| 1159 | int32_t indexAidl; |
| 1160 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1161 | aps->getStreamVolumeIndex(streamAidl, deviceAidl, &indexAidl))); |
| 1162 | if (index != nullptr) { |
| 1163 | *index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl)); |
| 1164 | } |
| 1165 | return OK; |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1168 | status_t AudioSystem::setVolumeIndexForAttributes(const audio_attributes_t& attr, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1169 | int index, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1170 | audio_devices_t device) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1171 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1172 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1173 | |
| 1174 | media::AudioAttributesInternal attrAidl = VALUE_OR_RETURN_STATUS( |
| 1175 | legacy2aidl_audio_attributes_t_AudioAttributesInternal(attr)); |
| 1176 | int32_t indexAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(index)); |
| 1177 | int32_t deviceAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_devices_t_int32_t(device)); |
| 1178 | return statusTFromBinderStatus( |
| 1179 | aps->setVolumeIndexForAttributes(attrAidl, deviceAidl, indexAidl)); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1180 | } |
| 1181 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1182 | status_t AudioSystem::getVolumeIndexForAttributes(const audio_attributes_t& attr, |
| 1183 | int& index, |
| 1184 | audio_devices_t device) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1185 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1186 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1187 | |
| 1188 | media::AudioAttributesInternal attrAidl = VALUE_OR_RETURN_STATUS( |
| 1189 | legacy2aidl_audio_attributes_t_AudioAttributesInternal(attr)); |
| 1190 | int32_t deviceAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_devices_t_int32_t(device)); |
| 1191 | int32_t indexAidl; |
| 1192 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1193 | aps->getVolumeIndexForAttributes(attrAidl, deviceAidl, &indexAidl))); |
| 1194 | index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl)); |
| 1195 | return OK; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1196 | } |
| 1197 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1198 | status_t AudioSystem::getMaxVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1199 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1200 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1201 | |
| 1202 | media::AudioAttributesInternal attrAidl = VALUE_OR_RETURN_STATUS( |
| 1203 | legacy2aidl_audio_attributes_t_AudioAttributesInternal(attr)); |
| 1204 | int32_t indexAidl; |
| 1205 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1206 | aps->getMaxVolumeIndexForAttributes(attrAidl, &indexAidl))); |
| 1207 | index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl)); |
| 1208 | return OK; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1209 | } |
| 1210 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1211 | status_t AudioSystem::getMinVolumeIndexForAttributes(const audio_attributes_t& attr, int& index) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1212 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1213 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1214 | |
| 1215 | media::AudioAttributesInternal attrAidl = VALUE_OR_RETURN_STATUS( |
| 1216 | legacy2aidl_audio_attributes_t_AudioAttributesInternal(attr)); |
| 1217 | int32_t indexAidl; |
| 1218 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1219 | aps->getMinVolumeIndexForAttributes(attrAidl, &indexAidl))); |
| 1220 | index = VALUE_OR_RETURN_STATUS(convertIntegral<int>(indexAidl)); |
| 1221 | return OK; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1222 | } |
| 1223 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1224 | product_strategy_t AudioSystem::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1225 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1226 | if (aps == 0) return PRODUCT_STRATEGY_NONE; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1227 | |
| 1228 | auto result = [&]() -> ConversionResult<product_strategy_t> { |
| 1229 | media::AudioStreamType streamAidl = VALUE_OR_RETURN( |
| 1230 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1231 | int32_t resultAidl; |
| 1232 | RETURN_IF_ERROR(statusTFromBinderStatus( |
| 1233 | aps->getStrategyForStream(streamAidl, &resultAidl))); |
| 1234 | return aidl2legacy_int32_t_product_strategy_t(resultAidl); |
| 1235 | }(); |
| 1236 | return result.value_or(PRODUCT_STRATEGY_NONE); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1239 | audio_devices_t AudioSystem::getDevicesForStream(audio_stream_type_t stream) { |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 1240 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Glenn Kasten | 45faf7e | 2014-01-17 10:23:01 -0800 | [diff] [blame] | 1241 | if (aps == 0) return AUDIO_DEVICE_NONE; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1242 | |
| 1243 | auto result = [&]() -> ConversionResult<audio_devices_t> { |
| 1244 | media::AudioStreamType streamAidl = VALUE_OR_RETURN( |
| 1245 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1246 | int32_t resultAidl; |
| 1247 | RETURN_IF_ERROR(statusTFromBinderStatus( |
| 1248 | aps->getDevicesForStream(streamAidl, &resultAidl))); |
| 1249 | return aidl2legacy_int32_t_audio_devices_t(resultAidl); |
| 1250 | }(); |
| 1251 | return result.value_or(AUDIO_DEVICE_NONE); |
Glenn Kasten | 6b2718c | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 1252 | } |
| 1253 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1254 | status_t AudioSystem::getDevicesForAttributes(const AudioAttributes& aa, |
| 1255 | AudioDeviceTypeAddrVector* devices) { |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1256 | if (devices == nullptr) { |
| 1257 | return BAD_VALUE; |
| 1258 | } |
| 1259 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1260 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1261 | |
| 1262 | media::AudioAttributesEx aaAidl = VALUE_OR_RETURN_STATUS( |
| 1263 | legacy2aidl_AudioAttributes_AudioAttributesEx(aa)); |
| 1264 | std::vector<media::AudioDevice> retAidl; |
| 1265 | RETURN_STATUS_IF_ERROR( |
| 1266 | statusTFromBinderStatus(aps->getDevicesForAttributes(aaAidl, &retAidl))); |
| 1267 | *devices = VALUE_OR_RETURN_STATUS( |
| 1268 | convertContainer<AudioDeviceTypeAddrVector>( |
| 1269 | retAidl, |
| 1270 | aidl2legacy_AudioDeviceTypeAddress)); |
| 1271 | return OK; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1272 | } |
| 1273 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1274 | audio_io_handle_t AudioSystem::getOutputForEffect(const effect_descriptor_t* desc) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1275 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Glenn Kasten | efa6ea9 | 2014-01-08 09:10:43 -0800 | [diff] [blame] | 1276 | // FIXME change return type to status_t, and return PERMISSION_DENIED here |
Glenn Kasten | 142f519 | 2014-03-25 17:44:59 -0700 | [diff] [blame] | 1277 | if (aps == 0) return AUDIO_IO_HANDLE_NONE; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1278 | |
| 1279 | auto result = [&]() -> ConversionResult<audio_io_handle_t> { |
| 1280 | media::EffectDescriptor descAidl = VALUE_OR_RETURN( |
| 1281 | legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc)); |
| 1282 | int32_t retAidl; |
| 1283 | RETURN_IF_ERROR( |
| 1284 | statusTFromBinderStatus(aps->getOutputForEffect(descAidl, &retAidl))); |
| 1285 | return aidl2legacy_int32_t_audio_io_handle_t(retAidl); |
| 1286 | }(); |
| 1287 | |
| 1288 | return result.value_or(AUDIO_IO_HANDLE_NONE); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1291 | status_t AudioSystem::registerEffect(const effect_descriptor_t* desc, |
| 1292 | audio_io_handle_t io, |
| 1293 | product_strategy_t strategy, |
| 1294 | audio_session_t session, |
| 1295 | int id) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1296 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1297 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1298 | |
| 1299 | media::EffectDescriptor descAidl = VALUE_OR_RETURN_STATUS( |
| 1300 | legacy2aidl_effect_descriptor_t_EffectDescriptor(*desc)); |
| 1301 | int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io)); |
| 1302 | int32_t strategyAidl = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_product_strategy_t(strategy)); |
| 1303 | int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session)); |
| 1304 | int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id)); |
| 1305 | return statusTFromBinderStatus( |
| 1306 | aps->registerEffect(descAidl, ioAidl, strategyAidl, sessionAidl, idAidl)); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1309 | status_t AudioSystem::unregisterEffect(int id) { |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1310 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1311 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1312 | |
| 1313 | int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id)); |
| 1314 | return statusTFromBinderStatus( |
| 1315 | aps->unregisterEffect(idAidl)); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1318 | status_t AudioSystem::setEffectEnabled(int id, bool enabled) { |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1319 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1320 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1321 | |
| 1322 | int32_t idAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(id)); |
| 1323 | return statusTFromBinderStatus( |
| 1324 | aps->setEffectEnabled(idAidl, enabled)); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1327 | status_t AudioSystem::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1328 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1329 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1330 | |
| 1331 | std::vector<int32_t> idsAidl = VALUE_OR_RETURN_STATUS( |
| 1332 | convertContainer<std::vector<int32_t>>(ids, convertReinterpret<int32_t, int>)); |
| 1333 | int32_t ioAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_io_handle_t_int32_t(io)); |
| 1334 | return statusTFromBinderStatus(aps->moveEffectsToIo(idsAidl, ioAidl)); |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1337 | status_t AudioSystem::isStreamActive(audio_stream_type_t stream, bool* state, uint32_t inPastMs) { |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1338 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1339 | if (aps == 0) return PERMISSION_DENIED; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 1340 | if (state == NULL) return BAD_VALUE; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1341 | |
| 1342 | media::AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS( |
| 1343 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1344 | int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs)); |
| 1345 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1346 | aps->isStreamActive(streamAidl, inPastMsAidl, state))); |
| 1347 | return OK; |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 1350 | status_t AudioSystem::isStreamActiveRemotely(audio_stream_type_t stream, bool* state, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1351 | uint32_t inPastMs) { |
Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 1352 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1353 | if (aps == 0) return PERMISSION_DENIED; |
| 1354 | if (state == NULL) return BAD_VALUE; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1355 | |
| 1356 | media::AudioStreamType streamAidl = VALUE_OR_RETURN_STATUS( |
| 1357 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1358 | int32_t inPastMsAidl = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(inPastMs)); |
| 1359 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1360 | aps->isStreamActiveRemotely(streamAidl, inPastMsAidl, state))); |
| 1361 | return OK; |
Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 1362 | } |
| 1363 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1364 | status_t AudioSystem::isSourceActive(audio_source_t stream, bool* state) { |
Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 1365 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1366 | if (aps == 0) return PERMISSION_DENIED; |
| 1367 | if (state == NULL) return BAD_VALUE; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1368 | |
| 1369 | media::AudioSourceType streamAidl = VALUE_OR_RETURN_STATUS( |
| 1370 | legacy2aidl_audio_source_t_AudioSourceType(stream)); |
| 1371 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1372 | aps->isSourceActive(streamAidl, state))); |
| 1373 | return OK; |
Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1376 | uint32_t AudioSystem::getPrimaryOutputSamplingRate() { |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1377 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1378 | if (af == 0) return 0; |
| 1379 | return af->getPrimaryOutputSamplingRate(); |
| 1380 | } |
| 1381 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1382 | size_t AudioSystem::getPrimaryOutputFrameCount() { |
Glenn Kasten | cc0f1cf | 2012-09-24 11:27:18 -0700 | [diff] [blame] | 1383 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1384 | if (af == 0) return 0; |
| 1385 | return af->getPrimaryOutputFrameCount(); |
| 1386 | } |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1387 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1388 | status_t AudioSystem::setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) { |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1389 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1390 | if (af == 0) return PERMISSION_DENIED; |
Andy Hung | 6f248bb | 2018-01-23 14:04:37 -0800 | [diff] [blame] | 1391 | return af->setLowRamDevice(isLowRamDevice, totalMemory); |
Glenn Kasten | 4182c4e | 2013-07-15 14:45:07 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1394 | void AudioSystem::clearAudioConfigCache() { |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1395 | // called by restoreTrack_l(), which needs new IAudioFlinger and IAudioPolicyService instances |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1396 | ALOGV("clearAudioConfigCache()"); |
Eric Laurent | f6778fd | 2014-11-18 17:26:58 -0800 | [diff] [blame] | 1397 | { |
| 1398 | Mutex::Autolock _l(gLock); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1399 | if (gAudioFlingerClient != 0) { |
| 1400 | gAudioFlingerClient->clearIoCache(); |
| 1401 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 1402 | gAudioFlinger.clear(); |
| 1403 | } |
| 1404 | { |
| 1405 | Mutex::Autolock _l(gLockAPS); |
| 1406 | gAudioPolicyService.clear(); |
| 1407 | } |
Eric Laurent | 9f6530f | 2011-08-30 10:18:54 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 1410 | status_t AudioSystem::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) { |
| 1411 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1412 | if (aps == nullptr) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1413 | |
| 1414 | std::vector<media::AudioUsage> systemUsagesAidl = VALUE_OR_RETURN_STATUS( |
| 1415 | convertContainer<std::vector<media::AudioUsage>>(systemUsages, |
| 1416 | legacy2aidl_audio_usage_t_AudioUsage)); |
| 1417 | return statusTFromBinderStatus(aps->setSupportedSystemUsages(systemUsagesAidl)); |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1420 | status_t AudioSystem::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) { |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1421 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1422 | if (aps == nullptr) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1423 | |
| 1424 | int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid)); |
| 1425 | int32_t capturePolicyAidl = VALUE_OR_RETURN_STATUS( |
| 1426 | legacy2aidl_audio_flags_mask_t_int32_t_mask(capturePolicy)); |
| 1427 | return statusTFromBinderStatus(aps->setAllowedCapturePolicy(uidAidl, capturePolicyAidl)); |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1430 | audio_offload_mode_t AudioSystem::getOffloadSupport(const audio_offload_info_t& info) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 1431 | ALOGV("%s", __func__); |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1432 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 1433 | if (aps == 0) return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1434 | |
| 1435 | auto result = [&]() -> ConversionResult<audio_offload_mode_t> { |
| 1436 | media::AudioOffloadInfo infoAidl = VALUE_OR_RETURN( |
| 1437 | legacy2aidl_audio_offload_info_t_AudioOffloadInfo(info)); |
| 1438 | media::AudioOffloadMode retAidl; |
| 1439 | RETURN_IF_ERROR( |
| 1440 | statusTFromBinderStatus(aps->getOffloadSupport(infoAidl, &retAidl))); |
| 1441 | return aidl2legacy_AudioOffloadMode_audio_offload_mode_t(retAidl); |
| 1442 | }(); |
| 1443 | |
| 1444 | return result.value_or(static_cast<audio_offload_mode_t>(0)); |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1447 | status_t AudioSystem::listAudioPorts(audio_port_role_t role, |
| 1448 | audio_port_type_t type, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1449 | unsigned int* num_ports, |
| 1450 | struct audio_port_v7* ports, |
| 1451 | unsigned int* generation) { |
| 1452 | if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) || |
| 1453 | generation == nullptr) { |
| 1454 | return BAD_VALUE; |
| 1455 | } |
| 1456 | |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1457 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1458 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1459 | |
| 1460 | media::AudioPortRole roleAidl = VALUE_OR_RETURN_STATUS( |
| 1461 | legacy2aidl_audio_port_role_t_AudioPortRole(role)); |
| 1462 | media::AudioPortType typeAidl = VALUE_OR_RETURN_STATUS( |
| 1463 | legacy2aidl_audio_port_type_t_AudioPortType(type)); |
| 1464 | media::Int numPortsAidl; |
| 1465 | numPortsAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_ports)); |
| 1466 | std::vector<media::AudioPort> portsAidl; |
| 1467 | int32_t generationAidl; |
| 1468 | |
| 1469 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1470 | aps->listAudioPorts(roleAidl, typeAidl, &numPortsAidl, &portsAidl, &generationAidl))); |
| 1471 | *num_ports = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPortsAidl.value)); |
| 1472 | *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl)); |
| 1473 | RETURN_STATUS_IF_ERROR(convertRange(portsAidl.begin(), portsAidl.end(), ports, |
| 1474 | aidl2legacy_AudioPort_audio_port_v7)); |
| 1475 | return OK; |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1478 | status_t AudioSystem::getAudioPort(struct audio_port_v7* port) { |
| 1479 | if (port == nullptr) { |
| 1480 | return BAD_VALUE; |
| 1481 | } |
| 1482 | |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1483 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1484 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1485 | |
| 1486 | media::AudioPort portAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_v7_AudioPort(*port)); |
| 1487 | RETURN_STATUS_IF_ERROR( |
| 1488 | statusTFromBinderStatus(aps->getAudioPort(portAidl, &portAidl))); |
| 1489 | *port = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioPort_audio_port_v7(portAidl)); |
| 1490 | return OK; |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1493 | status_t AudioSystem::createAudioPatch(const struct audio_patch* patch, |
| 1494 | audio_patch_handle_t* handle) { |
| 1495 | if (patch == nullptr || handle == nullptr) { |
| 1496 | return BAD_VALUE; |
| 1497 | } |
| 1498 | |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1499 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1500 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1501 | |
| 1502 | media::AudioPatch patchAidl = VALUE_OR_RETURN_STATUS( |
| 1503 | legacy2aidl_audio_patch_AudioPatch(*patch)); |
| 1504 | int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(*handle)); |
| 1505 | RETURN_STATUS_IF_ERROR( |
| 1506 | statusTFromBinderStatus(aps->createAudioPatch(patchAidl, handleAidl, &handleAidl))); |
| 1507 | *handle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_patch_handle_t(handleAidl)); |
| 1508 | return OK; |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1511 | status_t AudioSystem::releaseAudioPatch(audio_patch_handle_t handle) { |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1512 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1513 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1514 | |
| 1515 | int32_t handleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_patch_handle_t_int32_t(handle)); |
| 1516 | return statusTFromBinderStatus(aps->releaseAudioPatch(handleAidl)); |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1519 | status_t AudioSystem::listAudioPatches(unsigned int* num_patches, |
| 1520 | struct audio_patch* patches, |
| 1521 | unsigned int* generation) { |
| 1522 | if (num_patches == nullptr || (*num_patches != 0 && patches == nullptr) || |
| 1523 | generation == nullptr) { |
| 1524 | return BAD_VALUE; |
| 1525 | } |
| 1526 | |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1527 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1528 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1529 | |
| 1530 | |
| 1531 | media::Int numPatchesAidl; |
| 1532 | numPatchesAidl.value = VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*num_patches)); |
| 1533 | std::vector<media::AudioPatch> patchesAidl; |
| 1534 | int32_t generationAidl; |
| 1535 | |
| 1536 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1537 | aps->listAudioPatches(&numPatchesAidl, &patchesAidl, &generationAidl))); |
| 1538 | *num_patches = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(numPatchesAidl.value)); |
| 1539 | *generation = VALUE_OR_RETURN_STATUS(convertIntegral<unsigned int>(generationAidl)); |
| 1540 | RETURN_STATUS_IF_ERROR(convertRange(patchesAidl.begin(), patchesAidl.end(), patches, |
| 1541 | aidl2legacy_AudioPatch_audio_patch)); |
| 1542 | return OK; |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1545 | status_t AudioSystem::setAudioPortConfig(const struct audio_port_config* config) { |
| 1546 | if (config == nullptr) { |
| 1547 | return BAD_VALUE; |
| 1548 | } |
| 1549 | |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1550 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1551 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1552 | |
| 1553 | media::AudioPortConfig configAidl = VALUE_OR_RETURN_STATUS( |
| 1554 | legacy2aidl_audio_port_config_AudioPortConfig(*config)); |
| 1555 | return statusTFromBinderStatus(aps->setAudioPortConfig(configAidl)); |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1558 | status_t AudioSystem::addAudioPortCallback(const sp<AudioPortCallback>& callback) { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1559 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1560 | if (aps == 0) return PERMISSION_DENIED; |
| 1561 | |
| 1562 | Mutex::Autolock _l(gLockAPS); |
| 1563 | if (gAudioPolicyServiceClient == 0) { |
| 1564 | return NO_INIT; |
| 1565 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1566 | int ret = gAudioPolicyServiceClient->addAudioPortCallback(callback); |
| 1567 | if (ret == 1) { |
| 1568 | aps->setAudioPortCallbacksEnabled(true); |
| 1569 | } |
| 1570 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1573 | /*static*/ |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1574 | status_t AudioSystem::removeAudioPortCallback(const sp<AudioPortCallback>& callback) { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1575 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1576 | if (aps == 0) return PERMISSION_DENIED; |
| 1577 | |
| 1578 | Mutex::Autolock _l(gLockAPS); |
| 1579 | if (gAudioPolicyServiceClient == 0) { |
| 1580 | return NO_INIT; |
| 1581 | } |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 1582 | int ret = gAudioPolicyServiceClient->removeAudioPortCallback(callback); |
| 1583 | if (ret == 0) { |
| 1584 | aps->setAudioPortCallbacksEnabled(false); |
| 1585 | } |
| 1586 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1589 | status_t AudioSystem::addAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1590 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1591 | if (aps == 0) return PERMISSION_DENIED; |
| 1592 | |
| 1593 | Mutex::Autolock _l(gLockAPS); |
| 1594 | if (gAudioPolicyServiceClient == 0) { |
| 1595 | return NO_INIT; |
| 1596 | } |
| 1597 | int ret = gAudioPolicyServiceClient->addAudioVolumeGroupCallback(callback); |
| 1598 | if (ret == 1) { |
| 1599 | aps->setAudioVolumeGroupCallbacksEnabled(true); |
| 1600 | } |
| 1601 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
| 1602 | } |
| 1603 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1604 | status_t AudioSystem::removeAudioVolumeGroupCallback(const sp<AudioVolumeGroupCallback>& callback) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 1605 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1606 | if (aps == 0) return PERMISSION_DENIED; |
| 1607 | |
| 1608 | Mutex::Autolock _l(gLockAPS); |
| 1609 | if (gAudioPolicyServiceClient == 0) { |
| 1610 | return NO_INIT; |
| 1611 | } |
| 1612 | int ret = gAudioPolicyServiceClient->removeAudioVolumeGroupCallback(callback); |
| 1613 | if (ret == 0) { |
| 1614 | aps->setAudioVolumeGroupCallbacksEnabled(false); |
| 1615 | } |
| 1616 | return (ret < 0) ? INVALID_OPERATION : NO_ERROR; |
| 1617 | } |
| 1618 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1619 | status_t AudioSystem::addAudioDeviceCallback( |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1620 | const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1621 | audio_port_handle_t portId) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1622 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 1623 | if (afc == 0) { |
| 1624 | return NO_INIT; |
| 1625 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1626 | status_t status = afc->addAudioDeviceCallback(callback, audioIo, portId); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1627 | if (status == NO_ERROR) { |
| 1628 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1629 | if (af != 0) { |
| 1630 | af->registerClient(afc); |
| 1631 | } |
| 1632 | } |
| 1633 | return status; |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | status_t AudioSystem::removeAudioDeviceCallback( |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1637 | const wp<AudioDeviceCallback>& callback, audio_io_handle_t audioIo, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1638 | audio_port_handle_t portId) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1639 | const sp<AudioFlingerClient> afc = getAudioFlingerClient(); |
| 1640 | if (afc == 0) { |
| 1641 | return NO_INIT; |
| 1642 | } |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1643 | return afc->removeAudioDeviceCallback(callback, audioIo, portId); |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1646 | audio_port_handle_t AudioSystem::getDeviceIdForIo(audio_io_handle_t audioIo) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 1647 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1648 | if (af == 0) return PERMISSION_DENIED; |
| 1649 | const sp<AudioIoDescriptor> desc = getIoDescriptor(audioIo); |
| 1650 | if (desc == 0) { |
| 1651 | return AUDIO_PORT_HANDLE_NONE; |
| 1652 | } |
| 1653 | return desc->getDeviceId(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 1654 | } |
| 1655 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1656 | status_t AudioSystem::acquireSoundTriggerSession(audio_session_t* session, |
| 1657 | audio_io_handle_t* ioHandle, |
| 1658 | audio_devices_t* device) { |
| 1659 | if (session == nullptr || ioHandle == nullptr || device == nullptr) { |
| 1660 | return BAD_VALUE; |
| 1661 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1662 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1663 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1664 | |
| 1665 | media::SoundTriggerSession retAidl; |
| 1666 | RETURN_STATUS_IF_ERROR( |
| 1667 | statusTFromBinderStatus(aps->acquireSoundTriggerSession(&retAidl))); |
| 1668 | *session = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_session_t(retAidl.session)); |
| 1669 | *ioHandle = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_io_handle_t(retAidl.ioHandle)); |
| 1670 | *device = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_devices_t(retAidl.device)); |
| 1671 | return OK; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1674 | status_t AudioSystem::releaseSoundTriggerSession(audio_session_t session) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1675 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1676 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1677 | |
| 1678 | int32_t sessionAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_session_t_int32_t(session)); |
| 1679 | return statusTFromBinderStatus(aps->releaseSoundTriggerSession(sessionAidl)); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1680 | } |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1681 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1682 | audio_mode_t AudioSystem::getPhoneState() { |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1683 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1684 | if (aps == 0) return AUDIO_MODE_INVALID; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1685 | |
| 1686 | auto result = [&]() -> ConversionResult<audio_mode_t> { |
| 1687 | media::AudioMode retAidl; |
| 1688 | RETURN_IF_ERROR(statusTFromBinderStatus(aps->getPhoneState(&retAidl))); |
| 1689 | return aidl2legacy_AudioMode_audio_mode_t(retAidl); |
| 1690 | }(); |
| 1691 | |
| 1692 | return result.value_or(AUDIO_MODE_INVALID); |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1693 | } |
| 1694 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1695 | status_t AudioSystem::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) { |
Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 1696 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1697 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1698 | |
| 1699 | size_t mixesSize = std::min(mixes.size(), size_t{MAX_MIXES_PER_POLICY}); |
| 1700 | std::vector<media::AudioMix> mixesAidl; |
| 1701 | RETURN_STATUS_IF_ERROR( |
| 1702 | convertRange(mixes.begin(), mixes.begin() + mixesSize, std::back_inserter(mixesAidl), |
| 1703 | legacy2aidl_AudioMix)); |
| 1704 | return statusTFromBinderStatus(aps->registerPolicyMixes(mixesAidl, registration)); |
Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 1705 | } |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 1706 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1707 | status_t AudioSystem::setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 1708 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1709 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1710 | |
| 1711 | int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid)); |
| 1712 | std::vector<media::AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS( |
| 1713 | convertContainer<std::vector<media::AudioDevice>>(devices, |
| 1714 | legacy2aidl_AudioDeviceTypeAddress)); |
| 1715 | return statusTFromBinderStatus(aps->setUidDeviceAffinities(uidAidl, devicesAidl)); |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | status_t AudioSystem::removeUidDeviceAffinities(uid_t uid) { |
| 1719 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1720 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1721 | |
| 1722 | int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid)); |
| 1723 | return statusTFromBinderStatus(aps->removeUidDeviceAffinities(uidAidl)); |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 1724 | } |
| 1725 | |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 1726 | status_t AudioSystem::setUserIdDeviceAffinities(int userId, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1727 | const AudioDeviceTypeAddrVector& devices) { |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 1728 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1729 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1730 | |
| 1731 | int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId)); |
| 1732 | std::vector<media::AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS( |
| 1733 | convertContainer<std::vector<media::AudioDevice>>(devices, |
| 1734 | legacy2aidl_AudioDeviceTypeAddress)); |
| 1735 | return statusTFromBinderStatus( |
| 1736 | aps->setUserIdDeviceAffinities(userIdAidl, devicesAidl)); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 1737 | } |
| 1738 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1739 | status_t AudioSystem::removeUserIdDeviceAffinities(int userId) { |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 1740 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1741 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1742 | int32_t userIdAidl = VALUE_OR_RETURN_STATUS(convertReinterpret<int32_t>(userId)); |
| 1743 | return statusTFromBinderStatus(aps->removeUserIdDeviceAffinities(userIdAidl)); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1746 | status_t AudioSystem::startAudioSource(const struct audio_port_config* source, |
| 1747 | const audio_attributes_t* attributes, |
| 1748 | audio_port_handle_t* portId) { |
| 1749 | if (source == nullptr || attributes == nullptr || portId == nullptr) { |
| 1750 | return BAD_VALUE; |
| 1751 | } |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1752 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1753 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1754 | |
| 1755 | media::AudioPortConfig sourceAidl = VALUE_OR_RETURN_STATUS( |
| 1756 | legacy2aidl_audio_port_config_AudioPortConfig(*source)); |
| 1757 | media::AudioAttributesInternal attributesAidl = VALUE_OR_RETURN_STATUS( |
| 1758 | legacy2aidl_audio_attributes_t_AudioAttributesInternal(*attributes)); |
| 1759 | int32_t portIdAidl; |
| 1760 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1761 | aps->startAudioSource(sourceAidl, attributesAidl, &portIdAidl))); |
| 1762 | *portId = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_audio_port_handle_t(portIdAidl)); |
| 1763 | return OK; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1766 | status_t AudioSystem::stopAudioSource(audio_port_handle_t portId) { |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1767 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1768 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1769 | |
| 1770 | int32_t portIdAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(portId)); |
| 1771 | return statusTFromBinderStatus(aps->stopAudioSource(portIdAidl)); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1772 | } |
| 1773 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1774 | status_t AudioSystem::setMasterMono(bool mono) { |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1775 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1776 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1777 | return statusTFromBinderStatus(aps->setMasterMono(mono)); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1778 | } |
| 1779 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1780 | status_t AudioSystem::getMasterMono(bool* mono) { |
| 1781 | if (mono == nullptr) { |
| 1782 | return BAD_VALUE; |
| 1783 | } |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1784 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1785 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1786 | return statusTFromBinderStatus(aps->getMasterMono(mono)); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1787 | } |
| 1788 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1789 | status_t AudioSystem::setMasterBalance(float balance) { |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1790 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1791 | if (af == 0) return PERMISSION_DENIED; |
| 1792 | return af->setMasterBalance(balance); |
| 1793 | } |
| 1794 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1795 | status_t AudioSystem::getMasterBalance(float* balance) { |
Richard Folke Tullberg | 3fae037 | 2017-01-13 09:04:25 +0100 | [diff] [blame] | 1796 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1797 | if (af == 0) return PERMISSION_DENIED; |
| 1798 | return af->getMasterBalance(balance); |
| 1799 | } |
| 1800 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1801 | float |
| 1802 | AudioSystem::getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device) { |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 1803 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1804 | if (aps == 0) return NAN; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1805 | |
| 1806 | auto result = [&]() -> ConversionResult<float> { |
| 1807 | media::AudioStreamType streamAidl = VALUE_OR_RETURN( |
| 1808 | legacy2aidl_audio_stream_type_t_AudioStreamType(stream)); |
| 1809 | int32_t indexAidl = VALUE_OR_RETURN(convertIntegral<int32_t>(index)); |
| 1810 | int32_t deviceAidl = VALUE_OR_RETURN(legacy2aidl_audio_devices_t_int32_t(device)); |
| 1811 | float retAidl; |
| 1812 | RETURN_IF_ERROR(statusTFromBinderStatus( |
| 1813 | aps->getStreamVolumeDB(streamAidl, indexAidl, deviceAidl, &retAidl))); |
| 1814 | return retAidl; |
| 1815 | }(); |
| 1816 | return result.value_or(NAN); |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1819 | status_t AudioSystem::getMicrophones(std::vector<media::MicrophoneInfo>* microphones) { |
jiabin | 46a76fa | 2018-01-05 10:18:21 -0800 | [diff] [blame] | 1820 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1821 | if (af == 0) return PERMISSION_DENIED; |
| 1822 | return af->getMicrophones(microphones); |
| 1823 | } |
| 1824 | |
Eric Laurent | 42896a0 | 2019-09-27 15:40:33 -0700 | [diff] [blame] | 1825 | status_t AudioSystem::setAudioHalPids(const std::vector<pid_t>& pids) { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1826 | const sp<IAudioFlinger>& af = AudioSystem::get_audio_flinger(); |
| 1827 | if (af == nullptr) return PERMISSION_DENIED; |
| 1828 | return af->setAudioHalPids(pids); |
Eric Laurent | 42896a0 | 2019-09-27 15:40:33 -0700 | [diff] [blame] | 1829 | } |
| 1830 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1831 | status_t AudioSystem::getSurroundFormats(unsigned int* numSurroundFormats, |
| 1832 | audio_format_t* surroundFormats, |
| 1833 | bool* surroundFormatsEnabled, |
| 1834 | bool reported) { |
| 1835 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && |
| 1836 | (surroundFormats == nullptr || |
| 1837 | surroundFormatsEnabled == nullptr))) { |
| 1838 | return BAD_VALUE; |
| 1839 | } |
| 1840 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 1841 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1842 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1843 | media::Int numSurroundFormatsAidl; |
| 1844 | numSurroundFormatsAidl.value = |
| 1845 | VALUE_OR_RETURN_STATUS(convertIntegral<int32_t>(*numSurroundFormats)); |
| 1846 | std::vector<media::audio::common::AudioFormat> surroundFormatsAidl; |
Kriti Dang | 877b27e | 2021-02-02 12:10:40 +0100 | [diff] [blame] | 1847 | std::vector<bool> surroundFormatsEnabledAidl; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1848 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
Kriti Dang | 877b27e | 2021-02-02 12:10:40 +0100 | [diff] [blame] | 1849 | aps->getSurroundFormats(reported, &numSurroundFormatsAidl, |
| 1850 | &surroundFormatsAidl, &surroundFormatsEnabledAidl))); |
| 1851 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1852 | *numSurroundFormats = VALUE_OR_RETURN_STATUS( |
| 1853 | convertIntegral<unsigned int>(numSurroundFormatsAidl.value)); |
| 1854 | RETURN_STATUS_IF_ERROR( |
| 1855 | convertRange(surroundFormatsAidl.begin(), surroundFormatsAidl.end(), surroundFormats, |
| 1856 | aidl2legacy_AudioFormat_audio_format_t)); |
Kriti Dang | 877b27e | 2021-02-02 12:10:40 +0100 | [diff] [blame] | 1857 | std::copy(surroundFormatsEnabledAidl.begin(), surroundFormatsEnabledAidl.end(), |
| 1858 | surroundFormatsEnabled); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1859 | return OK; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1862 | status_t AudioSystem::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 1863 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1864 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1865 | |
| 1866 | media::audio::common::AudioFormat audioFormatAidl = VALUE_OR_RETURN_STATUS( |
| 1867 | legacy2aidl_audio_format_t_AudioFormat(audioFormat)); |
| 1868 | return statusTFromBinderStatus( |
| 1869 | aps->setSurroundFormatEnabled(audioFormatAidl, enabled)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1872 | status_t AudioSystem::setAssistantUid(uid_t uid) { |
| 1873 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1874 | if (aps == 0) return PERMISSION_DENIED; |
| 1875 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1876 | int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid)); |
| 1877 | return statusTFromBinderStatus(aps->setAssistantUid(uidAidl)); |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1878 | } |
| 1879 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1880 | status_t AudioSystem::setA11yServicesUids(const std::vector<uid_t>& uids) { |
| 1881 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1882 | if (aps == 0) return PERMISSION_DENIED; |
| 1883 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1884 | std::vector<int32_t> uidsAidl = VALUE_OR_RETURN_STATUS( |
| 1885 | convertContainer<std::vector<int32_t>>(uids, legacy2aidl_uid_t_int32_t)); |
| 1886 | return statusTFromBinderStatus(aps->setA11yServicesUids(uidsAidl)); |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1889 | status_t AudioSystem::setCurrentImeUid(uid_t uid) { |
| 1890 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
Kohsuke Yatoh | a623a13 | 2020-03-24 20:10:26 -0700 | [diff] [blame] | 1891 | if (aps == 0) return PERMISSION_DENIED; |
| 1892 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1893 | int32_t uidAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(uid)); |
| 1894 | return statusTFromBinderStatus(aps->setCurrentImeUid(uidAidl)); |
Kohsuke Yatoh | a623a13 | 2020-03-24 20:10:26 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1897 | bool AudioSystem::isHapticPlaybackSupported() { |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 1898 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1899 | if (aps == 0) return false; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1900 | |
| 1901 | auto result = [&]() -> ConversionResult<bool> { |
| 1902 | bool retVal; |
| 1903 | RETURN_IF_ERROR( |
| 1904 | statusTFromBinderStatus(aps->isHapticPlaybackSupported(&retVal))); |
| 1905 | return retVal; |
| 1906 | }(); |
| 1907 | return result.value_or(false); |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 1910 | status_t AudioSystem::getHwOffloadEncodingFormatsSupportedForA2DP( |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1911 | std::vector<audio_format_t>* formats) { |
| 1912 | if (formats == nullptr) { |
| 1913 | return BAD_VALUE; |
| 1914 | } |
| 1915 | |
| 1916 | const sp<IAudioPolicyService> |
| 1917 | & aps = AudioSystem::get_audio_policy_service(); |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1918 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1919 | |
| 1920 | std::vector<media::audio::common::AudioFormat> formatsAidl; |
| 1921 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1922 | aps->getHwOffloadEncodingFormatsSupportedForA2DP(&formatsAidl))); |
| 1923 | *formats = VALUE_OR_RETURN_STATUS( |
| 1924 | convertContainer<std::vector<audio_format_t>>(formatsAidl, |
| 1925 | aidl2legacy_AudioFormat_audio_format_t)); |
| 1926 | return OK; |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1927 | } |
| 1928 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1929 | status_t AudioSystem::listAudioProductStrategies(AudioProductStrategyVector& strategies) { |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 1930 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 1931 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1932 | |
| 1933 | std::vector<media::AudioProductStrategy> strategiesAidl; |
| 1934 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1935 | aps->listAudioProductStrategies(&strategiesAidl))); |
| 1936 | strategies = VALUE_OR_RETURN_STATUS( |
| 1937 | convertContainer<AudioProductStrategyVector>(strategiesAidl, |
| 1938 | aidl2legacy_AudioProductStrategy)); |
| 1939 | return OK; |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1940 | } |
| 1941 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1942 | audio_attributes_t AudioSystem::streamTypeToAttributes(audio_stream_type_t stream) { |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1943 | AudioProductStrategyVector strategies; |
| 1944 | listAudioProductStrategies(strategies); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1945 | for (const auto& strategy : strategies) { |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1946 | auto attrVect = strategy.getAudioAttributes(); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1947 | auto iter = std::find_if(begin(attrVect), end(attrVect), [&stream](const auto& attributes) { |
| 1948 | return attributes.getStreamType() == stream; |
| 1949 | }); |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1950 | if (iter != end(attrVect)) { |
| 1951 | return iter->getAttributes(); |
| 1952 | } |
| 1953 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1954 | ALOGE("invalid stream type %s when converting to attributes", toString(stream).c_str()); |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1955 | return AUDIO_ATTRIBUTES_INITIALIZER; |
| 1956 | } |
| 1957 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1958 | audio_stream_type_t AudioSystem::attributesToStreamType(const audio_attributes_t& attr) { |
François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 1959 | product_strategy_t psId; |
| 1960 | status_t ret = AudioSystem::getProductStrategyFromAudioAttributes(AudioAttributes(attr), psId); |
| 1961 | if (ret != NO_ERROR) { |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1962 | ALOGE("no strategy found for attributes %s", toString(attr).c_str()); |
François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 1963 | return AUDIO_STREAM_MUSIC; |
| 1964 | } |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1965 | AudioProductStrategyVector strategies; |
| 1966 | listAudioProductStrategies(strategies); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1967 | for (const auto& strategy : strategies) { |
François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 1968 | if (strategy.getId() == psId) { |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1969 | auto attrVect = strategy.getAudioAttributes(); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1970 | auto iter = std::find_if(begin(attrVect), end(attrVect), [&attr](const auto& refAttr) { |
| 1971 | return AudioProductStrategy::attributesMatches( |
| 1972 | refAttr.getAttributes(), attr); |
| 1973 | }); |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1974 | if (iter != end(attrVect)) { |
| 1975 | return iter->getStreamType(); |
| 1976 | } |
| 1977 | } |
| 1978 | } |
Jean-Michel Trivi | ed67865 | 2019-12-19 13:39:30 -0800 | [diff] [blame] | 1979 | switch (attr.usage) { |
| 1980 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
| 1981 | // virtual source is not expected to have an associated product strategy |
| 1982 | break; |
| 1983 | default: |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1984 | ALOGE("invalid attributes %s when converting to stream", toString(attr).c_str()); |
Jean-Michel Trivi | ed67865 | 2019-12-19 13:39:30 -0800 | [diff] [blame] | 1985 | break; |
| 1986 | } |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1987 | return AUDIO_STREAM_MUSIC; |
| 1988 | } |
| 1989 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1990 | status_t AudioSystem::getProductStrategyFromAudioAttributes(const AudioAttributes& aa, |
Francois Gaffie | 11b6592 | 2020-09-24 16:59:08 +0200 | [diff] [blame] | 1991 | product_strategy_t& productStrategy, |
| 1992 | bool fallbackOnDefault) { |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1993 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 1994 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 1995 | |
| 1996 | media::AudioAttributesEx aaAidl = VALUE_OR_RETURN_STATUS( |
| 1997 | legacy2aidl_AudioAttributes_AudioAttributesEx(aa)); |
| 1998 | int32_t productStrategyAidl; |
| 1999 | |
| 2000 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
Francois Gaffie | 11b6592 | 2020-09-24 16:59:08 +0200 | [diff] [blame] | 2001 | aps->getProductStrategyFromAudioAttributes(aaAidl, fallbackOnDefault, |
| 2002 | &productStrategyAidl))); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2003 | productStrategy = VALUE_OR_RETURN_STATUS( |
| 2004 | aidl2legacy_int32_t_product_strategy_t(productStrategyAidl)); |
| 2005 | return OK; |
François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 2006 | } |
| 2007 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2008 | status_t AudioSystem::listAudioVolumeGroups(AudioVolumeGroupVector& groups) { |
François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 2009 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2010 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2011 | |
| 2012 | std::vector<media::AudioVolumeGroup> groupsAidl; |
| 2013 | RETURN_STATUS_IF_ERROR( |
| 2014 | statusTFromBinderStatus(aps->listAudioVolumeGroups(&groupsAidl))); |
| 2015 | groups = VALUE_OR_RETURN_STATUS( |
| 2016 | convertContainer<AudioVolumeGroupVector>(groupsAidl, aidl2legacy_AudioVolumeGroup)); |
| 2017 | return OK; |
François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 2018 | } |
| 2019 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2020 | status_t AudioSystem::getVolumeGroupFromAudioAttributes(const AudioAttributes& aa, |
Francois Gaffie | 11b6592 | 2020-09-24 16:59:08 +0200 | [diff] [blame] | 2021 | volume_group_t& volumeGroup, |
| 2022 | bool fallbackOnDefault) { |
François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 2023 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2024 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2025 | |
| 2026 | media::AudioAttributesEx aaAidl = VALUE_OR_RETURN_STATUS( |
| 2027 | legacy2aidl_AudioAttributes_AudioAttributesEx(aa)); |
| 2028 | int32_t volumeGroupAidl; |
| 2029 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
Francois Gaffie | 11b6592 | 2020-09-24 16:59:08 +0200 | [diff] [blame] | 2030 | aps->getVolumeGroupFromAudioAttributes(aaAidl, fallbackOnDefault, &volumeGroupAidl))); |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2031 | volumeGroup = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_volume_group_t(volumeGroupAidl)); |
| 2032 | return OK; |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 2033 | } |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 2034 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2035 | status_t AudioSystem::setRttEnabled(bool enabled) { |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 2036 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2037 | if (aps == 0) return PERMISSION_DENIED; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2038 | return statusTFromBinderStatus(aps->setRttEnabled(enabled)); |
Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 2039 | } |
| 2040 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2041 | bool AudioSystem::isCallScreenModeSupported() { |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 2042 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2043 | if (aps == 0) return false; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2044 | |
| 2045 | auto result = [&]() -> ConversionResult<bool> { |
| 2046 | bool retAidl; |
| 2047 | RETURN_IF_ERROR( |
| 2048 | statusTFromBinderStatus(aps->isCallScreenModeSupported(&retAidl))); |
| 2049 | return retAidl; |
| 2050 | }(); |
| 2051 | return result.value_or(false); |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 2052 | } |
| 2053 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 2054 | status_t AudioSystem::setDevicesRoleForStrategy(product_strategy_t strategy, |
| 2055 | device_role_t role, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2056 | const AudioDeviceTypeAddrVector& devices) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 2057 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2058 | if (aps == 0) { |
| 2059 | return PERMISSION_DENIED; |
| 2060 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2061 | |
| 2062 | int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy)); |
| 2063 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| 2064 | std::vector<media::AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS( |
| 2065 | convertContainer<std::vector<media::AudioDevice>>(devices, |
| 2066 | legacy2aidl_AudioDeviceTypeAddress)); |
| 2067 | return statusTFromBinderStatus( |
| 2068 | aps->setDevicesRoleForStrategy(strategyAidl, roleAidl, devicesAidl)); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 2069 | } |
| 2070 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2071 | status_t |
| 2072 | AudioSystem::removeDevicesRoleForStrategy(product_strategy_t strategy, device_role_t role) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 2073 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2074 | if (aps == 0) { |
| 2075 | return PERMISSION_DENIED; |
| 2076 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2077 | int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy)); |
| 2078 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| 2079 | return statusTFromBinderStatus( |
| 2080 | aps->removeDevicesRoleForStrategy(strategyAidl, roleAidl)); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 2081 | } |
| 2082 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 2083 | status_t AudioSystem::getDevicesForRoleAndStrategy(product_strategy_t strategy, |
| 2084 | device_role_t role, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2085 | AudioDeviceTypeAddrVector& devices) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 2086 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2087 | if (aps == 0) { |
| 2088 | return PERMISSION_DENIED; |
| 2089 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2090 | int32_t strategyAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_product_strategy_t_int32_t(strategy)); |
| 2091 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| 2092 | std::vector<media::AudioDevice> devicesAidl; |
| 2093 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 2094 | aps->getDevicesForRoleAndStrategy(strategyAidl, roleAidl, &devicesAidl))); |
| 2095 | devices = VALUE_OR_RETURN_STATUS( |
| 2096 | convertContainer<AudioDeviceTypeAddrVector>(devicesAidl, |
| 2097 | aidl2legacy_AudioDeviceTypeAddress)); |
| 2098 | return OK; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2101 | status_t AudioSystem::setDevicesRoleForCapturePreset(audio_source_t audioSource, |
| 2102 | device_role_t role, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2103 | const AudioDeviceTypeAddrVector& devices) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2104 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2105 | if (aps == 0) { |
| 2106 | return PERMISSION_DENIED; |
| 2107 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2108 | |
| 2109 | media::AudioSourceType audioSourceAidl = VALUE_OR_RETURN_STATUS( |
| 2110 | legacy2aidl_audio_source_t_AudioSourceType(audioSource)); |
| 2111 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| 2112 | std::vector<media::AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS( |
| 2113 | convertContainer<std::vector<media::AudioDevice>>(devices, |
| 2114 | legacy2aidl_AudioDeviceTypeAddress)); |
| 2115 | return statusTFromBinderStatus( |
| 2116 | aps->setDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl)); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | status_t AudioSystem::addDevicesRoleForCapturePreset(audio_source_t audioSource, |
| 2120 | device_role_t role, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2121 | const AudioDeviceTypeAddrVector& devices) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2122 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2123 | if (aps == 0) { |
| 2124 | return PERMISSION_DENIED; |
| 2125 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2126 | media::AudioSourceType audioSourceAidl = VALUE_OR_RETURN_STATUS( |
| 2127 | legacy2aidl_audio_source_t_AudioSourceType(audioSource)); |
| 2128 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| 2129 | std::vector<media::AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS( |
| 2130 | convertContainer<std::vector<media::AudioDevice>>(devices, |
| 2131 | legacy2aidl_AudioDeviceTypeAddress)); |
| 2132 | return statusTFromBinderStatus( |
| 2133 | aps->addDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl)); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | status_t AudioSystem::removeDevicesRoleForCapturePreset( |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2137 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2138 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2139 | if (aps == 0) { |
| 2140 | return PERMISSION_DENIED; |
| 2141 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2142 | media::AudioSourceType audioSourceAidl = VALUE_OR_RETURN_STATUS( |
| 2143 | legacy2aidl_audio_source_t_AudioSourceType(audioSource)); |
| 2144 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| 2145 | std::vector<media::AudioDevice> devicesAidl = VALUE_OR_RETURN_STATUS( |
| 2146 | convertContainer<std::vector<media::AudioDevice>>(devices, |
| 2147 | legacy2aidl_AudioDeviceTypeAddress)); |
| 2148 | return statusTFromBinderStatus( |
| 2149 | aps->removeDevicesRoleForCapturePreset(audioSourceAidl, roleAidl, devicesAidl)); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | status_t AudioSystem::clearDevicesRoleForCapturePreset(audio_source_t audioSource, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2153 | device_role_t role) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2154 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2155 | if (aps == 0) { |
| 2156 | return PERMISSION_DENIED; |
| 2157 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2158 | media::AudioSourceType audioSourceAidl = VALUE_OR_RETURN_STATUS( |
| 2159 | legacy2aidl_audio_source_t_AudioSourceType(audioSource)); |
| 2160 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| 2161 | return statusTFromBinderStatus( |
| 2162 | aps->clearDevicesRoleForCapturePreset(audioSourceAidl, roleAidl)); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | status_t AudioSystem::getDevicesForRoleAndCapturePreset(audio_source_t audioSource, |
| 2166 | device_role_t role, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2167 | AudioDeviceTypeAddrVector& devices) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2168 | const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service(); |
| 2169 | if (aps == 0) { |
| 2170 | return PERMISSION_DENIED; |
| 2171 | } |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2172 | media::AudioSourceType audioSourceAidl = VALUE_OR_RETURN_STATUS( |
| 2173 | legacy2aidl_audio_source_t_AudioSourceType(audioSource)); |
| 2174 | media::DeviceRole roleAidl = VALUE_OR_RETURN_STATUS(legacy2aidl_device_role_t_DeviceRole(role)); |
| 2175 | std::vector<media::AudioDevice> devicesAidl; |
| 2176 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 2177 | aps->getDevicesForRoleAndCapturePreset(audioSourceAidl, roleAidl, &devicesAidl))); |
| 2178 | devices = VALUE_OR_RETURN_STATUS( |
| 2179 | convertContainer<AudioDeviceTypeAddrVector>(devicesAidl, |
| 2180 | aidl2legacy_AudioDeviceTypeAddress)); |
| 2181 | return OK; |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2184 | class CaptureStateListenerImpl : public media::BnCaptureStateListener, |
| 2185 | public IBinder::DeathRecipient { |
| 2186 | public: |
Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2187 | CaptureStateListenerImpl( |
| 2188 | const sp<IAudioPolicyService>& aps, |
| 2189 | const sp<AudioSystem::CaptureStateListener>& listener) |
Ytai Ben-Tsvi | a46b6d3 | 2020-08-31 13:29:11 -0700 | [diff] [blame] | 2190 | : mAps(aps), mListener(listener) {} |
| 2191 | |
| 2192 | void init() { |
Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2193 | bool active; |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2194 | status_t status = statusTFromBinderStatus( |
| 2195 | mAps->registerSoundTriggerCaptureStateListener(this, &active)); |
Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2196 | if (status != NO_ERROR) { |
| 2197 | mListener->onServiceDied(); |
| 2198 | return; |
| 2199 | } |
| 2200 | mListener->onStateChanged(active); |
Ytai Ben-Tsvi | a46b6d3 | 2020-08-31 13:29:11 -0700 | [diff] [blame] | 2201 | IInterface::asBinder(mAps)->linkToDeath(this); |
Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2204 | binder::Status setCaptureState(bool active) override { |
| 2205 | Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock); |
Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2206 | mListener->onStateChanged(active); |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2207 | return binder::Status::ok(); |
| 2208 | } |
| 2209 | |
| 2210 | void binderDied(const wp<IBinder>&) override { |
| 2211 | Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock); |
Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2212 | mListener->onServiceDied(); |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2213 | gSoundTriggerCaptureStateListener = nullptr; |
| 2214 | } |
Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2215 | |
| 2216 | private: |
| 2217 | // Need this in order to keep the death receipent alive. |
| 2218 | sp<IAudioPolicyService> mAps; |
| 2219 | sp<AudioSystem::CaptureStateListener> mListener; |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2220 | }; |
| 2221 | |
| 2222 | status_t AudioSystem::registerSoundTriggerCaptureStateListener( |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2223 | const sp<CaptureStateListener>& listener) { |
Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2224 | LOG_ALWAYS_FATAL_IF(listener == nullptr); |
| 2225 | |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2226 | const sp<IAudioPolicyService>& aps = |
| 2227 | AudioSystem::get_audio_policy_service(); |
| 2228 | if (aps == 0) { |
| 2229 | return PERMISSION_DENIED; |
| 2230 | } |
| 2231 | |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2232 | Mutex::Autolock _l(gSoundTriggerCaptureStateListenerLock); |
Ytai Ben-Tsvi | d9f8283 | 2020-08-27 09:31:10 -0700 | [diff] [blame] | 2233 | gSoundTriggerCaptureStateListener = new CaptureStateListenerImpl(aps, listener); |
Ytai Ben-Tsvi | a46b6d3 | 2020-08-31 13:29:11 -0700 | [diff] [blame] | 2234 | gSoundTriggerCaptureStateListener->init(); |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2235 | |
Ytai Ben-Tsvi | 5120eda | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 2236 | return NO_ERROR; |
| 2237 | } |
| 2238 | |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2239 | // --------------------------------------------------------------------------- |
| 2240 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 2241 | int AudioSystem::AudioPolicyServiceClient::addAudioPortCallback( |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2242 | const sp<AudioPortCallback>& callback) { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2243 | Mutex::Autolock _l(mLock); |
| 2244 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2245 | if (mAudioPortCallbacks[i] == callback) { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 2246 | return -1; |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2247 | } |
| 2248 | } |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2249 | mAudioPortCallbacks.add(callback); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 2250 | return mAudioPortCallbacks.size(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2251 | } |
| 2252 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 2253 | int AudioSystem::AudioPolicyServiceClient::removeAudioPortCallback( |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2254 | const sp<AudioPortCallback>& callback) { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2255 | Mutex::Autolock _l(mLock); |
| 2256 | size_t i; |
| 2257 | for (i = 0; i < mAudioPortCallbacks.size(); i++) { |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2258 | if (mAudioPortCallbacks[i] == callback) { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2259 | break; |
| 2260 | } |
| 2261 | } |
| 2262 | if (i == mAudioPortCallbacks.size()) { |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 2263 | return -1; |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2264 | } |
| 2265 | mAudioPortCallbacks.removeAt(i); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 2266 | return mAudioPortCallbacks.size(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2267 | } |
| 2268 | |
Eric Laurent | 296fb13 | 2015-05-01 11:38:42 -0700 | [diff] [blame] | 2269 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2270 | Status AudioSystem::AudioPolicyServiceClient::onAudioPortListUpdate() { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2271 | Mutex::Autolock _l(mLock); |
| 2272 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 2273 | mAudioPortCallbacks[i]->onAudioPortListUpdate(); |
| 2274 | } |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2275 | return Status::ok(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2276 | } |
| 2277 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2278 | Status AudioSystem::AudioPolicyServiceClient::onAudioPatchListUpdate() { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2279 | Mutex::Autolock _l(mLock); |
| 2280 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 2281 | mAudioPortCallbacks[i]->onAudioPatchListUpdate(); |
| 2282 | } |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2283 | return Status::ok(); |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2286 | // ---------------------------------------------------------------------------- |
| 2287 | int AudioSystem::AudioPolicyServiceClient::addAudioVolumeGroupCallback( |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2288 | const sp<AudioVolumeGroupCallback>& callback) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2289 | Mutex::Autolock _l(mLock); |
| 2290 | for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) { |
| 2291 | if (mAudioVolumeGroupCallback[i] == callback) { |
| 2292 | return -1; |
| 2293 | } |
| 2294 | } |
| 2295 | mAudioVolumeGroupCallback.add(callback); |
| 2296 | return mAudioVolumeGroupCallback.size(); |
| 2297 | } |
| 2298 | |
| 2299 | int AudioSystem::AudioPolicyServiceClient::removeAudioVolumeGroupCallback( |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2300 | const sp<AudioVolumeGroupCallback>& callback) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2301 | Mutex::Autolock _l(mLock); |
| 2302 | size_t i; |
| 2303 | for (i = 0; i < mAudioVolumeGroupCallback.size(); i++) { |
| 2304 | if (mAudioVolumeGroupCallback[i] == callback) { |
| 2305 | break; |
| 2306 | } |
| 2307 | } |
| 2308 | if (i == mAudioVolumeGroupCallback.size()) { |
| 2309 | return -1; |
| 2310 | } |
| 2311 | mAudioVolumeGroupCallback.removeAt(i); |
| 2312 | return mAudioVolumeGroupCallback.size(); |
| 2313 | } |
| 2314 | |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2315 | Status AudioSystem::AudioPolicyServiceClient::onAudioVolumeGroupChanged(int32_t group, |
| 2316 | int32_t flags) { |
| 2317 | volume_group_t groupLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 2318 | aidl2legacy_int32_t_volume_group_t(group)); |
| 2319 | int flagsLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(flags)); |
| 2320 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2321 | Mutex::Autolock _l(mLock); |
| 2322 | for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) { |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2323 | mAudioVolumeGroupCallback[i]->onAudioVolumeGroupChanged(groupLegacy, flagsLegacy); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2324 | } |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2325 | return Status::ok(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2326 | } |
| 2327 | // ---------------------------------------------------------------------------- |
| 2328 | |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2329 | Status AudioSystem::AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate( |
| 2330 | const ::std::string& regId, int32_t state) { |
| 2331 | ALOGV("AudioPolicyServiceClient::onDynamicPolicyMixStateUpdate(%s, %d)", regId.c_str(), state); |
| 2332 | |
| 2333 | String8 regIdLegacy = VALUE_OR_RETURN_BINDER_STATUS(aidl2legacy_string_view_String8(regId)); |
| 2334 | int stateLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(state)); |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 2335 | dynamic_policy_callback cb = NULL; |
| 2336 | { |
| 2337 | Mutex::Autolock _l(AudioSystem::gLock); |
| 2338 | cb = gDynPolicyCallback; |
| 2339 | } |
| 2340 | |
| 2341 | if (cb != NULL) { |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2342 | cb(DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE, regIdLegacy, stateLegacy); |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 2343 | } |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2344 | return Status::ok(); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 2345 | } |
| 2346 | |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2347 | Status AudioSystem::AudioPolicyServiceClient::onRecordingConfigurationUpdate( |
| 2348 | int32_t event, |
| 2349 | const media::RecordClientInfo& clientInfo, |
| 2350 | const media::AudioConfigBase& clientConfig, |
| 2351 | const std::vector<media::EffectDescriptor>& clientEffects, |
| 2352 | const media::AudioConfigBase& deviceConfig, |
| 2353 | const std::vector<media::EffectDescriptor>& effects, |
| 2354 | int32_t patchHandle, |
| 2355 | media::AudioSourceType source) { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2356 | record_config_callback cb = NULL; |
| 2357 | { |
| 2358 | Mutex::Autolock _l(AudioSystem::gLock); |
| 2359 | cb = gRecordConfigCallback; |
| 2360 | } |
| 2361 | |
| 2362 | if (cb != NULL) { |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2363 | int eventLegacy = VALUE_OR_RETURN_BINDER_STATUS(convertReinterpret<int>(event)); |
| 2364 | record_client_info_t clientInfoLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 2365 | aidl2legacy_RecordClientInfo_record_client_info_t(clientInfo)); |
| 2366 | audio_config_base_t clientConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 2367 | aidl2legacy_AudioConfigBase_audio_config_base_t(clientConfig)); |
| 2368 | std::vector<effect_descriptor_t> clientEffectsLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 2369 | convertContainer<std::vector<effect_descriptor_t>>( |
| 2370 | clientEffects, |
| 2371 | aidl2legacy_EffectDescriptor_effect_descriptor_t)); |
| 2372 | audio_config_base_t deviceConfigLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 2373 | aidl2legacy_AudioConfigBase_audio_config_base_t(deviceConfig)); |
| 2374 | std::vector<effect_descriptor_t> effectsLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 2375 | convertContainer<std::vector<effect_descriptor_t>>( |
| 2376 | effects, |
| 2377 | aidl2legacy_EffectDescriptor_effect_descriptor_t)); |
| 2378 | audio_patch_handle_t patchHandleLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 2379 | aidl2legacy_int32_t_audio_patch_handle_t(patchHandle)); |
| 2380 | audio_source_t sourceLegacy = VALUE_OR_RETURN_BINDER_STATUS( |
| 2381 | aidl2legacy_AudioSourceType_audio_source_t(source)); |
| 2382 | cb(eventLegacy, &clientInfoLegacy, &clientConfigLegacy, clientEffectsLegacy, |
| 2383 | &deviceConfigLegacy, effectsLegacy, patchHandleLegacy, sourceLegacy); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2384 | } |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2385 | return Status::ok(); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2386 | } |
| 2387 | |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 2388 | Status AudioSystem::AudioPolicyServiceClient::onRoutingUpdated() { |
| 2389 | routing_callback cb = NULL; |
| 2390 | { |
| 2391 | Mutex::Autolock _l(AudioSystem::gLock); |
| 2392 | cb = gRoutingCallback; |
| 2393 | } |
| 2394 | |
| 2395 | if (cb != NULL) { |
| 2396 | cb(); |
| 2397 | } |
| 2398 | return Status::ok(); |
| 2399 | } |
| 2400 | |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2401 | void AudioSystem::AudioPolicyServiceClient::binderDied(const wp<IBinder>& who __unused) { |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 2402 | { |
Eric Laurent | b28753e | 2015-04-01 13:06:28 -0700 | [diff] [blame] | 2403 | Mutex::Autolock _l(mLock); |
| 2404 | for (size_t i = 0; i < mAudioPortCallbacks.size(); i++) { |
| 2405 | mAudioPortCallbacks[i]->onServiceDied(); |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 2406 | } |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2407 | for (size_t i = 0; i < mAudioVolumeGroupCallback.size(); i++) { |
| 2408 | mAudioVolumeGroupCallback[i]->onServiceDied(); |
| 2409 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2410 | } |
Glenn Kasten | d2d089f | 2014-11-05 11:48:12 -0800 | [diff] [blame] | 2411 | { |
| 2412 | Mutex::Autolock _l(gLockAPS); |
| 2413 | AudioSystem::gAudioPolicyService.clear(); |
| 2414 | } |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2415 | |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2416 | ALOGW("AudioPolicyService server died!"); |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2417 | } |
| 2418 | |
Ytai Ben-Tsvi | 7e7a79d | 2020-12-15 16:48:16 -0800 | [diff] [blame] | 2419 | ConversionResult<record_client_info_t> |
| 2420 | aidl2legacy_RecordClientInfo_record_client_info_t(const media::RecordClientInfo& aidl) { |
| 2421 | record_client_info_t legacy; |
| 2422 | legacy.riid = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_unique_id_t(aidl.riid)); |
| 2423 | legacy.uid = VALUE_OR_RETURN(aidl2legacy_int32_t_uid_t(aidl.uid)); |
| 2424 | legacy.session = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_session_t(aidl.session)); |
| 2425 | legacy.source = VALUE_OR_RETURN(aidl2legacy_AudioSourceType_audio_source_t(aidl.source)); |
| 2426 | legacy.port_id = VALUE_OR_RETURN(aidl2legacy_int32_t_audio_port_handle_t(aidl.portId)); |
| 2427 | legacy.silenced = aidl.silenced; |
| 2428 | return legacy; |
| 2429 | } |
| 2430 | |
| 2431 | ConversionResult<media::RecordClientInfo> |
| 2432 | legacy2aidl_record_client_info_t_RecordClientInfo(const record_client_info_t& legacy) { |
| 2433 | media::RecordClientInfo aidl; |
| 2434 | aidl.riid = VALUE_OR_RETURN(legacy2aidl_audio_unique_id_t_int32_t(legacy.riid)); |
| 2435 | aidl.uid = VALUE_OR_RETURN(legacy2aidl_uid_t_int32_t(legacy.uid)); |
| 2436 | aidl.session = VALUE_OR_RETURN(legacy2aidl_audio_session_t_int32_t(legacy.session)); |
| 2437 | aidl.source = VALUE_OR_RETURN(legacy2aidl_audio_source_t_AudioSourceType(legacy.source)); |
| 2438 | aidl.portId = VALUE_OR_RETURN(legacy2aidl_audio_port_handle_t_int32_t(legacy.port_id)); |
| 2439 | aidl.silenced = legacy.silenced; |
| 2440 | return aidl; |
| 2441 | } |
| 2442 | |
Glenn Kasten | 40bc906 | 2015-03-20 09:09:33 -0700 | [diff] [blame] | 2443 | } // namespace android |