blob: abb502b1554a60b2953ff7588cbeae614371d040 [file] [log] [blame]
Eric Laurentc2f1f072009-07-17 12:17:14 -07001/*
2**
3** Copyright 2009, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "IAudioPolicyService"
19#include <utils/Log.h>
20
21#include <stdint.h>
Eric Laurentac9cef52017-06-09 15:46:26 -070022#include <math.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070023#include <sys/types.h>
24
Eric Laurentb1cc36b2017-12-11 12:14:16 -080025#include <binder/IPCThreadState.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070026#include <binder/Parcel.h>
Eric Laurentf1047e82018-04-16 19:18:20 -070027#include <cutils/multiuser.h>
Eric Laurent74adca92014-11-05 12:15:36 -080028#include <media/AudioEffect.h>
Eric Laurentc2f1f072009-07-17 12:17:14 -070029#include <media/IAudioPolicyService.h>
Eric Laurent3528c932018-02-23 17:17:22 -080030#include <media/TimeCheck.h>
Eric Laurent4980df22018-01-26 18:04:09 -080031#include <private/android_filesystem_config.h>
Dima Zavin64760242011-05-11 14:15:23 -070032#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070033
Eric Laurentc2f1f072009-07-17 12:17:14 -070034namespace android {
35
36enum {
37 SET_DEVICE_CONNECTION_STATE = IBinder::FIRST_CALL_TRANSACTION,
38 GET_DEVICE_CONNECTION_STATE,
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -080039 HANDLE_DEVICE_CONFIG_CHANGE,
Eric Laurentc2f1f072009-07-17 12:17:14 -070040 SET_PHONE_STATE,
Glenn Kasten0b07b802012-01-18 14:56:06 -080041 SET_RINGER_MODE, // reserved, no longer used
Eric Laurentc2f1f072009-07-17 12:17:14 -070042 SET_FORCE_USE,
43 GET_FORCE_USE,
44 GET_OUTPUT,
45 START_OUTPUT,
46 STOP_OUTPUT,
47 RELEASE_OUTPUT,
Eric Laurentcaf7f482014-11-25 17:50:47 -080048 GET_INPUT_FOR_ATTR,
Eric Laurentc2f1f072009-07-17 12:17:14 -070049 START_INPUT,
50 STOP_INPUT,
51 RELEASE_INPUT,
52 INIT_STREAM_VOLUME,
53 SET_STREAM_VOLUME,
Eric Laurentde070132010-07-13 04:45:46 -070054 GET_STREAM_VOLUME,
55 GET_STRATEGY_FOR_STREAM,
56 GET_OUTPUT_FOR_EFFECT,
57 REGISTER_EFFECT,
Eric Laurenteda6c362011-02-02 09:33:30 -080058 UNREGISTER_EFFECT,
Glenn Kasten6b2718c2011-02-04 13:54:26 -080059 IS_STREAM_ACTIVE,
Jean-Michel Trivid7086032012-10-10 12:11:16 -070060 IS_SOURCE_ACTIVE,
Glenn Kasten6b2718c2011-02-04 13:54:26 -080061 GET_DEVICES_FOR_STREAM,
Eric Laurentdb7c0792011-08-10 10:37:50 -070062 QUERY_DEFAULT_PRE_PROCESSING,
Jean-Michel Trivi272ab542013-02-04 16:26:02 -080063 SET_EFFECT_ENABLED,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +000064 IS_STREAM_ACTIVE_REMOTELY,
Eric Laurent203b1a12014-04-01 10:34:16 -070065 IS_OFFLOAD_SUPPORTED,
66 LIST_AUDIO_PORTS,
67 GET_AUDIO_PORT,
68 CREATE_AUDIO_PATCH,
69 RELEASE_AUDIO_PATCH,
70 LIST_AUDIO_PATCHES,
Eric Laurentb52c1522014-05-20 11:27:36 -070071 SET_AUDIO_PORT_CONFIG,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -070072 REGISTER_CLIENT,
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070073 GET_OUTPUT_FOR_ATTR,
74 ACQUIRE_SOUNDTRIGGER_SESSION,
Eric Laurentbb6c9a02014-09-25 14:11:47 -070075 RELEASE_SOUNDTRIGGER_SESSION,
Eric Laurentbaac1832014-12-01 17:52:59 -080076 GET_PHONE_STATE,
77 REGISTER_POLICY_MIXES,
Eric Laurent554a2772015-04-10 11:29:24 -070078 START_AUDIO_SOURCE,
Eric Laurente8726fe2015-06-26 09:39:24 -070079 STOP_AUDIO_SOURCE,
80 SET_AUDIO_PORT_CALLBACK_ENABLED,
Andy Hung2ddee192015-12-18 17:34:44 -080081 SET_MASTER_MONO,
82 GET_MASTER_MONO,
Eric Laurentac9cef52017-06-09 15:46:26 -070083 GET_STREAM_VOLUME_DB
Eric Laurentc2f1f072009-07-17 12:17:14 -070084};
85
Eric Laurent1d670b12015-02-06 10:44:24 -080086#define MAX_ITEMS_PER_LIST 1024
87
Eric Laurentc2f1f072009-07-17 12:17:14 -070088class BpAudioPolicyService : public BpInterface<IAudioPolicyService>
89{
90public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070091 explicit BpAudioPolicyService(const sp<IBinder>& impl)
Eric Laurentc2f1f072009-07-17 12:17:14 -070092 : BpInterface<IAudioPolicyService>(impl)
93 {
94 }
95
96 virtual status_t setDeviceConnectionState(
Dima Zavinfce7a472011-04-19 22:30:36 -070097 audio_devices_t device,
98 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080099 const char *device_address,
100 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700101 {
102 Parcel data, reply;
103 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
104 data.writeInt32(static_cast <uint32_t>(device));
105 data.writeInt32(static_cast <uint32_t>(state));
106 data.writeCString(device_address);
Paul McLeane743a472015-01-28 11:07:31 -0800107 data.writeCString(device_name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700108 remote()->transact(SET_DEVICE_CONNECTION_STATE, data, &reply);
109 return static_cast <status_t> (reply.readInt32());
110 }
111
Dima Zavinfce7a472011-04-19 22:30:36 -0700112 virtual audio_policy_dev_state_t getDeviceConnectionState(
113 audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700114 const char *device_address)
115 {
116 Parcel data, reply;
117 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
118 data.writeInt32(static_cast <uint32_t>(device));
119 data.writeCString(device_address);
120 remote()->transact(GET_DEVICE_CONNECTION_STATE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700121 return static_cast <audio_policy_dev_state_t>(reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700122 }
123
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800124 virtual status_t handleDeviceConfigChange(audio_devices_t device,
125 const char *device_address,
126 const char *device_name)
127 {
128 Parcel data, reply;
129 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
130 data.writeInt32(static_cast <uint32_t>(device));
131 data.writeCString(device_address);
132 data.writeCString(device_name);
133 remote()->transact(HANDLE_DEVICE_CONFIG_CHANGE, data, &reply);
134 return static_cast <status_t> (reply.readInt32());
135 }
136
Glenn Kastenf78aee72012-01-04 11:00:47 -0800137 virtual status_t setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700138 {
139 Parcel data, reply;
140 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
141 data.writeInt32(state);
142 remote()->transact(SET_PHONE_STATE, data, &reply);
143 return static_cast <status_t> (reply.readInt32());
144 }
145
Dima Zavinfce7a472011-04-19 22:30:36 -0700146 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700147 {
148 Parcel data, reply;
149 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
150 data.writeInt32(static_cast <uint32_t>(usage));
151 data.writeInt32(static_cast <uint32_t>(config));
152 remote()->transact(SET_FORCE_USE, data, &reply);
153 return static_cast <status_t> (reply.readInt32());
154 }
155
Dima Zavinfce7a472011-04-19 22:30:36 -0700156 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700157 {
158 Parcel data, reply;
159 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
160 data.writeInt32(static_cast <uint32_t>(usage));
161 remote()->transact(GET_FORCE_USE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700162 return static_cast <audio_policy_forced_cfg_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700163 }
164
Eric Laurentf4e63452017-11-06 19:31:46 +0000165 virtual audio_io_handle_t getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700166 {
167 Parcel data, reply;
168 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
169 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700170 remote()->transact(GET_OUTPUT, data, &reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -0700171 return static_cast <audio_io_handle_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700172 }
173
Eric Laurente83b55d2014-11-14 10:06:21 -0800174 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
175 audio_io_handle_t *output,
176 audio_session_t session,
177 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200178 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700179 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800180 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800181 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700182 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800183 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700184 {
185 Parcel data, reply;
186 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
187 if (attr == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800188 if (stream == NULL) {
189 ALOGE("getOutputForAttr(): NULL audio attributes and stream type");
190 return BAD_VALUE;
191 }
192 if (*stream == AUDIO_STREAM_DEFAULT) {
193 ALOGE("getOutputForAttr unspecified stream type");
194 return BAD_VALUE;
195 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700196 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800197 if (output == NULL) {
198 ALOGE("getOutputForAttr NULL output - shouldn't happen");
199 return BAD_VALUE;
200 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700201 if (selectedDeviceId == NULL) {
202 ALOGE("getOutputForAttr NULL selectedDeviceId - shouldn't happen");
203 return BAD_VALUE;
204 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800205 if (portId == NULL) {
206 ALOGE("getOutputForAttr NULL portId - shouldn't happen");
207 return BAD_VALUE;
208 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800209 if (attr == NULL) {
210 data.writeInt32(0);
211 } else {
212 data.writeInt32(1);
213 data.write(attr, sizeof(audio_attributes_t));
214 }
215 data.writeInt32(session);
216 if (stream == NULL) {
217 data.writeInt32(0);
218 } else {
219 data.writeInt32(1);
220 data.writeInt32(*stream);
221 }
Nadav Bar766fb022018-01-07 12:18:03 +0200222 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700223 data.writeInt32(uid);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800224 data.write(config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700225 data.writeInt32(static_cast <uint32_t>(flags));
Eric Laurent9ae8c592017-06-22 17:17:09 -0700226 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800227 data.writeInt32(*portId);
Eric Laurente83b55d2014-11-14 10:06:21 -0800228 status_t status = remote()->transact(GET_OUTPUT_FOR_ATTR, data, &reply);
229 if (status != NO_ERROR) {
230 return status;
231 }
232 status = (status_t)reply.readInt32();
233 if (status != NO_ERROR) {
234 return status;
235 }
236 *output = (audio_io_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800237 audio_stream_type_t lStream = (audio_stream_type_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800238 if (stream != NULL) {
Eric Laurent20b9ef02016-12-05 11:03:16 -0800239 *stream = lStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800240 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700241 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800242 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800243 return status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700244 }
245
Eric Laurentde070132010-07-13 04:45:46 -0700246 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700247 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800248 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700249 {
250 Parcel data, reply;
251 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfa2877b2009-07-28 08:44:33 -0700252 data.writeInt32(output);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800253 data.writeInt32((int32_t) stream);
Glenn Kastend848eb42016-03-08 13:42:11 -0800254 data.writeInt32((int32_t) session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700255 remote()->transact(START_OUTPUT, data, &reply);
256 return static_cast <status_t> (reply.readInt32());
257 }
258
Eric Laurentde070132010-07-13 04:45:46 -0700259 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700260 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800261 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700262 {
263 Parcel data, reply;
264 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfa2877b2009-07-28 08:44:33 -0700265 data.writeInt32(output);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800266 data.writeInt32((int32_t) stream);
Glenn Kastend848eb42016-03-08 13:42:11 -0800267 data.writeInt32((int32_t) session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700268 remote()->transact(STOP_OUTPUT, data, &reply);
269 return static_cast <status_t> (reply.readInt32());
270 }
271
Eric Laurente83b55d2014-11-14 10:06:21 -0800272 virtual void releaseOutput(audio_io_handle_t output,
273 audio_stream_type_t stream,
274 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700275 {
276 Parcel data, reply;
277 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfa2877b2009-07-28 08:44:33 -0700278 data.writeInt32(output);
Eric Laurente83b55d2014-11-14 10:06:21 -0800279 data.writeInt32((int32_t)stream);
280 data.writeInt32((int32_t)session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700281 remote()->transact(RELEASE_OUTPUT, data, &reply);
282 }
283
Eric Laurentcaf7f482014-11-25 17:50:47 -0800284 virtual status_t getInputForAttr(const audio_attributes_t *attr,
285 audio_io_handle_t *input,
286 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700287 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700288 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800289 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800290 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600291 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700292 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800293 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700294 {
295 Parcel data, reply;
296 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentcaf7f482014-11-25 17:50:47 -0800297 if (attr == NULL) {
298 ALOGE("getInputForAttr NULL attr - shouldn't happen");
299 return BAD_VALUE;
300 }
301 if (input == NULL) {
302 ALOGE("getInputForAttr NULL input - shouldn't happen");
303 return BAD_VALUE;
304 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700305 if (selectedDeviceId == NULL) {
306 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
307 return BAD_VALUE;
308 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800309 if (portId == NULL) {
310 ALOGE("getInputForAttr NULL portId - shouldn't happen");
311 return BAD_VALUE;
312 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800313 data.write(attr, sizeof(audio_attributes_t));
Eric Laurenta54f1282017-07-01 19:39:32 -0700314 data.writeInt32(*input);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800315 data.writeInt32(session);
Eric Laurentb2379ba2016-05-23 17:42:12 -0700316 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700317 data.writeInt32(uid);
Eric Laurentfee19762018-01-29 18:44:13 -0800318 data.writeString16(opPackageName);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800319 data.write(config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -0700320 data.writeInt32(flags);
Eric Laurent9ae8c592017-06-22 17:17:09 -0700321 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800322 data.writeInt32(*portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800323 status_t status = remote()->transact(GET_INPUT_FOR_ATTR, data, &reply);
324 if (status != NO_ERROR) {
325 return status;
326 }
327 status = reply.readInt32();
328 if (status != NO_ERROR) {
329 return status;
330 }
331 *input = (audio_io_handle_t)reply.readInt32();
Eric Laurent9ae8c592017-06-22 17:17:09 -0700332 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800333 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800334 return NO_ERROR;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700335 }
336
Eric Laurentfee19762018-01-29 18:44:13 -0800337 virtual status_t startInput(audio_port_handle_t portId,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800338 bool *silenced)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700339 {
340 Parcel data, reply;
341 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800342 data.writeInt32(portId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800343 data.writeInt32(*silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700344 remote()->transact(START_INPUT, data, &reply);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800345 status_t status = static_cast <status_t> (reply.readInt32());
346 *silenced = reply.readInt32() == 1;
347 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700348 }
349
Eric Laurentfee19762018-01-29 18:44:13 -0800350 virtual status_t stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700351 {
352 Parcel data, reply;
353 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800354 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700355 remote()->transact(STOP_INPUT, data, &reply);
356 return static_cast <status_t> (reply.readInt32());
357 }
358
Eric Laurentfee19762018-01-29 18:44:13 -0800359 virtual void releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700360 {
361 Parcel data, reply;
362 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800363 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700364 remote()->transact(RELEASE_INPUT, data, &reply);
365 }
366
Dima Zavinfce7a472011-04-19 22:30:36 -0700367 virtual status_t initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700368 int indexMin,
369 int indexMax)
370 {
371 Parcel data, reply;
372 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
373 data.writeInt32(static_cast <uint32_t>(stream));
374 data.writeInt32(indexMin);
375 data.writeInt32(indexMax);
376 remote()->transact(INIT_STREAM_VOLUME, data, &reply);
377 return static_cast <status_t> (reply.readInt32());
378 }
379
Eric Laurent83844cc2011-11-18 16:43:31 -0800380 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
381 int index,
382 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700383 {
384 Parcel data, reply;
385 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
386 data.writeInt32(static_cast <uint32_t>(stream));
387 data.writeInt32(index);
Eric Laurent83844cc2011-11-18 16:43:31 -0800388 data.writeInt32(static_cast <uint32_t>(device));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700389 remote()->transact(SET_STREAM_VOLUME, data, &reply);
390 return static_cast <status_t> (reply.readInt32());
391 }
392
Eric Laurent83844cc2011-11-18 16:43:31 -0800393 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
394 int *index,
395 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700396 {
397 Parcel data, reply;
398 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
399 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurent83844cc2011-11-18 16:43:31 -0800400 data.writeInt32(static_cast <uint32_t>(device));
401
Eric Laurentc2f1f072009-07-17 12:17:14 -0700402 remote()->transact(GET_STREAM_VOLUME, data, &reply);
403 int lIndex = reply.readInt32();
404 if (index) *index = lIndex;
405 return static_cast <status_t> (reply.readInt32());
406 }
Eric Laurentde070132010-07-13 04:45:46 -0700407
Dima Zavinfce7a472011-04-19 22:30:36 -0700408 virtual uint32_t getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700409 {
410 Parcel data, reply;
411 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
412 data.writeInt32(static_cast <uint32_t>(stream));
413 remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply);
414 return reply.readInt32();
415 }
416
Eric Laurent63742522012-03-08 13:42:42 -0800417 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800418 {
419 Parcel data, reply;
420 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
421 data.writeInt32(static_cast <uint32_t>(stream));
422 remote()->transact(GET_DEVICES_FOR_STREAM, data, &reply);
Eric Laurent63742522012-03-08 13:42:42 -0800423 return (audio_devices_t) reply.readInt32();
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800424 }
425
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700426 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700427 {
428 Parcel data, reply;
429 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
430 data.write(desc, sizeof(effect_descriptor_t));
431 remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply);
432 return static_cast <audio_io_handle_t> (reply.readInt32());
433 }
434
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700435 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700436 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700437 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800438 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700439 int id)
440 {
441 Parcel data, reply;
442 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
443 data.write(desc, sizeof(effect_descriptor_t));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700444 data.writeInt32(io);
Eric Laurentde070132010-07-13 04:45:46 -0700445 data.writeInt32(strategy);
446 data.writeInt32(session);
447 data.writeInt32(id);
448 remote()->transact(REGISTER_EFFECT, data, &reply);
449 return static_cast <status_t> (reply.readInt32());
450 }
451
452 virtual status_t unregisterEffect(int id)
453 {
454 Parcel data, reply;
455 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
456 data.writeInt32(id);
457 remote()->transact(UNREGISTER_EFFECT, data, &reply);
458 return static_cast <status_t> (reply.readInt32());
459 }
460
Eric Laurentdb7c0792011-08-10 10:37:50 -0700461 virtual status_t setEffectEnabled(int id, bool enabled)
462 {
463 Parcel data, reply;
464 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
465 data.writeInt32(id);
466 data.writeInt32(enabled);
467 remote()->transact(SET_EFFECT_ENABLED, data, &reply);
468 return static_cast <status_t> (reply.readInt32());
469 }
470
Glenn Kastenfff6d712012-01-12 16:38:12 -0800471 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800472 {
473 Parcel data, reply;
474 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Glenn Kastenfff6d712012-01-12 16:38:12 -0800475 data.writeInt32((int32_t) stream);
Eric Laurenteda6c362011-02-02 09:33:30 -0800476 data.writeInt32(inPastMs);
477 remote()->transact(IS_STREAM_ACTIVE, data, &reply);
478 return reply.readInt32();
479 }
Eric Laurent57dae992011-07-24 13:36:09 -0700480
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800481 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
482 {
483 Parcel data, reply;
484 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
485 data.writeInt32((int32_t) stream);
486 data.writeInt32(inPastMs);
487 remote()->transact(IS_STREAM_ACTIVE_REMOTELY, data, &reply);
488 return reply.readInt32();
489 }
490
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700491 virtual bool isSourceActive(audio_source_t source) const
492 {
493 Parcel data, reply;
494 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
495 data.writeInt32((int32_t) source);
496 remote()->transact(IS_SOURCE_ACTIVE, data, &reply);
497 return reply.readInt32();
498 }
499
Glenn Kastend848eb42016-03-08 13:42:11 -0800500 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700501 effect_descriptor_t *descriptors,
502 uint32_t *count)
503 {
504 if (descriptors == NULL || count == NULL) {
505 return BAD_VALUE;
506 }
507 Parcel data, reply;
508 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
509 data.writeInt32(audioSession);
510 data.writeInt32(*count);
511 status_t status = remote()->transact(QUERY_DEFAULT_PRE_PROCESSING, data, &reply);
512 if (status != NO_ERROR) {
513 return status;
514 }
515 status = static_cast <status_t> (reply.readInt32());
516 uint32_t retCount = reply.readInt32();
517 if (retCount != 0) {
518 uint32_t numDesc = (retCount < *count) ? retCount : *count;
519 reply.read(descriptors, sizeof(effect_descriptor_t) * numDesc);
520 }
521 *count = retCount;
522 return status;
523 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000524
525 virtual bool isOffloadSupported(const audio_offload_info_t& info)
526 {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100527 Parcel data, reply;
528 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
529 data.write(&info, sizeof(audio_offload_info_t));
530 remote()->transact(IS_OFFLOAD_SUPPORTED, data, &reply);
Eric Laurent203b1a12014-04-01 10:34:16 -0700531 return reply.readInt32();
532 }
533
534 virtual status_t listAudioPorts(audio_port_role_t role,
535 audio_port_type_t type,
536 unsigned int *num_ports,
537 struct audio_port *ports,
538 unsigned int *generation)
539 {
540 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
541 generation == NULL) {
542 return BAD_VALUE;
543 }
544 Parcel data, reply;
545 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
546 unsigned int numPortsReq = (ports == NULL) ? 0 : *num_ports;
547 data.writeInt32(role);
548 data.writeInt32(type);
549 data.writeInt32(numPortsReq);
550 status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply);
551 if (status == NO_ERROR) {
552 status = (status_t)reply.readInt32();
553 *num_ports = (unsigned int)reply.readInt32();
554 }
Eric Laurent203b1a12014-04-01 10:34:16 -0700555 if (status == NO_ERROR) {
556 if (numPortsReq > *num_ports) {
557 numPortsReq = *num_ports;
558 }
559 if (numPortsReq > 0) {
560 reply.read(ports, numPortsReq * sizeof(struct audio_port));
561 }
562 *generation = reply.readInt32();
563 }
564 return status;
565 }
566
567 virtual status_t getAudioPort(struct audio_port *port)
568 {
569 if (port == NULL) {
570 return BAD_VALUE;
571 }
572 Parcel data, reply;
573 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
574 data.write(port, sizeof(struct audio_port));
575 status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply);
576 if (status != NO_ERROR ||
577 (status = (status_t)reply.readInt32()) != NO_ERROR) {
578 return status;
579 }
580 reply.read(port, sizeof(struct audio_port));
581 return status;
582 }
583
584 virtual status_t createAudioPatch(const struct audio_patch *patch,
585 audio_patch_handle_t *handle)
586 {
587 if (patch == NULL || handle == NULL) {
588 return BAD_VALUE;
589 }
590 Parcel data, reply;
591 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
592 data.write(patch, sizeof(struct audio_patch));
593 data.write(handle, sizeof(audio_patch_handle_t));
594 status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply);
595 if (status != NO_ERROR ||
596 (status = (status_t)reply.readInt32()) != NO_ERROR) {
597 return status;
598 }
599 reply.read(handle, sizeof(audio_patch_handle_t));
600 return status;
601 }
602
603 virtual status_t releaseAudioPatch(audio_patch_handle_t handle)
604 {
605 Parcel data, reply;
606 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
607 data.write(&handle, sizeof(audio_patch_handle_t));
608 status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply);
609 if (status != NO_ERROR) {
610 status = (status_t)reply.readInt32();
611 }
612 return status;
613 }
614
615 virtual status_t listAudioPatches(unsigned int *num_patches,
616 struct audio_patch *patches,
617 unsigned int *generation)
618 {
619 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
620 generation == NULL) {
621 return BAD_VALUE;
622 }
623 Parcel data, reply;
624 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
625 unsigned int numPatchesReq = (patches == NULL) ? 0 : *num_patches;
626 data.writeInt32(numPatchesReq);
627 status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply);
628 if (status == NO_ERROR) {
629 status = (status_t)reply.readInt32();
630 *num_patches = (unsigned int)reply.readInt32();
631 }
632 if (status == NO_ERROR) {
633 if (numPatchesReq > *num_patches) {
634 numPatchesReq = *num_patches;
635 }
636 if (numPatchesReq > 0) {
637 reply.read(patches, numPatchesReq * sizeof(struct audio_patch));
638 }
639 *generation = reply.readInt32();
640 }
641 return status;
642 }
643
644 virtual status_t setAudioPortConfig(const struct audio_port_config *config)
645 {
646 if (config == NULL) {
647 return BAD_VALUE;
648 }
649 Parcel data, reply;
650 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
651 data.write(config, sizeof(struct audio_port_config));
652 status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply);
653 if (status != NO_ERROR) {
654 status = (status_t)reply.readInt32();
655 }
656 return status;
657 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700658
Eric Laurentb52c1522014-05-20 11:27:36 -0700659 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client)
660 {
661 Parcel data, reply;
662 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -0800663 data.writeStrongBinder(IInterface::asBinder(client));
Eric Laurentb52c1522014-05-20 11:27:36 -0700664 remote()->transact(REGISTER_CLIENT, data, &reply);
665 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700666
Eric Laurente8726fe2015-06-26 09:39:24 -0700667 virtual void setAudioPortCallbacksEnabled(bool enabled)
668 {
669 Parcel data, reply;
670 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
671 data.writeInt32(enabled ? 1 : 0);
672 remote()->transact(SET_AUDIO_PORT_CALLBACK_ENABLED, data, &reply);
673 }
674
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700675 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
676 audio_io_handle_t *ioHandle,
677 audio_devices_t *device)
678 {
679 if (session == NULL || ioHandle == NULL || device == NULL) {
680 return BAD_VALUE;
681 }
682 Parcel data, reply;
683 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
684 status_t status = remote()->transact(ACQUIRE_SOUNDTRIGGER_SESSION, data, &reply);
685 if (status != NO_ERROR) {
686 return status;
687 }
688 status = (status_t)reply.readInt32();
689 if (status == NO_ERROR) {
690 *session = (audio_session_t)reply.readInt32();
691 *ioHandle = (audio_io_handle_t)reply.readInt32();
692 *device = (audio_devices_t)reply.readInt32();
693 }
694 return status;
695 }
696
697 virtual status_t releaseSoundTriggerSession(audio_session_t session)
698 {
699 Parcel data, reply;
700 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
701 data.writeInt32(session);
702 status_t status = remote()->transact(RELEASE_SOUNDTRIGGER_SESSION, data, &reply);
703 if (status != NO_ERROR) {
704 return status;
705 }
706 return (status_t)reply.readInt32();
707 }
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700708
709 virtual audio_mode_t getPhoneState()
710 {
711 Parcel data, reply;
712 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
713 status_t status = remote()->transact(GET_PHONE_STATE, data, &reply);
714 if (status != NO_ERROR) {
715 return AUDIO_MODE_INVALID;
716 }
717 return (audio_mode_t)reply.readInt32();
718 }
Eric Laurentbaac1832014-12-01 17:52:59 -0800719
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700720 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800721 {
722 Parcel data, reply;
723 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
724 data.writeInt32(registration ? 1 : 0);
725 size_t size = mixes.size();
726 if (size > MAX_MIXES_PER_POLICY) {
727 size = MAX_MIXES_PER_POLICY;
728 }
729 size_t sizePosition = data.dataPosition();
730 data.writeInt32(size);
731 size_t finalSize = size;
732 for (size_t i = 0; i < size; i++) {
733 size_t position = data.dataPosition();
734 if (mixes[i].writeToParcel(&data) != NO_ERROR) {
735 data.setDataPosition(position);
736 finalSize--;
737 }
738 }
739 if (size != finalSize) {
740 size_t position = data.dataPosition();
741 data.setDataPosition(sizePosition);
742 data.writeInt32(finalSize);
743 data.setDataPosition(position);
744 }
745 status_t status = remote()->transact(REGISTER_POLICY_MIXES, data, &reply);
746 if (status == NO_ERROR) {
747 status = (status_t)reply.readInt32();
748 }
749 return status;
750 }
Eric Laurent554a2772015-04-10 11:29:24 -0700751
752 virtual status_t startAudioSource(const struct audio_port_config *source,
753 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -0700754 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700755 {
756 Parcel data, reply;
757 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
758 if (source == NULL || attributes == NULL || handle == NULL) {
759 return BAD_VALUE;
760 }
761 data.write(source, sizeof(struct audio_port_config));
762 data.write(attributes, sizeof(audio_attributes_t));
763 status_t status = remote()->transact(START_AUDIO_SOURCE, data, &reply);
764 if (status != NO_ERROR) {
765 return status;
766 }
767 status = (status_t)reply.readInt32();
768 if (status != NO_ERROR) {
769 return status;
770 }
Glenn Kasten559d4392016-03-29 13:42:57 -0700771 *handle = (audio_patch_handle_t)reply.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -0700772 return status;
773 }
774
Glenn Kasten559d4392016-03-29 13:42:57 -0700775 virtual status_t stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700776 {
777 Parcel data, reply;
778 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
779 data.writeInt32(handle);
780 status_t status = remote()->transact(STOP_AUDIO_SOURCE, data, &reply);
781 if (status != NO_ERROR) {
782 return status;
783 }
784 status = (status_t)reply.readInt32();
785 return status;
786 }
Andy Hung2ddee192015-12-18 17:34:44 -0800787
788 virtual status_t setMasterMono(bool mono)
789 {
790 Parcel data, reply;
791 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
792 data.writeInt32(static_cast<int32_t>(mono));
793 status_t status = remote()->transact(SET_MASTER_MONO, data, &reply);
794 if (status != NO_ERROR) {
795 return status;
796 }
797 return static_cast<status_t>(reply.readInt32());
798 }
799
800 virtual status_t getMasterMono(bool *mono)
801 {
802 if (mono == nullptr) {
803 return BAD_VALUE;
804 }
805 Parcel data, reply;
806 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
807
808 status_t status = remote()->transact(GET_MASTER_MONO, data, &reply);
809 if (status != NO_ERROR) {
810 return status;
811 }
812 status = static_cast<status_t>(reply.readInt32());
813 if (status == NO_ERROR) {
814 *mono = static_cast<bool>(reply.readInt32());
815 }
816 return status;
817 }
Eric Laurentac9cef52017-06-09 15:46:26 -0700818
819 virtual float getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
820 {
821 Parcel data, reply;
822 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
823 data.writeInt32(static_cast <int32_t>(stream));
824 data.writeInt32(static_cast <int32_t>(index));
825 data.writeUint32(static_cast <uint32_t>(device));
826 status_t status = remote()->transact(GET_STREAM_VOLUME_DB, data, &reply);
827 if (status != NO_ERROR) {
828 return NAN;
829 }
830 return reply.readFloat();
831 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700832};
833
834IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
835
836// ----------------------------------------------------------------------
837
Eric Laurentc2f1f072009-07-17 12:17:14 -0700838status_t BnAudioPolicyService::onTransact(
839 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
840{
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800841 // make sure transactions reserved to AudioFlinger do not come from other processes
842 switch (code) {
843 case START_OUTPUT:
844 case STOP_OUTPUT:
845 case RELEASE_OUTPUT:
846 case GET_INPUT_FOR_ATTR:
847 case START_INPUT:
848 case STOP_INPUT:
849 case RELEASE_INPUT:
850 case GET_STRATEGY_FOR_STREAM:
851 case GET_OUTPUT_FOR_EFFECT:
852 case REGISTER_EFFECT:
853 case UNREGISTER_EFFECT:
854 case SET_EFFECT_ENABLED:
855 case GET_OUTPUT_FOR_ATTR:
856 case ACQUIRE_SOUNDTRIGGER_SESSION:
857 case RELEASE_SOUNDTRIGGER_SESSION:
858 ALOGW("%s: transaction %d received from PID %d",
859 __func__, code, IPCThreadState::self()->getCallingPid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700860 // return status only for non void methods
861 switch (code) {
862 case RELEASE_OUTPUT:
863 case RELEASE_INPUT:
864 break;
865 default:
866 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
867 break;
868 }
869 return OK;
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800870 default:
871 break;
872 }
873
Eric Laurent4980df22018-01-26 18:04:09 -0800874 // make sure the following transactions come from system components
875 switch (code) {
876 case SET_DEVICE_CONNECTION_STATE:
877 case HANDLE_DEVICE_CONFIG_CHANGE:
878 case SET_PHONE_STATE:
Eric Laurent4980df22018-01-26 18:04:09 -0800879 case SET_FORCE_USE:
880 case INIT_STREAM_VOLUME:
881 case SET_STREAM_VOLUME:
882 case REGISTER_POLICY_MIXES:
Eric Laurent10b71232018-04-13 18:14:44 -0700883 case SET_MASTER_MONO:
884 case START_AUDIO_SOURCE:
885 case STOP_AUDIO_SOURCE: {
Eric Laurentf1047e82018-04-16 19:18:20 -0700886 if (multiuser_get_app_id(IPCThreadState::self()->getCallingUid()) >= AID_APP_START) {
Eric Laurent4980df22018-01-26 18:04:09 -0800887 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
888 __func__, code, IPCThreadState::self()->getCallingPid(),
889 IPCThreadState::self()->getCallingUid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700890 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
891 return OK;
Eric Laurent4980df22018-01-26 18:04:09 -0800892 }
Eric Laurent96c7eed2018-03-26 17:57:01 -0700893 } break;
Eric Laurent4980df22018-01-26 18:04:09 -0800894 default:
895 break;
896 }
897
Eric Laurent09e621a2018-03-26 16:50:47 -0700898 // FIXME: extend timeout for SET_DEVICE_CONNECTION_STATE and HANDLE_DEVICE_CONFIG_CHANGE
899 // while we investigate why BT A2DP device connection/disconnection can sometimes
900 // take more than 5 seconds
901 uint32_t timeoutMs = TimeCheck::kDefaultTimeOutMs;
902 switch (code) {
903 case SET_DEVICE_CONNECTION_STATE:
904 case HANDLE_DEVICE_CONFIG_CHANGE:
905 timeoutMs *= 2;
906 break;
907 default:
908 break;
909 }
910
911 TimeCheck check("IAudioPolicyService", timeoutMs);
Eric Laurent3528c932018-02-23 17:17:22 -0800912
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700913 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700914 case SET_DEVICE_CONNECTION_STATE: {
915 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700916 audio_devices_t device =
917 static_cast <audio_devices_t>(data.readInt32());
918 audio_policy_dev_state_t state =
919 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700920 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -0800921 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800922 if (device_address == nullptr || device_name == nullptr) {
923 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
924 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
925 } else {
926 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
927 state,
928 device_address,
929 device_name)));
930 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700931 return NO_ERROR;
932 } break;
933
934 case GET_DEVICE_CONNECTION_STATE: {
935 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700936 audio_devices_t device =
937 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700938 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800939 if (device_address == nullptr) {
940 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
941 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
942 } else {
943 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
944 device_address)));
945 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700946 return NO_ERROR;
947 } break;
948
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800949 case HANDLE_DEVICE_CONFIG_CHANGE: {
950 CHECK_INTERFACE(IAudioPolicyService, data, reply);
951 audio_devices_t device =
952 static_cast <audio_devices_t>(data.readInt32());
953 const char *device_address = data.readCString();
954 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800955 if (device_address == nullptr || device_name == nullptr) {
956 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
957 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
958 } else {
959 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
960 device_address,
961 device_name)));
962 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800963 return NO_ERROR;
964 } break;
965
Eric Laurentc2f1f072009-07-17 12:17:14 -0700966 case SET_PHONE_STATE: {
967 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700968 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
969 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700970 return NO_ERROR;
971 } break;
972
Eric Laurentc2f1f072009-07-17 12:17:14 -0700973 case SET_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());
Dima Zavinfce7a472011-04-19 22:30:36 -0700977 audio_policy_forced_cfg_t config =
978 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700979 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
980 return NO_ERROR;
981 } break;
982
983 case GET_FORCE_USE: {
984 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700985 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
986 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700987 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
988 return NO_ERROR;
989 } break;
990
991 case GET_OUTPUT: {
992 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700993 audio_stream_type_t stream =
994 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +0000995 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -0700996 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700997 return NO_ERROR;
998 } break;
999
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001000 case GET_OUTPUT_FOR_ATTR: {
1001 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001002 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -08001003 bool hasAttributes = data.readInt32() != 0;
1004 if (hasAttributes) {
1005 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001006 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -08001007 }
1008 audio_session_t session = (audio_session_t)data.readInt32();
1009 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
1010 bool hasStream = data.readInt32() != 0;
1011 if (hasStream) {
1012 stream = (audio_stream_type_t)data.readInt32();
1013 }
Nadav Bar766fb022018-01-07 12:18:03 +02001014 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001015 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001016 audio_config_t config;
1017 memset(&config, 0, sizeof(audio_config_t));
1018 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001019 audio_output_flags_t flags =
1020 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001021 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001022 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001023 audio_io_handle_t output = 0;
Eric Laurente83b55d2014-11-14 10:06:21 -08001024 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001025 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001026 &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001027 flags, &selectedDeviceId, &portId);
Eric Laurente83b55d2014-11-14 10:06:21 -08001028 reply->writeInt32(status);
1029 reply->writeInt32(output);
1030 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001031 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001032 reply->writeInt32(portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001033 return NO_ERROR;
1034 } break;
1035
Eric Laurentc2f1f072009-07-17 12:17:14 -07001036 case START_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>(startOutput(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 STOP_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 Laurentebcb2542014-03-05 18:30:08 -08001051 audio_stream_type_t stream =
1052 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001053 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001054 reply->writeInt32(static_cast <uint32_t>(stopOutput(output,
Eric Laurentebcb2542014-03-05 18:30:08 -08001055 stream,
Eric Laurentde070132010-07-13 04:45:46 -07001056 session)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001057 return NO_ERROR;
1058 } break;
1059
1060 case RELEASE_OUTPUT: {
1061 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001062 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001063 audio_stream_type_t stream = (audio_stream_type_t)data.readInt32();
1064 audio_session_t session = (audio_session_t)data.readInt32();
1065 releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001066 return NO_ERROR;
1067 } break;
1068
Eric Laurentcaf7f482014-11-25 17:50:47 -08001069 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001070 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001071 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001072 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001073 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001074 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001075 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001076 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001077 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001078 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001079 audio_config_base_t config;
1080 memset(&config, 0, sizeof(audio_config_base_t));
1081 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001082 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001083 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001084 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001085 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001086 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001087 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001088 reply->writeInt32(status);
1089 if (status == NO_ERROR) {
1090 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001091 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001092 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001093 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001094 return NO_ERROR;
1095 } break;
1096
1097 case START_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());
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001100 bool silenced = data.readInt32() == 1;
Eric Laurentfee19762018-01-29 18:44:13 -08001101 status_t status = startInput(portId, &silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001102 reply->writeInt32(static_cast <uint32_t>(status));
1103 reply->writeInt32(silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001104 return NO_ERROR;
1105 } break;
1106
1107 case STOP_INPUT: {
1108 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001109 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1110 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001111 return NO_ERROR;
1112 } break;
1113
1114 case RELEASE_INPUT: {
1115 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001116 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1117 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001118 return NO_ERROR;
1119 } break;
1120
1121 case INIT_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 indexMin = data.readInt32();
1126 int indexMax = data.readInt32();
1127 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1128 return NO_ERROR;
1129 } break;
1130
1131 case SET_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 Laurentc2f1f072009-07-17 12:17:14 -07001135 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001136 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1137 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1138 index,
1139 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001140 return NO_ERROR;
1141 } break;
1142
1143 case GET_STREAM_VOLUME: {
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 Laurent83844cc2011-11-18 16:43:31 -08001147 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001148 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001149 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001150 reply->writeInt32(index);
1151 reply->writeInt32(static_cast <uint32_t>(status));
1152 return NO_ERROR;
1153 } break;
1154
Eric Laurentde070132010-07-13 04:45:46 -07001155 case GET_STRATEGY_FOR_STREAM: {
1156 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001157 audio_stream_type_t stream =
1158 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentde070132010-07-13 04:45:46 -07001159 reply->writeInt32(getStrategyForStream(stream));
1160 return NO_ERROR;
1161 } break;
1162
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001163 case GET_DEVICES_FOR_STREAM: {
1164 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001165 audio_stream_type_t stream =
1166 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001167 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1168 return NO_ERROR;
1169 } break;
1170
Eric Laurentde070132010-07-13 04:45:46 -07001171 case GET_OUTPUT_FOR_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 Laurentde070132010-07-13 04:45:46 -07001178 audio_io_handle_t output = getOutputForEffect(&desc);
1179 reply->writeInt32(static_cast <int>(output));
1180 return NO_ERROR;
1181 } break;
1182
1183 case REGISTER_EFFECT: {
1184 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001185 effect_descriptor_t desc = {};
1186 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1187 android_errorWriteLog(0x534e4554, "73126106");
1188 }
1189 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001190 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001191 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001192 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001193 int id = data.readInt32();
1194 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001195 io,
Eric Laurentde070132010-07-13 04:45:46 -07001196 strategy,
1197 session,
1198 id)));
1199 return NO_ERROR;
1200 } break;
1201
1202 case UNREGISTER_EFFECT: {
1203 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1204 int id = data.readInt32();
1205 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1206 return NO_ERROR;
1207 } break;
1208
Eric Laurentdb7c0792011-08-10 10:37:50 -07001209 case SET_EFFECT_ENABLED: {
1210 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1211 int id = data.readInt32();
1212 bool enabled = static_cast <bool>(data.readInt32());
1213 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1214 return NO_ERROR;
1215 } break;
1216
Eric Laurenteda6c362011-02-02 09:33:30 -08001217 case IS_STREAM_ACTIVE: {
1218 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001219 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001220 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001221 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001222 return NO_ERROR;
1223 } break;
1224
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001225 case IS_STREAM_ACTIVE_REMOTELY: {
1226 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1227 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1228 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001229 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001230 return NO_ERROR;
1231 } break;
1232
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001233 case IS_SOURCE_ACTIVE: {
1234 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1235 audio_source_t source = (audio_source_t) data.readInt32();
1236 reply->writeInt32( isSourceActive(source));
1237 return NO_ERROR;
1238 }
1239
Eric Laurent57dae992011-07-24 13:36:09 -07001240 case QUERY_DEFAULT_PRE_PROCESSING: {
1241 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001242 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001243 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001244 if (count > AudioEffect::kMaxPreProcessing) {
1245 count = AudioEffect::kMaxPreProcessing;
1246 }
Eric Laurent57dae992011-07-24 13:36:09 -07001247 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001248 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001249 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1250 reply->writeInt32(status);
1251 if (status != NO_ERROR && status != NO_MEMORY) {
1252 retCount = 0;
1253 }
1254 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001255 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001256 if (retCount < count) {
1257 count = retCount;
1258 }
1259 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1260 }
1261 delete[] descriptors;
1262 return status;
1263 }
1264
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001265 case IS_OFFLOAD_SUPPORTED: {
1266 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001267 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001268 data.read(&info, sizeof(audio_offload_info_t));
1269 bool isSupported = isOffloadSupported(info);
1270 reply->writeInt32(isSupported);
1271 return NO_ERROR;
1272 }
1273
Eric Laurent203b1a12014-04-01 10:34:16 -07001274 case LIST_AUDIO_PORTS: {
1275 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1276 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1277 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1278 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001279 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1280 numPortsReq = MAX_ITEMS_PER_LIST;
1281 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001282 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001283 struct audio_port *ports =
1284 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001285 if (ports == NULL) {
1286 reply->writeInt32(NO_MEMORY);
1287 reply->writeInt32(0);
1288 return NO_ERROR;
1289 }
1290 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001291 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1292 reply->writeInt32(status);
1293 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001294
1295 if (status == NO_ERROR) {
1296 if (numPortsReq > numPorts) {
1297 numPortsReq = numPorts;
1298 }
1299 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1300 reply->writeInt32(generation);
1301 }
1302 free(ports);
1303 return NO_ERROR;
1304 }
1305
1306 case GET_AUDIO_PORT: {
1307 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001308 struct audio_port port = {};
1309 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1310 ALOGE("b/23912202");
1311 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001312 status_t status = getAudioPort(&port);
1313 reply->writeInt32(status);
1314 if (status == NO_ERROR) {
1315 reply->write(&port, sizeof(struct audio_port));
1316 }
1317 return NO_ERROR;
1318 }
1319
1320 case CREATE_AUDIO_PATCH: {
1321 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001322 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001323 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001324 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001325 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1326 ALOGE("b/23912202");
1327 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001328 status_t status = createAudioPatch(&patch, &handle);
1329 reply->writeInt32(status);
1330 if (status == NO_ERROR) {
1331 reply->write(&handle, sizeof(audio_patch_handle_t));
1332 }
1333 return NO_ERROR;
1334 }
1335
1336 case RELEASE_AUDIO_PATCH: {
1337 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001338 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001339 data.read(&handle, sizeof(audio_patch_handle_t));
1340 status_t status = releaseAudioPatch(handle);
1341 reply->writeInt32(status);
1342 return NO_ERROR;
1343 }
1344
1345 case LIST_AUDIO_PATCHES: {
1346 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1347 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001348 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1349 numPatchesReq = MAX_ITEMS_PER_LIST;
1350 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001351 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001352 struct audio_patch *patches =
1353 (struct audio_patch *)calloc(numPatchesReq,
1354 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001355 if (patches == NULL) {
1356 reply->writeInt32(NO_MEMORY);
1357 reply->writeInt32(0);
1358 return NO_ERROR;
1359 }
1360 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001361 status_t status = listAudioPatches(&numPatches, patches, &generation);
1362 reply->writeInt32(status);
1363 reply->writeInt32(numPatches);
1364 if (status == NO_ERROR) {
1365 if (numPatchesReq > numPatches) {
1366 numPatchesReq = numPatches;
1367 }
1368 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1369 reply->writeInt32(generation);
1370 }
1371 free(patches);
1372 return NO_ERROR;
1373 }
1374
1375 case SET_AUDIO_PORT_CONFIG: {
1376 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001377 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001378 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001379 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001380 status_t status = setAudioPortConfig(&config);
1381 reply->writeInt32(status);
1382 return NO_ERROR;
1383 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001384
Eric Laurentb52c1522014-05-20 11:27:36 -07001385 case REGISTER_CLIENT: {
1386 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1387 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1388 data.readStrongBinder());
1389 registerClient(client);
1390 return NO_ERROR;
1391 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001392
Eric Laurente8726fe2015-06-26 09:39:24 -07001393 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1394 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1395 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1396 return NO_ERROR;
1397 } break;
1398
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001399 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1400 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1401 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1402 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001403 audio_session_t session = AUDIO_SESSION_NONE;
1404 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1405 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001406 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1407 reply->writeInt32(status);
1408 if (status == NO_ERROR) {
1409 reply->writeInt32(session);
1410 reply->writeInt32(ioHandle);
1411 reply->writeInt32(device);
1412 }
1413 return NO_ERROR;
1414 } break;
1415
1416 case RELEASE_SOUNDTRIGGER_SESSION: {
1417 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1418 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1419 data.readStrongBinder());
1420 audio_session_t session = (audio_session_t)data.readInt32();
1421 status_t status = releaseSoundTriggerSession(session);
1422 reply->writeInt32(status);
1423 return NO_ERROR;
1424 } break;
1425
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001426 case GET_PHONE_STATE: {
1427 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1428 reply->writeInt32((int32_t)getPhoneState());
1429 return NO_ERROR;
1430 } break;
1431
Eric Laurentbaac1832014-12-01 17:52:59 -08001432 case REGISTER_POLICY_MIXES: {
1433 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1434 bool registration = data.readInt32() == 1;
1435 Vector<AudioMix> mixes;
1436 size_t size = (size_t)data.readInt32();
1437 if (size > MAX_MIXES_PER_POLICY) {
1438 size = MAX_MIXES_PER_POLICY;
1439 }
1440 for (size_t i = 0; i < size; i++) {
1441 AudioMix mix;
1442 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1443 mixes.add(mix);
1444 }
1445 }
1446 status_t status = registerPolicyMixes(mixes, registration);
1447 reply->writeInt32(status);
1448 return NO_ERROR;
1449 } break;
1450
Eric Laurent554a2772015-04-10 11:29:24 -07001451 case START_AUDIO_SOURCE: {
1452 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001453 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001454 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001455 (void)sanitizeAudioPortConfig(&source);
1456 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001457 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001458 sanetizeAudioAttributes(&attributes);
Glenn Kasten559d4392016-03-29 13:42:57 -07001459 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent554a2772015-04-10 11:29:24 -07001460 status_t status = startAudioSource(&source, &attributes, &handle);
1461 reply->writeInt32(status);
1462 reply->writeInt32(handle);
1463 return NO_ERROR;
1464 } break;
1465
1466 case STOP_AUDIO_SOURCE: {
1467 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten559d4392016-03-29 13:42:57 -07001468 audio_patch_handle_t handle = (audio_patch_handle_t) data.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -07001469 status_t status = stopAudioSource(handle);
1470 reply->writeInt32(status);
1471 return NO_ERROR;
1472 } break;
1473
Andy Hung2ddee192015-12-18 17:34:44 -08001474 case SET_MASTER_MONO: {
1475 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1476 bool mono = static_cast<bool>(data.readInt32());
1477 status_t status = setMasterMono(mono);
1478 reply->writeInt32(status);
1479 return NO_ERROR;
1480 } break;
1481
1482 case GET_MASTER_MONO: {
1483 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1484 bool mono;
1485 status_t status = getMasterMono(&mono);
1486 reply->writeInt32(status);
1487 if (status == NO_ERROR) {
1488 reply->writeInt32(static_cast<int32_t>(mono));
1489 }
1490 return NO_ERROR;
1491 } break;
1492
Eric Laurentac9cef52017-06-09 15:46:26 -07001493 case GET_STREAM_VOLUME_DB: {
1494 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1495 audio_stream_type_t stream =
1496 static_cast <audio_stream_type_t>(data.readInt32());
1497 int index = static_cast <int>(data.readInt32());
1498 audio_devices_t device =
1499 static_cast <audio_devices_t>(data.readUint32());
1500 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1501 return NO_ERROR;
1502 }
1503
Eric Laurentc2f1f072009-07-17 12:17:14 -07001504 default:
1505 return BBinder::onTransact(code, data, reply, flags);
1506 }
1507}
1508
Andy Hungb0272092018-04-12 11:06:56 -07001509/** returns true if string overflow was prevented by zero termination */
1510template <size_t size>
1511static bool preventStringOverflow(char (&s)[size]) {
1512 if (strnlen(s, size) < size) return false;
1513 s[size - 1] = '\0';
1514 return true;
1515}
1516
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001517void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
1518{
1519 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
1520 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
1521 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
1522 }
1523 attr->tags[tagsMaxSize - 1] = '\0';
1524}
1525
Andy Hungb0272092018-04-12 11:06:56 -07001526/** returns BAD_VALUE if sanitization was required. */
1527status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
1528{
1529 if (preventStringOverflow(desc->name)
1530 | /* always */ preventStringOverflow(desc->implementor)) {
1531 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
1532 return BAD_VALUE;
1533 }
1534 return NO_ERROR;
1535}
1536
1537/** returns BAD_VALUE if sanitization was required. */
1538status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
1539{
1540 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
1541 preventStringOverflow(config->ext.device.address)) {
1542 return BAD_VALUE;
1543 }
1544 return NO_ERROR;
1545}
1546
Eric Laurentc2f1f072009-07-17 12:17:14 -07001547// ----------------------------------------------------------------------------
1548
Glenn Kasten40bc9062015-03-20 09:09:33 -07001549} // namespace android