blob: d9f6e3678b9588b6431ed9b3b24003d36e4cbb70 [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
Kevin Rocard153f92d2018-12-18 18:33:28 -0800192 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,
196 pid_t pid,
197 uid_t uid,
198 const audio_config_t *config,
199 audio_output_flags_t flags,
200 audio_port_handle_t *selectedDeviceId,
201 audio_port_handle_t *portId,
202 std::vector<audio_io_handle_t> *secondaryOutputs) override
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700203 {
204 Parcel data, reply;
205 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
206 if (attr == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800207 if (stream == NULL) {
208 ALOGE("getOutputForAttr(): NULL audio attributes and stream type");
209 return BAD_VALUE;
210 }
211 if (*stream == AUDIO_STREAM_DEFAULT) {
212 ALOGE("getOutputForAttr unspecified stream type");
213 return BAD_VALUE;
214 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700215 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800216 if (output == NULL) {
217 ALOGE("getOutputForAttr NULL output - shouldn't happen");
218 return BAD_VALUE;
219 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700220 if (selectedDeviceId == NULL) {
221 ALOGE("getOutputForAttr NULL selectedDeviceId - shouldn't happen");
222 return BAD_VALUE;
223 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800224 if (portId == NULL) {
225 ALOGE("getOutputForAttr NULL portId - shouldn't happen");
226 return BAD_VALUE;
227 }
Kevin Rocard153f92d2018-12-18 18:33:28 -0800228 if (secondaryOutputs == NULL) {
229 ALOGE("getOutputForAttr NULL secondaryOutputs - shouldn't happen");
230 return BAD_VALUE;
231 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800232 if (attr == NULL) {
233 data.writeInt32(0);
234 } else {
235 data.writeInt32(1);
236 data.write(attr, sizeof(audio_attributes_t));
237 }
238 data.writeInt32(session);
239 if (stream == NULL) {
240 data.writeInt32(0);
241 } else {
242 data.writeInt32(1);
243 data.writeInt32(*stream);
244 }
Nadav Bar766fb022018-01-07 12:18:03 +0200245 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700246 data.writeInt32(uid);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800247 data.write(config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700248 data.writeInt32(static_cast <uint32_t>(flags));
Eric Laurent9ae8c592017-06-22 17:17:09 -0700249 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800250 data.writeInt32(*portId);
Eric Laurente83b55d2014-11-14 10:06:21 -0800251 status_t status = remote()->transact(GET_OUTPUT_FOR_ATTR, data, &reply);
252 if (status != NO_ERROR) {
253 return status;
254 }
255 status = (status_t)reply.readInt32();
256 if (status != NO_ERROR) {
257 return status;
258 }
259 *output = (audio_io_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800260 audio_stream_type_t lStream = (audio_stream_type_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800261 if (stream != NULL) {
Eric Laurent20b9ef02016-12-05 11:03:16 -0800262 *stream = lStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800263 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700264 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800265 *portId = (audio_port_handle_t)reply.readInt32();
Kevin Rocard153f92d2018-12-18 18:33:28 -0800266 secondaryOutputs->resize(reply.readInt32());
267 return reply.read(secondaryOutputs->data(),
268 secondaryOutputs->size() * sizeof(audio_io_handle_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700269 }
270
Eric Laurentd7fe0862018-07-14 16:48:01 -0700271 virtual status_t startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700272 {
273 Parcel data, reply;
274 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700275 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700276 remote()->transact(START_OUTPUT, data, &reply);
277 return static_cast <status_t> (reply.readInt32());
278 }
279
Eric Laurentd7fe0862018-07-14 16:48:01 -0700280 virtual status_t stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700281 {
282 Parcel data, reply;
283 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700284 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700285 remote()->transact(STOP_OUTPUT, data, &reply);
286 return static_cast <status_t> (reply.readInt32());
287 }
288
Eric Laurentd7fe0862018-07-14 16:48:01 -0700289 virtual void releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700290 {
291 Parcel data, reply;
292 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700293 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700294 remote()->transact(RELEASE_OUTPUT, data, &reply);
295 }
296
Eric Laurentcaf7f482014-11-25 17:50:47 -0800297 virtual status_t getInputForAttr(const audio_attributes_t *attr,
298 audio_io_handle_t *input,
299 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700300 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700301 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800302 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800303 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600304 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700305 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800306 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700307 {
308 Parcel data, reply;
309 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentcaf7f482014-11-25 17:50:47 -0800310 if (attr == NULL) {
311 ALOGE("getInputForAttr NULL attr - shouldn't happen");
312 return BAD_VALUE;
313 }
314 if (input == NULL) {
315 ALOGE("getInputForAttr NULL input - shouldn't happen");
316 return BAD_VALUE;
317 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700318 if (selectedDeviceId == NULL) {
319 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
320 return BAD_VALUE;
321 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800322 if (portId == NULL) {
323 ALOGE("getInputForAttr NULL portId - shouldn't happen");
324 return BAD_VALUE;
325 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800326 data.write(attr, sizeof(audio_attributes_t));
Eric Laurenta54f1282017-07-01 19:39:32 -0700327 data.writeInt32(*input);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800328 data.writeInt32(session);
Eric Laurentb2379ba2016-05-23 17:42:12 -0700329 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700330 data.writeInt32(uid);
Eric Laurentfee19762018-01-29 18:44:13 -0800331 data.writeString16(opPackageName);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800332 data.write(config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -0700333 data.writeInt32(flags);
Eric Laurent9ae8c592017-06-22 17:17:09 -0700334 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800335 data.writeInt32(*portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800336 status_t status = remote()->transact(GET_INPUT_FOR_ATTR, data, &reply);
337 if (status != NO_ERROR) {
338 return status;
339 }
340 status = reply.readInt32();
341 if (status != NO_ERROR) {
342 return status;
343 }
344 *input = (audio_io_handle_t)reply.readInt32();
Eric Laurent9ae8c592017-06-22 17:17:09 -0700345 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800346 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800347 return NO_ERROR;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700348 }
349
Eric Laurent4eb58f12018-12-07 16:41:02 -0800350 virtual status_t startInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700351 {
352 Parcel data, reply;
353 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800354 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700355 remote()->transact(START_INPUT, data, &reply);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800356 status_t status = static_cast <status_t> (reply.readInt32());
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800357 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700358 }
359
Eric Laurentfee19762018-01-29 18:44:13 -0800360 virtual status_t stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700361 {
362 Parcel data, reply;
363 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800364 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700365 remote()->transact(STOP_INPUT, data, &reply);
366 return static_cast <status_t> (reply.readInt32());
367 }
368
Eric Laurentfee19762018-01-29 18:44:13 -0800369 virtual void releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700370 {
371 Parcel data, reply;
372 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800373 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700374 remote()->transact(RELEASE_INPUT, data, &reply);
375 }
376
Dima Zavinfce7a472011-04-19 22:30:36 -0700377 virtual status_t initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700378 int indexMin,
379 int indexMax)
380 {
381 Parcel data, reply;
382 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
383 data.writeInt32(static_cast <uint32_t>(stream));
384 data.writeInt32(indexMin);
385 data.writeInt32(indexMax);
386 remote()->transact(INIT_STREAM_VOLUME, data, &reply);
387 return static_cast <status_t> (reply.readInt32());
388 }
389
Eric Laurent83844cc2011-11-18 16:43:31 -0800390 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
391 int index,
392 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700393 {
394 Parcel data, reply;
395 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
396 data.writeInt32(static_cast <uint32_t>(stream));
397 data.writeInt32(index);
Eric Laurent83844cc2011-11-18 16:43:31 -0800398 data.writeInt32(static_cast <uint32_t>(device));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700399 remote()->transact(SET_STREAM_VOLUME, data, &reply);
400 return static_cast <status_t> (reply.readInt32());
401 }
402
Eric Laurent83844cc2011-11-18 16:43:31 -0800403 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
404 int *index,
405 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700406 {
407 Parcel data, reply;
408 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
409 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurent83844cc2011-11-18 16:43:31 -0800410 data.writeInt32(static_cast <uint32_t>(device));
411
Eric Laurentc2f1f072009-07-17 12:17:14 -0700412 remote()->transact(GET_STREAM_VOLUME, data, &reply);
413 int lIndex = reply.readInt32();
414 if (index) *index = lIndex;
415 return static_cast <status_t> (reply.readInt32());
416 }
Eric Laurentde070132010-07-13 04:45:46 -0700417
Dima Zavinfce7a472011-04-19 22:30:36 -0700418 virtual uint32_t getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700419 {
420 Parcel data, reply;
421 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
422 data.writeInt32(static_cast <uint32_t>(stream));
423 remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply);
François Gaffiec005e562018-11-06 15:04:49 +0100424 return reply.readUint32();
Eric Laurentde070132010-07-13 04:45:46 -0700425 }
426
Eric Laurent63742522012-03-08 13:42:42 -0800427 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800428 {
429 Parcel data, reply;
430 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
431 data.writeInt32(static_cast <uint32_t>(stream));
432 remote()->transact(GET_DEVICES_FOR_STREAM, data, &reply);
Eric Laurent63742522012-03-08 13:42:42 -0800433 return (audio_devices_t) reply.readInt32();
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800434 }
435
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700436 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700437 {
438 Parcel data, reply;
439 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
440 data.write(desc, sizeof(effect_descriptor_t));
441 remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply);
442 return static_cast <audio_io_handle_t> (reply.readInt32());
443 }
444
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700445 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700446 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700447 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800448 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700449 int id)
450 {
451 Parcel data, reply;
452 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
453 data.write(desc, sizeof(effect_descriptor_t));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700454 data.writeInt32(io);
Eric Laurentde070132010-07-13 04:45:46 -0700455 data.writeInt32(strategy);
456 data.writeInt32(session);
457 data.writeInt32(id);
458 remote()->transact(REGISTER_EFFECT, data, &reply);
459 return static_cast <status_t> (reply.readInt32());
460 }
461
462 virtual status_t unregisterEffect(int id)
463 {
464 Parcel data, reply;
465 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
466 data.writeInt32(id);
467 remote()->transact(UNREGISTER_EFFECT, data, &reply);
468 return static_cast <status_t> (reply.readInt32());
469 }
470
Eric Laurentdb7c0792011-08-10 10:37:50 -0700471 virtual status_t setEffectEnabled(int id, bool enabled)
472 {
473 Parcel data, reply;
474 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
475 data.writeInt32(id);
476 data.writeInt32(enabled);
477 remote()->transact(SET_EFFECT_ENABLED, data, &reply);
478 return static_cast <status_t> (reply.readInt32());
479 }
480
Glenn Kastenfff6d712012-01-12 16:38:12 -0800481 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800482 {
483 Parcel data, reply;
484 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Glenn Kastenfff6d712012-01-12 16:38:12 -0800485 data.writeInt32((int32_t) stream);
Eric Laurenteda6c362011-02-02 09:33:30 -0800486 data.writeInt32(inPastMs);
487 remote()->transact(IS_STREAM_ACTIVE, data, &reply);
488 return reply.readInt32();
489 }
Eric Laurent57dae992011-07-24 13:36:09 -0700490
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800491 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
492 {
493 Parcel data, reply;
494 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
495 data.writeInt32((int32_t) stream);
496 data.writeInt32(inPastMs);
497 remote()->transact(IS_STREAM_ACTIVE_REMOTELY, data, &reply);
498 return reply.readInt32();
499 }
500
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700501 virtual bool isSourceActive(audio_source_t source) const
502 {
503 Parcel data, reply;
504 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
505 data.writeInt32((int32_t) source);
506 remote()->transact(IS_SOURCE_ACTIVE, data, &reply);
507 return reply.readInt32();
508 }
509
Glenn Kastend848eb42016-03-08 13:42:11 -0800510 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700511 effect_descriptor_t *descriptors,
512 uint32_t *count)
513 {
514 if (descriptors == NULL || count == NULL) {
515 return BAD_VALUE;
516 }
517 Parcel data, reply;
518 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
519 data.writeInt32(audioSession);
520 data.writeInt32(*count);
521 status_t status = remote()->transact(QUERY_DEFAULT_PRE_PROCESSING, data, &reply);
522 if (status != NO_ERROR) {
523 return status;
524 }
525 status = static_cast <status_t> (reply.readInt32());
526 uint32_t retCount = reply.readInt32();
527 if (retCount != 0) {
528 uint32_t numDesc = (retCount < *count) ? retCount : *count;
529 reply.read(descriptors, sizeof(effect_descriptor_t) * numDesc);
530 }
531 *count = retCount;
532 return status;
533 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000534
535 virtual bool isOffloadSupported(const audio_offload_info_t& info)
536 {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100537 Parcel data, reply;
538 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
539 data.write(&info, sizeof(audio_offload_info_t));
540 remote()->transact(IS_OFFLOAD_SUPPORTED, data, &reply);
Eric Laurent203b1a12014-04-01 10:34:16 -0700541 return reply.readInt32();
542 }
543
Michael Chana94fbb22018-04-24 14:31:19 +1000544 virtual bool isDirectOutputSupported(const audio_config_base_t& config,
545 const audio_attributes_t& attributes) {
546 Parcel data, reply;
547 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
548 data.write(&config, sizeof(audio_config_base_t));
549 data.write(&attributes, sizeof(audio_attributes_t));
550 status_t status = remote()->transact(IS_DIRECT_OUTPUT_SUPPORTED, data, &reply);
551 return status == NO_ERROR ? static_cast<bool>(reply.readInt32()) : false;
552 }
553
Eric Laurent203b1a12014-04-01 10:34:16 -0700554 virtual status_t listAudioPorts(audio_port_role_t role,
555 audio_port_type_t type,
556 unsigned int *num_ports,
557 struct audio_port *ports,
558 unsigned int *generation)
559 {
560 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
561 generation == NULL) {
562 return BAD_VALUE;
563 }
564 Parcel data, reply;
565 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
566 unsigned int numPortsReq = (ports == NULL) ? 0 : *num_ports;
567 data.writeInt32(role);
568 data.writeInt32(type);
569 data.writeInt32(numPortsReq);
570 status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply);
571 if (status == NO_ERROR) {
572 status = (status_t)reply.readInt32();
573 *num_ports = (unsigned int)reply.readInt32();
574 }
Eric Laurent203b1a12014-04-01 10:34:16 -0700575 if (status == NO_ERROR) {
576 if (numPortsReq > *num_ports) {
577 numPortsReq = *num_ports;
578 }
579 if (numPortsReq > 0) {
580 reply.read(ports, numPortsReq * sizeof(struct audio_port));
581 }
582 *generation = reply.readInt32();
583 }
584 return status;
585 }
586
587 virtual status_t getAudioPort(struct audio_port *port)
588 {
589 if (port == NULL) {
590 return BAD_VALUE;
591 }
592 Parcel data, reply;
593 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
594 data.write(port, sizeof(struct audio_port));
595 status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply);
596 if (status != NO_ERROR ||
597 (status = (status_t)reply.readInt32()) != NO_ERROR) {
598 return status;
599 }
600 reply.read(port, sizeof(struct audio_port));
601 return status;
602 }
603
604 virtual status_t createAudioPatch(const struct audio_patch *patch,
605 audio_patch_handle_t *handle)
606 {
607 if (patch == NULL || handle == NULL) {
608 return BAD_VALUE;
609 }
610 Parcel data, reply;
611 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
612 data.write(patch, sizeof(struct audio_patch));
613 data.write(handle, sizeof(audio_patch_handle_t));
614 status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply);
615 if (status != NO_ERROR ||
616 (status = (status_t)reply.readInt32()) != NO_ERROR) {
617 return status;
618 }
619 reply.read(handle, sizeof(audio_patch_handle_t));
620 return status;
621 }
622
623 virtual status_t releaseAudioPatch(audio_patch_handle_t handle)
624 {
625 Parcel data, reply;
626 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
627 data.write(&handle, sizeof(audio_patch_handle_t));
628 status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply);
629 if (status != NO_ERROR) {
630 status = (status_t)reply.readInt32();
631 }
632 return status;
633 }
634
635 virtual status_t listAudioPatches(unsigned int *num_patches,
636 struct audio_patch *patches,
637 unsigned int *generation)
638 {
639 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
640 generation == NULL) {
641 return BAD_VALUE;
642 }
643 Parcel data, reply;
644 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
645 unsigned int numPatchesReq = (patches == NULL) ? 0 : *num_patches;
646 data.writeInt32(numPatchesReq);
647 status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply);
648 if (status == NO_ERROR) {
649 status = (status_t)reply.readInt32();
650 *num_patches = (unsigned int)reply.readInt32();
651 }
652 if (status == NO_ERROR) {
653 if (numPatchesReq > *num_patches) {
654 numPatchesReq = *num_patches;
655 }
656 if (numPatchesReq > 0) {
657 reply.read(patches, numPatchesReq * sizeof(struct audio_patch));
658 }
659 *generation = reply.readInt32();
660 }
661 return status;
662 }
663
664 virtual status_t setAudioPortConfig(const struct audio_port_config *config)
665 {
666 if (config == NULL) {
667 return BAD_VALUE;
668 }
669 Parcel data, reply;
670 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
671 data.write(config, sizeof(struct audio_port_config));
672 status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply);
673 if (status != NO_ERROR) {
674 status = (status_t)reply.readInt32();
675 }
676 return status;
677 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700678
Eric Laurentb52c1522014-05-20 11:27:36 -0700679 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client)
680 {
681 Parcel data, reply;
682 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -0800683 data.writeStrongBinder(IInterface::asBinder(client));
Eric Laurentb52c1522014-05-20 11:27:36 -0700684 remote()->transact(REGISTER_CLIENT, data, &reply);
685 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700686
Eric Laurente8726fe2015-06-26 09:39:24 -0700687 virtual void setAudioPortCallbacksEnabled(bool enabled)
688 {
689 Parcel data, reply;
690 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
691 data.writeInt32(enabled ? 1 : 0);
692 remote()->transact(SET_AUDIO_PORT_CALLBACK_ENABLED, data, &reply);
693 }
694
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700695 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
696 audio_io_handle_t *ioHandle,
697 audio_devices_t *device)
698 {
699 if (session == NULL || ioHandle == NULL || device == NULL) {
700 return BAD_VALUE;
701 }
702 Parcel data, reply;
703 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
704 status_t status = remote()->transact(ACQUIRE_SOUNDTRIGGER_SESSION, data, &reply);
705 if (status != NO_ERROR) {
706 return status;
707 }
708 status = (status_t)reply.readInt32();
709 if (status == NO_ERROR) {
710 *session = (audio_session_t)reply.readInt32();
711 *ioHandle = (audio_io_handle_t)reply.readInt32();
712 *device = (audio_devices_t)reply.readInt32();
713 }
714 return status;
715 }
716
717 virtual status_t releaseSoundTriggerSession(audio_session_t session)
718 {
719 Parcel data, reply;
720 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
721 data.writeInt32(session);
722 status_t status = remote()->transact(RELEASE_SOUNDTRIGGER_SESSION, data, &reply);
723 if (status != NO_ERROR) {
724 return status;
725 }
726 return (status_t)reply.readInt32();
727 }
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700728
729 virtual audio_mode_t getPhoneState()
730 {
731 Parcel data, reply;
732 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
733 status_t status = remote()->transact(GET_PHONE_STATE, data, &reply);
734 if (status != NO_ERROR) {
735 return AUDIO_MODE_INVALID;
736 }
737 return (audio_mode_t)reply.readInt32();
738 }
Eric Laurentbaac1832014-12-01 17:52:59 -0800739
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700740 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800741 {
742 Parcel data, reply;
743 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
744 data.writeInt32(registration ? 1 : 0);
745 size_t size = mixes.size();
746 if (size > MAX_MIXES_PER_POLICY) {
747 size = MAX_MIXES_PER_POLICY;
748 }
749 size_t sizePosition = data.dataPosition();
750 data.writeInt32(size);
751 size_t finalSize = size;
752 for (size_t i = 0; i < size; i++) {
753 size_t position = data.dataPosition();
754 if (mixes[i].writeToParcel(&data) != NO_ERROR) {
755 data.setDataPosition(position);
756 finalSize--;
757 }
758 }
759 if (size != finalSize) {
760 size_t position = data.dataPosition();
761 data.setDataPosition(sizePosition);
762 data.writeInt32(finalSize);
763 data.setDataPosition(position);
764 }
765 status_t status = remote()->transact(REGISTER_POLICY_MIXES, data, &reply);
766 if (status == NO_ERROR) {
767 status = (status_t)reply.readInt32();
768 }
769 return status;
770 }
Eric Laurent554a2772015-04-10 11:29:24 -0700771
772 virtual status_t startAudioSource(const struct audio_port_config *source,
773 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700774 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -0700775 {
776 Parcel data, reply;
777 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700778 if (source == NULL || attributes == NULL || portId == NULL) {
Eric Laurent554a2772015-04-10 11:29:24 -0700779 return BAD_VALUE;
780 }
781 data.write(source, sizeof(struct audio_port_config));
782 data.write(attributes, sizeof(audio_attributes_t));
783 status_t status = remote()->transact(START_AUDIO_SOURCE, data, &reply);
784 if (status != NO_ERROR) {
785 return status;
786 }
787 status = (status_t)reply.readInt32();
788 if (status != NO_ERROR) {
789 return status;
790 }
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700791 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -0700792 return status;
793 }
794
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700795 virtual status_t stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -0700796 {
797 Parcel data, reply;
798 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700799 data.writeInt32(portId);
Eric Laurent554a2772015-04-10 11:29:24 -0700800 status_t status = remote()->transact(STOP_AUDIO_SOURCE, data, &reply);
801 if (status != NO_ERROR) {
802 return status;
803 }
804 status = (status_t)reply.readInt32();
805 return status;
806 }
Andy Hung2ddee192015-12-18 17:34:44 -0800807
808 virtual status_t setMasterMono(bool mono)
809 {
810 Parcel data, reply;
811 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
812 data.writeInt32(static_cast<int32_t>(mono));
813 status_t status = remote()->transact(SET_MASTER_MONO, data, &reply);
814 if (status != NO_ERROR) {
815 return status;
816 }
817 return static_cast<status_t>(reply.readInt32());
818 }
819
820 virtual status_t getMasterMono(bool *mono)
821 {
822 if (mono == nullptr) {
823 return BAD_VALUE;
824 }
825 Parcel data, reply;
826 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
827
828 status_t status = remote()->transact(GET_MASTER_MONO, data, &reply);
829 if (status != NO_ERROR) {
830 return status;
831 }
832 status = static_cast<status_t>(reply.readInt32());
833 if (status == NO_ERROR) {
834 *mono = static_cast<bool>(reply.readInt32());
835 }
836 return status;
837 }
Eric Laurentac9cef52017-06-09 15:46:26 -0700838
839 virtual float getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
840 {
841 Parcel data, reply;
842 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
843 data.writeInt32(static_cast <int32_t>(stream));
844 data.writeInt32(static_cast <int32_t>(index));
845 data.writeUint32(static_cast <uint32_t>(device));
846 status_t status = remote()->transact(GET_STREAM_VOLUME_DB, data, &reply);
847 if (status != NO_ERROR) {
848 return NAN;
849 }
850 return reply.readFloat();
851 }
jiabin81772902018-04-02 17:52:27 -0700852
853 virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
854 audio_format_t *surroundFormats,
855 bool *surroundFormatsEnabled,
856 bool reported)
857 {
858 if (numSurroundFormats == NULL || (*numSurroundFormats != 0 &&
859 (surroundFormats == NULL || surroundFormatsEnabled == NULL))) {
860 return BAD_VALUE;
861 }
862 Parcel data, reply;
863 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
864 unsigned int numSurroundFormatsReq = *numSurroundFormats;
865 data.writeUint32(numSurroundFormatsReq);
866 data.writeBool(reported);
867 status_t status = remote()->transact(GET_SURROUND_FORMATS, data, &reply);
868 if (status == NO_ERROR && (status = (status_t)reply.readInt32()) == NO_ERROR) {
869 *numSurroundFormats = reply.readUint32();
870 }
871 if (status == NO_ERROR) {
872 if (numSurroundFormatsReq > *numSurroundFormats) {
873 numSurroundFormatsReq = *numSurroundFormats;
874 }
875 if (numSurroundFormatsReq > 0) {
876 status = reply.read(surroundFormats,
877 numSurroundFormatsReq * sizeof(audio_format_t));
878 if (status != NO_ERROR) {
879 return status;
880 }
881 status = reply.read(surroundFormatsEnabled,
882 numSurroundFormatsReq * sizeof(bool));
883 }
884 }
885 return status;
886 }
887
888 virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
889 {
890 Parcel data, reply;
891 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
892 data.writeInt32(audioFormat);
893 data.writeBool(enabled);
894 status_t status = remote()->transact(SET_SURROUND_FORMAT_ENABLED, data, &reply);
895 if (status != NO_ERROR) {
896 return status;
897 }
898 return reply.readInt32();
899 }
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700900
Arun Mirpuri11029ad2018-12-19 20:45:19 -0800901 virtual status_t getHwOffloadEncodingFormatsSupportedForA2DP(
902 std::vector<audio_format_t> *formats)
903 {
904 if (formats == NULL) {
905 return BAD_VALUE;
906 }
907
908 Parcel data, reply;
909 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
910 status_t status = remote()->transact(GET_OFFLOAD_FORMATS_A2DP, data, &reply);
911 if (status != NO_ERROR || (status = (status_t)reply.readInt32()) != NO_ERROR) {
912 return status;
913 }
914
915 size_t list_size = reply.readUint32();
916
917 for (size_t i = 0; i < list_size; i++) {
918 formats->push_back(static_cast<audio_format_t>(reply.readInt32()));
919 }
920 return NO_ERROR;
921 }
922
923
924 virtual status_t addStreamDefaultEffect(const effect_uuid_t *type,
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700925 const String16& opPackageName,
926 const effect_uuid_t *uuid,
927 int32_t priority,
928 audio_usage_t usage,
929 audio_unique_id_t* id)
930 {
931 Parcel data, reply;
932 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
933 data.write(type, sizeof(effect_uuid_t));
934 data.writeString16(opPackageName);
935 data.write(uuid, sizeof(effect_uuid_t));
936 data.writeInt32(priority);
937 data.writeInt32((int32_t) usage);
938 status_t status = remote()->transact(ADD_STREAM_DEFAULT_EFFECT, data, &reply);
939 if (status != NO_ERROR) {
940 return status;
941 }
942 status = static_cast <status_t> (reply.readInt32());
943 *id = reply.readInt32();
944 return status;
945 }
946
947 virtual status_t removeStreamDefaultEffect(audio_unique_id_t id)
948 {
949 Parcel data, reply;
950 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
951 data.writeInt32(id);
952 status_t status = remote()->transact(REMOVE_STREAM_DEFAULT_EFFECT, data, &reply);
953 if (status != NO_ERROR) {
954 return status;
955 }
956 return static_cast <status_t> (reply.readInt32());
957 }
958
Ari Hausman-Cohen24628312018-08-13 15:01:09 -0700959 virtual status_t addSourceDefaultEffect(const effect_uuid_t *type,
960 const String16& opPackageName,
961 const effect_uuid_t *uuid,
962 int32_t priority,
963 audio_source_t source,
964 audio_unique_id_t* id)
965 {
966 Parcel data, reply;
967 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
968 data.write(type, sizeof(effect_uuid_t));
969 data.writeString16(opPackageName);
970 data.write(uuid, sizeof(effect_uuid_t));
971 data.writeInt32(priority);
972 data.writeInt32((int32_t) source);
973 status_t status = remote()->transact(ADD_SOURCE_DEFAULT_EFFECT, data, &reply);
974 if (status != NO_ERROR) {
975 return status;
976 }
977 status = static_cast <status_t> (reply.readInt32());
978 *id = reply.readInt32();
979 return status;
980 }
981
982 virtual status_t removeSourceDefaultEffect(audio_unique_id_t id)
983 {
984 Parcel data, reply;
985 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
986 data.writeInt32(id);
987 status_t status = remote()->transact(REMOVE_SOURCE_DEFAULT_EFFECT, data, &reply);
988 if (status != NO_ERROR) {
989 return status;
990 }
991 return static_cast <status_t> (reply.readInt32());
992 }
993
Eric Laurentb78763e2018-10-17 10:08:02 -0700994 virtual status_t setAssistantUid(uid_t uid)
995 {
996 Parcel data, reply;
997 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
998 data.writeInt32(uid);
999 status_t status = remote()->transact(SET_ASSISTANT_UID, data, &reply);
1000 if (status != NO_ERROR) {
1001 return status;
1002 }
1003 return static_cast <status_t> (reply.readInt32());
1004 }
1005
1006 virtual status_t setA11yServicesUids(const std::vector<uid_t>& uids)
1007 {
1008 Parcel data, reply;
1009 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1010 data.writeInt32(uids.size());
1011 for (auto uid : uids) {
1012 data.writeInt32(uid);
1013 }
1014 status_t status = remote()->transact(SET_A11Y_SERVICES_UIDS, data, &reply);
1015 if (status != NO_ERROR) {
1016 return status;
1017 }
1018 return static_cast <status_t> (reply.readInt32());
1019 }
1020
jiabin6012f912018-11-02 17:06:30 -07001021 virtual bool isHapticPlaybackSupported()
1022 {
1023 Parcel data, reply;
1024 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1025 status_t status = remote()->transact(IS_HAPTIC_PLAYBACK_SUPPORTED, data, &reply);
1026 if (status != NO_ERROR) {
1027 return false;
1028 }
1029 return reply.readBool();
1030 }
1031
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001032 virtual status_t setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1033 {
1034 Parcel data, reply;
1035 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1036
1037 data.writeInt32((int32_t) uid);
1038 size_t size = devices.size();
1039 size_t sizePosition = data.dataPosition();
1040 data.writeInt32((int32_t) size);
1041 size_t finalSize = size;
1042 for (size_t i = 0; i < size; i++) {
1043 size_t position = data.dataPosition();
1044 if (devices[i].writeToParcel(&data) != NO_ERROR) {
1045 data.setDataPosition(position);
1046 finalSize--;
1047 }
1048 }
1049 if (size != finalSize) {
1050 size_t position = data.dataPosition();
1051 data.setDataPosition(sizePosition);
1052 data.writeInt32(finalSize);
1053 data.setDataPosition(position);
1054 }
1055
1056 status_t status = remote()->transact(SET_UID_DEVICE_AFFINITY, data, &reply);
1057 if (status == NO_ERROR) {
1058 status = (status_t)reply.readInt32();
1059 }
1060 return status;
1061 }
1062
François Gaffied0ba9ed2018-11-05 11:50:42 +01001063 virtual status_t removeUidDeviceAffinities(uid_t uid) {
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001064 Parcel data, reply;
1065 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1066
1067 data.writeInt32((int32_t) uid);
1068
François Gaffied0ba9ed2018-11-05 11:50:42 +01001069 status_t status =
1070 remote()->transact(REMOVE_UID_DEVICE_AFFINITY, data, &reply);
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001071 if (status == NO_ERROR) {
François Gaffied0ba9ed2018-11-05 11:50:42 +01001072 status = (status_t) reply.readInt32();
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001073 }
1074 return status;
1075 }
François Gaffied0ba9ed2018-11-05 11:50:42 +01001076
1077 virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies)
1078 {
1079 Parcel data, reply;
1080 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1081
1082 status_t status = remote()->transact(LIST_AUDIO_PRODUCT_STRATEGIES, data, &reply);
1083 if (status != NO_ERROR) {
1084 ALOGE("%s: permission denied", __func__);
1085 return status;
1086 }
1087 status = static_cast<status_t>(reply.readInt32());
1088 if (status == NO_ERROR) {
1089 uint32_t numStrategies = static_cast<uint32_t>(reply.readInt32());
1090 for (size_t i = 0; i < numStrategies; i++) {
1091 AudioProductStrategy strategy;
1092 status = strategy.readFromParcel(&reply);
1093 if (status != NO_ERROR) {
1094 ALOGE("%s: failed to read strategies", __FUNCTION__);
1095 strategies.clear();
1096 return status;
1097 }
1098 strategies.push_back(strategy);
1099 }
1100 }
1101 return status;
1102 }
1103
1104 virtual product_strategy_t getProductStrategyFromAudioAttributes(const AudioAttributes &aa)
1105 {
1106 Parcel data, reply;
1107 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1108 status_t status = aa.writeToParcel(&data);
1109 if (status != NO_ERROR) {
1110 return PRODUCT_STRATEGY_NONE;
1111 }
1112 status = remote()->transact(GET_STRATEGY_FOR_ATTRIBUTES, data, &reply);
1113 if (status == NO_ERROR) {
1114 return static_cast<product_strategy_t>(reply.readInt32());
1115 }
1116 return PRODUCT_STRATEGY_NONE;
1117 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001118};
1119
1120IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
1121
1122// ----------------------------------------------------------------------
1123
Eric Laurentc2f1f072009-07-17 12:17:14 -07001124status_t BnAudioPolicyService::onTransact(
1125 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1126{
Eric Laurentb1cc36b2017-12-11 12:14:16 -08001127 // make sure transactions reserved to AudioFlinger do not come from other processes
1128 switch (code) {
1129 case START_OUTPUT:
1130 case STOP_OUTPUT:
1131 case RELEASE_OUTPUT:
1132 case GET_INPUT_FOR_ATTR:
1133 case START_INPUT:
1134 case STOP_INPUT:
1135 case RELEASE_INPUT:
Eric Laurentb1cc36b2017-12-11 12:14:16 -08001136 case GET_OUTPUT_FOR_EFFECT:
1137 case REGISTER_EFFECT:
1138 case UNREGISTER_EFFECT:
1139 case SET_EFFECT_ENABLED:
1140 case GET_OUTPUT_FOR_ATTR:
1141 case ACQUIRE_SOUNDTRIGGER_SESSION:
1142 case RELEASE_SOUNDTRIGGER_SESSION:
1143 ALOGW("%s: transaction %d received from PID %d",
1144 __func__, code, IPCThreadState::self()->getCallingPid());
Eric Laurentef92bff2018-04-26 10:44:50 -07001145 // return status only for non void methods
1146 switch (code) {
1147 case RELEASE_OUTPUT:
1148 case RELEASE_INPUT:
1149 break;
1150 default:
1151 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
1152 break;
1153 }
1154 return OK;
Eric Laurentb1cc36b2017-12-11 12:14:16 -08001155 default:
1156 break;
1157 }
1158
Eric Laurent4980df22018-01-26 18:04:09 -08001159 // make sure the following transactions come from system components
1160 switch (code) {
1161 case SET_DEVICE_CONNECTION_STATE:
1162 case HANDLE_DEVICE_CONFIG_CHANGE:
1163 case SET_PHONE_STATE:
Eric Laurente17378d2018-05-09 14:43:01 -07001164//FIXME: Allow SET_FORCE_USE calls from system apps until a better use case routing API is available
1165// case SET_FORCE_USE:
Eric Laurent4980df22018-01-26 18:04:09 -08001166 case INIT_STREAM_VOLUME:
1167 case SET_STREAM_VOLUME:
1168 case REGISTER_POLICY_MIXES:
Eric Laurent10b71232018-04-13 18:14:44 -07001169 case SET_MASTER_MONO:
1170 case START_AUDIO_SOURCE:
jiabin81772902018-04-02 17:52:27 -07001171 case STOP_AUDIO_SOURCE:
1172 case GET_SURROUND_FORMATS:
Eric Laurentb78763e2018-10-17 10:08:02 -07001173 case SET_SURROUND_FORMAT_ENABLED:
1174 case SET_ASSISTANT_UID:
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001175 case SET_A11Y_SERVICES_UIDS:
1176 case SET_UID_DEVICE_AFFINITY:
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001177 case REMOVE_UID_DEVICE_AFFINITY:
1178 case GET_OFFLOAD_FORMATS_A2DP: {
Andy Hung4ef19fa2018-05-15 19:35:29 -07001179 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
Eric Laurent4980df22018-01-26 18:04:09 -08001180 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1181 __func__, code, IPCThreadState::self()->getCallingPid(),
1182 IPCThreadState::self()->getCallingUid());
Eric Laurentef92bff2018-04-26 10:44:50 -07001183 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
1184 return OK;
Eric Laurent4980df22018-01-26 18:04:09 -08001185 }
Eric Laurent96c7eed2018-03-26 17:57:01 -07001186 } break;
Eric Laurent4980df22018-01-26 18:04:09 -08001187 default:
1188 break;
1189 }
1190
Eric Laurent39b09b52018-06-29 12:24:40 -07001191 std::string tag("IAudioPolicyService command " + std::to_string(code));
1192 TimeCheck check(tag.c_str());
Eric Laurent3528c932018-02-23 17:17:22 -08001193
Glenn Kastene53b9ea2012-03-12 16:29:55 -07001194 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001195 case SET_DEVICE_CONNECTION_STATE: {
1196 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001197 audio_devices_t device =
1198 static_cast <audio_devices_t>(data.readInt32());
1199 audio_policy_dev_state_t state =
1200 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001201 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -08001202 const char *device_name = data.readCString();
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001203 audio_format_t codecFormat = static_cast <audio_format_t>(data.readInt32());
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001204 if (device_address == nullptr || device_name == nullptr) {
1205 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
1206 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1207 } else {
1208 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
1209 state,
1210 device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001211 device_name,
1212 codecFormat)));
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001213 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001214 return NO_ERROR;
1215 } break;
1216
1217 case GET_DEVICE_CONNECTION_STATE: {
1218 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001219 audio_devices_t device =
1220 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001221 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001222 if (device_address == nullptr) {
1223 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
1224 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
1225 } else {
1226 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
1227 device_address)));
1228 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001229 return NO_ERROR;
1230 } break;
1231
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001232 case HANDLE_DEVICE_CONFIG_CHANGE: {
1233 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1234 audio_devices_t device =
1235 static_cast <audio_devices_t>(data.readInt32());
1236 const char *device_address = data.readCString();
1237 const char *device_name = data.readCString();
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001238 audio_format_t codecFormat =
1239 static_cast <audio_format_t>(data.readInt32());
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001240 if (device_address == nullptr || device_name == nullptr) {
1241 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
1242 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1243 } else {
1244 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
1245 device_address,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -08001246 device_name,
1247 codecFormat)));
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001248 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001249 return NO_ERROR;
1250 } break;
1251
Eric Laurentc2f1f072009-07-17 12:17:14 -07001252 case SET_PHONE_STATE: {
1253 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001254 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
1255 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001256 return NO_ERROR;
1257 } break;
1258
Eric Laurentc2f1f072009-07-17 12:17:14 -07001259 case SET_FORCE_USE: {
1260 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001261 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1262 data.readInt32());
Dima Zavinfce7a472011-04-19 22:30:36 -07001263 audio_policy_forced_cfg_t config =
1264 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001265 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
1266 return NO_ERROR;
1267 } break;
1268
1269 case GET_FORCE_USE: {
1270 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001271 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1272 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001273 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
1274 return NO_ERROR;
1275 } break;
1276
1277 case GET_OUTPUT: {
1278 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001279 audio_stream_type_t stream =
1280 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +00001281 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001282 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001283 return NO_ERROR;
1284 } break;
1285
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001286 case GET_OUTPUT_FOR_ATTR: {
1287 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001288 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -08001289 bool hasAttributes = data.readInt32() != 0;
1290 if (hasAttributes) {
1291 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001292 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -08001293 }
1294 audio_session_t session = (audio_session_t)data.readInt32();
1295 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
1296 bool hasStream = data.readInt32() != 0;
1297 if (hasStream) {
1298 stream = (audio_stream_type_t)data.readInt32();
1299 }
Nadav Bar766fb022018-01-07 12:18:03 +02001300 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001301 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001302 audio_config_t config;
1303 memset(&config, 0, sizeof(audio_config_t));
1304 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001305 audio_output_flags_t flags =
1306 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001307 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001308 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001309 audio_io_handle_t output = 0;
Kevin Rocard153f92d2018-12-18 18:33:28 -08001310 std::vector<audio_io_handle_t> secondaryOutputs;
Eric Laurente83b55d2014-11-14 10:06:21 -08001311 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001312 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001313 &config,
Kevin Rocard153f92d2018-12-18 18:33:28 -08001314 flags, &selectedDeviceId, &portId, &secondaryOutputs);
Eric Laurente83b55d2014-11-14 10:06:21 -08001315 reply->writeInt32(status);
1316 reply->writeInt32(output);
1317 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001318 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001319 reply->writeInt32(portId);
Kevin Rocard153f92d2018-12-18 18:33:28 -08001320 reply->writeInt32(secondaryOutputs.size());
1321 return reply->write(secondaryOutputs.data(),
1322 secondaryOutputs.size() * sizeof(audio_io_handle_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001323 } break;
1324
Eric Laurentc2f1f072009-07-17 12:17:14 -07001325 case START_OUTPUT: {
1326 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001327 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1328 reply->writeInt32(static_cast <uint32_t>(startOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001329 return NO_ERROR;
1330 } break;
1331
1332 case STOP_OUTPUT: {
1333 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001334 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1335 reply->writeInt32(static_cast <uint32_t>(stopOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001336 return NO_ERROR;
1337 } break;
1338
1339 case RELEASE_OUTPUT: {
1340 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001341 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1342 releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001343 return NO_ERROR;
1344 } break;
1345
Eric Laurentcaf7f482014-11-25 17:50:47 -08001346 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001347 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001348 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001349 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001350 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001351 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001352 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001353 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001354 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001355 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001356 audio_config_base_t config;
1357 memset(&config, 0, sizeof(audio_config_base_t));
1358 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001359 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001360 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001361 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001362 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001363 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001364 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001365 reply->writeInt32(status);
1366 if (status == NO_ERROR) {
1367 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001368 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001369 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001370 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001371 return NO_ERROR;
1372 } break;
1373
1374 case START_INPUT: {
1375 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001376 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
Eric Laurent4eb58f12018-12-07 16:41:02 -08001377 status_t status = startInput(portId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001378 reply->writeInt32(static_cast <uint32_t>(status));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001379 return NO_ERROR;
1380 } break;
1381
1382 case STOP_INPUT: {
1383 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001384 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1385 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001386 return NO_ERROR;
1387 } break;
1388
1389 case RELEASE_INPUT: {
1390 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001391 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1392 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001393 return NO_ERROR;
1394 } break;
1395
1396 case INIT_STREAM_VOLUME: {
1397 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001398 audio_stream_type_t stream =
1399 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001400 int indexMin = data.readInt32();
1401 int indexMax = data.readInt32();
1402 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1403 return NO_ERROR;
1404 } break;
1405
1406 case SET_STREAM_VOLUME: {
1407 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001408 audio_stream_type_t stream =
1409 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001410 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001411 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1412 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1413 index,
1414 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001415 return NO_ERROR;
1416 } break;
1417
1418 case GET_STREAM_VOLUME: {
1419 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001420 audio_stream_type_t stream =
1421 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurent83844cc2011-11-18 16:43:31 -08001422 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001423 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001424 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001425 reply->writeInt32(index);
1426 reply->writeInt32(static_cast <uint32_t>(status));
1427 return NO_ERROR;
1428 } break;
1429
Eric Laurentde070132010-07-13 04:45:46 -07001430 case GET_STRATEGY_FOR_STREAM: {
1431 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001432 audio_stream_type_t stream =
1433 static_cast <audio_stream_type_t>(data.readInt32());
François Gaffiec005e562018-11-06 15:04:49 +01001434 reply->writeUint32(getStrategyForStream(stream));
Eric Laurentde070132010-07-13 04:45:46 -07001435 return NO_ERROR;
1436 } break;
1437
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001438 case GET_DEVICES_FOR_STREAM: {
1439 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001440 audio_stream_type_t stream =
1441 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001442 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1443 return NO_ERROR;
1444 } break;
1445
Eric Laurentde070132010-07-13 04:45:46 -07001446 case GET_OUTPUT_FOR_EFFECT: {
1447 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001448 effect_descriptor_t desc = {};
1449 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1450 android_errorWriteLog(0x534e4554, "73126106");
1451 }
1452 (void)sanitizeEffectDescriptor(&desc);
Eric Laurentde070132010-07-13 04:45:46 -07001453 audio_io_handle_t output = getOutputForEffect(&desc);
1454 reply->writeInt32(static_cast <int>(output));
1455 return NO_ERROR;
1456 } break;
1457
1458 case REGISTER_EFFECT: {
1459 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001460 effect_descriptor_t desc = {};
1461 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1462 android_errorWriteLog(0x534e4554, "73126106");
1463 }
1464 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001465 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001466 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001467 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001468 int id = data.readInt32();
1469 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001470 io,
Eric Laurentde070132010-07-13 04:45:46 -07001471 strategy,
1472 session,
1473 id)));
1474 return NO_ERROR;
1475 } break;
1476
1477 case UNREGISTER_EFFECT: {
1478 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1479 int id = data.readInt32();
1480 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1481 return NO_ERROR;
1482 } break;
1483
Eric Laurentdb7c0792011-08-10 10:37:50 -07001484 case SET_EFFECT_ENABLED: {
1485 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1486 int id = data.readInt32();
1487 bool enabled = static_cast <bool>(data.readInt32());
1488 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1489 return NO_ERROR;
1490 } break;
1491
Eric Laurenteda6c362011-02-02 09:33:30 -08001492 case IS_STREAM_ACTIVE: {
1493 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001494 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001495 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001496 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001497 return NO_ERROR;
1498 } break;
1499
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001500 case IS_STREAM_ACTIVE_REMOTELY: {
1501 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1502 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1503 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001504 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001505 return NO_ERROR;
1506 } break;
1507
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001508 case IS_SOURCE_ACTIVE: {
1509 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1510 audio_source_t source = (audio_source_t) data.readInt32();
1511 reply->writeInt32( isSourceActive(source));
1512 return NO_ERROR;
1513 }
1514
Eric Laurent57dae992011-07-24 13:36:09 -07001515 case QUERY_DEFAULT_PRE_PROCESSING: {
1516 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001517 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001518 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001519 if (count > AudioEffect::kMaxPreProcessing) {
1520 count = AudioEffect::kMaxPreProcessing;
1521 }
Eric Laurent57dae992011-07-24 13:36:09 -07001522 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001523 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001524 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1525 reply->writeInt32(status);
1526 if (status != NO_ERROR && status != NO_MEMORY) {
1527 retCount = 0;
1528 }
1529 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001530 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001531 if (retCount < count) {
1532 count = retCount;
1533 }
1534 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1535 }
1536 delete[] descriptors;
1537 return status;
1538 }
1539
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001540 case IS_OFFLOAD_SUPPORTED: {
1541 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001542 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001543 data.read(&info, sizeof(audio_offload_info_t));
1544 bool isSupported = isOffloadSupported(info);
1545 reply->writeInt32(isSupported);
1546 return NO_ERROR;
1547 }
1548
Michael Chana94fbb22018-04-24 14:31:19 +10001549 case IS_DIRECT_OUTPUT_SUPPORTED: {
1550 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1551 audio_config_base_t config = {};
1552 audio_attributes_t attributes = {};
1553 status_t status = data.read(&config, sizeof(audio_config_base_t));
1554 if (status != NO_ERROR) return status;
1555 status = data.read(&attributes, sizeof(audio_attributes_t));
1556 if (status != NO_ERROR) return status;
1557 reply->writeInt32(isDirectOutputSupported(config, attributes));
1558 return NO_ERROR;
1559 }
1560
Eric Laurent203b1a12014-04-01 10:34:16 -07001561 case LIST_AUDIO_PORTS: {
1562 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1563 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1564 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1565 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001566 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1567 numPortsReq = MAX_ITEMS_PER_LIST;
1568 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001569 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001570 struct audio_port *ports =
1571 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001572 if (ports == NULL) {
1573 reply->writeInt32(NO_MEMORY);
1574 reply->writeInt32(0);
1575 return NO_ERROR;
1576 }
1577 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001578 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1579 reply->writeInt32(status);
1580 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001581
1582 if (status == NO_ERROR) {
1583 if (numPortsReq > numPorts) {
1584 numPortsReq = numPorts;
1585 }
1586 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1587 reply->writeInt32(generation);
1588 }
1589 free(ports);
1590 return NO_ERROR;
1591 }
1592
1593 case GET_AUDIO_PORT: {
1594 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001595 struct audio_port port = {};
1596 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1597 ALOGE("b/23912202");
1598 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001599 status_t status = getAudioPort(&port);
1600 reply->writeInt32(status);
1601 if (status == NO_ERROR) {
1602 reply->write(&port, sizeof(struct audio_port));
1603 }
1604 return NO_ERROR;
1605 }
1606
1607 case CREATE_AUDIO_PATCH: {
1608 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001609 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001610 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001611 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001612 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1613 ALOGE("b/23912202");
1614 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001615 status_t status = createAudioPatch(&patch, &handle);
1616 reply->writeInt32(status);
1617 if (status == NO_ERROR) {
1618 reply->write(&handle, sizeof(audio_patch_handle_t));
1619 }
1620 return NO_ERROR;
1621 }
1622
1623 case RELEASE_AUDIO_PATCH: {
1624 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001625 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001626 data.read(&handle, sizeof(audio_patch_handle_t));
1627 status_t status = releaseAudioPatch(handle);
1628 reply->writeInt32(status);
1629 return NO_ERROR;
1630 }
1631
1632 case LIST_AUDIO_PATCHES: {
1633 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1634 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001635 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1636 numPatchesReq = MAX_ITEMS_PER_LIST;
1637 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001638 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001639 struct audio_patch *patches =
1640 (struct audio_patch *)calloc(numPatchesReq,
1641 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001642 if (patches == NULL) {
1643 reply->writeInt32(NO_MEMORY);
1644 reply->writeInt32(0);
1645 return NO_ERROR;
1646 }
1647 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001648 status_t status = listAudioPatches(&numPatches, patches, &generation);
1649 reply->writeInt32(status);
1650 reply->writeInt32(numPatches);
1651 if (status == NO_ERROR) {
1652 if (numPatchesReq > numPatches) {
1653 numPatchesReq = numPatches;
1654 }
1655 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1656 reply->writeInt32(generation);
1657 }
1658 free(patches);
1659 return NO_ERROR;
1660 }
1661
1662 case SET_AUDIO_PORT_CONFIG: {
1663 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001664 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001665 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001666 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001667 status_t status = setAudioPortConfig(&config);
1668 reply->writeInt32(status);
1669 return NO_ERROR;
1670 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001671
Eric Laurentb52c1522014-05-20 11:27:36 -07001672 case REGISTER_CLIENT: {
1673 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1674 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1675 data.readStrongBinder());
1676 registerClient(client);
1677 return NO_ERROR;
1678 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001679
Eric Laurente8726fe2015-06-26 09:39:24 -07001680 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1681 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1682 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1683 return NO_ERROR;
1684 } break;
1685
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001686 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1687 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1688 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1689 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001690 audio_session_t session = AUDIO_SESSION_NONE;
1691 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1692 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001693 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1694 reply->writeInt32(status);
1695 if (status == NO_ERROR) {
1696 reply->writeInt32(session);
1697 reply->writeInt32(ioHandle);
1698 reply->writeInt32(device);
1699 }
1700 return NO_ERROR;
1701 } break;
1702
1703 case RELEASE_SOUNDTRIGGER_SESSION: {
1704 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1705 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1706 data.readStrongBinder());
1707 audio_session_t session = (audio_session_t)data.readInt32();
1708 status_t status = releaseSoundTriggerSession(session);
1709 reply->writeInt32(status);
1710 return NO_ERROR;
1711 } break;
1712
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001713 case GET_PHONE_STATE: {
1714 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1715 reply->writeInt32((int32_t)getPhoneState());
1716 return NO_ERROR;
1717 } break;
1718
Eric Laurentbaac1832014-12-01 17:52:59 -08001719 case REGISTER_POLICY_MIXES: {
1720 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1721 bool registration = data.readInt32() == 1;
1722 Vector<AudioMix> mixes;
1723 size_t size = (size_t)data.readInt32();
1724 if (size > MAX_MIXES_PER_POLICY) {
1725 size = MAX_MIXES_PER_POLICY;
1726 }
1727 for (size_t i = 0; i < size; i++) {
1728 AudioMix mix;
1729 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1730 mixes.add(mix);
1731 }
1732 }
1733 status_t status = registerPolicyMixes(mixes, registration);
1734 reply->writeInt32(status);
1735 return NO_ERROR;
1736 } break;
1737
Eric Laurent554a2772015-04-10 11:29:24 -07001738 case START_AUDIO_SOURCE: {
1739 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001740 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001741 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001742 (void)sanitizeAudioPortConfig(&source);
1743 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001744 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001745 sanetizeAudioAttributes(&attributes);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001746 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
1747 status_t status = startAudioSource(&source, &attributes, &portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001748 reply->writeInt32(status);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001749 reply->writeInt32(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001750 return NO_ERROR;
1751 } break;
1752
1753 case STOP_AUDIO_SOURCE: {
1754 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001755 audio_port_handle_t portId = (audio_port_handle_t) data.readInt32();
1756 status_t status = stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001757 reply->writeInt32(status);
1758 return NO_ERROR;
1759 } break;
1760
Andy Hung2ddee192015-12-18 17:34:44 -08001761 case SET_MASTER_MONO: {
1762 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1763 bool mono = static_cast<bool>(data.readInt32());
1764 status_t status = setMasterMono(mono);
1765 reply->writeInt32(status);
1766 return NO_ERROR;
1767 } break;
1768
1769 case GET_MASTER_MONO: {
1770 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1771 bool mono;
1772 status_t status = getMasterMono(&mono);
1773 reply->writeInt32(status);
1774 if (status == NO_ERROR) {
1775 reply->writeInt32(static_cast<int32_t>(mono));
1776 }
1777 return NO_ERROR;
1778 } break;
1779
Eric Laurentac9cef52017-06-09 15:46:26 -07001780 case GET_STREAM_VOLUME_DB: {
1781 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1782 audio_stream_type_t stream =
1783 static_cast <audio_stream_type_t>(data.readInt32());
1784 int index = static_cast <int>(data.readInt32());
1785 audio_devices_t device =
1786 static_cast <audio_devices_t>(data.readUint32());
1787 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1788 return NO_ERROR;
1789 }
1790
jiabin81772902018-04-02 17:52:27 -07001791 case GET_SURROUND_FORMATS: {
1792 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1793 unsigned int numSurroundFormatsReq = data.readUint32();
1794 if (numSurroundFormatsReq > MAX_ITEMS_PER_LIST) {
1795 numSurroundFormatsReq = MAX_ITEMS_PER_LIST;
1796 }
1797 bool reported = data.readBool();
1798 unsigned int numSurroundFormats = numSurroundFormatsReq;
1799 audio_format_t *surroundFormats = (audio_format_t *)calloc(
1800 numSurroundFormats, sizeof(audio_format_t));
1801 bool *surroundFormatsEnabled = (bool *)calloc(numSurroundFormats, sizeof(bool));
1802 if (numSurroundFormatsReq > 0 &&
1803 (surroundFormats == NULL || surroundFormatsEnabled == NULL)) {
1804 free(surroundFormats);
1805 free(surroundFormatsEnabled);
1806 reply->writeInt32(NO_MEMORY);
1807 return NO_ERROR;
1808 }
1809 status_t status = getSurroundFormats(
1810 &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1811 reply->writeInt32(status);
1812
1813 if (status == NO_ERROR) {
1814 reply->writeUint32(numSurroundFormats);
1815 if (numSurroundFormatsReq > numSurroundFormats) {
1816 numSurroundFormatsReq = numSurroundFormats;
1817 }
1818 reply->write(surroundFormats, numSurroundFormatsReq * sizeof(audio_format_t));
1819 reply->write(surroundFormatsEnabled, numSurroundFormatsReq * sizeof(bool));
1820 }
1821 free(surroundFormats);
1822 free(surroundFormatsEnabled);
1823 return NO_ERROR;
1824 }
1825
1826 case SET_SURROUND_FORMAT_ENABLED: {
1827 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1828 audio_format_t audioFormat = (audio_format_t) data.readInt32();
1829 bool enabled = data.readBool();
1830 status_t status = setSurroundFormatEnabled(audioFormat, enabled);
1831 reply->writeInt32(status);
1832 return NO_ERROR;
1833 }
1834
Arun Mirpuri11029ad2018-12-19 20:45:19 -08001835 case GET_OFFLOAD_FORMATS_A2DP: {
1836 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1837 std::vector<audio_format_t> encodingFormats;
1838 status_t status = getHwOffloadEncodingFormatsSupportedForA2DP(&encodingFormats);
1839 reply->writeInt32(status);
1840 if (status != NO_ERROR) {
1841 return NO_ERROR;
1842 }
1843 reply->writeUint32(static_cast<uint32_t>(encodingFormats.size()));
1844 for (size_t i = 0; i < encodingFormats.size(); i++)
1845 reply->writeInt32(static_cast<int32_t>(encodingFormats[i]));
1846 return NO_ERROR;
1847 }
1848
1849
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -07001850 case ADD_STREAM_DEFAULT_EFFECT: {
1851 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1852 effect_uuid_t type;
1853 status_t status = data.read(&type, sizeof(effect_uuid_t));
1854 if (status != NO_ERROR) {
1855 return status;
1856 }
1857 String16 opPackageName;
1858 status = data.readString16(&opPackageName);
1859 if (status != NO_ERROR) {
1860 return status;
1861 }
1862 effect_uuid_t uuid;
1863 status = data.read(&uuid, sizeof(effect_uuid_t));
1864 if (status != NO_ERROR) {
1865 return status;
1866 }
1867 int32_t priority = data.readInt32();
1868 audio_usage_t usage = (audio_usage_t) data.readInt32();
1869 audio_unique_id_t id = 0;
1870 reply->writeInt32(static_cast <int32_t>(addStreamDefaultEffect(&type,
1871 opPackageName,
1872 &uuid,
1873 priority,
1874 usage,
1875 &id)));
1876 reply->writeInt32(id);
1877 return NO_ERROR;
1878 }
1879
1880 case REMOVE_STREAM_DEFAULT_EFFECT: {
1881 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1882 audio_unique_id_t id = static_cast<audio_unique_id_t>(data.readInt32());
1883 reply->writeInt32(static_cast <int32_t>(removeStreamDefaultEffect(id)));
1884 return NO_ERROR;
1885 }
1886
Ari Hausman-Cohen24628312018-08-13 15:01:09 -07001887 case ADD_SOURCE_DEFAULT_EFFECT: {
1888 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1889 effect_uuid_t type;
1890 status_t status = data.read(&type, sizeof(effect_uuid_t));
1891 if (status != NO_ERROR) {
1892 return status;
1893 }
1894 String16 opPackageName;
1895 status = data.readString16(&opPackageName);
1896 if (status != NO_ERROR) {
1897 return status;
1898 }
1899 effect_uuid_t uuid;
1900 status = data.read(&uuid, sizeof(effect_uuid_t));
1901 if (status != NO_ERROR) {
1902 return status;
1903 }
1904 int32_t priority = data.readInt32();
1905 audio_source_t source = (audio_source_t) data.readInt32();
1906 audio_unique_id_t id = 0;
1907 reply->writeInt32(static_cast <int32_t>(addSourceDefaultEffect(&type,
1908 opPackageName,
1909 &uuid,
1910 priority,
1911 source,
1912 &id)));
1913 reply->writeInt32(id);
1914 return NO_ERROR;
1915 }
1916
1917 case REMOVE_SOURCE_DEFAULT_EFFECT: {
1918 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1919 audio_unique_id_t id = static_cast<audio_unique_id_t>(data.readInt32());
1920 reply->writeInt32(static_cast <int32_t>(removeSourceDefaultEffect(id)));
1921 return NO_ERROR;
1922 }
1923
Eric Laurentb78763e2018-10-17 10:08:02 -07001924 case SET_ASSISTANT_UID: {
1925 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1926 int32_t uid;
1927 status_t status = data.readInt32(&uid);
1928 if (status != NO_ERROR) {
1929 return status;
1930 }
1931 status = setAssistantUid(uid);
1932 reply->writeInt32(static_cast <int32_t>(status));
1933 return NO_ERROR;
1934 }
1935
1936 case SET_A11Y_SERVICES_UIDS: {
1937 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1938 std::vector<uid_t> uids;
1939 int32_t size;
1940 status_t status = data.readInt32(&size);
1941 if (status != NO_ERROR) {
1942 return status;
1943 }
1944 if (size > MAX_ITEMS_PER_LIST) {
1945 size = MAX_ITEMS_PER_LIST;
1946 }
1947 for (int32_t i = 0; i < size; i++) {
1948 int32_t uid;
1949 status = data.readInt32(&uid);
1950 if (status != NO_ERROR) {
1951 return status;
1952 }
1953 uids.push_back(uid);
1954 }
1955 status = setA11yServicesUids(uids);
1956 reply->writeInt32(static_cast <int32_t>(status));
1957 return NO_ERROR;
1958 }
1959
jiabin6012f912018-11-02 17:06:30 -07001960 case IS_HAPTIC_PLAYBACK_SUPPORTED: {
1961 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1962 bool isSupported = isHapticPlaybackSupported();
1963 reply->writeBool(isSupported);
Jean-Michel Trivibda70da2018-12-19 07:30:15 -08001964 return NO_ERROR;
1965 }
1966
1967 case SET_UID_DEVICE_AFFINITY: {
1968 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1969 const uid_t uid = (uid_t) data.readInt32();
1970 Vector<AudioDeviceTypeAddr> devices;
1971 size_t size = (size_t)data.readInt32();
1972 for (size_t i = 0; i < size; i++) {
1973 AudioDeviceTypeAddr device;
1974 if (device.readFromParcel((Parcel*)&data) == NO_ERROR) {
1975 devices.add(device);
1976 }
1977 }
1978 status_t status = setUidDeviceAffinities(uid, devices);
1979 reply->writeInt32(status);
1980 return NO_ERROR;
1981 }
1982
1983 case REMOVE_UID_DEVICE_AFFINITY: {
1984 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1985 const uid_t uid = (uid_t) data.readInt32();
1986 status_t status = removeUidDeviceAffinities(uid);
1987 reply->writeInt32(status);
jiabin6012f912018-11-02 17:06:30 -07001988 return NO_ERROR;
1989 }
1990
François Gaffied0ba9ed2018-11-05 11:50:42 +01001991 case LIST_AUDIO_PRODUCT_STRATEGIES: {
1992 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1993 AudioProductStrategyVector strategies;
1994 status_t status = listAudioProductStrategies(strategies);
1995 reply->writeInt32(status);
1996 if (status != NO_ERROR) {
1997 return status;
1998 }
1999 size_t size = strategies.size();
2000 size_t sizePosition = reply->dataPosition();
2001 reply->writeInt32(size);
2002 size_t finalSize = size;
2003 for (size_t i = 0; i < size; i++) {
2004 size_t position = reply->dataPosition();
2005 if (strategies[i].writeToParcel(reply) != NO_ERROR) {
2006 reply->setDataPosition(position);
2007 finalSize--;
2008 }
2009 }
2010 if (size != finalSize) {
2011 size_t position = reply->dataPosition();
2012 reply->setDataPosition(sizePosition);
2013 reply->writeInt32(finalSize);
2014 reply->setDataPosition(position);
2015 }
2016 return NO_ERROR;
2017 }
2018
2019 case GET_STRATEGY_FOR_ATTRIBUTES: {
2020 CHECK_INTERFACE(IAudioPolicyService, data, reply);
2021 AudioAttributes attributes;
2022 status_t status = attributes.readFromParcel(&data);
2023 if (status != NO_ERROR) {
2024 return status;
2025 }
2026 product_strategy_t strategy = getProductStrategyFromAudioAttributes(attributes);
2027 reply->writeUint32(static_cast<int>(strategy));
2028 return NO_ERROR;
2029 }
2030
Eric Laurentc2f1f072009-07-17 12:17:14 -07002031 default:
2032 return BBinder::onTransact(code, data, reply, flags);
2033 }
2034}
2035
Andy Hungb0272092018-04-12 11:06:56 -07002036/** returns true if string overflow was prevented by zero termination */
2037template <size_t size>
2038static bool preventStringOverflow(char (&s)[size]) {
2039 if (strnlen(s, size) < size) return false;
2040 s[size - 1] = '\0';
2041 return true;
2042}
2043
Kevin Rocard39fdbd02017-11-13 11:15:27 -08002044void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
2045{
2046 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
2047 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
2048 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
2049 }
2050 attr->tags[tagsMaxSize - 1] = '\0';
2051}
2052
Andy Hungb0272092018-04-12 11:06:56 -07002053/** returns BAD_VALUE if sanitization was required. */
2054status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
2055{
2056 if (preventStringOverflow(desc->name)
2057 | /* always */ preventStringOverflow(desc->implementor)) {
2058 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
2059 return BAD_VALUE;
2060 }
2061 return NO_ERROR;
2062}
2063
2064/** returns BAD_VALUE if sanitization was required. */
2065status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
2066{
2067 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
2068 preventStringOverflow(config->ext.device.address)) {
2069 return BAD_VALUE;
2070 }
2071 return NO_ERROR;
2072}
2073
Eric Laurentc2f1f072009-07-17 12:17:14 -07002074// ----------------------------------------------------------------------------
2075
Glenn Kasten40bc9062015-03-20 09:09:33 -07002076} // namespace android