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> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 21 | #include <media/ToneGenerator.h> |
| 22 | #include <utils/Vector.h> |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 23 | #include <binder/BinderService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 24 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 25 | #include <hardware/audio.h> |
| 26 | #include <hardware/audio_policy.h> |
| 27 | #include <hardware/audio_policy_hal.h> |
| 28 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | |
| 31 | class String8; |
| 32 | |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 35 | class AudioPolicyService : |
| 36 | public BinderService<AudioPolicyService>, |
| 37 | public BnAudioPolicyService, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 38 | // public AudioPolicyClientInterface, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 39 | public IBinder::DeathRecipient |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | { |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 41 | friend class BinderService<AudioPolicyService>; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 | |
| 43 | public: |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 44 | // for BinderService |
| 45 | static const char *getServiceName() { return "media.audio_policy"; } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 46 | |
| 47 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 48 | |
| 49 | // |
| 50 | // BnAudioPolicyService (see AudioPolicyInterface for method descriptions) |
| 51 | // |
| 52 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 53 | virtual status_t setDeviceConnectionState(audio_devices_t device, |
| 54 | audio_policy_dev_state_t state, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 55 | const char *device_address); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 56 | virtual audio_policy_dev_state_t getDeviceConnectionState( |
| 57 | audio_devices_t device, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 58 | const char *device_address); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 59 | virtual status_t setPhoneState(int state); |
| 60 | virtual status_t setRingerMode(uint32_t mode, uint32_t mask); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 61 | virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config); |
| 62 | virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage); |
| 63 | virtual audio_io_handle_t getOutput(audio_stream_type_t stream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 64 | uint32_t samplingRate = 0, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 65 | uint32_t format = AUDIO_FORMAT_DEFAULT, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 66 | uint32_t channels = 0, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 67 | audio_policy_output_flags_t flags = |
| 68 | AUDIO_POLICY_OUTPUT_FLAG_INDIRECT); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 69 | virtual status_t startOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 70 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 71 | int session = 0); |
| 72 | virtual status_t stopOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 73 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 74 | int session = 0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 75 | virtual void releaseOutput(audio_io_handle_t output); |
| 76 | virtual audio_io_handle_t getInput(int inputSource, |
| 77 | uint32_t samplingRate = 0, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 78 | uint32_t format = AUDIO_FORMAT_DEFAULT, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 79 | uint32_t channels = 0, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 80 | audio_in_acoustics_t acoustics = |
| 81 | (audio_in_acoustics_t)0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 82 | virtual status_t startInput(audio_io_handle_t input); |
| 83 | virtual status_t stopInput(audio_io_handle_t input); |
| 84 | virtual void releaseInput(audio_io_handle_t input); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 85 | virtual status_t initStreamVolume(audio_stream_type_t stream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 86 | int indexMin, |
| 87 | int indexMax); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 88 | virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, int index); |
| 89 | virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, int *index); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 90 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 91 | virtual uint32_t getStrategyForStream(audio_stream_type_t stream); |
| 92 | virtual uint32_t getDevicesForStream(audio_stream_type_t stream); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 93 | |
| 94 | virtual audio_io_handle_t getOutputForEffect(effect_descriptor_t *desc); |
| 95 | virtual status_t registerEffect(effect_descriptor_t *desc, |
| 96 | audio_io_handle_t output, |
| 97 | uint32_t strategy, |
| 98 | int session, |
| 99 | int id); |
| 100 | virtual status_t unregisterEffect(int id); |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 101 | virtual bool isStreamActive(int stream, uint32_t inPastMs = 0) const; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 102 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 103 | virtual status_t onTransact( |
| 104 | uint32_t code, |
| 105 | const Parcel& data, |
| 106 | Parcel* reply, |
| 107 | uint32_t flags); |
| 108 | |
| 109 | // IBinder::DeathRecipient |
| 110 | virtual void binderDied(const wp<IBinder>& who); |
| 111 | |
| 112 | // |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 113 | // Helpers for the struct audio_policy_service_ops implementation. |
| 114 | // This is used by the audio policy manager for certain operations that |
| 115 | // are implemented by the policy service. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 116 | // |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 117 | virtual void setParameters(audio_io_handle_t ioHandle, |
| 118 | const char *keyValuePairs, |
| 119 | int delayMs); |
| 120 | |
| 121 | virtual status_t setStreamVolume(audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 122 | float volume, |
| 123 | audio_io_handle_t output, |
| 124 | int delayMs = 0); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 125 | virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 126 | virtual status_t stopTone(); |
| 127 | virtual status_t setVoiceVolume(float volume, int delayMs = 0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 128 | |
| 129 | private: |
| 130 | AudioPolicyService(); |
| 131 | virtual ~AudioPolicyService(); |
| 132 | |
| 133 | status_t dumpInternals(int fd); |
| 134 | |
| 135 | // Thread used for tone playback and to send audio config commands to audio flinger |
| 136 | // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because startTone() |
| 137 | // and stopTone() are normally called with mLock locked and requesting a tone start or stop will cause |
| 138 | // calls to AudioPolicyService and an attempt to lock mLock. |
| 139 | // For audio config commands, it is necessary because audio flinger requires that the calling process (user) |
| 140 | // has permission to modify audio settings. |
| 141 | class AudioCommandThread : public Thread { |
| 142 | class AudioCommand; |
| 143 | public: |
| 144 | |
| 145 | // commands for tone AudioCommand |
| 146 | enum { |
| 147 | START_TONE, |
| 148 | STOP_TONE, |
| 149 | SET_VOLUME, |
| 150 | SET_PARAMETERS, |
| 151 | SET_VOICE_VOLUME |
| 152 | }; |
| 153 | |
| 154 | AudioCommandThread (String8 name); |
| 155 | virtual ~AudioCommandThread(); |
| 156 | |
| 157 | status_t dump(int fd); |
| 158 | |
| 159 | // Thread virtuals |
| 160 | virtual void onFirstRef(); |
| 161 | virtual bool threadLoop(); |
| 162 | |
| 163 | void exit(); |
| 164 | void startToneCommand(int type = 0, int stream = 0); |
| 165 | void stopToneCommand(); |
| 166 | status_t volumeCommand(int stream, float volume, int output, int delayMs = 0); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 167 | status_t parametersCommand(int ioHandle, const char *keyValuePairs, int delayMs = 0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 168 | status_t voiceVolumeCommand(float volume, int delayMs = 0); |
| 169 | void insertCommand_l(AudioCommand *command, int delayMs = 0); |
| 170 | |
| 171 | private: |
| 172 | // descriptor for requested tone playback event |
| 173 | class AudioCommand { |
| 174 | |
| 175 | public: |
| 176 | AudioCommand() |
| 177 | : mCommand(-1) {} |
| 178 | |
| 179 | void dump(char* buffer, size_t size); |
| 180 | |
| 181 | int mCommand; // START_TONE, STOP_TONE ... |
| 182 | nsecs_t mTime; // time stamp |
| 183 | Condition mCond; // condition for status return |
| 184 | status_t mStatus; // command status |
| 185 | bool mWaitStatus; // true if caller is waiting for status |
| 186 | void *mParam; // command parameter (ToneData, VolumeData, ParametersData) |
| 187 | }; |
| 188 | |
| 189 | class ToneData { |
| 190 | public: |
| 191 | int mType; // tone type (START_TONE only) |
| 192 | int mStream; // stream type (START_TONE only) |
| 193 | }; |
| 194 | |
| 195 | class VolumeData { |
| 196 | public: |
| 197 | int mStream; |
| 198 | float mVolume; |
| 199 | int mIO; |
| 200 | }; |
| 201 | |
| 202 | class ParametersData { |
| 203 | public: |
| 204 | int mIO; |
| 205 | String8 mKeyValuePairs; |
| 206 | }; |
| 207 | |
| 208 | class VoiceVolumeData { |
| 209 | public: |
| 210 | float mVolume; |
| 211 | }; |
| 212 | |
| 213 | Mutex mLock; |
| 214 | Condition mWaitWorkCV; |
| 215 | Vector <AudioCommand *> mAudioCommands; // list of pending commands |
| 216 | ToneGenerator *mpToneGenerator; // the tone generator |
| 217 | AudioCommand mLastCommand; // last processed command (used by dump) |
| 218 | String8 mName; // string used by wake lock fo delayed commands |
| 219 | }; |
| 220 | |
| 221 | // Internal dump utilities. |
| 222 | status_t dumpPermissionDenial(int fd); |
| 223 | |
| 224 | |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 225 | mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing |
| 226 | // device connection state or routing |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 227 | sp <AudioCommandThread> mAudioCommandThread; // audio commands thread |
| 228 | sp <AudioCommandThread> mTonePlaybackThread; // tone playback thread |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame^] | 229 | |
| 230 | struct audio_policy_device *mpAudioPolicyDev; |
| 231 | struct audio_policy *mpAudioPolicy; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | }; // namespace android |
| 235 | |
| 236 | #endif // ANDROID_AUDIOPOLICYSERVICE_H |