blob: d898a530cc8c2b51a58b1cfe8de58c211456af00 [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>
Eric Laurent327c27b2009-08-27 00:48:47 -070022#include <utils/Vector.h>
Eric Laurent464d5b32011-06-17 21:29:58 -070023#include <utils/SortedVector.h>
Glenn Kasten3515a332011-02-03 16:55:26 -080024#include <binder/BinderService.h>
Dima Zavin34bb4192011-05-11 14:15:23 -070025#include <system/audio.h>
Dima Zavin290029d2011-06-13 18:16:26 -070026#include <system/audio_policy.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070027#include <hardware/audio_policy.h>
Eric Laurent464d5b32011-06-17 21:29:58 -070028#include <media/IAudioPolicyService.h>
29#include <media/ToneGenerator.h>
30#include <media/AudioEffect.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070031
Eric Laurenta553c252009-07-17 12:17:14 -070032namespace android {
33
34class String8;
35
36// ----------------------------------------------------------------------------
37
Glenn Kasten3515a332011-02-03 16:55:26 -080038class AudioPolicyService :
39 public BinderService<AudioPolicyService>,
40 public BnAudioPolicyService,
Dima Zavin24fc2fb2011-04-19 22:30:36 -070041// public AudioPolicyClientInterface,
Eric Laurent8ed6ed02010-07-13 04:45:46 -070042 public IBinder::DeathRecipient
Eric Laurenta553c252009-07-17 12:17:14 -070043{
Glenn Kasten3515a332011-02-03 16:55:26 -080044 friend class BinderService<AudioPolicyService>;
Eric Laurenta553c252009-07-17 12:17:14 -070045
46public:
Glenn Kasten3515a332011-02-03 16:55:26 -080047 // for BinderService
48 static const char *getServiceName() { return "media.audio_policy"; }
Eric Laurenta553c252009-07-17 12:17:14 -070049
50 virtual status_t dump(int fd, const Vector<String16>& args);
51
52 //
53 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
54 //
55
Dima Zavin24fc2fb2011-04-19 22:30:36 -070056 virtual status_t setDeviceConnectionState(audio_devices_t device,
57 audio_policy_dev_state_t state,
Eric Laurenta553c252009-07-17 12:17:14 -070058 const char *device_address);
Dima Zavin24fc2fb2011-04-19 22:30:36 -070059 virtual audio_policy_dev_state_t getDeviceConnectionState(
60 audio_devices_t device,
Eric Laurent8ed6ed02010-07-13 04:45:46 -070061 const char *device_address);
Eric Laurenta553c252009-07-17 12:17:14 -070062 virtual status_t setPhoneState(int state);
63 virtual status_t setRingerMode(uint32_t mode, uint32_t mask);
Dima Zavin24fc2fb2011-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,
Eric Laurenta553c252009-07-17 12:17:14 -070067 uint32_t samplingRate = 0,
Dima Zavin24fc2fb2011-04-19 22:30:36 -070068 uint32_t format = AUDIO_FORMAT_DEFAULT,
Eric Laurenta553c252009-07-17 12:17:14 -070069 uint32_t channels = 0,
Dima Zavin24fc2fb2011-04-19 22:30:36 -070070 audio_policy_output_flags_t flags =
71 AUDIO_POLICY_OUTPUT_FLAG_INDIRECT);
Eric Laurent8ed6ed02010-07-13 04:45:46 -070072 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavin24fc2fb2011-04-19 22:30:36 -070073 audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -070074 int session = 0);
75 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavin24fc2fb2011-04-19 22:30:36 -070076 audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -070077 int session = 0);
Eric Laurenta553c252009-07-17 12:17:14 -070078 virtual void releaseOutput(audio_io_handle_t output);
79 virtual audio_io_handle_t getInput(int inputSource,
80 uint32_t samplingRate = 0,
Dima Zavin24fc2fb2011-04-19 22:30:36 -070081 uint32_t format = AUDIO_FORMAT_DEFAULT,
Eric Laurenta553c252009-07-17 12:17:14 -070082 uint32_t channels = 0,
Dima Zavin24fc2fb2011-04-19 22:30:36 -070083 audio_in_acoustics_t acoustics =
Eric Laurent464d5b32011-06-17 21:29:58 -070084 (audio_in_acoustics_t)0,
85 int audioSession = 0);
Eric Laurenta553c252009-07-17 12:17:14 -070086 virtual status_t startInput(audio_io_handle_t input);
87 virtual status_t stopInput(audio_io_handle_t input);
88 virtual void releaseInput(audio_io_handle_t input);
Dima Zavin24fc2fb2011-04-19 22:30:36 -070089 virtual status_t initStreamVolume(audio_stream_type_t stream,
Eric Laurenta553c252009-07-17 12:17:14 -070090 int indexMin,
91 int indexMax);
Dima Zavin24fc2fb2011-04-19 22:30:36 -070092 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, int index);
93 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, int *index);
Eric Laurenta553c252009-07-17 12:17:14 -070094
Dima Zavin24fc2fb2011-04-19 22:30:36 -070095 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
96 virtual uint32_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurent8ed6ed02010-07-13 04:45:46 -070097
98 virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc);
99 virtual status_t registerEffect(effect_descriptor_t *desc,
Eric Laurent464d5b32011-06-17 21:29:58 -0700100 audio_io_handle_t io,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700101 uint32_t strategy,
102 int session,
103 int id);
104 virtual status_t unregisterEffect(int id);
Eric Laurent6752ec82011-08-10 10:37:50 -0700105 virtual status_t setEffectEnabled(int id, bool enabled);
Eric Laurent25101b02011-02-02 09:33:30 -0800106 virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const;
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700107
Eric Laurent0f7f4ec2011-07-24 13:36:09 -0700108 virtual status_t queryDefaultPreProcessing(int audioSession,
109 effect_descriptor_t *descriptors,
110 uint32_t *count);
Eric Laurenta553c252009-07-17 12:17:14 -0700111 virtual status_t onTransact(
112 uint32_t code,
113 const Parcel& data,
114 Parcel* reply,
115 uint32_t flags);
116
117 // IBinder::DeathRecipient
118 virtual void binderDied(const wp<IBinder>& who);
119
120 //
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700121 // Helpers for the struct audio_policy_service_ops implementation.
122 // This is used by the audio policy manager for certain operations that
123 // are implemented by the policy service.
Eric Laurenta553c252009-07-17 12:17:14 -0700124 //
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700125 virtual void setParameters(audio_io_handle_t ioHandle,
126 const char *keyValuePairs,
127 int delayMs);
128
129 virtual status_t setStreamVolume(audio_stream_type_t stream,
Eric Laurent8ed6ed02010-07-13 04:45:46 -0700130 float volume,
131 audio_io_handle_t output,
132 int delayMs = 0);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700133 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
Eric Laurenta553c252009-07-17 12:17:14 -0700134 virtual status_t stopTone();
Eric Laurent415f3e22009-10-21 08:14:22 -0700135 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700136
137private:
138 AudioPolicyService();
139 virtual ~AudioPolicyService();
140
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800141 status_t dumpInternals(int fd);
142
Eric Laurenta553c252009-07-17 12:17:14 -0700143 // Thread used for tone playback and to send audio config commands to audio flinger
144 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because startTone()
145 // and stopTone() are normally called with mLock locked and requesting a tone start or stop will cause
146 // calls to AudioPolicyService and an attempt to lock mLock.
147 // For audio config commands, it is necessary because audio flinger requires that the calling process (user)
148 // has permission to modify audio settings.
149 class AudioCommandThread : public Thread {
Eric Laurent327c27b2009-08-27 00:48:47 -0700150 class AudioCommand;
Eric Laurenta553c252009-07-17 12:17:14 -0700151 public:
152
153 // commands for tone AudioCommand
154 enum {
155 START_TONE,
156 STOP_TONE,
157 SET_VOLUME,
Eric Laurent415f3e22009-10-21 08:14:22 -0700158 SET_PARAMETERS,
159 SET_VOICE_VOLUME
Eric Laurenta553c252009-07-17 12:17:14 -0700160 };
161
Eric Laurentcef3cd72009-12-10 01:03:50 -0800162 AudioCommandThread (String8 name);
Eric Laurenta553c252009-07-17 12:17:14 -0700163 virtual ~AudioCommandThread();
164
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800165 status_t dump(int fd);
166
Eric Laurenta553c252009-07-17 12:17:14 -0700167 // Thread virtuals
168 virtual void onFirstRef();
169 virtual bool threadLoop();
170
171 void exit();
172 void startToneCommand(int type = 0, int stream = 0);
173 void stopToneCommand();
Eric Laurent327c27b2009-08-27 00:48:47 -0700174 status_t volumeCommand(int stream, float volume, int output, int delayMs = 0);
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700175 status_t parametersCommand(int ioHandle, const char *keyValuePairs, int delayMs = 0);
Eric Laurent415f3e22009-10-21 08:14:22 -0700176 status_t voiceVolumeCommand(float volume, int delayMs = 0);
Eric Laurent327c27b2009-08-27 00:48:47 -0700177 void insertCommand_l(AudioCommand *command, int delayMs = 0);
Eric Laurenta553c252009-07-17 12:17:14 -0700178
179 private:
180 // descriptor for requested tone playback event
181 class AudioCommand {
Eric Laurent3fdb1262009-11-07 00:01:32 -0800182
Eric Laurenta553c252009-07-17 12:17:14 -0700183 public:
Eric Laurent3fdb1262009-11-07 00:01:32 -0800184 AudioCommand()
185 : mCommand(-1) {}
186
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800187 void dump(char* buffer, size_t size);
188
Eric Laurenta553c252009-07-17 12:17:14 -0700189 int mCommand; // START_TONE, STOP_TONE ...
Eric Laurent327c27b2009-08-27 00:48:47 -0700190 nsecs_t mTime; // time stamp
191 Condition mCond; // condition for status return
192 status_t mStatus; // command status
193 bool mWaitStatus; // true if caller is waiting for status
194 void *mParam; // command parameter (ToneData, VolumeData, ParametersData)
Eric Laurenta553c252009-07-17 12:17:14 -0700195 };
196
197 class ToneData {
198 public:
199 int mType; // tone type (START_TONE only)
200 int mStream; // stream type (START_TONE only)
201 };
202
203 class VolumeData {
204 public:
205 int mStream;
206 float mVolume;
Eric Laurentddb78e72009-07-28 08:44:33 -0700207 int mIO;
Eric Laurenta553c252009-07-17 12:17:14 -0700208 };
Eric Laurent415f3e22009-10-21 08:14:22 -0700209
Eric Laurenta553c252009-07-17 12:17:14 -0700210 class ParametersData {
211 public:
Eric Laurentddb78e72009-07-28 08:44:33 -0700212 int mIO;
Eric Laurenta553c252009-07-17 12:17:14 -0700213 String8 mKeyValuePairs;
214 };
215
Eric Laurent415f3e22009-10-21 08:14:22 -0700216 class VoiceVolumeData {
217 public:
218 float mVolume;
219 };
Eric Laurenta553c252009-07-17 12:17:14 -0700220
221 Mutex mLock;
222 Condition mWaitWorkCV;
Eric Laurent327c27b2009-08-27 00:48:47 -0700223 Vector <AudioCommand *> mAudioCommands; // list of pending commands
Eric Laurenta553c252009-07-17 12:17:14 -0700224 ToneGenerator *mpToneGenerator; // the tone generator
Eric Laurentcef3cd72009-12-10 01:03:50 -0800225 AudioCommand mLastCommand; // last processed command (used by dump)
226 String8 mName; // string used by wake lock fo delayed commands
Eric Laurenta553c252009-07-17 12:17:14 -0700227 };
228
Eric Laurent464d5b32011-06-17 21:29:58 -0700229 class EffectDesc {
230 public:
231 EffectDesc() {}
232 virtual ~EffectDesc() {}
233 char *mName;
234 effect_uuid_t mUuid;
235 Vector <effect_param_t *> mParams;
236 };
237
238 class InputSourceDesc {
239 public:
240 InputSourceDesc() {}
241 virtual ~InputSourceDesc() {}
242 Vector <EffectDesc *> mEffects;
243 };
244
245
246 class InputDesc {
247 public:
248 InputDesc() {}
249 virtual ~InputDesc() {}
250 int mSessionId;
251 Vector< sp<AudioEffect> >mEffects;
252 };
253
254 static const char *kInputSourceNames[AUDIO_SOURCE_CNT -1];
255
256 void setPreProcessorEnabled(InputDesc *inputDesc, bool enabled);
257 status_t loadPreProcessorConfig(const char *path);
258 status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects);
259 EffectDesc *loadEffect(cnode *root);
260 status_t loadInputSources(cnode *root, const Vector <EffectDesc *>& effects);
261 audio_source_t inputSourceNameToEnum(const char *name);
262 InputSourceDesc *loadInputSource(cnode *root, const Vector <EffectDesc *>& effects);
263 void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params);
264 effect_param_t *loadEffectParameter(cnode *root);
265 size_t readParamValue(cnode *node,
266 char *param,
267 size_t *curSize,
268 size_t *totSize);
269 size_t growParamSize(char *param,
270 size_t size,
271 size_t *curSize,
272 size_t *totSize);
273
Eric Laurenta553c252009-07-17 12:17:14 -0700274 // Internal dump utilities.
Eric Laurentf4ee40e2009-11-02 10:29:02 -0800275 status_t dumpPermissionDenial(int fd);
Eric Laurenta553c252009-07-17 12:17:14 -0700276
277
Eric Laurent25101b02011-02-02 09:33:30 -0800278 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
279 // device connection state or routing
Eric Laurenta553c252009-07-17 12:17:14 -0700280 sp <AudioCommandThread> mAudioCommandThread; // audio commands thread
Eric Laurent327c27b2009-08-27 00:48:47 -0700281 sp <AudioCommandThread> mTonePlaybackThread; // tone playback thread
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700282 struct audio_policy_device *mpAudioPolicyDev;
283 struct audio_policy *mpAudioPolicy;
Eric Laurent464d5b32011-06-17 21:29:58 -0700284 KeyedVector< audio_source_t, InputSourceDesc* > mInputSources;
285 KeyedVector< audio_io_handle_t, InputDesc* > mInputs;
Eric Laurenta553c252009-07-17 12:17:14 -0700286};
287
288}; // namespace android
289
290#endif // ANDROID_AUDIOPOLICYSERVICE_H