Eric Laurent | 9d91ad5 | 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 | 23490ef | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 23 | #include <utils/Vector.h> |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 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); |
Eric Laurent | 23490ef | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 102 | virtual status_t setStreamVolume(AudioSystem::stream_type stream, float volume, audio_io_handle_t output, int delayMs = 0); |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 103 | virtual status_t setStreamOutput(AudioSystem::stream_type stream, audio_io_handle_t output); |
Eric Laurent | 23490ef | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 104 | virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0); |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 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(); |
Eric Laurent | 63da2b6 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 108 | virtual status_t setVoiceVolume(float volume, int delayMs = 0); |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 109 | |
| 110 | private: |
| 111 | AudioPolicyService(); |
| 112 | virtual ~AudioPolicyService(); |
| 113 | |
Eric Laurent | 7f698b4 | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 114 | status_t dumpInternals(int fd); |
| 115 | |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 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 { |
Eric Laurent | 23490ef | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 123 | class AudioCommand; |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 124 | public: |
| 125 | |
| 126 | // commands for tone AudioCommand |
| 127 | enum { |
| 128 | START_TONE, |
| 129 | STOP_TONE, |
| 130 | SET_VOLUME, |
Eric Laurent | 63da2b6 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 131 | SET_PARAMETERS, |
| 132 | SET_VOICE_VOLUME |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | AudioCommandThread (); |
| 136 | virtual ~AudioCommandThread(); |
| 137 | |
Eric Laurent | 7f698b4 | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 138 | status_t dump(int fd); |
| 139 | |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 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(); |
Eric Laurent | 23490ef | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 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); |
Eric Laurent | 63da2b6 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 149 | status_t voiceVolumeCommand(float volume, int delayMs = 0); |
Eric Laurent | 23490ef | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 150 | void insertCommand_l(AudioCommand *command, int delayMs = 0); |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 151 | |
| 152 | private: |
| 153 | // descriptor for requested tone playback event |
| 154 | class AudioCommand { |
| 155 | public: |
Eric Laurent | 7f698b4 | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 156 | void dump(char* buffer, size_t size); |
| 157 | |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 158 | int mCommand; // START_TONE, STOP_TONE ... |
Eric Laurent | 23490ef | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 159 | nsecs_t mTime; // time stamp |
| 160 | Condition mCond; // condition for status return |
| 161 | status_t mStatus; // command status |
| 162 | bool mWaitStatus; // true if caller is waiting for status |
| 163 | void *mParam; // command parameter (ToneData, VolumeData, ParametersData) |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | class ToneData { |
| 167 | public: |
| 168 | int mType; // tone type (START_TONE only) |
| 169 | int mStream; // stream type (START_TONE only) |
| 170 | }; |
| 171 | |
| 172 | class VolumeData { |
| 173 | public: |
| 174 | int mStream; |
| 175 | float mVolume; |
Eric Laurent | e0e9ecc | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 176 | int mIO; |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 177 | }; |
Eric Laurent | 63da2b6 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 178 | |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 179 | class ParametersData { |
| 180 | public: |
Eric Laurent | e0e9ecc | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 181 | int mIO; |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 182 | String8 mKeyValuePairs; |
| 183 | }; |
| 184 | |
Eric Laurent | 63da2b6 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 185 | class VoiceVolumeData { |
| 186 | public: |
| 187 | float mVolume; |
| 188 | }; |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 189 | |
| 190 | Mutex mLock; |
| 191 | Condition mWaitWorkCV; |
Eric Laurent | 23490ef | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 192 | Vector <AudioCommand *> mAudioCommands; // list of pending commands |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 193 | ToneGenerator *mpToneGenerator; // the tone generator |
| 194 | }; |
| 195 | |
| 196 | // Internal dump utilities. |
Eric Laurent | 7f698b4 | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 197 | status_t dumpPermissionDenial(int fd); |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 198 | |
| 199 | |
| 200 | Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing device |
| 201 | // connection stated our routing |
| 202 | AudioPolicyInterface* mpPolicyManager; // the platform specific policy manager |
| 203 | sp <AudioCommandThread> mAudioCommandThread; // audio commands thread |
Eric Laurent | 23490ef | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 204 | sp <AudioCommandThread> mTonePlaybackThread; // tone playback thread |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | }; // namespace android |
| 208 | |
| 209 | #endif // ANDROID_AUDIOPOLICYSERVICE_H |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |