Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | #ifndef ANDROID_AUDIOPOLICYSERVICE_H |
| 18 | #define ANDROID_AUDIOPOLICYSERVICE_H |
| 19 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 20 | #include <cutils/misc.h> |
| 21 | #include <cutils/config_utils.h> |
Mathias Agopian | e762be9 | 2013-05-09 16:26:45 -0700 | [diff] [blame] | 22 | #include <cutils/compiler.h> |
Glenn Kasten | d1d8f23 | 2011-07-08 09:34:50 -0700 | [diff] [blame] | 23 | #include <utils/String8.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 24 | #include <utils/Vector.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 25 | #include <utils/SortedVector.h> |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 26 | #include <binder/BinderService.h> |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 27 | #include <binder/IUidObserver.h> |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 28 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 29 | #include <system/audio_policy.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 30 | #include <media/IAudioPolicyService.h> |
| 31 | #include <media/ToneGenerator.h> |
| 32 | #include <media/AudioEffect.h> |
Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 33 | #include <media/AudioPolicy.h> |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 34 | #include "AudioPolicyEffects.h" |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 35 | #include "managerdefault/AudioPolicyManager.h" |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 36 | #include <android/hardware/BnSensorPrivacyListener.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 37 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 38 | #include <unordered_map> |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 39 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | namespace android { |
| 41 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 | // ---------------------------------------------------------------------------- |
| 43 | |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 44 | class AudioPolicyService : |
| 45 | public BinderService<AudioPolicyService>, |
| 46 | public BnAudioPolicyService, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 47 | public IBinder::DeathRecipient |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 48 | { |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 49 | friend class BinderService<AudioPolicyService>; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 50 | |
| 51 | public: |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 52 | // for BinderService |
Mathias Agopian | e762be9 | 2013-05-09 16:26:45 -0700 | [diff] [blame] | 53 | static const char *getServiceName() ANDROID_API { return "media.audio_policy"; } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 54 | |
| 55 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 56 | |
| 57 | // |
| 58 | // BnAudioPolicyService (see AudioPolicyInterface for method descriptions) |
| 59 | // |
| 60 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 61 | virtual status_t setDeviceConnectionState(audio_devices_t device, |
| 62 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 63 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 64 | const char *device_name, |
| 65 | audio_format_t encodedFormat); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 66 | virtual audio_policy_dev_state_t getDeviceConnectionState( |
| 67 | audio_devices_t device, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 68 | const char *device_address); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 69 | virtual status_t handleDeviceConfigChange(audio_devices_t device, |
| 70 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 71 | const char *device_name, |
| 72 | audio_format_t encodedFormat); |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 73 | virtual status_t setPhoneState(audio_mode_t state); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 74 | virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config); |
| 75 | virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage); |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 76 | virtual audio_io_handle_t getOutput(audio_stream_type_t stream); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame^] | 77 | status_t getOutputForAttr(const audio_attributes_t *attr, |
| 78 | audio_io_handle_t *output, |
| 79 | audio_session_t session, |
| 80 | audio_stream_type_t *stream, |
| 81 | pid_t pid, |
| 82 | uid_t uid, |
| 83 | const audio_config_t *config, |
| 84 | audio_output_flags_t flags, |
| 85 | audio_port_handle_t *selectedDeviceId, |
| 86 | audio_port_handle_t *portId, |
| 87 | std::vector<audio_io_handle_t> *secondaryOutputs) override; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 88 | virtual status_t startOutput(audio_port_handle_t portId); |
| 89 | virtual status_t stopOutput(audio_port_handle_t portId); |
| 90 | virtual void releaseOutput(audio_port_handle_t portId); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 91 | virtual status_t getInputForAttr(const audio_attributes_t *attr, |
| 92 | audio_io_handle_t *input, |
| 93 | audio_session_t session, |
Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 94 | pid_t pid, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 95 | uid_t uid, |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 96 | const String16& opPackageName, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 97 | const audio_config_base_t *config, |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 98 | audio_input_flags_t flags, |
Eric Laurent | 9ae8c59 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 99 | audio_port_handle_t *selectedDeviceId = NULL, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 100 | audio_port_handle_t *portId = NULL); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 101 | virtual status_t startInput(audio_port_handle_t portId); |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 102 | virtual status_t stopInput(audio_port_handle_t portId); |
| 103 | virtual void releaseInput(audio_port_handle_t portId); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 104 | virtual status_t initStreamVolume(audio_stream_type_t stream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 105 | int indexMin, |
| 106 | int indexMax); |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 107 | virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, |
| 108 | int index, |
| 109 | audio_devices_t device); |
| 110 | virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, |
| 111 | int *index, |
| 112 | audio_devices_t device); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 113 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 114 | virtual uint32_t getStrategyForStream(audio_stream_type_t stream); |
Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 115 | virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 116 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 117 | virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc); |
| 118 | virtual status_t registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 119 | audio_io_handle_t io, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 120 | uint32_t strategy, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 121 | audio_session_t session, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 122 | int id); |
| 123 | virtual status_t unregisterEffect(int id); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 124 | virtual status_t setEffectEnabled(int id, bool enabled); |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 125 | virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const; |
Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 126 | virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const; |
Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 127 | virtual bool isSourceActive(audio_source_t source) const; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 128 | |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 129 | virtual status_t queryDefaultPreProcessing(audio_session_t audioSession, |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 130 | effect_descriptor_t *descriptors, |
| 131 | uint32_t *count); |
Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 132 | virtual status_t addSourceDefaultEffect(const effect_uuid_t *type, |
| 133 | const String16& opPackageName, |
| 134 | const effect_uuid_t *uuid, |
| 135 | int32_t priority, |
| 136 | audio_source_t source, |
| 137 | audio_unique_id_t* id); |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 138 | virtual status_t addStreamDefaultEffect(const effect_uuid_t *type, |
| 139 | const String16& opPackageName, |
| 140 | const effect_uuid_t *uuid, |
| 141 | int32_t priority, |
| 142 | audio_usage_t usage, |
| 143 | audio_unique_id_t* id); |
Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 144 | virtual status_t removeSourceDefaultEffect(audio_unique_id_t id); |
Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 145 | virtual status_t removeStreamDefaultEffect(audio_unique_id_t id); |
| 146 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 147 | virtual status_t onTransact( |
| 148 | uint32_t code, |
| 149 | const Parcel& data, |
| 150 | Parcel* reply, |
| 151 | uint32_t flags); |
| 152 | |
| 153 | // IBinder::DeathRecipient |
| 154 | virtual void binderDied(const wp<IBinder>& who); |
| 155 | |
Eric Laurent | f5ada6e | 2014-10-09 17:49:00 -0700 | [diff] [blame] | 156 | // RefBase |
| 157 | virtual void onFirstRef(); |
| 158 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 159 | // |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 160 | // Helpers for the struct audio_policy_service_ops implementation. |
| 161 | // This is used by the audio policy manager for certain operations that |
| 162 | // are implemented by the policy service. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 163 | // |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 164 | virtual void setParameters(audio_io_handle_t ioHandle, |
| 165 | const char *keyValuePairs, |
| 166 | int delayMs); |
| 167 | |
| 168 | virtual status_t setStreamVolume(audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 169 | float volume, |
| 170 | audio_io_handle_t output, |
| 171 | int delayMs = 0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 172 | virtual status_t setVoiceVolume(float volume, int delayMs = 0); |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 173 | virtual bool isOffloadSupported(const audio_offload_info_t &config); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 174 | virtual bool isDirectOutputSupported(const audio_config_base_t& config, |
| 175 | const audio_attributes_t& attributes); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 176 | |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 177 | virtual status_t listAudioPorts(audio_port_role_t role, |
| 178 | audio_port_type_t type, |
| 179 | unsigned int *num_ports, |
| 180 | struct audio_port *ports, |
| 181 | unsigned int *generation); |
| 182 | virtual status_t getAudioPort(struct audio_port *port); |
| 183 | virtual status_t createAudioPatch(const struct audio_patch *patch, |
| 184 | audio_patch_handle_t *handle); |
| 185 | virtual status_t releaseAudioPatch(audio_patch_handle_t handle); |
| 186 | virtual status_t listAudioPatches(unsigned int *num_patches, |
| 187 | struct audio_patch *patches, |
| 188 | unsigned int *generation); |
| 189 | virtual status_t setAudioPortConfig(const struct audio_port_config *config); |
| 190 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 191 | virtual void registerClient(const sp<IAudioPolicyServiceClient>& client); |
| 192 | |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 193 | virtual void setAudioPortCallbacksEnabled(bool enabled); |
| 194 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 195 | virtual status_t acquireSoundTriggerSession(audio_session_t *session, |
| 196 | audio_io_handle_t *ioHandle, |
| 197 | audio_devices_t *device); |
| 198 | |
| 199 | virtual status_t releaseSoundTriggerSession(audio_session_t session); |
| 200 | |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 201 | virtual audio_mode_t getPhoneState(); |
| 202 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 203 | virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration); |
Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 204 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 205 | virtual status_t setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices); |
| 206 | |
| 207 | virtual status_t removeUidDeviceAffinities(uid_t uid); |
| 208 | |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 209 | virtual status_t startAudioSource(const struct audio_port_config *source, |
| 210 | const audio_attributes_t *attributes, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 211 | audio_port_handle_t *portId); |
| 212 | virtual status_t stopAudioSource(audio_port_handle_t portId); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 213 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 214 | virtual status_t setMasterMono(bool mono); |
| 215 | virtual status_t getMasterMono(bool *mono); |
| 216 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 217 | virtual float getStreamVolumeDB( |
| 218 | audio_stream_type_t stream, int index, audio_devices_t device); |
| 219 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 220 | virtual status_t getSurroundFormats(unsigned int *numSurroundFormats, |
| 221 | audio_format_t *surroundFormats, |
| 222 | bool *surroundFormatsEnabled, |
| 223 | bool reported); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 224 | virtual status_t getHwOffloadEncodingFormatsSupportedForA2DP( |
| 225 | std::vector<audio_format_t> *formats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 226 | virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled); |
| 227 | |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 228 | virtual status_t setAssistantUid(uid_t uid); |
| 229 | virtual status_t setA11yServicesUids(const std::vector<uid_t>& uids); |
| 230 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 231 | virtual bool isHapticPlaybackSupported(); |
| 232 | |
François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 233 | virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies); |
| 234 | virtual product_strategy_t getProductStrategyFromAudioAttributes(const AudioAttributes &aa); |
| 235 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 236 | status_t doStopOutput(audio_port_handle_t portId); |
| 237 | void doReleaseOutput(audio_port_handle_t portId); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 238 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 239 | status_t clientCreateAudioPatch(const struct audio_patch *patch, |
| 240 | audio_patch_handle_t *handle, |
| 241 | int delayMs); |
| 242 | status_t clientReleaseAudioPatch(audio_patch_handle_t handle, |
| 243 | int delayMs); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 244 | virtual status_t clientSetAudioPortConfig(const struct audio_port_config *config, |
| 245 | int delayMs); |
| 246 | |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 247 | void removeNotificationClient(uid_t uid, pid_t pid); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 248 | void onAudioPortListUpdate(); |
| 249 | void doOnAudioPortListUpdate(); |
| 250 | void onAudioPatchListUpdate(); |
| 251 | void doOnAudioPatchListUpdate(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 252 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 253 | void onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state); |
| 254 | void doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state); |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 255 | void onRecordingConfigurationUpdate(int event, |
| 256 | const record_client_info_t *clientInfo, |
| 257 | const audio_config_base_t *clientConfig, |
| 258 | std::vector<effect_descriptor_t> clientEffects, |
| 259 | const audio_config_base_t *deviceConfig, |
| 260 | std::vector<effect_descriptor_t> effects, |
| 261 | audio_patch_handle_t patchHandle, |
| 262 | audio_source_t source); |
| 263 | void doOnRecordingConfigurationUpdate(int event, |
| 264 | const record_client_info_t *clientInfo, |
| 265 | const audio_config_base_t *clientConfig, |
| 266 | std::vector<effect_descriptor_t> clientEffects, |
| 267 | const audio_config_base_t *deviceConfig, |
| 268 | std::vector<effect_descriptor_t> effects, |
| 269 | audio_patch_handle_t patchHandle, |
| 270 | audio_source_t source); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 271 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 272 | private: |
Mathias Agopian | e762be9 | 2013-05-09 16:26:45 -0700 | [diff] [blame] | 273 | AudioPolicyService() ANDROID_API; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 274 | virtual ~AudioPolicyService(); |
| 275 | |
| 276 | status_t dumpInternals(int fd); |
| 277 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 278 | // Handles binder shell commands |
| 279 | virtual status_t shellCommand(int in, int out, int err, Vector<String16>& args); |
| 280 | |
| 281 | // Sets whether the given UID records only silence |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 282 | virtual void setAppState_l(uid_t uid, app_state_t state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 283 | |
| 284 | // Overrides the UID state as if it is idle |
| 285 | status_t handleSetUidState(Vector<String16>& args, int err); |
| 286 | |
| 287 | // Clears the override for the UID state |
| 288 | status_t handleResetUidState(Vector<String16>& args, int err); |
| 289 | |
| 290 | // Gets the UID state |
| 291 | status_t handleGetUidState(Vector<String16>& args, int out, int err); |
| 292 | |
| 293 | // Prints the shell command help |
| 294 | status_t printHelp(int out); |
| 295 | |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 296 | std::string getDeviceTypeStrForPortId(audio_port_handle_t portId); |
| 297 | |
Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 298 | status_t getAudioPolicyEffects(sp<AudioPolicyEffects>& audioPolicyEffects); |
| 299 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 300 | app_state_t apmStatFromAmState(int amState); |
| 301 | |
| 302 | void updateUidStates(); |
| 303 | void updateUidStates_l(); |
| 304 | |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 305 | void silenceAllRecordings_l(); |
| 306 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 307 | static bool isPrivacySensitive(audio_source_t source); |
| 308 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 309 | // If recording we need to make sure the UID is allowed to do that. If the UID is idle |
| 310 | // then it cannot record and gets buffers with zeros - silence. As soon as the UID |
| 311 | // transitions to an active state we will start reporting buffers with data. This approach |
| 312 | // transparently handles recording while the UID transitions between idle/active state |
| 313 | // avoiding to get stuck in a state receiving non-empty buffers while idle or in a state |
| 314 | // receiving empty buffers while active. |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 315 | class UidPolicy : public BnUidObserver, public virtual IBinder::DeathRecipient { |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 316 | public: |
| 317 | explicit UidPolicy(wp<AudioPolicyService> service) |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 318 | : mService(service), mObserverRegistered(false), mAssistantUid(0) {} |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 319 | |
| 320 | void registerSelf(); |
| 321 | void unregisterSelf(); |
| 322 | |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 323 | // IBinder::DeathRecipient implementation |
| 324 | void binderDied(const wp<IBinder> &who) override; |
| 325 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 326 | bool isUidActive(uid_t uid); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 327 | int getUidState(uid_t uid); |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 328 | void setAssistantUid(uid_t uid) { mAssistantUid = uid; } |
| 329 | bool isAssistantUid(uid_t uid) { return uid == mAssistantUid; } |
| 330 | void setA11yUids(const std::vector<uid_t>& uids) { mA11yUids.clear(); mA11yUids = uids; } |
| 331 | bool isA11yUid(uid_t uid); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 332 | bool isA11yOnTop(); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 333 | |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 334 | // BnUidObserver implementation |
| 335 | void onUidActive(uid_t uid) override; |
| 336 | void onUidGone(uid_t uid, bool disabled) override; |
| 337 | void onUidIdle(uid_t uid, bool disabled) override; |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 338 | void onUidStateChanged(uid_t uid, int32_t procState, int64_t procStateSeq); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 339 | |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 340 | void addOverrideUid(uid_t uid, bool active) { updateOverrideUid(uid, active, true); } |
| 341 | void removeOverrideUid(uid_t uid) { updateOverrideUid(uid, false, false); } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 342 | |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 343 | void updateUid(std::unordered_map<uid_t, std::pair<bool, int>> *uids, |
| 344 | uid_t uid, bool active, int state, bool insert); |
| 345 | |
| 346 | private: |
| 347 | void notifyService(); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 348 | void updateOverrideUid(uid_t uid, bool active, bool insert); |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 349 | void updateUidLocked(std::unordered_map<uid_t, std::pair<bool, int>> *uids, |
| 350 | uid_t uid, bool active, int state, bool insert); |
| 351 | void checkRegistered(); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 352 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 353 | wp<AudioPolicyService> mService; |
Mikhail Naganov | eae73eb | 2018-04-03 16:57:36 -0700 | [diff] [blame] | 354 | Mutex mLock; |
| 355 | bool mObserverRegistered; |
Eric Laurent | e8c8b43 | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 356 | std::unordered_map<uid_t, std::pair<bool, int>> mOverrideUids; |
| 357 | std::unordered_map<uid_t, std::pair<bool, int>> mCachedUids; |
Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 358 | uid_t mAssistantUid; |
| 359 | std::vector<uid_t> mA11yUids; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 360 | }; |
| 361 | |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 362 | // If sensor privacy is enabled then all apps, including those that are active, should be |
| 363 | // prevented from recording. This is handled similar to idle UIDs, any app that attempts |
| 364 | // to record while sensor privacy is enabled will receive buffers with zeros. As soon as |
| 365 | // sensor privacy is disabled active apps will receive the expected data when recording. |
| 366 | class SensorPrivacyPolicy : public hardware::BnSensorPrivacyListener { |
| 367 | public: |
| 368 | explicit SensorPrivacyPolicy(wp<AudioPolicyService> service) |
| 369 | : mService(service) {} |
| 370 | |
| 371 | void registerSelf(); |
| 372 | void unregisterSelf(); |
| 373 | |
| 374 | bool isSensorPrivacyEnabled(); |
| 375 | |
| 376 | binder::Status onSensorPrivacyChanged(bool enabled); |
| 377 | |
| 378 | private: |
| 379 | wp<AudioPolicyService> mService; |
| 380 | std::atomic_bool mSensorPrivacyEnabled; |
| 381 | }; |
| 382 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 383 | // Thread used to send audio config commands to audio flinger |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 384 | // For audio config commands, it is necessary because audio flinger requires that the calling |
| 385 | // process (user) has permission to modify audio settings. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 386 | class AudioCommandThread : public Thread { |
| 387 | class AudioCommand; |
| 388 | public: |
| 389 | |
| 390 | // commands for tone AudioCommand |
| 391 | enum { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 392 | SET_VOLUME, |
| 393 | SET_PARAMETERS, |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 394 | SET_VOICE_VOLUME, |
| 395 | STOP_OUTPUT, |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 396 | RELEASE_OUTPUT, |
| 397 | CREATE_AUDIO_PATCH, |
| 398 | RELEASE_AUDIO_PATCH, |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 399 | UPDATE_AUDIOPORT_LIST, |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 400 | UPDATE_AUDIOPATCH_LIST, |
| 401 | SET_AUDIOPORT_CONFIG, |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 402 | DYN_POLICY_MIX_STATE_UPDATE, |
| 403 | RECORDING_CONFIGURATION_UPDATE |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 404 | }; |
| 405 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 406 | AudioCommandThread (String8 name, const wp<AudioPolicyService>& service); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 407 | virtual ~AudioCommandThread(); |
| 408 | |
| 409 | status_t dump(int fd); |
| 410 | |
| 411 | // Thread virtuals |
| 412 | virtual void onFirstRef(); |
| 413 | virtual bool threadLoop(); |
| 414 | |
| 415 | void exit(); |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 416 | status_t volumeCommand(audio_stream_type_t stream, float volume, |
| 417 | audio_io_handle_t output, int delayMs = 0); |
| 418 | status_t parametersCommand(audio_io_handle_t ioHandle, |
| 419 | const char *keyValuePairs, int delayMs = 0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 420 | status_t voiceVolumeCommand(float volume, int delayMs = 0); |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 421 | void stopOutputCommand(audio_port_handle_t portId); |
| 422 | void releaseOutputCommand(audio_port_handle_t portId); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 423 | status_t sendCommand(sp<AudioCommand>& command, int delayMs = 0); |
| 424 | void insertCommand_l(sp<AudioCommand>& command, int delayMs = 0); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 425 | status_t createAudioPatchCommand(const struct audio_patch *patch, |
| 426 | audio_patch_handle_t *handle, |
| 427 | int delayMs); |
| 428 | status_t releaseAudioPatchCommand(audio_patch_handle_t handle, |
| 429 | int delayMs); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 430 | void updateAudioPortListCommand(); |
| 431 | void updateAudioPatchListCommand(); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 432 | status_t setAudioPortConfigCommand(const struct audio_port_config *config, |
| 433 | int delayMs); |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 434 | void dynamicPolicyMixStateUpdateCommand(const String8& regId, |
| 435 | int32_t state); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 436 | void recordingConfigurationUpdateCommand( |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 437 | int event, |
| 438 | const record_client_info_t *clientInfo, |
| 439 | const audio_config_base_t *clientConfig, |
| 440 | std::vector<effect_descriptor_t> clientEffects, |
| 441 | const audio_config_base_t *deviceConfig, |
| 442 | std::vector<effect_descriptor_t> effects, |
| 443 | audio_patch_handle_t patchHandle, |
| 444 | audio_source_t source); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 445 | void insertCommand_l(AudioCommand *command, int delayMs = 0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 446 | private: |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 447 | class AudioCommandData; |
| 448 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 449 | // descriptor for requested tone playback event |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 450 | class AudioCommand: public RefBase { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 451 | |
| 452 | public: |
| 453 | AudioCommand() |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 454 | : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {} |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 455 | |
| 456 | void dump(char* buffer, size_t size); |
| 457 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 458 | int mCommand; // SET_VOLUME, SET_PARAMETERS... |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 459 | nsecs_t mTime; // time stamp |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 460 | Mutex mLock; // mutex associated to mCond |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 461 | Condition mCond; // condition for status return |
| 462 | status_t mStatus; // command status |
| 463 | bool mWaitStatus; // true if caller is waiting for status |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 464 | sp<AudioCommandData> mParam; // command specific parameter data |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 465 | }; |
| 466 | |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 467 | class AudioCommandData: public RefBase { |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 468 | public: |
| 469 | virtual ~AudioCommandData() {} |
| 470 | protected: |
| 471 | AudioCommandData() {} |
| 472 | }; |
| 473 | |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 474 | class VolumeData : public AudioCommandData { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 475 | public: |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 476 | audio_stream_type_t mStream; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 477 | float mVolume; |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 478 | audio_io_handle_t mIO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 479 | }; |
| 480 | |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 481 | class ParametersData : public AudioCommandData { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 482 | public: |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 483 | audio_io_handle_t mIO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 484 | String8 mKeyValuePairs; |
| 485 | }; |
| 486 | |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 487 | class VoiceVolumeData : public AudioCommandData { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 488 | public: |
| 489 | float mVolume; |
| 490 | }; |
| 491 | |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 492 | class StopOutputData : public AudioCommandData { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 493 | public: |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 494 | audio_port_handle_t mPortId; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 495 | }; |
| 496 | |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 497 | class ReleaseOutputData : public AudioCommandData { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 498 | public: |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 499 | audio_port_handle_t mPortId; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 500 | }; |
| 501 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 502 | class CreateAudioPatchData : public AudioCommandData { |
| 503 | public: |
| 504 | struct audio_patch mPatch; |
| 505 | audio_patch_handle_t mHandle; |
| 506 | }; |
| 507 | |
| 508 | class ReleaseAudioPatchData : public AudioCommandData { |
| 509 | public: |
| 510 | audio_patch_handle_t mHandle; |
| 511 | }; |
| 512 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 513 | class SetAudioPortConfigData : public AudioCommandData { |
| 514 | public: |
| 515 | struct audio_port_config mConfig; |
| 516 | }; |
| 517 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 518 | class DynPolicyMixStateUpdateData : public AudioCommandData { |
| 519 | public: |
| 520 | String8 mRegId; |
| 521 | int32_t mState; |
| 522 | }; |
| 523 | |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 524 | class RecordingConfigurationUpdateData : public AudioCommandData { |
| 525 | public: |
| 526 | int mEvent; |
Jean-Michel Trivi | ac4e429 | 2016-12-22 11:39:31 -0800 | [diff] [blame] | 527 | record_client_info_t mClientInfo; |
Jean-Michel Trivi | 7281aa9 | 2016-02-17 15:33:40 -0800 | [diff] [blame] | 528 | struct audio_config_base mClientConfig; |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 529 | std::vector<effect_descriptor_t> mClientEffects; |
Jean-Michel Trivi | 7281aa9 | 2016-02-17 15:33:40 -0800 | [diff] [blame] | 530 | struct audio_config_base mDeviceConfig; |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 531 | std::vector<effect_descriptor_t> mEffects; |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 532 | audio_patch_handle_t mPatchHandle; |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 533 | audio_source_t mSource; |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 534 | }; |
| 535 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 536 | Mutex mLock; |
| 537 | Condition mWaitWorkCV; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 538 | Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 539 | sp<AudioCommand> mLastCommand; // last processed command (used by dump) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 540 | String8 mName; // string used by wake lock fo delayed commands |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 541 | wp<AudioPolicyService> mService; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 542 | }; |
| 543 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 544 | class AudioPolicyClient : public AudioPolicyClientInterface |
| 545 | { |
| 546 | public: |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 547 | explicit AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {} |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 548 | virtual ~AudioPolicyClient() {} |
| 549 | |
| 550 | // |
| 551 | // Audio HW module functions |
| 552 | // |
| 553 | |
| 554 | // loads a HW module. |
| 555 | virtual audio_module_handle_t loadHwModule(const char *name); |
| 556 | |
| 557 | // |
| 558 | // Audio output Control functions |
| 559 | // |
| 560 | |
| 561 | // opens an audio output with the requested parameters. The parameter values can indicate to use the default values |
| 562 | // in case the audio policy manager has no specific requirements for the output being opened. |
| 563 | // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream. |
| 564 | // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly. |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 565 | virtual status_t openOutput(audio_module_handle_t module, |
| 566 | audio_io_handle_t *output, |
| 567 | audio_config_t *config, |
| 568 | audio_devices_t *devices, |
| 569 | const String8& address, |
| 570 | uint32_t *latencyMs, |
| 571 | audio_output_flags_t flags); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 572 | // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by |
| 573 | // a special mixer thread in the AudioFlinger. |
| 574 | virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2); |
| 575 | // closes the output stream |
| 576 | virtual status_t closeOutput(audio_io_handle_t output); |
| 577 | // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in |
| 578 | // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded. |
| 579 | virtual status_t suspendOutput(audio_io_handle_t output); |
| 580 | // restores a suspended output. |
| 581 | virtual status_t restoreOutput(audio_io_handle_t output); |
| 582 | |
| 583 | // |
| 584 | // Audio input Control functions |
| 585 | // |
| 586 | |
| 587 | // opens an audio input |
| 588 | virtual audio_io_handle_t openInput(audio_module_handle_t module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 589 | audio_io_handle_t *input, |
| 590 | audio_config_t *config, |
| 591 | audio_devices_t *devices, |
| 592 | const String8& address, |
| 593 | audio_source_t source, |
Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 594 | audio_input_flags_t flags); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 595 | // closes an audio input |
| 596 | virtual status_t closeInput(audio_io_handle_t input); |
| 597 | // |
| 598 | // misc control functions |
| 599 | // |
| 600 | |
| 601 | // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes |
| 602 | // for each output (destination device) it is attached to. |
| 603 | virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0); |
| 604 | |
| 605 | // invalidate a stream type, causing a reroute to an unspecified new output |
| 606 | virtual status_t invalidateStream(audio_stream_type_t stream); |
| 607 | |
| 608 | // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface. |
| 609 | virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0); |
| 610 | // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager. |
| 611 | virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys); |
| 612 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 613 | // set down link audio volume. |
| 614 | virtual status_t setVoiceVolume(float volume, int delayMs = 0); |
| 615 | |
| 616 | // move effect to the specified output |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 617 | virtual status_t moveEffects(audio_session_t session, |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 618 | audio_io_handle_t srcOutput, |
| 619 | audio_io_handle_t dstOutput); |
| 620 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 621 | /* Create a patch between several source and sink ports */ |
| 622 | virtual status_t createAudioPatch(const struct audio_patch *patch, |
| 623 | audio_patch_handle_t *handle, |
| 624 | int delayMs); |
| 625 | |
| 626 | /* Release a patch */ |
| 627 | virtual status_t releaseAudioPatch(audio_patch_handle_t handle, |
| 628 | int delayMs); |
| 629 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 630 | /* Set audio port configuration */ |
| 631 | virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs); |
| 632 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 633 | virtual void onAudioPortListUpdate(); |
| 634 | virtual void onAudioPatchListUpdate(); |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 635 | virtual void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 636 | virtual void onRecordingConfigurationUpdate(int event, |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 637 | const record_client_info_t *clientInfo, |
| 638 | const audio_config_base_t *clientConfig, |
| 639 | std::vector<effect_descriptor_t> clientEffects, |
| 640 | const audio_config_base_t *deviceConfig, |
| 641 | std::vector<effect_descriptor_t> effects, |
| 642 | audio_patch_handle_t patchHandle, |
| 643 | audio_source_t source); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 644 | |
Glenn Kasten | eeecb98 | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 645 | virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use); |
Eric Laurent | de3f839 | 2014-07-27 18:38:22 -0700 | [diff] [blame] | 646 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 647 | private: |
| 648 | AudioPolicyService *mAudioPolicyService; |
| 649 | }; |
| 650 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 651 | // --- Notification Client --- |
| 652 | class NotificationClient : public IBinder::DeathRecipient { |
| 653 | public: |
| 654 | NotificationClient(const sp<AudioPolicyService>& service, |
| 655 | const sp<IAudioPolicyServiceClient>& client, |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 656 | uid_t uid, pid_t pid); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 657 | virtual ~NotificationClient(); |
| 658 | |
Jean-Michel Trivi | de80105 | 2015-04-14 19:10:14 -0700 | [diff] [blame] | 659 | void onAudioPortListUpdate(); |
| 660 | void onAudioPatchListUpdate(); |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 661 | void onDynamicPolicyMixStateUpdate(const String8& regId, |
| 662 | int32_t state); |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 663 | void onRecordingConfigurationUpdate( |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 664 | int event, |
| 665 | const record_client_info_t *clientInfo, |
| 666 | const audio_config_base_t *clientConfig, |
| 667 | std::vector<effect_descriptor_t> clientEffects, |
| 668 | const audio_config_base_t *deviceConfig, |
| 669 | std::vector<effect_descriptor_t> effects, |
| 670 | audio_patch_handle_t patchHandle, |
| 671 | audio_source_t source); |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 672 | void setAudioPortCallbacksEnabled(bool enabled); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 673 | |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 674 | uid_t uid() { |
| 675 | return mUid; |
| 676 | } |
| 677 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 678 | // IBinder::DeathRecipient |
| 679 | virtual void binderDied(const wp<IBinder>& who); |
| 680 | |
| 681 | private: |
| 682 | NotificationClient(const NotificationClient&); |
| 683 | NotificationClient& operator = (const NotificationClient&); |
| 684 | |
| 685 | const wp<AudioPolicyService> mService; |
| 686 | const uid_t mUid; |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 687 | const pid_t mPid; |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 688 | const sp<IAudioPolicyServiceClient> mAudioPolicyServiceClient; |
Eric Laurent | e8726fe | 2015-06-26 09:39:24 -0700 | [diff] [blame] | 689 | bool mAudioPortCallbacksEnabled; |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 690 | }; |
| 691 | |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 692 | class AudioClient : public virtual RefBase { |
| 693 | public: |
| 694 | AudioClient(const audio_attributes_t attributes, |
| 695 | const audio_io_handle_t io, uid_t uid, pid_t pid, |
| 696 | const audio_session_t session, const audio_port_handle_t deviceId) : |
| 697 | attributes(attributes), io(io), uid(uid), pid(pid), |
| 698 | session(session), deviceId(deviceId), active(false) {} |
| 699 | ~AudioClient() override = default; |
| 700 | |
| 701 | |
| 702 | const audio_attributes_t attributes; // source, flags ... |
| 703 | const audio_io_handle_t io; // audio HAL stream IO handle |
| 704 | const uid_t uid; // client UID |
| 705 | const pid_t pid; // client PID |
| 706 | const audio_session_t session; // audio session ID |
| 707 | const audio_port_handle_t deviceId; // selected input device port ID |
| 708 | bool active; // Playback/Capture is active or inactive |
| 709 | }; |
| 710 | |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 711 | // --- AudioRecordClient --- |
| 712 | // Information about each registered AudioRecord client |
| 713 | // (between calls to getInputForAttr() and releaseInput()) |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 714 | class AudioRecordClient : public AudioClient { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 715 | public: |
| 716 | AudioRecordClient(const audio_attributes_t attributes, |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 717 | const audio_io_handle_t io, uid_t uid, pid_t pid, |
| 718 | const audio_session_t session, const audio_port_handle_t deviceId, |
| 719 | const String16& opPackageName) : |
| 720 | AudioClient(attributes, io, uid, pid, session, deviceId), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 721 | opPackageName(opPackageName), startTimeNs(0) {} |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 722 | ~AudioRecordClient() override = default; |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 723 | |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 724 | const String16 opPackageName; // client package name |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 725 | nsecs_t startTimeNs; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 726 | }; |
| 727 | |
| 728 | // --- AudioPlaybackClient --- |
| 729 | // Information about each registered AudioTrack client |
| 730 | // (between calls to getOutputForAttr() and releaseOutput()) |
| 731 | class AudioPlaybackClient : public AudioClient { |
| 732 | public: |
| 733 | AudioPlaybackClient(const audio_attributes_t attributes, |
| 734 | const audio_io_handle_t io, uid_t uid, pid_t pid, |
| 735 | const audio_session_t session, audio_port_handle_t deviceId, |
| 736 | audio_stream_type_t stream) : |
| 737 | AudioClient(attributes, io, uid, pid, session, deviceId), stream(stream) {} |
| 738 | ~AudioPlaybackClient() override = default; |
| 739 | |
| 740 | const audio_stream_type_t stream; |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 741 | }; |
| 742 | |
Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 743 | // A class automatically clearing and restoring binder caller identity inside |
| 744 | // a code block (scoped variable) |
| 745 | // Declare one systematically before calling AudioPolicyManager methods so that they are |
| 746 | // executed with the same level of privilege as audioserver process. |
| 747 | class AutoCallerClear { |
| 748 | public: |
| 749 | AutoCallerClear() : |
| 750 | mToken(IPCThreadState::self()->clearCallingIdentity()) {} |
| 751 | ~AutoCallerClear() { |
| 752 | IPCThreadState::self()->restoreCallingIdentity(mToken); |
| 753 | } |
| 754 | |
| 755 | private: |
| 756 | const int64_t mToken; |
| 757 | }; |
| 758 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 759 | // Internal dump utilities. |
| 760 | status_t dumpPermissionDenial(int fd); |
| 761 | |
| 762 | |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 763 | mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing |
| 764 | // device connection state or routing |
Haynes Mathew George | bab7bf4 | 2015-10-30 18:02:23 -0700 | [diff] [blame] | 765 | mutable Mutex mEffectsLock; // serialize access to Effect state within APM. |
| 766 | // Note: lock acquisition order is always mLock > mEffectsLock: |
| 767 | // mLock protects AudioPolicyManager methods that can call into audio flinger |
| 768 | // and possibly back in to audio policy service and acquire mEffectsLock. |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 769 | sp<AudioCommandThread> mAudioCommandThread; // audio commands thread |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 770 | sp<AudioCommandThread> mOutputCommandThread; // process stop and release output |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 771 | struct audio_policy_device *mpAudioPolicyDev; |
| 772 | struct audio_policy *mpAudioPolicy; |
Eric Laurent | f269b8e | 2014-06-09 20:01:29 -0700 | [diff] [blame] | 773 | AudioPolicyInterface *mAudioPolicyManager; |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 774 | AudioPolicyClient *mAudioPolicyClient; |
| 775 | |
luochaojiang | 908c7d7 | 2018-06-21 14:58:04 +0800 | [diff] [blame] | 776 | DefaultKeyedVector< int64_t, sp<NotificationClient> > mNotificationClients; |
Eric Laurent | 0ebd5f9 | 2014-11-19 19:04:52 -0800 | [diff] [blame] | 777 | Mutex mNotificationClientsLock; // protects mNotificationClients |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 778 | // Manage all effects configured in audio_effects.conf |
| 779 | sp<AudioPolicyEffects> mAudioPolicyEffects; |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 780 | audio_mode_t mPhoneState; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 781 | |
| 782 | sp<UidPolicy> mUidPolicy; |
Michael Groover | cfd2830 | 2018-12-11 19:16:46 -0800 | [diff] [blame] | 783 | sp<SensorPrivacyPolicy> mSensorPrivacyPolicy; |
| 784 | |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 785 | DefaultKeyedVector< audio_port_handle_t, sp<AudioRecordClient> > mAudioRecordClients; |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 786 | DefaultKeyedVector< audio_port_handle_t, sp<AudioPlaybackClient> > mAudioPlaybackClients; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 787 | }; |
| 788 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 789 | } // namespace android |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 790 | |
| 791 | #endif // ANDROID_AUDIOPOLICYSERVICE_H |