Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ANDROID_IAUDIOFLINGER_H
     18 #define ANDROID_IAUDIOFLINGER_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 #include <unistd.h>
     23 
     24 #include <utils/RefBase.h>
     25 #include <utils/Errors.h>
     26 #include <binder/IInterface.h>
     27 #include <binder/Parcel.h>
     28 #include <binder/Parcelable.h>
     29 #include <media/AudioClient.h>
     30 #include <media/IAudioTrack.h>
     31 #include <media/IAudioFlingerClient.h>
     32 #include <system/audio.h>
     33 #include <system/audio_effect.h>
     34 #include <system/audio_policy.h>
     35 #include <media/IEffect.h>
     36 #include <media/IEffectClient.h>
     37 #include <utils/String8.h>
     38 #include <media/MicrophoneInfo.h>
     39 #include <vector>
     40 
     41 #include "android/media/IAudioRecord.h"
     42 
     43 namespace android {
     44 
     45 // ----------------------------------------------------------------------------
     46 
     47 class IAudioFlinger : public IInterface
     48 {
     49 public:
     50     DECLARE_META_INTERFACE(AudioFlinger);
     51 
     52     /* CreateTrackInput contains all input arguments sent by AudioTrack to AudioFlinger
     53      * when calling createTrack() including arguments that will be updated by AudioFlinger
     54      * and returned in CreateTrackOutput object
     55      */
     56     class CreateTrackInput : public Parcelable {
     57     public:
     58         status_t readFromParcel(const Parcel *parcel) override {
     59             /* input arguments*/
     60             memset(&attr, 0, sizeof(audio_attributes_t));
     61             if (parcel->read(&attr, sizeof(audio_attributes_t)) != NO_ERROR) {
     62                 return DEAD_OBJECT;
     63             }
     64             attr.tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE -1] = '\0';
     65             memset(&config, 0, sizeof(audio_config_t));
     66             if (parcel->read(&config, sizeof(audio_config_t)) != NO_ERROR) {
     67                 return DEAD_OBJECT;
     68             }
     69             if (clientInfo.readFromParcel(parcel) != NO_ERROR) {
     70                 return DEAD_OBJECT;
     71             }
     72             if (parcel->readInt32() != 0) {
     73                 sharedBuffer = interface_cast<IMemory>(parcel->readStrongBinder());
     74                 if (sharedBuffer == 0 || sharedBuffer->pointer() == NULL) {
     75                     return BAD_VALUE;
     76                 }
     77             }
     78             notificationsPerBuffer = parcel->readInt32();
     79             speed = parcel->readFloat();
     80 
     81             /* input/output arguments*/
     82             (void)parcel->read(&flags, sizeof(audio_output_flags_t));
     83             frameCount = parcel->readInt64();
     84             notificationFrameCount = parcel->readInt64();
     85             (void)parcel->read(&selectedDeviceId, sizeof(audio_port_handle_t));
     86             (void)parcel->read(&sessionId, sizeof(audio_session_t));
     87             return NO_ERROR;
     88         }
     89 
     90         status_t writeToParcel(Parcel *parcel) const override {
     91             /* input arguments*/
     92             (void)parcel->write(&attr, sizeof(audio_attributes_t));
     93             (void)parcel->write(&config, sizeof(audio_config_t));
     94             (void)clientInfo.writeToParcel(parcel);
     95             if (sharedBuffer != 0) {
     96                 (void)parcel->writeInt32(1);
     97                 (void)parcel->writeStrongBinder(IInterface::asBinder(sharedBuffer));
     98             } else {
     99                 (void)parcel->writeInt32(0);
    100             }
    101             (void)parcel->writeInt32(notificationsPerBuffer);
    102             (void)parcel->writeFloat(speed);
    103 
    104             /* input/output arguments*/
    105             (void)parcel->write(&flags, sizeof(audio_output_flags_t));
    106             (void)parcel->writeInt64(frameCount);
    107             (void)parcel->writeInt64(notificationFrameCount);
    108             (void)parcel->write(&selectedDeviceId, sizeof(audio_port_handle_t));
    109             (void)parcel->write(&sessionId, sizeof(audio_session_t));
    110             return NO_ERROR;
    111         }
    112 
    113         /* input */
    114         audio_attributes_t attr;
    115         audio_config_t config;
    116         AudioClient clientInfo;
    117         sp<IMemory> sharedBuffer;
    118         uint32_t notificationsPerBuffer;
    119         float speed;
    120 
    121         /* input/output */
    122         audio_output_flags_t flags;
    123         size_t frameCount;
    124         size_t notificationFrameCount;
    125         audio_port_handle_t selectedDeviceId;
    126         audio_session_t sessionId;
    127     };
    128 
    129     /* CreateTrackOutput contains all output arguments returned by AudioFlinger to AudioTrack
    130      * when calling createTrack() including arguments that were passed as I/O for update by
    131      * CreateTrackInput.
    132      */
    133     class CreateTrackOutput : public Parcelable {
    134     public:
    135         status_t readFromParcel(const Parcel *parcel) override {
    136             /* input/output arguments*/
    137             (void)parcel->read(&flags, sizeof(audio_output_flags_t));
    138             frameCount = parcel->readInt64();
    139             notificationFrameCount = parcel->readInt64();
    140             (void)parcel->read(&selectedDeviceId, sizeof(audio_port_handle_t));
    141             (void)parcel->read(&sessionId, sizeof(audio_session_t));
    142 
    143             /* output arguments*/
    144             sampleRate = parcel->readUint32();
    145             afFrameCount = parcel->readInt64();
    146             afSampleRate = parcel->readInt64();
    147             afLatencyMs = parcel->readInt32();
    148             (void)parcel->read(&outputId, sizeof(audio_io_handle_t));
    149             (void)parcel->read(&portId, sizeof(audio_port_handle_t));
    150             return NO_ERROR;
    151         }
    152 
    153         status_t writeToParcel(Parcel *parcel) const override {
    154             /* input/output arguments*/
    155             (void)parcel->write(&flags, sizeof(audio_output_flags_t));
    156             (void)parcel->writeInt64(frameCount);
    157             (void)parcel->writeInt64(notificationFrameCount);
    158             (void)parcel->write(&selectedDeviceId, sizeof(audio_port_handle_t));
    159             (void)parcel->write(&sessionId, sizeof(audio_session_t));
    160 
    161             /* output arguments*/
    162             (void)parcel->writeUint32(sampleRate);
    163             (void)parcel->writeInt64(afFrameCount);
    164             (void)parcel->writeInt64(afSampleRate);
    165             (void)parcel->writeInt32(afLatencyMs);
    166             (void)parcel->write(&outputId, sizeof(audio_io_handle_t));
    167             (void)parcel->write(&portId, sizeof(audio_port_handle_t));
    168             return NO_ERROR;
    169         }
    170 
    171         /* input/output */
    172         audio_output_flags_t flags;
    173         size_t frameCount;
    174         size_t notificationFrameCount;
    175         audio_port_handle_t selectedDeviceId;
    176         audio_session_t sessionId;
    177 
    178         /* output */
    179         uint32_t sampleRate;
    180         size_t   afFrameCount;
    181         uint32_t afSampleRate;
    182         uint32_t afLatencyMs;
    183         audio_io_handle_t outputId;
    184         audio_port_handle_t portId;
    185     };
    186 
    187     /* CreateRecordInput contains all input arguments sent by AudioRecord to AudioFlinger
    188      * when calling createRecord() including arguments that will be updated by AudioFlinger
    189      * and returned in CreateRecordOutput object
    190      */
    191     class CreateRecordInput : public Parcelable {
    192     public:
    193         status_t readFromParcel(const Parcel *parcel) override {
    194             /* input arguments*/
    195             memset(&attr, 0, sizeof(audio_attributes_t));
    196             if (parcel->read(&attr, sizeof(audio_attributes_t)) != NO_ERROR) {
    197                 return DEAD_OBJECT;
    198             }
    199             attr.tags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE -1] = '\0';
    200             memset(&config, 0, sizeof(audio_config_base_t));
    201             if (parcel->read(&config, sizeof(audio_config_base_t)) != NO_ERROR) {
    202                 return DEAD_OBJECT;
    203             }
    204             if (clientInfo.readFromParcel(parcel) != NO_ERROR) {
    205                 return DEAD_OBJECT;
    206             }
    207             opPackageName = parcel->readString16();
    208             if (parcel->read(&riid, sizeof(audio_unique_id_t)) != NO_ERROR) {
    209                 return DEAD_OBJECT;
    210             }
    211 
    212             /* input/output arguments*/
    213             (void)parcel->read(&flags, sizeof(audio_input_flags_t));
    214             frameCount = parcel->readInt64();
    215             notificationFrameCount = parcel->readInt64();
    216             (void)parcel->read(&selectedDeviceId, sizeof(audio_port_handle_t));
    217             (void)parcel->read(&sessionId, sizeof(audio_session_t));
    218             return NO_ERROR;
    219         }
    220 
    221         status_t writeToParcel(Parcel *parcel) const override {
    222             /* input arguments*/
    223             (void)parcel->write(&attr, sizeof(audio_attributes_t));
    224             (void)parcel->write(&config, sizeof(audio_config_base_t));
    225             (void)clientInfo.writeToParcel(parcel);
    226             (void)parcel->writeString16(opPackageName);
    227             (void)parcel->write(&riid, sizeof(audio_unique_id_t));
    228 
    229             /* input/output arguments*/
    230             (void)parcel->write(&flags, sizeof(audio_input_flags_t));
    231             (void)parcel->writeInt64(frameCount);
    232             (void)parcel->writeInt64(notificationFrameCount);
    233             (void)parcel->write(&selectedDeviceId, sizeof(audio_port_handle_t));
    234             (void)parcel->write(&sessionId, sizeof(audio_session_t));
    235             return NO_ERROR;
    236         }
    237 
    238         /* input */
    239         audio_attributes_t attr;
    240         audio_config_base_t config;
    241         AudioClient clientInfo;
    242         String16 opPackageName;
    243         audio_unique_id_t riid;
    244 
    245         /* input/output */
    246         audio_input_flags_t flags;
    247         size_t frameCount;
    248         size_t notificationFrameCount;
    249         audio_port_handle_t selectedDeviceId;
    250         audio_session_t sessionId;
    251     };
    252 
    253     /* CreateRecordOutput contains all output arguments returned by AudioFlinger to AudioRecord
    254      * when calling createRecord() including arguments that were passed as I/O for update by
    255      * CreateRecordInput.
    256      */
    257     class CreateRecordOutput : public Parcelable {
    258     public:
    259         status_t readFromParcel(const Parcel *parcel) override {
    260             /* input/output arguments*/
    261             (void)parcel->read(&flags, sizeof(audio_input_flags_t));
    262             frameCount = parcel->readInt64();
    263             notificationFrameCount = parcel->readInt64();
    264             (void)parcel->read(&selectedDeviceId, sizeof(audio_port_handle_t));
    265             (void)parcel->read(&sessionId, sizeof(audio_session_t));
    266 
    267             /* output arguments*/
    268             sampleRate = parcel->readUint32();
    269             (void)parcel->read(&inputId, sizeof(audio_io_handle_t));
    270             if (parcel->readInt32() != 0) {
    271                 cblk = interface_cast<IMemory>(parcel->readStrongBinder());
    272                 if (cblk == 0 || cblk->pointer() == NULL) {
    273                     return BAD_VALUE;
    274                 }
    275             }
    276             if (parcel->readInt32() != 0) {
    277                 buffers = interface_cast<IMemory>(parcel->readStrongBinder());
    278                 if (buffers == 0 || buffers->pointer() == NULL) {
    279                     return BAD_VALUE;
    280                 }
    281             }
    282             (void)parcel->read(&portId, sizeof(audio_port_handle_t));
    283             return NO_ERROR;
    284         }
    285 
    286         status_t writeToParcel(Parcel *parcel) const override {
    287             /* input/output arguments*/
    288             (void)parcel->write(&flags, sizeof(audio_input_flags_t));
    289             (void)parcel->writeInt64(frameCount);
    290             (void)parcel->writeInt64(notificationFrameCount);
    291             (void)parcel->write(&selectedDeviceId, sizeof(audio_port_handle_t));
    292             (void)parcel->write(&sessionId, sizeof(audio_session_t));
    293 
    294             /* output arguments*/
    295             (void)parcel->writeUint32(sampleRate);
    296             (void)parcel->write(&inputId, sizeof(audio_io_handle_t));
    297             if (cblk != 0) {
    298                 (void)parcel->writeInt32(1);
    299                 (void)parcel->writeStrongBinder(IInterface::asBinder(cblk));
    300             } else {
    301                 (void)parcel->writeInt32(0);
    302             }
    303             if (buffers != 0) {
    304                 (void)parcel->writeInt32(1);
    305                 (void)parcel->writeStrongBinder(IInterface::asBinder(buffers));
    306             } else {
    307                 (void)parcel->writeInt32(0);
    308             }
    309             (void)parcel->write(&portId, sizeof(audio_port_handle_t));
    310 
    311             return NO_ERROR;
    312         }
    313 
    314         /* input/output */
    315         audio_input_flags_t flags;
    316         size_t frameCount;
    317         size_t notificationFrameCount;
    318         audio_port_handle_t selectedDeviceId;
    319         audio_session_t sessionId;
    320 
    321         /* output */
    322         uint32_t sampleRate;
    323         audio_io_handle_t inputId;
    324         sp<IMemory> cblk;
    325         sp<IMemory> buffers;
    326         audio_port_handle_t portId;
    327     };
    328 
    329     // invariant on exit for all APIs that return an sp<>:
    330     //   (return value != 0) == (*status == NO_ERROR)
    331 
    332     /* create an audio track and registers it with AudioFlinger.
    333      * return null if the track cannot be created.
    334      */
    335     virtual sp<IAudioTrack> createTrack(const CreateTrackInput& input,
    336                                         CreateTrackOutput& output,
    337                                         status_t *status) = 0;
    338 
    339     virtual sp<media::IAudioRecord> createRecord(const CreateRecordInput& input,
    340                                         CreateRecordOutput& output,
    341                                         status_t *status) = 0;
    342 
    343     // FIXME Surprisingly, format/latency don't work for input handles
    344 
    345     /* query the audio hardware state. This state never changes,
    346      * and therefore can be cached.
    347      */
    348     virtual     uint32_t    sampleRate(audio_io_handle_t ioHandle) const = 0;
    349 
    350     // reserved; formerly channelCount()
    351 
    352     virtual     audio_format_t format(audio_io_handle_t output) const = 0;
    353     virtual     size_t      frameCount(audio_io_handle_t ioHandle) const = 0;
    354 
    355     // return estimated latency in milliseconds
    356     virtual     uint32_t    latency(audio_io_handle_t output) const = 0;
    357 
    358     /* set/get the audio hardware state. This will probably be used by
    359      * the preference panel, mostly.
    360      */
    361     virtual     status_t    setMasterVolume(float value) = 0;
    362     virtual     status_t    setMasterMute(bool muted) = 0;
    363 
    364     virtual     float       masterVolume() const = 0;
    365     virtual     bool        masterMute() const = 0;
    366 
    367     virtual     status_t    setMasterBalance(float balance) = 0;
    368     virtual     status_t    getMasterBalance(float *balance) const = 0;
    369 
    370     /* set/get stream type state. This will probably be used by
    371      * the preference panel, mostly.
    372      */
    373     virtual     status_t    setStreamVolume(audio_stream_type_t stream, float value,
    374                                     audio_io_handle_t output) = 0;
    375     virtual     status_t    setStreamMute(audio_stream_type_t stream, bool muted) = 0;
    376 
    377     virtual     float       streamVolume(audio_stream_type_t stream,
    378                                     audio_io_handle_t output) const = 0;
    379     virtual     bool        streamMute(audio_stream_type_t stream) const = 0;
    380 
    381     // set audio mode
    382     virtual     status_t    setMode(audio_mode_t mode) = 0;
    383 
    384     // mic mute/state
    385     virtual     status_t    setMicMute(bool state) = 0;
    386     virtual     bool        getMicMute() const = 0;
    387     virtual     void        setRecordSilenced(uid_t uid, bool silenced) = 0;
    388 
    389     virtual     status_t    setParameters(audio_io_handle_t ioHandle,
    390                                     const String8& keyValuePairs) = 0;
    391     virtual     String8     getParameters(audio_io_handle_t ioHandle, const String8& keys)
    392                                     const = 0;
    393 
    394     // Register an object to receive audio input/output change and track notifications.
    395     // For a given calling pid, AudioFlinger disregards any registrations after the first.
    396     // Thus the IAudioFlingerClient must be a singleton per process.
    397     virtual void registerClient(const sp<IAudioFlingerClient>& client) = 0;
    398 
    399     // retrieve the audio recording buffer size
    400     // FIXME This API assumes a route, and so should be deprecated.
    401     virtual size_t getInputBufferSize(uint32_t sampleRate, audio_format_t format,
    402             audio_channel_mask_t channelMask) const = 0;
    403 
    404     virtual status_t openOutput(audio_module_handle_t module,
    405                                 audio_io_handle_t *output,
    406                                 audio_config_t *config,
    407                                 audio_devices_t *devices,
    408                                 const String8& address,
    409                                 uint32_t *latencyMs,
    410                                 audio_output_flags_t flags) = 0;
    411     virtual audio_io_handle_t openDuplicateOutput(audio_io_handle_t output1,
    412                                     audio_io_handle_t output2) = 0;
    413     virtual status_t closeOutput(audio_io_handle_t output) = 0;
    414     virtual status_t suspendOutput(audio_io_handle_t output) = 0;
    415     virtual status_t restoreOutput(audio_io_handle_t output) = 0;
    416 
    417     virtual status_t openInput(audio_module_handle_t module,
    418                                audio_io_handle_t *input,
    419                                audio_config_t *config,
    420                                audio_devices_t *device,
    421                                const String8& address,
    422                                audio_source_t source,
    423                                audio_input_flags_t flags) = 0;
    424     virtual status_t closeInput(audio_io_handle_t input) = 0;
    425 
    426     virtual status_t invalidateStream(audio_stream_type_t stream) = 0;
    427 
    428     virtual status_t setVoiceVolume(float volume) = 0;
    429 
    430     virtual status_t getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
    431                                     audio_io_handle_t output) const = 0;
    432 
    433     virtual uint32_t getInputFramesLost(audio_io_handle_t ioHandle) const = 0;
    434 
    435     virtual audio_unique_id_t newAudioUniqueId(audio_unique_id_use_t use) = 0;
    436 
    437     virtual void acquireAudioSessionId(audio_session_t audioSession, pid_t pid) = 0;
    438     virtual void releaseAudioSessionId(audio_session_t audioSession, pid_t pid) = 0;
    439 
    440     virtual status_t queryNumberEffects(uint32_t *numEffects) const = 0;
    441 
    442     virtual status_t queryEffect(uint32_t index, effect_descriptor_t *pDescriptor) const = 0;
    443 
    444     virtual status_t getEffectDescriptor(const effect_uuid_t *pEffectUUID,
    445                                          const effect_uuid_t *pTypeUUID,
    446                                          uint32_t preferredTypeFlag,
    447                                          effect_descriptor_t *pDescriptor) const = 0;
    448 
    449     virtual sp<IEffect> createEffect(
    450                                     effect_descriptor_t *pDesc,
    451                                     const sp<IEffectClient>& client,
    452                                     int32_t priority,
    453                                     // AudioFlinger doesn't take over handle reference from client
    454                                     audio_io_handle_t output,
    455                                     audio_session_t sessionId,
    456                                     const String16& callingPackage,
    457                                     pid_t pid,
    458                                     status_t *status,
    459                                     int *id,
    460                                     int *enabled) = 0;
    461 
    462     virtual status_t moveEffects(audio_session_t session, audio_io_handle_t srcOutput,
    463                                     audio_io_handle_t dstOutput) = 0;
    464 
    465     virtual void setEffectSuspended(int effectId,
    466                                     audio_session_t sessionId,
    467                                     bool suspended) = 0;
    468 
    469     virtual audio_module_handle_t loadHwModule(const char *name) = 0;
    470 
    471     // helpers for android.media.AudioManager.getProperty(), see description there for meaning
    472     // FIXME move these APIs to AudioPolicy to permit a more accurate implementation
    473     // that looks on primary device for a stream with fast flag, primary flag, or first one.
    474     virtual uint32_t getPrimaryOutputSamplingRate() = 0;
    475     virtual size_t getPrimaryOutputFrameCount() = 0;
    476 
    477     // Intended for AudioService to inform AudioFlinger of device's low RAM attribute,
    478     // and should be called at most once.  For a definition of what "low RAM" means, see
    479     // android.app.ActivityManager.isLowRamDevice().  The totalMemory parameter
    480     // is obtained from android.app.ActivityManager.MemoryInfo.totalMem.
    481     virtual status_t setLowRamDevice(bool isLowRamDevice, int64_t totalMemory) = 0;
    482 
    483     /* List available audio ports and their attributes */
    484     virtual status_t listAudioPorts(unsigned int *num_ports,
    485                                     struct audio_port *ports) = 0;
    486 
    487     /* Get attributes for a given audio port */
    488     virtual status_t getAudioPort(struct audio_port *port) = 0;
    489 
    490     /* Create an audio patch between several source and sink ports */
    491     virtual status_t createAudioPatch(const struct audio_patch *patch,
    492                                        audio_patch_handle_t *handle) = 0;
    493 
    494     /* Release an audio patch */
    495     virtual status_t releaseAudioPatch(audio_patch_handle_t handle) = 0;
    496 
    497     /* List existing audio patches */
    498     virtual status_t listAudioPatches(unsigned int *num_patches,
    499                                       struct audio_patch *patches) = 0;
    500     /* Set audio port configuration */
    501     virtual status_t setAudioPortConfig(const struct audio_port_config *config) = 0;
    502 
    503     /* Get the HW synchronization source used for an audio session */
    504     virtual audio_hw_sync_t getAudioHwSyncForSession(audio_session_t sessionId) = 0;
    505 
    506     /* Indicate JAVA services are ready (scheduling, power management ...) */
    507     virtual status_t systemReady() = 0;
    508 
    509     // Returns the number of frames per audio HAL buffer.
    510     virtual size_t frameCountHAL(audio_io_handle_t ioHandle) const = 0;
    511 
    512     /* List available microphones and their characteristics */
    513     virtual status_t getMicrophones(std::vector<media::MicrophoneInfo> *microphones) = 0;
    514 };
    515 
    516 
    517 // ----------------------------------------------------------------------------
    518 
    519 class BnAudioFlinger : public BnInterface<IAudioFlinger>
    520 {
    521 public:
    522     virtual status_t    onTransact( uint32_t code,
    523                                     const Parcel& data,
    524                                     Parcel* reply,
    525                                     uint32_t flags = 0);
    526 
    527     // Requests media.log to start merging log buffers
    528     virtual void requestLogMerge() = 0;
    529 };
    530 
    531 // ----------------------------------------------------------------------------
    532 
    533 }; // namespace android
    534 
    535 #endif // ANDROID_IAUDIOFLINGER_H
    536