blob: a38160f5bcc659635f2b41f3956a397a469c8ab6 [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>
Dima Zavinfce7a472011-04-19 22:30:36 -070033
Mathias Agopian65ab4712010-07-14 17:59:35 -070034namespace android {
35
Mathias Agopian65ab4712010-07-14 17:59:35 -070036// ----------------------------------------------------------------------------
37
Glenn Kastend2dcb082011-02-03 16:55:26 -080038class AudioPolicyService :
39 public BinderService<AudioPolicyService>,
40 public BnAudioPolicyService,
Dima Zavinfce7a472011-04-19 22:30:36 -070041// public AudioPolicyClientInterface,
Eric Laurentde070132010-07-13 04:45:46 -070042 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070043{
Glenn Kastend2dcb082011-02-03 16:55:26 -080044 friend class BinderService<AudioPolicyService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070045
46public:
Glenn Kastend2dcb082011-02-03 16:55:26 -080047 // for BinderService
Mathias Agopiane762be92013-05-09 16:26:45 -070048 static const char *getServiceName() ANDROID_API { return "media.audio_policy"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
50 virtual status_t dump(int fd, const Vector<String16>& args);
51
52 //
53 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
54 //
55
Dima Zavinfce7a472011-04-19 22:30:36 -070056 virtual status_t setDeviceConnectionState(audio_devices_t device,
57 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -070058 const char *device_address);
Dima Zavinfce7a472011-04-19 22:30:36 -070059 virtual audio_policy_dev_state_t getDeviceConnectionState(
60 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -070061 const char *device_address);
Glenn Kastenf78aee72012-01-04 11:00:47 -080062 virtual status_t setPhoneState(audio_mode_t state);
Dima Zavinfce7a472011-04-19 22:30:36 -070063 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
64 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
65 virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -070066 uint32_t samplingRate = 0,
Glenn Kasten58f30212012-01-12 12:27:51 -080067 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten254af182012-07-03 14:59:05 -070068 audio_channel_mask_t channelMask = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -070069 audio_output_flags_t flags =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +000070 AUDIO_OUTPUT_FLAG_NONE,
71 const audio_offload_info_t *offloadInfo = NULL);
Eric Laurentde070132010-07-13 04:45:46 -070072 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070073 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -070074 int session = 0);
75 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070076 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -070077 int session = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -070078 virtual void releaseOutput(audio_io_handle_t output);
Glenn Kasteneba51fb2012-01-23 13:58:49 -080079 virtual audio_io_handle_t getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -070080 uint32_t samplingRate = 0,
Glenn Kasten58f30212012-01-12 12:27:51 -080081 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten254af182012-07-03 14:59:05 -070082 audio_channel_mask_t channelMask = 0,
Eric Laurent7c7f10b2011-06-17 21:29:58 -070083 int audioSession = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -070084 virtual status_t startInput(audio_io_handle_t input);
85 virtual status_t stopInput(audio_io_handle_t input);
86 virtual void releaseInput(audio_io_handle_t input);
Dima Zavinfce7a472011-04-19 22:30:36 -070087 virtual status_t initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -070088 int indexMin,
89 int indexMax);
Eric Laurent83844cc2011-11-18 16:43:31 -080090 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
91 int index,
92 audio_devices_t device);
93 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
94 int *index,
95 audio_devices_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -070096
Dima Zavinfce7a472011-04-19 22:30:36 -070097 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Eric Laurent63742522012-03-08 13:42:42 -080098 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurentde070132010-07-13 04:45:46 -070099
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700100 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
101 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700102 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700103 uint32_t strategy,
104 int session,
105 int id);
106 virtual status_t unregisterEffect(int id);
Eric Laurentdb7c0792011-08-10 10:37:50 -0700107 virtual status_t setEffectEnabled(int id, bool enabled);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800108 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800109 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700110 virtual bool isSourceActive(audio_source_t source) const;
Eric Laurentde070132010-07-13 04:45:46 -0700111
Eric Laurent57dae992011-07-24 13:36:09 -0700112 virtual status_t queryDefaultPreProcessing(int audioSession,
113 effect_descriptor_t *descriptors,
114 uint32_t *count);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700115 virtual status_t onTransact(
116 uint32_t code,
117 const Parcel& data,
118 Parcel* reply,
119 uint32_t flags);
120
121 // IBinder::DeathRecipient
122 virtual void binderDied(const wp<IBinder>& who);
123
124 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700125 // Helpers for the struct audio_policy_service_ops implementation.
126 // This is used by the audio policy manager for certain operations that
127 // are implemented by the policy service.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700128 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700129 virtual void setParameters(audio_io_handle_t ioHandle,
130 const char *keyValuePairs,
131 int delayMs);
132
133 virtual status_t setStreamVolume(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700134 float volume,
135 audio_io_handle_t output,
136 int delayMs = 0);
Dima Zavinfce7a472011-04-19 22:30:36 -0700137 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700138 virtual status_t stopTone();
139 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000140 virtual bool isOffloadSupported(const audio_offload_info_t &config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700141
Eric Laurentbfb1b832013-01-07 09:53:42 -0800142 status_t doStopOutput(audio_io_handle_t output,
143 audio_stream_type_t stream,
144 int session = 0);
145 void doReleaseOutput(audio_io_handle_t output);
146
Mathias Agopian65ab4712010-07-14 17:59:35 -0700147private:
Mathias Agopiane762be92013-05-09 16:26:45 -0700148 AudioPolicyService() ANDROID_API;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149 virtual ~AudioPolicyService();
150
151 status_t dumpInternals(int fd);
152
153 // Thread used for tone playback and to send audio config commands to audio flinger
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700154 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because
155 // startTone() and stopTone() are normally called with mLock locked and requesting a tone start
156 // or stop will cause calls to AudioPolicyService and an attempt to lock mLock.
157 // For audio config commands, it is necessary because audio flinger requires that the calling
158 // process (user) has permission to modify audio settings.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700159 class AudioCommandThread : public Thread {
160 class AudioCommand;
161 public:
162
163 // commands for tone AudioCommand
164 enum {
165 START_TONE,
166 STOP_TONE,
167 SET_VOLUME,
168 SET_PARAMETERS,
Eric Laurentbfb1b832013-01-07 09:53:42 -0800169 SET_VOICE_VOLUME,
170 STOP_OUTPUT,
171 RELEASE_OUTPUT
Mathias Agopian65ab4712010-07-14 17:59:35 -0700172 };
173
Eric Laurentbfb1b832013-01-07 09:53:42 -0800174 AudioCommandThread (String8 name, const wp<AudioPolicyService>& service);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700175 virtual ~AudioCommandThread();
176
177 status_t dump(int fd);
178
179 // Thread virtuals
180 virtual void onFirstRef();
181 virtual bool threadLoop();
182
183 void exit();
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800184 void startToneCommand(ToneGenerator::tone_type type,
185 audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186 void stopToneCommand();
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800187 status_t volumeCommand(audio_stream_type_t stream, float volume,
188 audio_io_handle_t output, int delayMs = 0);
189 status_t parametersCommand(audio_io_handle_t ioHandle,
190 const char *keyValuePairs, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700191 status_t voiceVolumeCommand(float volume, int delayMs = 0);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800192 void stopOutputCommand(audio_io_handle_t output,
193 audio_stream_type_t stream,
194 int session);
195 void releaseOutputCommand(audio_io_handle_t output);
196
Mathias Agopian65ab4712010-07-14 17:59:35 -0700197 void insertCommand_l(AudioCommand *command, int delayMs = 0);
198
199 private:
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100200 class AudioCommandData;
201
Mathias Agopian65ab4712010-07-14 17:59:35 -0700202 // descriptor for requested tone playback event
203 class AudioCommand {
204
205 public:
206 AudioCommand()
207 : mCommand(-1) {}
208
209 void dump(char* buffer, size_t size);
210
211 int mCommand; // START_TONE, STOP_TONE ...
212 nsecs_t mTime; // time stamp
213 Condition mCond; // condition for status return
214 status_t mStatus; // command status
215 bool mWaitStatus; // true if caller is waiting for status
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100216 AudioCommandData *mParam; // command specific parameter data
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217 };
218
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100219 class AudioCommandData {
220 public:
221 virtual ~AudioCommandData() {}
222 protected:
223 AudioCommandData() {}
224 };
225
226 class ToneData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700227 public:
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800228 ToneGenerator::tone_type mType; // tone type (START_TONE only)
Glenn Kastenfff6d712012-01-12 16:38:12 -0800229 audio_stream_type_t mStream; // stream type (START_TONE only)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700230 };
231
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100232 class VolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700233 public:
Glenn Kastenfff6d712012-01-12 16:38:12 -0800234 audio_stream_type_t mStream;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700235 float mVolume;
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800236 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700237 };
238
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100239 class ParametersData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700240 public:
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800241 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242 String8 mKeyValuePairs;
243 };
244
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100245 class VoiceVolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700246 public:
247 float mVolume;
248 };
249
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100250 class StopOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800251 public:
252 audio_io_handle_t mIO;
253 audio_stream_type_t mStream;
254 int mSession;
255 };
256
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100257 class ReleaseOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800258 public:
259 audio_io_handle_t mIO;
260 };
261
Mathias Agopian65ab4712010-07-14 17:59:35 -0700262 Mutex mLock;
263 Condition mWaitWorkCV;
264 Vector <AudioCommand *> mAudioCommands; // list of pending commands
265 ToneGenerator *mpToneGenerator; // the tone generator
266 AudioCommand mLastCommand; // last processed command (used by dump)
267 String8 mName; // string used by wake lock fo delayed commands
Eric Laurentbfb1b832013-01-07 09:53:42 -0800268 wp<AudioPolicyService> mService;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269 };
270
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700271 class EffectDesc {
272 public:
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800273 EffectDesc(const char *name, const effect_uuid_t& uuid) :
274 mName(strdup(name)),
275 mUuid(uuid) { }
276 EffectDesc(const EffectDesc& orig) :
277 mName(strdup(orig.mName)),
278 mUuid(orig.mUuid) {
279 // deep copy mParams
280 for (size_t k = 0; k < orig.mParams.size(); k++) {
281 effect_param_t *origParam = orig.mParams[k];
282 // psize and vsize are rounded up to an int boundary for allocation
283 size_t origSize = sizeof(effect_param_t) +
284 ((origParam->psize + 3) & ~3) +
285 ((origParam->vsize + 3) & ~3);
286 effect_param_t *dupParam = (effect_param_t *) malloc(origSize);
287 memcpy(dupParam, origParam, origSize);
288 // This works because the param buffer allocation is also done by
289 // multiples of 4 bytes originally. In theory we should memcpy only
290 // the actual param size, that is without rounding vsize.
291 mParams.add(dupParam);
292 }
293 }
294 /*virtual*/ ~EffectDesc() {
295 free(mName);
296 for (size_t k = 0; k < mParams.size(); k++) {
297 free(mParams[k]);
298 }
299 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700300 char *mName;
301 effect_uuid_t mUuid;
302 Vector <effect_param_t *> mParams;
303 };
304
305 class InputSourceDesc {
306 public:
307 InputSourceDesc() {}
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800308 /*virtual*/ ~InputSourceDesc() {
309 for (size_t j = 0; j < mEffects.size(); j++) {
310 delete mEffects[j];
311 }
312 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700313 Vector <EffectDesc *> mEffects;
314 };
315
316
317 class InputDesc {
318 public:
Glenn Kasten81872a22012-03-07 16:49:22 -0800319 InputDesc(int session) : mSessionId(session) {}
320 /*virtual*/ ~InputDesc() {}
321 const int mSessionId;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700322 Vector< sp<AudioEffect> >mEffects;
323 };
324
Glenn Kasten8dad0e32012-01-09 08:41:22 -0800325 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700326
Glenn Kasten81872a22012-03-07 16:49:22 -0800327 void setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700328 status_t loadPreProcessorConfig(const char *path);
329 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
330 EffectDesc *loadEffect(cnode *root);
331 status_t loadInputSources(cnode *root, const Vector <EffectDesc *>& effects);
332 audio_source_t inputSourceNameToEnum(const char *name);
333 InputSourceDesc *loadInputSource(cnode *root, const Vector <EffectDesc *>& effects);
334 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
335 effect_param_t *loadEffectParameter(cnode *root);
336 size_t readParamValue(cnode *node,
337 char *param,
338 size_t *curSize,
339 size_t *totSize);
340 size_t growParamSize(char *param,
341 size_t size,
342 size_t *curSize,
343 size_t *totSize);
344
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345 // Internal dump utilities.
346 status_t dumpPermissionDenial(int fd);
347
348
Eric Laurenteda6c362011-02-02 09:33:30 -0800349 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
350 // device connection state or routing
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700351 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread
352 sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -0800353 sp<AudioCommandThread> mOutputCommandThread; // process stop and release output
Dima Zavinfce7a472011-04-19 22:30:36 -0700354 struct audio_policy_device *mpAudioPolicyDev;
355 struct audio_policy *mpAudioPolicy;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700356 KeyedVector< audio_source_t, InputSourceDesc* > mInputSources;
357 KeyedVector< audio_io_handle_t, InputDesc* > mInputs;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700358};
359
360}; // namespace android
361
362#endif // ANDROID_AUDIOPOLICYSERVICE_H