Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2729ea92 authored by The Android Open Source Project's avatar The Android Open Source Project
Browse files

Initial Contribution

parents
Loading
Loading
Loading
Loading
+235 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef AUDIORECORD_H_
#define AUDIORECORD_H_

#include <stdint.h>
#include <sys/types.h>

#include <media/IAudioFlinger.h>
#include <media/IAudioRecord.h>
#include <media/AudioTrack.h>

#include <utils/RefBase.h>
#include <utils/Errors.h>
#include <utils/IInterface.h>
#include <utils/IMemory.h>
#include <utils/threads.h>


namespace android {

// ----------------------------------------------------------------------------

class AudioRecord
{
public: 
    
    enum stream_type {
        DEFAULT_INPUT   =-1,
        MIC_INPUT       = 0,
        NUM_STREAM_TYPES
    };
    
    static const int DEFAULT_SAMPLE_RATE = 8000; 
    
    /* Create Buffer on the stack and pass it to obtainBuffer()
     * and releaseBuffer().
     */

    class Buffer
    {
    public:
        enum {
            MUTE    = 0x00000001
        };
        uint32_t    flags;
        int         channelCount;
        int         format;
        size_t      frameCount;
        size_t      size;
        union {
            void*       raw;
            short*      i16;
            int8_t*     i8;
        };
    };

    /* These are static methods to control the system-wide AudioFlinger
     * only privileged processes can have access to them
     */

//    static status_t setMasterMute(bool mute);

    /* Returns AudioFlinger's frame count. AudioRecord's buffers will
     * be created with this size.
     */
    static  size_t      frameCount();

    /* As a convenience, if a callback is supplied, a handler thread
     * is automatically created with the appropriate priority. This thread
     * invokes the callback when a new buffer becomes availlable.
     */
    typedef bool (*callback_t)(void* user, const Buffer& info);

    /* Constructs an uninitialized AudioRecord. No connection with
     * AudioFlinger takes place.
     */
                        AudioRecord();
                        
    /* Creates an AudioRecord track and registers it with AudioFlinger.
     * Once created, the track needs to be started before it can be used.
     * Unspecified values are set to the audio hardware's current
     * values.
     */
     
                        AudioRecord(int streamType      = 0,
                                    uint32_t sampleRate = 0,
                                    int format          = 0,
                                    int channelCount    = 0,
                                    int bufferCount     = 0,
                                    uint32_t flags      = 0,
                                    callback_t cbf = 0, void* user = 0);


    /* Terminates the AudioRecord and unregisters it from AudioFlinger.
     * Also destroys all resources assotiated with the AudioRecord.
     */ 
                        ~AudioRecord();


    /* Initialize an uninitialized AudioRecord. */
            status_t    set(int streamType      = 0,
                            uint32_t sampleRate = 0,
                            int format          = 0,
                            int channelCount    = 0,
                            int bufferCount     = 0,
                            uint32_t flags      = 0,
                            callback_t cbf = 0, void* user = 0);
        

    /* Result of constructing the AudioRecord. This must be checked
     * before using any AudioRecord API (except for set()), using
     * an uninitialized AudioRecord prduces undefined results.
     */
            status_t    initCheck() const;

    /* Returns this track's latency in nanoseconds or framecount.
     * This only includes the latency due to the fill buffer size.
     * In particular, the hardware or driver latencies are not accounted.
     */
            nsecs_t     latency() const;

   /* getters, see constructor */ 
            
            uint32_t    sampleRate() const;
            int         format() const;
            int         channelCount() const;
            int         bufferCount() const;


    /* After it's created the track is not active. Call start() to
     * make it active. If set, the callback will start being called.
     */
            status_t    start();

    /* Stop a track. If set, the callback will cease being called and
     * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
     * and will fill up buffers until the pool is exhausted.
     */
            status_t    stop();
            bool        stopped() const;

    /* get sample rate for this track
     */
            uint32_t    getSampleRate();

