blob: 6b42ba35e3a25fc59135c412a7196b6909b749e4 [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
Mathias Agopian65ab4712010-07-14 17:59:35 -070041// ----------------------------------------------------------------------------
42
Glenn Kastend2dcb082011-02-03 16:55:26 -080043class AudioPolicyService :
44 public BinderService<AudioPolicyService>,
45 public BnAudioPolicyService,
Eric Laurentde070132010-07-13 04:45:46 -070046 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070047{
Glenn Kastend2dcb082011-02-03 16:55:26 -080048 friend class BinderService<AudioPolicyService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
50public:
Glenn Kastend2dcb082011-02-03 16:55:26 -080051 // for BinderService
Mathias Agopiane762be92013-05-09 16:26:45 -070052 static const char *getServiceName() ANDROID_API { return "media.audio_policy"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
54 virtual status_t dump(int fd, const Vector<String16>& args);
55
56 //
57 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
58 //
59
Dima Zavinfce7a472011-04-19 22:30:36 -070060 virtual status_t setDeviceConnectionState(audio_devices_t device,
61 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080062 const char *device_address,
63 const char *device_name);
Dima Zavinfce7a472011-04-19 22:30:36 -070064 virtual audio_policy_dev_state_t getDeviceConnectionState(
65 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -070066 const char *device_address);
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -080067 virtual status_t handleDeviceConfigChange(audio_devices_t device,
68 const char *device_address,
69 const char *device_name);
Glenn Kastenf78aee72012-01-04 11:00:47 -080070 virtual status_t setPhoneState(audio_mode_t state);
Dima Zavinfce7a472011-04-19 22:30:36 -070071 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
72 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
Eric Laurentf4e63452017-11-06 19:31:46 +000073 virtual audio_io_handle_t getOutput(audio_stream_type_t stream);
Eric Laurente83b55d2014-11-14 10:06:21 -080074 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
75 audio_io_handle_t *output,
76 audio_session_t session,
77 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +020078 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -070079 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -080080 const audio_config_t *config,
81 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -070082 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -080083 audio_port_handle_t *portId);
Eric Laurentde070132010-07-13 04:45:46 -070084 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070085 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -080086 audio_session_t session);
Eric Laurentde070132010-07-13 04:45:46 -070087 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070088 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -080089 audio_session_t session);
90 virtual void releaseOutput(audio_io_handle_t output,
91 audio_stream_type_t stream,
92 audio_session_t session);
Eric Laurentcaf7f482014-11-25 17:50:47 -080093 virtual status_t getInputForAttr(const audio_attributes_t *attr,
94 audio_io_handle_t *input,
95 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -070096 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -070097 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -080098 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -080099 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600100 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700101 audio_port_handle_t *selectedDeviceId = NULL,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800102 audio_port_handle_t *portId = NULL);
Eric Laurentfee19762018-01-29 18:44:13 -0800103 virtual status_t startInput(audio_port_handle_t portId,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800104 bool *silenced);
Eric Laurentfee19762018-01-29 18:44:13 -0800105 virtual status_t stopInput(audio_port_handle_t portId);
106 virtual void releaseInput(audio_port_handle_t portId);
Dima Zavinfce7a472011-04-19 22:30:36 -0700107 virtual status_t initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700108 int indexMin,
109 int indexMax);
Eric Laurent83844cc2011-11-18 16:43:31 -0800110 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
111 int index,
112 audio_devices_t device);
113 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
114 int *index,
115 audio_devices_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700116
Dima Zavinfce7a472011-04-19 22:30:36 -0700117 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Eric Laurent63742522012-03-08 13:42:42 -0800118 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurentde070132010-07-13 04:45:46 -0700119
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700120 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
121 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700122 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700123 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800124 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700125 int id);
126 virtual status_t unregisterEffect(int id);
Eric Laurentdb7c0792011-08-10 10:37:50 -0700127 virtual status_t setEffectEnabled(int id, bool enabled);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800128 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800129 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700130 virtual bool isSourceActive(audio_source_t source) const;
Eric Laurentde070132010-07-13 04:45:46 -0700131
Glenn Kastend848eb42016-03-08 13:42:11 -0800132 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700133 effect_descriptor_t *descriptors,
134 uint32_t *count);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700135 virtual status_t onTransact(
136 uint32_t code,
137 const Parcel& data,
138 Parcel* reply,
139 uint32_t flags);
140
141 // IBinder::DeathRecipient
142 virtual void binderDied(const wp<IBinder>& who);
143
Eric Laurentf5ada6e2014-10-09 17:49:00 -0700144 // RefBase
145 virtual void onFirstRef();
146
Mathias Agopian65ab4712010-07-14 17:59:35 -0700147 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700148 // Helpers for the struct audio_policy_service_ops implementation.
149 // This is used by the audio policy manager for certain operations that
150 // are implemented by the policy service.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700152 virtual void setParameters(audio_io_handle_t ioHandle,
153 const char *keyValuePairs,
154 int delayMs);
155
156 virtual status_t setStreamVolume(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700157 float volume,
158 audio_io_handle_t output,
159 int delayMs = 0);
Dima Zavinfce7a472011-04-19 22:30:36 -0700160 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700161 virtual status_t stopTone();
162 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000163 virtual bool isOffloadSupported(const audio_offload_info_t &config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700164
Eric Laurent203b1a12014-04-01 10:34:16 -0700165 virtual status_t listAudioPorts(audio_port_role_t role,
166 audio_port_type_t type,
167 unsigned int *num_ports,
168 struct audio_port *ports,
169 unsigned int *generation);
170 virtual status_t getAudioPort(struct audio_port *port);
171 virtual status_t createAudioPatch(const struct audio_patch *patch,
172 audio_patch_handle_t *handle);
173 virtual status_t releaseAudioPatch(audio_patch_handle_t handle);
174 virtual status_t listAudioPatches(unsigned int *num_patches,
175 struct audio_patch *patches,
176 unsigned int *generation);
177 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
178
Eric Laurentb52c1522014-05-20 11:27:36 -0700179 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client);
180
Eric Laurente8726fe2015-06-26 09:39:24 -0700181 virtual void setAudioPortCallbacksEnabled(bool enabled);
182
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700183 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
184 audio_io_handle_t *ioHandle,
185 audio_devices_t *device);
186
187 virtual status_t releaseSoundTriggerSession(audio_session_t session);
188
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700189 virtual audio_mode_t getPhoneState();
190
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700191 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration);
Eric Laurentbaac1832014-12-01 17:52:59 -0800192
Eric Laurent554a2772015-04-10 11:29:24 -0700193 virtual status_t startAudioSource(const struct audio_port_config *source,
194 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -0700195 audio_patch_handle_t *handle);
196 virtual status_t stopAudioSource(audio_patch_handle_t handle);
Eric Laurent554a2772015-04-10 11:29:24 -0700197
Andy Hung2ddee192015-12-18 17:34:44 -0800198 virtual status_t setMasterMono(bool mono);
199 virtual status_t getMasterMono(bool *mono);
200
Eric Laurentac9cef52017-06-09 15:46:26 -0700201 virtual float getStreamVolumeDB(
202 audio_stream_type_t stream, int index, audio_devices_t device);
203
jiabin81772902018-04-02 17:52:27 -0700204 virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
205 audio_format_t *surroundFormats,
206 bool *surroundFormatsEnabled,
207 bool reported);
208 virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled);
209
Eric Laurentbfb1b832013-01-07 09:53:42 -0800210 status_t doStopOutput(audio_io_handle_t output,
211 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800212 audio_session_t session);
213 void doReleaseOutput(audio_io_handle_t output,
214 audio_stream_type_t stream,
215 audio_session_t session);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800216
Eric Laurent951f4552014-05-20 10:48:17 -0700217 status_t clientCreateAudioPatch(const struct audio_patch *patch,
218 audio_patch_handle_t *handle,
219 int delayMs);
220 status_t clientReleaseAudioPatch(audio_patch_handle_t handle,
221 int delayMs);
Eric Laurente1715a42014-05-20 11:30:42 -0700222 virtual status_t clientSetAudioPortConfig(const struct audio_port_config *config,
223 int delayMs);
224
luochaojiang908c7d72018-06-21 14:58:04 +0800225 void removeNotificationClient(uid_t uid, pid_t pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700226 void onAudioPortListUpdate();
227 void doOnAudioPortListUpdate();
228 void onAudioPatchListUpdate();
229 void doOnAudioPatchListUpdate();
Eric Laurent951f4552014-05-20 10:48:17 -0700230
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700231 void onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state);
232 void doOnDynamicPolicyMixStateUpdate(const String8& regId, int32_t state);
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800233 void onRecordingConfigurationUpdate(int event, const record_client_info_t *clientInfo,
234 const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800235 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle);
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800236 void doOnRecordingConfigurationUpdate(int event, const record_client_info_t *clientInfo,
237 const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800238 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700239
Mathias Agopian65ab4712010-07-14 17:59:35 -0700240private:
Mathias Agopiane762be92013-05-09 16:26:45 -0700241 AudioPolicyService() ANDROID_API;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242 virtual ~AudioPolicyService();
243
244 status_t dumpInternals(int fd);
245
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800246 // Handles binder shell commands
247 virtual status_t shellCommand(int in, int out, int err, Vector<String16>& args);
248
249 // Sets whether the given UID records only silence
250 virtual void setRecordSilenced(uid_t uid, bool silenced);
251
252 // Overrides the UID state as if it is idle
253 status_t handleSetUidState(Vector<String16>& args, int err);
254
255 // Clears the override for the UID state
256 status_t handleResetUidState(Vector<String16>& args, int err);
257
258 // Gets the UID state
259 status_t handleGetUidState(Vector<String16>& args, int out, int err);
260
261 // Prints the shell command help
262 status_t printHelp(int out);
263
Eric Laurent99fcae42018-05-17 16:59:18 -0700264 std::string getDeviceTypeStrForPortId(audio_port_handle_t portId);
265
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800266 // If recording we need to make sure the UID is allowed to do that. If the UID is idle
267 // then it cannot record and gets buffers with zeros - silence. As soon as the UID
268 // transitions to an active state we will start reporting buffers with data. This approach
269 // transparently handles recording while the UID transitions between idle/active state
270 // avoiding to get stuck in a state receiving non-empty buffers while idle or in a state
271 // receiving empty buffers while active.
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700272 class UidPolicy : public BnUidObserver, public virtual IBinder::DeathRecipient {
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800273 public:
274 explicit UidPolicy(wp<AudioPolicyService> service)
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700275 : mService(service), mObserverRegistered(false) {}
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800276
277 void registerSelf();
278 void unregisterSelf();
279
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700280 // IBinder::DeathRecipient implementation
281 void binderDied(const wp<IBinder> &who) override;
282
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800283 bool isUidActive(uid_t uid);
284
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700285 // BnUidObserver implementation
286 void onUidActive(uid_t uid) override;
287 void onUidGone(uid_t uid, bool disabled) override;
288 void onUidIdle(uid_t uid, bool disabled) override;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800289
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700290 void addOverrideUid(uid_t uid, bool active) { updateOverrideUid(uid, active, true); }
291 void removeOverrideUid(uid_t uid) { updateOverrideUid(uid, false, false); }
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800292
293 private:
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700294 void notifyService(uid_t uid, bool active);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800295 void updateOverrideUid(uid_t uid, bool active, bool insert);
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700296 void updateUidCache(uid_t uid, bool active, bool insert);
297 void updateUidLocked(std::unordered_map<uid_t, bool> *uids,
298 uid_t uid, bool active, bool insert, bool *wasThere, bool *wasActive);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800299
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800300 wp<AudioPolicyService> mService;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700301 Mutex mLock;
302 bool mObserverRegistered;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800303 std::unordered_map<uid_t, bool> mOverrideUids;
Mikhail Naganoveae73eb2018-04-03 16:57:36 -0700304 std::unordered_map<uid_t, bool> mCachedUids;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800305 };
306
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307 // Thread used for tone playback and to send audio config commands to audio flinger
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700308 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because
309 // startTone() and stopTone() are normally called with mLock locked and requesting a tone start
310 // or stop will cause calls to AudioPolicyService and an attempt to lock mLock.
311 // For audio config commands, it is necessary because audio flinger requires that the calling
312 // process (user) has permission to modify audio settings.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313 class AudioCommandThread : public Thread {
314 class AudioCommand;
315 public:
316
317 // commands for tone AudioCommand
318 enum {
319 START_TONE,
320 STOP_TONE,
321 SET_VOLUME,
322 SET_PARAMETERS,
Eric Laurentbfb1b832013-01-07 09:53:42 -0800323 SET_VOICE_VOLUME,
324 STOP_OUTPUT,
Eric Laurent951f4552014-05-20 10:48:17 -0700325 RELEASE_OUTPUT,
326 CREATE_AUDIO_PATCH,
327 RELEASE_AUDIO_PATCH,
Eric Laurentb52c1522014-05-20 11:27:36 -0700328 UPDATE_AUDIOPORT_LIST,
Eric Laurente1715a42014-05-20 11:30:42 -0700329 UPDATE_AUDIOPATCH_LIST,
330 SET_AUDIOPORT_CONFIG,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800331 DYN_POLICY_MIX_STATE_UPDATE,
332 RECORDING_CONFIGURATION_UPDATE
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333 };
334
Eric Laurentbfb1b832013-01-07 09:53:42 -0800335 AudioCommandThread (String8 name, const wp<AudioPolicyService>& service);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 virtual ~AudioCommandThread();
337
338 status_t dump(int fd);
339
340 // Thread virtuals
341 virtual void onFirstRef();
342 virtual bool threadLoop();
343
344 void exit();
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800345 void startToneCommand(ToneGenerator::tone_type type,
346 audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 void stopToneCommand();
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800348 status_t volumeCommand(audio_stream_type_t stream, float volume,
349 audio_io_handle_t output, int delayMs = 0);
350 status_t parametersCommand(audio_io_handle_t ioHandle,
351 const char *keyValuePairs, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352 status_t voiceVolumeCommand(float volume, int delayMs = 0);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800353 void stopOutputCommand(audio_io_handle_t output,
354 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800355 audio_session_t session);
356 void releaseOutputCommand(audio_io_handle_t output,
357 audio_stream_type_t stream,
358 audio_session_t session);
Eric Laurent0ede8922014-05-09 18:04:42 -0700359 status_t sendCommand(sp<AudioCommand>& command, int delayMs = 0);
360 void insertCommand_l(sp<AudioCommand>& command, int delayMs = 0);
Eric Laurent951f4552014-05-20 10:48:17 -0700361 status_t createAudioPatchCommand(const struct audio_patch *patch,
362 audio_patch_handle_t *handle,
363 int delayMs);
364 status_t releaseAudioPatchCommand(audio_patch_handle_t handle,
365 int delayMs);
Eric Laurentb52c1522014-05-20 11:27:36 -0700366 void updateAudioPortListCommand();
367 void updateAudioPatchListCommand();
Eric Laurente1715a42014-05-20 11:30:42 -0700368 status_t setAudioPortConfigCommand(const struct audio_port_config *config,
369 int delayMs);
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700370 void dynamicPolicyMixStateUpdateCommand(const String8& regId, int32_t state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800371 void recordingConfigurationUpdateCommand(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800372 int event,
373 const record_client_info_t *clientInfo,
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800374 const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800375 const audio_config_base_t *deviceConfig,
376 audio_patch_handle_t patchHandle);
Eric Laurent951f4552014-05-20 10:48:17 -0700377 void insertCommand_l(AudioCommand *command, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378 private:
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100379 class AudioCommandData;
380
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381 // descriptor for requested tone playback event
Eric Laurent0ede8922014-05-09 18:04:42 -0700382 class AudioCommand: public RefBase {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383
384 public:
385 AudioCommand()
Eric Laurent0ede8922014-05-09 18:04:42 -0700386 : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700387
388 void dump(char* buffer, size_t size);
389
390 int mCommand; // START_TONE, STOP_TONE ...
391 nsecs_t mTime; // time stamp
Eric Laurent0ede8922014-05-09 18:04:42 -0700392 Mutex mLock; // mutex associated to mCond
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393 Condition mCond; // condition for status return
394 status_t mStatus; // command status
395 bool mWaitStatus; // true if caller is waiting for status
Eric Laurent0ede8922014-05-09 18:04:42 -0700396 sp<AudioCommandData> mParam; // command specific parameter data
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397 };
398
Eric Laurent0ede8922014-05-09 18:04:42 -0700399 class AudioCommandData: public RefBase {
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100400 public:
401 virtual ~AudioCommandData() {}
402 protected:
403 AudioCommandData() {}
404 };
405
406 class ToneData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407 public:
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800408 ToneGenerator::tone_type mType; // tone type (START_TONE only)
Glenn Kastenfff6d712012-01-12 16:38:12 -0800409 audio_stream_type_t mStream; // stream type (START_TONE only)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 };
411
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100412 class VolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413 public:
Glenn Kastenfff6d712012-01-12 16:38:12 -0800414 audio_stream_type_t mStream;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700415 float mVolume;
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800416 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700417 };
418
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100419 class ParametersData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700420 public:
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800421 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700422 String8 mKeyValuePairs;
423 };
424
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100425 class VoiceVolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700426 public:
427 float mVolume;
428 };
429
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100430 class StopOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800431 public:
432 audio_io_handle_t mIO;
433 audio_stream_type_t mStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800434 audio_session_t mSession;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800435 };
436
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100437 class ReleaseOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800438 public:
439 audio_io_handle_t mIO;
Eric Laurente83b55d2014-11-14 10:06:21 -0800440 audio_stream_type_t mStream;
441 audio_session_t mSession;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800442 };
443
Eric Laurent951f4552014-05-20 10:48:17 -0700444 class CreateAudioPatchData : public AudioCommandData {
445 public:
446 struct audio_patch mPatch;
447 audio_patch_handle_t mHandle;
448 };
449
450 class ReleaseAudioPatchData : public AudioCommandData {
451 public:
452 audio_patch_handle_t mHandle;
453 };
454
Eric Laurente1715a42014-05-20 11:30:42 -0700455 class SetAudioPortConfigData : public AudioCommandData {
456 public:
457 struct audio_port_config mConfig;
458 };
459
Jean-Michel Trivide801052015-04-14 19:10:14 -0700460 class DynPolicyMixStateUpdateData : public AudioCommandData {
461 public:
462 String8 mRegId;
463 int32_t mState;
464 };
465
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800466 class RecordingConfigurationUpdateData : public AudioCommandData {
467 public:
468 int mEvent;
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800469 record_client_info_t mClientInfo;
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800470 struct audio_config_base mClientConfig;
471 struct audio_config_base mDeviceConfig;
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800472 audio_patch_handle_t mPatchHandle;
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800473 };
474
Mathias Agopian65ab4712010-07-14 17:59:35 -0700475 Mutex mLock;
476 Condition mWaitWorkCV;
Eric Laurent0ede8922014-05-09 18:04:42 -0700477 Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands
Mathias Agopian65ab4712010-07-14 17:59:35 -0700478 ToneGenerator *mpToneGenerator; // the tone generator
Eric Laurent0ede8922014-05-09 18:04:42 -0700479 sp<AudioCommand> mLastCommand; // last processed command (used by dump)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700480 String8 mName; // string used by wake lock fo delayed commands
Eric Laurentbfb1b832013-01-07 09:53:42 -0800481 wp<AudioPolicyService> mService;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700482 };
483
Eric Laurentdce54a12014-03-10 12:19:46 -0700484 class AudioPolicyClient : public AudioPolicyClientInterface
485 {
486 public:
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700487 explicit AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {}
Eric Laurentdce54a12014-03-10 12:19:46 -0700488 virtual ~AudioPolicyClient() {}
489
490 //
491 // Audio HW module functions
492 //
493
494 // loads a HW module.
495 virtual audio_module_handle_t loadHwModule(const char *name);
496
497 //
498 // Audio output Control functions
499 //
500
501 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
502 // in case the audio policy manager has no specific requirements for the output being opened.
503 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
504 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
Eric Laurentcf2c0212014-07-25 16:20:43 -0700505 virtual status_t openOutput(audio_module_handle_t module,
506 audio_io_handle_t *output,
507 audio_config_t *config,
508 audio_devices_t *devices,
509 const String8& address,
510 uint32_t *latencyMs,
511 audio_output_flags_t flags);
Eric Laurentdce54a12014-03-10 12:19:46 -0700512 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
513 // a special mixer thread in the AudioFlinger.
514 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2);
515 // closes the output stream
516 virtual status_t closeOutput(audio_io_handle_t output);
517 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
518 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
519 virtual status_t suspendOutput(audio_io_handle_t output);
520 // restores a suspended output.
521 virtual status_t restoreOutput(audio_io_handle_t output);
522
523 //
524 // Audio input Control functions
525 //
526
527 // opens an audio input
528 virtual audio_io_handle_t openInput(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700529 audio_io_handle_t *input,
530 audio_config_t *config,
531 audio_devices_t *devices,
532 const String8& address,
533 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -0700534 audio_input_flags_t flags);
Eric Laurentdce54a12014-03-10 12:19:46 -0700535 // closes an audio input
536 virtual status_t closeInput(audio_io_handle_t input);
537 //
538 // misc control functions
539 //
540
541 // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
542 // for each output (destination device) it is attached to.
543 virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0);
544
545 // invalidate a stream type, causing a reroute to an unspecified new output
546 virtual status_t invalidateStream(audio_stream_type_t stream);
547
548 // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
549 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0);
550 // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager.
551 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
552
553 // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing
554 // over a telephony device during a phone call.
555 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
556 virtual status_t stopTone();
557
558 // set down link audio volume.
559 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
560
561 // move effect to the specified output
Glenn Kastend848eb42016-03-08 13:42:11 -0800562 virtual status_t moveEffects(audio_session_t session,
Eric Laurentdce54a12014-03-10 12:19:46 -0700563 audio_io_handle_t srcOutput,
564 audio_io_handle_t dstOutput);
565
Eric Laurent951f4552014-05-20 10:48:17 -0700566 /* Create a patch between several source and sink ports */
567 virtual status_t createAudioPatch(const struct audio_patch *patch,
568 audio_patch_handle_t *handle,
569 int delayMs);
570
571 /* Release a patch */
572 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
573 int delayMs);
574
Eric Laurente1715a42014-05-20 11:30:42 -0700575 /* Set audio port configuration */
576 virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs);
577
Eric Laurentb52c1522014-05-20 11:27:36 -0700578 virtual void onAudioPortListUpdate();
579 virtual void onAudioPatchListUpdate();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700580 virtual void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800581 virtual void onRecordingConfigurationUpdate(int event,
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800582 const record_client_info_t *clientInfo,
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800583 const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800584 const audio_config_base_t *deviceConfig, audio_patch_handle_t patchHandle);
Eric Laurentb52c1522014-05-20 11:27:36 -0700585
Glenn Kasteneeecb982016-02-26 10:44:04 -0800586 virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use);
Eric Laurentde3f8392014-07-27 18:38:22 -0700587
Eric Laurentdce54a12014-03-10 12:19:46 -0700588 private:
589 AudioPolicyService *mAudioPolicyService;
590 };
591
Eric Laurentb52c1522014-05-20 11:27:36 -0700592 // --- Notification Client ---
593 class NotificationClient : public IBinder::DeathRecipient {
594 public:
595 NotificationClient(const sp<AudioPolicyService>& service,
596 const sp<IAudioPolicyServiceClient>& client,
luochaojiang908c7d72018-06-21 14:58:04 +0800597 uid_t uid, pid_t pid);
Eric Laurentb52c1522014-05-20 11:27:36 -0700598 virtual ~NotificationClient();
599
Jean-Michel Trivide801052015-04-14 19:10:14 -0700600 void onAudioPortListUpdate();
601 void onAudioPatchListUpdate();
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700602 void onDynamicPolicyMixStateUpdate(const String8& regId, int32_t state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800603 void onRecordingConfigurationUpdate(
Jean-Michel Triviac4e4292016-12-22 11:39:31 -0800604 int event, const record_client_info_t *clientInfo,
Jean-Michel Trivi7281aa92016-02-17 15:33:40 -0800605 const audio_config_base_t *clientConfig,
Jean-Michel Trivi8c7cf3b2016-02-25 17:08:24 -0800606 const audio_config_base_t *deviceConfig,
607 audio_patch_handle_t patchHandle);
Eric Laurente8726fe2015-06-26 09:39:24 -0700608 void setAudioPortCallbacksEnabled(bool enabled);
Eric Laurentb52c1522014-05-20 11:27:36 -0700609
luochaojiang908c7d72018-06-21 14:58:04 +0800610 uid_t uid() {
611 return mUid;
612 }
613
Eric Laurentb52c1522014-05-20 11:27:36 -0700614 // IBinder::DeathRecipient
615 virtual void binderDied(const wp<IBinder>& who);
616
617 private:
618 NotificationClient(const NotificationClient&);
619 NotificationClient& operator = (const NotificationClient&);
620
621 const wp<AudioPolicyService> mService;
622 const uid_t mUid;
luochaojiang908c7d72018-06-21 14:58:04 +0800623 const pid_t mPid;
Eric Laurentb52c1522014-05-20 11:27:36 -0700624 const sp<IAudioPolicyServiceClient> mAudioPolicyServiceClient;
Eric Laurente8726fe2015-06-26 09:39:24 -0700625 bool mAudioPortCallbacksEnabled;
Eric Laurentb52c1522014-05-20 11:27:36 -0700626 };
627
Eric Laurentfee19762018-01-29 18:44:13 -0800628 // --- AudioRecordClient ---
629 // Information about each registered AudioRecord client
630 // (between calls to getInputForAttr() and releaseInput())
631 class AudioRecordClient : public RefBase {
632 public:
633 AudioRecordClient(const audio_attributes_t attributes,
634 const audio_io_handle_t input, uid_t uid, pid_t pid,
635 const String16& opPackageName, const audio_session_t session) :
636 attributes(attributes),
637 input(input), uid(uid), pid(pid),
638 opPackageName(opPackageName), session(session),
639 active(false), isConcurrent(false), isVirtualDevice(false) {}
640 virtual ~AudioRecordClient() {}
641
642 const audio_attributes_t attributes; // source, flags ...
643 const audio_io_handle_t input; // audio HAL input IO handle
644 const uid_t uid; // client UID
645 const pid_t pid; // client PID
646 const String16 opPackageName; // client package name
647 const audio_session_t session; // audio session ID
648 bool active; // Capture is active or inactive
649 bool isConcurrent; // is allowed to concurrent capture
Eric Laurent99fcae42018-05-17 16:59:18 -0700650 bool isVirtualDevice; // uses virtual device: updated by APM::getInputForAttr()
651 audio_port_handle_t deviceId; // selected input device port ID
Eric Laurentfee19762018-01-29 18:44:13 -0800652 };
653
Eric Laurent10b71232018-04-13 18:14:44 -0700654 // A class automatically clearing and restoring binder caller identity inside
655 // a code block (scoped variable)
656 // Declare one systematically before calling AudioPolicyManager methods so that they are
657 // executed with the same level of privilege as audioserver process.
658 class AutoCallerClear {
659 public:
660 AutoCallerClear() :
661 mToken(IPCThreadState::self()->clearCallingIdentity()) {}
662 ~AutoCallerClear() {
663 IPCThreadState::self()->restoreCallingIdentity(mToken);
664 }
665
666 private:
667 const int64_t mToken;
668 };
669
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 // Internal dump utilities.
671 status_t dumpPermissionDenial(int fd);
672
673
Eric Laurenteda6c362011-02-02 09:33:30 -0800674 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
675 // device connection state or routing
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700676 mutable Mutex mEffectsLock; // serialize access to Effect state within APM.
677 // Note: lock acquisition order is always mLock > mEffectsLock:
678 // mLock protects AudioPolicyManager methods that can call into audio flinger
679 // and possibly back in to audio policy service and acquire mEffectsLock.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700680 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread
681 sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -0800682 sp<AudioCommandThread> mOutputCommandThread; // process stop and release output
Dima Zavinfce7a472011-04-19 22:30:36 -0700683 struct audio_policy_device *mpAudioPolicyDev;
684 struct audio_policy *mpAudioPolicy;
Eric Laurentf269b8e2014-06-09 20:01:29 -0700685 AudioPolicyInterface *mAudioPolicyManager;
Eric Laurentdce54a12014-03-10 12:19:46 -0700686 AudioPolicyClient *mAudioPolicyClient;
687
luochaojiang908c7d72018-06-21 14:58:04 +0800688 DefaultKeyedVector< int64_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800689 Mutex mNotificationClientsLock; // protects mNotificationClients
bryant_liuba2b4392014-06-11 16:49:30 +0800690 // Manage all effects configured in audio_effects.conf
691 sp<AudioPolicyEffects> mAudioPolicyEffects;
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700692 audio_mode_t mPhoneState;
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800693
694 sp<UidPolicy> mUidPolicy;
Eric Laurentfee19762018-01-29 18:44:13 -0800695 DefaultKeyedVector< audio_port_handle_t, sp<AudioRecordClient> > mAudioRecordClients;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700696};
697
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800698} // namespace android
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699
700#endif // ANDROID_AUDIOPOLICYSERVICE_H