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 | |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 20 | #include <cutils/misc.h> |
| 21 | #include <cutils/config_utils.h> |
Mathias Agopian | e762be9 | 2013-05-09 16:26:45 -0700 | [diff] [blame] | 22 | #include <cutils/compiler.h> |
Glenn Kasten | d1d8f23 | 2011-07-08 09:34:50 -0700 | [diff] [blame] | 23 | #include <utils/String8.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 24 | #include <utils/Vector.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 25 | #include <utils/SortedVector.h> |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 26 | #include <binder/BinderService.h> |
Dima Zavin | 6476024 | 2011-05-11 14:15:23 -0700 | [diff] [blame] | 27 | #include <system/audio.h> |
Dima Zavin | 7394a4f | 2011-06-13 18:16:26 -0700 | [diff] [blame] | 28 | #include <system/audio_policy.h> |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 29 | #include <hardware/audio_policy.h> |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 30 | #include <media/IAudioPolicyService.h> |
| 31 | #include <media/ToneGenerator.h> |
| 32 | #include <media/AudioEffect.h> |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 33 | #include <hardware_legacy/AudioPolicyInterface.h> |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 34 | #include "AudioPolicyEffects.h" |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 35 | #include "AudioPolicyManager.h" |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 36 | |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 37 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 38 | namespace android { |
| 39 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | // ---------------------------------------------------------------------------- |
| 41 | |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 42 | class AudioPolicyService : |
| 43 | public BinderService<AudioPolicyService>, |
| 44 | public BnAudioPolicyService, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 45 | public IBinder::DeathRecipient |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 46 | { |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 47 | friend class BinderService<AudioPolicyService>; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 48 | |
| 49 | public: |
Glenn Kasten | d2dcb08 | 2011-02-03 16:55:26 -0800 | [diff] [blame] | 50 | // for BinderService |
Mathias Agopian | e762be9 | 2013-05-09 16:26:45 -0700 | [diff] [blame] | 51 | static const char *getServiceName() ANDROID_API { return "media.audio_policy"; } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | |
| 53 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 54 | |
| 55 | // |
| 56 | // BnAudioPolicyService (see AudioPolicyInterface for method descriptions) |
| 57 | // |
| 58 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 59 | virtual status_t setDeviceConnectionState(audio_devices_t device, |
| 60 | audio_policy_dev_state_t state, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 61 | const char *device_address); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 62 | virtual audio_policy_dev_state_t getDeviceConnectionState( |
| 63 | audio_devices_t device, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 64 | const char *device_address); |
Glenn Kasten | f78aee7 | 2012-01-04 11:00:47 -0800 | [diff] [blame] | 65 | virtual status_t setPhoneState(audio_mode_t state); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 66 | virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config); |
| 67 | virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage); |
| 68 | virtual audio_io_handle_t getOutput(audio_stream_type_t stream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 69 | uint32_t samplingRate = 0, |
Glenn Kasten | 58f3021 | 2012-01-12 12:27:51 -0800 | [diff] [blame] | 70 | audio_format_t format = AUDIO_FORMAT_DEFAULT, |
Glenn Kasten | 254af18 | 2012-07-03 14:59:05 -0700 | [diff] [blame] | 71 | audio_channel_mask_t channelMask = 0, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 72 | audio_output_flags_t flags = |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 73 | AUDIO_OUTPUT_FLAG_NONE, |
| 74 | const audio_offload_info_t *offloadInfo = NULL); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 75 | virtual audio_io_handle_t getOutputForAttr(const audio_attributes_t *attr, |
| 76 | uint32_t samplingRate = 0, |
| 77 | audio_format_t format = AUDIO_FORMAT_DEFAULT, |
| 78 | audio_channel_mask_t channelMask = 0, |
| 79 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, |
| 80 | const audio_offload_info_t *offloadInfo = NULL); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 81 | virtual status_t startOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 82 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 83 | int session = 0); |
| 84 | virtual status_t stopOutput(audio_io_handle_t output, |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 85 | audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 86 | int session = 0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 87 | virtual void releaseOutput(audio_io_handle_t output); |
Glenn Kasten | eba51fb | 2012-01-23 13:58:49 -0800 | [diff] [blame] | 88 | virtual audio_io_handle_t getInput(audio_source_t inputSource, |
Glenn Kasten | b3b1660 | 2014-07-16 08:36:31 -0700 | [diff] [blame] | 89 | uint32_t samplingRate, |
| 90 | audio_format_t format, |
| 91 | audio_channel_mask_t channelMask, |
| 92 | int audioSession, |
| 93 | audio_input_flags_t flags); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 94 | virtual status_t startInput(audio_io_handle_t input, |
| 95 | audio_session_t session); |
| 96 | virtual status_t stopInput(audio_io_handle_t input, |
| 97 | audio_session_t session); |
| 98 | virtual void releaseInput(audio_io_handle_t input, |
| 99 | audio_session_t session); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 100 | virtual status_t initStreamVolume(audio_stream_type_t stream, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 101 | int indexMin, |
| 102 | int indexMax); |
Eric Laurent | 83844cc | 2011-11-18 16:43:31 -0800 | [diff] [blame] | 103 | virtual status_t setStreamVolumeIndex(audio_stream_type_t stream, |
| 104 | int index, |
| 105 | audio_devices_t device); |
| 106 | virtual status_t getStreamVolumeIndex(audio_stream_type_t stream, |
| 107 | int *index, |
| 108 | audio_devices_t device); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 109 | |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 110 | virtual uint32_t getStrategyForStream(audio_stream_type_t stream); |
Eric Laurent | 6374252 | 2012-03-08 13:42:42 -0800 | [diff] [blame] | 111 | virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream); |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 112 | |
Glenn Kasten | 58e5aa3 | 2012-06-20 14:08:14 -0700 | [diff] [blame] | 113 | virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc); |
| 114 | virtual status_t registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | 7c7f10b | 2011-06-17 21:29:58 -0700 | [diff] [blame] | 115 | audio_io_handle_t io, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 116 | uint32_t strategy, |
| 117 | int session, |
| 118 | int id); |
| 119 | virtual status_t unregisterEffect(int id); |
Eric Laurent | db7c079 | 2011-08-10 10:37:50 -0700 | [diff] [blame] | 120 | virtual status_t setEffectEnabled(int id, bool enabled); |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 121 | virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const; |
Jean-Michel Trivi | 272ab54 | 2013-02-04 16:26:02 -0800 | [diff] [blame] | 122 | virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const; |
Jean-Michel Trivi | d708603 | 2012-10-10 12:11:16 -0700 | [diff] [blame] | 123 | virtual bool isSourceActive(audio_source_t source) const; |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 124 | |
Eric Laurent | 57dae99 | 2011-07-24 13:36:09 -0700 | [diff] [blame] | 125 | virtual status_t queryDefaultPreProcessing(int audioSession, |
| 126 | effect_descriptor_t *descriptors, |
| 127 | uint32_t *count); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 128 | virtual status_t onTransact( |
| 129 | uint32_t code, |
| 130 | const Parcel& data, |
| 131 | Parcel* reply, |
| 132 | uint32_t flags); |
| 133 | |
| 134 | // IBinder::DeathRecipient |
| 135 | virtual void binderDied(const wp<IBinder>& who); |
| 136 | |
Eric Laurent | f5ada6e | 2014-10-09 17:49:00 -0700 | [diff] [blame^] | 137 | // RefBase |
| 138 | virtual void onFirstRef(); |
| 139 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 140 | // |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 141 | // Helpers for the struct audio_policy_service_ops implementation. |
| 142 | // This is used by the audio policy manager for certain operations that |
| 143 | // are implemented by the policy service. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 144 | // |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 145 | virtual void setParameters(audio_io_handle_t ioHandle, |
| 146 | const char *keyValuePairs, |
| 147 | int delayMs); |
| 148 | |
| 149 | virtual status_t setStreamVolume(audio_stream_type_t stream, |
Eric Laurent | de07013 | 2010-07-13 04:45:46 -0700 | [diff] [blame] | 150 | float volume, |
| 151 | audio_io_handle_t output, |
| 152 | int delayMs = 0); |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 153 | 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] | 154 | virtual status_t stopTone(); |
| 155 | virtual status_t setVoiceVolume(float volume, int delayMs = 0); |
Richard Fitzgerald | ad3af33 | 2013-03-25 16:54:37 +0000 | [diff] [blame] | 156 | virtual bool isOffloadSupported(const audio_offload_info_t &config); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 157 | |
Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 158 | virtual status_t listAudioPorts(audio_port_role_t role, |
| 159 | audio_port_type_t type, |
| 160 | unsigned int *num_ports, |
| 161 | struct audio_port *ports, |
| 162 | unsigned int *generation); |
| 163 | virtual status_t getAudioPort(struct audio_port *port); |
| 164 | virtual status_t createAudioPatch(const struct audio_patch *patch, |
| 165 | audio_patch_handle_t *handle); |
| 166 | virtual status_t releaseAudioPatch(audio_patch_handle_t handle); |
| 167 | virtual status_t listAudioPatches(unsigned int *num_patches, |
| 168 | struct audio_patch *patches, |
| 169 | unsigned int *generation); |
| 170 | virtual status_t setAudioPortConfig(const struct audio_port_config *config); |
| 171 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 172 | virtual void registerClient(const sp<IAudioPolicyServiceClient>& client); |
| 173 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 174 | virtual status_t acquireSoundTriggerSession(audio_session_t *session, |
| 175 | audio_io_handle_t *ioHandle, |
| 176 | audio_devices_t *device); |
| 177 | |
| 178 | virtual status_t releaseSoundTriggerSession(audio_session_t session); |
| 179 | |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 180 | virtual audio_mode_t getPhoneState(); |
| 181 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 182 | status_t doStopOutput(audio_io_handle_t output, |
| 183 | audio_stream_type_t stream, |
| 184 | int session = 0); |
| 185 | void doReleaseOutput(audio_io_handle_t output); |
| 186 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 187 | status_t clientCreateAudioPatch(const struct audio_patch *patch, |
| 188 | audio_patch_handle_t *handle, |
| 189 | int delayMs); |
| 190 | status_t clientReleaseAudioPatch(audio_patch_handle_t handle, |
| 191 | int delayMs); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 192 | virtual status_t clientSetAudioPortConfig(const struct audio_port_config *config, |
| 193 | int delayMs); |
| 194 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 195 | void removeNotificationClient(uid_t uid); |
| 196 | void onAudioPortListUpdate(); |
| 197 | void doOnAudioPortListUpdate(); |
| 198 | void onAudioPatchListUpdate(); |
| 199 | void doOnAudioPatchListUpdate(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 200 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 201 | private: |
Mathias Agopian | e762be9 | 2013-05-09 16:26:45 -0700 | [diff] [blame] | 202 | AudioPolicyService() ANDROID_API; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 203 | virtual ~AudioPolicyService(); |
| 204 | |
| 205 | status_t dumpInternals(int fd); |
| 206 | |
| 207 | // Thread used for tone playback and to send audio config commands to audio flinger |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 208 | // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because |
| 209 | // startTone() and stopTone() are normally called with mLock locked and requesting a tone start |
| 210 | // or stop will cause calls to AudioPolicyService and an attempt to lock mLock. |
| 211 | // For audio config commands, it is necessary because audio flinger requires that the calling |
| 212 | // process (user) has permission to modify audio settings. |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 213 | class AudioCommandThread : public Thread { |
| 214 | class AudioCommand; |
| 215 | public: |
| 216 | |
| 217 | // commands for tone AudioCommand |
| 218 | enum { |
| 219 | START_TONE, |
| 220 | STOP_TONE, |
| 221 | SET_VOLUME, |
| 222 | SET_PARAMETERS, |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 223 | SET_VOICE_VOLUME, |
| 224 | STOP_OUTPUT, |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 225 | RELEASE_OUTPUT, |
| 226 | CREATE_AUDIO_PATCH, |
| 227 | RELEASE_AUDIO_PATCH, |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 228 | UPDATE_AUDIOPORT_LIST, |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 229 | UPDATE_AUDIOPATCH_LIST, |
| 230 | SET_AUDIOPORT_CONFIG, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 231 | }; |
| 232 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 233 | AudioCommandThread (String8 name, const wp<AudioPolicyService>& service); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 234 | virtual ~AudioCommandThread(); |
| 235 | |
| 236 | status_t dump(int fd); |
| 237 | |
| 238 | // Thread virtuals |
| 239 | virtual void onFirstRef(); |
| 240 | virtual bool threadLoop(); |
| 241 | |
| 242 | void exit(); |
Glenn Kasten | 3d2f877 | 2012-01-27 15:25:25 -0800 | [diff] [blame] | 243 | void startToneCommand(ToneGenerator::tone_type type, |
| 244 | audio_stream_type_t stream); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 245 | void stopToneCommand(); |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 246 | status_t volumeCommand(audio_stream_type_t stream, float volume, |
| 247 | audio_io_handle_t output, int delayMs = 0); |
| 248 | status_t parametersCommand(audio_io_handle_t ioHandle, |
| 249 | const char *keyValuePairs, int delayMs = 0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 250 | status_t voiceVolumeCommand(float volume, int delayMs = 0); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 251 | void stopOutputCommand(audio_io_handle_t output, |
| 252 | audio_stream_type_t stream, |
| 253 | int session); |
| 254 | void releaseOutputCommand(audio_io_handle_t output); |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 255 | status_t sendCommand(sp<AudioCommand>& command, int delayMs = 0); |
| 256 | void insertCommand_l(sp<AudioCommand>& command, int delayMs = 0); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 257 | status_t createAudioPatchCommand(const struct audio_patch *patch, |
| 258 | audio_patch_handle_t *handle, |
| 259 | int delayMs); |
| 260 | status_t releaseAudioPatchCommand(audio_patch_handle_t handle, |
| 261 | int delayMs); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 262 | void updateAudioPortListCommand(); |
| 263 | void updateAudioPatchListCommand(); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 264 | status_t setAudioPortConfigCommand(const struct audio_port_config *config, |
| 265 | int delayMs); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 266 | void insertCommand_l(AudioCommand *command, int delayMs = 0); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 267 | |
| 268 | private: |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 269 | class AudioCommandData; |
| 270 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 271 | // descriptor for requested tone playback event |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 272 | class AudioCommand: public RefBase { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 273 | |
| 274 | public: |
| 275 | AudioCommand() |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 276 | : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {} |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 277 | |
| 278 | void dump(char* buffer, size_t size); |
| 279 | |
| 280 | int mCommand; // START_TONE, STOP_TONE ... |
| 281 | nsecs_t mTime; // time stamp |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 282 | Mutex mLock; // mutex associated to mCond |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 283 | Condition mCond; // condition for status return |
| 284 | status_t mStatus; // command status |
| 285 | bool mWaitStatus; // true if caller is waiting for status |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 286 | sp<AudioCommandData> mParam; // command specific parameter data |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 287 | }; |
| 288 | |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 289 | class AudioCommandData: public RefBase { |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 290 | public: |
| 291 | virtual ~AudioCommandData() {} |
| 292 | protected: |
| 293 | AudioCommandData() {} |
| 294 | }; |
| 295 | |
| 296 | class ToneData : public AudioCommandData { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 297 | public: |
Glenn Kasten | 3d2f877 | 2012-01-27 15:25:25 -0800 | [diff] [blame] | 298 | ToneGenerator::tone_type mType; // tone type (START_TONE only) |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 299 | audio_stream_type_t mStream; // stream type (START_TONE only) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 300 | }; |
| 301 | |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 302 | class VolumeData : public AudioCommandData { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 303 | public: |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 304 | audio_stream_type_t mStream; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 305 | float mVolume; |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 306 | audio_io_handle_t mIO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 307 | }; |
| 308 | |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 309 | class ParametersData : public AudioCommandData { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 310 | public: |
Glenn Kasten | 72ef00d | 2012-01-17 11:09:42 -0800 | [diff] [blame] | 311 | audio_io_handle_t mIO; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 312 | String8 mKeyValuePairs; |
| 313 | }; |
| 314 | |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 315 | class VoiceVolumeData : public AudioCommandData { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 316 | public: |
| 317 | float mVolume; |
| 318 | }; |
| 319 | |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 320 | class StopOutputData : public AudioCommandData { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 321 | public: |
| 322 | audio_io_handle_t mIO; |
| 323 | audio_stream_type_t mStream; |
| 324 | int mSession; |
| 325 | }; |
| 326 | |
Jesper Tragardh | 48412dc | 2014-03-24 14:12:43 +0100 | [diff] [blame] | 327 | class ReleaseOutputData : public AudioCommandData { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 328 | public: |
| 329 | audio_io_handle_t mIO; |
| 330 | }; |
| 331 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 332 | class CreateAudioPatchData : public AudioCommandData { |
| 333 | public: |
| 334 | struct audio_patch mPatch; |
| 335 | audio_patch_handle_t mHandle; |
| 336 | }; |
| 337 | |
| 338 | class ReleaseAudioPatchData : public AudioCommandData { |
| 339 | public: |
| 340 | audio_patch_handle_t mHandle; |
| 341 | }; |
| 342 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 343 | class SetAudioPortConfigData : public AudioCommandData { |
| 344 | public: |
| 345 | struct audio_port_config mConfig; |
| 346 | }; |
| 347 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 348 | Mutex mLock; |
| 349 | Condition mWaitWorkCV; |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 350 | Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 351 | ToneGenerator *mpToneGenerator; // the tone generator |
Eric Laurent | 0ede892 | 2014-05-09 18:04:42 -0700 | [diff] [blame] | 352 | sp<AudioCommand> mLastCommand; // last processed command (used by dump) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 353 | String8 mName; // string used by wake lock fo delayed commands |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 354 | wp<AudioPolicyService> mService; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 355 | }; |
| 356 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 357 | class AudioPolicyClient : public AudioPolicyClientInterface |
| 358 | { |
| 359 | public: |
| 360 | AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {} |
| 361 | virtual ~AudioPolicyClient() {} |
| 362 | |
| 363 | // |
| 364 | // Audio HW module functions |
| 365 | // |
| 366 | |
| 367 | // loads a HW module. |
| 368 | virtual audio_module_handle_t loadHwModule(const char *name); |
| 369 | |
| 370 | // |
| 371 | // Audio output Control functions |
| 372 | // |
| 373 | |
| 374 | // opens an audio output with the requested parameters. The parameter values can indicate to use the default values |
| 375 | // in case the audio policy manager has no specific requirements for the output being opened. |
| 376 | // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream. |
| 377 | // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly. |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 378 | virtual status_t openOutput(audio_module_handle_t module, |
| 379 | audio_io_handle_t *output, |
| 380 | audio_config_t *config, |
| 381 | audio_devices_t *devices, |
| 382 | const String8& address, |
| 383 | uint32_t *latencyMs, |
| 384 | audio_output_flags_t flags); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 385 | // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by |
| 386 | // a special mixer thread in the AudioFlinger. |
| 387 | virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2); |
| 388 | // closes the output stream |
| 389 | virtual status_t closeOutput(audio_io_handle_t output); |
| 390 | // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in |
| 391 | // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded. |
| 392 | virtual status_t suspendOutput(audio_io_handle_t output); |
| 393 | // restores a suspended output. |
| 394 | virtual status_t restoreOutput(audio_io_handle_t output); |
| 395 | |
| 396 | // |
| 397 | // Audio input Control functions |
| 398 | // |
| 399 | |
| 400 | // opens an audio input |
| 401 | virtual audio_io_handle_t openInput(audio_module_handle_t module, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 402 | audio_io_handle_t *input, |
| 403 | audio_config_t *config, |
| 404 | audio_devices_t *devices, |
| 405 | const String8& address, |
| 406 | audio_source_t source, |
Glenn Kasten | ec40d28 | 2014-07-15 15:31:26 -0700 | [diff] [blame] | 407 | audio_input_flags_t flags); |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 408 | // closes an audio input |
| 409 | virtual status_t closeInput(audio_io_handle_t input); |
| 410 | // |
| 411 | // misc control functions |
| 412 | // |
| 413 | |
| 414 | // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes |
| 415 | // for each output (destination device) it is attached to. |
| 416 | virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0); |
| 417 | |
| 418 | // invalidate a stream type, causing a reroute to an unspecified new output |
| 419 | virtual status_t invalidateStream(audio_stream_type_t stream); |
| 420 | |
| 421 | // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface. |
| 422 | virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0); |
| 423 | // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager. |
| 424 | virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys); |
| 425 | |
| 426 | // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing |
| 427 | // over a telephony device during a phone call. |
| 428 | virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream); |
| 429 | virtual status_t stopTone(); |
| 430 | |
| 431 | // set down link audio volume. |
| 432 | virtual status_t setVoiceVolume(float volume, int delayMs = 0); |
| 433 | |
| 434 | // move effect to the specified output |
| 435 | virtual status_t moveEffects(int session, |
| 436 | audio_io_handle_t srcOutput, |
| 437 | audio_io_handle_t dstOutput); |
| 438 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 439 | /* Create a patch between several source and sink ports */ |
| 440 | virtual status_t createAudioPatch(const struct audio_patch *patch, |
| 441 | audio_patch_handle_t *handle, |
| 442 | int delayMs); |
| 443 | |
| 444 | /* Release a patch */ |
| 445 | virtual status_t releaseAudioPatch(audio_patch_handle_t handle, |
| 446 | int delayMs); |
| 447 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 448 | /* Set audio port configuration */ |
| 449 | virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs); |
| 450 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 451 | virtual void onAudioPortListUpdate(); |
| 452 | virtual void onAudioPatchListUpdate(); |
| 453 | |
Eric Laurent | de3f839 | 2014-07-27 18:38:22 -0700 | [diff] [blame] | 454 | virtual audio_unique_id_t newAudioUniqueId(); |
| 455 | |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 456 | private: |
| 457 | AudioPolicyService *mAudioPolicyService; |
| 458 | }; |
| 459 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 460 | // --- Notification Client --- |
| 461 | class NotificationClient : public IBinder::DeathRecipient { |
| 462 | public: |
| 463 | NotificationClient(const sp<AudioPolicyService>& service, |
| 464 | const sp<IAudioPolicyServiceClient>& client, |
| 465 | uid_t uid); |
| 466 | virtual ~NotificationClient(); |
| 467 | |
| 468 | void onAudioPortListUpdate(); |
| 469 | void onAudioPatchListUpdate(); |
| 470 | |
| 471 | // IBinder::DeathRecipient |
| 472 | virtual void binderDied(const wp<IBinder>& who); |
| 473 | |
| 474 | private: |
| 475 | NotificationClient(const NotificationClient&); |
| 476 | NotificationClient& operator = (const NotificationClient&); |
| 477 | |
| 478 | const wp<AudioPolicyService> mService; |
| 479 | const uid_t mUid; |
| 480 | const sp<IAudioPolicyServiceClient> mAudioPolicyServiceClient; |
| 481 | }; |
| 482 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 483 | // Internal dump utilities. |
| 484 | status_t dumpPermissionDenial(int fd); |
| 485 | |
| 486 | |
Eric Laurent | eda6c36 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 487 | mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing |
| 488 | // device connection state or routing |
Glenn Kasten | e53b9ea | 2012-03-12 16:29:55 -0700 | [diff] [blame] | 489 | sp<AudioCommandThread> mAudioCommandThread; // audio commands thread |
| 490 | sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 491 | sp<AudioCommandThread> mOutputCommandThread; // process stop and release output |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 492 | struct audio_policy_device *mpAudioPolicyDev; |
| 493 | struct audio_policy *mpAudioPolicy; |
Eric Laurent | f269b8e | 2014-06-09 20:01:29 -0700 | [diff] [blame] | 494 | AudioPolicyInterface *mAudioPolicyManager; |
Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 495 | AudioPolicyClient *mAudioPolicyClient; |
| 496 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 497 | DefaultKeyedVector< uid_t, sp<NotificationClient> > mNotificationClients; |
bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 498 | |
| 499 | // Manage all effects configured in audio_effects.conf |
| 500 | sp<AudioPolicyEffects> mAudioPolicyEffects; |
Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 501 | audio_mode_t mPhoneState; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 502 | }; |
| 503 | |
| 504 | }; // namespace android |
| 505 | |
| 506 | #endif // ANDROID_AUDIOPOLICYSERVICE_H |