    /* obtains a buffer of "frameCount" frames. The buffer must be
     * filled entirely. If the track is stopped, obtainBuffer() returns
     * STOPPED instead of NO_ERROR as long as there are buffers availlable,
     * at which point NO_MORE_BUFFERS is returned.
     * Buffers will be returned until the pool (buffercount())
     * is exhausted, at which point obtainBuffer() will either block 
     * or return WOULD_BLOCK depending on the value of the "blocking"
     * parameter. 
     */
     
        enum {
            NO_MORE_BUFFERS = 0x80000001,
            STOPPED = 1
        };
     
            status_t    obtainBuffer(Buffer* audioBuffer, bool blocking);
            void        releaseBuffer(Buffer* audioBuffer);


    /* As a convenience we provide a read() interface to the audio buffer.
     * This is implemented on top of lockBuffer/unlockBuffer. 
     */
            ssize_t     read(void* buffer, size_t size);

private:
    /* copying audio tracks is not allowed */
                        AudioRecord(const AudioRecord& other);
            AudioRecord& operator = (const AudioRecord& other);

    /* a small internal class to handle the callback */
    class ClientRecordThread : public Thread
    {
    public:
        ClientRecordThread(AudioRecord& receiver);
    private:
        friend class AudioRecord;
        virtual bool        threadLoop();
        virtual status_t    readyToRun() { return NO_ERROR; }
        virtual void        onFirstRef() {}
        AudioRecord& mReceiver;
    };
    
            bool processAudioBuffer(const sp<ClientRecordThread>& thread);

    sp<IAudioFlinger>       mAudioFlinger;
    sp<IAudioRecord>        mAudioRecord;
    sp<IMemory>             mCblkMemory;
    sp<ClientRecordThread>  mClientRecordThread;
    Mutex                   mRecordThreadLock;
    
    uint32_t                mSampleRate;
    size_t                  mFrameCount;

    audio_track_cblk_t*     mCblk;
    uint8_t                 mFormat;
    uint8_t                 mBufferCount;
    uint8_t                 mChannelCount   : 4;
    uint8_t                 mReserved       : 3;
    status_t                mStatus;
    nsecs_t                 mLatency;

    volatile int32_t        mActive;

    callback_t              mCbf;
    void*                   mUserData;
    
    AudioRecord::Buffer      mAudioBuffer;
    size_t                  mPosition;

    uint32_t                mReservedFBC[4];
};

}; // namespace android

#endif /*AUDIORECORD_H_*/
+122 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_AUDIOSYSTEM_H_
#define ANDROID_AUDIOSYSTEM_H_

#include <utils/RefBase.h>
#include <utils/threads.h>
#include <media/IAudioFlinger.h>

namespace android {

typedef void (*audio_error_callback)(status_t err);

class AudioSystem
{
public:

    enum audio_format {
        DEFAULT = 0,
        PCM_16_BIT,
        PCM_8_BIT,
        INVALID_FORMAT
    };

    enum audio_mode {
        MODE_INVALID = -2,
        MODE_CURRENT = -1,
        MODE_NORMAL = 0,
        MODE_RINGTONE,
        MODE_IN_CALL,
        NUM_MODES  // not a valid entry, denotes end-of-list
    };

    enum audio_routes {
        ROUTE_EARPIECE       = (1 << 0),
        ROUTE_SPEAKER        = (1 << 1),
        ROUTE_BLUETOOTH      = (1 << 2),
        ROUTE_HEADSET        = (1 << 3),
        ROUTE_ALL       = (ROUTE_EARPIECE | ROUTE_SPEAKER | ROUTE_BLUETOOTH | ROUTE_HEADSET)
    };

    /* These are static methods to control the system-wide AudioFlinger
     * only privileged processes can have access to them
     */

    // routing helper functions
    static status_t speakerphone(bool state);
    static status_t isSpeakerphoneOn(bool* state);
    static status_t bluetoothSco(bool state);
    static status_t isBluetoothScoOn(bool* state);
    static status_t muteMicrophone(bool state);
    static status_t isMicrophoneMuted(bool *state);

