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

Commit 3316d5e6 authored by Phil Burk's avatar Phil Burk
Browse files

libaaudio: changes for API council



Removed typedefs like aaudio_sample_rate_t
Removed use of handles. Just pass back opaque pointers.
Simplified gettersi in Stream.
Removed getters from Builder.
Update libaaudio.map.txt

Test: CTS test_aaudio.cpp
Change-Id: I63eaec3e5a8ecc516cfc1f950f4b4f54df1bd518
Signed-off-by: default avatarPhil Burk <philburk@google.com>
parent 3db4f4f8
Loading
Loading
Loading
Loading
+24 −37
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@

#define SAMPLE_RATE   48000
#define NUM_SECONDS   10
#define NANOS_PER_MICROSECOND ((int64_t)1000)
#define NANOS_PER_MILLISECOND (NANOS_PER_MICROSECOND * 1000)

static const char *getSharingModeText(aaudio_sharing_mode_t mode) {
    const char *modeText = "unknown";
@@ -51,18 +53,18 @@ int main(int argc, char **argv)
    int actualSamplesPerFrame = 0;
    const int requestedSampleRate = SAMPLE_RATE;
    int actualSampleRate = 0;
    const aaudio_audio_format_t requestedDataFormat = AAUDIO_FORMAT_PCM16;
    aaudio_audio_format_t actualDataFormat = AAUDIO_FORMAT_PCM16;
    const aaudio_audio_format_t requestedDataFormat = AAUDIO_FORMAT_PCM_I16;
    aaudio_audio_format_t actualDataFormat = AAUDIO_FORMAT_PCM_I16;

    const aaudio_sharing_mode_t requestedSharingMode = AAUDIO_SHARING_MODE_EXCLUSIVE;
    aaudio_sharing_mode_t actualSharingMode = AAUDIO_SHARING_MODE_SHARED;

    AAudioStreamBuilder aaudioBuilder = AAUDIO_STREAM_BUILDER_NONE;
    AAudioStream aaudioStream = AAUDIO_STREAM_NONE;
    AAudioStreamBuilder aaudioBuilder = nullptr;
    AAudioStream aaudioStream = nullptr;
    aaudio_stream_state_t state = AAUDIO_STREAM_STATE_UNINITIALIZED;
    aaudio_size_frames_t framesPerBurst = 0;
    aaudio_size_frames_t framesToPlay = 0;
    aaudio_size_frames_t framesLeft = 0;
    int32_t framesPerBurst = 0;
    int32_t framesToPlay = 0;
    int32_t framesLeft = 0;
    int32_t xRunCount = 0;
    int16_t *data = nullptr;

@@ -82,57 +84,42 @@ int main(int argc, char **argv)
    }

    // Request stream properties.
    result = AAudioStreamBuilder_setSampleRate(aaudioBuilder, requestedSampleRate);
    if (result != AAUDIO_OK) {
        goto finish;
    }
    result = AAudioStreamBuilder_setSamplesPerFrame(aaudioBuilder, requestedSamplesPerFrame);
    if (result != AAUDIO_OK) {
        goto finish;
    }
    result = AAudioStreamBuilder_setFormat(aaudioBuilder, requestedDataFormat);
    if (result != AAUDIO_OK) {
        goto finish;
    }
    result = AAudioStreamBuilder_setSharingMode(aaudioBuilder, requestedSharingMode);
    if (result != AAUDIO_OK) {
        goto finish;
    }
    AAudioStreamBuilder_setSampleRate(aaudioBuilder, requestedSampleRate);
    AAudioStreamBuilder_setSamplesPerFrame(aaudioBuilder, requestedSamplesPerFrame);
    AAudioStreamBuilder_setFormat(aaudioBuilder, requestedDataFormat);
    AAudioStreamBuilder_setSharingMode(aaudioBuilder, requestedSharingMode);


    // Create an AAudioStream using the Builder.
    result = AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream);
    printf("aaudioStream 0x%08x\n", aaudioStream);
    if (result != AAUDIO_OK) {
        goto finish;
    }

    result = AAudioStream_getState(aaudioStream, &state);
    state = AAudioStream_getState(aaudioStream);
    printf("after open, state = %s\n", AAudio_convertStreamStateToText(state));

    // Check to see what kind of stream we actually got.
    result = AAudioStream_getSampleRate(aaudioStream, &actualSampleRate);
    actualSampleRate = AAudioStream_getSampleRate(aaudioStream);
    printf("SampleRate: requested = %d, actual = %d\n", requestedSampleRate, actualSampleRate);

    sineOsc1.setup(440.0, actualSampleRate);
    sineOsc2.setup(660.0, actualSampleRate);

    result = AAudioStream_getSamplesPerFrame(aaudioStream, &actualSamplesPerFrame);
    actualSamplesPerFrame = AAudioStream_getSamplesPerFrame(aaudioStream);
    printf("SamplesPerFrame: requested = %d, actual = %d\n",
            requestedSamplesPerFrame, actualSamplesPerFrame);

    result = AAudioStream_getSharingMode(aaudioStream, &actualSharingMode);
    actualSharingMode = AAudioStream_getSharingMode(aaudioStream);
    printf("SharingMode: requested = %s, actual = %s\n",
            getSharingModeText(requestedSharingMode),
            getSharingModeText(actualSharingMode));

    // This is the number of frames that are read in one chunk by a DMA controller
    // or a DSP or a mixer.
    result = AAudioStream_getFramesPerBurst(aaudioStream, &framesPerBurst);
    framesPerBurst = AAudioStream_getFramesPerBurst(aaudioStream);
    printf("DataFormat: original framesPerBurst = %d\n",framesPerBurst);
    if (result != AAUDIO_OK) {
        fprintf(stderr, "ERROR - AAudioStream_getFramesPerBurst() returned %d\n", result);
        goto finish;
    }

    // Some DMA might use very short bursts of 16 frames. We don't need to write such small
    // buffers. But it helps to use a multiple of the burst size for predictable scheduling.
    while (framesPerBurst < 48) {
@@ -140,7 +127,7 @@ int main(int argc, char **argv)
    }
    printf("DataFormat: final framesPerBurst = %d\n",framesPerBurst);

    AAudioStream_getFormat(aaudioStream, &actualDataFormat);
    actualDataFormat = AAudioStream_getFormat(aaudioStream);
    printf("DataFormat: requested = %d, actual = %d\n", requestedDataFormat, actualDataFormat);
    // TODO handle other data formats

