blob: 73a8b747b4485ca057ead451af86d898915faf31 [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,
84 SET_SURROUND_FORMAT_ENABLED
Eric Laurentc2f1f072009-07-17 12:17:14 -070085};
86
Eric Laurent1d670b12015-02-06 10:44:24 -080087#define MAX_ITEMS_PER_LIST 1024
88
Eric Laurentc2f1f072009-07-17 12:17:14 -070089class BpAudioPolicyService : public BpInterface<IAudioPolicyService>
90{
91public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070092 explicit BpAudioPolicyService(const sp<IBinder>& impl)
Eric Laurentc2f1f072009-07-17 12:17:14 -070093 : BpInterface<IAudioPolicyService>(impl)
94 {
95 }
96
97 virtual status_t setDeviceConnectionState(
Dima Zavinfce7a472011-04-19 22:30:36 -070098 audio_devices_t device,
99 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800100 const char *device_address,
101 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700102 {
103 Parcel data, reply;
104 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
105 data.writeInt32(static_cast <uint32_t>(device));
106 data.writeInt32(static_cast <uint32_t>(state));
107 data.writeCString(device_address);
Paul McLeane743a472015-01-28 11:07:31 -0800108 data.writeCString(device_name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700109 remote()->transact(SET_DEVICE_CONNECTION_STATE, data, &reply);
110 return static_cast <status_t> (reply.readInt32());
111 }
112
Dima Zavinfce7a472011-04-19 22:30:36 -0700113 virtual audio_policy_dev_state_t getDeviceConnectionState(
114 audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700115 const char *device_address)
116 {
117 Parcel data, reply;
118 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
119 data.writeInt32(static_cast <uint32_t>(device));
120 data.writeCString(device_address);
121 remote()->transact(GET_DEVICE_CONNECTION_STATE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700122 return static_cast <audio_policy_dev_state_t>(reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700123 }
124
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800125 virtual status_t handleDeviceConfigChange(audio_devices_t device,
126 const char *device_address,
127 const char *device_name)
128 {
129 Parcel data, reply;
130 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
131 data.writeInt32(static_cast <uint32_t>(device));
132 data.writeCString(device_address);
133 data.writeCString(device_name);
134 remote()->transact(HANDLE_DEVICE_CONFIG_CHANGE, data, &reply);
135 return static_cast <status_t> (reply.readInt32());
136 }
137
Glenn Kastenf78aee72012-01-04 11:00:47 -0800138 virtual status_t setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700139 {
140 Parcel data, reply;
141 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
142 data.writeInt32(state);
143 remote()->transact(SET_PHONE_STATE, data, &reply);
144 return static_cast <status_t> (reply.readInt32());
145 }
146
Dima Zavinfce7a472011-04-19 22:30:36 -0700147 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700148 {
149 Parcel data, reply;
150 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
151 data.writeInt32(static_cast <uint32_t>(usage));
152 data.writeInt32(static_cast <uint32_t>(config));
153 remote()->transact(SET_FORCE_USE, data, &reply);
154 return static_cast <status_t> (reply.readInt32());
155 }
156
Dima Zavinfce7a472011-04-19 22:30:36 -0700157 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700158 {
159 Parcel data, reply;
160 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
161 data.writeInt32(static_cast <uint32_t>(usage));
162 remote()->transact(GET_FORCE_USE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700163 return static_cast <audio_policy_forced_cfg_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700164 }
165
Eric Laurentf4e63452017-11-06 19:31:46 +0000166 virtual audio_io_handle_t getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700167 {
168 Parcel data, reply;
169 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
170 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700171 remote()->transact(GET_OUTPUT, data, &reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -0700172 return static_cast <audio_io_handle_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700173 }
174
Eric Laurente83b55d2014-11-14 10:06:21 -0800175 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
176 audio_io_handle_t *output,
177 audio_session_t session,
178 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200179 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700180 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800181 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800182 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700183 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800184 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700185 {
186 Parcel data, reply;
187 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
188 if (attr == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800189 if (stream == NULL) {
190 ALOGE("getOutputForAttr(): NULL audio attributes and stream type");
191 return BAD_VALUE;
192 }
193 if (*stream == AUDIO_STREAM_DEFAULT) {
194 ALOGE("getOutputForAttr unspecified stream type");
195 return BAD_VALUE;
196 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700197 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800198 if (output == NULL) {
199 ALOGE("getOutputForAttr NULL output - shouldn't happen");
200 return BAD_VALUE;
201 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700202 if (selectedDeviceId == NULL) {
203 ALOGE("getOutputForAttr NULL selectedDeviceId - shouldn't happen");
204 return BAD_VALUE;
205 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800206 if (portId == NULL) {
207 ALOGE("getOutputForAttr NULL portId - shouldn't happen");
208 return BAD_VALUE;
209 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800210 if (attr == NULL) {
211 data.writeInt32(0);
212 } else {
213 data.writeInt32(1);
214 data.write(attr, sizeof(audio_attributes_t));
215 }
216 data.writeInt32(session);
217 if (stream == NULL) {
218 data.writeInt32(0);
219 } else {
220 data.writeInt32(1);
221 data.writeInt32(*stream);
222 }
Nadav Bar766fb022018-01-07 12:18:03 +0200223 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700224 data.writeInt32(uid);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800225 data.write(config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700226 data.writeInt32(static_cast <uint32_t>(flags));
Eric Laurent9ae8c592017-06-22 17:17:09 -0700227 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800228 data.writeInt32(*portId);
Eric Laurente83b55d2014-11-14 10:06:21 -0800229 status_t status = remote()->transact(GET_OUTPUT_FOR_ATTR, data, &reply);
230 if (status != NO_ERROR) {
231 return status;
232 }
233 status = (status_t)reply.readInt32();
234 if (status != NO_ERROR) {
235 return status;
236 }
237 *output = (audio_io_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800238 audio_stream_type_t lStream = (audio_stream_type_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800239 if (stream != NULL) {
Eric Laurent20b9ef02016-12-05 11:03:16 -0800240 *stream = lStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800241 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700242 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800243 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800244 return status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700245 }
246
Eric Laurentd7fe0862018-07-14 16:48:01 -0700247 virtual status_t startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700248 {
249 Parcel data, reply;
250 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700251 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700252 remote()->transact(START_OUTPUT, data, &reply);
253 return static_cast <status_t> (reply.readInt32());
254 }
255
Eric Laurentd7fe0862018-07-14 16:48:01 -0700256 virtual status_t stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700257 {
258 Parcel data, reply;
259 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700260 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700261 remote()->transact(STOP_OUTPUT, data, &reply);
262 return static_cast <status_t> (reply.readInt32());
263 }
264
Eric Laurentd7fe0862018-07-14 16:48:01 -0700265 virtual void releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700266 {
267 Parcel data, reply;
268 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700269 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700270 remote()->transact(RELEASE_OUTPUT, data, &reply);
271 }
272
Eric Laurentcaf7f482014-11-25 17:50:47 -0800273 virtual status_t getInputForAttr(const audio_attributes_t *attr,
274 audio_io_handle_t *input,
275 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700276 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700277 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800278 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800279 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600280 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700281 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800282 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700283 {
284 Parcel data, reply;
285 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentcaf7f482014-11-25 17:50:47 -0800286 if (attr == NULL) {
287 ALOGE("getInputForAttr NULL attr - shouldn't happen");
288 return BAD_VALUE;
289 }
290 if (input == NULL) {
291 ALOGE("getInputForAttr NULL input - shouldn't happen");
292 return BAD_VALUE;
293 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700294 if (selectedDeviceId == NULL) {
295 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
296 return BAD_VALUE;
297 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800298 if (portId == NULL) {
299 ALOGE("getInputForAttr NULL portId - shouldn't happen");
300 return BAD_VALUE;
301 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800302 data.write(attr, sizeof(audio_attributes_t));
Eric Laurenta54f1282017-07-01 19:39:32 -0700303 data.writeInt32(*input);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800304 data.writeInt32(session);
Eric Laurentb2379ba2016-05-23 17:42:12 -0700305 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700306 data.writeInt32(uid);
Eric Laurentfee19762018-01-29 18:44:13 -0800307 data.writeString16(opPackageName);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800308 data.write(config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -0700309 data.writeInt32(flags);
Eric Laurent9ae8c592017-06-22 17:17:09 -0700310 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800311 data.writeInt32(*portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800312 status_t status = remote()->transact(GET_INPUT_FOR_ATTR, data, &reply);
313 if (status != NO_ERROR) {
314 return status;
315 }
316 status = reply.readInt32();
317 if (status != NO_ERROR) {
318 return status;
319 }
320 *input = (audio_io_handle_t)reply.readInt32();
Eric Laurent9ae8c592017-06-22 17:17:09 -0700321 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800322 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800323 return NO_ERROR;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700324 }
325
Eric Laurentfee19762018-01-29 18:44:13 -0800326 virtual status_t startInput(audio_port_handle_t portId,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800327 bool *silenced)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700328 {
329 Parcel data, reply;
330 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800331 data.writeInt32(portId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800332 data.writeInt32(*silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700333 remote()->transact(START_INPUT, data, &reply);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800334 status_t status = static_cast <status_t> (reply.readInt32());
335 *silenced = reply.readInt32() == 1;
336 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700337 }
338
Eric Laurentfee19762018-01-29 18:44:13 -0800339 virtual status_t stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700340 {
341 Parcel data, reply;
342 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800343 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700344 remote()->transact(STOP_INPUT, data, &reply);
345 return static_cast <status_t> (reply.readInt32());
346 }
347
Eric Laurentfee19762018-01-29 18:44:13 -0800348 virtual void releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700349 {
350 Parcel data, reply;
351 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800352 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700353 remote()->transact(RELEASE_INPUT, data, &reply);
354 }
355
Dima Zavinfce7a472011-04-19 22:30:36 -0700356 virtual status_t initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700357 int indexMin,
358 int indexMax)
359 {
360 Parcel data, reply;
361 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
362 data.writeInt32(static_cast <uint32_t>(stream));
363 data.writeInt32(indexMin);
364 data.writeInt32(indexMax);
365 remote()->transact(INIT_STREAM_VOLUME, data, &reply);
366 return static_cast <status_t> (reply.readInt32());
367 }
368
Eric Laurent83844cc2011-11-18 16:43:31 -0800369 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
370 int index,
371 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700372 {
373 Parcel data, reply;
374 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
375 data.writeInt32(static_cast <uint32_t>(stream));
376 data.writeInt32(index);
Eric Laurent83844cc2011-11-18 16:43:31 -0800377 data.writeInt32(static_cast <uint32_t>(device));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700378 remote()->transact(SET_STREAM_VOLUME, data, &reply);
379 return static_cast <status_t> (reply.readInt32());
380 }
381
Eric Laurent83844cc2011-11-18 16:43:31 -0800382 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
383 int *index,
384 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700385 {
386 Parcel data, reply;
387 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
388 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurent83844cc2011-11-18 16:43:31 -0800389 data.writeInt32(static_cast <uint32_t>(device));
390
Eric Laurentc2f1f072009-07-17 12:17:14 -0700391 remote()->transact(GET_STREAM_VOLUME, data, &reply);
392 int lIndex = reply.readInt32();
393 if (index) *index = lIndex;
394 return static_cast <status_t> (reply.readInt32());
395 }
Eric Laurentde070132010-07-13 04:45:46 -0700396
Dima Zavinfce7a472011-04-19 22:30:36 -0700397 virtual uint32_t getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700398 {
399 Parcel data, reply;
400 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
401 data.writeInt32(static_cast <uint32_t>(stream));
402 remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply);
403 return reply.readInt32();
404 }
405
Eric Laurent63742522012-03-08 13:42:42 -0800406 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800407 {
408 Parcel data, reply;
409 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
410 data.writeInt32(static_cast <uint32_t>(stream));
411 remote()->transact(GET_DEVICES_FOR_STREAM, data, &reply);
Eric Laurent63742522012-03-08 13:42:42 -0800412 return (audio_devices_t) reply.readInt32();
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800413 }
414
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700415 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700416 {
417 Parcel data, reply;
418 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
419 data.write(desc, sizeof(effect_descriptor_t));
420 remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply);
421 return static_cast <audio_io_handle_t> (reply.readInt32());
422 }
423
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700424 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700425 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700426 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800427 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700428 int id)
429 {
430 Parcel data, reply;
431 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
432 data.write(desc, sizeof(effect_descriptor_t));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700433 data.writeInt32(io);
Eric Laurentde070132010-07-13 04:45:46 -0700434 data.writeInt32(strategy);
435 data.writeInt32(session);
436 data.writeInt32(id);
437 remote()->transact(REGISTER_EFFECT, data, &reply);
438 return static_cast <status_t> (reply.readInt32());
439 }
440
441 virtual status_t unregisterEffect(int id)
442 {
443 Parcel data, reply;
444 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
445 data.writeInt32(id);
446 remote()->transact(UNREGISTER_EFFECT, data, &reply);
447 return static_cast <status_t> (reply.readInt32());
448 }
449
Eric Laurentdb7c0792011-08-10 10:37:50 -0700450 virtual status_t setEffectEnabled(int id, bool enabled)
451 {
452 Parcel data, reply;
453 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
454 data.writeInt32(id);
455 data.writeInt32(enabled);
456 remote()->transact(SET_EFFECT_ENABLED, data, &reply);
457 return static_cast <status_t> (reply.readInt32());
458 }
459
Glenn Kastenfff6d712012-01-12 16:38:12 -0800460 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800461 {
462 Parcel data, reply;
463 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Glenn Kastenfff6d712012-01-12 16:38:12 -0800464 data.writeInt32((int32_t) stream);
Eric Laurenteda6c362011-02-02 09:33:30 -0800465 data.writeInt32(inPastMs);
466 remote()->transact(IS_STREAM_ACTIVE, data, &reply);
467 return reply.readInt32();
468 }
Eric Laurent57dae992011-07-24 13:36:09 -0700469
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800470 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
471 {
472 Parcel data, reply;
473 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
474 data.writeInt32((int32_t) stream);
475 data.writeInt32(inPastMs);
476 remote()->transact(IS_STREAM_ACTIVE_REMOTELY, data, &reply);
477 return reply.readInt32();
478 }
479
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700480 virtual bool isSourceActive(audio_source_t source) const
481 {
482 Parcel data, reply;
483 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
484 data.writeInt32((int32_t) source);
485 remote()->transact(IS_SOURCE_ACTIVE, data, &reply);
486 return reply.readInt32();
487 }
488
Glenn Kastend848eb42016-03-08 13:42:11 -0800489 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700490 effect_descriptor_t *descriptors,
491 uint32_t *count)
492 {
493 if (descriptors == NULL || count == NULL) {
494 return BAD_VALUE;
495 }
496 Parcel data, reply;
497 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
498 data.writeInt32(audioSession);
499 data.writeInt32(*count);
500 status_t status = remote()->transact(QUERY_DEFAULT_PRE_PROCESSING, data, &reply);
501 if (status != NO_ERROR) {
502 return status;
503 }
504 status = static_cast <status_t> (reply.readInt32());
505 uint32_t retCount = reply.readInt32();
506 if (retCount != 0) {
507 uint32_t numDesc = (retCount < *count) ? retCount : *count;
508 reply.read(descriptors, sizeof(effect_descriptor_t) * numDesc);
509 }
510 *count = retCount;
511 return status;
512 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000513
514 virtual bool isOffloadSupported(const audio_offload_info_t& info)
515 {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100516 Parcel data, reply;
517 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
518 data.write(&info, sizeof(audio_offload_info_t));
519 remote()->transact(IS_OFFLOAD_SUPPORTED, data, &reply);
Eric Laurent203b1a12014-04-01 10:34:16 -0700520 return reply.readInt32();
521 }
522
523 virtual status_t listAudioPorts(audio_port_role_t role,
524 audio_port_type_t type,
525 unsigned int *num_ports,
526 struct audio_port *ports,
527 unsigned int *generation)
528 {
529 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
530 generation == NULL) {
531 return BAD_VALUE;
532 }
533 Parcel data, reply;
534 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
535 unsigned int numPortsReq = (ports == NULL) ? 0 : *num_ports;
536 data.writeInt32(role);
537 data.writeInt32(type);
538 data.writeInt32(numPortsReq);
539 status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply);
540 if (status == NO_ERROR) {
541 status = (status_t)reply.readInt32();
542 *num_ports = (unsigned int)reply.readInt32();
543 }
Eric Laurent203b1a12014-04-01 10:34:16 -0700544 if (status == NO_ERROR) {
545 if (numPortsReq > *num_ports) {
546 numPortsReq = *num_ports;
547 }
548 if (numPortsReq > 0) {
549 reply.read(ports, numPortsReq * sizeof(struct audio_port));
550 }
551 *generation = reply.readInt32();
552 }
553 return status;
554 }
555
556 virtual status_t getAudioPort(struct audio_port *port)
557 {
558 if (port == NULL) {
559 return BAD_VALUE;
560 }
561 Parcel data, reply;
562 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
563 data.write(port, sizeof(struct audio_port));
564 status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply);
565 if (status != NO_ERROR ||
566 (status = (status_t)reply.readInt32()) != NO_ERROR) {
567 return status;
568 }
569 reply.read(port, sizeof(struct audio_port));
570 return status;
571 }
572
573 virtual status_t createAudioPatch(const struct audio_patch *patch,
574 audio_patch_handle_t *handle)
575 {
576 if (patch == NULL || handle == NULL) {
577 return BAD_VALUE;
578 }
579 Parcel data, reply;
580 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
581 data.write(patch, sizeof(struct audio_patch));
582 data.write(handle, sizeof(audio_patch_handle_t));
583 status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply);
584 if (status != NO_ERROR ||
585 (status = (status_t)reply.readInt32()) != NO_ERROR) {
586 return status;
587 }
588 reply.read(handle, sizeof(audio_patch_handle_t));
589 return status;
590 }
591
592 virtual status_t releaseAudioPatch(audio_patch_handle_t handle)
593 {
594 Parcel data, reply;
595 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
596 data.write(&handle, sizeof(audio_patch_handle_t));
597 status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply);
598 if (status != NO_ERROR) {
599 status = (status_t)reply.readInt32();
600 }
601 return status;
602 }
603
604 virtual status_t listAudioPatches(unsigned int *num_patches,
605 struct audio_patch *patches,
606 unsigned int *generation)
607 {
608 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
609 generation == NULL) {
610 return BAD_VALUE;
611 }
612 Parcel data, reply;
613 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
614 unsigned int numPatchesReq = (patches == NULL) ? 0 : *num_patches;
615 data.writeInt32(numPatchesReq);
616 status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply);
617 if (status == NO_ERROR) {
618 status = (status_t)reply.readInt32();
619 *num_patches = (unsigned int)reply.readInt32();
620 }
621 if (status == NO_ERROR) {
622 if (numPatchesReq > *num_patches) {
623 numPatchesReq = *num_patches;
624 }
625 if (numPatchesReq > 0) {
626 reply.read(patches, numPatchesReq * sizeof(struct audio_patch));
627 }
628 *generation = reply.readInt32();
629 }
630 return status;
631 }
632
633 virtual status_t setAudioPortConfig(const struct audio_port_config *config)
634 {
635 if (config == NULL) {
636 return BAD_VALUE;
637 }
638 Parcel data, reply;
639 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
640 data.write(config, sizeof(struct audio_port_config));
641 status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply);
642 if (status != NO_ERROR) {
643 status = (status_t)reply.readInt32();
644 }
645 return status;
646 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700647
Eric Laurentb52c1522014-05-20 11:27:36 -0700648 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client)
649 {
650 Parcel data, reply;
651 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -0800652 data.writeStrongBinder(IInterface::asBinder(client));
Eric Laurentb52c1522014-05-20 11:27:36 -0700653 remote()->transact(REGISTER_CLIENT, data, &reply);
654 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700655
Eric Laurente8726fe2015-06-26 09:39:24 -0700656 virtual void setAudioPortCallbacksEnabled(bool enabled)
657 {
658 Parcel data, reply;
659 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
660 data.writeInt32(enabled ? 1 : 0);
661 remote()->transact(SET_AUDIO_PORT_CALLBACK_ENABLED, data, &reply);
662 }
663
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700664 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
665 audio_io_handle_t *ioHandle,
666 audio_devices_t *device)
667 {
668 if (session == NULL || ioHandle == NULL || device == NULL) {
669 return BAD_VALUE;
670 }
671 Parcel data, reply;
672 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
673 status_t status = remote()->transact(ACQUIRE_SOUNDTRIGGER_SESSION, data, &reply);
674 if (status != NO_ERROR) {
675 return status;
676 }
677 status = (status_t)reply.readInt32();
678 if (status == NO_ERROR) {
679 *session = (audio_session_t)reply.readInt32();
680 *ioHandle = (audio_io_handle_t)reply.readInt32();
681 *device = (audio_devices_t)reply.readInt32();
682 }
683 return status;
684 }
685
686 virtual status_t releaseSoundTriggerSession(audio_session_t session)
687 {
688 Parcel data, reply;
689 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
690 data.writeInt32(session);
691 status_t status = remote()->transact(RELEASE_SOUNDTRIGGER_SESSION, data, &reply);
692 if (status != NO_ERROR) {
693 return status;
694 }
695 return (status_t)reply.readInt32();
696 }
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700697
698 virtual audio_mode_t getPhoneState()
699 {
700 Parcel data, reply;
701 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
702 status_t status = remote()->transact(GET_PHONE_STATE, data, &reply);
703 if (status != NO_ERROR) {
704 return AUDIO_MODE_INVALID;
705 }
706 return (audio_mode_t)reply.readInt32();
707 }
Eric Laurentbaac1832014-12-01 17:52:59 -0800708
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700709 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800710 {
711 Parcel data, reply;
712 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
713 data.writeInt32(registration ? 1 : 0);
714 size_t size = mixes.size();
715 if (size > MAX_MIXES_PER_POLICY) {
716 size = MAX_MIXES_PER_POLICY;
717 }
718 size_t sizePosition = data.dataPosition();
719 data.writeInt32(size);
720 size_t finalSize = size;
721 for (size_t i = 0; i < size; i++) {
722 size_t position = data.dataPosition();
723 if (mixes[i].writeToParcel(&data) != NO_ERROR) {
724 data.setDataPosition(position);
725 finalSize--;
726 }
727 }
728 if (size != finalSize) {
729 size_t position = data.dataPosition();
730 data.setDataPosition(sizePosition);
731 data.writeInt32(finalSize);
732 data.setDataPosition(position);
733 }
734 status_t status = remote()->transact(REGISTER_POLICY_MIXES, data, &reply);
735 if (status == NO_ERROR) {
736 status = (status_t)reply.readInt32();
737 }
738 return status;
739 }
Eric Laurent554a2772015-04-10 11:29:24 -0700740
741 virtual status_t startAudioSource(const struct audio_port_config *source,
742 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700743 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -0700744 {
745 Parcel data, reply;
746 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700747 if (source == NULL || attributes == NULL || portId == NULL) {
Eric Laurent554a2772015-04-10 11:29:24 -0700748 return BAD_VALUE;
749 }
750 data.write(source, sizeof(struct audio_port_config));
751 data.write(attributes, sizeof(audio_attributes_t));
752 status_t status = remote()->transact(START_AUDIO_SOURCE, data, &reply);
753 if (status != NO_ERROR) {
754 return status;
755 }
756 status = (status_t)reply.readInt32();
757 if (status != NO_ERROR) {
758 return status;
759 }
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700760 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -0700761 return status;
762 }
763
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700764 virtual status_t stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -0700765 {
766 Parcel data, reply;
767 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700768 data.writeInt32(portId);
Eric Laurent554a2772015-04-10 11:29:24 -0700769 status_t status = remote()->transact(STOP_AUDIO_SOURCE, data, &reply);
770 if (status != NO_ERROR) {
771 return status;
772 }
773 status = (status_t)reply.readInt32();
774 return status;
775 }
Andy Hung2ddee192015-12-18 17:34:44 -0800776
777 virtual status_t setMasterMono(bool mono)
778 {
779 Parcel data, reply;
780 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
781 data.writeInt32(static_cast<int32_t>(mono));
782 status_t status = remote()->transact(SET_MASTER_MONO, data, &reply);
783 if (status != NO_ERROR) {
784 return status;
785 }
786 return static_cast<status_t>(reply.readInt32());
787 }
788
789 virtual status_t getMasterMono(bool *mono)
790 {
791 if (mono == nullptr) {
792 return BAD_VALUE;
793 }
794 Parcel data, reply;
795 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
796
797 status_t status = remote()->transact(GET_MASTER_MONO, data, &reply);
798 if (status != NO_ERROR) {
799 return status;
800 }
801 status = static_cast<status_t>(reply.readInt32());
802 if (status == NO_ERROR) {
803 *mono = static_cast<bool>(reply.readInt32());
804 }
805 return status;
806 }
Eric Laurentac9cef52017-06-09 15:46:26 -0700807
808 virtual float getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
809 {
810 Parcel data, reply;
811 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
812 data.writeInt32(static_cast <int32_t>(stream));
813 data.writeInt32(static_cast <int32_t>(index));
814 data.writeUint32(static_cast <uint32_t>(device));
815 status_t status = remote()->transact(GET_STREAM_VOLUME_DB, data, &reply);
816 if (status != NO_ERROR) {
817 return NAN;
818 }
819 return reply.readFloat();
820 }
jiabin81772902018-04-02 17:52:27 -0700821
822 virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
823 audio_format_t *surroundFormats,
824 bool *surroundFormatsEnabled,
825 bool reported)
826 {
827 if (numSurroundFormats == NULL || (*numSurroundFormats != 0 &&
828 (surroundFormats == NULL || surroundFormatsEnabled == NULL))) {
829 return BAD_VALUE;
830 }
831 Parcel data, reply;
832 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
833 unsigned int numSurroundFormatsReq = *numSurroundFormats;
834 data.writeUint32(numSurroundFormatsReq);
835 data.writeBool(reported);
836 status_t status = remote()->transact(GET_SURROUND_FORMATS, data, &reply);
837 if (status == NO_ERROR && (status = (status_t)reply.readInt32()) == NO_ERROR) {
838 *numSurroundFormats = reply.readUint32();
839 }
840 if (status == NO_ERROR) {
841 if (numSurroundFormatsReq > *numSurroundFormats) {
842 numSurroundFormatsReq = *numSurroundFormats;
843 }
844 if (numSurroundFormatsReq > 0) {
845 status = reply.read(surroundFormats,
846 numSurroundFormatsReq * sizeof(audio_format_t));
847 if (status != NO_ERROR) {
848 return status;
849 }
850 status = reply.read(surroundFormatsEnabled,
851 numSurroundFormatsReq * sizeof(bool));
852 }
853 }
854 return status;
855 }
856
857 virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
858 {
859 Parcel data, reply;
860 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
861 data.writeInt32(audioFormat);
862 data.writeBool(enabled);
863 status_t status = remote()->transact(SET_SURROUND_FORMAT_ENABLED, data, &reply);
864 if (status != NO_ERROR) {
865 return status;
866 }
867 return reply.readInt32();
868 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700869};
870
871IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
872
873// ----------------------------------------------------------------------
874
Eric Laurentc2f1f072009-07-17 12:17:14 -0700875status_t BnAudioPolicyService::onTransact(
876 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
877{
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800878 // make sure transactions reserved to AudioFlinger do not come from other processes
879 switch (code) {
880 case START_OUTPUT:
881 case STOP_OUTPUT:
882 case RELEASE_OUTPUT:
883 case GET_INPUT_FOR_ATTR:
884 case START_INPUT:
885 case STOP_INPUT:
886 case RELEASE_INPUT:
887 case GET_STRATEGY_FOR_STREAM:
888 case GET_OUTPUT_FOR_EFFECT:
889 case REGISTER_EFFECT:
890 case UNREGISTER_EFFECT:
891 case SET_EFFECT_ENABLED:
892 case GET_OUTPUT_FOR_ATTR:
893 case ACQUIRE_SOUNDTRIGGER_SESSION:
894 case RELEASE_SOUNDTRIGGER_SESSION:
895 ALOGW("%s: transaction %d received from PID %d",
896 __func__, code, IPCThreadState::self()->getCallingPid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700897 // return status only for non void methods
898 switch (code) {
899 case RELEASE_OUTPUT:
900 case RELEASE_INPUT:
901 break;
902 default:
903 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
904 break;
905 }
906 return OK;
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800907 default:
908 break;
909 }
910
Eric Laurent4980df22018-01-26 18:04:09 -0800911 // make sure the following transactions come from system components
912 switch (code) {
913 case SET_DEVICE_CONNECTION_STATE:
914 case HANDLE_DEVICE_CONFIG_CHANGE:
915 case SET_PHONE_STATE:
Eric Laurente17378d2018-05-09 14:43:01 -0700916//FIXME: Allow SET_FORCE_USE calls from system apps until a better use case routing API is available
917// case SET_FORCE_USE:
Eric Laurent4980df22018-01-26 18:04:09 -0800918 case INIT_STREAM_VOLUME:
919 case SET_STREAM_VOLUME:
920 case REGISTER_POLICY_MIXES:
Eric Laurent10b71232018-04-13 18:14:44 -0700921 case SET_MASTER_MONO:
922 case START_AUDIO_SOURCE:
jiabin81772902018-04-02 17:52:27 -0700923 case STOP_AUDIO_SOURCE:
924 case GET_SURROUND_FORMATS:
925 case SET_SURROUND_FORMAT_ENABLED: {
Andy Hung4ef19fa2018-05-15 19:35:29 -0700926 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
Eric Laurent4980df22018-01-26 18:04:09 -0800927 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
928 __func__, code, IPCThreadState::self()->getCallingPid(),
929 IPCThreadState::self()->getCallingUid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700930 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
931 return OK;
Eric Laurent4980df22018-01-26 18:04:09 -0800932 }
Eric Laurent96c7eed2018-03-26 17:57:01 -0700933 } break;
Eric Laurent4980df22018-01-26 18:04:09 -0800934 default:
935 break;
936 }
937
Eric Laurent39b09b52018-06-29 12:24:40 -0700938 std::string tag("IAudioPolicyService command " + std::to_string(code));
939 TimeCheck check(tag.c_str());
Eric Laurent3528c932018-02-23 17:17:22 -0800940
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700941 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700942 case SET_DEVICE_CONNECTION_STATE: {
943 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700944 audio_devices_t device =
945 static_cast <audio_devices_t>(data.readInt32());
946 audio_policy_dev_state_t state =
947 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700948 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -0800949 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800950 if (device_address == nullptr || device_name == nullptr) {
951 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
952 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
953 } else {
954 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
955 state,
956 device_address,
957 device_name)));
958 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700959 return NO_ERROR;
960 } break;
961
962 case GET_DEVICE_CONNECTION_STATE: {
963 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700964 audio_devices_t device =
965 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700966 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800967 if (device_address == nullptr) {
968 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
969 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
970 } else {
971 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
972 device_address)));
973 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700974 return NO_ERROR;
975 } break;
976
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800977 case HANDLE_DEVICE_CONFIG_CHANGE: {
978 CHECK_INTERFACE(IAudioPolicyService, data, reply);
979 audio_devices_t device =
980 static_cast <audio_devices_t>(data.readInt32());
981 const char *device_address = data.readCString();
982 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800983 if (device_address == nullptr || device_name == nullptr) {
984 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
985 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
986 } else {
987 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
988 device_address,
989 device_name)));
990 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800991 return NO_ERROR;
992 } break;
993
Eric Laurentc2f1f072009-07-17 12:17:14 -0700994 case SET_PHONE_STATE: {
995 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700996 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
997 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700998 return NO_ERROR;
999 } break;
1000
Eric Laurentc2f1f072009-07-17 12:17:14 -07001001 case SET_FORCE_USE: {
1002 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001003 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1004 data.readInt32());
Dima Zavinfce7a472011-04-19 22:30:36 -07001005 audio_policy_forced_cfg_t config =
1006 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001007 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
1008 return NO_ERROR;
1009 } break;
1010
1011 case GET_FORCE_USE: {
1012 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001013 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1014 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001015 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
1016 return NO_ERROR;
1017 } break;
1018
1019 case GET_OUTPUT: {
1020 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001021 audio_stream_type_t stream =
1022 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +00001023 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001024 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001025 return NO_ERROR;
1026 } break;
1027
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001028 case GET_OUTPUT_FOR_ATTR: {
1029 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001030 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -08001031 bool hasAttributes = data.readInt32() != 0;
1032 if (hasAttributes) {
1033 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001034 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -08001035 }
1036 audio_session_t session = (audio_session_t)data.readInt32();
1037 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
1038 bool hasStream = data.readInt32() != 0;
1039 if (hasStream) {
1040 stream = (audio_stream_type_t)data.readInt32();
1041 }
Nadav Bar766fb022018-01-07 12:18:03 +02001042 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001043 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001044 audio_config_t config;
1045 memset(&config, 0, sizeof(audio_config_t));
1046 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001047 audio_output_flags_t flags =
1048 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001049 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001050 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001051 audio_io_handle_t output = 0;
Eric Laurente83b55d2014-11-14 10:06:21 -08001052 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001053 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001054 &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001055 flags, &selectedDeviceId, &portId);
Eric Laurente83b55d2014-11-14 10:06:21 -08001056 reply->writeInt32(status);
1057 reply->writeInt32(output);
1058 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001059 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001060 reply->writeInt32(portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001061 return NO_ERROR;
1062 } break;
1063
Eric Laurentc2f1f072009-07-17 12:17:14 -07001064 case START_OUTPUT: {
1065 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001066 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1067 reply->writeInt32(static_cast <uint32_t>(startOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001068 return NO_ERROR;
1069 } break;
1070
1071 case STOP_OUTPUT: {
1072 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001073 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1074 reply->writeInt32(static_cast <uint32_t>(stopOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001075 return NO_ERROR;
1076 } break;
1077
1078 case RELEASE_OUTPUT: {
1079 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001080 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1081 releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001082 return NO_ERROR;
1083 } break;
1084
Eric Laurentcaf7f482014-11-25 17:50:47 -08001085 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001086 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001087 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001088 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001089 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001090 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001091 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001092 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001093 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001094 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001095 audio_config_base_t config;
1096 memset(&config, 0, sizeof(audio_config_base_t));
1097 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001098 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001099 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001100 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001101 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001102 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001103 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001104 reply->writeInt32(status);
1105 if (status == NO_ERROR) {
1106 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001107 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001108 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001109 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001110 return NO_ERROR;
1111 } break;
1112
1113 case START_INPUT: {
1114 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001115 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001116 bool silenced = data.readInt32() == 1;
Eric Laurentfee19762018-01-29 18:44:13 -08001117 status_t status = startInput(portId, &silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001118 reply->writeInt32(static_cast <uint32_t>(status));
1119 reply->writeInt32(silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001120 return NO_ERROR;
1121 } break;
1122
1123 case STOP_INPUT: {
1124 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001125 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1126 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001127 return NO_ERROR;
1128 } break;
1129
1130 case RELEASE_INPUT: {
1131 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001132 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1133 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001134 return NO_ERROR;
1135 } break;
1136
1137 case INIT_STREAM_VOLUME: {
1138 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001139 audio_stream_type_t stream =
1140 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001141 int indexMin = data.readInt32();
1142 int indexMax = data.readInt32();
1143 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1144 return NO_ERROR;
1145 } break;
1146
1147 case SET_STREAM_VOLUME: {
1148 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001149 audio_stream_type_t stream =
1150 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001151 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001152 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1153 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1154 index,
1155 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001156 return NO_ERROR;
1157 } break;
1158
1159 case GET_STREAM_VOLUME: {
1160 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001161 audio_stream_type_t stream =
1162 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurent83844cc2011-11-18 16:43:31 -08001163 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001164 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001165 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001166 reply->writeInt32(index);
1167 reply->writeInt32(static_cast <uint32_t>(status));
1168 return NO_ERROR;
1169 } break;
1170
Eric Laurentde070132010-07-13 04:45:46 -07001171 case GET_STRATEGY_FOR_STREAM: {
1172 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001173 audio_stream_type_t stream =
1174 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentde070132010-07-13 04:45:46 -07001175 reply->writeInt32(getStrategyForStream(stream));
1176 return NO_ERROR;
1177 } break;
1178
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001179 case GET_DEVICES_FOR_STREAM: {
1180 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001181 audio_stream_type_t stream =
1182 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001183 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1184 return NO_ERROR;
1185 } break;
1186
Eric Laurentde070132010-07-13 04:45:46 -07001187 case GET_OUTPUT_FOR_EFFECT: {
1188 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001189 effect_descriptor_t desc = {};
1190 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1191 android_errorWriteLog(0x534e4554, "73126106");
1192 }
1193 (void)sanitizeEffectDescriptor(&desc);
Eric Laurentde070132010-07-13 04:45:46 -07001194 audio_io_handle_t output = getOutputForEffect(&desc);
1195 reply->writeInt32(static_cast <int>(output));
1196 return NO_ERROR;
1197 } break;
1198
1199 case REGISTER_EFFECT: {
1200 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001201 effect_descriptor_t desc = {};
1202 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1203 android_errorWriteLog(0x534e4554, "73126106");
1204 }
1205 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001206 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001207 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001208 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001209 int id = data.readInt32();
1210 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001211 io,
Eric Laurentde070132010-07-13 04:45:46 -07001212 strategy,
1213 session,
1214 id)));
1215 return NO_ERROR;
1216 } break;
1217
1218 case UNREGISTER_EFFECT: {
1219 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1220 int id = data.readInt32();
1221 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1222 return NO_ERROR;
1223 } break;
1224
Eric Laurentdb7c0792011-08-10 10:37:50 -07001225 case SET_EFFECT_ENABLED: {
1226 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1227 int id = data.readInt32();
1228 bool enabled = static_cast <bool>(data.readInt32());
1229 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1230 return NO_ERROR;
1231 } break;
1232
Eric Laurenteda6c362011-02-02 09:33:30 -08001233 case IS_STREAM_ACTIVE: {
1234 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001235 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001236 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001237 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001238 return NO_ERROR;
1239 } break;
1240
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001241 case IS_STREAM_ACTIVE_REMOTELY: {
1242 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1243 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1244 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001245 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001246 return NO_ERROR;
1247 } break;
1248
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001249 case IS_SOURCE_ACTIVE: {
1250 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1251 audio_source_t source = (audio_source_t) data.readInt32();
1252 reply->writeInt32( isSourceActive(source));
1253 return NO_ERROR;
1254 }
1255
Eric Laurent57dae992011-07-24 13:36:09 -07001256 case QUERY_DEFAULT_PRE_PROCESSING: {
1257 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001258 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001259 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001260 if (count > AudioEffect::kMaxPreProcessing) {
1261 count = AudioEffect::kMaxPreProcessing;
1262 }
Eric Laurent57dae992011-07-24 13:36:09 -07001263 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001264 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001265 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1266 reply->writeInt32(status);
1267 if (status != NO_ERROR && status != NO_MEMORY) {
1268 retCount = 0;
1269 }
1270 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001271 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001272 if (retCount < count) {
1273 count = retCount;
1274 }
1275 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1276 }
1277 delete[] descriptors;
1278 return status;
1279 }
1280
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001281 case IS_OFFLOAD_SUPPORTED: {
1282 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001283 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001284 data.read(&info, sizeof(audio_offload_info_t));
1285 bool isSupported = isOffloadSupported(info);
1286 reply->writeInt32(isSupported);
1287 return NO_ERROR;
1288 }
1289
Eric Laurent203b1a12014-04-01 10:34:16 -07001290 case LIST_AUDIO_PORTS: {
1291 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1292 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1293 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1294 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001295 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1296 numPortsReq = MAX_ITEMS_PER_LIST;
1297 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001298 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001299 struct audio_port *ports =
1300 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001301 if (ports == NULL) {
1302 reply->writeInt32(NO_MEMORY);
1303 reply->writeInt32(0);
1304 return NO_ERROR;
1305 }
1306 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001307 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1308 reply->writeInt32(status);
1309 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001310
1311 if (status == NO_ERROR) {
1312 if (numPortsReq > numPorts) {
1313 numPortsReq = numPorts;
1314 }
1315 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1316 reply->writeInt32(generation);
1317 }
1318 free(ports);
1319 return NO_ERROR;
1320 }
1321
1322 case GET_AUDIO_PORT: {
1323 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001324 struct audio_port port = {};
1325 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1326 ALOGE("b/23912202");
1327 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001328 status_t status = getAudioPort(&port);
1329 reply->writeInt32(status);
1330 if (status == NO_ERROR) {
1331 reply->write(&port, sizeof(struct audio_port));
1332 }
1333 return NO_ERROR;
1334 }
1335
1336 case CREATE_AUDIO_PATCH: {
1337 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001338 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001339 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001340 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001341 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1342 ALOGE("b/23912202");
1343 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001344 status_t status = createAudioPatch(&patch, &handle);
1345 reply->writeInt32(status);
1346 if (status == NO_ERROR) {
1347 reply->write(&handle, sizeof(audio_patch_handle_t));
1348 }
1349 return NO_ERROR;
1350 }
1351
1352 case RELEASE_AUDIO_PATCH: {
1353 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001354 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001355 data.read(&handle, sizeof(audio_patch_handle_t));
1356 status_t status = releaseAudioPatch(handle);
1357 reply->writeInt32(status);
1358 return NO_ERROR;
1359 }
1360
1361 case LIST_AUDIO_PATCHES: {
1362 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1363 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001364 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1365 numPatchesReq = MAX_ITEMS_PER_LIST;
1366 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001367 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001368 struct audio_patch *patches =
1369 (struct audio_patch *)calloc(numPatchesReq,
1370 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001371 if (patches == NULL) {
1372 reply->writeInt32(NO_MEMORY);
1373 reply->writeInt32(0);
1374 return NO_ERROR;
1375 }
1376 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001377 status_t status = listAudioPatches(&numPatches, patches, &generation);
1378 reply->writeInt32(status);
1379 reply->writeInt32(numPatches);
1380 if (status == NO_ERROR) {
1381 if (numPatchesReq > numPatches) {
1382 numPatchesReq = numPatches;
1383 }
1384 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1385 reply->writeInt32(generation);
1386 }
1387 free(patches);
1388 return NO_ERROR;
1389 }
1390
1391 case SET_AUDIO_PORT_CONFIG: {
1392 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001393 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001394 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001395 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001396 status_t status = setAudioPortConfig(&config);
1397 reply->writeInt32(status);
1398 return NO_ERROR;
1399 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001400
Eric Laurentb52c1522014-05-20 11:27:36 -07001401 case REGISTER_CLIENT: {
1402 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1403 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1404 data.readStrongBinder());
1405 registerClient(client);
1406 return NO_ERROR;
1407 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001408
Eric Laurente8726fe2015-06-26 09:39:24 -07001409 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1410 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1411 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1412 return NO_ERROR;
1413 } break;
1414
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001415 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1416 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1417 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1418 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001419 audio_session_t session = AUDIO_SESSION_NONE;
1420 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1421 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001422 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1423 reply->writeInt32(status);
1424 if (status == NO_ERROR) {
1425 reply->writeInt32(session);
1426 reply->writeInt32(ioHandle);
1427 reply->writeInt32(device);
1428 }
1429 return NO_ERROR;
1430 } break;
1431
1432 case RELEASE_SOUNDTRIGGER_SESSION: {
1433 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1434 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1435 data.readStrongBinder());
1436 audio_session_t session = (audio_session_t)data.readInt32();
1437 status_t status = releaseSoundTriggerSession(session);
1438 reply->writeInt32(status);
1439 return NO_ERROR;
1440 } break;
1441
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001442 case GET_PHONE_STATE: {
1443 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1444 reply->writeInt32((int32_t)getPhoneState());
1445 return NO_ERROR;
1446 } break;
1447
Eric Laurentbaac1832014-12-01 17:52:59 -08001448 case REGISTER_POLICY_MIXES: {
1449 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1450 bool registration = data.readInt32() == 1;
1451 Vector<AudioMix> mixes;
1452 size_t size = (size_t)data.readInt32();
1453 if (size > MAX_MIXES_PER_POLICY) {
1454 size = MAX_MIXES_PER_POLICY;
1455 }
1456 for (size_t i = 0; i < size; i++) {
1457 AudioMix mix;
1458 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1459 mixes.add(mix);
1460 }
1461 }
1462 status_t status = registerPolicyMixes(mixes, registration);
1463 reply->writeInt32(status);
1464 return NO_ERROR;
1465 } break;
1466
Eric Laurent554a2772015-04-10 11:29:24 -07001467 case START_AUDIO_SOURCE: {
1468 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001469 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001470 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001471 (void)sanitizeAudioPortConfig(&source);
1472 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001473 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001474 sanetizeAudioAttributes(&attributes);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001475 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
1476 status_t status = startAudioSource(&source, &attributes, &portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001477 reply->writeInt32(status);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001478 reply->writeInt32(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001479 return NO_ERROR;
1480 } break;
1481
1482 case STOP_AUDIO_SOURCE: {
1483 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001484 audio_port_handle_t portId = (audio_port_handle_t) data.readInt32();
1485 status_t status = stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001486 reply->writeInt32(status);
1487 return NO_ERROR;
1488 } break;
1489
Andy Hung2ddee192015-12-18 17:34:44 -08001490 case SET_MASTER_MONO: {
1491 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1492 bool mono = static_cast<bool>(data.readInt32());
1493 status_t status = setMasterMono(mono);
1494 reply->writeInt32(status);
1495 return NO_ERROR;
1496 } break;
1497
1498 case GET_MASTER_MONO: {
1499 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1500 bool mono;
1501 status_t status = getMasterMono(&mono);
1502 reply->writeInt32(status);
1503 if (status == NO_ERROR) {
1504 reply->writeInt32(static_cast<int32_t>(mono));
1505 }
1506 return NO_ERROR;
1507 } break;
1508
Eric Laurentac9cef52017-06-09 15:46:26 -07001509 case GET_STREAM_VOLUME_DB: {
1510 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1511 audio_stream_type_t stream =
1512 static_cast <audio_stream_type_t>(data.readInt32());
1513 int index = static_cast <int>(data.readInt32());
1514 audio_devices_t device =
1515 static_cast <audio_devices_t>(data.readUint32());
1516 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1517 return NO_ERROR;
1518 }
1519
jiabin81772902018-04-02 17:52:27 -07001520 case GET_SURROUND_FORMATS: {
1521 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1522 unsigned int numSurroundFormatsReq = data.readUint32();
1523 if (numSurroundFormatsReq > MAX_ITEMS_PER_LIST) {
1524 numSurroundFormatsReq = MAX_ITEMS_PER_LIST;
1525 }
1526 bool reported = data.readBool();
1527 unsigned int numSurroundFormats = numSurroundFormatsReq;
1528 audio_format_t *surroundFormats = (audio_format_t *)calloc(
1529 numSurroundFormats, sizeof(audio_format_t));
1530 bool *surroundFormatsEnabled = (bool *)calloc(numSurroundFormats, sizeof(bool));
1531 if (numSurroundFormatsReq > 0 &&
1532 (surroundFormats == NULL || surroundFormatsEnabled == NULL)) {
1533 free(surroundFormats);
1534 free(surroundFormatsEnabled);
1535 reply->writeInt32(NO_MEMORY);
1536 return NO_ERROR;
1537 }
1538 status_t status = getSurroundFormats(
1539 &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1540 reply->writeInt32(status);
1541
1542 if (status == NO_ERROR) {
1543 reply->writeUint32(numSurroundFormats);
1544 if (numSurroundFormatsReq > numSurroundFormats) {
1545 numSurroundFormatsReq = numSurroundFormats;
1546 }
1547 reply->write(surroundFormats, numSurroundFormatsReq * sizeof(audio_format_t));
1548 reply->write(surroundFormatsEnabled, numSurroundFormatsReq * sizeof(bool));
1549 }
1550 free(surroundFormats);
1551 free(surroundFormatsEnabled);
1552 return NO_ERROR;
1553 }
1554
1555 case SET_SURROUND_FORMAT_ENABLED: {
1556 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1557 audio_format_t audioFormat = (audio_format_t) data.readInt32();
1558 bool enabled = data.readBool();
1559 status_t status = setSurroundFormatEnabled(audioFormat, enabled);
1560 reply->writeInt32(status);
1561 return NO_ERROR;
1562 }
1563
Eric Laurentc2f1f072009-07-17 12:17:14 -07001564 default:
1565 return BBinder::onTransact(code, data, reply, flags);
1566 }
1567}
1568
Andy Hungb0272092018-04-12 11:06:56 -07001569/** returns true if string overflow was prevented by zero termination */
1570template <size_t size>
1571static bool preventStringOverflow(char (&s)[size]) {
1572 if (strnlen(s, size) < size) return false;
1573 s[size - 1] = '\0';
1574 return true;
1575}
1576
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001577void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
1578{
1579 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
1580 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
1581 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
1582 }
1583 attr->tags[tagsMaxSize - 1] = '\0';
1584}
1585
Andy Hungb0272092018-04-12 11:06:56 -07001586/** returns BAD_VALUE if sanitization was required. */
1587status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
1588{
1589 if (preventStringOverflow(desc->name)
1590 | /* always */ preventStringOverflow(desc->implementor)) {
1591 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
1592 return BAD_VALUE;
1593 }
1594 return NO_ERROR;
1595}
1596
1597/** returns BAD_VALUE if sanitization was required. */
1598status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
1599{
1600 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
1601 preventStringOverflow(config->ext.device.address)) {
1602 return BAD_VALUE;
1603 }
1604 return NO_ERROR;
1605}
1606
Eric Laurentc2f1f072009-07-17 12:17:14 -07001607// ----------------------------------------------------------------------------
1608
Glenn Kasten40bc9062015-03-20 09:09:33 -07001609} // namespace android