    static status_t setMasterVolume(float value);
    static status_t setMasterMute(bool mute);
    static status_t getMasterVolume(float* volume);
    static status_t getMasterMute(bool* mute);

    static status_t setStreamVolume(int stream, float value);
    static status_t setStreamMute(int stream, bool mute);
    static status_t getStreamVolume(int stream, float* volume);
    static status_t getStreamMute(int stream, bool* mute);

    static status_t setMode(int mode);
    static status_t getMode(int* mode);

    static status_t setRouting(int mode, uint32_t routes, uint32_t mask);
    static status_t getRouting(int mode, uint32_t* routes);

    static status_t isMusicActive(bool *state);

    // Temporary interface, do not use
    // TODO: Replace with a more generic key:value get/set mechanism
    static status_t setParameter(const char* key, const char* value);
    
    static void setErrorCallback(audio_error_callback cb);

    // helper function to obtain AudioFlinger service handle
    static const sp<IAudioFlinger>& get_audio_flinger();

    static float linearToLog(int volume);
    static int logToLinear(float volume);

    // ----------------------------------------------------------------------------

private:

    class DeathNotifier: public IBinder::DeathRecipient
    {
    public:
        DeathNotifier() {      
        }
        
        virtual void binderDied(const wp<IBinder>& who);
    };

    static sp<DeathNotifier> gDeathNotifier;

    friend class DeathNotifier;

    static Mutex gLock;
    static sp<IAudioFlinger> gAudioFlinger;
    static audio_error_callback gAudioErrorCallback;
};

};  // namespace android

#endif  /*ANDROID_AUDIOSYSTEM_H_*/
+277 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_AUDIOTRACK_H
#define ANDROID_AUDIOTRACK_H

#include <stdint.h>
#include <sys/types.h>

#include <media/IAudioFlinger.h>
#include <media/IAudioTrack.h>
#include <media/AudioSystem.h>

#include <utils/RefBase.h>
#include <utils/Errors.h>
#include <utils/IInterface.h>
#include <utils/IMemory.h>
#include <utils/threads.h>


namespace android {

// ----------------------------------------------------------------------------

class audio_track_cblk_t;

// ----------------------------------------------------------------------------

class AudioTrack
{
public: 

    enum stream_type {
        DEFAULT     =-1,
        VOICE_CALL  = 0,
        SYSTEM      = 1,
        RING        = 2,
        MUSIC       = 3,
        ALARM       = 4,
        NUM_STREAM_TYPES
    };

    enum channel_index {
        MONO   = 0,
        LEFT   = 0,
        RIGHT  = 1
    };

    /* Create Buffer on the stack and pass it to obtainBuffer()
     * and releaseBuffer().
     */

    class Buffer
    {
    public:
        enum {
            MUTE    = 0x00000001
        };
        uint32_t    flags;
        int         channelCount;
        int         format;
        size_t      frameCount;
        size_t      size;
        union {
            void*       raw;
            short*      i16;
            int8_t*     i8;
        };
    };

    /* Returns AudioFlinger's frame count. AudioTrack's buffers will
     * be created with this size.
     */
    static  size_t      frameCount();

    /* As a convenience, if a callback is supplied, a handler thread
     * is automatically created with the appropriate priority. This thread
     * invokes the callback when a new buffer becomes availlable.
     */
    typedef void (*callback_t)(void* user, const Buffer& info);

    /* Constructs an uninitialized AudioTrack. No connection with
     * AudioFlinger takes place.
     */
                        AudioTrack();
                        
    /* Creates an audio track and registers it with AudioFlinger.
     * Once created, the track needs to be started before it can be used.
     * Unspecified values are set to the audio hardware's current
     * values.
     */
     
                        AudioTrack( int streamType,
                                    uint32_t sampleRate = 0,
                                    int format          = 0,
                                    int channelCount    = 0,
                                    int bufferCount     = 0,
                                    uint32_t flags      = 0,
                                    callback_t cbf = 0, void* user = 0);