@@ -160,7 +147,7 @@ int main(int argc, char **argv)
        goto finish;
    }

    result = AAudioStream_getState(aaudioStream, &state);
    state = AAudioStream_getState(aaudioStream);
    printf("after start, state = %s\n", AAudio_convertStreamStateToText(state));

    // Play for a while.
@@ -174,7 +161,7 @@ int main(int argc, char **argv)
        }

        // Write audio data to the stream.
        aaudio_nanoseconds_t timeoutNanos = 100 * AAUDIO_NANOS_PER_MILLISECOND;
        int64_t timeoutNanos = 100 * NANOS_PER_MILLISECOND;
        int minFrames = (framesToPlay < framesPerBurst) ? framesToPlay : framesPerBurst;
        int actual = AAudioStream_write(aaudioStream, data, minFrames, timeoutNanos);
        if (actual < 0) {
@@ -187,7 +174,7 @@ int main(int argc, char **argv)
        framesLeft -= actual;
    }

    result = AAudioStream_getXRunCount(aaudioStream, &xRunCount);
    xRunCount = AAudioStream_getXRunCount(aaudioStream);
    printf("AAudioStream_getXRunCount %d\n", xRunCount);

finish:
+36 −45
Original line number Diff line number Diff line
@@ -27,13 +27,17 @@
#include "SineGenerator.h"

#define NUM_SECONDS           10
#define NANOS_PER_MICROSECOND ((int64_t)1000)
#define NANOS_PER_MILLISECOND (NANOS_PER_MICROSECOND * 1000)
#define MILLIS_PER_SECOND     1000
#define NANOS_PER_SECOND      (NANOS_PER_MILLISECOND * MILLIS_PER_SECOND)

#define SHARING_MODE  AAUDIO_SHARING_MODE_EXCLUSIVE
//#define SHARING_MODE  AAUDIO_SHARING_MODE_SHARED
//#define SHARING_MODE  AAUDIO_SHARING_MODE_EXCLUSIVE
#define SHARING_MODE  AAUDIO_SHARING_MODE_SHARED

// Prototype for a callback.
typedef int audio_callback_proc_t(float *outputBuffer,
                                     aaudio_size_frames_t numFrames,
                                     int32_t numFrames,
                                     void *userContext);

