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