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