static void *SimpleAAudioPlayerThreadProc(void *arg);
@@ -75,33 +79,27 @@ public:
        result = AAudio_createStreamBuilder(&mBuilder);
        if (result != AAUDIO_OK) return result;

        result = AAudioStreamBuilder_setSharingMode(mBuilder, mRequestedSharingMode);
        if (result != AAUDIO_OK) goto finish1;
        AAudioStreamBuilder_setSharingMode(mBuilder, mRequestedSharingMode);

        // Open an AAudioStream using the Builder.
        result = AAudioStreamBuilder_openStream(mBuilder, &mStream);
        if (result != AAUDIO_OK) goto finish1;

        // Check to see what kind of stream we actually got.
        result = AAudioStream_getSampleRate(mStream, &mFramesPerSecond);
        mFramesPerSecond = AAudioStream_getSampleRate(mStream);
        printf("open() mFramesPerSecond = %d\n", mFramesPerSecond);
        if (result != AAUDIO_OK) goto finish2;

        result = AAudioStream_getSamplesPerFrame(mStream, &mSamplesPerFrame);
        mSamplesPerFrame = AAudioStream_getSamplesPerFrame(mStream);
        printf("open() mSamplesPerFrame = %d\n", mSamplesPerFrame);
        if (result != AAUDIO_OK) goto finish2;

        {
            aaudio_size_frames_t bufferCapacity;
            result = AAudioStream_getBufferCapacity(mStream, &bufferCapacity);
            if (result != AAUDIO_OK) goto finish2;
            int32_t bufferCapacity = AAudioStream_getBufferCapacityInFrames(mStream);
            printf("open() got bufferCapacity = %d\n", bufferCapacity);
        }

        // This is the number of frames that are read in one chunk by a DMA controller
        // or a DSP or a mixer.
        result = AAudioStream_getFramesPerBurst(mStream, &mFramesPerBurst);
        if (result != AAUDIO_OK) goto finish2;
        mFramesPerBurst = AAudioStream_getFramesPerBurst(mStream);
        // Some DMA might use very short bursts. We don't need to write such small
        // buffers. But it helps to use a multiple of the burst size for predictable scheduling.
        while (mFramesPerBurst < 48) {
@@ -109,11 +107,7 @@ public:
        }
        printf("DataFormat: final framesPerBurst = %d\n",mFramesPerBurst);

        result = AAudioStream_getFormat(mStream, &mDataFormat);
        if (result != AAUDIO_OK) {
            fprintf(stderr, "ERROR - AAudioStream_getFormat() returned %d\n", result);
            goto finish2;
        }
        mDataFormat = AAudioStream_getFormat(mStream);

        // Allocate a buffer for the audio data.
        mOutputBuffer = new float[mFramesPerBurst * mSamplesPerFrame];
