blob: 9d77376d898de4f8dfb674a946b3391f7d93f0d0 [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,
Glenn Kasten559d4392016-03-29 13:42:57 -0700743 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700744 {
745 Parcel data, reply;
746 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
747 if (source == NULL || attributes == NULL || handle == NULL) {
748 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 }
Glenn Kasten559d4392016-03-29 13:42:57 -0700760 *handle = (audio_patch_handle_t)reply.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -0700761 return status;
762 }
763
Glenn Kasten559d4392016-03-29 13:42:57 -0700764 virtual status_t stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700765 {
766 Parcel data, reply;
767 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
768 data.writeInt32(handle);
769 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 Laurentda6a5a82018-05-02 18:47:34 -0700938 TimeCheck check("IAudioPolicyService");
Eric Laurent3528c932018-02-23 17:17:22 -0800939
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700940 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700941 case SET_DEVICE_CONNECTION_STATE: {
942 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700943 audio_devices_t device =
944 static_cast <audio_devices_t>(data.readInt32());
945 audio_policy_dev_state_t state =
946 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700947 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -0800948 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800949 if (device_address == nullptr || device_name == nullptr) {
950 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
951 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
952 } else {
953 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
954 state,
955 device_address,
956 device_name)));
957 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700958 return NO_ERROR;
959 } break;
960
961 case GET_DEVICE_CONNECTION_STATE: {
962 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700963 audio_devices_t device =
964 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700965 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800966 if (device_address == nullptr) {
967 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
968 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
969 } else {
970 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
971 device_address)));
972 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700973 return NO_ERROR;
974 } break;
975
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800976 case HANDLE_DEVICE_CONFIG_CHANGE: {
977 CHECK_INTERFACE(IAudioPolicyService, data, reply);
978 audio_devices_t device =
979 static_cast <audio_devices_t>(data.readInt32());
980 const char *device_address = data.readCString();
981 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800982 if (device_address == nullptr || device_name == nullptr) {
983 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
984 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
985 } else {
986 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
987 device_address,
988 device_name)));
989 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800990 return NO_ERROR;
991 } break;
992
Eric Laurentc2f1f072009-07-17 12:17:14 -0700993 case SET_PHONE_STATE: {
994 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700995 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
996 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700997 return NO_ERROR;
998 } break;
999
Eric Laurentc2f1f072009-07-17 12:17:14 -07001000 case SET_FORCE_USE: {
1001 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001002 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1003 data.readInt32());
Dima Zavinfce7a472011-04-19 22:30:36 -07001004 audio_policy_forced_cfg_t config =
1005 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001006 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
1007 return NO_ERROR;
1008 } break;
1009
1010 case GET_FORCE_USE: {
1011 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001012 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1013 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001014 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
1015 return NO_ERROR;
1016 } break;
1017
1018 case GET_OUTPUT: {
1019 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001020 audio_stream_type_t stream =
1021 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +00001022 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001023 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001024 return NO_ERROR;
1025 } break;
1026
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001027 case GET_OUTPUT_FOR_ATTR: {
1028 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001029 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -08001030 bool hasAttributes = data.readInt32() != 0;
1031 if (hasAttributes) {
1032 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001033 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -08001034 }
1035 audio_session_t session = (audio_session_t)data.readInt32();
1036 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
1037 bool hasStream = data.readInt32() != 0;
1038 if (hasStream) {
1039 stream = (audio_stream_type_t)data.readInt32();
1040 }
Nadav Bar766fb022018-01-07 12:18:03 +02001041 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001042 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001043 audio_config_t config;
1044 memset(&config, 0, sizeof(audio_config_t));
1045 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001046 audio_output_flags_t flags =
1047 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001048 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001049 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001050 audio_io_handle_t output = 0;
Eric Laurente83b55d2014-11-14 10:06:21 -08001051 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001052 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001053 &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001054 flags, &selectedDeviceId, &portId);
Eric Laurente83b55d2014-11-14 10:06:21 -08001055 reply->writeInt32(status);
1056 reply->writeInt32(output);
1057 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001058 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001059 reply->writeInt32(portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001060 return NO_ERROR;
1061 } break;
1062
Eric Laurentc2f1f072009-07-17 12:17:14 -07001063 case START_OUTPUT: {
1064 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001065 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1066 reply->writeInt32(static_cast <uint32_t>(startOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001067 return NO_ERROR;
1068 } break;
1069
1070 case STOP_OUTPUT: {
1071 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001072 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1073 reply->writeInt32(static_cast <uint32_t>(stopOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001074 return NO_ERROR;
1075 } break;
1076
1077 case RELEASE_OUTPUT: {
1078 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001079 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1080 releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001081 return NO_ERROR;
1082 } break;
1083
Eric Laurentcaf7f482014-11-25 17:50:47 -08001084 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001085 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001086 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001087 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001088 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001089 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001090 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001091 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001092 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001093 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001094 audio_config_base_t config;
1095 memset(&config, 0, sizeof(audio_config_base_t));
1096 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001097 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001098 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001099 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001100 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001101 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001102 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001103 reply->writeInt32(status);
1104 if (status == NO_ERROR) {
1105 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001106 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001107 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001108 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001109 return NO_ERROR;
1110 } break;
1111
1112 case START_INPUT: {
1113 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001114 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001115 bool silenced = data.readInt32() == 1;
Eric Laurentfee19762018-01-29 18:44:13 -08001116 status_t status = startInput(portId, &silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001117 reply->writeInt32(static_cast <uint32_t>(status));
1118 reply->writeInt32(silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001119 return NO_ERROR;
1120 } break;
1121
1122 case STOP_INPUT: {
1123 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001124 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1125 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001126 return NO_ERROR;
1127 } break;
1128
1129 case RELEASE_INPUT: {
1130 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001131 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1132 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001133 return NO_ERROR;
1134 } break;
1135
1136 case INIT_STREAM_VOLUME: {
1137 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001138 audio_stream_type_t stream =
1139 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001140 int indexMin = data.readInt32();
1141 int indexMax = data.readInt32();
1142 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1143 return NO_ERROR;
1144 } break;
1145
1146 case SET_STREAM_VOLUME: {
1147 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001148 audio_stream_type_t stream =
1149 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001150 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001151 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1152 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1153 index,
1154 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001155 return NO_ERROR;
1156 } break;
1157
1158 case GET_STREAM_VOLUME: {
1159 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001160 audio_stream_type_t stream =
1161 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurent83844cc2011-11-18 16:43:31 -08001162 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001163 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001164 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001165 reply->writeInt32(index);
1166 reply->writeInt32(static_cast <uint32_t>(status));
1167 return NO_ERROR;
1168 } break;
1169
Eric Laurentde070132010-07-13 04:45:46 -07001170 case GET_STRATEGY_FOR_STREAM: {
1171 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001172 audio_stream_type_t stream =
1173 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentde070132010-07-13 04:45:46 -07001174 reply->writeInt32(getStrategyForStream(stream));
1175 return NO_ERROR;
1176 } break;
1177
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001178 case GET_DEVICES_FOR_STREAM: {
1179 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001180 audio_stream_type_t stream =
1181 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001182 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1183 return NO_ERROR;
1184 } break;
1185
Eric Laurentde070132010-07-13 04:45:46 -07001186 case GET_OUTPUT_FOR_EFFECT: {
1187 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001188 effect_descriptor_t desc = {};
1189 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1190 android_errorWriteLog(0x534e4554, "73126106");
1191 }
1192 (void)sanitizeEffectDescriptor(&desc);
Eric Laurentde070132010-07-13 04:45:46 -07001193 audio_io_handle_t output = getOutputForEffect(&desc);
1194 reply->writeInt32(static_cast <int>(output));
1195 return NO_ERROR;
1196 } break;
1197
1198 case REGISTER_EFFECT: {
1199 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001200 effect_descriptor_t desc = {};
1201 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1202 android_errorWriteLog(0x534e4554, "73126106");
1203 }
1204 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001205 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001206 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001207 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001208 int id = data.readInt32();
1209 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001210 io,
Eric Laurentde070132010-07-13 04:45:46 -07001211 strategy,
1212 session,
1213 id)));
1214 return NO_ERROR;
1215 } break;
1216
1217 case UNREGISTER_EFFECT: {
1218 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1219 int id = data.readInt32();
1220 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1221 return NO_ERROR;
1222 } break;
1223
Eric Laurentdb7c0792011-08-10 10:37:50 -07001224 case SET_EFFECT_ENABLED: {
1225 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1226 int id = data.readInt32();
1227 bool enabled = static_cast <bool>(data.readInt32());
1228 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1229 return NO_ERROR;
1230 } break;
1231
Eric Laurenteda6c362011-02-02 09:33:30 -08001232 case IS_STREAM_ACTIVE: {
1233 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001234 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001235 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001236 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001237 return NO_ERROR;
1238 } break;
1239
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001240 case IS_STREAM_ACTIVE_REMOTELY: {
1241 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1242 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1243 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001244 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001245 return NO_ERROR;
1246 } break;
1247
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001248 case IS_SOURCE_ACTIVE: {
1249 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1250 audio_source_t source = (audio_source_t) data.readInt32();
1251 reply->writeInt32( isSourceActive(source));
1252 return NO_ERROR;
1253 }
1254
Eric Laurent57dae992011-07-24 13:36:09 -07001255 case QUERY_DEFAULT_PRE_PROCESSING: {
1256 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001257 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001258 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001259 if (count > AudioEffect::kMaxPreProcessing) {
1260 count = AudioEffect::kMaxPreProcessing;
1261 }
Eric Laurent57dae992011-07-24 13:36:09 -07001262 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001263 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001264 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1265 reply->writeInt32(status);
1266 if (status != NO_ERROR && status != NO_MEMORY) {
1267 retCount = 0;
1268 }
1269 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001270 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001271 if (retCount < count) {
1272 count = retCount;
1273 }
1274 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1275 }
1276 delete[] descriptors;
1277 return status;
1278 }
1279
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001280 case IS_OFFLOAD_SUPPORTED: {
1281 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001282 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001283 data.read(&info, sizeof(audio_offload_info_t));
1284 bool isSupported = isOffloadSupported(info);
1285 reply->writeInt32(isSupported);
1286 return NO_ERROR;
1287 }
1288
Eric Laurent203b1a12014-04-01 10:34:16 -07001289 case LIST_AUDIO_PORTS: {
1290 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1291 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1292 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1293 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001294 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1295 numPortsReq = MAX_ITEMS_PER_LIST;
1296 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001297 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001298 struct audio_port *ports =
1299 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001300 if (ports == NULL) {
1301 reply->writeInt32(NO_MEMORY);
1302 reply->writeInt32(0);
1303 return NO_ERROR;
1304 }
1305 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001306 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1307 reply->writeInt32(status);
1308 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001309
1310 if (status == NO_ERROR) {
1311 if (numPortsReq > numPorts) {
1312 numPortsReq = numPorts;
1313 }
1314 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1315 reply->writeInt32(generation);
1316 }
1317 free(ports);
1318 return NO_ERROR;
1319 }
1320
1321 case GET_AUDIO_PORT: {
1322 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001323 struct audio_port port = {};
1324 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1325 ALOGE("b/23912202");
1326 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001327 status_t status = getAudioPort(&port);
1328 reply->writeInt32(status);
1329 if (status == NO_ERROR) {
1330 reply->write(&port, sizeof(struct audio_port));
1331 }
1332 return NO_ERROR;
1333 }
1334
1335 case CREATE_AUDIO_PATCH: {
1336 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001337 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001338 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001339 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001340 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1341 ALOGE("b/23912202");
1342 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001343 status_t status = createAudioPatch(&patch, &handle);
1344 reply->writeInt32(status);
1345 if (status == NO_ERROR) {
1346 reply->write(&handle, sizeof(audio_patch_handle_t));
1347 }
1348 return NO_ERROR;
1349 }
1350
1351 case RELEASE_AUDIO_PATCH: {
1352 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001353 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001354 data.read(&handle, sizeof(audio_patch_handle_t));
1355 status_t status = releaseAudioPatch(handle);
1356 reply->writeInt32(status);
1357 return NO_ERROR;
1358 }
1359
1360 case LIST_AUDIO_PATCHES: {
1361 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1362 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001363 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1364 numPatchesReq = MAX_ITEMS_PER_LIST;
1365 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001366 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001367 struct audio_patch *patches =
1368 (struct audio_patch *)calloc(numPatchesReq,
1369 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001370 if (patches == NULL) {
1371 reply->writeInt32(NO_MEMORY);
1372 reply->writeInt32(0);
1373 return NO_ERROR;
1374 }
1375 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001376 status_t status = listAudioPatches(&numPatches, patches, &generation);
1377 reply->writeInt32(status);
1378 reply->writeInt32(numPatches);
1379 if (status == NO_ERROR) {
1380 if (numPatchesReq > numPatches) {
1381 numPatchesReq = numPatches;
1382 }
1383 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1384 reply->writeInt32(generation);
1385 }
1386 free(patches);
1387 return NO_ERROR;
1388 }
1389
1390 case SET_AUDIO_PORT_CONFIG: {
1391 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001392 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001393 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001394 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001395 status_t status = setAudioPortConfig(&config);
1396 reply->writeInt32(status);
1397 return NO_ERROR;
1398 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001399
Eric Laurentb52c1522014-05-20 11:27:36 -07001400 case REGISTER_CLIENT: {
1401 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1402 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1403 data.readStrongBinder());
1404 registerClient(client);
1405 return NO_ERROR;
1406 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001407
Eric Laurente8726fe2015-06-26 09:39:24 -07001408 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1409 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1410 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1411 return NO_ERROR;
1412 } break;
1413
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001414 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1415 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1416 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1417 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001418 audio_session_t session = AUDIO_SESSION_NONE;
1419 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1420 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001421 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1422 reply->writeInt32(status);
1423 if (status == NO_ERROR) {
1424 reply->writeInt32(session);
1425 reply->writeInt32(ioHandle);
1426 reply->writeInt32(device);
1427 }
1428 return NO_ERROR;
1429 } break;
1430
1431 case RELEASE_SOUNDTRIGGER_SESSION: {
1432 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1433 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1434 data.readStrongBinder());
1435 audio_session_t session = (audio_session_t)data.readInt32();
1436 status_t status = releaseSoundTriggerSession(session);
1437 reply->writeInt32(status);
1438 return NO_ERROR;
1439 } break;
1440
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001441 case GET_PHONE_STATE: {
1442 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1443 reply->writeInt32((int32_t)getPhoneState());
1444 return NO_ERROR;
1445 } break;
1446
Eric Laurentbaac1832014-12-01 17:52:59 -08001447 case REGISTER_POLICY_MIXES: {
1448 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1449 bool registration = data.readInt32() == 1;
1450 Vector<AudioMix> mixes;
1451 size_t size = (size_t)data.readInt32();
1452 if (size > MAX_MIXES_PER_POLICY) {
1453 size = MAX_MIXES_PER_POLICY;
1454 }
1455 for (size_t i = 0; i < size; i++) {
1456 AudioMix mix;
1457 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1458 mixes.add(mix);
1459 }
1460 }
1461 status_t status = registerPolicyMixes(mixes, registration);
1462 reply->writeInt32(status);
1463 return NO_ERROR;
1464 } break;
1465
Eric Laurent554a2772015-04-10 11:29:24 -07001466 case START_AUDIO_SOURCE: {
1467 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001468 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001469 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001470 (void)sanitizeAudioPortConfig(&source);
1471 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001472 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001473 sanetizeAudioAttributes(&attributes);
Glenn Kasten559d4392016-03-29 13:42:57 -07001474 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent554a2772015-04-10 11:29:24 -07001475 status_t status = startAudioSource(&source, &attributes, &handle);
1476 reply->writeInt32(status);
1477 reply->writeInt32(handle);
1478 return NO_ERROR;
1479 } break;
1480
1481 case STOP_AUDIO_SOURCE: {
1482 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten559d4392016-03-29 13:42:57 -07001483 audio_patch_handle_t handle = (audio_patch_handle_t) data.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -07001484 status_t status = stopAudioSource(handle);
1485 reply->writeInt32(status);
1486 return NO_ERROR;
1487 } break;
1488
Andy Hung2ddee192015-12-18 17:34:44 -08001489 case SET_MASTER_MONO: {
1490 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1491 bool mono = static_cast<bool>(data.readInt32());
1492 status_t status = setMasterMono(mono);
1493 reply->writeInt32(status);
1494 return NO_ERROR;
1495 } break;
1496
1497 case GET_MASTER_MONO: {
1498 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1499 bool mono;
1500 status_t status = getMasterMono(&mono);
1501 reply->writeInt32(status);
1502 if (status == NO_ERROR) {
1503 reply->writeInt32(static_cast<int32_t>(mono));
1504 }
1505 return NO_ERROR;
1506 } break;
1507
Eric Laurentac9cef52017-06-09 15:46:26 -07001508 case GET_STREAM_VOLUME_DB: {
1509 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1510 audio_stream_type_t stream =
1511 static_cast <audio_stream_type_t>(data.readInt32());
1512 int index = static_cast <int>(data.readInt32());
1513 audio_devices_t device =
1514 static_cast <audio_devices_t>(data.readUint32());
1515 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1516 return NO_ERROR;
1517 }
1518
jiabin81772902018-04-02 17:52:27 -07001519 case GET_SURROUND_FORMATS: {
1520 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1521 unsigned int numSurroundFormatsReq = data.readUint32();
1522 if (numSurroundFormatsReq > MAX_ITEMS_PER_LIST) {
1523 numSurroundFormatsReq = MAX_ITEMS_PER_LIST;
1524 }
1525 bool reported = data.readBool();
1526 unsigned int numSurroundFormats = numSurroundFormatsReq;
1527 audio_format_t *surroundFormats = (audio_format_t *)calloc(
1528 numSurroundFormats, sizeof(audio_format_t));
1529 bool *surroundFormatsEnabled = (bool *)calloc(numSurroundFormats, sizeof(bool));
1530 if (numSurroundFormatsReq > 0 &&
1531 (surroundFormats == NULL || surroundFormatsEnabled == NULL)) {
1532 free(surroundFormats);
1533 free(surroundFormatsEnabled);
1534 reply->writeInt32(NO_MEMORY);
1535 return NO_ERROR;
1536 }
1537 status_t status = getSurroundFormats(
1538 &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1539 reply->writeInt32(status);
1540
1541 if (status == NO_ERROR) {
1542 reply->writeUint32(numSurroundFormats);
1543 if (numSurroundFormatsReq > numSurroundFormats) {
1544 numSurroundFormatsReq = numSurroundFormats;
1545 }
1546 reply->write(surroundFormats, numSurroundFormatsReq * sizeof(audio_format_t));
1547 reply->write(surroundFormatsEnabled, numSurroundFormatsReq * sizeof(bool));
1548 }
1549 free(surroundFormats);
1550 free(surroundFormatsEnabled);
1551 return NO_ERROR;
1552 }
1553
1554 case SET_SURROUND_FORMAT_ENABLED: {
1555 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1556 audio_format_t audioFormat = (audio_format_t) data.readInt32();
1557 bool enabled = data.readBool();
1558 status_t status = setSurroundFormatEnabled(audioFormat, enabled);
1559 reply->writeInt32(status);
1560 return NO_ERROR;
1561 }
1562
Eric Laurentc2f1f072009-07-17 12:17:14 -07001563 default:
1564 return BBinder::onTransact(code, data, reply, flags);
1565 }
1566}
1567
Andy Hungb0272092018-04-12 11:06:56 -07001568/** returns true if string overflow was prevented by zero termination */
1569template <size_t size>
1570static bool preventStringOverflow(char (&s)[size]) {
1571 if (strnlen(s, size) < size) return false;
1572 s[size - 1] = '\0';
1573 return true;
1574}
1575
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001576void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
1577{
1578 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
1579 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
1580 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
1581 }
1582 attr->tags[tagsMaxSize - 1] = '\0';
1583}
1584
Andy Hungb0272092018-04-12 11:06:56 -07001585/** returns BAD_VALUE if sanitization was required. */
1586status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
1587{
1588 if (preventStringOverflow(desc->name)
1589 | /* always */ preventStringOverflow(desc->implementor)) {
1590 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
1591 return BAD_VALUE;
1592 }
1593 return NO_ERROR;
1594}
1595
1596/** returns BAD_VALUE if sanitization was required. */
1597status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
1598{
1599 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
1600 preventStringOverflow(config->ext.device.address)) {
1601 return BAD_VALUE;
1602 }
1603 return NO_ERROR;
1604}
1605
Eric Laurentc2f1f072009-07-17 12:17:14 -07001606// ----------------------------------------------------------------------------
1607
Glenn Kasten40bc9062015-03-20 09:09:33 -07001608} // namespace android