blob: 80284a4487c9356bb017f7d0cfa0d8303d44c47d [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"
Eric Laurentdce54a12014-03-10 12:19:46 -070038#include "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,
Mathias Agopian65ab4712010-07-14 17:59:35 -070064 const char *device_address);
Dima Zavinfce7a472011-04-19 22:30:36 -070065 virtual audio_policy_dev_state_t getDeviceConnectionState(
66 audio_devices_t device,
Eric Laurentde070132010-07-13 04:45:46 -070067 const char *device_address);
Glenn Kastenf78aee72012-01-04 11:00:47 -080068 virtual status_t setPhoneState(audio_mode_t state);
Dima Zavinfce7a472011-04-19 22:30:36 -070069 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config);
70 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage);
71 virtual audio_io_handle_t getOutput(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -070072 uint32_t samplingRate = 0,
Glenn Kasten58f30212012-01-12 12:27:51 -080073 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten254af182012-07-03 14:59:05 -070074 audio_channel_mask_t channelMask = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -070075 audio_output_flags_t flags =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +000076 AUDIO_OUTPUT_FLAG_NONE,
77 const audio_offload_info_t *offloadInfo = NULL);
Eric Laurente83b55d2014-11-14 10:06:21 -080078 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
79 audio_io_handle_t *output,
80 audio_session_t session,
81 audio_stream_type_t *stream,
82 uint32_t samplingRate = 0,
83 audio_format_t format = AUDIO_FORMAT_DEFAULT,
84 audio_channel_mask_t channelMask = 0,
85 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
86 const audio_offload_info_t *offloadInfo = NULL);
Eric Laurentde070132010-07-13 04:45:46 -070087 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -070088 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -080089 audio_session_t session);
Eric Laurentde070132010-07-13 04:45:46 -070090 virtual status_t stopOutput(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);
93 virtual void releaseOutput(audio_io_handle_t output,
94 audio_stream_type_t stream,
95 audio_session_t session);
Eric Laurentcaf7f482014-11-25 17:50:47 -080096 virtual status_t getInputForAttr(const audio_attributes_t *attr,
97 audio_io_handle_t *input,
98 audio_session_t session,
99 uint32_t samplingRate,
100 audio_format_t format,
101 audio_channel_mask_t channelMask,
102 audio_input_flags_t flags);
Eric Laurent4dc68062014-07-28 17:26:49 -0700103 virtual status_t startInput(audio_io_handle_t input,
104 audio_session_t session);
105 virtual status_t stopInput(audio_io_handle_t input,
106 audio_session_t session);
107 virtual void releaseInput(audio_io_handle_t input,
108 audio_session_t session);
Dima Zavinfce7a472011-04-19 22:30:36 -0700109 virtual status_t initStreamVolume(audio_stream_type_t stream,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700110 int indexMin,
111 int indexMax);
Eric Laurent83844cc2011-11-18 16:43:31 -0800112 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
113 int index,
114 audio_devices_t device);
115 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
116 int *index,
117 audio_devices_t device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700118
Dima Zavinfce7a472011-04-19 22:30:36 -0700119 virtual uint32_t getStrategyForStream(audio_stream_type_t stream);
Eric Laurent63742522012-03-08 13:42:42 -0800120 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream);
Eric Laurentde070132010-07-13 04:45:46 -0700121
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700122 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc);
123 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700124 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700125 uint32_t strategy,
126 int session,
127 int id);
128 virtual status_t unregisterEffect(int id);
Eric Laurentdb7c0792011-08-10 10:37:50 -0700129 virtual status_t setEffectEnabled(int id, bool enabled);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800130 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800131 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs = 0) const;
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700132 virtual bool isSourceActive(audio_source_t source) const;
Eric Laurentde070132010-07-13 04:45:46 -0700133
Eric Laurent57dae992011-07-24 13:36:09 -0700134 virtual status_t queryDefaultPreProcessing(int audioSession,
135 effect_descriptor_t *descriptors,
136 uint32_t *count);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700137 virtual status_t onTransact(
138 uint32_t code,
139 const Parcel& data,
140 Parcel* reply,
141 uint32_t flags);
142
143 // IBinder::DeathRecipient
144 virtual void binderDied(const wp<IBinder>& who);
145
Eric Laurentf5ada6e2014-10-09 17:49:00 -0700146 // RefBase
147 virtual void onFirstRef();
148
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700150 // Helpers for the struct audio_policy_service_ops implementation.
151 // This is used by the audio policy manager for certain operations that
152 // are implemented by the policy service.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700153 //
Dima Zavinfce7a472011-04-19 22:30:36 -0700154 virtual void setParameters(audio_io_handle_t ioHandle,
155 const char *keyValuePairs,
156 int delayMs);
157
158 virtual status_t setStreamVolume(audio_stream_type_t stream,
Eric Laurentde070132010-07-13 04:45:46 -0700159 float volume,
160 audio_io_handle_t output,
161 int delayMs = 0);
Dima Zavinfce7a472011-04-19 22:30:36 -0700162 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700163 virtual status_t stopTone();
164 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000165 virtual bool isOffloadSupported(const audio_offload_info_t &config);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700166
Eric Laurent203b1a12014-04-01 10:34:16 -0700167 virtual status_t listAudioPorts(audio_port_role_t role,
168 audio_port_type_t type,
169 unsigned int *num_ports,
170 struct audio_port *ports,
171 unsigned int *generation);
172 virtual status_t getAudioPort(struct audio_port *port);
173 virtual status_t createAudioPatch(const struct audio_patch *patch,
174 audio_patch_handle_t *handle);
175 virtual status_t releaseAudioPatch(audio_patch_handle_t handle);
176 virtual status_t listAudioPatches(unsigned int *num_patches,
177 struct audio_patch *patches,
178 unsigned int *generation);
179 virtual status_t setAudioPortConfig(const struct audio_port_config *config);
180
Eric Laurentb52c1522014-05-20 11:27:36 -0700181 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client);
182
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700183 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
184 audio_io_handle_t *ioHandle,
185 audio_devices_t *device);
186
187 virtual status_t releaseSoundTriggerSession(audio_session_t session);
188
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700189 virtual audio_mode_t getPhoneState();
190
Eric Laurentbaac1832014-12-01 17:52:59 -0800191 virtual status_t registerPolicyMixes(Vector<AudioMix> mixes, bool registration);
192
Eric Laurentbfb1b832013-01-07 09:53:42 -0800193 status_t doStopOutput(audio_io_handle_t output,
194 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800195 audio_session_t session);
196 void doReleaseOutput(audio_io_handle_t output,
197 audio_stream_type_t stream,
198 audio_session_t session);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800199
Eric Laurent951f4552014-05-20 10:48:17 -0700200 status_t clientCreateAudioPatch(const struct audio_patch *patch,
201 audio_patch_handle_t *handle,
202 int delayMs);
203 status_t clientReleaseAudioPatch(audio_patch_handle_t handle,
204 int delayMs);
Eric Laurente1715a42014-05-20 11:30:42 -0700205 virtual status_t clientSetAudioPortConfig(const struct audio_port_config *config,
206 int delayMs);
207
Eric Laurentb52c1522014-05-20 11:27:36 -0700208 void removeNotificationClient(uid_t uid);
209 void onAudioPortListUpdate();
210 void doOnAudioPortListUpdate();
211 void onAudioPatchListUpdate();
212 void doOnAudioPatchListUpdate();
Eric Laurent951f4552014-05-20 10:48:17 -0700213
Mathias Agopian65ab4712010-07-14 17:59:35 -0700214private:
Mathias Agopiane762be92013-05-09 16:26:45 -0700215 AudioPolicyService() ANDROID_API;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700216 virtual ~AudioPolicyService();
217
218 status_t dumpInternals(int fd);
219
220 // Thread used for tone playback and to send audio config commands to audio flinger
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700221 // For tone playback, using a separate thread is necessary to avoid deadlock with mLock because
222 // startTone() and stopTone() are normally called with mLock locked and requesting a tone start
223 // or stop will cause calls to AudioPolicyService and an attempt to lock mLock.
224 // For audio config commands, it is necessary because audio flinger requires that the calling
225 // process (user) has permission to modify audio settings.
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226 class AudioCommandThread : public Thread {
227 class AudioCommand;
228 public:
229
230 // commands for tone AudioCommand
231 enum {
232 START_TONE,
233 STOP_TONE,
234 SET_VOLUME,
235 SET_PARAMETERS,
Eric Laurentbfb1b832013-01-07 09:53:42 -0800236 SET_VOICE_VOLUME,
237 STOP_OUTPUT,
Eric Laurent951f4552014-05-20 10:48:17 -0700238 RELEASE_OUTPUT,
239 CREATE_AUDIO_PATCH,
240 RELEASE_AUDIO_PATCH,
Eric Laurentb52c1522014-05-20 11:27:36 -0700241 UPDATE_AUDIOPORT_LIST,
Eric Laurente1715a42014-05-20 11:30:42 -0700242 UPDATE_AUDIOPATCH_LIST,
243 SET_AUDIOPORT_CONFIG,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244 };
245
Eric Laurentbfb1b832013-01-07 09:53:42 -0800246 AudioCommandThread (String8 name, const wp<AudioPolicyService>& service);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700247 virtual ~AudioCommandThread();
248
249 status_t dump(int fd);
250
251 // Thread virtuals
252 virtual void onFirstRef();
253 virtual bool threadLoop();
254
255 void exit();
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800256 void startToneCommand(ToneGenerator::tone_type type,
257 audio_stream_type_t stream);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700258 void stopToneCommand();
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800259 status_t volumeCommand(audio_stream_type_t stream, float volume,
260 audio_io_handle_t output, int delayMs = 0);
261 status_t parametersCommand(audio_io_handle_t ioHandle,
262 const char *keyValuePairs, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263 status_t voiceVolumeCommand(float volume, int delayMs = 0);
Eric Laurentbfb1b832013-01-07 09:53:42 -0800264 void stopOutputCommand(audio_io_handle_t output,
265 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800266 audio_session_t session);
267 void releaseOutputCommand(audio_io_handle_t output,
268 audio_stream_type_t stream,
269 audio_session_t session);
Eric Laurent0ede8922014-05-09 18:04:42 -0700270 status_t sendCommand(sp<AudioCommand>& command, int delayMs = 0);
271 void insertCommand_l(sp<AudioCommand>& command, int delayMs = 0);
Eric Laurent951f4552014-05-20 10:48:17 -0700272 status_t createAudioPatchCommand(const struct audio_patch *patch,
273 audio_patch_handle_t *handle,
274 int delayMs);
275 status_t releaseAudioPatchCommand(audio_patch_handle_t handle,
276 int delayMs);
Eric Laurentb52c1522014-05-20 11:27:36 -0700277 void updateAudioPortListCommand();
278 void updateAudioPatchListCommand();
Eric Laurente1715a42014-05-20 11:30:42 -0700279 status_t setAudioPortConfigCommand(const struct audio_port_config *config,
280 int delayMs);
Eric Laurent951f4552014-05-20 10:48:17 -0700281 void insertCommand_l(AudioCommand *command, int delayMs = 0);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282
283 private:
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100284 class AudioCommandData;
285
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286 // descriptor for requested tone playback event
Eric Laurent0ede8922014-05-09 18:04:42 -0700287 class AudioCommand: public RefBase {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700288
289 public:
290 AudioCommand()
Eric Laurent0ede8922014-05-09 18:04:42 -0700291 : mCommand(-1), mStatus(NO_ERROR), mWaitStatus(false) {}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292
293 void dump(char* buffer, size_t size);
294
295 int mCommand; // START_TONE, STOP_TONE ...
296 nsecs_t mTime; // time stamp
Eric Laurent0ede8922014-05-09 18:04:42 -0700297 Mutex mLock; // mutex associated to mCond
Mathias Agopian65ab4712010-07-14 17:59:35 -0700298 Condition mCond; // condition for status return
299 status_t mStatus; // command status
300 bool mWaitStatus; // true if caller is waiting for status
Eric Laurent0ede8922014-05-09 18:04:42 -0700301 sp<AudioCommandData> mParam; // command specific parameter data
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302 };
303
Eric Laurent0ede8922014-05-09 18:04:42 -0700304 class AudioCommandData: public RefBase {
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100305 public:
306 virtual ~AudioCommandData() {}
307 protected:
308 AudioCommandData() {}
309 };
310
311 class ToneData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312 public:
Glenn Kasten3d2f8772012-01-27 15:25:25 -0800313 ToneGenerator::tone_type mType; // tone type (START_TONE only)
Glenn Kastenfff6d712012-01-12 16:38:12 -0800314 audio_stream_type_t mStream; // stream type (START_TONE only)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315 };
316
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100317 class VolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700318 public:
Glenn Kastenfff6d712012-01-12 16:38:12 -0800319 audio_stream_type_t mStream;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 float mVolume;
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800321 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700322 };
323
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100324 class ParametersData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325 public:
Glenn Kasten72ef00d2012-01-17 11:09:42 -0800326 audio_io_handle_t mIO;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327 String8 mKeyValuePairs;
328 };
329
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100330 class VoiceVolumeData : public AudioCommandData {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700331 public:
332 float mVolume;
333 };
334
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100335 class StopOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800336 public:
337 audio_io_handle_t mIO;
338 audio_stream_type_t mStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800339 audio_session_t mSession;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800340 };
341
Jesper Tragardh48412dc2014-03-24 14:12:43 +0100342 class ReleaseOutputData : public AudioCommandData {
Eric Laurentbfb1b832013-01-07 09:53:42 -0800343 public:
344 audio_io_handle_t mIO;
Eric Laurente83b55d2014-11-14 10:06:21 -0800345 audio_stream_type_t mStream;
346 audio_session_t mSession;
Eric Laurentbfb1b832013-01-07 09:53:42 -0800347 };
348
Eric Laurent951f4552014-05-20 10:48:17 -0700349 class CreateAudioPatchData : public AudioCommandData {
350 public:
351 struct audio_patch mPatch;
352 audio_patch_handle_t mHandle;
353 };
354
355 class ReleaseAudioPatchData : public AudioCommandData {
356 public:
357 audio_patch_handle_t mHandle;
358 };
359
Eric Laurente1715a42014-05-20 11:30:42 -0700360 class SetAudioPortConfigData : public AudioCommandData {
361 public:
362 struct audio_port_config mConfig;
363 };
364
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365 Mutex mLock;
366 Condition mWaitWorkCV;
Eric Laurent0ede8922014-05-09 18:04:42 -0700367 Vector < sp<AudioCommand> > mAudioCommands; // list of pending commands
Mathias Agopian65ab4712010-07-14 17:59:35 -0700368 ToneGenerator *mpToneGenerator; // the tone generator
Eric Laurent0ede8922014-05-09 18:04:42 -0700369 sp<AudioCommand> mLastCommand; // last processed command (used by dump)
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 String8 mName; // string used by wake lock fo delayed commands
Eric Laurentbfb1b832013-01-07 09:53:42 -0800371 wp<AudioPolicyService> mService;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700372 };
373
Eric Laurentdce54a12014-03-10 12:19:46 -0700374 class AudioPolicyClient : public AudioPolicyClientInterface
375 {
376 public:
377 AudioPolicyClient(AudioPolicyService *service) : mAudioPolicyService(service) {}
378 virtual ~AudioPolicyClient() {}
379
380 //
381 // Audio HW module functions
382 //
383
384 // loads a HW module.
385 virtual audio_module_handle_t loadHwModule(const char *name);
386
387 //
388 // Audio output Control functions
389 //
390
391 // opens an audio output with the requested parameters. The parameter values can indicate to use the default values
392 // in case the audio policy manager has no specific requirements for the output being opened.
393 // When the function returns, the parameter values reflect the actual values used by the audio hardware output stream.
394 // The audio policy manager can check if the proposed parameters are suitable or not and act accordingly.
Eric Laurentcf2c0212014-07-25 16:20:43 -0700395 virtual status_t openOutput(audio_module_handle_t module,
396 audio_io_handle_t *output,
397 audio_config_t *config,
398 audio_devices_t *devices,
399 const String8& address,
400 uint32_t *latencyMs,
401 audio_output_flags_t flags);
Eric Laurentdce54a12014-03-10 12:19:46 -0700402 // creates a special output that is duplicated to the two outputs passed as arguments. The duplication is performed by
403 // a special mixer thread in the AudioFlinger.
404 virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1, audio_io_handle_t output2);
405 // closes the output stream
406 virtual status_t closeOutput(audio_io_handle_t output);
407 // suspends the output. When an output is suspended, the corresponding audio hardware output stream is placed in
408 // standby and the AudioTracks attached to the mixer thread are still processed but the output mix is discarded.
409 virtual status_t suspendOutput(audio_io_handle_t output);
410 // restores a suspended output.
411 virtual status_t restoreOutput(audio_io_handle_t output);
412
413 //
414 // Audio input Control functions
415 //
416
417 // opens an audio input
418 virtual audio_io_handle_t openInput(audio_module_handle_t module,
Eric Laurentcf2c0212014-07-25 16:20:43 -0700419 audio_io_handle_t *input,
420 audio_config_t *config,
421 audio_devices_t *devices,
422 const String8& address,
423 audio_source_t source,
Glenn Kastenec40d282014-07-15 15:31:26 -0700424 audio_input_flags_t flags);
Eric Laurentdce54a12014-03-10 12:19:46 -0700425 // closes an audio input
426 virtual status_t closeInput(audio_io_handle_t input);
427 //
428 // misc control functions
429 //
430
431 // set a stream volume for a particular output. For the same user setting, a given stream type can have different volumes
432 // for each output (destination device) it is attached to.
433 virtual status_t setStreamVolume(audio_stream_type_t stream, float volume, audio_io_handle_t output, int delayMs = 0);
434
435 // invalidate a stream type, causing a reroute to an unspecified new output
436 virtual status_t invalidateStream(audio_stream_type_t stream);
437
438 // function enabling to send proprietary informations directly from audio policy manager to audio hardware interface.
439 virtual void setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs, int delayMs = 0);
440 // function enabling to receive proprietary informations directly from audio hardware interface to audio policy manager.
441 virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys);
442
443 // request the playback of a tone on the specified stream: used for instance to replace notification sounds when playing
444 // over a telephony device during a phone call.
445 virtual status_t startTone(audio_policy_tone_t tone, audio_stream_type_t stream);
446 virtual status_t stopTone();
447
448 // set down link audio volume.
449 virtual status_t setVoiceVolume(float volume, int delayMs = 0);
450
451 // move effect to the specified output
452 virtual status_t moveEffects(int session,
453 audio_io_handle_t srcOutput,
454 audio_io_handle_t dstOutput);
455
Eric Laurent951f4552014-05-20 10:48:17 -0700456 /* Create a patch between several source and sink ports */
457 virtual status_t createAudioPatch(const struct audio_patch *patch,
458 audio_patch_handle_t *handle,
459 int delayMs);
460
461 /* Release a patch */
462 virtual status_t releaseAudioPatch(audio_patch_handle_t handle,
463 int delayMs);
464
Eric Laurente1715a42014-05-20 11:30:42 -0700465 /* Set audio port configuration */
466 virtual status_t setAudioPortConfig(const struct audio_port_config *config, int delayMs);
467
Eric Laurentb52c1522014-05-20 11:27:36 -0700468 virtual void onAudioPortListUpdate();
469 virtual void onAudioPatchListUpdate();
470
Eric Laurentde3f8392014-07-27 18:38:22 -0700471 virtual audio_unique_id_t newAudioUniqueId();
472
Eric Laurentdce54a12014-03-10 12:19:46 -0700473 private:
474 AudioPolicyService *mAudioPolicyService;
475 };
476
Eric Laurentb52c1522014-05-20 11:27:36 -0700477 // --- Notification Client ---
478 class NotificationClient : public IBinder::DeathRecipient {
479 public:
480 NotificationClient(const sp<AudioPolicyService>& service,
481 const sp<IAudioPolicyServiceClient>& client,
482 uid_t uid);
483 virtual ~NotificationClient();
484
485 void onAudioPortListUpdate();
486 void onAudioPatchListUpdate();
487
488 // IBinder::DeathRecipient
489 virtual void binderDied(const wp<IBinder>& who);
490
491 private:
492 NotificationClient(const NotificationClient&);
493 NotificationClient& operator = (const NotificationClient&);
494
495 const wp<AudioPolicyService> mService;
496 const uid_t mUid;
497 const sp<IAudioPolicyServiceClient> mAudioPolicyServiceClient;
498 };
499
Mathias Agopian65ab4712010-07-14 17:59:35 -0700500 // Internal dump utilities.
501 status_t dumpPermissionDenial(int fd);
502
503
Eric Laurenteda6c362011-02-02 09:33:30 -0800504 mutable Mutex mLock; // prevents concurrent access to AudioPolicy manager functions changing
505 // device connection state or routing
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700506 sp<AudioCommandThread> mAudioCommandThread; // audio commands thread
507 sp<AudioCommandThread> mTonePlaybackThread; // tone playback thread
Eric Laurentbfb1b832013-01-07 09:53:42 -0800508 sp<AudioCommandThread> mOutputCommandThread; // process stop and release output
Dima Zavinfce7a472011-04-19 22:30:36 -0700509 struct audio_policy_device *mpAudioPolicyDev;
510 struct audio_policy *mpAudioPolicy;
Eric Laurentf269b8e2014-06-09 20:01:29 -0700511 AudioPolicyInterface *mAudioPolicyManager;
Eric Laurentdce54a12014-03-10 12:19:46 -0700512 AudioPolicyClient *mAudioPolicyClient;
513
Eric Laurentb52c1522014-05-20 11:27:36 -0700514 DefaultKeyedVector< uid_t, sp<NotificationClient> > mNotificationClients;
Eric Laurent0ebd5f92014-11-19 19:04:52 -0800515 Mutex mNotificationClientsLock; // protects mNotificationClients
bryant_liuba2b4392014-06-11 16:49:30 +0800516 // Manage all effects configured in audio_effects.conf
517 sp<AudioPolicyEffects> mAudioPolicyEffects;
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700518 audio_mode_t mPhoneState;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519};
520
521}; // namespace android
522
523#endif // ANDROID_AUDIOPOLICYSERVICE_H