blob: 8cd4a85794000cc423c7efd88aeec42f408cbad2 [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,
jiabin81772902018-04-02 17:52:27 -070083 GET_STREAM_VOLUME_DB,
84 GET_SURROUND_FORMATS,
85 SET_SURROUND_FORMAT_ENABLED
Eric Laurentc2f1f072009-07-17 12:17:14 -070086};
87
Eric Laurent1d670b12015-02-06 10:44:24 -080088#define MAX_ITEMS_PER_LIST 1024
89
Eric Laurentc2f1f072009-07-17 12:17:14 -070090class BpAudioPolicyService : public BpInterface<IAudioPolicyService>
91{
92public:
Chih-Hung Hsieh090ef602016-04-27 10:39:54 -070093 explicit BpAudioPolicyService(const sp<IBinder>& impl)
Eric Laurentc2f1f072009-07-17 12:17:14 -070094 : BpInterface<IAudioPolicyService>(impl)
95 {
96 }
97
98 virtual status_t setDeviceConnectionState(
Dima Zavinfce7a472011-04-19 22:30:36 -070099 audio_devices_t device,
100 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -0800101 const char *device_address,
102 const char *device_name)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700103 {
104 Parcel data, reply;
105 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
106 data.writeInt32(static_cast <uint32_t>(device));
107 data.writeInt32(static_cast <uint32_t>(state));
108 data.writeCString(device_address);
Paul McLeane743a472015-01-28 11:07:31 -0800109 data.writeCString(device_name);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700110 remote()->transact(SET_DEVICE_CONNECTION_STATE, data, &reply);
111 return static_cast <status_t> (reply.readInt32());
112 }
113
Dima Zavinfce7a472011-04-19 22:30:36 -0700114 virtual audio_policy_dev_state_t getDeviceConnectionState(
115 audio_devices_t device,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700116 const char *device_address)
117 {
118 Parcel data, reply;
119 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
120 data.writeInt32(static_cast <uint32_t>(device));
121 data.writeCString(device_address);
122 remote()->transact(GET_DEVICE_CONNECTION_STATE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700123 return static_cast <audio_policy_dev_state_t>(reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700124 }
125
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800126 virtual status_t handleDeviceConfigChange(audio_devices_t device,
127 const char *device_address,
128 const char *device_name)
129 {
130 Parcel data, reply;
131 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
132 data.writeInt32(static_cast <uint32_t>(device));
133 data.writeCString(device_address);
134 data.writeCString(device_name);
135 remote()->transact(HANDLE_DEVICE_CONFIG_CHANGE, data, &reply);
136 return static_cast <status_t> (reply.readInt32());
137 }
138
Glenn Kastenf78aee72012-01-04 11:00:47 -0800139 virtual status_t setPhoneState(audio_mode_t state)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700140 {
141 Parcel data, reply;
142 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
143 data.writeInt32(state);
144 remote()->transact(SET_PHONE_STATE, data, &reply);
145 return static_cast <status_t> (reply.readInt32());
146 }
147
Dima Zavinfce7a472011-04-19 22:30:36 -0700148 virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700149 {
150 Parcel data, reply;
151 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
152 data.writeInt32(static_cast <uint32_t>(usage));
153 data.writeInt32(static_cast <uint32_t>(config));
154 remote()->transact(SET_FORCE_USE, data, &reply);
155 return static_cast <status_t> (reply.readInt32());
156 }
157
Dima Zavinfce7a472011-04-19 22:30:36 -0700158 virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700159 {
160 Parcel data, reply;
161 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
162 data.writeInt32(static_cast <uint32_t>(usage));
163 remote()->transact(GET_FORCE_USE, data, &reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700164 return static_cast <audio_policy_forced_cfg_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700165 }
166
Eric Laurentf4e63452017-11-06 19:31:46 +0000167 virtual audio_io_handle_t getOutput(audio_stream_type_t stream)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700168 {
169 Parcel data, reply;
170 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
171 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700172 remote()->transact(GET_OUTPUT, data, &reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -0700173 return static_cast <audio_io_handle_t> (reply.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700174 }
175
Eric Laurente83b55d2014-11-14 10:06:21 -0800176 virtual status_t getOutputForAttr(const audio_attributes_t *attr,
177 audio_io_handle_t *output,
178 audio_session_t session,
179 audio_stream_type_t *stream,
Nadav Bar766fb022018-01-07 12:18:03 +0200180 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700181 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800182 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800183 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700184 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800185 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700186 {
187 Parcel data, reply;
188 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
189 if (attr == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800190 if (stream == NULL) {
191 ALOGE("getOutputForAttr(): NULL audio attributes and stream type");
192 return BAD_VALUE;
193 }
194 if (*stream == AUDIO_STREAM_DEFAULT) {
195 ALOGE("getOutputForAttr unspecified stream type");
196 return BAD_VALUE;
197 }
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700198 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800199 if (output == NULL) {
200 ALOGE("getOutputForAttr NULL output - shouldn't happen");
201 return BAD_VALUE;
202 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700203 if (selectedDeviceId == NULL) {
204 ALOGE("getOutputForAttr NULL selectedDeviceId - shouldn't happen");
205 return BAD_VALUE;
206 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800207 if (portId == NULL) {
208 ALOGE("getOutputForAttr NULL portId - shouldn't happen");
209 return BAD_VALUE;
210 }
Eric Laurente83b55d2014-11-14 10:06:21 -0800211 if (attr == NULL) {
212 data.writeInt32(0);
213 } else {
214 data.writeInt32(1);
215 data.write(attr, sizeof(audio_attributes_t));
216 }
217 data.writeInt32(session);
218 if (stream == NULL) {
219 data.writeInt32(0);
220 } else {
221 data.writeInt32(1);
222 data.writeInt32(*stream);
223 }
Nadav Bar766fb022018-01-07 12:18:03 +0200224 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700225 data.writeInt32(uid);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800226 data.write(config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700227 data.writeInt32(static_cast <uint32_t>(flags));
Eric Laurent9ae8c592017-06-22 17:17:09 -0700228 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800229 data.writeInt32(*portId);
Eric Laurente83b55d2014-11-14 10:06:21 -0800230 status_t status = remote()->transact(GET_OUTPUT_FOR_ATTR, data, &reply);
231 if (status != NO_ERROR) {
232 return status;
233 }
234 status = (status_t)reply.readInt32();
235 if (status != NO_ERROR) {
236 return status;
237 }
238 *output = (audio_io_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800239 audio_stream_type_t lStream = (audio_stream_type_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800240 if (stream != NULL) {
Eric Laurent20b9ef02016-12-05 11:03:16 -0800241 *stream = lStream;
Eric Laurente83b55d2014-11-14 10:06:21 -0800242 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700243 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800244 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurente83b55d2014-11-14 10:06:21 -0800245 return status;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700246 }
247
Eric Laurentde070132010-07-13 04:45:46 -0700248 virtual status_t startOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700249 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800250 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700251 {
252 Parcel data, reply;
253 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfa2877b2009-07-28 08:44:33 -0700254 data.writeInt32(output);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800255 data.writeInt32((int32_t) stream);
Glenn Kastend848eb42016-03-08 13:42:11 -0800256 data.writeInt32((int32_t) session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700257 remote()->transact(START_OUTPUT, data, &reply);
258 return static_cast <status_t> (reply.readInt32());
259 }
260
Eric Laurentde070132010-07-13 04:45:46 -0700261 virtual status_t stopOutput(audio_io_handle_t output,
Dima Zavinfce7a472011-04-19 22:30:36 -0700262 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800263 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700264 {
265 Parcel data, reply;
266 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfa2877b2009-07-28 08:44:33 -0700267 data.writeInt32(output);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800268 data.writeInt32((int32_t) stream);
Glenn Kastend848eb42016-03-08 13:42:11 -0800269 data.writeInt32((int32_t) session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700270 remote()->transact(STOP_OUTPUT, data, &reply);
271 return static_cast <status_t> (reply.readInt32());
272 }
273
Eric Laurente83b55d2014-11-14 10:06:21 -0800274 virtual void releaseOutput(audio_io_handle_t output,
275 audio_stream_type_t stream,
276 audio_session_t session)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700277 {
278 Parcel data, reply;
279 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfa2877b2009-07-28 08:44:33 -0700280 data.writeInt32(output);
Eric Laurente83b55d2014-11-14 10:06:21 -0800281 data.writeInt32((int32_t)stream);
282 data.writeInt32((int32_t)session);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700283 remote()->transact(RELEASE_OUTPUT, data, &reply);
284 }
285
Eric Laurentcaf7f482014-11-25 17:50:47 -0800286 virtual status_t getInputForAttr(const audio_attributes_t *attr,
287 audio_io_handle_t *input,
288 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700289 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700290 uid_t uid,
Eric Laurentfee19762018-01-29 18:44:13 -0800291 const String16& opPackageName,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800292 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600293 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700294 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800295 audio_port_handle_t *portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700296 {
297 Parcel data, reply;
298 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentcaf7f482014-11-25 17:50:47 -0800299 if (attr == NULL) {
300 ALOGE("getInputForAttr NULL attr - shouldn't happen");
301 return BAD_VALUE;
302 }
303 if (input == NULL) {
304 ALOGE("getInputForAttr NULL input - shouldn't happen");
305 return BAD_VALUE;
306 }
Eric Laurent9ae8c592017-06-22 17:17:09 -0700307 if (selectedDeviceId == NULL) {
308 ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
309 return BAD_VALUE;
310 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800311 if (portId == NULL) {
312 ALOGE("getInputForAttr NULL portId - shouldn't happen");
313 return BAD_VALUE;
314 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800315 data.write(attr, sizeof(audio_attributes_t));
Eric Laurenta54f1282017-07-01 19:39:32 -0700316 data.writeInt32(*input);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800317 data.writeInt32(session);
Eric Laurentb2379ba2016-05-23 17:42:12 -0700318 data.writeInt32(pid);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700319 data.writeInt32(uid);
Eric Laurentfee19762018-01-29 18:44:13 -0800320 data.writeString16(opPackageName);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800321 data.write(config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -0700322 data.writeInt32(flags);
Eric Laurent9ae8c592017-06-22 17:17:09 -0700323 data.writeInt32(*selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -0800324 data.writeInt32(*portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -0800325 status_t status = remote()->transact(GET_INPUT_FOR_ATTR, data, &reply);
326 if (status != NO_ERROR) {
327 return status;
328 }
329 status = reply.readInt32();
330 if (status != NO_ERROR) {
331 return status;
332 }
333 *input = (audio_io_handle_t)reply.readInt32();
Eric Laurent9ae8c592017-06-22 17:17:09 -0700334 *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -0800335 *portId = (audio_port_handle_t)reply.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -0800336 return NO_ERROR;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700337 }
338
Eric Laurentfee19762018-01-29 18:44:13 -0800339 virtual status_t startInput(audio_port_handle_t portId,
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800340 bool *silenced)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700341 {
342 Parcel data, reply;
343 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800344 data.writeInt32(portId);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800345 data.writeInt32(*silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700346 remote()->transact(START_INPUT, data, &reply);
Svet Ganovf4ddfef2018-01-16 07:37:58 -0800347 status_t status = static_cast <status_t> (reply.readInt32());
348 *silenced = reply.readInt32() == 1;
349 return status;
Eric Laurentc2f1f072009-07-17 12:17:14 -0700350 }
351
Eric Laurentfee19762018-01-29 18:44:13 -0800352 virtual status_t stopInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700353 {
354 Parcel data, reply;
355 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800356 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700357 remote()->transact(STOP_INPUT, data, &reply);
358 return static_cast <status_t> (reply.readInt32());
359 }
360
Eric Laurentfee19762018-01-29 18:44:13 -0800361 virtual void releaseInput(audio_port_handle_t portId)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700362 {
363 Parcel data, reply;
364 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Eric Laurentfee19762018-01-29 18:44:13 -0800365 data.writeInt32(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -0700366 remote()->transact(RELEASE_INPUT, data, &reply);
367 }
368
Dima Zavinfce7a472011-04-19 22:30:36 -0700369 virtual status_t initStreamVolume(audio_stream_type_t stream,
Eric Laurentc2f1f072009-07-17 12:17:14 -0700370 int indexMin,
371 int indexMax)
372 {
373 Parcel data, reply;
374 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
375 data.writeInt32(static_cast <uint32_t>(stream));
376 data.writeInt32(indexMin);
377 data.writeInt32(indexMax);
378 remote()->transact(INIT_STREAM_VOLUME, data, &reply);
379 return static_cast <status_t> (reply.readInt32());
380 }
381
Eric Laurent83844cc2011-11-18 16:43:31 -0800382 virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
383 int index,
384 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700385 {
386 Parcel data, reply;
387 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
388 data.writeInt32(static_cast <uint32_t>(stream));
389 data.writeInt32(index);
Eric Laurent83844cc2011-11-18 16:43:31 -0800390 data.writeInt32(static_cast <uint32_t>(device));
Eric Laurentc2f1f072009-07-17 12:17:14 -0700391 remote()->transact(SET_STREAM_VOLUME, data, &reply);
392 return static_cast <status_t> (reply.readInt32());
393 }
394
Eric Laurent83844cc2011-11-18 16:43:31 -0800395 virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
396 int *index,
397 audio_devices_t device)
Eric Laurentc2f1f072009-07-17 12:17:14 -0700398 {
399 Parcel data, reply;
400 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
401 data.writeInt32(static_cast <uint32_t>(stream));
Eric Laurent83844cc2011-11-18 16:43:31 -0800402 data.writeInt32(static_cast <uint32_t>(device));
403
Eric Laurentc2f1f072009-07-17 12:17:14 -0700404 remote()->transact(GET_STREAM_VOLUME, data, &reply);
405 int lIndex = reply.readInt32();
406 if (index) *index = lIndex;
407 return static_cast <status_t> (reply.readInt32());
408 }
Eric Laurentde070132010-07-13 04:45:46 -0700409
Dima Zavinfce7a472011-04-19 22:30:36 -0700410 virtual uint32_t getStrategyForStream(audio_stream_type_t stream)
Eric Laurentde070132010-07-13 04:45:46 -0700411 {
412 Parcel data, reply;
413 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
414 data.writeInt32(static_cast <uint32_t>(stream));
415 remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply);
416 return reply.readInt32();
417 }
418
Eric Laurent63742522012-03-08 13:42:42 -0800419 virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream)
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800420 {
421 Parcel data, reply;
422 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
423 data.writeInt32(static_cast <uint32_t>(stream));
424 remote()->transact(GET_DEVICES_FOR_STREAM, data, &reply);
Eric Laurent63742522012-03-08 13:42:42 -0800425 return (audio_devices_t) reply.readInt32();
Glenn Kasten6b2718c2011-02-04 13:54:26 -0800426 }
427
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700428 virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc)
Eric Laurentde070132010-07-13 04:45:46 -0700429 {
430 Parcel data, reply;
431 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
432 data.write(desc, sizeof(effect_descriptor_t));
433 remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply);
434 return static_cast <audio_io_handle_t> (reply.readInt32());
435 }
436
Glenn Kasten58e5aa32012-06-20 14:08:14 -0700437 virtual status_t registerEffect(const effect_descriptor_t *desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700438 audio_io_handle_t io,
Eric Laurentde070132010-07-13 04:45:46 -0700439 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800440 audio_session_t session,
Eric Laurentde070132010-07-13 04:45:46 -0700441 int id)
442 {
443 Parcel data, reply;
444 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
445 data.write(desc, sizeof(effect_descriptor_t));
Eric Laurent7c7f10b2011-06-17 21:29:58 -0700446 data.writeInt32(io);
Eric Laurentde070132010-07-13 04:45:46 -0700447 data.writeInt32(strategy);
448 data.writeInt32(session);
449 data.writeInt32(id);
450 remote()->transact(REGISTER_EFFECT, data, &reply);
451 return static_cast <status_t> (reply.readInt32());
452 }
453
454 virtual status_t unregisterEffect(int id)
455 {
456 Parcel data, reply;
457 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
458 data.writeInt32(id);
459 remote()->transact(UNREGISTER_EFFECT, data, &reply);
460 return static_cast <status_t> (reply.readInt32());
461 }
462
Eric Laurentdb7c0792011-08-10 10:37:50 -0700463 virtual status_t setEffectEnabled(int id, bool enabled)
464 {
465 Parcel data, reply;
466 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
467 data.writeInt32(id);
468 data.writeInt32(enabled);
469 remote()->transact(SET_EFFECT_ENABLED, data, &reply);
470 return static_cast <status_t> (reply.readInt32());
471 }
472
Glenn Kastenfff6d712012-01-12 16:38:12 -0800473 virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
Eric Laurenteda6c362011-02-02 09:33:30 -0800474 {
475 Parcel data, reply;
476 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Glenn Kastenfff6d712012-01-12 16:38:12 -0800477 data.writeInt32((int32_t) stream);
Eric Laurenteda6c362011-02-02 09:33:30 -0800478 data.writeInt32(inPastMs);
479 remote()->transact(IS_STREAM_ACTIVE, data, &reply);
480 return reply.readInt32();
481 }
Eric Laurent57dae992011-07-24 13:36:09 -0700482
Jean-Michel Trivi272ab542013-02-04 16:26:02 -0800483 virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
484 {
485 Parcel data, reply;
486 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
487 data.writeInt32((int32_t) stream);
488 data.writeInt32(inPastMs);
489 remote()->transact(IS_STREAM_ACTIVE_REMOTELY, data, &reply);
490 return reply.readInt32();
491 }
492
Jean-Michel Trivid7086032012-10-10 12:11:16 -0700493 virtual bool isSourceActive(audio_source_t source) const
494 {
495 Parcel data, reply;
496 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
497 data.writeInt32((int32_t) source);
498 remote()->transact(IS_SOURCE_ACTIVE, data, &reply);
499 return reply.readInt32();
500 }
501
Glenn Kastend848eb42016-03-08 13:42:11 -0800502 virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent57dae992011-07-24 13:36:09 -0700503 effect_descriptor_t *descriptors,
504 uint32_t *count)
505 {
506 if (descriptors == NULL || count == NULL) {
507 return BAD_VALUE;
508 }
509 Parcel data, reply;
510 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
511 data.writeInt32(audioSession);
512 data.writeInt32(*count);
513 status_t status = remote()->transact(QUERY_DEFAULT_PRE_PROCESSING, data, &reply);
514 if (status != NO_ERROR) {
515 return status;
516 }
517 status = static_cast <status_t> (reply.readInt32());
518 uint32_t retCount = reply.readInt32();
519 if (retCount != 0) {
520 uint32_t numDesc = (retCount < *count) ? retCount : *count;
521 reply.read(descriptors, sizeof(effect_descriptor_t) * numDesc);
522 }
523 *count = retCount;
524 return status;
525 }
Richard Fitzgeraldad3af332013-03-25 16:54:37 +0000526
527 virtual bool isOffloadSupported(const audio_offload_info_t& info)
528 {
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +0100529 Parcel data, reply;
530 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
531 data.write(&info, sizeof(audio_offload_info_t));
532 remote()->transact(IS_OFFLOAD_SUPPORTED, data, &reply);
Eric Laurent203b1a12014-04-01 10:34:16 -0700533 return reply.readInt32();
534 }
535
536 virtual status_t listAudioPorts(audio_port_role_t role,
537 audio_port_type_t type,
538 unsigned int *num_ports,
539 struct audio_port *ports,
540 unsigned int *generation)
541 {
542 if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
543 generation == NULL) {
544 return BAD_VALUE;
545 }
546 Parcel data, reply;
547 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
548 unsigned int numPortsReq = (ports == NULL) ? 0 : *num_ports;
549 data.writeInt32(role);
550 data.writeInt32(type);
551 data.writeInt32(numPortsReq);
552 status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply);
553 if (status == NO_ERROR) {
554 status = (status_t)reply.readInt32();
555 *num_ports = (unsigned int)reply.readInt32();
556 }
Eric Laurent203b1a12014-04-01 10:34:16 -0700557 if (status == NO_ERROR) {
558 if (numPortsReq > *num_ports) {
559 numPortsReq = *num_ports;
560 }
561 if (numPortsReq > 0) {
562 reply.read(ports, numPortsReq * sizeof(struct audio_port));
563 }
564 *generation = reply.readInt32();
565 }
566 return status;
567 }
568
569 virtual status_t getAudioPort(struct audio_port *port)
570 {
571 if (port == NULL) {
572 return BAD_VALUE;
573 }
574 Parcel data, reply;
575 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
576 data.write(port, sizeof(struct audio_port));
577 status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply);
578 if (status != NO_ERROR ||
579 (status = (status_t)reply.readInt32()) != NO_ERROR) {
580 return status;
581 }
582 reply.read(port, sizeof(struct audio_port));
583 return status;
584 }
585
586 virtual status_t createAudioPatch(const struct audio_patch *patch,
587 audio_patch_handle_t *handle)
588 {
589 if (patch == NULL || handle == NULL) {
590 return BAD_VALUE;
591 }
592 Parcel data, reply;
593 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
594 data.write(patch, sizeof(struct audio_patch));
595 data.write(handle, sizeof(audio_patch_handle_t));
596 status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply);
597 if (status != NO_ERROR ||
598 (status = (status_t)reply.readInt32()) != NO_ERROR) {
599 return status;
600 }
601 reply.read(handle, sizeof(audio_patch_handle_t));
602 return status;
603 }
604
605 virtual status_t releaseAudioPatch(audio_patch_handle_t handle)
606 {
607 Parcel data, reply;
608 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
609 data.write(&handle, sizeof(audio_patch_handle_t));
610 status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply);
611 if (status != NO_ERROR) {
612 status = (status_t)reply.readInt32();
613 }
614 return status;
615 }
616
617 virtual status_t listAudioPatches(unsigned int *num_patches,
618 struct audio_patch *patches,
619 unsigned int *generation)
620 {
621 if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
622 generation == NULL) {
623 return BAD_VALUE;
624 }
625 Parcel data, reply;
626 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
627 unsigned int numPatchesReq = (patches == NULL) ? 0 : *num_patches;
628 data.writeInt32(numPatchesReq);
629 status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply);
630 if (status == NO_ERROR) {
631 status = (status_t)reply.readInt32();
632 *num_patches = (unsigned int)reply.readInt32();
633 }
634 if (status == NO_ERROR) {
635 if (numPatchesReq > *num_patches) {
636 numPatchesReq = *num_patches;
637 }
638 if (numPatchesReq > 0) {
639 reply.read(patches, numPatchesReq * sizeof(struct audio_patch));
640 }
641 *generation = reply.readInt32();
642 }
643 return status;
644 }
645
646 virtual status_t setAudioPortConfig(const struct audio_port_config *config)
647 {
648 if (config == NULL) {
649 return BAD_VALUE;
650 }
651 Parcel data, reply;
652 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
653 data.write(config, sizeof(struct audio_port_config));
654 status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply);
655 if (status != NO_ERROR) {
656 status = (status_t)reply.readInt32();
657 }
658 return status;
659 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700660
Eric Laurentb52c1522014-05-20 11:27:36 -0700661 virtual void registerClient(const sp<IAudioPolicyServiceClient>& client)
662 {
663 Parcel data, reply;
664 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -0800665 data.writeStrongBinder(IInterface::asBinder(client));
Eric Laurentb52c1522014-05-20 11:27:36 -0700666 remote()->transact(REGISTER_CLIENT, data, &reply);
667 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700668
Eric Laurente8726fe2015-06-26 09:39:24 -0700669 virtual void setAudioPortCallbacksEnabled(bool enabled)
670 {
671 Parcel data, reply;
672 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
673 data.writeInt32(enabled ? 1 : 0);
674 remote()->transact(SET_AUDIO_PORT_CALLBACK_ENABLED, data, &reply);
675 }
676
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700677 virtual status_t acquireSoundTriggerSession(audio_session_t *session,
678 audio_io_handle_t *ioHandle,
679 audio_devices_t *device)
680 {
681 if (session == NULL || ioHandle == NULL || device == NULL) {
682 return BAD_VALUE;
683 }
684 Parcel data, reply;
685 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
686 status_t status = remote()->transact(ACQUIRE_SOUNDTRIGGER_SESSION, data, &reply);
687 if (status != NO_ERROR) {
688 return status;
689 }
690 status = (status_t)reply.readInt32();
691 if (status == NO_ERROR) {
692 *session = (audio_session_t)reply.readInt32();
693 *ioHandle = (audio_io_handle_t)reply.readInt32();
694 *device = (audio_devices_t)reply.readInt32();
695 }
696 return status;
697 }
698
699 virtual status_t releaseSoundTriggerSession(audio_session_t session)
700 {
701 Parcel data, reply;
702 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
703 data.writeInt32(session);
704 status_t status = remote()->transact(RELEASE_SOUNDTRIGGER_SESSION, data, &reply);
705 if (status != NO_ERROR) {
706 return status;
707 }
708 return (status_t)reply.readInt32();
709 }
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700710
711 virtual audio_mode_t getPhoneState()
712 {
713 Parcel data, reply;
714 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
715 status_t status = remote()->transact(GET_PHONE_STATE, data, &reply);
716 if (status != NO_ERROR) {
717 return AUDIO_MODE_INVALID;
718 }
719 return (audio_mode_t)reply.readInt32();
720 }
Eric Laurentbaac1832014-12-01 17:52:59 -0800721
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700722 virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800723 {
724 Parcel data, reply;
725 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
726 data.writeInt32(registration ? 1 : 0);
727 size_t size = mixes.size();
728 if (size > MAX_MIXES_PER_POLICY) {
729 size = MAX_MIXES_PER_POLICY;
730 }
731 size_t sizePosition = data.dataPosition();
732 data.writeInt32(size);
733 size_t finalSize = size;
734 for (size_t i = 0; i < size; i++) {
735 size_t position = data.dataPosition();
736 if (mixes[i].writeToParcel(&data) != NO_ERROR) {
737 data.setDataPosition(position);
738 finalSize--;
739 }
740 }
741 if (size != finalSize) {
742 size_t position = data.dataPosition();
743 data.setDataPosition(sizePosition);
744 data.writeInt32(finalSize);
745 data.setDataPosition(position);
746 }
747 status_t status = remote()->transact(REGISTER_POLICY_MIXES, data, &reply);
748 if (status == NO_ERROR) {
749 status = (status_t)reply.readInt32();
750 }
751 return status;
752 }
Eric Laurent554a2772015-04-10 11:29:24 -0700753
754 virtual status_t startAudioSource(const struct audio_port_config *source,
755 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -0700756 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700757 {
758 Parcel data, reply;
759 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
760 if (source == NULL || attributes == NULL || handle == NULL) {
761 return BAD_VALUE;
762 }
763 data.write(source, sizeof(struct audio_port_config));
764 data.write(attributes, sizeof(audio_attributes_t));
765 status_t status = remote()->transact(START_AUDIO_SOURCE, data, &reply);
766 if (status != NO_ERROR) {
767 return status;
768 }
769 status = (status_t)reply.readInt32();
770 if (status != NO_ERROR) {
771 return status;
772 }
Glenn Kasten559d4392016-03-29 13:42:57 -0700773 *handle = (audio_patch_handle_t)reply.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -0700774 return status;
775 }
776
Glenn Kasten559d4392016-03-29 13:42:57 -0700777 virtual status_t stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700778 {
779 Parcel data, reply;
780 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
781 data.writeInt32(handle);
782 status_t status = remote()->transact(STOP_AUDIO_SOURCE, data, &reply);
783 if (status != NO_ERROR) {
784 return status;
785 }
786 status = (status_t)reply.readInt32();
787 return status;
788 }
Andy Hung2ddee192015-12-18 17:34:44 -0800789
790 virtual status_t setMasterMono(bool mono)
791 {
792 Parcel data, reply;
793 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
794 data.writeInt32(static_cast<int32_t>(mono));
795 status_t status = remote()->transact(SET_MASTER_MONO, data, &reply);
796 if (status != NO_ERROR) {
797 return status;
798 }
799 return static_cast<status_t>(reply.readInt32());
800 }
801
802 virtual status_t getMasterMono(bool *mono)
803 {
804 if (mono == nullptr) {
805 return BAD_VALUE;
806 }
807 Parcel data, reply;
808 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
809
810 status_t status = remote()->transact(GET_MASTER_MONO, data, &reply);
811 if (status != NO_ERROR) {
812 return status;
813 }
814 status = static_cast<status_t>(reply.readInt32());
815 if (status == NO_ERROR) {
816 *mono = static_cast<bool>(reply.readInt32());
817 }
818 return status;
819 }
Eric Laurentac9cef52017-06-09 15:46:26 -0700820
821 virtual float getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
822 {
823 Parcel data, reply;
824 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
825 data.writeInt32(static_cast <int32_t>(stream));
826 data.writeInt32(static_cast <int32_t>(index));
827 data.writeUint32(static_cast <uint32_t>(device));
828 status_t status = remote()->transact(GET_STREAM_VOLUME_DB, data, &reply);
829 if (status != NO_ERROR) {
830 return NAN;
831 }
832 return reply.readFloat();
833 }
jiabin81772902018-04-02 17:52:27 -0700834
835 virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
836 audio_format_t *surroundFormats,
837 bool *surroundFormatsEnabled,
838 bool reported)
839 {
840 if (numSurroundFormats == NULL || (*numSurroundFormats != 0 &&
841 (surroundFormats == NULL || surroundFormatsEnabled == NULL))) {
842 return BAD_VALUE;
843 }
844 Parcel data, reply;
845 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
846 unsigned int numSurroundFormatsReq = *numSurroundFormats;
847 data.writeUint32(numSurroundFormatsReq);
848 data.writeBool(reported);
849 status_t status = remote()->transact(GET_SURROUND_FORMATS, data, &reply);
850 if (status == NO_ERROR && (status = (status_t)reply.readInt32()) == NO_ERROR) {
851 *numSurroundFormats = reply.readUint32();
852 }
853 if (status == NO_ERROR) {
854 if (numSurroundFormatsReq > *numSurroundFormats) {
855 numSurroundFormatsReq = *numSurroundFormats;
856 }
857 if (numSurroundFormatsReq > 0) {
858 status = reply.read(surroundFormats,
859 numSurroundFormatsReq * sizeof(audio_format_t));
860 if (status != NO_ERROR) {
861 return status;
862 }
863 status = reply.read(surroundFormatsEnabled,
864 numSurroundFormatsReq * sizeof(bool));
865 }
866 }
867 return status;
868 }
869
870 virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
871 {
872 Parcel data, reply;
873 data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
874 data.writeInt32(audioFormat);
875 data.writeBool(enabled);
876 status_t status = remote()->transact(SET_SURROUND_FORMAT_ENABLED, data, &reply);
877 if (status != NO_ERROR) {
878 return status;
879 }
880 return reply.readInt32();
881 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700882};
883
884IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
885
886// ----------------------------------------------------------------------
887
Eric Laurentc2f1f072009-07-17 12:17:14 -0700888status_t BnAudioPolicyService::onTransact(
889 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
890{
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800891 // make sure transactions reserved to AudioFlinger do not come from other processes
892 switch (code) {
893 case START_OUTPUT:
894 case STOP_OUTPUT:
895 case RELEASE_OUTPUT:
896 case GET_INPUT_FOR_ATTR:
897 case START_INPUT:
898 case STOP_INPUT:
899 case RELEASE_INPUT:
900 case GET_STRATEGY_FOR_STREAM:
901 case GET_OUTPUT_FOR_EFFECT:
902 case REGISTER_EFFECT:
903 case UNREGISTER_EFFECT:
904 case SET_EFFECT_ENABLED:
905 case GET_OUTPUT_FOR_ATTR:
906 case ACQUIRE_SOUNDTRIGGER_SESSION:
907 case RELEASE_SOUNDTRIGGER_SESSION:
908 ALOGW("%s: transaction %d received from PID %d",
909 __func__, code, IPCThreadState::self()->getCallingPid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700910 // return status only for non void methods
911 switch (code) {
912 case RELEASE_OUTPUT:
913 case RELEASE_INPUT:
914 break;
915 default:
916 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
917 break;
918 }
919 return OK;
Eric Laurentb1cc36b2017-12-11 12:14:16 -0800920 default:
921 break;
922 }
923
Eric Laurent4980df22018-01-26 18:04:09 -0800924 // make sure the following transactions come from system components
925 switch (code) {
926 case SET_DEVICE_CONNECTION_STATE:
927 case HANDLE_DEVICE_CONFIG_CHANGE:
928 case SET_PHONE_STATE:
Eric Laurente17378d2018-05-09 14:43:01 -0700929//FIXME: Allow SET_FORCE_USE calls from system apps until a better use case routing API is available
930// case SET_FORCE_USE:
Eric Laurent4980df22018-01-26 18:04:09 -0800931 case INIT_STREAM_VOLUME:
932 case SET_STREAM_VOLUME:
933 case REGISTER_POLICY_MIXES:
Eric Laurent10b71232018-04-13 18:14:44 -0700934 case SET_MASTER_MONO:
935 case START_AUDIO_SOURCE:
jiabin81772902018-04-02 17:52:27 -0700936 case STOP_AUDIO_SOURCE:
937 case GET_SURROUND_FORMATS:
938 case SET_SURROUND_FORMAT_ENABLED: {
Eric Laurentf1047e82018-04-16 19:18:20 -0700939 if (multiuser_get_app_id(IPCThreadState::self()->getCallingUid()) >= AID_APP_START) {
Eric Laurent4980df22018-01-26 18:04:09 -0800940 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
941 __func__, code, IPCThreadState::self()->getCallingPid(),
942 IPCThreadState::self()->getCallingUid());
Eric Laurentef92bff2018-04-26 10:44:50 -0700943 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
944 return OK;
Eric Laurent4980df22018-01-26 18:04:09 -0800945 }
Eric Laurent96c7eed2018-03-26 17:57:01 -0700946 } break;
Eric Laurent4980df22018-01-26 18:04:09 -0800947 default:
948 break;
949 }
950
Stanley Tngde9e33d2018-10-10 18:48:33 -0700951 char timeCheckString[64];
952 snprintf(timeCheckString, sizeof(timeCheckString), "IAudioPolicyService: %d", code);
953 TimeCheck check(timeCheckString);
Eric Laurent3528c932018-02-23 17:17:22 -0800954
Glenn Kastene53b9ea2012-03-12 16:29:55 -0700955 switch (code) {
Eric Laurentc2f1f072009-07-17 12:17:14 -0700956 case SET_DEVICE_CONNECTION_STATE: {
957 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700958 audio_devices_t device =
959 static_cast <audio_devices_t>(data.readInt32());
960 audio_policy_dev_state_t state =
961 static_cast <audio_policy_dev_state_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700962 const char *device_address = data.readCString();
Paul McLeane743a472015-01-28 11:07:31 -0800963 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800964 if (device_address == nullptr || device_name == nullptr) {
965 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
966 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
967 } else {
968 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
969 state,
970 device_address,
971 device_name)));
972 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700973 return NO_ERROR;
974 } break;
975
976 case GET_DEVICE_CONNECTION_STATE: {
977 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -0700978 audio_devices_t device =
979 static_cast<audio_devices_t> (data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -0700980 const char *device_address = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800981 if (device_address == nullptr) {
982 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
983 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
984 } else {
985 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
986 device_address)));
987 }
Eric Laurentc2f1f072009-07-17 12:17:14 -0700988 return NO_ERROR;
989 } break;
990
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -0800991 case HANDLE_DEVICE_CONFIG_CHANGE: {
992 CHECK_INTERFACE(IAudioPolicyService, data, reply);
993 audio_devices_t device =
994 static_cast <audio_devices_t>(data.readInt32());
995 const char *device_address = data.readCString();
996 const char *device_name = data.readCString();
Pavlin Radoslavovc694ff42017-01-09 23:27:29 -0800997 if (device_address == nullptr || device_name == nullptr) {
998 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
999 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1000 } else {
1001 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
1002 device_address,
1003 device_name)));
1004 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -08001005 return NO_ERROR;
1006 } break;
1007
Eric Laurentc2f1f072009-07-17 12:17:14 -07001008 case SET_PHONE_STATE: {
1009 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001010 reply->writeInt32(static_cast <uint32_t>(setPhoneState(
1011 (audio_mode_t) data.readInt32())));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001012 return NO_ERROR;
1013 } break;
1014
Eric Laurentc2f1f072009-07-17 12:17:14 -07001015 case SET_FORCE_USE: {
1016 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001017 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1018 data.readInt32());
Dima Zavinfce7a472011-04-19 22:30:36 -07001019 audio_policy_forced_cfg_t config =
1020 static_cast <audio_policy_forced_cfg_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001021 reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
1022 return NO_ERROR;
1023 } break;
1024
1025 case GET_FORCE_USE: {
1026 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten85ab62c2012-11-01 11:11:38 -07001027 audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1028 data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001029 reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
1030 return NO_ERROR;
1031 } break;
1032
1033 case GET_OUTPUT: {
1034 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001035 audio_stream_type_t stream =
1036 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentf4e63452017-11-06 19:31:46 +00001037 audio_io_handle_t output = getOutput(stream);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001038 reply->writeInt32(static_cast <int>(output));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001039 return NO_ERROR;
1040 } break;
1041
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001042 case GET_OUTPUT_FOR_ATTR: {
1043 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001044 audio_attributes_t attr = {};
Eric Laurente83b55d2014-11-14 10:06:21 -08001045 bool hasAttributes = data.readInt32() != 0;
1046 if (hasAttributes) {
1047 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001048 sanetizeAudioAttributes(&attr);
Eric Laurente83b55d2014-11-14 10:06:21 -08001049 }
1050 audio_session_t session = (audio_session_t)data.readInt32();
1051 audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
1052 bool hasStream = data.readInt32() != 0;
1053 if (hasStream) {
1054 stream = (audio_stream_type_t)data.readInt32();
1055 }
Nadav Bar766fb022018-01-07 12:18:03 +02001056 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001057 uid_t uid = (uid_t)data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001058 audio_config_t config;
1059 memset(&config, 0, sizeof(audio_config_t));
1060 data.read(&config, sizeof(audio_config_t));
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001061 audio_output_flags_t flags =
1062 static_cast <audio_output_flags_t>(data.readInt32());
Paul McLeanaa981192015-03-21 09:55:15 -07001063 audio_port_handle_t selectedDeviceId = data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001064 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Robert Shiha946d842015-09-02 16:46:59 -07001065 audio_io_handle_t output = 0;
Eric Laurente83b55d2014-11-14 10:06:21 -08001066 status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
Nadav Bar766fb022018-01-07 12:18:03 +02001067 &output, session, &stream, pid, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -08001068 &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001069 flags, &selectedDeviceId, &portId);
Eric Laurente83b55d2014-11-14 10:06:21 -08001070 reply->writeInt32(status);
1071 reply->writeInt32(output);
1072 reply->writeInt32(stream);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001073 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001074 reply->writeInt32(portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -07001075 return NO_ERROR;
1076 } break;
1077
Eric Laurentc2f1f072009-07-17 12:17:14 -07001078 case START_OUTPUT: {
1079 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001080 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurentebcb2542014-03-05 18:30:08 -08001081 audio_stream_type_t stream =
1082 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001083 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001084 reply->writeInt32(static_cast <uint32_t>(startOutput(output,
Eric Laurentebcb2542014-03-05 18:30:08 -08001085 stream,
Eric Laurentde070132010-07-13 04:45:46 -07001086 session)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001087 return NO_ERROR;
1088 } break;
1089
1090 case STOP_OUTPUT: {
1091 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001092 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurentebcb2542014-03-05 18:30:08 -08001093 audio_stream_type_t stream =
1094 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001095 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001096 reply->writeInt32(static_cast <uint32_t>(stopOutput(output,
Eric Laurentebcb2542014-03-05 18:30:08 -08001097 stream,
Eric Laurentde070132010-07-13 04:45:46 -07001098 session)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001099 return NO_ERROR;
1100 } break;
1101
1102 case RELEASE_OUTPUT: {
1103 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfa2877b2009-07-28 08:44:33 -07001104 audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
Eric Laurente83b55d2014-11-14 10:06:21 -08001105 audio_stream_type_t stream = (audio_stream_type_t)data.readInt32();
1106 audio_session_t session = (audio_session_t)data.readInt32();
1107 releaseOutput(output, stream, session);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001108 return NO_ERROR;
1109 } break;
1110
Eric Laurentcaf7f482014-11-25 17:50:47 -08001111 case GET_INPUT_FOR_ATTR: {
Eric Laurentc2f1f072009-07-17 12:17:14 -07001112 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001113 audio_attributes_t attr = {};
Eric Laurentcaf7f482014-11-25 17:50:47 -08001114 data.read(&attr, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001115 sanetizeAudioAttributes(&attr);
Eric Laurenta54f1282017-07-01 19:39:32 -07001116 audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
Eric Laurentcaf7f482014-11-25 17:50:47 -08001117 audio_session_t session = (audio_session_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001118 pid_t pid = (pid_t)data.readInt32();
Eric Laurent8c7e6da2015-04-21 17:37:00 -07001119 uid_t uid = (uid_t)data.readInt32();
Eric Laurentfee19762018-01-29 18:44:13 -08001120 const String16 opPackageName = data.readString16();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001121 audio_config_base_t config;
1122 memset(&config, 0, sizeof(audio_config_base_t));
1123 data.read(&config, sizeof(audio_config_base_t));
Glenn Kastenb3b16602014-07-16 08:36:31 -07001124 audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
Paul McLean466dc8e2015-04-17 13:15:36 -06001125 audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
Eric Laurent20b9ef02016-12-05 11:03:16 -08001126 audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
Eric Laurentb2379ba2016-05-23 17:42:12 -07001127 status_t status = getInputForAttr(&attr, &input, session, pid, uid,
Eric Laurentfee19762018-01-29 18:44:13 -08001128 opPackageName, &config,
Eric Laurent9ae8c592017-06-22 17:17:09 -07001129 flags, &selectedDeviceId, &portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001130 reply->writeInt32(status);
1131 if (status == NO_ERROR) {
1132 reply->writeInt32(input);
Eric Laurent9ae8c592017-06-22 17:17:09 -07001133 reply->writeInt32(selectedDeviceId);
Eric Laurent20b9ef02016-12-05 11:03:16 -08001134 reply->writeInt32(portId);
Eric Laurentcaf7f482014-11-25 17:50:47 -08001135 }
Eric Laurentc2f1f072009-07-17 12:17:14 -07001136 return NO_ERROR;
1137 } break;
1138
1139 case START_INPUT: {
1140 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001141 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001142 bool silenced = data.readInt32() == 1;
Eric Laurentfee19762018-01-29 18:44:13 -08001143 status_t status = startInput(portId, &silenced);
Svet Ganovf4ddfef2018-01-16 07:37:58 -08001144 reply->writeInt32(static_cast <uint32_t>(status));
1145 reply->writeInt32(silenced ? 1 : 0);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001146 return NO_ERROR;
1147 } break;
1148
1149 case STOP_INPUT: {
1150 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001151 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1152 reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001153 return NO_ERROR;
1154 } break;
1155
1156 case RELEASE_INPUT: {
1157 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Eric Laurentfee19762018-01-29 18:44:13 -08001158 audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1159 releaseInput(portId);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001160 return NO_ERROR;
1161 } break;
1162
1163 case INIT_STREAM_VOLUME: {
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());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001167 int indexMin = data.readInt32();
1168 int indexMax = data.readInt32();
1169 reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1170 return NO_ERROR;
1171 } break;
1172
1173 case SET_STREAM_VOLUME: {
1174 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001175 audio_stream_type_t stream =
1176 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentc2f1f072009-07-17 12:17:14 -07001177 int index = data.readInt32();
Eric Laurent83844cc2011-11-18 16:43:31 -08001178 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1179 reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1180 index,
1181 device)));
Eric Laurentc2f1f072009-07-17 12:17:14 -07001182 return NO_ERROR;
1183 } break;
1184
1185 case GET_STREAM_VOLUME: {
1186 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001187 audio_stream_type_t stream =
1188 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurent83844cc2011-11-18 16:43:31 -08001189 audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
Robert Shih89235432015-09-02 16:46:59 -07001190 int index = 0;
Eric Laurent83844cc2011-11-18 16:43:31 -08001191 status_t status = getStreamVolumeIndex(stream, &index, device);
Eric Laurentc2f1f072009-07-17 12:17:14 -07001192 reply->writeInt32(index);
1193 reply->writeInt32(static_cast <uint32_t>(status));
1194 return NO_ERROR;
1195 } break;
1196
Eric Laurentde070132010-07-13 04:45:46 -07001197 case GET_STRATEGY_FOR_STREAM: {
1198 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001199 audio_stream_type_t stream =
1200 static_cast <audio_stream_type_t>(data.readInt32());
Eric Laurentde070132010-07-13 04:45:46 -07001201 reply->writeInt32(getStrategyForStream(stream));
1202 return NO_ERROR;
1203 } break;
1204
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001205 case GET_DEVICES_FOR_STREAM: {
1206 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Dima Zavinfce7a472011-04-19 22:30:36 -07001207 audio_stream_type_t stream =
1208 static_cast <audio_stream_type_t>(data.readInt32());
Glenn Kasten6b2718c2011-02-04 13:54:26 -08001209 reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1210 return NO_ERROR;
1211 } break;
1212
Eric Laurentde070132010-07-13 04:45:46 -07001213 case GET_OUTPUT_FOR_EFFECT: {
1214 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001215 effect_descriptor_t desc = {};
1216 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1217 android_errorWriteLog(0x534e4554, "73126106");
1218 }
1219 (void)sanitizeEffectDescriptor(&desc);
Eric Laurentde070132010-07-13 04:45:46 -07001220 audio_io_handle_t output = getOutputForEffect(&desc);
1221 reply->writeInt32(static_cast <int>(output));
1222 return NO_ERROR;
1223 } break;
1224
1225 case REGISTER_EFFECT: {
1226 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001227 effect_descriptor_t desc = {};
1228 if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1229 android_errorWriteLog(0x534e4554, "73126106");
1230 }
1231 (void)sanitizeEffectDescriptor(&desc);
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001232 audio_io_handle_t io = data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001233 uint32_t strategy = data.readInt32();
Glenn Kastend848eb42016-03-08 13:42:11 -08001234 audio_session_t session = (audio_session_t) data.readInt32();
Eric Laurentde070132010-07-13 04:45:46 -07001235 int id = data.readInt32();
1236 reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
Eric Laurent7c7f10b2011-06-17 21:29:58 -07001237 io,
Eric Laurentde070132010-07-13 04:45:46 -07001238 strategy,
1239 session,
1240 id)));
1241 return NO_ERROR;
1242 } break;
1243
1244 case UNREGISTER_EFFECT: {
1245 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1246 int id = data.readInt32();
1247 reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1248 return NO_ERROR;
1249 } break;
1250
Eric Laurentdb7c0792011-08-10 10:37:50 -07001251 case SET_EFFECT_ENABLED: {
1252 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1253 int id = data.readInt32();
1254 bool enabled = static_cast <bool>(data.readInt32());
1255 reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1256 return NO_ERROR;
1257 } break;
1258
Eric Laurenteda6c362011-02-02 09:33:30 -08001259 case IS_STREAM_ACTIVE: {
1260 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastenfff6d712012-01-12 16:38:12 -08001261 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
Eric Laurenteda6c362011-02-02 09:33:30 -08001262 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001263 reply->writeInt32( isStreamActive(stream, inPastMs) );
Eric Laurenteda6c362011-02-02 09:33:30 -08001264 return NO_ERROR;
1265 } break;
1266
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001267 case IS_STREAM_ACTIVE_REMOTELY: {
1268 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1269 audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1270 uint32_t inPastMs = (uint32_t)data.readInt32();
Eric Laurentebcb2542014-03-05 18:30:08 -08001271 reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
Jean-Michel Trivi272ab542013-02-04 16:26:02 -08001272 return NO_ERROR;
1273 } break;
1274
Jean-Michel Trivid7086032012-10-10 12:11:16 -07001275 case IS_SOURCE_ACTIVE: {
1276 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1277 audio_source_t source = (audio_source_t) data.readInt32();
1278 reply->writeInt32( isSourceActive(source));
1279 return NO_ERROR;
1280 }
1281
Eric Laurent57dae992011-07-24 13:36:09 -07001282 case QUERY_DEFAULT_PRE_PROCESSING: {
1283 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kastend848eb42016-03-08 13:42:11 -08001284 audio_session_t audioSession = (audio_session_t) data.readInt32();
Eric Laurent57dae992011-07-24 13:36:09 -07001285 uint32_t count = data.readInt32();
Eric Laurent74adca92014-11-05 12:15:36 -08001286 if (count > AudioEffect::kMaxPreProcessing) {
1287 count = AudioEffect::kMaxPreProcessing;
1288 }
Eric Laurent57dae992011-07-24 13:36:09 -07001289 uint32_t retCount = count;
Andy Hungb0272092018-04-12 11:06:56 -07001290 effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
Eric Laurent57dae992011-07-24 13:36:09 -07001291 status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1292 reply->writeInt32(status);
1293 if (status != NO_ERROR && status != NO_MEMORY) {
1294 retCount = 0;
1295 }
1296 reply->writeInt32(retCount);
Eric Laurent74adca92014-11-05 12:15:36 -08001297 if (retCount != 0) {
Eric Laurent57dae992011-07-24 13:36:09 -07001298 if (retCount < count) {
1299 count = retCount;
1300 }
1301 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1302 }
1303 delete[] descriptors;
1304 return status;
1305 }
1306
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001307 case IS_OFFLOAD_SUPPORTED: {
1308 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001309 audio_offload_info_t info = {};
Richard Fitzgeraldb1a270d2013-05-14 12:12:21 +01001310 data.read(&info, sizeof(audio_offload_info_t));
1311 bool isSupported = isOffloadSupported(info);
1312 reply->writeInt32(isSupported);
1313 return NO_ERROR;
1314 }
1315
Eric Laurent203b1a12014-04-01 10:34:16 -07001316 case LIST_AUDIO_PORTS: {
1317 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1318 audio_port_role_t role = (audio_port_role_t)data.readInt32();
1319 audio_port_type_t type = (audio_port_type_t)data.readInt32();
1320 unsigned int numPortsReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001321 if (numPortsReq > MAX_ITEMS_PER_LIST) {
1322 numPortsReq = MAX_ITEMS_PER_LIST;
1323 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001324 unsigned int numPorts = numPortsReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001325 struct audio_port *ports =
1326 (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
Eric Laurent1d670b12015-02-06 10:44:24 -08001327 if (ports == NULL) {
1328 reply->writeInt32(NO_MEMORY);
1329 reply->writeInt32(0);
1330 return NO_ERROR;
1331 }
1332 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001333 status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1334 reply->writeInt32(status);
1335 reply->writeInt32(numPorts);
Eric Laurent203b1a12014-04-01 10:34:16 -07001336
1337 if (status == NO_ERROR) {
1338 if (numPortsReq > numPorts) {
1339 numPortsReq = numPorts;
1340 }
1341 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1342 reply->writeInt32(generation);
1343 }
1344 free(ports);
1345 return NO_ERROR;
1346 }
1347
1348 case GET_AUDIO_PORT: {
1349 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Wei Jiae995e472015-09-09 09:48:34 -07001350 struct audio_port port = {};
1351 if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1352 ALOGE("b/23912202");
1353 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001354 status_t status = getAudioPort(&port);
1355 reply->writeInt32(status);
1356 if (status == NO_ERROR) {
1357 reply->write(&port, sizeof(struct audio_port));
1358 }
1359 return NO_ERROR;
1360 }
1361
1362 case CREATE_AUDIO_PATCH: {
1363 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001364 struct audio_patch patch = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001365 data.read(&patch, sizeof(struct audio_patch));
Glenn Kastena13cde92016-03-28 15:26:02 -07001366 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Wei Jiae995e472015-09-09 09:48:34 -07001367 if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1368 ALOGE("b/23912202");
1369 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001370 status_t status = createAudioPatch(&patch, &handle);
1371 reply->writeInt32(status);
1372 if (status == NO_ERROR) {
1373 reply->write(&handle, sizeof(audio_patch_handle_t));
1374 }
1375 return NO_ERROR;
1376 }
1377
1378 case RELEASE_AUDIO_PATCH: {
1379 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001380 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent203b1a12014-04-01 10:34:16 -07001381 data.read(&handle, sizeof(audio_patch_handle_t));
1382 status_t status = releaseAudioPatch(handle);
1383 reply->writeInt32(status);
1384 return NO_ERROR;
1385 }
1386
1387 case LIST_AUDIO_PATCHES: {
1388 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1389 unsigned int numPatchesReq = data.readInt32();
Eric Laurent1d670b12015-02-06 10:44:24 -08001390 if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1391 numPatchesReq = MAX_ITEMS_PER_LIST;
1392 }
Eric Laurent203b1a12014-04-01 10:34:16 -07001393 unsigned int numPatches = numPatchesReq;
Eric Laurent203b1a12014-04-01 10:34:16 -07001394 struct audio_patch *patches =
1395 (struct audio_patch *)calloc(numPatchesReq,
1396 sizeof(struct audio_patch));
Eric Laurent1d670b12015-02-06 10:44:24 -08001397 if (patches == NULL) {
1398 reply->writeInt32(NO_MEMORY);
1399 reply->writeInt32(0);
1400 return NO_ERROR;
1401 }
1402 unsigned int generation;
Eric Laurent203b1a12014-04-01 10:34:16 -07001403 status_t status = listAudioPatches(&numPatches, patches, &generation);
1404 reply->writeInt32(status);
1405 reply->writeInt32(numPatches);
1406 if (status == NO_ERROR) {
1407 if (numPatchesReq > numPatches) {
1408 numPatchesReq = numPatches;
1409 }
1410 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1411 reply->writeInt32(generation);
1412 }
1413 free(patches);
1414 return NO_ERROR;
1415 }
1416
1417 case SET_AUDIO_PORT_CONFIG: {
1418 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001419 struct audio_port_config config = {};
Eric Laurent203b1a12014-04-01 10:34:16 -07001420 data.read(&config, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001421 (void)sanitizeAudioPortConfig(&config);
Eric Laurent203b1a12014-04-01 10:34:16 -07001422 status_t status = setAudioPortConfig(&config);
1423 reply->writeInt32(status);
1424 return NO_ERROR;
1425 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001426
Eric Laurentb52c1522014-05-20 11:27:36 -07001427 case REGISTER_CLIENT: {
1428 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1429 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1430 data.readStrongBinder());
1431 registerClient(client);
1432 return NO_ERROR;
1433 } break;
Eric Laurent203b1a12014-04-01 10:34:16 -07001434
Eric Laurente8726fe2015-06-26 09:39:24 -07001435 case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1436 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1437 setAudioPortCallbacksEnabled(data.readInt32() == 1);
1438 return NO_ERROR;
1439 } break;
1440
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001441 case ACQUIRE_SOUNDTRIGGER_SESSION: {
1442 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1443 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1444 data.readStrongBinder());
Glenn Kastena13cde92016-03-28 15:26:02 -07001445 audio_session_t session = AUDIO_SESSION_NONE;
1446 audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1447 audio_devices_t device = AUDIO_DEVICE_NONE;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -07001448 status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1449 reply->writeInt32(status);
1450 if (status == NO_ERROR) {
1451 reply->writeInt32(session);
1452 reply->writeInt32(ioHandle);
1453 reply->writeInt32(device);
1454 }
1455 return NO_ERROR;
1456 } break;
1457
1458 case RELEASE_SOUNDTRIGGER_SESSION: {
1459 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1460 sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1461 data.readStrongBinder());
1462 audio_session_t session = (audio_session_t)data.readInt32();
1463 status_t status = releaseSoundTriggerSession(session);
1464 reply->writeInt32(status);
1465 return NO_ERROR;
1466 } break;
1467
Eric Laurentbb6c9a02014-09-25 14:11:47 -07001468 case GET_PHONE_STATE: {
1469 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1470 reply->writeInt32((int32_t)getPhoneState());
1471 return NO_ERROR;
1472 } break;
1473
Eric Laurentbaac1832014-12-01 17:52:59 -08001474 case REGISTER_POLICY_MIXES: {
1475 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1476 bool registration = data.readInt32() == 1;
1477 Vector<AudioMix> mixes;
1478 size_t size = (size_t)data.readInt32();
1479 if (size > MAX_MIXES_PER_POLICY) {
1480 size = MAX_MIXES_PER_POLICY;
1481 }
1482 for (size_t i = 0; i < size; i++) {
1483 AudioMix mix;
1484 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1485 mixes.add(mix);
1486 }
1487 }
1488 status_t status = registerPolicyMixes(mixes, registration);
1489 reply->writeInt32(status);
1490 return NO_ERROR;
1491 } break;
1492
Eric Laurent554a2772015-04-10 11:29:24 -07001493 case START_AUDIO_SOURCE: {
1494 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Andy Hungb0272092018-04-12 11:06:56 -07001495 struct audio_port_config source = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001496 data.read(&source, sizeof(struct audio_port_config));
Andy Hungb0272092018-04-12 11:06:56 -07001497 (void)sanitizeAudioPortConfig(&source);
1498 audio_attributes_t attributes = {};
Eric Laurent554a2772015-04-10 11:29:24 -07001499 data.read(&attributes, sizeof(audio_attributes_t));
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001500 sanetizeAudioAttributes(&attributes);
Glenn Kasten559d4392016-03-29 13:42:57 -07001501 audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
Eric Laurent554a2772015-04-10 11:29:24 -07001502 status_t status = startAudioSource(&source, &attributes, &handle);
1503 reply->writeInt32(status);
1504 reply->writeInt32(handle);
1505 return NO_ERROR;
1506 } break;
1507
1508 case STOP_AUDIO_SOURCE: {
1509 CHECK_INTERFACE(IAudioPolicyService, data, reply);
Glenn Kasten559d4392016-03-29 13:42:57 -07001510 audio_patch_handle_t handle = (audio_patch_handle_t) data.readInt32();
Eric Laurent554a2772015-04-10 11:29:24 -07001511 status_t status = stopAudioSource(handle);
1512 reply->writeInt32(status);
1513 return NO_ERROR;
1514 } break;
1515
Andy Hung2ddee192015-12-18 17:34:44 -08001516 case SET_MASTER_MONO: {
1517 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1518 bool mono = static_cast<bool>(data.readInt32());
1519 status_t status = setMasterMono(mono);
1520 reply->writeInt32(status);
1521 return NO_ERROR;
1522 } break;
1523
1524 case GET_MASTER_MONO: {
1525 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1526 bool mono;
1527 status_t status = getMasterMono(&mono);
1528 reply->writeInt32(status);
1529 if (status == NO_ERROR) {
1530 reply->writeInt32(static_cast<int32_t>(mono));
1531 }
1532 return NO_ERROR;
1533 } break;
1534
Eric Laurentac9cef52017-06-09 15:46:26 -07001535 case GET_STREAM_VOLUME_DB: {
1536 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1537 audio_stream_type_t stream =
1538 static_cast <audio_stream_type_t>(data.readInt32());
1539 int index = static_cast <int>(data.readInt32());
1540 audio_devices_t device =
1541 static_cast <audio_devices_t>(data.readUint32());
1542 reply->writeFloat(getStreamVolumeDB(stream, index, device));
1543 return NO_ERROR;
1544 }
1545
jiabin81772902018-04-02 17:52:27 -07001546 case GET_SURROUND_FORMATS: {
1547 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1548 unsigned int numSurroundFormatsReq = data.readUint32();
1549 if (numSurroundFormatsReq > MAX_ITEMS_PER_LIST) {
1550 numSurroundFormatsReq = MAX_ITEMS_PER_LIST;
1551 }
1552 bool reported = data.readBool();
1553 unsigned int numSurroundFormats = numSurroundFormatsReq;
1554 audio_format_t *surroundFormats = (audio_format_t *)calloc(
1555 numSurroundFormats, sizeof(audio_format_t));
1556 bool *surroundFormatsEnabled = (bool *)calloc(numSurroundFormats, sizeof(bool));
1557 if (numSurroundFormatsReq > 0 &&
1558 (surroundFormats == NULL || surroundFormatsEnabled == NULL)) {
1559 free(surroundFormats);
1560 free(surroundFormatsEnabled);
1561 reply->writeInt32(NO_MEMORY);
1562 return NO_ERROR;
1563 }
1564 status_t status = getSurroundFormats(
1565 &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
1566 reply->writeInt32(status);
1567
1568 if (status == NO_ERROR) {
1569 reply->writeUint32(numSurroundFormats);
1570 if (numSurroundFormatsReq > numSurroundFormats) {
1571 numSurroundFormatsReq = numSurroundFormats;
1572 }
1573 reply->write(surroundFormats, numSurroundFormatsReq * sizeof(audio_format_t));
1574 reply->write(surroundFormatsEnabled, numSurroundFormatsReq * sizeof(bool));
1575 }
1576 free(surroundFormats);
1577 free(surroundFormatsEnabled);
1578 return NO_ERROR;
1579 }
1580
1581 case SET_SURROUND_FORMAT_ENABLED: {
1582 CHECK_INTERFACE(IAudioPolicyService, data, reply);
1583 audio_format_t audioFormat = (audio_format_t) data.readInt32();
1584 bool enabled = data.readBool();
1585 status_t status = setSurroundFormatEnabled(audioFormat, enabled);
1586 reply->writeInt32(status);
1587 return NO_ERROR;
1588 }
1589
Eric Laurentc2f1f072009-07-17 12:17:14 -07001590 default:
1591 return BBinder::onTransact(code, data, reply, flags);
1592 }
1593}
1594
Andy Hungb0272092018-04-12 11:06:56 -07001595/** returns true if string overflow was prevented by zero termination */
1596template <size_t size>
1597static bool preventStringOverflow(char (&s)[size]) {
1598 if (strnlen(s, size) < size) return false;
1599 s[size - 1] = '\0';
1600 return true;
1601}
1602
Kevin Rocard39fdbd02017-11-13 11:15:27 -08001603void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
1604{
1605 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
1606 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
1607 android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
1608 }
1609 attr->tags[tagsMaxSize - 1] = '\0';
1610}
1611
Andy Hungb0272092018-04-12 11:06:56 -07001612/** returns BAD_VALUE if sanitization was required. */
1613status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
1614{
1615 if (preventStringOverflow(desc->name)
1616 | /* always */ preventStringOverflow(desc->implementor)) {
1617 android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
1618 return BAD_VALUE;
1619 }
1620 return NO_ERROR;
1621}
1622
1623/** returns BAD_VALUE if sanitization was required. */
1624status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
1625{
1626 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
1627 preventStringOverflow(config->ext.device.address)) {
1628 return BAD_VALUE;
1629 }
1630 return NO_ERROR;
1631}
1632
Eric Laurentc2f1f072009-07-17 12:17:14 -07001633// ----------------------------------------------------------------------------
1634
Glenn Kasten40bc9062015-03-20 09:09:33 -07001635} // namespace android