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