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