blob: cf4b600b57ae58b613c5bb78861428222005c01b [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 Laurentde070132010-07-13 04:45:46 -0700247 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700248 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800249 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700250 {
251 Parcel data, reply;
252 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfa2877b2009-07-28 08:44:33 -0700253 data.writeInt32(output);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800254 data.writeInt32((int32_t) stream);
Glenn Kastend848eb42016-03-08 13:42:11 -0800255 data.writeInt32((int32_t) session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700256 remote()->transact(START_OUTPUT, data, &reply);
257 return static_cast <status_t> (reply.readInt32());
258 }
259
Eric Laurentde070132010-07-13 04:45:46 -0700260 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700261 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800262 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700263 {
264 Parcel data, reply;
265 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfa2877b2009-07-28 08:44:33 -0700266 data.writeInt32(output);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800267 data.writeInt32((int32_t) stream);
Glenn Kastend848eb42016-03-08 13:42:11 -0800268 data.writeInt32((int32_t) session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700269 remote()->transact(STOP_OUTPUT, data, &reply);
270 return static_cast <status_t> (reply.readInt32());
271 }
272
Eric Laurente83b55d2014-11-14 10:06:21 -0800273 virtual void releaseOutput(audio_io_handle_t output,
274 audio_stream_type_t stream,
275 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700276 {
277 Parcel data, reply;
278 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfa2877b2009-07-28 08:44:33 -0700279 data.writeInt32(output);
Eric Laurente83b55d2014-11-14 10:06:21 -0800280 data.writeInt32((int32_t)stream);
281 data.writeInt32((int32_t)session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700282 remote()->transact(RELEASE_OUTPUT, data, &reply);
283 }
284
Eric Laurentcaf7f482014-11-25 17:50:47 -0800285 virtual status_t getInputForAttr(const audio_attributes_t *attr,
286 audio_io_handle_t *input,
287 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700288 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700289 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800290 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800291 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600292 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700293 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800294 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700295 {
296 Parcel data, reply;
297 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentcaf7f482014-11-25 17:50:47 -0800298 if (attr == NULL) {
299 ALOGE("getInputForAttr NULL attr - shouldn't happen");
300 return BAD_VALUE;
301 }
302 if (input == NULL) {
303 ALOGE("getInputForAttr NULL input - shouldn't happen");
304 return BAD_VALUE;
305 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700306 if (selectedDeviceId == NULL) {
307 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
308 return BAD_VALUE;
309 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800310 if (portId == NULL) {
311 ALOGE("getInputForAttr NULL portId - shouldn't happen");
312 return BAD_VALUE;
313 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800314 data.write(attr, sizeof(audio_attributes_t));
Eric Laurenta54f1282017-07-01 19:39:32 -0700315 data.writeInt32(*input);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800316 data.writeInt32(session);
Eric Laurentb2379ba2016-05-23 17:42:12 -0700317 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700318 data.writeInt32(uid);
Eric Laurentfee19762018-01-29 18:44:13 -0800319 data.writeString16(opPackageName);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800320 data.write(config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -0700321 data.writeInt32(flags);
Eric Laurent9ae8c592017-06-22 17:17:09 -0700322 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800323 data.writeInt32(*portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800324 status_t status = remote()->transact(GET_INPUT_FOR_ATTR, data, &reply);
325 if (status != NO_ERROR) {
326 return status;
327 }
328 status = reply.readInt32();
329 if (status != NO_ERROR) {
330 return status;
331 }
332 *input = (audio_io_handle_t)reply.readInt32();
Eric Laurent9ae8c592017-06-22 17:17:09 -0700333 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800334 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800335 return NO_ERROR;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700336 }
337
Eric Laurentfee19762018-01-29 18:44:13 -0800338 virtual status_t startInput(audio_port_handle_t portId,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800339 bool *silenced)
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);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800344 data.writeInt32(*silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700345 remote()->transact(START_INPUT, data, &reply);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800346 status_t status = static_cast <status_t> (reply.readInt32());
347 *silenced = reply.readInt32() == 1;
348 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700349 }
350
Eric Laurentfee19762018-01-29 18:44:13 -0800351 virtual status_t stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700352 {
353 Parcel data, reply;
354 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800355 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700356 remote()->transact(STOP_INPUT, data, &reply);
357 return static_cast <status_t> (reply.readInt32());
358 }
359
Eric Laurentfee19762018-01-29 18:44:13 -0800360 virtual void releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700361 {
362 Parcel data, reply;
363 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800364 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700365 remote()->transact(RELEASE_INPUT, data, &reply);
366 }
367
Dima Zavinfce7a472011-04-19 22:30:36 -0700368 virtual status_t initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700369 int indexMin,
370 int indexMax)
371 {
372 Parcel data, reply;
373 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
374 data.writeInt32(static_cast <uint32_t>(stream));
375 data.writeInt32(indexMin);
376 data.writeInt32(indexMax);
377 remote()->transact(INIT_STREAM_VOLUME, data, &reply);
378 return static_cast <status_t> (reply.readInt32());
379 }
380
Eric Laurent83844cc2011-11-18 16:43:31 -0800381 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
382 int index,
383 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700384 {
385 Parcel data, reply;
386 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
387 data.writeInt32(static_cast <uint32_t>(stream));
388 data.writeInt32(index);
Eric Laurent83844cc2011-11-18 16:43:31 -0800389 data.writeInt32(static_cast <uint32_t>(device));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700390 remote()->transact(SET_STREAM_VOLUME, data, &reply);
391 return static_cast <status_t> (reply.readInt32());
392 }
393
Eric Laurent83844cc2011-11-18 16:43:31 -0800394 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
395 int *index,
396 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700397 {
398 Parcel data, reply;
399 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
400 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurent83844cc2011-11-18 16:43:31 -0800401 data.writeInt32(static_cast <uint32_t>(device));
402
Eric Laurentc2f1f072009-07-17 12:17:14 -0700403 remote()->transact(GET_STREAM_VOLUME, data, &reply);
404 int lIndex = reply.readInt32();
405 if (index) *index = lIndex;
406 return static_cast <status_t> (reply.readInt32());
407 }
Eric Laurentde070132010-07-13 04:45:46 -0700408
Dima Zavinfce7a472011-04-19 22:30:36 -0700409 virtual uint32_t getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700410 {
411 Parcel data, reply;
412 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
413 data.writeInt32(static_cast <uint32_t>(stream));
414 remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply);
415 return reply.readInt32();
416 }
417
Eric Laurent63742522012-03-08 13:42:42 -0800418 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800419 {
420 Parcel data, reply;
421 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
422 data.writeInt32(static_cast <uint32_t>(stream));
423 remote()->transact(GET_DEVICES_FOR_STREAM, data, &reply);
Eric Laurent63742522012-03-08 13:42:42 -0800424 return (audio_devices_t) reply.readInt32();
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800425 }
426
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700427 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700428 {
429 Parcel data, reply;
430 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
431 data.write(desc, sizeof(effect_descriptor_t));
432 remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply);
433 return static_cast <audio_io_handle_t> (reply.readInt32());
434 }
435
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700436 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700437 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700438 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800439 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700440 int id)
441 {
442 Parcel data, reply;
443 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
444 data.write(desc, sizeof(effect_descriptor_t));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700445 data.writeInt32(io);
Eric Laurentde070132010-07-13 04:45:46 -0700446 data.writeInt32(strategy);
447 data.writeInt32(session);
448 data.writeInt32(id);
449 remote()->transact(REGISTER_EFFECT, data, &reply);
450 return static_cast <status_t> (reply.readInt32());
451 }
452
453 virtual status_t unregisterEffect(int id)
454 {
455 Parcel data, reply;
456 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
457 data.writeInt32(id);
458 remote()->transact(UNREGISTER_EFFECT, data, &reply);
459 return static_cast <status_t> (reply.readInt32());
460 }
461
Eric Laurentdb7c0792011-08-10 10:37:50 -0700462 virtual status_t setEffectEnabled(int id, bool enabled)
463 {
464 Parcel data, reply;
465 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
466 data.writeInt32(id);
467 data.writeInt32(enabled);
468 remote()->transact(SET_EFFECT_ENABLED, data, &reply);
469 return static_cast <status_t> (reply.readInt32());
470 }
471
Glenn Kastenfff6d712012-01-12 16:38:12 -0800472 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800473 {
474 Parcel data, reply;
475 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Glenn Kastenfff6d712012-01-12 16:38:12 -0800476 data.writeInt32((int32_t) stream);
Eric Laurenteda6c362011-02-02 09:33:30 -0800477 data.writeInt32(inPastMs);
478 remote()->transact(IS_STREAM_ACTIVE, data, &reply);
479 return reply.readInt32();
480 }
Eric Laurent57dae992011-07-24 13:36:09 -0700481
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800482 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
483 {
484 Parcel data, reply;
485 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
486 data.writeInt32((int32_t) stream);
487 data.writeInt32(inPastMs);
488 remote()->transact(IS_STREAM_ACTIVE_REMOTELY, data, &reply);
489 return reply.readInt32();
490 }
491
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700492 virtual bool isSourceActive(audio_source_t source) const
493 {
494 Parcel data, reply;
495 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
496 data.writeInt32((int32_t) source);
497 remote()->transact(IS_SOURCE_ACTIVE, data, &reply);
498 return reply.readInt32();
499 }
500
Glenn Kastend848eb42016-03-08 13:42:11 -0800501 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700502 effect_descriptor_t *descriptors,
503 uint32_t *count)
504 {
505 if (descriptors == NULL || count == NULL) {
506 return BAD_VALUE;
507 }
508 Parcel data, reply;
509 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
510 data.writeInt32(audioSession);
511 data.writeInt32(*count);
512 status_t status = remote()->transact(QUERY_DEFAULT_PRE_PROCESSING, data, &reply);
513 if (status != NO_ERROR) {
514 return status;
515 }
516 status = static_cast <status_t> (reply.readInt32());
517 uint32_t retCount = reply.readInt32();
518 if (retCount != 0) {
519 uint32_t numDesc = (retCount < *count) ? retCount : *count;
520 reply.read(descriptors, sizeof(effect_descriptor_t) * numDesc);
521 }
522 *count = retCount;
523 return status;
524 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000525
526 virtual bool isOffloadSupported(const audio_offload_info_t& info)
527 {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100528 Parcel data, reply;
529 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
530 data.write(&info, sizeof(audio_offload_info_t));
531 remote()->transact(IS_OFFLOAD_SUPPORTED, data, &reply);
Eric Laurent203b1a12014-04-01 10:34:16 -0700532 return reply.readInt32();
533 }
534
535 virtual status_t listAudioPorts(audio_port_role_t role,
536 audio_port_type_t type,
537 unsigned int *num_ports,
538 struct audio_port *ports,
539 unsigned int *generation)
540 {
541 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
542 generation == NULL) {
543 return BAD_VALUE;
544 }
545 Parcel data, reply;
546 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
547 unsigned int numPortsReq = (ports == NULL) ? 0 : *num_ports;
548 data.writeInt32(role);
549 data.writeInt32(type);
550 data.writeInt32(numPortsReq);
551 status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply);
552 if (status == NO_ERROR) {
553 status = (status_t)reply.readInt32();
554 *num_ports = (unsigned int)reply.readInt32();
555 }
Eric Laurent203b1a12014-04-01 10:34:16 -0700556 if (status == NO_ERROR) {
557 if (numPortsReq > *num_ports) {
558 numPortsReq = *num_ports;
559 }
560 if (numPortsReq > 0) {
561 reply.read(ports, numPortsReq * sizeof(struct audio_port));
562 }
563 *generation = reply.readInt32();
564 }
565 return status;
566 }
567
568 virtual status_t getAudioPort(struct audio_port *port)
569 {
570 if (port == NULL) {
571 return BAD_VALUE;
572 }
573 Parcel data, reply;
574 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
575 data.write(port, sizeof(struct audio_port));
576 status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply);
577 if (status != NO_ERROR ||
578 (status = (status_t)reply.readInt32()) != NO_ERROR) {
579 return status;
580 }
581 reply.read(port, sizeof(struct audio_port));
582 return status;
583 }
584
585 virtual status_t createAudioPatch(const struct audio_patch *patch,
586 audio_patch_handle_t *handle)
587 {
588 if (patch == NULL || handle == NULL) {
589 return BAD_VALUE;
590 }
591 Parcel data, reply;
592 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
593 data.write(patch, sizeof(struct audio_patch));
594 data.write(handle, sizeof(audio_patch_handle_t));
595 status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply);
596 if (status != NO_ERROR ||
597 (status = (status_t)reply.readInt32()) != NO_ERROR) {
598 return status;
599 }
600 reply.read(handle, sizeof(audio_patch_handle_t));
601 return status;
602 }
603
604 virtual status_t releaseAudioPatch(audio_patch_handle_t handle)
605 {
606 Parcel data, reply;
607 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
608 data.write(&handle, sizeof(audio_patch_handle_t));
609 status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply);
610 if (status != NO_ERROR) {
611 status = (status_t)reply.readInt32();
612 }
613 return status;
614 }
615
616 virtual status_t listAudioPatches(unsigned int *num_patches,
617 struct audio_patch *patches,
618 unsigned int *generation)
619 {
620 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
621 generation == NULL) {
622 return BAD_VALUE;
623 }
624 Parcel data, reply;
625 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
626 unsigned int numPatchesReq = (patches == NULL) ? 0 : *num_patches;
627 data.writeInt32(numPatchesReq);
628 status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply);
629 if (status == NO_ERROR) {
630 status = (status_t)reply.readInt32();
631 *num_patches = (unsigned int)reply.readInt32();
632 }
633 if (status == NO_ERROR) {
634 if (numPatchesReq > *num_patches) {
635 numPatchesReq = *num_patches;
636 }
637 if (numPatchesReq > 0) {
638 reply.read(patches, numPatchesReq * sizeof(struct audio_patch));
639 }
640 *generation = reply.readInt32();
641 }
642 return status;
643 }
644
645 virtual status_t setAudioPortConfig(const struct audio_port_config *config)
646 {
647 if (config == NULL) {
648 return BAD_VALUE;
649 }
650 Parcel data, reply;
651 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
652 data.write(config, sizeof(struct audio_port_config));
653 status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply);
654 if (status != NO_ERROR) {
655 status = (status_t)reply.readInt32();
656 }
657 return status;
658 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700659
Eric Laurentb52c1522014-05-20 11:27:36 -0700660 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client)
661 {
662 Parcel data, reply;
663 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -0800664 data.writeStrongBinder(IInterface::asBinder(client));
Eric Laurentb52c1522014-05-20 11:27:36 -0700665 remote()->transact(REGISTER_CLIENT, data, &reply);
666 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700667
Eric Laurente8726fe2015-06-26 09:39:24 -0700668 virtual void setAudioPortCallbacksEnabled(bool enabled)
669 {
670 Parcel data, reply;
671 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
672 data.writeInt32(enabled ? 1 : 0);
673 remote()->transact(SET_AUDIO_PORT_CALLBACK_ENABLED, data, &reply);
674 }
675
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700676 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
677 audio_io_handle_t *ioHandle,
678 audio_devices_t *device)
679 {
680 if (session == NULL || ioHandle == NULL || device == NULL) {
681 return BAD_VALUE;
682 }
683 Parcel data, reply;
684 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
685 status_t status = remote()->transact(ACQUIRE_SOUNDTRIGGER_SESSION, data, &reply);
686 if (status != NO_ERROR) {
687 return status;
688 }
689 status = (status_t)reply.readInt32();
690 if (status == NO_ERROR) {
691 *session = (audio_session_t)reply.readInt32();
692 *ioHandle = (audio_io_handle_t)reply.readInt32();
693 *device = (audio_devices_t)reply.readInt32();
694 }
695 return status;
696 }
697
698 virtual status_t releaseSoundTriggerSession(audio_session_t session)
699 {
700 Parcel data, reply;
701 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
702 data.writeInt32(session);
703 status_t status = remote()->transact(RELEASE_SOUNDTRIGGER_SESSION, data, &reply);
704 if (status != NO_ERROR) {
705 return status;
706 }
707 return (status_t)reply.readInt32();
708 }
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700709
710 virtual audio_mode_t getPhoneState()
711 {
712 Parcel data, reply;
713 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
714 status_t status = remote()->transact(GET_PHONE_STATE, data, &reply);
715 if (status != NO_ERROR) {
716 return AUDIO_MODE_INVALID;
717 }
718 return (audio_mode_t)reply.readInt32();
719 }
Eric Laurentbaac1832014-12-01 17:52:59 -0800720
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700721 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800722 {
723 Parcel data, reply;
724 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
725 data.writeInt32(registration ? 1 : 0);
726 size_t size = mixes.size();
727 if (size > MAX_MIXES_PER_POLICY) {
728 size = MAX_MIXES_PER_POLICY;
729 }
730 size_t sizePosition = data.dataPosition();
731 data.writeInt32(size);
732 size_t finalSize = size;
733 for (size_t i = 0; i < size; i++) {
734 size_t position = data.dataPosition();
735 if (mixes[i].writeToParcel(&data) != NO_ERROR) {
736 data.setDataPosition(position);
737 finalSize--;
738 }
739 }
740 if (size != finalSize) {
741 size_t position = data.dataPosition();
742 data.setDataPosition(sizePosition);
743 data.writeInt32(finalSize);
744 data.setDataPosition(position);
745 }
746 status_t status = remote()->transact(REGISTER_POLICY_MIXES, data, &reply);
747 if (status == NO_ERROR) {
748 status = (status_t)reply.readInt32();
749 }
750 return status;
751 }
Eric Laurent554a2772015-04-10 11:29:24 -0700752
753 virtual status_t startAudioSource(const struct audio_port_config *source,
754 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -0700755 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700756 {
757 Parcel data, reply;
758 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
759 if (source == NULL || attributes == NULL || handle == NULL) {
760 return BAD_VALUE;
761 }
762 data.write(source, sizeof(struct audio_port_config));
763 data.write(attributes, sizeof(audio_attributes_t));
764 status_t status = remote()->transact(START_AUDIO_SOURCE, data, &reply);
765 if (status != NO_ERROR) {
766 return status;
767 }
768 status = (status_t)reply.readInt32();
769 if (status != NO_ERROR) {
770 return status;
771 }
Glenn Kasten559d4392016-03-29 13:42:57 -0700772 *handle = (audio_patch_handle_t)reply.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -0700773 return status;
774 }
775
Glenn Kasten559d4392016-03-29 13:42:57 -0700776 virtual status_t stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700777 {
778 Parcel data, reply;
779 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
780 data.writeInt32(handle);
781 status_t status = remote()->transact(STOP_AUDIO_SOURCE, data, &reply);
782 if (status != NO_ERROR) {
783 return status;
784 }
785 status = (status_t)reply.readInt32();
786 return status;
787 }
Andy Hung2ddee192015-12-18 17:34:44 -0800788
789 virtual status_t setMasterMono(bool mono)
790 {
791 Parcel data, reply;
792 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
793 data.writeInt32(static_cast<int32_t>(mono));
794 status_t status = remote()->transact(SET_MASTER_MONO, data, &reply);
795 if (status != NO_ERROR) {
796 return status;
797 }
798 return static_cast<status_t>(reply.readInt32());
799 }
800
801 virtual status_t getMasterMono(bool *mono)
802 {
803 if (mono == nullptr) {
804 return BAD_VALUE;
805 }
806 Parcel data, reply;
807 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
808
809 status_t status = remote()->transact(GET_MASTER_MONO, data, &reply);
810 if (status != NO_ERROR) {
811 return status;
812 }
813 status = static_cast<status_t>(reply.readInt32());
814 if (status == NO_ERROR) {
815 *mono = static_cast<bool>(reply.readInt32());
816 }
817 return status;
818 }
Eric Laurentac9cef52017-06-09 15:46:26 -0700819
820 virtual float getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
821 {
822 Parcel data, reply;
823 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
824 data.writeInt32(static_cast <int32_t>(stream));
825 data.writeInt32(static_cast <int32_t>(index));
826 data.writeUint32(static_cast <uint32_t>(device));
827 status_t status = remote()->transact(GET_STREAM_VOLUME_DB, data, &reply);
828 if (status != NO_ERROR) {
829 return NAN;
830 }
831 return reply.readFloat();
832 }
jiabin81772902018-04-02 17:52:27 -0700833
834 virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
835 audio_format_t *surroundFormats,
836 bool *surroundFormatsEnabled,
837 bool reported)
838 {
839 if (numSurroundFormats == NULL || (*numSurroundFormats != 0 &&
840 (surroundFormats == NULL || surroundFormatsEnabled == NULL))) {
841 return BAD_VALUE;
842 }
843 Parcel data, reply;
844 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
845 unsigned int numSurroundFormatsReq = *numSurroundFormats;
846 data.writeUint32(numSurroundFormatsReq);
847 data.writeBool(reported);
848 status_t status = remote()->transact(GET_SURROUND_FORMATS, data, &reply);
849 if (status == NO_ERROR && (status = (status_t)reply.readInt32()) == NO_ERROR) {
850 *numSurroundFormats = reply.readUint32();
851 }
852 if (status == NO_ERROR) {
853 if (numSurroundFormatsReq > *numSurroundFormats) {
854 numSurroundFormatsReq = *numSurroundFormats;
855 }
856 if (numSurroundFormatsReq > 0) {
857 status = reply.read(surroundFormats,
858 numSurroundFormatsReq * sizeof(audio_format_t));
859 if (status != NO_ERROR) {
860 return status;
861 }
862 status = reply.read(surroundFormatsEnabled,
863 numSurroundFormatsReq * sizeof(bool));
864 }
865 }
866 return status;
867 }
868
869 virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
870 {
871 Parcel data, reply;
872 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
873 data.writeInt32(audioFormat);
874 data.writeBool(enabled);
875 status_t status = remote()->transact(SET_SURROUND_FORMAT_ENABLED, data, &reply);
876 if (status != NO_ERROR) {
877 return status;
878 }
879 return reply.readInt32();
880 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700881};
882
883IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
884
885// ----------------------------------------------------------------------
886
Eric Laurentc2f1f072009-07-17 12:17:14 -0700887status_t BnAudioPolicyService::onTransact(
888 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
889{
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800890 // make sure transactions reserved to AudioFlinger do not come from other processes
891 switch (code) {
892 case START_OUTPUT:
893 case STOP_OUTPUT:
894 case RELEASE_OUTPUT:
895 case GET_INPUT_FOR_ATTR:
896 case START_INPUT:
897 case STOP_INPUT:
898 case RELEASE_INPUT:
899 case GET_STRATEGY_FOR_STREAM:
900 case GET_OUTPUT_FOR_EFFECT:
901 case REGISTER_EFFECT:
902 case UNREGISTER_EFFECT:
903 case SET_EFFECT_ENABLED:
904 case GET_OUTPUT_FOR_ATTR:
905 case ACQUIRE_SOUNDTRIGGER_SESSION:
906 case RELEASE_SOUNDTRIGGER_SESSION:
907 ALOGW("%s: transaction %d received from PID %d",
908 __func__, code, IPCThreadState::self()->getCallingPid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700909 // return status only for non void methods
910 switch (code) {
911 case RELEASE_OUTPUT:
912 case RELEASE_INPUT:
913 break;
914 default:
915 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
916 break;
917 }
918 return OK;
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800919 default:
920 break;
921 }
922
Eric Laurent4980df22018-01-26 18:04:09 -0800923 // make sure the following transactions come from system components
924 switch (code) {
925 case SET_DEVICE_CONNECTION_STATE:
926 case HANDLE_DEVICE_CONFIG_CHANGE:
927 case SET_PHONE_STATE:
Eric Laurente17378d2018-05-09 14:43:01 -0700928//FIXME: Allow SET_FORCE_USE calls from system apps until a better use case routing API is available
929// case SET_FORCE_USE:
Eric Laurent4980df22018-01-26 18:04:09 -0800930 case INIT_STREAM_VOLUME:
931 case SET_STREAM_VOLUME:
932 case REGISTER_POLICY_MIXES:
Eric Laurent10b71232018-04-13 18:14:44 -0700933 case SET_MASTER_MONO:
934 case START_AUDIO_SOURCE:
jiabin81772902018-04-02 17:52:27 -0700935 case STOP_AUDIO_SOURCE:
936 case GET_SURROUND_FORMATS:
937 case SET_SURROUND_FORMAT_ENABLED: {
Andy Hung4ef19fa2018-05-15 19:35:29 -0700938 if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
Eric Laurent4980df22018-01-26 18:04:09 -0800939 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
940 __func__, code, IPCThreadState::self()->getCallingPid(),
941 IPCThreadState::self()->getCallingUid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700942 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
943 return OK;
Eric Laurent4980df22018-01-26 18:04:09 -0800944 }
Eric Laurent96c7eed2018-03-26 17:57:01 -0700945 } break;
Eric Laurent4980df22018-01-26 18:04:09 -0800946 default:
947 break;
948 }
949
Eric Laurent39b09b52018-06-29 12:24:40 -0700950 std::string tag("IAudioPolicyService command " + std::to_string(code));
951 TimeCheck check(tag.c_str());
Eric Laurent3528c932018-02-23 17:17:22 -0800952
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700953 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700954 case SET_DEVICE_CONNECTION_STATE: {
955 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700956 audio_devices_t device =
957 static_cast <audio_devices_t>(data.readInt32());
958 audio_policy_dev_state_t state =
959 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700960 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -0800961 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800962 if (device_address == nullptr || device_name == nullptr) {
963 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
964 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
965 } else {
966 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
967 state,
968 device_address,
969 device_name)));
970 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700971 return NO_ERROR;
972 } break;
973
974 case GET_DEVICE_CONNECTION_STATE: {
975 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700976 audio_devices_t device =
977 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700978 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800979 if (device_address == nullptr) {
980 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
981 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
982 } else {
983 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
984 device_address)));
985 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700986 return NO_ERROR;
987 } break;
988
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800989 case HANDLE_DEVICE_CONFIG_CHANGE: {
990 CHECK_INTERFACE(IAudioPolicyService, data, reply);
991 audio_devices_t device =
992 static_cast <audio_devices_t>(data.readInt32());
993 const char *device_address = data.readCString();
994 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800995 if (device_address == nullptr || device_name == nullptr) {
996 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
997 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
998 } else {
999 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
1000 device_address,
1001 device_name)));
1002 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001003 return NO_ERROR;
1004 } break;
1005
Eric Laurentc2f1f072009-07-17 12:17:14 -07001006 case SET_PHONE_STATE: {
1007 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001008 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
1009 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001010 return NO_ERROR;
1011 } break;
1012
Eric Laurentc2f1f072009-07-17 12:17:14 -07001013 case SET_FORCE_USE: {
1014 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001015 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1016 data.readInt32());
Dima Zavinfce7a472011-04-19 22:30:36 -07001017 audio_policy_forced_cfg_t config =
1018 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001019 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
1020 return NO_ERROR;
1021 } break;
1022
1023 case GET_FORCE_USE: {
1024 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001025 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1026 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001027 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
1028 return NO_ERROR;
1029 } break;
1030
1031 case GET_OUTPUT: {
1032 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001033 audio_stream_type_t stream =
1034 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +00001035 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001036 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001037 return NO_ERROR;
1038 } break;
1039
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001040 case GET_OUTPUT_FOR_ATTR: {
1041 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001042 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -08001043 bool hasAttributes = data.readInt32() != 0;
1044 if (hasAttributes) {
1045 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001046 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -08001047 }
1048 audio_session_t session = (audio_session_t)data.readInt32();
1049 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
1050 bool hasStream = data.readInt32() != 0;
1051 if (hasStream) {
1052 stream = (audio_stream_type_t)data.readInt32();
1053 }
Nadav Bar766fb022018-01-07 12:18:03 +02001054 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001055 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001056 audio_config_t config;
1057 memset(&config, 0, sizeof(audio_config_t));
1058 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001059 audio_output_flags_t flags =
1060 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001061 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001062 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001063 audio_io_handle_t output = 0;
Eric Laurente83b55d2014-11-14 10:06:21 -08001064 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001065 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001066 &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001067 flags, &selectedDeviceId, &portId);
Eric Laurente83b55d2014-11-14 10:06:21 -08001068 reply->writeInt32(status);
1069 reply->writeInt32(output);
1070 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001071 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001072 reply->writeInt32(portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001073 return NO_ERROR;
1074 } break;
1075
Eric Laurentc2f1f072009-07-17 12:17:14 -07001076 case START_OUTPUT: {
1077 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001078 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurentebcb2542014-03-05 18:30:08 -08001079 audio_stream_type_t stream =
1080 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001081 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001082 reply->writeInt32(static_cast <uint32_t>(startOutput(output,
Eric Laurentebcb2542014-03-05 18:30:08 -08001083 stream,
Eric Laurentde070132010-07-13 04:45:46 -07001084 session)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001085 return NO_ERROR;
1086 } break;
1087
1088 case STOP_OUTPUT: {
1089 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001090 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurentebcb2542014-03-05 18:30:08 -08001091 audio_stream_type_t stream =
1092 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001093 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001094 reply->writeInt32(static_cast <uint32_t>(stopOutput(output,
Eric Laurentebcb2542014-03-05 18:30:08 -08001095 stream,
Eric Laurentde070132010-07-13 04:45:46 -07001096 session)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001097 return NO_ERROR;
1098 } break;
1099
1100 case RELEASE_OUTPUT: {
1101 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001102 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001103 audio_stream_type_t stream = (audio_stream_type_t)data.readInt32();
1104 audio_session_t session = (audio_session_t)data.readInt32();
1105 releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001106 return NO_ERROR;
1107 } break;
1108
Eric Laurentcaf7f482014-11-25 17:50:47 -08001109 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001110 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001111 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001112 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001113 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001114 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001115 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001116 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001117 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001118 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001119 audio_config_base_t config;
1120 memset(&config, 0, sizeof(audio_config_base_t));
1121 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001122 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001123 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001124 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001125 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001126 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001127 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001128 reply->writeInt32(status);
1129 if (status == NO_ERROR) {
1130 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001131 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001132 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001133 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001134 return NO_ERROR;
1135 } break;
1136
1137 case START_INPUT: {
1138 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001139 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001140 bool silenced = data.readInt32() == 1;
Eric Laurentfee19762018-01-29 18:44:13 -08001141 status_t status = startInput(portId, &silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001142 reply->writeInt32(static_cast <uint32_t>(status));
1143 reply->writeInt32(silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001144 return NO_ERROR;
1145 } break;
1146
1147 case STOP_INPUT: {
1148 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001149 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1150 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001151 return NO_ERROR;
1152 } break;
1153
1154 case RELEASE_INPUT: {
1155 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001156 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1157 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001158 return NO_ERROR;
1159 } break;
1160
1161 case INIT_STREAM_VOLUME: {
1162 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001163 audio_stream_type_t stream =
1164 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001165 int indexMin = data.readInt32();
1166 int indexMax = data.readInt32();
1167 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1168 return NO_ERROR;
1169 } break;
1170
1171 case SET_STREAM_VOLUME: {
1172 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001173 audio_stream_type_t stream =
1174 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001175 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001176 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1177 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1178 index,
1179 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001180 return NO_ERROR;
1181 } break;
1182
1183 case GET_STREAM_VOLUME: {
1184 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001185 audio_stream_type_t stream =
1186 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurent83844cc2011-11-18 16:43:31 -08001187 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001188 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001189 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001190 reply->writeInt32(index);
1191 reply->writeInt32(static_cast <uint32_t>(status));
1192 return NO_ERROR;
1193 } break;
1194
Eric Laurentde070132010-07-13 04:45:46 -07001195 case GET_STRATEGY_FOR_STREAM: {
1196 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001197 audio_stream_type_t stream =
1198 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentde070132010-07-13 04:45:46 -07001199 reply->writeInt32(getStrategyForStream(stream));
1200 return NO_ERROR;
1201 } break;
1202
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001203 case GET_DEVICES_FOR_STREAM: {
1204 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001205 audio_stream_type_t stream =
1206 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001207 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1208 return NO_ERROR;
1209 } break;
1210
Eric Laurentde070132010-07-13 04:45:46 -07001211 case GET_OUTPUT_FOR_EFFECT: {
1212 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001213 effect_descriptor_t desc = {};
1214 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1215 android_errorWriteLog(0x534e4554, "73126106");
1216 }
1217 (void)sanitizeEffectDescriptor(&desc);
Eric Laurentde070132010-07-13 04:45:46 -07001218 audio_io_handle_t output = getOutputForEffect(&desc);
1219 reply->writeInt32(static_cast <int>(output));
1220 return NO_ERROR;
1221 } break;
1222
1223 case REGISTER_EFFECT: {
1224 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001225 effect_descriptor_t desc = {};
1226 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1227 android_errorWriteLog(0x534e4554, "73126106");
1228 }
1229 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001230 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001231 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001232 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001233 int id = data.readInt32();
1234 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001235 io,
Eric Laurentde070132010-07-13 04:45:46 -07001236 strategy,
1237 session,
1238 id)));
1239 return NO_ERROR;
1240 } break;
1241
1242 case UNREGISTER_EFFECT: {
1243 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1244 int id = data.readInt32();
1245 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1246 return NO_ERROR;
1247 } break;
1248
Eric Laurentdb7c0792011-08-10 10:37:50 -07001249 case SET_EFFECT_ENABLED: {
1250 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1251 int id = data.readInt32();
1252 bool enabled = static_cast <bool>(data.readInt32());
1253 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1254 return NO_ERROR;
1255 } break;
1256
Eric Laurenteda6c362011-02-02 09:33:30 -08001257 case IS_STREAM_ACTIVE: {
1258 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001259 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001260 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001261 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001262 return NO_ERROR;
1263 } break;
1264
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001265 case IS_STREAM_ACTIVE_REMOTELY: {
1266 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1267 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1268 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001269 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001270 return NO_ERROR;
1271 } break;
1272
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001273 case IS_SOURCE_ACTIVE: {
1274 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1275 audio_source_t source = (audio_source_t) data.readInt32();
1276 reply->writeInt32( isSourceActive(source));
1277 return NO_ERROR;
1278 }
1279
Eric Laurent57dae992011-07-24 13:36:09 -07001280 case QUERY_DEFAULT_PRE_PROCESSING: {
1281 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001282 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001283 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001284 if (count > AudioEffect::kMaxPreProcessing) {
1285 count = AudioEffect::kMaxPreProcessing;
1286 }
Eric Laurent57dae992011-07-24 13:36:09 -07001287 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001288 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001289 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1290 reply->writeInt32(status);
1291 if (status != NO_ERROR && status != NO_MEMORY) {
1292 retCount = 0;
1293 }
1294 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001295 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001296 if (retCount < count) {
1297 count = retCount;
1298 }
1299 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1300 }
1301 delete[] descriptors;
1302 return status;
1303 }
1304
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001305 case IS_OFFLOAD_SUPPORTED: {
1306 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001307 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001308 data.read(&info, sizeof(audio_offload_info_t));
1309 bool isSupported = isOffloadSupported(info);
1310 reply->writeInt32(isSupported);
1311 return NO_ERROR;
1312 }
1313
Eric Laurent203b1a12014-04-01 10:34:16 -07001314 case LIST_AUDIO_PORTS: {
1315 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1316 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1317 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1318 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001319 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1320 numPortsReq = MAX_ITEMS_PER_LIST;
1321 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001322 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001323 struct audio_port *ports =
1324 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001325 if (ports == NULL) {
1326 reply->writeInt32(NO_MEMORY);
1327 reply->writeInt32(0);
1328 return NO_ERROR;
1329 }
1330 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001331 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1332 reply->writeInt32(status);
1333 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001334
1335 if (status == NO_ERROR) {
1336 if (numPortsReq > numPorts) {
1337 numPortsReq = numPorts;
1338 }
1339 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1340 reply->writeInt32(generation);
1341 }
1342 free(ports);
1343 return NO_ERROR;
1344 }
1345
1346 case GET_AUDIO_PORT: {
1347 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001348 struct audio_port port = {};
1349 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1350 ALOGE("b/23912202");
1351 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001352 status_t status = getAudioPort(&port);
1353 reply->writeInt32(status);
1354 if (status == NO_ERROR) {
1355 reply->write(&port, sizeof(struct audio_port));
1356 }
1357 return NO_ERROR;
1358 }
1359
1360 case CREATE_AUDIO_PATCH: {
1361 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001362 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001363 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001364 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001365 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1366 ALOGE("b/23912202");
1367 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001368 status_t status = createAudioPatch(&patch, &handle);
1369 reply->writeInt32(status);
1370 if (status == NO_ERROR) {
1371 reply->write(&handle, sizeof(audio_patch_handle_t));
1372 }
1373 return NO_ERROR;
1374 }
1375
1376 case RELEASE_AUDIO_PATCH: {
1377 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001378 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001379 data.read(&handle, sizeof(audio_patch_handle_t));
1380 status_t status = releaseAudioPatch(handle);
1381 reply->writeInt32(status);
1382 return NO_ERROR;
1383 }
1384
1385 case LIST_AUDIO_PATCHES: {
1386 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1387 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001388 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1389 numPatchesReq = MAX_ITEMS_PER_LIST;
1390 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001391 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001392 struct audio_patch *patches =
1393 (struct audio_patch *)calloc(numPatchesReq,
1394 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001395 if (patches == NULL) {
1396 reply->writeInt32(NO_MEMORY);
1397 reply->writeInt32(0);
1398 return NO_ERROR;
1399 }
1400 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001401 status_t status = listAudioPatches(&numPatches, patches, &generation);
1402 reply->writeInt32(status);
1403 reply->writeInt32(numPatches);
1404 if (status == NO_ERROR) {
1405 if (numPatchesReq > numPatches) {
1406 numPatchesReq = numPatches;
1407 }
1408 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1409 reply->writeInt32(generation);
1410 }
1411 free(patches);
1412 return NO_ERROR;
1413 }
1414
1415 case SET_AUDIO_PORT_CONFIG: {
1416 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001417 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001418 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001419 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001420 status_t status = setAudioPortConfig(&config);
1421 reply->writeInt32(status);
1422 return NO_ERROR;
1423 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001424
Eric Laurentb52c1522014-05-20 11:27:36 -07001425 case REGISTER_CLIENT: {
1426 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1427 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1428 data.readStrongBinder());
1429 registerClient(client);
1430 return NO_ERROR;
1431 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001432
Eric Laurente8726fe2015-06-26 09:39:24 -07001433 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1434 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1435 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1436 return NO_ERROR;
1437 } break;
1438
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001439 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1440 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1441 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1442 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001443 audio_session_t session = AUDIO_SESSION_NONE;
1444 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1445 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001446 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1447 reply->writeInt32(status);
1448 if (status == NO_ERROR) {
1449 reply->writeInt32(session);
1450 reply->writeInt32(ioHandle);
1451 reply->writeInt32(device);
1452 }
1453 return NO_ERROR;
1454 } break;
1455
1456 case RELEASE_SOUNDTRIGGER_SESSION: {
1457 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1458 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1459 data.readStrongBinder());
1460 audio_session_t session = (audio_session_t)data.readInt32();
1461 status_t status = releaseSoundTriggerSession(session);
1462 reply->writeInt32(status);
1463 return NO_ERROR;
1464 } break;
1465
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001466 case GET_PHONE_STATE: {
1467 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1468 reply->writeInt32((int32_t)getPhoneState());
1469 return NO_ERROR;
1470 } break;
1471
Eric Laurentbaac1832014-12-01 17:52:59 -08001472 case REGISTER_POLICY_MIXES: {
1473 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1474 bool registration = data.readInt32() == 1;
1475 Vector<AudioMix> mixes;
1476 size_t size = (size_t)data.readInt32();
1477 if (size > MAX_MIXES_PER_POLICY) {
1478 size = MAX_MIXES_PER_POLICY;
1479 }
1480 for (size_t i = 0; i < size; i++) {
1481 AudioMix mix;
1482 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1483 mixes.add(mix);
1484 }
1485 }
1486 status_t status = registerPolicyMixes(mixes, registration);
1487 reply->writeInt32(status);
1488 return NO_ERROR;
1489 } break;
1490
Eric Laurent554a2772015-04-10 11:29:24 -07001491 case START_AUDIO_SOURCE: {
1492 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001493 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001494 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001495 (void)sanitizeAudioPortConfig(&source);
1496 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001497 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001498 sanetizeAudioAttributes(&attributes);
Glenn Kasten559d4392016-03-29 13:42:57 -07001499 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent554a2772015-04-10 11:29:24 -07001500 status_t status = startAudioSource(&source, &attributes, &handle);
1501 reply->writeInt32(status);
1502 reply->writeInt32(handle);
1503 return NO_ERROR;
1504 } break;
1505
1506 case STOP_AUDIO_SOURCE: {
1507 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten559d4392016-03-29 13:42:57 -07001508 audio_patch_handle_t handle = (audio_patch_handle_t) data.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -07001509 status_t status = stopAudioSource(handle);
1510 reply->writeInt32(status);
1511 return NO_ERROR;
1512 } break;
1513
Andy Hung2ddee192015-12-18 17:34:44 -08001514 case SET_MASTER_MONO: {
1515 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1516 bool mono = static_cast<bool>(data.readInt32());
1517 status_t status = setMasterMono(mono);
1518 reply->writeInt32(status);
1519 return NO_ERROR;
1520 } break;
1521
1522 case GET_MASTER_MONO: {
1523 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1524 bool mono;
1525 status_t status = getMasterMono(&mono);
1526 reply->writeInt32(status);
1527 if (status == NO_ERROR) {
1528 reply->writeInt32(static_cast<int32_t>(mono));
1529 }
1530 return NO_ERROR;
1531 } break;
1532
Eric Laurentac9cef52017-06-09 15:46:26 -07001533 case GET_STREAM_VOLUME_DB: {
1534 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1535 audio_stream_type_t stream =
1536 static_cast <audio_stream_type_t>(data.readInt32());
1537 int index = static_cast <int>(data.readInt32());
1538 audio_devices_t device =
1539 static_cast <audio_devices_t>(data.readUint32());
1540 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1541 return NO_ERROR;
1542 }
1543
jiabin81772902018-04-02 17:52:27 -07001544 case GET_SURROUND_FORMATS: {
1545 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1546 unsigned int numSurroundFormatsReq = data.readUint32();
1547 if (numSurroundFormatsReq > MAX_ITEMS_PER_LIST) {
1548 numSurroundFormatsReq = MAX_ITEMS_PER_LIST;
1549 }
1550 bool reported = data.readBool();
1551 unsigned int numSurroundFormats = numSurroundFormatsReq;
1552 audio_format_t *surroundFormats = (audio_format_t *)calloc(
1553 numSurroundFormats, sizeof(audio_format_t));
1554 bool *surroundFormatsEnabled = (bool *)calloc(numSurroundFormats, sizeof(bool));
1555 if (numSurroundFormatsReq > 0 &&
1556 (surroundFormats == NULL || surroundFormatsEnabled == NULL)) {
1557 free(surroundFormats);
1558 free(surroundFormatsEnabled);
1559 reply->writeInt32(NO_MEMORY);
1560 return NO_ERROR;
1561 }
1562 status_t status = getSurroundFormats(
1563 &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1564 reply->writeInt32(status);
1565
1566 if (status == NO_ERROR) {
1567 reply->writeUint32(numSurroundFormats);
1568 if (numSurroundFormatsReq > numSurroundFormats) {
1569 numSurroundFormatsReq = numSurroundFormats;
1570 }
1571 reply->write(surroundFormats, numSurroundFormatsReq * sizeof(audio_format_t));
1572 reply->write(surroundFormatsEnabled, numSurroundFormatsReq * sizeof(bool));
1573 }
1574 free(surroundFormats);
1575 free(surroundFormatsEnabled);
1576 return NO_ERROR;
1577 }
1578
1579 case SET_SURROUND_FORMAT_ENABLED: {
1580 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1581 audio_format_t audioFormat = (audio_format_t) data.readInt32();
1582 bool enabled = data.readBool();
1583 status_t status = setSurroundFormatEnabled(audioFormat, enabled);
1584 reply->writeInt32(status);
1585 return NO_ERROR;
1586 }
1587
Eric Laurentc2f1f072009-07-17 12:17:14 -07001588 default:
1589 return BBinder::onTransact(code, data, reply, flags);
1590 }
1591}
1592
Andy Hungb0272092018-04-12 11:06:56 -07001593/** returns true if string overflow was prevented by zero termination */
1594template <size_t size>
1595static bool preventStringOverflow(char (&s)[size]) {
1596 if (strnlen(s, size) < size) return false;
1597 s[size - 1] = '\0';
1598 return true;
1599}
1600
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001601void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
1602{
1603 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
1604 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
1605 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
1606 }
1607 attr->tags[tagsMaxSize - 1] = '\0';
1608}
1609
Andy Hungb0272092018-04-12 11:06:56 -07001610/** returns BAD_VALUE if sanitization was required. */
1611status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
1612{
1613 if (preventStringOverflow(desc->name)
1614 | /* always */ preventStringOverflow(desc->implementor)) {
1615 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
1616 return BAD_VALUE;
1617 }
1618 return NO_ERROR;
1619}
1620
1621/** returns BAD_VALUE if sanitization was required. */
1622status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
1623{
1624 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
1625 preventStringOverflow(config->ext.device.address)) {
1626 return BAD_VALUE;
1627 }
1628 return NO_ERROR;
1629}
1630
Eric Laurentc2f1f072009-07-17 12:17:14 -07001631// ----------------------------------------------------------------------------
1632
Glenn Kasten40bc9062015-03-20 09:09:33 -07001633} // namespace android