blob: a0516936e55a2d783c3dd830eedccc76b81d9a53 [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>
27
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());
860 return INVALID_OPERATION;
861 default:
862 break;
863 }
864
Eric Laurent4980df22018-01-26 18:04:09 -0800865 // make sure the following transactions come from system components
866 switch (code) {
867 case SET_DEVICE_CONNECTION_STATE:
868 case HANDLE_DEVICE_CONFIG_CHANGE:
869 case SET_PHONE_STATE:
870 case SET_RINGER_MODE:
871 case SET_FORCE_USE:
872 case INIT_STREAM_VOLUME:
873 case SET_STREAM_VOLUME:
874 case REGISTER_POLICY_MIXES:
Eric Laurent96c7eed2018-03-26 17:57:01 -0700875 case SET_MASTER_MONO: {
876 uid_t multiUserClientUid = IPCThreadState::self()->getCallingUid() % AID_USER_OFFSET;
877 if (multiUserClientUid >= AID_APP_START) {
Eric Laurent4980df22018-01-26 18:04:09 -0800878 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
879 __func__, code, IPCThreadState::self()->getCallingPid(),
880 IPCThreadState::self()->getCallingUid());
881 return INVALID_OPERATION;
882 }
Eric Laurent96c7eed2018-03-26 17:57:01 -0700883 } break;
Eric Laurent4980df22018-01-26 18:04:09 -0800884 default:
885 break;
886 }
887
Eric Laurent09e621a2018-03-26 16:50:47 -0700888 // FIXME: extend timeout for SET_DEVICE_CONNECTION_STATE and HANDLE_DEVICE_CONFIG_CHANGE
889 // while we investigate why BT A2DP device connection/disconnection can sometimes
890 // take more than 5 seconds
891 uint32_t timeoutMs = TimeCheck::kDefaultTimeOutMs;
892 switch (code) {
893 case SET_DEVICE_CONNECTION_STATE:
894 case HANDLE_DEVICE_CONFIG_CHANGE:
895 timeoutMs *= 2;
896 break;
897 default:
898 break;
899 }
900
901 TimeCheck check("IAudioPolicyService", timeoutMs);
Eric Laurent3528c932018-02-23 17:17:22 -0800902
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700903 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700904 case SET_DEVICE_CONNECTION_STATE: {
905 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700906 audio_devices_t device =
907 static_cast <audio_devices_t>(data.readInt32());
908 audio_policy_dev_state_t state =
909 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700910 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -0800911 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800912 if (device_address == nullptr || device_name == nullptr) {
913 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
914 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
915 } else {
916 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
917 state,
918 device_address,
919 device_name)));
920 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700921 return NO_ERROR;
922 } break;
923
924 case GET_DEVICE_CONNECTION_STATE: {
925 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700926 audio_devices_t device =
927 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700928 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800929 if (device_address == nullptr) {
930 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
931 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
932 } else {
933 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
934 device_address)));
935 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700936 return NO_ERROR;
937 } break;
938
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800939 case HANDLE_DEVICE_CONFIG_CHANGE: {
940 CHECK_INTERFACE(IAudioPolicyService, data, reply);
941 audio_devices_t device =
942 static_cast <audio_devices_t>(data.readInt32());
943 const char *device_address = data.readCString();
944 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800945 if (device_address == nullptr || device_name == nullptr) {
946 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
947 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
948 } else {
949 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
950 device_address,
951 device_name)));
952 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800953 return NO_ERROR;
954 } break;
955
Eric Laurentc2f1f072009-07-17 12:17:14 -0700956 case SET_PHONE_STATE: {
957 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700958 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
959 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700960 return NO_ERROR;
961 } break;
962
Eric Laurentc2f1f072009-07-17 12:17:14 -0700963 case SET_FORCE_USE: {
964 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700965 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
966 data.readInt32());
Dima Zavinfce7a472011-04-19 22:30:36 -0700967 audio_policy_forced_cfg_t config =
968 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700969 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
970 return NO_ERROR;
971 } break;
972
973 case GET_FORCE_USE: {
974 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700975 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
976 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700977 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
978 return NO_ERROR;
979 } break;
980
981 case GET_OUTPUT: {
982 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700983 audio_stream_type_t stream =
984 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +0000985 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -0700986 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700987 return NO_ERROR;
988 } break;
989
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700990 case GET_OUTPUT_FOR_ATTR: {
991 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -0700992 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -0800993 bool hasAttributes = data.readInt32() != 0;
994 if (hasAttributes) {
995 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -0800996 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -0800997 }
998 audio_session_t session = (audio_session_t)data.readInt32();
999 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
1000 bool hasStream = data.readInt32() != 0;
1001 if (hasStream) {
1002 stream = (audio_stream_type_t)data.readInt32();
1003 }
Nadav Bar766fb022018-01-07 12:18:03 +02001004 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001005 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001006 audio_config_t config;
1007 memset(&config, 0, sizeof(audio_config_t));
1008 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001009 audio_output_flags_t flags =
1010 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001011 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001012 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001013 audio_io_handle_t output = 0;
Eric Laurente83b55d2014-11-14 10:06:21 -08001014 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001015 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001016 &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001017 flags, &selectedDeviceId, &portId);
Eric Laurente83b55d2014-11-14 10:06:21 -08001018 reply->writeInt32(status);
1019 reply->writeInt32(output);
1020 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001021 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001022 reply->writeInt32(portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001023 return NO_ERROR;
1024 } break;
1025
Eric Laurentc2f1f072009-07-17 12:17:14 -07001026 case START_OUTPUT: {
1027 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001028 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurentebcb2542014-03-05 18:30:08 -08001029 audio_stream_type_t stream =
1030 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001031 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001032 reply->writeInt32(static_cast <uint32_t>(startOutput(output,
Eric Laurentebcb2542014-03-05 18:30:08 -08001033 stream,
Eric Laurentde070132010-07-13 04:45:46 -07001034 session)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001035 return NO_ERROR;
1036 } break;
1037
1038 case STOP_OUTPUT: {
1039 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001040 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurentebcb2542014-03-05 18:30:08 -08001041 audio_stream_type_t stream =
1042 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001043 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001044 reply->writeInt32(static_cast <uint32_t>(stopOutput(output,
Eric Laurentebcb2542014-03-05 18:30:08 -08001045 stream,
Eric Laurentde070132010-07-13 04:45:46 -07001046 session)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001047 return NO_ERROR;
1048 } break;
1049
1050 case RELEASE_OUTPUT: {
1051 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001052 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001053 audio_stream_type_t stream = (audio_stream_type_t)data.readInt32();
1054 audio_session_t session = (audio_session_t)data.readInt32();
1055 releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001056 return NO_ERROR;
1057 } break;
1058
Eric Laurentcaf7f482014-11-25 17:50:47 -08001059 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001060 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001061 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001062 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001063 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001064 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001065 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001066 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001067 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001068 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001069 audio_config_base_t config;
1070 memset(&config, 0, sizeof(audio_config_base_t));
1071 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001072 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001073 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001074 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001075 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001076 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001077 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001078 reply->writeInt32(status);
1079 if (status == NO_ERROR) {
1080 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001081 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001082 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001083 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001084 return NO_ERROR;
1085 } break;
1086
1087 case START_INPUT: {
1088 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001089 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001090 bool silenced = data.readInt32() == 1;
Eric Laurentfee19762018-01-29 18:44:13 -08001091 status_t status = startInput(portId, &silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001092 reply->writeInt32(static_cast <uint32_t>(status));
1093 reply->writeInt32(silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001094 return NO_ERROR;
1095 } break;
1096
1097 case STOP_INPUT: {
1098 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001099 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1100 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001101 return NO_ERROR;
1102 } break;
1103
1104 case RELEASE_INPUT: {
1105 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001106 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1107 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001108 return NO_ERROR;
1109 } break;
1110
1111 case INIT_STREAM_VOLUME: {
1112 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001113 audio_stream_type_t stream =
1114 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001115 int indexMin = data.readInt32();
1116 int indexMax = data.readInt32();
1117 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1118 return NO_ERROR;
1119 } break;
1120
1121 case SET_STREAM_VOLUME: {
1122 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001123 audio_stream_type_t stream =
1124 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001125 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001126 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1127 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1128 index,
1129 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001130 return NO_ERROR;
1131 } break;
1132
1133 case GET_STREAM_VOLUME: {
1134 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001135 audio_stream_type_t stream =
1136 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurent83844cc2011-11-18 16:43:31 -08001137 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001138 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001139 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001140 reply->writeInt32(index);
1141 reply->writeInt32(static_cast <uint32_t>(status));
1142 return NO_ERROR;
1143 } break;
1144
Eric Laurentde070132010-07-13 04:45:46 -07001145 case GET_STRATEGY_FOR_STREAM: {
1146 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001147 audio_stream_type_t stream =
1148 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentde070132010-07-13 04:45:46 -07001149 reply->writeInt32(getStrategyForStream(stream));
1150 return NO_ERROR;
1151 } break;
1152
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001153 case GET_DEVICES_FOR_STREAM: {
1154 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001155 audio_stream_type_t stream =
1156 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001157 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1158 return NO_ERROR;
1159 } break;
1160
Eric Laurentde070132010-07-13 04:45:46 -07001161 case GET_OUTPUT_FOR_EFFECT: {
1162 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001163 effect_descriptor_t desc = {};
1164 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1165 android_errorWriteLog(0x534e4554, "73126106");
1166 }
1167 (void)sanitizeEffectDescriptor(&desc);
Eric Laurentde070132010-07-13 04:45:46 -07001168 audio_io_handle_t output = getOutputForEffect(&desc);
1169 reply->writeInt32(static_cast <int>(output));
1170 return NO_ERROR;
1171 } break;
1172
1173 case REGISTER_EFFECT: {
1174 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001175 effect_descriptor_t desc = {};
1176 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1177 android_errorWriteLog(0x534e4554, "73126106");
1178 }
1179 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001180 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001181 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001182 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001183 int id = data.readInt32();
1184 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001185 io,
Eric Laurentde070132010-07-13 04:45:46 -07001186 strategy,
1187 session,
1188 id)));
1189 return NO_ERROR;
1190 } break;
1191
1192 case UNREGISTER_EFFECT: {
1193 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1194 int id = data.readInt32();
1195 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1196 return NO_ERROR;
1197 } break;
1198
Eric Laurentdb7c0792011-08-10 10:37:50 -07001199 case SET_EFFECT_ENABLED: {
1200 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1201 int id = data.readInt32();
1202 bool enabled = static_cast <bool>(data.readInt32());
1203 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1204 return NO_ERROR;
1205 } break;
1206
Eric Laurenteda6c362011-02-02 09:33:30 -08001207 case IS_STREAM_ACTIVE: {
1208 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001209 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001210 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001211 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001212 return NO_ERROR;
1213 } break;
1214
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001215 case IS_STREAM_ACTIVE_REMOTELY: {
1216 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1217 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1218 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001219 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001220 return NO_ERROR;
1221 } break;
1222
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001223 case IS_SOURCE_ACTIVE: {
1224 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1225 audio_source_t source = (audio_source_t) data.readInt32();
1226 reply->writeInt32( isSourceActive(source));
1227 return NO_ERROR;
1228 }
1229
Eric Laurent57dae992011-07-24 13:36:09 -07001230 case QUERY_DEFAULT_PRE_PROCESSING: {
1231 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001232 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001233 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001234 if (count > AudioEffect::kMaxPreProcessing) {
1235 count = AudioEffect::kMaxPreProcessing;
1236 }
Eric Laurent57dae992011-07-24 13:36:09 -07001237 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001238 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001239 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1240 reply->writeInt32(status);
1241 if (status != NO_ERROR && status != NO_MEMORY) {
1242 retCount = 0;
1243 }
1244 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001245 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001246 if (retCount < count) {
1247 count = retCount;
1248 }
1249 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1250 }
1251 delete[] descriptors;
1252 return status;
1253 }
1254
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001255 case IS_OFFLOAD_SUPPORTED: {
1256 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001257 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001258 data.read(&info, sizeof(audio_offload_info_t));
1259 bool isSupported = isOffloadSupported(info);
1260 reply->writeInt32(isSupported);
1261 return NO_ERROR;
1262 }
1263
Eric Laurent203b1a12014-04-01 10:34:16 -07001264 case LIST_AUDIO_PORTS: {
1265 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1266 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1267 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1268 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001269 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1270 numPortsReq = MAX_ITEMS_PER_LIST;
1271 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001272 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001273 struct audio_port *ports =
1274 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001275 if (ports == NULL) {
1276 reply->writeInt32(NO_MEMORY);
1277 reply->writeInt32(0);
1278 return NO_ERROR;
1279 }
1280 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001281 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1282 reply->writeInt32(status);
1283 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001284
1285 if (status == NO_ERROR) {
1286 if (numPortsReq > numPorts) {
1287 numPortsReq = numPorts;
1288 }
1289 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1290 reply->writeInt32(generation);
1291 }
1292 free(ports);
1293 return NO_ERROR;
1294 }
1295
1296 case GET_AUDIO_PORT: {
1297 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001298 struct audio_port port = {};
1299 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1300 ALOGE("b/23912202");
1301 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001302 status_t status = getAudioPort(&port);
1303 reply->writeInt32(status);
1304 if (status == NO_ERROR) {
1305 reply->write(&port, sizeof(struct audio_port));
1306 }
1307 return NO_ERROR;
1308 }
1309
1310 case CREATE_AUDIO_PATCH: {
1311 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001312 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001313 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001314 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001315 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1316 ALOGE("b/23912202");
1317 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001318 status_t status = createAudioPatch(&patch, &handle);
1319 reply->writeInt32(status);
1320 if (status == NO_ERROR) {
1321 reply->write(&handle, sizeof(audio_patch_handle_t));
1322 }
1323 return NO_ERROR;
1324 }
1325
1326 case RELEASE_AUDIO_PATCH: {
1327 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001328 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001329 data.read(&handle, sizeof(audio_patch_handle_t));
1330 status_t status = releaseAudioPatch(handle);
1331 reply->writeInt32(status);
1332 return NO_ERROR;
1333 }
1334
1335 case LIST_AUDIO_PATCHES: {
1336 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1337 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001338 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1339 numPatchesReq = MAX_ITEMS_PER_LIST;
1340 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001341 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001342 struct audio_patch *patches =
1343 (struct audio_patch *)calloc(numPatchesReq,
1344 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001345 if (patches == NULL) {
1346 reply->writeInt32(NO_MEMORY);
1347 reply->writeInt32(0);
1348 return NO_ERROR;
1349 }
1350 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001351 status_t status = listAudioPatches(&numPatches, patches, &generation);
1352 reply->writeInt32(status);
1353 reply->writeInt32(numPatches);
1354 if (status == NO_ERROR) {
1355 if (numPatchesReq > numPatches) {
1356 numPatchesReq = numPatches;
1357 }
1358 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1359 reply->writeInt32(generation);
1360 }
1361 free(patches);
1362 return NO_ERROR;
1363 }
1364
1365 case SET_AUDIO_PORT_CONFIG: {
1366 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001367 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001368 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001369 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001370 status_t status = setAudioPortConfig(&config);
1371 reply->writeInt32(status);
1372 return NO_ERROR;
1373 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001374
Eric Laurentb52c1522014-05-20 11:27:36 -07001375 case REGISTER_CLIENT: {
1376 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1377 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1378 data.readStrongBinder());
1379 registerClient(client);
1380 return NO_ERROR;
1381 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001382
Eric Laurente8726fe2015-06-26 09:39:24 -07001383 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1384 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1385 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1386 return NO_ERROR;
1387 } break;
1388
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001389 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1390 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1391 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1392 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001393 audio_session_t session = AUDIO_SESSION_NONE;
1394 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1395 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001396 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1397 reply->writeInt32(status);
1398 if (status == NO_ERROR) {
1399 reply->writeInt32(session);
1400 reply->writeInt32(ioHandle);
1401 reply->writeInt32(device);
1402 }
1403 return NO_ERROR;
1404 } break;
1405
1406 case RELEASE_SOUNDTRIGGER_SESSION: {
1407 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1408 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1409 data.readStrongBinder());
1410 audio_session_t session = (audio_session_t)data.readInt32();
1411 status_t status = releaseSoundTriggerSession(session);
1412 reply->writeInt32(status);
1413 return NO_ERROR;
1414 } break;
1415
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001416 case GET_PHONE_STATE: {
1417 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1418 reply->writeInt32((int32_t)getPhoneState());
1419 return NO_ERROR;
1420 } break;
1421
Eric Laurentbaac1832014-12-01 17:52:59 -08001422 case REGISTER_POLICY_MIXES: {
1423 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1424 bool registration = data.readInt32() == 1;
1425 Vector<AudioMix> mixes;
1426 size_t size = (size_t)data.readInt32();
1427 if (size > MAX_MIXES_PER_POLICY) {
1428 size = MAX_MIXES_PER_POLICY;
1429 }
1430 for (size_t i = 0; i < size; i++) {
1431 AudioMix mix;
1432 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1433 mixes.add(mix);
1434 }
1435 }
1436 status_t status = registerPolicyMixes(mixes, registration);
1437 reply->writeInt32(status);
1438 return NO_ERROR;
1439 } break;
1440
Eric Laurent554a2772015-04-10 11:29:24 -07001441 case START_AUDIO_SOURCE: {
1442 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001443 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001444 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001445 (void)sanitizeAudioPortConfig(&source);
1446 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001447 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001448 sanetizeAudioAttributes(&attributes);
Glenn Kasten559d4392016-03-29 13:42:57 -07001449 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent554a2772015-04-10 11:29:24 -07001450 status_t status = startAudioSource(&source, &attributes, &handle);
1451 reply->writeInt32(status);
1452 reply->writeInt32(handle);
1453 return NO_ERROR;
1454 } break;
1455
1456 case STOP_AUDIO_SOURCE: {
1457 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten559d4392016-03-29 13:42:57 -07001458 audio_patch_handle_t handle = (audio_patch_handle_t) data.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -07001459 status_t status = stopAudioSource(handle);
1460 reply->writeInt32(status);
1461 return NO_ERROR;
1462 } break;
1463
Andy Hung2ddee192015-12-18 17:34:44 -08001464 case SET_MASTER_MONO: {
1465 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1466 bool mono = static_cast<bool>(data.readInt32());
1467 status_t status = setMasterMono(mono);
1468 reply->writeInt32(status);
1469 return NO_ERROR;
1470 } break;
1471
1472 case GET_MASTER_MONO: {
1473 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1474 bool mono;
1475 status_t status = getMasterMono(&mono);
1476 reply->writeInt32(status);
1477 if (status == NO_ERROR) {
1478 reply->writeInt32(static_cast<int32_t>(mono));
1479 }
1480 return NO_ERROR;
1481 } break;
1482
Eric Laurentac9cef52017-06-09 15:46:26 -07001483 case GET_STREAM_VOLUME_DB: {
1484 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1485 audio_stream_type_t stream =
1486 static_cast <audio_stream_type_t>(data.readInt32());
1487 int index = static_cast <int>(data.readInt32());
1488 audio_devices_t device =
1489 static_cast <audio_devices_t>(data.readUint32());
1490 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1491 return NO_ERROR;
1492 }
1493
Eric Laurentc2f1f072009-07-17 12:17:14 -07001494 default:
1495 return BBinder::onTransact(code, data, reply, flags);
1496 }
1497}
1498
Andy Hungb0272092018-04-12 11:06:56 -07001499/** returns true if string overflow was prevented by zero termination */
1500template <size_t size>
1501static bool preventStringOverflow(char (&s)[size]) {
1502 if (strnlen(s, size) < size) return false;
1503 s[size - 1] = '\0';
1504 return true;
1505}
1506
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001507void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
1508{
1509 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
1510 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
1511 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
1512 }
1513 attr->tags[tagsMaxSize - 1] = '\0';
1514}
1515
Andy Hungb0272092018-04-12 11:06:56 -07001516/** returns BAD_VALUE if sanitization was required. */
1517status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
1518{
1519 if (preventStringOverflow(desc->name)
1520 | /* always */ preventStringOverflow(desc->implementor)) {
1521 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
1522 return BAD_VALUE;
1523 }
1524 return NO_ERROR;
1525}
1526
1527/** returns BAD_VALUE if sanitization was required. */
1528status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
1529{
1530 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
1531 preventStringOverflow(config->ext.device.address)) {
1532 return BAD_VALUE;
1533 }
1534 return NO_ERROR;
1535}
1536
Eric Laurentc2f1f072009-07-17 12:17:14 -07001537// ----------------------------------------------------------------------------
1538
Glenn Kasten40bc9062015-03-20 09:09:33 -07001539} // namespace android