blob: 92653c1b735c58e2152939c7be7534d00b760db4 [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>
Glenn Kastend1d8f232011-07-08 09:34:50 -070022#include <utils/String8.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070023#include <utils/Vector.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070024#include <utils/SortedVector.h>
Glenn Kastend2dcb082011-02-03 16:55:26 -080025#include <binder/BinderService.h>
Dima Zavin64760242011-05-11 14:15:23 -070026#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070027#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070028#include <hardware/audio_policy.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070029#include <media/IAudioPolicyService.h>
30#include <media/ToneGenerator.h>
31#include <media/AudioEffect.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070032
Mathias Agopian65ab4712010-07-14 17:59:35 -070033namespace android {
34
Mathias Agopian65ab4712010-07-14 17:59:35 -070035// ----------------------------------------------------------------------------
36
Glenn Kastend2dcb082011-02-03 16:55:26 -080037class AudioPolicyService :
38 public BinderService<AudioPolicyService>,
39 public BnAudioPolicyService,
Dima Zavinfce7a472011-04-19 22:30:36 -070040// public AudioPolicyClientInterface,
Eric Laurentde070132010-07-13 04:45:46 -070041 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070042{
Glenn Kastend2dcb082011-02-03 16:55:26 -080043 friend class BinderService<AudioPolicyService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070044
45public:
Glenn Kastend2dcb082011-02-03 16:55:26 -080046 // for BinderService
47 static const char *getServiceName() { return "media.audio_policy"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
49 virtual status_t dump(int fd, const Vector<String16>& args);
50
51 //
52 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
53 //
54
Dima Zavinfce7a472011-04-19 22:30:36 -070055 virtual status_t setDeviceConnectionState(audio_devices_t device,
56 audio_policy_dev_state_t state,
Mathias Agopian65ab4712010-07-14 17:59:35 -070057 const char *device_address);
Dima Zavinfce7a472011-04-19 22:30:36 -070058 virtual audio_policy_dev_state_t getDeviceConnectionState(
59 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -070060 const char *device_address);
Glenn Kastenf78aee72012-01-04 11:00:47 -080061 virtual status_t setPhoneState(audio_mode_t state);
Dima Zavinfce7a472011-04-19 22:30:36 -070062 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
63 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
64 virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -070065 uint32_t samplingRate = 0,
Glenn Kasten58f30212012-01-12 12:27:51 -080066 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten254af182012-07-03 14:59:05 -070067 audio_channel_mask_t channelMask = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -070068 audio_output_flags_t flags =
69 AUDIO_OUTPUT_FLAG_NONE);
Eric Laurentde070132010-07-13 04:45:46 -070070 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070071 audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -070072 int session = 0);
73 virtual status_t stopOutput(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);
Mathias Agopian65ab4712010-07-14 17:59:35 -070076 virtual void releaseOutput(audio_io_handle_t output);
Glenn Kasteneba51fb2012-01-23 13:58:49 -080077 virtual audio_io_handle_t getInput(audio_source_t inputSource,
Mathias Agopian65ab4712010-07-14 17:59:35 -070078 uint32_t samplingRate = 0,
Glenn Kasten58f30212012-01-12 12:27:51 -080079 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten254af182012-07-03 14:59:05 -070080 audio_channel_mask_t channelMask = 0,
Eric Laurent7c7f10b2011-06-17 21:29:58 -070081 int audioSession = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -070082 virtual status_t startInput(audio_io_handle_t input);
83 virtual status_t stopInput(audio_io_handle_t input);
84 virtual void releaseInput(audio_io_handle_t input);
Dima Zavinfce7a472011-04-19 22:30:36 -070085 virtual status_t initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -070086 int indexMin,
87 int indexMax);
Eric Laurent83844cc2011-11-18 16:43:31 -080088 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
89 int index,
90 audio_devices_t device);
91 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
92 int *index,
93 audio_devices_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -070094
Dima Zavinfce7a472011-04-19 22:30:36 -070095 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Eric Laurent63742522012-03-08 13:42:42 -080096 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurentde070132010-07-13 04:45:46 -070097
Glenn Kasten58e5aa32012-06-20 14:08:14 -070098 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
99 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700100 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700101 uint32_t strategy,
102 int session,
103 int id);
104 virtual status_t unregisterEffect(int id);
Eric Laurentdb7c0792011-08-10 10:37:50 -0700105 virtual status_t setEffectEnabled(int id, bool enabled);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800106 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700107 virtual bool isSourceActive(audio_source_t source) const;
Eric Laurentde070132010-07-13 04:45:46 -0700108
Eric Laurent57dae992011-07-24 13:36:09 -0700109 virtual status_t queryDefaultPreProcessing(int audioSession,
110 effect_descriptor_t *descriptors,
111 uint32_t *count);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700112 virtual status_t onTransact(
113 uint32_t code,
114 const Parcel& data,
115 Parcel* reply,
116 uint32_t flags);
117
118 // IBinder::DeathRecipient
119 virtual void binderDied(const wp<IBinder>& who);
120
121 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700122 // Helpers for the struct audio_policy_service_ops implementation.
123 // This is used by the audio policy manager for certain operations that
124 // are implemented by the policy service.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700125 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700126 virtual void setParameters(audio_io_handle_t ioHandle,
127 const char *keyValuePairs,
128 int delayMs);
129
130 virtual status_t setStreamVolume(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700131 float volume,
132 audio_io_handle_t output,
133 int delayMs = 0);
Dima Zavinfce7a472011-04-19 22:30:36 -0700134 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700135 virtual status_t stopTone();
136 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700137
138private:
139 AudioPolicyService();
140 virtual ~AudioPolicyService();
141
142 status_t dumpInternals(int fd);
143
144 // Thread used for tone playback and to send audio config commands to audio flinger
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700145 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because
146 // startTone() and stopTone() are normally called with mLock locked and requesting a tone start
147 // or stop will cause calls to AudioPolicyService and an attempt to lock mLock.
148 // For audio config commands, it is necessary because audio flinger requires that the calling
149 // process (user) has permission to modify audio settings.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700150 class AudioCommandThread : public Thread {
151 class AudioCommand;
152 public:
153
154 // commands for tone AudioCommand
155 enum {
156 START_TONE,
157 STOP_TONE,
158 SET_VOLUME,
159 SET_PARAMETERS,
160 SET_VOICE_VOLUME
161 };
162
163 AudioCommandThread (String8 name);
164 virtual ~AudioCommandThread();
165
166 status_t dump(int fd);
167
168 // Thread virtuals
169 virtual void onFirstRef();
170 virtual bool threadLoop();
171
172 void exit();
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800173 void startToneCommand(ToneGenerator::tone_type type,
174 audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700175 void stopToneCommand();
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800176 status_t volumeCommand(audio_stream_type_t stream, float volume,
177 audio_io_handle_t output, int delayMs = 0);
178 status_t parametersCommand(audio_io_handle_t ioHandle,
179 const char *keyValuePairs, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700180 status_t voiceVolumeCommand(float volume, int delayMs = 0);
181 void insertCommand_l(AudioCommand *command, int delayMs = 0);
182
183 private:
184 // descriptor for requested tone playback event
185 class AudioCommand {
186
187 public:
188 AudioCommand()
189 : mCommand(-1) {}
190
191 void dump(char* buffer, size_t size);
192
193 int mCommand; // START_TONE, STOP_TONE ...
194 nsecs_t mTime; // time stamp
195 Condition mCond; // condition for status return
196 status_t mStatus; // command status
197 bool mWaitStatus; // true if caller is waiting for status
198 void *mParam; // command parameter (ToneData, VolumeData, ParametersData)
199 };
200
201 class ToneData {
202 public:
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800203 ToneGenerator::tone_type mType; // tone type (START_TONE only)
Glenn Kastenfff6d712012-01-12 16:38:12 -0800204 audio_stream_type_t mStream; // stream type (START_TONE only)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 };
206
207 class VolumeData {
208 public:
Glenn Kastenfff6d712012-01-12 16:38:12 -0800209 audio_stream_type_t mStream;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700210 float mVolume;
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800211 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700212 };
213
214 class ParametersData {
215 public:
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800216 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217 String8 mKeyValuePairs;
218 };
219
220 class VoiceVolumeData {
221 public:
222 float mVolume;
223 };
224
225 Mutex mLock;
226 Condition mWaitWorkCV;
227 Vector <AudioCommand *> mAudioCommands; // list of pending commands
228 ToneGenerator *mpToneGenerator; // the tone generator
229 AudioCommand mLastCommand; // last processed command (used by dump)
230 String8 mName; // string used by wake lock fo delayed commands
231 };
232
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700233 class EffectDesc {
234 public:
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800235 EffectDesc(const char *name, const effect_uuid_t& uuid) :
236 mName(strdup(name)),
237 mUuid(uuid) { }
238 EffectDesc(const EffectDesc& orig) :
239 mName(strdup(orig.mName)),
240 mUuid(orig.mUuid) {
241 // deep copy mParams
242 for (size_t k = 0; k < orig.mParams.size(); k++) {
243 effect_param_t *origParam = orig.mParams[k];
244 // psize and vsize are rounded up to an int boundary for allocation
245 size_t origSize = sizeof(effect_param_t) +
246 ((origParam->psize + 3) & ~3) +
247 ((origParam->vsize + 3) & ~3);
248 effect_param_t *dupParam = (effect_param_t *) malloc(origSize);
249 memcpy(dupParam, origParam, origSize);
250 // This works because the param buffer allocation is also done by
251 // multiples of 4 bytes originally. In theory we should memcpy only
252 // the actual param size, that is without rounding vsize.
253 mParams.add(dupParam);
254 }
255 }
256 /*virtual*/ ~EffectDesc() {
257 free(mName);
258 for (size_t k = 0; k < mParams.size(); k++) {
259 free(mParams[k]);
260 }
261 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700262 char *mName;
263 effect_uuid_t mUuid;
264 Vector <effect_param_t *> mParams;
265 };
266
267 class InputSourceDesc {
268 public:
269 InputSourceDesc() {}
Glenn Kasten9fda4b82012-02-02 14:04:37 -0800270 /*virtual*/ ~InputSourceDesc() {
271 for (size_t j = 0; j < mEffects.size(); j++) {
272 delete mEffects[j];
273 }
274 }
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700275 Vector <EffectDesc *> mEffects;
276 };
277
278
279 class InputDesc {
280 public:
Glenn Kasten81872a22012-03-07 16:49:22 -0800281 InputDesc(int session) : mSessionId(session) {}
282 /*virtual*/ ~InputDesc() {}
283 const int mSessionId;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700284 Vector< sp<AudioEffect> >mEffects;
285 };
286
Glenn Kasten8dad0e32012-01-09 08:41:22 -0800287 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700288
Glenn Kasten81872a22012-03-07 16:49:22 -0800289 void setPreProcessorEnabled(const InputDesc *inputDesc, bool enabled);
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700290 status_t loadPreProcessorConfig(const char *path);
291 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
292 EffectDesc *loadEffect(cnode *root);
293 status_t loadInputSources(cnode *root, const Vector <EffectDesc *>& effects);
294 audio_source_t inputSourceNameToEnum(const char *name);
295 InputSourceDesc *loadInputSource(cnode *root, const Vector <EffectDesc *>& effects);
296 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
297 effect_param_t *loadEffectParameter(cnode *root);
298 size_t readParamValue(cnode *node,
299 char *param,
300 size_t *curSize,
301 size_t *totSize);
302 size_t growParamSize(char *param,
303 size_t size,
304 size_t *curSize,
305 size_t *totSize);
306
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307 // Internal dump utilities.
308 status_t dumpPermissionDenial(int fd);
309
310
Eric Laurenteda6c362011-02-02 09:33:30 -0800311 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
312 // device connection state or routing
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700313 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread
314 sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread
Dima Zavinfce7a472011-04-19 22:30:36 -0700315 struct audio_policy_device *mpAudioPolicyDev;
316 struct audio_policy *mpAudioPolicy;
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700317 KeyedVector< audio_source_t, InputSourceDesc* > mInputSources;
318 KeyedVector< audio_io_handle_t, InputDesc* > mInputs;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700319};
320
321}; // namespace android
322
323#endif // ANDROID_AUDIOPOLICYSERVICE_H