blob: 65ff6ef4262cd669b44c98f2bcf695f0e16ac7fe [file] [log] [blame]
Eric Laurenta553c252009-07-17 12:17:14 -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 Laurent464d5b32011-06-17 21:29:58 -070020#include <cutils/misc.h>
21#include <cutils/config_utils.h>
Glenn Kasten778170d2011-07-08 09:34:50 -070022#include <utils/String8.h>
Eric Laurent327c27b2009-08-27 00:48:47 -070023#include <utils/Vector.h>
Eric Laurent464d5b32011-06-17 21:29:58 -070024#include <utils/SortedVector.h>
Glenn Kasten3515a332011-02-03 16:55:26 -080025#include <binder/BinderService.h>
Dima Zavin34bb4192011-05-11 14:15:23 -070026#include <system/audio.h>
Dima Zavin290029d2011-06-13 18:16:26 -070027#include <system/audio_policy.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070028#include <hardware/audio_policy.h>
Eric Laurent464d5b32011-06-17 21:29:58 -070029#include <media/IAudioPolicyService.h>
30#include <media/ToneGenerator.h>
31#include <media/AudioEffect.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070032
Eric Laurenta553c252009-07-17 12:17:14 -070033namespace android {
34
Eric Laurenta553c252009-07-17 12:17:14 -070035// ----------------------------------------------------------------------------
36
Glenn Kasten3515a332011-02-03 16:55:26 -080037class AudioPolicyService :
38 public BinderService<AudioPolicyService>,
39 public BnAudioPolicyService,
Dima Zavin24fc2fb2011-04-19 22:30:36 -070040// public AudioPolicyClientInterface,
Eric Laurent8ed6ed02010-07-13 04:45:46 -070041 public IBinder::DeathRecipient
Eric Laurenta553c252009-07-17 12:17:14 -070042{
Glenn Kasten3515a332011-02-03 16:55:26 -080043 friend class BinderService<AudioPolicyService>;
Eric Laurenta553c252009-07-17 12:17:14 -070044
45public:
Glenn Kasten3515a332011-02-03 16:55:26 -080046 // for BinderService
47 static const char *getServiceName() { return "media.audio_policy"; }
Eric Laurenta553c252009-07-17 12:17:14 -070048
49 virtual status_t dump(int fd, const Vector<String16>& args);
50
51 //
52 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
53 //
54
Dima Zavin24fc2fb2011-04-19 22:30:36 -070055 virtual status_t setDeviceConnectionState(audio_devices_t device,
56 audio_policy_dev_state_t state,
Eric Laurenta553c252009-07-17 12:17:14 -070057 const char *device_address);
Dima Zavin24fc2fb2011-04-19 22:30:36 -070058 virtual audio_policy_dev_state_t getDeviceConnectionState(
59 audio_devices_t device,
Eric Laurent8ed6ed02010-07-13 04:45:46 -070060 const char *device_address);
Glenn Kastenaccb1142012-01-04 11:00:47 -080061 virtual status_t setPhoneState(audio_mode_t state);
Dima Zavin24fc2fb2011-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,
Eric Laurenta553c252009-07-17 12:17:14 -070065 uint32_t samplingRate = 0,
Glenn Kasten0a204ed2012-01-12 12:27:51 -080066 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Eric Laurenta553c252009-07-17 12:17:14 -070067 uint32_t channels = 0,
Dima Zavin24fc2fb2011-04-19 22:30:36 -070068 audio_policy_output_flags_t flags =
69 AUDIO_POLICY_OUTPUT_FLAG_INDIRECT);
Eric Laurent8ed6ed02010-07-13 04:45:46 -070070 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavin24fc2fb2011-04-19 22:30:36 -070071 audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -070072 int session = 0);
73 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavin24fc2fb2011-04-19 22:30:36 -070074 audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -070075 int session = 0);
Eric Laurenta553c252009-07-17 12:17:14 -070076 virtual void releaseOutput(audio_io_handle_t output);
77 virtual audio_io_handle_t getInput(int inputSource,
78 uint32_t samplingRate = 0,
Glenn Kasten0a204ed2012-01-12 12:27:51 -080079 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Eric Laurenta553c252009-07-17 12:17:14 -070080 uint32_t channels = 0,
Dima Zavin24fc2fb2011-04-19 22:30:36 -070081 audio_in_acoustics_t acoustics =
Eric Laurent464d5b32011-06-17 21:29:58 -070082 (audio_in_acoustics_t)0,
83 int audioSession = 0);
Eric Laurenta553c252009-07-17 12:17:14 -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 Zavin24fc2fb2011-04-19 22:30:36 -070087 virtual status_t initStreamVolume(audio_stream_type_t stream,
Eric Laurenta553c252009-07-17 12:17:14 -070088 int indexMin,
89 int indexMax);
Eric Laurent9bc8358d2011-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);
Eric Laurenta553c252009-07-17 12:17:14 -070096
Dima Zavin24fc2fb2011-04-19 22:30:36 -070097 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
98 virtual uint32_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurent8ed6ed02010-07-13 04:45:46 -070099
100 virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc);
101 virtual status_t registerEffect(effect_descriptor_t *desc,
Eric Laurent464d5b32011-06-17 21:29:58 -0700102 audio_io_handle_t io,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700103 uint32_t strategy,
104 int session,
105 int id);
106 virtual status_t unregisterEffect(int id);
Eric Laurent6752ec82011-08-10 10:37:50 -0700107 virtual status_t setEffectEnabled(int id, bool enabled);
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800108 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700109
Eric Laurent0f7f4ec2011-07-24 13:36:09 -0700110 virtual status_t queryDefaultPreProcessing(int audioSession,
111 effect_descriptor_t *descriptors,
112 uint32_t *count);
Eric Laurenta553c252009-07-17 12:17:14 -0700113 virtual status_t onTransact(
114 uint32_t code,
115 const Parcel& data,
116 Parcel* reply,
117 uint32_t flags);
118
119 // IBinder::DeathRecipient
120 virtual void binderDied(const wp<IBinder>& who);
121
122 //
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700123 // Helpers for the struct audio_policy_service_ops implementation.
124 // This is used by the audio policy manager for certain operations that
125 // are implemented by the policy service.
Eric Laurenta553c252009-07-17 12:17:14 -0700126 //
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700127 virtual void setParameters(audio_io_handle_t ioHandle,
128 const char *keyValuePairs,
129 int delayMs);
130
131 virtual status_t setStreamVolume(audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700132 float volume,
133 audio_io_handle_t output,
134 int delayMs = 0);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700135 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
Eric Laurenta553c252009-07-17 12:17:14 -0700136 virtual status_t stopTone();
Eric Laurent415f3e22009-10-21 08:14:22 -0700137 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700138
139private:
140 AudioPolicyService();
141 virtual ~AudioPolicyService();
142
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800143 status_t dumpInternals(int fd);
144
Eric Laurenta553c252009-07-17 12:17:14 -0700145 // Thread used for tone playback and to send audio config commands to audio flinger
146 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because startTone()
147 // and stopTone() are normally called with mLock locked and requesting a tone start or stop will cause
148 // calls to AudioPolicyService and an attempt to lock mLock.
149 // For audio config commands, it is necessary because audio flinger requires that the calling process (user)
150 // has permission to modify audio settings.
151 class AudioCommandThread : public Thread {
Eric Laurent327c27b2009-08-27 00:48:47 -0700152 class AudioCommand;
Eric Laurenta553c252009-07-17 12:17:14 -0700153 public:
154
155 // commands for tone AudioCommand
156 enum {
157 START_TONE,
158 STOP_TONE,
159 SET_VOLUME,
Eric Laurent415f3e22009-10-21 08:14:22 -0700160 SET_PARAMETERS,
161 SET_VOICE_VOLUME
Eric Laurenta553c252009-07-17 12:17:14 -0700162 };
163
Eric Laurentcef3cd72009-12-10 01:03:50 -0800164 AudioCommandThread (String8 name);
Eric Laurenta553c252009-07-17 12:17:14 -0700165 virtual ~AudioCommandThread();
166
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800167 status_t dump(int fd);
168
Eric Laurenta553c252009-07-17 12:17:14 -0700169 // Thread virtuals
170 virtual void onFirstRef();
171 virtual bool threadLoop();
172
173 void exit();
Glenn Kasten23f7ad32012-01-27 15:25:25 -0800174 void startToneCommand(ToneGenerator::tone_type type,
175 audio_stream_type_t stream);
Eric Laurenta553c252009-07-17 12:17:14 -0700176 void stopToneCommand();
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800177 status_t volumeCommand(audio_stream_type_t stream, float volume, int output, int delayMs = 0);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700178 status_t parametersCommand(int ioHandle, const char *keyValuePairs, int delayMs = 0);
Eric Laurent415f3e22009-10-21 08:14:22 -0700179 status_t voiceVolumeCommand(float volume, int delayMs = 0);
Eric Laurent327c27b2009-08-27 00:48:47 -0700180 void insertCommand_l(AudioCommand *command, int delayMs = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700181
182 private:
183 // descriptor for requested tone playback event
184 class AudioCommand {
Eric Laurent3fdb1262009-11-07 00:01:32 -0800185
Eric Laurenta553c252009-07-17 12:17:14 -0700186 public:
Eric Laurent3fdb1262009-11-07 00:01:32 -0800187 AudioCommand()
188 : mCommand(-1) {}
189
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800190 void dump(char* buffer, size_t size);
191
Eric Laurenta553c252009-07-17 12:17:14 -0700192 int mCommand; // START_TONE, STOP_TONE ...
Eric Laurent327c27b2009-08-27 00:48:47 -0700193 nsecs_t mTime; // time stamp
194 Condition mCond; // condition for status return
195 status_t mStatus; // command status
196 bool mWaitStatus; // true if caller is waiting for status
197 void *mParam; // command parameter (ToneData, VolumeData, ParametersData)
Eric Laurenta553c252009-07-17 12:17:14 -0700198 };
199
200 class ToneData {
201 public:
Glenn Kasten23f7ad32012-01-27 15:25:25 -0800202 ToneGenerator::tone_type mType; // tone type (START_TONE only)
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800203 audio_stream_type_t mStream; // stream type (START_TONE only)
Eric Laurenta553c252009-07-17 12:17:14 -0700204 };
205
206 class VolumeData {
207 public:
Glenn Kastenbc1d77b2012-01-12 16:38:12 -0800208 audio_stream_type_t mStream;
Eric Laurenta553c252009-07-17 12:17:14 -0700209 float mVolume;
Eric Laurentddb78e72009-07-28 08:44:33 -0700210 int mIO;
Eric Laurenta553c252009-07-17 12:17:14 -0700211 };
Eric Laurent415f3e22009-10-21 08:14:22 -0700212
Eric Laurenta553c252009-07-17 12:17:14 -0700213 class ParametersData {
214 public:
Eric Laurentddb78e72009-07-28 08:44:33 -0700215 int mIO;
Eric Laurenta553c252009-07-17 12:17:14 -0700216 String8 mKeyValuePairs;
217 };
218
Eric Laurent415f3e22009-10-21 08:14:22 -0700219 class VoiceVolumeData {
220 public:
221 float mVolume;
222 };
Eric Laurenta553c252009-07-17 12:17:14 -0700223
224 Mutex mLock;
225 Condition mWaitWorkCV;
Eric Laurent327c27b2009-08-27 00:48:47 -0700226 Vector <AudioCommand *> mAudioCommands; // list of pending commands
Eric Laurenta553c252009-07-17 12:17:14 -0700227 ToneGenerator *mpToneGenerator; // the tone generator
Eric Laurentcef3cd72009-12-10 01:03:50 -0800228 AudioCommand mLastCommand; // last processed command (used by dump)
229 String8 mName; // string used by wake lock fo delayed commands
Eric Laurenta553c252009-07-17 12:17:14 -0700230 };
231
Eric Laurent464d5b32011-06-17 21:29:58 -0700232 class EffectDesc {
233 public:
234 EffectDesc() {}
235 virtual ~EffectDesc() {}
236 char *mName;
237 effect_uuid_t mUuid;
238 Vector <effect_param_t *> mParams;
239 };
240
241 class InputSourceDesc {
242 public:
243 InputSourceDesc() {}
244 virtual ~InputSourceDesc() {}
245 Vector <EffectDesc *> mEffects;
246 };
247
248
249 class InputDesc {
250 public:
251 InputDesc() {}
252 virtual ~InputDesc() {}
253 int mSessionId;
254 Vector< sp<AudioEffect> >mEffects;
255 };
256
Glenn Kasten05d33102012-01-09 08:41:22 -0800257 static const char * const kInputSourceNames[AUDIO_SOURCE_CNT -1];
Eric Laurent464d5b32011-06-17 21:29:58 -0700258
259 void setPreProcessorEnabled(InputDesc *inputDesc, bool enabled);
260 status_t loadPreProcessorConfig(const char *path);
261 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
262 EffectDesc *loadEffect(cnode *root);
263 status_t loadInputSources(cnode *root, const Vector <EffectDesc *>& effects);
264 audio_source_t inputSourceNameToEnum(const char *name);
265 InputSourceDesc *loadInputSource(cnode *root, const Vector <EffectDesc *>& effects);
266 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
267 effect_param_t *loadEffectParameter(cnode *root);
268 size_t readParamValue(cnode *node,
269 char *param,
270 size_t *curSize,
271 size_t *totSize);
272 size_t growParamSize(char *param,
273 size_t size,
274 size_t *curSize,
275 size_t *totSize);
276
Eric Laurenta553c252009-07-17 12:17:14 -0700277 // Internal dump utilities.
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800278 status_t dumpPermissionDenial(int fd);
Eric Laurenta553c252009-07-17 12:17:14 -0700279
280
Eric Laurent25101b02011-02-02 09:33:30 -0800281 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
282 // device connection state or routing
Eric Laurenta553c252009-07-17 12:17:14 -0700283 sp <AudioCommandThread> mAudioCommandThread; // audio commands thread
Eric Laurent327c27b2009-08-27 00:48:47 -0700284 sp <AudioCommandThread> mTonePlaybackThread; // tone playback thread
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700285 struct audio_policy_device *mpAudioPolicyDev;
286 struct audio_policy *mpAudioPolicy;
Eric Laurent464d5b32011-06-17 21:29:58 -0700287 KeyedVector< audio_source_t, InputSourceDesc* > mInputSources;
288 KeyedVector< audio_io_handle_t, InputDesc* > mInputs;
Eric Laurenta553c252009-07-17 12:17:14 -0700289};
290
291}; // namespace android
292
293#endif // ANDROID_AUDIOPOLICYSERVICE_H