blob: 0db56e8609987fb5d35e1303289b12e2ee3e0ff0 [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,
Eric Laurentc2f1f072009-07-17 12:17:14 -070098};
99
Eric Laurent1d670b12015-02-06 10:44:24 -0800100#define MAX_ITEMS_PER_LIST 1024
101
Eric Laurentc2f1f072009-07-17 12:17:14 -0700102class BpAudioPolicyService : public BpInterface<IAudioPolicyService>
103{
104public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -0700105 explicit BpAudioPolicyService(const sp<IBinder>& impl)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700106 : BpInterface<IAudioPolicyService>(impl)
107 {
108 }
109
110 virtual status_t setDeviceConnectionState(
Dima Zavinfce7a472011-04-19 22:30:36 -0700111 audio_devices_t device,
112 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800113 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800114 const char *device_name,
115 audio_format_t encodedFormat)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700116 {
117 Parcel data, reply;
118 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
119 data.writeInt32(static_cast <uint32_t>(device));
120 data.writeInt32(static_cast <uint32_t>(state));
121 data.writeCString(device_address);
Paul McLeane743a472015-01-28 11:07:31 -0800122 data.writeCString(device_name);
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800123 data.writeInt32(static_cast <uint32_t>(encodedFormat));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700124 remote()->transact(SET_DEVICE_CONNECTION_STATE, data, &reply);
125 return static_cast <status_t> (reply.readInt32());
126 }
127
Dima Zavinfce7a472011-04-19 22:30:36 -0700128 virtual audio_policy_dev_state_t getDeviceConnectionState(
129 audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700130 const char *device_address)
131 {
132 Parcel data, reply;
133 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
134 data.writeInt32(static_cast <uint32_t>(device));
135 data.writeCString(device_address);
136 remote()->transact(GET_DEVICE_CONNECTION_STATE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700137 return static_cast <audio_policy_dev_state_t>(reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700138 }
139
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800140 virtual status_t handleDeviceConfigChange(audio_devices_t device,
141 const char *device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800142 const char *device_name,
143 audio_format_t encodedFormat)
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800144 {
145 Parcel data, reply;
146 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
147 data.writeInt32(static_cast <uint32_t>(device));
148 data.writeCString(device_address);
149 data.writeCString(device_name);
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800150 data.writeInt32(static_cast <uint32_t>(encodedFormat));
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800151 remote()->transact(HANDLE_DEVICE_CONFIG_CHANGE, data, &reply);
152 return static_cast <status_t> (reply.readInt32());
153 }
154
Glenn Kastenf78aee72012-01-04 11:00:47 -0800155 virtual status_t setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700156 {
157 Parcel data, reply;
158 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
159 data.writeInt32(state);
160 remote()->transact(SET_PHONE_STATE, data, &reply);
161 return static_cast <status_t> (reply.readInt32());
162 }
163
Dima Zavinfce7a472011-04-19 22:30:36 -0700164 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700165 {
166 Parcel data, reply;
167 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
168 data.writeInt32(static_cast <uint32_t>(usage));
169 data.writeInt32(static_cast <uint32_t>(config));
170 remote()->transact(SET_FORCE_USE, data, &reply);
171 return static_cast <status_t> (reply.readInt32());
172 }
173
Dima Zavinfce7a472011-04-19 22:30:36 -0700174 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700175 {
176 Parcel data, reply;
177 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
178 data.writeInt32(static_cast <uint32_t>(usage));
179 remote()->transact(GET_FORCE_USE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700180 return static_cast <audio_policy_forced_cfg_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700181 }
182
Eric Laurentf4e63452017-11-06 19:31:46 +0000183 virtual audio_io_handle_t getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700184 {
185 Parcel data, reply;
186 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
187 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700188 remote()->transact(GET_OUTPUT, data, &reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -0700189 return static_cast <audio_io_handle_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700190 }
191
Eric Laurente83b55d2014-11-14 10:06:21 -0800192 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
193 audio_io_handle_t *output,
194 audio_session_t session,
195 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200196 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700197 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800198 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800199 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700200 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800201 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700202 {
203 Parcel data, reply;
204 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
205 if (attr == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800206 if (stream == NULL) {
207 ALOGE("getOutputForAttr(): NULL audio attributes and stream type");
208 return BAD_VALUE;
209 }
210 if (*stream == AUDIO_STREAM_DEFAULT) {
211 ALOGE("getOutputForAttr unspecified stream type");
212 return BAD_VALUE;
213 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700214 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800215 if (output == NULL) {
216 ALOGE("getOutputForAttr NULL output - shouldn't happen");
217 return BAD_VALUE;
218 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700219 if (selectedDeviceId == NULL) {
220 ALOGE("getOutputForAttr NULL selectedDeviceId - shouldn't happen");
221 return BAD_VALUE;
222 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800223 if (portId == NULL) {
224 ALOGE("getOutputForAttr NULL portId - shouldn't happen");
225 return BAD_VALUE;
226 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800227 if (attr == NULL) {
228 data.writeInt32(0);
229 } else {
230 data.writeInt32(1);
231 data.write(attr, sizeof(audio_attributes_t));
232 }
233 data.writeInt32(session);
234 if (stream == NULL) {
235 data.writeInt32(0);
236 } else {
237 data.writeInt32(1);
238 data.writeInt32(*stream);
239 }
Nadav Bar766fb022018-01-07 12:18:03 +0200240 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700241 data.writeInt32(uid);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800242 data.write(config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700243 data.writeInt32(static_cast <uint32_t>(flags));
Eric Laurent9ae8c592017-06-22 17:17:09 -0700244 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800245 data.writeInt32(*portId);
Eric Laurente83b55d2014-11-14 10:06:21 -0800246 status_t status = remote()->transact(GET_OUTPUT_FOR_ATTR, data, &reply);
247 if (status != NO_ERROR) {
248 return status;
249 }
250 status = (status_t)reply.readInt32();
251 if (status != NO_ERROR) {
252 return status;
253 }
254 *output = (audio_io_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800255 audio_stream_type_t lStream = (audio_stream_type_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800256 if (stream != NULL) {
Eric Laurent20b9ef02016-12-05 11:03:16 -0800257 *stream = lStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800258 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700259 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800260 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800261 return status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700262 }
263
Eric Laurentd7fe0862018-07-14 16:48:01 -0700264 virtual status_t startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700265 {
266 Parcel data, reply;
267 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700268 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700269 remote()->transact(START_OUTPUT, data, &reply);
270 return static_cast <status_t> (reply.readInt32());
271 }
272
Eric Laurentd7fe0862018-07-14 16:48:01 -0700273 virtual status_t stopOutput(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(STOP_OUTPUT, data, &reply);
279 return static_cast <status_t> (reply.readInt32());
280 }
281
Eric Laurentd7fe0862018-07-14 16:48:01 -0700282 virtual void releaseOutput(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(RELEASE_OUTPUT, data, &reply);
288 }
289
Eric Laurentcaf7f482014-11-25 17:50:47 -0800290 virtual status_t getInputForAttr(const audio_attributes_t *attr,
291 audio_io_handle_t *input,
292 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700293 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700294 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800295 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800296 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600297 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700298 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800299 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700300 {
301 Parcel data, reply;
302 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentcaf7f482014-11-25 17:50:47 -0800303 if (attr == NULL) {
304 ALOGE("getInputForAttr NULL attr - shouldn't happen");
305 return BAD_VALUE;
306 }
307 if (input == NULL) {
308 ALOGE("getInputForAttr NULL input - shouldn't happen");
309 return BAD_VALUE;
310 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700311 if (selectedDeviceId == NULL) {
312 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
313 return BAD_VALUE;
314 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800315 if (portId == NULL) {
316 ALOGE("getInputForAttr NULL portId - shouldn't happen");
317 return BAD_VALUE;
318 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800319 data.write(attr, sizeof(audio_attributes_t));
Eric Laurenta54f1282017-07-01 19:39:32 -0700320 data.writeInt32(*input);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800321 data.writeInt32(session);
Eric Laurentb2379ba2016-05-23 17:42:12 -0700322 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700323 data.writeInt32(uid);
Eric Laurentfee19762018-01-29 18:44:13 -0800324 data.writeString16(opPackageName);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800325 data.write(config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -0700326 data.writeInt32(flags);
Eric Laurent9ae8c592017-06-22 17:17:09 -0700327 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800328 data.writeInt32(*portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800329 status_t status = remote()->transact(GET_INPUT_FOR_ATTR, data, &reply);
330 if (status != NO_ERROR) {
331 return status;
332 }
333 status = reply.readInt32();
334 if (status != NO_ERROR) {
335 return status;
336 }
337 *input = (audio_io_handle_t)reply.readInt32();
Eric Laurent9ae8c592017-06-22 17:17:09 -0700338 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800339 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800340 return NO_ERROR;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700341 }
342
Eric Laurent4eb58f12018-12-07 16:41:02 -0800343 virtual status_t startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700344 {
345 Parcel data, reply;
346 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800347 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700348 remote()->transact(START_INPUT, data, &reply);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800349 status_t status = static_cast <status_t> (reply.readInt32());
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800350 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700351 }
352
Eric Laurentfee19762018-01-29 18:44:13 -0800353 virtual status_t stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700354 {
355 Parcel data, reply;
356 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800357 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700358 remote()->transact(STOP_INPUT, data, &reply);
359 return static_cast <status_t> (reply.readInt32());
360 }
361
Eric Laurentfee19762018-01-29 18:44:13 -0800362 virtual void releaseInput(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(RELEASE_INPUT, data, &reply);
368 }
369
Dima Zavinfce7a472011-04-19 22:30:36 -0700370 virtual status_t initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700371 int indexMin,
372 int indexMax)
373 {
374 Parcel data, reply;
375 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
376 data.writeInt32(static_cast <uint32_t>(stream));
377 data.writeInt32(indexMin);
378 data.writeInt32(indexMax);
379 remote()->transact(INIT_STREAM_VOLUME, data, &reply);
380 return static_cast <status_t> (reply.readInt32());
381 }
382
Eric Laurent83844cc2011-11-18 16:43:31 -0800383 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
384 int index,
385 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700386 {
387 Parcel data, reply;
388 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
389 data.writeInt32(static_cast <uint32_t>(stream));
390 data.writeInt32(index);
Eric Laurent83844cc2011-11-18 16:43:31 -0800391 data.writeInt32(static_cast <uint32_t>(device));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700392 remote()->transact(SET_STREAM_VOLUME, data, &reply);
393 return static_cast <status_t> (reply.readInt32());
394 }
395
Eric Laurent83844cc2011-11-18 16:43:31 -0800396 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
397 int *index,
398 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700399 {
400 Parcel data, reply;
401 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
402 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurent83844cc2011-11-18 16:43:31 -0800403 data.writeInt32(static_cast <uint32_t>(device));
404
Eric Laurentc2f1f072009-07-17 12:17:14 -0700405 remote()->transact(GET_STREAM_VOLUME, data, &reply);
406 int lIndex = reply.readInt32();
407 if (index) *index = lIndex;
408 return static_cast <status_t> (reply.readInt32());
409 }
Eric Laurentde070132010-07-13 04:45:46 -0700410
Dima Zavinfce7a472011-04-19 22:30:36 -0700411 virtual uint32_t getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700412 {
413 Parcel data, reply;
414 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
415 data.writeInt32(static_cast <uint32_t>(stream));
416 remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply);
François Gaffiec005e562018-11-06 15:04:49 +0100417 return reply.readUint32();
Eric Laurentde070132010-07-13 04:45:46 -0700418 }
419
Eric Laurent63742522012-03-08 13:42:42 -0800420 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800421 {
422 Parcel data, reply;
423 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
424 data.writeInt32(static_cast <uint32_t>(stream));
425 remote()->transact(GET_DEVICES_FOR_STREAM, data, &reply);
Eric Laurent63742522012-03-08 13:42:42 -0800426 return (audio_devices_t) reply.readInt32();
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800427 }
428
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700429 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700430 {
431 Parcel data, reply;
432 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
433 data.write(desc, sizeof(effect_descriptor_t));
434 remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply);
435 return static_cast <audio_io_handle_t> (reply.readInt32());
436 }
437
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700438 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700439 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700440 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800441 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700442 int id)
443 {
444 Parcel data, reply;
445 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
446 data.write(desc, sizeof(effect_descriptor_t));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700447 data.writeInt32(io);
Eric Laurentde070132010-07-13 04:45:46 -0700448 data.writeInt32(strategy);
449 data.writeInt32(session);
450 data.writeInt32(id);
451 remote()->transact(REGISTER_EFFECT, data, &reply);
452 return static_cast <status_t> (reply.readInt32());
453 }
454
455 virtual status_t unregisterEffect(int id)
456 {
457 Parcel data, reply;
458 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
459 data.writeInt32(id);
460 remote()->transact(UNREGISTER_EFFECT, data, &reply);
461 return static_cast <status_t> (reply.readInt32());
462 }
463
Eric Laurentdb7c0792011-08-10 10:37:50 -0700464 virtual status_t setEffectEnabled(int id, bool enabled)
465 {
466 Parcel data, reply;
467 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
468 data.writeInt32(id);
469 data.writeInt32(enabled);
470 remote()->transact(SET_EFFECT_ENABLED, data, &reply);
471 return static_cast <status_t> (reply.readInt32());
472 }
473
Glenn Kastenfff6d712012-01-12 16:38:12 -0800474 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800475 {
476 Parcel data, reply;
477 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Glenn Kastenfff6d712012-01-12 16:38:12 -0800478 data.writeInt32((int32_t) stream);
Eric Laurenteda6c362011-02-02 09:33:30 -0800479 data.writeInt32(inPastMs);
480 remote()->transact(IS_STREAM_ACTIVE, data, &reply);
481 return reply.readInt32();
482 }
Eric Laurent57dae992011-07-24 13:36:09 -0700483
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800484 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
485 {
486 Parcel data, reply;
487 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
488 data.writeInt32((int32_t) stream);
489 data.writeInt32(inPastMs);
490 remote()->transact(IS_STREAM_ACTIVE_REMOTELY, data, &reply);
491 return reply.readInt32();
492 }
493
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700494 virtual bool isSourceActive(audio_source_t source) const
495 {
496 Parcel data, reply;
497 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
498 data.writeInt32((int32_t) source);
499 remote()->transact(IS_SOURCE_ACTIVE, data, &reply);
500 return reply.readInt32();
501 }
502
Glenn Kastend848eb42016-03-08 13:42:11 -0800503 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700504 effect_descriptor_t *descriptors,
505 uint32_t *count)
506 {
507 if (descriptors == NULL || count == NULL) {
508 return BAD_VALUE;
509 }
510 Parcel data, reply;
511 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
512 data.writeInt32(audioSession);
513 data.writeInt32(*count);
514 status_t status = remote()->transact(QUERY_DEFAULT_PRE_PROCESSING, data, &reply);
515 if (status != NO_ERROR) {
516 return status;
517 }
518 status = static_cast <status_t> (reply.readInt32());
519 uint32_t retCount = reply.readInt32();
520 if (retCount != 0) {
521 uint32_t numDesc = (retCount < *count) ? retCount : *count;
522 reply.read(descriptors, sizeof(effect_descriptor_t) * numDesc);
523 }
524 *count = retCount;
525 return status;
526 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000527
528 virtual bool isOffloadSupported(const audio_offload_info_t& info)
529 {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100530 Parcel data, reply;
531 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
532 data.write(&info, sizeof(audio_offload_info_t));
533 remote()->transact(IS_OFFLOAD_SUPPORTED, data, &reply);
Eric Laurent203b1a12014-04-01 10:34:16 -0700534 return reply.readInt32();
535 }
536
Michael Chana94fbb22018-04-24 14:31:19 +1000537 virtual bool isDirectOutputSupported(const audio_config_base_t& config,
538 const audio_attributes_t& attributes) {
539 Parcel data, reply;
540 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
541 data.write(&config, sizeof(audio_config_base_t));
542 data.write(&attributes, sizeof(audio_attributes_t));
543 status_t status = remote()->transact(IS_DIRECT_OUTPUT_SUPPORTED, data, &reply);
544 return status == NO_ERROR ? static_cast<bool>(reply.readInt32()) : false;
545 }
546
Eric Laurent203b1a12014-04-01 10:34:16 -0700547 virtual status_t listAudioPorts(audio_port_role_t role,
548 audio_port_type_t type,
549 unsigned int *num_ports,
550 struct audio_port *ports,
551 unsigned int *generation)
552 {
553 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
554 generation == NULL) {
555 return BAD_VALUE;
556 }
557 Parcel data, reply;
558 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
559 unsigned int numPortsReq = (ports == NULL) ? 0 : *num_ports;
560 data.writeInt32(role);
561 data.writeInt32(type);
562 data.writeInt32(numPortsReq);
563 status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply);
564 if (status == NO_ERROR) {
565 status = (status_t)reply.readInt32();
566 *num_ports = (unsigned int)reply.readInt32();
567 }
Eric Laurent203b1a12014-04-01 10:34:16 -0700568 if (status == NO_ERROR) {
569 if (numPortsReq > *num_ports) {
570 numPortsReq = *num_ports;
571 }
572 if (numPortsReq > 0) {
573 reply.read(ports, numPortsReq * sizeof(struct audio_port));
574 }
575 *generation = reply.readInt32();
576 }
577 return status;
578 }
579
580 virtual status_t getAudioPort(struct audio_port *port)
581 {
582 if (port == NULL) {
583 return BAD_VALUE;
584 }
585 Parcel data, reply;
586 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
587 data.write(port, sizeof(struct audio_port));
588 status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply);
589 if (status != NO_ERROR ||
590 (status = (status_t)reply.readInt32()) != NO_ERROR) {
591 return status;
592 }
593 reply.read(port, sizeof(struct audio_port));
594 return status;
595 }
596
597 virtual status_t createAudioPatch(const struct audio_patch *patch,
598 audio_patch_handle_t *handle)
599 {
600 if (patch == NULL || handle == NULL) {
601 return BAD_VALUE;
602 }
603 Parcel data, reply;
604 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
605 data.write(patch, sizeof(struct audio_patch));
606 data.write(handle, sizeof(audio_patch_handle_t));
607 status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply);
608 if (status != NO_ERROR ||
609 (status = (status_t)reply.readInt32()) != NO_ERROR) {
610 return status;
611 }
612 reply.read(handle, sizeof(audio_patch_handle_t));
613 return status;
614 }
615
616 virtual status_t releaseAudioPatch(audio_patch_handle_t handle)
617 {
618 Parcel data, reply;
619 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
620 data.write(&handle, sizeof(audio_patch_handle_t));
621 status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply);
622 if (status != NO_ERROR) {
623 status = (status_t)reply.readInt32();
624 }
625 return status;
626 }
627
628 virtual status_t listAudioPatches(unsigned int *num_patches,
629 struct audio_patch *patches,
630 unsigned int *generation)
631 {
632 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
633 generation == NULL) {
634 return BAD_VALUE;
635 }
636 Parcel data, reply;
637 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
638 unsigned int numPatchesReq = (patches == NULL) ? 0 : *num_patches;
639 data.writeInt32(numPatchesReq);
640 status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply);
641 if (status == NO_ERROR) {
642 status = (status_t)reply.readInt32();
643 *num_patches = (unsigned int)reply.readInt32();
644 }
645 if (status == NO_ERROR) {
646 if (numPatchesReq > *num_patches) {
647 numPatchesReq = *num_patches;
648 }
649 if (numPatchesReq > 0) {
650 reply.read(patches, numPatchesReq * sizeof(struct audio_patch));
651 }
652 *generation = reply.readInt32();
653 }
654 return status;
655 }
656
657 virtual status_t setAudioPortConfig(const struct audio_port_config *config)
658 {
659 if (config == NULL) {
660 return BAD_VALUE;
661 }
662 Parcel data, reply;
663 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
664 data.write(config, sizeof(struct audio_port_config));
665 status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply);
666 if (status != NO_ERROR) {
667 status = (status_t)reply.readInt32();
668 }
669 return status;
670 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700671
Eric Laurentb52c1522014-05-20 11:27:36 -0700672 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client)
673 {
674 Parcel data, reply;
675 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -0800676 data.writeStrongBinder(IInterface::asBinder(client));
Eric Laurentb52c1522014-05-20 11:27:36 -0700677 remote()->transact(REGISTER_CLIENT, data, &reply);
678 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700679
Eric Laurente8726fe2015-06-26 09:39:24 -0700680 virtual void setAudioPortCallbacksEnabled(bool enabled)
681 {
682 Parcel data, reply;
683 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
684 data.writeInt32(enabled ? 1 : 0);
685 remote()->transact(SET_AUDIO_PORT_CALLBACK_ENABLED, data, &reply);
686 }
687
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700688 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
689 audio_io_handle_t *ioHandle,
690 audio_devices_t *device)
691 {
692 if (session == NULL || ioHandle == NULL || device == NULL) {
693 return BAD_VALUE;
694 }
695 Parcel data, reply;
696 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
697 status_t status = remote()->transact(ACQUIRE_SOUNDTRIGGER_SESSION, data, &reply);
698 if (status != NO_ERROR) {
699 return status;
700 }
701 status = (status_t)reply.readInt32();
702 if (status == NO_ERROR) {
703 *session = (audio_session_t)reply.readInt32();
704 *ioHandle = (audio_io_handle_t)reply.readInt32();
705 *device = (audio_devices_t)reply.readInt32();
706 }
707 return status;
708 }
709
710 virtual status_t releaseSoundTriggerSession(audio_session_t session)
711 {
712 Parcel data, reply;
713 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
714 data.writeInt32(session);
715 status_t status = remote()->transact(RELEASE_SOUNDTRIGGER_SESSION, data, &reply);
716 if (status != NO_ERROR) {
717 return status;
718 }
719 return (status_t)reply.readInt32();
720 }
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700721
722 virtual audio_mode_t getPhoneState()
723 {
724 Parcel data, reply;
725 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
726 status_t status = remote()->transact(GET_PHONE_STATE, data, &reply);
727 if (status != NO_ERROR) {
728 return AUDIO_MODE_INVALID;
729 }
730 return (audio_mode_t)reply.readInt32();
731 }
Eric Laurentbaac1832014-12-01 17:52:59 -0800732
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700733 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800734 {
735 Parcel data, reply;
736 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
737 data.writeInt32(registration ? 1 : 0);
738 size_t size = mixes.size();
739 if (size > MAX_MIXES_PER_POLICY) {
740 size = MAX_MIXES_PER_POLICY;
741 }
742 size_t sizePosition = data.dataPosition();
743 data.writeInt32(size);
744 size_t finalSize = size;
745 for (size_t i = 0; i < size; i++) {
746 size_t position = data.dataPosition();
747 if (mixes[i].writeToParcel(&data) != NO_ERROR) {
748 data.setDataPosition(position);
749 finalSize--;
750 }
751 }
752 if (size != finalSize) {
753 size_t position = data.dataPosition();
754 data.setDataPosition(sizePosition);
755 data.writeInt32(finalSize);
756 data.setDataPosition(position);
757 }
758 status_t status = remote()->transact(REGISTER_POLICY_MIXES, data, &reply);
759 if (status == NO_ERROR) {
760 status = (status_t)reply.readInt32();
761 }
762 return status;
763 }
Eric Laurent554a2772015-04-10 11:29:24 -0700764
765 virtual status_t startAudioSource(const struct audio_port_config *source,
766 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700767 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -0700768 {
769 Parcel data, reply;
770 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700771 if (source == NULL || attributes == NULL || portId == NULL) {
Eric Laurent554a2772015-04-10 11:29:24 -0700772 return BAD_VALUE;
773 }
774 data.write(source, sizeof(struct audio_port_config));
775 data.write(attributes, sizeof(audio_attributes_t));
776 status_t status = remote()->transact(START_AUDIO_SOURCE, data, &reply);
777 if (status != NO_ERROR) {
778 return status;
779 }
780 status = (status_t)reply.readInt32();
781 if (status != NO_ERROR) {
782 return status;
783 }
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700784 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -0700785 return status;
786 }
787
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700788 virtual status_t stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -0700789 {
790 Parcel data, reply;
791 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700792 data.writeInt32(portId);
Eric Laurent554a2772015-04-10 11:29:24 -0700793 status_t status = remote()->transact(STOP_AUDIO_SOURCE, data, &reply);
794 if (status != NO_ERROR) {
795 return status;
796 }
797 status = (status_t)reply.readInt32();
798 return status;
799 }
Andy Hung2ddee192015-12-18 17:34:44 -0800800
801 virtual status_t setMasterMono(bool mono)
802 {
803 Parcel data, reply;
804 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
805 data.writeInt32(static_cast<int32_t>(mono));
806 status_t status = remote()->transact(SET_MASTER_MONO, data, &reply);
807 if (status != NO_ERROR) {
808 return status;
809 }
810 return static_cast<status_t>(reply.readInt32());
811 }
812
813 virtual status_t getMasterMono(bool *mono)
814 {
815 if (mono == nullptr) {
816 return BAD_VALUE;
817 }
818 Parcel data, reply;
819 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
820
821 status_t status = remote()->transact(GET_MASTER_MONO, data, &reply);
822 if (status != NO_ERROR) {
823 return status;
824 }
825 status = static_cast<status_t>(reply.readInt32());
826 if (status == NO_ERROR) {
827 *mono = static_cast<bool>(reply.readInt32());
828 }
829 return status;
830 }
Eric Laurentac9cef52017-06-09 15:46:26 -0700831
832 virtual float getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
833 {
834 Parcel data, reply;
835 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
836 data.writeInt32(static_cast <int32_t>(stream));
837 data.writeInt32(static_cast <int32_t>(index));
838 data.writeUint32(static_cast <uint32_t>(device));
839 status_t status = remote()->transact(GET_STREAM_VOLUME_DB, data, &reply);
840 if (status != NO_ERROR) {
841 return NAN;
842 }
843 return reply.readFloat();
844 }
jiabin81772902018-04-02 17:52:27 -0700845
846 virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
847 audio_format_t *surroundFormats,
848 bool *surroundFormatsEnabled,
849 bool reported)
850 {
851 if (numSurroundFormats == NULL || (*numSurroundFormats != 0 &&
852 (surroundFormats == NULL || surroundFormatsEnabled == NULL))) {
853 return BAD_VALUE;
854 }
855 Parcel data, reply;
856 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
857 unsigned int numSurroundFormatsReq = *numSurroundFormats;
858 data.writeUint32(numSurroundFormatsReq);
859 data.writeBool(reported);
860 status_t status = remote()->transact(GET_SURROUND_FORMATS, data, &reply);
861 if (status == NO_ERROR && (status = (status_t)reply.readInt32()) == NO_ERROR) {
862 *numSurroundFormats = reply.readUint32();
863 }
864 if (status == NO_ERROR) {
865 if (numSurroundFormatsReq > *numSurroundFormats) {
866 numSurroundFormatsReq = *numSurroundFormats;
867 }
868 if (numSurroundFormatsReq > 0) {
869 status = reply.read(surroundFormats,
870 numSurroundFormatsReq * sizeof(audio_format_t));
871 if (status != NO_ERROR) {
872 return status;
873 }
874 status = reply.read(surroundFormatsEnabled,
875 numSurroundFormatsReq * sizeof(bool));
876 }
877 }
878 return status;
879 }
880
881 virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
882 {
883 Parcel data, reply;
884 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
885 data.writeInt32(audioFormat);
886 data.writeBool(enabled);
887 status_t status = remote()->transact(SET_SURROUND_FORMAT_ENABLED, data, &reply);
888 if (status != NO_ERROR) {
889 return status;
890 }
891 return reply.readInt32();
892 }
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700893
Arun Mirpuri11029ad2018-12-19 20:45:19 -0800894 virtual status_t getHwOffloadEncodingFormatsSupportedForA2DP(
895 std::vector<audio_format_t> *formats)
896 {
897 if (formats == NULL) {
898 return BAD_VALUE;
899 }
900
901 Parcel data, reply;
902 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
903 status_t status = remote()->transact(GET_OFFLOAD_FORMATS_A2DP, data, &reply);
904 if (status != NO_ERROR || (status = (status_t)reply.readInt32()) != NO_ERROR) {
905 return status;
906 }
907
908 size_t list_size = reply.readUint32();
909
910 for (size_t i = 0; i < list_size; i++) {
911 formats->push_back(static_cast<audio_format_t>(reply.readInt32()));
912 }
913 return NO_ERROR;
914 }
915
916
917 virtual status_t addStreamDefaultEffect(const effect_uuid_t *type,
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700918 const String16& opPackageName,
919 const effect_uuid_t *uuid,
920 int32_t priority,
921 audio_usage_t usage,
922 audio_unique_id_t* id)
923 {
924 Parcel data, reply;
925 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
926 data.write(type, sizeof(effect_uuid_t));
927 data.writeString16(opPackageName);
928 data.write(uuid, sizeof(effect_uuid_t));
929 data.writeInt32(priority);
930 data.writeInt32((int32_t) usage);
931 status_t status = remote()->transact(ADD_STREAM_DEFAULT_EFFECT, data, &reply);
932 if (status != NO_ERROR) {
933 return status;
934 }
935 status = static_cast <status_t> (reply.readInt32());
936 *id = reply.readInt32();
937 return status;
938 }
939
940 virtual status_t removeStreamDefaultEffect(audio_unique_id_t id)
941 {
942 Parcel data, reply;
943 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
944 data.writeInt32(id);
945 status_t status = remote()->transact(REMOVE_STREAM_DEFAULT_EFFECT, data, &reply);
946 if (status != NO_ERROR) {
947 return status;
948 }
949 return static_cast <status_t> (reply.readInt32());
950 }
951
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700952 virtual status_t addSourceDefaultEffect(const effect_uuid_t *type,
953 const String16& opPackageName,
954 const effect_uuid_t *uuid,
955 int32_t priority,
956 audio_source_t source,
957 audio_unique_id_t* id)
958 {
959 Parcel data, reply;
960 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
961 data.write(type, sizeof(effect_uuid_t));
962 data.writeString16(opPackageName);
963 data.write(uuid, sizeof(effect_uuid_t));
964 data.writeInt32(priority);
965 data.writeInt32((int32_t) source);
966 status_t status = remote()->transact(ADD_SOURCE_DEFAULT_EFFECT, data, &reply);
967 if (status != NO_ERROR) {
968 return status;
969 }
970 status = static_cast <status_t> (reply.readInt32());
971 *id = reply.readInt32();
972 return status;
973 }
974
975 virtual status_t removeSourceDefaultEffect(audio_unique_id_t id)
976 {
977 Parcel data, reply;
978 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
979 data.writeInt32(id);
980 status_t status = remote()->transact(REMOVE_SOURCE_DEFAULT_EFFECT, data, &reply);
981 if (status != NO_ERROR) {
982 return status;
983 }
984 return static_cast <status_t> (reply.readInt32());
985 }
986
Eric Laurentb78763e2018-10-17 10:08:02 -0700987 virtual status_t setAssistantUid(uid_t uid)
988 {
989 Parcel data, reply;
990 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
991 data.writeInt32(uid);
992 status_t status = remote()->transact(SET_ASSISTANT_UID, data, &reply);
993 if (status != NO_ERROR) {
994 return status;
995 }
996 return static_cast <status_t> (reply.readInt32());
997 }
998
999 virtual status_t setA11yServicesUids(const std::vector<uid_t>& uids)
1000 {
1001 Parcel data, reply;
1002 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1003 data.writeInt32(uids.size());
1004 for (auto uid : uids) {
1005 data.writeInt32(uid);
1006 }
1007 status_t status = remote()->transact(SET_A11Y_SERVICES_UIDS, data, &reply);
1008 if (status != NO_ERROR) {
1009 return status;
1010 }
1011 return static_cast <status_t> (reply.readInt32());
1012 }
1013
jiabin6012f912018-11-02 17:06:30 -07001014 virtual bool isHapticPlaybackSupported()
1015 {
1016 Parcel data, reply;
1017 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1018 status_t status = remote()->transact(IS_HAPTIC_PLAYBACK_SUPPORTED, data, &reply);
1019 if (status != NO_ERROR) {
1020 return false;
1021 }
1022 return reply.readBool();
1023 }
1024
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001025 virtual status_t setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1026 {
1027 Parcel data, reply;
1028 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1029
1030 data.writeInt32((int32_t) uid);
1031 size_t size = devices.size();
1032 size_t sizePosition = data.dataPosition();
1033 data.writeInt32((int32_t) size);
1034 size_t finalSize = size;
1035 for (size_t i = 0; i < size; i++) {
1036 size_t position = data.dataPosition();
1037 if (devices[i].writeToParcel(&data) != NO_ERROR) {
1038 data.setDataPosition(position);
1039 finalSize--;
1040 }
1041 }
1042 if (size != finalSize) {
1043 size_t position = data.dataPosition();
1044 data.setDataPosition(sizePosition);
1045 data.writeInt32(finalSize);
1046 data.setDataPosition(position);
1047 }
1048
1049 status_t status = remote()->transact(SET_UID_DEVICE_AFFINITY, data, &reply);
1050 if (status == NO_ERROR) {
1051 status = (status_t)reply.readInt32();
1052 }
1053 return status;
1054 }
1055
François Gaffied0ba9ed2018-11-05 11:50:42 +01001056 virtual status_t removeUidDeviceAffinities(uid_t uid) {
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001057 Parcel data, reply;
1058 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1059
1060 data.writeInt32((int32_t) uid);
1061
François Gaffied0ba9ed2018-11-05 11:50:42 +01001062 status_t status =
1063 remote()->transact(REMOVE_UID_DEVICE_AFFINITY, data, &reply);
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001064 if (status == NO_ERROR) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001065 status = (status_t) reply.readInt32();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001066 }
1067 return status;
1068 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001069
1070 virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies)
1071 {
1072 Parcel data, reply;
1073 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1074
1075 status_t status = remote()->transact(LIST_AUDIO_PRODUCT_STRATEGIES, data, &reply);
1076 if (status != NO_ERROR) {
1077 ALOGE("%s: permission denied", __func__);
1078 return status;
1079 }
1080 status = static_cast<status_t>(reply.readInt32());
1081 if (status == NO_ERROR) {
1082 uint32_t numStrategies = static_cast<uint32_t>(reply.readInt32());
1083 for (size_t i = 0; i < numStrategies; i++) {
1084 AudioProductStrategy strategy;
1085 status = strategy.readFromParcel(&reply);
1086 if (status != NO_ERROR) {
1087 ALOGE("%s: failed to read strategies", __FUNCTION__);
1088 strategies.clear();
1089 return status;
1090 }
1091 strategies.push_back(strategy);
1092 }
1093 }
1094 return status;
1095 }
1096
1097 virtual product_strategy_t getProductStrategyFromAudioAttributes(const AudioAttributes &aa)
1098 {
1099 Parcel data, reply;
1100 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1101 status_t status = aa.writeToParcel(&data);
1102 if (status != NO_ERROR) {
1103 return PRODUCT_STRATEGY_NONE;
1104 }
1105 status = remote()->transact(GET_STRATEGY_FOR_ATTRIBUTES, data, &reply);
1106 if (status == NO_ERROR) {
1107 return static_cast<product_strategy_t>(reply.readInt32());
1108 }
1109 return PRODUCT_STRATEGY_NONE;
1110 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001111};
1112
1113IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
1114
1115// ----------------------------------------------------------------------
1116
Eric Laurentc2f1f072009-07-17 12:17:14 -07001117status_t BnAudioPolicyService::onTransact(
1118 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1119{
Eric Laurentb1cc36b2017-12-11 12:14:16 -08001120 // make sure transactions reserved to AudioFlinger do not come from other processes
1121 switch (code) {
1122 case START_OUTPUT:
1123 case STOP_OUTPUT:
1124 case RELEASE_OUTPUT:
1125 case GET_INPUT_FOR_ATTR:
1126 case START_INPUT:
1127 case STOP_INPUT:
1128 case RELEASE_INPUT:
Eric Laurentb1cc36b2017-12-11 12:14:16 -08001129 case GET_OUTPUT_FOR_EFFECT:
1130 case REGISTER_EFFECT:
1131 case UNREGISTER_EFFECT:
1132 case SET_EFFECT_ENABLED:
1133 case GET_OUTPUT_FOR_ATTR:
1134 case ACQUIRE_SOUNDTRIGGER_SESSION:
1135 case RELEASE_SOUNDTRIGGER_SESSION:
1136 ALOGW("%s: transaction %d received from PID %d",
1137 __func__, code, IPCThreadState::self()->getCallingPid());
Eric Laurentef92bff2018-04-26 10:44:50 -07001138 // return status only for non void methods
1139 switch (code) {
1140 case RELEASE_OUTPUT:
1141 case RELEASE_INPUT:
1142 break;
1143 default:
1144 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
1145 break;
1146 }
1147 return OK;
Eric Laurentb1cc36b2017-12-11 12:14:16 -08001148 default:
1149 break;
1150 }
1151
Eric Laurent4980df22018-01-26 18:04:09 -08001152 // make sure the following transactions come from system components
1153 switch (code) {
1154 case SET_DEVICE_CONNECTION_STATE:
1155 case HANDLE_DEVICE_CONFIG_CHANGE:
1156 case SET_PHONE_STATE:
Eric Laurente17378d2018-05-09 14:43:01 -07001157//FIXME: Allow SET_FORCE_USE calls from system apps until a better use case routing API is available
1158// case SET_FORCE_USE:
Eric Laurent4980df22018-01-26 18:04:09 -08001159 case INIT_STREAM_VOLUME:
1160 case SET_STREAM_VOLUME:
1161 case REGISTER_POLICY_MIXES:
Eric Laurent10b71232018-04-13 18:14:44 -07001162 case SET_MASTER_MONO:
1163 case START_AUDIO_SOURCE:
jiabin81772902018-04-02 17:52:27 -07001164 case STOP_AUDIO_SOURCE:
1165 case GET_SURROUND_FORMATS:
Eric Laurentb78763e2018-10-17 10:08:02 -07001166 case SET_SURROUND_FORMAT_ENABLED:
1167 case SET_ASSISTANT_UID:
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001168 case SET_A11Y_SERVICES_UIDS:
1169 case SET_UID_DEVICE_AFFINITY:
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001170 case REMOVE_UID_DEVICE_AFFINITY:
1171 case GET_OFFLOAD_FORMATS_A2DP: {
Andy Hung4ef19fa2018-05-15 19:35:29 -07001172 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
Eric Laurent4980df22018-01-26 18:04:09 -08001173 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1174 __func__, code, IPCThreadState::self()->getCallingPid(),
1175 IPCThreadState::self()->getCallingUid());
Eric Laurentef92bff2018-04-26 10:44:50 -07001176 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
1177 return OK;
Eric Laurent4980df22018-01-26 18:04:09 -08001178 }
Eric Laurent96c7eed2018-03-26 17:57:01 -07001179 } break;
Eric Laurent4980df22018-01-26 18:04:09 -08001180 default:
1181 break;
1182 }
1183
Eric Laurent39b09b52018-06-29 12:24:40 -07001184 std::string tag("IAudioPolicyService command " + std::to_string(code));
1185 TimeCheck check(tag.c_str());
Eric Laurent3528c932018-02-23 17:17:22 -08001186
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001187 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001188 case SET_DEVICE_CONNECTION_STATE: {
1189 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001190 audio_devices_t device =
1191 static_cast <audio_devices_t>(data.readInt32());
1192 audio_policy_dev_state_t state =
1193 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001194 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -08001195 const char *device_name = data.readCString();
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001196 audio_format_t codecFormat = static_cast <audio_format_t>(data.readInt32());
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001197 if (device_address == nullptr || device_name == nullptr) {
1198 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
1199 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1200 } else {
1201 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
1202 state,
1203 device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001204 device_name,
1205 codecFormat)));
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001206 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001207 return NO_ERROR;
1208 } break;
1209
1210 case GET_DEVICE_CONNECTION_STATE: {
1211 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001212 audio_devices_t device =
1213 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001214 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001215 if (device_address == nullptr) {
1216 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
1217 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
1218 } else {
1219 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
1220 device_address)));
1221 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001222 return NO_ERROR;
1223 } break;
1224
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001225 case HANDLE_DEVICE_CONFIG_CHANGE: {
1226 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1227 audio_devices_t device =
1228 static_cast <audio_devices_t>(data.readInt32());
1229 const char *device_address = data.readCString();
1230 const char *device_name = data.readCString();
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001231 audio_format_t codecFormat =
1232 static_cast <audio_format_t>(data.readInt32());
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001233 if (device_address == nullptr || device_name == nullptr) {
1234 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
1235 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1236 } else {
1237 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
1238 device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001239 device_name,
1240 codecFormat)));
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001241 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001242 return NO_ERROR;
1243 } break;
1244
Eric Laurentc2f1f072009-07-17 12:17:14 -07001245 case SET_PHONE_STATE: {
1246 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001247 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
1248 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001249 return NO_ERROR;
1250 } break;
1251
Eric Laurentc2f1f072009-07-17 12:17:14 -07001252 case SET_FORCE_USE: {
1253 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001254 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1255 data.readInt32());
Dima Zavinfce7a472011-04-19 22:30:36 -07001256 audio_policy_forced_cfg_t config =
1257 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001258 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
1259 return NO_ERROR;
1260 } break;
1261
1262 case GET_FORCE_USE: {
1263 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001264 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1265 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001266 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
1267 return NO_ERROR;
1268 } break;
1269
1270 case GET_OUTPUT: {
1271 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001272 audio_stream_type_t stream =
1273 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +00001274 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001275 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001276 return NO_ERROR;
1277 } break;
1278
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001279 case GET_OUTPUT_FOR_ATTR: {
1280 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001281 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -08001282 bool hasAttributes = data.readInt32() != 0;
1283 if (hasAttributes) {
1284 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001285 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -08001286 }
1287 audio_session_t session = (audio_session_t)data.readInt32();
1288 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
1289 bool hasStream = data.readInt32() != 0;
1290 if (hasStream) {
1291 stream = (audio_stream_type_t)data.readInt32();
1292 }
Nadav Bar766fb022018-01-07 12:18:03 +02001293 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001294 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001295 audio_config_t config;
1296 memset(&config, 0, sizeof(audio_config_t));
1297 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001298 audio_output_flags_t flags =
1299 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001300 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001301 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001302 audio_io_handle_t output = 0;
Eric Laurente83b55d2014-11-14 10:06:21 -08001303 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001304 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001305 &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001306 flags, &selectedDeviceId, &portId);
Eric Laurente83b55d2014-11-14 10:06:21 -08001307 reply->writeInt32(status);
1308 reply->writeInt32(output);
1309 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001310 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001311 reply->writeInt32(portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001312 return NO_ERROR;
1313 } break;
1314
Eric Laurentc2f1f072009-07-17 12:17:14 -07001315 case START_OUTPUT: {
1316 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001317 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1318 reply->writeInt32(static_cast <uint32_t>(startOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001319 return NO_ERROR;
1320 } break;
1321
1322 case STOP_OUTPUT: {
1323 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001324 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1325 reply->writeInt32(static_cast <uint32_t>(stopOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001326 return NO_ERROR;
1327 } break;
1328
1329 case RELEASE_OUTPUT: {
1330 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001331 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1332 releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001333 return NO_ERROR;
1334 } break;
1335
Eric Laurentcaf7f482014-11-25 17:50:47 -08001336 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001337 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001338 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001339 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001340 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001341 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001342 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001343 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001344 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001345 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001346 audio_config_base_t config;
1347 memset(&config, 0, sizeof(audio_config_base_t));
1348 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001349 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001350 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001351 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001352 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001353 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001354 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001355 reply->writeInt32(status);
1356 if (status == NO_ERROR) {
1357 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001358 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001359 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001360 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001361 return NO_ERROR;
1362 } break;
1363
1364 case START_INPUT: {
1365 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001366 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
Eric Laurent4eb58f12018-12-07 16:41:02 -08001367 status_t status = startInput(portId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001368 reply->writeInt32(static_cast <uint32_t>(status));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001369 return NO_ERROR;
1370 } break;
1371
1372 case STOP_INPUT: {
1373 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001374 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1375 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001376 return NO_ERROR;
1377 } break;
1378
1379 case RELEASE_INPUT: {
1380 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001381 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1382 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001383 return NO_ERROR;
1384 } break;
1385
1386 case INIT_STREAM_VOLUME: {
1387 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001388 audio_stream_type_t stream =
1389 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001390 int indexMin = data.readInt32();
1391 int indexMax = data.readInt32();
1392 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1393 return NO_ERROR;
1394 } break;
1395
1396 case SET_STREAM_VOLUME: {
1397 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001398 audio_stream_type_t stream =
1399 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001400 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001401 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1402 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1403 index,
1404 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001405 return NO_ERROR;
1406 } break;
1407
1408 case GET_STREAM_VOLUME: {
1409 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001410 audio_stream_type_t stream =
1411 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurent83844cc2011-11-18 16:43:31 -08001412 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001413 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001414 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001415 reply->writeInt32(index);
1416 reply->writeInt32(static_cast <uint32_t>(status));
1417 return NO_ERROR;
1418 } break;
1419
Eric Laurentde070132010-07-13 04:45:46 -07001420 case GET_STRATEGY_FOR_STREAM: {
1421 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001422 audio_stream_type_t stream =
1423 static_cast <audio_stream_type_t>(data.readInt32());
François Gaffiec005e562018-11-06 15:04:49 +01001424 reply->writeUint32(getStrategyForStream(stream));
Eric Laurentde070132010-07-13 04:45:46 -07001425 return NO_ERROR;
1426 } break;
1427
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001428 case GET_DEVICES_FOR_STREAM: {
1429 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001430 audio_stream_type_t stream =
1431 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001432 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1433 return NO_ERROR;
1434 } break;
1435
Eric Laurentde070132010-07-13 04:45:46 -07001436 case GET_OUTPUT_FOR_EFFECT: {
1437 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001438 effect_descriptor_t desc = {};
1439 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1440 android_errorWriteLog(0x534e4554, "73126106");
1441 }
1442 (void)sanitizeEffectDescriptor(&desc);
Eric Laurentde070132010-07-13 04:45:46 -07001443 audio_io_handle_t output = getOutputForEffect(&desc);
1444 reply->writeInt32(static_cast <int>(output));
1445 return NO_ERROR;
1446 } break;
1447
1448 case REGISTER_EFFECT: {
1449 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001450 effect_descriptor_t desc = {};
1451 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1452 android_errorWriteLog(0x534e4554, "73126106");
1453 }
1454 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001455 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001456 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001457 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001458 int id = data.readInt32();
1459 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001460 io,
Eric Laurentde070132010-07-13 04:45:46 -07001461 strategy,
1462 session,
1463 id)));
1464 return NO_ERROR;
1465 } break;
1466
1467 case UNREGISTER_EFFECT: {
1468 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1469 int id = data.readInt32();
1470 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1471 return NO_ERROR;
1472 } break;
1473
Eric Laurentdb7c0792011-08-10 10:37:50 -07001474 case SET_EFFECT_ENABLED: {
1475 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1476 int id = data.readInt32();
1477 bool enabled = static_cast <bool>(data.readInt32());
1478 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1479 return NO_ERROR;
1480 } break;
1481
Eric Laurenteda6c362011-02-02 09:33:30 -08001482 case IS_STREAM_ACTIVE: {
1483 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001484 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001485 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001486 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001487 return NO_ERROR;
1488 } break;
1489
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001490 case IS_STREAM_ACTIVE_REMOTELY: {
1491 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1492 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1493 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001494 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001495 return NO_ERROR;
1496 } break;
1497
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001498 case IS_SOURCE_ACTIVE: {
1499 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1500 audio_source_t source = (audio_source_t) data.readInt32();
1501 reply->writeInt32( isSourceActive(source));
1502 return NO_ERROR;
1503 }
1504
Eric Laurent57dae992011-07-24 13:36:09 -07001505 case QUERY_DEFAULT_PRE_PROCESSING: {
1506 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001507 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001508 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001509 if (count > AudioEffect::kMaxPreProcessing) {
1510 count = AudioEffect::kMaxPreProcessing;
1511 }
Eric Laurent57dae992011-07-24 13:36:09 -07001512 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001513 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001514 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1515 reply->writeInt32(status);
1516 if (status != NO_ERROR && status != NO_MEMORY) {
1517 retCount = 0;
1518 }
1519 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001520 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001521 if (retCount < count) {
1522 count = retCount;
1523 }
1524 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1525 }
1526 delete[] descriptors;
1527 return status;
1528 }
1529
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001530 case IS_OFFLOAD_SUPPORTED: {
1531 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001532 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001533 data.read(&info, sizeof(audio_offload_info_t));
1534 bool isSupported = isOffloadSupported(info);
1535 reply->writeInt32(isSupported);
1536 return NO_ERROR;
1537 }
1538
Michael Chana94fbb22018-04-24 14:31:19 +10001539 case IS_DIRECT_OUTPUT_SUPPORTED: {
1540 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1541 audio_config_base_t config = {};
1542 audio_attributes_t attributes = {};
1543 status_t status = data.read(&config, sizeof(audio_config_base_t));
1544 if (status != NO_ERROR) return status;
1545 status = data.read(&attributes, sizeof(audio_attributes_t));
1546 if (status != NO_ERROR) return status;
1547 reply->writeInt32(isDirectOutputSupported(config, attributes));
1548 return NO_ERROR;
1549 }
1550
Eric Laurent203b1a12014-04-01 10:34:16 -07001551 case LIST_AUDIO_PORTS: {
1552 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1553 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1554 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1555 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001556 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1557 numPortsReq = MAX_ITEMS_PER_LIST;
1558 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001559 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001560 struct audio_port *ports =
1561 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001562 if (ports == NULL) {
1563 reply->writeInt32(NO_MEMORY);
1564 reply->writeInt32(0);
1565 return NO_ERROR;
1566 }
1567 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001568 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1569 reply->writeInt32(status);
1570 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001571
1572 if (status == NO_ERROR) {
1573 if (numPortsReq > numPorts) {
1574 numPortsReq = numPorts;
1575 }
1576 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1577 reply->writeInt32(generation);
1578 }
1579 free(ports);
1580 return NO_ERROR;
1581 }
1582
1583 case GET_AUDIO_PORT: {
1584 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001585 struct audio_port port = {};
1586 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1587 ALOGE("b/23912202");
1588 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001589 status_t status = getAudioPort(&port);
1590 reply->writeInt32(status);
1591 if (status == NO_ERROR) {
1592 reply->write(&port, sizeof(struct audio_port));
1593 }
1594 return NO_ERROR;
1595 }
1596
1597 case CREATE_AUDIO_PATCH: {
1598 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001599 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001600 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001601 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001602 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1603 ALOGE("b/23912202");
1604 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001605 status_t status = createAudioPatch(&patch, &handle);
1606 reply->writeInt32(status);
1607 if (status == NO_ERROR) {
1608 reply->write(&handle, sizeof(audio_patch_handle_t));
1609 }
1610 return NO_ERROR;
1611 }
1612
1613 case RELEASE_AUDIO_PATCH: {
1614 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001615 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001616 data.read(&handle, sizeof(audio_patch_handle_t));
1617 status_t status = releaseAudioPatch(handle);
1618 reply->writeInt32(status);
1619 return NO_ERROR;
1620 }
1621
1622 case LIST_AUDIO_PATCHES: {
1623 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1624 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001625 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1626 numPatchesReq = MAX_ITEMS_PER_LIST;
1627 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001628 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001629 struct audio_patch *patches =
1630 (struct audio_patch *)calloc(numPatchesReq,
1631 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001632 if (patches == NULL) {
1633 reply->writeInt32(NO_MEMORY);
1634 reply->writeInt32(0);
1635 return NO_ERROR;
1636 }
1637 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001638 status_t status = listAudioPatches(&numPatches, patches, &generation);
1639 reply->writeInt32(status);
1640 reply->writeInt32(numPatches);
1641 if (status == NO_ERROR) {
1642 if (numPatchesReq > numPatches) {
1643 numPatchesReq = numPatches;
1644 }
1645 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1646 reply->writeInt32(generation);
1647 }
1648 free(patches);
1649 return NO_ERROR;
1650 }
1651
1652 case SET_AUDIO_PORT_CONFIG: {
1653 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001654 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001655 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001656 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001657 status_t status = setAudioPortConfig(&config);
1658 reply->writeInt32(status);
1659 return NO_ERROR;
1660 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001661
Eric Laurentb52c1522014-05-20 11:27:36 -07001662 case REGISTER_CLIENT: {
1663 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1664 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1665 data.readStrongBinder());
1666 registerClient(client);
1667 return NO_ERROR;
1668 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001669
Eric Laurente8726fe2015-06-26 09:39:24 -07001670 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1671 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1672 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1673 return NO_ERROR;
1674 } break;
1675
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001676 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1677 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1678 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1679 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001680 audio_session_t session = AUDIO_SESSION_NONE;
1681 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1682 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001683 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1684 reply->writeInt32(status);
1685 if (status == NO_ERROR) {
1686 reply->writeInt32(session);
1687 reply->writeInt32(ioHandle);
1688 reply->writeInt32(device);
1689 }
1690 return NO_ERROR;
1691 } break;
1692
1693 case RELEASE_SOUNDTRIGGER_SESSION: {
1694 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1695 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1696 data.readStrongBinder());
1697 audio_session_t session = (audio_session_t)data.readInt32();
1698 status_t status = releaseSoundTriggerSession(session);
1699 reply->writeInt32(status);
1700 return NO_ERROR;
1701 } break;
1702
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001703 case GET_PHONE_STATE: {
1704 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1705 reply->writeInt32((int32_t)getPhoneState());
1706 return NO_ERROR;
1707 } break;
1708
Eric Laurentbaac1832014-12-01 17:52:59 -08001709 case REGISTER_POLICY_MIXES: {
1710 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1711 bool registration = data.readInt32() == 1;
1712 Vector<AudioMix> mixes;
1713 size_t size = (size_t)data.readInt32();
1714 if (size > MAX_MIXES_PER_POLICY) {
1715 size = MAX_MIXES_PER_POLICY;
1716 }
1717 for (size_t i = 0; i < size; i++) {
1718 AudioMix mix;
1719 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1720 mixes.add(mix);
1721 }
1722 }
1723 status_t status = registerPolicyMixes(mixes, registration);
1724 reply->writeInt32(status);
1725 return NO_ERROR;
1726 } break;
1727
Eric Laurent554a2772015-04-10 11:29:24 -07001728 case START_AUDIO_SOURCE: {
1729 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001730 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001731 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001732 (void)sanitizeAudioPortConfig(&source);
1733 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001734 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001735 sanetizeAudioAttributes(&attributes);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001736 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
1737 status_t status = startAudioSource(&source, &attributes, &portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001738 reply->writeInt32(status);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001739 reply->writeInt32(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001740 return NO_ERROR;
1741 } break;
1742
1743 case STOP_AUDIO_SOURCE: {
1744 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001745 audio_port_handle_t portId = (audio_port_handle_t) data.readInt32();
1746 status_t status = stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001747 reply->writeInt32(status);
1748 return NO_ERROR;
1749 } break;
1750
Andy Hung2ddee192015-12-18 17:34:44 -08001751 case SET_MASTER_MONO: {
1752 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1753 bool mono = static_cast<bool>(data.readInt32());
1754 status_t status = setMasterMono(mono);
1755 reply->writeInt32(status);
1756 return NO_ERROR;
1757 } break;
1758
1759 case GET_MASTER_MONO: {
1760 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1761 bool mono;
1762 status_t status = getMasterMono(&mono);
1763 reply->writeInt32(status);
1764 if (status == NO_ERROR) {
1765 reply->writeInt32(static_cast<int32_t>(mono));
1766 }
1767 return NO_ERROR;
1768 } break;
1769
Eric Laurentac9cef52017-06-09 15:46:26 -07001770 case GET_STREAM_VOLUME_DB: {
1771 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1772 audio_stream_type_t stream =
1773 static_cast <audio_stream_type_t>(data.readInt32());
1774 int index = static_cast <int>(data.readInt32());
1775 audio_devices_t device =
1776 static_cast <audio_devices_t>(data.readUint32());
1777 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1778 return NO_ERROR;
1779 }
1780
jiabin81772902018-04-02 17:52:27 -07001781 case GET_SURROUND_FORMATS: {
1782 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1783 unsigned int numSurroundFormatsReq = data.readUint32();
1784 if (numSurroundFormatsReq > MAX_ITEMS_PER_LIST) {
1785 numSurroundFormatsReq = MAX_ITEMS_PER_LIST;
1786 }
1787 bool reported = data.readBool();
1788 unsigned int numSurroundFormats = numSurroundFormatsReq;
1789 audio_format_t *surroundFormats = (audio_format_t *)calloc(
1790 numSurroundFormats, sizeof(audio_format_t));
1791 bool *surroundFormatsEnabled = (bool *)calloc(numSurroundFormats, sizeof(bool));
1792 if (numSurroundFormatsReq > 0 &&
1793 (surroundFormats == NULL || surroundFormatsEnabled == NULL)) {
1794 free(surroundFormats);
1795 free(surroundFormatsEnabled);
1796 reply->writeInt32(NO_MEMORY);
1797 return NO_ERROR;
1798 }
1799 status_t status = getSurroundFormats(
1800 &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1801 reply->writeInt32(status);
1802
1803 if (status == NO_ERROR) {
1804 reply->writeUint32(numSurroundFormats);
1805 if (numSurroundFormatsReq > numSurroundFormats) {
1806 numSurroundFormatsReq = numSurroundFormats;
1807 }
1808 reply->write(surroundFormats, numSurroundFormatsReq * sizeof(audio_format_t));
1809 reply->write(surroundFormatsEnabled, numSurroundFormatsReq * sizeof(bool));
1810 }
1811 free(surroundFormats);
1812 free(surroundFormatsEnabled);
1813 return NO_ERROR;
1814 }
1815
1816 case SET_SURROUND_FORMAT_ENABLED: {
1817 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1818 audio_format_t audioFormat = (audio_format_t) data.readInt32();
1819 bool enabled = data.readBool();
1820 status_t status = setSurroundFormatEnabled(audioFormat, enabled);
1821 reply->writeInt32(status);
1822 return NO_ERROR;
1823 }
1824
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001825 case GET_OFFLOAD_FORMATS_A2DP: {
1826 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1827 std::vector<audio_format_t> encodingFormats;
1828 status_t status = getHwOffloadEncodingFormatsSupportedForA2DP(&encodingFormats);
1829 reply->writeInt32(status);
1830 if (status != NO_ERROR) {
1831 return NO_ERROR;
1832 }
1833 reply->writeUint32(static_cast<uint32_t>(encodingFormats.size()));
1834 for (size_t i = 0; i < encodingFormats.size(); i++)
1835 reply->writeInt32(static_cast<int32_t>(encodingFormats[i]));
1836 return NO_ERROR;
1837 }
1838
1839
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -07001840 case ADD_STREAM_DEFAULT_EFFECT: {
1841 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1842 effect_uuid_t type;
1843 status_t status = data.read(&type, sizeof(effect_uuid_t));
1844 if (status != NO_ERROR) {
1845 return status;
1846 }
1847 String16 opPackageName;
1848 status = data.readString16(&opPackageName);
1849 if (status != NO_ERROR) {
1850 return status;
1851 }
1852 effect_uuid_t uuid;
1853 status = data.read(&uuid, sizeof(effect_uuid_t));
1854 if (status != NO_ERROR) {
1855 return status;
1856 }
1857 int32_t priority = data.readInt32();
1858 audio_usage_t usage = (audio_usage_t) data.readInt32();
1859 audio_unique_id_t id = 0;
1860 reply->writeInt32(static_cast <int32_t>(addStreamDefaultEffect(&type,
1861 opPackageName,
1862 &uuid,
1863 priority,
1864 usage,
1865 &id)));
1866 reply->writeInt32(id);
1867 return NO_ERROR;
1868 }
1869
1870 case REMOVE_STREAM_DEFAULT_EFFECT: {
1871 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1872 audio_unique_id_t id = static_cast<audio_unique_id_t>(data.readInt32());
1873 reply->writeInt32(static_cast <int32_t>(removeStreamDefaultEffect(id)));
1874 return NO_ERROR;
1875 }
1876
Ari Hausman-Cohen24628312018-08-13 15:01:09 -07001877 case ADD_SOURCE_DEFAULT_EFFECT: {
1878 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1879 effect_uuid_t type;
1880 status_t status = data.read(&type, sizeof(effect_uuid_t));
1881 if (status != NO_ERROR) {
1882 return status;
1883 }
1884 String16 opPackageName;
1885 status = data.readString16(&opPackageName);
1886 if (status != NO_ERROR) {
1887 return status;
1888 }
1889 effect_uuid_t uuid;
1890 status = data.read(&uuid, sizeof(effect_uuid_t));
1891 if (status != NO_ERROR) {
1892 return status;
1893 }
1894 int32_t priority = data.readInt32();
1895 audio_source_t source = (audio_source_t) data.readInt32();
1896 audio_unique_id_t id = 0;
1897 reply->writeInt32(static_cast <int32_t>(addSourceDefaultEffect(&type,
1898 opPackageName,
1899 &uuid,
1900 priority,
1901 source,
1902 &id)));
1903 reply->writeInt32(id);
1904 return NO_ERROR;
1905 }
1906
1907 case REMOVE_SOURCE_DEFAULT_EFFECT: {
1908 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1909 audio_unique_id_t id = static_cast<audio_unique_id_t>(data.readInt32());
1910 reply->writeInt32(static_cast <int32_t>(removeSourceDefaultEffect(id)));
1911 return NO_ERROR;
1912 }
1913
Eric Laurentb78763e2018-10-17 10:08:02 -07001914 case SET_ASSISTANT_UID: {
1915 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1916 int32_t uid;
1917 status_t status = data.readInt32(&uid);
1918 if (status != NO_ERROR) {
1919 return status;
1920 }
1921 status = setAssistantUid(uid);
1922 reply->writeInt32(static_cast <int32_t>(status));
1923 return NO_ERROR;
1924 }
1925
1926 case SET_A11Y_SERVICES_UIDS: {
1927 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1928 std::vector<uid_t> uids;
1929 int32_t size;
1930 status_t status = data.readInt32(&size);
1931 if (status != NO_ERROR) {
1932 return status;
1933 }
1934 if (size > MAX_ITEMS_PER_LIST) {
1935 size = MAX_ITEMS_PER_LIST;
1936 }
1937 for (int32_t i = 0; i < size; i++) {
1938 int32_t uid;
1939 status = data.readInt32(&uid);
1940 if (status != NO_ERROR) {
1941 return status;
1942 }
1943 uids.push_back(uid);
1944 }
1945 status = setA11yServicesUids(uids);
1946 reply->writeInt32(static_cast <int32_t>(status));
1947 return NO_ERROR;
1948 }
1949
jiabin6012f912018-11-02 17:06:30 -07001950 case IS_HAPTIC_PLAYBACK_SUPPORTED: {
1951 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1952 bool isSupported = isHapticPlaybackSupported();
1953 reply->writeBool(isSupported);
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001954 return NO_ERROR;
1955 }
1956
1957 case SET_UID_DEVICE_AFFINITY: {
1958 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1959 const uid_t uid = (uid_t) data.readInt32();
1960 Vector<AudioDeviceTypeAddr> devices;
1961 size_t size = (size_t)data.readInt32();
1962 for (size_t i = 0; i < size; i++) {
1963 AudioDeviceTypeAddr device;
1964 if (device.readFromParcel((Parcel*)&data) == NO_ERROR) {
1965 devices.add(device);
1966 }
1967 }
1968 status_t status = setUidDeviceAffinities(uid, devices);
1969 reply->writeInt32(status);
1970 return NO_ERROR;
1971 }
1972
1973 case REMOVE_UID_DEVICE_AFFINITY: {
1974 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1975 const uid_t uid = (uid_t) data.readInt32();
1976 status_t status = removeUidDeviceAffinities(uid);
1977 reply->writeInt32(status);
jiabin6012f912018-11-02 17:06:30 -07001978 return NO_ERROR;
1979 }
1980
François Gaffied0ba9ed2018-11-05 11:50:42 +01001981 case LIST_AUDIO_PRODUCT_STRATEGIES: {
1982 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1983 AudioProductStrategyVector strategies;
1984 status_t status = listAudioProductStrategies(strategies);
1985 reply->writeInt32(status);
1986 if (status != NO_ERROR) {
1987 return status;
1988 }
1989 size_t size = strategies.size();
1990 size_t sizePosition = reply->dataPosition();
1991 reply->writeInt32(size);
1992 size_t finalSize = size;
1993 for (size_t i = 0; i < size; i++) {
1994 size_t position = reply->dataPosition();
1995 if (strategies[i].writeToParcel(reply) != NO_ERROR) {
1996 reply->setDataPosition(position);
1997 finalSize--;
1998 }
1999 }
2000 if (size != finalSize) {
2001 size_t position = reply->dataPosition();
2002 reply->setDataPosition(sizePosition);
2003 reply->writeInt32(finalSize);
2004 reply->setDataPosition(position);
2005 }
2006 return NO_ERROR;
2007 }
2008
2009 case GET_STRATEGY_FOR_ATTRIBUTES: {
2010 CHECK_INTERFACE(IAudioPolicyService, data, reply);
2011 AudioAttributes attributes;
2012 status_t status = attributes.readFromParcel(&data);
2013 if (status != NO_ERROR) {
2014 return status;
2015 }
2016 product_strategy_t strategy = getProductStrategyFromAudioAttributes(attributes);
2017 reply->writeUint32(static_cast<int>(strategy));
2018 return NO_ERROR;
2019 }
2020
Eric Laurentc2f1f072009-07-17 12:17:14 -07002021 default:
2022 return BBinder::onTransact(code, data, reply, flags);
2023 }
2024}
2025
Andy Hungb0272092018-04-12 11:06:56 -07002026/** returns true if string overflow was prevented by zero termination */
2027template <size_t size>
2028static bool preventStringOverflow(char (&s)[size]) {
2029 if (strnlen(s, size) < size) return false;
2030 s[size - 1] = '\0';
2031 return true;
2032}
2033
Kevin Rocard39fdbd02017-11-13 11:15:27 -08002034void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
2035{
2036 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
2037 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
2038 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
2039 }
2040 attr->tags[tagsMaxSize - 1] = '\0';
2041}
2042
Andy Hungb0272092018-04-12 11:06:56 -07002043/** returns BAD_VALUE if sanitization was required. */
2044status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
2045{
2046 if (preventStringOverflow(desc->name)
2047 | /* always */ preventStringOverflow(desc->implementor)) {
2048 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
2049 return BAD_VALUE;
2050 }
2051 return NO_ERROR;
2052}
2053
2054/** returns BAD_VALUE if sanitization was required. */
2055status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
2056{
2057 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
2058 preventStringOverflow(config->ext.device.address)) {
2059 return BAD_VALUE;
2060 }
2061 return NO_ERROR;
2062}
2063
Eric Laurentc2f1f072009-07-17 12:17:14 -07002064// ----------------------------------------------------------------------------
2065
Glenn Kasten40bc9062015-03-20 09:09:33 -07002066} // namespace android