blob: abf74f80e406af7f09e430d8d42a4ec2068bedd4 [file] [log] [blame]
Eric Laurentc2f1f072009-07-17 12:17:14 -07001/*
2**
3** Copyright 2009, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "IAudioPolicyService"
19#include <utils/Log.h>
20
21#include <stdint.h>
Eric Laurentac9cef52017-06-09 15:46:26 -070022#include <math.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070023#include <sys/types.h>
24
Eric Laurentb1cc36b2017-12-11 12:14:16 -080025#include <binder/IPCThreadState.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070026#include <binder/Parcel.h>
Eric Laurent74adca92014-11-05 12:15:36 -080027#include <media/AudioEffect.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070028#include <media/IAudioPolicyService.h>
Eric Laurent3528c932018-02-23 17:17:22 -080029#include <media/TimeCheck.h>
Andy Hung4ef19fa2018-05-15 19:35:29 -070030#include <mediautils/ServiceUtilities.h>
Dima Zavin64760242011-05-11 14:15:23 -070031#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070032
Eric Laurentc2f1f072009-07-17 12:17:14 -070033namespace android {
34
35enum {
36 SET_DEVICE_CONNECTION_STATE = IBinder::FIRST_CALL_TRANSACTION,
37 GET_DEVICE_CONNECTION_STATE,
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -080038 HANDLE_DEVICE_CONFIG_CHANGE,
Eric Laurentc2f1f072009-07-17 12:17:14 -070039 SET_PHONE_STATE,
Glenn Kasten0b07b802012-01-18 14:56:06 -080040 SET_RINGER_MODE, // reserved, no longer used
Eric Laurentc2f1f072009-07-17 12:17:14 -070041 SET_FORCE_USE,
42 GET_FORCE_USE,
43 GET_OUTPUT,
44 START_OUTPUT,
45 STOP_OUTPUT,
46 RELEASE_OUTPUT,
Eric Laurentcaf7f482014-11-25 17:50:47 -080047 GET_INPUT_FOR_ATTR,
Eric Laurentc2f1f072009-07-17 12:17:14 -070048 START_INPUT,
49 STOP_INPUT,
50 RELEASE_INPUT,
51 INIT_STREAM_VOLUME,
52 SET_STREAM_VOLUME,
Eric Laurentde070132010-07-13 04:45:46 -070053 GET_STREAM_VOLUME,
54 GET_STRATEGY_FOR_STREAM,
55 GET_OUTPUT_FOR_EFFECT,
56 REGISTER_EFFECT,
Eric Laurenteda6c362011-02-02 09:33:30 -080057 UNREGISTER_EFFECT,
Glenn Kasten6b2718c2011-02-04 13:54:26 -080058 IS_STREAM_ACTIVE,
Jean-Michel Trivid7086032012-10-10 12:11:16 -070059 IS_SOURCE_ACTIVE,
Glenn Kasten6b2718c2011-02-04 13:54:26 -080060 GET_DEVICES_FOR_STREAM,
Eric Laurentdb7c0792011-08-10 10:37:50 -070061 QUERY_DEFAULT_PRE_PROCESSING,
Jean-Michel Trivi272ab542013-02-04 16:26:02 -080062 SET_EFFECT_ENABLED,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +000063 IS_STREAM_ACTIVE_REMOTELY,
Eric Laurent203b1a12014-04-01 10:34:16 -070064 IS_OFFLOAD_SUPPORTED,
65 LIST_AUDIO_PORTS,
66 GET_AUDIO_PORT,
67 CREATE_AUDIO_PATCH,
68 RELEASE_AUDIO_PATCH,
69 LIST_AUDIO_PATCHES,
Eric Laurentb52c1522014-05-20 11:27:36 -070070 SET_AUDIO_PORT_CONFIG,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -070071 REGISTER_CLIENT,
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070072 GET_OUTPUT_FOR_ATTR,
73 ACQUIRE_SOUNDTRIGGER_SESSION,
Eric Laurentbb6c9a02014-09-25 14:11:47 -070074 RELEASE_SOUNDTRIGGER_SESSION,
Eric Laurentbaac1832014-12-01 17:52:59 -080075 GET_PHONE_STATE,
76 REGISTER_POLICY_MIXES,
Eric Laurent554a2772015-04-10 11:29:24 -070077 START_AUDIO_SOURCE,
Eric Laurente8726fe2015-06-26 09:39:24 -070078 STOP_AUDIO_SOURCE,
79 SET_AUDIO_PORT_CALLBACK_ENABLED,
Andy Hung2ddee192015-12-18 17:34:44 -080080 SET_MASTER_MONO,
81 GET_MASTER_MONO,
jiabin81772902018-04-02 17:52:27 -070082 GET_STREAM_VOLUME_DB,
83 GET_SURROUND_FORMATS,
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -070084 SET_SURROUND_FORMAT_ENABLED,
85 ADD_STREAM_DEFAULT_EFFECT,
86 REMOVE_STREAM_DEFAULT_EFFECT
Eric Laurentc2f1f072009-07-17 12:17:14 -070087};
88
Eric Laurent1d670b12015-02-06 10:44:24 -080089#define MAX_ITEMS_PER_LIST 1024
90
Eric Laurentc2f1f072009-07-17 12:17:14 -070091class BpAudioPolicyService : public BpInterface<IAudioPolicyService>
92{
93public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070094 explicit BpAudioPolicyService(const sp<IBinder>& impl)
Eric Laurentc2f1f072009-07-17 12:17:14 -070095 : BpInterface<IAudioPolicyService>(impl)
96 {
97 }
98
99 virtual status_t setDeviceConnectionState(
Dima Zavinfce7a472011-04-19 22:30:36 -0700100 audio_devices_t device,
101 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800102 const char *device_address,
103 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700104 {
105 Parcel data, reply;
106 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
107 data.writeInt32(static_cast <uint32_t>(device));
108 data.writeInt32(static_cast <uint32_t>(state));
109 data.writeCString(device_address);
Paul McLeane743a472015-01-28 11:07:31 -0800110 data.writeCString(device_name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700111 remote()->transact(SET_DEVICE_CONNECTION_STATE, data, &reply);
112 return static_cast <status_t> (reply.readInt32());
113 }
114
Dima Zavinfce7a472011-04-19 22:30:36 -0700115 virtual audio_policy_dev_state_t getDeviceConnectionState(
116 audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700117 const char *device_address)
118 {
119 Parcel data, reply;
120 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
121 data.writeInt32(static_cast <uint32_t>(device));
122 data.writeCString(device_address);
123 remote()->transact(GET_DEVICE_CONNECTION_STATE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700124 return static_cast <audio_policy_dev_state_t>(reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700125 }
126
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800127 virtual status_t handleDeviceConfigChange(audio_devices_t device,
128 const char *device_address,
129 const char *device_name)
130 {
131 Parcel data, reply;
132 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
133 data.writeInt32(static_cast <uint32_t>(device));
134 data.writeCString(device_address);
135 data.writeCString(device_name);
136 remote()->transact(HANDLE_DEVICE_CONFIG_CHANGE, data, &reply);
137 return static_cast <status_t> (reply.readInt32());
138 }
139
Glenn Kastenf78aee72012-01-04 11:00:47 -0800140 virtual status_t setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700141 {
142 Parcel data, reply;
143 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
144 data.writeInt32(state);
145 remote()->transact(SET_PHONE_STATE, data, &reply);
146 return static_cast <status_t> (reply.readInt32());
147 }
148
Dima Zavinfce7a472011-04-19 22:30:36 -0700149 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700150 {
151 Parcel data, reply;
152 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
153 data.writeInt32(static_cast <uint32_t>(usage));
154 data.writeInt32(static_cast <uint32_t>(config));
155 remote()->transact(SET_FORCE_USE, data, &reply);
156 return static_cast <status_t> (reply.readInt32());
157 }
158
Dima Zavinfce7a472011-04-19 22:30:36 -0700159 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700160 {
161 Parcel data, reply;
162 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
163 data.writeInt32(static_cast <uint32_t>(usage));
164 remote()->transact(GET_FORCE_USE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700165 return static_cast <audio_policy_forced_cfg_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700166 }
167
Eric Laurentf4e63452017-11-06 19:31:46 +0000168 virtual audio_io_handle_t getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700169 {
170 Parcel data, reply;
171 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
172 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700173 remote()->transact(GET_OUTPUT, data, &reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -0700174 return static_cast <audio_io_handle_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700175 }
176
Eric Laurente83b55d2014-11-14 10:06:21 -0800177 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
178 audio_io_handle_t *output,
179 audio_session_t session,
180 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200181 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700182 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800183 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800184 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700185 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800186 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700187 {
188 Parcel data, reply;
189 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
190 if (attr == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800191 if (stream == NULL) {
192 ALOGE("getOutputForAttr(): NULL audio attributes and stream type");
193 return BAD_VALUE;
194 }
195 if (*stream == AUDIO_STREAM_DEFAULT) {
196 ALOGE("getOutputForAttr unspecified stream type");
197 return BAD_VALUE;
198 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700199 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800200 if (output == NULL) {
201 ALOGE("getOutputForAttr NULL output - shouldn't happen");
202 return BAD_VALUE;
203 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700204 if (selectedDeviceId == NULL) {
205 ALOGE("getOutputForAttr NULL selectedDeviceId - shouldn't happen");
206 return BAD_VALUE;
207 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800208 if (portId == NULL) {
209 ALOGE("getOutputForAttr NULL portId - shouldn't happen");
210 return BAD_VALUE;
211 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800212 if (attr == NULL) {
213 data.writeInt32(0);
214 } else {
215 data.writeInt32(1);
216 data.write(attr, sizeof(audio_attributes_t));
217 }
218 data.writeInt32(session);
219 if (stream == NULL) {
220 data.writeInt32(0);
221 } else {
222 data.writeInt32(1);
223 data.writeInt32(*stream);
224 }
Nadav Bar766fb022018-01-07 12:18:03 +0200225 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700226 data.writeInt32(uid);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800227 data.write(config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700228 data.writeInt32(static_cast <uint32_t>(flags));
Eric Laurent9ae8c592017-06-22 17:17:09 -0700229 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800230 data.writeInt32(*portId);
Eric Laurente83b55d2014-11-14 10:06:21 -0800231 status_t status = remote()->transact(GET_OUTPUT_FOR_ATTR, data, &reply);
232 if (status != NO_ERROR) {
233 return status;
234 }
235 status = (status_t)reply.readInt32();
236 if (status != NO_ERROR) {
237 return status;
238 }
239 *output = (audio_io_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800240 audio_stream_type_t lStream = (audio_stream_type_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800241 if (stream != NULL) {
Eric Laurent20b9ef02016-12-05 11:03:16 -0800242 *stream = lStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800243 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700244 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800245 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800246 return status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700247 }
248
Eric Laurentd7fe0862018-07-14 16:48:01 -0700249 virtual status_t startOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700250 {
251 Parcel data, reply;
252 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700253 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700254 remote()->transact(START_OUTPUT, data, &reply);
255 return static_cast <status_t> (reply.readInt32());
256 }
257
Eric Laurentd7fe0862018-07-14 16:48:01 -0700258 virtual status_t stopOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700259 {
260 Parcel data, reply;
261 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700262 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700263 remote()->transact(STOP_OUTPUT, data, &reply);
264 return static_cast <status_t> (reply.readInt32());
265 }
266
Eric Laurentd7fe0862018-07-14 16:48:01 -0700267 virtual void releaseOutput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700268 {
269 Parcel data, reply;
270 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentd7fe0862018-07-14 16:48:01 -0700271 data.writeInt32((int32_t)portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700272 remote()->transact(RELEASE_OUTPUT, data, &reply);
273 }
274
Eric Laurentcaf7f482014-11-25 17:50:47 -0800275 virtual status_t getInputForAttr(const audio_attributes_t *attr,
276 audio_io_handle_t *input,
277 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700278 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700279 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800280 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800281 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600282 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700283 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800284 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700285 {
286 Parcel data, reply;
287 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentcaf7f482014-11-25 17:50:47 -0800288 if (attr == NULL) {
289 ALOGE("getInputForAttr NULL attr - shouldn't happen");
290 return BAD_VALUE;
291 }
292 if (input == NULL) {
293 ALOGE("getInputForAttr NULL input - shouldn't happen");
294 return BAD_VALUE;
295 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700296 if (selectedDeviceId == NULL) {
297 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
298 return BAD_VALUE;
299 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800300 if (portId == NULL) {
301 ALOGE("getInputForAttr NULL portId - shouldn't happen");
302 return BAD_VALUE;
303 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800304 data.write(attr, sizeof(audio_attributes_t));
Eric Laurenta54f1282017-07-01 19:39:32 -0700305 data.writeInt32(*input);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800306 data.writeInt32(session);
Eric Laurentb2379ba2016-05-23 17:42:12 -0700307 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700308 data.writeInt32(uid);
Eric Laurentfee19762018-01-29 18:44:13 -0800309 data.writeString16(opPackageName);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800310 data.write(config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -0700311 data.writeInt32(flags);
Eric Laurent9ae8c592017-06-22 17:17:09 -0700312 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800313 data.writeInt32(*portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800314 status_t status = remote()->transact(GET_INPUT_FOR_ATTR, data, &reply);
315 if (status != NO_ERROR) {
316 return status;
317 }
318 status = reply.readInt32();
319 if (status != NO_ERROR) {
320 return status;
321 }
322 *input = (audio_io_handle_t)reply.readInt32();
Eric Laurent9ae8c592017-06-22 17:17:09 -0700323 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800324 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800325 return NO_ERROR;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700326 }
327
Eric Laurentfee19762018-01-29 18:44:13 -0800328 virtual status_t startInput(audio_port_handle_t portId,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800329 bool *silenced)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700330 {
331 Parcel data, reply;
332 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800333 data.writeInt32(portId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800334 data.writeInt32(*silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700335 remote()->transact(START_INPUT, data, &reply);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800336 status_t status = static_cast <status_t> (reply.readInt32());
337 *silenced = reply.readInt32() == 1;
338 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700339 }
340
Eric Laurentfee19762018-01-29 18:44:13 -0800341 virtual status_t stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700342 {
343 Parcel data, reply;
344 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800345 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700346 remote()->transact(STOP_INPUT, data, &reply);
347 return static_cast <status_t> (reply.readInt32());
348 }
349
Eric Laurentfee19762018-01-29 18:44:13 -0800350 virtual void releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700351 {
352 Parcel data, reply;
353 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800354 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700355 remote()->transact(RELEASE_INPUT, data, &reply);
356 }
357
Dima Zavinfce7a472011-04-19 22:30:36 -0700358 virtual status_t initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700359 int indexMin,
360 int indexMax)
361 {
362 Parcel data, reply;
363 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
364 data.writeInt32(static_cast <uint32_t>(stream));
365 data.writeInt32(indexMin);
366 data.writeInt32(indexMax);
367 remote()->transact(INIT_STREAM_VOLUME, data, &reply);
368 return static_cast <status_t> (reply.readInt32());
369 }
370
Eric Laurent83844cc2011-11-18 16:43:31 -0800371 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
372 int index,
373 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700374 {
375 Parcel data, reply;
376 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
377 data.writeInt32(static_cast <uint32_t>(stream));
378 data.writeInt32(index);
Eric Laurent83844cc2011-11-18 16:43:31 -0800379 data.writeInt32(static_cast <uint32_t>(device));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700380 remote()->transact(SET_STREAM_VOLUME, data, &reply);
381 return static_cast <status_t> (reply.readInt32());
382 }
383
Eric Laurent83844cc2011-11-18 16:43:31 -0800384 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
385 int *index,
386 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700387 {
388 Parcel data, reply;
389 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
390 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurent83844cc2011-11-18 16:43:31 -0800391 data.writeInt32(static_cast <uint32_t>(device));
392
Eric Laurentc2f1f072009-07-17 12:17:14 -0700393 remote()->transact(GET_STREAM_VOLUME, data, &reply);
394 int lIndex = reply.readInt32();
395 if (index) *index = lIndex;
396 return static_cast <status_t> (reply.readInt32());
397 }
Eric Laurentde070132010-07-13 04:45:46 -0700398
Dima Zavinfce7a472011-04-19 22:30:36 -0700399 virtual uint32_t getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700400 {
401 Parcel data, reply;
402 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
403 data.writeInt32(static_cast <uint32_t>(stream));
404 remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply);
405 return reply.readInt32();
406 }
407
Eric Laurent63742522012-03-08 13:42:42 -0800408 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800409 {
410 Parcel data, reply;
411 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
412 data.writeInt32(static_cast <uint32_t>(stream));
413 remote()->transact(GET_DEVICES_FOR_STREAM, data, &reply);
Eric Laurent63742522012-03-08 13:42:42 -0800414 return (audio_devices_t) reply.readInt32();
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800415 }
416
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700417 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700418 {
419 Parcel data, reply;
420 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
421 data.write(desc, sizeof(effect_descriptor_t));
422 remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply);
423 return static_cast <audio_io_handle_t> (reply.readInt32());
424 }
425
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700426 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700427 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700428 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800429 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700430 int id)
431 {
432 Parcel data, reply;
433 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
434 data.write(desc, sizeof(effect_descriptor_t));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700435 data.writeInt32(io);
Eric Laurentde070132010-07-13 04:45:46 -0700436 data.writeInt32(strategy);
437 data.writeInt32(session);
438 data.writeInt32(id);
439 remote()->transact(REGISTER_EFFECT, data, &reply);
440 return static_cast <status_t> (reply.readInt32());
441 }
442
443 virtual status_t unregisterEffect(int id)
444 {
445 Parcel data, reply;
446 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
447 data.writeInt32(id);
448 remote()->transact(UNREGISTER_EFFECT, data, &reply);
449 return static_cast <status_t> (reply.readInt32());
450 }
451
Eric Laurentdb7c0792011-08-10 10:37:50 -0700452 virtual status_t setEffectEnabled(int id, bool enabled)
453 {
454 Parcel data, reply;
455 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
456 data.writeInt32(id);
457 data.writeInt32(enabled);
458 remote()->transact(SET_EFFECT_ENABLED, data, &reply);
459 return static_cast <status_t> (reply.readInt32());
460 }
461
Glenn Kastenfff6d712012-01-12 16:38:12 -0800462 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800463 {
464 Parcel data, reply;
465 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Glenn Kastenfff6d712012-01-12 16:38:12 -0800466 data.writeInt32((int32_t) stream);
Eric Laurenteda6c362011-02-02 09:33:30 -0800467 data.writeInt32(inPastMs);
468 remote()->transact(IS_STREAM_ACTIVE, data, &reply);
469 return reply.readInt32();
470 }
Eric Laurent57dae992011-07-24 13:36:09 -0700471
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800472 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
473 {
474 Parcel data, reply;
475 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
476 data.writeInt32((int32_t) stream);
477 data.writeInt32(inPastMs);
478 remote()->transact(IS_STREAM_ACTIVE_REMOTELY, data, &reply);
479 return reply.readInt32();
480 }
481
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700482 virtual bool isSourceActive(audio_source_t source) const
483 {
484 Parcel data, reply;
485 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
486 data.writeInt32((int32_t) source);
487 remote()->transact(IS_SOURCE_ACTIVE, data, &reply);
488 return reply.readInt32();
489 }
490
Glenn Kastend848eb42016-03-08 13:42:11 -0800491 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700492 effect_descriptor_t *descriptors,
493 uint32_t *count)
494 {
495 if (descriptors == NULL || count == NULL) {
496 return BAD_VALUE;
497 }
498 Parcel data, reply;
499 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
500 data.writeInt32(audioSession);
501 data.writeInt32(*count);
502 status_t status = remote()->transact(QUERY_DEFAULT_PRE_PROCESSING, data, &reply);
503 if (status != NO_ERROR) {
504 return status;
505 }
506 status = static_cast <status_t> (reply.readInt32());
507 uint32_t retCount = reply.readInt32();
508 if (retCount != 0) {
509 uint32_t numDesc = (retCount < *count) ? retCount : *count;
510 reply.read(descriptors, sizeof(effect_descriptor_t) * numDesc);
511 }
512 *count = retCount;
513 return status;
514 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000515
516 virtual bool isOffloadSupported(const audio_offload_info_t& info)
517 {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100518 Parcel data, reply;
519 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
520 data.write(&info, sizeof(audio_offload_info_t));
521 remote()->transact(IS_OFFLOAD_SUPPORTED, data, &reply);
Eric Laurent203b1a12014-04-01 10:34:16 -0700522 return reply.readInt32();
523 }
524
525 virtual status_t listAudioPorts(audio_port_role_t role,
526 audio_port_type_t type,
527 unsigned int *num_ports,
528 struct audio_port *ports,
529 unsigned int *generation)
530 {
531 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
532 generation == NULL) {
533 return BAD_VALUE;
534 }
535 Parcel data, reply;
536 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
537 unsigned int numPortsReq = (ports == NULL) ? 0 : *num_ports;
538 data.writeInt32(role);
539 data.writeInt32(type);
540 data.writeInt32(numPortsReq);
541 status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply);
542 if (status == NO_ERROR) {
543 status = (status_t)reply.readInt32();
544 *num_ports = (unsigned int)reply.readInt32();
545 }
Eric Laurent203b1a12014-04-01 10:34:16 -0700546 if (status == NO_ERROR) {
547 if (numPortsReq > *num_ports) {
548 numPortsReq = *num_ports;
549 }
550 if (numPortsReq > 0) {
551 reply.read(ports, numPortsReq * sizeof(struct audio_port));
552 }
553 *generation = reply.readInt32();
554 }
555 return status;
556 }
557
558 virtual status_t getAudioPort(struct audio_port *port)
559 {
560 if (port == NULL) {
561 return BAD_VALUE;
562 }
563 Parcel data, reply;
564 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
565 data.write(port, sizeof(struct audio_port));
566 status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply);
567 if (status != NO_ERROR ||
568 (status = (status_t)reply.readInt32()) != NO_ERROR) {
569 return status;
570 }
571 reply.read(port, sizeof(struct audio_port));
572 return status;
573 }
574
575 virtual status_t createAudioPatch(const struct audio_patch *patch,
576 audio_patch_handle_t *handle)
577 {
578 if (patch == NULL || handle == NULL) {
579 return BAD_VALUE;
580 }
581 Parcel data, reply;
582 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
583 data.write(patch, sizeof(struct audio_patch));
584 data.write(handle, sizeof(audio_patch_handle_t));
585 status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply);
586 if (status != NO_ERROR ||
587 (status = (status_t)reply.readInt32()) != NO_ERROR) {
588 return status;
589 }
590 reply.read(handle, sizeof(audio_patch_handle_t));
591 return status;
592 }
593
594 virtual status_t releaseAudioPatch(audio_patch_handle_t handle)
595 {
596 Parcel data, reply;
597 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
598 data.write(&handle, sizeof(audio_patch_handle_t));
599 status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply);
600 if (status != NO_ERROR) {
601 status = (status_t)reply.readInt32();
602 }
603 return status;
604 }
605
606 virtual status_t listAudioPatches(unsigned int *num_patches,
607 struct audio_patch *patches,
608 unsigned int *generation)
609 {
610 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
611 generation == NULL) {
612 return BAD_VALUE;
613 }
614 Parcel data, reply;
615 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
616 unsigned int numPatchesReq = (patches == NULL) ? 0 : *num_patches;
617 data.writeInt32(numPatchesReq);
618 status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply);
619 if (status == NO_ERROR) {
620 status = (status_t)reply.readInt32();
621 *num_patches = (unsigned int)reply.readInt32();
622 }
623 if (status == NO_ERROR) {
624 if (numPatchesReq > *num_patches) {
625 numPatchesReq = *num_patches;
626 }
627 if (numPatchesReq > 0) {
628 reply.read(patches, numPatchesReq * sizeof(struct audio_patch));
629 }
630 *generation = reply.readInt32();
631 }
632 return status;
633 }
634
635 virtual status_t setAudioPortConfig(const struct audio_port_config *config)
636 {
637 if (config == NULL) {
638 return BAD_VALUE;
639 }
640 Parcel data, reply;
641 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
642 data.write(config, sizeof(struct audio_port_config));
643 status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply);
644 if (status != NO_ERROR) {
645 status = (status_t)reply.readInt32();
646 }
647 return status;
648 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700649
Eric Laurentb52c1522014-05-20 11:27:36 -0700650 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client)
651 {
652 Parcel data, reply;
653 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -0800654 data.writeStrongBinder(IInterface::asBinder(client));
Eric Laurentb52c1522014-05-20 11:27:36 -0700655 remote()->transact(REGISTER_CLIENT, data, &reply);
656 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700657
Eric Laurente8726fe2015-06-26 09:39:24 -0700658 virtual void setAudioPortCallbacksEnabled(bool enabled)
659 {
660 Parcel data, reply;
661 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
662 data.writeInt32(enabled ? 1 : 0);
663 remote()->transact(SET_AUDIO_PORT_CALLBACK_ENABLED, data, &reply);
664 }
665
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700666 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
667 audio_io_handle_t *ioHandle,
668 audio_devices_t *device)
669 {
670 if (session == NULL || ioHandle == NULL || device == NULL) {
671 return BAD_VALUE;
672 }
673 Parcel data, reply;
674 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
675 status_t status = remote()->transact(ACQUIRE_SOUNDTRIGGER_SESSION, data, &reply);
676 if (status != NO_ERROR) {
677 return status;
678 }
679 status = (status_t)reply.readInt32();
680 if (status == NO_ERROR) {
681 *session = (audio_session_t)reply.readInt32();
682 *ioHandle = (audio_io_handle_t)reply.readInt32();
683 *device = (audio_devices_t)reply.readInt32();
684 }
685 return status;
686 }
687
688 virtual status_t releaseSoundTriggerSession(audio_session_t session)
689 {
690 Parcel data, reply;
691 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
692 data.writeInt32(session);
693 status_t status = remote()->transact(RELEASE_SOUNDTRIGGER_SESSION, data, &reply);
694 if (status != NO_ERROR) {
695 return status;
696 }
697 return (status_t)reply.readInt32();
698 }
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700699
700 virtual audio_mode_t getPhoneState()
701 {
702 Parcel data, reply;
703 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
704 status_t status = remote()->transact(GET_PHONE_STATE, data, &reply);
705 if (status != NO_ERROR) {
706 return AUDIO_MODE_INVALID;
707 }
708 return (audio_mode_t)reply.readInt32();
709 }
Eric Laurentbaac1832014-12-01 17:52:59 -0800710
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700711 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800712 {
713 Parcel data, reply;
714 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
715 data.writeInt32(registration ? 1 : 0);
716 size_t size = mixes.size();
717 if (size > MAX_MIXES_PER_POLICY) {
718 size = MAX_MIXES_PER_POLICY;
719 }
720 size_t sizePosition = data.dataPosition();
721 data.writeInt32(size);
722 size_t finalSize = size;
723 for (size_t i = 0; i < size; i++) {
724 size_t position = data.dataPosition();
725 if (mixes[i].writeToParcel(&data) != NO_ERROR) {
726 data.setDataPosition(position);
727 finalSize--;
728 }
729 }
730 if (size != finalSize) {
731 size_t position = data.dataPosition();
732 data.setDataPosition(sizePosition);
733 data.writeInt32(finalSize);
734 data.setDataPosition(position);
735 }
736 status_t status = remote()->transact(REGISTER_POLICY_MIXES, data, &reply);
737 if (status == NO_ERROR) {
738 status = (status_t)reply.readInt32();
739 }
740 return status;
741 }
Eric Laurent554a2772015-04-10 11:29:24 -0700742
743 virtual status_t startAudioSource(const struct audio_port_config *source,
744 const audio_attributes_t *attributes,
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700745 audio_port_handle_t *portId)
Eric Laurent554a2772015-04-10 11:29:24 -0700746 {
747 Parcel data, reply;
748 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700749 if (source == NULL || attributes == NULL || portId == NULL) {
Eric Laurent554a2772015-04-10 11:29:24 -0700750 return BAD_VALUE;
751 }
752 data.write(source, sizeof(struct audio_port_config));
753 data.write(attributes, sizeof(audio_attributes_t));
754 status_t status = remote()->transact(START_AUDIO_SOURCE, data, &reply);
755 if (status != NO_ERROR) {
756 return status;
757 }
758 status = (status_t)reply.readInt32();
759 if (status != NO_ERROR) {
760 return status;
761 }
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700762 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -0700763 return status;
764 }
765
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700766 virtual status_t stopAudioSource(audio_port_handle_t portId)
Eric Laurent554a2772015-04-10 11:29:24 -0700767 {
768 Parcel data, reply;
769 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurent3e6c7e12018-07-27 17:09:23 -0700770 data.writeInt32(portId);
Eric Laurent554a2772015-04-10 11:29:24 -0700771 status_t status = remote()->transact(STOP_AUDIO_SOURCE, data, &reply);
772 if (status != NO_ERROR) {
773 return status;
774 }
775 status = (status_t)reply.readInt32();
776 return status;
777 }
Andy Hung2ddee192015-12-18 17:34:44 -0800778
779 virtual status_t setMasterMono(bool mono)
780 {
781 Parcel data, reply;
782 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
783 data.writeInt32(static_cast<int32_t>(mono));
784 status_t status = remote()->transact(SET_MASTER_MONO, data, &reply);
785 if (status != NO_ERROR) {
786 return status;
787 }
788 return static_cast<status_t>(reply.readInt32());
789 }
790
791 virtual status_t getMasterMono(bool *mono)
792 {
793 if (mono == nullptr) {
794 return BAD_VALUE;
795 }
796 Parcel data, reply;
797 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
798
799 status_t status = remote()->transact(GET_MASTER_MONO, data, &reply);
800 if (status != NO_ERROR) {
801 return status;
802 }
803 status = static_cast<status_t>(reply.readInt32());
804 if (status == NO_ERROR) {
805 *mono = static_cast<bool>(reply.readInt32());
806 }
807 return status;
808 }
Eric Laurentac9cef52017-06-09 15:46:26 -0700809
810 virtual float getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
811 {
812 Parcel data, reply;
813 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
814 data.writeInt32(static_cast <int32_t>(stream));
815 data.writeInt32(static_cast <int32_t>(index));
816 data.writeUint32(static_cast <uint32_t>(device));
817 status_t status = remote()->transact(GET_STREAM_VOLUME_DB, data, &reply);
818 if (status != NO_ERROR) {
819 return NAN;
820 }
821 return reply.readFloat();
822 }
jiabin81772902018-04-02 17:52:27 -0700823
824 virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
825 audio_format_t *surroundFormats,
826 bool *surroundFormatsEnabled,
827 bool reported)
828 {
829 if (numSurroundFormats == NULL || (*numSurroundFormats != 0 &&
830 (surroundFormats == NULL || surroundFormatsEnabled == NULL))) {
831 return BAD_VALUE;
832 }
833 Parcel data, reply;
834 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
835 unsigned int numSurroundFormatsReq = *numSurroundFormats;
836 data.writeUint32(numSurroundFormatsReq);
837 data.writeBool(reported);
838 status_t status = remote()->transact(GET_SURROUND_FORMATS, data, &reply);
839 if (status == NO_ERROR && (status = (status_t)reply.readInt32()) == NO_ERROR) {
840 *numSurroundFormats = reply.readUint32();
841 }
842 if (status == NO_ERROR) {
843 if (numSurroundFormatsReq > *numSurroundFormats) {
844 numSurroundFormatsReq = *numSurroundFormats;
845 }
846 if (numSurroundFormatsReq > 0) {
847 status = reply.read(surroundFormats,
848 numSurroundFormatsReq * sizeof(audio_format_t));
849 if (status != NO_ERROR) {
850 return status;
851 }
852 status = reply.read(surroundFormatsEnabled,
853 numSurroundFormatsReq * sizeof(bool));
854 }
855 }
856 return status;
857 }
858
859 virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
860 {
861 Parcel data, reply;
862 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
863 data.writeInt32(audioFormat);
864 data.writeBool(enabled);
865 status_t status = remote()->transact(SET_SURROUND_FORMAT_ENABLED, data, &reply);
866 if (status != NO_ERROR) {
867 return status;
868 }
869 return reply.readInt32();
870 }
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -0700871
872 virtual status_t addStreamDefaultEffect(const effect_uuid_t *type,
873 const String16& opPackageName,
874 const effect_uuid_t *uuid,
875 int32_t priority,
876 audio_usage_t usage,
877 audio_unique_id_t* id)
878 {
879 Parcel data, reply;
880 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
881 data.write(type, sizeof(effect_uuid_t));
882 data.writeString16(opPackageName);
883 data.write(uuid, sizeof(effect_uuid_t));
884 data.writeInt32(priority);
885 data.writeInt32((int32_t) usage);
886 status_t status = remote()->transact(ADD_STREAM_DEFAULT_EFFECT, data, &reply);
887 if (status != NO_ERROR) {
888 return status;
889 }
890 status = static_cast <status_t> (reply.readInt32());
891 *id = reply.readInt32();
892 return status;
893 }
894
895 virtual status_t removeStreamDefaultEffect(audio_unique_id_t id)
896 {
897 Parcel data, reply;
898 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
899 data.writeInt32(id);
900 status_t status = remote()->transact(REMOVE_STREAM_DEFAULT_EFFECT, data, &reply);
901 if (status != NO_ERROR) {
902 return status;
903 }
904 return static_cast <status_t> (reply.readInt32());
905 }
906
Eric Laurentc2f1f072009-07-17 12:17:14 -0700907};
908
909IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
910
911// ----------------------------------------------------------------------
912
Eric Laurentc2f1f072009-07-17 12:17:14 -0700913status_t BnAudioPolicyService::onTransact(
914 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
915{
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800916 // make sure transactions reserved to AudioFlinger do not come from other processes
917 switch (code) {
918 case START_OUTPUT:
919 case STOP_OUTPUT:
920 case RELEASE_OUTPUT:
921 case GET_INPUT_FOR_ATTR:
922 case START_INPUT:
923 case STOP_INPUT:
924 case RELEASE_INPUT:
925 case GET_STRATEGY_FOR_STREAM:
926 case GET_OUTPUT_FOR_EFFECT:
927 case REGISTER_EFFECT:
928 case UNREGISTER_EFFECT:
929 case SET_EFFECT_ENABLED:
930 case GET_OUTPUT_FOR_ATTR:
931 case ACQUIRE_SOUNDTRIGGER_SESSION:
932 case RELEASE_SOUNDTRIGGER_SESSION:
933 ALOGW("%s: transaction %d received from PID %d",
934 __func__, code, IPCThreadState::self()->getCallingPid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700935 // return status only for non void methods
936 switch (code) {
937 case RELEASE_OUTPUT:
938 case RELEASE_INPUT:
939 break;
940 default:
941 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
942 break;
943 }
944 return OK;
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800945 default:
946 break;
947 }
948
Eric Laurent4980df22018-01-26 18:04:09 -0800949 // make sure the following transactions come from system components
950 switch (code) {
951 case SET_DEVICE_CONNECTION_STATE:
952 case HANDLE_DEVICE_CONFIG_CHANGE:
953 case SET_PHONE_STATE:
Eric Laurente17378d2018-05-09 14:43:01 -0700954//FIXME: Allow SET_FORCE_USE calls from system apps until a better use case routing API is available
955// case SET_FORCE_USE:
Eric Laurent4980df22018-01-26 18:04:09 -0800956 case INIT_STREAM_VOLUME:
957 case SET_STREAM_VOLUME:
958 case REGISTER_POLICY_MIXES:
Eric Laurent10b71232018-04-13 18:14:44 -0700959 case SET_MASTER_MONO:
960 case START_AUDIO_SOURCE:
jiabin81772902018-04-02 17:52:27 -0700961 case STOP_AUDIO_SOURCE:
962 case GET_SURROUND_FORMATS:
963 case SET_SURROUND_FORMAT_ENABLED: {
Andy Hung4ef19fa2018-05-15 19:35:29 -0700964 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
Eric Laurent4980df22018-01-26 18:04:09 -0800965 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
966 __func__, code, IPCThreadState::self()->getCallingPid(),
967 IPCThreadState::self()->getCallingUid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700968 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
969 return OK;
Eric Laurent4980df22018-01-26 18:04:09 -0800970 }
Eric Laurent96c7eed2018-03-26 17:57:01 -0700971 } break;
Eric Laurent4980df22018-01-26 18:04:09 -0800972 default:
973 break;
974 }
975
Eric Laurent39b09b52018-06-29 12:24:40 -0700976 std::string tag("IAudioPolicyService command " + std::to_string(code));
977 TimeCheck check(tag.c_str());
Eric Laurent3528c932018-02-23 17:17:22 -0800978
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700979 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700980 case SET_DEVICE_CONNECTION_STATE: {
981 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700982 audio_devices_t device =
983 static_cast <audio_devices_t>(data.readInt32());
984 audio_policy_dev_state_t state =
985 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700986 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -0800987 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800988 if (device_address == nullptr || device_name == nullptr) {
989 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
990 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
991 } else {
992 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
993 state,
994 device_address,
995 device_name)));
996 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700997 return NO_ERROR;
998 } break;
999
1000 case GET_DEVICE_CONNECTION_STATE: {
1001 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001002 audio_devices_t device =
1003 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001004 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001005 if (device_address == nullptr) {
1006 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
1007 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
1008 } else {
1009 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
1010 device_address)));
1011 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001012 return NO_ERROR;
1013 } break;
1014
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001015 case HANDLE_DEVICE_CONFIG_CHANGE: {
1016 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1017 audio_devices_t device =
1018 static_cast <audio_devices_t>(data.readInt32());
1019 const char *device_address = data.readCString();
1020 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -08001021 if (device_address == nullptr || device_name == nullptr) {
1022 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
1023 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1024 } else {
1025 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
1026 device_address,
1027 device_name)));
1028 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001029 return NO_ERROR;
1030 } break;
1031
Eric Laurentc2f1f072009-07-17 12:17:14 -07001032 case SET_PHONE_STATE: {
1033 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001034 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
1035 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001036 return NO_ERROR;
1037 } break;
1038
Eric Laurentc2f1f072009-07-17 12:17:14 -07001039 case SET_FORCE_USE: {
1040 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001041 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1042 data.readInt32());
Dima Zavinfce7a472011-04-19 22:30:36 -07001043 audio_policy_forced_cfg_t config =
1044 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001045 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
1046 return NO_ERROR;
1047 } break;
1048
1049 case GET_FORCE_USE: {
1050 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001051 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1052 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001053 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
1054 return NO_ERROR;
1055 } break;
1056
1057 case GET_OUTPUT: {
1058 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001059 audio_stream_type_t stream =
1060 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +00001061 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001062 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001063 return NO_ERROR;
1064 } break;
1065
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001066 case GET_OUTPUT_FOR_ATTR: {
1067 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001068 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -08001069 bool hasAttributes = data.readInt32() != 0;
1070 if (hasAttributes) {
1071 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001072 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -08001073 }
1074 audio_session_t session = (audio_session_t)data.readInt32();
1075 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
1076 bool hasStream = data.readInt32() != 0;
1077 if (hasStream) {
1078 stream = (audio_stream_type_t)data.readInt32();
1079 }
Nadav Bar766fb022018-01-07 12:18:03 +02001080 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001081 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001082 audio_config_t config;
1083 memset(&config, 0, sizeof(audio_config_t));
1084 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001085 audio_output_flags_t flags =
1086 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001087 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001088 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001089 audio_io_handle_t output = 0;
Eric Laurente83b55d2014-11-14 10:06:21 -08001090 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001091 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001092 &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001093 flags, &selectedDeviceId, &portId);
Eric Laurente83b55d2014-11-14 10:06:21 -08001094 reply->writeInt32(status);
1095 reply->writeInt32(output);
1096 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001097 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001098 reply->writeInt32(portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001099 return NO_ERROR;
1100 } break;
1101
Eric Laurentc2f1f072009-07-17 12:17:14 -07001102 case START_OUTPUT: {
1103 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001104 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1105 reply->writeInt32(static_cast <uint32_t>(startOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001106 return NO_ERROR;
1107 } break;
1108
1109 case STOP_OUTPUT: {
1110 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001111 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1112 reply->writeInt32(static_cast <uint32_t>(stopOutput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001113 return NO_ERROR;
1114 } break;
1115
1116 case RELEASE_OUTPUT: {
1117 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentd7fe0862018-07-14 16:48:01 -07001118 const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1119 releaseOutput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001120 return NO_ERROR;
1121 } break;
1122
Eric Laurentcaf7f482014-11-25 17:50:47 -08001123 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001124 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001125 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001126 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001127 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001128 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001129 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001130 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001131 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001132 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001133 audio_config_base_t config;
1134 memset(&config, 0, sizeof(audio_config_base_t));
1135 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001136 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001137 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001138 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001139 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001140 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001141 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001142 reply->writeInt32(status);
1143 if (status == NO_ERROR) {
1144 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001145 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001146 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001147 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001148 return NO_ERROR;
1149 } break;
1150
1151 case START_INPUT: {
1152 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001153 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001154 bool silenced = data.readInt32() == 1;
Eric Laurentfee19762018-01-29 18:44:13 -08001155 status_t status = startInput(portId, &silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001156 reply->writeInt32(static_cast <uint32_t>(status));
1157 reply->writeInt32(silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001158 return NO_ERROR;
1159 } break;
1160
1161 case STOP_INPUT: {
1162 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001163 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1164 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001165 return NO_ERROR;
1166 } break;
1167
1168 case RELEASE_INPUT: {
1169 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001170 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1171 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001172 return NO_ERROR;
1173 } break;
1174
1175 case INIT_STREAM_VOLUME: {
1176 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001177 audio_stream_type_t stream =
1178 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001179 int indexMin = data.readInt32();
1180 int indexMax = data.readInt32();
1181 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1182 return NO_ERROR;
1183 } break;
1184
1185 case SET_STREAM_VOLUME: {
1186 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001187 audio_stream_type_t stream =
1188 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001189 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001190 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1191 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1192 index,
1193 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001194 return NO_ERROR;
1195 } break;
1196
1197 case GET_STREAM_VOLUME: {
1198 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001199 audio_stream_type_t stream =
1200 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurent83844cc2011-11-18 16:43:31 -08001201 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001202 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001203 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001204 reply->writeInt32(index);
1205 reply->writeInt32(static_cast <uint32_t>(status));
1206 return NO_ERROR;
1207 } break;
1208
Eric Laurentde070132010-07-13 04:45:46 -07001209 case GET_STRATEGY_FOR_STREAM: {
1210 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001211 audio_stream_type_t stream =
1212 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentde070132010-07-13 04:45:46 -07001213 reply->writeInt32(getStrategyForStream(stream));
1214 return NO_ERROR;
1215 } break;
1216
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001217 case GET_DEVICES_FOR_STREAM: {
1218 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001219 audio_stream_type_t stream =
1220 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001221 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1222 return NO_ERROR;
1223 } break;
1224
Eric Laurentde070132010-07-13 04:45:46 -07001225 case GET_OUTPUT_FOR_EFFECT: {
1226 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001227 effect_descriptor_t desc = {};
1228 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1229 android_errorWriteLog(0x534e4554, "73126106");
1230 }
1231 (void)sanitizeEffectDescriptor(&desc);
Eric Laurentde070132010-07-13 04:45:46 -07001232 audio_io_handle_t output = getOutputForEffect(&desc);
1233 reply->writeInt32(static_cast <int>(output));
1234 return NO_ERROR;
1235 } break;
1236
1237 case REGISTER_EFFECT: {
1238 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001239 effect_descriptor_t desc = {};
1240 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1241 android_errorWriteLog(0x534e4554, "73126106");
1242 }
1243 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001244 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001245 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001246 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001247 int id = data.readInt32();
1248 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001249 io,
Eric Laurentde070132010-07-13 04:45:46 -07001250 strategy,
1251 session,
1252 id)));
1253 return NO_ERROR;
1254 } break;
1255
1256 case UNREGISTER_EFFECT: {
1257 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1258 int id = data.readInt32();
1259 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1260 return NO_ERROR;
1261 } break;
1262
Eric Laurentdb7c0792011-08-10 10:37:50 -07001263 case SET_EFFECT_ENABLED: {
1264 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1265 int id = data.readInt32();
1266 bool enabled = static_cast <bool>(data.readInt32());
1267 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1268 return NO_ERROR;
1269 } break;
1270
Eric Laurenteda6c362011-02-02 09:33:30 -08001271 case IS_STREAM_ACTIVE: {
1272 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001273 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001274 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001275 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001276 return NO_ERROR;
1277 } break;
1278
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001279 case IS_STREAM_ACTIVE_REMOTELY: {
1280 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1281 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1282 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001283 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001284 return NO_ERROR;
1285 } break;
1286
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001287 case IS_SOURCE_ACTIVE: {
1288 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1289 audio_source_t source = (audio_source_t) data.readInt32();
1290 reply->writeInt32( isSourceActive(source));
1291 return NO_ERROR;
1292 }
1293
Eric Laurent57dae992011-07-24 13:36:09 -07001294 case QUERY_DEFAULT_PRE_PROCESSING: {
1295 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001296 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001297 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001298 if (count > AudioEffect::kMaxPreProcessing) {
1299 count = AudioEffect::kMaxPreProcessing;
1300 }
Eric Laurent57dae992011-07-24 13:36:09 -07001301 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001302 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001303 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1304 reply->writeInt32(status);
1305 if (status != NO_ERROR && status != NO_MEMORY) {
1306 retCount = 0;
1307 }
1308 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001309 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001310 if (retCount < count) {
1311 count = retCount;
1312 }
1313 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1314 }
1315 delete[] descriptors;
1316 return status;
1317 }
1318
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001319 case IS_OFFLOAD_SUPPORTED: {
1320 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001321 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001322 data.read(&info, sizeof(audio_offload_info_t));
1323 bool isSupported = isOffloadSupported(info);
1324 reply->writeInt32(isSupported);
1325 return NO_ERROR;
1326 }
1327
Eric Laurent203b1a12014-04-01 10:34:16 -07001328 case LIST_AUDIO_PORTS: {
1329 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1330 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1331 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1332 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001333 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1334 numPortsReq = MAX_ITEMS_PER_LIST;
1335 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001336 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001337 struct audio_port *ports =
1338 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001339 if (ports == NULL) {
1340 reply->writeInt32(NO_MEMORY);
1341 reply->writeInt32(0);
1342 return NO_ERROR;
1343 }
1344 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001345 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1346 reply->writeInt32(status);
1347 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001348
1349 if (status == NO_ERROR) {
1350 if (numPortsReq > numPorts) {
1351 numPortsReq = numPorts;
1352 }
1353 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1354 reply->writeInt32(generation);
1355 }
1356 free(ports);
1357 return NO_ERROR;
1358 }
1359
1360 case GET_AUDIO_PORT: {
1361 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001362 struct audio_port port = {};
1363 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1364 ALOGE("b/23912202");
1365 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001366 status_t status = getAudioPort(&port);
1367 reply->writeInt32(status);
1368 if (status == NO_ERROR) {
1369 reply->write(&port, sizeof(struct audio_port));
1370 }
1371 return NO_ERROR;
1372 }
1373
1374 case CREATE_AUDIO_PATCH: {
1375 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001376 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001377 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001378 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001379 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1380 ALOGE("b/23912202");
1381 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001382 status_t status = createAudioPatch(&patch, &handle);
1383 reply->writeInt32(status);
1384 if (status == NO_ERROR) {
1385 reply->write(&handle, sizeof(audio_patch_handle_t));
1386 }
1387 return NO_ERROR;
1388 }
1389
1390 case RELEASE_AUDIO_PATCH: {
1391 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001392 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001393 data.read(&handle, sizeof(audio_patch_handle_t));
1394 status_t status = releaseAudioPatch(handle);
1395 reply->writeInt32(status);
1396 return NO_ERROR;
1397 }
1398
1399 case LIST_AUDIO_PATCHES: {
1400 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1401 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001402 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1403 numPatchesReq = MAX_ITEMS_PER_LIST;
1404 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001405 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001406 struct audio_patch *patches =
1407 (struct audio_patch *)calloc(numPatchesReq,
1408 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001409 if (patches == NULL) {
1410 reply->writeInt32(NO_MEMORY);
1411 reply->writeInt32(0);
1412 return NO_ERROR;
1413 }
1414 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001415 status_t status = listAudioPatches(&numPatches, patches, &generation);
1416 reply->writeInt32(status);
1417 reply->writeInt32(numPatches);
1418 if (status == NO_ERROR) {
1419 if (numPatchesReq > numPatches) {
1420 numPatchesReq = numPatches;
1421 }
1422 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1423 reply->writeInt32(generation);
1424 }
1425 free(patches);
1426 return NO_ERROR;
1427 }
1428
1429 case SET_AUDIO_PORT_CONFIG: {
1430 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001431 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001432 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001433 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001434 status_t status = setAudioPortConfig(&config);
1435 reply->writeInt32(status);
1436 return NO_ERROR;
1437 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001438
Eric Laurentb52c1522014-05-20 11:27:36 -07001439 case REGISTER_CLIENT: {
1440 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1441 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1442 data.readStrongBinder());
1443 registerClient(client);
1444 return NO_ERROR;
1445 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001446
Eric Laurente8726fe2015-06-26 09:39:24 -07001447 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1448 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1449 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1450 return NO_ERROR;
1451 } break;
1452
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001453 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1454 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1455 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1456 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001457 audio_session_t session = AUDIO_SESSION_NONE;
1458 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1459 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001460 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1461 reply->writeInt32(status);
1462 if (status == NO_ERROR) {
1463 reply->writeInt32(session);
1464 reply->writeInt32(ioHandle);
1465 reply->writeInt32(device);
1466 }
1467 return NO_ERROR;
1468 } break;
1469
1470 case RELEASE_SOUNDTRIGGER_SESSION: {
1471 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1472 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1473 data.readStrongBinder());
1474 audio_session_t session = (audio_session_t)data.readInt32();
1475 status_t status = releaseSoundTriggerSession(session);
1476 reply->writeInt32(status);
1477 return NO_ERROR;
1478 } break;
1479
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001480 case GET_PHONE_STATE: {
1481 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1482 reply->writeInt32((int32_t)getPhoneState());
1483 return NO_ERROR;
1484 } break;
1485
Eric Laurentbaac1832014-12-01 17:52:59 -08001486 case REGISTER_POLICY_MIXES: {
1487 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1488 bool registration = data.readInt32() == 1;
1489 Vector<AudioMix> mixes;
1490 size_t size = (size_t)data.readInt32();
1491 if (size > MAX_MIXES_PER_POLICY) {
1492 size = MAX_MIXES_PER_POLICY;
1493 }
1494 for (size_t i = 0; i < size; i++) {
1495 AudioMix mix;
1496 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1497 mixes.add(mix);
1498 }
1499 }
1500 status_t status = registerPolicyMixes(mixes, registration);
1501 reply->writeInt32(status);
1502 return NO_ERROR;
1503 } break;
1504
Eric Laurent554a2772015-04-10 11:29:24 -07001505 case START_AUDIO_SOURCE: {
1506 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001507 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001508 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001509 (void)sanitizeAudioPortConfig(&source);
1510 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001511 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001512 sanetizeAudioAttributes(&attributes);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001513 audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
1514 status_t status = startAudioSource(&source, &attributes, &portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001515 reply->writeInt32(status);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001516 reply->writeInt32(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001517 return NO_ERROR;
1518 } break;
1519
1520 case STOP_AUDIO_SOURCE: {
1521 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurent3e6c7e12018-07-27 17:09:23 -07001522 audio_port_handle_t portId = (audio_port_handle_t) data.readInt32();
1523 status_t status = stopAudioSource(portId);
Eric Laurent554a2772015-04-10 11:29:24 -07001524 reply->writeInt32(status);
1525 return NO_ERROR;
1526 } break;
1527
Andy Hung2ddee192015-12-18 17:34:44 -08001528 case SET_MASTER_MONO: {
1529 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1530 bool mono = static_cast<bool>(data.readInt32());
1531 status_t status = setMasterMono(mono);
1532 reply->writeInt32(status);
1533 return NO_ERROR;
1534 } break;
1535
1536 case GET_MASTER_MONO: {
1537 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1538 bool mono;
1539 status_t status = getMasterMono(&mono);
1540 reply->writeInt32(status);
1541 if (status == NO_ERROR) {
1542 reply->writeInt32(static_cast<int32_t>(mono));
1543 }
1544 return NO_ERROR;
1545 } break;
1546
Eric Laurentac9cef52017-06-09 15:46:26 -07001547 case GET_STREAM_VOLUME_DB: {
1548 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1549 audio_stream_type_t stream =
1550 static_cast <audio_stream_type_t>(data.readInt32());
1551 int index = static_cast <int>(data.readInt32());
1552 audio_devices_t device =
1553 static_cast <audio_devices_t>(data.readUint32());
1554 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1555 return NO_ERROR;
1556 }
1557
jiabin81772902018-04-02 17:52:27 -07001558 case GET_SURROUND_FORMATS: {
1559 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1560 unsigned int numSurroundFormatsReq = data.readUint32();
1561 if (numSurroundFormatsReq > MAX_ITEMS_PER_LIST) {
1562 numSurroundFormatsReq = MAX_ITEMS_PER_LIST;
1563 }
1564 bool reported = data.readBool();
1565 unsigned int numSurroundFormats = numSurroundFormatsReq;
1566 audio_format_t *surroundFormats = (audio_format_t *)calloc(
1567 numSurroundFormats, sizeof(audio_format_t));
1568 bool *surroundFormatsEnabled = (bool *)calloc(numSurroundFormats, sizeof(bool));
1569 if (numSurroundFormatsReq > 0 &&
1570 (surroundFormats == NULL || surroundFormatsEnabled == NULL)) {
1571 free(surroundFormats);
1572 free(surroundFormatsEnabled);
1573 reply->writeInt32(NO_MEMORY);
1574 return NO_ERROR;
1575 }
1576 status_t status = getSurroundFormats(
1577 &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1578 reply->writeInt32(status);
1579
1580 if (status == NO_ERROR) {
1581 reply->writeUint32(numSurroundFormats);
1582 if (numSurroundFormatsReq > numSurroundFormats) {
1583 numSurroundFormatsReq = numSurroundFormats;
1584 }
1585 reply->write(surroundFormats, numSurroundFormatsReq * sizeof(audio_format_t));
1586 reply->write(surroundFormatsEnabled, numSurroundFormatsReq * sizeof(bool));
1587 }
1588 free(surroundFormats);
1589 free(surroundFormatsEnabled);
1590 return NO_ERROR;
1591 }
1592
1593 case SET_SURROUND_FORMAT_ENABLED: {
1594 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1595 audio_format_t audioFormat = (audio_format_t) data.readInt32();
1596 bool enabled = data.readBool();
1597 status_t status = setSurroundFormatEnabled(audioFormat, enabled);
1598 reply->writeInt32(status);
1599 return NO_ERROR;
1600 }
1601
Ari Hausman-Cohen433722e2018-04-24 14:25:22 -07001602 case ADD_STREAM_DEFAULT_EFFECT: {
1603 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1604 effect_uuid_t type;
1605 status_t status = data.read(&type, sizeof(effect_uuid_t));
1606 if (status != NO_ERROR) {
1607 return status;
1608 }
1609 String16 opPackageName;
1610 status = data.readString16(&opPackageName);
1611 if (status != NO_ERROR) {
1612 return status;
1613 }
1614 effect_uuid_t uuid;
1615 status = data.read(&uuid, sizeof(effect_uuid_t));
1616 if (status != NO_ERROR) {
1617 return status;
1618 }
1619 int32_t priority = data.readInt32();
1620 audio_usage_t usage = (audio_usage_t) data.readInt32();
1621 audio_unique_id_t id = 0;
1622 reply->writeInt32(static_cast <int32_t>(addStreamDefaultEffect(&type,
1623 opPackageName,
1624 &uuid,
1625 priority,
1626 usage,
1627 &id)));
1628 reply->writeInt32(id);
1629 return NO_ERROR;
1630 }
1631
1632 case REMOVE_STREAM_DEFAULT_EFFECT: {
1633 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1634 audio_unique_id_t id = static_cast<audio_unique_id_t>(data.readInt32());
1635 reply->writeInt32(static_cast <int32_t>(removeStreamDefaultEffect(id)));
1636 return NO_ERROR;
1637 }
1638
Eric Laurentc2f1f072009-07-17 12:17:14 -07001639 default:
1640 return BBinder::onTransact(code, data, reply, flags);
1641 }
1642}
1643
Andy Hungb0272092018-04-12 11:06:56 -07001644/** returns true if string overflow was prevented by zero termination */
1645template <size_t size>
1646static bool preventStringOverflow(char (&s)[size]) {
1647 if (strnlen(s, size) < size) return false;
1648 s[size - 1] = '\0';
1649 return true;
1650}
1651
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001652void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
1653{
1654 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
1655 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
1656 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
1657 }
1658 attr->tags[tagsMaxSize - 1] = '\0';
1659}
1660
Andy Hungb0272092018-04-12 11:06:56 -07001661/** returns BAD_VALUE if sanitization was required. */
1662status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
1663{
1664 if (preventStringOverflow(desc->name)
1665 | /* always */ preventStringOverflow(desc->implementor)) {
1666 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
1667 return BAD_VALUE;
1668 }
1669 return NO_ERROR;
1670}
1671
1672/** returns BAD_VALUE if sanitization was required. */
1673status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
1674{
1675 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
1676 preventStringOverflow(config->ext.device.address)) {
1677 return BAD_VALUE;
1678 }
1679 return NO_ERROR;
1680}
1681
Eric Laurentc2f1f072009-07-17 12:17:14 -07001682// ----------------------------------------------------------------------------
1683
Glenn Kasten40bc9062015-03-20 09:09:33 -07001684} // namespace android