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