Eric Laurent | a553c25 | 2009-07-17 12:17:14 -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 | |
| 20 | #include <media/IAudioPolicyService.h> |
| 21 | #include <hardware_legacy/AudioPolicyInterface.h> |
| 22 | #include <media/ToneGenerator.h> |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 23 | #include <utils/Vector.h> |
Glenn Kasten | 3515a33 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 24 | #include <binder/BinderService.h> |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | class String8; |
| 29 | |
| 30 | // ---------------------------------------------------------------------------- |
| 31 | |
Glenn Kasten | 3515a33 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 32 | class AudioPolicyService : |
| 33 | public BinderService<AudioPolicyService>, |
| 34 | public BnAudioPolicyService, |
| 35 | public AudioPolicyClientInterface, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 36 | public IBinder::DeathRecipient |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 37 | { |
Glenn Kasten | 3515a33 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 38 | friend class BinderService<AudioPolicyService>; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 39 | |
| 40 | public: |
Glenn Kasten | 3515a33 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 41 | // for BinderService |
| 42 | static const char *getServiceName() { return "media.audio_policy"; } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 43 | |
| 44 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 45 | |
| 46 | // |
| 47 | // BnAudioPolicyService (see AudioPolicyInterface for method descriptions) |
| 48 | // |
| 49 | |
| 50 | virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device, |
| 51 | AudioSystem::device_connection_state state, |
| 52 | const char *device_address); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 53 | virtual AudioSystem::device_connection_state getDeviceConnectionState( |
| 54 | AudioSystem::audio_devices device, |
| 55 | const char *device_address); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 56 | virtual status_t setPhoneState(int state); |
| 57 | virtual status_t setRingerMode(uint32_t mode, uint32_t mask); |
| 58 | virtual status_t setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config); |
| 59 | virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage); |
| 60 | virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream, |
| 61 | uint32_t samplingRate = 0, |
| 62 | uint32_t format = AudioSystem::FORMAT_DEFAULT, |
| 63 | uint32_t channels = 0, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 64 | AudioSystem::output_flags flags = |
| 65 | AudioSystem::OUTPUT_FLAG_INDIRECT); |
| 66 | virtual status_t startOutput(audio_io_handle_t output, |
| 67 | AudioSystem::stream_type stream, |
| 68 | int session = 0); |
| 69 | virtual status_t stopOutput(audio_io_handle_t output, |
| 70 | AudioSystem::stream_type stream, |
| 71 | int session = 0); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 72 | virtual void releaseOutput(audio_io_handle_t output); |
| 73 | virtual audio_io_handle_t getInput(int inputSource, |
| 74 | uint32_t samplingRate = 0, |
| 75 | uint32_t format = AudioSystem::FORMAT_DEFAULT, |
| 76 | uint32_t channels = 0, |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 77 | AudioSystem::audio_in_acoustics acoustics = |
| 78 | (AudioSystem::audio_in_acoustics)0); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 79 | virtual status_t startInput(audio_io_handle_t input); |
| 80 | virtual status_t stopInput(audio_io_handle_t input); |
| 81 | virtual void releaseInput(audio_io_handle_t input); |
| 82 | virtual status_t initStreamVolume(AudioSystem::stream_type stream, |
| 83 | int indexMin, |
| 84 | int indexMax); |
| 85 | virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index); |
| 86 | virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index); |
| 87 | |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 88 | virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream); |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 89 | virtual uint32_t getDevicesForStream(AudioSystem::stream_type stream); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 90 | |
| 91 | virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc); |
| 92 | virtual status_t registerEffect(effect_descriptor_t *desc, |
| 93 | audio_io_handle_t output, |
| 94 | uint32_t strategy, |
| 95 | int session, |
| 96 | int id); |
| 97 | virtual status_t unregisterEffect(int id); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 98 | virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const; |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 99 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 100 | virtual status_t onTransact( |
| 101 | uint32_t code, |
| 102 | const Parcel& data, |
| 103 | Parcel* reply, |
| 104 | uint32_t flags); |
| 105 | |
| 106 | // IBinder::DeathRecipient |
| 107 | virtual void binderDied(const wp<IBinder>& who); |
| 108 | |
| 109 | // |
| 110 | // AudioPolicyClientInterface |
| 111 | // |
| 112 | virtual audio_io_handle_t openOutput(uint32_t *pDevices, |
| 113 | uint32_t *pSamplingRate, |
| 114 | uint32_t *pFormat, |
| 115 | uint32_t *pChannels, |
| 116 | uint32_t *pLatencyMs, |
| 117 | AudioSystem::output_flags flags); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 118 | virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, |
| 119 | audio_io_handle_t output2); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 120 | virtual status_t closeOutput(audio_io_handle_t output); |
| 121 | virtual status_t suspendOutput(audio_io_handle_t output); |
| 122 | virtual status_t restoreOutput(audio_io_handle_t output); |
| 123 | virtual audio_io_handle_t openInput(uint32_t *pDevices, |
| 124 | uint32_t *pSamplingRate, |
| 125 | uint32_t *pFormat, |
| 126 | uint32_t *pChannels, |
| 127 | uint32_t acoustics); |
| 128 | virtual status_t closeInput(audio_io_handle_t input); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 129 | virtual status_t setStreamVolume(AudioSystem::stream_type stream, |
| 130 | float volume, |
| 131 | audio_io_handle_t output, |
| 132 | int delayMs = 0); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 133 | virtual status_t setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 134 | virtual void setParameters(audio_io_handle_t ioHandle, |
| 135 | const String8& keyValuePairs, |
| 136 | int delayMs = 0); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 137 | virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys); |
| 138 | virtual status_t startTone(ToneGenerator::tone_type tone, AudioSystem::stream_type stream); |
| 139 | virtual status_t stopTone(); |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 140 | virtual status_t setVoiceVolume(float volume, int delayMs = 0); |
Eric Laurent | 8ed6ed0 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 141 | virtual status_t moveEffects(int session, |
| 142 | audio_io_handle_t srcOutput, |
| 143 | audio_io_handle_t dstOutput); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 144 | |
| 145 | private: |
| 146 | AudioPolicyService(); |
| 147 | virtual ~AudioPolicyService(); |
| 148 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 149 | status_t dumpInternals(int fd); |
| 150 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 151 | // Thread used for tone playback and to send audio config commands to audio flinger |
| 152 | // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because startTone() |
| 153 | // and stopTone() are normally called with mLock locked and requesting a tone start or stop will cause |
| 154 | // calls to AudioPolicyService and an attempt to lock mLock. |
| 155 | // For audio config commands, it is necessary because audio flinger requires that the calling process (user) |
| 156 | // has permission to modify audio settings. |
| 157 | class AudioCommandThread : public Thread { |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 158 | class AudioCommand; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 159 | public: |
| 160 | |
| 161 | // commands for tone AudioCommand |
| 162 | enum { |
| 163 | START_TONE, |
| 164 | STOP_TONE, |
| 165 | SET_VOLUME, |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 166 | SET_PARAMETERS, |
| 167 | SET_VOICE_VOLUME |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 170 | AudioCommandThread (String8 name); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 171 | virtual ~AudioCommandThread(); |
| 172 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 173 | status_t dump(int fd); |
| 174 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 175 | // Thread virtuals |
| 176 | virtual void onFirstRef(); |
| 177 | virtual bool threadLoop(); |
| 178 | |
| 179 | void exit(); |
| 180 | void startToneCommand(int type = 0, int stream = 0); |
| 181 | void stopToneCommand(); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 182 | status_t volumeCommand(int stream, float volume, int output, int delayMs = 0); |
| 183 | status_t parametersCommand(int ioHandle, const String8& keyValuePairs, int delayMs = 0); |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 184 | status_t voiceVolumeCommand(float volume, int delayMs = 0); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 185 | void insertCommand_l(AudioCommand *command, int delayMs = 0); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 186 | |
| 187 | private: |
| 188 | // descriptor for requested tone playback event |
| 189 | class AudioCommand { |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 190 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 191 | public: |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 192 | AudioCommand() |
| 193 | : mCommand(-1) {} |
| 194 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 195 | void dump(char* buffer, size_t size); |
| 196 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 197 | int mCommand; // START_TONE, STOP_TONE ... |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 198 | nsecs_t mTime; // time stamp |
| 199 | Condition mCond; // condition for status return |
| 200 | status_t mStatus; // command status |
| 201 | bool mWaitStatus; // true if caller is waiting for status |
| 202 | void *mParam; // command parameter (ToneData, VolumeData, ParametersData) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | class ToneData { |
| 206 | public: |
| 207 | int mType; // tone type (START_TONE only) |
| 208 | int mStream; // stream type (START_TONE only) |
| 209 | }; |
| 210 | |
| 211 | class VolumeData { |
| 212 | public: |
| 213 | int mStream; |
| 214 | float mVolume; |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 215 | int mIO; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 216 | }; |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 217 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 218 | class ParametersData { |
| 219 | public: |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 220 | int mIO; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 221 | String8 mKeyValuePairs; |
| 222 | }; |
| 223 | |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 224 | class VoiceVolumeData { |
| 225 | public: |
| 226 | float mVolume; |
| 227 | }; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 228 | |
| 229 | Mutex mLock; |
| 230 | Condition mWaitWorkCV; |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 231 | Vector <AudioCommand *> mAudioCommands; // list of pending commands |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 232 | ToneGenerator *mpToneGenerator; // the tone generator |
Eric Laurent | cef3cd7 | 2009-12-10 01:03:50 -0800 | [diff] [blame] | 233 | AudioCommand mLastCommand; // last processed command (used by dump) |
| 234 | String8 mName; // string used by wake lock fo delayed commands |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | // Internal dump utilities. |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 238 | status_t dumpPermissionDenial(int fd); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 239 | |
| 240 | |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 241 | mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing |
| 242 | // device connection state or routing |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 243 | AudioPolicyInterface* mpPolicyManager; // the platform specific policy manager |
| 244 | sp <AudioCommandThread> mAudioCommandThread; // audio commands thread |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 245 | sp <AudioCommandThread> mTonePlaybackThread; // tone playback thread |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | }; // namespace android |
| 249 | |
| 250 | #endif // ANDROID_AUDIOPOLICYSERVICE_H |