    /* Terminates the AudioTrack and unregisters it from AudioFlinger.
     * Also destroys all resources assotiated with the AudioTrack.
     */ 
                        ~AudioTrack();


    /* Initialize an uninitialized AudioTrack. */
            status_t    set(int streamType      =-1,
                            uint32_t sampleRate = 0,
                            int format          = 0,
                            int channelCount    = 0,
                            int bufferCount     = 0,
                            uint32_t flags      = 0,
                            callback_t cbf = 0, void* user = 0);
        

    /* Result of constructing the AudioTrack. This must be checked
     * before using any AudioTrack API (except for set()), using
     * an uninitialized AudoiTrack prduces undefined results.
     */
            status_t    initCheck() const;

    /* Returns this track's latency in nanoseconds or framecount.
     * This only includes the latency due to the fill buffer size.
     * In particular, the hardware or driver latencies are not accounted.
     */
            nsecs_t     latency() const;

   /* getters, see constructor */ 
            
            int         streamType() const;
            uint32_t    sampleRate() const;
            int         format() const;
            int         channelCount() const;
            int         bufferCount() const;


    /* After it's created the track is not active. Call start() to
     * make it active. If set, the callback will start being called.
     */
            void        start();

    /* Stop a track. If set, the callback will cease being called and
     * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
     * and will fill up buffers until the pool is exhausted.
     */
            void        stop();
            bool        stopped() const;

    /* flush a stopped track. All pending buffers are discarded.
     * This function has no effect if the track is not stoped.
     */
            void        flush();

    /* Pause a track. If set, the callback will cease being called and
     * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
     * and will fill up buffers until the pool is exhausted.
     */
            void        pause();

    /* mute or unmutes this track.
     * While mutted, the callback, if set, is still called.
     */
            void        mute(bool);
            bool        muted() const;


    /* set volume for this track, mostly used for games' sound effects
     */
            void        setVolume(float left, float right);
            void        getVolume(float* left, float* right);

    /* set sample rate for this track, mostly used for games' sound effects
     */
            void        setSampleRate(int sampleRate);
            uint32_t    getSampleRate();

    /* obtains a buffer of "frameCount" frames. The buffer must be
     * filled entirely. If the track is stopped, obtainBuffer() returns
     * STOPPED instead of NO_ERROR as long as there are buffers availlable,
     * at which point NO_MORE_BUFFERS is returned.
     * Buffers will be returned until the pool (buffercount())
     * is exhausted, at which point obtainBuffer() will either block 
     * or return WOULD_BLOCK depending on the value of the "blocking"
     * parameter. 
     */
     
        enum {
            NO_MORE_BUFFERS = 0x80000001,
            STOPPED = 1
        };
     
            status_t    obtainBuffer(Buffer* audioBuffer, bool blocking);
            void        releaseBuffer(Buffer* audioBuffer);


    /* As a convenience we provide a write() interface to the audio buffer.
     * This is implemented on top of lockBuffer/unlockBuffer. For best
     * performance
     * 
     */
            ssize_t     write(const void* buffer, size_t size);
            
    /*
     * Dumps the state of an audio track.
     */
            status_t dump(int fd, const Vector<String16>& args) const;

private:
    /* copying audio tracks is not allowed */
                        AudioTrack(const AudioTrack& other);
            AudioTrack& operator = (const AudioTrack& other);

    /* a small internal class to handle the callback */
    class AudioTrackThread : public Thread
    {
    public:
        AudioTrackThread(AudioTrack& receiver);
    private:
        friend class AudioTrack;
        virtual bool        threadLoop();
        virtual status_t    readyToRun();
        virtual void        onFirstRef();
        AudioTrack& mReceiver;
        Mutex       mLock;
    };
    
            bool processAudioBuffer(const sp<AudioTrackThread>& thread);

    sp<IAudioFlinger>       mAudioFlinger;
    sp<IAudioTrack>         mAudioTrack;
    sp<IMemory>             mCblkMemory;
    sp<AudioTrackThread>    mAudioTrackThread;
    
