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