blob: feb131764eb9ab11cfa3448d096891da2604ba63 [file] [log] [blame]
Eric Laurentc2f1f072009-07-17 12:17:14 -07001/*
2**
3** Copyright 2009, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "IAudioPolicyService"
19#include <utils/Log.h>
20
21#include <stdint.h>
Eric Laurentac9cef52017-06-09 15:46:26 -070022#include <math.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070023#include <sys/types.h>
24
Eric Laurentb1cc36b2017-12-11 12:14:16 -080025#include <binder/IPCThreadState.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070026#include <binder/Parcel.h>
Eric Laurent74adca92014-11-05 12:15:36 -080027#include <media/AudioEffect.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070028#include <media/IAudioPolicyService.h>
Eric Laurent3528c932018-02-23 17:17:22 -080029#include <media/TimeCheck.h>
Andy Hung4ef19fa2018-05-15 19:35:29 -070030#include <mediautils/ServiceUtilities.h>
Dima Zavin64760242011-05-11 14:15:23 -070031#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070032
Eric Laurentc2f1f072009-07-17 12:17:14 -070033namespace android {
34
35enum {
36 SET_DEVICE_CONNECTION_STATE = IBinder::FIRST_CALL_TRANSACTION,
37 GET_DEVICE_CONNECTION_STATE,
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -080038 HANDLE_DEVICE_CONFIG_CHANGE,
Eric Laurentc2f1f072009-07-17 12:17:14 -070039 SET_PHONE_STATE,
Glenn Kasten0b07b802012-01-18 14:56:06 -080040 SET_RINGER_MODE, // reserved, no longer used
Eric Laurentc2f1f072009-07-17 12:17:14 -070041 SET_FORCE_USE,
42 GET_FORCE_USE,
43 GET_OUTPUT,
44 START_OUTPUT,
45 STOP_OUTPUT,
46 RELEASE_OUTPUT,
Eric Laurentcaf7f482014-11-25 17:50:47 -080047 GET_INPUT_FOR_ATTR,
Eric Laurentc2f1f072009-07-17 12:17:14 -070048 START_INPUT,
49 STOP_INPUT,
50 RELEASE_INPUT,
51 INIT_STREAM_VOLUME,
52 SET_STREAM_VOLUME,
Eric Laurentde070132010-07-13 04:45:46 -070053 GET_STREAM_VOLUME,
54 GET_STRATEGY_FOR_STREAM,
55 GET_OUTPUT_FOR_EFFECT,
56 REGISTER_EFFECT,
Eric Laurenteda6c362011-02-02 09:33:30 -080057 UNREGISTER_EFFECT,
Glenn Kasten6b2718c2011-02-04 13:54:26 -080058 IS_STREAM_ACTIVE,
Jean-Michel Trivid7086032012-10-10 12:11:16 -070059 IS_SOURCE_ACTIVE,
Glenn Kasten6b2718c2011-02-04 13:54:26 -080060 GET_DEVICES_FOR_STREAM,
Eric Laurentdb7c0792011-08-10 10:37:50 -070061 QUERY_DEFAULT_PRE_PROCESSING,
Jean-Michel Trivi272ab542013-02-04 16:26:02 -080062 SET_EFFECT_ENABLED,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +000063 IS_STREAM_ACTIVE_REMOTELY,
Eric Laurent203b1a12014-04-01 10:34:16 -070064 IS_OFFLOAD_SUPPORTED,
Michael Chana94fbb22018-04-24 14:31:19 +100065 IS_DIRECT_OUTPUT_SUPPORTED,
Eric Laurent203b1a12014-04-01 10:34:16 -070066 LIST_AUDIO_PORTS,
67 GET_AUDIO_PORT,
68 CREATE_AUDIO_PATCH,
69 RELEASE_AUDIO_PATCH,
70 LIST_AUDIO_PATCHES,
Eric Laurentb52c1522014-05-20 11:27:36 -070071 SET_AUDIO_PORT_CONFIG,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -070072 REGISTER_CLIENT,
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070073 GET_OUTPUT_FOR_ATTR,
74 ACQUIRE_SOUNDTRIGGER_SESSION,
Eric Laurentbb6c9a02014-09-25 14:11:47 -070075 RELEASE_SOUNDTRIGGER_SESSION,
Eric Laurentbaac1832014-12-01 17:52:59 -080076 GET_PHONE_STATE,
77 REGISTER_POLICY_MIXES,
Eric Laurent554a2772015-04-10 11:29:24 -070078 START_AUDIO_SOURCE,
Eric Laurente8726fe2015-06-26 09:39:24 -070079 STOP_AUDIO_SOURCE,
80 SET_AUDIO_PORT_CALLBACK_ENABLED,
Andy Hung2ddee192015-12-18 17:34:44 -080081 SET_MASTER_MONO,
82 GET_MASTER_MONO,
jiabin81772902018-04-02 17:52:27 -070083 GET_STREAM_VOLUME_DB,
84 GET_SURROUND_FORMATS,
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -070085 SET_SURROUND_FORMAT_ENABLED,
86 ADD_STREAM_DEFAULT_EFFECT,
Ari Hausman-Cohen24628312018-08-13 15:01:09 -070087 REMOVE_STREAM_DEFAULT_EFFECT,
88 ADD_SOURCE_DEFAULT_EFFECT,
Eric Laurentb78763e2018-10-17 10:08:02 -070089 REMOVE_SOURCE_DEFAULT_EFFECT,
90 SET_ASSISTANT_UID,
91 SET_A11Y_SERVICES_UIDS,
jiabin6012f912018-11-02 17:06:30 -070092 IS_HAPTIC_PLAYBACK_SUPPORTED,
Jean-Michel Trivibda70da2018-12-19 07:30:15 -080093 SET_UID_DEVICE_AFFINITY,
94 REMOVE_UID_DEVICE_AFFINITY,
François Gaffied0ba9ed2018-11-05 11:50:42 +010095 GET_OFFLOAD_FORMATS_A2DP,
96 LIST_AUDIO_PRODUCT_STRATEGIES,
97 GET_STRATEGY_FOR_ATTRIBUTES,
François Gaffie4b2018b2018-11-07 11:18:59 +010098 LIST_AUDIO_VOLUME_GROUPS,
99 GET_VOLUME_GROUP_FOR_ATTRIBUTES
Eric Laurentc2f1f072009-07-17 12:17:14 -0700100};
101
Eric Laurent1d670b12015-02-06 10:44:24 -0800102#define MAX_ITEMS_PER_LIST 1024
103
Eric Laurentc2f1f072009-07-17 12:17:14 -0700104class BpAudioPolicyService : public BpInterface<IAudioPolicyService>
105{
106public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700107 explicit BpAudioPolicyService(const sp<IBinder>& impl)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700108 : BpInterface<IAudioPolicyService>(impl)
109 {
110 }
111
112 virtual status_t setDeviceConnectionState(
Dima Zavinfce7a472011-04-19 22:30:36 -0700113 audio_devices_t device,
114 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800115 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800116 const char *device_name,
117 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700118 {
119 Parcel data, reply;
120 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
121 data.writeInt32(static_cast <uint32_t>(device));
122 data.writeInt32(static_cast <uint32_t>(state));
123 data.writeCString(device_address);
Paul McLeane743a472015-01-28 11:07:31 -0800124 data.writeCString(device_name);
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800125 data.writeInt32(static_cast <uint32_t>(encodedFormat));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700126 remote()->transact(SET_DEVICE_CONNECTION_STATE, data, &reply);
127 return static_cast <status_t> (reply.readInt32());
128 }
129
Dima Zavinfce7a472011-04-19 22:30:36 -0700130 virtual audio_policy_dev_state_t getDeviceConnectionState(
131 audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700132 const char *device_address)
133 {
134 Parcel data, reply;
135 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
136 data.writeInt32(static_cast <uint32_t>(device));
137 data.writeCString(device_address);
138 remote()->transact(GET_DEVICE_CONNECTION_STATE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700139 return static_cast <audio_policy_dev_state_t>(reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700140 }
141
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800142 virtual status_t handleDeviceConfigChange(audio_devices_t device,
143 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800144 const char *device_name,
145 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800146 {
147 Parcel data, reply;
148 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
149 data.writeInt32(static_cast <uint32_t>(device));
150 data.writeCString(device_address);
151 data.writeCString(device_name);
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800152 data.writeInt32(static_cast <uint32_t>(encodedFormat));
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800153 remote()->transact(HANDLE_DEVICE_CONFIG_CHANGE, data, &reply);
154 return static_cast <status_t> (reply.readInt32());
155 }
156
Glenn Kastenf78aee72012-01-04 11:00:47 -0800157 virtual status_t setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700158 {
159 Parcel data, reply;
160 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
161 data.writeInt32(state);
162 remote()->transact(SET_PHONE_STATE, data, &reply);
163 return static_cast <status_t> (reply.readInt32());
164 }
165
Dima Zavinfce7a472011-04-19 22:30:36 -0700166 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700167 {
168 Parcel data, reply;
169 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
170 data.writeInt32(static_cast <uint32_t>(usage));
171 data.writeInt32(static_cast <uint32_t>(config));
172 remote()->transact(SET_FORCE_USE, data, &reply);
173 return static_cast <status_t> (reply.readInt32());
174 }
175
Dima Zavinfce7a472011-04-19 22:30:36 -0700176 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700177 {
178 Parcel data, reply;
179 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
180 data.writeInt32(static_cast <uint32_t>(usage));
181 remote()->transact(GET_FORCE_USE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700182 return static_cast <audio_policy_forced_cfg_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700183 }
184
Eric Laurentf4e63452017-11-06 19:31:46 +0000185 virtual audio_io_handle_t getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700186 {
187 Parcel data, reply;
188 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
189 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700190 remote()->transact(GET_OUTPUT, data, &reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -0700191 return static_cast <audio_io_handle_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700192 }
193
Kevin Rocard153f92d2018-12-18 18:33:28 -0800194 status_t getOutputForAttr(const audio_attributes_t *attr,
195 audio_io_handle_t *output,
196 audio_session_t session,
197 audio_stream_type_t *stream,
198 pid_t pid,
199 uid_t uid,
200 const audio_config_t *config,
201 audio_output_flags_t flags,
202 audio_port_handle_t *selectedDeviceId,
203 audio_port_handle_t *portId,
204 std::vector<audio_io_handle_t> *secondaryOutputs) override
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700205 {
206 Parcel data, reply;
207 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
208 if (attr == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800209 if (stream == NULL) {
210 ALOGE("getOutputForAttr(): NULL audio attributes and stream type");
211 return BAD_VALUE;
212 }
213 if (*stream == AUDIO_STREAM_DEFAULT) {
214 ALOGE("getOutputForAttr unspecified stream type");
215 return BAD_VALUE;
216 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700217 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800218 if (output == NULL) {
219 ALOGE("getOutputForAttr NULL output - shouldn't happen");
220 return BAD_VALUE;
221 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700222 if (selectedDeviceId == NULL) {
223 ALOGE("getOutputForAttr NULL selectedDeviceId - shouldn't happen");
224 return BAD_VALUE;
225 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800226 if (portId == NULL) {
227 ALOGE("getOutputForAttr NULL portId - shouldn't happen");
228 return BAD_VALUE;
229 }
Kevin Rocard153f92d2018-12-18 18:33:28 -0800230 if (secondaryOutputs == NULL) {
231 ALOGE("getOutputForAttr NULL secondaryOutputs - shouldn't happen");
232 return BAD_VALUE;
233 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800234 if (attr == NULL) {
235 data.writeInt32(0);
236 } else {
237 data.writeInt32(1);
238 data.write(attr, sizeof(audio_attributes_t));
239 }
240 data.writeInt32(session);
241 if (stream == NULL) {
242 data.writeInt32(0);
243 } else {
244 data.writeInt32(1);
245 data.writeInt32(*stream);
246 }
Nadav Bar766fb022018-01-07 12:18:03 +0200247 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700248 data.writeInt32(uid);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800249 data.write(config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700250 data.writeInt32(static_cast <uint32_t>(flags));
Eric Laurent9ae8c592017-06-22 17:17:09 -0700251 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800252 data.writeInt32(*portId);
Eric Laurente83b55d2014-11-14 10:06:21 -0800253 status_t status = remote()->transact(GET_OUTPUT_FOR_ATTR, data, &reply);
254 if (status != NO_ERROR) {
255 return status;
256 }
257 status = (status_t)reply.readInt32();
258 if (status != NO_ERROR) {
259 return status;
260 }
261 *output = (audio_io_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800262 audio_stream_type_t lStream = (audio_stream_type_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800263 if (stream != NULL) {
Eric Laurent20b9ef02016-12-05 11:03:16 -0800264 *stream = lStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800265 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700266 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800267 *portId = (audio_port_handle_t)reply.readInt32();
Kevin Rocard153f92d2018-12-18 18:33:28 -0800268 secondaryOutputs->resize(reply.readInt32());
269 return reply.read(secondaryOutputs->data(),
270 secondaryOutputs->size() * sizeof(audio_io_handle_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700271 }
272
Eric Laurentd7fe0862018-07-14 16:48:01 -0700273 virtual status_t startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700274 {
275 Parcel data, reply;
276 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700277 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700278 remote()->transact(START_OUTPUT, data, &reply);
279 return static_cast <status_t> (reply.readInt32());
280 }
281
Eric Laurentd7fe0862018-07-14 16:48:01 -0700282 virtual status_t stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700283 {
284 Parcel data, reply;
285 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700286 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700287 remote()->transact(STOP_OUTPUT, data, &reply);
288 return static_cast <status_t> (reply.readInt32());
289 }
290
Eric Laurentd7fe0862018-07-14 16:48:01 -0700291 virtual void releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700292 {
293 Parcel data, reply;
294 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700295 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700296 remote()->transact(RELEASE_OUTPUT, data, &reply);
297 }
298
Eric Laurentcaf7f482014-11-25 17:50:47 -0800299 virtual status_t getInputForAttr(const audio_attributes_t *attr,
300 audio_io_handle_t *input,
301 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700302 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700303 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800304 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800305 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600306 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700307 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800308 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700309 {
310 Parcel data, reply;
311 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentcaf7f482014-11-25 17:50:47 -0800312 if (attr == NULL) {
313 ALOGE("getInputForAttr NULL attr - shouldn't happen");
314 return BAD_VALUE;
315 }
316 if (input == NULL) {
317 ALOGE("getInputForAttr NULL input - shouldn't happen");
318 return BAD_VALUE;
319 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700320 if (selectedDeviceId == NULL) {
321 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
322 return BAD_VALUE;
323 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800324 if (portId == NULL) {
325 ALOGE("getInputForAttr NULL portId - shouldn't happen");
326 return BAD_VALUE;
327 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800328 data.write(attr, sizeof(audio_attributes_t));
Eric Laurenta54f1282017-07-01 19:39:32 -0700329 data.writeInt32(*input);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800330 data.writeInt32(session);
Eric Laurentb2379ba2016-05-23 17:42:12 -0700331 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700332 data.writeInt32(uid);
Eric Laurentfee19762018-01-29 18:44:13 -0800333 data.writeString16(opPackageName);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800334 data.write(config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -0700335 data.writeInt32(flags);
Eric Laurent9ae8c592017-06-22 17:17:09 -0700336 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800337 data.writeInt32(*portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800338 status_t status = remote()->transact(GET_INPUT_FOR_ATTR, data, &reply);
339 if (status != NO_ERROR) {
340 return status;
341 }
342 status = reply.readInt32();
343 if (status != NO_ERROR) {
344 return status;
345 }
346 *input = (audio_io_handle_t)reply.readInt32();
Eric Laurent9ae8c592017-06-22 17:17:09 -0700347 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800348 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800349 return NO_ERROR;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700350 }
351
Eric Laurent4eb58f12018-12-07 16:41:02 -0800352 virtual status_t startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700353 {
354 Parcel data, reply;
355 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800356 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700357 remote()->transact(START_INPUT, data, &reply);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800358 status_t status = static_cast <status_t> (reply.readInt32());
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800359 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700360 }
361
Eric Laurentfee19762018-01-29 18:44:13 -0800362 virtual status_t stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700363 {
364 Parcel data, reply;
365 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800366 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700367 remote()->transact(STOP_INPUT, data, &reply);
368 return static_cast <status_t> (reply.readInt32());
369 }
370
Eric Laurentfee19762018-01-29 18:44:13 -0800371 virtual void releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700372 {
373 Parcel data, reply;
374 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800375 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700376 remote()->transact(RELEASE_INPUT, data, &reply);
377 }
378
Dima Zavinfce7a472011-04-19 22:30:36 -0700379 virtual status_t initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700380 int indexMin,
381 int indexMax)
382 {
383 Parcel data, reply;
384 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
385 data.writeInt32(static_cast <uint32_t>(stream));
386 data.writeInt32(indexMin);
387 data.writeInt32(indexMax);
388 remote()->transact(INIT_STREAM_VOLUME, data, &reply);
389 return static_cast <status_t> (reply.readInt32());
390 }
391
Eric Laurent83844cc2011-11-18 16:43:31 -0800392 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
393 int index,
394 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700395 {
396 Parcel data, reply;
397 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
398 data.writeInt32(static_cast <uint32_t>(stream));
399 data.writeInt32(index);
Eric Laurent83844cc2011-11-18 16:43:31 -0800400 data.writeInt32(static_cast <uint32_t>(device));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700401 remote()->transact(SET_STREAM_VOLUME, data, &reply);
402 return static_cast <status_t> (reply.readInt32());
403 }
404
Eric Laurent83844cc2011-11-18 16:43:31 -0800405 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
406 int *index,
407 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700408 {
409 Parcel data, reply;
410 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
411 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurent83844cc2011-11-18 16:43:31 -0800412 data.writeInt32(static_cast <uint32_t>(device));
413
Eric Laurentc2f1f072009-07-17 12:17:14 -0700414 remote()->transact(GET_STREAM_VOLUME, data, &reply);
415 int lIndex = reply.readInt32();
416 if (index) *index = lIndex;
417 return static_cast <status_t> (reply.readInt32());
418 }
Eric Laurentde070132010-07-13 04:45:46 -0700419
Dima Zavinfce7a472011-04-19 22:30:36 -0700420 virtual uint32_t getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700421 {
422 Parcel data, reply;
423 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
424 data.writeInt32(static_cast <uint32_t>(stream));
425 remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply);
François Gaffiec005e562018-11-06 15:04:49 +0100426 return reply.readUint32();
Eric Laurentde070132010-07-13 04:45:46 -0700427 }
428
Eric Laurent63742522012-03-08 13:42:42 -0800429 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800430 {
431 Parcel data, reply;
432 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
433 data.writeInt32(static_cast <uint32_t>(stream));
434 remote()->transact(GET_DEVICES_FOR_STREAM, data, &reply);
Eric Laurent63742522012-03-08 13:42:42 -0800435 return (audio_devices_t) reply.readInt32();
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800436 }
437
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700438 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700439 {
440 Parcel data, reply;
441 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
442 data.write(desc, sizeof(effect_descriptor_t));
443 remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply);
444 return static_cast <audio_io_handle_t> (reply.readInt32());
445 }
446
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700447 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700448 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700449 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800450 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700451 int id)
452 {
453 Parcel data, reply;
454 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
455 data.write(desc, sizeof(effect_descriptor_t));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700456 data.writeInt32(io);
Eric Laurentde070132010-07-13 04:45:46 -0700457 data.writeInt32(strategy);
458 data.writeInt32(session);
459 data.writeInt32(id);
460 remote()->transact(REGISTER_EFFECT, data, &reply);
461 return static_cast <status_t> (reply.readInt32());
462 }
463
464 virtual status_t unregisterEffect(int id)
465 {
466 Parcel data, reply;
467 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
468 data.writeInt32(id);
469 remote()->transact(UNREGISTER_EFFECT, data, &reply);
470 return static_cast <status_t> (reply.readInt32());
471 }
472
Eric Laurentdb7c0792011-08-10 10:37:50 -0700473 virtual status_t setEffectEnabled(int id, bool enabled)
474 {
475 Parcel data, reply;
476 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
477 data.writeInt32(id);
478 data.writeInt32(enabled);
479 remote()->transact(SET_EFFECT_ENABLED, data, &reply);
480 return static_cast <status_t> (reply.readInt32());
481 }
482
Glenn Kastenfff6d712012-01-12 16:38:12 -0800483 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800484 {
485 Parcel data, reply;
486 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Glenn Kastenfff6d712012-01-12 16:38:12 -0800487 data.writeInt32((int32_t) stream);
Eric Laurenteda6c362011-02-02 09:33:30 -0800488 data.writeInt32(inPastMs);
489 remote()->transact(IS_STREAM_ACTIVE, data, &reply);
490 return reply.readInt32();
491 }
Eric Laurent57dae992011-07-24 13:36:09 -0700492
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800493 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
494 {
495 Parcel data, reply;
496 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
497 data.writeInt32((int32_t) stream);
498 data.writeInt32(inPastMs);
499 remote()->transact(IS_STREAM_ACTIVE_REMOTELY, data, &reply);
500 return reply.readInt32();
501 }
502
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700503 virtual bool isSourceActive(audio_source_t source) const
504 {
505 Parcel data, reply;
506 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
507 data.writeInt32((int32_t) source);
508 remote()->transact(IS_SOURCE_ACTIVE, data, &reply);
509 return reply.readInt32();
510 }
511
Glenn Kastend848eb42016-03-08 13:42:11 -0800512 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700513 effect_descriptor_t *descriptors,
514 uint32_t *count)
515 {
516 if (descriptors == NULL || count == NULL) {
517 return BAD_VALUE;
518 }
519 Parcel data, reply;
520 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
521 data.writeInt32(audioSession);
522 data.writeInt32(*count);
523 status_t status = remote()->transact(QUERY_DEFAULT_PRE_PROCESSING, data, &reply);
524 if (status != NO_ERROR) {
525 return status;
526 }
527 status = static_cast <status_t> (reply.readInt32());
528 uint32_t retCount = reply.readInt32();
529 if (retCount != 0) {
530 uint32_t numDesc = (retCount < *count) ? retCount : *count;
531 reply.read(descriptors, sizeof(effect_descriptor_t) * numDesc);
532 }
533 *count = retCount;
534 return status;
535 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000536
537 virtual bool isOffloadSupported(const audio_offload_info_t& info)
538 {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100539 Parcel data, reply;
540 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
541 data.write(&info, sizeof(audio_offload_info_t));
542 remote()->transact(IS_OFFLOAD_SUPPORTED, data, &reply);
Eric Laurent203b1a12014-04-01 10:34:16 -0700543 return reply.readInt32();
544 }
545
Michael Chana94fbb22018-04-24 14:31:19 +1000546 virtual bool isDirectOutputSupported(const audio_config_base_t& config,
547 const audio_attributes_t& attributes) {
548 Parcel data, reply;
549 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
550 data.write(&config, sizeof(audio_config_base_t));
551 data.write(&attributes, sizeof(audio_attributes_t));
552 status_t status = remote()->transact(IS_DIRECT_OUTPUT_SUPPORTED, data, &reply);
553 return status == NO_ERROR ? static_cast<bool>(reply.readInt32()) : false;
554 }
555
Eric Laurent203b1a12014-04-01 10:34:16 -0700556 virtual status_t listAudioPorts(audio_port_role_t role,
557 audio_port_type_t type,
558 unsigned int *num_ports,
559 struct audio_port *ports,
560 unsigned int *generation)
561 {
562 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
563 generation == NULL) {
564 return BAD_VALUE;
565 }
566 Parcel data, reply;
567 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
568 unsigned int numPortsReq = (ports == NULL) ? 0 : *num_ports;
569 data.writeInt32(role);
570 data.writeInt32(type);
571 data.writeInt32(numPortsReq);
572 status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply);
573 if (status == NO_ERROR) {
574 status = (status_t)reply.readInt32();
575 *num_ports = (unsigned int)reply.readInt32();
576 }
Eric Laurent203b1a12014-04-01 10:34:16 -0700577 if (status == NO_ERROR) {
578 if (numPortsReq > *num_ports) {
579 numPortsReq = *num_ports;
580 }
581 if (numPortsReq > 0) {
582 reply.read(ports, numPortsReq * sizeof(struct audio_port));
583 }
584 *generation = reply.readInt32();
585 }
586 return status;
587 }
588
589 virtual status_t getAudioPort(struct audio_port *port)
590 {
591 if (port == NULL) {
592 return BAD_VALUE;
593 }
594 Parcel data, reply;
595 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
596 data.write(port, sizeof(struct audio_port));
597 status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply);
598 if (status != NO_ERROR ||
599 (status = (status_t)reply.readInt32()) != NO_ERROR) {
600 return status;
601 }
602 reply.read(port, sizeof(struct audio_port));
603 return status;
604 }
605
606 virtual status_t createAudioPatch(const struct audio_patch *patch,
607 audio_patch_handle_t *handle)
608 {
609 if (patch == NULL || handle == NULL) {
610 return BAD_VALUE;
611 }
612 Parcel data, reply;
613 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
614 data.write(patch, sizeof(struct audio_patch));
615 data.write(handle, sizeof(audio_patch_handle_t));
616 status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply);
617 if (status != NO_ERROR ||
618 (status = (status_t)reply.readInt32()) != NO_ERROR) {
619 return status;
620 }
621 reply.read(handle, sizeof(audio_patch_handle_t));
622 return status;
623 }
624
625 virtual status_t releaseAudioPatch(audio_patch_handle_t handle)
626 {
627 Parcel data, reply;
628 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
629 data.write(&handle, sizeof(audio_patch_handle_t));
630 status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply);
631 if (status != NO_ERROR) {
632 status = (status_t)reply.readInt32();
633 }
634 return status;
635 }
636
637 virtual status_t listAudioPatches(unsigned int *num_patches,
638 struct audio_patch *patches,
639 unsigned int *generation)
640 {
641 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
642 generation == NULL) {
643 return BAD_VALUE;
644 }
645 Parcel data, reply;
646 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
647 unsigned int numPatchesReq = (patches == NULL) ? 0 : *num_patches;
648 data.writeInt32(numPatchesReq);
649 status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply);
650 if (status == NO_ERROR) {
651 status = (status_t)reply.readInt32();
652 *num_patches = (unsigned int)reply.readInt32();
653 }
654 if (status == NO_ERROR) {
655 if (numPatchesReq > *num_patches) {
656 numPatchesReq = *num_patches;
657 }
658 if (numPatchesReq > 0) {
659 reply.read(patches, numPatchesReq * sizeof(struct audio_patch));
660 }
661 *generation = reply.readInt32();
662 }
663 return status;
664 }
665
666 virtual status_t setAudioPortConfig(const struct audio_port_config *config)
667 {
668 if (config == NULL) {
669 return BAD_VALUE;
670 }
671 Parcel data, reply;
672 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
673 data.write(config, sizeof(struct audio_port_config));
674 status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply);
675 if (status != NO_ERROR) {
676 status = (status_t)reply.readInt32();
677 }
678 return status;
679 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700680
Eric Laurentb52c1522014-05-20 11:27:36 -0700681 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client)
682 {
683 Parcel data, reply;
684 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -0800685 data.writeStrongBinder(IInterface::asBinder(client));
Eric Laurentb52c1522014-05-20 11:27:36 -0700686 remote()->transact(REGISTER_CLIENT, data, &reply);
687 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700688
Eric Laurente8726fe2015-06-26 09:39:24 -0700689 virtual void setAudioPortCallbacksEnabled(bool enabled)
690 {
691 Parcel data, reply;
692 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
693 data.writeInt32(enabled ? 1 : 0);
694 remote()->transact(SET_AUDIO_PORT_CALLBACK_ENABLED, data, &reply);
695 }
696
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700697 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
698 audio_io_handle_t *ioHandle,
699 audio_devices_t *device)
700 {
701 if (session == NULL || ioHandle == NULL || device == NULL) {
702 return BAD_VALUE;
703 }
704 Parcel data, reply;
705 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
706 status_t status = remote()->transact(ACQUIRE_SOUNDTRIGGER_SESSION, data, &reply);
707 if (status != NO_ERROR) {
708 return status;
709 }
710 status = (status_t)reply.readInt32();
711 if (status == NO_ERROR) {
712 *session = (audio_session_t)reply.readInt32();
713 *ioHandle = (audio_io_handle_t)reply.readInt32();
714 *device = (audio_devices_t)reply.readInt32();
715 }
716 return status;
717 }
718
719 virtual status_t releaseSoundTriggerSession(audio_session_t session)
720 {
721 Parcel data, reply;
722 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
723 data.writeInt32(session);
724 status_t status = remote()->transact(RELEASE_SOUNDTRIGGER_SESSION, data, &reply);
725 if (status != NO_ERROR) {
726 return status;
727 }
728 return (status_t)reply.readInt32();
729 }
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700730
731 virtual audio_mode_t getPhoneState()
732 {
733 Parcel data, reply;
734 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
735 status_t status = remote()->transact(GET_PHONE_STATE, data, &reply);
736 if (status != NO_ERROR) {
737 return AUDIO_MODE_INVALID;
738 }
739 return (audio_mode_t)reply.readInt32();
740 }
Eric Laurentbaac1832014-12-01 17:52:59 -0800741
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700742 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800743 {
744 Parcel data, reply;
745 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
746 data.writeInt32(registration ? 1 : 0);
747 size_t size = mixes.size();
748 if (size > MAX_MIXES_PER_POLICY) {
749 size = MAX_MIXES_PER_POLICY;
750 }
751 size_t sizePosition = data.dataPosition();
752 data.writeInt32(size);
753 size_t finalSize = size;
754 for (size_t i = 0; i < size; i++) {
755 size_t position = data.dataPosition();
756 if (mixes[i].writeToParcel(&data) != NO_ERROR) {
757 data.setDataPosition(position);
758 finalSize--;
759 }
760 }
761 if (size != finalSize) {
762 size_t position = data.dataPosition();
763 data.setDataPosition(sizePosition);
764 data.writeInt32(finalSize);
765 data.setDataPosition(position);
766 }
767 status_t status = remote()->transact(REGISTER_POLICY_MIXES, data, &reply);
768 if (status == NO_ERROR) {
769 status = (status_t)reply.readInt32();
770 }
771 return status;
772 }
Eric Laurent554a2772015-04-10 11:29:24 -0700773
774 virtual status_t startAudioSource(const struct audio_port_config *source,
775 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700776 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -0700777 {
778 Parcel data, reply;
779 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700780 if (source == NULL || attributes == NULL || portId == NULL) {
Eric Laurent554a2772015-04-10 11:29:24 -0700781 return BAD_VALUE;
782 }
783 data.write(source, sizeof(struct audio_port_config));
784 data.write(attributes, sizeof(audio_attributes_t));
785 status_t status = remote()->transact(START_AUDIO_SOURCE, data, &reply);
786 if (status != NO_ERROR) {
787 return status;
788 }
789 status = (status_t)reply.readInt32();
790 if (status != NO_ERROR) {
791 return status;
792 }
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700793 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -0700794 return status;
795 }
796
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700797 virtual status_t stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -0700798 {
799 Parcel data, reply;
800 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700801 data.writeInt32(portId);
Eric Laurent554a2772015-04-10 11:29:24 -0700802 status_t status = remote()->transact(STOP_AUDIO_SOURCE, data, &reply);
803 if (status != NO_ERROR) {
804 return status;
805 }
806 status = (status_t)reply.readInt32();
807 return status;
808 }
Andy Hung2ddee192015-12-18 17:34:44 -0800809
810 virtual status_t setMasterMono(bool mono)
811 {
812 Parcel data, reply;
813 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
814 data.writeInt32(static_cast<int32_t>(mono));
815 status_t status = remote()->transact(SET_MASTER_MONO, data, &reply);
816 if (status != NO_ERROR) {
817 return status;
818 }
819 return static_cast<status_t>(reply.readInt32());
820 }
821
822 virtual status_t getMasterMono(bool *mono)
823 {
824 if (mono == nullptr) {
825 return BAD_VALUE;
826 }
827 Parcel data, reply;
828 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
829
830 status_t status = remote()->transact(GET_MASTER_MONO, data, &reply);
831 if (status != NO_ERROR) {
832 return status;
833 }
834 status = static_cast<status_t>(reply.readInt32());
835 if (status == NO_ERROR) {
836 *mono = static_cast<bool>(reply.readInt32());
837 }
838 return status;
839 }
Eric Laurentac9cef52017-06-09 15:46:26 -0700840
841 virtual float getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
842 {
843 Parcel data, reply;
844 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
845 data.writeInt32(static_cast <int32_t>(stream));
846 data.writeInt32(static_cast <int32_t>(index));
847 data.writeUint32(static_cast <uint32_t>(device));
848 status_t status = remote()->transact(GET_STREAM_VOLUME_DB, data, &reply);
849 if (status != NO_ERROR) {
850 return NAN;
851 }
852 return reply.readFloat();
853 }
jiabin81772902018-04-02 17:52:27 -0700854
855 virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
856 audio_format_t *surroundFormats,
857 bool *surroundFormatsEnabled,
858 bool reported)
859 {
860 if (numSurroundFormats == NULL || (*numSurroundFormats != 0 &&
861 (surroundFormats == NULL || surroundFormatsEnabled == NULL))) {
862 return BAD_VALUE;
863 }
864 Parcel data, reply;
865 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
866 unsigned int numSurroundFormatsReq = *numSurroundFormats;
867 data.writeUint32(numSurroundFormatsReq);
868 data.writeBool(reported);
869 status_t status = remote()->transact(GET_SURROUND_FORMATS, data, &reply);
870 if (status == NO_ERROR && (status = (status_t)reply.readInt32()) == NO_ERROR) {
871 *numSurroundFormats = reply.readUint32();
872 }
873 if (status == NO_ERROR) {
874 if (numSurroundFormatsReq > *numSurroundFormats) {
875 numSurroundFormatsReq = *numSurroundFormats;
876 }
877 if (numSurroundFormatsReq > 0) {
878 status = reply.read(surroundFormats,
879 numSurroundFormatsReq * sizeof(audio_format_t));
880 if (status != NO_ERROR) {
881 return status;
882 }
883 status = reply.read(surroundFormatsEnabled,
884 numSurroundFormatsReq * sizeof(bool));
885 }
886 }
887 return status;
888 }
889
890 virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
891 {
892 Parcel data, reply;
893 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
894 data.writeInt32(audioFormat);
895 data.writeBool(enabled);
896 status_t status = remote()->transact(SET_SURROUND_FORMAT_ENABLED, data, &reply);
897 if (status != NO_ERROR) {
898 return status;
899 }
900 return reply.readInt32();
901 }
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700902
Arun Mirpuri11029ad2018-12-19 20:45:19 -0800903 virtual status_t getHwOffloadEncodingFormatsSupportedForA2DP(
904 std::vector<audio_format_t> *formats)
905 {
906 if (formats == NULL) {
907 return BAD_VALUE;
908 }
909
910 Parcel data, reply;
911 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
912 status_t status = remote()->transact(GET_OFFLOAD_FORMATS_A2DP, data, &reply);
913 if (status != NO_ERROR || (status = (status_t)reply.readInt32()) != NO_ERROR) {
914 return status;
915 }
916
917 size_t list_size = reply.readUint32();
918
919 for (size_t i = 0; i < list_size; i++) {
920 formats->push_back(static_cast<audio_format_t>(reply.readInt32()));
921 }
922 return NO_ERROR;
923 }
924
925
926 virtual status_t addStreamDefaultEffect(const effect_uuid_t *type,
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700927 const String16& opPackageName,
928 const effect_uuid_t *uuid,
929 int32_t priority,
930 audio_usage_t usage,
931 audio_unique_id_t* id)
932 {
933 Parcel data, reply;
934 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
935 data.write(type, sizeof(effect_uuid_t));
936 data.writeString16(opPackageName);
937 data.write(uuid, sizeof(effect_uuid_t));
938 data.writeInt32(priority);
939 data.writeInt32((int32_t) usage);
940 status_t status = remote()->transact(ADD_STREAM_DEFAULT_EFFECT, data, &reply);
941 if (status != NO_ERROR) {
942 return status;
943 }
944 status = static_cast <status_t> (reply.readInt32());
945 *id = reply.readInt32();
946 return status;
947 }
948
949 virtual status_t removeStreamDefaultEffect(audio_unique_id_t id)
950 {
951 Parcel data, reply;
952 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
953 data.writeInt32(id);
954 status_t status = remote()->transact(REMOVE_STREAM_DEFAULT_EFFECT, data, &reply);
955 if (status != NO_ERROR) {
956 return status;
957 }
958 return static_cast <status_t> (reply.readInt32());
959 }
960
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700961 virtual status_t addSourceDefaultEffect(const effect_uuid_t *type,
962 const String16& opPackageName,
963 const effect_uuid_t *uuid,
964 int32_t priority,
965 audio_source_t source,
966 audio_unique_id_t* id)
967 {
968 Parcel data, reply;
969 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
970 data.write(type, sizeof(effect_uuid_t));
971 data.writeString16(opPackageName);
972 data.write(uuid, sizeof(effect_uuid_t));
973 data.writeInt32(priority);
974 data.writeInt32((int32_t) source);
975 status_t status = remote()->transact(ADD_SOURCE_DEFAULT_EFFECT, data, &reply);
976 if (status != NO_ERROR) {
977 return status;
978 }
979 status = static_cast <status_t> (reply.readInt32());
980 *id = reply.readInt32();
981 return status;
982 }
983
984 virtual status_t removeSourceDefaultEffect(audio_unique_id_t id)
985 {
986 Parcel data, reply;
987 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
988 data.writeInt32(id);
989 status_t status = remote()->transact(REMOVE_SOURCE_DEFAULT_EFFECT, data, &reply);
990 if (status != NO_ERROR) {
991 return status;
992 }
993 return static_cast <status_t> (reply.readInt32());
994 }
995
Eric Laurentb78763e2018-10-17 10:08:02 -0700996 virtual status_t setAssistantUid(uid_t uid)
997 {
998 Parcel data, reply;
999 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1000 data.writeInt32(uid);
1001 status_t status = remote()->transact(SET_ASSISTANT_UID, data, &reply);
1002 if (status != NO_ERROR) {
1003 return status;
1004 }
1005 return static_cast <status_t> (reply.readInt32());
1006 }
1007
1008 virtual status_t setA11yServicesUids(const std::vector<uid_t>& uids)
1009 {
1010 Parcel data, reply;
1011 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1012 data.writeInt32(uids.size());
1013 for (auto uid : uids) {
1014 data.writeInt32(uid);
1015 }
1016 status_t status = remote()->transact(SET_A11Y_SERVICES_UIDS, data, &reply);
1017 if (status != NO_ERROR) {
1018 return status;
1019 }
1020 return static_cast <status_t> (reply.readInt32());
1021 }
1022
jiabin6012f912018-11-02 17:06:30 -07001023 virtual bool isHapticPlaybackSupported()
1024 {
1025 Parcel data, reply;
1026 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1027 status_t status = remote()->transact(IS_HAPTIC_PLAYBACK_SUPPORTED, data, &reply);
1028 if (status != NO_ERROR) {
1029 return false;
1030 }
1031 return reply.readBool();
1032 }
1033
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001034 virtual status_t setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1035 {
1036 Parcel data, reply;
1037 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1038
1039 data.writeInt32((int32_t) uid);
1040 size_t size = devices.size();
1041 size_t sizePosition = data.dataPosition();
1042 data.writeInt32((int32_t) size);
1043 size_t finalSize = size;
1044 for (size_t i = 0; i < size; i++) {
1045 size_t position = data.dataPosition();
1046 if (devices[i].writeToParcel(&data) != NO_ERROR) {
1047 data.setDataPosition(position);
1048 finalSize--;
1049 }
1050 }
1051 if (size != finalSize) {
1052 size_t position = data.dataPosition();
1053 data.setDataPosition(sizePosition);
1054 data.writeInt32(finalSize);
1055 data.setDataPosition(position);
1056 }
1057
1058 status_t status = remote()->transact(SET_UID_DEVICE_AFFINITY, data, &reply);
1059 if (status == NO_ERROR) {
1060 status = (status_t)reply.readInt32();
1061 }
1062 return status;
1063 }
1064
François Gaffied0ba9ed2018-11-05 11:50:42 +01001065 virtual status_t removeUidDeviceAffinities(uid_t uid) {
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001066 Parcel data, reply;
1067 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1068
1069 data.writeInt32((int32_t) uid);
1070
François Gaffied0ba9ed2018-11-05 11:50:42 +01001071 status_t status =
1072 remote()->transact(REMOVE_UID_DEVICE_AFFINITY, data, &reply);
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001073 if (status == NO_ERROR) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001074 status = (status_t) reply.readInt32();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001075 }
1076 return status;
1077 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001078
1079 virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies)
1080 {
1081 Parcel data, reply;
1082 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1083
1084 status_t status = remote()->transact(LIST_AUDIO_PRODUCT_STRATEGIES, data, &reply);
1085 if (status != NO_ERROR) {
1086 ALOGE("%s: permission denied", __func__);
1087 return status;
1088 }
1089 status = static_cast<status_t>(reply.readInt32());
François Gaffie4b2018b2018-11-07 11:18:59 +01001090 if (status != NO_ERROR) {
1091 return status;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001092 }
François Gaffie4b2018b2018-11-07 11:18:59 +01001093 uint32_t numStrategies = static_cast<uint32_t>(reply.readInt32());
1094 for (size_t i = 0; i < numStrategies; i++) {
1095 AudioProductStrategy strategy;
1096 status = strategy.readFromParcel(&reply);
1097 if (status != NO_ERROR) {
1098 ALOGE("%s: failed to read strategies", __FUNCTION__);
1099 strategies.clear();
1100 return status;
1101 }
1102 strategies.push_back(strategy);
1103 }
1104 return NO_ERROR;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001105 }
1106
François Gaffie4b2018b2018-11-07 11:18:59 +01001107 virtual status_t getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1108 product_strategy_t &productStrategy)
François Gaffied0ba9ed2018-11-05 11:50:42 +01001109 {
1110 Parcel data, reply;
1111 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1112 status_t status = aa.writeToParcel(&data);
1113 if (status != NO_ERROR) {
François Gaffie4b2018b2018-11-07 11:18:59 +01001114 return status;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001115 }
1116 status = remote()->transact(GET_STRATEGY_FOR_ATTRIBUTES, data, &reply);
François Gaffie4b2018b2018-11-07 11:18:59 +01001117 if (status != NO_ERROR) {
1118 return status;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001119 }
François Gaffie4b2018b2018-11-07 11:18:59 +01001120 status = static_cast<status_t>(reply.readInt32());
1121 if (status != NO_ERROR) {
1122 return status;
1123 }
1124 productStrategy = static_cast<product_strategy_t>(reply.readInt32());
1125 return NO_ERROR;
1126 }
1127
1128 virtual status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1129 {
1130 Parcel data, reply;
1131 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1132
1133 status_t status = remote()->transact(LIST_AUDIO_VOLUME_GROUPS, data, &reply);
1134 if (status != NO_ERROR) {
1135 return status;
1136 }
1137 status = static_cast<status_t>(reply.readInt32());
1138 if (status != NO_ERROR) {
1139 return status;
1140 }
1141 uint32_t numGroups = static_cast<uint32_t>(reply.readInt32());
1142 for (size_t i = 0; i < numGroups; i++) {
1143 AudioVolumeGroup group;
1144 status = group.readFromParcel(&reply);
1145 if (status != NO_ERROR) {
1146 ALOGE("%s: failed to read volume groups", __FUNCTION__);
1147 groups.clear();
1148 return status;
1149 }
1150 groups.push_back(group);
1151 }
1152 return NO_ERROR;
1153 }
1154
1155 virtual status_t getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1156 volume_group_t &volumeGroup)
1157 {
1158 Parcel data, reply;
1159 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1160 status_t status = aa.writeToParcel(&data);
1161 if (status != NO_ERROR) {
1162 return status;
1163 }
1164 status = remote()->transact(GET_VOLUME_GROUP_FOR_ATTRIBUTES, data, &reply);
1165 if (status != NO_ERROR) {
1166 return status;
1167 }
1168 status = static_cast<status_t>(reply.readInt32());
1169 if (status != NO_ERROR) {
1170 return status;
1171 }
1172 volumeGroup = static_cast<volume_group_t>(reply.readInt32());
1173 return NO_ERROR;
François Gaffied0ba9ed2018-11-05 11:50:42 +01001174 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001175};
1176
1177IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
1178
1179// ----------------------------------------------------------------------
1180
Eric Laurentc2f1f072009-07-17 12:17:14 -07001181status_t BnAudioPolicyService::onTransact(
1182 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1183{
Eric Laurentb1cc36b2017-12-11 12:14:16 -08001184 // make sure transactions reserved to AudioFlinger do not come from other processes
1185 switch (code) {
1186 case START_OUTPUT:
1187 case STOP_OUTPUT:
1188 case RELEASE_OUTPUT:
1189 case GET_INPUT_FOR_ATTR:
1190 case START_INPUT:
1191 case STOP_INPUT:
1192 case RELEASE_INPUT:
Eric Laurentb1cc36b2017-12-11 12:14:16 -08001193 case GET_OUTPUT_FOR_EFFECT:
1194 case REGISTER_EFFECT:
1195 case UNREGISTER_EFFECT:
1196 case SET_EFFECT_ENABLED:
1197 case GET_OUTPUT_FOR_ATTR:
1198 case ACQUIRE_SOUNDTRIGGER_SESSION:
1199 case RELEASE_SOUNDTRIGGER_SESSION:
1200 ALOGW("%s: transaction %d received from PID %d",
1201 __func__, code, IPCThreadState::self()->getCallingPid());
Eric Laurentef92bff2018-04-26 10:44:50 -07001202 // return status only for non void methods
1203 switch (code) {
1204 case RELEASE_OUTPUT:
1205 case RELEASE_INPUT:
1206 break;
1207 default:
1208 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
1209 break;
1210 }
1211 return OK;
Eric Laurentb1cc36b2017-12-11 12:14:16 -08001212 default:
1213 break;
1214 }
1215
Eric Laurent4980df22018-01-26 18:04:09 -08001216 // make sure the following transactions come from system components
1217 switch (code) {
1218 case SET_DEVICE_CONNECTION_STATE:
1219 case HANDLE_DEVICE_CONFIG_CHANGE:
1220 case SET_PHONE_STATE:
Eric Laurente17378d2018-05-09 14:43:01 -07001221//FIXME: Allow SET_FORCE_USE calls from system apps until a better use case routing API is available
1222// case SET_FORCE_USE:
Eric Laurent4980df22018-01-26 18:04:09 -08001223 case INIT_STREAM_VOLUME:
1224 case SET_STREAM_VOLUME:
1225 case REGISTER_POLICY_MIXES:
Eric Laurent10b71232018-04-13 18:14:44 -07001226 case SET_MASTER_MONO:
jiabin81772902018-04-02 17:52:27 -07001227 case GET_SURROUND_FORMATS:
Eric Laurentb78763e2018-10-17 10:08:02 -07001228 case SET_SURROUND_FORMAT_ENABLED:
1229 case SET_ASSISTANT_UID:
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001230 case SET_A11Y_SERVICES_UIDS:
1231 case SET_UID_DEVICE_AFFINITY:
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001232 case REMOVE_UID_DEVICE_AFFINITY:
François Gaffie4b2018b2018-11-07 11:18:59 +01001233 case GET_OFFLOAD_FORMATS_A2DP:
1234 case LIST_AUDIO_VOLUME_GROUPS:
1235 case GET_VOLUME_GROUP_FOR_ATTRIBUTES: {
Andy Hung4ef19fa2018-05-15 19:35:29 -07001236 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
Eric Laurent4980df22018-01-26 18:04:09 -08001237 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1238 __func__, code, IPCThreadState::self()->getCallingPid(),
1239 IPCThreadState::self()->getCallingUid());
Eric Laurentef92bff2018-04-26 10:44:50 -07001240 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
1241 return OK;
Eric Laurent4980df22018-01-26 18:04:09 -08001242 }
Eric Laurent96c7eed2018-03-26 17:57:01 -07001243 } break;
Eric Laurent4980df22018-01-26 18:04:09 -08001244 default:
1245 break;
1246 }
1247
Eric Laurent39b09b52018-06-29 12:24:40 -07001248 std::string tag("IAudioPolicyService command " + std::to_string(code));
1249 TimeCheck check(tag.c_str());
Eric Laurent3528c932018-02-23 17:17:22 -08001250
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001251 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001252 case SET_DEVICE_CONNECTION_STATE: {
1253 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001254 audio_devices_t device =
1255 static_cast <audio_devices_t>(data.readInt32());
1256 audio_policy_dev_state_t state =
1257 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001258 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -08001259 const char *device_name = data.readCString();
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001260 audio_format_t codecFormat = static_cast <audio_format_t>(data.readInt32());
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001261 if (device_address == nullptr || device_name == nullptr) {
1262 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
1263 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1264 } else {
1265 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
1266 state,
1267 device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001268 device_name,
1269 codecFormat)));
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001270 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001271 return NO_ERROR;
1272 } break;
1273
1274 case GET_DEVICE_CONNECTION_STATE: {
1275 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001276 audio_devices_t device =
1277 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001278 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001279 if (device_address == nullptr) {
1280 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
1281 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
1282 } else {
1283 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
1284 device_address)));
1285 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001286 return NO_ERROR;
1287 } break;
1288
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001289 case HANDLE_DEVICE_CONFIG_CHANGE: {
1290 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1291 audio_devices_t device =
1292 static_cast <audio_devices_t>(data.readInt32());
1293 const char *device_address = data.readCString();
1294 const char *device_name = data.readCString();
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001295 audio_format_t codecFormat =
1296 static_cast <audio_format_t>(data.readInt32());
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001297 if (device_address == nullptr || device_name == nullptr) {
1298 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
1299 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1300 } else {
1301 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
1302 device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001303 device_name,
1304 codecFormat)));
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001305 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001306 return NO_ERROR;
1307 } break;
1308
Eric Laurentc2f1f072009-07-17 12:17:14 -07001309 case SET_PHONE_STATE: {
1310 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001311 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
1312 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001313 return NO_ERROR;
1314 } break;
1315
Eric Laurentc2f1f072009-07-17 12:17:14 -07001316 case SET_FORCE_USE: {
1317 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001318 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1319 data.readInt32());
Dima Zavinfce7a472011-04-19 22:30:36 -07001320 audio_policy_forced_cfg_t config =
1321 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001322 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
1323 return NO_ERROR;
1324 } break;
1325
1326 case GET_FORCE_USE: {
1327 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001328 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1329 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001330 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
1331 return NO_ERROR;
1332 } break;
1333
1334 case GET_OUTPUT: {
1335 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001336 audio_stream_type_t stream =
1337 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +00001338 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001339 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001340 return NO_ERROR;
1341 } break;
1342
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001343 case GET_OUTPUT_FOR_ATTR: {
1344 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001345 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -08001346 bool hasAttributes = data.readInt32() != 0;
1347 if (hasAttributes) {
1348 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001349 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -08001350 }
1351 audio_session_t session = (audio_session_t)data.readInt32();
1352 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
1353 bool hasStream = data.readInt32() != 0;
1354 if (hasStream) {
1355 stream = (audio_stream_type_t)data.readInt32();
1356 }
Nadav Bar766fb022018-01-07 12:18:03 +02001357 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001358 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001359 audio_config_t config;
1360 memset(&config, 0, sizeof(audio_config_t));
1361 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001362 audio_output_flags_t flags =
1363 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001364 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001365 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001366 audio_io_handle_t output = 0;
Kevin Rocard153f92d2018-12-18 18:33:28 -08001367 std::vector<audio_io_handle_t> secondaryOutputs;
Eric Laurente83b55d2014-11-14 10:06:21 -08001368 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001369 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001370 &config,
Kevin Rocard153f92d2018-12-18 18:33:28 -08001371 flags, &selectedDeviceId, &portId, &secondaryOutputs);
Eric Laurente83b55d2014-11-14 10:06:21 -08001372 reply->writeInt32(status);
1373 reply->writeInt32(output);
1374 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001375 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001376 reply->writeInt32(portId);
Kevin Rocard153f92d2018-12-18 18:33:28 -08001377 reply->writeInt32(secondaryOutputs.size());
1378 return reply->write(secondaryOutputs.data(),
1379 secondaryOutputs.size() * sizeof(audio_io_handle_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001380 } break;
1381
Eric Laurentc2f1f072009-07-17 12:17:14 -07001382 case START_OUTPUT: {
1383 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001384 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1385 reply->writeInt32(static_cast <uint32_t>(startOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001386 return NO_ERROR;
1387 } break;
1388
1389 case STOP_OUTPUT: {
1390 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001391 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1392 reply->writeInt32(static_cast <uint32_t>(stopOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001393 return NO_ERROR;
1394 } break;
1395
1396 case RELEASE_OUTPUT: {
1397 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001398 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1399 releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001400 return NO_ERROR;
1401 } break;
1402
Eric Laurentcaf7f482014-11-25 17:50:47 -08001403 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001404 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001405 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001406 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001407 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001408 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001409 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001410 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001411 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001412 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001413 audio_config_base_t config;
1414 memset(&config, 0, sizeof(audio_config_base_t));
1415 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001416 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001417 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001418 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001419 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001420 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001421 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001422 reply->writeInt32(status);
1423 if (status == NO_ERROR) {
1424 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001425 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001426 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001427 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001428 return NO_ERROR;
1429 } break;
1430
1431 case START_INPUT: {
1432 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001433 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
Eric Laurent4eb58f12018-12-07 16:41:02 -08001434 status_t status = startInput(portId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001435 reply->writeInt32(static_cast <uint32_t>(status));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001436 return NO_ERROR;
1437 } break;
1438
1439 case STOP_INPUT: {
1440 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001441 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1442 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001443 return NO_ERROR;
1444 } break;
1445
1446 case RELEASE_INPUT: {
1447 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001448 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1449 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001450 return NO_ERROR;
1451 } break;
1452
1453 case INIT_STREAM_VOLUME: {
1454 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001455 audio_stream_type_t stream =
1456 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001457 int indexMin = data.readInt32();
1458 int indexMax = data.readInt32();
1459 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1460 return NO_ERROR;
1461 } break;
1462
1463 case SET_STREAM_VOLUME: {
1464 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001465 audio_stream_type_t stream =
1466 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001467 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001468 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1469 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1470 index,
1471 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001472 return NO_ERROR;
1473 } break;
1474
1475 case GET_STREAM_VOLUME: {
1476 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001477 audio_stream_type_t stream =
1478 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurent83844cc2011-11-18 16:43:31 -08001479 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001480 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001481 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001482 reply->writeInt32(index);
1483 reply->writeInt32(static_cast <uint32_t>(status));
1484 return NO_ERROR;
1485 } break;
1486
Eric Laurentde070132010-07-13 04:45:46 -07001487 case GET_STRATEGY_FOR_STREAM: {
1488 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001489 audio_stream_type_t stream =
1490 static_cast <audio_stream_type_t>(data.readInt32());
François Gaffiec005e562018-11-06 15:04:49 +01001491 reply->writeUint32(getStrategyForStream(stream));
Eric Laurentde070132010-07-13 04:45:46 -07001492 return NO_ERROR;
1493 } break;
1494
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001495 case GET_DEVICES_FOR_STREAM: {
1496 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001497 audio_stream_type_t stream =
1498 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001499 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1500 return NO_ERROR;
1501 } break;
1502
Eric Laurentde070132010-07-13 04:45:46 -07001503 case GET_OUTPUT_FOR_EFFECT: {
1504 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001505 effect_descriptor_t desc = {};
1506 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1507 android_errorWriteLog(0x534e4554, "73126106");
1508 }
1509 (void)sanitizeEffectDescriptor(&desc);
Eric Laurentde070132010-07-13 04:45:46 -07001510 audio_io_handle_t output = getOutputForEffect(&desc);
1511 reply->writeInt32(static_cast <int>(output));
1512 return NO_ERROR;
1513 } break;
1514
1515 case REGISTER_EFFECT: {
1516 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001517 effect_descriptor_t desc = {};
1518 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1519 android_errorWriteLog(0x534e4554, "73126106");
1520 }
1521 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001522 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001523 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001524 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001525 int id = data.readInt32();
1526 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001527 io,
Eric Laurentde070132010-07-13 04:45:46 -07001528 strategy,
1529 session,
1530 id)));
1531 return NO_ERROR;
1532 } break;
1533
1534 case UNREGISTER_EFFECT: {
1535 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1536 int id = data.readInt32();
1537 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1538 return NO_ERROR;
1539 } break;
1540
Eric Laurentdb7c0792011-08-10 10:37:50 -07001541 case SET_EFFECT_ENABLED: {
1542 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1543 int id = data.readInt32();
1544 bool enabled = static_cast <bool>(data.readInt32());
1545 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1546 return NO_ERROR;
1547 } break;
1548
Eric Laurenteda6c362011-02-02 09:33:30 -08001549 case IS_STREAM_ACTIVE: {
1550 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001551 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001552 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001553 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001554 return NO_ERROR;
1555 } break;
1556
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001557 case IS_STREAM_ACTIVE_REMOTELY: {
1558 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1559 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1560 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001561 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001562 return NO_ERROR;
1563 } break;
1564
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001565 case IS_SOURCE_ACTIVE: {
1566 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1567 audio_source_t source = (audio_source_t) data.readInt32();
1568 reply->writeInt32( isSourceActive(source));
1569 return NO_ERROR;
1570 }
1571
Eric Laurent57dae992011-07-24 13:36:09 -07001572 case QUERY_DEFAULT_PRE_PROCESSING: {
1573 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001574 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001575 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001576 if (count > AudioEffect::kMaxPreProcessing) {
1577 count = AudioEffect::kMaxPreProcessing;
1578 }
Eric Laurent57dae992011-07-24 13:36:09 -07001579 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001580 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001581 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1582 reply->writeInt32(status);
1583 if (status != NO_ERROR && status != NO_MEMORY) {
1584 retCount = 0;
1585 }
1586 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001587 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001588 if (retCount < count) {
1589 count = retCount;
1590 }
1591 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1592 }
1593 delete[] descriptors;
1594 return status;
1595 }
1596
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001597 case IS_OFFLOAD_SUPPORTED: {
1598 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001599 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001600 data.read(&info, sizeof(audio_offload_info_t));
1601 bool isSupported = isOffloadSupported(info);
1602 reply->writeInt32(isSupported);
1603 return NO_ERROR;
1604 }
1605
Michael Chana94fbb22018-04-24 14:31:19 +10001606 case IS_DIRECT_OUTPUT_SUPPORTED: {
1607 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1608 audio_config_base_t config = {};
1609 audio_attributes_t attributes = {};
1610 status_t status = data.read(&config, sizeof(audio_config_base_t));
1611 if (status != NO_ERROR) return status;
1612 status = data.read(&attributes, sizeof(audio_attributes_t));
1613 if (status != NO_ERROR) return status;
1614 reply->writeInt32(isDirectOutputSupported(config, attributes));
1615 return NO_ERROR;
1616 }
1617
Eric Laurent203b1a12014-04-01 10:34:16 -07001618 case LIST_AUDIO_PORTS: {
1619 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1620 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1621 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1622 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001623 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1624 numPortsReq = MAX_ITEMS_PER_LIST;
1625 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001626 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001627 struct audio_port *ports =
1628 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001629 if (ports == NULL) {
1630 reply->writeInt32(NO_MEMORY);
1631 reply->writeInt32(0);
1632 return NO_ERROR;
1633 }
1634 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001635 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1636 reply->writeInt32(status);
1637 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001638
1639 if (status == NO_ERROR) {
1640 if (numPortsReq > numPorts) {
1641 numPortsReq = numPorts;
1642 }
1643 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1644 reply->writeInt32(generation);
1645 }
1646 free(ports);
1647 return NO_ERROR;
1648 }
1649
1650 case GET_AUDIO_PORT: {
1651 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001652 struct audio_port port = {};
1653 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1654 ALOGE("b/23912202");
1655 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001656 status_t status = getAudioPort(&port);
1657 reply->writeInt32(status);
1658 if (status == NO_ERROR) {
1659 reply->write(&port, sizeof(struct audio_port));
1660 }
1661 return NO_ERROR;
1662 }
1663
1664 case CREATE_AUDIO_PATCH: {
1665 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001666 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001667 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001668 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001669 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1670 ALOGE("b/23912202");
1671 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001672 status_t status = createAudioPatch(&patch, &handle);
1673 reply->writeInt32(status);
1674 if (status == NO_ERROR) {
1675 reply->write(&handle, sizeof(audio_patch_handle_t));
1676 }
1677 return NO_ERROR;
1678 }
1679
1680 case RELEASE_AUDIO_PATCH: {
1681 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001682 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001683 data.read(&handle, sizeof(audio_patch_handle_t));
1684 status_t status = releaseAudioPatch(handle);
1685 reply->writeInt32(status);
1686 return NO_ERROR;
1687 }
1688
1689 case LIST_AUDIO_PATCHES: {
1690 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1691 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001692 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1693 numPatchesReq = MAX_ITEMS_PER_LIST;
1694 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001695 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001696 struct audio_patch *patches =
1697 (struct audio_patch *)calloc(numPatchesReq,
1698 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001699 if (patches == NULL) {
1700 reply->writeInt32(NO_MEMORY);
1701 reply->writeInt32(0);
1702 return NO_ERROR;
1703 }
1704 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001705 status_t status = listAudioPatches(&numPatches, patches, &generation);
1706 reply->writeInt32(status);
1707 reply->writeInt32(numPatches);
1708 if (status == NO_ERROR) {
1709 if (numPatchesReq > numPatches) {
1710 numPatchesReq = numPatches;
1711 }
1712 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1713 reply->writeInt32(generation);
1714 }
1715 free(patches);
1716 return NO_ERROR;
1717 }
1718
1719 case SET_AUDIO_PORT_CONFIG: {
1720 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001721 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001722 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001723 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001724 status_t status = setAudioPortConfig(&config);
1725 reply->writeInt32(status);
1726 return NO_ERROR;
1727 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001728
Eric Laurentb52c1522014-05-20 11:27:36 -07001729 case REGISTER_CLIENT: {
1730 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1731 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1732 data.readStrongBinder());
1733 registerClient(client);
1734 return NO_ERROR;
1735 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001736
Eric Laurente8726fe2015-06-26 09:39:24 -07001737 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1738 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1739 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1740 return NO_ERROR;
1741 } break;
1742
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001743 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1744 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1745 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1746 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001747 audio_session_t session = AUDIO_SESSION_NONE;
1748 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1749 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001750 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1751 reply->writeInt32(status);
1752 if (status == NO_ERROR) {
1753 reply->writeInt32(session);
1754 reply->writeInt32(ioHandle);
1755 reply->writeInt32(device);
1756 }
1757 return NO_ERROR;
1758 } break;
1759
1760 case RELEASE_SOUNDTRIGGER_SESSION: {
1761 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1762 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1763 data.readStrongBinder());
1764 audio_session_t session = (audio_session_t)data.readInt32();
1765 status_t status = releaseSoundTriggerSession(session);
1766 reply->writeInt32(status);
1767 return NO_ERROR;
1768 } break;
1769
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001770 case GET_PHONE_STATE: {
1771 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1772 reply->writeInt32((int32_t)getPhoneState());
1773 return NO_ERROR;
1774 } break;
1775
Eric Laurentbaac1832014-12-01 17:52:59 -08001776 case REGISTER_POLICY_MIXES: {
1777 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1778 bool registration = data.readInt32() == 1;
1779 Vector<AudioMix> mixes;
1780 size_t size = (size_t)data.readInt32();
1781 if (size > MAX_MIXES_PER_POLICY) {
1782 size = MAX_MIXES_PER_POLICY;
1783 }
1784 for (size_t i = 0; i < size; i++) {
1785 AudioMix mix;
1786 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1787 mixes.add(mix);
1788 }
1789 }
1790 status_t status = registerPolicyMixes(mixes, registration);
1791 reply->writeInt32(status);
1792 return NO_ERROR;
1793 } break;
1794
Eric Laurent554a2772015-04-10 11:29:24 -07001795 case START_AUDIO_SOURCE: {
1796 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001797 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001798 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001799 (void)sanitizeAudioPortConfig(&source);
1800 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001801 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001802 sanetizeAudioAttributes(&attributes);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001803 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
1804 status_t status = startAudioSource(&source, &attributes, &portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001805 reply->writeInt32(status);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001806 reply->writeInt32(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001807 return NO_ERROR;
1808 } break;
1809
1810 case STOP_AUDIO_SOURCE: {
1811 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001812 audio_port_handle_t portId = (audio_port_handle_t) data.readInt32();
1813 status_t status = stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001814 reply->writeInt32(status);
1815 return NO_ERROR;
1816 } break;
1817
Andy Hung2ddee192015-12-18 17:34:44 -08001818 case SET_MASTER_MONO: {
1819 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1820 bool mono = static_cast<bool>(data.readInt32());
1821 status_t status = setMasterMono(mono);
1822 reply->writeInt32(status);
1823 return NO_ERROR;
1824 } break;
1825
1826 case GET_MASTER_MONO: {
1827 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1828 bool mono;
1829 status_t status = getMasterMono(&mono);
1830 reply->writeInt32(status);
1831 if (status == NO_ERROR) {
1832 reply->writeInt32(static_cast<int32_t>(mono));
1833 }
1834 return NO_ERROR;
1835 } break;
1836
Eric Laurentac9cef52017-06-09 15:46:26 -07001837 case GET_STREAM_VOLUME_DB: {
1838 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1839 audio_stream_type_t stream =
1840 static_cast <audio_stream_type_t>(data.readInt32());
1841 int index = static_cast <int>(data.readInt32());
1842 audio_devices_t device =
1843 static_cast <audio_devices_t>(data.readUint32());
1844 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1845 return NO_ERROR;
1846 }
1847
jiabin81772902018-04-02 17:52:27 -07001848 case GET_SURROUND_FORMATS: {
1849 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1850 unsigned int numSurroundFormatsReq = data.readUint32();
1851 if (numSurroundFormatsReq > MAX_ITEMS_PER_LIST) {
1852 numSurroundFormatsReq = MAX_ITEMS_PER_LIST;
1853 }
1854 bool reported = data.readBool();
1855 unsigned int numSurroundFormats = numSurroundFormatsReq;
1856 audio_format_t *surroundFormats = (audio_format_t *)calloc(
1857 numSurroundFormats, sizeof(audio_format_t));
1858 bool *surroundFormatsEnabled = (bool *)calloc(numSurroundFormats, sizeof(bool));
1859 if (numSurroundFormatsReq > 0 &&
1860 (surroundFormats == NULL || surroundFormatsEnabled == NULL)) {
1861 free(surroundFormats);
1862 free(surroundFormatsEnabled);
1863 reply->writeInt32(NO_MEMORY);
1864 return NO_ERROR;
1865 }
1866 status_t status = getSurroundFormats(
1867 &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1868 reply->writeInt32(status);
1869
1870 if (status == NO_ERROR) {
1871 reply->writeUint32(numSurroundFormats);
1872 if (numSurroundFormatsReq > numSurroundFormats) {
1873 numSurroundFormatsReq = numSurroundFormats;
1874 }
1875 reply->write(surroundFormats, numSurroundFormatsReq * sizeof(audio_format_t));
1876 reply->write(surroundFormatsEnabled, numSurroundFormatsReq * sizeof(bool));
1877 }
1878 free(surroundFormats);
1879 free(surroundFormatsEnabled);
1880 return NO_ERROR;
1881 }
1882
1883 case SET_SURROUND_FORMAT_ENABLED: {
1884 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1885 audio_format_t audioFormat = (audio_format_t) data.readInt32();
1886 bool enabled = data.readBool();
1887 status_t status = setSurroundFormatEnabled(audioFormat, enabled);
1888 reply->writeInt32(status);
1889 return NO_ERROR;
1890 }
1891
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001892 case GET_OFFLOAD_FORMATS_A2DP: {
1893 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1894 std::vector<audio_format_t> encodingFormats;
1895 status_t status = getHwOffloadEncodingFormatsSupportedForA2DP(&encodingFormats);
1896 reply->writeInt32(status);
1897 if (status != NO_ERROR) {
1898 return NO_ERROR;
1899 }
1900 reply->writeUint32(static_cast<uint32_t>(encodingFormats.size()));
1901 for (size_t i = 0; i < encodingFormats.size(); i++)
1902 reply->writeInt32(static_cast<int32_t>(encodingFormats[i]));
1903 return NO_ERROR;
1904 }
1905
1906
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -07001907 case ADD_STREAM_DEFAULT_EFFECT: {
1908 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1909 effect_uuid_t type;
1910 status_t status = data.read(&type, sizeof(effect_uuid_t));
1911 if (status != NO_ERROR) {
1912 return status;
1913 }
1914 String16 opPackageName;
1915 status = data.readString16(&opPackageName);
1916 if (status != NO_ERROR) {
1917 return status;
1918 }
1919 effect_uuid_t uuid;
1920 status = data.read(&uuid, sizeof(effect_uuid_t));
1921 if (status != NO_ERROR) {
1922 return status;
1923 }
1924 int32_t priority = data.readInt32();
1925 audio_usage_t usage = (audio_usage_t) data.readInt32();
1926 audio_unique_id_t id = 0;
1927 reply->writeInt32(static_cast <int32_t>(addStreamDefaultEffect(&type,
1928 opPackageName,
1929 &uuid,
1930 priority,
1931 usage,
1932 &id)));
1933 reply->writeInt32(id);
1934 return NO_ERROR;
1935 }
1936
1937 case REMOVE_STREAM_DEFAULT_EFFECT: {
1938 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1939 audio_unique_id_t id = static_cast<audio_unique_id_t>(data.readInt32());
1940 reply->writeInt32(static_cast <int32_t>(removeStreamDefaultEffect(id)));
1941 return NO_ERROR;
1942 }
1943
Ari Hausman-Cohen24628312018-08-13 15:01:09 -07001944 case ADD_SOURCE_DEFAULT_EFFECT: {
1945 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1946 effect_uuid_t type;
1947 status_t status = data.read(&type, sizeof(effect_uuid_t));
1948 if (status != NO_ERROR) {
1949 return status;
1950 }
1951 String16 opPackageName;
1952 status = data.readString16(&opPackageName);
1953 if (status != NO_ERROR) {
1954 return status;
1955 }
1956 effect_uuid_t uuid;
1957 status = data.read(&uuid, sizeof(effect_uuid_t));
1958 if (status != NO_ERROR) {
1959 return status;
1960 }
1961 int32_t priority = data.readInt32();
1962 audio_source_t source = (audio_source_t) data.readInt32();
1963 audio_unique_id_t id = 0;
1964 reply->writeInt32(static_cast <int32_t>(addSourceDefaultEffect(&type,
1965 opPackageName,
1966 &uuid,
1967 priority,
1968 source,
1969 &id)));
1970 reply->writeInt32(id);
1971 return NO_ERROR;
1972 }
1973
1974 case REMOVE_SOURCE_DEFAULT_EFFECT: {
1975 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1976 audio_unique_id_t id = static_cast<audio_unique_id_t>(data.readInt32());
1977 reply->writeInt32(static_cast <int32_t>(removeSourceDefaultEffect(id)));
1978 return NO_ERROR;
1979 }
1980
Eric Laurentb78763e2018-10-17 10:08:02 -07001981 case SET_ASSISTANT_UID: {
1982 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1983 int32_t uid;
1984 status_t status = data.readInt32(&uid);
1985 if (status != NO_ERROR) {
1986 return status;
1987 }
1988 status = setAssistantUid(uid);
1989 reply->writeInt32(static_cast <int32_t>(status));
1990 return NO_ERROR;
1991 }
1992
1993 case SET_A11Y_SERVICES_UIDS: {
1994 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1995 std::vector<uid_t> uids;
1996 int32_t size;
1997 status_t status = data.readInt32(&size);
1998 if (status != NO_ERROR) {
1999 return status;
2000 }
2001 if (size > MAX_ITEMS_PER_LIST) {
2002 size = MAX_ITEMS_PER_LIST;
2003 }
2004 for (int32_t i = 0; i < size; i++) {
2005 int32_t uid;
2006 status = data.readInt32(&uid);
2007 if (status != NO_ERROR) {
2008 return status;
2009 }
2010 uids.push_back(uid);
2011 }
2012 status = setA11yServicesUids(uids);
2013 reply->writeInt32(static_cast <int32_t>(status));
2014 return NO_ERROR;
2015 }
2016
jiabin6012f912018-11-02 17:06:30 -07002017 case IS_HAPTIC_PLAYBACK_SUPPORTED: {
2018 CHECK_INTERFACE(IAudioPolicyService, data, reply);
2019 bool isSupported = isHapticPlaybackSupported();
2020 reply->writeBool(isSupported);
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08002021 return NO_ERROR;
2022 }
2023
2024 case SET_UID_DEVICE_AFFINITY: {
2025 CHECK_INTERFACE(IAudioPolicyService, data, reply);
2026 const uid_t uid = (uid_t) data.readInt32();
2027 Vector<AudioDeviceTypeAddr> devices;
2028 size_t size = (size_t)data.readInt32();
2029 for (size_t i = 0; i < size; i++) {
2030 AudioDeviceTypeAddr device;
2031 if (device.readFromParcel((Parcel*)&data) == NO_ERROR) {
2032 devices.add(device);
2033 }
2034 }
2035 status_t status = setUidDeviceAffinities(uid, devices);
2036 reply->writeInt32(status);
2037 return NO_ERROR;
2038 }
2039
2040 case REMOVE_UID_DEVICE_AFFINITY: {
2041 CHECK_INTERFACE(IAudioPolicyService, data, reply);
2042 const uid_t uid = (uid_t) data.readInt32();
2043 status_t status = removeUidDeviceAffinities(uid);
2044 reply->writeInt32(status);
jiabin6012f912018-11-02 17:06:30 -07002045 return NO_ERROR;
2046 }
2047
François Gaffied0ba9ed2018-11-05 11:50:42 +01002048 case LIST_AUDIO_PRODUCT_STRATEGIES: {
2049 CHECK_INTERFACE(IAudioPolicyService, data, reply);
2050 AudioProductStrategyVector strategies;
2051 status_t status = listAudioProductStrategies(strategies);
2052 reply->writeInt32(status);
2053 if (status != NO_ERROR) {
François Gaffie4b2018b2018-11-07 11:18:59 +01002054 return NO_ERROR;
François Gaffied0ba9ed2018-11-05 11:50:42 +01002055 }
2056 size_t size = strategies.size();
2057 size_t sizePosition = reply->dataPosition();
2058 reply->writeInt32(size);
2059 size_t finalSize = size;
2060 for (size_t i = 0; i < size; i++) {
2061 size_t position = reply->dataPosition();
2062 if (strategies[i].writeToParcel(reply) != NO_ERROR) {
2063 reply->setDataPosition(position);
2064 finalSize--;
2065 }
2066 }
2067 if (size != finalSize) {
2068 size_t position = reply->dataPosition();
2069 reply->setDataPosition(sizePosition);
2070 reply->writeInt32(finalSize);
2071 reply->setDataPosition(position);
2072 }
2073 return NO_ERROR;
2074 }
2075
2076 case GET_STRATEGY_FOR_ATTRIBUTES: {
2077 CHECK_INTERFACE(IAudioPolicyService, data, reply);
2078 AudioAttributes attributes;
2079 status_t status = attributes.readFromParcel(&data);
2080 if (status != NO_ERROR) {
2081 return status;
2082 }
François Gaffie4b2018b2018-11-07 11:18:59 +01002083 product_strategy_t strategy;
2084 status = getProductStrategyFromAudioAttributes(attributes, strategy);
2085 reply->writeInt32(status);
2086 if (status != NO_ERROR) {
2087 return NO_ERROR;
2088 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01002089 reply->writeUint32(static_cast<int>(strategy));
2090 return NO_ERROR;
2091 }
2092
François Gaffie4b2018b2018-11-07 11:18:59 +01002093 case LIST_AUDIO_VOLUME_GROUPS: {
2094 CHECK_INTERFACE(IAudioPolicyService, data, reply);
2095 AudioVolumeGroupVector groups;
2096 status_t status = listAudioVolumeGroups(groups);
2097 reply->writeInt32(status);
2098 if (status != NO_ERROR) {
2099 return NO_ERROR;
2100 }
2101 size_t size = groups.size();
2102 size_t sizePosition = reply->dataPosition();
2103 reply->writeInt32(size);
2104 size_t finalSize = size;
2105 for (size_t i = 0; i < size; i++) {
2106 size_t position = reply->dataPosition();
2107 if (groups[i].writeToParcel(reply) != NO_ERROR) {
2108 reply->setDataPosition(position);
2109 finalSize--;
2110 }
2111 }
2112 if (size != finalSize) {
2113 size_t position = reply->dataPosition();
2114 reply->setDataPosition(sizePosition);
2115 reply->writeInt32(finalSize);
2116 reply->setDataPosition(position);
2117 }
2118 return NO_ERROR;
2119 }
2120
2121 case GET_VOLUME_GROUP_FOR_ATTRIBUTES: {
2122 CHECK_INTERFACE(IAudioPolicyService, data, reply);
2123 AudioAttributes attributes;
2124 status_t status = attributes.readFromParcel(&data);
2125 if (status != NO_ERROR) {
2126 return status;
2127 }
2128 volume_group_t group;
2129 status = getVolumeGroupFromAudioAttributes(attributes, group);
2130 reply->writeInt32(status);
2131 if (status != NO_ERROR) {
2132 return NO_ERROR;
2133 }
2134 reply->writeUint32(static_cast<int>(group));
2135 return NO_ERROR;
2136 }
2137
Eric Laurentc2f1f072009-07-17 12:17:14 -07002138 default:
2139 return BBinder::onTransact(code, data, reply, flags);
2140 }
2141}
2142
Andy Hungb0272092018-04-12 11:06:56 -07002143/** returns true if string overflow was prevented by zero termination */
2144template <size_t size>
2145static bool preventStringOverflow(char (&s)[size]) {
2146 if (strnlen(s, size) < size) return false;
2147 s[size - 1] = '\0';
2148 return true;
2149}
2150
Kevin Rocard39fdbd02017-11-13 11:15:27 -08002151void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
2152{
2153 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
2154 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
2155 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
2156 }
2157 attr->tags[tagsMaxSize - 1] = '\0';
2158}
2159
Andy Hungb0272092018-04-12 11:06:56 -07002160/** returns BAD_VALUE if sanitization was required. */
2161status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
2162{
2163 if (preventStringOverflow(desc->name)
2164 | /* always */ preventStringOverflow(desc->implementor)) {
2165 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
2166 return BAD_VALUE;
2167 }
2168 return NO_ERROR;
2169}
2170
2171/** returns BAD_VALUE if sanitization was required. */
2172status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
2173{
2174 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
2175 preventStringOverflow(config->ext.device.address)) {
2176 return BAD_VALUE;
2177 }
2178 return NO_ERROR;
2179}
2180
Eric Laurentc2f1f072009-07-17 12:17:14 -07002181// ----------------------------------------------------------------------------
2182
Glenn Kasten40bc9062015-03-20 09:09:33 -07002183} // namespace android