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> 23 #include <utils/Vector.h> 24 25 namespace android { 26 27 class String8; 28 29 // ---------------------------------------------------------------------------- 30 31 class AudioPolicyService: public BnAudioPolicyService, public AudioPolicyClientInterface, 32 public IBinder::DeathRecipient 33 { 34 35 public: 36 static void instantiate(); 37 38 virtual status_t dump(int fd, const Vector<String16>& args); 39 40 // 41 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions) 42 // 43 44 virtual status_t setDeviceConnectionState(AudioSystem::audio_devices device, 45 AudioSystem::device_connection_state state, 46 const char *device_address); 47 virtual AudioSystem::device_connection_state getDeviceConnectionState( 48 AudioSystem::audio_devices device, 49 const char *device_address); 50 virtual status_t setPhoneState(int state); 51 virtual status_t setRingerMode(uint32_t mode, uint32_t mask); 52 virtual status_t setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config); 53 virtual AudioSystem::forced_config getForceUse(AudioSystem::force_use usage); 54 virtual audio_io_handle_t getOutput(AudioSystem::stream_type stream, 55 uint32_t samplingRate = 0, 56 uint32_t format = AudioSystem::FORMAT_DEFAULT, 57 uint32_t channels = 0, 58 AudioSystem::output_flags flags = 59 AudioSystem::OUTPUT_FLAG_INDIRECT); 60 virtual status_t startOutput(audio_io_handle_t output, 61 AudioSystem::stream_type stream, 62 int session = 0); 63 virtual status_t stopOutput(audio_io_handle_t output, 64 AudioSystem::stream_type stream, 65 int session = 0); 66 virtual void releaseOutput(audio_io_handle_t output); 67 virtual audio_io_handle_t getInput(int inputSource, 68 uint32_t samplingRate = 0, 69 uint32_t format = AudioSystem::FORMAT_DEFAULT, 70 uint32_t channels = 0, 71 AudioSystem::audio_in_acoustics acoustics = 72 (AudioSystem::audio_in_acoustics)0); 73 virtual status_t startInput(audio_io_handle_t input); 74 virtual status_t stopInput(audio_io_handle_t input); 75 virtual void releaseInput(audio_io_handle_t input); 76 virtual status_t initStreamVolume(AudioSystem::stream_type stream, 77 int indexMin, 78 int indexMax); 79 virtual status_t setStreamVolumeIndex(AudioSystem::stream_type stream, int index); 80 virtual status_t getStreamVolumeIndex(AudioSystem::stream_type stream, int *index); 81 82 virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream); 83 84 virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc); 85 virtual status_t registerEffect(effect_descriptor_t *desc, 86 audio_io_handle_t output, 87 uint32_t strategy, 88 int session, 89 int id); 90 virtual status_t unregisterEffect(int id); 91 92 virtual status_t onTransact( 93 uint32_t code, 94 const Parcel& data, 95 Parcel* reply, 96 uint32_t flags); 97 98 // IBinder::DeathRecipient 99 virtual void binderDied(const wp<IBinder>& who); 100 101 // 102 // AudioPolicyClientInterface 103 // 104 virtual audio_io_handle_t openOutput(uint32_t *pDevices, 105 uint32_t *pSamplingRate, 106 uint32_t *pFormat, 107 uint32_t *pChannels, 108 uint32_t *pLatencyMs, 109 AudioSystem::output_flags flags); 110 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, 111 audio_io_handle_t output2); 112 virtual status_t closeOutput(audio_io_handle_t output); 113 virtual status_t suspendOutput(audio_io_handle_t output); 114 virtual status_t restoreOutput(audio_io_handle_t output); 115 virtual audio_io_handle_t openInput(uint32_t *pDevices, 116 uint32_t *pSamplingRate, 117 uint32_t *pFormat, 118 uint32_t *pChannels, 119 uint32_t acoustics); 120 virtual status_t closeInput(audio_io_handle_t input); 121 virtual status_t setStreamVolume(AudioSystem::stream_type stream, 122 float volume, 123 audio_io_handle_t output, 124 int delayMs = 0); 125 virtual status_t setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output); 126 virtual void setParameters(audio_io_handle_t ioHandle, 127 const String8& keyValuePairs, 128 int delayMs = 0); 129 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys); 130 virtual status_t startTone(ToneGenerator::tone_type tone, AudioSystem::stream_type stream); 131 virtual status_t stopTone(); 132 virtual status_t setVoiceVolume(float volume, int delayMs = 0); 133 virtual status_t moveEffects(int session, 134 audio_io_handle_t srcOutput, 135 audio_io_handle_t dstOutput); 136 137 private: 138 AudioPolicyService(); 139 virtual ~AudioPolicyService(); 140 141 status_t dumpInternals(int fd); 142 143 // 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 { 150 class AudioCommand; 151 public: 152 153 // commands for tone AudioCommand 154 enum { 155 START_TONE, 156 STOP_TONE, 157 SET_VOLUME, 158 SET_PARAMETERS, 159 SET_VOICE_VOLUME 160 }; 161 162 AudioCommandThread (String8 name); 163 virtual ~AudioCommandThread(); 164 165 status_t dump(int fd); 166 167 // 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(); 174 status_t volumeCommand(int stream, float volume, int output, int delayMs = 0); 175 status_t parametersCommand(int ioHandle, const String8& keyValuePairs, int delayMs = 0); 176 status_t voiceVolumeCommand(float volume, int delayMs = 0); 177 void insertCommand_l(AudioCommand *command, int delayMs = 0); 178 179 private: 180 // descriptor for requested tone playback event 181 class AudioCommand { 182 183 public: 184 AudioCommand() 185 : mCommand(-1) {} 186 187 void dump(char* buffer, size_t size); 188 189 int mCommand; // START_TONE, STOP_TONE ... 190 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) 195 }; 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; 207 int mIO; 208 }; 209 210 class ParametersData { 211 public: 212 int mIO; 213 String8 mKeyValuePairs; 214 }; 215 216 class VoiceVolumeData { 217 public: 218 float mVolume; 219 }; 220 221 Mutex mLock; 222 Condition mWaitWorkCV; 223 Vector <AudioCommand *> mAudioCommands; // list of pending commands 224 ToneGenerator *mpToneGenerator; // the tone generator 225 AudioCommand mLastCommand; // last processed command (used by dump) 226 String8 mName; // string used by wake lock fo delayed commands 227 }; 228 229 // Internal dump utilities. 230 status_t dumpPermissionDenial(int fd); 231 232 233 Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing device 234 // connection stated our routing 235 AudioPolicyInterface* mpPolicyManager; // the platform specific policy manager 236 sp <AudioCommandThread> mAudioCommandThread; // audio commands thread 237 sp <AudioCommandThread> mTonePlaybackThread; // tone playback thread 238 }; 239 240 }; // namespace android 241 242 #endif // ANDROID_AUDIOPOLICYSERVICE_H 243 244 245 246 247 248 249 250 251