AudioPolicyService.h revision 6b2718c67aa7b1a8e3b0f25a73a0d5f72c59ffc3
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#include <binder/BinderService.h> 25 26namespace android { 27 28class String8; 29 30// ---------------------------------------------------------------------------- 31 32class AudioPolicyService : 33 public BinderService<AudioPolicyService>, 34 public BnAudioPolicyService, 35 public AudioPolicyClientInterface, 36 public IBinder::DeathRecipient 37{ 38 friend class BinderService<AudioPolicyService>; 39 40public: 41 // for BinderService 42 static const char *getServiceName() { return "media.audio_policy"; } 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); 53 virtual AudioSystem::device_connection_state getDeviceConnectionState( 54 AudioSystem::audio_devices device, 55 const char *device_address); 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, 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); 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, 77 AudioSystem::audio_in_acoustics acoustics = 78 (AudioSystem::audio_in_acoustics)0); 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 88 virtual uint32_t getStrategyForStream(AudioSystem::stream_type stream); 89 virtual uint32_t getDevicesForStream(AudioSystem::stream_type stream); 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); 98 virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const; 99 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); 118 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, 119 audio_io_handle_t output2); 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); 129 virtual status_t setStreamVolume(AudioSystem::stream_type stream, 130 float volume, 131 audio_io_handle_t output, 132 int delayMs = 0); 133 virtual status_t setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output); 134 virtual void setParameters(audio_io_handle_t ioHandle, 135 const String8& keyValuePairs, 136 int delayMs = 0); 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(); 140 virtual status_t setVoiceVolume(float volume, int delayMs = 0); 141 virtual status_t moveEffects(int session, 142 audio_io_handle_t srcOutput, 143 audio_io_handle_t dstOutput); 144 145private: 146 AudioPolicyService(); 147 virtual ~AudioPolicyService(); 148 149 status_t dumpInternals(int fd); 150 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 { 158 class AudioCommand; 159 public: 160 161 // commands for tone AudioCommand 162 enum { 163 START_TONE, 164 STOP_TONE, 165 SET_VOLUME, 166 SET_PARAMETERS, 167 SET_VOICE_VOLUME 168 }; 169 170 AudioCommandThread (String8 name); 171 virtual ~AudioCommandThread(); 172 173 status_t dump(int fd); 174 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(); 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); 184 status_t voiceVolumeCommand(float volume, int delayMs = 0); 185 void insertCommand_l(AudioCommand *command, int delayMs = 0); 186 187 private: 188 // descriptor for requested tone playback event 189 class AudioCommand { 190 191 public: 192 AudioCommand() 193 : mCommand(-1) {} 194 195 void dump(char* buffer, size_t size); 196 197 int mCommand; // START_TONE, STOP_TONE ... 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) 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; 215 int mIO; 216 }; 217 218 class ParametersData { 219 public: 220 int mIO; 221 String8 mKeyValuePairs; 222 }; 223 224 class VoiceVolumeData { 225 public: 226 float mVolume; 227 }; 228 229 Mutex mLock; 230 Condition mWaitWorkCV; 231 Vector <AudioCommand *> mAudioCommands; // list of pending commands 232 ToneGenerator *mpToneGenerator; // the tone generator 233 AudioCommand mLastCommand; // last processed command (used by dump) 234 String8 mName; // string used by wake lock fo delayed commands 235 }; 236 237 // Internal dump utilities. 238 status_t dumpPermissionDenial(int fd); 239 240 241 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing 242 // device connection state or routing 243 AudioPolicyInterface* mpPolicyManager; // the platform specific policy manager 244 sp <AudioCommandThread> mAudioCommandThread; // audio commands thread 245 sp <AudioCommandThread> mTonePlaybackThread; // tone playback thread 246}; 247 248}; // namespace android 249 250#endif // ANDROID_AUDIOPOLICYSERVICE_H 251