blob: a91c560abf6575e710df031b5c580aec599f7d95 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
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 Laurent7c7f10b2011-06-17 21:29:58 -070020#include <cutils/misc.h>
21#include <cutils/config_utils.h>
Mathias Agopiane762be92013-05-09 16:26:45 -070022#include <cutils/compiler.h>
Glenn Kastend1d8f232011-07-08 09:34:50 -070023#include <utils/String8.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024#include <utils/Vector.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070025#include <utils/SortedVector.h>
Glenn Kastend2dcb082011-02-03 16:55:26 -080026#include <binder/BinderService.h>
Dima Zavin64760242011-05-11 14:15:23 -070027#include <system/audio.h>
Dima Zavin7394a4f2011-06-13 18:16:26 -070028#include <system/audio_policy.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070029#include <hardware/audio_policy.h>
Eric Laurent7c7f10b2011-06-17 21:29:58 -070030#include <media/IAudioPolicyService.h>
31#include <media/ToneGenerator.h>
32#include <media/AudioEffect.h>
Eric Laurentbaac1832014-12-01 17:52:59 -080033#include <media/AudioPolicy.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080034#ifdef USE_LEGACY_AUDIO_POLICY
Eric Laurentdce54a12014-03-10 12:19:46 -070035#include <hardware_legacy/AudioPolicyInterface.h>
Chih-Hung Hsiehc84d9d22014-11-14 13:33:34 -080036#endif
bryant_liuba2b4392014-06-11 16:49:30 +080037#include "AudioPolicyEffects.h"
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080038#include "managerdefault/AudioPolicyManager.h"
Dima Zavinfce7a472011-04-19 22:30:36 -070039
bryant_liuba2b4392014-06-11 16:49:30 +080040
Mathias Agopian65ab4712010-07-14 17:59:35 -070041namespace android {
42
Mathias Agopian65ab4712010-07-14 17:59:35 -070043// ----------------------------------------------------------------------------
44
Glenn Kastend2dcb082011-02-03 16:55:26 -080045class AudioPolicyService :
46 public BinderService<AudioPolicyService>,
47 public BnAudioPolicyService,
Eric Laurentde070132010-07-13 04:45:46 -070048 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070049{
Glenn Kastend2dcb082011-02-03 16:55:26 -080050 friend class BinderService<AudioPolicyService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
52public:
Glenn Kastend2dcb082011-02-03 16:55:26 -080053 // for BinderService
Mathias Agopiane762be92013-05-09 16:26:45 -070054 static const char *getServiceName() ANDROID_API { return "media.audio_policy"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070055
56 virtual status_t dump(int fd, const Vector<String16>& args);
57
58 //
59 // BnAudioPolicyService (see AudioPolicyInterface for method descriptions)
60 //
61
Dima Zavinfce7a472011-04-19 22:30:36 -070062 virtual status_t setDeviceConnectionState(audio_devices_t device,
63 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080064 const char *device_address,
65 const char *device_name);
Dima Zavinfce7a472011-04-19 22:30:36 -070066 virtual audio_policy_dev_state_t getDeviceConnectionState(
67 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -070068 const char *device_address);
Glenn Kastenf78aee72012-01-04 11:00:47 -080069 virtual status_t setPhoneState(audio_mode_t state);
Dima Zavinfce7a472011-04-19 22:30:36 -070070 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
71 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
72 virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -070073 uint32_t samplingRate = 0,
Glenn Kasten58f30212012-01-12 12:27:51 -080074 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten254af182012-07-03 14:59:05 -070075 audio_channel_mask_t channelMask = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -070076 audio_output_flags_t flags =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +000077 AUDIO_OUTPUT_FLAG_NONE,
78 const audio_offload_info_t *offloadInfo = NULL);
Eric Laurente83b55d2014-11-14 10:06:21 -080079 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
80 audio_io_handle_t *output,
81 audio_session_t session,
82 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -070083 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -080084 uint32_t samplingRate = 0,
85 audio_format_t format = AUDIO_FORMAT_DEFAULT,
86 audio_channel_mask_t channelMask = 0,
87 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
Paul McLean466dc8e2015-04-17 13:15:36 -060088 audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE,
Eric Laurente83b55d2014-11-14 10:06:21 -080089 const audio_offload_info_t *offloadInfo = NULL);
Eric Laurentde070132010-07-13 04:45:46 -070090 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070091 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -080092 audio_session_t session);
Eric Laurentde070132010-07-13 04:45:46 -070093 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070094 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -080095 audio_session_t session);
96 virtual void releaseOutput(audio_io_handle_t output,
97 audio_stream_type_t stream,
98 audio_session_t session);
Eric Laurentcaf7f482014-11-25 17:50:47 -080099 virtual status_t getInputForAttr(const audio_attributes_t *attr,
100 audio_io_handle_t *input,
101 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700102 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800103 uint32_t samplingRate,
104 audio_format_t format,
105 audio_channel_mask_t channelMask,
Paul McLean466dc8e2015-04-17 13:15:36 -0600106 audio_input_flags_t flags,
107 audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE);
Eric Laurent4dc68062014-07-28 17:26:49 -0700108 virtual status_t startInput(audio_io_handle_t input,
109 audio_session_t session);
110 virtual status_t stopInput(audio_io_handle_t input,
111 audio_session_t session);
112 virtual void releaseInput(audio_io_handle_t input,
113 audio_session_t session);
Dima Zavinfce7a472011-04-19 22:30:36 -0700114 virtual status_t initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700115 int indexMin,
116 int indexMax);
Eric Laurent83844cc2011-11-18 16:43:31 -0800117 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
118 int index,
119 audio_devices_t device);
120 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
121 int *index,
122 audio_devices_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700123
Dima Zavinfce7a472011-04-19 22:30:36 -0700124 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Eric Laurent63742522012-03-08 13:42:42 -0800125 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurentde070132010-07-13 04:45:46 -0700126
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700127 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
128 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700129 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700130 uint32_t strategy,
131 int session,
132 int id);
133 virtual status_t unregisterEffect(int id);
Eric Laurentdb7c0792011-08-10 10:37:50 -0700134 virtual status_t setEffectEnabled(int id, bool enabled);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800135 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800136 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700137 virtual bool isSourceActive(audio_source_t source) const;
Eric Laurentde070132010-07-13 04:45:46 -0700138
Eric Laurent57dae992011-07-24 13:36:09 -0700139 virtual status_t queryDefaultPreProcessing(int audioSession,
140 effect_descriptor_t *descriptors,
141 uint32_t *count);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700142 virtual status_t onTransact(
143 uint32_t code,
144 const Parcel& data,
145 Parcel* reply,
146 uint32_t flags);
147
148 // IBinder::DeathRecipient
149 virtual void binderDied(const wp<IBinder>& who);
150
Eric Laurentf5ada6e2014-10-09 17:49:00 -0700151 // RefBase
152 virtual void onFirstRef();
153
Mathias Agopian65ab4712010-07-14 17:59:35 -0700154 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700155 // Helpers for the struct audio_policy_service_ops implementation.
156 // This is used by the audio policy manager for certain operations that
157 // are implemented by the policy service.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700159 virtual void setParameters(audio_io_handle_t ioHandle,
160 const char *keyValuePairs,
161 int delayMs);
162
163 virtual status_t setStreamVolume(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700164 float volume,
165 audio_io_handle_t output,
166 int delayMs = 0);
Dima Zavinfce7a472011-04-19 22:30:36 -0700167 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168 virtual status_t stopTone();
169 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000170 virtual bool isOffloadSupported(const audio_offload_info_t &config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700171
Eric Laurent203b1a12014-04-01 10:34:16 -0700172 virtual status_t listAudioPorts(audio_port_role_t role,
173 audio_port_type_t type,
174 unsigned int *num_ports,
175 struct audio_port *ports,
176 unsigned int *generation);
177 virtual status_t getAudioPort(struct audio_port *port);
178 virtual status_t createAudioPatch(const struct audio_patch *patch,
179 audio_patch_handle_t *handle);
180 virtual status_t releaseAudioPatch(audio_patch_handle_t handle);
181 virtual status_t listAudioPatches(unsigned int *num_patches,
182 struct audio_patch *patches,
183 unsigned int *generation);
184 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
185
Eric Laurentb52c1522014-05-20 11:27:36 -0700186 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client);
187
Eric Laurente8726fe2015-06-26 09:39:24 -0700188 virtual void setAudioPortCallbacksEnabled(bool enabled);
189
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700190 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
191 audio_io_handle_t *ioHandle,
192 audio_devices_t *device);
193
194 virtual status_t releaseSoundTriggerSession(audio_session_t session);
195
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700196 virtual audio_mode_t getPhoneState();
197
Eric Laurentbaac1832014-12-01 17:52:59 -0800198 virtual status_t registerPolicyMixes(Vector<AudioMix> mixes, bool registration);
199
Eric Laurent554a2772015-04-10 11:29:24 -0700200 virtual status_t startAudioSource(const struct audio_port_config *source,
201 const audio_attributes_t *attributes,
202 audio_io_handle_t *handle);
203 virtual status_t stopAudioSource(audio_io_handle_t handle);
204
Andy Hung2ddee192015-12-18 17:34:44 -0800205 virtual status_t setMasterMono(bool mono);
206 virtual status_t getMasterMono(bool *mono);
207
Eric Laurentbfb1b832013-01-07 09:53:42 -0800208 status_t doStopOutput(audio_io_handle_t output,
209 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800210 audio_session_t session);
211 void doReleaseOutput(audio_io_handle_t output,
212 audio_stream_type_t stream,
213 audio_session_t session);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800214
Eric Laurent951f4552014-05-20 10:48:17 -0700215 status_t clientCreateAudioPatch(const struct audio_patch *patch,
216 audio_patch_handle_t *handle,
217 int delayMs);
218 status_t clientReleaseAudioPatch(audio_patch_handle_t handle,
219 int delayMs);
Eric Laurente1715a42014-05-20 11:30:42 -0700220 virtual status_t clientSetAudioPortConfig(const struct audio_port_config *config,
221 int delayMs);
222
Eric Laurentb52c1522014-05-20 11:27:36 -0700223 void removeNotificationClient(uid_t uid);
224 void onAudioPortListUpdate();
225 void doOnAudioPortListUpdate();
226 void onAudioPatchListUpdate();
227 void doOnAudioPatchListUpdate();
Eric Laurent951f4552014-05-20 10:48:17 -0700228
Jean-Michel Trivide801052015-04-14 19:10:14 -0700229 void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state);
230 void doOnDynamicPolicyMixStateUpdate(String8 regId, int32_t state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800231 void onRecordingConfigurationUpdate(int event, audio_session_t session,
232 audio_source_t source);
233 void doOnRecordingConfigurationUpdate(int event, audio_session_t session,
234 audio_source_t source);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700235
Mathias Agopian65ab4712010-07-14 17:59:35 -0700236private:
Mathias Agopiane762be92013-05-09 16:26:45 -0700237 AudioPolicyService() ANDROID_API;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238 virtual ~AudioPolicyService();
239
240 status_t dumpInternals(int fd);
241
242 // Thread used for tone playback and to send audio config commands to audio flinger
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700243 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because
244 // startTone() and stopTone() are normally called with mLock locked and requesting a tone start
245 // or stop will cause calls to AudioPolicyService and an attempt to lock mLock.
246 // For audio config commands, it is necessary because audio flinger requires that the calling
247 // process (user) has permission to modify audio settings.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700248 class AudioCommandThread : public Thread {
249 class AudioCommand;
250 public:
251
252 // commands for tone AudioCommand
253 enum {
254 START_TONE,
255 STOP_TONE,
256 SET_VOLUME,
257 SET_PARAMETERS,
Eric Laurentbfb1b832013-01-07 09:53:42 -0800258 SET_VOICE_VOLUME,
259 STOP_OUTPUT,
Eric Laurent951f4552014-05-20 10:48:17 -0700260 RELEASE_OUTPUT,
261 CREATE_AUDIO_PATCH,
262 RELEASE_AUDIO_PATCH,
Eric Laurentb52c1522014-05-20 11:27:36 -0700263 UPDATE_AUDIOPORT_LIST,
Eric Laurente1715a42014-05-20 11:30:42 -0700264 UPDATE_AUDIOPATCH_LIST,
265 SET_AUDIOPORT_CONFIG,
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800266 DYN_POLICY_MIX_STATE_UPDATE,
267 RECORDING_CONFIGURATION_UPDATE
Mathias Agopian65ab4712010-07-14 17:59:35 -0700268 };
269
Eric Laurentbfb1b832013-01-07 09:53:42 -0800270 AudioCommandThread (String8 name, const wp<AudioPolicyService>& service);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700271 virtual ~AudioCommandThread();
272
273 status_t dump(int fd);
274
275 // Thread virtuals
276 virtual void onFirstRef();
277 virtual bool threadLoop();
278
279 void exit();
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800280 void startToneCommand(ToneGenerator::tone_type type,
281 audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282 void stopToneCommand();
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800283 status_t volumeCommand(audio_stream_type_t stream, float volume,
284 audio_io_handle_t output, int delayMs = 0);
285 status_t parametersCommand(audio_io_handle_t ioHandle,
286 const char *keyValuePairs, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287 status_t voiceVolumeCommand(float volume, int delayMs = 0);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800288 void stopOutputCommand(audio_io_handle_t output,
289 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800290 audio_session_t session);
291 void releaseOutputCommand(audio_io_handle_t output,
292 audio_stream_type_t stream,
293 audio_session_t session);
Eric Laurent0ede8922014-05-09 18:04:42 -0700294 status_t sendCommand(sp<AudioCommand>& command, int delayMs = 0);
295 void insertCommand_l(sp<AudioCommand>& command, int delayMs = 0);
Eric Laurent951f4552014-05-20 10:48:17 -0700296 status_t createAudioPatchCommand(const struct audio_patch *patch,
297 audio_patch_handle_t *handle,
298 int delayMs);
299 status_t releaseAudioPatchCommand(audio_patch_handle_t handle,
300 int delayMs);
Eric Laurentb52c1522014-05-20 11:27:36 -0700301 void updateAudioPortListCommand();
302 void updateAudioPatchListCommand();
Eric Laurente1715a42014-05-20 11:30:42 -0700303 status_t setAudioPortConfigCommand(const struct audio_port_config *config,
304 int delayMs);
Jean-Michel Trivide801052015-04-14 19:10:14 -0700305 void dynamicPolicyMixStateUpdateCommand(String8 regId, int32_t state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800306 void recordingConfigurationUpdateCommand(
307 int event, audio_session_t session,
308 audio_source_t source);
Eric Laurent951f4552014-05-20 10:48:17 -0700309 void insertCommand_l(AudioCommand *command, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310
311 private:
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100312 class AudioCommandData;
313
Mathias Agopian65ab4712010-07-14 17:59:35 -0700314 // descriptor for requested tone playback event
Eric Laurent0ede8922014-05-09 18:04:42 -0700315 class AudioCommand: public RefBase {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700316
317 public:
318 AudioCommand()
Eric Laurent0ede8922014-05-09 18:04:42 -0700319 : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320
321 void dump(char* buffer, size_t size);
322
323 int mCommand; // START_TONE, STOP_TONE ...
324 nsecs_t mTime; // time stamp
Eric Laurent0ede8922014-05-09 18:04:42 -0700325 Mutex mLock; // mutex associated to mCond
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326 Condition mCond; // condition for status return
327 status_t mStatus; // command status
328 bool mWaitStatus; // true if caller is waiting for status
Eric Laurent0ede8922014-05-09 18:04:42 -0700329 sp<AudioCommandData> mParam; // command specific parameter data
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330 };
331
Eric Laurent0ede8922014-05-09 18:04:42 -0700332 class AudioCommandData: public RefBase {
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100333 public:
334 virtual ~AudioCommandData() {}
335 protected:
336 AudioCommandData() {}
337 };
338
339 class ToneData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700340 public:
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800341 ToneGenerator::tone_type mType; // tone type (START_TONE only)
Glenn Kastenfff6d712012-01-12 16:38:12 -0800342 audio_stream_type_t mStream; // stream type (START_TONE only)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343 };
344
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100345 class VolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346 public:
Glenn Kastenfff6d712012-01-12 16:38:12 -0800347 audio_stream_type_t mStream;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 float mVolume;
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800349 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350 };
351
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100352 class ParametersData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 public:
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800354 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355 String8 mKeyValuePairs;
356 };
357
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100358 class VoiceVolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359 public:
360 float mVolume;
361 };
362
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100363 class StopOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800364 public:
365 audio_io_handle_t mIO;
366 audio_stream_type_t mStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800367 audio_session_t mSession;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800368 };
369
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100370 class ReleaseOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800371 public:
372 audio_io_handle_t mIO;
Eric Laurente83b55d2014-11-14 10:06:21 -0800373 audio_stream_type_t mStream;
374 audio_session_t mSession;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800375 };
376
Eric Laurent951f4552014-05-20 10:48:17 -0700377 class CreateAudioPatchData : public AudioCommandData {
378 public:
379 struct audio_patch mPatch;
380 audio_patch_handle_t mHandle;
381 };
382
383 class ReleaseAudioPatchData : public AudioCommandData {
384 public:
385 audio_patch_handle_t mHandle;
386 };
387
Eric Laurente1715a42014-05-20 11:30:42 -0700388 class SetAudioPortConfigData : public AudioCommandData {
389 public:
390 struct audio_port_config mConfig;
391 };
392
Jean-Michel Trivide801052015-04-14 19:10:14 -0700393 class DynPolicyMixStateUpdateData : public AudioCommandData {
394 public:
395 String8 mRegId;
396 int32_t mState;
397 };
398
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800399 class RecordingConfigurationUpdateData : public AudioCommandData {
400 public:
401 int mEvent;
402 audio_session_t mSession;
403 audio_source_t mSource;
404 };
405
Mathias Agopian65ab4712010-07-14 17:59:35 -0700406 Mutex mLock;
407 Condition mWaitWorkCV;
Eric Laurent0ede8922014-05-09 18:04:42 -0700408 Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409 ToneGenerator *mpToneGenerator; // the tone generator
Eric Laurent0ede8922014-05-09 18:04:42 -0700410 sp<AudioCommand> mLastCommand; // last processed command (used by dump)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411 String8 mName; // string used by wake lock fo delayed commands
Eric Laurentbfb1b832013-01-07 09:53:42 -0800412 wp<AudioPolicyService> mService;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700413 };
414
Eric Laurentdce54a12014-03-10 12:19:46 -0700415 class AudioPolicyClient : public AudioPolicyClientInterface
416 {
417 public:
418 AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {}
419 virtual ~AudioPolicyClient() {}
420
421 //
422 // Audio HW module functions
423 //
424
425 // loads a HW module.
426 virtual audio_module_handle_t loadHwModule(const char *name);
427
428 //
429 // Audio output Control functions
430 //
431
432 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
433 // in case the audio policy manager has no specific requirements for the output being opened.
434 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
435 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
Eric Laurentcf2c0212014-07-25 16:20:43 -0700436 virtual status_t openOutput(audio_module_handle_t module,
437 audio_io_handle_t *output,
438 audio_config_t *config,
439 audio_devices_t *devices,
440 const String8& address,
441 uint32_t *latencyMs,
442 audio_output_flags_t flags);
Eric Laurentdce54a12014-03-10 12:19:46 -0700443 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
444 // a special mixer thread in the AudioFlinger.
445 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2);
446 // closes the output stream
447 virtual status_t closeOutput(audio_io_handle_t output);
448 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
449 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
450 virtual status_t suspendOutput(audio_io_handle_t output);
451 // restores a suspended output.
452 virtual status_t restoreOutput(audio_io_handle_t output);
453
454 //
455 // Audio input Control functions
456 //
457
458 // opens an audio input
459 virtual audio_io_handle_t openInput(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700460 audio_io_handle_t *input,
461 audio_config_t *config,
462 audio_devices_t *devices,
463 const String8& address,
464 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -0700465 audio_input_flags_t flags);
Eric Laurentdce54a12014-03-10 12:19:46 -0700466 // closes an audio input
467 virtual status_t closeInput(audio_io_handle_t input);
468 //
469 // misc control functions
470 //
471
472 // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
473 // for each output (destination device) it is attached to.
474 virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0);
475
476 // invalidate a stream type, causing a reroute to an unspecified new output
477 virtual status_t invalidateStream(audio_stream_type_t stream);
478
479 // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
480 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0);
481 // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager.
482 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
483
484 // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing
485 // over a telephony device during a phone call.
486 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
487 virtual status_t stopTone();
488
489 // set down link audio volume.
490 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
491
492 // move effect to the specified output
493 virtual status_t moveEffects(int session,
494 audio_io_handle_t srcOutput,
495 audio_io_handle_t dstOutput);
496
Eric Laurent951f4552014-05-20 10:48:17 -0700497 /* Create a patch between several source and sink ports */
498 virtual status_t createAudioPatch(const struct audio_patch *patch,
499 audio_patch_handle_t *handle,
500 int delayMs);
501
502 /* Release a patch */
503 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
504 int delayMs);
505
Eric Laurente1715a42014-05-20 11:30:42 -0700506 /* Set audio port configuration */
507 virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs);
508
Eric Laurentb52c1522014-05-20 11:27:36 -0700509 virtual void onAudioPortListUpdate();
510 virtual void onAudioPatchListUpdate();
Jean-Michel Trivide801052015-04-14 19:10:14 -0700511 virtual void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800512 virtual void onRecordingConfigurationUpdate(int event,
513 audio_session_t session, audio_source_t source);
Eric Laurentb52c1522014-05-20 11:27:36 -0700514
Eric Laurentde3f8392014-07-27 18:38:22 -0700515 virtual audio_unique_id_t newAudioUniqueId();
516
Eric Laurentdce54a12014-03-10 12:19:46 -0700517 private:
518 AudioPolicyService *mAudioPolicyService;
519 };
520
Eric Laurentb52c1522014-05-20 11:27:36 -0700521 // --- Notification Client ---
522 class NotificationClient : public IBinder::DeathRecipient {
523 public:
524 NotificationClient(const sp<AudioPolicyService>& service,
525 const sp<IAudioPolicyServiceClient>& client,
526 uid_t uid);
527 virtual ~NotificationClient();
528
Jean-Michel Trivide801052015-04-14 19:10:14 -0700529 void onAudioPortListUpdate();
530 void onAudioPatchListUpdate();
531 void onDynamicPolicyMixStateUpdate(String8 regId, int32_t state);
Jean-Michel Trivi2f4fe9f2015-12-04 16:20:59 -0800532 void onRecordingConfigurationUpdate(
533 int event, audio_session_t session,
534 audio_source_t source);
Eric Laurente8726fe2015-06-26 09:39:24 -0700535 void setAudioPortCallbacksEnabled(bool enabled);
Eric Laurentb52c1522014-05-20 11:27:36 -0700536
537 // IBinder::DeathRecipient
538 virtual void binderDied(const wp<IBinder>& who);
539
540 private:
541 NotificationClient(const NotificationClient&);
542 NotificationClient& operator = (const NotificationClient&);
543
544 const wp<AudioPolicyService> mService;
545 const uid_t mUid;
546 const sp<IAudioPolicyServiceClient> mAudioPolicyServiceClient;
Eric Laurente8726fe2015-06-26 09:39:24 -0700547 bool mAudioPortCallbacksEnabled;
Eric Laurentb52c1522014-05-20 11:27:36 -0700548 };
549
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 // Internal dump utilities.
551 status_t dumpPermissionDenial(int fd);
552
553
Eric Laurenteda6c362011-02-02 09:33:30 -0800554 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
555 // device connection state or routing
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700556 mutable Mutex mEffectsLock; // serialize access to Effect state within APM.
557 // Note: lock acquisition order is always mLock > mEffectsLock:
558 // mLock protects AudioPolicyManager methods that can call into audio flinger
559 // and possibly back in to audio policy service and acquire mEffectsLock.
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700560 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread
561 sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -0800562 sp<AudioCommandThread> mOutputCommandThread; // process stop and release output
Dima Zavinfce7a472011-04-19 22:30:36 -0700563 struct audio_policy_device *mpAudioPolicyDev;
564 struct audio_policy *mpAudioPolicy;
Eric Laurentf269b8e2014-06-09 20:01:29 -0700565 AudioPolicyInterface *mAudioPolicyManager;
Eric Laurentdce54a12014-03-10 12:19:46 -0700566 AudioPolicyClient *mAudioPolicyClient;
567
Eric Laurentb52c1522014-05-20 11:27:36 -0700568 DefaultKeyedVector< uid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800569 Mutex mNotificationClientsLock; // protects mNotificationClients
bryant_liuba2b4392014-06-11 16:49:30 +0800570 // Manage all effects configured in audio_effects.conf
571 sp<AudioPolicyEffects> mAudioPolicyEffects;
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700572 audio_mode_t mPhoneState;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573};
574
575}; // namespace android
576
577#endif // ANDROID_AUDIOPOLICYSERVICE_H