    float                   mVolume[2];
    uint32_t                mSampleRate;
    size_t                  mFrameCount;

    audio_track_cblk_t*     mCblk;
    uint8_t                 mStreamType;
    uint8_t                 mFormat;
    uint8_t                 mBufferCount;
    uint8_t                 mChannelCount   : 4;
    uint8_t                 mMuted          : 1;
    uint8_t                 mReserved       : 2;
    status_t                mStatus;
    nsecs_t                 mLatency;

    volatile int32_t        mActive;

    callback_t              mCbf;
    void*                   mUserData;
    
    AudioTrack::Buffer      mAudioBuffer;
    size_t                  mPosition;

    uint32_t                mReservedFBC[4];
};


}; // namespace android

#endif // ANDROID_AUDIOTRACK_H
+124 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_IAUDIOFLINGER_H
#define ANDROID_IAUDIOFLINGER_H

#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>

#include <utils/RefBase.h>
#include <utils/Errors.h>
#include <utils/IInterface.h>
#include <media/IAudioTrack.h>
#include <media/IAudioRecord.h>


namespace android {

// ----------------------------------------------------------------------------

class IAudioFlinger : public IInterface
{
public:
    DECLARE_META_INTERFACE(AudioFlinger);

    /* create an audio track and registers it with AudioFlinger.
     * return null if the track cannot be created.
     */
    virtual sp<IAudioTrack> createTrack(
                                pid_t pid,
                                int streamType,
                                uint32_t sampleRate,
                                int format,
                                int channelCount,
                                int bufferCount,
                                uint32_t flags) = 0;

    virtual sp<IAudioRecord> openRecord(
                                pid_t pid,
                                int streamType,
                                uint32_t sampleRate,
                                int format,
                                int channelCount,
                                int bufferCount,
                                uint32_t flags) = 0;

    /* query the audio hardware state. This state never changes,
     * and therefore can be cached.
     */
    virtual     uint32_t    sampleRate() const = 0;
    virtual     int         channelCount() const = 0;
    virtual     int         format() const = 0;
    virtual     size_t      frameCount() const = 0;

    /* set/get the audio hardware state. This will probably be used by
     * the preference panel, mostly.
     */
    virtual     status_t    setMasterVolume(float value) = 0;
    virtual     status_t    setMasterMute(bool muted) = 0;

    virtual     float       masterVolume() const = 0;
    virtual     bool        masterMute() const = 0;

    /* set/get stream type state. This will probably be used by
     * the preference panel, mostly.
     */
    virtual     status_t    setStreamVolume(int stream, float value) = 0;
    virtual     status_t    setStreamMute(int stream, bool muted) = 0;

    virtual     float       streamVolume(int stream) const = 0;
    virtual     bool        streamMute(int stream) const = 0;

    // set/get audio routing
    virtual     status_t    setRouting(int mode, uint32_t routes, uint32_t mask) = 0;
    virtual     uint32_t    getRouting(int mode) const = 0;

    // set/get audio mode
    virtual     status_t    setMode(int mode) = 0;
    virtual     int         getMode() const = 0;

    // mic mute/state
    virtual     status_t    setMicMute(bool state) = 0;
    virtual     bool        getMicMute() const = 0;

    // is a music stream active?
    virtual     bool        isMusicActive() const = 0;

    // pass a generic configuration parameter to libaudio
    // Temporary interface, do not use
    // TODO: Replace with a more generic key:value get/set mechanism
    virtual     status_t  setParameter(const char* key, const char* value) = 0;
};


// ----------------------------------------------------------------------------

class BnAudioFlinger : public BnInterface<IAudioFlinger>
{
public:
    virtual status_t    onTransact( uint32_t code,
                                    const Parcel& data,
                                    Parcel* reply,
                                    uint32_t flags = 0);
};

// ----------------------------------------------------------------------------

}; // namespace android

#endif // ANDROID_IAUDIOFLINGER_H
+0 −0

File added.

Preview size limit exceeded, changes collapsed.