blob: eca6fee728b34298dd8aab913c1560695b01c3e7 [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 Laurente17378d2018-05-09 14:43:01 -0700879//FIXME: Allow SET_FORCE_USE calls from system apps until a better use case routing API is available
880// case SET_FORCE_USE:
Eric Laurent4980df22018-01-26 18:04:09 -0800881 case INIT_STREAM_VOLUME:
882 case SET_STREAM_VOLUME:
883 case REGISTER_POLICY_MIXES:
Eric Laurent10b71232018-04-13 18:14:44 -0700884 case SET_MASTER_MONO:
885 case START_AUDIO_SOURCE:
886 case STOP_AUDIO_SOURCE: {
Eric Laurentf1047e82018-04-16 19:18:20 -0700887 if (multiuser_get_app_id(IPCThreadState::self()->getCallingUid()) >= AID_APP_START) {
Eric Laurent4980df22018-01-26 18:04:09 -0800888 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
889 __func__, code, IPCThreadState::self()->getCallingPid(),
890 IPCThreadState::self()->getCallingUid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700891 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
892 return OK;
Eric Laurent4980df22018-01-26 18:04:09 -0800893 }
Eric Laurent96c7eed2018-03-26 17:57:01 -0700894 } break;
Eric Laurent4980df22018-01-26 18:04:09 -0800895 default:
896 break;
897 }
898
Eric Laurentda6a5a82018-05-02 18:47:34 -0700899 TimeCheck check("IAudioPolicyService");
Eric Laurent3528c932018-02-23 17:17:22 -0800900
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700901 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700902 case SET_DEVICE_CONNECTION_STATE: {
903 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700904 audio_devices_t device =
905 static_cast <audio_devices_t>(data.readInt32());
906 audio_policy_dev_state_t state =
907 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700908 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -0800909 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800910 if (device_address == nullptr || device_name == nullptr) {
911 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
912 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
913 } else {
914 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
915 state,
916 device_address,
917 device_name)));
918 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700919 return NO_ERROR;
920 } break;
921
922 case GET_DEVICE_CONNECTION_STATE: {
923 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700924 audio_devices_t device =
925 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700926 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800927 if (device_address == nullptr) {
928 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
929 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
930 } else {
931 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
932 device_address)));
933 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700934 return NO_ERROR;
935 } break;
936
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800937 case HANDLE_DEVICE_CONFIG_CHANGE: {
938 CHECK_INTERFACE(IAudioPolicyService, data, reply);
939 audio_devices_t device =
940 static_cast <audio_devices_t>(data.readInt32());
941 const char *device_address = data.readCString();
942 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800943 if (device_address == nullptr || device_name == nullptr) {
944 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
945 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
946 } else {
947 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
948 device_address,
949 device_name)));
950 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800951 return NO_ERROR;
952 } break;
953
Eric Laurentc2f1f072009-07-17 12:17:14 -0700954 case SET_PHONE_STATE: {
955 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700956 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
957 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700958 return NO_ERROR;
959 } break;
960
Eric Laurentc2f1f072009-07-17 12:17:14 -0700961 case SET_FORCE_USE: {
962 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700963 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
964 data.readInt32());
Dima Zavinfce7a472011-04-19 22:30:36 -0700965 audio_policy_forced_cfg_t config =
966 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700967 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
968 return NO_ERROR;
969 } break;
970
971 case GET_FORCE_USE: {
972 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700973 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
974 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700975 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
976 return NO_ERROR;
977 } break;
978
979 case GET_OUTPUT: {
980 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700981 audio_stream_type_t stream =
982 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +0000983 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -0700984 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700985 return NO_ERROR;
986 } break;
987
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700988 case GET_OUTPUT_FOR_ATTR: {
989 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -0700990 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -0800991 bool hasAttributes = data.readInt32() != 0;
992 if (hasAttributes) {
993 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -0800994 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -0800995 }
996 audio_session_t session = (audio_session_t)data.readInt32();
997 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
998 bool hasStream = data.readInt32() != 0;
999 if (hasStream) {
1000 stream = (audio_stream_type_t)data.readInt32();
1001 }
Nadav Bar766fb022018-01-07 12:18:03 +02001002 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001003 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001004 audio_config_t config;
1005 memset(&config, 0, sizeof(audio_config_t));
1006 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001007 audio_output_flags_t flags =
1008 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001009 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001010 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001011 audio_io_handle_t output = 0;
Eric Laurente83b55d2014-11-14 10:06:21 -08001012 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001013 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001014 &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001015 flags, &selectedDeviceId, &portId);
Eric Laurente83b55d2014-11-14 10:06:21 -08001016 reply->writeInt32(status);
1017 reply->writeInt32(output);
1018 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001019 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001020 reply->writeInt32(portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001021 return NO_ERROR;
1022 } break;
1023
Eric Laurentc2f1f072009-07-17 12:17:14 -07001024 case START_OUTPUT: {
1025 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001026 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurentebcb2542014-03-05 18:30:08 -08001027 audio_stream_type_t stream =
1028 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001029 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001030 reply->writeInt32(static_cast <uint32_t>(startOutput(output,
Eric Laurentebcb2542014-03-05 18:30:08 -08001031 stream,
Eric Laurentde070132010-07-13 04:45:46 -07001032 session)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001033 return NO_ERROR;
1034 } break;
1035
1036 case STOP_OUTPUT: {
1037 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001038 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurentebcb2542014-03-05 18:30:08 -08001039 audio_stream_type_t stream =
1040 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001041 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001042 reply->writeInt32(static_cast <uint32_t>(stopOutput(output,
Eric Laurentebcb2542014-03-05 18:30:08 -08001043 stream,
Eric Laurentde070132010-07-13 04:45:46 -07001044 session)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001045 return NO_ERROR;
1046 } break;
1047
1048 case RELEASE_OUTPUT: {
1049 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001050 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001051 audio_stream_type_t stream = (audio_stream_type_t)data.readInt32();
1052 audio_session_t session = (audio_session_t)data.readInt32();
1053 releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001054 return NO_ERROR;
1055 } break;
1056
Eric Laurentcaf7f482014-11-25 17:50:47 -08001057 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001058 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001059 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001060 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001061 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001062 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001063 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001064 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001065 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001066 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001067 audio_config_base_t config;
1068 memset(&config, 0, sizeof(audio_config_base_t));
1069 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001070 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001071 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001072 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001073 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001074 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001075 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001076 reply->writeInt32(status);
1077 if (status == NO_ERROR) {
1078 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001079 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001080 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001081 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001082 return NO_ERROR;
1083 } break;
1084
1085 case START_INPUT: {
1086 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001087 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001088 bool silenced = data.readInt32() == 1;
Eric Laurentfee19762018-01-29 18:44:13 -08001089 status_t status = startInput(portId, &silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001090 reply->writeInt32(static_cast <uint32_t>(status));
1091 reply->writeInt32(silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001092 return NO_ERROR;
1093 } break;
1094
1095 case STOP_INPUT: {
1096 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001097 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1098 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001099 return NO_ERROR;
1100 } break;
1101
1102 case RELEASE_INPUT: {
1103 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001104 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1105 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001106 return NO_ERROR;
1107 } break;
1108
1109 case INIT_STREAM_VOLUME: {
1110 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001111 audio_stream_type_t stream =
1112 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001113 int indexMin = data.readInt32();
1114 int indexMax = data.readInt32();
1115 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1116 return NO_ERROR;
1117 } break;
1118
1119 case SET_STREAM_VOLUME: {
1120 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001121 audio_stream_type_t stream =
1122 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001123 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001124 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1125 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1126 index,
1127 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001128 return NO_ERROR;
1129 } break;
1130
1131 case GET_STREAM_VOLUME: {
1132 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001133 audio_stream_type_t stream =
1134 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurent83844cc2011-11-18 16:43:31 -08001135 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001136 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001137 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001138 reply->writeInt32(index);
1139 reply->writeInt32(static_cast <uint32_t>(status));
1140 return NO_ERROR;
1141 } break;
1142
Eric Laurentde070132010-07-13 04:45:46 -07001143 case GET_STRATEGY_FOR_STREAM: {
1144 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001145 audio_stream_type_t stream =
1146 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentde070132010-07-13 04:45:46 -07001147 reply->writeInt32(getStrategyForStream(stream));
1148 return NO_ERROR;
1149 } break;
1150
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001151 case GET_DEVICES_FOR_STREAM: {
1152 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001153 audio_stream_type_t stream =
1154 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001155 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1156 return NO_ERROR;
1157 } break;
1158
Eric Laurentde070132010-07-13 04:45:46 -07001159 case GET_OUTPUT_FOR_EFFECT: {
1160 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001161 effect_descriptor_t desc = {};
1162 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1163 android_errorWriteLog(0x534e4554, "73126106");
1164 }
1165 (void)sanitizeEffectDescriptor(&desc);
Eric Laurentde070132010-07-13 04:45:46 -07001166 audio_io_handle_t output = getOutputForEffect(&desc);
1167 reply->writeInt32(static_cast <int>(output));
1168 return NO_ERROR;
1169 } break;
1170
1171 case REGISTER_EFFECT: {
1172 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001173 effect_descriptor_t desc = {};
1174 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1175 android_errorWriteLog(0x534e4554, "73126106");
1176 }
1177 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001178 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001179 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001180 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001181 int id = data.readInt32();
1182 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001183 io,
Eric Laurentde070132010-07-13 04:45:46 -07001184 strategy,
1185 session,
1186 id)));
1187 return NO_ERROR;
1188 } break;
1189
1190 case UNREGISTER_EFFECT: {
1191 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1192 int id = data.readInt32();
1193 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1194 return NO_ERROR;
1195 } break;
1196
Eric Laurentdb7c0792011-08-10 10:37:50 -07001197 case SET_EFFECT_ENABLED: {
1198 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1199 int id = data.readInt32();
1200 bool enabled = static_cast <bool>(data.readInt32());
1201 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1202 return NO_ERROR;
1203 } break;
1204
Eric Laurenteda6c362011-02-02 09:33:30 -08001205 case IS_STREAM_ACTIVE: {
1206 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001207 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001208 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001209 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001210 return NO_ERROR;
1211 } break;
1212
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001213 case IS_STREAM_ACTIVE_REMOTELY: {
1214 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1215 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1216 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001217 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001218 return NO_ERROR;
1219 } break;
1220
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001221 case IS_SOURCE_ACTIVE: {
1222 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1223 audio_source_t source = (audio_source_t) data.readInt32();
1224 reply->writeInt32( isSourceActive(source));
1225 return NO_ERROR;
1226 }
1227
Eric Laurent57dae992011-07-24 13:36:09 -07001228 case QUERY_DEFAULT_PRE_PROCESSING: {
1229 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001230 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001231 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001232 if (count > AudioEffect::kMaxPreProcessing) {
1233 count = AudioEffect::kMaxPreProcessing;
1234 }
Eric Laurent57dae992011-07-24 13:36:09 -07001235 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001236 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001237 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1238 reply->writeInt32(status);
1239 if (status != NO_ERROR && status != NO_MEMORY) {
1240 retCount = 0;
1241 }
1242 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001243 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001244 if (retCount < count) {
1245 count = retCount;
1246 }
1247 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1248 }
1249 delete[] descriptors;
1250 return status;
1251 }
1252
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001253 case IS_OFFLOAD_SUPPORTED: {
1254 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001255 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001256 data.read(&info, sizeof(audio_offload_info_t));
1257 bool isSupported = isOffloadSupported(info);
1258 reply->writeInt32(isSupported);
1259 return NO_ERROR;
1260 }
1261
Eric Laurent203b1a12014-04-01 10:34:16 -07001262 case LIST_AUDIO_PORTS: {
1263 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1264 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1265 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1266 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001267 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1268 numPortsReq = MAX_ITEMS_PER_LIST;
1269 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001270 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001271 struct audio_port *ports =
1272 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001273 if (ports == NULL) {
1274 reply->writeInt32(NO_MEMORY);
1275 reply->writeInt32(0);
1276 return NO_ERROR;
1277 }
1278 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001279 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1280 reply->writeInt32(status);
1281 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001282
1283 if (status == NO_ERROR) {
1284 if (numPortsReq > numPorts) {
1285 numPortsReq = numPorts;
1286 }
1287 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1288 reply->writeInt32(generation);
1289 }
1290 free(ports);
1291 return NO_ERROR;
1292 }
1293
1294 case GET_AUDIO_PORT: {
1295 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001296 struct audio_port port = {};
1297 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1298 ALOGE("b/23912202");
1299 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001300 status_t status = getAudioPort(&port);
1301 reply->writeInt32(status);
1302 if (status == NO_ERROR) {
1303 reply->write(&port, sizeof(struct audio_port));
1304 }
1305 return NO_ERROR;
1306 }
1307
1308 case CREATE_AUDIO_PATCH: {
1309 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001310 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001311 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001312 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001313 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1314 ALOGE("b/23912202");
1315 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001316 status_t status = createAudioPatch(&patch, &handle);
1317 reply->writeInt32(status);
1318 if (status == NO_ERROR) {
1319 reply->write(&handle, sizeof(audio_patch_handle_t));
1320 }
1321 return NO_ERROR;
1322 }
1323
1324 case RELEASE_AUDIO_PATCH: {
1325 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001326 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001327 data.read(&handle, sizeof(audio_patch_handle_t));
1328 status_t status = releaseAudioPatch(handle);
1329 reply->writeInt32(status);
1330 return NO_ERROR;
1331 }
1332
1333 case LIST_AUDIO_PATCHES: {
1334 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1335 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001336 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1337 numPatchesReq = MAX_ITEMS_PER_LIST;
1338 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001339 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001340 struct audio_patch *patches =
1341 (struct audio_patch *)calloc(numPatchesReq,
1342 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001343 if (patches == NULL) {
1344 reply->writeInt32(NO_MEMORY);
1345 reply->writeInt32(0);
1346 return NO_ERROR;
1347 }
1348 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001349 status_t status = listAudioPatches(&numPatches, patches, &generation);
1350 reply->writeInt32(status);
1351 reply->writeInt32(numPatches);
1352 if (status == NO_ERROR) {
1353 if (numPatchesReq > numPatches) {
1354 numPatchesReq = numPatches;
1355 }
1356 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1357 reply->writeInt32(generation);
1358 }
1359 free(patches);
1360 return NO_ERROR;
1361 }
1362
1363 case SET_AUDIO_PORT_CONFIG: {
1364 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001365 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001366 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001367 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001368 status_t status = setAudioPortConfig(&config);
1369 reply->writeInt32(status);
1370 return NO_ERROR;
1371 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001372
Eric Laurentb52c1522014-05-20 11:27:36 -07001373 case REGISTER_CLIENT: {
1374 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1375 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1376 data.readStrongBinder());
1377 registerClient(client);
1378 return NO_ERROR;
1379 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001380
Eric Laurente8726fe2015-06-26 09:39:24 -07001381 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1382 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1383 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1384 return NO_ERROR;
1385 } break;
1386
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001387 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1388 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1389 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1390 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001391 audio_session_t session = AUDIO_SESSION_NONE;
1392 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1393 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001394 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1395 reply->writeInt32(status);
1396 if (status == NO_ERROR) {
1397 reply->writeInt32(session);
1398 reply->writeInt32(ioHandle);
1399 reply->writeInt32(device);
1400 }
1401 return NO_ERROR;
1402 } break;
1403
1404 case RELEASE_SOUNDTRIGGER_SESSION: {
1405 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1406 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1407 data.readStrongBinder());
1408 audio_session_t session = (audio_session_t)data.readInt32();
1409 status_t status = releaseSoundTriggerSession(session);
1410 reply->writeInt32(status);
1411 return NO_ERROR;
1412 } break;
1413
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001414 case GET_PHONE_STATE: {
1415 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1416 reply->writeInt32((int32_t)getPhoneState());
1417 return NO_ERROR;
1418 } break;
1419
Eric Laurentbaac1832014-12-01 17:52:59 -08001420 case REGISTER_POLICY_MIXES: {
1421 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1422 bool registration = data.readInt32() == 1;
1423 Vector<AudioMix> mixes;
1424 size_t size = (size_t)data.readInt32();
1425 if (size > MAX_MIXES_PER_POLICY) {
1426 size = MAX_MIXES_PER_POLICY;
1427 }
1428 for (size_t i = 0; i < size; i++) {
1429 AudioMix mix;
1430 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1431 mixes.add(mix);
1432 }
1433 }
1434 status_t status = registerPolicyMixes(mixes, registration);
1435 reply->writeInt32(status);
1436 return NO_ERROR;
1437 } break;
1438
Eric Laurent554a2772015-04-10 11:29:24 -07001439 case START_AUDIO_SOURCE: {
1440 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001441 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001442 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001443 (void)sanitizeAudioPortConfig(&source);
1444 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001445 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001446 sanetizeAudioAttributes(&attributes);
Glenn Kasten559d4392016-03-29 13:42:57 -07001447 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent554a2772015-04-10 11:29:24 -07001448 status_t status = startAudioSource(&source, &attributes, &handle);
1449 reply->writeInt32(status);
1450 reply->writeInt32(handle);
1451 return NO_ERROR;
1452 } break;
1453
1454 case STOP_AUDIO_SOURCE: {
1455 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten559d4392016-03-29 13:42:57 -07001456 audio_patch_handle_t handle = (audio_patch_handle_t) data.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -07001457 status_t status = stopAudioSource(handle);
1458 reply->writeInt32(status);
1459 return NO_ERROR;
1460 } break;
1461
Andy Hung2ddee192015-12-18 17:34:44 -08001462 case SET_MASTER_MONO: {
1463 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1464 bool mono = static_cast<bool>(data.readInt32());
1465 status_t status = setMasterMono(mono);
1466 reply->writeInt32(status);
1467 return NO_ERROR;
1468 } break;
1469
1470 case GET_MASTER_MONO: {
1471 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1472 bool mono;
1473 status_t status = getMasterMono(&mono);
1474 reply->writeInt32(status);
1475 if (status == NO_ERROR) {
1476 reply->writeInt32(static_cast<int32_t>(mono));
1477 }
1478 return NO_ERROR;
1479 } break;
1480
Eric Laurentac9cef52017-06-09 15:46:26 -07001481 case GET_STREAM_VOLUME_DB: {
1482 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1483 audio_stream_type_t stream =
1484 static_cast <audio_stream_type_t>(data.readInt32());
1485 int index = static_cast <int>(data.readInt32());
1486 audio_devices_t device =
1487 static_cast <audio_devices_t>(data.readUint32());
1488 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1489 return NO_ERROR;
1490 }
1491
Eric Laurentc2f1f072009-07-17 12:17:14 -07001492 default:
1493 return BBinder::onTransact(code, data, reply, flags);
1494 }
1495}
1496
Andy Hungb0272092018-04-12 11:06:56 -07001497/** returns true if string overflow was prevented by zero termination */
1498template <size_t size>
1499static bool preventStringOverflow(char (&s)[size]) {
1500 if (strnlen(s, size) < size) return false;
1501 s[size - 1] = '\0';
1502 return true;
1503}
1504
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001505void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
1506{
1507 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
1508 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
1509 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
1510 }
1511 attr->tags[tagsMaxSize - 1] = '\0';
1512}
1513
Andy Hungb0272092018-04-12 11:06:56 -07001514/** returns BAD_VALUE if sanitization was required. */
1515status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
1516{
1517 if (preventStringOverflow(desc->name)
1518 | /* always */ preventStringOverflow(desc->implementor)) {
1519 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
1520 return BAD_VALUE;
1521 }
1522 return NO_ERROR;
1523}
1524
1525/** returns BAD_VALUE if sanitization was required. */
1526status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
1527{
1528 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
1529 preventStringOverflow(config->ext.device.address)) {
1530 return BAD_VALUE;
1531 }
1532 return NO_ERROR;
1533}
1534
Eric Laurentc2f1f072009-07-17 12:17:14 -07001535// ----------------------------------------------------------------------------
1536
Glenn Kasten40bc9062015-03-20 09:09:33 -07001537} // namespace android