blob: 53238fa1bce04639d9f91ff2dc81f413ef370972 [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 =
70 AUDIO_OUTPUT_FLAG_NONE);
Eric Laurentde070132010-07-13 04:45:46 -070071 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070072 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -070073 int session = 0);
74 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070075 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -070076 int session = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -070077 virtual void releaseOutput(audio_io_handle_t output);
Glenn Kasteneba51fb2012-01-23 13:58:49 -080078 virtual audio_io_handle_t getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -070079 uint32_t samplingRate = 0,
Glenn Kasten58f30212012-01-12 12:27:51 -080080 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten254af182012-07-03 14:59:05 -070081 audio_channel_mask_t channelMask = 0,
Eric Laurent7c7f10b2011-06-17 21:29:58 -070082 int audioSession = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -070083 virtual status_t startInput(audio_io_handle_t input);
84 virtual status_t stopInput(audio_io_handle_t input);
85 virtual void releaseInput(audio_io_handle_t input);
Dima Zavinfce7a472011-04-19 22:30:36 -070086 virtual status_t initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -070087 int indexMin,
88 int indexMax);
Eric Laurent83844cc2011-11-18 16:43:31 -080089 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
90 int index,
91 audio_devices_t device);
92 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
93 int *index,
94 audio_devices_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -070095
Dima Zavinfce7a472011-04-19 22:30:36 -070096 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Eric Laurent63742522012-03-08 13:42:42 -080097 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurentde070132010-07-13 04:45:46 -070098
Glenn Kasten58e5aa32012-06-20 14:08:14 -070099 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
100 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700101 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700102 uint32_t strategy,
103 int session,
104 int id);
105 virtual status_t unregisterEffect(int id);
Eric Laurentdb7c0792011-08-10 10:37:50 -0700106 virtual status_t setEffectEnabled(int id, bool enabled);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800107 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800108 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700109 virtual bool isSourceActive(audio_source_t source) const;
Eric Laurentde070132010-07-13 04:45:46 -0700110
Eric Laurent57dae992011-07-24 13:36:09 -0700111 virtual status_t queryDefaultPreProcessing(int audioSession,
112 effect_descriptor_t *descriptors,
113 uint32_t *count);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700114 virtual status_t onTransact(
115 uint32_t code,
116 const Parcel& data,
117 Parcel* reply,
118 uint32_t flags);
119
120 // IBinder::DeathRecipient
121 virtual void binderDied(const wp<IBinder>& who);
122
123 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700124 // Helpers for the struct audio_policy_service_ops implementation.
125 // This is used by the audio policy manager for certain operations that
126 // are implemented by the policy service.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700127 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700128 virtual void setParameters(audio_io_handle_t ioHandle,
129 const char *keyValuePairs,
130 int delayMs);
131
132 virtual status_t setStreamVolume(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700133 float volume,
134 audio_io_handle_t output,
135 int delayMs = 0);
Dima Zavinfce7a472011-04-19 22:30:36 -0700136 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700137 virtual status_t stopTone();
138 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700139
140private:
Mathias Agopiane762be92013-05-09 16:26:45 -0700141 AudioPolicyService() ANDROID_API;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700142 virtual ~AudioPolicyService();
143
144 status_t dumpInternals(int fd);
145
146 // Thread used for tone playback and to send audio config commands to audio flinger
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700147 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because
148 // startTone() and stopTone() are normally called with mLock locked and requesting a tone start
149 // or stop will cause calls to AudioPolicyService and an attempt to lock mLock.
150 // For audio config commands, it is necessary because audio flinger requires that the calling
151 // process (user) has permission to modify audio settings.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700152 class AudioCommandThread : public Thread {
153 class AudioCommand;
154 public:
155
156 // commands for tone AudioCommand
157 enum {
158 START_TONE,
159 STOP_TONE,
160 SET_VOLUME,
161 SET_PARAMETERS,
162 SET_VOICE_VOLUME
163 };
164
165 AudioCommandThread (String8 name);
166 virtual ~AudioCommandThread();
167
168 status_t dump(int fd);
169
170 // Thread virtuals
171 virtual void onFirstRef();
172 virtual bool threadLoop();
173
174 void exit();
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800175 void startToneCommand(ToneGenerator::tone_type type,
176 audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700177 void stopToneCommand();
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800178 status_t volumeCommand(audio_stream_type_t stream, float volume,
179 audio_io_handle_t output, int delayMs = 0);
180 status_t parametersCommand(audio_io_handle_t ioHandle,
181 const char *keyValuePairs, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700182 status_t voiceVolumeCommand(float volume, int delayMs = 0);
183 void insertCommand_l(AudioCommand *command, int delayMs = 0);
184
185 private:
186 // descriptor for requested tone playback event
187 class AudioCommand {
188
189 public:
190 AudioCommand()
191 : mCommand(-1) {}
192
193 void dump(char* buffer, size_t size);
194
195 int mCommand; // START_TONE, STOP_TONE ...
196 nsecs_t mTime; // time stamp
197 Condition mCond; // condition for status return
198 status_t mStatus; // command status
199 bool mWaitStatus; // true if caller is waiting for status
200 void *mParam; // command parameter (ToneData, VolumeData, ParametersData)
201 };
202
203 class ToneData {
204 public:
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800205 ToneGenerator::tone_type mType; // tone type (START_TONE only)
Glenn Kastenfff6d712012-01-12 16:38:12 -0800206 audio_stream_type_t mStream; // stream type (START_TONE only)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207 };
208
209 class VolumeData {
210 public:
Glenn Kastenfff6d712012-01-12 16:38:12 -0800211 audio_stream_type_t mStream;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212 float mVolume;
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800213 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700214 };
215
216 class ParametersData {
217 public:
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800218 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700219 String8 mKeyValuePairs;
220 };
221
222 class VoiceVolumeData {
223 public:
224 float mVolume;
225 };
226
227 Mutex mLock;
228 Condition mWaitWorkCV;
229 Vector <AudioCommand *> mAudioCommands; // list of pending commands
230 ToneGenerator *mpToneGenerator; // the tone generator
231 AudioCommand mLastCommand; // last processed command (used by dump)
232 String8 mName; // string used by wake lock fo delayed commands
233 };
234
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700235 class EffectDesc {
236 public:
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800237 EffectDesc(const char *name, const effect_uuid_t& uuid) :
238 mName(strdup(name)),
239 mUuid(uuid) { }
240 EffectDesc(const EffectDesc& orig) :
241 mName(strdup(orig.mName)),
242 mUuid(orig.mUuid) {
243 // deep copy mParams
244 for (size_t k = 0; k < orig.mParams.size(); k++) {
245 effect_param_t *origParam = orig.mParams[k];
246 // psize and vsize are rounded up to an int boundary for allocation
247 size_t origSize = sizeof(effect_param_t) +
248 ((origParam->psize + 3) & ~3) +
249 ((origParam->vsize + 3) & ~3);
250 effect_param_t *dupParam = (effect_param_t *) malloc(origSize);
251 memcpy(dupParam, origParam, origSize);
252 // This works because the param buffer allocation is also done by
253 // multiples of 4 bytes originally. In theory we should memcpy only
254 // the actual param size, that is without rounding vsize.
255 mParams.add(dupParam);
256 }
257 }
258 /*virtual*/ ~EffectDesc() {
259 free(mName);
260 for (size_t k = 0; k < mParams.size(); k++) {
261 free(mParams[k]);
262 }
263 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700264 char *mName;
265 effect_uuid_t mUuid;
266 Vector <effect_param_t *> mParams;
267 };
268
269 class InputSourceDesc {
270 public:
271 InputSourceDesc() {}
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800272 /*virtual*/ ~InputSourceDesc() {
273 for (size_t j = 0; j < mEffects.size(); j++) {
274 delete mEffects[j];
275 }
276 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700277 Vector <EffectDesc *> mEffects;
278 };
279
280
281 class InputDesc {
282 public:
Glenn Kasten81872a22012-03-07 16:49:22 -0800283 InputDesc(int session) : mSessionId(session) {}
284 /*virtual*/ ~InputDesc() {}
285 const int mSessionId;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700286 Vector< sp<AudioEffect> >mEffects;
287 };
288
Glenn Kasten8dad0e32012-01-09 08:41:22 -0800289 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700290
Glenn Kasten81872a22012-03-07 16:49:22 -0800291 void setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700292 status_t loadPreProcessorConfig(const char *path);
293 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
294 EffectDesc *loadEffect(cnode *root);
295 status_t loadInputSources(cnode *root, const Vector <EffectDesc *>& effects);
296 audio_source_t inputSourceNameToEnum(const char *name);
297 InputSourceDesc *loadInputSource(cnode *root, const Vector <EffectDesc *>& effects);
298 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
299 effect_param_t *loadEffectParameter(cnode *root);
300 size_t readParamValue(cnode *node,
301 char *param,
302 size_t *curSize,
303 size_t *totSize);
304 size_t growParamSize(char *param,
305 size_t size,
306 size_t *curSize,
307 size_t *totSize);
308
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309 // Internal dump utilities.
310 status_t dumpPermissionDenial(int fd);
311
312
Eric Laurenteda6c362011-02-02 09:33:30 -0800313 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
314 // device connection state or routing
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700315 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread
316 sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread
Dima Zavinfce7a472011-04-19 22:30:36 -0700317 struct audio_policy_device *mpAudioPolicyDev;
318 struct audio_policy *mpAudioPolicy;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700319 KeyedVector< audio_source_t, InputSourceDesc* > mInputSources;
320 KeyedVector< audio_io_handle_t, InputDesc* > mInputs;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700321};
322
323}; // namespace android
324
325#endif // ANDROID_AUDIOPOLICYSERVICE_H