Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
| 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 Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 20 | #include <cutils/misc.h> |
| 21 | #include <cutils/config_utils.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 22 | #include <utils/Vector.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 23 | #include <utils/SortedVector.h> |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 24 | #include <binder/BinderService.h> |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 25 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 26 | #include <system/audio_policy.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 27 | #include <hardware/audio_policy.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 28 | #include <media/IAudioPolicyService.h> |
| 29 | #include <media/ToneGenerator.h> |
| 30 | #include <media/AudioEffect.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 31 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 32 | namespace android { |
| 33 | |
| 34 | class String8; |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 38 | class AudioPolicyService : |
| 39 | public BinderService<AudioPolicyService>, |
| 40 | public BnAudioPolicyService, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 41 | // public AudioPolicyClientInterface, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 42 | public IBinder::DeathRecipient |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 43 | { |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 44 | friend class BinderService<AudioPolicyService>; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 45 | |
| 46 | public: |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 47 | // for BinderService |
| 48 | static const char *getServiceName() { return "media.audio_policy"; } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 49 | |
| 50 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 51 | |
| 52 | // |
| 53 | // BnAudioPolicyService (see AudioPolicyInterface for method descriptions) |
| 54 | // |
| 55 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 56 | virtual status_t setDeviceConnectionState(audio_devices_t device, |
| 57 | audio_policy_dev_state_t state, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 58 | const char *device_address); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 59 | virtual audio_policy_dev_state_t getDeviceConnectionState( |
| 60 | audio_devices_t device, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 61 | const char *device_address); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | virtual status_t setPhoneState(int state); |
| 63 | virtual status_t setRingerMode(uint32_t mode, uint32_t mask); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 64 | 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, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 67 | uint32_t samplingRate = 0, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 68 | uint32_t format = AUDIO_FORMAT_DEFAULT, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 69 | uint32_t channels = 0, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 70 | audio_policy_output_flags_t flags = |
| 71 | AUDIO_POLICY_OUTPUT_FLAG_INDIRECT); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 72 | virtual status_t startOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 73 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 74 | int session = 0); |
| 75 | virtual status_t stopOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 76 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 77 | int session = 0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 78 | virtual void releaseOutput(audio_io_handle_t output); |
| 79 | virtual audio_io_handle_t getInput(int inputSource, |
| 80 | uint32_t samplingRate = 0, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 81 | uint32_t format = AUDIO_FORMAT_DEFAULT, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 82 | uint32_t channels = 0, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 83 | audio_in_acoustics_t acoustics = |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 84 | (audio_in_acoustics_t)0, |
| 85 | int audioSession = 0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 86 | 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 Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 89 | virtual status_t initStreamVolume(audio_stream_type_t stream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 90 | int indexMin, |
| 91 | int indexMax); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 92 | virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, int index); |
| 93 | virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, int *index); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 94 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 95 | virtual uint32_t getStrategyForStream(audio_stream_type_t stream); |
| 96 | virtual uint32_t getDevicesForStream(audio_stream_type_t stream); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 97 | |
| 98 | virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc); |
| 99 | virtual status_t registerEffect(effect_descriptor_t *desc, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 100 | audio_io_handle_t io, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 101 | uint32_t strategy, |
| 102 | int session, |
| 103 | int id); |
| 104 | virtual status_t unregisterEffect(int id); |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 105 | virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 106 | |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame^] | 107 | virtual status_t queryDefaultPreProcessing(int audioSession, |
| 108 | effect_descriptor_t *descriptors, |
| 109 | uint32_t *count); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 110 | virtual status_t onTransact( |
| 111 | uint32_t code, |
| 112 | const Parcel& data, |
| 113 | Parcel* reply, |
| 114 | uint32_t flags); |
| 115 | |
| 116 | // IBinder::DeathRecipient |
| 117 | virtual void binderDied(const wp<IBinder>& who); |
| 118 | |
| 119 | // |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 120 | // Helpers for the struct audio_policy_service_ops implementation. |
| 121 | // This is used by the audio policy manager for certain operations that |
| 122 | // are implemented by the policy service. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 123 | // |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 124 | virtual void setParameters(audio_io_handle_t ioHandle, |
| 125 | const char *keyValuePairs, |
| 126 | int delayMs); |
| 127 | |
| 128 | virtual status_t setStreamVolume(audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 129 | float volume, |
| 130 | audio_io_handle_t output, |
| 131 | int delayMs = 0); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 132 | virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 133 | virtual status_t stopTone(); |
| 134 | virtual status_t setVoiceVolume(float volume, int delayMs = 0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 135 | |
| 136 | private: |
| 137 | AudioPolicyService(); |
| 138 | virtual ~AudioPolicyService(); |
| 139 | |
| 140 | status_t dumpInternals(int fd); |
| 141 | |
| 142 | // Thread used for tone playback and to send audio config commands to audio flinger |
| 143 | // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because startTone() |
| 144 | // and stopTone() are normally called with mLock locked and requesting a tone start or stop will cause |
| 145 | // calls to AudioPolicyService and an attempt to lock mLock. |
| 146 | // For audio config commands, it is necessary because audio flinger requires that the calling process (user) |
| 147 | // has permission to modify audio settings. |
| 148 | class AudioCommandThread : public Thread { |
| 149 | class AudioCommand; |
| 150 | public: |
| 151 | |
| 152 | // commands for tone AudioCommand |
| 153 | enum { |
| 154 | START_TONE, |
| 155 | STOP_TONE, |
| 156 | SET_VOLUME, |
| 157 | SET_PARAMETERS, |
| 158 | SET_VOICE_VOLUME |
| 159 | }; |
| 160 | |
| 161 | AudioCommandThread (String8 name); |
| 162 | virtual ~AudioCommandThread(); |
| 163 | |
| 164 | status_t dump(int fd); |
| 165 | |
| 166 | // Thread virtuals |
| 167 | virtual void onFirstRef(); |
| 168 | virtual bool threadLoop(); |
| 169 | |
| 170 | void exit(); |
| 171 | void startToneCommand(int type = 0, int stream = 0); |
| 172 | void stopToneCommand(); |
| 173 | status_t volumeCommand(int stream, float volume, int output, int delayMs = 0); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 174 | status_t parametersCommand(int ioHandle, const char *keyValuePairs, int delayMs = 0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 175 | status_t voiceVolumeCommand(float volume, int delayMs = 0); |
| 176 | void insertCommand_l(AudioCommand *command, int delayMs = 0); |
| 177 | |
| 178 | private: |
| 179 | // descriptor for requested tone playback event |
| 180 | class AudioCommand { |
| 181 | |
| 182 | public: |
| 183 | AudioCommand() |
| 184 | : mCommand(-1) {} |
| 185 | |
| 186 | void dump(char* buffer, size_t size); |
| 187 | |
| 188 | int mCommand; // START_TONE, STOP_TONE ... |
| 189 | nsecs_t mTime; // time stamp |
| 190 | Condition mCond; // condition for status return |
| 191 | status_t mStatus; // command status |
| 192 | bool mWaitStatus; // true if caller is waiting for status |
| 193 | void *mParam; // command parameter (ToneData, VolumeData, ParametersData) |
| 194 | }; |
| 195 | |
| 196 | class ToneData { |
| 197 | public: |
| 198 | int mType; // tone type (START_TONE only) |
| 199 | int mStream; // stream type (START_TONE only) |
| 200 | }; |
| 201 | |
| 202 | class VolumeData { |
| 203 | public: |
| 204 | int mStream; |
| 205 | float mVolume; |
| 206 | int mIO; |
| 207 | }; |
| 208 | |
| 209 | class ParametersData { |
| 210 | public: |
| 211 | int mIO; |
| 212 | String8 mKeyValuePairs; |
| 213 | }; |
| 214 | |
| 215 | class VoiceVolumeData { |
| 216 | public: |
| 217 | float mVolume; |
| 218 | }; |
| 219 | |
| 220 | Mutex mLock; |
| 221 | Condition mWaitWorkCV; |
| 222 | Vector <AudioCommand *> mAudioCommands; // list of pending commands |
| 223 | ToneGenerator *mpToneGenerator; // the tone generator |
| 224 | AudioCommand mLastCommand; // last processed command (used by dump) |
| 225 | String8 mName; // string used by wake lock fo delayed commands |
| 226 | }; |
| 227 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 228 | class EffectDesc { |
| 229 | public: |
| 230 | EffectDesc() {} |
| 231 | virtual ~EffectDesc() {} |
| 232 | char *mName; |
| 233 | effect_uuid_t mUuid; |
| 234 | Vector <effect_param_t *> mParams; |
| 235 | }; |
| 236 | |
| 237 | class InputSourceDesc { |
| 238 | public: |
| 239 | InputSourceDesc() {} |
| 240 | virtual ~InputSourceDesc() {} |
| 241 | Vector <EffectDesc *> mEffects; |
| 242 | }; |
| 243 | |
| 244 | |
| 245 | class InputDesc { |
| 246 | public: |
| 247 | InputDesc() {} |
| 248 | virtual ~InputDesc() {} |
| 249 | int mSessionId; |
| 250 | Vector< sp<AudioEffect> >mEffects; |
| 251 | }; |
| 252 | |
| 253 | static const char *kInputSourceNames[AUDIO_SOURCE_CNT -1]; |
| 254 | |
| 255 | void setPreProcessorEnabled(InputDesc *inputDesc, bool enabled); |
| 256 | status_t loadPreProcessorConfig(const char *path); |
| 257 | status_t loadEffects(cnode *root, Vector <EffectDesc *>& effects); |
| 258 | EffectDesc *loadEffect(cnode *root); |
| 259 | status_t loadInputSources(cnode *root, const Vector <EffectDesc *>& effects); |
| 260 | audio_source_t inputSourceNameToEnum(const char *name); |
| 261 | InputSourceDesc *loadInputSource(cnode *root, const Vector <EffectDesc *>& effects); |
| 262 | void loadEffectParameters(cnode *root, Vector <effect_param_t *>& params); |
| 263 | effect_param_t *loadEffectParameter(cnode *root); |
| 264 | size_t readParamValue(cnode *node, |
| 265 | char *param, |
| 266 | size_t *curSize, |
| 267 | size_t *totSize); |
| 268 | size_t growParamSize(char *param, |
| 269 | size_t size, |
| 270 | size_t *curSize, |
| 271 | size_t *totSize); |
| 272 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 273 | // Internal dump utilities. |
| 274 | status_t dumpPermissionDenial(int fd); |
| 275 | |
| 276 | |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 277 | mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing |
| 278 | // device connection state or routing |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 279 | sp <AudioCommandThread> mAudioCommandThread; // audio commands thread |
| 280 | sp <AudioCommandThread> mTonePlaybackThread; // tone playback thread |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 281 | struct audio_policy_device *mpAudioPolicyDev; |
| 282 | struct audio_policy *mpAudioPolicy; |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 283 | KeyedVector< audio_source_t, InputSourceDesc* > mInputSources; |
| 284 | KeyedVector< audio_io_handle_t, InputDesc* > mInputs; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 285 | }; |
| 286 | |
| 287 | }; // namespace android |
| 288 | |
| 289 | #endif // ANDROID_AUDIOPOLICYSERVICE_H |