blob: 40f589b1e9d9644084e949d062d337035c18d744 [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);
Eric Laurentde070132010-07-13 04:45:46 -070073 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070074 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -070075 int session = 0);
76 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070077 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -070078 int session = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -070079 virtual void releaseOutput(audio_io_handle_t output);
Glenn Kasteneba51fb2012-01-23 13:58:49 -080080 virtual audio_io_handle_t getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -070081 uint32_t samplingRate = 0,
Glenn Kasten58f30212012-01-12 12:27:51 -080082 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten254af182012-07-03 14:59:05 -070083 audio_channel_mask_t channelMask = 0,
Eric Laurent7c7f10b2011-06-17 21:29:58 -070084 int audioSession = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -070085 virtual status_t startInput(audio_io_handle_t input);
86 virtual status_t stopInput(audio_io_handle_t input);
87 virtual void releaseInput(audio_io_handle_t input);
Dima Zavinfce7a472011-04-19 22:30:36 -070088 virtual status_t initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -070089 int indexMin,
90 int indexMax);
Eric Laurent83844cc2011-11-18 16:43:31 -080091 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
92 int index,
93 audio_devices_t device);
94 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
95 int *index,
96 audio_devices_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -070097
Dima Zavinfce7a472011-04-19 22:30:36 -070098 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Eric Laurent63742522012-03-08 13:42:42 -080099 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurentde070132010-07-13 04:45:46 -0700100
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700101 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
102 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700103 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700104 uint32_t strategy,
105 int session,
106 int id);
107 virtual status_t unregisterEffect(int id);
Eric Laurentdb7c0792011-08-10 10:37:50 -0700108 virtual status_t setEffectEnabled(int id, bool enabled);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800109 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800110 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700111 virtual bool isSourceActive(audio_source_t source) const;
Eric Laurentde070132010-07-13 04:45:46 -0700112
Eric Laurent57dae992011-07-24 13:36:09 -0700113 virtual status_t queryDefaultPreProcessing(int audioSession,
114 effect_descriptor_t *descriptors,
115 uint32_t *count);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700116 virtual status_t onTransact(
117 uint32_t code,
118 const Parcel& data,
119 Parcel* reply,
120 uint32_t flags);
121
122 // IBinder::DeathRecipient
123 virtual void binderDied(const wp<IBinder>& who);
124
125 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700126 // Helpers for the struct audio_policy_service_ops implementation.
127 // This is used by the audio policy manager for certain operations that
128 // are implemented by the policy service.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700129 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700130 virtual void setParameters(audio_io_handle_t ioHandle,
131 const char *keyValuePairs,
132 int delayMs);
133
134 virtual status_t setStreamVolume(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700135 float volume,
136 audio_io_handle_t output,
137 int delayMs = 0);
Dima Zavinfce7a472011-04-19 22:30:36 -0700138 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700139 virtual status_t stopTone();
140 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000141 virtual bool isOffloadSupported(const audio_offload_info_t &config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700142
Eric Laurent203b1a12014-04-01 10:34:16 -0700143 virtual status_t listAudioPorts(audio_port_role_t role,
144 audio_port_type_t type,
145 unsigned int *num_ports,
146 struct audio_port *ports,
147 unsigned int *generation);
148 virtual status_t getAudioPort(struct audio_port *port);
149 virtual status_t createAudioPatch(const struct audio_patch *patch,
150 audio_patch_handle_t *handle);
151 virtual status_t releaseAudioPatch(audio_patch_handle_t handle);
152 virtual status_t listAudioPatches(unsigned int *num_patches,
153 struct audio_patch *patches,
154 unsigned int *generation);
155 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
156
Eric Laurentb52c1522014-05-20 11:27:36 -0700157 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client);
158
Eric Laurentbfb1b832013-01-07 09:53:42 -0800159 status_t doStopOutput(audio_io_handle_t output,
160 audio_stream_type_t stream,
161 int session = 0);
162 void doReleaseOutput(audio_io_handle_t output);
163
Eric Laurent951f4552014-05-20 10:48:17 -0700164 status_t clientCreateAudioPatch(const struct audio_patch *patch,
165 audio_patch_handle_t *handle,
166 int delayMs);
167 status_t clientReleaseAudioPatch(audio_patch_handle_t handle,
168 int delayMs);
Eric Laurente1715a42014-05-20 11:30:42 -0700169 virtual status_t clientSetAudioPortConfig(const struct audio_port_config *config,
170 int delayMs);
171
Eric Laurentb52c1522014-05-20 11:27:36 -0700172 void removeNotificationClient(uid_t uid);
173 void onAudioPortListUpdate();
174 void doOnAudioPortListUpdate();
175 void onAudioPatchListUpdate();
176 void doOnAudioPatchListUpdate();
Eric Laurent951f4552014-05-20 10:48:17 -0700177
Mathias Agopian65ab4712010-07-14 17:59:35 -0700178private:
Mathias Agopiane762be92013-05-09 16:26:45 -0700179 AudioPolicyService() ANDROID_API;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700180 virtual ~AudioPolicyService();
181
182 status_t dumpInternals(int fd);
183
184 // Thread used for tone playback and to send audio config commands to audio flinger
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700185 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because
186 // startTone() and stopTone() are normally called with mLock locked and requesting a tone start
187 // or stop will cause calls to AudioPolicyService and an attempt to lock mLock.
188 // For audio config commands, it is necessary because audio flinger requires that the calling
189 // process (user) has permission to modify audio settings.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700190 class AudioCommandThread : public Thread {
191 class AudioCommand;
192 public:
193
194 // commands for tone AudioCommand
195 enum {
196 START_TONE,
197 STOP_TONE,
198 SET_VOLUME,
199 SET_PARAMETERS,
Eric Laurentbfb1b832013-01-07 09:53:42 -0800200 SET_VOICE_VOLUME,
201 STOP_OUTPUT,
Eric Laurent951f4552014-05-20 10:48:17 -0700202 RELEASE_OUTPUT,
203 CREATE_AUDIO_PATCH,
204 RELEASE_AUDIO_PATCH,
Eric Laurentb52c1522014-05-20 11:27:36 -0700205 UPDATE_AUDIOPORT_LIST,
Eric Laurente1715a42014-05-20 11:30:42 -0700206 UPDATE_AUDIOPATCH_LIST,
207 SET_AUDIOPORT_CONFIG,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208 };
209
Eric Laurentbfb1b832013-01-07 09:53:42 -0800210 AudioCommandThread (String8 name, const wp<AudioPolicyService>& service);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700211 virtual ~AudioCommandThread();
212
213 status_t dump(int fd);
214
215 // Thread virtuals
216 virtual void onFirstRef();
217 virtual bool threadLoop();
218
219 void exit();
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800220 void startToneCommand(ToneGenerator::tone_type type,
221 audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700222 void stopToneCommand();
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800223 status_t volumeCommand(audio_stream_type_t stream, float volume,
224 audio_io_handle_t output, int delayMs = 0);
225 status_t parametersCommand(audio_io_handle_t ioHandle,
226 const char *keyValuePairs, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700227 status_t voiceVolumeCommand(float volume, int delayMs = 0);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800228 void stopOutputCommand(audio_io_handle_t output,
229 audio_stream_type_t stream,
230 int session);
231 void releaseOutputCommand(audio_io_handle_t output);
Eric Laurent0ede8922014-05-09 18:04:42 -0700232 status_t sendCommand(sp<AudioCommand>& command, int delayMs = 0);
233 void insertCommand_l(sp<AudioCommand>& command, int delayMs = 0);
Eric Laurent951f4552014-05-20 10:48:17 -0700234 status_t createAudioPatchCommand(const struct audio_patch *patch,
235 audio_patch_handle_t *handle,
236 int delayMs);
237 status_t releaseAudioPatchCommand(audio_patch_handle_t handle,
238 int delayMs);
Eric Laurentb52c1522014-05-20 11:27:36 -0700239 void updateAudioPortListCommand();
240 void updateAudioPatchListCommand();
Eric Laurente1715a42014-05-20 11:30:42 -0700241 status_t setAudioPortConfigCommand(const struct audio_port_config *config,
242 int delayMs);
Eric Laurent951f4552014-05-20 10:48:17 -0700243 void insertCommand_l(AudioCommand *command, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244
245 private:
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100246 class AudioCommandData;
247
Mathias Agopian65ab4712010-07-14 17:59:35 -0700248 // descriptor for requested tone playback event
Eric Laurent0ede8922014-05-09 18:04:42 -0700249 class AudioCommand: public RefBase {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700250
251 public:
252 AudioCommand()
Eric Laurent0ede8922014-05-09 18:04:42 -0700253 : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254
255 void dump(char* buffer, size_t size);
256
257 int mCommand; // START_TONE, STOP_TONE ...
258 nsecs_t mTime; // time stamp
Eric Laurent0ede8922014-05-09 18:04:42 -0700259 Mutex mLock; // mutex associated to mCond
Mathias Agopian65ab4712010-07-14 17:59:35 -0700260 Condition mCond; // condition for status return
261 status_t mStatus; // command status
262 bool mWaitStatus; // true if caller is waiting for status
Eric Laurent0ede8922014-05-09 18:04:42 -0700263 sp<AudioCommandData> mParam; // command specific parameter data
Mathias Agopian65ab4712010-07-14 17:59:35 -0700264 };
265
Eric Laurent0ede8922014-05-09 18:04:42 -0700266 class AudioCommandData: public RefBase {
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100267 public:
268 virtual ~AudioCommandData() {}
269 protected:
270 AudioCommandData() {}
271 };
272
273 class ToneData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274 public:
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800275 ToneGenerator::tone_type mType; // tone type (START_TONE only)
Glenn Kastenfff6d712012-01-12 16:38:12 -0800276 audio_stream_type_t mStream; // stream type (START_TONE only)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277 };
278
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100279 class VolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700280 public:
Glenn Kastenfff6d712012-01-12 16:38:12 -0800281 audio_stream_type_t mStream;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282 float mVolume;
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800283 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284 };
285
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100286 class ParametersData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287 public:
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800288 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289 String8 mKeyValuePairs;
290 };
291
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100292 class VoiceVolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700293 public:
294 float mVolume;
295 };
296
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100297 class StopOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800298 public:
299 audio_io_handle_t mIO;
300 audio_stream_type_t mStream;
301 int mSession;
302 };
303
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100304 class ReleaseOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800305 public:
306 audio_io_handle_t mIO;
307 };
308
Eric Laurent951f4552014-05-20 10:48:17 -0700309 class CreateAudioPatchData : public AudioCommandData {
310 public:
311 struct audio_patch mPatch;
312 audio_patch_handle_t mHandle;
313 };
314
315 class ReleaseAudioPatchData : public AudioCommandData {
316 public:
317 audio_patch_handle_t mHandle;
318 };
319
Eric Laurente1715a42014-05-20 11:30:42 -0700320 class SetAudioPortConfigData : public AudioCommandData {
321 public:
322 struct audio_port_config mConfig;
323 };
324
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325 Mutex mLock;
326 Condition mWaitWorkCV;
Eric Laurent0ede8922014-05-09 18:04:42 -0700327 Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328 ToneGenerator *mpToneGenerator; // the tone generator
Eric Laurent0ede8922014-05-09 18:04:42 -0700329 sp<AudioCommand> mLastCommand; // last processed command (used by dump)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330 String8 mName; // string used by wake lock fo delayed commands
Eric Laurentbfb1b832013-01-07 09:53:42 -0800331 wp<AudioPolicyService> mService;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332 };
333
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700334 class EffectDesc {
335 public:
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800336 EffectDesc(const char *name, const effect_uuid_t& uuid) :
337 mName(strdup(name)),
338 mUuid(uuid) { }
339 EffectDesc(const EffectDesc& orig) :
340 mName(strdup(orig.mName)),
341 mUuid(orig.mUuid) {
342 // deep copy mParams
343 for (size_t k = 0; k < orig.mParams.size(); k++) {
344 effect_param_t *origParam = orig.mParams[k];
345 // psize and vsize are rounded up to an int boundary for allocation
346 size_t origSize = sizeof(effect_param_t) +
347 ((origParam->psize + 3) & ~3) +
348 ((origParam->vsize + 3) & ~3);
349 effect_param_t *dupParam = (effect_param_t *) malloc(origSize);
350 memcpy(dupParam, origParam, origSize);
351 // This works because the param buffer allocation is also done by
352 // multiples of 4 bytes originally. In theory we should memcpy only
353 // the actual param size, that is without rounding vsize.
354 mParams.add(dupParam);
355 }
356 }
357 /*virtual*/ ~EffectDesc() {
358 free(mName);
359 for (size_t k = 0; k < mParams.size(); k++) {
360 free(mParams[k]);
361 }
362 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700363 char *mName;
364 effect_uuid_t mUuid;
365 Vector <effect_param_t *> mParams;
366 };
367
368 class InputSourceDesc {
369 public:
370 InputSourceDesc() {}
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800371 /*virtual*/ ~InputSourceDesc() {
372 for (size_t j = 0; j < mEffects.size(); j++) {
373 delete mEffects[j];
374 }
375 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700376 Vector <EffectDesc *> mEffects;
377 };
378
379
380 class InputDesc {
381 public:
Glenn Kasten81872a22012-03-07 16:49:22 -0800382 InputDesc(int session) : mSessionId(session) {}
383 /*virtual*/ ~InputDesc() {}
384 const int mSessionId;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700385 Vector< sp<AudioEffect> >mEffects;
386 };
387
Eric Laurentdce54a12014-03-10 12:19:46 -0700388 class AudioPolicyClient : public AudioPolicyClientInterface
389 {
390 public:
391 AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {}
392 virtual ~AudioPolicyClient() {}
393
394 //
395 // Audio HW module functions
396 //
397
398 // loads a HW module.
399 virtual audio_module_handle_t loadHwModule(const char *name);
400
401 //
402 // Audio output Control functions
403 //
404
405 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
406 // in case the audio policy manager has no specific requirements for the output being opened.
407 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
408 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
409 virtual audio_io_handle_t openOutput(audio_module_handle_t module,
410 audio_devices_t *pDevices,
411 uint32_t *pSamplingRate,
412 audio_format_t *pFormat,
413 audio_channel_mask_t *pChannelMask,
414 uint32_t *pLatencyMs,
415 audio_output_flags_t flags,
416 const audio_offload_info_t *offloadInfo = NULL);
417 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
418 // a special mixer thread in the AudioFlinger.
419 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2);
420 // closes the output stream
421 virtual status_t closeOutput(audio_io_handle_t output);
422 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
423 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
424 virtual status_t suspendOutput(audio_io_handle_t output);
425 // restores a suspended output.
426 virtual status_t restoreOutput(audio_io_handle_t output);
427
428 //
429 // Audio input Control functions
430 //
431
432 // opens an audio input
433 virtual audio_io_handle_t openInput(audio_module_handle_t module,
434 audio_devices_t *pDevices,
435 uint32_t *pSamplingRate,
436 audio_format_t *pFormat,
437 audio_channel_mask_t *pChannelMask);
438 // 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();
483
Eric Laurentdce54a12014-03-10 12:19:46 -0700484 private:
485 AudioPolicyService *mAudioPolicyService;
486 };
487
Eric Laurentb52c1522014-05-20 11:27:36 -0700488 // --- Notification Client ---
489 class NotificationClient : public IBinder::DeathRecipient {
490 public:
491 NotificationClient(const sp<AudioPolicyService>& service,
492 const sp<IAudioPolicyServiceClient>& client,
493 uid_t uid);
494 virtual ~NotificationClient();
495
496 void onAudioPortListUpdate();
497 void onAudioPatchListUpdate();
498
499 // IBinder::DeathRecipient
500 virtual void binderDied(const wp<IBinder>& who);
501
502 private:
503 NotificationClient(const NotificationClient&);
504 NotificationClient& operator = (const NotificationClient&);
505
506 const wp<AudioPolicyService> mService;
507 const uid_t mUid;
508 const sp<IAudioPolicyServiceClient> mAudioPolicyServiceClient;
509 };
510
Glenn Kasten8dad0e32012-01-09 08:41:22 -0800511 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700512
Glenn Kasten81872a22012-03-07 16:49:22 -0800513 void setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700514 status_t loadPreProcessorConfig(const char *path);
515 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
516 EffectDesc *loadEffect(cnode *root);
517 status_t loadInputSources(cnode *root, const Vector <EffectDesc *>& effects);
518 audio_source_t inputSourceNameToEnum(const char *name);
519 InputSourceDesc *loadInputSource(cnode *root, const Vector <EffectDesc *>& effects);
520 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
521 effect_param_t *loadEffectParameter(cnode *root);
522 size_t readParamValue(cnode *node,
523 char *param,
524 size_t *curSize,
525 size_t *totSize);
526 size_t growParamSize(char *param,
527 size_t size,
528 size_t *curSize,
529 size_t *totSize);
530
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 // Internal dump utilities.
532 status_t dumpPermissionDenial(int fd);
533
534
Eric Laurenteda6c362011-02-02 09:33:30 -0800535 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
536 // device connection state or routing
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700537 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread
538 sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -0800539 sp<AudioCommandThread> mOutputCommandThread; // process stop and release output
Dima Zavinfce7a472011-04-19 22:30:36 -0700540 struct audio_policy_device *mpAudioPolicyDev;
541 struct audio_policy *mpAudioPolicy;
Eric Laurentdce54a12014-03-10 12:19:46 -0700542 AudioPolicyManager *mAudioPolicyManager;
543 AudioPolicyClient *mAudioPolicyClient;
544
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700545 KeyedVector< audio_source_t, InputSourceDesc* > mInputSources;
546 KeyedVector< audio_io_handle_t, InputDesc* > mInputs;
Eric Laurentb52c1522014-05-20 11:27:36 -0700547
548 DefaultKeyedVector< uid_t, sp<NotificationClient> > mNotificationClients;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549};
550
551}; // namespace android
552
553#endif // ANDROID_AUDIOPOLICYSERVICE_H