blob: 407d7a5cde52c38413fa5f4feed5636f8223749d [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>
Glenn Kastend2dcb082011-02-03 16:55:26 -080026#include <binder/BinderService.h>
Svet Ganovf4ddfef2018-01-16 07:37:58 -080027#include <binder/IUidObserver.h>
Dima Zavin64760242011-05-11 14:15:23 -070028#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070029#include <system/audio_policy.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070030#include <media/IAudioPolicyService.h>
31#include <media/ToneGenerator.h>
32#include <media/AudioEffect.h>
Eric Laurentbaac1832014-12-01 17:52:59 -080033#include <media/AudioPolicy.h>
bryant_liuba2b4392014-06-11 16:49:30 +080034#include "AudioPolicyEffects.h"
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080035#include "managerdefault/AudioPolicyManager.h"
Dima Zavinfce7a472011-04-19 22:30:36 -070036
Svet Ganovf4ddfef2018-01-16 07:37:58 -080037#include <unordered_map>
bryant_liuba2b4392014-06-11 16:49:30 +080038
Mathias Agopian65ab4712010-07-14 17:59:35 -070039namespace android {
40
Svet Ganovf4ddfef2018-01-16 07:37:58 -080041using namespace std;
42
Mathias Agopian65ab4712010-07-14 17:59:35 -070043// ----------------------------------------------------------------------------
44
Glenn Kastend2dcb082011-02-03 16:55:26 -080045class AudioPolicyService :
46 public BinderService<AudioPolicyService>,
47 public BnAudioPolicyService,
Eric Laurentde070132010-07-13 04:45:46 -070048 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070049{
Glenn Kastend2dcb082011-02-03 16:55:26 -080050 friend class BinderService<AudioPolicyService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
52public:
Glenn Kastend2dcb082011-02-03 16:55:26 -080053 // for BinderService
Mathias Agopiane762be92013-05-09 16:26:45 -070054 static const char *getServiceName() ANDROID_API { return "media.audio_policy"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070055
56 virtual status_t dump(int fd, const Vector<String16>& args);
57
58 //
59 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
60 //
61
Dima Zavinfce7a472011-04-19 22:30:36 -070062 virtual status_t setDeviceConnectionState(audio_devices_t device,
63 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080064 const char *device_address,
65 const char *device_name);
Dima Zavinfce7a472011-04-19 22:30:36 -070066 virtual audio_policy_dev_state_t getDeviceConnectionState(
67 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -070068 const char *device_address);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -080069 virtual status_t handleDeviceConfigChange(audio_devices_t device,
70 const char *device_address,
71 const char *device_name);
Glenn Kastenf78aee72012-01-04 11:00:47 -080072 virtual status_t setPhoneState(audio_mode_t state);
Dima Zavinfce7a472011-04-19 22:30:36 -070073 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
74 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
Eric Laurentf4e63452017-11-06 19:31:46 +000075 virtual audio_io_handle_t getOutput(audio_stream_type_t stream);
Eric Laurente83b55d2014-11-14 10:06:21 -080076 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
77 audio_io_handle_t *output,
78 audio_session_t session,
79 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +020080 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -070081 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -080082 const audio_config_t *config,
83 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -070084 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -080085 audio_port_handle_t *portId);
Eric Laurentde070132010-07-13 04:45:46 -070086 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070087 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -080088 audio_session_t session);
Eric Laurentde070132010-07-13 04:45:46 -070089 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070090 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -080091 audio_session_t session);
92 virtual void releaseOutput(audio_io_handle_t output,
93 audio_stream_type_t stream,
94 audio_session_t session);
Eric Laurentcaf7f482014-11-25 17:50:47 -080095 virtual status_t getInputForAttr(const audio_attributes_t *attr,
96 audio_io_handle_t *input,
97 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 Laurentfee19762018-01-29 18:44:13 -0800105 virtual status_t startInput(audio_port_handle_t portId,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800106 bool *silenced);
Eric Laurentfee19762018-01-29 18:44:13 -0800107 virtual status_t stopInput(audio_port_handle_t portId);
108 virtual void releaseInput(audio_port_handle_t portId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700109 virtual status_t initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700110 int indexMin,
111 int indexMax);
Eric Laurent83844cc2011-11-18 16:43:31 -0800112 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
113 int index,
114 audio_devices_t device);
115 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
116 int *index,
117 audio_devices_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700118
Dima Zavinfce7a472011-04-19 22:30:36 -0700119 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Eric Laurent63742522012-03-08 13:42:42 -0800120 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurentde070132010-07-13 04:45:46 -0700121
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700122 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
123 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700124 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700125 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800126 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700127 int id);
128 virtual status_t unregisterEffect(int id);
Eric Laurentdb7c0792011-08-10 10:37:50 -0700129 virtual status_t setEffectEnabled(int id, bool enabled);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800130 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800131 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700132 virtual bool isSourceActive(audio_source_t source) const;
Eric Laurentde070132010-07-13 04:45:46 -0700133
Glenn Kastend848eb42016-03-08 13:42:11 -0800134 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700135 effect_descriptor_t *descriptors,
136 uint32_t *count);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700137 virtual status_t onTransact(
138 uint32_t code,
139 const Parcel& data,
140 Parcel* reply,
141 uint32_t flags);
142
143 // IBinder::DeathRecipient
144 virtual void binderDied(const wp<IBinder>& who);
145
Eric Laurentf5ada6e2014-10-09 17:49:00 -0700146 // RefBase
147 virtual void onFirstRef();
148
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700150 // Helpers for the struct audio_policy_service_ops implementation.
151 // This is used by the audio policy manager for certain operations that
152 // are implemented by the policy service.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700153 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700154 virtual void setParameters(audio_io_handle_t ioHandle,
155 const char *keyValuePairs,
156 int delayMs);
157
158 virtual status_t setStreamVolume(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700159 float volume,
160 audio_io_handle_t output,
161 int delayMs = 0);
Dima Zavinfce7a472011-04-19 22:30:36 -0700162 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700163 virtual status_t stopTone();
164 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000165 virtual bool isOffloadSupported(const audio_offload_info_t &config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700166
Eric Laurent203b1a12014-04-01 10:34:16 -0700167 virtual status_t listAudioPorts(audio_port_role_t role,
168 audio_port_type_t type,
169 unsigned int *num_ports,
170 struct audio_port *ports,
171 unsigned int *generation);
172 virtual status_t getAudioPort(struct audio_port *port);
173 virtual status_t createAudioPatch(const struct audio_patch *patch,
174 audio_patch_handle_t *handle);
175 virtual status_t releaseAudioPatch(audio_patch_handle_t handle);
176 virtual status_t listAudioPatches(unsigned int *num_patches,
177 struct audio_patch *patches,
178 unsigned int *generation);
179 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
180
Eric Laurentb52c1522014-05-20 11:27:36 -0700181 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client);
182
Eric Laurente8726fe2015-06-26 09:39:24 -0700183 virtual void setAudioPortCallbacksEnabled(bool enabled);
184
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700185 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
186 audio_io_handle_t *ioHandle,
187 audio_devices_t *device);
188
189 virtual status_t releaseSoundTriggerSession(audio_session_t session);
190
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700191 virtual audio_mode_t getPhoneState();
192
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700193 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration);
Eric Laurentbaac1832014-12-01 17:52:59 -0800194
Eric Laurent554a2772015-04-10 11:29:24 -0700195 virtual status_t startAudioSource(const struct audio_port_config *source,
196 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -0700197 audio_patch_handle_t *handle);
198 virtual status_t stopAudioSource(audio_patch_handle_t handle);
Eric Laurent554a2772015-04-10 11:29:24 -0700199
Andy Hung2ddee192015-12-18 17:34:44 -0800200 virtual status_t setMasterMono(bool mono);
201 virtual status_t getMasterMono(bool *mono);
202
Eric Laurentac9cef52017-06-09 15:46:26 -0700203 virtual float getStreamVolumeDB(
204 audio_stream_type_t stream, int index, audio_devices_t device);
205
jiabin81772902018-04-02 17:52:27 -0700206 virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
207 audio_format_t *surroundFormats,
208 bool *surroundFormatsEnabled,
209 bool reported);
210 virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled);
211
Eric Laurentbfb1b832013-01-07 09:53:42 -0800212 status_t doStopOutput(audio_io_handle_t output,
213 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800214 audio_session_t session);
215 void doReleaseOutput(audio_io_handle_t output,
216 audio_stream_type_t stream,
217 audio_session_t session);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800218
Eric Laurent951f4552014-05-20 10:48:17 -0700219 status_t clientCreateAudioPatch(const struct audio_patch *patch,
220 audio_patch_handle_t *handle,
221 int delayMs);
222 status_t clientReleaseAudioPatch(audio_patch_handle_t handle,
223 int delayMs);
Eric Laurente1715a42014-05-20 11:30:42 -0700224 virtual status_t clientSetAudioPortConfig(const struct audio_port_config *config,
225 int delayMs);
226
Eric Laurentb52c1522014-05-20 11:27:36 -0700227 void removeNotificationClient(uid_t uid);
228 void onAudioPortListUpdate();
229 void doOnAudioPortListUpdate();
230 void onAudioPatchListUpdate();
231 void doOnAudioPatchListUpdate();
Eric Laurent951f4552014-05-20 10:48:17 -0700232
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700233 void onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state);
234 void doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state);
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800235 void onRecordingConfigurationUpdate(int event, const record_client_info_t *clientInfo,
236 const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800237 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle);
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800238 void doOnRecordingConfigurationUpdate(int event, const record_client_info_t *clientInfo,
239 const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800240 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700241
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242private:
Mathias Agopiane762be92013-05-09 16:26:45 -0700243 AudioPolicyService() ANDROID_API;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244 virtual ~AudioPolicyService();
245
246 status_t dumpInternals(int fd);
247
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800248 // Handles binder shell commands
249 virtual status_t shellCommand(int in, int out, int err, Vector<String16>& args);
250
251 // Sets whether the given UID records only silence
252 virtual void setRecordSilenced(uid_t uid, bool silenced);
253
254 // Overrides the UID state as if it is idle
255 status_t handleSetUidState(Vector<String16>& args, int err);
256
257 // Clears the override for the UID state
258 status_t handleResetUidState(Vector<String16>& args, int err);
259
260 // Gets the UID state
261 status_t handleGetUidState(Vector<String16>& args, int out, int err);
262
263 // Prints the shell command help
264 status_t printHelp(int out);
265
Eric Laurent99fcae42018-05-17 16:59:18 -0700266 std::string getDeviceTypeStrForPortId(audio_port_handle_t portId);
267
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800268 // If recording we need to make sure the UID is allowed to do that. If the UID is idle
269 // then it cannot record and gets buffers with zeros - silence. As soon as the UID
270 // transitions to an active state we will start reporting buffers with data. This approach
271 // transparently handles recording while the UID transitions between idle/active state
272 // avoiding to get stuck in a state receiving non-empty buffers while idle or in a state
273 // receiving empty buffers while active.
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700274 class UidPolicy : public BnUidObserver, public virtual IBinder::DeathRecipient {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800275 public:
276 explicit UidPolicy(wp<AudioPolicyService> service)
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700277 : mService(service), mObserverRegistered(false) {}
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800278
279 void registerSelf();
280 void unregisterSelf();
281
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700282 // IBinder::DeathRecipient implementation
283 void binderDied(const wp<IBinder> &who) override;
284
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800285 bool isUidActive(uid_t uid);
286
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700287 // BnUidObserver implementation
288 void onUidActive(uid_t uid) override;
289 void onUidGone(uid_t uid, bool disabled) override;
290 void onUidIdle(uid_t uid, bool disabled) override;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800291
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700292 void addOverrideUid(uid_t uid, bool active) { updateOverrideUid(uid, active, true); }
293 void removeOverrideUid(uid_t uid) { updateOverrideUid(uid, false, false); }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800294
295 private:
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700296 bool isServiceUid(uid_t uid) const;
297 void notifyService(uid_t uid, bool active);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800298 void updateOverrideUid(uid_t uid, bool active, bool insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700299 void updateUidCache(uid_t uid, bool active, bool insert);
300 void updateUidLocked(std::unordered_map<uid_t, bool> *uids,
301 uid_t uid, bool active, bool insert, bool *wasThere, bool *wasActive);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800302
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800303 wp<AudioPolicyService> mService;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700304 Mutex mLock;
305 bool mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800306 std::unordered_map<uid_t, bool> mOverrideUids;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700307 std::unordered_map<uid_t, bool> mCachedUids;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800308 };
309
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310 // Thread used for tone playback and to send audio config commands to audio flinger
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700311 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because
312 // startTone() and stopTone() are normally called with mLock locked and requesting a tone start
313 // or stop will cause calls to AudioPolicyService and an attempt to lock mLock.
314 // For audio config commands, it is necessary because audio flinger requires that the calling
315 // process (user) has permission to modify audio settings.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316 class AudioCommandThread : public Thread {
317 class AudioCommand;
318 public:
319
320 // commands for tone AudioCommand
321 enum {
322 START_TONE,
323 STOP_TONE,
324 SET_VOLUME,
325 SET_PARAMETERS,
Eric Laurentbfb1b832013-01-07 09:53:42 -0800326 SET_VOICE_VOLUME,
327 STOP_OUTPUT,
Eric Laurent951f4552014-05-20 10:48:17 -0700328 RELEASE_OUTPUT,
329 CREATE_AUDIO_PATCH,
330 RELEASE_AUDIO_PATCH,
Eric Laurentb52c1522014-05-20 11:27:36 -0700331 UPDATE_AUDIOPORT_LIST,
Eric Laurente1715a42014-05-20 11:30:42 -0700332 UPDATE_AUDIOPATCH_LIST,
333 SET_AUDIOPORT_CONFIG,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800334 DYN_POLICY_MIX_STATE_UPDATE,
335 RECORDING_CONFIGURATION_UPDATE
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 };
337
Eric Laurentbfb1b832013-01-07 09:53:42 -0800338 AudioCommandThread (String8 name, const wp<AudioPolicyService>& service);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700339 virtual ~AudioCommandThread();
340
341 status_t dump(int fd);
342
343 // Thread virtuals
344 virtual void onFirstRef();
345 virtual bool threadLoop();
346
347 void exit();
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800348 void startToneCommand(ToneGenerator::tone_type type,
349 audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350 void stopToneCommand();
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800351 status_t volumeCommand(audio_stream_type_t stream, float volume,
352 audio_io_handle_t output, int delayMs = 0);
353 status_t parametersCommand(audio_io_handle_t ioHandle,
354 const char *keyValuePairs, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355 status_t voiceVolumeCommand(float volume, int delayMs = 0);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800356 void stopOutputCommand(audio_io_handle_t output,
357 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800358 audio_session_t session);
359 void releaseOutputCommand(audio_io_handle_t output,
360 audio_stream_type_t stream,
361 audio_session_t session);
Eric Laurent0ede8922014-05-09 18:04:42 -0700362 status_t sendCommand(sp<AudioCommand>& command, int delayMs = 0);
363 void insertCommand_l(sp<AudioCommand>& command, int delayMs = 0);
Eric Laurent951f4552014-05-20 10:48:17 -0700364 status_t createAudioPatchCommand(const struct audio_patch *patch,
365 audio_patch_handle_t *handle,
366 int delayMs);
367 status_t releaseAudioPatchCommand(audio_patch_handle_t handle,
368 int delayMs);
Eric Laurentb52c1522014-05-20 11:27:36 -0700369 void updateAudioPortListCommand();
370 void updateAudioPatchListCommand();
Eric Laurente1715a42014-05-20 11:30:42 -0700371 status_t setAudioPortConfigCommand(const struct audio_port_config *config,
372 int delayMs);
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700373 void dynamicPolicyMixStateUpdateCommand(const String8& regId, int32_t state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800374 void recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800375 int event,
376 const record_client_info_t *clientInfo,
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800377 const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800378 const audio_config_base_t *deviceConfig,
379 audio_patch_handle_t patchHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700380 void insertCommand_l(AudioCommand *command, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381 private:
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100382 class AudioCommandData;
383
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 // descriptor for requested tone playback event
Eric Laurent0ede8922014-05-09 18:04:42 -0700385 class AudioCommand: public RefBase {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700386
387 public:
388 AudioCommand()
Eric Laurent0ede8922014-05-09 18:04:42 -0700389 : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700390
391 void dump(char* buffer, size_t size);
392
393 int mCommand; // START_TONE, STOP_TONE ...
394 nsecs_t mTime; // time stamp
Eric Laurent0ede8922014-05-09 18:04:42 -0700395 Mutex mLock; // mutex associated to mCond
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396 Condition mCond; // condition for status return
397 status_t mStatus; // command status
398 bool mWaitStatus; // true if caller is waiting for status
Eric Laurent0ede8922014-05-09 18:04:42 -0700399 sp<AudioCommandData> mParam; // command specific parameter data
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400 };
401
Eric Laurent0ede8922014-05-09 18:04:42 -0700402 class AudioCommandData: public RefBase {
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100403 public:
404 virtual ~AudioCommandData() {}
405 protected:
406 AudioCommandData() {}
407 };
408
409 class ToneData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 public:
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800411 ToneGenerator::tone_type mType; // tone type (START_TONE only)
Glenn Kastenfff6d712012-01-12 16:38:12 -0800412 audio_stream_type_t mStream; // stream type (START_TONE only)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413 };
414
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100415 class VolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700416 public:
Glenn Kastenfff6d712012-01-12 16:38:12 -0800417 audio_stream_type_t mStream;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700418 float mVolume;
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800419 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420 };
421
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100422 class ParametersData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700423 public:
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800424 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425 String8 mKeyValuePairs;
426 };
427
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100428 class VoiceVolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700429 public:
430 float mVolume;
431 };
432
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100433 class StopOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800434 public:
435 audio_io_handle_t mIO;
436 audio_stream_type_t mStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800437 audio_session_t mSession;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800438 };
439
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100440 class ReleaseOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800441 public:
442 audio_io_handle_t mIO;
Eric Laurente83b55d2014-11-14 10:06:21 -0800443 audio_stream_type_t mStream;
444 audio_session_t mSession;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800445 };
446
Eric Laurent951f4552014-05-20 10:48:17 -0700447 class CreateAudioPatchData : public AudioCommandData {
448 public:
449 struct audio_patch mPatch;
450 audio_patch_handle_t mHandle;
451 };
452
453 class ReleaseAudioPatchData : public AudioCommandData {
454 public:
455 audio_patch_handle_t mHandle;
456 };
457
Eric Laurente1715a42014-05-20 11:30:42 -0700458 class SetAudioPortConfigData : public AudioCommandData {
459 public:
460 struct audio_port_config mConfig;
461 };
462
Jean-Michel Trivide801052015-04-14 19:10:14 -0700463 class DynPolicyMixStateUpdateData : public AudioCommandData {
464 public:
465 String8 mRegId;
466 int32_t mState;
467 };
468
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800469 class RecordingConfigurationUpdateData : public AudioCommandData {
470 public:
471 int mEvent;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800472 record_client_info_t mClientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800473 struct audio_config_base mClientConfig;
474 struct audio_config_base mDeviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800475 audio_patch_handle_t mPatchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800476 };
477
Mathias Agopian65ab4712010-07-14 17:59:35 -0700478 Mutex mLock;
479 Condition mWaitWorkCV;
Eric Laurent0ede8922014-05-09 18:04:42 -0700480 Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands
Mathias Agopian65ab4712010-07-14 17:59:35 -0700481 ToneGenerator *mpToneGenerator; // the tone generator
Eric Laurent0ede8922014-05-09 18:04:42 -0700482 sp<AudioCommand> mLastCommand; // last processed command (used by dump)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700483 String8 mName; // string used by wake lock fo delayed commands
Eric Laurentbfb1b832013-01-07 09:53:42 -0800484 wp<AudioPolicyService> mService;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700485 };
486
Eric Laurentdce54a12014-03-10 12:19:46 -0700487 class AudioPolicyClient : public AudioPolicyClientInterface
488 {
489 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700490 explicit AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {}
Eric Laurentdce54a12014-03-10 12:19:46 -0700491 virtual ~AudioPolicyClient() {}
492
493 //
494 // Audio HW module functions
495 //
496
497 // loads a HW module.
498 virtual audio_module_handle_t loadHwModule(const char *name);
499
500 //
501 // Audio output Control functions
502 //
503
504 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
505 // in case the audio policy manager has no specific requirements for the output being opened.
506 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
507 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
Eric Laurentcf2c0212014-07-25 16:20:43 -0700508 virtual status_t openOutput(audio_module_handle_t module,
509 audio_io_handle_t *output,
510 audio_config_t *config,
511 audio_devices_t *devices,
512 const String8& address,
513 uint32_t *latencyMs,
514 audio_output_flags_t flags);
Eric Laurentdce54a12014-03-10 12:19:46 -0700515 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
516 // a special mixer thread in the AudioFlinger.
517 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2);
518 // closes the output stream
519 virtual status_t closeOutput(audio_io_handle_t output);
520 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
521 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
522 virtual status_t suspendOutput(audio_io_handle_t output);
523 // restores a suspended output.
524 virtual status_t restoreOutput(audio_io_handle_t output);
525
526 //
527 // Audio input Control functions
528 //
529
530 // opens an audio input
531 virtual audio_io_handle_t openInput(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700532 audio_io_handle_t *input,
533 audio_config_t *config,
534 audio_devices_t *devices,
535 const String8& address,
536 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -0700537 audio_input_flags_t flags);
Eric Laurentdce54a12014-03-10 12:19:46 -0700538 // closes an audio input
539 virtual status_t closeInput(audio_io_handle_t input);
540 //
541 // misc control functions
542 //
543
544 // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
545 // for each output (destination device) it is attached to.
546 virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0);
547
548 // invalidate a stream type, causing a reroute to an unspecified new output
549 virtual status_t invalidateStream(audio_stream_type_t stream);
550
551 // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
552 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0);
553 // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager.
554 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
555
556 // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing
557 // over a telephony device during a phone call.
558 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
559 virtual status_t stopTone();
560
561 // set down link audio volume.
562 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
563
564 // move effect to the specified output
Glenn Kastend848eb42016-03-08 13:42:11 -0800565 virtual status_t moveEffects(audio_session_t session,
Eric Laurentdce54a12014-03-10 12:19:46 -0700566 audio_io_handle_t srcOutput,
567 audio_io_handle_t dstOutput);
568
Eric Laurent951f4552014-05-20 10:48:17 -0700569 /* Create a patch between several source and sink ports */
570 virtual status_t createAudioPatch(const struct audio_patch *patch,
571 audio_patch_handle_t *handle,
572 int delayMs);
573
574 /* Release a patch */
575 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
576 int delayMs);
577
Eric Laurente1715a42014-05-20 11:30:42 -0700578 /* Set audio port configuration */
579 virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs);
580
Eric Laurentb52c1522014-05-20 11:27:36 -0700581 virtual void onAudioPortListUpdate();
582 virtual void onAudioPatchListUpdate();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700583 virtual void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800584 virtual void onRecordingConfigurationUpdate(int event,
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800585 const record_client_info_t *clientInfo,
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800586 const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800587 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle);
Eric Laurentb52c1522014-05-20 11:27:36 -0700588
Glenn Kasteneeecb982016-02-26 10:44:04 -0800589 virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use);
Eric Laurentde3f8392014-07-27 18:38:22 -0700590
Eric Laurentdce54a12014-03-10 12:19:46 -0700591 private:
592 AudioPolicyService *mAudioPolicyService;
593 };
594
Eric Laurentb52c1522014-05-20 11:27:36 -0700595 // --- Notification Client ---
596 class NotificationClient : public IBinder::DeathRecipient {
597 public:
598 NotificationClient(const sp<AudioPolicyService>& service,
599 const sp<IAudioPolicyServiceClient>& client,
600 uid_t uid);
601 virtual ~NotificationClient();
602
Jean-Michel Trivide801052015-04-14 19:10:14 -0700603 void onAudioPortListUpdate();
604 void onAudioPatchListUpdate();
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700605 void onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800606 void onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800607 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800608 const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800609 const audio_config_base_t *deviceConfig,
610 audio_patch_handle_t patchHandle);
Eric Laurente8726fe2015-06-26 09:39:24 -0700611 void setAudioPortCallbacksEnabled(bool enabled);
Eric Laurentb52c1522014-05-20 11:27:36 -0700612
613 // IBinder::DeathRecipient
614 virtual void binderDied(const wp<IBinder>& who);
615
616 private:
617 NotificationClient(const NotificationClient&);
618 NotificationClient& operator = (const NotificationClient&);
619
620 const wp<AudioPolicyService> mService;
621 const uid_t mUid;
622 const sp<IAudioPolicyServiceClient> mAudioPolicyServiceClient;
Eric Laurente8726fe2015-06-26 09:39:24 -0700623 bool mAudioPortCallbacksEnabled;
Eric Laurentb52c1522014-05-20 11:27:36 -0700624 };
625
Eric Laurentfee19762018-01-29 18:44:13 -0800626 // --- AudioRecordClient ---
627 // Information about each registered AudioRecord client
628 // (between calls to getInputForAttr() and releaseInput())
629 class AudioRecordClient : public RefBase {
630 public:
631 AudioRecordClient(const audio_attributes_t attributes,
632 const audio_io_handle_t input, uid_t uid, pid_t pid,
633 const String16& opPackageName, const audio_session_t session) :
634 attributes(attributes),
635 input(input), uid(uid), pid(pid),
636 opPackageName(opPackageName), session(session),
637 active(false), isConcurrent(false), isVirtualDevice(false) {}
638 virtual ~AudioRecordClient() {}
639
640 const audio_attributes_t attributes; // source, flags ...
641 const audio_io_handle_t input; // audio HAL input IO handle
642 const uid_t uid; // client UID
643 const pid_t pid; // client PID
644 const String16 opPackageName; // client package name
645 const audio_session_t session; // audio session ID
646 bool active; // Capture is active or inactive
647 bool isConcurrent; // is allowed to concurrent capture
Eric Laurent99fcae42018-05-17 16:59:18 -0700648 bool isVirtualDevice; // uses virtual device: updated by APM::getInputForAttr()
649 audio_port_handle_t deviceId; // selected input device port ID
Eric Laurentfee19762018-01-29 18:44:13 -0800650 };
651
Eric Laurent10b71232018-04-13 18:14:44 -0700652 // A class automatically clearing and restoring binder caller identity inside
653 // a code block (scoped variable)
654 // Declare one systematically before calling AudioPolicyManager methods so that they are
655 // executed with the same level of privilege as audioserver process.
656 class AutoCallerClear {
657 public:
658 AutoCallerClear() :
659 mToken(IPCThreadState::self()->clearCallingIdentity()) {}
660 ~AutoCallerClear() {
661 IPCThreadState::self()->restoreCallingIdentity(mToken);
662 }
663
664 private:
665 const int64_t mToken;
666 };
667
Mathias Agopian65ab4712010-07-14 17:59:35 -0700668 // Internal dump utilities.
669 status_t dumpPermissionDenial(int fd);
670
671
Eric Laurenteda6c362011-02-02 09:33:30 -0800672 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
673 // device connection state or routing
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700674 mutable Mutex mEffectsLock; // serialize access to Effect state within APM.
675 // Note: lock acquisition order is always mLock > mEffectsLock:
676 // mLock protects AudioPolicyManager methods that can call into audio flinger
677 // and possibly back in to audio policy service and acquire mEffectsLock.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700678 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread
679 sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -0800680 sp<AudioCommandThread> mOutputCommandThread; // process stop and release output
Dima Zavinfce7a472011-04-19 22:30:36 -0700681 struct audio_policy_device *mpAudioPolicyDev;
682 struct audio_policy *mpAudioPolicy;
Eric Laurentf269b8e2014-06-09 20:01:29 -0700683 AudioPolicyInterface *mAudioPolicyManager;
Eric Laurentdce54a12014-03-10 12:19:46 -0700684 AudioPolicyClient *mAudioPolicyClient;
685
Eric Laurentb52c1522014-05-20 11:27:36 -0700686 DefaultKeyedVector< uid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800687 Mutex mNotificationClientsLock; // protects mNotificationClients
bryant_liuba2b4392014-06-11 16:49:30 +0800688 // Manage all effects configured in audio_effects.conf
689 sp<AudioPolicyEffects> mAudioPolicyEffects;
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700690 audio_mode_t mPhoneState;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800691
692 sp<UidPolicy> mUidPolicy;
Eric Laurentfee19762018-01-29 18:44:13 -0800693 DefaultKeyedVector< audio_port_handle_t, sp<AudioRecordClient> > mAudioRecordClients;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694};
695
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800696} // namespace android
Mathias Agopian65ab4712010-07-14 17:59:35 -0700697
698#endif // ANDROID_AUDIOPOLICYSERVICE_H