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