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