Home | History | Annotate | Download | only in libmedia
      1 /*
      2 **
      3 ** Copyright 2007, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 #define LOG_TAG "IAudioFlinger"
     19 //#define LOG_NDEBUG 0
     20 #include <utils/Log.h>
     21 
     22 #include <stdint.h>
     23 #include <sys/types.h>
     24 
     25 #include <binder/Parcel.h>
     26 
     27 #include <media/IAudioFlinger.h>
     28 
     29 namespace android {
     30 
     31 enum {
     32     CREATE_TRACK = IBinder::FIRST_CALL_TRANSACTION,
     33     OPEN_RECORD,
     34     SAMPLE_RATE,
     35     CHANNEL_COUNT,  // obsolete
     36     FORMAT,
     37     FRAME_COUNT,
     38     LATENCY,
     39     SET_MASTER_VOLUME,
     40     SET_MASTER_MUTE,
     41     MASTER_VOLUME,
     42     MASTER_MUTE,
     43     SET_STREAM_VOLUME,
     44     SET_STREAM_MUTE,
     45     STREAM_VOLUME,
     46     STREAM_MUTE,
     47     SET_MODE,
     48     SET_MIC_MUTE,
     49     GET_MIC_MUTE,
     50     SET_PARAMETERS,
     51     GET_PARAMETERS,
     52     REGISTER_CLIENT,
     53     GET_INPUTBUFFERSIZE,
     54     OPEN_OUTPUT,
     55     OPEN_DUPLICATE_OUTPUT,
     56     CLOSE_OUTPUT,
     57     SUSPEND_OUTPUT,
     58     RESTORE_OUTPUT,
     59     OPEN_INPUT,
     60     CLOSE_INPUT,
     61     SET_STREAM_OUTPUT,
     62     SET_VOICE_VOLUME,
     63     GET_RENDER_POSITION,
     64     GET_INPUT_FRAMES_LOST,
     65     NEW_AUDIO_SESSION_ID,
     66     ACQUIRE_AUDIO_SESSION_ID,
     67     RELEASE_AUDIO_SESSION_ID,
     68     QUERY_NUM_EFFECTS,
     69     QUERY_EFFECT,
     70     GET_EFFECT_DESCRIPTOR,
     71     CREATE_EFFECT,
     72     MOVE_EFFECTS,
     73     LOAD_HW_MODULE,
     74     GET_PRIMARY_OUTPUT_SAMPLING_RATE,
     75     GET_PRIMARY_OUTPUT_FRAME_COUNT,
     76 };
     77 
     78 class BpAudioFlinger : public BpInterface<IAudioFlinger>
     79 {
     80 public:
     81     BpAudioFlinger(const sp<IBinder>& impl)
     82         : BpInterface<IAudioFlinger>(impl)
     83     {
     84     }
     85 
     86     virtual sp<IAudioTrack> createTrack(
     87                                 pid_t pid,
     88                                 audio_stream_type_t streamType,
     89                                 uint32_t sampleRate,
     90                                 audio_format_t format,
     91                                 audio_channel_mask_t channelMask,
     92                                 int frameCount,
     93                                 track_flags_t flags,
     94                                 const sp<IMemory>& sharedBuffer,
     95                                 audio_io_handle_t output,
     96                                 pid_t tid,
     97                                 int *sessionId,
     98                                 status_t *status)
     99     {
    100         Parcel data, reply;
    101         sp<IAudioTrack> track;
    102         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    103         data.writeInt32(pid);
    104         data.writeInt32((int32_t) streamType);
    105         data.writeInt32(sampleRate);
    106         data.writeInt32(format);
    107         data.writeInt32(channelMask);
    108         data.writeInt32(frameCount);
    109         data.writeInt32((int32_t) flags);
    110         data.writeStrongBinder(sharedBuffer->asBinder());
    111         data.writeInt32((int32_t) output);
    112         data.writeInt32((int32_t) tid);
    113         int lSessionId = 0;
    114         if (sessionId != NULL) {
    115             lSessionId = *sessionId;
    116         }
    117         data.writeInt32(lSessionId);
    118         status_t lStatus = remote()->transact(CREATE_TRACK, data, &reply);
    119         if (lStatus != NO_ERROR) {
    120             ALOGE("createTrack error: %s", strerror(-lStatus));
    121         } else {
    122             lSessionId = reply.readInt32();
    123             if (sessionId != NULL) {
    124                 *sessionId = lSessionId;
    125             }
    126             lStatus = reply.readInt32();
    127             track = interface_cast<IAudioTrack>(reply.readStrongBinder());
    128         }
    129         if (status) {
    130             *status = lStatus;
    131         }
    132         return track;
    133     }
    134 
    135     virtual sp<IAudioRecord> openRecord(
    136                                 pid_t pid,
    137                                 audio_io_handle_t input,
    138                                 uint32_t sampleRate,
    139                                 audio_format_t format,
    140                                 audio_channel_mask_t channelMask,
    141                                 int frameCount,
    142                                 track_flags_t flags,
    143                                 pid_t tid,
    144                                 int *sessionId,
    145                                 status_t *status)
    146     {
    147         Parcel data, reply;
    148         sp<IAudioRecord> record;
    149         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    150         data.writeInt32(pid);
    151         data.writeInt32((int32_t) input);
    152         data.writeInt32(sampleRate);
    153         data.writeInt32(format);
    154         data.writeInt32(channelMask);
    155         data.writeInt32(frameCount);
    156         data.writeInt32(flags);
    157         data.writeInt32((int32_t) tid);
    158         int lSessionId = 0;
    159         if (sessionId != NULL) {
    160             lSessionId = *sessionId;
    161         }
    162         data.writeInt32(lSessionId);
    163         status_t lStatus = remote()->transact(OPEN_RECORD, data, &reply);
    164         if (lStatus != NO_ERROR) {
    165             ALOGE("openRecord error: %s", strerror(-lStatus));
    166         } else {
    167             lSessionId = reply.readInt32();
    168             if (sessionId != NULL) {
    169                 *sessionId = lSessionId;
    170             }
    171             lStatus = reply.readInt32();
    172             record = interface_cast<IAudioRecord>(reply.readStrongBinder());
    173         }
    174         if (status) {
    175             *status = lStatus;
    176         }
    177         return record;
    178     }
    179 
    180     virtual uint32_t sampleRate(audio_io_handle_t output) const
    181     {
    182         Parcel data, reply;
    183         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    184         data.writeInt32((int32_t) output);
    185         remote()->transact(SAMPLE_RATE, data, &reply);
    186         return reply.readInt32();
    187     }
    188 
    189 #if 0
    190     virtual int channelCount(audio_io_handle_t output) const
    191     {
    192         Parcel data, reply;
    193         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    194         data.writeInt32((int32_t) output);
    195         remote()->transact(CHANNEL_COUNT, data, &reply);
    196         return reply.readInt32();
    197     }
    198 #endif
    199 
    200     virtual audio_format_t format(audio_io_handle_t output) const
    201     {
    202         Parcel data, reply;
    203         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    204         data.writeInt32((int32_t) output);
    205         remote()->transact(FORMAT, data, &reply);
    206         return (audio_format_t) reply.readInt32();
    207     }
    208 
    209     virtual size_t frameCount(audio_io_handle_t output) const
    210     {
    211         Parcel data, reply;
    212         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    213         data.writeInt32((int32_t) output);
    214         remote()->transact(FRAME_COUNT, data, &reply);
    215         return reply.readInt32();
    216     }
    217 
    218     virtual uint32_t latency(audio_io_handle_t output) const
    219     {
    220         Parcel data, reply;
    221         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    222         data.writeInt32((int32_t) output);
    223         remote()->transact(LATENCY, data, &reply);
    224         return reply.readInt32();
    225     }
    226 
    227     virtual status_t setMasterVolume(float value)
    228     {
    229         Parcel data, reply;
    230         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    231         data.writeFloat(value);
    232         remote()->transact(SET_MASTER_VOLUME, data, &reply);
    233         return reply.readInt32();
    234     }
    235 
    236     virtual status_t setMasterMute(bool muted)
    237     {
    238         Parcel data, reply;
    239         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    240         data.writeInt32(muted);
    241         remote()->transact(SET_MASTER_MUTE, data, &reply);
    242         return reply.readInt32();
    243     }
    244 
    245     virtual float masterVolume() const
    246     {
    247         Parcel data, reply;
    248         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    249         remote()->transact(MASTER_VOLUME, data, &reply);
    250         return reply.readFloat();
    251     }
    252 
    253     virtual bool masterMute() const
    254     {
    255         Parcel data, reply;
    256         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    257         remote()->transact(MASTER_MUTE, data, &reply);
    258         return reply.readInt32();
    259     }
    260 
    261     virtual status_t setStreamVolume(audio_stream_type_t stream, float value,
    262             audio_io_handle_t output)
    263     {
    264         Parcel data, reply;
    265         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    266         data.writeInt32((int32_t) stream);
    267         data.writeFloat(value);
    268         data.writeInt32((int32_t) output);
    269         remote()->transact(SET_STREAM_VOLUME, data, &reply);
    270         return reply.readInt32();
    271     }
    272 
    273     virtual status_t setStreamMute(audio_stream_type_t stream, bool muted)
    274     {
    275         Parcel data, reply;
    276         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    277         data.writeInt32((int32_t) stream);
    278         data.writeInt32(muted);
    279         remote()->transact(SET_STREAM_MUTE, data, &reply);
    280         return reply.readInt32();
    281     }
    282 
    283     virtual float streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
    284     {
    285         Parcel data, reply;
    286         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    287         data.writeInt32((int32_t) stream);
    288         data.writeInt32((int32_t) output);
    289         remote()->transact(STREAM_VOLUME, data, &reply);
    290         return reply.readFloat();
    291     }
    292 
    293     virtual bool streamMute(audio_stream_type_t stream) const
    294     {
    295         Parcel data, reply;
    296         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    297         data.writeInt32((int32_t) stream);
    298         remote()->transact(STREAM_MUTE, data, &reply);
    299         return reply.readInt32();
    300     }
    301 
    302     virtual status_t setMode(audio_mode_t mode)
    303     {
    304         Parcel data, reply;
    305         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    306         data.writeInt32(mode);
    307         remote()->transact(SET_MODE, data, &reply);
    308         return reply.readInt32();
    309     }
    310 
    311     virtual status_t setMicMute(bool state)
    312     {
    313         Parcel data, reply;
    314         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    315         data.writeInt32(state);
    316         remote()->transact(SET_MIC_MUTE, data, &reply);
    317         return reply.readInt32();
    318     }
    319 
    320     virtual bool getMicMute() const
    321     {
    322         Parcel data, reply;
    323         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    324         remote()->transact(GET_MIC_MUTE, data, &reply);
    325         return reply.readInt32();
    326     }
    327 
    328     virtual status_t setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
    329     {
    330         Parcel data, reply;
    331         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    332         data.writeInt32((int32_t) ioHandle);
    333         data.writeString8(keyValuePairs);
    334         remote()->transact(SET_PARAMETERS, data, &reply);
    335         return reply.readInt32();
    336     }
    337 
    338     virtual String8 getParameters(audio_io_handle_t ioHandle, const String8& keys) const
    339     {
    340         Parcel data, reply;
    341         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    342         data.writeInt32((int32_t) ioHandle);
    343         data.writeString8(keys);
    344         remote()->transact(GET_PARAMETERS, data, &reply);
    345         return reply.readString8();
    346     }
    347 
    348     virtual void registerClient(const sp<IAudioFlingerClient>& client)
    349     {
    350         Parcel data, reply;
    351         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    352         data.writeStrongBinder(client->asBinder());
    353         remote()->transact(REGISTER_CLIENT, data, &reply);
    354     }
    355 
    356     virtual size_t getInputBufferSize(uint32_t sampleRate, audio_format_t format,
    357             audio_channel_mask_t channelMask) const
    358     {
    359         Parcel data, reply;
    360         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    361         data.writeInt32(sampleRate);
    362         data.writeInt32(format);
    363         data.writeInt32(channelMask);
    364         remote()->transact(GET_INPUTBUFFERSIZE, data, &reply);
    365         return reply.readInt32();
    366     }
    367 
    368     virtual audio_io_handle_t openOutput(audio_module_handle_t module,
    369                                          audio_devices_t *pDevices,
    370                                          uint32_t *pSamplingRate,
    371                                          audio_format_t *pFormat,
    372                                          audio_channel_mask_t *pChannelMask,
    373                                          uint32_t *pLatencyMs,
    374                                          audio_output_flags_t flags)
    375     {
    376         Parcel data, reply;
    377         audio_devices_t devices = pDevices ? *pDevices : (audio_devices_t)0;
    378         uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
    379         audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT;
    380         audio_channel_mask_t channelMask = pChannelMask ? *pChannelMask : (audio_channel_mask_t)0;
    381         uint32_t latency = pLatencyMs ? *pLatencyMs : 0;
    382 
    383         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    384         data.writeInt32(module);
    385         data.writeInt32(devices);
    386         data.writeInt32(samplingRate);
    387         data.writeInt32(format);
    388         data.writeInt32(channelMask);
    389         data.writeInt32(latency);
    390         data.writeInt32((int32_t) flags);
    391         remote()->transact(OPEN_OUTPUT, data, &reply);
    392         audio_io_handle_t output = (audio_io_handle_t) reply.readInt32();
    393         ALOGV("openOutput() returned output, %d", output);
    394         devices = (audio_devices_t)reply.readInt32();
    395         if (pDevices) *pDevices = devices;
    396         samplingRate = reply.readInt32();
    397         if (pSamplingRate) *pSamplingRate = samplingRate;
    398         format = (audio_format_t) reply.readInt32();
    399         if (pFormat) *pFormat = format;
    400         channelMask = (audio_channel_mask_t)reply.readInt32();
    401         if (pChannelMask) *pChannelMask = channelMask;
    402         latency = reply.readInt32();
    403         if (pLatencyMs) *pLatencyMs = latency;
    404         return output;
    405     }
    406 
    407     virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
    408             audio_io_handle_t output2)
    409     {
    410         Parcel data, reply;
    411         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    412         data.writeInt32((int32_t) output1);
    413         data.writeInt32((int32_t) output2);
    414         remote()->transact(OPEN_DUPLICATE_OUTPUT, data, &reply);
    415         return (audio_io_handle_t) reply.readInt32();
    416     }
    417 
    418     virtual status_t closeOutput(audio_io_handle_t output)
    419     {
    420         Parcel data, reply;
    421         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    422         data.writeInt32((int32_t) output);
    423         remote()->transact(CLOSE_OUTPUT, data, &reply);
    424         return reply.readInt32();
    425     }
    426 
    427     virtual status_t suspendOutput(audio_io_handle_t output)
    428     {
    429         Parcel data, reply;
    430         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    431         data.writeInt32((int32_t) output);
    432         remote()->transact(SUSPEND_OUTPUT, data, &reply);
    433         return reply.readInt32();
    434     }
    435 
    436     virtual status_t restoreOutput(audio_io_handle_t output)
    437     {
    438         Parcel data, reply;
    439         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    440         data.writeInt32((int32_t) output);
    441         remote()->transact(RESTORE_OUTPUT, data, &reply);
    442         return reply.readInt32();
    443     }
    444 
    445     virtual audio_io_handle_t openInput(audio_module_handle_t module,
    446                                         audio_devices_t *pDevices,
    447                                         uint32_t *pSamplingRate,
    448                                         audio_format_t *pFormat,
    449                                         audio_channel_mask_t *pChannelMask)
    450     {
    451         Parcel data, reply;
    452         audio_devices_t devices = pDevices ? *pDevices : (audio_devices_t)0;
    453         uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
    454         audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT;
    455         audio_channel_mask_t channelMask = pChannelMask ? *pChannelMask : (audio_channel_mask_t)0;
    456 
    457         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    458         data.writeInt32(module);
    459         data.writeInt32(devices);
    460         data.writeInt32(samplingRate);
    461         data.writeInt32(format);
    462         data.writeInt32(channelMask);
    463         remote()->transact(OPEN_INPUT, data, &reply);
    464         audio_io_handle_t input = (audio_io_handle_t) reply.readInt32();
    465         devices = (audio_devices_t)reply.readInt32();
    466         if (pDevices) *pDevices = devices;
    467         samplingRate = reply.readInt32();
    468         if (pSamplingRate) *pSamplingRate = samplingRate;
    469         format = (audio_format_t) reply.readInt32();
    470         if (pFormat) *pFormat = format;
    471         channelMask = (audio_channel_mask_t)reply.readInt32();
    472         if (pChannelMask) *pChannelMask = channelMask;
    473         return input;
    474     }
    475 
    476     virtual status_t closeInput(int input)
    477     {
    478         Parcel data, reply;
    479         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    480         data.writeInt32(input);
    481         remote()->transact(CLOSE_INPUT, data, &reply);
    482         return reply.readInt32();
    483     }
    484 
    485     virtual status_t setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
    486     {
    487         Parcel data, reply;
    488         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    489         data.writeInt32((int32_t) stream);
    490         data.writeInt32((int32_t) output);
    491         remote()->transact(SET_STREAM_OUTPUT, data, &reply);
    492         return reply.readInt32();
    493     }
    494 
    495     virtual status_t setVoiceVolume(float volume)
    496     {
    497         Parcel data, reply;
    498         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    499         data.writeFloat(volume);
    500         remote()->transact(SET_VOICE_VOLUME, data, &reply);
    501         return reply.readInt32();
    502     }
    503 
    504     virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
    505             audio_io_handle_t output) const
    506     {
    507         Parcel data, reply;
    508         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    509         data.writeInt32((int32_t) output);
    510         remote()->transact(GET_RENDER_POSITION, data, &reply);
    511         status_t status = reply.readInt32();
    512         if (status == NO_ERROR) {
    513             uint32_t tmp = reply.readInt32();
    514             if (halFrames) {
    515                 *halFrames = tmp;
    516             }
    517             tmp = reply.readInt32();
    518             if (dspFrames) {
    519                 *dspFrames = tmp;
    520             }
    521         }
    522         return status;
    523     }
    524 
    525     virtual unsigned int getInputFramesLost(audio_io_handle_t ioHandle) const
    526     {
    527         Parcel data, reply;
    528         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    529         data.writeInt32((int32_t) ioHandle);
    530         remote()->transact(GET_INPUT_FRAMES_LOST, data, &reply);
    531         return reply.readInt32();
    532     }
    533 
    534     virtual int newAudioSessionId()
    535     {
    536         Parcel data, reply;
    537         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    538         status_t status = remote()->transact(NEW_AUDIO_SESSION_ID, data, &reply);
    539         int id = 0;
    540         if (status == NO_ERROR) {
    541             id = reply.readInt32();
    542         }
    543         return id;
    544     }
    545 
    546     virtual void acquireAudioSessionId(int audioSession)
    547     {
    548         Parcel data, reply;
    549         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    550         data.writeInt32(audioSession);
    551         remote()->transact(ACQUIRE_AUDIO_SESSION_ID, data, &reply);
    552     }
    553 
    554     virtual void releaseAudioSessionId(int audioSession)
    555     {
    556         Parcel data, reply;
    557         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    558         data.writeInt32(audioSession);
    559         remote()->transact(RELEASE_AUDIO_SESSION_ID, data, &reply);
    560     }
    561 
    562     virtual status_t queryNumberEffects(uint32_t *numEffects) const
    563     {
    564         Parcel data, reply;
    565         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    566         status_t status = remote()->transact(QUERY_NUM_EFFECTS, data, &reply);
    567         if (status != NO_ERROR) {
    568             return status;
    569         }
    570         status = reply.readInt32();
    571         if (status != NO_ERROR) {
    572             return status;
    573         }
    574         if (numEffects != NULL) {
    575             *numEffects = (uint32_t)reply.readInt32();
    576         }
    577         return NO_ERROR;
    578     }
    579 
    580     virtual status_t queryEffect(uint32_t index, effect_descriptor_t *pDescriptor) const
    581     {
    582         if (pDescriptor == NULL) {
    583             return BAD_VALUE;
    584         }
    585         Parcel data, reply;
    586         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    587         data.writeInt32(index);
    588         status_t status = remote()->transact(QUERY_EFFECT, data, &reply);
    589         if (status != NO_ERROR) {
    590             return status;
    591         }
    592         status = reply.readInt32();
    593         if (status != NO_ERROR) {
    594             return status;
    595         }
    596         reply.read(pDescriptor, sizeof(effect_descriptor_t));
    597         return NO_ERROR;
    598     }
    599 
    600     virtual status_t getEffectDescriptor(const effect_uuid_t *pUuid,
    601             effect_descriptor_t *pDescriptor) const
    602     {
    603         if (pUuid == NULL || pDescriptor == NULL) {
    604             return BAD_VALUE;
    605         }
    606         Parcel data, reply;
    607         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    608         data.write(pUuid, sizeof(effect_uuid_t));
    609         status_t status = remote()->transact(GET_EFFECT_DESCRIPTOR, data, &reply);
    610         if (status != NO_ERROR) {
    611             return status;
    612         }
    613         status = reply.readInt32();
    614         if (status != NO_ERROR) {
    615             return status;
    616         }
    617         reply.read(pDescriptor, sizeof(effect_descriptor_t));
    618         return NO_ERROR;
    619     }
    620 
    621     virtual sp<IEffect> createEffect(pid_t pid,
    622                                     effect_descriptor_t *pDesc,
    623                                     const sp<IEffectClient>& client,
    624                                     int32_t priority,
    625                                     audio_io_handle_t output,
    626                                     int sessionId,
    627                                     status_t *status,
    628                                     int *id,
    629                                     int *enabled)
    630     {
    631         Parcel data, reply;
    632         sp<IEffect> effect;
    633 
    634         if (pDesc == NULL) {
    635             return effect;
    636             if (status) {
    637                 *status = BAD_VALUE;
    638             }
    639         }
    640 
    641         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    642         data.writeInt32(pid);
    643         data.write(pDesc, sizeof(effect_descriptor_t));
    644         data.writeStrongBinder(client->asBinder());
    645         data.writeInt32(priority);
    646         data.writeInt32((int32_t) output);
    647         data.writeInt32(sessionId);
    648 
    649         status_t lStatus = remote()->transact(CREATE_EFFECT, data, &reply);
    650         if (lStatus != NO_ERROR) {
    651             ALOGE("createEffect error: %s", strerror(-lStatus));
    652         } else {
    653             lStatus = reply.readInt32();
    654             int tmp = reply.readInt32();
    655             if (id) {
    656                 *id = tmp;
    657             }
    658             tmp = reply.readInt32();
    659             if (enabled != NULL) {
    660                 *enabled = tmp;
    661             }
    662             effect = interface_cast<IEffect>(reply.readStrongBinder());
    663             reply.read(pDesc, sizeof(effect_descriptor_t));
    664         }
    665         if (status) {
    666             *status = lStatus;
    667         }
    668 
    669         return effect;
    670     }
    671 
    672     virtual status_t moveEffects(int session, audio_io_handle_t srcOutput,
    673             audio_io_handle_t dstOutput)
    674     {
    675         Parcel data, reply;
    676         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    677         data.writeInt32(session);
    678         data.writeInt32((int32_t) srcOutput);
    679         data.writeInt32((int32_t) dstOutput);
    680         remote()->transact(MOVE_EFFECTS, data, &reply);
    681         return reply.readInt32();
    682     }
    683 
    684     virtual audio_module_handle_t loadHwModule(const char *name)
    685     {
    686         Parcel data, reply;
    687         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    688         data.writeCString(name);
    689         remote()->transact(LOAD_HW_MODULE, data, &reply);
    690         return (audio_module_handle_t) reply.readInt32();
    691     }
    692 
    693     virtual int32_t getPrimaryOutputSamplingRate()
    694     {
    695         Parcel data, reply;
    696         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    697         remote()->transact(GET_PRIMARY_OUTPUT_SAMPLING_RATE, data, &reply);
    698         return reply.readInt32();
    699     }
    700 
    701     virtual int32_t getPrimaryOutputFrameCount()
    702     {
    703         Parcel data, reply;
    704         data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
    705         remote()->transact(GET_PRIMARY_OUTPUT_FRAME_COUNT, data, &reply);
    706         return reply.readInt32();
    707     }
    708 
    709 };
    710 
    711 IMPLEMENT_META_INTERFACE(AudioFlinger, "android.media.IAudioFlinger");
    712 
    713 // ----------------------------------------------------------------------
    714 
    715 status_t BnAudioFlinger::onTransact(
    716     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
    717 {
    718     switch (code) {
    719         case CREATE_TRACK: {
    720             CHECK_INTERFACE(IAudioFlinger, data, reply);
    721             pid_t pid = data.readInt32();
    722             int streamType = data.readInt32();
    723             uint32_t sampleRate = data.readInt32();
    724             audio_format_t format = (audio_format_t) data.readInt32();
    725             audio_channel_mask_t channelMask = data.readInt32();
    726             size_t bufferCount = data.readInt32();
    727             track_flags_t flags = (track_flags_t) data.readInt32();
    728             sp<IMemory> buffer = interface_cast<IMemory>(data.readStrongBinder());
    729             audio_io_handle_t output = (audio_io_handle_t) data.readInt32();
    730             pid_t tid = (pid_t) data.readInt32();
    731             int sessionId = data.readInt32();
    732             status_t status;
    733             sp<IAudioTrack> track = createTrack(pid,
    734                     (audio_stream_type_t) streamType, sampleRate, format,
    735                     channelMask, bufferCount, flags, buffer, output, tid, &sessionId, &status);
    736             reply->writeInt32(sessionId);
    737             reply->writeInt32(status);
    738             reply->writeStrongBinder(track->asBinder());
    739             return NO_ERROR;
    740         } break;
    741         case OPEN_RECORD: {
    742             CHECK_INTERFACE(IAudioFlinger, data, reply);
    743             pid_t pid = data.readInt32();
    744             audio_io_handle_t input = (audio_io_handle_t) data.readInt32();
    745             uint32_t sampleRate = data.readInt32();
    746             audio_format_t format = (audio_format_t) data.readInt32();
    747             audio_channel_mask_t channelMask = data.readInt32();
    748             size_t bufferCount = data.readInt32();
    749             track_flags_t flags = (track_flags_t) data.readInt32();
    750             pid_t tid = (pid_t) data.readInt32();
    751             int sessionId = data.readInt32();
    752             status_t status;
    753             sp<IAudioRecord> record = openRecord(pid, input,
    754                     sampleRate, format, channelMask, bufferCount, flags, tid, &sessionId, &status);
    755             reply->writeInt32(sessionId);
    756             reply->writeInt32(status);
    757             reply->writeStrongBinder(record->asBinder());
    758             return NO_ERROR;
    759         } break;
    760         case SAMPLE_RATE: {
    761             CHECK_INTERFACE(IAudioFlinger, data, reply);
    762             reply->writeInt32( sampleRate((audio_io_handle_t) data.readInt32()) );
    763             return NO_ERROR;
    764         } break;
    765 #if 0
    766         case CHANNEL_COUNT: {
    767             CHECK_INTERFACE(IAudioFlinger, data, reply);
    768             reply->writeInt32( channelCount((audio_io_handle_t) data.readInt32()) );
    769             return NO_ERROR;
    770         } break;
    771 #endif
    772         case FORMAT: {
    773             CHECK_INTERFACE(IAudioFlinger, data, reply);
    774             reply->writeInt32( format((audio_io_handle_t) data.readInt32()) );
    775             return NO_ERROR;
    776         } break;
    777         case FRAME_COUNT: {
    778             CHECK_INTERFACE(IAudioFlinger, data, reply);
    779             reply->writeInt32( frameCount((audio_io_handle_t) data.readInt32()) );
    780             return NO_ERROR;
    781         } break;
    782         case LATENCY: {
    783             CHECK_INTERFACE(IAudioFlinger, data, reply);
    784             reply->writeInt32( latency((audio_io_handle_t) data.readInt32()) );
    785             return NO_ERROR;
    786         } break;
    787         case SET_MASTER_VOLUME: {
    788             CHECK_INTERFACE(IAudioFlinger, data, reply);
    789             reply->writeInt32( setMasterVolume(data.readFloat()) );
    790             return NO_ERROR;
    791         } break;
    792         case SET_MASTER_MUTE: {
    793             CHECK_INTERFACE(IAudioFlinger, data, reply);
    794             reply->writeInt32( setMasterMute(data.readInt32()) );
    795             return NO_ERROR;
    796         } break;
    797         case MASTER_VOLUME: {
    798             CHECK_INTERFACE(IAudioFlinger, data, reply);
    799             reply->writeFloat( masterVolume() );
    800             return NO_ERROR;
    801         } break;
    802         case MASTER_MUTE: {
    803             CHECK_INTERFACE(IAudioFlinger, data, reply);
    804             reply->writeInt32( masterMute() );
    805             return NO_ERROR;
    806         } break;
    807         case SET_STREAM_VOLUME: {
    808             CHECK_INTERFACE(IAudioFlinger, data, reply);
    809             int stream = data.readInt32();
    810             float volume = data.readFloat();
    811             audio_io_handle_t output = (audio_io_handle_t) data.readInt32();
    812             reply->writeInt32( setStreamVolume((audio_stream_type_t) stream, volume, output) );
    813             return NO_ERROR;
    814         } break;
    815         case SET_STREAM_MUTE: {
    816             CHECK_INTERFACE(IAudioFlinger, data, reply);
    817             int stream = data.readInt32();
    818             reply->writeInt32( setStreamMute((audio_stream_type_t) stream, data.readInt32()) );
    819             return NO_ERROR;
    820         } break;
    821         case STREAM_VOLUME: {
    822             CHECK_INTERFACE(IAudioFlinger, data, reply);
    823             int stream = data.readInt32();
    824             int output = data.readInt32();
    825             reply->writeFloat( streamVolume((audio_stream_type_t) stream, output) );
    826             return NO_ERROR;
    827         } break;
    828         case STREAM_MUTE: {
    829             CHECK_INTERFACE(IAudioFlinger, data, reply);
    830             int stream = data.readInt32();
    831             reply->writeInt32( streamMute((audio_stream_type_t) stream) );
    832             return NO_ERROR;
    833         } break;
    834         case SET_MODE: {
    835             CHECK_INTERFACE(IAudioFlinger, data, reply);
    836             audio_mode_t mode = (audio_mode_t) data.readInt32();
    837             reply->writeInt32( setMode(mode) );
    838             return NO_ERROR;
    839         } break;
    840         case SET_MIC_MUTE: {
    841             CHECK_INTERFACE(IAudioFlinger, data, reply);
    842             int state = data.readInt32();
    843             reply->writeInt32( setMicMute(state) );
    844             return NO_ERROR;
    845         } break;
    846         case GET_MIC_MUTE: {
    847             CHECK_INTERFACE(IAudioFlinger, data, reply);
    848             reply->writeInt32( getMicMute() );
    849             return NO_ERROR;
    850         } break;
    851         case SET_PARAMETERS: {
    852             CHECK_INTERFACE(IAudioFlinger, data, reply);
    853             audio_io_handle_t ioHandle = (audio_io_handle_t) data.readInt32();
    854             String8 keyValuePairs(data.readString8());
    855             reply->writeInt32(setParameters(ioHandle, keyValuePairs));
    856             return NO_ERROR;
    857         } break;
    858         case GET_PARAMETERS: {
    859             CHECK_INTERFACE(IAudioFlinger, data, reply);
    860             audio_io_handle_t ioHandle = (audio_io_handle_t) data.readInt32();
    861             String8 keys(data.readString8());
    862             reply->writeString8(getParameters(ioHandle, keys));
    863             return NO_ERROR;
    864         } break;
    865 
    866         case REGISTER_CLIENT: {
    867             CHECK_INTERFACE(IAudioFlinger, data, reply);
    868             sp<IAudioFlingerClient> client = interface_cast<IAudioFlingerClient>(data.readStrongBinder());
    869             registerClient(client);
    870             return NO_ERROR;
    871         } break;
    872         case GET_INPUTBUFFERSIZE: {
    873             CHECK_INTERFACE(IAudioFlinger, data, reply);
    874             uint32_t sampleRate = data.readInt32();
    875             audio_format_t format = (audio_format_t) data.readInt32();
    876             audio_channel_mask_t channelMask = data.readInt32();
    877             reply->writeInt32( getInputBufferSize(sampleRate, format, channelMask) );
    878             return NO_ERROR;
    879         } break;
    880         case OPEN_OUTPUT: {
    881             CHECK_INTERFACE(IAudioFlinger, data, reply);
    882             audio_module_handle_t module = (audio_module_handle_t)data.readInt32();
    883             audio_devices_t devices = (audio_devices_t)data.readInt32();
    884             uint32_t samplingRate = data.readInt32();
    885             audio_format_t format = (audio_format_t) data.readInt32();
    886             audio_channel_mask_t channelMask = (audio_channel_mask_t)data.readInt32();
    887             uint32_t latency = data.readInt32();
    888             audio_output_flags_t flags = (audio_output_flags_t) data.readInt32();
    889             audio_io_handle_t output = openOutput(module,
    890                                                  &devices,
    891                                                  &samplingRate,
    892                                                  &format,
    893                                                  &channelMask,
    894                                                  &latency,
    895                                                  flags);
    896             ALOGV("OPEN_OUTPUT output, %p", output);
    897             reply->writeInt32((int32_t) output);
    898             reply->writeInt32(devices);
    899             reply->writeInt32(samplingRate);
    900             reply->writeInt32(format);
    901             reply->writeInt32(channelMask);
    902             reply->writeInt32(latency);
    903             return NO_ERROR;
    904         } break;
    905         case OPEN_DUPLICATE_OUTPUT: {
    906             CHECK_INTERFACE(IAudioFlinger, data, reply);
    907             audio_io_handle_t output1 = (audio_io_handle_t) data.readInt32();
    908             audio_io_handle_t output2 = (audio_io_handle_t) data.readInt32();
    909             reply->writeInt32((int32_t) openDuplicateOutput(output1, output2));
    910             return NO_ERROR;
    911         } break;
    912         case CLOSE_OUTPUT: {
    913             CHECK_INTERFACE(IAudioFlinger, data, reply);
    914             reply->writeInt32(closeOutput((audio_io_handle_t) data.readInt32()));
    915             return NO_ERROR;
    916         } break;
    917         case SUSPEND_OUTPUT: {
    918             CHECK_INTERFACE(IAudioFlinger, data, reply);
    919             reply->writeInt32(suspendOutput((audio_io_handle_t) data.readInt32()));
    920             return NO_ERROR;
    921         } break;
    922         case RESTORE_OUTPUT: {
    923             CHECK_INTERFACE(IAudioFlinger, data, reply);
    924             reply->writeInt32(restoreOutput((audio_io_handle_t) data.readInt32()));
    925             return NO_ERROR;
    926         } break;
    927         case OPEN_INPUT: {
    928             CHECK_INTERFACE(IAudioFlinger, data, reply);
    929             audio_module_handle_t module = (audio_module_handle_t)data.readInt32();
    930             audio_devices_t devices = (audio_devices_t)data.readInt32();
    931             uint32_t samplingRate = data.readInt32();
    932             audio_format_t format = (audio_format_t) data.readInt32();
    933             audio_channel_mask_t channelMask = (audio_channel_mask_t)data.readInt32();
    934 
    935             audio_io_handle_t input = openInput(module,
    936                                              &devices,
    937                                              &samplingRate,
    938                                              &format,
    939                                              &channelMask);
    940             reply->writeInt32((int32_t) input);
    941             reply->writeInt32(devices);
    942             reply->writeInt32(samplingRate);
    943             reply->writeInt32(format);
    944             reply->writeInt32(channelMask);
    945             return NO_ERROR;
    946         } break;
    947         case CLOSE_INPUT: {
    948             CHECK_INTERFACE(IAudioFlinger, data, reply);
    949             reply->writeInt32(closeInput((audio_io_handle_t) data.readInt32()));
    950             return NO_ERROR;
    951         } break;
    952         case SET_STREAM_OUTPUT: {
    953             CHECK_INTERFACE(IAudioFlinger, data, reply);
    954             uint32_t stream = data.readInt32();
    955             audio_io_handle_t output = (audio_io_handle_t) data.readInt32();
    956             reply->writeInt32(setStreamOutput((audio_stream_type_t) stream, output));
    957             return NO_ERROR;
    958         } break;
    959         case SET_VOICE_VOLUME: {
    960             CHECK_INTERFACE(IAudioFlinger, data, reply);
    961             float volume = data.readFloat();
    962             reply->writeInt32( setVoiceVolume(volume) );
    963             return NO_ERROR;
    964         } break;
    965         case GET_RENDER_POSITION: {
    966             CHECK_INTERFACE(IAudioFlinger, data, reply);
    967             audio_io_handle_t output = (audio_io_handle_t) data.readInt32();
    968             uint32_t halFrames;
    969             uint32_t dspFrames;
    970             status_t status = getRenderPosition(&halFrames, &dspFrames, output);
    971             reply->writeInt32(status);
    972             if (status == NO_ERROR) {
    973                 reply->writeInt32(halFrames);
    974                 reply->writeInt32(dspFrames);
    975             }
    976             return NO_ERROR;
    977         }
    978         case GET_INPUT_FRAMES_LOST: {
    979             CHECK_INTERFACE(IAudioFlinger, data, reply);
    980             audio_io_handle_t ioHandle = (audio_io_handle_t) data.readInt32();
    981             reply->writeInt32(getInputFramesLost(ioHandle));
    982             return NO_ERROR;
    983         } break;
    984         case NEW_AUDIO_SESSION_ID: {
    985             CHECK_INTERFACE(IAudioFlinger, data, reply);
    986             reply->writeInt32(newAudioSessionId());
    987             return NO_ERROR;
    988         } break;
    989         case ACQUIRE_AUDIO_SESSION_ID: {
    990             CHECK_INTERFACE(IAudioFlinger, data, reply);
    991             int audioSession = data.readInt32();
    992             acquireAudioSessionId(audioSession);
    993             return NO_ERROR;
    994         } break;
    995         case RELEASE_AUDIO_SESSION_ID: {
    996             CHECK_INTERFACE(IAudioFlinger, data, reply);
    997             int audioSession = data.readInt32();
    998             releaseAudioSessionId(audioSession);
    999             return NO_ERROR;
   1000         } break;
   1001         case QUERY_NUM_EFFECTS: {
   1002             CHECK_INTERFACE(IAudioFlinger, data, reply);
   1003             uint32_t numEffects;
   1004             status_t status = queryNumberEffects(&numEffects);
   1005             reply->writeInt32(status);
   1006             if (status == NO_ERROR) {
   1007                 reply->writeInt32((int32_t)numEffects);
   1008             }
   1009             return NO_ERROR;
   1010         }
   1011         case QUERY_EFFECT: {
   1012             CHECK_INTERFACE(IAudioFlinger, data, reply);
   1013             effect_descriptor_t desc;
   1014             status_t status = queryEffect(data.readInt32(), &desc);
   1015             reply->writeInt32(status);
   1016             if (status == NO_ERROR) {
   1017                 reply->write(&desc, sizeof(effect_descriptor_t));
   1018             }
   1019             return NO_ERROR;
   1020         }
   1021         case GET_EFFECT_DESCRIPTOR: {
   1022             CHECK_INTERFACE(IAudioFlinger, data, reply);
   1023             effect_uuid_t uuid;
   1024             data.read(&uuid, sizeof(effect_uuid_t));
   1025             effect_descriptor_t desc;
   1026             status_t status = getEffectDescriptor(&uuid, &desc);
   1027             reply->writeInt32(status);
   1028             if (status == NO_ERROR) {
   1029                 reply->write(&desc, sizeof(effect_descriptor_t));
   1030             }
   1031             return NO_ERROR;
   1032         }
   1033         case CREATE_EFFECT: {
   1034             CHECK_INTERFACE(IAudioFlinger, data, reply);
   1035             pid_t pid = data.readInt32();
   1036             effect_descriptor_t desc;
   1037             data.read(&desc, sizeof(effect_descriptor_t));
   1038             sp<IEffectClient> client = interface_cast<IEffectClient>(data.readStrongBinder());
   1039             int32_t priority = data.readInt32();
   1040             audio_io_handle_t output = (audio_io_handle_t) data.readInt32();
   1041             int sessionId = data.readInt32();
   1042             status_t status;
   1043             int id;
   1044             int enabled;
   1045 
   1046             sp<IEffect> effect = createEffect(pid, &desc, client, priority, output, sessionId, &status, &id, &enabled);
   1047             reply->writeInt32(status);
   1048             reply->writeInt32(id);
   1049             reply->writeInt32(enabled);
   1050             reply->writeStrongBinder(effect->asBinder());
   1051             reply->write(&desc, sizeof(effect_descriptor_t));
   1052             return NO_ERROR;
   1053         } break;
   1054         case MOVE_EFFECTS: {
   1055             CHECK_INTERFACE(IAudioFlinger, data, reply);
   1056             int session = data.readInt32();
   1057             audio_io_handle_t srcOutput = (audio_io_handle_t) data.readInt32();
   1058             audio_io_handle_t dstOutput = (audio_io_handle_t) data.readInt32();
   1059             reply->writeInt32(moveEffects(session, srcOutput, dstOutput));
   1060             return NO_ERROR;
   1061         } break;
   1062         case LOAD_HW_MODULE: {
   1063             CHECK_INTERFACE(IAudioFlinger, data, reply);
   1064             reply->writeInt32(loadHwModule(data.readCString()));
   1065             return NO_ERROR;
   1066         } break;
   1067         case GET_PRIMARY_OUTPUT_SAMPLING_RATE: {
   1068             CHECK_INTERFACE(IAudioFlinger, data, reply);
   1069             reply->writeInt32(getPrimaryOutputSamplingRate());
   1070             return NO_ERROR;
   1071         } break;
   1072         case GET_PRIMARY_OUTPUT_FRAME_COUNT: {
   1073             CHECK_INTERFACE(IAudioFlinger, data, reply);
   1074             reply->writeInt32(getPrimaryOutputFrameCount());
   1075             return NO_ERROR;
   1076         } break;
   1077         default:
   1078             return BBinder::onTransact(code, data, reply, flags);
   1079     }
   1080 }
   1081 
   1082 // ----------------------------------------------------------------------------
   1083 
   1084 }; // namespace android
   1085