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 <sys/types.h>
23
24#include <binder/Parcel.h>
25
26#include <media/AudioEffect.h>
27#include <media/IAudioPolicyService.h>
28
29#include <system/audio.h>
30
31namespace android {
32
33enum {
34    SET_DEVICE_CONNECTION_STATE = IBinder::FIRST_CALL_TRANSACTION,
35    GET_DEVICE_CONNECTION_STATE,
36    HANDLE_DEVICE_CONFIG_CHANGE,
37    SET_PHONE_STATE,
38    SET_RINGER_MODE,    // reserved, no longer used
39    SET_FORCE_USE,
40    GET_FORCE_USE,
41    GET_OUTPUT,
42    START_OUTPUT,
43    STOP_OUTPUT,
44    RELEASE_OUTPUT,
45    GET_INPUT_FOR_ATTR,
46    START_INPUT,
47    STOP_INPUT,
48    RELEASE_INPUT,
49    INIT_STREAM_VOLUME,
50    SET_STREAM_VOLUME,
51    GET_STREAM_VOLUME,
52    GET_STRATEGY_FOR_STREAM,
53    GET_OUTPUT_FOR_EFFECT,
54    REGISTER_EFFECT,
55    UNREGISTER_EFFECT,
56    IS_STREAM_ACTIVE,
57    IS_SOURCE_ACTIVE,
58    GET_DEVICES_FOR_STREAM,
59    QUERY_DEFAULT_PRE_PROCESSING,
60    SET_EFFECT_ENABLED,
61    IS_STREAM_ACTIVE_REMOTELY,
62    IS_OFFLOAD_SUPPORTED,
63    LIST_AUDIO_PORTS,
64    GET_AUDIO_PORT,
65    CREATE_AUDIO_PATCH,
66    RELEASE_AUDIO_PATCH,
67    LIST_AUDIO_PATCHES,
68    SET_AUDIO_PORT_CONFIG,
69    REGISTER_CLIENT,
70    GET_OUTPUT_FOR_ATTR,
71    ACQUIRE_SOUNDTRIGGER_SESSION,
72    RELEASE_SOUNDTRIGGER_SESSION,
73    GET_PHONE_STATE,
74    REGISTER_POLICY_MIXES,
75    START_AUDIO_SOURCE,
76    STOP_AUDIO_SOURCE,
77    SET_AUDIO_PORT_CALLBACK_ENABLED,
78    SET_MASTER_MONO,
79    GET_MASTER_MONO,
80};
81
82#define MAX_ITEMS_PER_LIST 1024
83
84class BpAudioPolicyService : public BpInterface<IAudioPolicyService>
85{
86public:
87    explicit BpAudioPolicyService(const sp<IBinder>& impl)
88        : BpInterface<IAudioPolicyService>(impl)
89    {
90    }
91
92    virtual status_t setDeviceConnectionState(
93                                    audio_devices_t device,
94                                    audio_policy_dev_state_t state,
95                                    const char *device_address,
96                                    const char *device_name)
97    {
98        Parcel data, reply;
99        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
100        data.writeInt32(static_cast <uint32_t>(device));
101        data.writeInt32(static_cast <uint32_t>(state));
102        data.writeCString(device_address);
103        data.writeCString(device_name);
104        remote()->transact(SET_DEVICE_CONNECTION_STATE, data, &reply);
105        return static_cast <status_t> (reply.readInt32());
106    }
107
108    virtual audio_policy_dev_state_t getDeviceConnectionState(
109                                    audio_devices_t device,
110                                    const char *device_address)
111    {
112        Parcel data, reply;
113        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
114        data.writeInt32(static_cast <uint32_t>(device));
115        data.writeCString(device_address);
116        remote()->transact(GET_DEVICE_CONNECTION_STATE, data, &reply);
117        return static_cast <audio_policy_dev_state_t>(reply.readInt32());
118    }
119
120    virtual status_t handleDeviceConfigChange(audio_devices_t device,
121                                              const char *device_address,
122                                              const char *device_name)
123    {
124        Parcel data, reply;
125        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
126        data.writeInt32(static_cast <uint32_t>(device));
127        data.writeCString(device_address);
128        data.writeCString(device_name);
129        remote()->transact(HANDLE_DEVICE_CONFIG_CHANGE, data, &reply);
130        return static_cast <status_t> (reply.readInt32());
131    }
132
133    virtual status_t setPhoneState(audio_mode_t state)
134    {
135        Parcel data, reply;
136        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
137        data.writeInt32(state);
138        remote()->transact(SET_PHONE_STATE, data, &reply);
139        return static_cast <status_t> (reply.readInt32());
140    }
141
142    virtual status_t setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
143    {
144        Parcel data, reply;
145        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
146        data.writeInt32(static_cast <uint32_t>(usage));
147        data.writeInt32(static_cast <uint32_t>(config));
148        remote()->transact(SET_FORCE_USE, data, &reply);
149        return static_cast <status_t> (reply.readInt32());
150    }
151
152    virtual audio_policy_forced_cfg_t getForceUse(audio_policy_force_use_t usage)
153    {
154        Parcel data, reply;
155        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
156        data.writeInt32(static_cast <uint32_t>(usage));
157        remote()->transact(GET_FORCE_USE, data, &reply);
158        return static_cast <audio_policy_forced_cfg_t> (reply.readInt32());
159    }
160
161    virtual audio_io_handle_t getOutput(
162                                        audio_stream_type_t stream,
163                                        uint32_t samplingRate,
164                                        audio_format_t format,
165                                        audio_channel_mask_t channelMask,
166                                        audio_output_flags_t flags,
167                                        const audio_offload_info_t *offloadInfo)
168    {
169        Parcel data, reply;
170        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
171        data.writeInt32(static_cast <uint32_t>(stream));
172        data.writeInt32(samplingRate);
173        data.writeInt32(static_cast <uint32_t>(format));
174        data.writeInt32(channelMask);
175        data.writeInt32(static_cast <uint32_t>(flags));
176        // hasOffloadInfo
177        if (offloadInfo == NULL) {
178            data.writeInt32(0);
179        } else {
180            data.writeInt32(1);
181            data.write(offloadInfo, sizeof(audio_offload_info_t));
182        }
183        remote()->transact(GET_OUTPUT, data, &reply);
184        return static_cast <audio_io_handle_t> (reply.readInt32());
185    }
186
187    virtual status_t getOutputForAttr(const audio_attributes_t *attr,
188                                        audio_io_handle_t *output,
189                                        audio_session_t session,
190                                        audio_stream_type_t *stream,
191                                        uid_t uid,
192                                        const audio_config_t *config,
193                                        audio_output_flags_t flags,
194                                        audio_port_handle_t selectedDeviceId,
195                                        audio_port_handle_t *portId)
196        {
197            Parcel data, reply;
198            data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
199            if (attr == NULL) {
200                if (stream == NULL) {
201                    ALOGE("getOutputForAttr(): NULL audio attributes and stream type");
202                    return BAD_VALUE;
203                }
204                if (*stream == AUDIO_STREAM_DEFAULT) {
205                    ALOGE("getOutputForAttr unspecified stream type");
206                    return BAD_VALUE;
207                }
208            }
209            if (output == NULL) {
210                ALOGE("getOutputForAttr NULL output - shouldn't happen");
211                return BAD_VALUE;
212            }
213            if (portId == NULL) {
214                ALOGE("getOutputForAttr NULL portId - shouldn't happen");
215                return BAD_VALUE;
216            }
217            if (attr == NULL) {
218                data.writeInt32(0);
219            } else {
220                data.writeInt32(1);
221                data.write(attr, sizeof(audio_attributes_t));
222            }
223            data.writeInt32(session);
224            if (stream == NULL) {
225                data.writeInt32(0);
226            } else {
227                data.writeInt32(1);
228                data.writeInt32(*stream);
229            }
230            data.writeInt32(uid);
231            data.write(config, sizeof(audio_config_t));
232            data.writeInt32(static_cast <uint32_t>(flags));
233            data.writeInt32(selectedDeviceId);
234            data.writeInt32(*portId);
235            status_t status = remote()->transact(GET_OUTPUT_FOR_ATTR, data, &reply);
236            if (status != NO_ERROR) {
237                return status;
238            }
239            status = (status_t)reply.readInt32();
240            if (status != NO_ERROR) {
241                return status;
242            }
243            *output = (audio_io_handle_t)reply.readInt32();
244            audio_stream_type_t lStream = (audio_stream_type_t)reply.readInt32();
245            if (stream != NULL) {
246                *stream = lStream;
247            }
248            *portId = (audio_port_handle_t)reply.readInt32();
249            return status;
250        }
251
252    virtual status_t startOutput(audio_io_handle_t output,
253                                 audio_stream_type_t stream,
254                                 audio_session_t session)
255    {
256        Parcel data, reply;
257        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
258        data.writeInt32(output);
259        data.writeInt32((int32_t) stream);
260        data.writeInt32((int32_t) session);
261        remote()->transact(START_OUTPUT, data, &reply);
262        return static_cast <status_t> (reply.readInt32());
263    }
264
265    virtual status_t stopOutput(audio_io_handle_t output,
266                                audio_stream_type_t stream,
267                                audio_session_t session)
268    {
269        Parcel data, reply;
270        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
271        data.writeInt32(output);
272        data.writeInt32((int32_t) stream);
273        data.writeInt32((int32_t) session);
274        remote()->transact(STOP_OUTPUT, data, &reply);
275        return static_cast <status_t> (reply.readInt32());
276    }
277
278    virtual void releaseOutput(audio_io_handle_t output,
279                               audio_stream_type_t stream,
280                               audio_session_t session)
281    {
282        Parcel data, reply;
283        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
284        data.writeInt32(output);
285        data.writeInt32((int32_t)stream);
286        data.writeInt32((int32_t)session);
287        remote()->transact(RELEASE_OUTPUT, data, &reply);
288    }
289
290    virtual status_t getInputForAttr(const audio_attributes_t *attr,
291                                     audio_io_handle_t *input,
292                                     audio_session_t session,
293                                     pid_t pid,
294                                     uid_t uid,
295                                     const audio_config_base_t *config,
296                                     audio_input_flags_t flags,
297                                     audio_port_handle_t selectedDeviceId,
298                                     audio_port_handle_t *portId)
299    {
300        Parcel data, reply;
301        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
302        if (attr == NULL) {
303            ALOGE("getInputForAttr NULL attr - shouldn't happen");
304            return BAD_VALUE;
305        }
306        if (input == NULL) {
307            ALOGE("getInputForAttr NULL input - shouldn't happen");
308            return BAD_VALUE;
309        }
310        if (portId == NULL) {
311            ALOGE("getInputForAttr NULL portId - shouldn't happen");
312            return BAD_VALUE;
313        }
314        data.write(attr, sizeof(audio_attributes_t));
315        data.writeInt32(session);
316        data.writeInt32(pid);
317        data.writeInt32(uid);
318        data.write(config, sizeof(audio_config_base_t));
319        data.writeInt32(flags);
320        data.writeInt32(selectedDeviceId);
321        data.writeInt32(*portId);
322        status_t status = remote()->transact(GET_INPUT_FOR_ATTR, data, &reply);
323        if (status != NO_ERROR) {
324            return status;
325        }
326        status = reply.readInt32();
327        if (status != NO_ERROR) {
328            return status;
329        }
330        *input = (audio_io_handle_t)reply.readInt32();
331        *portId = (audio_port_handle_t)reply.readInt32();
332        return NO_ERROR;
333    }
334
335    virtual status_t startInput(audio_io_handle_t input,
336                                audio_session_t session)
337    {
338        Parcel data, reply;
339        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
340        data.writeInt32(input);
341        data.writeInt32(session);
342        remote()->transact(START_INPUT, data, &reply);
343        return static_cast <status_t> (reply.readInt32());
344    }
345
346    virtual status_t stopInput(audio_io_handle_t input,
347                               audio_session_t session)
348    {
349        Parcel data, reply;
350        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
351        data.writeInt32(input);
352        data.writeInt32(session);
353        remote()->transact(STOP_INPUT, data, &reply);
354        return static_cast <status_t> (reply.readInt32());
355    }
356
357    virtual void releaseInput(audio_io_handle_t input,
358                              audio_session_t session)
359    {
360        Parcel data, reply;
361        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
362        data.writeInt32(input);
363        data.writeInt32(session);
364        remote()->transact(RELEASE_INPUT, data, &reply);
365    }
366
367    virtual status_t initStreamVolume(audio_stream_type_t stream,
368                                    int indexMin,
369                                    int indexMax)
370    {
371        Parcel data, reply;
372        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
373        data.writeInt32(static_cast <uint32_t>(stream));
374        data.writeInt32(indexMin);
375        data.writeInt32(indexMax);
376        remote()->transact(INIT_STREAM_VOLUME, data, &reply);
377        return static_cast <status_t> (reply.readInt32());
378    }
379
380    virtual status_t setStreamVolumeIndex(audio_stream_type_t stream,
381                                          int index,
382                                          audio_devices_t device)
383    {
384        Parcel data, reply;
385        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
386        data.writeInt32(static_cast <uint32_t>(stream));
387        data.writeInt32(index);
388        data.writeInt32(static_cast <uint32_t>(device));
389        remote()->transact(SET_STREAM_VOLUME, data, &reply);
390        return static_cast <status_t> (reply.readInt32());
391    }
392
393    virtual status_t getStreamVolumeIndex(audio_stream_type_t stream,
394                                          int *index,
395                                          audio_devices_t device)
396    {
397        Parcel data, reply;
398        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
399        data.writeInt32(static_cast <uint32_t>(stream));
400        data.writeInt32(static_cast <uint32_t>(device));
401
402        remote()->transact(GET_STREAM_VOLUME, data, &reply);
403        int lIndex = reply.readInt32();
404        if (index) *index = lIndex;
405        return static_cast <status_t> (reply.readInt32());
406    }
407
408    virtual uint32_t getStrategyForStream(audio_stream_type_t stream)
409    {
410        Parcel data, reply;
411        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
412        data.writeInt32(static_cast <uint32_t>(stream));
413        remote()->transact(GET_STRATEGY_FOR_STREAM, data, &reply);
414        return reply.readInt32();
415    }
416
417    virtual audio_devices_t getDevicesForStream(audio_stream_type_t stream)
418    {
419        Parcel data, reply;
420        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
421        data.writeInt32(static_cast <uint32_t>(stream));
422        remote()->transact(GET_DEVICES_FOR_STREAM, data, &reply);
423        return (audio_devices_t) reply.readInt32();
424    }
425
426    virtual audio_io_handle_t getOutputForEffect(const effect_descriptor_t *desc)
427    {
428        Parcel data, reply;
429        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
430        data.write(desc, sizeof(effect_descriptor_t));
431        remote()->transact(GET_OUTPUT_FOR_EFFECT, data, &reply);
432        return static_cast <audio_io_handle_t> (reply.readInt32());
433    }
434
435    virtual status_t registerEffect(const effect_descriptor_t *desc,
436                                        audio_io_handle_t io,
437                                        uint32_t strategy,
438                                        audio_session_t session,
439                                        int id)
440    {
441        Parcel data, reply;
442        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
443        data.write(desc, sizeof(effect_descriptor_t));
444        data.writeInt32(io);
445        data.writeInt32(strategy);
446        data.writeInt32(session);
447        data.writeInt32(id);
448        remote()->transact(REGISTER_EFFECT, data, &reply);
449        return static_cast <status_t> (reply.readInt32());
450    }
451
452    virtual status_t unregisterEffect(int id)
453    {
454        Parcel data, reply;
455        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
456        data.writeInt32(id);
457        remote()->transact(UNREGISTER_EFFECT, data, &reply);
458        return static_cast <status_t> (reply.readInt32());
459    }
460
461    virtual status_t setEffectEnabled(int id, bool enabled)
462    {
463        Parcel data, reply;
464        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
465        data.writeInt32(id);
466        data.writeInt32(enabled);
467        remote()->transact(SET_EFFECT_ENABLED, data, &reply);
468        return static_cast <status_t> (reply.readInt32());
469    }
470
471    virtual bool isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
472    {
473        Parcel data, reply;
474        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
475        data.writeInt32((int32_t) stream);
476        data.writeInt32(inPastMs);
477        remote()->transact(IS_STREAM_ACTIVE, data, &reply);
478        return reply.readInt32();
479    }
480
481    virtual bool isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
482    {
483        Parcel data, reply;
484        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
485        data.writeInt32((int32_t) stream);
486        data.writeInt32(inPastMs);
487        remote()->transact(IS_STREAM_ACTIVE_REMOTELY, data, &reply);
488        return reply.readInt32();
489    }
490
491    virtual bool isSourceActive(audio_source_t source) const
492    {
493        Parcel data, reply;
494        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
495        data.writeInt32((int32_t) source);
496        remote()->transact(IS_SOURCE_ACTIVE, data, &reply);
497        return reply.readInt32();
498    }
499
500    virtual status_t queryDefaultPreProcessing(audio_session_t audioSession,
501                                               effect_descriptor_t *descriptors,
502                                               uint32_t *count)
503    {
504        if (descriptors == NULL || count == NULL) {
505            return BAD_VALUE;
506        }
507        Parcel data, reply;
508        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
509        data.writeInt32(audioSession);
510        data.writeInt32(*count);
511        status_t status = remote()->transact(QUERY_DEFAULT_PRE_PROCESSING, data, &reply);
512        if (status != NO_ERROR) {
513            return status;
514        }
515        status = static_cast <status_t> (reply.readInt32());
516        uint32_t retCount = reply.readInt32();
517        if (retCount != 0) {
518            uint32_t numDesc = (retCount < *count) ? retCount : *count;
519            reply.read(descriptors, sizeof(effect_descriptor_t) * numDesc);
520        }
521        *count = retCount;
522        return status;
523    }
524
525    virtual bool isOffloadSupported(const audio_offload_info_t& info)
526    {
527        Parcel data, reply;
528        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
529        data.write(&info, sizeof(audio_offload_info_t));
530        remote()->transact(IS_OFFLOAD_SUPPORTED, data, &reply);
531        return reply.readInt32();
532    }
533
534    virtual status_t listAudioPorts(audio_port_role_t role,
535                                    audio_port_type_t type,
536                                    unsigned int *num_ports,
537                                    struct audio_port *ports,
538                                    unsigned int *generation)
539    {
540        if (num_ports == NULL || (*num_ports != 0 && ports == NULL) ||
541                generation == NULL) {
542            return BAD_VALUE;
543        }
544        Parcel data, reply;
545        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
546        unsigned int numPortsReq = (ports == NULL) ? 0 : *num_ports;
547        data.writeInt32(role);
548        data.writeInt32(type);
549        data.writeInt32(numPortsReq);
550        status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply);
551        if (status == NO_ERROR) {
552            status = (status_t)reply.readInt32();
553            *num_ports = (unsigned int)reply.readInt32();
554        }
555        if (status == NO_ERROR) {
556            if (numPortsReq > *num_ports) {
557                numPortsReq = *num_ports;
558            }
559            if (numPortsReq > 0) {
560                reply.read(ports, numPortsReq * sizeof(struct audio_port));
561            }
562            *generation = reply.readInt32();
563        }
564        return status;
565    }
566
567    virtual status_t getAudioPort(struct audio_port *port)
568    {
569        if (port == NULL) {
570            return BAD_VALUE;
571        }
572        Parcel data, reply;
573        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
574        data.write(port, sizeof(struct audio_port));
575        status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply);
576        if (status != NO_ERROR ||
577                (status = (status_t)reply.readInt32()) != NO_ERROR) {
578            return status;
579        }
580        reply.read(port, sizeof(struct audio_port));
581        return status;
582    }
583
584    virtual status_t createAudioPatch(const struct audio_patch *patch,
585                                       audio_patch_handle_t *handle)
586    {
587        if (patch == NULL || handle == NULL) {
588            return BAD_VALUE;
589        }
590        Parcel data, reply;
591        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
592        data.write(patch, sizeof(struct audio_patch));
593        data.write(handle, sizeof(audio_patch_handle_t));
594        status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply);
595        if (status != NO_ERROR ||
596                (status = (status_t)reply.readInt32()) != NO_ERROR) {
597            return status;
598        }
599        reply.read(handle, sizeof(audio_patch_handle_t));
600        return status;
601    }
602
603    virtual status_t releaseAudioPatch(audio_patch_handle_t handle)
604    {
605        Parcel data, reply;
606        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
607        data.write(&handle, sizeof(audio_patch_handle_t));
608        status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply);
609        if (status != NO_ERROR) {
610            status = (status_t)reply.readInt32();
611        }
612        return status;
613    }
614
615    virtual status_t listAudioPatches(unsigned int *num_patches,
616                                      struct audio_patch *patches,
617                                      unsigned int *generation)
618    {
619        if (num_patches == NULL || (*num_patches != 0 && patches == NULL) ||
620                generation == NULL) {
621            return BAD_VALUE;
622        }
623        Parcel data, reply;
624        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
625        unsigned int numPatchesReq = (patches == NULL) ? 0 : *num_patches;
626        data.writeInt32(numPatchesReq);
627        status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply);
628        if (status == NO_ERROR) {
629            status = (status_t)reply.readInt32();
630            *num_patches = (unsigned int)reply.readInt32();
631        }
632        if (status == NO_ERROR) {
633            if (numPatchesReq > *num_patches) {
634                numPatchesReq = *num_patches;
635            }
636            if (numPatchesReq > 0) {
637                reply.read(patches, numPatchesReq * sizeof(struct audio_patch));
638            }
639            *generation = reply.readInt32();
640        }
641        return status;
642    }
643
644    virtual status_t setAudioPortConfig(const struct audio_port_config *config)
645    {
646        if (config == NULL) {
647            return BAD_VALUE;
648        }
649        Parcel data, reply;
650        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
651        data.write(config, sizeof(struct audio_port_config));
652        status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply);
653        if (status != NO_ERROR) {
654            status = (status_t)reply.readInt32();
655        }
656        return status;
657    }
658
659    virtual void registerClient(const sp<IAudioPolicyServiceClient>& client)
660    {
661        Parcel data, reply;
662        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
663        data.writeStrongBinder(IInterface::asBinder(client));
664        remote()->transact(REGISTER_CLIENT, data, &reply);
665    }
666
667    virtual void setAudioPortCallbacksEnabled(bool enabled)
668    {
669        Parcel data, reply;
670        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
671        data.writeInt32(enabled ? 1 : 0);
672        remote()->transact(SET_AUDIO_PORT_CALLBACK_ENABLED, data, &reply);
673    }
674
675    virtual status_t acquireSoundTriggerSession(audio_session_t *session,
676                                            audio_io_handle_t *ioHandle,
677                                            audio_devices_t *device)
678    {
679        if (session == NULL || ioHandle == NULL || device == NULL) {
680            return BAD_VALUE;
681        }
682        Parcel data, reply;
683        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
684        status_t status = remote()->transact(ACQUIRE_SOUNDTRIGGER_SESSION, data, &reply);
685        if (status != NO_ERROR) {
686            return status;
687        }
688        status = (status_t)reply.readInt32();
689        if (status == NO_ERROR) {
690            *session = (audio_session_t)reply.readInt32();
691            *ioHandle = (audio_io_handle_t)reply.readInt32();
692            *device = (audio_devices_t)reply.readInt32();
693        }
694        return status;
695    }
696
697    virtual status_t releaseSoundTriggerSession(audio_session_t session)
698    {
699        Parcel data, reply;
700        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
701        data.writeInt32(session);
702        status_t status = remote()->transact(RELEASE_SOUNDTRIGGER_SESSION, data, &reply);
703        if (status != NO_ERROR) {
704            return status;
705        }
706        return (status_t)reply.readInt32();
707    }
708
709    virtual audio_mode_t getPhoneState()
710    {
711        Parcel data, reply;
712        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
713        status_t status = remote()->transact(GET_PHONE_STATE, data, &reply);
714        if (status != NO_ERROR) {
715            return AUDIO_MODE_INVALID;
716        }
717        return (audio_mode_t)reply.readInt32();
718    }
719
720    virtual status_t registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
721    {
722        Parcel data, reply;
723        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
724        data.writeInt32(registration ? 1 : 0);
725        size_t size = mixes.size();
726        if (size > MAX_MIXES_PER_POLICY) {
727            size = MAX_MIXES_PER_POLICY;
728        }
729        size_t sizePosition = data.dataPosition();
730        data.writeInt32(size);
731        size_t finalSize = size;
732        for (size_t i = 0; i < size; i++) {
733            size_t position = data.dataPosition();
734            if (mixes[i].writeToParcel(&data) != NO_ERROR) {
735                data.setDataPosition(position);
736                finalSize--;
737            }
738        }
739        if (size != finalSize) {
740            size_t position = data.dataPosition();
741            data.setDataPosition(sizePosition);
742            data.writeInt32(finalSize);
743            data.setDataPosition(position);
744        }
745        status_t status = remote()->transact(REGISTER_POLICY_MIXES, data, &reply);
746        if (status == NO_ERROR) {
747            status = (status_t)reply.readInt32();
748        }
749        return status;
750    }
751
752    virtual status_t startAudioSource(const struct audio_port_config *source,
753                                      const audio_attributes_t *attributes,
754                                      audio_patch_handle_t *handle)
755    {
756        Parcel data, reply;
757        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
758        if (source == NULL || attributes == NULL || handle == NULL) {
759            return BAD_VALUE;
760        }
761        data.write(source, sizeof(struct audio_port_config));
762        data.write(attributes, sizeof(audio_attributes_t));
763        status_t status = remote()->transact(START_AUDIO_SOURCE, data, &reply);
764        if (status != NO_ERROR) {
765            return status;
766        }
767        status = (status_t)reply.readInt32();
768        if (status != NO_ERROR) {
769            return status;
770        }
771        *handle = (audio_patch_handle_t)reply.readInt32();
772        return status;
773    }
774
775    virtual status_t stopAudioSource(audio_patch_handle_t handle)
776    {
777        Parcel data, reply;
778        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
779        data.writeInt32(handle);
780        status_t status = remote()->transact(STOP_AUDIO_SOURCE, data, &reply);
781        if (status != NO_ERROR) {
782            return status;
783        }
784        status = (status_t)reply.readInt32();
785        return status;
786    }
787
788    virtual status_t setMasterMono(bool mono)
789    {
790        Parcel data, reply;
791        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
792        data.writeInt32(static_cast<int32_t>(mono));
793        status_t status = remote()->transact(SET_MASTER_MONO, data, &reply);
794        if (status != NO_ERROR) {
795            return status;
796        }
797        return static_cast<status_t>(reply.readInt32());
798    }
799
800    virtual status_t getMasterMono(bool *mono)
801    {
802        if (mono == nullptr) {
803            return BAD_VALUE;
804        }
805        Parcel data, reply;
806        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
807
808        status_t status = remote()->transact(GET_MASTER_MONO, data, &reply);
809        if (status != NO_ERROR) {
810            return status;
811        }
812        status = static_cast<status_t>(reply.readInt32());
813        if (status == NO_ERROR) {
814            *mono = static_cast<bool>(reply.readInt32());
815        }
816        return status;
817    }
818};
819
820IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
821
822// ----------------------------------------------------------------------
823
824
825status_t BnAudioPolicyService::onTransact(
826    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
827{
828    switch (code) {
829        case SET_DEVICE_CONNECTION_STATE: {
830            CHECK_INTERFACE(IAudioPolicyService, data, reply);
831            audio_devices_t device =
832                    static_cast <audio_devices_t>(data.readInt32());
833            audio_policy_dev_state_t state =
834                    static_cast <audio_policy_dev_state_t>(data.readInt32());
835            const char *device_address = data.readCString();
836            const char *device_name = data.readCString();
837            if (device_address == nullptr || device_name == nullptr) {
838                ALOGE("Bad Binder transaction: SET_DEVICE_CONNECTION_STATE for device %u", device);
839                reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
840            } else {
841                reply->writeInt32(static_cast<uint32_t> (setDeviceConnectionState(device,
842                                                                                  state,
843                                                                                  device_address,
844                                                                                  device_name)));
845            }
846            return NO_ERROR;
847        } break;
848
849        case GET_DEVICE_CONNECTION_STATE: {
850            CHECK_INTERFACE(IAudioPolicyService, data, reply);
851            audio_devices_t device =
852                    static_cast<audio_devices_t> (data.readInt32());
853            const char *device_address = data.readCString();
854            if (device_address == nullptr) {
855                ALOGE("Bad Binder transaction: GET_DEVICE_CONNECTION_STATE for device %u", device);
856                reply->writeInt32(static_cast<int32_t> (AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE));
857            } else {
858                reply->writeInt32(static_cast<uint32_t> (getDeviceConnectionState(device,
859                                                                                  device_address)));
860            }
861            return NO_ERROR;
862        } break;
863
864        case HANDLE_DEVICE_CONFIG_CHANGE: {
865            CHECK_INTERFACE(IAudioPolicyService, data, reply);
866            audio_devices_t device =
867                    static_cast <audio_devices_t>(data.readInt32());
868            const char *device_address = data.readCString();
869            const char *device_name = data.readCString();
870            if (device_address == nullptr || device_name == nullptr) {
871                ALOGE("Bad Binder transaction: HANDLE_DEVICE_CONFIG_CHANGE for device %u", device);
872                reply->writeInt32(static_cast<int32_t> (BAD_VALUE));
873            } else {
874                reply->writeInt32(static_cast<uint32_t> (handleDeviceConfigChange(device,
875                                                                                  device_address,
876                                                                                  device_name)));
877            }
878            return NO_ERROR;
879        } break;
880
881        case SET_PHONE_STATE: {
882            CHECK_INTERFACE(IAudioPolicyService, data, reply);
883            reply->writeInt32(static_cast <uint32_t>(setPhoneState(
884                    (audio_mode_t) data.readInt32())));
885            return NO_ERROR;
886        } break;
887
888        case SET_FORCE_USE: {
889            CHECK_INTERFACE(IAudioPolicyService, data, reply);
890            audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
891                    data.readInt32());
892            audio_policy_forced_cfg_t config =
893                    static_cast <audio_policy_forced_cfg_t>(data.readInt32());
894            reply->writeInt32(static_cast <uint32_t>(setForceUse(usage, config)));
895            return NO_ERROR;
896        } break;
897
898        case GET_FORCE_USE: {
899            CHECK_INTERFACE(IAudioPolicyService, data, reply);
900            audio_policy_force_use_t usage = static_cast <audio_policy_force_use_t>(
901                    data.readInt32());
902            reply->writeInt32(static_cast <uint32_t>(getForceUse(usage)));
903            return NO_ERROR;
904        } break;
905
906        case GET_OUTPUT: {
907            CHECK_INTERFACE(IAudioPolicyService, data, reply);
908            audio_stream_type_t stream =
909                    static_cast <audio_stream_type_t>(data.readInt32());
910            uint32_t samplingRate = data.readInt32();
911            audio_format_t format = (audio_format_t) data.readInt32();
912            audio_channel_mask_t channelMask = data.readInt32();
913            audio_output_flags_t flags =
914                    static_cast <audio_output_flags_t>(data.readInt32());
915            bool hasOffloadInfo = data.readInt32() != 0;
916            audio_offload_info_t offloadInfo;
917            if (hasOffloadInfo) {
918                data.read(&offloadInfo, sizeof(audio_offload_info_t));
919            }
920            audio_io_handle_t output = getOutput(stream,
921                                                 samplingRate,
922                                                 format,
923                                                 channelMask,
924                                                 flags,
925                                                 hasOffloadInfo ? &offloadInfo : NULL);
926            reply->writeInt32(static_cast <int>(output));
927            return NO_ERROR;
928        } break;
929
930        case GET_OUTPUT_FOR_ATTR: {
931            CHECK_INTERFACE(IAudioPolicyService, data, reply);
932            audio_attributes_t attr;
933            bool hasAttributes = data.readInt32() != 0;
934            if (hasAttributes) {
935                data.read(&attr, sizeof(audio_attributes_t));
936            }
937            audio_session_t session = (audio_session_t)data.readInt32();
938            audio_stream_type_t stream = AUDIO_STREAM_DEFAULT;
939            bool hasStream = data.readInt32() != 0;
940            if (hasStream) {
941                stream = (audio_stream_type_t)data.readInt32();
942            }
943            uid_t uid = (uid_t)data.readInt32();
944            audio_config_t config;
945            memset(&config, 0, sizeof(audio_config_t));
946            data.read(&config, sizeof(audio_config_t));
947            audio_output_flags_t flags =
948                    static_cast <audio_output_flags_t>(data.readInt32());
949            audio_port_handle_t selectedDeviceId = data.readInt32();
950            audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
951            audio_io_handle_t output = 0;
952            status_t status = getOutputForAttr(hasAttributes ? &attr : NULL,
953                    &output, session, &stream, uid,
954                    &config,
955                    flags, selectedDeviceId, &portId);
956            reply->writeInt32(status);
957            reply->writeInt32(output);
958            reply->writeInt32(stream);
959            reply->writeInt32(portId);
960            return NO_ERROR;
961        } break;
962
963        case START_OUTPUT: {
964            CHECK_INTERFACE(IAudioPolicyService, data, reply);
965            audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
966            audio_stream_type_t stream =
967                                static_cast <audio_stream_type_t>(data.readInt32());
968            audio_session_t session = (audio_session_t)data.readInt32();
969            reply->writeInt32(static_cast <uint32_t>(startOutput(output,
970                                                                 stream,
971                                                                 session)));
972            return NO_ERROR;
973        } break;
974
975        case STOP_OUTPUT: {
976            CHECK_INTERFACE(IAudioPolicyService, data, reply);
977            audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
978            audio_stream_type_t stream =
979                                static_cast <audio_stream_type_t>(data.readInt32());
980            audio_session_t session = (audio_session_t)data.readInt32();
981            reply->writeInt32(static_cast <uint32_t>(stopOutput(output,
982                                                                stream,
983                                                                session)));
984            return NO_ERROR;
985        } break;
986
987        case RELEASE_OUTPUT: {
988            CHECK_INTERFACE(IAudioPolicyService, data, reply);
989            audio_io_handle_t output = static_cast <audio_io_handle_t>(data.readInt32());
990            audio_stream_type_t stream = (audio_stream_type_t)data.readInt32();
991            audio_session_t session = (audio_session_t)data.readInt32();
992            releaseOutput(output, stream, session);
993            return NO_ERROR;
994        } break;
995
996        case GET_INPUT_FOR_ATTR: {
997            CHECK_INTERFACE(IAudioPolicyService, data, reply);
998            audio_attributes_t attr;
999            data.read(&attr, sizeof(audio_attributes_t));
1000            audio_session_t session = (audio_session_t)data.readInt32();
1001            pid_t pid = (pid_t)data.readInt32();
1002            uid_t uid = (uid_t)data.readInt32();
1003            audio_config_base_t config;
1004            memset(&config, 0, sizeof(audio_config_base_t));
1005            data.read(&config, sizeof(audio_config_base_t));
1006            audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
1007            audio_port_handle_t selectedDeviceId = (audio_port_handle_t) data.readInt32();
1008            audio_port_handle_t portId = (audio_port_handle_t)data.readInt32();
1009            audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
1010            status_t status = getInputForAttr(&attr, &input, session, pid, uid,
1011                                              &config,
1012                                              flags, selectedDeviceId, &portId);
1013            reply->writeInt32(status);
1014            if (status == NO_ERROR) {
1015                reply->writeInt32(input);
1016                reply->writeInt32(portId);
1017            }
1018            return NO_ERROR;
1019        } break;
1020
1021        case START_INPUT: {
1022            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1023            audio_io_handle_t input = static_cast <audio_io_handle_t>(data.readInt32());
1024            audio_session_t session = static_cast <audio_session_t>(data.readInt32());
1025            reply->writeInt32(static_cast <uint32_t>(startInput(input, session)));
1026            return NO_ERROR;
1027        } break;
1028
1029        case STOP_INPUT: {
1030            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1031            audio_io_handle_t input = static_cast <audio_io_handle_t>(data.readInt32());
1032            audio_session_t session = static_cast <audio_session_t>(data.readInt32());
1033            reply->writeInt32(static_cast <uint32_t>(stopInput(input, session)));
1034            return NO_ERROR;
1035        } break;
1036
1037        case RELEASE_INPUT: {
1038            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1039            audio_io_handle_t input = static_cast <audio_io_handle_t>(data.readInt32());
1040            audio_session_t session = static_cast <audio_session_t>(data.readInt32());
1041            releaseInput(input, session);
1042            return NO_ERROR;
1043        } break;
1044
1045        case INIT_STREAM_VOLUME: {
1046            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1047            audio_stream_type_t stream =
1048                    static_cast <audio_stream_type_t>(data.readInt32());
1049            int indexMin = data.readInt32();
1050            int indexMax = data.readInt32();
1051            reply->writeInt32(static_cast <uint32_t>(initStreamVolume(stream, indexMin,indexMax)));
1052            return NO_ERROR;
1053        } break;
1054
1055        case SET_STREAM_VOLUME: {
1056            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1057            audio_stream_type_t stream =
1058                    static_cast <audio_stream_type_t>(data.readInt32());
1059            int index = data.readInt32();
1060            audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1061            reply->writeInt32(static_cast <uint32_t>(setStreamVolumeIndex(stream,
1062                                                                          index,
1063                                                                          device)));
1064            return NO_ERROR;
1065        } break;
1066
1067        case GET_STREAM_VOLUME: {
1068            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1069            audio_stream_type_t stream =
1070                    static_cast <audio_stream_type_t>(data.readInt32());
1071            audio_devices_t device = static_cast <audio_devices_t>(data.readInt32());
1072            int index = 0;
1073            status_t status = getStreamVolumeIndex(stream, &index, device);
1074            reply->writeInt32(index);
1075            reply->writeInt32(static_cast <uint32_t>(status));
1076            return NO_ERROR;
1077        } break;
1078
1079        case GET_STRATEGY_FOR_STREAM: {
1080            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1081            audio_stream_type_t stream =
1082                    static_cast <audio_stream_type_t>(data.readInt32());
1083            reply->writeInt32(getStrategyForStream(stream));
1084            return NO_ERROR;
1085        } break;
1086
1087        case GET_DEVICES_FOR_STREAM: {
1088            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1089            audio_stream_type_t stream =
1090                    static_cast <audio_stream_type_t>(data.readInt32());
1091            reply->writeInt32(static_cast <int>(getDevicesForStream(stream)));
1092            return NO_ERROR;
1093        } break;
1094
1095        case GET_OUTPUT_FOR_EFFECT: {
1096            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1097            effect_descriptor_t desc;
1098            data.read(&desc, sizeof(effect_descriptor_t));
1099            audio_io_handle_t output = getOutputForEffect(&desc);
1100            reply->writeInt32(static_cast <int>(output));
1101            return NO_ERROR;
1102        } break;
1103
1104        case REGISTER_EFFECT: {
1105            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1106            effect_descriptor_t desc;
1107            data.read(&desc, sizeof(effect_descriptor_t));
1108            audio_io_handle_t io = data.readInt32();
1109            uint32_t strategy = data.readInt32();
1110            audio_session_t session = (audio_session_t) data.readInt32();
1111            int id = data.readInt32();
1112            reply->writeInt32(static_cast <int32_t>(registerEffect(&desc,
1113                                                                   io,
1114                                                                   strategy,
1115                                                                   session,
1116                                                                   id)));
1117            return NO_ERROR;
1118        } break;
1119
1120        case UNREGISTER_EFFECT: {
1121            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1122            int id = data.readInt32();
1123            reply->writeInt32(static_cast <int32_t>(unregisterEffect(id)));
1124            return NO_ERROR;
1125        } break;
1126
1127        case SET_EFFECT_ENABLED: {
1128            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1129            int id = data.readInt32();
1130            bool enabled = static_cast <bool>(data.readInt32());
1131            reply->writeInt32(static_cast <int32_t>(setEffectEnabled(id, enabled)));
1132            return NO_ERROR;
1133        } break;
1134
1135        case IS_STREAM_ACTIVE: {
1136            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1137            audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1138            uint32_t inPastMs = (uint32_t)data.readInt32();
1139            reply->writeInt32( isStreamActive(stream, inPastMs) );
1140            return NO_ERROR;
1141        } break;
1142
1143        case IS_STREAM_ACTIVE_REMOTELY: {
1144            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1145            audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1146            uint32_t inPastMs = (uint32_t)data.readInt32();
1147            reply->writeInt32( isStreamActiveRemotely(stream, inPastMs) );
1148            return NO_ERROR;
1149        } break;
1150
1151        case IS_SOURCE_ACTIVE: {
1152            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1153            audio_source_t source = (audio_source_t) data.readInt32();
1154            reply->writeInt32( isSourceActive(source));
1155            return NO_ERROR;
1156        }
1157
1158        case QUERY_DEFAULT_PRE_PROCESSING: {
1159            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1160            audio_session_t audioSession = (audio_session_t) data.readInt32();
1161            uint32_t count = data.readInt32();
1162            if (count > AudioEffect::kMaxPreProcessing) {
1163                count = AudioEffect::kMaxPreProcessing;
1164            }
1165            uint32_t retCount = count;
1166            effect_descriptor_t *descriptors = new effect_descriptor_t[count];
1167            status_t status = queryDefaultPreProcessing(audioSession, descriptors, &retCount);
1168            reply->writeInt32(status);
1169            if (status != NO_ERROR && status != NO_MEMORY) {
1170                retCount = 0;
1171            }
1172            reply->writeInt32(retCount);
1173            if (retCount != 0) {
1174                if (retCount < count) {
1175                    count = retCount;
1176                }
1177                reply->write(descriptors, sizeof(effect_descriptor_t) * count);
1178            }
1179            delete[] descriptors;
1180            return status;
1181        }
1182
1183        case IS_OFFLOAD_SUPPORTED: {
1184            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1185            audio_offload_info_t info;
1186            data.read(&info, sizeof(audio_offload_info_t));
1187            bool isSupported = isOffloadSupported(info);
1188            reply->writeInt32(isSupported);
1189            return NO_ERROR;
1190        }
1191
1192        case LIST_AUDIO_PORTS: {
1193            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1194            audio_port_role_t role = (audio_port_role_t)data.readInt32();
1195            audio_port_type_t type = (audio_port_type_t)data.readInt32();
1196            unsigned int numPortsReq = data.readInt32();
1197            if (numPortsReq > MAX_ITEMS_PER_LIST) {
1198                numPortsReq = MAX_ITEMS_PER_LIST;
1199            }
1200            unsigned int numPorts = numPortsReq;
1201            struct audio_port *ports =
1202                    (struct audio_port *)calloc(numPortsReq, sizeof(struct audio_port));
1203            if (ports == NULL) {
1204                reply->writeInt32(NO_MEMORY);
1205                reply->writeInt32(0);
1206                return NO_ERROR;
1207            }
1208            unsigned int generation;
1209            status_t status = listAudioPorts(role, type, &numPorts, ports, &generation);
1210            reply->writeInt32(status);
1211            reply->writeInt32(numPorts);
1212
1213            if (status == NO_ERROR) {
1214                if (numPortsReq > numPorts) {
1215                    numPortsReq = numPorts;
1216                }
1217                reply->write(ports, numPortsReq * sizeof(struct audio_port));
1218                reply->writeInt32(generation);
1219            }
1220            free(ports);
1221            return NO_ERROR;
1222        }
1223
1224        case GET_AUDIO_PORT: {
1225            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1226            struct audio_port port = {};
1227            if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1228                ALOGE("b/23912202");
1229            }
1230            status_t status = getAudioPort(&port);
1231            reply->writeInt32(status);
1232            if (status == NO_ERROR) {
1233                reply->write(&port, sizeof(struct audio_port));
1234            }
1235            return NO_ERROR;
1236        }
1237
1238        case CREATE_AUDIO_PATCH: {
1239            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1240            struct audio_patch patch;
1241            data.read(&patch, sizeof(struct audio_patch));
1242            audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
1243            if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1244                ALOGE("b/23912202");
1245            }
1246            status_t status = createAudioPatch(&patch, &handle);
1247            reply->writeInt32(status);
1248            if (status == NO_ERROR) {
1249                reply->write(&handle, sizeof(audio_patch_handle_t));
1250            }
1251            return NO_ERROR;
1252        }
1253
1254        case RELEASE_AUDIO_PATCH: {
1255            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1256            audio_patch_handle_t handle;
1257            data.read(&handle, sizeof(audio_patch_handle_t));
1258            status_t status = releaseAudioPatch(handle);
1259            reply->writeInt32(status);
1260            return NO_ERROR;
1261        }
1262
1263        case LIST_AUDIO_PATCHES: {
1264            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1265            unsigned int numPatchesReq = data.readInt32();
1266            if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1267                numPatchesReq = MAX_ITEMS_PER_LIST;
1268            }
1269            unsigned int numPatches = numPatchesReq;
1270            struct audio_patch *patches =
1271                    (struct audio_patch *)calloc(numPatchesReq,
1272                                                 sizeof(struct audio_patch));
1273            if (patches == NULL) {
1274                reply->writeInt32(NO_MEMORY);
1275                reply->writeInt32(0);
1276                return NO_ERROR;
1277            }
1278            unsigned int generation;
1279            status_t status = listAudioPatches(&numPatches, patches, &generation);
1280            reply->writeInt32(status);
1281            reply->writeInt32(numPatches);
1282            if (status == NO_ERROR) {
1283                if (numPatchesReq > numPatches) {
1284                    numPatchesReq = numPatches;
1285                }
1286                reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1287                reply->writeInt32(generation);
1288            }
1289            free(patches);
1290            return NO_ERROR;
1291        }
1292
1293        case SET_AUDIO_PORT_CONFIG: {
1294            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1295            struct audio_port_config config;
1296            data.read(&config, sizeof(struct audio_port_config));
1297            status_t status = setAudioPortConfig(&config);
1298            reply->writeInt32(status);
1299            return NO_ERROR;
1300        }
1301
1302        case REGISTER_CLIENT: {
1303            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1304            sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1305                    data.readStrongBinder());
1306            registerClient(client);
1307            return NO_ERROR;
1308        } break;
1309
1310        case SET_AUDIO_PORT_CALLBACK_ENABLED: {
1311            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1312            setAudioPortCallbacksEnabled(data.readInt32() == 1);
1313            return NO_ERROR;
1314        } break;
1315
1316        case ACQUIRE_SOUNDTRIGGER_SESSION: {
1317            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1318            sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1319                    data.readStrongBinder());
1320            audio_session_t session = AUDIO_SESSION_NONE;
1321            audio_io_handle_t ioHandle = AUDIO_IO_HANDLE_NONE;
1322            audio_devices_t device = AUDIO_DEVICE_NONE;
1323            status_t status = acquireSoundTriggerSession(&session, &ioHandle, &device);
1324            reply->writeInt32(status);
1325            if (status == NO_ERROR) {
1326                reply->writeInt32(session);
1327                reply->writeInt32(ioHandle);
1328                reply->writeInt32(device);
1329            }
1330            return NO_ERROR;
1331        } break;
1332
1333        case RELEASE_SOUNDTRIGGER_SESSION: {
1334            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1335            sp<IAudioPolicyServiceClient> client = interface_cast<IAudioPolicyServiceClient>(
1336                    data.readStrongBinder());
1337            audio_session_t session = (audio_session_t)data.readInt32();
1338            status_t status = releaseSoundTriggerSession(session);
1339            reply->writeInt32(status);
1340            return NO_ERROR;
1341        } break;
1342
1343        case GET_PHONE_STATE: {
1344            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1345            reply->writeInt32((int32_t)getPhoneState());
1346            return NO_ERROR;
1347        } break;
1348
1349        case REGISTER_POLICY_MIXES: {
1350            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1351            bool registration = data.readInt32() == 1;
1352            Vector<AudioMix> mixes;
1353            size_t size = (size_t)data.readInt32();
1354            if (size > MAX_MIXES_PER_POLICY) {
1355                size = MAX_MIXES_PER_POLICY;
1356            }
1357            for (size_t i = 0; i < size; i++) {
1358                AudioMix mix;
1359                if (mix.readFromParcel((Parcel*)&data) == NO_ERROR) {
1360                    mixes.add(mix);
1361                }
1362            }
1363            status_t status = registerPolicyMixes(mixes, registration);
1364            reply->writeInt32(status);
1365            return NO_ERROR;
1366        } break;
1367
1368        case START_AUDIO_SOURCE: {
1369            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1370            struct audio_port_config source;
1371            data.read(&source, sizeof(struct audio_port_config));
1372            audio_attributes_t attributes;
1373            data.read(&attributes, sizeof(audio_attributes_t));
1374            audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
1375            status_t status = startAudioSource(&source, &attributes, &handle);
1376            reply->writeInt32(status);
1377            reply->writeInt32(handle);
1378            return NO_ERROR;
1379        } break;
1380
1381        case STOP_AUDIO_SOURCE: {
1382            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1383            audio_patch_handle_t handle = (audio_patch_handle_t) data.readInt32();
1384            status_t status = stopAudioSource(handle);
1385            reply->writeInt32(status);
1386            return NO_ERROR;
1387        } break;
1388
1389        case SET_MASTER_MONO: {
1390            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1391            bool mono = static_cast<bool>(data.readInt32());
1392            status_t status = setMasterMono(mono);
1393            reply->writeInt32(status);
1394            return NO_ERROR;
1395        } break;
1396
1397        case GET_MASTER_MONO: {
1398            CHECK_INTERFACE(IAudioPolicyService, data, reply);
1399            bool mono;
1400            status_t status = getMasterMono(&mono);
1401            reply->writeInt32(status);
1402            if (status == NO_ERROR) {
1403                reply->writeInt32(static_cast<int32_t>(mono));
1404            }
1405            return NO_ERROR;
1406        } break;
1407
1408        default:
1409            return BBinder::onTransact(code, data, reply, flags);
1410    }
1411}
1412
1413// ----------------------------------------------------------------------------
1414
1415} // namespace android
1416