1 /*
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>
22 #include <math.h>
23 #include <sys/types.h>
24 
25 #include <binder/IPCThreadState.h>
26 #include <binder/Parcel.h>
27 #include <media/AudioEffect.h>
28 #include <media/IAudioPolicyService.h>
29 #include <mediautils/ServiceUtilities.h>
30 #include <mediautils/TimeCheck.h>
31 #include <system/audio.h>
32 
33 namespace android {
34 
35 enum {
36     SET_DEVICE_CONNECTION_STATE = IBinder::FIRST_CALL_TRANSACTION,
37     GET_DEVICE_CONNECTION_STATE,
38     HANDLE_DEVICE_CONFIG_CHANGE,
39     SET_PHONE_STATE,
40     SET_RINGER_MODE,    // reserved, no longer used
41     SET_FORCE_USE,
42     GET_FORCE_USE,
43     GET_OUTPUT,
44     START_OUTPUT,
45     STOP_OUTPUT,
46     RELEASE_OUTPUT,
47     GET_INPUT_FOR_ATTR,
48     START_INPUT,
49     STOP_INPUT,
50     RELEASE_INPUT,
51     INIT_STREAM_VOLUME,
52     SET_STREAM_VOLUME,
53     GET_STREAM_VOLUME,
54     SET_VOLUME_ATTRIBUTES,
55     GET_VOLUME_ATTRIBUTES,
56     GET_MIN_VOLUME_FOR_ATTRIBUTES,
57     GET_MAX_VOLUME_FOR_ATTRIBUTES,
58     GET_STRATEGY_FOR_STREAM,
59     GET_OUTPUT_FOR_EFFECT,
60     REGISTER_EFFECT,
61     UNREGISTER_EFFECT,
62     IS_STREAM_ACTIVE,
63     IS_SOURCE_ACTIVE,
64     GET_DEVICES_FOR_STREAM,
65     QUERY_DEFAULT_PRE_PROCESSING,
66     SET_EFFECT_ENABLED,
67     IS_STREAM_ACTIVE_REMOTELY,
68     IS_OFFLOAD_SUPPORTED,
69     IS_DIRECT_OUTPUT_SUPPORTED,
70     LIST_AUDIO_PORTS,
71     GET_AUDIO_PORT,
72     CREATE_AUDIO_PATCH,
73     RELEASE_AUDIO_PATCH,
74     LIST_AUDIO_PATCHES,
75     SET_AUDIO_PORT_CONFIG,
76     REGISTER_CLIENT,
77     GET_OUTPUT_FOR_ATTR,
78     ACQUIRE_SOUNDTRIGGER_SESSION,
79     RELEASE_SOUNDTRIGGER_SESSION,
80     GET_PHONE_STATE,
81     REGISTER_POLICY_MIXES,
82     START_AUDIO_SOURCE,
83     STOP_AUDIO_SOURCE,
84     SET_AUDIO_PORT_CALLBACK_ENABLED,
85     SET_AUDIO_VOLUME_GROUP_CALLBACK_ENABLED,
86     SET_MASTER_MONO,
87     GET_MASTER_MONO,
88     GET_STREAM_VOLUME_DB,
89     GET_SURROUND_FORMATS,
90     SET_SURROUND_FORMAT_ENABLED,
91     ADD_STREAM_DEFAULT_EFFECT,
92     REMOVE_STREAM_DEFAULT_EFFECT,
93     ADD_SOURCE_DEFAULT_EFFECT,
94     REMOVE_SOURCE_DEFAULT_EFFECT,
95     SET_ASSISTANT_UID,
96     SET_A11Y_SERVICES_UIDS,
97     IS_HAPTIC_PLAYBACK_SUPPORTED,
98     SET_UID_DEVICE_AFFINITY,
99     REMOVE_UID_DEVICE_AFFINITY,
100     GET_OFFLOAD_FORMATS_A2DP,
101     LIST_AUDIO_PRODUCT_STRATEGIES,
102     GET_STRATEGY_FOR_ATTRIBUTES,
103     LIST_AUDIO_VOLUME_GROUPS,
104     GET_VOLUME_GROUP_FOR_ATTRIBUTES,
105     SET_ALLOWED_CAPTURE_POLICY,
106     MOVE_EFFECTS_TO_IO,
107     SET_RTT_ENABLED,
108     SET_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY,
109     REMOVE_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY,
110     GET_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY,
111     AUDIO_MODULES_UPDATED,  // oneway
112 };
113 
114 #define MAX_ITEMS_PER_LIST 1024
115 
116 class BpAudioPolicyService : public BpInterface<IAudioPolicyService>
117 {
118 public:
BpAudioPolicyService(const sp<IBinder> & impl)119     explicit BpAudioPolicyService(const sp<IBinder>& impl)
120         : BpInterface<IAudioPolicyService>(impl)
121     {
122     }
123 
setDeviceConnectionState(audio_devices_t device,audio_policy_dev_state_t state,const char * device_address,const char * device_name,audio_format_t encodedFormat)124     virtual status_t setDeviceConnectionState(
125                                     audio_devices_t device,
126                                     audio_policy_dev_state_t state,
127                                     const char *device_address,
128                                     const char *device_name,
129                                     audio_format_t encodedFormat)
130     {
131         Parcel data, reply;
132         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
133         data.writeInt32(static_cast <uint32_t>(device));
134         data.writeInt32(static_cast <uint32_t>(state));
135         data.writeCString(device_address);
136         data.writeCString(device_name);
137         data.writeInt32(static_cast <uint32_t>(encodedFormat));
138         remote()->transact(SET_DEVICE_CONNECTION_STATE, data, &reply);
139         return static_cast <status_t> (reply.readInt32());
140     }
141 
getDeviceConnectionState(audio_devices_t device,const char * device_address)142     virtual audio_policy_dev_state_t getDeviceConnectionState(
143                                     audio_devices_t device,
144                                     const char *device_address)
145     {
146         Parcel data, reply;
147         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
148         data.writeInt32(static_cast <uint32_t>(device));
149         data.writeCString(device_address);
150         remote()->transact(GET_DEVICE_CONNECTION_STATE, data, &reply);
151         return static_cast <audio_policy_dev_state_t>(reply.readInt32());
152     }
153 
handleDeviceConfigChange(audio_devices_t device,const char * device_address,const char * device_name,audio_format_t encodedFormat)154     virtual status_t handleDeviceConfigChange(audio_devices_t device,
155                                               const char *device_address,
156                                               const char *device_name,
157                                               audio_format_t encodedFormat)
158     {
159         Parcel data, reply;
160         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
161         data.writeInt32(static_cast <uint32_t>(device));
162         data.writeCString(device_address);
163         data.writeCString(device_name);
164         data.writeInt32(static_cast <uint32_t>(encodedFormat));
165         remote()->transact(HANDLE_DEVICE_CONFIG_CHANGE, data, &reply);
166         return static_cast <status_t> (reply.readInt32());
167     }
168 
setPhoneState(audio_mode_t state)169     virtual status_t setPhoneState(audio_mode_t state)
170     {
171         Parcel data, reply;
172         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
173         data.writeInt32(state);
174         remote()->transact(SET_PHONE_STATE, data, &reply);
175         return static_cast <status_t> (reply.readInt32());
176     }
177 
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)178     virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
179     {
180         Parcel data, reply;
181         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
182         data.writeInt32(static_cast <uint32_t>(usage));
183         data.writeInt32(static_cast <uint32_t>(config));
184         remote()->transact(SET_FORCE_USE, data, &reply);
185         return static_cast <status_t> (reply.readInt32());
186     }
187 
getForceUse(audio_policy_force_use_t usage)188     virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage)
189     {
190         Parcel data, reply;
191         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
192         data.writeInt32(static_cast <uint32_t>(usage));
193         remote()->transact(GET_FORCE_USE, data, &reply);
194         return static_cast <audio_policy_forced_cfg_t> (reply.readInt32());
195     }
196 
getOutput(audio_stream_type_t stream)197     virtual audio_io_handle_t getOutput(audio_stream_type_t stream)
198     {
199         Parcel data, reply;
200         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
201         data.writeInt32(static_cast <uint32_t>(stream));
202         remote()->transact(GET_OUTPUT, data, &reply);
203         return static_cast <audio_io_handle_t> (reply.readInt32());
204     }
205 
getOutputForAttr(audio_attributes_t * attr,audio_io_handle_t * output,audio_session_t session,audio_stream_type_t * stream,pid_t pid,uid_t uid,const audio_config_t * config,audio_output_flags_t flags,audio_port_handle_t * selectedDeviceId,audio_port_handle_t * portId,std::vector<audio_io_handle_t> * secondaryOutputs)206     status_t getOutputForAttr(audio_attributes_t *attr,
207                               audio_io_handle_t *output,
208                               audio_session_t session,
209                               audio_stream_type_t *stream,
210                               pid_t pid,
211                               uid_t uid,
212                               const audio_config_t *config,
213                               audio_output_flags_t flags,
214                               audio_port_handle_t *selectedDeviceId,
215                               audio_port_handle_t *portId,
216                               std::vector<audio_io_handle_t> *secondaryOutputs) override
217         {
218             Parcel data, reply;
219             data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
220             if (attr == nullptr) {
221                 ALOGE("%s NULL audio attributes", __func__);
222                 return BAD_VALUE;
223             }
224             if (output == nullptr) {
225                 ALOGE("%s NULL output - shouldn't happen", __func__);
226                 return BAD_VALUE;
227             }
228             if (selectedDeviceId == nullptr) {
229                 ALOGE("%s NULL selectedDeviceId - shouldn't happen", __func__);
230                 return BAD_VALUE;
231             }
232             if (portId == nullptr) {
233                 ALOGE("%s NULL portId - shouldn't happen", __func__);
234                 return BAD_VALUE;
235             }
236             if (secondaryOutputs == nullptr) {
237                 ALOGE("%s NULL secondaryOutputs - shouldn't happen", __func__);
238                 return BAD_VALUE;
239             }
240             data.write(attr, sizeof(audio_attributes_t));
241             data.writeInt32(session);
242             if (stream == NULL) {
243                 data.writeInt32(0);
244             } else {
245                 data.writeInt32(1);
246                 data.writeInt32(*stream);
247             }
248             data.writeInt32(pid);
249             data.writeInt32(uid);
250             data.write(config, sizeof(audio_config_t));
251             data.writeInt32(static_cast <uint32_t>(flags));
252             data.writeInt32(*selectedDeviceId);
253             data.writeInt32(*portId);
254             status_t status = remote()->transact(GET_OUTPUT_FOR_ATTR, data, &reply);
255             if (status != NO_ERROR) {
256                 return status;
257             }
258             status = (status_t)reply.readInt32();
259             if (status != NO_ERROR) {
260                 return status;
261             }
262             status = (status_t)reply.read(&attr, sizeof(audio_attributes_t));
263             if (status != NO_ERROR) {
264                 return status;
265             }
266             *output = (audio_io_handle_t)reply.readInt32();
267             audio_stream_type_t lStream = (audio_stream_type_t)reply.readInt32();
268             if (stream != NULL) {
269                 *stream = lStream;
270             }
271             *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
272             *portId = (audio_port_handle_t)reply.readInt32();
273             secondaryOutputs->resize(reply.readInt32());
274             return reply.read(secondaryOutputs->data(),
275                               secondaryOutputs->size() * sizeof(audio_io_handle_t));
276         }
277 
startOutput(audio_port_handle_t portId)278     virtual status_t startOutput(audio_port_handle_t portId)
279     {
280         Parcel data, reply;
281         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
282         data.writeInt32((int32_t)portId);
283         remote()->transact(START_OUTPUT, data, &reply);
284         return static_cast <status_t> (reply.readInt32());
285     }
286 
stopOutput(audio_port_handle_t portId)287     virtual status_t stopOutput(audio_port_handle_t portId)
288     {
289         Parcel data, reply;
290         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
291         data.writeInt32((int32_t)portId);
292         remote()->transact(STOP_OUTPUT, data, &reply);
293         return static_cast <status_t> (reply.readInt32());
294     }
295 
releaseOutput(audio_port_handle_t portId)296     virtual void releaseOutput(audio_port_handle_t portId)
297     {
298         Parcel data, reply;
299         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
300         data.writeInt32((int32_t)portId);
301         remote()->transact(RELEASE_OUTPUT, data, &reply);
302     }
303 
getInputForAttr(const audio_attributes_t * attr,audio_io_handle_t * input,audio_unique_id_t riid,audio_session_t session,pid_t pid,uid_t uid,const String16 & opPackageName,const audio_config_base_t * config,audio_input_flags_t flags,audio_port_handle_t * selectedDeviceId,audio_port_handle_t * portId)304     virtual status_t getInputForAttr(const audio_attributes_t *attr,
305                                      audio_io_handle_t *input,
306                                      audio_unique_id_t riid,
307                                      audio_session_t session,
308                                      pid_t pid,
309                                      uid_t uid,
310                                      const String16& opPackageName,
311                                      const audio_config_base_t *config,
312                                      audio_input_flags_t flags,
313                                      audio_port_handle_t *selectedDeviceId,
314                                      audio_port_handle_t *portId)
315     {
316         Parcel data, reply;
317         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
318         if (attr == NULL) {
319             ALOGE("getInputForAttr NULL attr - shouldn't happen");
320             return BAD_VALUE;
321         }
322         if (input == NULL) {
323             ALOGE("getInputForAttr NULL input - shouldn't happen");
324             return BAD_VALUE;
325         }
326         if (selectedDeviceId == NULL) {
327             ALOGE("getInputForAttr NULL selectedDeviceId - shouldn't happen");
328             return BAD_VALUE;
329         }
330         if (portId == NULL) {
331             ALOGE("getInputForAttr NULL portId - shouldn't happen");
332             return BAD_VALUE;
333         }
334         data.write(attr, sizeof(audio_attributes_t));
335         data.writeInt32(*input);
336         data.writeInt32(riid);
337         data.writeInt32(session);
338         data.writeInt32(pid);
339         data.writeInt32(uid);
340         data.writeString16(opPackageName);
341         data.write(config, sizeof(audio_config_base_t));
342         data.writeInt32(flags);
343         data.writeInt32(*selectedDeviceId);
344         data.writeInt32(*portId);
345         status_t status = remote()->transact(GET_INPUT_FOR_ATTR, data, &reply);
346         if (status != NO_ERROR) {
347             return status;
348         }
349         status = reply.readInt32();
350         if (status != NO_ERROR) {
351             return status;
352         }
353         *input = (audio_io_handle_t)reply.readInt32();
354         *selectedDeviceId = (audio_port_handle_t)reply.readInt32();
355         *portId = (audio_port_handle_t)reply.readInt32();
356         return NO_ERROR;
357     }
358 
startInput(audio_port_handle_t portId)359     virtual status_t startInput(audio_port_handle_t portId)
360     {
361         Parcel data, reply;
362         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
363         data.writeInt32(portId);
364         remote()->transact(START_INPUT, data, &reply);
365         status_t status = static_cast <status_t> (reply.readInt32());
366         return status;
367     }
368 
stopInput(audio_port_handle_t portId)369     virtual status_t stopInput(audio_port_handle_t portId)
370     {
371         Parcel data, reply;
372         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
373         data.writeInt32(portId);
374         remote()->transact(STOP_INPUT, data, &reply);
375         return static_cast <status_t> (reply.readInt32());
376     }
377 
releaseInput(audio_port_handle_t portId)378     virtual void releaseInput(audio_port_handle_t portId)
379     {
380         Parcel data, reply;
381         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
382         data.writeInt32(portId);
383         remote()->transact(RELEASE_INPUT, data, &reply);
384     }
385 
initStreamVolume(audio_stream_type_t stream,int indexMin,int indexMax)386     virtual status_t initStreamVolume(audio_stream_type_t stream,
387                                     int indexMin,
388                                     int indexMax)
389     {
390         Parcel data, reply;
391         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
392         data.writeInt32(static_cast <uint32_t>(stream));
393         data.writeInt32(indexMin);
394         data.writeInt32(indexMax);
395         remote()->transact(INIT_STREAM_VOLUME, data, &reply);
396         return static_cast <status_t> (reply.readInt32());
397     }
398 
setStreamVolumeIndex(audio_stream_type_t stream,int index,audio_devices_t device)399     virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
400                                           int index,
401                                           audio_devices_t device)
402     {
403         Parcel data, reply;
404         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
405         data.writeInt32(static_cast <uint32_t>(stream));
406         data.writeInt32(index);
407         data.writeInt32(static_cast <uint32_t>(device));
408         remote()->transact(SET_STREAM_VOLUME, data, &reply);
409         return static_cast <status_t> (reply.readInt32());
410     }
411 
getStreamVolumeIndex(audio_stream_type_t stream,int * index,audio_devices_t device)412     virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
413                                           int *index,
414                                           audio_devices_t device)
415     {
416         Parcel data, reply;
417         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
418         data.writeInt32(static_cast <uint32_t>(stream));
419         data.writeInt32(static_cast <uint32_t>(device));
420 
421         remote()->transact(GET_STREAM_VOLUME, data, &reply);
422         int lIndex = reply.readInt32();
423         if (index) *index = lIndex;
424         return static_cast <status_t> (reply.readInt32());
425     }
426 
setVolumeIndexForAttributes(const audio_attributes_t & attr,int index,audio_devices_t device)427     virtual status_t setVolumeIndexForAttributes(const audio_attributes_t &attr, int index,
428                                                  audio_devices_t device)
429     {
430         Parcel data, reply;
431         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
432         data.write(&attr, sizeof(audio_attributes_t));
433         data.writeInt32(index);
434         data.writeInt32(static_cast <uint32_t>(device));
435         status_t status = remote()->transact(SET_VOLUME_ATTRIBUTES, data, &reply);
436         if (status != NO_ERROR) {
437             return status;
438         }
439         return static_cast <status_t> (reply.readInt32());
440     }
getVolumeIndexForAttributes(const audio_attributes_t & attr,int & index,audio_devices_t device)441     virtual status_t getVolumeIndexForAttributes(const audio_attributes_t &attr, int &index,
442                                                  audio_devices_t device)
443     {
444         Parcel data, reply;
445         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
446         data.write(&attr, sizeof(audio_attributes_t));
447         data.writeInt32(static_cast <uint32_t>(device));
448         status_t status = remote()->transact(GET_VOLUME_ATTRIBUTES, data, &reply);
449         if (status != NO_ERROR) {
450             return status;
451         }
452         status = static_cast <status_t> (reply.readInt32());
453         if (status != NO_ERROR) {
454             return status;
455         }
456         index = reply.readInt32();
457         return NO_ERROR;
458     }
getMinVolumeIndexForAttributes(const audio_attributes_t & attr,int & index)459     virtual status_t getMinVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
460     {
461         Parcel data, reply;
462         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
463         data.write(&attr, sizeof(audio_attributes_t));
464         status_t status = remote()->transact(GET_MIN_VOLUME_FOR_ATTRIBUTES, data, &reply);
465         if (status != NO_ERROR) {
466             return status;
467         }
468         status = static_cast <status_t> (reply.readInt32());
469         if (status != NO_ERROR) {
470             return status;
471         }
472         index = reply.readInt32();
473         return NO_ERROR;
474     }
getMaxVolumeIndexForAttributes(const audio_attributes_t & attr,int & index)475     virtual status_t getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, int &index)
476     {
477         Parcel data, reply;
478         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
479         data.write(&attr, sizeof(audio_attributes_t));
480         status_t status = remote()->transact(GET_MAX_VOLUME_FOR_ATTRIBUTES, data, &reply);
481         if (status != NO_ERROR) {
482             return status;
483         }
484         status = static_cast <status_t> (reply.readInt32());
485         if (status != NO_ERROR) {
486             return status;
487         }
488         index = reply.readInt32();
489         return NO_ERROR;
490     }
getStrategyForStream(audio_stream_type_t stream)491     virtual uint32_t getStrategyForStream(audio_stream_type_t stream)
492     {
493         Parcel data, reply;
494         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
495         data.writeInt32(static_cast <uint32_t>(stream));
496         remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply);
497         return reply.readUint32();
498     }
499 
getDevicesForStream(audio_stream_type_t stream)500     virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream)
501     {
502         Parcel data, reply;
503         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
504         data.writeInt32(static_cast <uint32_t>(stream));
505         remote()->transact(GET_DEVICES_FOR_STREAM, data, &reply);
506         return (audio_devices_t) reply.readInt32();
507     }
508 
getOutputForEffect(const effect_descriptor_t * desc)509     virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc)
510     {
511         Parcel data, reply;
512         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
513         data.write(desc, sizeof(effect_descriptor_t));
514         remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply);
515         return static_cast <audio_io_handle_t> (reply.readInt32());
516     }
517 
registerEffect(const effect_descriptor_t * desc,audio_io_handle_t io,uint32_t strategy,audio_session_t session,int id)518     virtual status_t registerEffect(const effect_descriptor_t *desc,
519                                         audio_io_handle_t io,
520                                         uint32_t strategy,
521                                         audio_session_t session,
522                                         int id)
523     {
524         Parcel data, reply;
525         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
526         data.write(desc, sizeof(effect_descriptor_t));
527         data.writeInt32(io);
528         data.writeInt32(strategy);
529         data.writeInt32(session);
530         data.writeInt32(id);
531         remote()->transact(REGISTER_EFFECT, data, &reply);
532         return static_cast <status_t> (reply.readInt32());
533     }
534 
unregisterEffect(int id)535     virtual status_t unregisterEffect(int id)
536     {
537         Parcel data, reply;
538         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
539         data.writeInt32(id);
540         remote()->transact(UNREGISTER_EFFECT, data, &reply);
541         return static_cast <status_t> (reply.readInt32());
542     }
543 
setEffectEnabled(int id,bool enabled)544     virtual status_t setEffectEnabled(int id, bool enabled)
545     {
546         Parcel data, reply;
547         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
548         data.writeInt32(id);
549         data.writeInt32(enabled);
550         remote()->transact(SET_EFFECT_ENABLED, data, &reply);
551         return static_cast <status_t> (reply.readInt32());
552     }
553 
moveEffectsToIo(const std::vector<int> & ids,audio_io_handle_t io)554     status_t moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) override
555     {
556         Parcel data, reply;
557         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
558         data.writeInt32(ids.size());
559         for (auto id : ids) {
560             data.writeInt32(id);
561         }
562         data.writeInt32(io);
563         status_t status = remote()->transact(MOVE_EFFECTS_TO_IO, data, &reply);
564         if (status != NO_ERROR) {
565             return status;
566         }
567         return static_cast <status_t> (reply.readInt32());
568     }
569 
isStreamActive(audio_stream_type_t stream,uint32_t inPastMs) const570     virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
571     {
572         Parcel data, reply;
573         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
574         data.writeInt32((int32_t) stream);
575         data.writeInt32(inPastMs);
576         remote()->transact(IS_STREAM_ACTIVE, data, &reply);
577         return reply.readInt32();
578     }
579 
isStreamActiveRemotely(audio_stream_type_t stream,uint32_t inPastMs) const580     virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
581     {
582         Parcel data, reply;
583         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
584         data.writeInt32((int32_t) stream);
585         data.writeInt32(inPastMs);
586         remote()->transact(IS_STREAM_ACTIVE_REMOTELY, data, &reply);
587         return reply.readInt32();
588     }
589 
isSourceActive(audio_source_t source) const590     virtual bool isSourceActive(audio_source_t source) const
591     {
592         Parcel data, reply;
593         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
594         data.writeInt32((int32_t) source);
595         remote()->transact(IS_SOURCE_ACTIVE, data, &reply);
596         return reply.readInt32();
597     }
598 
queryDefaultPreProcessing(audio_session_t audioSession,effect_descriptor_t * descriptors,uint32_t * count)599     virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
600                                                effect_descriptor_t *descriptors,
601                                                uint32_t *count)
602     {
603         if (descriptors == NULL || count == NULL) {
604             return BAD_VALUE;
605         }
606         Parcel data, reply;
607         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
608         data.writeInt32(audioSession);
609         data.writeInt32(*count);
610         status_t status = remote()->transact(QUERY_DEFAULT_PRE_PROCESSING, data, &reply);
611         if (status != NO_ERROR) {
612             return status;
613         }
614         status = static_cast <status_t> (reply.readInt32());
615         uint32_t retCount = reply.readInt32();
616         if (retCount != 0) {
617             uint32_t numDesc = (retCount < *count) ? retCount : *count;
618             reply.read(descriptors, sizeof(effect_descriptor_t) * numDesc);
619         }
620         *count = retCount;
621         return status;
622     }
623 
setAllowedCapturePolicy(uid_t uid,audio_flags_mask_t flags)624     status_t setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t flags) override {
625         Parcel data, reply;
626         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
627         data.writeInt32(uid);
628         data.writeInt32(flags);
629         remote()->transact(SET_ALLOWED_CAPTURE_POLICY, data, &reply);
630         return reply.readInt32();
631     }
632 
isOffloadSupported(const audio_offload_info_t & info)633     virtual bool isOffloadSupported(const audio_offload_info_t& info)
634     {
635         Parcel data, reply;
636         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
637         data.write(&info, sizeof(audio_offload_info_t));
638         remote()->transact(IS_OFFLOAD_SUPPORTED, data, &reply);
639         return reply.readInt32();
640     }
641 
isDirectOutputSupported(const audio_config_base_t & config,const audio_attributes_t & attributes)642     virtual bool isDirectOutputSupported(const audio_config_base_t& config,
643                                          const audio_attributes_t& attributes) {
644         Parcel data, reply;
645         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
646         data.write(&config, sizeof(audio_config_base_t));
647         data.write(&attributes, sizeof(audio_attributes_t));
648         status_t status = remote()->transact(IS_DIRECT_OUTPUT_SUPPORTED, data, &reply);
649         return status == NO_ERROR ? static_cast<bool>(reply.readInt32()) : false;
650     }
651 
listAudioPorts(audio_port_role_t role,audio_port_type_t type,unsigned int * num_ports,struct audio_port * ports,unsigned int * generation)652     virtual status_t listAudioPorts(audio_port_role_t role,
653                                     audio_port_type_t type,
654                                     unsigned int *num_ports,
655                                     struct audio_port *ports,
656                                     unsigned int *generation)
657     {
658         if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
659                 generation == NULL) {
660             return BAD_VALUE;
661         }
662         Parcel data, reply;
663         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
664         unsigned int numPortsReq = (ports == NULL) ? 0 : *num_ports;
665         data.writeInt32(role);
666         data.writeInt32(type);
667         data.writeInt32(numPortsReq);
668         status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply);
669         if (status == NO_ERROR) {
670             status = (status_t)reply.readInt32();
671             *num_ports = (unsigned int)reply.readInt32();
672         }
673         if (status == NO_ERROR) {
674             if (numPortsReq > *num_ports) {
675                 numPortsReq = *num_ports;
676             }
677             if (numPortsReq > 0) {
678                 reply.read(ports, numPortsReq * sizeof(struct audio_port));
679             }
680             *generation = reply.readInt32();
681         }
682         return status;
683     }
684 
getAudioPort(struct audio_port * port)685     virtual status_t getAudioPort(struct audio_port *port)
686     {
687         if (port == NULL) {
688             return BAD_VALUE;
689         }
690         Parcel data, reply;
691         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
692         data.write(port, sizeof(struct audio_port));
693         status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply);
694         if (status != NO_ERROR ||
695                 (status = (status_t)reply.readInt32()) != NO_ERROR) {
696             return status;
697         }
698         reply.read(port, sizeof(struct audio_port));
699         return status;
700     }
701 
createAudioPatch(const struct audio_patch * patch,audio_patch_handle_t * handle)702     virtual status_t createAudioPatch(const struct audio_patch *patch,
703                                        audio_patch_handle_t *handle)
704     {
705         if (patch == NULL || handle == NULL) {
706             return BAD_VALUE;
707         }
708         Parcel data, reply;
709         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
710         data.write(patch, sizeof(struct audio_patch));
711         data.write(handle, sizeof(audio_patch_handle_t));
712         status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply);
713         if (status != NO_ERROR ||
714                 (status = (status_t)reply.readInt32()) != NO_ERROR) {
715             return status;
716         }
717         reply.read(handle, sizeof(audio_patch_handle_t));
718         return status;
719     }
720 
releaseAudioPatch(audio_patch_handle_t handle)721     virtual status_t releaseAudioPatch(audio_patch_handle_t handle)
722     {
723         Parcel data, reply;
724         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
725         data.write(&handle, sizeof(audio_patch_handle_t));
726         status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply);
727         if (status != NO_ERROR) {
728             status = (status_t)reply.readInt32();
729         }
730         return status;
731     }
732 
listAudioPatches(unsigned int * num_patches,struct audio_patch * patches,unsigned int * generation)733     virtual status_t listAudioPatches(unsigned int *num_patches,
734                                       struct audio_patch *patches,
735                                       unsigned int *generation)
736     {
737         if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
738                 generation == NULL) {
739             return BAD_VALUE;
740         }
741         Parcel data, reply;
742         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
743         unsigned int numPatchesReq = (patches == NULL) ? 0 : *num_patches;
744         data.writeInt32(numPatchesReq);
745         status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply);
746         if (status == NO_ERROR) {
747             status = (status_t)reply.readInt32();
748             *num_patches = (unsigned int)reply.readInt32();
749         }
750         if (status == NO_ERROR) {
751             if (numPatchesReq > *num_patches) {
752                 numPatchesReq = *num_patches;
753             }
754             if (numPatchesReq > 0) {
755                 reply.read(patches, numPatchesReq * sizeof(struct audio_patch));
756             }
757             *generation = reply.readInt32();
758         }
759         return status;
760     }
761 
setAudioPortConfig(const struct audio_port_config * config)762     virtual status_t setAudioPortConfig(const struct audio_port_config *config)
763     {
764         if (config == NULL) {
765             return BAD_VALUE;
766         }
767         Parcel data, reply;
768         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
769         data.write(config, sizeof(struct audio_port_config));
770         status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply);
771         if (status != NO_ERROR) {
772             status = (status_t)reply.readInt32();
773         }
774         return status;
775     }
776 
registerClient(const sp<IAudioPolicyServiceClient> & client)777     virtual void registerClient(const sp<IAudioPolicyServiceClient>& client)
778     {
779         Parcel data, reply;
780         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
781         data.writeStrongBinder(IInterface::asBinder(client));
782         remote()->transact(REGISTER_CLIENT, data, &reply);
783     }
784 
setAudioPortCallbacksEnabled(bool enabled)785     virtual void setAudioPortCallbacksEnabled(bool enabled)
786     {
787         Parcel data, reply;
788         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
789         data.writeInt32(enabled ? 1 : 0);
790         remote()->transact(SET_AUDIO_PORT_CALLBACK_ENABLED, data, &reply);
791     }
792 
setAudioVolumeGroupCallbacksEnabled(bool enabled)793     virtual void setAudioVolumeGroupCallbacksEnabled(bool enabled)
794     {
795         Parcel data, reply;
796         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
797         data.writeInt32(enabled ? 1 : 0);
798         remote()->transact(SET_AUDIO_VOLUME_GROUP_CALLBACK_ENABLED, data, &reply);
799     }
800 
acquireSoundTriggerSession(audio_session_t * session,audio_io_handle_t * ioHandle,audio_devices_t * device)801     virtual status_t acquireSoundTriggerSession(audio_session_t *session,
802                                             audio_io_handle_t *ioHandle,
803                                             audio_devices_t *device)
804     {
805         if (session == NULL || ioHandle == NULL || device == NULL) {
806             return BAD_VALUE;
807         }
808         Parcel data, reply;
809         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
810         status_t status = remote()->transact(ACQUIRE_SOUNDTRIGGER_SESSION, data, &reply);
811         if (status != NO_ERROR) {
812             return status;
813         }
814         status = (status_t)reply.readInt32();
815         if (status == NO_ERROR) {
816             *session = (audio_session_t)reply.readInt32();
817             *ioHandle = (audio_io_handle_t)reply.readInt32();
818             *device = (audio_devices_t)reply.readInt32();
819         }
820         return status;
821     }
822 
releaseSoundTriggerSession(audio_session_t session)823     virtual status_t releaseSoundTriggerSession(audio_session_t session)
824     {
825         Parcel data, reply;
826         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
827         data.writeInt32(session);
828         status_t status = remote()->transact(RELEASE_SOUNDTRIGGER_SESSION, data, &reply);
829         if (status != NO_ERROR) {
830             return status;
831         }
832         return (status_t)reply.readInt32();
833     }
834 
getPhoneState()835     virtual audio_mode_t getPhoneState()
836     {
837         Parcel data, reply;
838         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
839         status_t status = remote()->transact(GET_PHONE_STATE, data, &reply);
840         if (status != NO_ERROR) {
841             return AUDIO_MODE_INVALID;
842         }
843         return (audio_mode_t)reply.readInt32();
844     }
845 
registerPolicyMixes(const Vector<AudioMix> & mixes,bool registration)846     virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
847     {
848         Parcel data, reply;
849         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
850         data.writeInt32(registration ? 1 : 0);
851         size_t size = mixes.size();
852         if (size > MAX_MIXES_PER_POLICY) {
853             size = MAX_MIXES_PER_POLICY;
854         }
855         size_t sizePosition = data.dataPosition();
856         data.writeInt32(size);
857         size_t finalSize = size;
858         for (size_t i = 0; i < size; i++) {
859             size_t position = data.dataPosition();
860             if (mixes[i].writeToParcel(&data) != NO_ERROR) {
861                 data.setDataPosition(position);
862                 finalSize--;
863             }
864         }
865         if (size != finalSize) {
866             size_t position = data.dataPosition();
867             data.setDataPosition(sizePosition);
868             data.writeInt32(finalSize);
869             data.setDataPosition(position);
870         }
871         status_t status = remote()->transact(REGISTER_POLICY_MIXES, data, &reply);
872         if (status == NO_ERROR) {
873             status = (status_t)reply.readInt32();
874         }
875         return status;
876     }
877 
startAudioSource(const struct audio_port_config * source,const audio_attributes_t * attributes,audio_port_handle_t * portId)878     virtual status_t startAudioSource(const struct audio_port_config *source,
879                                       const audio_attributes_t *attributes,
880                                       audio_port_handle_t *portId)
881     {
882         Parcel data, reply;
883         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
884         if (source == NULL || attributes == NULL || portId == NULL) {
885             return BAD_VALUE;
886         }
887         data.write(source, sizeof(struct audio_port_config));
888         data.write(attributes, sizeof(audio_attributes_t));
889         status_t status = remote()->transact(START_AUDIO_SOURCE, data, &reply);
890         if (status != NO_ERROR) {
891             return status;
892         }
893         status = (status_t)reply.readInt32();
894         if (status != NO_ERROR) {
895             return status;
896         }
897         *portId = (audio_port_handle_t)reply.readInt32();
898         return status;
899     }
900 
stopAudioSource(audio_port_handle_t portId)901     virtual status_t stopAudioSource(audio_port_handle_t portId)
902     {
903         Parcel data, reply;
904         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
905         data.writeInt32(portId);
906         status_t status = remote()->transact(STOP_AUDIO_SOURCE, data, &reply);
907         if (status != NO_ERROR) {
908             return status;
909         }
910         status = (status_t)reply.readInt32();
911         return status;
912     }
913 
setMasterMono(bool mono)914     virtual status_t setMasterMono(bool mono)
915     {
916         Parcel data, reply;
917         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
918         data.writeInt32(static_cast<int32_t>(mono));
919         status_t status = remote()->transact(SET_MASTER_MONO, data, &reply);
920         if (status != NO_ERROR) {
921             return status;
922         }
923         return static_cast<status_t>(reply.readInt32());
924     }
925 
getMasterMono(bool * mono)926     virtual status_t getMasterMono(bool *mono)
927     {
928         if (mono == nullptr) {
929             return BAD_VALUE;
930         }
931         Parcel data, reply;
932         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
933 
934         status_t status = remote()->transact(GET_MASTER_MONO, data, &reply);
935         if (status != NO_ERROR) {
936             return status;
937         }
938         status = static_cast<status_t>(reply.readInt32());
939         if (status == NO_ERROR) {
940             *mono = static_cast<bool>(reply.readInt32());
941         }
942         return status;
943     }
944 
getStreamVolumeDB(audio_stream_type_t stream,int index,audio_devices_t device)945     virtual float getStreamVolumeDB(audio_stream_type_t stream, int index, audio_devices_t device)
946     {
947         Parcel data, reply;
948         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
949         data.writeInt32(static_cast <int32_t>(stream));
950         data.writeInt32(static_cast <int32_t>(index));
951         data.writeUint32(static_cast <uint32_t>(device));
952         status_t status = remote()->transact(GET_STREAM_VOLUME_DB, data, &reply);
953         if (status != NO_ERROR) {
954             return NAN;
955         }
956         return reply.readFloat();
957     }
958 
getSurroundFormats(unsigned int * numSurroundFormats,audio_format_t * surroundFormats,bool * surroundFormatsEnabled,bool reported)959     virtual status_t getSurroundFormats(unsigned int *numSurroundFormats,
960                                         audio_format_t *surroundFormats,
961                                         bool *surroundFormatsEnabled,
962                                         bool reported)
963     {
964         if (numSurroundFormats == NULL || (*numSurroundFormats != 0 &&
965                 (surroundFormats == NULL || surroundFormatsEnabled == NULL))) {
966             return BAD_VALUE;
967         }
968         Parcel data, reply;
969         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
970         unsigned int numSurroundFormatsReq = *numSurroundFormats;
971         data.writeUint32(numSurroundFormatsReq);
972         data.writeBool(reported);
973         status_t status = remote()->transact(GET_SURROUND_FORMATS, data, &reply);
974         if (status == NO_ERROR && (status = (status_t)reply.readInt32()) == NO_ERROR) {
975             *numSurroundFormats = reply.readUint32();
976         }
977         if (status == NO_ERROR) {
978             if (numSurroundFormatsReq > *numSurroundFormats) {
979                 numSurroundFormatsReq = *numSurroundFormats;
980             }
981             if (numSurroundFormatsReq > 0) {
982                 status = reply.read(surroundFormats,
983                                     numSurroundFormatsReq * sizeof(audio_format_t));
984                 if (status != NO_ERROR) {
985                     return status;
986                 }
987                 status = reply.read(surroundFormatsEnabled,
988                                     numSurroundFormatsReq * sizeof(bool));
989             }
990         }
991         return status;
992     }
993 
setSurroundFormatEnabled(audio_format_t audioFormat,bool enabled)994     virtual status_t setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled)
995     {
996         Parcel data, reply;
997         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
998         data.writeInt32(audioFormat);
999         data.writeBool(enabled);
1000         status_t status = remote()->transact(SET_SURROUND_FORMAT_ENABLED, data, &reply);
1001         if (status != NO_ERROR) {
1002             return status;
1003         }
1004         return reply.readInt32();
1005     }
1006 
getHwOffloadEncodingFormatsSupportedForA2DP(std::vector<audio_format_t> * formats)1007     virtual status_t getHwOffloadEncodingFormatsSupportedForA2DP(
1008                 std::vector<audio_format_t> *formats)
1009     {
1010         if (formats == NULL) {
1011             return BAD_VALUE;
1012         }
1013 
1014         Parcel data, reply;
1015         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1016         status_t status = remote()->transact(GET_OFFLOAD_FORMATS_A2DP, data, &reply);
1017         if (status != NO_ERROR || (status = (status_t)reply.readInt32()) != NO_ERROR) {
1018             return status;
1019         }
1020 
1021         size_t list_size = reply.readUint32();
1022 
1023         for (size_t i = 0; i < list_size; i++) {
1024             formats->push_back(static_cast<audio_format_t>(reply.readInt32()));
1025         }
1026         return NO_ERROR;
1027     }
1028 
1029 
addStreamDefaultEffect(const effect_uuid_t * type,const String16 & opPackageName,const effect_uuid_t * uuid,int32_t priority,audio_usage_t usage,audio_unique_id_t * id)1030      virtual status_t addStreamDefaultEffect(const effect_uuid_t *type,
1031                                             const String16& opPackageName,
1032                                             const effect_uuid_t *uuid,
1033                                             int32_t priority,
1034                                             audio_usage_t usage,
1035                                             audio_unique_id_t* id)
1036     {
1037         Parcel data, reply;
1038         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1039         data.write(type, sizeof(effect_uuid_t));
1040         data.writeString16(opPackageName);
1041         data.write(uuid, sizeof(effect_uuid_t));
1042         data.writeInt32(priority);
1043         data.writeInt32((int32_t) usage);
1044         status_t status = remote()->transact(ADD_STREAM_DEFAULT_EFFECT, data, &reply);
1045         if (status != NO_ERROR) {
1046             return status;
1047         }
1048         status = static_cast <status_t> (reply.readInt32());
1049         *id = reply.readInt32();
1050         return status;
1051     }
1052 
removeStreamDefaultEffect(audio_unique_id_t id)1053     virtual status_t removeStreamDefaultEffect(audio_unique_id_t id)
1054     {
1055         Parcel data, reply;
1056         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1057         data.writeInt32(id);
1058         status_t status = remote()->transact(REMOVE_STREAM_DEFAULT_EFFECT, data, &reply);
1059         if (status != NO_ERROR) {
1060             return status;
1061         }
1062         return static_cast <status_t> (reply.readInt32());
1063     }
1064 
addSourceDefaultEffect(const effect_uuid_t * type,const String16 & opPackageName,const effect_uuid_t * uuid,int32_t priority,audio_source_t source,audio_unique_id_t * id)1065     virtual status_t addSourceDefaultEffect(const effect_uuid_t *type,
1066                                             const String16& opPackageName,
1067                                             const effect_uuid_t *uuid,
1068                                             int32_t priority,
1069                                             audio_source_t source,
1070                                             audio_unique_id_t* id)
1071     {
1072         Parcel data, reply;
1073         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1074         data.write(type, sizeof(effect_uuid_t));
1075         data.writeString16(opPackageName);
1076         data.write(uuid, sizeof(effect_uuid_t));
1077         data.writeInt32(priority);
1078         data.writeInt32((int32_t) source);
1079         status_t status = remote()->transact(ADD_SOURCE_DEFAULT_EFFECT, data, &reply);
1080         if (status != NO_ERROR) {
1081             return status;
1082         }
1083         status = static_cast <status_t> (reply.readInt32());
1084         *id = reply.readInt32();
1085         return status;
1086     }
1087 
removeSourceDefaultEffect(audio_unique_id_t id)1088     virtual status_t removeSourceDefaultEffect(audio_unique_id_t id)
1089     {
1090         Parcel data, reply;
1091         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1092         data.writeInt32(id);
1093         status_t status = remote()->transact(REMOVE_SOURCE_DEFAULT_EFFECT, data, &reply);
1094         if (status != NO_ERROR) {
1095             return status;
1096         }
1097         return static_cast <status_t> (reply.readInt32());
1098     }
1099 
setAssistantUid(uid_t uid)1100     virtual status_t setAssistantUid(uid_t uid)
1101     {
1102         Parcel data, reply;
1103         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1104         data.writeInt32(uid);
1105         status_t status = remote()->transact(SET_ASSISTANT_UID, data, &reply);
1106         if (status != NO_ERROR) {
1107             return status;
1108         }
1109         return static_cast <status_t> (reply.readInt32());
1110     }
1111 
setA11yServicesUids(const std::vector<uid_t> & uids)1112     virtual status_t setA11yServicesUids(const std::vector<uid_t>& uids)
1113     {
1114         Parcel data, reply;
1115         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1116         data.writeInt32(uids.size());
1117         for (auto uid : uids) {
1118             data.writeInt32(uid);
1119         }
1120         status_t status = remote()->transact(SET_A11Y_SERVICES_UIDS, data, &reply);
1121         if (status != NO_ERROR) {
1122             return status;
1123         }
1124         return static_cast <status_t> (reply.readInt32());
1125     }
1126 
isHapticPlaybackSupported()1127     virtual bool isHapticPlaybackSupported()
1128     {
1129         Parcel data, reply;
1130         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1131         status_t status = remote()->transact(IS_HAPTIC_PLAYBACK_SUPPORTED, data, &reply);
1132         if (status != NO_ERROR) {
1133             return false;
1134         }
1135         return reply.readBool();
1136     }
1137 
setUidDeviceAffinities(uid_t uid,const Vector<AudioDeviceTypeAddr> & devices)1138     virtual status_t setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices)
1139     {
1140         Parcel data, reply;
1141         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1142 
1143         data.writeInt32((int32_t) uid);
1144         size_t size = devices.size();
1145         size_t sizePosition = data.dataPosition();
1146         data.writeInt32((int32_t) size);
1147         size_t finalSize = size;
1148         for (size_t i = 0; i < size; i++) {
1149             size_t position = data.dataPosition();
1150             if (devices[i].writeToParcel(&data) != NO_ERROR) {
1151                 data.setDataPosition(position);
1152                 finalSize--;
1153             }
1154         }
1155         if (size != finalSize) {
1156             size_t position = data.dataPosition();
1157             data.setDataPosition(sizePosition);
1158             data.writeInt32(finalSize);
1159             data.setDataPosition(position);
1160         }
1161 
1162         status_t status = remote()->transact(SET_UID_DEVICE_AFFINITY, data, &reply);
1163         if (status == NO_ERROR) {
1164             status = (status_t)reply.readInt32();
1165         }
1166         return status;
1167     }
1168 
removeUidDeviceAffinities(uid_t uid)1169     virtual status_t removeUidDeviceAffinities(uid_t uid) {
1170         Parcel data, reply;
1171         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1172 
1173         data.writeInt32((int32_t) uid);
1174 
1175         status_t status =
1176             remote()->transact(REMOVE_UID_DEVICE_AFFINITY, data, &reply);
1177         if (status == NO_ERROR) {
1178             status = (status_t) reply.readInt32();
1179         }
1180         return status;
1181     }
1182 
listAudioProductStrategies(AudioProductStrategyVector & strategies)1183     virtual status_t listAudioProductStrategies(AudioProductStrategyVector &strategies)
1184     {
1185         Parcel data, reply;
1186         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1187 
1188         status_t status = remote()->transact(LIST_AUDIO_PRODUCT_STRATEGIES, data, &reply);
1189         if (status != NO_ERROR) {
1190             ALOGE("%s: permission denied", __func__);
1191             return status;
1192         }
1193         status = static_cast<status_t>(reply.readInt32());
1194         if (status != NO_ERROR) {
1195             return status;
1196         }
1197         uint32_t numStrategies = static_cast<uint32_t>(reply.readInt32());
1198         for (size_t i = 0; i < numStrategies; i++) {
1199             AudioProductStrategy strategy;
1200             status = strategy.readFromParcel(&reply);
1201             if (status != NO_ERROR) {
1202                 ALOGE("%s: failed to read strategies", __FUNCTION__);
1203                 strategies.clear();
1204                 return status;
1205             }
1206             strategies.push_back(strategy);
1207         }
1208         return NO_ERROR;
1209     }
1210 
getProductStrategyFromAudioAttributes(const AudioAttributes & aa,product_strategy_t & productStrategy)1211     virtual status_t getProductStrategyFromAudioAttributes(const AudioAttributes &aa,
1212                                                            product_strategy_t &productStrategy)
1213     {
1214         Parcel data, reply;
1215         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1216         status_t status = aa.writeToParcel(&data);
1217         if (status != NO_ERROR) {
1218             return status;
1219         }
1220         status = remote()->transact(GET_STRATEGY_FOR_ATTRIBUTES, data, &reply);
1221         if (status != NO_ERROR) {
1222             return status;
1223         }
1224         status = static_cast<status_t>(reply.readInt32());
1225         if (status != NO_ERROR) {
1226             return status;
1227         }
1228         productStrategy = static_cast<product_strategy_t>(reply.readInt32());
1229         return NO_ERROR;
1230     }
1231 
listAudioVolumeGroups(AudioVolumeGroupVector & groups)1232     virtual status_t listAudioVolumeGroups(AudioVolumeGroupVector &groups)
1233     {
1234         Parcel data, reply;
1235         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1236 
1237         status_t status = remote()->transact(LIST_AUDIO_VOLUME_GROUPS, data, &reply);
1238         if (status != NO_ERROR) {
1239             return status;
1240         }
1241         status = static_cast<status_t>(reply.readInt32());
1242         if (status != NO_ERROR) {
1243             return status;
1244         }
1245         uint32_t numGroups = static_cast<uint32_t>(reply.readInt32());
1246         for (size_t i = 0; i < numGroups; i++) {
1247             AudioVolumeGroup group;
1248             status = group.readFromParcel(&reply);
1249             if (status != NO_ERROR) {
1250                 ALOGE("%s: failed to read volume groups", __FUNCTION__);
1251                 groups.clear();
1252                 return status;
1253             }
1254             groups.push_back(group);
1255         }
1256         return NO_ERROR;
1257     }
1258 
getVolumeGroupFromAudioAttributes(const AudioAttributes & aa,volume_group_t & volumeGroup)1259     virtual status_t getVolumeGroupFromAudioAttributes(const AudioAttributes &aa,
1260                                                        volume_group_t &volumeGroup)
1261     {
1262         Parcel data, reply;
1263         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1264         status_t status = aa.writeToParcel(&data);
1265         if (status != NO_ERROR) {
1266             return status;
1267         }
1268         status = remote()->transact(GET_VOLUME_GROUP_FOR_ATTRIBUTES, data, &reply);
1269         if (status != NO_ERROR) {
1270             return status;
1271         }
1272         status = static_cast<status_t>(reply.readInt32());
1273         if (status != NO_ERROR) {
1274             return status;
1275         }
1276         volumeGroup = static_cast<volume_group_t>(reply.readInt32());
1277         return NO_ERROR;
1278     }
1279 
setRttEnabled(bool enabled)1280     virtual status_t setRttEnabled(bool enabled)
1281     {
1282         Parcel data, reply;
1283         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1284         data.writeInt32(static_cast<int32_t>(enabled));
1285         status_t status = remote()->transact(SET_RTT_ENABLED, data, &reply);
1286         if (status != NO_ERROR) {
1287            return status;
1288         }
1289         return static_cast<status_t>(reply.readInt32());
1290     }
1291 
setPreferredDeviceForStrategy(product_strategy_t strategy,const AudioDeviceTypeAddr & device)1292     virtual status_t setPreferredDeviceForStrategy(product_strategy_t strategy,
1293             const AudioDeviceTypeAddr &device)
1294     {
1295         Parcel data, reply;
1296         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1297         data.writeUint32(static_cast<uint32_t>(strategy));
1298         status_t status = device.writeToParcel(&data);
1299         if (status != NO_ERROR) {
1300             return BAD_VALUE;
1301         }
1302         status = remote()->transact(SET_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY,
1303                 data, &reply);
1304         if (status != NO_ERROR) {
1305            return status;
1306         }
1307         return static_cast<status_t>(reply.readInt32());
1308     }
1309 
removePreferredDeviceForStrategy(product_strategy_t strategy)1310     virtual status_t removePreferredDeviceForStrategy(product_strategy_t strategy)
1311     {
1312         Parcel data, reply;
1313         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1314         data.writeUint32(static_cast<uint32_t>(strategy));
1315         status_t status = remote()->transact(REMOVE_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY,
1316                 data, &reply);
1317         if (status != NO_ERROR) {
1318            return status;
1319         }
1320         return static_cast<status_t>(reply.readInt32());
1321     }
1322 
getPreferredDeviceForStrategy(product_strategy_t strategy,AudioDeviceTypeAddr & device)1323     virtual status_t getPreferredDeviceForStrategy(product_strategy_t strategy,
1324             AudioDeviceTypeAddr &device)
1325     {
1326         Parcel data, reply;
1327         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1328         data.writeUint32(static_cast<uint32_t>(strategy));
1329         status_t status = remote()->transact(GET_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY,
1330                 data, &reply);
1331         if (status != NO_ERROR) {
1332             return status;
1333         }
1334         status = device.readFromParcel(&reply);
1335         if (status != NO_ERROR) {
1336             return status;
1337         }
1338         return static_cast<status_t>(reply.readInt32());
1339     }
1340 
onNewAudioModulesAvailable()1341     virtual void onNewAudioModulesAvailable()
1342     {
1343         Parcel data, reply;
1344         data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
1345         remote()->transact(AUDIO_MODULES_UPDATED, data, &reply, IBinder::FLAG_ONEWAY);
1346     }
1347 };
1348 
1349 IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
1350 
1351 // ----------------------------------------------------------------------
1352 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)1353 status_t BnAudioPolicyService::onTransact(
1354     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
1355 {
1356     // make sure transactions reserved to AudioFlinger do not come from other processes
1357     switch (code) {
1358         case START_OUTPUT:
1359         case STOP_OUTPUT:
1360         case RELEASE_OUTPUT:
1361         case GET_INPUT_FOR_ATTR:
1362         case START_INPUT:
1363         case STOP_INPUT:
1364         case RELEASE_INPUT:
1365         case GET_OUTPUT_FOR_EFFECT:
1366         case REGISTER_EFFECT:
1367         case UNREGISTER_EFFECT:
1368         case SET_EFFECT_ENABLED:
1369         case GET_OUTPUT_FOR_ATTR:
1370         case ACQUIRE_SOUNDTRIGGER_SESSION:
1371         case RELEASE_SOUNDTRIGGER_SESSION:
1372         case MOVE_EFFECTS_TO_IO:
1373             ALOGW("%s: transaction %d received from PID %d",
1374                   __func__, code, IPCThreadState::self()->getCallingPid());
1375             // return status only for non void methods
1376             switch (code) {
1377                 case RELEASE_OUTPUT:
1378                 case RELEASE_INPUT:
1379                     break;
1380                 default:
1381                     reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
1382                     break;
1383             }
1384             return OK;
1385         default:
1386             break;
1387     }
1388 
1389     // make sure the following transactions come from system components
1390     switch (code) {
1391         case SET_DEVICE_CONNECTION_STATE:
1392         case HANDLE_DEVICE_CONFIG_CHANGE:
1393         case SET_PHONE_STATE:
1394 //FIXME: Allow SET_FORCE_USE calls from system apps until a better use case routing API is available
1395 //      case SET_FORCE_USE:
1396         case INIT_STREAM_VOLUME:
1397         case SET_STREAM_VOLUME:
1398         case REGISTER_POLICY_MIXES:
1399         case SET_MASTER_MONO:
1400         case GET_SURROUND_FORMATS:
1401         case SET_SURROUND_FORMAT_ENABLED:
1402         case SET_ASSISTANT_UID:
1403         case SET_A11Y_SERVICES_UIDS:
1404         case SET_UID_DEVICE_AFFINITY:
1405         case REMOVE_UID_DEVICE_AFFINITY:
1406         case GET_OFFLOAD_FORMATS_A2DP:
1407         case LIST_AUDIO_VOLUME_GROUPS:
1408         case GET_VOLUME_GROUP_FOR_ATTRIBUTES:
1409         case SET_RTT_ENABLED:
1410         case SET_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY:
1411         case REMOVE_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY:
1412         case GET_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY:
1413         case SET_ALLOWED_CAPTURE_POLICY:
1414         case AUDIO_MODULES_UPDATED: {
1415             if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
1416                 ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
1417                       __func__, code, IPCThreadState::self()->getCallingPid(),
1418                       IPCThreadState::self()->getCallingUid());
1419                 reply->writeInt32(static_cast<int32_t> (INVALID_OPERATION));
1420                 return OK;
1421             }
1422         } break;
1423         default:
1424             break;
1425     }
1426 
1427     std::string tag("IAudioPolicyService command " + std::to_string(code));
1428     TimeCheck check(tag.c_str());
1429 
1430     switch (code) {
1431         case SET_DEVICE_CONNECTION_STATE: {
1432             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1433             audio_devices_t device =
1434                     static_cast <audio_devices_t>(data.readInt32());
1435             audio_policy_dev_state_t state =
1436                     static_cast <audio_policy_dev_state_t>(data.readInt32());
1437             const char *device_address = data.readCString();
1438             const char *device_name = data.readCString();
1439             audio_format_t codecFormat = static_cast <audio_format_t>(data.readInt32());
1440             if (device_address == nullptr || device_name == nullptr) {
1441                 ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
1442                 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1443             } else {
1444                 reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
1445                                                                                   state,
1446                                                                                   device_address,
1447                                                                                   device_name,
1448                                                                                   codecFormat)));
1449             }
1450             return NO_ERROR;
1451         } break;
1452 
1453         case GET_DEVICE_CONNECTION_STATE: {
1454             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1455             audio_devices_t device =
1456                     static_cast<audio_devices_t> (data.readInt32());
1457             const char *device_address = data.readCString();
1458             if (device_address == nullptr) {
1459                 ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
1460                 reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
1461             } else {
1462                 reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
1463                                                                                   device_address)));
1464             }
1465             return NO_ERROR;
1466         } break;
1467 
1468         case HANDLE_DEVICE_CONFIG_CHANGE: {
1469             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1470             audio_devices_t device =
1471                     static_cast <audio_devices_t>(data.readInt32());
1472             const char *device_address = data.readCString();
1473             const char *device_name = data.readCString();
1474             audio_format_t codecFormat =
1475                     static_cast <audio_format_t>(data.readInt32());
1476             if (device_address == nullptr || device_name == nullptr) {
1477                 ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
1478                 reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
1479             } else {
1480                 reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
1481                                                                                   device_address,
1482                                                                                   device_name,
1483                                                                                   codecFormat)));
1484             }
1485             return NO_ERROR;
1486         } break;
1487 
1488         case SET_PHONE_STATE: {
1489             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1490             reply->writeInt32(static_cast <uint32_t>(setPhoneState(
1491                     (audio_mode_t) data.readInt32())));
1492             return NO_ERROR;
1493         } break;
1494 
1495         case SET_FORCE_USE: {
1496             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1497             audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1498                     data.readInt32());
1499             audio_policy_forced_cfg_t config =
1500                     static_cast <audio_policy_forced_cfg_t>(data.readInt32());
1501             reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
1502             return NO_ERROR;
1503         } break;
1504 
1505         case GET_FORCE_USE: {
1506             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1507             audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
1508                     data.readInt32());
1509             reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
1510             return NO_ERROR;
1511         } break;
1512 
1513         case GET_OUTPUT: {
1514             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1515             audio_stream_type_t stream =
1516                     static_cast <audio_stream_type_t>(data.readInt32());
1517             audio_io_handle_t output = getOutput(stream);
1518             reply->writeInt32(static_cast <int>(output));
1519             return NO_ERROR;
1520         } break;
1521 
1522         case GET_OUTPUT_FOR_ATTR: {
1523             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1524             audio_attributes_t attr = AUDIO_ATTRIBUTES_INITIALIZER;
1525             status_t status = data.read(&attr, sizeof(audio_attributes_t));
1526             if (status != NO_ERROR) {
1527                 return status;
1528             }
1529             sanetizeAudioAttributes(&attr);
1530             audio_session_t session = (audio_session_t)data.readInt32();
1531             audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
1532             bool hasStream = data.readInt32() != 0;
1533             if (hasStream) {
1534                 stream = (audio_stream_type_t)data.readInt32();
1535             }
1536             pid_t pid = (pid_t)data.readInt32();
1537             uid_t uid = (uid_t)data.readInt32();
1538             audio_config_t config;
1539             memset(&config, 0, sizeof(audio_config_t));
1540             data.read(&config, sizeof(audio_config_t));
1541             audio_output_flags_t flags =
1542                     static_cast <audio_output_flags_t>(data.readInt32());
1543             audio_port_handle_t selectedDeviceId = data.readInt32();
1544             audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
1545             audio_io_handle_t output = 0;
1546             std::vector<audio_io_handle_t> secondaryOutputs;
1547             status = getOutputForAttr(&attr,
1548                     &output, session, &stream, pid, uid,
1549                     &config,
1550                     flags, &selectedDeviceId, &portId, &secondaryOutputs);
1551             reply->writeInt32(status);
1552             status = reply->write(&attr, sizeof(audio_attributes_t));
1553             if (status != NO_ERROR) {
1554                 return status;
1555             }
1556             reply->writeInt32(output);
1557             reply->writeInt32(stream);
1558             reply->writeInt32(selectedDeviceId);
1559             reply->writeInt32(portId);
1560             reply->writeInt32(secondaryOutputs.size());
1561             return reply->write(secondaryOutputs.data(),
1562                                 secondaryOutputs.size() * sizeof(audio_io_handle_t));
1563         } break;
1564 
1565         case START_OUTPUT: {
1566             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1567             const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1568             reply->writeInt32(static_cast <uint32_t>(startOutput(portId)));
1569             return NO_ERROR;
1570         } break;
1571 
1572         case STOP_OUTPUT: {
1573             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1574             const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1575             reply->writeInt32(static_cast <uint32_t>(stopOutput(portId)));
1576             return NO_ERROR;
1577         } break;
1578 
1579         case RELEASE_OUTPUT: {
1580             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1581             const audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1582             releaseOutput(portId);
1583             return NO_ERROR;
1584         } break;
1585 
1586         case GET_INPUT_FOR_ATTR: {
1587             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1588             audio_attributes_t attr = {};
1589             data.read(&attr, sizeof(audio_attributes_t));
1590             sanetizeAudioAttributes(&attr);
1591             audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
1592             audio_unique_id_t riid = (audio_unique_id_t)data.readInt32();
1593             audio_session_t session = (audio_session_t)data.readInt32();
1594             pid_t pid = (pid_t)data.readInt32();
1595             uid_t uid = (uid_t)data.readInt32();
1596             const String16 opPackageName = data.readString16();
1597             audio_config_base_t config;
1598             memset(&config, 0, sizeof(audio_config_base_t));
1599             data.read(&config, sizeof(audio_config_base_t));
1600             audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
1601             audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
1602             audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
1603             status_t status = getInputForAttr(&attr, &input, riid, session, pid, uid,
1604                                               opPackageName, &config,
1605                                               flags, &selectedDeviceId, &portId);
1606             reply->writeInt32(status);
1607             if (status == NO_ERROR) {
1608                 reply->writeInt32(input);
1609                 reply->writeInt32(selectedDeviceId);
1610                 reply->writeInt32(portId);
1611             }
1612             return NO_ERROR;
1613         } break;
1614 
1615         case START_INPUT: {
1616             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1617             audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1618             status_t status = startInput(portId);
1619             reply->writeInt32(static_cast <uint32_t>(status));
1620             return NO_ERROR;
1621         } break;
1622 
1623         case STOP_INPUT: {
1624             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1625             audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1626             reply->writeInt32(static_cast <uint32_t>(stopInput(portId)));
1627             return NO_ERROR;
1628         } break;
1629 
1630         case RELEASE_INPUT: {
1631             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1632             audio_port_handle_t portId = static_cast <audio_port_handle_t>(data.readInt32());
1633             releaseInput(portId);
1634             return NO_ERROR;
1635         } break;
1636 
1637         case INIT_STREAM_VOLUME: {
1638             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1639             audio_stream_type_t stream =
1640                     static_cast <audio_stream_type_t>(data.readInt32());
1641             int indexMin = data.readInt32();
1642             int indexMax = data.readInt32();
1643             reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1644             return NO_ERROR;
1645         } break;
1646 
1647         case SET_STREAM_VOLUME: {
1648             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1649             audio_stream_type_t stream =
1650                     static_cast <audio_stream_type_t>(data.readInt32());
1651             int index = data.readInt32();
1652             audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1653             reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1654                                                                           index,
1655                                                                           device)));
1656             return NO_ERROR;
1657         } break;
1658 
1659         case GET_STREAM_VOLUME: {
1660             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1661             audio_stream_type_t stream =
1662                     static_cast <audio_stream_type_t>(data.readInt32());
1663             audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1664             int index = 0;
1665             status_t status = getStreamVolumeIndex(stream, &index, device);
1666             reply->writeInt32(index);
1667             reply->writeInt32(static_cast <uint32_t>(status));
1668             return NO_ERROR;
1669         } break;
1670 
1671         case GET_STRATEGY_FOR_STREAM: {
1672             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1673             audio_stream_type_t stream =
1674                     static_cast <audio_stream_type_t>(data.readInt32());
1675             reply->writeUint32(getStrategyForStream(stream));
1676             return NO_ERROR;
1677         } break;
1678 
1679         case SET_VOLUME_ATTRIBUTES: {
1680             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1681             audio_attributes_t attributes = {};
1682             status_t status = data.read(&attributes, sizeof(audio_attributes_t));
1683             if (status != NO_ERROR) {
1684                 return status;
1685             }
1686             int index = data.readInt32();
1687             audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1688 
1689             reply->writeInt32(static_cast <uint32_t>(setVolumeIndexForAttributes(attributes,
1690                                                                                  index, device)));
1691             return NO_ERROR;
1692         } break;
1693 
1694         case GET_VOLUME_ATTRIBUTES: {
1695             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1696             audio_attributes_t attributes = {};
1697             status_t status = data.read(&attributes, sizeof(audio_attributes_t));
1698             if (status != NO_ERROR) {
1699                 return status;
1700             }
1701             audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1702 
1703             int index = 0;
1704             status = getVolumeIndexForAttributes(attributes, index, device);
1705             reply->writeInt32(static_cast <uint32_t>(status));
1706             if (status == NO_ERROR) {
1707                 reply->writeInt32(index);
1708             }
1709             return NO_ERROR;
1710         } break;
1711 
1712         case GET_MIN_VOLUME_FOR_ATTRIBUTES: {
1713             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1714             audio_attributes_t attributes = {};
1715             status_t status = data.read(&attributes, sizeof(audio_attributes_t));
1716             if (status != NO_ERROR) {
1717                 return status;
1718             }
1719 
1720             int index = 0;
1721             status = getMinVolumeIndexForAttributes(attributes, index);
1722             reply->writeInt32(static_cast <uint32_t>(status));
1723             if (status == NO_ERROR) {
1724                 reply->writeInt32(index);
1725             }
1726             return NO_ERROR;
1727         } break;
1728 
1729         case GET_MAX_VOLUME_FOR_ATTRIBUTES: {
1730             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1731             audio_attributes_t attributes = {};
1732             status_t status = data.read(&attributes, sizeof(audio_attributes_t));
1733             if (status != NO_ERROR) {
1734                 return status;
1735             }
1736 
1737             int index = 0;
1738             status = getMaxVolumeIndexForAttributes(attributes, index);
1739             reply->writeInt32(static_cast <uint32_t>(status));
1740             if (status == NO_ERROR) {
1741                 reply->writeInt32(index);
1742             }
1743             return NO_ERROR;
1744         } break;
1745 
1746         case GET_DEVICES_FOR_STREAM: {
1747             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1748             audio_stream_type_t stream =
1749                     static_cast <audio_stream_type_t>(data.readInt32());
1750             reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1751             return NO_ERROR;
1752         } break;
1753 
1754         case GET_OUTPUT_FOR_EFFECT: {
1755             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1756             effect_descriptor_t desc = {};
1757             if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1758                 android_errorWriteLog(0x534e4554, "73126106");
1759             }
1760             (void)sanitizeEffectDescriptor(&desc);
1761             audio_io_handle_t output = getOutputForEffect(&desc);
1762             reply->writeInt32(static_cast <int>(output));
1763             return NO_ERROR;
1764         } break;
1765 
1766         case REGISTER_EFFECT: {
1767             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1768             effect_descriptor_t desc = {};
1769             if (data.read(&desc, sizeof(desc)) != NO_ERROR) {
1770                 android_errorWriteLog(0x534e4554, "73126106");
1771             }
1772             (void)sanitizeEffectDescriptor(&desc);
1773             audio_io_handle_t io = data.readInt32();
1774             uint32_t strategy = data.readInt32();
1775             audio_session_t session = (audio_session_t) data.readInt32();
1776             int id = data.readInt32();
1777             reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
1778                                                                    io,
1779                                                                    strategy,
1780                                                                    session,
1781                                                                    id)));
1782             return NO_ERROR;
1783         } break;
1784 
1785         case UNREGISTER_EFFECT: {
1786             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1787             int id = data.readInt32();
1788             reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1789             return NO_ERROR;
1790         } break;
1791 
1792         case SET_EFFECT_ENABLED: {
1793             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1794             int id = data.readInt32();
1795             bool enabled = static_cast <bool>(data.readInt32());
1796             reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1797             return NO_ERROR;
1798         } break;
1799 
1800         case MOVE_EFFECTS_TO_IO: {
1801             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1802             std::vector<int> ids;
1803             int32_t size;
1804             status_t status = data.readInt32(&size);
1805             if (status != NO_ERROR) {
1806                 return status;
1807             }
1808             if (size > MAX_ITEMS_PER_LIST) {
1809                 return BAD_VALUE;
1810             }
1811             for (int32_t i = 0; i < size; i++) {
1812                 int id;
1813                 status =  data.readInt32(&id);
1814                 if (status != NO_ERROR) {
1815                     return status;
1816                 }
1817                 ids.push_back(id);
1818             }
1819 
1820             audio_io_handle_t io = data.readInt32();
1821             reply->writeInt32(static_cast <int32_t>(moveEffectsToIo(ids, io)));
1822             return NO_ERROR;
1823         } break;
1824 
1825         case IS_STREAM_ACTIVE: {
1826             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1827             audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1828             uint32_t inPastMs = (uint32_t)data.readInt32();
1829             reply->writeInt32( isStreamActive(stream, inPastMs) );
1830             return NO_ERROR;
1831         } break;
1832 
1833         case IS_STREAM_ACTIVE_REMOTELY: {
1834             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1835             audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1836             uint32_t inPastMs = (uint32_t)data.readInt32();
1837             reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
1838             return NO_ERROR;
1839         } break;
1840 
1841         case IS_SOURCE_ACTIVE: {
1842             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1843             audio_source_t source = (audio_source_t) data.readInt32();
1844             reply->writeInt32( isSourceActive(source));
1845             return NO_ERROR;
1846         }
1847 
1848         case QUERY_DEFAULT_PRE_PROCESSING: {
1849             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1850             audio_session_t audioSession = (audio_session_t) data.readInt32();
1851             uint32_t count = data.readInt32();
1852             if (count > AudioEffect::kMaxPreProcessing) {
1853                 count = AudioEffect::kMaxPreProcessing;
1854             }
1855             uint32_t retCount = count;
1856             effect_descriptor_t *descriptors = new effect_descriptor_t[count]{};
1857             status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1858             reply->writeInt32(status);
1859             if (status != NO_ERROR && status != NO_MEMORY) {
1860                 retCount = 0;
1861             }
1862             reply->writeInt32(retCount);
1863             if (retCount != 0) {
1864                 if (retCount < count) {
1865                     count = retCount;
1866                 }
1867                 reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1868             }
1869             delete[] descriptors;
1870             return status;
1871         }
1872 
1873         case IS_OFFLOAD_SUPPORTED: {
1874             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1875             audio_offload_info_t info = {};
1876             data.read(&info, sizeof(audio_offload_info_t));
1877             bool isSupported = isOffloadSupported(info);
1878             reply->writeInt32(isSupported);
1879             return NO_ERROR;
1880         }
1881 
1882         case IS_DIRECT_OUTPUT_SUPPORTED: {
1883             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1884             audio_config_base_t config = {};
1885             audio_attributes_t attributes = {};
1886             status_t status = data.read(&config, sizeof(audio_config_base_t));
1887             if (status != NO_ERROR) return status;
1888             status = data.read(&attributes, sizeof(audio_attributes_t));
1889             if (status != NO_ERROR) return status;
1890             reply->writeInt32(isDirectOutputSupported(config, attributes));
1891             return NO_ERROR;
1892         }
1893 
1894         case LIST_AUDIO_PORTS: {
1895             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1896             audio_port_role_t role = (audio_port_role_t)data.readInt32();
1897             audio_port_type_t type = (audio_port_type_t)data.readInt32();
1898             unsigned int numPortsReq = data.readInt32();
1899             if (numPortsReq > MAX_ITEMS_PER_LIST) {
1900                 numPortsReq = MAX_ITEMS_PER_LIST;
1901             }
1902             unsigned int numPorts = numPortsReq;
1903             struct audio_port *ports =
1904                     (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
1905             if (ports == NULL) {
1906                 reply->writeInt32(NO_MEMORY);
1907                 reply->writeInt32(0);
1908                 return NO_ERROR;
1909             }
1910             unsigned int generation;
1911             status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1912             reply->writeInt32(status);
1913             reply->writeInt32(numPorts);
1914 
1915             if (status == NO_ERROR) {
1916                 if (numPortsReq > numPorts) {
1917                     numPortsReq = numPorts;
1918                 }
1919                 reply->write(ports, numPortsReq * sizeof(struct audio_port));
1920                 reply->writeInt32(generation);
1921             }
1922             free(ports);
1923             return NO_ERROR;
1924         }
1925 
1926         case GET_AUDIO_PORT: {
1927             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1928             struct audio_port port = {};
1929             if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1930                 ALOGE("b/23912202");
1931             }
1932             status_t status = getAudioPort(&port);
1933             reply->writeInt32(status);
1934             if (status == NO_ERROR) {
1935                 reply->write(&port, sizeof(struct audio_port));
1936             }
1937             return NO_ERROR;
1938         }
1939 
1940         case CREATE_AUDIO_PATCH: {
1941             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1942             struct audio_patch patch = {};
1943             data.read(&patch, sizeof(struct audio_patch));
1944             audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
1945             if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1946                 ALOGE("b/23912202");
1947             }
1948             status_t status = createAudioPatch(&patch, &handle);
1949             reply->writeInt32(status);
1950             if (status == NO_ERROR) {
1951                 reply->write(&handle, sizeof(audio_patch_handle_t));
1952             }
1953             return NO_ERROR;
1954         }
1955 
1956         case RELEASE_AUDIO_PATCH: {
1957             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1958             audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
1959             data.read(&handle, sizeof(audio_patch_handle_t));
1960             status_t status = releaseAudioPatch(handle);
1961             reply->writeInt32(status);
1962             return NO_ERROR;
1963         }
1964 
1965         case LIST_AUDIO_PATCHES: {
1966             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1967             unsigned int numPatchesReq = data.readInt32();
1968             if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1969                 numPatchesReq = MAX_ITEMS_PER_LIST;
1970             }
1971             unsigned int numPatches = numPatchesReq;
1972             struct audio_patch *patches =
1973                     (struct audio_patch *)calloc(numPatchesReq,
1974                                                  sizeof(struct audio_patch));
1975             if (patches == NULL) {
1976                 reply->writeInt32(NO_MEMORY);
1977                 reply->writeInt32(0);
1978                 return NO_ERROR;
1979             }
1980             unsigned int generation;
1981             status_t status = listAudioPatches(&numPatches, patches, &generation);
1982             reply->writeInt32(status);
1983             reply->writeInt32(numPatches);
1984             if (status == NO_ERROR) {
1985                 if (numPatchesReq > numPatches) {
1986                     numPatchesReq = numPatches;
1987                 }
1988                 reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1989                 reply->writeInt32(generation);
1990             }
1991             free(patches);
1992             return NO_ERROR;
1993         }
1994 
1995         case SET_AUDIO_PORT_CONFIG: {
1996             CHECK_INTERFACE(IAudioPolicyService, data, reply);
1997             struct audio_port_config config = {};
1998             data.read(&config, sizeof(struct audio_port_config));
1999             (void)sanitizeAudioPortConfig(&config);
2000             status_t status = setAudioPortConfig(&config);
2001             reply->writeInt32(status);
2002             return NO_ERROR;
2003         }
2004 
2005         case REGISTER_CLIENT: {
2006             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2007             sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
2008                     data.readStrongBinder());
2009             registerClient(client);
2010             return NO_ERROR;
2011         } break;
2012 
2013         case SET_AUDIO_PORT_CALLBACK_ENABLED: {
2014             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2015             setAudioPortCallbacksEnabled(data.readInt32() == 1);
2016             return NO_ERROR;
2017         } break;
2018 
2019         case SET_AUDIO_VOLUME_GROUP_CALLBACK_ENABLED: {
2020             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2021             setAudioVolumeGroupCallbacksEnabled(data.readInt32() == 1);
2022             return NO_ERROR;
2023         } break;
2024 
2025         case ACQUIRE_SOUNDTRIGGER_SESSION: {
2026             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2027             sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
2028                     data.readStrongBinder());
2029             audio_session_t session = AUDIO_SESSION_NONE;
2030             audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
2031             audio_devices_t device = AUDIO_DEVICE_NONE;
2032             status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
2033             reply->writeInt32(status);
2034             if (status == NO_ERROR) {
2035                 reply->writeInt32(session);
2036                 reply->writeInt32(ioHandle);
2037                 reply->writeInt32(device);
2038             }
2039             return NO_ERROR;
2040         } break;
2041 
2042         case RELEASE_SOUNDTRIGGER_SESSION: {
2043             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2044             sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
2045                     data.readStrongBinder());
2046             audio_session_t session = (audio_session_t)data.readInt32();
2047             status_t status = releaseSoundTriggerSession(session);
2048             reply->writeInt32(status);
2049             return NO_ERROR;
2050         } break;
2051 
2052         case GET_PHONE_STATE: {
2053             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2054             reply->writeInt32((int32_t)getPhoneState());
2055             return NO_ERROR;
2056         } break;
2057 
2058         case REGISTER_POLICY_MIXES: {
2059             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2060             bool registration = data.readInt32() == 1;
2061             Vector<AudioMix> mixes;
2062             size_t size = (size_t)data.readInt32();
2063             if (size > MAX_MIXES_PER_POLICY) {
2064                 size = MAX_MIXES_PER_POLICY;
2065             }
2066             for (size_t i = 0; i < size; i++) {
2067                 AudioMix mix;
2068                 if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
2069                     mixes.add(mix);
2070                 }
2071             }
2072             status_t status = registerPolicyMixes(mixes, registration);
2073             reply->writeInt32(status);
2074             return NO_ERROR;
2075         } break;
2076 
2077         case START_AUDIO_SOURCE: {
2078             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2079             struct audio_port_config source = {};
2080             data.read(&source, sizeof(struct audio_port_config));
2081             (void)sanitizeAudioPortConfig(&source);
2082             audio_attributes_t attributes = {};
2083             data.read(&attributes, sizeof(audio_attributes_t));
2084             sanetizeAudioAttributes(&attributes);
2085             audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
2086             status_t status = startAudioSource(&source, &attributes, &portId);
2087             reply->writeInt32(status);
2088             reply->writeInt32(portId);
2089             return NO_ERROR;
2090         } break;
2091 
2092         case STOP_AUDIO_SOURCE: {
2093             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2094             audio_port_handle_t portId = (audio_port_handle_t) data.readInt32();
2095             status_t status = stopAudioSource(portId);
2096             reply->writeInt32(status);
2097             return NO_ERROR;
2098         } break;
2099 
2100         case SET_MASTER_MONO: {
2101             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2102             bool mono = static_cast<bool>(data.readInt32());
2103             status_t status = setMasterMono(mono);
2104             reply->writeInt32(status);
2105             return NO_ERROR;
2106         } break;
2107 
2108         case GET_MASTER_MONO: {
2109             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2110             bool mono;
2111             status_t status = getMasterMono(&mono);
2112             reply->writeInt32(status);
2113             if (status == NO_ERROR) {
2114                 reply->writeInt32(static_cast<int32_t>(mono));
2115             }
2116             return NO_ERROR;
2117         } break;
2118 
2119         case GET_STREAM_VOLUME_DB: {
2120             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2121             audio_stream_type_t stream =
2122                     static_cast <audio_stream_type_t>(data.readInt32());
2123             int index = static_cast <int>(data.readInt32());
2124             audio_devices_t device =
2125                     static_cast <audio_devices_t>(data.readUint32());
2126             reply->writeFloat(getStreamVolumeDB(stream, index, device));
2127             return NO_ERROR;
2128         }
2129 
2130         case GET_SURROUND_FORMATS: {
2131             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2132             unsigned int numSurroundFormatsReq = data.readUint32();
2133             if (numSurroundFormatsReq > MAX_ITEMS_PER_LIST) {
2134                 numSurroundFormatsReq = MAX_ITEMS_PER_LIST;
2135             }
2136             bool reported = data.readBool();
2137             unsigned int numSurroundFormats = numSurroundFormatsReq;
2138             audio_format_t *surroundFormats = (audio_format_t *)calloc(
2139                     numSurroundFormats, sizeof(audio_format_t));
2140             bool *surroundFormatsEnabled = (bool *)calloc(numSurroundFormats, sizeof(bool));
2141             if (numSurroundFormatsReq > 0 &&
2142                     (surroundFormats == NULL || surroundFormatsEnabled == NULL)) {
2143                 free(surroundFormats);
2144                 free(surroundFormatsEnabled);
2145                 reply->writeInt32(NO_MEMORY);
2146                 return NO_ERROR;
2147             }
2148             status_t status = getSurroundFormats(
2149                     &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported);
2150             reply->writeInt32(status);
2151 
2152             if (status == NO_ERROR) {
2153                 reply->writeUint32(numSurroundFormats);
2154                 if (numSurroundFormatsReq > numSurroundFormats) {
2155                     numSurroundFormatsReq = numSurroundFormats;
2156                 }
2157                 reply->write(surroundFormats, numSurroundFormatsReq * sizeof(audio_format_t));
2158                 reply->write(surroundFormatsEnabled, numSurroundFormatsReq * sizeof(bool));
2159             }
2160             free(surroundFormats);
2161             free(surroundFormatsEnabled);
2162             return NO_ERROR;
2163         }
2164 
2165         case SET_SURROUND_FORMAT_ENABLED: {
2166             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2167             audio_format_t audioFormat = (audio_format_t) data.readInt32();
2168             bool enabled = data.readBool();
2169             status_t status = setSurroundFormatEnabled(audioFormat, enabled);
2170             reply->writeInt32(status);
2171             return NO_ERROR;
2172         }
2173 
2174         case GET_OFFLOAD_FORMATS_A2DP: {
2175             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2176             std::vector<audio_format_t> encodingFormats;
2177             status_t status = getHwOffloadEncodingFormatsSupportedForA2DP(&encodingFormats);
2178             reply->writeInt32(status);
2179             if (status != NO_ERROR) {
2180                 return NO_ERROR;
2181             }
2182             reply->writeUint32(static_cast<uint32_t>(encodingFormats.size()));
2183             for (size_t i = 0; i < encodingFormats.size(); i++)
2184                 reply->writeInt32(static_cast<int32_t>(encodingFormats[i]));
2185             return NO_ERROR;
2186         }
2187 
2188 
2189         case ADD_STREAM_DEFAULT_EFFECT: {
2190             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2191             effect_uuid_t type;
2192             status_t status = data.read(&type, sizeof(effect_uuid_t));
2193             if (status != NO_ERROR) {
2194                 return status;
2195             }
2196             String16 opPackageName;
2197             status = data.readString16(&opPackageName);
2198             if (status != NO_ERROR) {
2199                 return status;
2200             }
2201             effect_uuid_t uuid;
2202             status = data.read(&uuid, sizeof(effect_uuid_t));
2203             if (status != NO_ERROR) {
2204                 return status;
2205             }
2206             int32_t priority = data.readInt32();
2207             audio_usage_t usage = (audio_usage_t) data.readInt32();
2208             audio_unique_id_t id = 0;
2209             reply->writeInt32(static_cast <int32_t>(addStreamDefaultEffect(&type,
2210                                                                            opPackageName,
2211                                                                            &uuid,
2212                                                                            priority,
2213                                                                            usage,
2214                                                                            &id)));
2215             reply->writeInt32(id);
2216             return NO_ERROR;
2217         }
2218 
2219         case REMOVE_STREAM_DEFAULT_EFFECT: {
2220             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2221             audio_unique_id_t id = static_cast<audio_unique_id_t>(data.readInt32());
2222             reply->writeInt32(static_cast <int32_t>(removeStreamDefaultEffect(id)));
2223             return NO_ERROR;
2224         }
2225 
2226         case ADD_SOURCE_DEFAULT_EFFECT: {
2227             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2228             effect_uuid_t type;
2229             status_t status = data.read(&type, sizeof(effect_uuid_t));
2230             if (status != NO_ERROR) {
2231                 return status;
2232             }
2233             String16 opPackageName;
2234             status = data.readString16(&opPackageName);
2235             if (status != NO_ERROR) {
2236                 return status;
2237             }
2238             effect_uuid_t uuid;
2239             status = data.read(&uuid, sizeof(effect_uuid_t));
2240             if (status != NO_ERROR) {
2241                 return status;
2242             }
2243             int32_t priority = data.readInt32();
2244             audio_source_t source = (audio_source_t) data.readInt32();
2245             audio_unique_id_t id = 0;
2246             reply->writeInt32(static_cast <int32_t>(addSourceDefaultEffect(&type,
2247                                                                            opPackageName,
2248                                                                            &uuid,
2249                                                                            priority,
2250                                                                            source,
2251                                                                            &id)));
2252             reply->writeInt32(id);
2253             return NO_ERROR;
2254         }
2255 
2256         case REMOVE_SOURCE_DEFAULT_EFFECT: {
2257             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2258             audio_unique_id_t id = static_cast<audio_unique_id_t>(data.readInt32());
2259             reply->writeInt32(static_cast <int32_t>(removeSourceDefaultEffect(id)));
2260             return NO_ERROR;
2261         }
2262 
2263         case SET_ASSISTANT_UID: {
2264             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2265             int32_t uid;
2266             status_t status = data.readInt32(&uid);
2267             if (status != NO_ERROR) {
2268                 return status;
2269             }
2270             status = setAssistantUid(uid);
2271             reply->writeInt32(static_cast <int32_t>(status));
2272             return NO_ERROR;
2273         }
2274 
2275         case SET_A11Y_SERVICES_UIDS: {
2276             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2277             std::vector<uid_t> uids;
2278             int32_t size;
2279             status_t status = data.readInt32(&size);
2280             if (status != NO_ERROR) {
2281                 return status;
2282             }
2283             if (size > MAX_ITEMS_PER_LIST) {
2284                 size = MAX_ITEMS_PER_LIST;
2285             }
2286             for (int32_t i = 0; i < size; i++) {
2287                 int32_t uid;
2288                 status =  data.readInt32(&uid);
2289                 if (status != NO_ERROR) {
2290                     return status;
2291                 }
2292                 uids.push_back(uid);
2293             }
2294             status = setA11yServicesUids(uids);
2295             reply->writeInt32(static_cast <int32_t>(status));
2296             return NO_ERROR;
2297         }
2298 
2299         case IS_HAPTIC_PLAYBACK_SUPPORTED: {
2300             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2301             bool isSupported = isHapticPlaybackSupported();
2302             reply->writeBool(isSupported);
2303             return NO_ERROR;
2304         }
2305 
2306         case SET_UID_DEVICE_AFFINITY: {
2307             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2308             const uid_t uid = (uid_t) data.readInt32();
2309             Vector<AudioDeviceTypeAddr> devices;
2310             size_t size = (size_t)data.readInt32();
2311             for (size_t i = 0; i < size; i++) {
2312                 AudioDeviceTypeAddr device;
2313                 if (device.readFromParcel((Parcel*)&data) == NO_ERROR) {
2314                     devices.add(device);
2315                 }
2316             }
2317             status_t status = setUidDeviceAffinities(uid, devices);
2318             reply->writeInt32(status);
2319             return NO_ERROR;
2320         }
2321 
2322         case REMOVE_UID_DEVICE_AFFINITY: {
2323             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2324             const uid_t uid = (uid_t) data.readInt32();
2325             status_t status = removeUidDeviceAffinities(uid);
2326             reply->writeInt32(status);
2327             return NO_ERROR;
2328         }
2329 
2330         case LIST_AUDIO_PRODUCT_STRATEGIES: {
2331             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2332             AudioProductStrategyVector strategies;
2333             status_t status = listAudioProductStrategies(strategies);
2334             reply->writeInt32(status);
2335             if (status != NO_ERROR) {
2336                 return NO_ERROR;
2337             }
2338             size_t size = strategies.size();
2339             size_t sizePosition = reply->dataPosition();
2340             reply->writeInt32(size);
2341             size_t finalSize = size;
2342             for (size_t i = 0; i < size; i++) {
2343                 size_t position = reply->dataPosition();
2344                 if (strategies[i].writeToParcel(reply) != NO_ERROR) {
2345                     reply->setDataPosition(position);
2346                     finalSize--;
2347                 }
2348             }
2349             if (size != finalSize) {
2350                 size_t position = reply->dataPosition();
2351                 reply->setDataPosition(sizePosition);
2352                 reply->writeInt32(finalSize);
2353                 reply->setDataPosition(position);
2354             }
2355             return NO_ERROR;
2356         }
2357 
2358         case GET_STRATEGY_FOR_ATTRIBUTES: {
2359             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2360             AudioAttributes attributes;
2361             status_t status = attributes.readFromParcel(&data);
2362             if (status != NO_ERROR) {
2363                 return status;
2364             }
2365             product_strategy_t strategy;
2366             status = getProductStrategyFromAudioAttributes(attributes, strategy);
2367             reply->writeInt32(status);
2368             if (status != NO_ERROR) {
2369                 return NO_ERROR;
2370             }
2371             reply->writeUint32(static_cast<int>(strategy));
2372             return NO_ERROR;
2373         }
2374 
2375         case LIST_AUDIO_VOLUME_GROUPS: {
2376             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2377             AudioVolumeGroupVector groups;
2378             status_t status = listAudioVolumeGroups(groups);
2379             reply->writeInt32(status);
2380             if (status != NO_ERROR) {
2381                 return NO_ERROR;
2382             }
2383             size_t size = groups.size();
2384             size_t sizePosition = reply->dataPosition();
2385             reply->writeInt32(size);
2386             size_t finalSize = size;
2387             for (size_t i = 0; i < size; i++) {
2388                 size_t position = reply->dataPosition();
2389                 if (groups[i].writeToParcel(reply) != NO_ERROR) {
2390                     reply->setDataPosition(position);
2391                     finalSize--;
2392                 }
2393             }
2394             if (size != finalSize) {
2395                 size_t position = reply->dataPosition();
2396                 reply->setDataPosition(sizePosition);
2397                 reply->writeInt32(finalSize);
2398                 reply->setDataPosition(position);
2399             }
2400             return NO_ERROR;
2401         }
2402 
2403         case GET_VOLUME_GROUP_FOR_ATTRIBUTES: {
2404             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2405             AudioAttributes attributes;
2406             status_t status = attributes.readFromParcel(&data);
2407             if (status != NO_ERROR) {
2408                 return status;
2409             }
2410             volume_group_t group;
2411             status = getVolumeGroupFromAudioAttributes(attributes, group);
2412             reply->writeInt32(status);
2413             if (status != NO_ERROR) {
2414                 return NO_ERROR;
2415             }
2416             reply->writeUint32(static_cast<int>(group));
2417             return NO_ERROR;
2418         }
2419 
2420         case SET_ALLOWED_CAPTURE_POLICY: {
2421             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2422             uid_t uid = data.readInt32();
2423             audio_flags_mask_t flags = data.readInt32();
2424             status_t status = setAllowedCapturePolicy(uid, flags);
2425             reply->writeInt32(status);
2426             return NO_ERROR;
2427         }
2428 
2429         case SET_RTT_ENABLED: {
2430             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2431             bool enabled = static_cast<bool>(data.readInt32());
2432             status_t status = setRttEnabled(enabled);
2433             reply->writeInt32(status);
2434             return NO_ERROR;
2435         }
2436 
2437         case SET_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY: {
2438             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2439             product_strategy_t strategy = (product_strategy_t) data.readUint32();
2440             AudioDeviceTypeAddr device;
2441             status_t status = device.readFromParcel((Parcel*)&data);
2442             if (status != NO_ERROR) {
2443                 return status;
2444             }
2445             status = setPreferredDeviceForStrategy(strategy, device);
2446             reply->writeInt32(status);
2447             return NO_ERROR;
2448         }
2449 
2450         case REMOVE_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY: {
2451             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2452             product_strategy_t strategy = (product_strategy_t) data.readUint32();
2453             status_t status = removePreferredDeviceForStrategy(strategy);
2454             reply->writeInt32(status);
2455             return NO_ERROR;
2456         }
2457 
2458         case GET_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY: {
2459             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2460             product_strategy_t strategy = (product_strategy_t) data.readUint32();
2461             AudioDeviceTypeAddr device;
2462             status_t status = getPreferredDeviceForStrategy(strategy, device);
2463             status_t marshall_status = device.writeToParcel(reply);
2464             if (marshall_status != NO_ERROR) {
2465                 return marshall_status;
2466             }
2467             reply->writeInt32(status);
2468             return NO_ERROR;
2469         }
2470 
2471         case AUDIO_MODULES_UPDATED: {
2472             CHECK_INTERFACE(IAudioPolicyService, data, reply);
2473             onNewAudioModulesAvailable();
2474             return NO_ERROR;
2475         } break;
2476 
2477         default:
2478             return BBinder::onTransact(code, data, reply, flags);
2479     }
2480 }
2481 
2482 /** returns true if string overflow was prevented by zero termination */
2483 template <size_t size>
preventStringOverflow(char (& s)[size])2484 static bool preventStringOverflow(char (&s)[size]) {
2485     if (strnlen(s, size) < size) return false;
2486     s[size - 1] = '\0';
2487     return true;
2488 }
2489 
sanetizeAudioAttributes(audio_attributes_t * attr)2490 void BnAudioPolicyService::sanetizeAudioAttributes(audio_attributes_t* attr)
2491 {
2492     const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
2493     if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
2494         android_errorWriteLog(0x534e4554, "68953950"); // SafetyNet logging
2495     }
2496     attr->tags[tagsMaxSize - 1] = '\0';
2497 }
2498 
2499 /** returns BAD_VALUE if sanitization was required. */
sanitizeEffectDescriptor(effect_descriptor_t * desc)2500 status_t BnAudioPolicyService::sanitizeEffectDescriptor(effect_descriptor_t* desc)
2501 {
2502     if (preventStringOverflow(desc->name)
2503         | /* always */ preventStringOverflow(desc->implementor)) {
2504         android_errorWriteLog(0x534e4554, "73126106"); // SafetyNet logging
2505         return BAD_VALUE;
2506     }
2507     return NO_ERROR;
2508 }
2509 
2510 /** returns BAD_VALUE if sanitization was required. */
sanitizeAudioPortConfig(struct audio_port_config * config)2511 status_t BnAudioPolicyService::sanitizeAudioPortConfig(struct audio_port_config* config)
2512 {
2513     if (config->type == AUDIO_PORT_TYPE_DEVICE &&
2514         preventStringOverflow(config->ext.device.address)) {
2515         return BAD_VALUE;
2516     }
2517     return NO_ERROR;
2518 }
2519 
2520 // ----------------------------------------------------------------------------
2521 
2522 } // namespace android
2523