blob: fbd8f0eec93e4a8476ebeec4a409b7ce0122a010 [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>
Dima Zavin64760242011-05-11 14:15:23 -070027#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070028#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070029#include <hardware/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>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080034#ifdef USE_LEGACY_AUDIO_POLICY
Eric Laurentdce54a12014-03-10 12:19:46 -070035#include <hardware_legacy/AudioPolicyInterface.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080036#endif
bryant_liuba2b4392014-06-11 16:49:30 +080037#include "AudioPolicyEffects.h"
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080038#include "managerdefault/AudioPolicyManager.h"
Dima Zavinfce7a472011-04-19 22:30:36 -070039
bryant_liuba2b4392014-06-11 16:49:30 +080040
Mathias Agopian65ab4712010-07-14 17:59:35 -070041namespace android {
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);
Glenn Kastenf78aee72012-01-04 11:00:47 -080069 virtual status_t setPhoneState(audio_mode_t state);
Dima Zavinfce7a472011-04-19 22:30:36 -070070 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
71 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
72 virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -070073 uint32_t samplingRate = 0,
Glenn Kasten58f30212012-01-12 12:27:51 -080074 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten254af182012-07-03 14:59:05 -070075 audio_channel_mask_t channelMask = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -070076 audio_output_flags_t flags =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +000077 AUDIO_OUTPUT_FLAG_NONE,
78 const audio_offload_info_t *offloadInfo = NULL);
Eric Laurente83b55d2014-11-14 10:06:21 -080079 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
80 audio_io_handle_t *output,
81 audio_session_t session,
82 audio_stream_type_t *stream,
83 uint32_t samplingRate = 0,
84 audio_format_t format = AUDIO_FORMAT_DEFAULT,
85 audio_channel_mask_t channelMask = 0,
86 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
Paul McLeanaa981192015-03-21 09:55:15 -070087 int selectedDeviceId = AUDIO_PORT_HANDLE_NONE,
Eric Laurente83b55d2014-11-14 10:06:21 -080088 const audio_offload_info_t *offloadInfo = NULL);
Eric Laurentde070132010-07-13 04:45:46 -070089 virtual status_t startOutput(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);
Eric Laurentde070132010-07-13 04:45:46 -070092 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070093 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -080094 audio_session_t session);
95 virtual void releaseOutput(audio_io_handle_t output,
96 audio_stream_type_t stream,
97 audio_session_t session);
Eric Laurentcaf7f482014-11-25 17:50:47 -080098 virtual status_t getInputForAttr(const audio_attributes_t *attr,
99 audio_io_handle_t *input,
100 audio_session_t session,
101 uint32_t samplingRate,
102 audio_format_t format,
103 audio_channel_mask_t channelMask,
104 audio_input_flags_t flags);
Eric Laurent4dc68062014-07-28 17:26:49 -0700105 virtual status_t startInput(audio_io_handle_t input,
106 audio_session_t session);
107 virtual status_t stopInput(audio_io_handle_t input,
108 audio_session_t session);
109 virtual void releaseInput(audio_io_handle_t input,
110 audio_session_t session);
Dima Zavinfce7a472011-04-19 22:30:36 -0700111 virtual status_t initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700112 int indexMin,
113 int indexMax);
Eric Laurent83844cc2011-11-18 16:43:31 -0800114 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
115 int index,
116 audio_devices_t device);
117 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
118 int *index,
119 audio_devices_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700120
Dima Zavinfce7a472011-04-19 22:30:36 -0700121 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Eric Laurent63742522012-03-08 13:42:42 -0800122 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurentde070132010-07-13 04:45:46 -0700123
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700124 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
125 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700126 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700127 uint32_t strategy,
128 int session,
129 int id);
130 virtual status_t unregisterEffect(int id);
Eric Laurentdb7c0792011-08-10 10:37:50 -0700131 virtual status_t setEffectEnabled(int id, bool enabled);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800132 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800133 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700134 virtual bool isSourceActive(audio_source_t source) const;
Eric Laurentde070132010-07-13 04:45:46 -0700135
Eric Laurent57dae992011-07-24 13:36:09 -0700136 virtual status_t queryDefaultPreProcessing(int audioSession,
137 effect_descriptor_t *descriptors,
138 uint32_t *count);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700139 virtual status_t onTransact(
140 uint32_t code,
141 const Parcel& data,
142 Parcel* reply,
143 uint32_t flags);
144
145 // IBinder::DeathRecipient
146 virtual void binderDied(const wp<IBinder>& who);
147
Eric Laurentf5ada6e2014-10-09 17:49:00 -0700148 // RefBase
149 virtual void onFirstRef();
150
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700152 // Helpers for the struct audio_policy_service_ops implementation.
153 // This is used by the audio policy manager for certain operations that
154 // are implemented by the policy service.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700156 virtual void setParameters(audio_io_handle_t ioHandle,
157 const char *keyValuePairs,
158 int delayMs);
159
160 virtual status_t setStreamVolume(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700161 float volume,
162 audio_io_handle_t output,
163 int delayMs = 0);
Dima Zavinfce7a472011-04-19 22:30:36 -0700164 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165 virtual status_t stopTone();
166 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000167 virtual bool isOffloadSupported(const audio_offload_info_t &config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168
Eric Laurent203b1a12014-04-01 10:34:16 -0700169 virtual status_t listAudioPorts(audio_port_role_t role,
170 audio_port_type_t type,
171 unsigned int *num_ports,
172 struct audio_port *ports,
173 unsigned int *generation);
174 virtual status_t getAudioPort(struct audio_port *port);
175 virtual status_t createAudioPatch(const struct audio_patch *patch,
176 audio_patch_handle_t *handle);
177 virtual status_t releaseAudioPatch(audio_patch_handle_t handle);
178 virtual status_t listAudioPatches(unsigned int *num_patches,
179 struct audio_patch *patches,
180 unsigned int *generation);
181 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
182
Eric Laurentb52c1522014-05-20 11:27:36 -0700183 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client);
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
Eric Laurentbaac1832014-12-01 17:52:59 -0800193 virtual status_t registerPolicyMixes(Vector<AudioMix> mixes, bool registration);
194
Eric Laurentbfb1b832013-01-07 09:53:42 -0800195 status_t doStopOutput(audio_io_handle_t output,
196 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800197 audio_session_t session);
198 void doReleaseOutput(audio_io_handle_t output,
199 audio_stream_type_t stream,
200 audio_session_t session);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800201
Eric Laurent951f4552014-05-20 10:48:17 -0700202 status_t clientCreateAudioPatch(const struct audio_patch *patch,
203 audio_patch_handle_t *handle,
204 int delayMs);
205 status_t clientReleaseAudioPatch(audio_patch_handle_t handle,
206 int delayMs);
Eric Laurente1715a42014-05-20 11:30:42 -0700207 virtual status_t clientSetAudioPortConfig(const struct audio_port_config *config,
208 int delayMs);
209
Eric Laurentb52c1522014-05-20 11:27:36 -0700210 void removeNotificationClient(uid_t uid);
211 void onAudioPortListUpdate();
212 void doOnAudioPortListUpdate();
213 void onAudioPatchListUpdate();
214 void doOnAudioPatchListUpdate();
Eric Laurent951f4552014-05-20 10:48:17 -0700215
Jean-Michel Trivide801052015-04-14 19:10:14 -0700216 void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state);
217 void doOnDynamicPolicyMixStateUpdate(String8 regId, int32_t state);
218
Mathias Agopian65ab4712010-07-14 17:59:35 -0700219private:
Mathias Agopiane762be92013-05-09 16:26:45 -0700220 AudioPolicyService() ANDROID_API;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700221 virtual ~AudioPolicyService();
222
223 status_t dumpInternals(int fd);
224
225 // Thread used for tone playback and to send audio config commands to audio flinger
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700226 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because
227 // startTone() and stopTone() are normally called with mLock locked and requesting a tone start
228 // or stop will cause calls to AudioPolicyService and an attempt to lock mLock.
229 // For audio config commands, it is necessary because audio flinger requires that the calling
230 // process (user) has permission to modify audio settings.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231 class AudioCommandThread : public Thread {
232 class AudioCommand;
233 public:
234
235 // commands for tone AudioCommand
236 enum {
237 START_TONE,
238 STOP_TONE,
239 SET_VOLUME,
240 SET_PARAMETERS,
Eric Laurentbfb1b832013-01-07 09:53:42 -0800241 SET_VOICE_VOLUME,
242 STOP_OUTPUT,
Eric Laurent951f4552014-05-20 10:48:17 -0700243 RELEASE_OUTPUT,
244 CREATE_AUDIO_PATCH,
245 RELEASE_AUDIO_PATCH,
Eric Laurentb52c1522014-05-20 11:27:36 -0700246 UPDATE_AUDIOPORT_LIST,
Eric Laurente1715a42014-05-20 11:30:42 -0700247 UPDATE_AUDIOPATCH_LIST,
248 SET_AUDIOPORT_CONFIG,
Jean-Michel Trivide801052015-04-14 19:10:14 -0700249 DYN_POLICY_MIX_STATE_UPDATE
Mathias Agopian65ab4712010-07-14 17:59:35 -0700250 };
251
Eric Laurentbfb1b832013-01-07 09:53:42 -0800252 AudioCommandThread (String8 name, const wp<AudioPolicyService>& service);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700253 virtual ~AudioCommandThread();
254
255 status_t dump(int fd);
256
257 // Thread virtuals
258 virtual void onFirstRef();
259 virtual bool threadLoop();
260
261 void exit();
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800262 void startToneCommand(ToneGenerator::tone_type type,
263 audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700264 void stopToneCommand();
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800265 status_t volumeCommand(audio_stream_type_t stream, float volume,
266 audio_io_handle_t output, int delayMs = 0);
267 status_t parametersCommand(audio_io_handle_t ioHandle,
268 const char *keyValuePairs, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269 status_t voiceVolumeCommand(float volume, int delayMs = 0);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800270 void stopOutputCommand(audio_io_handle_t output,
271 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800272 audio_session_t session);
273 void releaseOutputCommand(audio_io_handle_t output,
274 audio_stream_type_t stream,
275 audio_session_t session);
Eric Laurent0ede8922014-05-09 18:04:42 -0700276 status_t sendCommand(sp<AudioCommand>& command, int delayMs = 0);
277 void insertCommand_l(sp<AudioCommand>& command, int delayMs = 0);
Eric Laurent951f4552014-05-20 10:48:17 -0700278 status_t createAudioPatchCommand(const struct audio_patch *patch,
279 audio_patch_handle_t *handle,
280 int delayMs);
281 status_t releaseAudioPatchCommand(audio_patch_handle_t handle,
282 int delayMs);
Eric Laurentb52c1522014-05-20 11:27:36 -0700283 void updateAudioPortListCommand();
284 void updateAudioPatchListCommand();
Eric Laurente1715a42014-05-20 11:30:42 -0700285 status_t setAudioPortConfigCommand(const struct audio_port_config *config,
286 int delayMs);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700287 void dynamicPolicyMixStateUpdateCommand(String8 regId, int32_t state);
Eric Laurent951f4552014-05-20 10:48:17 -0700288 void insertCommand_l(AudioCommand *command, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289
290 private:
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100291 class AudioCommandData;
292
Mathias Agopian65ab4712010-07-14 17:59:35 -0700293 // descriptor for requested tone playback event
Eric Laurent0ede8922014-05-09 18:04:42 -0700294 class AudioCommand: public RefBase {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295
296 public:
297 AudioCommand()
Eric Laurent0ede8922014-05-09 18:04:42 -0700298 : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299
300 void dump(char* buffer, size_t size);
301
302 int mCommand; // START_TONE, STOP_TONE ...
303 nsecs_t mTime; // time stamp
Eric Laurent0ede8922014-05-09 18:04:42 -0700304 Mutex mLock; // mutex associated to mCond
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305 Condition mCond; // condition for status return
306 status_t mStatus; // command status
307 bool mWaitStatus; // true if caller is waiting for status
Eric Laurent0ede8922014-05-09 18:04:42 -0700308 sp<AudioCommandData> mParam; // command specific parameter data
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309 };
310
Eric Laurent0ede8922014-05-09 18:04:42 -0700311 class AudioCommandData: public RefBase {
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100312 public:
313 virtual ~AudioCommandData() {}
314 protected:
315 AudioCommandData() {}
316 };
317
318 class ToneData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319 public:
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800320 ToneGenerator::tone_type mType; // tone type (START_TONE only)
Glenn Kastenfff6d712012-01-12 16:38:12 -0800321 audio_stream_type_t mStream; // stream type (START_TONE only)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322 };
323
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100324 class VolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325 public:
Glenn Kastenfff6d712012-01-12 16:38:12 -0800326 audio_stream_type_t mStream;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327 float mVolume;
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800328 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700329 };
330
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100331 class ParametersData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332 public:
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800333 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334 String8 mKeyValuePairs;
335 };
336
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100337 class VoiceVolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338 public:
339 float mVolume;
340 };
341
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100342 class StopOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800343 public:
344 audio_io_handle_t mIO;
345 audio_stream_type_t mStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800346 audio_session_t mSession;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800347 };
348
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100349 class ReleaseOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800350 public:
351 audio_io_handle_t mIO;
Eric Laurente83b55d2014-11-14 10:06:21 -0800352 audio_stream_type_t mStream;
353 audio_session_t mSession;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800354 };
355
Eric Laurent951f4552014-05-20 10:48:17 -0700356 class CreateAudioPatchData : public AudioCommandData {
357 public:
358 struct audio_patch mPatch;
359 audio_patch_handle_t mHandle;
360 };
361
362 class ReleaseAudioPatchData : public AudioCommandData {
363 public:
364 audio_patch_handle_t mHandle;
365 };
366
Eric Laurente1715a42014-05-20 11:30:42 -0700367 class SetAudioPortConfigData : public AudioCommandData {
368 public:
369 struct audio_port_config mConfig;
370 };
371
Jean-Michel Trivide801052015-04-14 19:10:14 -0700372 class DynPolicyMixStateUpdateData : public AudioCommandData {
373 public:
374 String8 mRegId;
375 int32_t mState;
376 };
377
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378 Mutex mLock;
379 Condition mWaitWorkCV;
Eric Laurent0ede8922014-05-09 18:04:42 -0700380 Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381 ToneGenerator *mpToneGenerator; // the tone generator
Eric Laurent0ede8922014-05-09 18:04:42 -0700382 sp<AudioCommand> mLastCommand; // last processed command (used by dump)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700383 String8 mName; // string used by wake lock fo delayed commands
Eric Laurentbfb1b832013-01-07 09:53:42 -0800384 wp<AudioPolicyService> mService;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385 };
386
Eric Laurentdce54a12014-03-10 12:19:46 -0700387 class AudioPolicyClient : public AudioPolicyClientInterface
388 {
389 public:
390 AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {}
391 virtual ~AudioPolicyClient() {}
392
393 //
394 // Audio HW module functions
395 //
396
397 // loads a HW module.
398 virtual audio_module_handle_t loadHwModule(const char *name);
399
400 //
401 // Audio output Control functions
402 //
403
404 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
405 // in case the audio policy manager has no specific requirements for the output being opened.
406 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
407 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
Eric Laurentcf2c0212014-07-25 16:20:43 -0700408 virtual status_t openOutput(audio_module_handle_t module,
409 audio_io_handle_t *output,
410 audio_config_t *config,
411 audio_devices_t *devices,
412 const String8& address,
413 uint32_t *latencyMs,
414 audio_output_flags_t flags);
Eric Laurentdce54a12014-03-10 12:19:46 -0700415 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
416 // a special mixer thread in the AudioFlinger.
417 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2);
418 // closes the output stream
419 virtual status_t closeOutput(audio_io_handle_t output);
420 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
421 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
422 virtual status_t suspendOutput(audio_io_handle_t output);
423 // restores a suspended output.
424 virtual status_t restoreOutput(audio_io_handle_t output);
425
426 //
427 // Audio input Control functions
428 //
429
430 // opens an audio input
431 virtual audio_io_handle_t openInput(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700432 audio_io_handle_t *input,
433 audio_config_t *config,
434 audio_devices_t *devices,
435 const String8& address,
436 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -0700437 audio_input_flags_t flags);
Eric Laurentdce54a12014-03-10 12:19:46 -0700438 // closes an audio input
439 virtual status_t closeInput(audio_io_handle_t input);
440 //
441 // misc control functions
442 //
443
444 // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
445 // for each output (destination device) it is attached to.
446 virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0);
447
448 // invalidate a stream type, causing a reroute to an unspecified new output
449 virtual status_t invalidateStream(audio_stream_type_t stream);
450
451 // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
452 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0);
453 // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager.
454 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
455
456 // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing
457 // over a telephony device during a phone call.
458 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
459 virtual status_t stopTone();
460
461 // set down link audio volume.
462 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
463
464 // move effect to the specified output
465 virtual status_t moveEffects(int session,
466 audio_io_handle_t srcOutput,
467 audio_io_handle_t dstOutput);
468
Eric Laurent951f4552014-05-20 10:48:17 -0700469 /* Create a patch between several source and sink ports */
470 virtual status_t createAudioPatch(const struct audio_patch *patch,
471 audio_patch_handle_t *handle,
472 int delayMs);
473
474 /* Release a patch */
475 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
476 int delayMs);
477
Eric Laurente1715a42014-05-20 11:30:42 -0700478 /* Set audio port configuration */
479 virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs);
480
Eric Laurentb52c1522014-05-20 11:27:36 -0700481 virtual void onAudioPortListUpdate();
482 virtual void onAudioPatchListUpdate();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700483 virtual void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state);
Eric Laurentb52c1522014-05-20 11:27:36 -0700484
Eric Laurentde3f8392014-07-27 18:38:22 -0700485 virtual audio_unique_id_t newAudioUniqueId();
486
Eric Laurentdce54a12014-03-10 12:19:46 -0700487 private:
488 AudioPolicyService *mAudioPolicyService;
489 };
490
Eric Laurentb52c1522014-05-20 11:27:36 -0700491 // --- Notification Client ---
492 class NotificationClient : public IBinder::DeathRecipient {
493 public:
494 NotificationClient(const sp<AudioPolicyService>& service,
495 const sp<IAudioPolicyServiceClient>& client,
496 uid_t uid);
497 virtual ~NotificationClient();
498
Jean-Michel Trivide801052015-04-14 19:10:14 -0700499 void onAudioPortListUpdate();
500 void onAudioPatchListUpdate();
501 void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state);
Eric Laurentb52c1522014-05-20 11:27:36 -0700502
503 // IBinder::DeathRecipient
504 virtual void binderDied(const wp<IBinder>& who);
505
506 private:
507 NotificationClient(const NotificationClient&);
508 NotificationClient& operator = (const NotificationClient&);
509
510 const wp<AudioPolicyService> mService;
511 const uid_t mUid;
512 const sp<IAudioPolicyServiceClient> mAudioPolicyServiceClient;
513 };
514
Mathias Agopian65ab4712010-07-14 17:59:35 -0700515 // Internal dump utilities.
516 status_t dumpPermissionDenial(int fd);
517
518
Eric Laurenteda6c362011-02-02 09:33:30 -0800519 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
520 // device connection state or routing
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700521 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread
522 sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -0800523 sp<AudioCommandThread> mOutputCommandThread; // process stop and release output
Dima Zavinfce7a472011-04-19 22:30:36 -0700524 struct audio_policy_device *mpAudioPolicyDev;
525 struct audio_policy *mpAudioPolicy;
Eric Laurentf269b8e2014-06-09 20:01:29 -0700526 AudioPolicyInterface *mAudioPolicyManager;
Eric Laurentdce54a12014-03-10 12:19:46 -0700527 AudioPolicyClient *mAudioPolicyClient;
528
Eric Laurentb52c1522014-05-20 11:27:36 -0700529 DefaultKeyedVector< uid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800530 Mutex mNotificationClientsLock; // protects mNotificationClients
bryant_liuba2b4392014-06-11 16:49:30 +0800531 // Manage all effects configured in audio_effects.conf
532 sp<AudioPolicyEffects> mAudioPolicyEffects;
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700533 audio_mode_t mPhoneState;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700534};
535
536}; // namespace android
537
538#endif // ANDROID_AUDIOPOLICYSERVICE_H