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> |
Eric Laurent | a553c25 | 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 | 327c27b | 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 | a553c25 | 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 | 327c27b | 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 | a553c25 | 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 | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 108 | virtual status_t setVoiceVolume(float volume, int delayMs = 0); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 109 | |
| 110 | private: |
| 111 | AudioPolicyService(); |
| 112 | virtual ~AudioPolicyService(); |
| 113 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 114 | status_t dumpInternals(int fd); |
| 115 | |
Eric Laurent | a553c25 | 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 | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 123 | class AudioCommand; |
Eric Laurent | a553c25 | 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 | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 131 | SET_PARAMETERS, |
| 132 | SET_VOICE_VOLUME |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | AudioCommandThread (); |
| 136 | virtual ~AudioCommandThread(); |
| 137 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 138 | status_t dump(int fd); |
| 139 | |
Eric Laurent | a553c25 | 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 | 327c27b | 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 | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 149 | status_t voiceVolumeCommand(float volume, int delayMs = 0); |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 150 | void insertCommand_l(AudioCommand *command, int delayMs = 0); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 151 | |
| 152 | private: |
| 153 | // descriptor for requested tone playback event |
| 154 | class AudioCommand { |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 155 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 156 | public: |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 157 | AudioCommand() |
| 158 | : mCommand(-1) {} |
| 159 | |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 160 | void dump(char* buffer, size_t size); |
| 161 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 162 | int mCommand; // START_TONE, STOP_TONE ... |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 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) |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 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; |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 180 | int mIO; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 181 | }; |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 182 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 183 | class ParametersData { |
| 184 | public: |
Eric Laurent | ddb78e7 | 2009-07-28 08:44:33 -0700 | [diff] [blame] | 185 | int mIO; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 186 | String8 mKeyValuePairs; |
| 187 | }; |
| 188 | |
Eric Laurent | 415f3e2 | 2009-10-21 08:14:22 -0700 | [diff] [blame] | 189 | class VoiceVolumeData { |
| 190 | public: |
| 191 | float mVolume; |
| 192 | }; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 193 | |
| 194 | Mutex mLock; |
| 195 | Condition mWaitWorkCV; |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 196 | Vector <AudioCommand *> mAudioCommands; // list of pending commands |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 197 | ToneGenerator *mpToneGenerator; // the tone generator |
Eric Laurent | 3fdb126 | 2009-11-07 00:01:32 -0800 | [diff] [blame] | 198 | AudioCommand mLastCommand; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | // Internal dump utilities. |
Eric Laurent | f4ee40e | 2009-11-02 10:29:02 -0800 | [diff] [blame] | 202 | status_t dumpPermissionDenial(int fd); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 203 | |
| 204 | |
| 205 | Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing device |
| 206 | // connection stated our routing |
| 207 | AudioPolicyInterface* mpPolicyManager; // the platform specific policy manager |
| 208 | sp <AudioCommandThread> mAudioCommandThread; // audio commands thread |
Eric Laurent | 327c27b | 2009-08-27 00:48:47 -0700 | [diff] [blame] | 209 | sp <AudioCommandThread> mTonePlaybackThread; // tone playback thread |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | }; // namespace android |
| 213 | |
| 214 | #endif // ANDROID_AUDIOPOLICYSERVICE_H |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |