blob: 69673cd7565f3ed6ceb826ec381f46f1181c5bcf [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 Laurentdce54a12014-03-10 12:19:46 -070033#include <hardware_legacy/AudioPolicyInterface.h>
34#include "AudioPolicyManager.h"
Dima Zavinfce7a472011-04-19 22:30:36 -070035
Mathias Agopian65ab4712010-07-14 17:59:35 -070036namespace android {
37
Mathias Agopian65ab4712010-07-14 17:59:35 -070038// ----------------------------------------------------------------------------
39
Glenn Kastend2dcb082011-02-03 16:55:26 -080040class AudioPolicyService :
41 public BinderService<AudioPolicyService>,
42 public BnAudioPolicyService,
Eric Laurentde070132010-07-13 04:45:46 -070043 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070044{
Glenn Kastend2dcb082011-02-03 16:55:26 -080045 friend class BinderService<AudioPolicyService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070046
47public:
Glenn Kastend2dcb082011-02-03 16:55:26 -080048 // for BinderService
Mathias Agopiane762be92013-05-09 16:26:45 -070049 static const char *getServiceName() ANDROID_API { return "media.audio_policy"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
51 virtual status_t dump(int fd, const Vector<String16>& args);
52
53 //
54 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
55 //
56
Dima Zavinfce7a472011-04-19 22:30:36 -070057 virtual status_t setDeviceConnectionState(audio_devices_t device,
58 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -070059 const char *device_address);
Dima Zavinfce7a472011-04-19 22:30:36 -070060 virtual audio_policy_dev_state_t getDeviceConnectionState(
61 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -070062 const char *device_address);
Glenn Kastenf78aee72012-01-04 11:00:47 -080063 virtual status_t setPhoneState(audio_mode_t state);
Dima Zavinfce7a472011-04-19 22:30:36 -070064 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
65 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
66 virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -070067 uint32_t samplingRate = 0,
Glenn Kasten58f30212012-01-12 12:27:51 -080068 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten254af182012-07-03 14:59:05 -070069 audio_channel_mask_t channelMask = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -070070 audio_output_flags_t flags =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +000071 AUDIO_OUTPUT_FLAG_NONE,
72 const audio_offload_info_t *offloadInfo = NULL);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -070073 virtual audio_io_handle_t getOutputForAttr(const audio_attributes_t *attr,
74 uint32_t samplingRate = 0,
75 audio_format_t format = AUDIO_FORMAT_DEFAULT,
76 audio_channel_mask_t channelMask = 0,
77 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
78 const audio_offload_info_t *offloadInfo = NULL);
Eric Laurentde070132010-07-13 04:45:46 -070079 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070080 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -070081 int session = 0);
82 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070083 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -070084 int session = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -070085 virtual void releaseOutput(audio_io_handle_t output);
Glenn Kasteneba51fb2012-01-23 13:58:49 -080086 virtual audio_io_handle_t getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -070087 uint32_t samplingRate = 0,
Glenn Kasten58f30212012-01-12 12:27:51 -080088 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten254af182012-07-03 14:59:05 -070089 audio_channel_mask_t channelMask = 0,
Eric Laurent7c7f10b2011-06-17 21:29:58 -070090 int audioSession = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -070091 virtual status_t startInput(audio_io_handle_t input);
92 virtual status_t stopInput(audio_io_handle_t input);
93 virtual void releaseInput(audio_io_handle_t input);
Dima Zavinfce7a472011-04-19 22:30:36 -070094 virtual status_t initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -070095 int indexMin,
96 int indexMax);
Eric Laurent83844cc2011-11-18 16:43:31 -080097 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
98 int index,
99 audio_devices_t device);
100 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
101 int *index,
102 audio_devices_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103
Dima Zavinfce7a472011-04-19 22:30:36 -0700104 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Eric Laurent63742522012-03-08 13:42:42 -0800105 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurentde070132010-07-13 04:45:46 -0700106
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700107 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
108 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700109 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700110 uint32_t strategy,
111 int session,
112 int id);
113 virtual status_t unregisterEffect(int id);
Eric Laurentdb7c0792011-08-10 10:37:50 -0700114 virtual status_t setEffectEnabled(int id, bool enabled);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800115 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800116 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700117 virtual bool isSourceActive(audio_source_t source) const;
Eric Laurentde070132010-07-13 04:45:46 -0700118
Eric Laurent57dae992011-07-24 13:36:09 -0700119 virtual status_t queryDefaultPreProcessing(int audioSession,
120 effect_descriptor_t *descriptors,
121 uint32_t *count);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700122 virtual status_t onTransact(
123 uint32_t code,
124 const Parcel& data,
125 Parcel* reply,
126 uint32_t flags);
127
128 // IBinder::DeathRecipient
129 virtual void binderDied(const wp<IBinder>& who);
130
131 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700132 // Helpers for the struct audio_policy_service_ops implementation.
133 // This is used by the audio policy manager for certain operations that
134 // are implemented by the policy service.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700135 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700136 virtual void setParameters(audio_io_handle_t ioHandle,
137 const char *keyValuePairs,
138 int delayMs);
139
140 virtual status_t setStreamVolume(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700141 float volume,
142 audio_io_handle_t output,
143 int delayMs = 0);
Dima Zavinfce7a472011-04-19 22:30:36 -0700144 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145 virtual status_t stopTone();
146 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000147 virtual bool isOffloadSupported(const audio_offload_info_t &config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700148
Eric Laurent203b1a12014-04-01 10:34:16 -0700149 virtual status_t listAudioPorts(audio_port_role_t role,
150 audio_port_type_t type,
151 unsigned int *num_ports,
152 struct audio_port *ports,
153 unsigned int *generation);
154 virtual status_t getAudioPort(struct audio_port *port);
155 virtual status_t createAudioPatch(const struct audio_patch *patch,
156 audio_patch_handle_t *handle);
157 virtual status_t releaseAudioPatch(audio_patch_handle_t handle);
158 virtual status_t listAudioPatches(unsigned int *num_patches,
159 struct audio_patch *patches,
160 unsigned int *generation);
161 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
162
Eric Laurentb52c1522014-05-20 11:27:36 -0700163 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client);
164
Eric Laurentbfb1b832013-01-07 09:53:42 -0800165 status_t doStopOutput(audio_io_handle_t output,
166 audio_stream_type_t stream,
167 int session = 0);
168 void doReleaseOutput(audio_io_handle_t output);
169
Eric Laurent951f4552014-05-20 10:48:17 -0700170 status_t clientCreateAudioPatch(const struct audio_patch *patch,
171 audio_patch_handle_t *handle,
172 int delayMs);
173 status_t clientReleaseAudioPatch(audio_patch_handle_t handle,
174 int delayMs);
Eric Laurente1715a42014-05-20 11:30:42 -0700175 virtual status_t clientSetAudioPortConfig(const struct audio_port_config *config,
176 int delayMs);
177
Eric Laurentb52c1522014-05-20 11:27:36 -0700178 void removeNotificationClient(uid_t uid);
179 void onAudioPortListUpdate();
180 void doOnAudioPortListUpdate();
181 void onAudioPatchListUpdate();
182 void doOnAudioPatchListUpdate();
Eric Laurent951f4552014-05-20 10:48:17 -0700183
Mathias Agopian65ab4712010-07-14 17:59:35 -0700184private:
Mathias Agopiane762be92013-05-09 16:26:45 -0700185 AudioPolicyService() ANDROID_API;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186 virtual ~AudioPolicyService();
187
188 status_t dumpInternals(int fd);
189
190 // Thread used for tone playback and to send audio config commands to audio flinger
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700191 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because
192 // startTone() and stopTone() are normally called with mLock locked and requesting a tone start
193 // or stop will cause calls to AudioPolicyService and an attempt to lock mLock.
194 // For audio config commands, it is necessary because audio flinger requires that the calling
195 // process (user) has permission to modify audio settings.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196 class AudioCommandThread : public Thread {
197 class AudioCommand;
198 public:
199
200 // commands for tone AudioCommand
201 enum {
202 START_TONE,
203 STOP_TONE,
204 SET_VOLUME,
205 SET_PARAMETERS,
Eric Laurentbfb1b832013-01-07 09:53:42 -0800206 SET_VOICE_VOLUME,
207 STOP_OUTPUT,
Eric Laurent951f4552014-05-20 10:48:17 -0700208 RELEASE_OUTPUT,
209 CREATE_AUDIO_PATCH,
210 RELEASE_AUDIO_PATCH,
Eric Laurentb52c1522014-05-20 11:27:36 -0700211 UPDATE_AUDIOPORT_LIST,
Eric Laurente1715a42014-05-20 11:30:42 -0700212 UPDATE_AUDIOPATCH_LIST,
213 SET_AUDIOPORT_CONFIG,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700214 };
215
Eric Laurentbfb1b832013-01-07 09:53:42 -0800216 AudioCommandThread (String8 name, const wp<AudioPolicyService>& service);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217 virtual ~AudioCommandThread();
218
219 status_t dump(int fd);
220
221 // Thread virtuals
222 virtual void onFirstRef();
223 virtual bool threadLoop();
224
225 void exit();
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800226 void startToneCommand(ToneGenerator::tone_type type,
227 audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228 void stopToneCommand();
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800229 status_t volumeCommand(audio_stream_type_t stream, float volume,
230 audio_io_handle_t output, int delayMs = 0);
231 status_t parametersCommand(audio_io_handle_t ioHandle,
232 const char *keyValuePairs, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700233 status_t voiceVolumeCommand(float volume, int delayMs = 0);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800234 void stopOutputCommand(audio_io_handle_t output,
235 audio_stream_type_t stream,
236 int session);
237 void releaseOutputCommand(audio_io_handle_t output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700238 status_t sendCommand(sp<AudioCommand>& command, int delayMs = 0);
239 void insertCommand_l(sp<AudioCommand>& command, int delayMs = 0);
Eric Laurent951f4552014-05-20 10:48:17 -0700240 status_t createAudioPatchCommand(const struct audio_patch *patch,
241 audio_patch_handle_t *handle,
242 int delayMs);
243 status_t releaseAudioPatchCommand(audio_patch_handle_t handle,
244 int delayMs);
Eric Laurentb52c1522014-05-20 11:27:36 -0700245 void updateAudioPortListCommand();
246 void updateAudioPatchListCommand();
Eric Laurente1715a42014-05-20 11:30:42 -0700247 status_t setAudioPortConfigCommand(const struct audio_port_config *config,
248 int delayMs);
Eric Laurent951f4552014-05-20 10:48:17 -0700249 void insertCommand_l(AudioCommand *command, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700250
251 private:
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100252 class AudioCommandData;
253
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254 // descriptor for requested tone playback event
Eric Laurent0ede8922014-05-09 18:04:42 -0700255 class AudioCommand: public RefBase {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700256
257 public:
258 AudioCommand()
Eric Laurent0ede8922014-05-09 18:04:42 -0700259 : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700260
261 void dump(char* buffer, size_t size);
262
263 int mCommand; // START_TONE, STOP_TONE ...
264 nsecs_t mTime; // time stamp
Eric Laurent0ede8922014-05-09 18:04:42 -0700265 Mutex mLock; // mutex associated to mCond
Mathias Agopian65ab4712010-07-14 17:59:35 -0700266 Condition mCond; // condition for status return
267 status_t mStatus; // command status
268 bool mWaitStatus; // true if caller is waiting for status
Eric Laurent0ede8922014-05-09 18:04:42 -0700269 sp<AudioCommandData> mParam; // command specific parameter data
Mathias Agopian65ab4712010-07-14 17:59:35 -0700270 };
271
Eric Laurent0ede8922014-05-09 18:04:42 -0700272 class AudioCommandData: public RefBase {
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100273 public:
274 virtual ~AudioCommandData() {}
275 protected:
276 AudioCommandData() {}
277 };
278
279 class ToneData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280 public:
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800281 ToneGenerator::tone_type mType; // tone type (START_TONE only)
Glenn Kastenfff6d712012-01-12 16:38:12 -0800282 audio_stream_type_t mStream; // stream type (START_TONE only)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700283 };
284
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100285 class VolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286 public:
Glenn Kastenfff6d712012-01-12 16:38:12 -0800287 audio_stream_type_t mStream;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700288 float mVolume;
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800289 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700290 };
291
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100292 class ParametersData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700293 public:
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800294 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295 String8 mKeyValuePairs;
296 };
297
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100298 class VoiceVolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299 public:
300 float mVolume;
301 };
302
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100303 class StopOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800304 public:
305 audio_io_handle_t mIO;
306 audio_stream_type_t mStream;
307 int mSession;
308 };
309
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100310 class ReleaseOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800311 public:
312 audio_io_handle_t mIO;
313 };
314
Eric Laurent951f4552014-05-20 10:48:17 -0700315 class CreateAudioPatchData : public AudioCommandData {
316 public:
317 struct audio_patch mPatch;
318 audio_patch_handle_t mHandle;
319 };
320
321 class ReleaseAudioPatchData : public AudioCommandData {
322 public:
323 audio_patch_handle_t mHandle;
324 };
325
Eric Laurente1715a42014-05-20 11:30:42 -0700326 class SetAudioPortConfigData : public AudioCommandData {
327 public:
328 struct audio_port_config mConfig;
329 };
330
Mathias Agopian65ab4712010-07-14 17:59:35 -0700331 Mutex mLock;
332 Condition mWaitWorkCV;
Eric Laurent0ede8922014-05-09 18:04:42 -0700333 Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands
Mathias Agopian65ab4712010-07-14 17:59:35 -0700334 ToneGenerator *mpToneGenerator; // the tone generator
Eric Laurent0ede8922014-05-09 18:04:42 -0700335 sp<AudioCommand> mLastCommand; // last processed command (used by dump)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 String8 mName; // string used by wake lock fo delayed commands
Eric Laurentbfb1b832013-01-07 09:53:42 -0800337 wp<AudioPolicyService> mService;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338 };
339
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700340 class EffectDesc {
341 public:
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800342 EffectDesc(const char *name, const effect_uuid_t& uuid) :
343 mName(strdup(name)),
344 mUuid(uuid) { }
345 EffectDesc(const EffectDesc& orig) :
346 mName(strdup(orig.mName)),
347 mUuid(orig.mUuid) {
348 // deep copy mParams
349 for (size_t k = 0; k < orig.mParams.size(); k++) {
350 effect_param_t *origParam = orig.mParams[k];
351 // psize and vsize are rounded up to an int boundary for allocation
352 size_t origSize = sizeof(effect_param_t) +
353 ((origParam->psize + 3) & ~3) +
354 ((origParam->vsize + 3) & ~3);
355 effect_param_t *dupParam = (effect_param_t *) malloc(origSize);
356 memcpy(dupParam, origParam, origSize);
357 // This works because the param buffer allocation is also done by
358 // multiples of 4 bytes originally. In theory we should memcpy only
359 // the actual param size, that is without rounding vsize.
360 mParams.add(dupParam);
361 }
362 }
363 /*virtual*/ ~EffectDesc() {
364 free(mName);
365 for (size_t k = 0; k < mParams.size(); k++) {
366 free(mParams[k]);
367 }
368 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700369 char *mName;
370 effect_uuid_t mUuid;
371 Vector <effect_param_t *> mParams;
372 };
373
374 class InputSourceDesc {
375 public:
376 InputSourceDesc() {}
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800377 /*virtual*/ ~InputSourceDesc() {
378 for (size_t j = 0; j < mEffects.size(); j++) {
379 delete mEffects[j];
380 }
381 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700382 Vector <EffectDesc *> mEffects;
383 };
384
385
386 class InputDesc {
387 public:
Glenn Kasten81872a22012-03-07 16:49:22 -0800388 InputDesc(int session) : mSessionId(session) {}
389 /*virtual*/ ~InputDesc() {}
390 const int mSessionId;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700391 Vector< sp<AudioEffect> >mEffects;
392 };
393
Eric Laurentdce54a12014-03-10 12:19:46 -0700394 class AudioPolicyClient : public AudioPolicyClientInterface
395 {
396 public:
397 AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {}
398 virtual ~AudioPolicyClient() {}
399
400 //
401 // Audio HW module functions
402 //
403
404 // loads a HW module.
405 virtual audio_module_handle_t loadHwModule(const char *name);
406
407 //
408 // Audio output Control functions
409 //
410
411 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
412 // in case the audio policy manager has no specific requirements for the output being opened.
413 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
414 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
415 virtual audio_io_handle_t openOutput(audio_module_handle_t module,
416 audio_devices_t *pDevices,
417 uint32_t *pSamplingRate,
418 audio_format_t *pFormat,
419 audio_channel_mask_t *pChannelMask,
420 uint32_t *pLatencyMs,
421 audio_output_flags_t flags,
422 const audio_offload_info_t *offloadInfo = NULL);
423 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
424 // a special mixer thread in the AudioFlinger.
425 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2);
426 // closes the output stream
427 virtual status_t closeOutput(audio_io_handle_t output);
428 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
429 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
430 virtual status_t suspendOutput(audio_io_handle_t output);
431 // restores a suspended output.
432 virtual status_t restoreOutput(audio_io_handle_t output);
433
434 //
435 // Audio input Control functions
436 //
437
438 // opens an audio input
439 virtual audio_io_handle_t openInput(audio_module_handle_t module,
440 audio_devices_t *pDevices,
441 uint32_t *pSamplingRate,
442 audio_format_t *pFormat,
443 audio_channel_mask_t *pChannelMask);
444 // closes an audio input
445 virtual status_t closeInput(audio_io_handle_t input);
446 //
447 // misc control functions
448 //
449
450 // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
451 // for each output (destination device) it is attached to.
452 virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0);
453
454 // invalidate a stream type, causing a reroute to an unspecified new output
455 virtual status_t invalidateStream(audio_stream_type_t stream);
456
457 // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
458 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0);
459 // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager.
460 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
461
462 // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing
463 // over a telephony device during a phone call.
464 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
465 virtual status_t stopTone();
466
467 // set down link audio volume.
468 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
469
470 // move effect to the specified output
471 virtual status_t moveEffects(int session,
472 audio_io_handle_t srcOutput,
473 audio_io_handle_t dstOutput);
474
Eric Laurent951f4552014-05-20 10:48:17 -0700475 /* Create a patch between several source and sink ports */
476 virtual status_t createAudioPatch(const struct audio_patch *patch,
477 audio_patch_handle_t *handle,
478 int delayMs);
479
480 /* Release a patch */
481 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
482 int delayMs);
483
Eric Laurente1715a42014-05-20 11:30:42 -0700484 /* Set audio port configuration */
485 virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs);
486
Eric Laurentb52c1522014-05-20 11:27:36 -0700487 virtual void onAudioPortListUpdate();
488 virtual void onAudioPatchListUpdate();
489
Eric Laurentdce54a12014-03-10 12:19:46 -0700490 private:
491 AudioPolicyService *mAudioPolicyService;
492 };
493
Eric Laurentb52c1522014-05-20 11:27:36 -0700494 // --- Notification Client ---
495 class NotificationClient : public IBinder::DeathRecipient {
496 public:
497 NotificationClient(const sp<AudioPolicyService>& service,
498 const sp<IAudioPolicyServiceClient>& client,
499 uid_t uid);
500 virtual ~NotificationClient();
501
502 void onAudioPortListUpdate();
503 void onAudioPatchListUpdate();
504
505 // IBinder::DeathRecipient
506 virtual void binderDied(const wp<IBinder>& who);
507
508 private:
509 NotificationClient(const NotificationClient&);
510 NotificationClient& operator = (const NotificationClient&);
511
512 const wp<AudioPolicyService> mService;
513 const uid_t mUid;
514 const sp<IAudioPolicyServiceClient> mAudioPolicyServiceClient;
515 };
516
Glenn Kasten8dad0e32012-01-09 08:41:22 -0800517 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700518
Glenn Kasten81872a22012-03-07 16:49:22 -0800519 void setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700520 status_t loadPreProcessorConfig(const char *path);
521 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
522 EffectDesc *loadEffect(cnode *root);
523 status_t loadInputSources(cnode *root, const Vector <EffectDesc *>& effects);
524 audio_source_t inputSourceNameToEnum(const char *name);
525 InputSourceDesc *loadInputSource(cnode *root, const Vector <EffectDesc *>& effects);
526 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
527 effect_param_t *loadEffectParameter(cnode *root);
528 size_t readParamValue(cnode *node,
529 char *param,
530 size_t *curSize,
531 size_t *totSize);
532 size_t growParamSize(char *param,
533 size_t size,
534 size_t *curSize,
535 size_t *totSize);
536
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537 // Internal dump utilities.
538 status_t dumpPermissionDenial(int fd);
539
540
Eric Laurenteda6c362011-02-02 09:33:30 -0800541 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
542 // device connection state or routing
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700543 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread
544 sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -0800545 sp<AudioCommandThread> mOutputCommandThread; // process stop and release output
Dima Zavinfce7a472011-04-19 22:30:36 -0700546 struct audio_policy_device *mpAudioPolicyDev;
547 struct audio_policy *mpAudioPolicy;
Eric Laurentf269b8e2014-06-09 20:01:29 -0700548 AudioPolicyInterface *mAudioPolicyManager;
Eric Laurentdce54a12014-03-10 12:19:46 -0700549 AudioPolicyClient *mAudioPolicyClient;
550
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700551 KeyedVector< audio_source_t, InputSourceDesc* > mInputSources;
552 KeyedVector< audio_io_handle_t, InputDesc* > mInputs;
Eric Laurentb52c1522014-05-20 11:27:36 -0700553
554 DefaultKeyedVector< uid_t, sp<NotificationClient> > mNotificationClients;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700555};
556
557}; // namespace android
558
559#endif // ANDROID_AUDIOPOLICYSERVICE_H