blob: 32a71f3b6a8aa4fa13d9dc6f7c427e9a2d8f8c92 [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,
65 LIST_AUDIO_PORTS,
66 GET_AUDIO_PORT,
67 CREATE_AUDIO_PATCH,
68 RELEASE_AUDIO_PATCH,
69 LIST_AUDIO_PATCHES,
Eric Laurentb52c1522014-05-20 11:27:36 -070070 SET_AUDIO_PORT_CONFIG,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -070071 REGISTER_CLIENT,
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070072 GET_OUTPUT_FOR_ATTR,
73 ACQUIRE_SOUNDTRIGGER_SESSION,
Eric Laurentbb6c9a02014-09-25 14:11:47 -070074 RELEASE_SOUNDTRIGGER_SESSION,
Eric Laurentbaac1832014-12-01 17:52:59 -080075 GET_PHONE_STATE,
76 REGISTER_POLICY_MIXES,
Eric Laurent554a2772015-04-10 11:29:24 -070077 START_AUDIO_SOURCE,
Eric Laurente8726fe2015-06-26 09:39:24 -070078 STOP_AUDIO_SOURCE,
79 SET_AUDIO_PORT_CALLBACK_ENABLED,
Andy Hung2ddee192015-12-18 17:34:44 -080080 SET_MASTER_MONO,
81 GET_MASTER_MONO,
jiabin81772902018-04-02 17:52:27 -070082 GET_STREAM_VOLUME_DB,
83 GET_SURROUND_FORMATS,
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -070084 SET_SURROUND_FORMAT_ENABLED,
85 ADD_STREAM_DEFAULT_EFFECT,
Ari Hausman-Cohen24628312018-08-13 15:01:09 -070086 REMOVE_STREAM_DEFAULT_EFFECT,
87 ADD_SOURCE_DEFAULT_EFFECT,
88 REMOVE_SOURCE_DEFAULT_EFFECT
Eric Laurentc2f1f072009-07-17 12:17:14 -070089};
90
Eric Laurent1d670b12015-02-06 10:44:24 -080091#define MAX_ITEMS_PER_LIST 1024
92
Eric Laurentc2f1f072009-07-17 12:17:14 -070093class BpAudioPolicyService : public BpInterface<IAudioPolicyService>
94{
95public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070096 explicit BpAudioPolicyService(const sp<IBinder>& impl)
Eric Laurentc2f1f072009-07-17 12:17:14 -070097 : BpInterface<IAudioPolicyService>(impl)
98 {
99 }
100
101 virtual status_t setDeviceConnectionState(
Dima Zavinfce7a472011-04-19 22:30:36 -0700102 audio_devices_t device,
103 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800104 const char *device_address,
105 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700106 {
107 Parcel data, reply;
108 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
109 data.writeInt32(static_cast <uint32_t>(device));
110 data.writeInt32(static_cast <uint32_t>(state));
111 data.writeCString(device_address);
Paul McLeane743a472015-01-28 11:07:31 -0800112 data.writeCString(device_name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700113 remote()->transact(SET_DEVICE_CONNECTION_STATE, data, &reply);
114 return static_cast <status_t> (reply.readInt32());
115 }
116
Dima Zavinfce7a472011-04-19 22:30:36 -0700117 virtual audio_policy_dev_state_t getDeviceConnectionState(
118 audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700119 const char *device_address)
120 {
121 Parcel data, reply;
122 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
123 data.writeInt32(static_cast <uint32_t>(device));
124 data.writeCString(device_address);
125 remote()->transact(GET_DEVICE_CONNECTION_STATE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700126 return static_cast <audio_policy_dev_state_t>(reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700127 }
128
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800129 virtual status_t handleDeviceConfigChange(audio_devices_t device,
130 const char *device_address,
131 const char *device_name)
132 {
133 Parcel data, reply;
134 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
135 data.writeInt32(static_cast <uint32_t>(device));
136 data.writeCString(device_address);
137 data.writeCString(device_name);
138 remote()->transact(HANDLE_DEVICE_CONFIG_CHANGE, data, &reply);
139 return static_cast <status_t> (reply.readInt32());
140 }
141
Glenn Kastenf78aee72012-01-04 11:00:47 -0800142 virtual status_t setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700143 {
144 Parcel data, reply;
145 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
146 data.writeInt32(state);
147 remote()->transact(SET_PHONE_STATE, data, &reply);
148 return static_cast <status_t> (reply.readInt32());
149 }
150
Dima Zavinfce7a472011-04-19 22:30:36 -0700151 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700152 {
153 Parcel data, reply;
154 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
155 data.writeInt32(static_cast <uint32_t>(usage));
156 data.writeInt32(static_cast <uint32_t>(config));
157 remote()->transact(SET_FORCE_USE, data, &reply);
158 return static_cast <status_t> (reply.readInt32());
159 }
160
Dima Zavinfce7a472011-04-19 22:30:36 -0700161 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700162 {
163 Parcel data, reply;
164 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
165 data.writeInt32(static_cast <uint32_t>(usage));
166 remote()->transact(GET_FORCE_USE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700167 return static_cast <audio_policy_forced_cfg_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700168 }
169
Eric Laurentf4e63452017-11-06 19:31:46 +0000170 virtual audio_io_handle_t getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700171 {
172 Parcel data, reply;
173 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
174 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700175 remote()->transact(GET_OUTPUT, data, &reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -0700176 return static_cast <audio_io_handle_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700177 }
178
Eric Laurente83b55d2014-11-14 10:06:21 -0800179 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
180 audio_io_handle_t *output,
181 audio_session_t session,
182 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200183 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700184 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800185 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800186 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700187 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800188 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700189 {
190 Parcel data, reply;
191 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
192 if (attr == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800193 if (stream == NULL) {
194 ALOGE("getOutputForAttr(): NULL audio attributes and stream type");
195 return BAD_VALUE;
196 }
197 if (*stream == AUDIO_STREAM_DEFAULT) {
198 ALOGE("getOutputForAttr unspecified stream type");
199 return BAD_VALUE;
200 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700201 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800202 if (output == NULL) {
203 ALOGE("getOutputForAttr NULL output - shouldn't happen");
204 return BAD_VALUE;
205 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700206 if (selectedDeviceId == NULL) {
207 ALOGE("getOutputForAttr NULL selectedDeviceId - shouldn't happen");
208 return BAD_VALUE;
209 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800210 if (portId == NULL) {
211 ALOGE("getOutputForAttr NULL portId - shouldn't happen");
212 return BAD_VALUE;
213 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800214 if (attr == NULL) {
215 data.writeInt32(0);
216 } else {
217 data.writeInt32(1);
218 data.write(attr, sizeof(audio_attributes_t));
219 }
220 data.writeInt32(session);
221 if (stream == NULL) {
222 data.writeInt32(0);
223 } else {
224 data.writeInt32(1);
225 data.writeInt32(*stream);
226 }
Nadav Bar766fb022018-01-07 12:18:03 +0200227 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700228 data.writeInt32(uid);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800229 data.write(config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700230 data.writeInt32(static_cast <uint32_t>(flags));
Eric Laurent9ae8c592017-06-22 17:17:09 -0700231 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800232 data.writeInt32(*portId);
Eric Laurente83b55d2014-11-14 10:06:21 -0800233 status_t status = remote()->transact(GET_OUTPUT_FOR_ATTR, data, &reply);
234 if (status != NO_ERROR) {
235 return status;
236 }
237 status = (status_t)reply.readInt32();
238 if (status != NO_ERROR) {
239 return status;
240 }
241 *output = (audio_io_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800242 audio_stream_type_t lStream = (audio_stream_type_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800243 if (stream != NULL) {
Eric Laurent20b9ef02016-12-05 11:03:16 -0800244 *stream = lStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800245 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700246 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800247 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800248 return status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700249 }
250
Eric Laurentd7fe0862018-07-14 16:48:01 -0700251 virtual status_t startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700252 {
253 Parcel data, reply;
254 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700255 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700256 remote()->transact(START_OUTPUT, data, &reply);
257 return static_cast <status_t> (reply.readInt32());
258 }
259
Eric Laurentd7fe0862018-07-14 16:48:01 -0700260 virtual status_t stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700261 {
262 Parcel data, reply;
263 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700264 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700265 remote()->transact(STOP_OUTPUT, data, &reply);
266 return static_cast <status_t> (reply.readInt32());
267 }
268
Eric Laurentd7fe0862018-07-14 16:48:01 -0700269 virtual void releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700270 {
271 Parcel data, reply;
272 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700273 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700274 remote()->transact(RELEASE_OUTPUT, data, &reply);
275 }
276
Eric Laurentcaf7f482014-11-25 17:50:47 -0800277 virtual status_t getInputForAttr(const audio_attributes_t *attr,
278 audio_io_handle_t *input,
279 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700280 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700281 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800282 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800283 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600284 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700285 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800286 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700287 {
288 Parcel data, reply;
289 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentcaf7f482014-11-25 17:50:47 -0800290 if (attr == NULL) {
291 ALOGE("getInputForAttr NULL attr - shouldn't happen");
292 return BAD_VALUE;
293 }
294 if (input == NULL) {
295 ALOGE("getInputForAttr NULL input - shouldn't happen");
296 return BAD_VALUE;
297 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700298 if (selectedDeviceId == NULL) {
299 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
300 return BAD_VALUE;
301 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800302 if (portId == NULL) {
303 ALOGE("getInputForAttr NULL portId - shouldn't happen");
304 return BAD_VALUE;
305 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800306 data.write(attr, sizeof(audio_attributes_t));
Eric Laurenta54f1282017-07-01 19:39:32 -0700307 data.writeInt32(*input);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800308 data.writeInt32(session);
Eric Laurentb2379ba2016-05-23 17:42:12 -0700309 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700310 data.writeInt32(uid);
Eric Laurentfee19762018-01-29 18:44:13 -0800311 data.writeString16(opPackageName);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800312 data.write(config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -0700313 data.writeInt32(flags);
Eric Laurent9ae8c592017-06-22 17:17:09 -0700314 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800315 data.writeInt32(*portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800316 status_t status = remote()->transact(GET_INPUT_FOR_ATTR, data, &reply);
317 if (status != NO_ERROR) {
318 return status;
319 }
320 status = reply.readInt32();
321 if (status != NO_ERROR) {
322 return status;
323 }
324 *input = (audio_io_handle_t)reply.readInt32();
Eric Laurent9ae8c592017-06-22 17:17:09 -0700325 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800326 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800327 return NO_ERROR;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700328 }
329
Eric Laurentfee19762018-01-29 18:44:13 -0800330 virtual status_t startInput(audio_port_handle_t portId,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800331 bool *silenced)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700332 {
333 Parcel data, reply;
334 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800335 data.writeInt32(portId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800336 data.writeInt32(*silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700337 remote()->transact(START_INPUT, data, &reply);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800338 status_t status = static_cast <status_t> (reply.readInt32());
339 *silenced = reply.readInt32() == 1;
340 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700341 }
342
Eric Laurentfee19762018-01-29 18:44:13 -0800343 virtual status_t stopInput(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(STOP_INPUT, data, &reply);
349 return static_cast <status_t> (reply.readInt32());
350 }
351
Eric Laurentfee19762018-01-29 18:44:13 -0800352 virtual void releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700353 {
354 Parcel data, reply;
355 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800356 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700357 remote()->transact(RELEASE_INPUT, data, &reply);
358 }
359
Dima Zavinfce7a472011-04-19 22:30:36 -0700360 virtual status_t initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700361 int indexMin,
362 int indexMax)
363 {
364 Parcel data, reply;
365 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
366 data.writeInt32(static_cast <uint32_t>(stream));
367 data.writeInt32(indexMin);
368 data.writeInt32(indexMax);
369 remote()->transact(INIT_STREAM_VOLUME, data, &reply);
370 return static_cast <status_t> (reply.readInt32());
371 }
372
Eric Laurent83844cc2011-11-18 16:43:31 -0800373 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
374 int index,
375 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700376 {
377 Parcel data, reply;
378 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
379 data.writeInt32(static_cast <uint32_t>(stream));
380 data.writeInt32(index);
Eric Laurent83844cc2011-11-18 16:43:31 -0800381 data.writeInt32(static_cast <uint32_t>(device));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700382 remote()->transact(SET_STREAM_VOLUME, data, &reply);
383 return static_cast <status_t> (reply.readInt32());
384 }
385
Eric Laurent83844cc2011-11-18 16:43:31 -0800386 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
387 int *index,
388 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700389 {
390 Parcel data, reply;
391 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
392 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurent83844cc2011-11-18 16:43:31 -0800393 data.writeInt32(static_cast <uint32_t>(device));
394
Eric Laurentc2f1f072009-07-17 12:17:14 -0700395 remote()->transact(GET_STREAM_VOLUME, data, &reply);
396 int lIndex = reply.readInt32();
397 if (index) *index = lIndex;
398 return static_cast <status_t> (reply.readInt32());
399 }
Eric Laurentde070132010-07-13 04:45:46 -0700400
Dima Zavinfce7a472011-04-19 22:30:36 -0700401 virtual uint32_t getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700402 {
403 Parcel data, reply;
404 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
405 data.writeInt32(static_cast <uint32_t>(stream));
406 remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply);
407 return reply.readInt32();
408 }
409
Eric Laurent63742522012-03-08 13:42:42 -0800410 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800411 {
412 Parcel data, reply;
413 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
414 data.writeInt32(static_cast <uint32_t>(stream));
415 remote()->transact(GET_DEVICES_FOR_STREAM, data, &reply);
Eric Laurent63742522012-03-08 13:42:42 -0800416 return (audio_devices_t) reply.readInt32();
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800417 }
418
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700419 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700420 {
421 Parcel data, reply;
422 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
423 data.write(desc, sizeof(effect_descriptor_t));
424 remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply);
425 return static_cast <audio_io_handle_t> (reply.readInt32());
426 }
427
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700428 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700429 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700430 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800431 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700432 int id)
433 {
434 Parcel data, reply;
435 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
436 data.write(desc, sizeof(effect_descriptor_t));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700437 data.writeInt32(io);
Eric Laurentde070132010-07-13 04:45:46 -0700438 data.writeInt32(strategy);
439 data.writeInt32(session);
440 data.writeInt32(id);
441 remote()->transact(REGISTER_EFFECT, data, &reply);
442 return static_cast <status_t> (reply.readInt32());
443 }
444
445 virtual status_t unregisterEffect(int id)
446 {
447 Parcel data, reply;
448 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
449 data.writeInt32(id);
450 remote()->transact(UNREGISTER_EFFECT, data, &reply);
451 return static_cast <status_t> (reply.readInt32());
452 }
453
Eric Laurentdb7c0792011-08-10 10:37:50 -0700454 virtual status_t setEffectEnabled(int id, bool enabled)
455 {
456 Parcel data, reply;
457 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
458 data.writeInt32(id);
459 data.writeInt32(enabled);
460 remote()->transact(SET_EFFECT_ENABLED, data, &reply);
461 return static_cast <status_t> (reply.readInt32());
462 }
463
Glenn Kastenfff6d712012-01-12 16:38:12 -0800464 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800465 {
466 Parcel data, reply;
467 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Glenn Kastenfff6d712012-01-12 16:38:12 -0800468 data.writeInt32((int32_t) stream);
Eric Laurenteda6c362011-02-02 09:33:30 -0800469 data.writeInt32(inPastMs);
470 remote()->transact(IS_STREAM_ACTIVE, data, &reply);
471 return reply.readInt32();
472 }
Eric Laurent57dae992011-07-24 13:36:09 -0700473
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800474 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
475 {
476 Parcel data, reply;
477 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
478 data.writeInt32((int32_t) stream);
479 data.writeInt32(inPastMs);
480 remote()->transact(IS_STREAM_ACTIVE_REMOTELY, data, &reply);
481 return reply.readInt32();
482 }
483
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700484 virtual bool isSourceActive(audio_source_t source) const
485 {
486 Parcel data, reply;
487 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
488 data.writeInt32((int32_t) source);
489 remote()->transact(IS_SOURCE_ACTIVE, data, &reply);
490 return reply.readInt32();
491 }
492
Glenn Kastend848eb42016-03-08 13:42:11 -0800493 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700494 effect_descriptor_t *descriptors,
495 uint32_t *count)
496 {
497 if (descriptors == NULL || count == NULL) {
498 return BAD_VALUE;
499 }
500 Parcel data, reply;
501 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
502 data.writeInt32(audioSession);
503 data.writeInt32(*count);
504 status_t status = remote()->transact(QUERY_DEFAULT_PRE_PROCESSING, data, &reply);
505 if (status != NO_ERROR) {
506 return status;
507 }
508 status = static_cast <status_t> (reply.readInt32());
509 uint32_t retCount = reply.readInt32();
510 if (retCount != 0) {
511 uint32_t numDesc = (retCount < *count) ? retCount : *count;
512 reply.read(descriptors, sizeof(effect_descriptor_t) * numDesc);
513 }
514 *count = retCount;
515 return status;
516 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000517
518 virtual bool isOffloadSupported(const audio_offload_info_t& info)
519 {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100520 Parcel data, reply;
521 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
522 data.write(&info, sizeof(audio_offload_info_t));
523 remote()->transact(IS_OFFLOAD_SUPPORTED, data, &reply);
Eric Laurent203b1a12014-04-01 10:34:16 -0700524 return reply.readInt32();
525 }
526
527 virtual status_t listAudioPorts(audio_port_role_t role,
528 audio_port_type_t type,
529 unsigned int *num_ports,
530 struct audio_port *ports,
531 unsigned int *generation)
532 {
533 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
534 generation == NULL) {
535 return BAD_VALUE;
536 }
537 Parcel data, reply;
538 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
539 unsigned int numPortsReq = (ports == NULL) ? 0 : *num_ports;
540 data.writeInt32(role);
541 data.writeInt32(type);
542 data.writeInt32(numPortsReq);
543 status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply);
544 if (status == NO_ERROR) {
545 status = (status_t)reply.readInt32();
546 *num_ports = (unsigned int)reply.readInt32();
547 }
Eric Laurent203b1a12014-04-01 10:34:16 -0700548 if (status == NO_ERROR) {
549 if (numPortsReq > *num_ports) {
550 numPortsReq = *num_ports;
551 }
552 if (numPortsReq > 0) {
553 reply.read(ports, numPortsReq * sizeof(struct audio_port));
554 }
555 *generation = reply.readInt32();
556 }
557 return status;
558 }
559
560 virtual status_t getAudioPort(struct audio_port *port)
561 {
562 if (port == NULL) {
563 return BAD_VALUE;
564 }
565 Parcel data, reply;
566 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
567 data.write(port, sizeof(struct audio_port));
568 status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply);
569 if (status != NO_ERROR ||
570 (status = (status_t)reply.readInt32()) != NO_ERROR) {
571 return status;
572 }
573 reply.read(port, sizeof(struct audio_port));
574 return status;
575 }
576
577 virtual status_t createAudioPatch(const struct audio_patch *patch,
578 audio_patch_handle_t *handle)
579 {
580 if (patch == NULL || handle == NULL) {
581 return BAD_VALUE;
582 }
583 Parcel data, reply;
584 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
585 data.write(patch, sizeof(struct audio_patch));
586 data.write(handle, sizeof(audio_patch_handle_t));
587 status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply);
588 if (status != NO_ERROR ||
589 (status = (status_t)reply.readInt32()) != NO_ERROR) {
590 return status;
591 }
592 reply.read(handle, sizeof(audio_patch_handle_t));
593 return status;
594 }
595
596 virtual status_t releaseAudioPatch(audio_patch_handle_t handle)
597 {
598 Parcel data, reply;
599 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
600 data.write(&handle, sizeof(audio_patch_handle_t));
601 status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply);
602 if (status != NO_ERROR) {
603 status = (status_t)reply.readInt32();
604 }
605 return status;
606 }
607
608 virtual status_t listAudioPatches(unsigned int *num_patches,
609 struct audio_patch *patches,
610 unsigned int *generation)
611 {
612 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
613 generation == NULL) {
614 return BAD_VALUE;
615 }
616 Parcel data, reply;
617 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
618 unsigned int numPatchesReq = (patches == NULL) ? 0 : *num_patches;
619 data.writeInt32(numPatchesReq);
620 status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply);
621 if (status == NO_ERROR) {
622 status = (status_t)reply.readInt32();
623 *num_patches = (unsigned int)reply.readInt32();
624 }
625 if (status == NO_ERROR) {
626 if (numPatchesReq > *num_patches) {
627 numPatchesReq = *num_patches;
628 }
629 if (numPatchesReq > 0) {
630 reply.read(patches, numPatchesReq * sizeof(struct audio_patch));
631 }
632 *generation = reply.readInt32();
633 }
634 return status;
635 }
636
637 virtual status_t setAudioPortConfig(const struct audio_port_config *config)
638 {
639 if (config == NULL) {
640 return BAD_VALUE;
641 }
642 Parcel data, reply;
643 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
644 data.write(config, sizeof(struct audio_port_config));
645 status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply);
646 if (status != NO_ERROR) {
647 status = (status_t)reply.readInt32();
648 }
649 return status;
650 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700651
Eric Laurentb52c1522014-05-20 11:27:36 -0700652 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client)
653 {
654 Parcel data, reply;
655 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -0800656 data.writeStrongBinder(IInterface::asBinder(client));
Eric Laurentb52c1522014-05-20 11:27:36 -0700657 remote()->transact(REGISTER_CLIENT, data, &reply);
658 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700659
Eric Laurente8726fe2015-06-26 09:39:24 -0700660 virtual void setAudioPortCallbacksEnabled(bool enabled)
661 {
662 Parcel data, reply;
663 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
664 data.writeInt32(enabled ? 1 : 0);
665 remote()->transact(SET_AUDIO_PORT_CALLBACK_ENABLED, data, &reply);
666 }
667
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700668 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
669 audio_io_handle_t *ioHandle,
670 audio_devices_t *device)
671 {
672 if (session == NULL || ioHandle == NULL || device == NULL) {
673 return BAD_VALUE;
674 }
675 Parcel data, reply;
676 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
677 status_t status = remote()->transact(ACQUIRE_SOUNDTRIGGER_SESSION, data, &reply);
678 if (status != NO_ERROR) {
679 return status;
680 }
681 status = (status_t)reply.readInt32();
682 if (status == NO_ERROR) {
683 *session = (audio_session_t)reply.readInt32();
684 *ioHandle = (audio_io_handle_t)reply.readInt32();
685 *device = (audio_devices_t)reply.readInt32();
686 }
687 return status;
688 }
689
690 virtual status_t releaseSoundTriggerSession(audio_session_t session)
691 {
692 Parcel data, reply;
693 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
694 data.writeInt32(session);
695 status_t status = remote()->transact(RELEASE_SOUNDTRIGGER_SESSION, data, &reply);
696 if (status != NO_ERROR) {
697 return status;
698 }
699 return (status_t)reply.readInt32();
700 }
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700701
702 virtual audio_mode_t getPhoneState()
703 {
704 Parcel data, reply;
705 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
706 status_t status = remote()->transact(GET_PHONE_STATE, data, &reply);
707 if (status != NO_ERROR) {
708 return AUDIO_MODE_INVALID;
709 }
710 return (audio_mode_t)reply.readInt32();
711 }
Eric Laurentbaac1832014-12-01 17:52:59 -0800712
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700713 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800714 {
715 Parcel data, reply;
716 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
717 data.writeInt32(registration ? 1 : 0);
718 size_t size = mixes.size();
719 if (size > MAX_MIXES_PER_POLICY) {
720 size = MAX_MIXES_PER_POLICY;
721 }
722 size_t sizePosition = data.dataPosition();
723 data.writeInt32(size);
724 size_t finalSize = size;
725 for (size_t i = 0; i < size; i++) {
726 size_t position = data.dataPosition();
727 if (mixes[i].writeToParcel(&data) != NO_ERROR) {
728 data.setDataPosition(position);
729 finalSize--;
730 }
731 }
732 if (size != finalSize) {
733 size_t position = data.dataPosition();
734 data.setDataPosition(sizePosition);
735 data.writeInt32(finalSize);
736 data.setDataPosition(position);
737 }
738 status_t status = remote()->transact(REGISTER_POLICY_MIXES, data, &reply);
739 if (status == NO_ERROR) {
740 status = (status_t)reply.readInt32();
741 }
742 return status;
743 }
Eric Laurent554a2772015-04-10 11:29:24 -0700744
745 virtual status_t startAudioSource(const struct audio_port_config *source,
746 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700747 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -0700748 {
749 Parcel data, reply;
750 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700751 if (source == NULL || attributes == NULL || portId == NULL) {
Eric Laurent554a2772015-04-10 11:29:24 -0700752 return BAD_VALUE;
753 }
754 data.write(source, sizeof(struct audio_port_config));
755 data.write(attributes, sizeof(audio_attributes_t));
756 status_t status = remote()->transact(START_AUDIO_SOURCE, data, &reply);
757 if (status != NO_ERROR) {
758 return status;
759 }
760 status = (status_t)reply.readInt32();
761 if (status != NO_ERROR) {
762 return status;
763 }
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700764 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -0700765 return status;
766 }
767
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700768 virtual status_t stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -0700769 {
770 Parcel data, reply;
771 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700772 data.writeInt32(portId);
Eric Laurent554a2772015-04-10 11:29:24 -0700773 status_t status = remote()->transact(STOP_AUDIO_SOURCE, data, &reply);
774 if (status != NO_ERROR) {
775 return status;
776 }
777 status = (status_t)reply.readInt32();
778 return status;
779 }
Andy Hung2ddee192015-12-18 17:34:44 -0800780
781 virtual status_t setMasterMono(bool mono)
782 {
783 Parcel data, reply;
784 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
785 data.writeInt32(static_cast<int32_t>(mono));
786 status_t status = remote()->transact(SET_MASTER_MONO, data, &reply);
787 if (status != NO_ERROR) {
788 return status;
789 }
790 return static_cast<status_t>(reply.readInt32());
791 }
792
793 virtual status_t getMasterMono(bool *mono)
794 {
795 if (mono == nullptr) {
796 return BAD_VALUE;
797 }
798 Parcel data, reply;
799 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
800
801 status_t status = remote()->transact(GET_MASTER_MONO, data, &reply);
802 if (status != NO_ERROR) {
803 return status;
804 }
805 status = static_cast<status_t>(reply.readInt32());
806 if (status == NO_ERROR) {
807 *mono = static_cast<bool>(reply.readInt32());
808 }
809 return status;
810 }
Eric Laurentac9cef52017-06-09 15:46:26 -0700811
812 virtual float getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
813 {
814 Parcel data, reply;
815 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
816 data.writeInt32(static_cast <int32_t>(stream));
817 data.writeInt32(static_cast <int32_t>(index));
818 data.writeUint32(static_cast <uint32_t>(device));
819 status_t status = remote()->transact(GET_STREAM_VOLUME_DB, data, &reply);
820 if (status != NO_ERROR) {
821 return NAN;
822 }
823 return reply.readFloat();
824 }
jiabin81772902018-04-02 17:52:27 -0700825
826 virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
827 audio_format_t *surroundFormats,
828 bool *surroundFormatsEnabled,
829 bool reported)
830 {
831 if (numSurroundFormats == NULL || (*numSurroundFormats != 0 &&
832 (surroundFormats == NULL || surroundFormatsEnabled == NULL))) {
833 return BAD_VALUE;
834 }
835 Parcel data, reply;
836 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
837 unsigned int numSurroundFormatsReq = *numSurroundFormats;
838 data.writeUint32(numSurroundFormatsReq);
839 data.writeBool(reported);
840 status_t status = remote()->transact(GET_SURROUND_FORMATS, data, &reply);
841 if (status == NO_ERROR && (status = (status_t)reply.readInt32()) == NO_ERROR) {
842 *numSurroundFormats = reply.readUint32();
843 }
844 if (status == NO_ERROR) {
845 if (numSurroundFormatsReq > *numSurroundFormats) {
846 numSurroundFormatsReq = *numSurroundFormats;
847 }
848 if (numSurroundFormatsReq > 0) {
849 status = reply.read(surroundFormats,
850 numSurroundFormatsReq * sizeof(audio_format_t));
851 if (status != NO_ERROR) {
852 return status;
853 }
854 status = reply.read(surroundFormatsEnabled,
855 numSurroundFormatsReq * sizeof(bool));
856 }
857 }
858 return status;
859 }
860
861 virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
862 {
863 Parcel data, reply;
864 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
865 data.writeInt32(audioFormat);
866 data.writeBool(enabled);
867 status_t status = remote()->transact(SET_SURROUND_FORMAT_ENABLED, data, &reply);
868 if (status != NO_ERROR) {
869 return status;
870 }
871 return reply.readInt32();
872 }
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700873
874 virtual status_t addStreamDefaultEffect(const effect_uuid_t *type,
875 const String16& opPackageName,
876 const effect_uuid_t *uuid,
877 int32_t priority,
878 audio_usage_t usage,
879 audio_unique_id_t* id)
880 {
881 Parcel data, reply;
882 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
883 data.write(type, sizeof(effect_uuid_t));
884 data.writeString16(opPackageName);
885 data.write(uuid, sizeof(effect_uuid_t));
886 data.writeInt32(priority);
887 data.writeInt32((int32_t) usage);
888 status_t status = remote()->transact(ADD_STREAM_DEFAULT_EFFECT, data, &reply);
889 if (status != NO_ERROR) {
890 return status;
891 }
892 status = static_cast <status_t> (reply.readInt32());
893 *id = reply.readInt32();
894 return status;
895 }
896
897 virtual status_t removeStreamDefaultEffect(audio_unique_id_t id)
898 {
899 Parcel data, reply;
900 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
901 data.writeInt32(id);
902 status_t status = remote()->transact(REMOVE_STREAM_DEFAULT_EFFECT, data, &reply);
903 if (status != NO_ERROR) {
904 return status;
905 }
906 return static_cast <status_t> (reply.readInt32());
907 }
908
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700909 virtual status_t addSourceDefaultEffect(const effect_uuid_t *type,
910 const String16& opPackageName,
911 const effect_uuid_t *uuid,
912 int32_t priority,
913 audio_source_t source,
914 audio_unique_id_t* id)
915 {
916 Parcel data, reply;
917 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
918 data.write(type, sizeof(effect_uuid_t));
919 data.writeString16(opPackageName);
920 data.write(uuid, sizeof(effect_uuid_t));
921 data.writeInt32(priority);
922 data.writeInt32((int32_t) source);
923 status_t status = remote()->transact(ADD_SOURCE_DEFAULT_EFFECT, data, &reply);
924 if (status != NO_ERROR) {
925 return status;
926 }
927 status = static_cast <status_t> (reply.readInt32());
928 *id = reply.readInt32();
929 return status;
930 }
931
932 virtual status_t removeSourceDefaultEffect(audio_unique_id_t id)
933 {
934 Parcel data, reply;
935 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
936 data.writeInt32(id);
937 status_t status = remote()->transact(REMOVE_SOURCE_DEFAULT_EFFECT, data, &reply);
938 if (status != NO_ERROR) {
939 return status;
940 }
941 return static_cast <status_t> (reply.readInt32());
942 }
943
Eric Laurentc2f1f072009-07-17 12:17:14 -0700944};
945
946IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
947
948// ----------------------------------------------------------------------
949
Eric Laurentc2f1f072009-07-17 12:17:14 -0700950status_t BnAudioPolicyService::onTransact(
951 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
952{
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800953 // make sure transactions reserved to AudioFlinger do not come from other processes
954 switch (code) {
955 case START_OUTPUT:
956 case STOP_OUTPUT:
957 case RELEASE_OUTPUT:
958 case GET_INPUT_FOR_ATTR:
959 case START_INPUT:
960 case STOP_INPUT:
961 case RELEASE_INPUT:
962 case GET_STRATEGY_FOR_STREAM:
963 case GET_OUTPUT_FOR_EFFECT:
964 case REGISTER_EFFECT:
965 case UNREGISTER_EFFECT:
966 case SET_EFFECT_ENABLED:
967 case GET_OUTPUT_FOR_ATTR:
968 case ACQUIRE_SOUNDTRIGGER_SESSION:
969 case RELEASE_SOUNDTRIGGER_SESSION:
970 ALOGW("%s: transaction %d received from PID %d",
971 __func__, code, IPCThreadState::self()->getCallingPid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700972 // return status only for non void methods
973 switch (code) {
974 case RELEASE_OUTPUT:
975 case RELEASE_INPUT:
976 break;
977 default:
978 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
979 break;
980 }
981 return OK;
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800982 default:
983 break;
984 }
985
Eric Laurent4980df22018-01-26 18:04:09 -0800986 // make sure the following transactions come from system components
987 switch (code) {
988 case SET_DEVICE_CONNECTION_STATE:
989 case HANDLE_DEVICE_CONFIG_CHANGE:
990 case SET_PHONE_STATE:
Eric Laurente17378d2018-05-09 14:43:01 -0700991//FIXME: Allow SET_FORCE_USE calls from system apps until a better use case routing API is available
992// case SET_FORCE_USE:
Eric Laurent4980df22018-01-26 18:04:09 -0800993 case INIT_STREAM_VOLUME:
994 case SET_STREAM_VOLUME:
995 case REGISTER_POLICY_MIXES:
Eric Laurent10b71232018-04-13 18:14:44 -0700996 case SET_MASTER_MONO:
997 case START_AUDIO_SOURCE:
jiabin81772902018-04-02 17:52:27 -0700998 case STOP_AUDIO_SOURCE:
999 case GET_SURROUND_FORMATS:
1000 case SET_SURROUND_FORMAT_ENABLED: {
Andy Hung4ef19fa2018-05-15 19:35:29 -07001001 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
Eric Laurent4980df22018-01-26 18:04:09 -08001002 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1003 __func__, code, IPCThreadState::self()->getCallingPid(),
1004 IPCThreadState::self()->getCallingUid());
Eric Laurentef92bff2018-04-26 10:44:50 -07001005 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
1006 return OK;
Eric Laurent4980df22018-01-26 18:04:09 -08001007 }
Eric Laurent96c7eed2018-03-26 17:57:01 -07001008 } break;
Eric Laurent4980df22018-01-26 18:04:09 -08001009 default:
1010 break;
1011 }
1012
Eric Laurent39b09b52018-06-29 12:24:40 -07001013 std::string tag("IAudioPolicyService command " + std::to_string(code));
1014 TimeCheck check(tag.c_str());
Eric Laurent3528c932018-02-23 17:17:22 -08001015
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001016 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001017 case SET_DEVICE_CONNECTION_STATE: {
1018 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001019 audio_devices_t device =
1020 static_cast <audio_devices_t>(data.readInt32());
1021 audio_policy_dev_state_t state =
1022 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001023 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -08001024 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001025 if (device_address == nullptr || device_name == nullptr) {
1026 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
1027 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1028 } else {
1029 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
1030 state,
1031 device_address,
1032 device_name)));
1033 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001034 return NO_ERROR;
1035 } break;
1036
1037 case GET_DEVICE_CONNECTION_STATE: {
1038 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001039 audio_devices_t device =
1040 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001041 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001042 if (device_address == nullptr) {
1043 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
1044 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
1045 } else {
1046 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
1047 device_address)));
1048 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001049 return NO_ERROR;
1050 } break;
1051
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001052 case HANDLE_DEVICE_CONFIG_CHANGE: {
1053 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1054 audio_devices_t device =
1055 static_cast <audio_devices_t>(data.readInt32());
1056 const char *device_address = data.readCString();
1057 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001058 if (device_address == nullptr || device_name == nullptr) {
1059 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
1060 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1061 } else {
1062 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
1063 device_address,
1064 device_name)));
1065 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001066 return NO_ERROR;
1067 } break;
1068
Eric Laurentc2f1f072009-07-17 12:17:14 -07001069 case SET_PHONE_STATE: {
1070 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001071 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
1072 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001073 return NO_ERROR;
1074 } break;
1075
Eric Laurentc2f1f072009-07-17 12:17:14 -07001076 case SET_FORCE_USE: {
1077 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001078 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1079 data.readInt32());
Dima Zavinfce7a472011-04-19 22:30:36 -07001080 audio_policy_forced_cfg_t config =
1081 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001082 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
1083 return NO_ERROR;
1084 } break;
1085
1086 case GET_FORCE_USE: {
1087 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001088 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1089 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001090 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
1091 return NO_ERROR;
1092 } break;
1093
1094 case GET_OUTPUT: {
1095 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001096 audio_stream_type_t stream =
1097 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +00001098 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001099 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001100 return NO_ERROR;
1101 } break;
1102
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001103 case GET_OUTPUT_FOR_ATTR: {
1104 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001105 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -08001106 bool hasAttributes = data.readInt32() != 0;
1107 if (hasAttributes) {
1108 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001109 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -08001110 }
1111 audio_session_t session = (audio_session_t)data.readInt32();
1112 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
1113 bool hasStream = data.readInt32() != 0;
1114 if (hasStream) {
1115 stream = (audio_stream_type_t)data.readInt32();
1116 }
Nadav Bar766fb022018-01-07 12:18:03 +02001117 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001118 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001119 audio_config_t config;
1120 memset(&config, 0, sizeof(audio_config_t));
1121 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001122 audio_output_flags_t flags =
1123 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001124 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001125 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001126 audio_io_handle_t output = 0;
Eric Laurente83b55d2014-11-14 10:06:21 -08001127 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001128 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001129 &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001130 flags, &selectedDeviceId, &portId);
Eric Laurente83b55d2014-11-14 10:06:21 -08001131 reply->writeInt32(status);
1132 reply->writeInt32(output);
1133 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001134 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001135 reply->writeInt32(portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001136 return NO_ERROR;
1137 } break;
1138
Eric Laurentc2f1f072009-07-17 12:17:14 -07001139 case START_OUTPUT: {
1140 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001141 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1142 reply->writeInt32(static_cast <uint32_t>(startOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001143 return NO_ERROR;
1144 } break;
1145
1146 case STOP_OUTPUT: {
1147 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001148 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1149 reply->writeInt32(static_cast <uint32_t>(stopOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001150 return NO_ERROR;
1151 } break;
1152
1153 case RELEASE_OUTPUT: {
1154 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001155 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1156 releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001157 return NO_ERROR;
1158 } break;
1159
Eric Laurentcaf7f482014-11-25 17:50:47 -08001160 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001161 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001162 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001163 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001164 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001165 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001166 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001167 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001168 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001169 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001170 audio_config_base_t config;
1171 memset(&config, 0, sizeof(audio_config_base_t));
1172 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001173 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001174 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001175 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001176 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001177 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001178 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001179 reply->writeInt32(status);
1180 if (status == NO_ERROR) {
1181 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001182 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001183 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001184 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001185 return NO_ERROR;
1186 } break;
1187
1188 case START_INPUT: {
1189 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001190 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001191 bool silenced = data.readInt32() == 1;
Eric Laurentfee19762018-01-29 18:44:13 -08001192 status_t status = startInput(portId, &silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001193 reply->writeInt32(static_cast <uint32_t>(status));
1194 reply->writeInt32(silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001195 return NO_ERROR;
1196 } break;
1197
1198 case STOP_INPUT: {
1199 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001200 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1201 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001202 return NO_ERROR;
1203 } break;
1204
1205 case RELEASE_INPUT: {
1206 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001207 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1208 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001209 return NO_ERROR;
1210 } break;
1211
1212 case INIT_STREAM_VOLUME: {
1213 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001214 audio_stream_type_t stream =
1215 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001216 int indexMin = data.readInt32();
1217 int indexMax = data.readInt32();
1218 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1219 return NO_ERROR;
1220 } break;
1221
1222 case SET_STREAM_VOLUME: {
1223 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001224 audio_stream_type_t stream =
1225 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001226 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001227 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1228 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1229 index,
1230 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001231 return NO_ERROR;
1232 } break;
1233
1234 case GET_STREAM_VOLUME: {
1235 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001236 audio_stream_type_t stream =
1237 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurent83844cc2011-11-18 16:43:31 -08001238 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001239 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001240 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001241 reply->writeInt32(index);
1242 reply->writeInt32(static_cast <uint32_t>(status));
1243 return NO_ERROR;
1244 } break;
1245
Eric Laurentde070132010-07-13 04:45:46 -07001246 case GET_STRATEGY_FOR_STREAM: {
1247 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001248 audio_stream_type_t stream =
1249 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentde070132010-07-13 04:45:46 -07001250 reply->writeInt32(getStrategyForStream(stream));
1251 return NO_ERROR;
1252 } break;
1253
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001254 case GET_DEVICES_FOR_STREAM: {
1255 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001256 audio_stream_type_t stream =
1257 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001258 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1259 return NO_ERROR;
1260 } break;
1261
Eric Laurentde070132010-07-13 04:45:46 -07001262 case GET_OUTPUT_FOR_EFFECT: {
1263 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001264 effect_descriptor_t desc = {};
1265 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1266 android_errorWriteLog(0x534e4554, "73126106");
1267 }
1268 (void)sanitizeEffectDescriptor(&desc);
Eric Laurentde070132010-07-13 04:45:46 -07001269 audio_io_handle_t output = getOutputForEffect(&desc);
1270 reply->writeInt32(static_cast <int>(output));
1271 return NO_ERROR;
1272 } break;
1273
1274 case REGISTER_EFFECT: {
1275 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001276 effect_descriptor_t desc = {};
1277 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1278 android_errorWriteLog(0x534e4554, "73126106");
1279 }
1280 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001281 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001282 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001283 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001284 int id = data.readInt32();
1285 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001286 io,
Eric Laurentde070132010-07-13 04:45:46 -07001287 strategy,
1288 session,
1289 id)));
1290 return NO_ERROR;
1291 } break;
1292
1293 case UNREGISTER_EFFECT: {
1294 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1295 int id = data.readInt32();
1296 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1297 return NO_ERROR;
1298 } break;
1299
Eric Laurentdb7c0792011-08-10 10:37:50 -07001300 case SET_EFFECT_ENABLED: {
1301 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1302 int id = data.readInt32();
1303 bool enabled = static_cast <bool>(data.readInt32());
1304 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1305 return NO_ERROR;
1306 } break;
1307
Eric Laurenteda6c362011-02-02 09:33:30 -08001308 case IS_STREAM_ACTIVE: {
1309 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001310 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001311 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001312 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001313 return NO_ERROR;
1314 } break;
1315
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001316 case IS_STREAM_ACTIVE_REMOTELY: {
1317 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1318 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1319 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001320 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001321 return NO_ERROR;
1322 } break;
1323
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001324 case IS_SOURCE_ACTIVE: {
1325 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1326 audio_source_t source = (audio_source_t) data.readInt32();
1327 reply->writeInt32( isSourceActive(source));
1328 return NO_ERROR;
1329 }
1330
Eric Laurent57dae992011-07-24 13:36:09 -07001331 case QUERY_DEFAULT_PRE_PROCESSING: {
1332 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001333 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001334 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001335 if (count > AudioEffect::kMaxPreProcessing) {
1336 count = AudioEffect::kMaxPreProcessing;
1337 }
Eric Laurent57dae992011-07-24 13:36:09 -07001338 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001339 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001340 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1341 reply->writeInt32(status);
1342 if (status != NO_ERROR && status != NO_MEMORY) {
1343 retCount = 0;
1344 }
1345 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001346 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001347 if (retCount < count) {
1348 count = retCount;
1349 }
1350 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1351 }
1352 delete[] descriptors;
1353 return status;
1354 }
1355
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001356 case IS_OFFLOAD_SUPPORTED: {
1357 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001358 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001359 data.read(&info, sizeof(audio_offload_info_t));
1360 bool isSupported = isOffloadSupported(info);
1361 reply->writeInt32(isSupported);
1362 return NO_ERROR;
1363 }
1364
Eric Laurent203b1a12014-04-01 10:34:16 -07001365 case LIST_AUDIO_PORTS: {
1366 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1367 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1368 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1369 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001370 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1371 numPortsReq = MAX_ITEMS_PER_LIST;
1372 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001373 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001374 struct audio_port *ports =
1375 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001376 if (ports == NULL) {
1377 reply->writeInt32(NO_MEMORY);
1378 reply->writeInt32(0);
1379 return NO_ERROR;
1380 }
1381 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001382 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1383 reply->writeInt32(status);
1384 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001385
1386 if (status == NO_ERROR) {
1387 if (numPortsReq > numPorts) {
1388 numPortsReq = numPorts;
1389 }
1390 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1391 reply->writeInt32(generation);
1392 }
1393 free(ports);
1394 return NO_ERROR;
1395 }
1396
1397 case GET_AUDIO_PORT: {
1398 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001399 struct audio_port port = {};
1400 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1401 ALOGE("b/23912202");
1402 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001403 status_t status = getAudioPort(&port);
1404 reply->writeInt32(status);
1405 if (status == NO_ERROR) {
1406 reply->write(&port, sizeof(struct audio_port));
1407 }
1408 return NO_ERROR;
1409 }
1410
1411 case CREATE_AUDIO_PATCH: {
1412 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001413 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001414 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001415 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001416 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1417 ALOGE("b/23912202");
1418 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001419 status_t status = createAudioPatch(&patch, &handle);
1420 reply->writeInt32(status);
1421 if (status == NO_ERROR) {
1422 reply->write(&handle, sizeof(audio_patch_handle_t));
1423 }
1424 return NO_ERROR;
1425 }
1426
1427 case RELEASE_AUDIO_PATCH: {
1428 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001429 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001430 data.read(&handle, sizeof(audio_patch_handle_t));
1431 status_t status = releaseAudioPatch(handle);
1432 reply->writeInt32(status);
1433 return NO_ERROR;
1434 }
1435
1436 case LIST_AUDIO_PATCHES: {
1437 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1438 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001439 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1440 numPatchesReq = MAX_ITEMS_PER_LIST;
1441 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001442 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001443 struct audio_patch *patches =
1444 (struct audio_patch *)calloc(numPatchesReq,
1445 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001446 if (patches == NULL) {
1447 reply->writeInt32(NO_MEMORY);
1448 reply->writeInt32(0);
1449 return NO_ERROR;
1450 }
1451 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001452 status_t status = listAudioPatches(&numPatches, patches, &generation);
1453 reply->writeInt32(status);
1454 reply->writeInt32(numPatches);
1455 if (status == NO_ERROR) {
1456 if (numPatchesReq > numPatches) {
1457 numPatchesReq = numPatches;
1458 }
1459 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1460 reply->writeInt32(generation);
1461 }
1462 free(patches);
1463 return NO_ERROR;
1464 }
1465
1466 case SET_AUDIO_PORT_CONFIG: {
1467 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001468 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001469 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001470 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001471 status_t status = setAudioPortConfig(&config);
1472 reply->writeInt32(status);
1473 return NO_ERROR;
1474 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001475
Eric Laurentb52c1522014-05-20 11:27:36 -07001476 case REGISTER_CLIENT: {
1477 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1478 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1479 data.readStrongBinder());
1480 registerClient(client);
1481 return NO_ERROR;
1482 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001483
Eric Laurente8726fe2015-06-26 09:39:24 -07001484 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1485 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1486 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1487 return NO_ERROR;
1488 } break;
1489
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001490 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1491 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1492 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1493 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001494 audio_session_t session = AUDIO_SESSION_NONE;
1495 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1496 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001497 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1498 reply->writeInt32(status);
1499 if (status == NO_ERROR) {
1500 reply->writeInt32(session);
1501 reply->writeInt32(ioHandle);
1502 reply->writeInt32(device);
1503 }
1504 return NO_ERROR;
1505 } break;
1506
1507 case RELEASE_SOUNDTRIGGER_SESSION: {
1508 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1509 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1510 data.readStrongBinder());
1511 audio_session_t session = (audio_session_t)data.readInt32();
1512 status_t status = releaseSoundTriggerSession(session);
1513 reply->writeInt32(status);
1514 return NO_ERROR;
1515 } break;
1516
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001517 case GET_PHONE_STATE: {
1518 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1519 reply->writeInt32((int32_t)getPhoneState());
1520 return NO_ERROR;
1521 } break;
1522
Eric Laurentbaac1832014-12-01 17:52:59 -08001523 case REGISTER_POLICY_MIXES: {
1524 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1525 bool registration = data.readInt32() == 1;
1526 Vector<AudioMix> mixes;
1527 size_t size = (size_t)data.readInt32();
1528 if (size > MAX_MIXES_PER_POLICY) {
1529 size = MAX_MIXES_PER_POLICY;
1530 }
1531 for (size_t i = 0; i < size; i++) {
1532 AudioMix mix;
1533 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1534 mixes.add(mix);
1535 }
1536 }
1537 status_t status = registerPolicyMixes(mixes, registration);
1538 reply->writeInt32(status);
1539 return NO_ERROR;
1540 } break;
1541
Eric Laurent554a2772015-04-10 11:29:24 -07001542 case START_AUDIO_SOURCE: {
1543 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001544 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001545 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001546 (void)sanitizeAudioPortConfig(&source);
1547 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001548 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001549 sanetizeAudioAttributes(&attributes);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001550 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
1551 status_t status = startAudioSource(&source, &attributes, &portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001552 reply->writeInt32(status);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001553 reply->writeInt32(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001554 return NO_ERROR;
1555 } break;
1556
1557 case STOP_AUDIO_SOURCE: {
1558 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001559 audio_port_handle_t portId = (audio_port_handle_t) data.readInt32();
1560 status_t status = stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001561 reply->writeInt32(status);
1562 return NO_ERROR;
1563 } break;
1564
Andy Hung2ddee192015-12-18 17:34:44 -08001565 case SET_MASTER_MONO: {
1566 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1567 bool mono = static_cast<bool>(data.readInt32());
1568 status_t status = setMasterMono(mono);
1569 reply->writeInt32(status);
1570 return NO_ERROR;
1571 } break;
1572
1573 case GET_MASTER_MONO: {
1574 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1575 bool mono;
1576 status_t status = getMasterMono(&mono);
1577 reply->writeInt32(status);
1578 if (status == NO_ERROR) {
1579 reply->writeInt32(static_cast<int32_t>(mono));
1580 }
1581 return NO_ERROR;
1582 } break;
1583
Eric Laurentac9cef52017-06-09 15:46:26 -07001584 case GET_STREAM_VOLUME_DB: {
1585 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1586 audio_stream_type_t stream =
1587 static_cast <audio_stream_type_t>(data.readInt32());
1588 int index = static_cast <int>(data.readInt32());
1589 audio_devices_t device =
1590 static_cast <audio_devices_t>(data.readUint32());
1591 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1592 return NO_ERROR;
1593 }
1594
jiabin81772902018-04-02 17:52:27 -07001595 case GET_SURROUND_FORMATS: {
1596 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1597 unsigned int numSurroundFormatsReq = data.readUint32();
1598 if (numSurroundFormatsReq > MAX_ITEMS_PER_LIST) {
1599 numSurroundFormatsReq = MAX_ITEMS_PER_LIST;
1600 }
1601 bool reported = data.readBool();
1602 unsigned int numSurroundFormats = numSurroundFormatsReq;
1603 audio_format_t *surroundFormats = (audio_format_t *)calloc(
1604 numSurroundFormats, sizeof(audio_format_t));
1605 bool *surroundFormatsEnabled = (bool *)calloc(numSurroundFormats, sizeof(bool));
1606 if (numSurroundFormatsReq > 0 &&
1607 (surroundFormats == NULL || surroundFormatsEnabled == NULL)) {
1608 free(surroundFormats);
1609 free(surroundFormatsEnabled);
1610 reply->writeInt32(NO_MEMORY);
1611 return NO_ERROR;
1612 }
1613 status_t status = getSurroundFormats(
1614 &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1615 reply->writeInt32(status);
1616
1617 if (status == NO_ERROR) {
1618 reply->writeUint32(numSurroundFormats);
1619 if (numSurroundFormatsReq > numSurroundFormats) {
1620 numSurroundFormatsReq = numSurroundFormats;
1621 }
1622 reply->write(surroundFormats, numSurroundFormatsReq * sizeof(audio_format_t));
1623 reply->write(surroundFormatsEnabled, numSurroundFormatsReq * sizeof(bool));
1624 }
1625 free(surroundFormats);
1626 free(surroundFormatsEnabled);
1627 return NO_ERROR;
1628 }
1629
1630 case SET_SURROUND_FORMAT_ENABLED: {
1631 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1632 audio_format_t audioFormat = (audio_format_t) data.readInt32();
1633 bool enabled = data.readBool();
1634 status_t status = setSurroundFormatEnabled(audioFormat, enabled);
1635 reply->writeInt32(status);
1636 return NO_ERROR;
1637 }
1638
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -07001639 case ADD_STREAM_DEFAULT_EFFECT: {
1640 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1641 effect_uuid_t type;
1642 status_t status = data.read(&type, sizeof(effect_uuid_t));
1643 if (status != NO_ERROR) {
1644 return status;
1645 }
1646 String16 opPackageName;
1647 status = data.readString16(&opPackageName);
1648 if (status != NO_ERROR) {
1649 return status;
1650 }
1651 effect_uuid_t uuid;
1652 status = data.read(&uuid, sizeof(effect_uuid_t));
1653 if (status != NO_ERROR) {
1654 return status;
1655 }
1656 int32_t priority = data.readInt32();
1657 audio_usage_t usage = (audio_usage_t) data.readInt32();
1658 audio_unique_id_t id = 0;
1659 reply->writeInt32(static_cast <int32_t>(addStreamDefaultEffect(&type,
1660 opPackageName,
1661 &uuid,
1662 priority,
1663 usage,
1664 &id)));
1665 reply->writeInt32(id);
1666 return NO_ERROR;
1667 }
1668
1669 case REMOVE_STREAM_DEFAULT_EFFECT: {
1670 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1671 audio_unique_id_t id = static_cast<audio_unique_id_t>(data.readInt32());
1672 reply->writeInt32(static_cast <int32_t>(removeStreamDefaultEffect(id)));
1673 return NO_ERROR;
1674 }
1675
Ari Hausman-Cohen24628312018-08-13 15:01:09 -07001676 case ADD_SOURCE_DEFAULT_EFFECT: {
1677 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1678 effect_uuid_t type;
1679 status_t status = data.read(&type, sizeof(effect_uuid_t));
1680 if (status != NO_ERROR) {
1681 return status;
1682 }
1683 String16 opPackageName;
1684 status = data.readString16(&opPackageName);
1685 if (status != NO_ERROR) {
1686 return status;
1687 }
1688 effect_uuid_t uuid;
1689 status = data.read(&uuid, sizeof(effect_uuid_t));
1690 if (status != NO_ERROR) {
1691 return status;
1692 }
1693 int32_t priority = data.readInt32();
1694 audio_source_t source = (audio_source_t) data.readInt32();
1695 audio_unique_id_t id = 0;
1696 reply->writeInt32(static_cast <int32_t>(addSourceDefaultEffect(&type,
1697 opPackageName,
1698 &uuid,
1699 priority,
1700 source,
1701 &id)));
1702 reply->writeInt32(id);
1703 return NO_ERROR;
1704 }
1705
1706 case REMOVE_SOURCE_DEFAULT_EFFECT: {
1707 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1708 audio_unique_id_t id = static_cast<audio_unique_id_t>(data.readInt32());
1709 reply->writeInt32(static_cast <int32_t>(removeSourceDefaultEffect(id)));
1710 return NO_ERROR;
1711 }
1712
Eric Laurentc2f1f072009-07-17 12:17:14 -07001713 default:
1714 return BBinder::onTransact(code, data, reply, flags);
1715 }
1716}
1717
Andy Hungb0272092018-04-12 11:06:56 -07001718/** returns true if string overflow was prevented by zero termination */
1719template <size_t size>
1720static bool preventStringOverflow(char (&s)[size]) {
1721 if (strnlen(s, size) < size) return false;
1722 s[size - 1] = '\0';
1723 return true;
1724}
1725
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001726void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
1727{
1728 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
1729 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
1730 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
1731 }
1732 attr->tags[tagsMaxSize - 1] = '\0';
1733}
1734
Andy Hungb0272092018-04-12 11:06:56 -07001735/** returns BAD_VALUE if sanitization was required. */
1736status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
1737{
1738 if (preventStringOverflow(desc->name)
1739 | /* always */ preventStringOverflow(desc->implementor)) {
1740 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
1741 return BAD_VALUE;
1742 }
1743 return NO_ERROR;
1744}
1745
1746/** returns BAD_VALUE if sanitization was required. */
1747status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
1748{
1749 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
1750 preventStringOverflow(config->ext.device.address)) {
1751 return BAD_VALUE;
1752 }
1753 return NO_ERROR;
1754}
1755
Eric Laurentc2f1f072009-07-17 12:17:14 -07001756// ----------------------------------------------------------------------------
1757
Glenn Kasten40bc9062015-03-20 09:09:33 -07001758} // namespace android