blob: edd8e8011d02de979569a6773b86a315733c57c2 [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);
417 return reply.readInt32();
418 }
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:
1129 case GET_STRATEGY_FOR_STREAM:
1130 case GET_OUTPUT_FOR_EFFECT:
1131 case REGISTER_EFFECT:
1132 case UNREGISTER_EFFECT:
1133 case SET_EFFECT_ENABLED:
1134 case GET_OUTPUT_FOR_ATTR:
1135 case ACQUIRE_SOUNDTRIGGER_SESSION:
1136 case RELEASE_SOUNDTRIGGER_SESSION:
1137 ALOGW("%s: transaction %d received from PID %d",
1138 __func__, code, IPCThreadState::self()->getCallingPid());
Eric Laurentef92bff2018-04-26 10:44:50 -07001139 // return status only for non void methods
1140 switch (code) {
1141 case RELEASE_OUTPUT:
1142 case RELEASE_INPUT:
1143 break;
1144 default:
1145 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
1146 break;
1147 }
1148 return OK;
Eric Laurentb1cc36b2017-12-11 12:14:16 -08001149 default:
1150 break;
1151 }
1152
Eric Laurent4980df22018-01-26 18:04:09 -08001153 // make sure the following transactions come from system components
1154 switch (code) {
1155 case SET_DEVICE_CONNECTION_STATE:
1156 case HANDLE_DEVICE_CONFIG_CHANGE:
1157 case SET_PHONE_STATE:
Eric Laurente17378d2018-05-09 14:43:01 -07001158//FIXME: Allow SET_FORCE_USE calls from system apps until a better use case routing API is available
1159// case SET_FORCE_USE:
Eric Laurent4980df22018-01-26 18:04:09 -08001160 case INIT_STREAM_VOLUME:
1161 case SET_STREAM_VOLUME:
1162 case REGISTER_POLICY_MIXES:
Eric Laurent10b71232018-04-13 18:14:44 -07001163 case SET_MASTER_MONO:
1164 case START_AUDIO_SOURCE:
jiabin81772902018-04-02 17:52:27 -07001165 case STOP_AUDIO_SOURCE:
1166 case GET_SURROUND_FORMATS:
Eric Laurentb78763e2018-10-17 10:08:02 -07001167 case SET_SURROUND_FORMAT_ENABLED:
1168 case SET_ASSISTANT_UID:
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001169 case SET_A11Y_SERVICES_UIDS:
1170 case SET_UID_DEVICE_AFFINITY:
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001171 case REMOVE_UID_DEVICE_AFFINITY:
1172 case GET_OFFLOAD_FORMATS_A2DP: {
Andy Hung4ef19fa2018-05-15 19:35:29 -07001173 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
Eric Laurent4980df22018-01-26 18:04:09 -08001174 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1175 __func__, code, IPCThreadState::self()->getCallingPid(),
1176 IPCThreadState::self()->getCallingUid());
Eric Laurentef92bff2018-04-26 10:44:50 -07001177 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
1178 return OK;
Eric Laurent4980df22018-01-26 18:04:09 -08001179 }
Eric Laurent96c7eed2018-03-26 17:57:01 -07001180 } break;
Eric Laurent4980df22018-01-26 18:04:09 -08001181 default:
1182 break;
1183 }
1184
Eric Laurent39b09b52018-06-29 12:24:40 -07001185 std::string tag("IAudioPolicyService command " + std::to_string(code));
1186 TimeCheck check(tag.c_str());
Eric Laurent3528c932018-02-23 17:17:22 -08001187
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001188 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001189 case SET_DEVICE_CONNECTION_STATE: {
1190 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001191 audio_devices_t device =
1192 static_cast <audio_devices_t>(data.readInt32());
1193 audio_policy_dev_state_t state =
1194 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001195 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -08001196 const char *device_name = data.readCString();
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001197 audio_format_t codecFormat = static_cast <audio_format_t>(data.readInt32());
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001198 if (device_address == nullptr || device_name == nullptr) {
1199 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
1200 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1201 } else {
1202 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
1203 state,
1204 device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001205 device_name,
1206 codecFormat)));
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001207 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001208 return NO_ERROR;
1209 } break;
1210
1211 case GET_DEVICE_CONNECTION_STATE: {
1212 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001213 audio_devices_t device =
1214 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001215 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001216 if (device_address == nullptr) {
1217 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
1218 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
1219 } else {
1220 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
1221 device_address)));
1222 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001223 return NO_ERROR;
1224 } break;
1225
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001226 case HANDLE_DEVICE_CONFIG_CHANGE: {
1227 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1228 audio_devices_t device =
1229 static_cast <audio_devices_t>(data.readInt32());
1230 const char *device_address = data.readCString();
1231 const char *device_name = data.readCString();
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001232 audio_format_t codecFormat =
1233 static_cast <audio_format_t>(data.readInt32());
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001234 if (device_address == nullptr || device_name == nullptr) {
1235 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
1236 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1237 } else {
1238 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
1239 device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001240 device_name,
1241 codecFormat)));
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001242 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001243 return NO_ERROR;
1244 } break;
1245
Eric Laurentc2f1f072009-07-17 12:17:14 -07001246 case SET_PHONE_STATE: {
1247 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001248 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
1249 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001250 return NO_ERROR;
1251 } break;
1252
Eric Laurentc2f1f072009-07-17 12:17:14 -07001253 case SET_FORCE_USE: {
1254 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001255 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1256 data.readInt32());
Dima Zavinfce7a472011-04-19 22:30:36 -07001257 audio_policy_forced_cfg_t config =
1258 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001259 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
1260 return NO_ERROR;
1261 } break;
1262
1263 case GET_FORCE_USE: {
1264 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001265 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1266 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001267 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
1268 return NO_ERROR;
1269 } break;
1270
1271 case GET_OUTPUT: {
1272 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001273 audio_stream_type_t stream =
1274 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +00001275 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001276 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001277 return NO_ERROR;
1278 } break;
1279
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001280 case GET_OUTPUT_FOR_ATTR: {
1281 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001282 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -08001283 bool hasAttributes = data.readInt32() != 0;
1284 if (hasAttributes) {
1285 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001286 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -08001287 }
1288 audio_session_t session = (audio_session_t)data.readInt32();
1289 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
1290 bool hasStream = data.readInt32() != 0;
1291 if (hasStream) {
1292 stream = (audio_stream_type_t)data.readInt32();
1293 }
Nadav Bar766fb022018-01-07 12:18:03 +02001294 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001295 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001296 audio_config_t config;
1297 memset(&config, 0, sizeof(audio_config_t));
1298 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001299 audio_output_flags_t flags =
1300 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001301 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001302 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001303 audio_io_handle_t output = 0;
Eric Laurente83b55d2014-11-14 10:06:21 -08001304 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001305 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001306 &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001307 flags, &selectedDeviceId, &portId);
Eric Laurente83b55d2014-11-14 10:06:21 -08001308 reply->writeInt32(status);
1309 reply->writeInt32(output);
1310 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001311 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001312 reply->writeInt32(portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001313 return NO_ERROR;
1314 } break;
1315
Eric Laurentc2f1f072009-07-17 12:17:14 -07001316 case START_OUTPUT: {
1317 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001318 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1319 reply->writeInt32(static_cast <uint32_t>(startOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001320 return NO_ERROR;
1321 } break;
1322
1323 case STOP_OUTPUT: {
1324 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001325 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1326 reply->writeInt32(static_cast <uint32_t>(stopOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001327 return NO_ERROR;
1328 } break;
1329
1330 case RELEASE_OUTPUT: {
1331 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001332 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1333 releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001334 return NO_ERROR;
1335 } break;
1336
Eric Laurentcaf7f482014-11-25 17:50:47 -08001337 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001338 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001339 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001340 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001341 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001342 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001343 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001344 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001345 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001346 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001347 audio_config_base_t config;
1348 memset(&config, 0, sizeof(audio_config_base_t));
1349 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001350 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001351 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001352 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001353 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001354 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001355 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001356 reply->writeInt32(status);
1357 if (status == NO_ERROR) {
1358 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001359 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001360 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001361 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001362 return NO_ERROR;
1363 } break;
1364
1365 case START_INPUT: {
1366 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001367 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
Eric Laurent4eb58f12018-12-07 16:41:02 -08001368 status_t status = startInput(portId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001369 reply->writeInt32(static_cast <uint32_t>(status));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001370 return NO_ERROR;
1371 } break;
1372
1373 case STOP_INPUT: {
1374 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001375 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1376 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001377 return NO_ERROR;
1378 } break;
1379
1380 case RELEASE_INPUT: {
1381 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001382 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1383 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001384 return NO_ERROR;
1385 } break;
1386
1387 case INIT_STREAM_VOLUME: {
1388 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001389 audio_stream_type_t stream =
1390 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001391 int indexMin = data.readInt32();
1392 int indexMax = data.readInt32();
1393 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1394 return NO_ERROR;
1395 } break;
1396
1397 case SET_STREAM_VOLUME: {
1398 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001399 audio_stream_type_t stream =
1400 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001401 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001402 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1403 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1404 index,
1405 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001406 return NO_ERROR;
1407 } break;
1408
1409 case GET_STREAM_VOLUME: {
1410 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001411 audio_stream_type_t stream =
1412 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurent83844cc2011-11-18 16:43:31 -08001413 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001414 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001415 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001416 reply->writeInt32(index);
1417 reply->writeInt32(static_cast <uint32_t>(status));
1418 return NO_ERROR;
1419 } break;
1420
Eric Laurentde070132010-07-13 04:45:46 -07001421 case GET_STRATEGY_FOR_STREAM: {
1422 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001423 audio_stream_type_t stream =
1424 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentde070132010-07-13 04:45:46 -07001425 reply->writeInt32(getStrategyForStream(stream));
1426 return NO_ERROR;
1427 } break;
1428
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001429 case GET_DEVICES_FOR_STREAM: {
1430 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001431 audio_stream_type_t stream =
1432 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001433 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1434 return NO_ERROR;
1435 } break;
1436
Eric Laurentde070132010-07-13 04:45:46 -07001437 case GET_OUTPUT_FOR_EFFECT: {
1438 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001439 effect_descriptor_t desc = {};
1440 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1441 android_errorWriteLog(0x534e4554, "73126106");
1442 }
1443 (void)sanitizeEffectDescriptor(&desc);
Eric Laurentde070132010-07-13 04:45:46 -07001444 audio_io_handle_t output = getOutputForEffect(&desc);
1445 reply->writeInt32(static_cast <int>(output));
1446 return NO_ERROR;
1447 } break;
1448
1449 case REGISTER_EFFECT: {
1450 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001451 effect_descriptor_t desc = {};
1452 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1453 android_errorWriteLog(0x534e4554, "73126106");
1454 }
1455 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001456 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001457 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001458 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001459 int id = data.readInt32();
1460 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001461 io,
Eric Laurentde070132010-07-13 04:45:46 -07001462 strategy,
1463 session,
1464 id)));
1465 return NO_ERROR;
1466 } break;
1467
1468 case UNREGISTER_EFFECT: {
1469 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1470 int id = data.readInt32();
1471 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1472 return NO_ERROR;
1473 } break;
1474
Eric Laurentdb7c0792011-08-10 10:37:50 -07001475 case SET_EFFECT_ENABLED: {
1476 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1477 int id = data.readInt32();
1478 bool enabled = static_cast <bool>(data.readInt32());
1479 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1480 return NO_ERROR;
1481 } break;
1482
Eric Laurenteda6c362011-02-02 09:33:30 -08001483 case IS_STREAM_ACTIVE: {
1484 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001485 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001486 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001487 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001488 return NO_ERROR;
1489 } break;
1490
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001491 case IS_STREAM_ACTIVE_REMOTELY: {
1492 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1493 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1494 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001495 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001496 return NO_ERROR;
1497 } break;
1498
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001499 case IS_SOURCE_ACTIVE: {
1500 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1501 audio_source_t source = (audio_source_t) data.readInt32();
1502 reply->writeInt32( isSourceActive(source));
1503 return NO_ERROR;
1504 }
1505
Eric Laurent57dae992011-07-24 13:36:09 -07001506 case QUERY_DEFAULT_PRE_PROCESSING: {
1507 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001508 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001509 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001510 if (count > AudioEffect::kMaxPreProcessing) {
1511 count = AudioEffect::kMaxPreProcessing;
1512 }
Eric Laurent57dae992011-07-24 13:36:09 -07001513 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001514 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001515 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1516 reply->writeInt32(status);
1517 if (status != NO_ERROR && status != NO_MEMORY) {
1518 retCount = 0;
1519 }
1520 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001521 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001522 if (retCount < count) {
1523 count = retCount;
1524 }
1525 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1526 }
1527 delete[] descriptors;
1528 return status;
1529 }
1530
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001531 case IS_OFFLOAD_SUPPORTED: {
1532 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001533 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001534 data.read(&info, sizeof(audio_offload_info_t));
1535 bool isSupported = isOffloadSupported(info);
1536 reply->writeInt32(isSupported);
1537 return NO_ERROR;
1538 }
1539
Michael Chana94fbb22018-04-24 14:31:19 +10001540 case IS_DIRECT_OUTPUT_SUPPORTED: {
1541 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1542 audio_config_base_t config = {};
1543 audio_attributes_t attributes = {};
1544 status_t status = data.read(&config, sizeof(audio_config_base_t));
1545 if (status != NO_ERROR) return status;
1546 status = data.read(&attributes, sizeof(audio_attributes_t));
1547 if (status != NO_ERROR) return status;
1548 reply->writeInt32(isDirectOutputSupported(config, attributes));
1549 return NO_ERROR;
1550 }
1551
Eric Laurent203b1a12014-04-01 10:34:16 -07001552 case LIST_AUDIO_PORTS: {
1553 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1554 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1555 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1556 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001557 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1558 numPortsReq = MAX_ITEMS_PER_LIST;
1559 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001560 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001561 struct audio_port *ports =
1562 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001563 if (ports == NULL) {
1564 reply->writeInt32(NO_MEMORY);
1565 reply->writeInt32(0);
1566 return NO_ERROR;
1567 }
1568 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001569 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1570 reply->writeInt32(status);
1571 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001572
1573 if (status == NO_ERROR) {
1574 if (numPortsReq > numPorts) {
1575 numPortsReq = numPorts;
1576 }
1577 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1578 reply->writeInt32(generation);
1579 }
1580 free(ports);
1581 return NO_ERROR;
1582 }
1583
1584 case GET_AUDIO_PORT: {
1585 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001586 struct audio_port port = {};
1587 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1588 ALOGE("b/23912202");
1589 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001590 status_t status = getAudioPort(&port);
1591 reply->writeInt32(status);
1592 if (status == NO_ERROR) {
1593 reply->write(&port, sizeof(struct audio_port));
1594 }
1595 return NO_ERROR;
1596 }
1597
1598 case CREATE_AUDIO_PATCH: {
1599 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001600 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001601 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001602 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001603 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1604 ALOGE("b/23912202");
1605 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001606 status_t status = createAudioPatch(&patch, &handle);
1607 reply->writeInt32(status);
1608 if (status == NO_ERROR) {
1609 reply->write(&handle, sizeof(audio_patch_handle_t));
1610 }
1611 return NO_ERROR;
1612 }
1613
1614 case RELEASE_AUDIO_PATCH: {
1615 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001616 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001617 data.read(&handle, sizeof(audio_patch_handle_t));
1618 status_t status = releaseAudioPatch(handle);
1619 reply->writeInt32(status);
1620 return NO_ERROR;
1621 }
1622
1623 case LIST_AUDIO_PATCHES: {
1624 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1625 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001626 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1627 numPatchesReq = MAX_ITEMS_PER_LIST;
1628 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001629 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001630 struct audio_patch *patches =
1631 (struct audio_patch *)calloc(numPatchesReq,
1632 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001633 if (patches == NULL) {
1634 reply->writeInt32(NO_MEMORY);
1635 reply->writeInt32(0);
1636 return NO_ERROR;
1637 }
1638 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001639 status_t status = listAudioPatches(&numPatches, patches, &generation);
1640 reply->writeInt32(status);
1641 reply->writeInt32(numPatches);
1642 if (status == NO_ERROR) {
1643 if (numPatchesReq > numPatches) {
1644 numPatchesReq = numPatches;
1645 }
1646 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1647 reply->writeInt32(generation);
1648 }
1649 free(patches);
1650 return NO_ERROR;
1651 }
1652
1653 case SET_AUDIO_PORT_CONFIG: {
1654 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001655 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001656 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001657 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001658 status_t status = setAudioPortConfig(&config);
1659 reply->writeInt32(status);
1660 return NO_ERROR;
1661 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001662
Eric Laurentb52c1522014-05-20 11:27:36 -07001663 case REGISTER_CLIENT: {
1664 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1665 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1666 data.readStrongBinder());
1667 registerClient(client);
1668 return NO_ERROR;
1669 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001670
Eric Laurente8726fe2015-06-26 09:39:24 -07001671 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1672 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1673 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1674 return NO_ERROR;
1675 } break;
1676
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001677 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1678 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1679 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1680 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001681 audio_session_t session = AUDIO_SESSION_NONE;
1682 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1683 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001684 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1685 reply->writeInt32(status);
1686 if (status == NO_ERROR) {
1687 reply->writeInt32(session);
1688 reply->writeInt32(ioHandle);
1689 reply->writeInt32(device);
1690 }
1691 return NO_ERROR;
1692 } break;
1693
1694 case RELEASE_SOUNDTRIGGER_SESSION: {
1695 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1696 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1697 data.readStrongBinder());
1698 audio_session_t session = (audio_session_t)data.readInt32();
1699 status_t status = releaseSoundTriggerSession(session);
1700 reply->writeInt32(status);
1701 return NO_ERROR;
1702 } break;
1703
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001704 case GET_PHONE_STATE: {
1705 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1706 reply->writeInt32((int32_t)getPhoneState());
1707 return NO_ERROR;
1708 } break;
1709
Eric Laurentbaac1832014-12-01 17:52:59 -08001710 case REGISTER_POLICY_MIXES: {
1711 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1712 bool registration = data.readInt32() == 1;
1713 Vector<AudioMix> mixes;
1714 size_t size = (size_t)data.readInt32();
1715 if (size > MAX_MIXES_PER_POLICY) {
1716 size = MAX_MIXES_PER_POLICY;
1717 }
1718 for (size_t i = 0; i < size; i++) {
1719 AudioMix mix;
1720 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1721 mixes.add(mix);
1722 }
1723 }
1724 status_t status = registerPolicyMixes(mixes, registration);
1725 reply->writeInt32(status);
1726 return NO_ERROR;
1727 } break;
1728
Eric Laurent554a2772015-04-10 11:29:24 -07001729 case START_AUDIO_SOURCE: {
1730 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001731 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001732 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001733 (void)sanitizeAudioPortConfig(&source);
1734 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001735 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001736 sanetizeAudioAttributes(&attributes);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001737 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
1738 status_t status = startAudioSource(&source, &attributes, &portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001739 reply->writeInt32(status);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001740 reply->writeInt32(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001741 return NO_ERROR;
1742 } break;
1743
1744 case STOP_AUDIO_SOURCE: {
1745 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001746 audio_port_handle_t portId = (audio_port_handle_t) data.readInt32();
1747 status_t status = stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001748 reply->writeInt32(status);
1749 return NO_ERROR;
1750 } break;
1751
Andy Hung2ddee192015-12-18 17:34:44 -08001752 case SET_MASTER_MONO: {
1753 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1754 bool mono = static_cast<bool>(data.readInt32());
1755 status_t status = setMasterMono(mono);
1756 reply->writeInt32(status);
1757 return NO_ERROR;
1758 } break;
1759
1760 case GET_MASTER_MONO: {
1761 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1762 bool mono;
1763 status_t status = getMasterMono(&mono);
1764 reply->writeInt32(status);
1765 if (status == NO_ERROR) {
1766 reply->writeInt32(static_cast<int32_t>(mono));
1767 }
1768 return NO_ERROR;
1769 } break;
1770
Eric Laurentac9cef52017-06-09 15:46:26 -07001771 case GET_STREAM_VOLUME_DB: {
1772 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1773 audio_stream_type_t stream =
1774 static_cast <audio_stream_type_t>(data.readInt32());
1775 int index = static_cast <int>(data.readInt32());
1776 audio_devices_t device =
1777 static_cast <audio_devices_t>(data.readUint32());
1778 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1779 return NO_ERROR;
1780 }
1781
jiabin81772902018-04-02 17:52:27 -07001782 case GET_SURROUND_FORMATS: {
1783 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1784 unsigned int numSurroundFormatsReq = data.readUint32();
1785 if (numSurroundFormatsReq > MAX_ITEMS_PER_LIST) {
1786 numSurroundFormatsReq = MAX_ITEMS_PER_LIST;
1787 }
1788 bool reported = data.readBool();
1789 unsigned int numSurroundFormats = numSurroundFormatsReq;
1790 audio_format_t *surroundFormats = (audio_format_t *)calloc(
1791 numSurroundFormats, sizeof(audio_format_t));
1792 bool *surroundFormatsEnabled = (bool *)calloc(numSurroundFormats, sizeof(bool));
1793 if (numSurroundFormatsReq > 0 &&
1794 (surroundFormats == NULL || surroundFormatsEnabled == NULL)) {
1795 free(surroundFormats);
1796 free(surroundFormatsEnabled);
1797 reply->writeInt32(NO_MEMORY);
1798 return NO_ERROR;
1799 }
1800 status_t status = getSurroundFormats(
1801 &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1802 reply->writeInt32(status);
1803
1804 if (status == NO_ERROR) {
1805 reply->writeUint32(numSurroundFormats);
1806 if (numSurroundFormatsReq > numSurroundFormats) {
1807 numSurroundFormatsReq = numSurroundFormats;
1808 }
1809 reply->write(surroundFormats, numSurroundFormatsReq * sizeof(audio_format_t));
1810 reply->write(surroundFormatsEnabled, numSurroundFormatsReq * sizeof(bool));
1811 }
1812 free(surroundFormats);
1813 free(surroundFormatsEnabled);
1814 return NO_ERROR;
1815 }
1816
1817 case SET_SURROUND_FORMAT_ENABLED: {
1818 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1819 audio_format_t audioFormat = (audio_format_t) data.readInt32();
1820 bool enabled = data.readBool();
1821 status_t status = setSurroundFormatEnabled(audioFormat, enabled);
1822 reply->writeInt32(status);
1823 return NO_ERROR;
1824 }
1825
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001826 case GET_OFFLOAD_FORMATS_A2DP: {
1827 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1828 std::vector<audio_format_t> encodingFormats;
1829 status_t status = getHwOffloadEncodingFormatsSupportedForA2DP(&encodingFormats);
1830 reply->writeInt32(status);
1831 if (status != NO_ERROR) {
1832 return NO_ERROR;
1833 }
1834 reply->writeUint32(static_cast<uint32_t>(encodingFormats.size()));
1835 for (size_t i = 0; i < encodingFormats.size(); i++)
1836 reply->writeInt32(static_cast<int32_t>(encodingFormats[i]));
1837 return NO_ERROR;
1838 }
1839
1840
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -07001841 case ADD_STREAM_DEFAULT_EFFECT: {
1842 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1843 effect_uuid_t type;
1844 status_t status = data.read(&type, sizeof(effect_uuid_t));
1845 if (status != NO_ERROR) {
1846 return status;
1847 }
1848 String16 opPackageName;
1849 status = data.readString16(&opPackageName);
1850 if (status != NO_ERROR) {
1851 return status;
1852 }
1853 effect_uuid_t uuid;
1854 status = data.read(&uuid, sizeof(effect_uuid_t));
1855 if (status != NO_ERROR) {
1856 return status;
1857 }
1858 int32_t priority = data.readInt32();
1859 audio_usage_t usage = (audio_usage_t) data.readInt32();
1860 audio_unique_id_t id = 0;
1861 reply->writeInt32(static_cast <int32_t>(addStreamDefaultEffect(&type,
1862 opPackageName,
1863 &uuid,
1864 priority,
1865 usage,
1866 &id)));
1867 reply->writeInt32(id);
1868 return NO_ERROR;
1869 }
1870
1871 case REMOVE_STREAM_DEFAULT_EFFECT: {
1872 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1873 audio_unique_id_t id = static_cast<audio_unique_id_t>(data.readInt32());
1874 reply->writeInt32(static_cast <int32_t>(removeStreamDefaultEffect(id)));
1875 return NO_ERROR;
1876 }
1877
Ari Hausman-Cohen24628312018-08-13 15:01:09 -07001878 case ADD_SOURCE_DEFAULT_EFFECT: {
1879 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1880 effect_uuid_t type;
1881 status_t status = data.read(&type, sizeof(effect_uuid_t));
1882 if (status != NO_ERROR) {
1883 return status;
1884 }
1885 String16 opPackageName;
1886 status = data.readString16(&opPackageName);
1887 if (status != NO_ERROR) {
1888 return status;
1889 }
1890 effect_uuid_t uuid;
1891 status = data.read(&uuid, sizeof(effect_uuid_t));
1892 if (status != NO_ERROR) {
1893 return status;
1894 }
1895 int32_t priority = data.readInt32();
1896 audio_source_t source = (audio_source_t) data.readInt32();
1897 audio_unique_id_t id = 0;
1898 reply->writeInt32(static_cast <int32_t>(addSourceDefaultEffect(&type,
1899 opPackageName,
1900 &uuid,
1901 priority,
1902 source,
1903 &id)));
1904 reply->writeInt32(id);
1905 return NO_ERROR;
1906 }
1907
1908 case REMOVE_SOURCE_DEFAULT_EFFECT: {
1909 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1910 audio_unique_id_t id = static_cast<audio_unique_id_t>(data.readInt32());
1911 reply->writeInt32(static_cast <int32_t>(removeSourceDefaultEffect(id)));
1912 return NO_ERROR;
1913 }
1914
Eric Laurentb78763e2018-10-17 10:08:02 -07001915 case SET_ASSISTANT_UID: {
1916 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1917 int32_t uid;
1918 status_t status = data.readInt32(&uid);
1919 if (status != NO_ERROR) {
1920 return status;
1921 }
1922 status = setAssistantUid(uid);
1923 reply->writeInt32(static_cast <int32_t>(status));
1924 return NO_ERROR;
1925 }
1926
1927 case SET_A11Y_SERVICES_UIDS: {
1928 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1929 std::vector<uid_t> uids;
1930 int32_t size;
1931 status_t status = data.readInt32(&size);
1932 if (status != NO_ERROR) {
1933 return status;
1934 }
1935 if (size > MAX_ITEMS_PER_LIST) {
1936 size = MAX_ITEMS_PER_LIST;
1937 }
1938 for (int32_t i = 0; i < size; i++) {
1939 int32_t uid;
1940 status = data.readInt32(&uid);
1941 if (status != NO_ERROR) {
1942 return status;
1943 }
1944 uids.push_back(uid);
1945 }
1946 status = setA11yServicesUids(uids);
1947 reply->writeInt32(static_cast <int32_t>(status));
1948 return NO_ERROR;
1949 }
1950
jiabin6012f912018-11-02 17:06:30 -07001951 case IS_HAPTIC_PLAYBACK_SUPPORTED: {
1952 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1953 bool isSupported = isHapticPlaybackSupported();
1954 reply->writeBool(isSupported);
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001955 return NO_ERROR;
1956 }
1957
1958 case SET_UID_DEVICE_AFFINITY: {
1959 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1960 const uid_t uid = (uid_t) data.readInt32();
1961 Vector<AudioDeviceTypeAddr> devices;
1962 size_t size = (size_t)data.readInt32();
1963 for (size_t i = 0; i < size; i++) {
1964 AudioDeviceTypeAddr device;
1965 if (device.readFromParcel((Parcel*)&data) == NO_ERROR) {
1966 devices.add(device);
1967 }
1968 }
1969 status_t status = setUidDeviceAffinities(uid, devices);
1970 reply->writeInt32(status);
1971 return NO_ERROR;
1972 }
1973
1974 case REMOVE_UID_DEVICE_AFFINITY: {
1975 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1976 const uid_t uid = (uid_t) data.readInt32();
1977 status_t status = removeUidDeviceAffinities(uid);
1978 reply->writeInt32(status);
jiabin6012f912018-11-02 17:06:30 -07001979 return NO_ERROR;
1980 }
1981
François Gaffied0ba9ed2018-11-05 11:50:42 +01001982 case LIST_AUDIO_PRODUCT_STRATEGIES: {
1983 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1984 AudioProductStrategyVector strategies;
1985 status_t status = listAudioProductStrategies(strategies);
1986 reply->writeInt32(status);
1987 if (status != NO_ERROR) {
1988 return status;
1989 }
1990 size_t size = strategies.size();
1991 size_t sizePosition = reply->dataPosition();
1992 reply->writeInt32(size);
1993 size_t finalSize = size;
1994 for (size_t i = 0; i < size; i++) {
1995 size_t position = reply->dataPosition();
1996 if (strategies[i].writeToParcel(reply) != NO_ERROR) {
1997 reply->setDataPosition(position);
1998 finalSize--;
1999 }
2000 }
2001 if (size != finalSize) {
2002 size_t position = reply->dataPosition();
2003 reply->setDataPosition(sizePosition);
2004 reply->writeInt32(finalSize);
2005 reply->setDataPosition(position);
2006 }
2007 return NO_ERROR;
2008 }
2009
2010 case GET_STRATEGY_FOR_ATTRIBUTES: {
2011 CHECK_INTERFACE(IAudioPolicyService, data, reply);
2012 AudioAttributes attributes;
2013 status_t status = attributes.readFromParcel(&data);
2014 if (status != NO_ERROR) {
2015 return status;
2016 }
2017 product_strategy_t strategy = getProductStrategyFromAudioAttributes(attributes);
2018 reply->writeUint32(static_cast<int>(strategy));
2019 return NO_ERROR;
2020 }
2021
Eric Laurentc2f1f072009-07-17 12:17:14 -07002022 default:
2023 return BBinder::onTransact(code, data, reply, flags);
2024 }
2025}
2026
Andy Hungb0272092018-04-12 11:06:56 -07002027/** returns true if string overflow was prevented by zero termination */
2028template <size_t size>
2029static bool preventStringOverflow(char (&s)[size]) {
2030 if (strnlen(s, size) < size) return false;
2031 s[size - 1] = '\0';
2032 return true;
2033}
2034
Kevin Rocard39fdbd02017-11-13 11:15:27 -08002035void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
2036{
2037 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
2038 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
2039 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
2040 }
2041 attr->tags[tagsMaxSize - 1] = '\0';
2042}
2043
Andy Hungb0272092018-04-12 11:06:56 -07002044/** returns BAD_VALUE if sanitization was required. */
2045status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
2046{
2047 if (preventStringOverflow(desc->name)
2048 | /* always */ preventStringOverflow(desc->implementor)) {
2049 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
2050 return BAD_VALUE;
2051 }
2052 return NO_ERROR;
2053}
2054
2055/** returns BAD_VALUE if sanitization was required. */
2056status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
2057{
2058 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
2059 preventStringOverflow(config->ext.device.address)) {
2060 return BAD_VALUE;
2061 }
2062 return NO_ERROR;
2063}
2064
Eric Laurentc2f1f072009-07-17 12:17:14 -07002065// ----------------------------------------------------------------------------
2066
Glenn Kasten40bc9062015-03-20 09:09:33 -07002067} // namespace android