blob: c3c87f15761cb1e57e5cb4a1897702cce40e5105 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
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 Laurent7c7f10b2011-06-17 21:29:58 -070020#include <cutils/misc.h>
21#include <cutils/config_utils.h>
Mathias Agopiane762be92013-05-09 16:26:45 -070022#include <cutils/compiler.h>
Glenn Kastend1d8f232011-07-08 09:34:50 -070023#include <utils/String8.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024#include <utils/Vector.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070025#include <utils/SortedVector.h>
Steven Moreland2f348142019-07-02 15:59:07 -070026#include <binder/ActivityManager.h>
Glenn Kastend2dcb082011-02-03 16:55:26 -080027#include <binder/BinderService.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080028#include <binder/IUidObserver.h>
Dima Zavin64760242011-05-11 14:15:23 -070029#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070030#include <system/audio_policy.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070031#include <media/IAudioPolicyService.h>
32#include <media/ToneGenerator.h>
33#include <media/AudioEffect.h>
Eric Laurentbaac1832014-12-01 17:52:59 -080034#include <media/AudioPolicy.h>
Kevin Rocard8be94972019-02-22 13:26:25 -080035#include <mediautils/ServiceUtilities.h>
bryant_liuba2b4392014-06-11 16:49:30 +080036#include "AudioPolicyEffects.h"
François Gaffie962ba212019-04-03 16:21:39 +020037#include <AudioPolicyInterface.h>
Michael Groovercfd28302018-12-11 19:16:46 -080038#include <android/hardware/BnSensorPrivacyListener.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070039
Svet Ganovf4ddfef2018-01-16 07:37:58 -080040#include <unordered_map>
bryant_liuba2b4392014-06-11 16:49:30 +080041
Mathias Agopian65ab4712010-07-14 17:59:35 -070042namespace android {
43
Mathias Agopian65ab4712010-07-14 17:59:35 -070044// ----------------------------------------------------------------------------
45
Glenn Kastend2dcb082011-02-03 16:55:26 -080046class AudioPolicyService :
47 public BinderService<AudioPolicyService>,
48 public BnAudioPolicyService,
Eric Laurentde070132010-07-13 04:45:46 -070049 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070050{
Glenn Kastend2dcb082011-02-03 16:55:26 -080051 friend class BinderService<AudioPolicyService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
53public:
Glenn Kastend2dcb082011-02-03 16:55:26 -080054 // for BinderService
Mathias Agopiane762be92013-05-09 16:26:45 -070055 static const char *getServiceName() ANDROID_API { return "media.audio_policy"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
57 virtual status_t dump(int fd, const Vector<String16>& args);
58
59 //
60 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
61 //
62
Dima Zavinfce7a472011-04-19 22:30:36 -070063 virtual status_t setDeviceConnectionState(audio_devices_t device,
64 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080065 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -080066 const char *device_name,
67 audio_format_t encodedFormat);
Dima Zavinfce7a472011-04-19 22:30:36 -070068 virtual audio_policy_dev_state_t getDeviceConnectionState(
69 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -070070 const char *device_address);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -080071 virtual status_t handleDeviceConfigChange(audio_devices_t device,
72 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -080073 const char *device_name,
74 audio_format_t encodedFormat);
Eric Laurent00dba062020-02-11 15:52:09 -080075 virtual status_t setPhoneState(audio_mode_t state, uid_t uid);
Dima Zavinfce7a472011-04-19 22:30:36 -070076 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
77 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
Eric Laurentf4e63452017-11-06 19:31:46 +000078 virtual audio_io_handle_t getOutput(audio_stream_type_t stream);
Eric Laurent42984412019-05-09 17:57:03 -070079 status_t getOutputForAttr(audio_attributes_t *attr,
Kevin Rocard153f92d2018-12-18 18:33:28 -080080 audio_io_handle_t *output,
81 audio_session_t session,
82 audio_stream_type_t *stream,
83 pid_t pid,
84 uid_t uid,
Ricardo Correaac26cf72020-01-06 14:43:38 -080085 const String16& opPackageName,
Kevin Rocard153f92d2018-12-18 18:33:28 -080086 const audio_config_t *config,
87 audio_output_flags_t flags,
88 audio_port_handle_t *selectedDeviceId,
89 audio_port_handle_t *portId,
90 std::vector<audio_io_handle_t> *secondaryOutputs) override;
Eric Laurentd7fe0862018-07-14 16:48:01 -070091 virtual status_t startOutput(audio_port_handle_t portId);
92 virtual status_t stopOutput(audio_port_handle_t portId);
93 virtual void releaseOutput(audio_port_handle_t portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -080094 virtual status_t getInputForAttr(const audio_attributes_t *attr,
95 audio_io_handle_t *input,
Mikhail Naganov2996f672019-04-18 12:29:59 -070096 audio_unique_id_t riid,
Eric Laurentcaf7f482014-11-25 17:50:47 -080097 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -070098 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -070099 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800100 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800101 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600102 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700103 audio_port_handle_t *selectedDeviceId = NULL,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800104 audio_port_handle_t *portId = NULL);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800105 virtual status_t startInput(audio_port_handle_t portId);
Eric Laurentfee19762018-01-29 18:44:13 -0800106 virtual status_t stopInput(audio_port_handle_t portId);
107 virtual void releaseInput(audio_port_handle_t portId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700108 virtual status_t initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700109 int indexMin,
110 int indexMax);
Eric Laurent83844cc2011-11-18 16:43:31 -0800111 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
112 int index,
113 audio_devices_t device);
114 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
115 int *index,
116 audio_devices_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700117
François Gaffiecfe17322018-11-07 13:41:29 +0100118 virtual status_t setVolumeIndexForAttributes(const audio_attributes_t &attr,
119 int index,
120 audio_devices_t device);
121 virtual status_t getVolumeIndexForAttributes(const audio_attributes_t &attr,
122 int &index,
123 audio_devices_t device);
124 virtual status_t getMinVolumeIndexForAttributes(const audio_attributes_t &attr,
125 int &index);
126 virtual status_t getMaxVolumeIndexForAttributes(const audio_attributes_t &attr,
127 int &index);
128
Dima Zavinfce7a472011-04-19 22:30:36 -0700129 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Eric Laurent63742522012-03-08 13:42:42 -0800130 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Jean-Michel Trivif41599b2020-01-07 14:22:08 -0800131 virtual status_t getDevicesForAttributes(const AudioAttributes &aa,
132 AudioDeviceTypeAddrVector *devices) const;
Eric Laurentde070132010-07-13 04:45:46 -0700133
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700134 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
135 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700136 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700137 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800138 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700139 int id);
140 virtual status_t unregisterEffect(int id);
Eric Laurentdb7c0792011-08-10 10:37:50 -0700141 virtual status_t setEffectEnabled(int id, bool enabled);
Eric Laurent6c796322019-04-09 14:13:17 -0700142 status_t moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) override;
Glenn Kastenfff6d712012-01-12 16:38:12 -0800143 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800144 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700145 virtual bool isSourceActive(audio_source_t source) const;
Eric Laurentde070132010-07-13 04:45:46 -0700146
Glenn Kastend848eb42016-03-08 13:42:11 -0800147 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700148 effect_descriptor_t *descriptors,
149 uint32_t *count);
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700150 virtual status_t addSourceDefaultEffect(const effect_uuid_t *type,
151 const String16& opPackageName,
152 const effect_uuid_t *uuid,
153 int32_t priority,
154 audio_source_t source,
155 audio_unique_id_t* id);
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700156 virtual status_t addStreamDefaultEffect(const effect_uuid_t *type,
157 const String16& opPackageName,
158 const effect_uuid_t *uuid,
159 int32_t priority,
160 audio_usage_t usage,
161 audio_unique_id_t* id);
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700162 virtual status_t removeSourceDefaultEffect(audio_unique_id_t id);
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700163 virtual status_t removeStreamDefaultEffect(audio_unique_id_t id);
164
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165 virtual status_t onTransact(
166 uint32_t code,
167 const Parcel& data,
168 Parcel* reply,
169 uint32_t flags);
170
171 // IBinder::DeathRecipient
172 virtual void binderDied(const wp<IBinder>& who);
173
Eric Laurentf5ada6e2014-10-09 17:49:00 -0700174 // RefBase
175 virtual void onFirstRef();
176
Mathias Agopian65ab4712010-07-14 17:59:35 -0700177 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700178 // Helpers for the struct audio_policy_service_ops implementation.
179 // This is used by the audio policy manager for certain operations that
180 // are implemented by the policy service.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700181 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700182 virtual void setParameters(audio_io_handle_t ioHandle,
183 const char *keyValuePairs,
184 int delayMs);
185
186 virtual status_t setStreamVolume(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700187 float volume,
188 audio_io_handle_t output,
189 int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700190 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Hayden Gomes524159d2019-12-23 14:41:47 -0800191 status_t setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages);
Kevin Rocardb99cc752019-03-21 20:52:24 -0700192 status_t setAllowedCapturePolicy(uint_t uid, audio_flags_mask_t capturePolicy) override;
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000193 virtual bool isOffloadSupported(const audio_offload_info_t &config);
Michael Chana94fbb22018-04-24 14:31:19 +1000194 virtual bool isDirectOutputSupported(const audio_config_base_t& config,
195 const audio_attributes_t& attributes);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196
Eric Laurent203b1a12014-04-01 10:34:16 -0700197 virtual status_t listAudioPorts(audio_port_role_t role,
198 audio_port_type_t type,
199 unsigned int *num_ports,
200 struct audio_port *ports,
201 unsigned int *generation);
202 virtual status_t getAudioPort(struct audio_port *port);
203 virtual status_t createAudioPatch(const struct audio_patch *patch,
204 audio_patch_handle_t *handle);
205 virtual status_t releaseAudioPatch(audio_patch_handle_t handle);
206 virtual status_t listAudioPatches(unsigned int *num_patches,
207 struct audio_patch *patches,
208 unsigned int *generation);
209 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
210
Eric Laurentb52c1522014-05-20 11:27:36 -0700211 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client);
212
Eric Laurente8726fe2015-06-26 09:39:24 -0700213 virtual void setAudioPortCallbacksEnabled(bool enabled);
214
François Gaffiecfe17322018-11-07 13:41:29 +0100215 virtual void setAudioVolumeGroupCallbacksEnabled(bool enabled);
216
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700217 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
218 audio_io_handle_t *ioHandle,
219 audio_devices_t *device);
220
221 virtual status_t releaseSoundTriggerSession(audio_session_t session);
222
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700223 virtual audio_mode_t getPhoneState();
224
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700225 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration);
Eric Laurentbaac1832014-12-01 17:52:59 -0800226
Jean-Michel Trivibda70da2018-12-19 07:30:15 -0800227 virtual status_t setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices);
228
229 virtual status_t removeUidDeviceAffinities(uid_t uid);
230
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700231 virtual status_t setPreferredDeviceForStrategy(product_strategy_t strategy,
232 const AudioDeviceTypeAddr &device);
233
234 virtual status_t removePreferredDeviceForStrategy(product_strategy_t strategy);
235
236
237 virtual status_t getPreferredDeviceForStrategy(product_strategy_t strategy,
238 AudioDeviceTypeAddr &device);
Oscar Azucena90e77632019-11-27 17:12:28 -0800239 virtual status_t setUserIdDeviceAffinities(int userId, const Vector<AudioDeviceTypeAddr>& devices);
240
241 virtual status_t removeUserIdDeviceAffinities(int userId);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700242
Eric Laurent554a2772015-04-10 11:29:24 -0700243 virtual status_t startAudioSource(const struct audio_port_config *source,
244 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700245 audio_port_handle_t *portId);
246 virtual status_t stopAudioSource(audio_port_handle_t portId);
Eric Laurent554a2772015-04-10 11:29:24 -0700247
Andy Hung2ddee192015-12-18 17:34:44 -0800248 virtual status_t setMasterMono(bool mono);
249 virtual status_t getMasterMono(bool *mono);
250
Eric Laurentac9cef52017-06-09 15:46:26 -0700251 virtual float getStreamVolumeDB(
252 audio_stream_type_t stream, int index, audio_devices_t device);
253
jiabin81772902018-04-02 17:52:27 -0700254 virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
255 audio_format_t *surroundFormats,
256 bool *surroundFormatsEnabled,
257 bool reported);
Arun Mirpuri11029ad2018-12-19 20:45:19 -0800258 virtual status_t getHwOffloadEncodingFormatsSupportedForA2DP(
259 std::vector<audio_format_t> *formats);
jiabin81772902018-04-02 17:52:27 -0700260 virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled);
261
Eric Laurentb78763e2018-10-17 10:08:02 -0700262 virtual status_t setAssistantUid(uid_t uid);
263 virtual status_t setA11yServicesUids(const std::vector<uid_t>& uids);
264
jiabin6012f912018-11-02 17:06:30 -0700265 virtual bool isHapticPlaybackSupported();
266
François Gaffied0ba9ed2018-11-05 11:50:42 +0100267 virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies);
François Gaffie4b2018b2018-11-07 11:18:59 +0100268 virtual status_t getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
269 product_strategy_t &productStrategy);
270
271 virtual status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups);
272
273 virtual status_t getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
274 volume_group_t &volumeGroup);
François Gaffied0ba9ed2018-11-05 11:50:42 +0100275
Eric Laurent6ede98f2019-06-11 14:50:30 -0700276 virtual status_t setRttEnabled(bool enabled);
277
Eric Laurent8340e672019-11-06 11:01:08 -0800278 bool isCallScreenModeSupported() override;
279
Eric Laurentd7fe0862018-07-14 16:48:01 -0700280 status_t doStopOutput(audio_port_handle_t portId);
281 void doReleaseOutput(audio_port_handle_t portId);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800282
Eric Laurent951f4552014-05-20 10:48:17 -0700283 status_t clientCreateAudioPatch(const struct audio_patch *patch,
284 audio_patch_handle_t *handle,
285 int delayMs);
286 status_t clientReleaseAudioPatch(audio_patch_handle_t handle,
287 int delayMs);
Eric Laurente1715a42014-05-20 11:30:42 -0700288 virtual status_t clientSetAudioPortConfig(const struct audio_port_config *config,
289 int delayMs);
290
luochaojiang908c7d72018-06-21 14:58:04 +0800291 void removeNotificationClient(uid_t uid, pid_t pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700292 void onAudioPortListUpdate();
293 void doOnAudioPortListUpdate();
294 void onAudioPatchListUpdate();
295 void doOnAudioPatchListUpdate();
Eric Laurent951f4552014-05-20 10:48:17 -0700296
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700297 void onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state);
298 void doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state);
Eric Laurenta9f86652018-11-28 17:23:11 -0800299 void onRecordingConfigurationUpdate(int event,
300 const record_client_info_t *clientInfo,
301 const audio_config_base_t *clientConfig,
302 std::vector<effect_descriptor_t> clientEffects,
303 const audio_config_base_t *deviceConfig,
304 std::vector<effect_descriptor_t> effects,
305 audio_patch_handle_t patchHandle,
306 audio_source_t source);
307 void doOnRecordingConfigurationUpdate(int event,
308 const record_client_info_t *clientInfo,
309 const audio_config_base_t *clientConfig,
310 std::vector<effect_descriptor_t> clientEffects,
311 const audio_config_base_t *deviceConfig,
312 std::vector<effect_descriptor_t> effects,
313 audio_patch_handle_t patchHandle,
314 audio_source_t source);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700315
François Gaffiecfe17322018-11-07 13:41:29 +0100316 void onAudioVolumeGroupChanged(volume_group_t group, int flags);
317 void doOnAudioVolumeGroupChanged(volume_group_t group, int flags);
Eric Laurentb20cf7d2019-04-05 19:37:34 -0700318 void setEffectSuspended(int effectId,
319 audio_session_t sessionId,
320 bool suspended);
François Gaffiecfe17322018-11-07 13:41:29 +0100321
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322private:
Mathias Agopiane762be92013-05-09 16:26:45 -0700323 AudioPolicyService() ANDROID_API;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 virtual ~AudioPolicyService();
325
326 status_t dumpInternals(int fd);
327
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800328 // Handles binder shell commands
329 virtual status_t shellCommand(int in, int out, int err, Vector<String16>& args);
330
331 // Sets whether the given UID records only silence
Eric Laurent5ada82e2019-08-29 17:53:54 -0700332 virtual void setAppState_l(audio_port_handle_t portId, app_state_t state);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800333
334 // Overrides the UID state as if it is idle
335 status_t handleSetUidState(Vector<String16>& args, int err);
336
337 // Clears the override for the UID state
338 status_t handleResetUidState(Vector<String16>& args, int err);
339
340 // Gets the UID state
341 status_t handleGetUidState(Vector<String16>& args, int out, int err);
342
343 // Prints the shell command help
344 status_t printHelp(int out);
345
Eric Laurent99fcae42018-05-17 16:59:18 -0700346 std::string getDeviceTypeStrForPortId(audio_port_handle_t portId);
347
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700348 status_t getAudioPolicyEffects(sp<AudioPolicyEffects>& audioPolicyEffects);
349
Eric Laurente8c8b432018-10-17 10:08:02 -0700350 app_state_t apmStatFromAmState(int amState);
351
Hayden Gomes524159d2019-12-23 14:41:47 -0800352 bool isSupportedSystemUsage(audio_usage_t usage);
353 status_t validateUsage(audio_usage_t usage);
354 status_t validateUsage(audio_usage_t usage, pid_t pid, uid_t uid);
355
Eric Laurente8c8b432018-10-17 10:08:02 -0700356 void updateUidStates();
357 void updateUidStates_l();
358
Michael Groovercfd28302018-12-11 19:16:46 -0800359 void silenceAllRecordings_l();
360
Eric Laurent2dc962b2019-03-01 08:25:25 -0800361 static bool isVirtualSource(audio_source_t source);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800362
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800363 // If recording we need to make sure the UID is allowed to do that. If the UID is idle
364 // then it cannot record and gets buffers with zeros - silence. As soon as the UID
365 // transitions to an active state we will start reporting buffers with data. This approach
366 // transparently handles recording while the UID transitions between idle/active state
367 // avoiding to get stuck in a state receiving non-empty buffers while idle or in a state
368 // receiving empty buffers while active.
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700369 class UidPolicy : public BnUidObserver, public virtual IBinder::DeathRecipient {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800370 public:
371 explicit UidPolicy(wp<AudioPolicyService> service)
Eric Laurent6ede98f2019-06-11 14:50:30 -0700372 : mService(service), mObserverRegistered(false),
373 mAssistantUid(0), mRttEnabled(false) {}
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800374
375 void registerSelf();
376 void unregisterSelf();
377
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700378 // IBinder::DeathRecipient implementation
379 void binderDied(const wp<IBinder> &who) override;
380
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800381 bool isUidActive(uid_t uid);
Eric Laurente8c8b432018-10-17 10:08:02 -0700382 int getUidState(uid_t uid);
Eric Laurentb78763e2018-10-17 10:08:02 -0700383 void setAssistantUid(uid_t uid) { mAssistantUid = uid; }
384 bool isAssistantUid(uid_t uid) { return uid == mAssistantUid; }
385 void setA11yUids(const std::vector<uid_t>& uids) { mA11yUids.clear(); mA11yUids = uids; }
386 bool isA11yUid(uid_t uid);
Eric Laurent4eb58f12018-12-07 16:41:02 -0800387 bool isA11yOnTop();
Eric Laurent6ede98f2019-06-11 14:50:30 -0700388 void setRttEnabled(bool enabled) { mRttEnabled = enabled; }
389 bool isRttEnabled() { return mRttEnabled; }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800390
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700391 // BnUidObserver implementation
392 void onUidActive(uid_t uid) override;
393 void onUidGone(uid_t uid, bool disabled) override;
394 void onUidIdle(uid_t uid, bool disabled) override;
Hui Yu13ad0eb2019-09-09 10:27:07 -0700395 void onUidStateChanged(uid_t uid, int32_t procState, int64_t procStateSeq,
396 int32_t capability);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800397
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700398 void addOverrideUid(uid_t uid, bool active) { updateOverrideUid(uid, active, true); }
399 void removeOverrideUid(uid_t uid) { updateOverrideUid(uid, false, false); }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800400
Eric Laurente8c8b432018-10-17 10:08:02 -0700401 void updateUid(std::unordered_map<uid_t, std::pair<bool, int>> *uids,
402 uid_t uid, bool active, int state, bool insert);
403
404 private:
405 void notifyService();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800406 void updateOverrideUid(uid_t uid, bool active, bool insert);
Eric Laurente8c8b432018-10-17 10:08:02 -0700407 void updateUidLocked(std::unordered_map<uid_t, std::pair<bool, int>> *uids,
408 uid_t uid, bool active, int state, bool insert);
409 void checkRegistered();
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800410
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800411 wp<AudioPolicyService> mService;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700412 Mutex mLock;
Steven Moreland2f348142019-07-02 15:59:07 -0700413 ActivityManager mAm;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700414 bool mObserverRegistered;
Eric Laurente8c8b432018-10-17 10:08:02 -0700415 std::unordered_map<uid_t, std::pair<bool, int>> mOverrideUids;
416 std::unordered_map<uid_t, std::pair<bool, int>> mCachedUids;
Eric Laurentb78763e2018-10-17 10:08:02 -0700417 uid_t mAssistantUid;
418 std::vector<uid_t> mA11yUids;
Eric Laurent6ede98f2019-06-11 14:50:30 -0700419 bool mRttEnabled;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800420 };
421
Michael Groovercfd28302018-12-11 19:16:46 -0800422 // If sensor privacy is enabled then all apps, including those that are active, should be
423 // prevented from recording. This is handled similar to idle UIDs, any app that attempts
424 // to record while sensor privacy is enabled will receive buffers with zeros. As soon as
425 // sensor privacy is disabled active apps will receive the expected data when recording.
426 class SensorPrivacyPolicy : public hardware::BnSensorPrivacyListener {
427 public:
428 explicit SensorPrivacyPolicy(wp<AudioPolicyService> service)
429 : mService(service) {}
430
431 void registerSelf();
432 void unregisterSelf();
433
434 bool isSensorPrivacyEnabled();
435
436 binder::Status onSensorPrivacyChanged(bool enabled);
437
438 private:
439 wp<AudioPolicyService> mService;
440 std::atomic_bool mSensorPrivacyEnabled;
441 };
442
Eric Laurentdcd4ab12018-06-29 17:45:13 -0700443 // Thread used to send audio config commands to audio flinger
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700444 // For audio config commands, it is necessary because audio flinger requires that the calling
445 // process (user) has permission to modify audio settings.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700446 class AudioCommandThread : public Thread {
447 class AudioCommand;
448 public:
449
450 // commands for tone AudioCommand
451 enum {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452 SET_VOLUME,
453 SET_PARAMETERS,
Eric Laurentbfb1b832013-01-07 09:53:42 -0800454 SET_VOICE_VOLUME,
455 STOP_OUTPUT,
Eric Laurent951f4552014-05-20 10:48:17 -0700456 RELEASE_OUTPUT,
457 CREATE_AUDIO_PATCH,
458 RELEASE_AUDIO_PATCH,
Eric Laurentb52c1522014-05-20 11:27:36 -0700459 UPDATE_AUDIOPORT_LIST,
Eric Laurente1715a42014-05-20 11:30:42 -0700460 UPDATE_AUDIOPATCH_LIST,
François Gaffiecfe17322018-11-07 13:41:29 +0100461 CHANGED_AUDIOVOLUMEGROUP,
Eric Laurente1715a42014-05-20 11:30:42 -0700462 SET_AUDIOPORT_CONFIG,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800463 DYN_POLICY_MIX_STATE_UPDATE,
Eric Laurentb20cf7d2019-04-05 19:37:34 -0700464 RECORDING_CONFIGURATION_UPDATE,
465 SET_EFFECT_SUSPENDED,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700466 };
467
Eric Laurentbfb1b832013-01-07 09:53:42 -0800468 AudioCommandThread (String8 name, const wp<AudioPolicyService>& service);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700469 virtual ~AudioCommandThread();
470
471 status_t dump(int fd);
472
473 // Thread virtuals
474 virtual void onFirstRef();
475 virtual bool threadLoop();
476
477 void exit();
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800478 status_t volumeCommand(audio_stream_type_t stream, float volume,
479 audio_io_handle_t output, int delayMs = 0);
480 status_t parametersCommand(audio_io_handle_t ioHandle,
481 const char *keyValuePairs, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700482 status_t voiceVolumeCommand(float volume, int delayMs = 0);
Eric Laurentd7fe0862018-07-14 16:48:01 -0700483 void stopOutputCommand(audio_port_handle_t portId);
484 void releaseOutputCommand(audio_port_handle_t portId);
Eric Laurent0ede8922014-05-09 18:04:42 -0700485 status_t sendCommand(sp<AudioCommand>& command, int delayMs = 0);
486 void insertCommand_l(sp<AudioCommand>& command, int delayMs = 0);
Eric Laurent951f4552014-05-20 10:48:17 -0700487 status_t createAudioPatchCommand(const struct audio_patch *patch,
488 audio_patch_handle_t *handle,
489 int delayMs);
490 status_t releaseAudioPatchCommand(audio_patch_handle_t handle,
491 int delayMs);
Eric Laurentb52c1522014-05-20 11:27:36 -0700492 void updateAudioPortListCommand();
493 void updateAudioPatchListCommand();
François Gaffiecfe17322018-11-07 13:41:29 +0100494 void changeAudioVolumeGroupCommand(volume_group_t group, int flags);
Eric Laurente1715a42014-05-20 11:30:42 -0700495 status_t setAudioPortConfigCommand(const struct audio_port_config *config,
496 int delayMs);
Eric Laurenta9f86652018-11-28 17:23:11 -0800497 void dynamicPolicyMixStateUpdateCommand(const String8& regId,
498 int32_t state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800499 void recordingConfigurationUpdateCommand(
Eric Laurenta9f86652018-11-28 17:23:11 -0800500 int event,
501 const record_client_info_t *clientInfo,
502 const audio_config_base_t *clientConfig,
503 std::vector<effect_descriptor_t> clientEffects,
504 const audio_config_base_t *deviceConfig,
505 std::vector<effect_descriptor_t> effects,
506 audio_patch_handle_t patchHandle,
507 audio_source_t source);
Eric Laurentb20cf7d2019-04-05 19:37:34 -0700508 void setEffectSuspendedCommand(int effectId,
509 audio_session_t sessionId,
510 bool suspended);
Eric Laurent951f4552014-05-20 10:48:17 -0700511 void insertCommand_l(AudioCommand *command, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700512 private:
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100513 class AudioCommandData;
514
Mathias Agopian65ab4712010-07-14 17:59:35 -0700515 // descriptor for requested tone playback event
Eric Laurent0ede8922014-05-09 18:04:42 -0700516 class AudioCommand: public RefBase {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700517
518 public:
519 AudioCommand()
Eric Laurent0ede8922014-05-09 18:04:42 -0700520 : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700521
522 void dump(char* buffer, size_t size);
523
Eric Laurentdcd4ab12018-06-29 17:45:13 -0700524 int mCommand; // SET_VOLUME, SET_PARAMETERS...
Mathias Agopian65ab4712010-07-14 17:59:35 -0700525 nsecs_t mTime; // time stamp
Eric Laurent0ede8922014-05-09 18:04:42 -0700526 Mutex mLock; // mutex associated to mCond
Mathias Agopian65ab4712010-07-14 17:59:35 -0700527 Condition mCond; // condition for status return
528 status_t mStatus; // command status
529 bool mWaitStatus; // true if caller is waiting for status
Eric Laurent0ede8922014-05-09 18:04:42 -0700530 sp<AudioCommandData> mParam; // command specific parameter data
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 };
532
Eric Laurent0ede8922014-05-09 18:04:42 -0700533 class AudioCommandData: public RefBase {
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100534 public:
535 virtual ~AudioCommandData() {}
536 protected:
537 AudioCommandData() {}
538 };
539
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100540 class VolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 public:
Glenn Kastenfff6d712012-01-12 16:38:12 -0800542 audio_stream_type_t mStream;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 float mVolume;
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800544 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545 };
546
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100547 class ParametersData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 public:
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800549 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 String8 mKeyValuePairs;
551 };
552
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100553 class VoiceVolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700554 public:
555 float mVolume;
556 };
557
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100558 class StopOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800559 public:
Eric Laurentd7fe0862018-07-14 16:48:01 -0700560 audio_port_handle_t mPortId;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800561 };
562
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100563 class ReleaseOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800564 public:
Eric Laurentd7fe0862018-07-14 16:48:01 -0700565 audio_port_handle_t mPortId;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800566 };
567
Eric Laurent951f4552014-05-20 10:48:17 -0700568 class CreateAudioPatchData : public AudioCommandData {
569 public:
570 struct audio_patch mPatch;
571 audio_patch_handle_t mHandle;
572 };
573
574 class ReleaseAudioPatchData : public AudioCommandData {
575 public:
576 audio_patch_handle_t mHandle;
577 };
578
François Gaffiecfe17322018-11-07 13:41:29 +0100579 class AudioVolumeGroupData : public AudioCommandData {
580 public:
581 volume_group_t mGroup;
582 int mFlags;
583 };
584
Eric Laurente1715a42014-05-20 11:30:42 -0700585 class SetAudioPortConfigData : public AudioCommandData {
586 public:
587 struct audio_port_config mConfig;
588 };
589
Jean-Michel Trivide801052015-04-14 19:10:14 -0700590 class DynPolicyMixStateUpdateData : public AudioCommandData {
591 public:
592 String8 mRegId;
593 int32_t mState;
594 };
595
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800596 class RecordingConfigurationUpdateData : public AudioCommandData {
597 public:
598 int mEvent;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800599 record_client_info_t mClientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800600 struct audio_config_base mClientConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -0800601 std::vector<effect_descriptor_t> mClientEffects;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800602 struct audio_config_base mDeviceConfig;
Eric Laurenta9f86652018-11-28 17:23:11 -0800603 std::vector<effect_descriptor_t> mEffects;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800604 audio_patch_handle_t mPatchHandle;
Eric Laurenta9f86652018-11-28 17:23:11 -0800605 audio_source_t mSource;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800606 };
607
Eric Laurentb20cf7d2019-04-05 19:37:34 -0700608 class SetEffectSuspendedData : public AudioCommandData {
609 public:
610 int mEffectId;
611 audio_session_t mSessionId;
612 bool mSuspended;
613 };
614
Mathias Agopian65ab4712010-07-14 17:59:35 -0700615 Mutex mLock;
616 Condition mWaitWorkCV;
Eric Laurent0ede8922014-05-09 18:04:42 -0700617 Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands
Eric Laurent0ede8922014-05-09 18:04:42 -0700618 sp<AudioCommand> mLastCommand; // last processed command (used by dump)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700619 String8 mName; // string used by wake lock fo delayed commands
Eric Laurentbfb1b832013-01-07 09:53:42 -0800620 wp<AudioPolicyService> mService;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 };
622
Eric Laurentdce54a12014-03-10 12:19:46 -0700623 class AudioPolicyClient : public AudioPolicyClientInterface
624 {
625 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700626 explicit AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {}
Eric Laurentdce54a12014-03-10 12:19:46 -0700627 virtual ~AudioPolicyClient() {}
628
629 //
630 // Audio HW module functions
631 //
632
633 // loads a HW module.
634 virtual audio_module_handle_t loadHwModule(const char *name);
635
636 //
637 // Audio output Control functions
638 //
639
640 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
641 // in case the audio policy manager has no specific requirements for the output being opened.
642 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
643 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
Eric Laurentcf2c0212014-07-25 16:20:43 -0700644 virtual status_t openOutput(audio_module_handle_t module,
645 audio_io_handle_t *output,
646 audio_config_t *config,
jiabin43810402019-10-24 14:58:31 -0700647 const sp<DeviceDescriptorBase>& device,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700648 uint32_t *latencyMs,
649 audio_output_flags_t flags);
Eric Laurentdce54a12014-03-10 12:19:46 -0700650 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
651 // a special mixer thread in the AudioFlinger.
652 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2);
653 // closes the output stream
654 virtual status_t closeOutput(audio_io_handle_t output);
655 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
656 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
657 virtual status_t suspendOutput(audio_io_handle_t output);
658 // restores a suspended output.
659 virtual status_t restoreOutput(audio_io_handle_t output);
660
661 //
662 // Audio input Control functions
663 //
664
665 // opens an audio input
666 virtual audio_io_handle_t openInput(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700667 audio_io_handle_t *input,
668 audio_config_t *config,
669 audio_devices_t *devices,
670 const String8& address,
671 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -0700672 audio_input_flags_t flags);
Eric Laurentdce54a12014-03-10 12:19:46 -0700673 // closes an audio input
674 virtual status_t closeInput(audio_io_handle_t input);
675 //
676 // misc control functions
677 //
678
679 // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
680 // for each output (destination device) it is attached to.
681 virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0);
682
683 // invalidate a stream type, causing a reroute to an unspecified new output
684 virtual status_t invalidateStream(audio_stream_type_t stream);
685
686 // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
687 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0);
688 // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager.
689 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
690
Eric Laurentdce54a12014-03-10 12:19:46 -0700691 // set down link audio volume.
692 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
693
694 // move effect to the specified output
Glenn Kastend848eb42016-03-08 13:42:11 -0800695 virtual status_t moveEffects(audio_session_t session,
Eric Laurentdce54a12014-03-10 12:19:46 -0700696 audio_io_handle_t srcOutput,
697 audio_io_handle_t dstOutput);
698
Eric Laurentb20cf7d2019-04-05 19:37:34 -0700699 void setEffectSuspended(int effectId,
700 audio_session_t sessionId,
701 bool suspended) override;
702
Eric Laurent951f4552014-05-20 10:48:17 -0700703 /* Create a patch between several source and sink ports */
704 virtual status_t createAudioPatch(const struct audio_patch *patch,
705 audio_patch_handle_t *handle,
706 int delayMs);
707
708 /* Release a patch */
709 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
710 int delayMs);
711
Eric Laurente1715a42014-05-20 11:30:42 -0700712 /* Set audio port configuration */
713 virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs);
714
Eric Laurentb52c1522014-05-20 11:27:36 -0700715 virtual void onAudioPortListUpdate();
716 virtual void onAudioPatchListUpdate();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700717 virtual void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800718 virtual void onRecordingConfigurationUpdate(int event,
Eric Laurenta9f86652018-11-28 17:23:11 -0800719 const record_client_info_t *clientInfo,
720 const audio_config_base_t *clientConfig,
721 std::vector<effect_descriptor_t> clientEffects,
722 const audio_config_base_t *deviceConfig,
723 std::vector<effect_descriptor_t> effects,
724 audio_patch_handle_t patchHandle,
725 audio_source_t source);
Eric Laurentb52c1522014-05-20 11:27:36 -0700726
François Gaffiecfe17322018-11-07 13:41:29 +0100727 virtual void onAudioVolumeGroupChanged(volume_group_t group, int flags);
728
Glenn Kasteneeecb982016-02-26 10:44:04 -0800729 virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use);
Eric Laurentde3f8392014-07-27 18:38:22 -0700730
Ytai Ben-Tsvi74cd6b02019-10-25 10:06:40 -0700731 void setSoundTriggerCaptureState(bool active) override;
732
Eric Laurentdce54a12014-03-10 12:19:46 -0700733 private:
734 AudioPolicyService *mAudioPolicyService;
735 };
736
Eric Laurentb52c1522014-05-20 11:27:36 -0700737 // --- Notification Client ---
738 class NotificationClient : public IBinder::DeathRecipient {
739 public:
740 NotificationClient(const sp<AudioPolicyService>& service,
741 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800742 uid_t uid, pid_t pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700743 virtual ~NotificationClient();
744
Jean-Michel Trivide801052015-04-14 19:10:14 -0700745 void onAudioPortListUpdate();
746 void onAudioPatchListUpdate();
Eric Laurenta9f86652018-11-28 17:23:11 -0800747 void onDynamicPolicyMixStateUpdate(const String8& regId,
748 int32_t state);
François Gaffiecfe17322018-11-07 13:41:29 +0100749 void onAudioVolumeGroupChanged(volume_group_t group, int flags);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800750 void onRecordingConfigurationUpdate(
Eric Laurenta9f86652018-11-28 17:23:11 -0800751 int event,
752 const record_client_info_t *clientInfo,
753 const audio_config_base_t *clientConfig,
754 std::vector<effect_descriptor_t> clientEffects,
755 const audio_config_base_t *deviceConfig,
756 std::vector<effect_descriptor_t> effects,
757 audio_patch_handle_t patchHandle,
758 audio_source_t source);
Eric Laurente8726fe2015-06-26 09:39:24 -0700759 void setAudioPortCallbacksEnabled(bool enabled);
François Gaffiecfe17322018-11-07 13:41:29 +0100760 void setAudioVolumeGroupCallbacksEnabled(bool enabled);
Eric Laurentb52c1522014-05-20 11:27:36 -0700761
luochaojiang908c7d72018-06-21 14:58:04 +0800762 uid_t uid() {
763 return mUid;
764 }
765
Eric Laurentb52c1522014-05-20 11:27:36 -0700766 // IBinder::DeathRecipient
767 virtual void binderDied(const wp<IBinder>& who);
768
769 private:
770 NotificationClient(const NotificationClient&);
771 NotificationClient& operator = (const NotificationClient&);
772
773 const wp<AudioPolicyService> mService;
774 const uid_t mUid;
luochaojiang908c7d72018-06-21 14:58:04 +0800775 const pid_t mPid;
Eric Laurentb52c1522014-05-20 11:27:36 -0700776 const sp<IAudioPolicyServiceClient> mAudioPolicyServiceClient;
Eric Laurente8726fe2015-06-26 09:39:24 -0700777 bool mAudioPortCallbacksEnabled;
François Gaffiecfe17322018-11-07 13:41:29 +0100778 bool mAudioVolumeGroupCallbacksEnabled;
Eric Laurentb52c1522014-05-20 11:27:36 -0700779 };
780
Eric Laurentd7fe0862018-07-14 16:48:01 -0700781 class AudioClient : public virtual RefBase {
782 public:
783 AudioClient(const audio_attributes_t attributes,
784 const audio_io_handle_t io, uid_t uid, pid_t pid,
Eric Laurent5ada82e2019-08-29 17:53:54 -0700785 const audio_session_t session, audio_port_handle_t portId,
786 const audio_port_handle_t deviceId) :
Eric Laurentd7fe0862018-07-14 16:48:01 -0700787 attributes(attributes), io(io), uid(uid), pid(pid),
Eric Laurent5ada82e2019-08-29 17:53:54 -0700788 session(session), portId(portId), deviceId(deviceId), active(false) {}
Eric Laurentd7fe0862018-07-14 16:48:01 -0700789 ~AudioClient() override = default;
790
791
792 const audio_attributes_t attributes; // source, flags ...
793 const audio_io_handle_t io; // audio HAL stream IO handle
794 const uid_t uid; // client UID
795 const pid_t pid; // client PID
796 const audio_session_t session; // audio session ID
Eric Laurent5ada82e2019-08-29 17:53:54 -0700797 const audio_port_handle_t portId;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700798 const audio_port_handle_t deviceId; // selected input device port ID
799 bool active; // Playback/Capture is active or inactive
800 };
801
Eric Laurentfee19762018-01-29 18:44:13 -0800802 // --- AudioRecordClient ---
803 // Information about each registered AudioRecord client
804 // (between calls to getInputForAttr() and releaseInput())
Eric Laurentd7fe0862018-07-14 16:48:01 -0700805 class AudioRecordClient : public AudioClient {
Eric Laurentfee19762018-01-29 18:44:13 -0800806 public:
807 AudioRecordClient(const audio_attributes_t attributes,
Eric Laurentd7fe0862018-07-14 16:48:01 -0700808 const audio_io_handle_t io, uid_t uid, pid_t pid,
Eric Laurent5ada82e2019-08-29 17:53:54 -0700809 const audio_session_t session, audio_port_handle_t portId,
810 const audio_port_handle_t deviceId, const String16& opPackageName,
Ricardo Correaac26cf72020-01-06 14:43:38 -0800811 bool canCaptureCallOrOutput, bool canCaptureHotword) :
Eric Laurent5ada82e2019-08-29 17:53:54 -0700812 AudioClient(attributes, io, uid, pid, session, portId, deviceId),
Eric Laurent1ff16a72019-03-14 18:35:04 -0700813 opPackageName(opPackageName), startTimeNs(0),
Ricardo Correaac26cf72020-01-06 14:43:38 -0800814 canCaptureCallOrOutput(canCaptureCallOrOutput),
815 canCaptureHotword(canCaptureHotword) {}
Eric Laurentd7fe0862018-07-14 16:48:01 -0700816 ~AudioRecordClient() override = default;
Eric Laurentfee19762018-01-29 18:44:13 -0800817
Eric Laurentfee19762018-01-29 18:44:13 -0800818 const String16 opPackageName; // client package name
Eric Laurent4eb58f12018-12-07 16:41:02 -0800819 nsecs_t startTimeNs;
Ricardo Correaac26cf72020-01-06 14:43:38 -0800820 const bool canCaptureCallOrOutput;
Eric Laurent1ff16a72019-03-14 18:35:04 -0700821 const bool canCaptureHotword;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700822 };
823
824 // --- AudioPlaybackClient ---
825 // Information about each registered AudioTrack client
826 // (between calls to getOutputForAttr() and releaseOutput())
827 class AudioPlaybackClient : public AudioClient {
828 public:
829 AudioPlaybackClient(const audio_attributes_t attributes,
830 const audio_io_handle_t io, uid_t uid, pid_t pid,
Eric Laurent5ada82e2019-08-29 17:53:54 -0700831 const audio_session_t session, audio_port_handle_t portId,
832 audio_port_handle_t deviceId, audio_stream_type_t stream) :
833 AudioClient(attributes, io, uid, pid, session, portId, deviceId), stream(stream) {}
Eric Laurentd7fe0862018-07-14 16:48:01 -0700834 ~AudioPlaybackClient() override = default;
835
836 const audio_stream_type_t stream;
Eric Laurentfee19762018-01-29 18:44:13 -0800837 };
838
Eric Laurentbcfe5be2019-04-09 19:56:39 -0700839 void getPlaybackClientAndEffects(audio_port_handle_t portId,
840 sp<AudioPlaybackClient>& client,
841 sp<AudioPolicyEffects>& effects,
842 const char *context);
843
844
Eric Laurent10b71232018-04-13 18:14:44 -0700845 // A class automatically clearing and restoring binder caller identity inside
846 // a code block (scoped variable)
847 // Declare one systematically before calling AudioPolicyManager methods so that they are
848 // executed with the same level of privilege as audioserver process.
849 class AutoCallerClear {
850 public:
851 AutoCallerClear() :
852 mToken(IPCThreadState::self()->clearCallingIdentity()) {}
853 ~AutoCallerClear() {
854 IPCThreadState::self()->restoreCallingIdentity(mToken);
855 }
856
857 private:
858 const int64_t mToken;
859 };
860
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861 // Internal dump utilities.
862 status_t dumpPermissionDenial(int fd);
863
864
Eric Laurenteda6c362011-02-02 09:33:30 -0800865 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
866 // device connection state or routing
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700867 // Note: lock acquisition order is always mLock > mEffectsLock:
868 // mLock protects AudioPolicyManager methods that can call into audio flinger
869 // and possibly back in to audio policy service and acquire mEffectsLock.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700870 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread
Eric Laurentbfb1b832013-01-07 09:53:42 -0800871 sp<AudioCommandThread> mOutputCommandThread; // process stop and release output
Dima Zavinfce7a472011-04-19 22:30:36 -0700872 struct audio_policy_device *mpAudioPolicyDev;
873 struct audio_policy *mpAudioPolicy;
Eric Laurentf269b8e2014-06-09 20:01:29 -0700874 AudioPolicyInterface *mAudioPolicyManager;
Eric Laurentdce54a12014-03-10 12:19:46 -0700875 AudioPolicyClient *mAudioPolicyClient;
Hayden Gomes524159d2019-12-23 14:41:47 -0800876 std::vector<audio_usage_t> mSupportedSystemUsages;
Eric Laurentdce54a12014-03-10 12:19:46 -0700877
luochaojiang908c7d72018-06-21 14:58:04 +0800878 DefaultKeyedVector< int64_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800879 Mutex mNotificationClientsLock; // protects mNotificationClients
bryant_liuba2b4392014-06-11 16:49:30 +0800880 // Manage all effects configured in audio_effects.conf
Eric Laurent6c796322019-04-09 14:13:17 -0700881 // never hold AudioPolicyService::mLock when calling AudioPolicyEffects methods as
882 // those can call back into AudioPolicyService methods and try to acquire the mutex
bryant_liuba2b4392014-06-11 16:49:30 +0800883 sp<AudioPolicyEffects> mAudioPolicyEffects;
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700884 audio_mode_t mPhoneState;
Eric Laurent00dba062020-02-11 15:52:09 -0800885 uid_t mPhoneStateOwnerUid;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800886
887 sp<UidPolicy> mUidPolicy;
Michael Groovercfd28302018-12-11 19:16:46 -0800888 sp<SensorPrivacyPolicy> mSensorPrivacyPolicy;
889
Eric Laurentfee19762018-01-29 18:44:13 -0800890 DefaultKeyedVector< audio_port_handle_t, sp<AudioRecordClient> > mAudioRecordClients;
Eric Laurentd7fe0862018-07-14 16:48:01 -0700891 DefaultKeyedVector< audio_port_handle_t, sp<AudioPlaybackClient> > mAudioPlaybackClients;
Kevin Rocard8be94972019-02-22 13:26:25 -0800892
893 MediaPackageManager mPackageManager; // To check allowPlaybackCapture
Mathias Agopian65ab4712010-07-14 17:59:35 -0700894};
895
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800896} // namespace android
Mathias Agopian65ab4712010-07-14 17:59:35 -0700897
898#endif // ANDROID_AUDIOPOLICYSERVICE_H