blob: 79b3e0f21d5cbbb1ef8d3f08e682ea1b5823cc78 [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 Laurentf1047e82018-04-16 19:18:20 -070027#include <cutils/multiuser.h>
Eric Laurent74adca92014-11-05 12:15:36 -080028#include <media/AudioEffect.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070029#include <media/IAudioPolicyService.h>
Eric Laurent3528c932018-02-23 17:17:22 -080030#include <media/TimeCheck.h>
Eric Laurent4980df22018-01-26 18:04:09 -080031#include <private/android_filesystem_config.h>
Dima Zavin64760242011-05-11 14:15:23 -070032#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070033
Eric Laurentc2f1f072009-07-17 12:17:14 -070034namespace android {
35
36enum {
37 SET_DEVICE_CONNECTION_STATE = IBinder::FIRST_CALL_TRANSACTION,
38 GET_DEVICE_CONNECTION_STATE,
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -080039 HANDLE_DEVICE_CONFIG_CHANGE,
Eric Laurentc2f1f072009-07-17 12:17:14 -070040 SET_PHONE_STATE,
Glenn Kasten0b07b802012-01-18 14:56:06 -080041 SET_RINGER_MODE, // reserved, no longer used
Eric Laurentc2f1f072009-07-17 12:17:14 -070042 SET_FORCE_USE,
43 GET_FORCE_USE,
44 GET_OUTPUT,
45 START_OUTPUT,
46 STOP_OUTPUT,
47 RELEASE_OUTPUT,
Eric Laurentcaf7f482014-11-25 17:50:47 -080048 GET_INPUT_FOR_ATTR,
Eric Laurentc2f1f072009-07-17 12:17:14 -070049 START_INPUT,
50 STOP_INPUT,
51 RELEASE_INPUT,
52 INIT_STREAM_VOLUME,
53 SET_STREAM_VOLUME,
Eric Laurentde070132010-07-13 04:45:46 -070054 GET_STREAM_VOLUME,
55 GET_STRATEGY_FOR_STREAM,
56 GET_OUTPUT_FOR_EFFECT,
57 REGISTER_EFFECT,
Eric Laurenteda6c362011-02-02 09:33:30 -080058 UNREGISTER_EFFECT,
Glenn Kasten6b2718c2011-02-04 13:54:26 -080059 IS_STREAM_ACTIVE,
Jean-Michel Trivid7086032012-10-10 12:11:16 -070060 IS_SOURCE_ACTIVE,
Glenn Kasten6b2718c2011-02-04 13:54:26 -080061 GET_DEVICES_FOR_STREAM,
Eric Laurentdb7c0792011-08-10 10:37:50 -070062 QUERY_DEFAULT_PRE_PROCESSING,
Jean-Michel Trivi272ab542013-02-04 16:26:02 -080063 SET_EFFECT_ENABLED,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +000064 IS_STREAM_ACTIVE_REMOTELY,
Eric Laurent203b1a12014-04-01 10:34:16 -070065 IS_OFFLOAD_SUPPORTED,
66 LIST_AUDIO_PORTS,
67 GET_AUDIO_PORT,
68 CREATE_AUDIO_PATCH,
69 RELEASE_AUDIO_PATCH,
70 LIST_AUDIO_PATCHES,
Eric Laurentb52c1522014-05-20 11:27:36 -070071 SET_AUDIO_PORT_CONFIG,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -070072 REGISTER_CLIENT,
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070073 GET_OUTPUT_FOR_ATTR,
74 ACQUIRE_SOUNDTRIGGER_SESSION,
Eric Laurentbb6c9a02014-09-25 14:11:47 -070075 RELEASE_SOUNDTRIGGER_SESSION,
Eric Laurentbaac1832014-12-01 17:52:59 -080076 GET_PHONE_STATE,
77 REGISTER_POLICY_MIXES,
Eric Laurent554a2772015-04-10 11:29:24 -070078 START_AUDIO_SOURCE,
Eric Laurente8726fe2015-06-26 09:39:24 -070079 STOP_AUDIO_SOURCE,
80 SET_AUDIO_PORT_CALLBACK_ENABLED,
Andy Hung2ddee192015-12-18 17:34:44 -080081 SET_MASTER_MONO,
82 GET_MASTER_MONO,
Eric Laurentac9cef52017-06-09 15:46:26 -070083 GET_STREAM_VOLUME_DB
Eric Laurentc2f1f072009-07-17 12:17:14 -070084};
85
Eric Laurent1d670b12015-02-06 10:44:24 -080086#define MAX_ITEMS_PER_LIST 1024
87
Eric Laurentc2f1f072009-07-17 12:17:14 -070088class BpAudioPolicyService : public BpInterface<IAudioPolicyService>
89{
90public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070091 explicit BpAudioPolicyService(const sp<IBinder>& impl)
Eric Laurentc2f1f072009-07-17 12:17:14 -070092 : BpInterface<IAudioPolicyService>(impl)
93 {
94 }
95
96 virtual status_t setDeviceConnectionState(
Dima Zavinfce7a472011-04-19 22:30:36 -070097 audio_devices_t device,
98 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080099 const char *device_address,
100 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700101 {
102 Parcel data, reply;
103 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
104 data.writeInt32(static_cast <uint32_t>(device));
105 data.writeInt32(static_cast <uint32_t>(state));
106 data.writeCString(device_address);
Paul McLeane743a472015-01-28 11:07:31 -0800107 data.writeCString(device_name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700108 remote()->transact(SET_DEVICE_CONNECTION_STATE, data, &reply);
109 return static_cast <status_t> (reply.readInt32());
110 }
111
Dima Zavinfce7a472011-04-19 22:30:36 -0700112 virtual audio_policy_dev_state_t getDeviceConnectionState(
113 audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700114 const char *device_address)
115 {
116 Parcel data, reply;
117 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
118 data.writeInt32(static_cast <uint32_t>(device));
119 data.writeCString(device_address);
120 remote()->transact(GET_DEVICE_CONNECTION_STATE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700121 return static_cast <audio_policy_dev_state_t>(reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700122 }
123
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800124 virtual status_t handleDeviceConfigChange(audio_devices_t device,
125 const char *device_address,
126 const char *device_name)
127 {
128 Parcel data, reply;
129 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
130 data.writeInt32(static_cast <uint32_t>(device));
131 data.writeCString(device_address);
132 data.writeCString(device_name);
133 remote()->transact(HANDLE_DEVICE_CONFIG_CHANGE, data, &reply);
134 return static_cast <status_t> (reply.readInt32());
135 }
136
Glenn Kastenf78aee72012-01-04 11:00:47 -0800137 virtual status_t setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700138 {
139 Parcel data, reply;
140 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
141 data.writeInt32(state);
142 remote()->transact(SET_PHONE_STATE, data, &reply);
143 return static_cast <status_t> (reply.readInt32());
144 }
145
Dima Zavinfce7a472011-04-19 22:30:36 -0700146 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700147 {
148 Parcel data, reply;
149 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
150 data.writeInt32(static_cast <uint32_t>(usage));
151 data.writeInt32(static_cast <uint32_t>(config));
152 remote()->transact(SET_FORCE_USE, data, &reply);
153 return static_cast <status_t> (reply.readInt32());
154 }
155
Dima Zavinfce7a472011-04-19 22:30:36 -0700156 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700157 {
158 Parcel data, reply;
159 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
160 data.writeInt32(static_cast <uint32_t>(usage));
161 remote()->transact(GET_FORCE_USE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700162 return static_cast <audio_policy_forced_cfg_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700163 }
164
Eric Laurentf4e63452017-11-06 19:31:46 +0000165 virtual audio_io_handle_t getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700166 {
167 Parcel data, reply;
168 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
169 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700170 remote()->transact(GET_OUTPUT, data, &reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -0700171 return static_cast <audio_io_handle_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700172 }
173
Eric Laurente83b55d2014-11-14 10:06:21 -0800174 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
175 audio_io_handle_t *output,
176 audio_session_t session,
177 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200178 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700179 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800180 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800181 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700182 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800183 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700184 {
185 Parcel data, reply;
186 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
187 if (attr == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800188 if (stream == NULL) {
189 ALOGE("getOutputForAttr(): NULL audio attributes and stream type");
190 return BAD_VALUE;
191 }
192 if (*stream == AUDIO_STREAM_DEFAULT) {
193 ALOGE("getOutputForAttr unspecified stream type");
194 return BAD_VALUE;
195 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700196 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800197 if (output == NULL) {
198 ALOGE("getOutputForAttr NULL output - shouldn't happen");
199 return BAD_VALUE;
200 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700201 if (selectedDeviceId == NULL) {
202 ALOGE("getOutputForAttr NULL selectedDeviceId - shouldn't happen");
203 return BAD_VALUE;
204 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800205 if (portId == NULL) {
206 ALOGE("getOutputForAttr NULL portId - shouldn't happen");
207 return BAD_VALUE;
208 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800209 if (attr == NULL) {
210 data.writeInt32(0);
211 } else {
212 data.writeInt32(1);
213 data.write(attr, sizeof(audio_attributes_t));
214 }
215 data.writeInt32(session);
216 if (stream == NULL) {
217 data.writeInt32(0);
218 } else {
219 data.writeInt32(1);
220 data.writeInt32(*stream);
221 }
Nadav Bar766fb022018-01-07 12:18:03 +0200222 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700223 data.writeInt32(uid);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800224 data.write(config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700225 data.writeInt32(static_cast <uint32_t>(flags));
Eric Laurent9ae8c592017-06-22 17:17:09 -0700226 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800227 data.writeInt32(*portId);
Eric Laurente83b55d2014-11-14 10:06:21 -0800228 status_t status = remote()->transact(GET_OUTPUT_FOR_ATTR, data, &reply);
229 if (status != NO_ERROR) {
230 return status;
231 }
232 status = (status_t)reply.readInt32();
233 if (status != NO_ERROR) {
234 return status;
235 }
236 *output = (audio_io_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800237 audio_stream_type_t lStream = (audio_stream_type_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800238 if (stream != NULL) {
Eric Laurent20b9ef02016-12-05 11:03:16 -0800239 *stream = lStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800240 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700241 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800242 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800243 return status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700244 }
245
Eric Laurentde070132010-07-13 04:45:46 -0700246 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700247 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800248 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700249 {
250 Parcel data, reply;
251 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfa2877b2009-07-28 08:44:33 -0700252 data.writeInt32(output);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800253 data.writeInt32((int32_t) stream);
Glenn Kastend848eb42016-03-08 13:42:11 -0800254 data.writeInt32((int32_t) session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700255 remote()->transact(START_OUTPUT, data, &reply);
256 return static_cast <status_t> (reply.readInt32());
257 }
258
Eric Laurentde070132010-07-13 04:45:46 -0700259 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700260 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800261 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700262 {
263 Parcel data, reply;
264 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfa2877b2009-07-28 08:44:33 -0700265 data.writeInt32(output);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800266 data.writeInt32((int32_t) stream);
Glenn Kastend848eb42016-03-08 13:42:11 -0800267 data.writeInt32((int32_t) session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700268 remote()->transact(STOP_OUTPUT, data, &reply);
269 return static_cast <status_t> (reply.readInt32());
270 }
271
Eric Laurente83b55d2014-11-14 10:06:21 -0800272 virtual void releaseOutput(audio_io_handle_t output,
273 audio_stream_type_t stream,
274 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700275 {
276 Parcel data, reply;
277 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfa2877b2009-07-28 08:44:33 -0700278 data.writeInt32(output);
Eric Laurente83b55d2014-11-14 10:06:21 -0800279 data.writeInt32((int32_t)stream);
280 data.writeInt32((int32_t)session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700281 remote()->transact(RELEASE_OUTPUT, data, &reply);
282 }
283
Eric Laurentcaf7f482014-11-25 17:50:47 -0800284 virtual status_t getInputForAttr(const audio_attributes_t *attr,
285 audio_io_handle_t *input,
286 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700287 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700288 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800289 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800290 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600291 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700292 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800293 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700294 {
295 Parcel data, reply;
296 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentcaf7f482014-11-25 17:50:47 -0800297 if (attr == NULL) {
298 ALOGE("getInputForAttr NULL attr - shouldn't happen");
299 return BAD_VALUE;
300 }
301 if (input == NULL) {
302 ALOGE("getInputForAttr NULL input - shouldn't happen");
303 return BAD_VALUE;
304 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700305 if (selectedDeviceId == NULL) {
306 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
307 return BAD_VALUE;
308 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800309 if (portId == NULL) {
310 ALOGE("getInputForAttr NULL portId - shouldn't happen");
311 return BAD_VALUE;
312 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800313 data.write(attr, sizeof(audio_attributes_t));
Eric Laurenta54f1282017-07-01 19:39:32 -0700314 data.writeInt32(*input);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800315 data.writeInt32(session);
Eric Laurentb2379ba2016-05-23 17:42:12 -0700316 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700317 data.writeInt32(uid);
Eric Laurentfee19762018-01-29 18:44:13 -0800318 data.writeString16(opPackageName);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800319 data.write(config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -0700320 data.writeInt32(flags);
Eric Laurent9ae8c592017-06-22 17:17:09 -0700321 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800322 data.writeInt32(*portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800323 status_t status = remote()->transact(GET_INPUT_FOR_ATTR, data, &reply);
324 if (status != NO_ERROR) {
325 return status;
326 }
327 status = reply.readInt32();
328 if (status != NO_ERROR) {
329 return status;
330 }
331 *input = (audio_io_handle_t)reply.readInt32();
Eric Laurent9ae8c592017-06-22 17:17:09 -0700332 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800333 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800334 return NO_ERROR;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700335 }
336
Eric Laurentfee19762018-01-29 18:44:13 -0800337 virtual status_t startInput(audio_port_handle_t portId,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800338 bool *silenced)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700339 {
340 Parcel data, reply;
341 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800342 data.writeInt32(portId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800343 data.writeInt32(*silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700344 remote()->transact(START_INPUT, data, &reply);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800345 status_t status = static_cast <status_t> (reply.readInt32());
346 *silenced = reply.readInt32() == 1;
347 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700348 }
349
Eric Laurentfee19762018-01-29 18:44:13 -0800350 virtual status_t stopInput(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(STOP_INPUT, data, &reply);
356 return static_cast <status_t> (reply.readInt32());
357 }
358
Eric Laurentfee19762018-01-29 18:44:13 -0800359 virtual void releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700360 {
361 Parcel data, reply;
362 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800363 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700364 remote()->transact(RELEASE_INPUT, data, &reply);
365 }
366
Dima Zavinfce7a472011-04-19 22:30:36 -0700367 virtual status_t initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700368 int indexMin,
369 int indexMax)
370 {
371 Parcel data, reply;
372 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
373 data.writeInt32(static_cast <uint32_t>(stream));
374 data.writeInt32(indexMin);
375 data.writeInt32(indexMax);
376 remote()->transact(INIT_STREAM_VOLUME, data, &reply);
377 return static_cast <status_t> (reply.readInt32());
378 }
379
Eric Laurent83844cc2011-11-18 16:43:31 -0800380 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
381 int index,
382 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700383 {
384 Parcel data, reply;
385 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
386 data.writeInt32(static_cast <uint32_t>(stream));
387 data.writeInt32(index);
Eric Laurent83844cc2011-11-18 16:43:31 -0800388 data.writeInt32(static_cast <uint32_t>(device));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700389 remote()->transact(SET_STREAM_VOLUME, data, &reply);
390 return static_cast <status_t> (reply.readInt32());
391 }
392
Eric Laurent83844cc2011-11-18 16:43:31 -0800393 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
394 int *index,
395 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700396 {
397 Parcel data, reply;
398 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
399 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurent83844cc2011-11-18 16:43:31 -0800400 data.writeInt32(static_cast <uint32_t>(device));
401
Eric Laurentc2f1f072009-07-17 12:17:14 -0700402 remote()->transact(GET_STREAM_VOLUME, data, &reply);
403 int lIndex = reply.readInt32();
404 if (index) *index = lIndex;
405 return static_cast <status_t> (reply.readInt32());
406 }
Eric Laurentde070132010-07-13 04:45:46 -0700407
Dima Zavinfce7a472011-04-19 22:30:36 -0700408 virtual uint32_t getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700409 {
410 Parcel data, reply;
411 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
412 data.writeInt32(static_cast <uint32_t>(stream));
413 remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply);
414 return reply.readInt32();
415 }
416
Eric Laurent63742522012-03-08 13:42:42 -0800417 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800418 {
419 Parcel data, reply;
420 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
421 data.writeInt32(static_cast <uint32_t>(stream));
422 remote()->transact(GET_DEVICES_FOR_STREAM, data, &reply);
Eric Laurent63742522012-03-08 13:42:42 -0800423 return (audio_devices_t) reply.readInt32();
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800424 }
425
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700426 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700427 {
428 Parcel data, reply;
429 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
430 data.write(desc, sizeof(effect_descriptor_t));
431 remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply);
432 return static_cast <audio_io_handle_t> (reply.readInt32());
433 }
434
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700435 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700436 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700437 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800438 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700439 int id)
440 {
441 Parcel data, reply;
442 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
443 data.write(desc, sizeof(effect_descriptor_t));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700444 data.writeInt32(io);
Eric Laurentde070132010-07-13 04:45:46 -0700445 data.writeInt32(strategy);
446 data.writeInt32(session);
447 data.writeInt32(id);
448 remote()->transact(REGISTER_EFFECT, data, &reply);
449 return static_cast <status_t> (reply.readInt32());
450 }
451
452 virtual status_t unregisterEffect(int id)
453 {
454 Parcel data, reply;
455 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
456 data.writeInt32(id);
457 remote()->transact(UNREGISTER_EFFECT, data, &reply);
458 return static_cast <status_t> (reply.readInt32());
459 }
460
Eric Laurentdb7c0792011-08-10 10:37:50 -0700461 virtual status_t setEffectEnabled(int id, bool enabled)
462 {
463 Parcel data, reply;
464 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
465 data.writeInt32(id);
466 data.writeInt32(enabled);
467 remote()->transact(SET_EFFECT_ENABLED, data, &reply);
468 return static_cast <status_t> (reply.readInt32());
469 }
470
Glenn Kastenfff6d712012-01-12 16:38:12 -0800471 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800472 {
473 Parcel data, reply;
474 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Glenn Kastenfff6d712012-01-12 16:38:12 -0800475 data.writeInt32((int32_t) stream);
Eric Laurenteda6c362011-02-02 09:33:30 -0800476 data.writeInt32(inPastMs);
477 remote()->transact(IS_STREAM_ACTIVE, data, &reply);
478 return reply.readInt32();
479 }
Eric Laurent57dae992011-07-24 13:36:09 -0700480
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800481 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
482 {
483 Parcel data, reply;
484 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
485 data.writeInt32((int32_t) stream);
486 data.writeInt32(inPastMs);
487 remote()->transact(IS_STREAM_ACTIVE_REMOTELY, data, &reply);
488 return reply.readInt32();
489 }
490
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700491 virtual bool isSourceActive(audio_source_t source) const
492 {
493 Parcel data, reply;
494 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
495 data.writeInt32((int32_t) source);
496 remote()->transact(IS_SOURCE_ACTIVE, data, &reply);
497 return reply.readInt32();
498 }
499
Glenn Kastend848eb42016-03-08 13:42:11 -0800500 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700501 effect_descriptor_t *descriptors,
502 uint32_t *count)
503 {
504 if (descriptors == NULL || count == NULL) {
505 return BAD_VALUE;
506 }
507 Parcel data, reply;
508 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
509 data.writeInt32(audioSession);
510 data.writeInt32(*count);
511 status_t status = remote()->transact(QUERY_DEFAULT_PRE_PROCESSING, data, &reply);
512 if (status != NO_ERROR) {
513 return status;
514 }
515 status = static_cast <status_t> (reply.readInt32());
516 uint32_t retCount = reply.readInt32();
517 if (retCount != 0) {
518 uint32_t numDesc = (retCount < *count) ? retCount : *count;
519 reply.read(descriptors, sizeof(effect_descriptor_t) * numDesc);
520 }
521 *count = retCount;
522 return status;
523 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000524
525 virtual bool isOffloadSupported(const audio_offload_info_t& info)
526 {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100527 Parcel data, reply;
528 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
529 data.write(&info, sizeof(audio_offload_info_t));
530 remote()->transact(IS_OFFLOAD_SUPPORTED, data, &reply);
Eric Laurent203b1a12014-04-01 10:34:16 -0700531 return reply.readInt32();
532 }
533
534 virtual status_t listAudioPorts(audio_port_role_t role,
535 audio_port_type_t type,
536 unsigned int *num_ports,
537 struct audio_port *ports,
538 unsigned int *generation)
539 {
540 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
541 generation == NULL) {
542 return BAD_VALUE;
543 }
544 Parcel data, reply;
545 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
546 unsigned int numPortsReq = (ports == NULL) ? 0 : *num_ports;
547 data.writeInt32(role);
548 data.writeInt32(type);
549 data.writeInt32(numPortsReq);
550 status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply);
551 if (status == NO_ERROR) {
552 status = (status_t)reply.readInt32();
553 *num_ports = (unsigned int)reply.readInt32();
554 }
Eric Laurent203b1a12014-04-01 10:34:16 -0700555 if (status == NO_ERROR) {
556 if (numPortsReq > *num_ports) {
557 numPortsReq = *num_ports;
558 }
559 if (numPortsReq > 0) {
560 reply.read(ports, numPortsReq * sizeof(struct audio_port));
561 }
562 *generation = reply.readInt32();
563 }
564 return status;
565 }
566
567 virtual status_t getAudioPort(struct audio_port *port)
568 {
569 if (port == NULL) {
570 return BAD_VALUE;
571 }
572 Parcel data, reply;
573 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
574 data.write(port, sizeof(struct audio_port));
575 status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply);
576 if (status != NO_ERROR ||
577 (status = (status_t)reply.readInt32()) != NO_ERROR) {
578 return status;
579 }
580 reply.read(port, sizeof(struct audio_port));
581 return status;
582 }
583
584 virtual status_t createAudioPatch(const struct audio_patch *patch,
585 audio_patch_handle_t *handle)
586 {
587 if (patch == NULL || handle == NULL) {
588 return BAD_VALUE;
589 }
590 Parcel data, reply;
591 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
592 data.write(patch, sizeof(struct audio_patch));
593 data.write(handle, sizeof(audio_patch_handle_t));
594 status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply);
595 if (status != NO_ERROR ||
596 (status = (status_t)reply.readInt32()) != NO_ERROR) {
597 return status;
598 }
599 reply.read(handle, sizeof(audio_patch_handle_t));
600 return status;
601 }
602
603 virtual status_t releaseAudioPatch(audio_patch_handle_t handle)
604 {
605 Parcel data, reply;
606 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
607 data.write(&handle, sizeof(audio_patch_handle_t));
608 status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply);
609 if (status != NO_ERROR) {
610 status = (status_t)reply.readInt32();
611 }
612 return status;
613 }
614
615 virtual status_t listAudioPatches(unsigned int *num_patches,
616 struct audio_patch *patches,
617 unsigned int *generation)
618 {
619 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
620 generation == NULL) {
621 return BAD_VALUE;
622 }
623 Parcel data, reply;
624 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
625 unsigned int numPatchesReq = (patches == NULL) ? 0 : *num_patches;
626 data.writeInt32(numPatchesReq);
627 status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply);
628 if (status == NO_ERROR) {
629 status = (status_t)reply.readInt32();
630 *num_patches = (unsigned int)reply.readInt32();
631 }
632 if (status == NO_ERROR) {
633 if (numPatchesReq > *num_patches) {
634 numPatchesReq = *num_patches;
635 }
636 if (numPatchesReq > 0) {
637 reply.read(patches, numPatchesReq * sizeof(struct audio_patch));
638 }
639 *generation = reply.readInt32();
640 }
641 return status;
642 }
643
644 virtual status_t setAudioPortConfig(const struct audio_port_config *config)
645 {
646 if (config == NULL) {
647 return BAD_VALUE;
648 }
649 Parcel data, reply;
650 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
651 data.write(config, sizeof(struct audio_port_config));
652 status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply);
653 if (status != NO_ERROR) {
654 status = (status_t)reply.readInt32();
655 }
656 return status;
657 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700658
Eric Laurentb52c1522014-05-20 11:27:36 -0700659 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client)
660 {
661 Parcel data, reply;
662 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -0800663 data.writeStrongBinder(IInterface::asBinder(client));
Eric Laurentb52c1522014-05-20 11:27:36 -0700664 remote()->transact(REGISTER_CLIENT, data, &reply);
665 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700666
Eric Laurente8726fe2015-06-26 09:39:24 -0700667 virtual void setAudioPortCallbacksEnabled(bool enabled)
668 {
669 Parcel data, reply;
670 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
671 data.writeInt32(enabled ? 1 : 0);
672 remote()->transact(SET_AUDIO_PORT_CALLBACK_ENABLED, data, &reply);
673 }
674
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700675 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
676 audio_io_handle_t *ioHandle,
677 audio_devices_t *device)
678 {
679 if (session == NULL || ioHandle == NULL || device == NULL) {
680 return BAD_VALUE;
681 }
682 Parcel data, reply;
683 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
684 status_t status = remote()->transact(ACQUIRE_SOUNDTRIGGER_SESSION, data, &reply);
685 if (status != NO_ERROR) {
686 return status;
687 }
688 status = (status_t)reply.readInt32();
689 if (status == NO_ERROR) {
690 *session = (audio_session_t)reply.readInt32();
691 *ioHandle = (audio_io_handle_t)reply.readInt32();
692 *device = (audio_devices_t)reply.readInt32();
693 }
694 return status;
695 }
696
697 virtual status_t releaseSoundTriggerSession(audio_session_t session)
698 {
699 Parcel data, reply;
700 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
701 data.writeInt32(session);
702 status_t status = remote()->transact(RELEASE_SOUNDTRIGGER_SESSION, data, &reply);
703 if (status != NO_ERROR) {
704 return status;
705 }
706 return (status_t)reply.readInt32();
707 }
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700708
709 virtual audio_mode_t getPhoneState()
710 {
711 Parcel data, reply;
712 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
713 status_t status = remote()->transact(GET_PHONE_STATE, data, &reply);
714 if (status != NO_ERROR) {
715 return AUDIO_MODE_INVALID;
716 }
717 return (audio_mode_t)reply.readInt32();
718 }
Eric Laurentbaac1832014-12-01 17:52:59 -0800719
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700720 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800721 {
722 Parcel data, reply;
723 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
724 data.writeInt32(registration ? 1 : 0);
725 size_t size = mixes.size();
726 if (size > MAX_MIXES_PER_POLICY) {
727 size = MAX_MIXES_PER_POLICY;
728 }
729 size_t sizePosition = data.dataPosition();
730 data.writeInt32(size);
731 size_t finalSize = size;
732 for (size_t i = 0; i < size; i++) {
733 size_t position = data.dataPosition();
734 if (mixes[i].writeToParcel(&data) != NO_ERROR) {
735 data.setDataPosition(position);
736 finalSize--;
737 }
738 }
739 if (size != finalSize) {
740 size_t position = data.dataPosition();
741 data.setDataPosition(sizePosition);
742 data.writeInt32(finalSize);
743 data.setDataPosition(position);
744 }
745 status_t status = remote()->transact(REGISTER_POLICY_MIXES, data, &reply);
746 if (status == NO_ERROR) {
747 status = (status_t)reply.readInt32();
748 }
749 return status;
750 }
Eric Laurent554a2772015-04-10 11:29:24 -0700751
752 virtual status_t startAudioSource(const struct audio_port_config *source,
753 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -0700754 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700755 {
756 Parcel data, reply;
757 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
758 if (source == NULL || attributes == NULL || handle == NULL) {
759 return BAD_VALUE;
760 }
761 data.write(source, sizeof(struct audio_port_config));
762 data.write(attributes, sizeof(audio_attributes_t));
763 status_t status = remote()->transact(START_AUDIO_SOURCE, data, &reply);
764 if (status != NO_ERROR) {
765 return status;
766 }
767 status = (status_t)reply.readInt32();
768 if (status != NO_ERROR) {
769 return status;
770 }
Glenn Kasten559d4392016-03-29 13:42:57 -0700771 *handle = (audio_patch_handle_t)reply.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -0700772 return status;
773 }
774
Glenn Kasten559d4392016-03-29 13:42:57 -0700775 virtual status_t stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700776 {
777 Parcel data, reply;
778 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
779 data.writeInt32(handle);
780 status_t status = remote()->transact(STOP_AUDIO_SOURCE, data, &reply);
781 if (status != NO_ERROR) {
782 return status;
783 }
784 status = (status_t)reply.readInt32();
785 return status;
786 }
Andy Hung2ddee192015-12-18 17:34:44 -0800787
788 virtual status_t setMasterMono(bool mono)
789 {
790 Parcel data, reply;
791 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
792 data.writeInt32(static_cast<int32_t>(mono));
793 status_t status = remote()->transact(SET_MASTER_MONO, data, &reply);
794 if (status != NO_ERROR) {
795 return status;
796 }
797 return static_cast<status_t>(reply.readInt32());
798 }
799
800 virtual status_t getMasterMono(bool *mono)
801 {
802 if (mono == nullptr) {
803 return BAD_VALUE;
804 }
805 Parcel data, reply;
806 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
807
808 status_t status = remote()->transact(GET_MASTER_MONO, data, &reply);
809 if (status != NO_ERROR) {
810 return status;
811 }
812 status = static_cast<status_t>(reply.readInt32());
813 if (status == NO_ERROR) {
814 *mono = static_cast<bool>(reply.readInt32());
815 }
816 return status;
817 }
Eric Laurentac9cef52017-06-09 15:46:26 -0700818
819 virtual float getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
820 {
821 Parcel data, reply;
822 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
823 data.writeInt32(static_cast <int32_t>(stream));
824 data.writeInt32(static_cast <int32_t>(index));
825 data.writeUint32(static_cast <uint32_t>(device));
826 status_t status = remote()->transact(GET_STREAM_VOLUME_DB, data, &reply);
827 if (status != NO_ERROR) {
828 return NAN;
829 }
830 return reply.readFloat();
831 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700832};
833
834IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
835
836// ----------------------------------------------------------------------
837
Eric Laurentc2f1f072009-07-17 12:17:14 -0700838status_t BnAudioPolicyService::onTransact(
839 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
840{
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800841 // make sure transactions reserved to AudioFlinger do not come from other processes
842 switch (code) {
843 case START_OUTPUT:
844 case STOP_OUTPUT:
845 case RELEASE_OUTPUT:
846 case GET_INPUT_FOR_ATTR:
847 case START_INPUT:
848 case STOP_INPUT:
849 case RELEASE_INPUT:
850 case GET_STRATEGY_FOR_STREAM:
851 case GET_OUTPUT_FOR_EFFECT:
852 case REGISTER_EFFECT:
853 case UNREGISTER_EFFECT:
854 case SET_EFFECT_ENABLED:
855 case GET_OUTPUT_FOR_ATTR:
856 case ACQUIRE_SOUNDTRIGGER_SESSION:
857 case RELEASE_SOUNDTRIGGER_SESSION:
858 ALOGW("%s: transaction %d received from PID %d",
859 __func__, code, IPCThreadState::self()->getCallingPid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700860 // return status only for non void methods
861 switch (code) {
862 case RELEASE_OUTPUT:
863 case RELEASE_INPUT:
864 break;
865 default:
866 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
867 break;
868 }
869 return OK;
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800870 default:
871 break;
872 }
873
Eric Laurent4980df22018-01-26 18:04:09 -0800874 // make sure the following transactions come from system components
875 switch (code) {
876 case SET_DEVICE_CONNECTION_STATE:
877 case HANDLE_DEVICE_CONFIG_CHANGE:
878 case SET_PHONE_STATE:
Eric Laurent4980df22018-01-26 18:04:09 -0800879 case SET_FORCE_USE:
880 case INIT_STREAM_VOLUME:
881 case SET_STREAM_VOLUME:
882 case REGISTER_POLICY_MIXES:
Eric Laurent10b71232018-04-13 18:14:44 -0700883 case SET_MASTER_MONO:
884 case START_AUDIO_SOURCE:
885 case STOP_AUDIO_SOURCE: {
Eric Laurentf1047e82018-04-16 19:18:20 -0700886 if (multiuser_get_app_id(IPCThreadState::self()->getCallingUid()) >= AID_APP_START) {
Eric Laurent4980df22018-01-26 18:04:09 -0800887 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
888 __func__, code, IPCThreadState::self()->getCallingPid(),
889 IPCThreadState::self()->getCallingUid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700890 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
891 return OK;
Eric Laurent4980df22018-01-26 18:04:09 -0800892 }
Eric Laurent96c7eed2018-03-26 17:57:01 -0700893 } break;
Eric Laurent4980df22018-01-26 18:04:09 -0800894 default:
895 break;
896 }
897
Eric Laurentda6a5a82018-05-02 18:47:34 -0700898 TimeCheck check("IAudioPolicyService");
Eric Laurent3528c932018-02-23 17:17:22 -0800899
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700900 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700901 case SET_DEVICE_CONNECTION_STATE: {
902 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700903 audio_devices_t device =
904 static_cast <audio_devices_t>(data.readInt32());
905 audio_policy_dev_state_t state =
906 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700907 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -0800908 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800909 if (device_address == nullptr || device_name == nullptr) {
910 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
911 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
912 } else {
913 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
914 state,
915 device_address,
916 device_name)));
917 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700918 return NO_ERROR;
919 } break;
920
921 case GET_DEVICE_CONNECTION_STATE: {
922 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700923 audio_devices_t device =
924 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700925 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800926 if (device_address == nullptr) {
927 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
928 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
929 } else {
930 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
931 device_address)));
932 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700933 return NO_ERROR;
934 } break;
935
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800936 case HANDLE_DEVICE_CONFIG_CHANGE: {
937 CHECK_INTERFACE(IAudioPolicyService, data, reply);
938 audio_devices_t device =
939 static_cast <audio_devices_t>(data.readInt32());
940 const char *device_address = data.readCString();
941 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800942 if (device_address == nullptr || device_name == nullptr) {
943 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
944 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
945 } else {
946 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
947 device_address,
948 device_name)));
949 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800950 return NO_ERROR;
951 } break;
952
Eric Laurentc2f1f072009-07-17 12:17:14 -0700953 case SET_PHONE_STATE: {
954 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700955 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
956 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700957 return NO_ERROR;
958 } break;
959
Eric Laurentc2f1f072009-07-17 12:17:14 -0700960 case SET_FORCE_USE: {
961 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700962 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
963 data.readInt32());
Dima Zavinfce7a472011-04-19 22:30:36 -0700964 audio_policy_forced_cfg_t config =
965 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700966 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
967 return NO_ERROR;
968 } break;
969
970 case GET_FORCE_USE: {
971 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700972 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
973 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700974 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
975 return NO_ERROR;
976 } break;
977
978 case GET_OUTPUT: {
979 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700980 audio_stream_type_t stream =
981 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +0000982 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -0700983 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700984 return NO_ERROR;
985 } break;
986
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700987 case GET_OUTPUT_FOR_ATTR: {
988 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -0700989 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -0800990 bool hasAttributes = data.readInt32() != 0;
991 if (hasAttributes) {
992 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -0800993 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -0800994 }
995 audio_session_t session = (audio_session_t)data.readInt32();
996 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
997 bool hasStream = data.readInt32() != 0;
998 if (hasStream) {
999 stream = (audio_stream_type_t)data.readInt32();
1000 }
Nadav Bar766fb022018-01-07 12:18:03 +02001001 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001002 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001003 audio_config_t config;
1004 memset(&config, 0, sizeof(audio_config_t));
1005 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001006 audio_output_flags_t flags =
1007 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001008 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001009 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001010 audio_io_handle_t output = 0;
Eric Laurente83b55d2014-11-14 10:06:21 -08001011 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001012 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001013 &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001014 flags, &selectedDeviceId, &portId);
Eric Laurente83b55d2014-11-14 10:06:21 -08001015 reply->writeInt32(status);
1016 reply->writeInt32(output);
1017 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001018 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001019 reply->writeInt32(portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001020 return NO_ERROR;
1021 } break;
1022
Eric Laurentc2f1f072009-07-17 12:17:14 -07001023 case START_OUTPUT: {
1024 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001025 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurentebcb2542014-03-05 18:30:08 -08001026 audio_stream_type_t stream =
1027 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001028 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001029 reply->writeInt32(static_cast <uint32_t>(startOutput(output,
Eric Laurentebcb2542014-03-05 18:30:08 -08001030 stream,
Eric Laurentde070132010-07-13 04:45:46 -07001031 session)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001032 return NO_ERROR;
1033 } break;
1034
1035 case STOP_OUTPUT: {
1036 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001037 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurentebcb2542014-03-05 18:30:08 -08001038 audio_stream_type_t stream =
1039 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001040 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001041 reply->writeInt32(static_cast <uint32_t>(stopOutput(output,
Eric Laurentebcb2542014-03-05 18:30:08 -08001042 stream,
Eric Laurentde070132010-07-13 04:45:46 -07001043 session)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001044 return NO_ERROR;
1045 } break;
1046
1047 case RELEASE_OUTPUT: {
1048 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001049 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001050 audio_stream_type_t stream = (audio_stream_type_t)data.readInt32();
1051 audio_session_t session = (audio_session_t)data.readInt32();
1052 releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001053 return NO_ERROR;
1054 } break;
1055
Eric Laurentcaf7f482014-11-25 17:50:47 -08001056 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001057 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001058 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001059 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001060 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001061 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001062 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001063 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001064 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001065 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001066 audio_config_base_t config;
1067 memset(&config, 0, sizeof(audio_config_base_t));
1068 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001069 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001070 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001071 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001072 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001073 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001074 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001075 reply->writeInt32(status);
1076 if (status == NO_ERROR) {
1077 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001078 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001079 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001080 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001081 return NO_ERROR;
1082 } break;
1083
1084 case START_INPUT: {
1085 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001086 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001087 bool silenced = data.readInt32() == 1;
Eric Laurentfee19762018-01-29 18:44:13 -08001088 status_t status = startInput(portId, &silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001089 reply->writeInt32(static_cast <uint32_t>(status));
1090 reply->writeInt32(silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001091 return NO_ERROR;
1092 } break;
1093
1094 case STOP_INPUT: {
1095 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001096 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1097 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001098 return NO_ERROR;
1099 } break;
1100
1101 case RELEASE_INPUT: {
1102 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001103 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1104 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001105 return NO_ERROR;
1106 } break;
1107
1108 case INIT_STREAM_VOLUME: {
1109 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001110 audio_stream_type_t stream =
1111 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001112 int indexMin = data.readInt32();
1113 int indexMax = data.readInt32();
1114 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1115 return NO_ERROR;
1116 } break;
1117
1118 case SET_STREAM_VOLUME: {
1119 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001120 audio_stream_type_t stream =
1121 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001122 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001123 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1124 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1125 index,
1126 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001127 return NO_ERROR;
1128 } break;
1129
1130 case GET_STREAM_VOLUME: {
1131 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001132 audio_stream_type_t stream =
1133 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurent83844cc2011-11-18 16:43:31 -08001134 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001135 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001136 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001137 reply->writeInt32(index);
1138 reply->writeInt32(static_cast <uint32_t>(status));
1139 return NO_ERROR;
1140 } break;
1141
Eric Laurentde070132010-07-13 04:45:46 -07001142 case GET_STRATEGY_FOR_STREAM: {
1143 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001144 audio_stream_type_t stream =
1145 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentde070132010-07-13 04:45:46 -07001146 reply->writeInt32(getStrategyForStream(stream));
1147 return NO_ERROR;
1148 } break;
1149
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001150 case GET_DEVICES_FOR_STREAM: {
1151 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001152 audio_stream_type_t stream =
1153 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001154 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1155 return NO_ERROR;
1156 } break;
1157
Eric Laurentde070132010-07-13 04:45:46 -07001158 case GET_OUTPUT_FOR_EFFECT: {
1159 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001160 effect_descriptor_t desc = {};
1161 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1162 android_errorWriteLog(0x534e4554, "73126106");
1163 }
1164 (void)sanitizeEffectDescriptor(&desc);
Eric Laurentde070132010-07-13 04:45:46 -07001165 audio_io_handle_t output = getOutputForEffect(&desc);
1166 reply->writeInt32(static_cast <int>(output));
1167 return NO_ERROR;
1168 } break;
1169
1170 case REGISTER_EFFECT: {
1171 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001172 effect_descriptor_t desc = {};
1173 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1174 android_errorWriteLog(0x534e4554, "73126106");
1175 }
1176 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001177 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001178 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001179 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001180 int id = data.readInt32();
1181 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001182 io,
Eric Laurentde070132010-07-13 04:45:46 -07001183 strategy,
1184 session,
1185 id)));
1186 return NO_ERROR;
1187 } break;
1188
1189 case UNREGISTER_EFFECT: {
1190 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1191 int id = data.readInt32();
1192 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1193 return NO_ERROR;
1194 } break;
1195
Eric Laurentdb7c0792011-08-10 10:37:50 -07001196 case SET_EFFECT_ENABLED: {
1197 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1198 int id = data.readInt32();
1199 bool enabled = static_cast <bool>(data.readInt32());
1200 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1201 return NO_ERROR;
1202 } break;
1203
Eric Laurenteda6c362011-02-02 09:33:30 -08001204 case IS_STREAM_ACTIVE: {
1205 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001206 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001207 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001208 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001209 return NO_ERROR;
1210 } break;
1211
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001212 case IS_STREAM_ACTIVE_REMOTELY: {
1213 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1214 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1215 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001216 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001217 return NO_ERROR;
1218 } break;
1219
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001220 case IS_SOURCE_ACTIVE: {
1221 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1222 audio_source_t source = (audio_source_t) data.readInt32();
1223 reply->writeInt32( isSourceActive(source));
1224 return NO_ERROR;
1225 }
1226
Eric Laurent57dae992011-07-24 13:36:09 -07001227 case QUERY_DEFAULT_PRE_PROCESSING: {
1228 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001229 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001230 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001231 if (count > AudioEffect::kMaxPreProcessing) {
1232 count = AudioEffect::kMaxPreProcessing;
1233 }
Eric Laurent57dae992011-07-24 13:36:09 -07001234 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001235 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001236 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1237 reply->writeInt32(status);
1238 if (status != NO_ERROR && status != NO_MEMORY) {
1239 retCount = 0;
1240 }
1241 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001242 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001243 if (retCount < count) {
1244 count = retCount;
1245 }
1246 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1247 }
1248 delete[] descriptors;
1249 return status;
1250 }
1251
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001252 case IS_OFFLOAD_SUPPORTED: {
1253 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001254 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001255 data.read(&info, sizeof(audio_offload_info_t));
1256 bool isSupported = isOffloadSupported(info);
1257 reply->writeInt32(isSupported);
1258 return NO_ERROR;
1259 }
1260
Eric Laurent203b1a12014-04-01 10:34:16 -07001261 case LIST_AUDIO_PORTS: {
1262 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1263 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1264 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1265 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001266 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1267 numPortsReq = MAX_ITEMS_PER_LIST;
1268 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001269 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001270 struct audio_port *ports =
1271 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001272 if (ports == NULL) {
1273 reply->writeInt32(NO_MEMORY);
1274 reply->writeInt32(0);
1275 return NO_ERROR;
1276 }
1277 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001278 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1279 reply->writeInt32(status);
1280 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001281
1282 if (status == NO_ERROR) {
1283 if (numPortsReq > numPorts) {
1284 numPortsReq = numPorts;
1285 }
1286 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1287 reply->writeInt32(generation);
1288 }
1289 free(ports);
1290 return NO_ERROR;
1291 }
1292
1293 case GET_AUDIO_PORT: {
1294 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001295 struct audio_port port = {};
1296 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1297 ALOGE("b/23912202");
1298 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001299 status_t status = getAudioPort(&port);
1300 reply->writeInt32(status);
1301 if (status == NO_ERROR) {
1302 reply->write(&port, sizeof(struct audio_port));
1303 }
1304 return NO_ERROR;
1305 }
1306
1307 case CREATE_AUDIO_PATCH: {
1308 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001309 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001310 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001311 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001312 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1313 ALOGE("b/23912202");
1314 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001315 status_t status = createAudioPatch(&patch, &handle);
1316 reply->writeInt32(status);
1317 if (status == NO_ERROR) {
1318 reply->write(&handle, sizeof(audio_patch_handle_t));
1319 }
1320 return NO_ERROR;
1321 }
1322
1323 case RELEASE_AUDIO_PATCH: {
1324 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001325 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001326 data.read(&handle, sizeof(audio_patch_handle_t));
1327 status_t status = releaseAudioPatch(handle);
1328 reply->writeInt32(status);
1329 return NO_ERROR;
1330 }
1331
1332 case LIST_AUDIO_PATCHES: {
1333 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1334 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001335 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1336 numPatchesReq = MAX_ITEMS_PER_LIST;
1337 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001338 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001339 struct audio_patch *patches =
1340 (struct audio_patch *)calloc(numPatchesReq,
1341 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001342 if (patches == NULL) {
1343 reply->writeInt32(NO_MEMORY);
1344 reply->writeInt32(0);
1345 return NO_ERROR;
1346 }
1347 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001348 status_t status = listAudioPatches(&numPatches, patches, &generation);
1349 reply->writeInt32(status);
1350 reply->writeInt32(numPatches);
1351 if (status == NO_ERROR) {
1352 if (numPatchesReq > numPatches) {
1353 numPatchesReq = numPatches;
1354 }
1355 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1356 reply->writeInt32(generation);
1357 }
1358 free(patches);
1359 return NO_ERROR;
1360 }
1361
1362 case SET_AUDIO_PORT_CONFIG: {
1363 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001364 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001365 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001366 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001367 status_t status = setAudioPortConfig(&config);
1368 reply->writeInt32(status);
1369 return NO_ERROR;
1370 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001371
Eric Laurentb52c1522014-05-20 11:27:36 -07001372 case REGISTER_CLIENT: {
1373 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1374 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1375 data.readStrongBinder());
1376 registerClient(client);
1377 return NO_ERROR;
1378 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001379
Eric Laurente8726fe2015-06-26 09:39:24 -07001380 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1381 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1382 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1383 return NO_ERROR;
1384 } break;
1385
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001386 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1387 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1388 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1389 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001390 audio_session_t session = AUDIO_SESSION_NONE;
1391 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1392 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001393 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1394 reply->writeInt32(status);
1395 if (status == NO_ERROR) {
1396 reply->writeInt32(session);
1397 reply->writeInt32(ioHandle);
1398 reply->writeInt32(device);
1399 }
1400 return NO_ERROR;
1401 } break;
1402
1403 case RELEASE_SOUNDTRIGGER_SESSION: {
1404 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1405 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1406 data.readStrongBinder());
1407 audio_session_t session = (audio_session_t)data.readInt32();
1408 status_t status = releaseSoundTriggerSession(session);
1409 reply->writeInt32(status);
1410 return NO_ERROR;
1411 } break;
1412
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001413 case GET_PHONE_STATE: {
1414 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1415 reply->writeInt32((int32_t)getPhoneState());
1416 return NO_ERROR;
1417 } break;
1418
Eric Laurentbaac1832014-12-01 17:52:59 -08001419 case REGISTER_POLICY_MIXES: {
1420 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1421 bool registration = data.readInt32() == 1;
1422 Vector<AudioMix> mixes;
1423 size_t size = (size_t)data.readInt32();
1424 if (size > MAX_MIXES_PER_POLICY) {
1425 size = MAX_MIXES_PER_POLICY;
1426 }
1427 for (size_t i = 0; i < size; i++) {
1428 AudioMix mix;
1429 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1430 mixes.add(mix);
1431 }
1432 }
1433 status_t status = registerPolicyMixes(mixes, registration);
1434 reply->writeInt32(status);
1435 return NO_ERROR;
1436 } break;
1437
Eric Laurent554a2772015-04-10 11:29:24 -07001438 case START_AUDIO_SOURCE: {
1439 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001440 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001441 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001442 (void)sanitizeAudioPortConfig(&source);
1443 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001444 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001445 sanetizeAudioAttributes(&attributes);
Glenn Kasten559d4392016-03-29 13:42:57 -07001446 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent554a2772015-04-10 11:29:24 -07001447 status_t status = startAudioSource(&source, &attributes, &handle);
1448 reply->writeInt32(status);
1449 reply->writeInt32(handle);
1450 return NO_ERROR;
1451 } break;
1452
1453 case STOP_AUDIO_SOURCE: {
1454 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten559d4392016-03-29 13:42:57 -07001455 audio_patch_handle_t handle = (audio_patch_handle_t) data.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -07001456 status_t status = stopAudioSource(handle);
1457 reply->writeInt32(status);
1458 return NO_ERROR;
1459 } break;
1460
Andy Hung2ddee192015-12-18 17:34:44 -08001461 case SET_MASTER_MONO: {
1462 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1463 bool mono = static_cast<bool>(data.readInt32());
1464 status_t status = setMasterMono(mono);
1465 reply->writeInt32(status);
1466 return NO_ERROR;
1467 } break;
1468
1469 case GET_MASTER_MONO: {
1470 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1471 bool mono;
1472 status_t status = getMasterMono(&mono);
1473 reply->writeInt32(status);
1474 if (status == NO_ERROR) {
1475 reply->writeInt32(static_cast<int32_t>(mono));
1476 }
1477 return NO_ERROR;
1478 } break;
1479
Eric Laurentac9cef52017-06-09 15:46:26 -07001480 case GET_STREAM_VOLUME_DB: {
1481 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1482 audio_stream_type_t stream =
1483 static_cast <audio_stream_type_t>(data.readInt32());
1484 int index = static_cast <int>(data.readInt32());
1485 audio_devices_t device =
1486 static_cast <audio_devices_t>(data.readUint32());
1487 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1488 return NO_ERROR;
1489 }
1490
Eric Laurentc2f1f072009-07-17 12:17:14 -07001491 default:
1492 return BBinder::onTransact(code, data, reply, flags);
1493 }
1494}
1495
Andy Hungb0272092018-04-12 11:06:56 -07001496/** returns true if string overflow was prevented by zero termination */
1497template <size_t size>
1498static bool preventStringOverflow(char (&s)[size]) {
1499 if (strnlen(s, size) < size) return false;
1500 s[size - 1] = '\0';
1501 return true;
1502}
1503
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001504void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
1505{
1506 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
1507 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
1508 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
1509 }
1510 attr->tags[tagsMaxSize - 1] = '\0';
1511}
1512
Andy Hungb0272092018-04-12 11:06:56 -07001513/** returns BAD_VALUE if sanitization was required. */
1514status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
1515{
1516 if (preventStringOverflow(desc->name)
1517 | /* always */ preventStringOverflow(desc->implementor)) {
1518 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
1519 return BAD_VALUE;
1520 }
1521 return NO_ERROR;
1522}
1523
1524/** returns BAD_VALUE if sanitization was required. */
1525status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
1526{
1527 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
1528 preventStringOverflow(config->ext.device.address)) {
1529 return BAD_VALUE;
1530 }
1531 return NO_ERROR;
1532}
1533
Eric Laurentc2f1f072009-07-17 12:17:14 -07001534// ----------------------------------------------------------------------------
1535
Glenn Kasten40bc9062015-03-20 09:09:33 -07001536} // namespace android