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