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