1/*
2**
3** Copyright 2007, 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 "IAudioFlinger"
19//#define LOG_NDEBUG 0
20#include <utils/Log.h>
21
22#include <stdint.h>
23#include <sys/types.h>
24
25#include <binder/Parcel.h>
26
27#include "IAudioFlinger.h"
28
29namespace android {
30
31enum {
32    CREATE_TRACK = IBinder::FIRST_CALL_TRANSACTION,
33    OPEN_RECORD,
34    SAMPLE_RATE,
35    RESERVED,   // obsolete, was CHANNEL_COUNT
36    FORMAT,
37    FRAME_COUNT,
38    LATENCY,
39    SET_MASTER_VOLUME,
40    SET_MASTER_MUTE,
41    MASTER_VOLUME,
42    MASTER_MUTE,
43    SET_STREAM_VOLUME,
44    SET_STREAM_MUTE,
45    STREAM_VOLUME,
46    STREAM_MUTE,
47    SET_MODE,
48    SET_MIC_MUTE,
49    GET_MIC_MUTE,
50    SET_PARAMETERS,
51    GET_PARAMETERS,
52    REGISTER_CLIENT,
53    GET_INPUTBUFFERSIZE,
54    OPEN_OUTPUT,
55    OPEN_DUPLICATE_OUTPUT,
56    CLOSE_OUTPUT,
57    SUSPEND_OUTPUT,
58    RESTORE_OUTPUT,
59    OPEN_INPUT,
60    CLOSE_INPUT,
61    INVALIDATE_STREAM,
62    SET_VOICE_VOLUME,
63    GET_RENDER_POSITION,
64    GET_INPUT_FRAMES_LOST,
65    NEW_AUDIO_SESSION_ID,
66    ACQUIRE_AUDIO_SESSION_ID,
67    RELEASE_AUDIO_SESSION_ID,
68    QUERY_NUM_EFFECTS,
69    QUERY_EFFECT,
70    GET_EFFECT_DESCRIPTOR,
71    CREATE_EFFECT,
72    MOVE_EFFECTS,
73    LOAD_HW_MODULE,
74    GET_PRIMARY_OUTPUT_SAMPLING_RATE,
75    GET_PRIMARY_OUTPUT_FRAME_COUNT,
76    SET_LOW_RAM_DEVICE,
77    LIST_AUDIO_PORTS,
78    GET_AUDIO_PORT,
79    CREATE_AUDIO_PATCH,
80    RELEASE_AUDIO_PATCH,
81    LIST_AUDIO_PATCHES,
82    SET_AUDIO_PORT_CONFIG,
83    GET_AUDIO_HW_SYNC,
84    SYSTEM_READY,
85    FRAME_COUNT_HAL,
86};
87
88#define MAX_ITEMS_PER_LIST 1024
89
90class BpAudioFlinger : public BpInterface<IAudioFlinger>
91{
92public:
93    explicit BpAudioFlinger(const sp<IBinder>& impl)
94        : BpInterface<IAudioFlinger>(impl)
95    {
96    }
97
98    virtual sp<IAudioTrack> createTrack(
99                                audio_stream_type_t streamType,
100                                uint32_t sampleRate,
101                                audio_format_t format,
102                                audio_channel_mask_t channelMask,
103                                size_t *pFrameCount,
104                                audio_output_flags_t *flags,
105                                const sp<IMemory>& sharedBuffer,
106                                audio_io_handle_t output,
107                                pid_t pid,
108                                pid_t tid,
109                                audio_session_t *sessionId,
110                                int clientUid,
111                                status_t *status,
112                                audio_port_handle_t portId)
113    {
114        Parcel data, reply;
115        sp<IAudioTrack> track;
116        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
117        data.writeInt32((int32_t) streamType);
118        data.writeInt32(sampleRate);
119        data.writeInt32(format);
120        data.writeInt32(channelMask);
121        size_t frameCount = pFrameCount != NULL ? *pFrameCount : 0;
122        data.writeInt64(frameCount);
123        audio_output_flags_t lFlags = flags != NULL ? *flags : AUDIO_OUTPUT_FLAG_NONE;
124        data.writeInt32(lFlags);
125        // haveSharedBuffer
126        if (sharedBuffer != 0) {
127            data.writeInt32(true);
128            data.writeStrongBinder(IInterface::asBinder(sharedBuffer));
129        } else {
130            data.writeInt32(false);
131        }
132        data.writeInt32((int32_t) output);
133        data.writeInt32((int32_t) pid);
134        data.writeInt32((int32_t) tid);
135        audio_session_t lSessionId = AUDIO_SESSION_ALLOCATE;
136        if (sessionId != NULL) {
137            lSessionId = *sessionId;
138        }
139        data.writeInt32(lSessionId);
140        data.writeInt32(clientUid);
141        data.writeInt32(portId);
142        status_t lStatus = remote()->transact(CREATE_TRACK, data, &reply);
143        if (lStatus != NO_ERROR) {
144            ALOGE("createTrack error: %s", strerror(-lStatus));
145        } else {
146            frameCount = reply.readInt64();
147            if (pFrameCount != NULL) {
148                *pFrameCount = frameCount;
149            }
150            lFlags = (audio_output_flags_t)reply.readInt32();
151            if (flags != NULL) {
152                *flags = lFlags;
153            }
154            lSessionId = (audio_session_t) reply.readInt32();
155            if (sessionId != NULL) {
156                *sessionId = lSessionId;
157            }
158            lStatus = reply.readInt32();
159            track = interface_cast<IAudioTrack>(reply.readStrongBinder());
160            if (lStatus == NO_ERROR) {
161                if (track == 0) {
162                    ALOGE("createTrack should have returned an IAudioTrack");
163                    lStatus = UNKNOWN_ERROR;
164                }
165            } else {
166                if (track != 0) {
167                    ALOGE("createTrack returned an IAudioTrack but with status %d", lStatus);
168                    track.clear();
169                }
170            }
171        }
172        if (status != NULL) {
173            *status = lStatus;
174        }
175        return track;
176    }
177
178    virtual sp<IAudioRecord> openRecord(
179                                audio_io_handle_t input,
180                                uint32_t sampleRate,
181                                audio_format_t format,
182                                audio_channel_mask_t channelMask,
183                                const String16& opPackageName,
184                                size_t *pFrameCount,
185                                audio_input_flags_t *flags,
186                                pid_t pid,
187                                pid_t tid,
188                                int clientUid,
189                                audio_session_t *sessionId,
190                                size_t *notificationFrames,
191                                sp<IMemory>& cblk,
192                                sp<IMemory>& buffers,
193                                status_t *status,
194                                audio_port_handle_t portId)
195    {
196        Parcel data, reply;
197        sp<IAudioRecord> record;
198        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
199        data.writeInt32((int32_t) input);
200        data.writeInt32(sampleRate);
201        data.writeInt32(format);
202        data.writeInt32(channelMask);
203        data.writeString16(opPackageName);
204        size_t frameCount = pFrameCount != NULL ? *pFrameCount : 0;
205        data.writeInt64(frameCount);
206        audio_input_flags_t lFlags = flags != NULL ? *flags : AUDIO_INPUT_FLAG_NONE;
207        data.writeInt32(lFlags);
208        data.writeInt32((int32_t) pid);
209        data.writeInt32((int32_t) tid);
210        data.writeInt32((int32_t) clientUid);
211        audio_session_t lSessionId = AUDIO_SESSION_ALLOCATE;
212        if (sessionId != NULL) {
213            lSessionId = *sessionId;
214        }
215        data.writeInt32(lSessionId);
216        data.writeInt64(notificationFrames != NULL ? *notificationFrames : 0);
217        data.writeInt32(portId);
218        cblk.clear();
219        buffers.clear();
220        status_t lStatus = remote()->transact(OPEN_RECORD, data, &reply);
221        if (lStatus != NO_ERROR) {
222            ALOGE("openRecord error: %s", strerror(-lStatus));
223        } else {
224            frameCount = reply.readInt64();
225            if (pFrameCount != NULL) {
226                *pFrameCount = frameCount;
227            }
228            lFlags = (audio_input_flags_t)reply.readInt32();
229            if (flags != NULL) {
230                *flags = lFlags;
231            }
232            lSessionId = (audio_session_t) reply.readInt32();
233            if (sessionId != NULL) {
234                *sessionId = lSessionId;
235            }
236            size_t lNotificationFrames = (size_t) reply.readInt64();
237            if (notificationFrames != NULL) {
238                *notificationFrames = lNotificationFrames;
239            }
240            lStatus = reply.readInt32();
241            record = interface_cast<IAudioRecord>(reply.readStrongBinder());
242            cblk = interface_cast<IMemory>(reply.readStrongBinder());
243            if (cblk != 0 && cblk->pointer() == NULL) {
244                cblk.clear();
245            }
246            buffers = interface_cast<IMemory>(reply.readStrongBinder());
247            if (buffers != 0 && buffers->pointer() == NULL) {
248                buffers.clear();
249            }
250            if (lStatus == NO_ERROR) {
251                if (record == 0) {
252                    ALOGE("openRecord should have returned an IAudioRecord");
253                    lStatus = UNKNOWN_ERROR;
254                } else if (cblk == 0) {
255                    ALOGE("openRecord should have returned a cblk");
256                    lStatus = NO_MEMORY;
257                }
258                // buffers is permitted to be 0
259            } else {
260                if (record != 0 || cblk != 0 || buffers != 0) {
261                    ALOGE("openRecord returned an IAudioRecord, cblk, "
262                          "or buffers but with status %d", lStatus);
263                }
264            }
265            if (lStatus != NO_ERROR) {
266                record.clear();
267                cblk.clear();
268                buffers.clear();
269            }
270        }
271        if (status != NULL) {
272            *status = lStatus;
273        }
274        return record;
275    }
276
277    virtual uint32_t sampleRate(audio_io_handle_t ioHandle) const
278    {
279        Parcel data, reply;
280        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
281        data.writeInt32((int32_t) ioHandle);
282        remote()->transact(SAMPLE_RATE, data, &reply);
283        return reply.readInt32();
284    }
285
286    // RESERVED for channelCount()
287
288    virtual audio_format_t format(audio_io_handle_t output) const
289    {
290        Parcel data, reply;
291        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
292        data.writeInt32((int32_t) output);
293        remote()->transact(FORMAT, data, &reply);
294        return (audio_format_t) reply.readInt32();
295    }
296
297    virtual size_t frameCount(audio_io_handle_t ioHandle) const
298    {
299        Parcel data, reply;
300        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
301        data.writeInt32((int32_t) ioHandle);
302        remote()->transact(FRAME_COUNT, data, &reply);
303        return reply.readInt64();
304    }
305
306    virtual uint32_t latency(audio_io_handle_t output) const
307    {
308        Parcel data, reply;
309        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
310        data.writeInt32((int32_t) output);
311        remote()->transact(LATENCY, data, &reply);
312        return reply.readInt32();
313    }
314
315    virtual status_t setMasterVolume(float value)
316    {
317        Parcel data, reply;
318        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
319        data.writeFloat(value);
320        remote()->transact(SET_MASTER_VOLUME, data, &reply);
321        return reply.readInt32();
322    }
323
324    virtual status_t setMasterMute(bool muted)
325    {
326        Parcel data, reply;
327        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
328        data.writeInt32(muted);
329        remote()->transact(SET_MASTER_MUTE, data, &reply);
330        return reply.readInt32();
331    }
332
333    virtual float masterVolume() const
334    {
335        Parcel data, reply;
336        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
337        remote()->transact(MASTER_VOLUME, data, &reply);
338        return reply.readFloat();
339    }
340
341    virtual bool masterMute() const
342    {
343        Parcel data, reply;
344        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
345        remote()->transact(MASTER_MUTE, data, &reply);
346        return reply.readInt32();
347    }
348
349    virtual status_t setStreamVolume(audio_stream_type_t stream, float value,
350            audio_io_handle_t output)
351    {
352        Parcel data, reply;
353        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
354        data.writeInt32((int32_t) stream);
355        data.writeFloat(value);
356        data.writeInt32((int32_t) output);
357        remote()->transact(SET_STREAM_VOLUME, data, &reply);
358        return reply.readInt32();
359    }
360
361    virtual status_t setStreamMute(audio_stream_type_t stream, bool muted)
362    {
363        Parcel data, reply;
364        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
365        data.writeInt32((int32_t) stream);
366        data.writeInt32(muted);
367        remote()->transact(SET_STREAM_MUTE, data, &reply);
368        return reply.readInt32();
369    }
370
371    virtual float streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
372    {
373        Parcel data, reply;
374        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
375        data.writeInt32((int32_t) stream);
376        data.writeInt32((int32_t) output);
377        remote()->transact(STREAM_VOLUME, data, &reply);
378        return reply.readFloat();
379    }
380
381    virtual bool streamMute(audio_stream_type_t stream) const
382    {
383        Parcel data, reply;
384        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
385        data.writeInt32((int32_t) stream);
386        remote()->transact(STREAM_MUTE, data, &reply);
387        return reply.readInt32();
388    }
389
390    virtual status_t setMode(audio_mode_t mode)
391    {
392        Parcel data, reply;
393        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
394        data.writeInt32(mode);
395        remote()->transact(SET_MODE, data, &reply);
396        return reply.readInt32();
397    }
398
399    virtual status_t setMicMute(bool state)
400    {
401        Parcel data, reply;
402        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
403        data.writeInt32(state);
404        remote()->transact(SET_MIC_MUTE, data, &reply);
405        return reply.readInt32();
406    }
407
408    virtual bool getMicMute() const
409    {
410        Parcel data, reply;
411        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
412        remote()->transact(GET_MIC_MUTE, data, &reply);
413        return reply.readInt32();
414    }
415
416    virtual status_t setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
417    {
418        Parcel data, reply;
419        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
420        data.writeInt32((int32_t) ioHandle);
421        data.writeString8(keyValuePairs);
422        remote()->transact(SET_PARAMETERS, data, &reply);
423        return reply.readInt32();
424    }
425
426    virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) const
427    {
428        Parcel data, reply;
429        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
430        data.writeInt32((int32_t) ioHandle);
431        data.writeString8(keys);
432        remote()->transact(GET_PARAMETERS, data, &reply);
433        return reply.readString8();
434    }
435
436    virtual void registerClient(const sp<IAudioFlingerClient>& client)
437    {
438        Parcel data, reply;
439        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
440        data.writeStrongBinder(IInterface::asBinder(client));
441        remote()->transact(REGISTER_CLIENT, data, &reply);
442    }
443
444    virtual size_t getInputBufferSize(uint32_t sampleRate, audio_format_t format,
445            audio_channel_mask_t channelMask) const
446    {
447        Parcel data, reply;
448        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
449        data.writeInt32(sampleRate);
450        data.writeInt32(format);
451        data.writeInt32(channelMask);
452        remote()->transact(GET_INPUTBUFFERSIZE, data, &reply);
453        return reply.readInt64();
454    }
455
456    virtual status_t openOutput(audio_module_handle_t module,
457                                audio_io_handle_t *output,
458                                audio_config_t *config,
459                                audio_devices_t *devices,
460                                const String8& address,
461                                uint32_t *latencyMs,
462                                audio_output_flags_t flags)
463    {
464        if (output == NULL || config == NULL || devices == NULL || latencyMs == NULL) {
465            return BAD_VALUE;
466        }
467        Parcel data, reply;
468        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
469        data.writeInt32(module);
470        data.write(config, sizeof(audio_config_t));
471        data.writeInt32(*devices);
472        data.writeString8(address);
473        data.writeInt32((int32_t) flags);
474        status_t status = remote()->transact(OPEN_OUTPUT, data, &reply);
475        if (status != NO_ERROR) {
476            *output = AUDIO_IO_HANDLE_NONE;
477            return status;
478        }
479        status = (status_t)reply.readInt32();
480        if (status != NO_ERROR) {
481            *output = AUDIO_IO_HANDLE_NONE;
482            return status;
483        }
484        *output = (audio_io_handle_t)reply.readInt32();
485        ALOGV("openOutput() returned output, %d", *output);
486        reply.read(config, sizeof(audio_config_t));
487        *devices = (audio_devices_t)reply.readInt32();
488        *latencyMs = reply.readInt32();
489        return NO_ERROR;
490    }
491
492    virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
493            audio_io_handle_t output2)
494    {
495        Parcel data, reply;
496        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
497        data.writeInt32((int32_t) output1);
498        data.writeInt32((int32_t) output2);
499        remote()->transact(OPEN_DUPLICATE_OUTPUT, data, &reply);
500        return (audio_io_handle_t) reply.readInt32();
501    }
502
503    virtual status_t closeOutput(audio_io_handle_t output)
504    {
505        Parcel data, reply;
506        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
507        data.writeInt32((int32_t) output);
508        remote()->transact(CLOSE_OUTPUT, data, &reply);
509        return reply.readInt32();
510    }
511
512    virtual status_t suspendOutput(audio_io_handle_t output)
513    {
514        Parcel data, reply;
515        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
516        data.writeInt32((int32_t) output);
517        remote()->transact(SUSPEND_OUTPUT, data, &reply);
518        return reply.readInt32();
519    }
520
521    virtual status_t restoreOutput(audio_io_handle_t output)
522    {
523        Parcel data, reply;
524        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
525        data.writeInt32((int32_t) output);
526        remote()->transact(RESTORE_OUTPUT, data, &reply);
527        return reply.readInt32();
528    }
529
530    virtual status_t openInput(audio_module_handle_t module,
531                               audio_io_handle_t *input,
532                               audio_config_t *config,
533                               audio_devices_t *device,
534                               const String8& address,
535                               audio_source_t source,
536                               audio_input_flags_t flags)
537    {
538        if (input == NULL || config == NULL || device == NULL) {
539            return BAD_VALUE;
540        }
541        Parcel data, reply;
542        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
543        data.writeInt32(module);
544        data.writeInt32(*input);
545        data.write(config, sizeof(audio_config_t));
546        data.writeInt32(*device);
547        data.writeString8(address);
548        data.writeInt32(source);
549        data.writeInt32(flags);
550        status_t status = remote()->transact(OPEN_INPUT, data, &reply);
551        if (status != NO_ERROR) {
552            *input = AUDIO_IO_HANDLE_NONE;
553            return status;
554        }
555        status = (status_t)reply.readInt32();
556        if (status != NO_ERROR) {
557            *input = AUDIO_IO_HANDLE_NONE;
558            return status;
559        }
560        *input = (audio_io_handle_t)reply.readInt32();
561        reply.read(config, sizeof(audio_config_t));
562        *device = (audio_devices_t)reply.readInt32();
563        return NO_ERROR;
564    }
565
566    virtual status_t closeInput(int input)
567    {
568        Parcel data, reply;
569        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
570        data.writeInt32(input);
571        remote()->transact(CLOSE_INPUT, data, &reply);
572        return reply.readInt32();
573    }
574
575    virtual status_t invalidateStream(audio_stream_type_t stream)
576    {
577        Parcel data, reply;
578        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
579        data.writeInt32((int32_t) stream);
580        remote()->transact(INVALIDATE_STREAM, data, &reply);
581        return reply.readInt32();
582    }
583
584    virtual status_t setVoiceVolume(float volume)
585    {
586        Parcel data, reply;
587        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
588        data.writeFloat(volume);
589        remote()->transact(SET_VOICE_VOLUME, data, &reply);
590        return reply.readInt32();
591    }
592
593    virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
594            audio_io_handle_t output) const
595    {
596        Parcel data, reply;
597        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
598        data.writeInt32((int32_t) output);
599        remote()->transact(GET_RENDER_POSITION, data, &reply);
600        status_t status = reply.readInt32();
601        if (status == NO_ERROR) {
602            uint32_t tmp = reply.readInt32();
603            if (halFrames != NULL) {
604                *halFrames = tmp;
605            }
606            tmp = reply.readInt32();
607            if (dspFrames != NULL) {
608                *dspFrames = tmp;
609            }
610        }
611        return status;
612    }
613
614    virtual uint32_t getInputFramesLost(audio_io_handle_t ioHandle) const
615    {
616        Parcel data, reply;
617        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
618        data.writeInt32((int32_t) ioHandle);
619        status_t status = remote()->transact(GET_INPUT_FRAMES_LOST, data, &reply);
620        if (status != NO_ERROR) {
621            return 0;
622        }
623        return (uint32_t) reply.readInt32();
624    }
625
626    virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use)
627    {
628        Parcel data, reply;
629        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
630        data.writeInt32((int32_t) use);
631        status_t status = remote()->transact(NEW_AUDIO_SESSION_ID, data, &reply);
632        audio_unique_id_t id = AUDIO_SESSION_ALLOCATE;
633        if (status == NO_ERROR) {
634            id = reply.readInt32();
635        }
636        return id;
637    }
638
639    virtual void acquireAudioSessionId(audio_session_t audioSession, int pid)
640    {
641        Parcel data, reply;
642        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
643        data.writeInt32(audioSession);
644        data.writeInt32(pid);
645        remote()->transact(ACQUIRE_AUDIO_SESSION_ID, data, &reply);
646    }
647
648    virtual void releaseAudioSessionId(audio_session_t audioSession, int pid)
649    {
650        Parcel data, reply;
651        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
652        data.writeInt32(audioSession);
653        data.writeInt32(pid);
654        remote()->transact(RELEASE_AUDIO_SESSION_ID, data, &reply);
655    }
656
657    virtual status_t queryNumberEffects(uint32_t *numEffects) const
658    {
659        Parcel data, reply;
660        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
661        status_t status = remote()->transact(QUERY_NUM_EFFECTS, data, &reply);
662        if (status != NO_ERROR) {
663            return status;
664        }
665        status = reply.readInt32();
666        if (status != NO_ERROR) {
667            return status;
668        }
669        if (numEffects != NULL) {
670            *numEffects = (uint32_t)reply.readInt32();
671        }
672        return NO_ERROR;
673    }
674
675    virtual status_t queryEffect(uint32_t index, effect_descriptor_t *pDescriptor) const
676    {
677        if (pDescriptor == NULL) {
678            return BAD_VALUE;
679        }
680        Parcel data, reply;
681        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
682        data.writeInt32(index);
683        status_t status = remote()->transact(QUERY_EFFECT, data, &reply);
684        if (status != NO_ERROR) {
685            return status;
686        }
687        status = reply.readInt32();
688        if (status != NO_ERROR) {
689            return status;
690        }
691        reply.read(pDescriptor, sizeof(effect_descriptor_t));
692        return NO_ERROR;
693    }
694
695    virtual status_t getEffectDescriptor(const effect_uuid_t *pUuid,
696            effect_descriptor_t *pDescriptor) const
697    {
698        if (pUuid == NULL || pDescriptor == NULL) {
699            return BAD_VALUE;
700        }
701        Parcel data, reply;
702        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
703        data.write(pUuid, sizeof(effect_uuid_t));
704        status_t status = remote()->transact(GET_EFFECT_DESCRIPTOR, data, &reply);
705        if (status != NO_ERROR) {
706            return status;
707        }
708        status = reply.readInt32();
709        if (status != NO_ERROR) {
710            return status;
711        }
712        reply.read(pDescriptor, sizeof(effect_descriptor_t));
713        return NO_ERROR;
714    }
715
716    virtual sp<IEffect> createEffect(
717                                    effect_descriptor_t *pDesc,
718                                    const sp<IEffectClient>& client,
719                                    int32_t priority,
720                                    audio_io_handle_t output,
721                                    audio_session_t sessionId,
722                                    const String16& opPackageName,
723                                    pid_t pid,
724                                    status_t *status,
725                                    int *id,
726                                    int *enabled)
727    {
728        Parcel data, reply;
729        sp<IEffect> effect;
730
731        if (pDesc == NULL) {
732            return effect;
733            if (status != NULL) {
734                *status = BAD_VALUE;
735            }
736        }
737
738        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
739        data.write(pDesc, sizeof(effect_descriptor_t));
740        data.writeStrongBinder(IInterface::asBinder(client));
741        data.writeInt32(priority);
742        data.writeInt32((int32_t) output);
743        data.writeInt32(sessionId);
744        data.writeString16(opPackageName);
745        data.writeInt32((int32_t) pid);
746
747        status_t lStatus = remote()->transact(CREATE_EFFECT, data, &reply);
748        if (lStatus != NO_ERROR) {
749            ALOGE("createEffect error: %s", strerror(-lStatus));
750        } else {
751            lStatus = reply.readInt32();
752            int tmp = reply.readInt32();
753            if (id != NULL) {
754                *id = tmp;
755            }
756            tmp = reply.readInt32();
757            if (enabled != NULL) {
758                *enabled = tmp;
759            }
760            effect = interface_cast<IEffect>(reply.readStrongBinder());
761            reply.read(pDesc, sizeof(effect_descriptor_t));
762        }
763        if (status != NULL) {
764            *status = lStatus;
765        }
766
767        return effect;
768    }
769
770    virtual status_t moveEffects(audio_session_t session, audio_io_handle_t srcOutput,
771            audio_io_handle_t dstOutput)
772    {
773        Parcel data, reply;
774        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
775        data.writeInt32(session);
776        data.writeInt32((int32_t) srcOutput);
777        data.writeInt32((int32_t) dstOutput);
778        remote()->transact(MOVE_EFFECTS, data, &reply);
779        return reply.readInt32();
780    }
781
782    virtual audio_module_handle_t loadHwModule(const char *name)
783    {
784        Parcel data, reply;
785        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
786        data.writeCString(name);
787        remote()->transact(LOAD_HW_MODULE, data, &reply);
788        return (audio_module_handle_t) reply.readInt32();
789    }
790
791    virtual uint32_t getPrimaryOutputSamplingRate()
792    {
793        Parcel data, reply;
794        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
795        remote()->transact(GET_PRIMARY_OUTPUT_SAMPLING_RATE, data, &reply);
796        return reply.readInt32();
797    }
798
799    virtual size_t getPrimaryOutputFrameCount()
800    {
801        Parcel data, reply;
802        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
803        remote()->transact(GET_PRIMARY_OUTPUT_FRAME_COUNT, data, &reply);
804        return reply.readInt64();
805    }
806
807    virtual status_t setLowRamDevice(bool isLowRamDevice)
808    {
809        Parcel data, reply;
810        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
811        data.writeInt32((int) isLowRamDevice);
812        remote()->transact(SET_LOW_RAM_DEVICE, data, &reply);
813        return reply.readInt32();
814    }
815    virtual status_t listAudioPorts(unsigned int *num_ports,
816                                    struct audio_port *ports)
817    {
818        if (num_ports == NULL || *num_ports == 0 || ports == NULL) {
819            return BAD_VALUE;
820        }
821        Parcel data, reply;
822        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
823        data.writeInt32(*num_ports);
824        status_t status = remote()->transact(LIST_AUDIO_PORTS, data, &reply);
825        if (status != NO_ERROR ||
826                (status = (status_t)reply.readInt32()) != NO_ERROR) {
827            return status;
828        }
829        *num_ports = (unsigned int)reply.readInt32();
830        reply.read(ports, *num_ports * sizeof(struct audio_port));
831        return status;
832    }
833    virtual status_t getAudioPort(struct audio_port *port)
834    {
835        if (port == NULL) {
836            return BAD_VALUE;
837        }
838        Parcel data, reply;
839        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
840        data.write(port, sizeof(struct audio_port));
841        status_t status = remote()->transact(GET_AUDIO_PORT, data, &reply);
842        if (status != NO_ERROR ||
843                (status = (status_t)reply.readInt32()) != NO_ERROR) {
844            return status;
845        }
846        reply.read(port, sizeof(struct audio_port));
847        return status;
848    }
849    virtual status_t createAudioPatch(const struct audio_patch *patch,
850                                       audio_patch_handle_t *handle)
851    {
852        if (patch == NULL || handle == NULL) {
853            return BAD_VALUE;
854        }
855        Parcel data, reply;
856        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
857        data.write(patch, sizeof(struct audio_patch));
858        data.write(handle, sizeof(audio_patch_handle_t));
859        status_t status = remote()->transact(CREATE_AUDIO_PATCH, data, &reply);
860        if (status != NO_ERROR ||
861                (status = (status_t)reply.readInt32()) != NO_ERROR) {
862            return status;
863        }
864        reply.read(handle, sizeof(audio_patch_handle_t));
865        return status;
866    }
867    virtual status_t releaseAudioPatch(audio_patch_handle_t handle)
868    {
869        Parcel data, reply;
870        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
871        data.write(&handle, sizeof(audio_patch_handle_t));
872        status_t status = remote()->transact(RELEASE_AUDIO_PATCH, data, &reply);
873        if (status != NO_ERROR) {
874            status = (status_t)reply.readInt32();
875        }
876        return status;
877    }
878    virtual status_t listAudioPatches(unsigned int *num_patches,
879                                      struct audio_patch *patches)
880    {
881        if (num_patches == NULL || *num_patches == 0 || patches == NULL) {
882            return BAD_VALUE;
883        }
884        Parcel data, reply;
885        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
886        data.writeInt32(*num_patches);
887        status_t status = remote()->transact(LIST_AUDIO_PATCHES, data, &reply);
888        if (status != NO_ERROR ||
889                (status = (status_t)reply.readInt32()) != NO_ERROR) {
890            return status;
891        }
892        *num_patches = (unsigned int)reply.readInt32();
893        reply.read(patches, *num_patches * sizeof(struct audio_patch));
894        return status;
895    }
896    virtual status_t setAudioPortConfig(const struct audio_port_config *config)
897    {
898        if (config == NULL) {
899            return BAD_VALUE;
900        }
901        Parcel data, reply;
902        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
903        data.write(config, sizeof(struct audio_port_config));
904        status_t status = remote()->transact(SET_AUDIO_PORT_CONFIG, data, &reply);
905        if (status != NO_ERROR) {
906            status = (status_t)reply.readInt32();
907        }
908        return status;
909    }
910    virtual audio_hw_sync_t getAudioHwSyncForSession(audio_session_t sessionId)
911    {
912        Parcel data, reply;
913        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
914        data.writeInt32(sessionId);
915        status_t status = remote()->transact(GET_AUDIO_HW_SYNC, data, &reply);
916        if (status != NO_ERROR) {
917            return AUDIO_HW_SYNC_INVALID;
918        }
919        return (audio_hw_sync_t)reply.readInt32();
920    }
921    virtual status_t systemReady()
922    {
923        Parcel data, reply;
924        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
925        return remote()->transact(SYSTEM_READY, data, &reply, IBinder::FLAG_ONEWAY);
926    }
927    virtual size_t frameCountHAL(audio_io_handle_t ioHandle) const
928    {
929        Parcel data, reply;
930        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
931        data.writeInt32((int32_t) ioHandle);
932        status_t status = remote()->transact(FRAME_COUNT_HAL, data, &reply);
933        if (status != NO_ERROR) {
934            return 0;
935        }
936        return reply.readInt64();
937    }
938};
939
940IMPLEMENT_META_INTERFACE(AudioFlinger, "android.media.IAudioFlinger");
941
942// ----------------------------------------------------------------------
943
944status_t BnAudioFlinger::onTransact(
945    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
946{
947    // Whitelist of relevant events to trigger log merging.
948    // Log merging should activate during audio activity of any kind. This are considered the
949    // most relevant events.
950    // TODO should select more wisely the items from the list
951    switch (code) {
952        case CREATE_TRACK:
953        case OPEN_RECORD:
954        case SET_MASTER_VOLUME:
955        case SET_MASTER_MUTE:
956        case SET_STREAM_VOLUME:
957        case SET_STREAM_MUTE:
958        case SET_MIC_MUTE:
959        case SET_PARAMETERS:
960        case OPEN_INPUT:
961        case SET_VOICE_VOLUME:
962        case CREATE_EFFECT:
963        case SYSTEM_READY: {
964            requestLogMerge();
965            break;
966        }
967        default:
968            break;
969    }
970    switch (code) {
971        case CREATE_TRACK: {
972            CHECK_INTERFACE(IAudioFlinger, data, reply);
973            int streamType = data.readInt32();
974            uint32_t sampleRate = data.readInt32();
975            audio_format_t format = (audio_format_t) data.readInt32();
976            audio_channel_mask_t channelMask = data.readInt32();
977            size_t frameCount = data.readInt64();
978            audio_output_flags_t flags = (audio_output_flags_t) data.readInt32();
979            bool haveSharedBuffer = data.readInt32() != 0;
980            sp<IMemory> buffer;
981            if (haveSharedBuffer) {
982                buffer = interface_cast<IMemory>(data.readStrongBinder());
983            }
984            audio_io_handle_t output = (audio_io_handle_t) data.readInt32();
985            pid_t pid = (pid_t) data.readInt32();
986            pid_t tid = (pid_t) data.readInt32();
987            audio_session_t sessionId = (audio_session_t) data.readInt32();
988            int clientUid = data.readInt32();
989            audio_port_handle_t portId = (audio_port_handle_t) data.readInt32();
990            status_t status = NO_ERROR;
991            sp<IAudioTrack> track;
992            if ((haveSharedBuffer && (buffer == 0)) ||
993                    ((buffer != 0) && (buffer->pointer() == NULL))) {
994                ALOGW("CREATE_TRACK: cannot retrieve shared memory");
995                status = DEAD_OBJECT;
996            } else {
997                track = createTrack(
998                        (audio_stream_type_t) streamType, sampleRate, format,
999                        channelMask, &frameCount, &flags, buffer, output, pid, tid,
1000                        &sessionId, clientUid, &status, portId);
1001                LOG_ALWAYS_FATAL_IF((track != 0) != (status == NO_ERROR));
1002            }
1003            reply->writeInt64(frameCount);
1004            reply->writeInt32(flags);
1005            reply->writeInt32(sessionId);
1006            reply->writeInt32(status);
1007            reply->writeStrongBinder(IInterface::asBinder(track));
1008            return NO_ERROR;
1009        } break;
1010        case OPEN_RECORD: {
1011            CHECK_INTERFACE(IAudioFlinger, data, reply);
1012            audio_io_handle_t input = (audio_io_handle_t) data.readInt32();
1013            uint32_t sampleRate = data.readInt32();
1014            audio_format_t format = (audio_format_t) data.readInt32();
1015            audio_channel_mask_t channelMask = data.readInt32();
1016            const String16& opPackageName = data.readString16();
1017            size_t frameCount = data.readInt64();
1018            audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
1019            pid_t pid = (pid_t) data.readInt32();
1020            pid_t tid = (pid_t) data.readInt32();
1021            int clientUid = data.readInt32();
1022            audio_session_t sessionId = (audio_session_t) data.readInt32();
1023            size_t notificationFrames = data.readInt64();
1024            audio_port_handle_t portId = (audio_port_handle_t) data.readInt32();
1025            sp<IMemory> cblk;
1026            sp<IMemory> buffers;
1027            status_t status = NO_ERROR;
1028            sp<IAudioRecord> record = openRecord(input,
1029                    sampleRate, format, channelMask, opPackageName, &frameCount, &flags,
1030                    pid, tid, clientUid, &sessionId, &notificationFrames, cblk, buffers,
1031                    &status, portId);
1032            LOG_ALWAYS_FATAL_IF((record != 0) != (status == NO_ERROR));
1033            reply->writeInt64(frameCount);
1034            reply->writeInt32(flags);
1035            reply->writeInt32(sessionId);
1036            reply->writeInt64(notificationFrames);
1037            reply->writeInt32(status);
1038            reply->writeStrongBinder(IInterface::asBinder(record));
1039            reply->writeStrongBinder(IInterface::asBinder(cblk));
1040            reply->writeStrongBinder(IInterface::asBinder(buffers));
1041            return NO_ERROR;
1042        } break;
1043        case SAMPLE_RATE: {
1044            CHECK_INTERFACE(IAudioFlinger, data, reply);
1045            reply->writeInt32( sampleRate((audio_io_handle_t) data.readInt32()) );
1046            return NO_ERROR;
1047        } break;
1048
1049        // RESERVED for channelCount()
1050
1051        case FORMAT: {
1052            CHECK_INTERFACE(IAudioFlinger, data, reply);
1053            reply->writeInt32( format((audio_io_handle_t) data.readInt32()) );
1054            return NO_ERROR;
1055        } break;
1056        case FRAME_COUNT: {
1057            CHECK_INTERFACE(IAudioFlinger, data, reply);
1058            reply->writeInt64( frameCount((audio_io_handle_t) data.readInt32()) );
1059            return NO_ERROR;
1060        } break;
1061        case LATENCY: {
1062            CHECK_INTERFACE(IAudioFlinger, data, reply);
1063            reply->writeInt32( latency((audio_io_handle_t) data.readInt32()) );
1064            return NO_ERROR;
1065        } break;
1066        case SET_MASTER_VOLUME: {
1067            CHECK_INTERFACE(IAudioFlinger, data, reply);
1068            reply->writeInt32( setMasterVolume(data.readFloat()) );
1069            return NO_ERROR;
1070        } break;
1071        case SET_MASTER_MUTE: {
1072            CHECK_INTERFACE(IAudioFlinger, data, reply);
1073            reply->writeInt32( setMasterMute(data.readInt32()) );
1074            return NO_ERROR;
1075        } break;
1076        case MASTER_VOLUME: {
1077            CHECK_INTERFACE(IAudioFlinger, data, reply);
1078            reply->writeFloat( masterVolume() );
1079            return NO_ERROR;
1080        } break;
1081        case MASTER_MUTE: {
1082            CHECK_INTERFACE(IAudioFlinger, data, reply);
1083            reply->writeInt32( masterMute() );
1084            return NO_ERROR;
1085        } break;
1086        case SET_STREAM_VOLUME: {
1087            CHECK_INTERFACE(IAudioFlinger, data, reply);
1088            int stream = data.readInt32();
1089            float volume = data.readFloat();
1090            audio_io_handle_t output = (audio_io_handle_t) data.readInt32();
1091            reply->writeInt32( setStreamVolume((audio_stream_type_t) stream, volume, output) );
1092            return NO_ERROR;
1093        } break;
1094        case SET_STREAM_MUTE: {
1095            CHECK_INTERFACE(IAudioFlinger, data, reply);
1096            int stream = data.readInt32();
1097            reply->writeInt32( setStreamMute((audio_stream_type_t) stream, data.readInt32()) );
1098            return NO_ERROR;
1099        } break;
1100        case STREAM_VOLUME: {
1101            CHECK_INTERFACE(IAudioFlinger, data, reply);
1102            int stream = data.readInt32();
1103            int output = data.readInt32();
1104            reply->writeFloat( streamVolume((audio_stream_type_t) stream, output) );
1105            return NO_ERROR;
1106        } break;
1107        case STREAM_MUTE: {
1108            CHECK_INTERFACE(IAudioFlinger, data, reply);
1109            int stream = data.readInt32();
1110            reply->writeInt32( streamMute((audio_stream_type_t) stream) );
1111            return NO_ERROR;
1112        } break;
1113        case SET_MODE: {
1114            CHECK_INTERFACE(IAudioFlinger, data, reply);
1115            audio_mode_t mode = (audio_mode_t) data.readInt32();
1116            reply->writeInt32( setMode(mode) );
1117            return NO_ERROR;
1118        } break;
1119        case SET_MIC_MUTE: {
1120            CHECK_INTERFACE(IAudioFlinger, data, reply);
1121            int state = data.readInt32();
1122            reply->writeInt32( setMicMute(state) );
1123            return NO_ERROR;
1124        } break;
1125        case GET_MIC_MUTE: {
1126            CHECK_INTERFACE(IAudioFlinger, data, reply);
1127            reply->writeInt32( getMicMute() );
1128            return NO_ERROR;
1129        } break;
1130        case SET_PARAMETERS: {
1131            CHECK_INTERFACE(IAudioFlinger, data, reply);
1132            audio_io_handle_t ioHandle = (audio_io_handle_t) data.readInt32();
1133            String8 keyValuePairs(data.readString8());
1134            reply->writeInt32(setParameters(ioHandle, keyValuePairs));
1135            return NO_ERROR;
1136        } break;
1137        case GET_PARAMETERS: {
1138            CHECK_INTERFACE(IAudioFlinger, data, reply);
1139            audio_io_handle_t ioHandle = (audio_io_handle_t) data.readInt32();
1140            String8 keys(data.readString8());
1141            reply->writeString8(getParameters(ioHandle, keys));
1142            return NO_ERROR;
1143        } break;
1144
1145        case REGISTER_CLIENT: {
1146            CHECK_INTERFACE(IAudioFlinger, data, reply);
1147            sp<IAudioFlingerClient> client = interface_cast<IAudioFlingerClient>(
1148                    data.readStrongBinder());
1149            registerClient(client);
1150            return NO_ERROR;
1151        } break;
1152        case GET_INPUTBUFFERSIZE: {
1153            CHECK_INTERFACE(IAudioFlinger, data, reply);
1154            uint32_t sampleRate = data.readInt32();
1155            audio_format_t format = (audio_format_t) data.readInt32();
1156            audio_channel_mask_t channelMask = data.readInt32();
1157            reply->writeInt64( getInputBufferSize(sampleRate, format, channelMask) );
1158            return NO_ERROR;
1159        } break;
1160        case OPEN_OUTPUT: {
1161            CHECK_INTERFACE(IAudioFlinger, data, reply);
1162            audio_module_handle_t module = (audio_module_handle_t)data.readInt32();
1163            audio_config_t config = {};
1164            if (data.read(&config, sizeof(audio_config_t)) != NO_ERROR) {
1165                ALOGE("b/23905951");
1166            }
1167            audio_devices_t devices = (audio_devices_t)data.readInt32();
1168            String8 address(data.readString8());
1169            audio_output_flags_t flags = (audio_output_flags_t) data.readInt32();
1170            uint32_t latencyMs = 0;
1171            audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
1172            status_t status = openOutput(module, &output, &config,
1173                                         &devices, address, &latencyMs, flags);
1174            ALOGV("OPEN_OUTPUT output, %d", output);
1175            reply->writeInt32((int32_t)status);
1176            if (status == NO_ERROR) {
1177                reply->writeInt32((int32_t)output);
1178                reply->write(&config, sizeof(audio_config_t));
1179                reply->writeInt32(devices);
1180                reply->writeInt32(latencyMs);
1181            }
1182            return NO_ERROR;
1183        } break;
1184        case OPEN_DUPLICATE_OUTPUT: {
1185            CHECK_INTERFACE(IAudioFlinger, data, reply);
1186            audio_io_handle_t output1 = (audio_io_handle_t) data.readInt32();
1187            audio_io_handle_t output2 = (audio_io_handle_t) data.readInt32();
1188            reply->writeInt32((int32_t) openDuplicateOutput(output1, output2));
1189            return NO_ERROR;
1190        } break;
1191        case CLOSE_OUTPUT: {
1192            CHECK_INTERFACE(IAudioFlinger, data, reply);
1193            reply->writeInt32(closeOutput((audio_io_handle_t) data.readInt32()));
1194            return NO_ERROR;
1195        } break;
1196        case SUSPEND_OUTPUT: {
1197            CHECK_INTERFACE(IAudioFlinger, data, reply);
1198            reply->writeInt32(suspendOutput((audio_io_handle_t) data.readInt32()));
1199            return NO_ERROR;
1200        } break;
1201        case RESTORE_OUTPUT: {
1202            CHECK_INTERFACE(IAudioFlinger, data, reply);
1203            reply->writeInt32(restoreOutput((audio_io_handle_t) data.readInt32()));
1204            return NO_ERROR;
1205        } break;
1206        case OPEN_INPUT: {
1207            CHECK_INTERFACE(IAudioFlinger, data, reply);
1208            audio_module_handle_t module = (audio_module_handle_t)data.readInt32();
1209            audio_io_handle_t input = (audio_io_handle_t)data.readInt32();
1210            audio_config_t config = {};
1211            if (data.read(&config, sizeof(audio_config_t)) != NO_ERROR) {
1212                ALOGE("b/23905951");
1213            }
1214            audio_devices_t device = (audio_devices_t)data.readInt32();
1215            String8 address(data.readString8());
1216            audio_source_t source = (audio_source_t)data.readInt32();
1217            audio_input_flags_t flags = (audio_input_flags_t) data.readInt32();
1218
1219            status_t status = openInput(module, &input, &config,
1220                                        &device, address, source, flags);
1221            reply->writeInt32((int32_t) status);
1222            if (status == NO_ERROR) {
1223                reply->writeInt32((int32_t) input);
1224                reply->write(&config, sizeof(audio_config_t));
1225                reply->writeInt32(device);
1226            }
1227            return NO_ERROR;
1228        } break;
1229        case CLOSE_INPUT: {
1230            CHECK_INTERFACE(IAudioFlinger, data, reply);
1231            reply->writeInt32(closeInput((audio_io_handle_t) data.readInt32()));
1232            return NO_ERROR;
1233        } break;
1234        case INVALIDATE_STREAM: {
1235            CHECK_INTERFACE(IAudioFlinger, data, reply);
1236            audio_stream_type_t stream = (audio_stream_type_t) data.readInt32();
1237            reply->writeInt32(invalidateStream(stream));
1238            return NO_ERROR;
1239        } break;
1240        case SET_VOICE_VOLUME: {
1241            CHECK_INTERFACE(IAudioFlinger, data, reply);
1242            float volume = data.readFloat();
1243            reply->writeInt32( setVoiceVolume(volume) );
1244            return NO_ERROR;
1245        } break;
1246        case GET_RENDER_POSITION: {
1247            CHECK_INTERFACE(IAudioFlinger, data, reply);
1248            audio_io_handle_t output = (audio_io_handle_t) data.readInt32();
1249            uint32_t halFrames = 0;
1250            uint32_t dspFrames = 0;
1251            status_t status = getRenderPosition(&halFrames, &dspFrames, output);
1252            reply->writeInt32(status);
1253            if (status == NO_ERROR) {
1254                reply->writeInt32(halFrames);
1255                reply->writeInt32(dspFrames);
1256            }
1257            return NO_ERROR;
1258        }
1259        case GET_INPUT_FRAMES_LOST: {
1260            CHECK_INTERFACE(IAudioFlinger, data, reply);
1261            audio_io_handle_t ioHandle = (audio_io_handle_t) data.readInt32();
1262            reply->writeInt32((int32_t) getInputFramesLost(ioHandle));
1263            return NO_ERROR;
1264        } break;
1265        case NEW_AUDIO_SESSION_ID: {
1266            CHECK_INTERFACE(IAudioFlinger, data, reply);
1267            reply->writeInt32(newAudioUniqueId((audio_unique_id_use_t) data.readInt32()));
1268            return NO_ERROR;
1269        } break;
1270        case ACQUIRE_AUDIO_SESSION_ID: {
1271            CHECK_INTERFACE(IAudioFlinger, data, reply);
1272            audio_session_t audioSession = (audio_session_t) data.readInt32();
1273            int pid = data.readInt32();
1274            acquireAudioSessionId(audioSession, pid);
1275            return NO_ERROR;
1276        } break;
1277        case RELEASE_AUDIO_SESSION_ID: {
1278            CHECK_INTERFACE(IAudioFlinger, data, reply);
1279            audio_session_t audioSession = (audio_session_t) data.readInt32();
1280            int pid = data.readInt32();
1281            releaseAudioSessionId(audioSession, pid);
1282            return NO_ERROR;
1283        } break;
1284        case QUERY_NUM_EFFECTS: {
1285            CHECK_INTERFACE(IAudioFlinger, data, reply);
1286            uint32_t numEffects = 0;
1287            status_t status = queryNumberEffects(&numEffects);
1288            reply->writeInt32(status);
1289            if (status == NO_ERROR) {
1290                reply->writeInt32((int32_t)numEffects);
1291            }
1292            return NO_ERROR;
1293        }
1294        case QUERY_EFFECT: {
1295            CHECK_INTERFACE(IAudioFlinger, data, reply);
1296            effect_descriptor_t desc = {};
1297            status_t status = queryEffect(data.readInt32(), &desc);
1298            reply->writeInt32(status);
1299            if (status == NO_ERROR) {
1300                reply->write(&desc, sizeof(effect_descriptor_t));
1301            }
1302            return NO_ERROR;
1303        }
1304        case GET_EFFECT_DESCRIPTOR: {
1305            CHECK_INTERFACE(IAudioFlinger, data, reply);
1306            effect_uuid_t uuid;
1307            data.read(&uuid, sizeof(effect_uuid_t));
1308            effect_descriptor_t desc = {};
1309            status_t status = getEffectDescriptor(&uuid, &desc);
1310            reply->writeInt32(status);
1311            if (status == NO_ERROR) {
1312                reply->write(&desc, sizeof(effect_descriptor_t));
1313            }
1314            return NO_ERROR;
1315        }
1316        case CREATE_EFFECT: {
1317            CHECK_INTERFACE(IAudioFlinger, data, reply);
1318            effect_descriptor_t desc = {};
1319            if (data.read(&desc, sizeof(effect_descriptor_t)) != NO_ERROR) {
1320                ALOGE("b/23905951");
1321            }
1322            sp<IEffectClient> client = interface_cast<IEffectClient>(data.readStrongBinder());
1323            int32_t priority = data.readInt32();
1324            audio_io_handle_t output = (audio_io_handle_t) data.readInt32();
1325            audio_session_t sessionId = (audio_session_t) data.readInt32();
1326            const String16 opPackageName = data.readString16();
1327            pid_t pid = (pid_t)data.readInt32();
1328
1329            status_t status = NO_ERROR;
1330            int id = 0;
1331            int enabled = 0;
1332
1333            sp<IEffect> effect = createEffect(&desc, client, priority, output, sessionId,
1334                    opPackageName, pid, &status, &id, &enabled);
1335            reply->writeInt32(status);
1336            reply->writeInt32(id);
1337            reply->writeInt32(enabled);
1338            reply->writeStrongBinder(IInterface::asBinder(effect));
1339            reply->write(&desc, sizeof(effect_descriptor_t));
1340            return NO_ERROR;
1341        } break;
1342        case MOVE_EFFECTS: {
1343            CHECK_INTERFACE(IAudioFlinger, data, reply);
1344            audio_session_t session = (audio_session_t) data.readInt32();
1345            audio_io_handle_t srcOutput = (audio_io_handle_t) data.readInt32();
1346            audio_io_handle_t dstOutput = (audio_io_handle_t) data.readInt32();
1347            reply->writeInt32(moveEffects(session, srcOutput, dstOutput));
1348            return NO_ERROR;
1349        } break;
1350        case LOAD_HW_MODULE: {
1351            CHECK_INTERFACE(IAudioFlinger, data, reply);
1352            reply->writeInt32(loadHwModule(data.readCString()));
1353            return NO_ERROR;
1354        } break;
1355        case GET_PRIMARY_OUTPUT_SAMPLING_RATE: {
1356            CHECK_INTERFACE(IAudioFlinger, data, reply);
1357            reply->writeInt32(getPrimaryOutputSamplingRate());
1358            return NO_ERROR;
1359        } break;
1360        case GET_PRIMARY_OUTPUT_FRAME_COUNT: {
1361            CHECK_INTERFACE(IAudioFlinger, data, reply);
1362            reply->writeInt64(getPrimaryOutputFrameCount());
1363            return NO_ERROR;
1364        } break;
1365        case SET_LOW_RAM_DEVICE: {
1366            CHECK_INTERFACE(IAudioFlinger, data, reply);
1367            bool isLowRamDevice = data.readInt32() != 0;
1368            reply->writeInt32(setLowRamDevice(isLowRamDevice));
1369            return NO_ERROR;
1370        } break;
1371        case LIST_AUDIO_PORTS: {
1372            CHECK_INTERFACE(IAudioFlinger, data, reply);
1373            unsigned int numPortsReq = data.readInt32();
1374            if (numPortsReq > MAX_ITEMS_PER_LIST) {
1375                numPortsReq = MAX_ITEMS_PER_LIST;
1376            }
1377            unsigned int numPorts = numPortsReq;
1378            struct audio_port *ports =
1379                    (struct audio_port *)calloc(numPortsReq,
1380                                                           sizeof(struct audio_port));
1381            if (ports == NULL) {
1382                reply->writeInt32(NO_MEMORY);
1383                reply->writeInt32(0);
1384                return NO_ERROR;
1385            }
1386            status_t status = listAudioPorts(&numPorts, ports);
1387            reply->writeInt32(status);
1388            reply->writeInt32(numPorts);
1389            if (status == NO_ERROR) {
1390                if (numPortsReq > numPorts) {
1391                    numPortsReq = numPorts;
1392                }
1393                reply->write(ports, numPortsReq * sizeof(struct audio_port));
1394            }
1395            free(ports);
1396            return NO_ERROR;
1397        } break;
1398        case GET_AUDIO_PORT: {
1399            CHECK_INTERFACE(IAudioFlinger, data, reply);
1400            struct audio_port port = {};
1401            if (data.read(&port, sizeof(struct audio_port)) != NO_ERROR) {
1402                ALOGE("b/23905951");
1403            }
1404            status_t status = getAudioPort(&port);
1405            reply->writeInt32(status);
1406            if (status == NO_ERROR) {
1407                reply->write(&port, sizeof(struct audio_port));
1408            }
1409            return NO_ERROR;
1410        } break;
1411        case CREATE_AUDIO_PATCH: {
1412            CHECK_INTERFACE(IAudioFlinger, data, reply);
1413            struct audio_patch patch;
1414            data.read(&patch, sizeof(struct audio_patch));
1415            audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE;
1416            if (data.read(&handle, sizeof(audio_patch_handle_t)) != NO_ERROR) {
1417                ALOGE("b/23905951");
1418            }
1419            status_t status = createAudioPatch(&patch, &handle);
1420            reply->writeInt32(status);
1421            if (status == NO_ERROR) {
1422                reply->write(&handle, sizeof(audio_patch_handle_t));
1423            }
1424            return NO_ERROR;
1425        } break;
1426        case RELEASE_AUDIO_PATCH: {
1427            CHECK_INTERFACE(IAudioFlinger, data, reply);
1428            audio_patch_handle_t handle;
1429            data.read(&handle, sizeof(audio_patch_handle_t));
1430            status_t status = releaseAudioPatch(handle);
1431            reply->writeInt32(status);
1432            return NO_ERROR;
1433        } break;
1434        case LIST_AUDIO_PATCHES: {
1435            CHECK_INTERFACE(IAudioFlinger, data, reply);
1436            unsigned int numPatchesReq = data.readInt32();
1437            if (numPatchesReq > MAX_ITEMS_PER_LIST) {
1438                numPatchesReq = MAX_ITEMS_PER_LIST;
1439            }
1440            unsigned int numPatches = numPatchesReq;
1441            struct audio_patch *patches =
1442                    (struct audio_patch *)calloc(numPatchesReq,
1443                                                 sizeof(struct audio_patch));
1444            if (patches == NULL) {
1445                reply->writeInt32(NO_MEMORY);
1446                reply->writeInt32(0);
1447                return NO_ERROR;
1448            }
1449            status_t status = listAudioPatches(&numPatches, patches);
1450            reply->writeInt32(status);
1451            reply->writeInt32(numPatches);
1452            if (status == NO_ERROR) {
1453                if (numPatchesReq > numPatches) {
1454                    numPatchesReq = numPatches;
1455                }
1456                reply->write(patches, numPatchesReq * sizeof(struct audio_patch));
1457            }
1458            free(patches);
1459            return NO_ERROR;
1460        } break;
1461        case SET_AUDIO_PORT_CONFIG: {
1462            CHECK_INTERFACE(IAudioFlinger, data, reply);
1463            struct audio_port_config config;
1464            data.read(&config, sizeof(struct audio_port_config));
1465            status_t status = setAudioPortConfig(&config);
1466            reply->writeInt32(status);
1467            return NO_ERROR;
1468        } break;
1469        case GET_AUDIO_HW_SYNC: {
1470            CHECK_INTERFACE(IAudioFlinger, data, reply);
1471            reply->writeInt32(getAudioHwSyncForSession((audio_session_t) data.readInt32()));
1472            return NO_ERROR;
1473        } break;
1474        case SYSTEM_READY: {
1475            CHECK_INTERFACE(IAudioFlinger, data, reply);
1476            systemReady();
1477            return NO_ERROR;
1478        } break;
1479        case FRAME_COUNT_HAL: {
1480            CHECK_INTERFACE(IAudioFlinger, data, reply);
1481            reply->writeInt64( frameCountHAL((audio_io_handle_t) data.readInt32()) );
1482            return NO_ERROR;
1483        } break;
1484        default:
1485            return BBinder::onTransact(code, data, reply, flags);
1486    }
1487}
1488
1489// ----------------------------------------------------------------------------
1490
1491} // namespace android
1492