@@ -123,7 +117,7 @@ public:
        }

        // If needed allocate a buffer for converting float to int16_t.
        if (mDataFormat == AAUDIO_FORMAT_PCM16) {
        if (mDataFormat == AAUDIO_FORMAT_PCM_I16) {
            mConversionBuffer = new int16_t[mFramesPerBurst * mSamplesPerFrame];
            if (mConversionBuffer == nullptr) {
                fprintf(stderr, "ERROR - could not allocate conversion buffer\n");
@@ -132,23 +126,20 @@ public:
        }
        return result;

     finish2:
        AAudioStream_close(mStream);
        mStream = AAUDIO_HANDLE_INVALID;
     finish1:
        AAudioStreamBuilder_delete(mBuilder);
        mBuilder = AAUDIO_HANDLE_INVALID;
        mBuilder = nullptr;
        return result;
    }

    aaudio_result_t close() {
        if (mStream != AAUDIO_HANDLE_INVALID) {
        if (mStream != nullptr) {
            stop();
            printf("call AAudioStream_close(0x%08x)\n", mStream);  fflush(stdout);
            printf("call AAudioStream_close(%p)\n", mStream);  fflush(stdout);
            AAudioStream_close(mStream);
            mStream = AAUDIO_HANDLE_INVALID;
            mStream = nullptr;
            AAudioStreamBuilder_delete(mBuilder);
            mBuilder = AAUDIO_HANDLE_INVALID;
            mBuilder = nullptr;
            delete mOutputBuffer;
            mOutputBuffer = nullptr;
            delete mConversionBuffer;
@@ -160,7 +151,7 @@ public:
    // Start a thread that will call the callback proc.
    aaudio_result_t start() {
        mEnabled = true;
        aaudio_nanoseconds_t nanosPerBurst = mFramesPerBurst * AAUDIO_NANOS_PER_SECOND
        int64_t nanosPerBurst = mFramesPerBurst * NANOS_PER_SECOND
                                           / mFramesPerSecond;
        return AAudioStream_createThread(mStream, nanosPerBurst,
                                       SimpleAAudioPlayerThreadProc,
@@ -170,7 +161,7 @@ public:
    // Tell the thread to stop.
    aaudio_result_t stop() {
        mEnabled = false;
        return AAudioStream_joinThread(mStream, nullptr, 2 * AAUDIO_NANOS_PER_SECOND);
        return AAudioStream_joinThread(mStream, nullptr, 2 * NANOS_PER_SECOND);
    }

    aaudio_result_t callbackLoop() {
@@ -186,8 +177,8 @@ public:

        // Give up after several burst periods have passed.
        const int burstsPerTimeout = 8;
        aaudio_nanoseconds_t nanosPerTimeout =
                        burstsPerTimeout * mFramesPerBurst * AAUDIO_NANOS_PER_SECOND
        int64_t nanosPerTimeout =
                        burstsPerTimeout * mFramesPerBurst * NANOS_PER_SECOND
                        / mFramesPerSecond;

        while (mEnabled && result >= 0) {
@@ -213,7 +204,7 @@ public:
            }
        }

        result = AAudioStream_getXRunCount(mStream, &xRunCount);
        xRunCount = AAudioStream_getXRunCount(mStream);
        printf("AAudioStream_getXRunCount %d\n", xRunCount);

        result = AAudioStream_requestStop(mStream);
@@ -226,8 +217,8 @@ public:
    }

private:
    AAudioStreamBuilder   mBuilder = AAUDIO_HANDLE_INVALID;
    AAudioStream          mStream = AAUDIO_HANDLE_INVALID;
    AAudioStreamBuilder   mBuilder = nullptr;
    AAudioStream          mStream = nullptr;
    float                *mOutputBuffer = nullptr;
    int16_t              *mConversionBuffer = nullptr;

@@ -236,8 +227,8 @@ private:
    aaudio_sharing_mode_t mRequestedSharingMode = SHARING_MODE;
    int32_t               mSamplesPerFrame = 0;
    int32_t               mFramesPerSecond = 0;
    aaudio_size_frames_t  mFramesPerBurst = 0;
    aaudio_audio_format_t mDataFormat = AAUDIO_FORMAT_PCM16;
    int32_t               mFramesPerBurst = 0;
    aaudio_audio_format_t mDataFormat = AAUDIO_FORMAT_PCM_I16;

    volatile bool         mEnabled = false; // used to request that callback exit its loop
};
+113 −188

File changed.

Preview size limit exceeded, changes collapsed.

+4 −41
Original line number Diff line number Diff line
@@ -23,25 +23,7 @@
extern "C" {
#endif

typedef int32_t  aaudio_handle_t; // negative handles are error codes
typedef int32_t  aaudio_result_t;
/**
 * A platform specific identifier for a device.
 */
typedef int32_t  aaudio_device_id_t;
typedef int32_t  aaudio_sample_rate_t;
/** This is used for small quantities such as the number of frames in a buffer. */
typedef int32_t  aaudio_size_frames_t;
/** This is used for small quantities such as the number of bytes in a frame. */
typedef int32_t  aaudio_size_bytes_t;
/**
 * This is used for large quantities, such as the number of frames that have
 * been played since a stream was started.
 * At 48000 Hz, a 32-bit integer would wrap around in just over 12 hours.
 */
typedef int64_t  aaudio_position_frames_t;

typedef int64_t  aaudio_nanoseconds_t;

/**
 * This is used to represent a value that has not been specified.
@@ -50,18 +32,11 @@ typedef int64_t aaudio_nanoseconds_t;
 * and would accept whatever it was given.
 */
#define AAUDIO_UNSPECIFIED           0
#define AAUDIO_DEVICE_UNSPECIFIED    ((aaudio_device_id_t) -1)
#define AAUDIO_NANOS_PER_MICROSECOND ((int64_t)1000)
#define AAUDIO_NANOS_PER_MILLISECOND (AAUDIO_NANOS_PER_MICROSECOND * 1000)
#define AAUDIO_MILLIS_PER_SECOND     1000
#define AAUDIO_NANOS_PER_SECOND      (AAUDIO_NANOS_PER_MILLISECOND * AAUDIO_MILLIS_PER_SECOND)

#define AAUDIO_HANDLE_INVALID     ((aaudio_handle_t)-1)
#define AAUDIO_DEVICE_UNSPECIFIED    ((int32_t) -1)

enum aaudio_direction_t {
    AAUDIO_DIRECTION_OUTPUT,
    AAUDIO_DIRECTION_INPUT,
    AAUDIO_DIRECTION_COUNT // This should always be last.
    AAUDIO_DIRECTION_INPUT
};

enum aaudio_audio_format_t {
@@ -73,11 +48,6 @@ enum aaudio_audio_format_t {
    AAUDIO_FORMAT_PCM_I32
};

// TODO These are deprecated. Remove these aliases once all references are replaced.
#define AAUDIO_FORMAT_PCM16    AAUDIO_FORMAT_PCM_I16
#define AAUDIO_FORMAT_PCM824   AAUDIO_FORMAT_PCM_I8_24
#define AAUDIO_FORMAT_PCM32    AAUDIO_FORMAT_PCM_I32

enum {
    AAUDIO_OK,
    AAUDIO_ERROR_BASE = -900, // TODO review
@@ -102,15 +72,10 @@ enum {
    AAUDIO_ERROR_NO_SERVICE
};

typedef enum {
    AAUDIO_CLOCK_MONOTONIC, // Clock since booted, pauses when CPU is sleeping.
    AAUDIO_CLOCK_BOOTTIME,  // Clock since booted, runs all the time.
    AAUDIO_CLOCK_COUNT // This should always be last.
} aaudio_clockid_t;

typedef enum
{
    AAUDIO_STREAM_STATE_UNINITIALIZED = 0,
    AAUDIO_STREAM_STATE_UNKNOWN,
    AAUDIO_STREAM_STATE_OPEN,
    AAUDIO_STREAM_STATE_STARTING,
    AAUDIO_STREAM_STATE_STARTED,
@@ -135,9 +100,7 @@ typedef enum {
     * Multiple applications will be mixed by the AAudio Server.
     * This will have higher latency than the EXCLUSIVE mode.
     */
    AAUDIO_SHARING_MODE_SHARED,

    AAUDIO_SHARING_MODE_COUNT // This should always be last.
    AAUDIO_SHARING_MODE_SHARED
} aaudio_sharing_mode_t;

#ifdef __cplusplus
+5 −9
Original line number Diff line number Diff line
LIBAAUDIO {
  global:
    AAudio_getNanoseconds;
    AAudio_convertResultToText;
    AAudio_convertStreamStateToText;
    AAudio_createStreamBuilder;
    AAudioStreamBuilder_setDeviceId;
    AAudioStreamBuilder_setSampleRate;
    AAudioStreamBuilder_getSampleRate;
    AAudioStreamBuilder_setSamplesPerFrame;
    AAudioStreamBuilder_getSamplesPerFrame;
    AAudioStreamBuilder_setFormat;
    AAudioStreamBuilder_getFormat;
    AAudioStreamBuilder_setSharingMode;
    AAudioStreamBuilder_getSharingMode;
    AAudioStreamBuilder_setDirection;
    AAudioStreamBuilder_getDirection;
    AAudioStreamBuilder_setBufferCapacityInFrames;
    AAudioStreamBuilder_openStream;
    AAudioStreamBuilder_delete;
    AAudioStream_close;
@@ -28,13 +23,14 @@ LIBAAUDIO {
    AAudioStream_write;
    AAudioStream_createThread;
    AAudioStream_joinThread;
    AAudioStream_setBufferSize;
    AAudioStream_getBufferSize;
    AAudioStream_setBufferSizeInFrames;
    AAudioStream_getBufferSizeInFrames;
    AAudioStream_getFramesPerBurst;
    AAudioStream_getBufferCapacity;
    AAudioStream_getBufferCapacityInFrames;
    AAudioStream_getXRunCount;
    AAudioStream_getSampleRate;
    AAudioStream_getSamplesPerFrame;
    AAudioStream_getDeviceId;
    AAudioStream_getFormat;
    AAudioStream_getSharingMode;
    AAudioStream_getDirection;
Loading