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