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