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

Commit 5086a64b authored by Phil Burk's avatar Phil Burk Committed by Android (Google) Code Review
Browse files

Merge "liboboe: rename Oboe to AAudio"

parents 8dc4ee85 5ed503c7
Loading
Loading
Loading
Loading
+3 −3
Original line number Original line Diff line number Diff line
@@ -13,11 +13,11 @@
// limitations under the License.
// limitations under the License.


ndk_headers {
ndk_headers {
    name: "libOboe_headers",
    name: "libAAudio_headers",
    from: "include",
    from: "include",
    to: "",
    to: "",
    srcs: ["include/oboe/*.h"],
    srcs: ["include/aaudio/*.h"],
    license: "include/oboe/NOTICE",
    license: "include/aaudio/NOTICE",
}
}


ndk_library {
ndk_library {
+1 −1
Original line number Original line Diff line number Diff line
Oboe Audio input/output API
AAudio Audio input/output API
+2 −2
Original line number Original line Diff line number Diff line
# cd to this directory
# cd to this directory
mkdir -p jni/include/oboe
mkdir -p jni/include/aaudio
ln -s $PLATFORM/frameworks/av/media/liboboe/include/oboe/*.h jni/include/oboe
ln -s $PLATFORM/frameworks/av/media/liboboe/include/aaudio/*.h jni/include/aaudio
ln -s $PLATFORM/out/target/product/$TARGET_PRODUCT/symbols/out/soong/ndk/platforms/android-current/arch-arm64/usr/lib/liboboe.so jni
ln -s $PLATFORM/out/target/product/$TARGET_PRODUCT/symbols/out/soong/ndk/platforms/android-current/arch-arm64/usr/lib/liboboe.so jni
$NDK/ndk-build
$NDK/ndk-build
adb push libs/arm64-v8a/write_sine_threaded /data
adb push libs/arm64-v8a/write_sine_threaded /data
+64 −64
Original line number Original line Diff line number Diff line
@@ -14,31 +14,31 @@
 * limitations under the License.
 * limitations under the License.
 */
 */


// Play sine waves using Oboe.
// Play sine waves using AAudio.


#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <math.h>
#include <math.h>
#include <oboe/OboeDefinitions.h>
#include <aaudio/AAudioDefinitions.h>
#include <oboe/OboeAudio.h>
#include <aaudio/AAudio.h>
#include "SineGenerator.h"
#include "SineGenerator.h"


#define SAMPLE_RATE   48000
#define SAMPLE_RATE   48000
#define NUM_SECONDS   10
#define NUM_SECONDS   10


static const char *getSharingModeText(oboe_sharing_mode_t mode) {
static const char *getSharingModeText(aaudio_sharing_mode_t mode) {
    const char *modeText = "unknown";
    const char *modeText = "unknown";
    switch (mode) {
    switch (mode) {
    case OBOE_SHARING_MODE_EXCLUSIVE:
    case AAUDIO_SHARING_MODE_EXCLUSIVE:
        modeText = "EXCLUSIVE";
        modeText = "EXCLUSIVE";
        break;
        break;
    case OBOE_SHARING_MODE_LEGACY:
    case AAUDIO_SHARING_MODE_LEGACY:
        modeText = "LEGACY";
        modeText = "LEGACY";
        break;
        break;
    case OBOE_SHARING_MODE_SHARED:
    case AAUDIO_SHARING_MODE_SHARED:
        modeText = "SHARED";
        modeText = "SHARED";
        break;
        break;
    case OBOE_SHARING_MODE_PUBLIC_MIX:
    case AAUDIO_SHARING_MODE_PUBLIC_MIX:
        modeText = "PUBLIC_MIX";
        modeText = "PUBLIC_MIX";
        break;
        break;
    default:
    default:
@@ -51,25 +51,25 @@ int main(int argc, char **argv)
{
{
    (void)argc; // unused
    (void)argc; // unused


    oboe_result_t result = OBOE_OK;
    aaudio_result_t result = AAUDIO_OK;


    const int requestedSamplesPerFrame = 2;
    const int requestedSamplesPerFrame = 2;
    int actualSamplesPerFrame = 0;
    int actualSamplesPerFrame = 0;
    const int requestedSampleRate = SAMPLE_RATE;
    const int requestedSampleRate = SAMPLE_RATE;
    int actualSampleRate = 0;
    int actualSampleRate = 0;
    const oboe_audio_format_t requestedDataFormat = OBOE_AUDIO_FORMAT_PCM16;
    const aaudio_audio_format_t requestedDataFormat = AAUDIO_FORMAT_PCM16;
    oboe_audio_format_t actualDataFormat = OBOE_AUDIO_FORMAT_PCM16;
    aaudio_audio_format_t actualDataFormat = AAUDIO_FORMAT_PCM16;


    const oboe_sharing_mode_t requestedSharingMode = OBOE_SHARING_MODE_EXCLUSIVE;
    const aaudio_sharing_mode_t requestedSharingMode = AAUDIO_SHARING_MODE_EXCLUSIVE;
    //const oboe_sharing_mode_t requestedSharingMode = OBOE_SHARING_MODE_LEGACY;
    //const aaudio_sharing_mode_t requestedSharingMode = AAUDIO_SHARING_MODE_LEGACY;
    oboe_sharing_mode_t actualSharingMode = OBOE_SHARING_MODE_LEGACY;
    aaudio_sharing_mode_t actualSharingMode = AAUDIO_SHARING_MODE_LEGACY;


    OboeStreamBuilder oboeBuilder = OBOE_STREAM_BUILDER_NONE;
    AAudioStreamBuilder aaudioBuilder = AAUDIO_STREAM_BUILDER_NONE;
    OboeStream oboeStream = OBOE_STREAM_NONE;
    AAudioStream aaudioStream = AAUDIO_STREAM_NONE;
    oboe_stream_state_t state = OBOE_STREAM_STATE_UNINITIALIZED;
    aaudio_stream_state_t state = AAUDIO_STREAM_STATE_UNINITIALIZED;
    oboe_size_frames_t framesPerBurst = 0;
    aaudio_size_frames_t framesPerBurst = 0;
    oboe_size_frames_t framesToPlay = 0;
    aaudio_size_frames_t framesToPlay = 0;
    oboe_size_frames_t framesLeft = 0;
    aaudio_size_frames_t framesLeft = 0;
    int32_t xRunCount = 0;
    int32_t xRunCount = 0;
    int16_t *data = nullptr;
    int16_t *data = nullptr;


@@ -80,64 +80,64 @@ int main(int argc, char **argv)
    // in a buffer if we hang or crash.
    // in a buffer if we hang or crash.
    setvbuf(stdout, nullptr, _IONBF, (size_t) 0);
    setvbuf(stdout, nullptr, _IONBF, (size_t) 0);


    printf("%s - Play a sine wave using Oboe\n", argv[0]);
    printf("%s - Play a sine wave using AAudio\n", argv[0]);


    // Use an OboeStreamBuilder to contain requested parameters.
    // Use an AAudioStreamBuilder to contain requested parameters.
    result = Oboe_createStreamBuilder(&oboeBuilder);
    result = AAudio_createStreamBuilder(&aaudioBuilder);
    if (result != OBOE_OK) {
    if (result != AAUDIO_OK) {
        goto finish;
        goto finish;
    }
    }


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


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


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


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


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


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


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


    // This is the number of frames that are read in one chunk by a DMA controller
    // This is the number of frames that are read in one chunk by a DMA controller
    // or a DSP or a mixer.
    // or a DSP or a mixer.
    result = OboeStream_getFramesPerBurst(oboeStream, &framesPerBurst);
    result = AAudioStream_getFramesPerBurst(aaudioStream, &framesPerBurst);
    printf("DataFormat: original framesPerBurst = %d\n",framesPerBurst);
    printf("DataFormat: original framesPerBurst = %d\n",framesPerBurst);
    if (result != OBOE_OK) {
    if (result != AAUDIO_OK) {
        fprintf(stderr, "ERROR - OboeStream_getFramesPerBurst() returned %d\n", result);
        fprintf(stderr, "ERROR - AAudioStream_getFramesPerBurst() returned %d\n", result);
        goto finish;
        goto finish;
    }
    }
    // Some DMA might use very short bursts of 16 frames. We don't need to write such small
    // Some DMA might use very short bursts of 16 frames. We don't need to write such small
@@ -147,7 +147,7 @@ int main(int argc, char **argv)
    }
    }
    printf("DataFormat: final framesPerBurst = %d\n",framesPerBurst);
    printf("DataFormat: final framesPerBurst = %d\n",framesPerBurst);


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


@@ -155,20 +155,20 @@ int main(int argc, char **argv)
    data = new int16_t[framesPerBurst * actualSamplesPerFrame];
    data = new int16_t[framesPerBurst * actualSamplesPerFrame];
    if (data == nullptr) {
    if (data == nullptr) {
        fprintf(stderr, "ERROR - could not allocate data buffer\n");
        fprintf(stderr, "ERROR - could not allocate data buffer\n");
        result = OBOE_ERROR_NO_MEMORY;
        result = AAUDIO_ERROR_NO_MEMORY;
        goto finish;
        goto finish;
    }
    }


    // Start the stream.
    // Start the stream.
    printf("call OboeStream_requestStart()\n");
    printf("call AAudioStream_requestStart()\n");
    result = OboeStream_requestStart(oboeStream);
    result = AAudioStream_requestStart(aaudioStream);
    if (result != OBOE_OK) {
    if (result != AAUDIO_OK) {
        fprintf(stderr, "ERROR - OboeStream_requestStart() returned %d\n", result);
        fprintf(stderr, "ERROR - AAudioStream_requestStart() returned %d\n", result);
        goto finish;
        goto finish;
    }
    }


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


    // Play for a while.
    // Play for a while.
    framesToPlay = actualSampleRate * NUM_SECONDS;
    framesToPlay = actualSampleRate * NUM_SECONDS;
@@ -181,27 +181,27 @@ int main(int argc, char **argv)
        }
        }


        // Write audio data to the stream.
        // Write audio data to the stream.
        oboe_nanoseconds_t timeoutNanos = 100 * OBOE_NANOS_PER_MILLISECOND;
        aaudio_nanoseconds_t timeoutNanos = 100 * AAUDIO_NANOS_PER_MILLISECOND;
        int minFrames = (framesToPlay < framesPerBurst) ? framesToPlay : framesPerBurst;
        int minFrames = (framesToPlay < framesPerBurst) ? framesToPlay : framesPerBurst;
        int actual = OboeStream_write(oboeStream, data, minFrames, timeoutNanos);
        int actual = AAudioStream_write(aaudioStream, data, minFrames, timeoutNanos);
        if (actual < 0) {
        if (actual < 0) {
            fprintf(stderr, "ERROR - OboeStream_write() returned %zd\n", actual);
            fprintf(stderr, "ERROR - AAudioStream_write() returned %zd\n", actual);
            goto finish;
            goto finish;
        } else if (actual == 0) {
        } else if (actual == 0) {
            fprintf(stderr, "WARNING - OboeStream_write() returned %zd\n", actual);
            fprintf(stderr, "WARNING - AAudioStream_write() returned %zd\n", actual);
            goto finish;
            goto finish;
        }
        }
        framesLeft -= actual;
        framesLeft -= actual;
    }
    }


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


finish:
finish:
    delete[] data;
    delete[] data;
    OboeStream_close(oboeStream);
    AAudioStream_close(aaudioStream);
    OboeStreamBuilder_delete(oboeBuilder);
    AAudioStreamBuilder_delete(aaudioBuilder);
    printf("exiting - Oboe result = %d = %s\n", result, Oboe_convertResultToText(result));
    printf("exiting - AAudio result = %d = %s\n", result, AAudio_convertResultToText(result));
    return (result != OBOE_OK) ? EXIT_FAILURE : EXIT_SUCCESS;
    return (result != AAUDIO_OK) ? EXIT_FAILURE : EXIT_SUCCESS;
}
}
+82 −82
Original line number Original line Diff line number Diff line
@@ -14,7 +14,7 @@
 * limitations under the License.
 * limitations under the License.
 */
 */


// Play sine waves using an Oboe background thread.
// Play sine waves using an AAudio background thread.


#include <assert.h>
#include <assert.h>
#include <unistd.h>
#include <unistd.h>
@@ -22,34 +22,34 @@
#include <stdio.h>
#include <stdio.h>
#include <math.h>
#include <math.h>
#include <time.h>
#include <time.h>
#include <oboe/OboeDefinitions.h>
#include <aaudio/AAudioDefinitions.h>
#include <oboe/OboeAudio.h>
#include <aaudio/AAudio.h>
#include "SineGenerator.h"
#include "SineGenerator.h"


#define NUM_SECONDS   10
#define NUM_SECONDS   10


#define SHARING_MODE  OBOE_SHARING_MODE_EXCLUSIVE
#define SHARING_MODE  AAUDIO_SHARING_MODE_EXCLUSIVE
//#define SHARING_MODE  OBOE_SHARING_MODE_LEGACY
//#define SHARING_MODE  AAUDIO_SHARING_MODE_LEGACY


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


static void *SimpleOboePlayerThreadProc(void *arg);
static void *SimpleAAudioPlayerThreadProc(void *arg);


/**
/**
 * Simple wrapper for Oboe that opens a default stream and then calls
 * Simple wrapper for AAudio that opens a default stream and then calls
 * a callback function to fill the output buffers.
 * a callback function to fill the output buffers.
 */
 */
class SimpleOboePlayer {
class SimpleAAudioPlayer {
public:
public:
    SimpleOboePlayer() {}
    SimpleAAudioPlayer() {}
    virtual ~SimpleOboePlayer() {
    virtual ~SimpleAAudioPlayer() {
        close();
        close();
    };
    };


    void setSharingMode(oboe_sharing_mode_t requestedSharingMode) {
    void setSharingMode(aaudio_sharing_mode_t requestedSharingMode) {
        mRequestedSharingMode = requestedSharingMode;
        mRequestedSharingMode = requestedSharingMode;
    }
    }


@@ -66,34 +66,34 @@ public:
    /**
    /**
     * Open a stream
     * Open a stream
     */
     */
    oboe_result_t open(audio_callback_proc_t *proc, void *userContext) {
    aaudio_result_t open(audio_callback_proc_t *proc, void *userContext) {
        mCallbackProc = proc;
        mCallbackProc = proc;
        mUserContext = userContext;
        mUserContext = userContext;
        oboe_result_t result = OBOE_OK;
        aaudio_result_t result = AAUDIO_OK;


        // Use an OboeStreamBuilder to contain requested parameters.
        // Use an AAudioStreamBuilder to contain requested parameters.
        result = Oboe_createStreamBuilder(&mBuilder);
        result = AAudio_createStreamBuilder(&mBuilder);
        if (result != OBOE_OK) return result;
        if (result != AAUDIO_OK) return result;


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


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


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


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


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


@@ -111,75 +111,75 @@ public:
        mOutputBuffer = new float[mFramesPerBurst * mSamplesPerFrame];
        mOutputBuffer = new float[mFramesPerBurst * mSamplesPerFrame];
        if (mOutputBuffer == nullptr) {
        if (mOutputBuffer == nullptr) {
            fprintf(stderr, "ERROR - could not allocate data buffer\n");
            fprintf(stderr, "ERROR - could not allocate data buffer\n");
            result = OBOE_ERROR_NO_MEMORY;
            result = AAUDIO_ERROR_NO_MEMORY;
        }
        }


        // If needed allocate a buffer for converting float to int16_t.
        // If needed allocate a buffer for converting float to int16_t.
        if (mDataFormat == OBOE_AUDIO_FORMAT_PCM16) {
        if (mDataFormat == AAUDIO_FORMAT_PCM16) {
            mConversionBuffer = new int16_t[mFramesPerBurst * mSamplesPerFrame];
            mConversionBuffer = new int16_t[mFramesPerBurst * mSamplesPerFrame];
            if (mConversionBuffer == nullptr) {
            if (mConversionBuffer == nullptr) {
                fprintf(stderr, "ERROR - could not allocate conversion buffer\n");
                fprintf(stderr, "ERROR - could not allocate conversion buffer\n");
                result = OBOE_ERROR_NO_MEMORY;
                result = AAUDIO_ERROR_NO_MEMORY;
            }
            }
        }
        }
        return result;
        return result;


     finish2:
     finish2:
        OboeStream_close(mStream);
        AAudioStream_close(mStream);
        mStream = OBOE_HANDLE_INVALID;
        mStream = AAUDIO_HANDLE_INVALID;
     finish1:
     finish1:
        OboeStreamBuilder_delete(mBuilder);
        AAudioStreamBuilder_delete(mBuilder);
        mBuilder = OBOE_HANDLE_INVALID;
        mBuilder = AAUDIO_HANDLE_INVALID;
        return result;
        return result;
    }
    }


    oboe_result_t close() {
    aaudio_result_t close() {
        if (mStream != OBOE_HANDLE_INVALID) {
        if (mStream != AAUDIO_HANDLE_INVALID) {
            stop();
            stop();
            printf("call OboeStream_close(0x%08x)\n", mStream);  fflush(stdout);
            printf("call AAudioStream_close(0x%08x)\n", mStream);  fflush(stdout);
            OboeStream_close(mStream);
            AAudioStream_close(mStream);
            mStream = OBOE_HANDLE_INVALID;
            mStream = AAUDIO_HANDLE_INVALID;
            OboeStreamBuilder_delete(mBuilder);
            AAudioStreamBuilder_delete(mBuilder);
            mBuilder = OBOE_HANDLE_INVALID;
            mBuilder = AAUDIO_HANDLE_INVALID;
            delete mOutputBuffer;
            delete mOutputBuffer;
            mOutputBuffer = nullptr;
            mOutputBuffer = nullptr;
            delete mConversionBuffer;
            delete mConversionBuffer;
            mConversionBuffer = nullptr;
            mConversionBuffer = nullptr;
        }
        }
        return OBOE_OK;
        return AAUDIO_OK;
    }
    }


    // Start a thread that will call the callback proc.
    // Start a thread that will call the callback proc.
    oboe_result_t start() {
    aaudio_result_t start() {
        mEnabled = true;
        mEnabled = true;
        oboe_nanoseconds_t nanosPerBurst = mFramesPerBurst * OBOE_NANOS_PER_SECOND
        aaudio_nanoseconds_t nanosPerBurst = mFramesPerBurst * AAUDIO_NANOS_PER_SECOND
                                           / mFramesPerSecond;
                                           / mFramesPerSecond;
        return OboeStream_createThread(mStream, nanosPerBurst,
        return AAudioStream_createThread(mStream, nanosPerBurst,
                                       SimpleOboePlayerThreadProc,
                                       SimpleAAudioPlayerThreadProc,
                                       this);
                                       this);
    }
    }


    // Tell the thread to stop.
    // Tell the thread to stop.
    oboe_result_t stop() {
    aaudio_result_t stop() {
        mEnabled = false;
        mEnabled = false;
        return OboeStream_joinThread(mStream, nullptr, 2 * OBOE_NANOS_PER_SECOND);
        return AAudioStream_joinThread(mStream, nullptr, 2 * AAUDIO_NANOS_PER_SECOND);
    }
    }


    oboe_result_t callbackLoop() {
    aaudio_result_t callbackLoop() {
        int32_t framesWritten = 0;
        int32_t framesWritten = 0;
        int32_t xRunCount = 0;
        int32_t xRunCount = 0;
        oboe_result_t result = OBOE_OK;
        aaudio_result_t result = AAUDIO_OK;


        result = OboeStream_requestStart(mStream);
        result = AAudioStream_requestStart(mStream);
        if (result != OBOE_OK) {
        if (result != AAUDIO_OK) {
            fprintf(stderr, "ERROR - OboeStream_requestStart() returned %d\n", result);
            fprintf(stderr, "ERROR - AAudioStream_requestStart() returned %d\n", result);
            return result;
            return result;
        }
        }


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


        while (mEnabled && result >= 0) {
        while (mEnabled && result >= 0) {
@@ -194,23 +194,23 @@ public:
                    mConversionBuffer[i] = (int16_t)(32767.0 * mOutputBuffer[i]);
                    mConversionBuffer[i] = (int16_t)(32767.0 * mOutputBuffer[i]);
                }
                }
                // Write the application data to stream.
                // Write the application data to stream.
                result = OboeStream_write(mStream, mConversionBuffer, mFramesPerBurst, nanosPerTimeout);
                result = AAudioStream_write(mStream, mConversionBuffer, mFramesPerBurst, nanosPerTimeout);
            } else {
            } else {
                // Write the application data to stream.
                // Write the application data to stream.
                result = OboeStream_write(mStream, mOutputBuffer, mFramesPerBurst, nanosPerTimeout);
                result = AAudioStream_write(mStream, mOutputBuffer, mFramesPerBurst, nanosPerTimeout);
            }
            }
            framesWritten += result;
            framesWritten += result;
            if (result < 0) {
            if (result < 0) {
                fprintf(stderr, "ERROR - OboeStream_write() returned %zd\n", result);
                fprintf(stderr, "ERROR - AAudioStream_write() returned %zd\n", result);
            }
            }
        }
        }


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


        result = OboeStream_requestStop(mStream);
        result = AAudioStream_requestStop(mStream);
        if (result != OBOE_OK) {
        if (result != AAUDIO_OK) {
            fprintf(stderr, "ERROR - OboeStream_requestStart() returned %d\n", result);
            fprintf(stderr, "ERROR - AAudioStream_requestStart() returned %d\n", result);
            return result;
            return result;
        }
        }


@@ -218,24 +218,24 @@ public:
    }
    }


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


    audio_callback_proc_t * mCallbackProc = nullptr;
    audio_callback_proc_t * mCallbackProc = nullptr;
    void             *  mUserContext = nullptr;
    void             *  mUserContext = nullptr;
    oboe_sharing_mode_t mRequestedSharingMode = SHARING_MODE;
    aaudio_sharing_mode_t mRequestedSharingMode = SHARING_MODE;
    int32_t             mSamplesPerFrame = 0;
    int32_t             mSamplesPerFrame = 0;
    int32_t             mFramesPerSecond = 0;
    int32_t             mFramesPerSecond = 0;
    oboe_size_frames_t  mFramesPerBurst = 0;
    aaudio_size_frames_t  mFramesPerBurst = 0;
    oboe_audio_format_t mDataFormat = OBOE_AUDIO_FORMAT_PCM16;
    aaudio_audio_format_t mDataFormat = AAUDIO_FORMAT_PCM16;


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


static void *SimpleOboePlayerThreadProc(void *arg) {
static void *SimpleAAudioPlayerThreadProc(void *arg) {
    SimpleOboePlayer *player = (SimpleOboePlayer *) arg;
    SimpleAAudioPlayer *player = (SimpleAAudioPlayer *) arg;
    player->callbackLoop();
    player->callbackLoop();
    return nullptr;
    return nullptr;
}
}
@@ -261,17 +261,17 @@ int MyCallbackProc(float *outputBuffer, int32_t numFrames, void *userContext) {
int main(int argc, char **argv)
int main(int argc, char **argv)
{
{
    (void)argc; // unused
    (void)argc; // unused
    SimpleOboePlayer player;
    SimpleAAudioPlayer player;
    SineThreadedData_t myData;
    SineThreadedData_t myData;
    oboe_result_t result;
    aaudio_result_t result;


    // Make printf print immediately so that debug info is not stuck
    // Make printf print immediately so that debug info is not stuck
    // in a buffer if we hang or crash.
    // in a buffer if we hang or crash.
    setvbuf(stdout, nullptr, _IONBF, (size_t) 0);
    setvbuf(stdout, nullptr, _IONBF, (size_t) 0);
    printf("%s - Play a sine wave using an Oboe Thread\n", argv[0]);
    printf("%s - Play a sine wave using an AAudio Thread\n", argv[0]);


    result = player.open(MyCallbackProc, &myData);
    result = player.open(MyCallbackProc, &myData);
    if (result != OBOE_OK) {
    if (result != AAUDIO_OK) {
        fprintf(stderr, "ERROR -  player.open() returned %d\n", result);
        fprintf(stderr, "ERROR -  player.open() returned %d\n", result);
        goto error;
        goto error;
    }
    }
@@ -284,7 +284,7 @@ int main(int argc, char **argv)
    myData.samplesPerFrame = player.getSamplesPerFrame();
    myData.samplesPerFrame = player.getSamplesPerFrame();


    result = player.start();
    result = player.start();
    if (result != OBOE_OK) {
    if (result != AAUDIO_OK) {
        fprintf(stderr, "ERROR -  player.start() returned %d\n", result);
        fprintf(stderr, "ERROR -  player.start() returned %d\n", result);
        goto error;
        goto error;
    }
    }
@@ -299,12 +299,12 @@ int main(int argc, char **argv)
    printf("Woke up now.\n");
    printf("Woke up now.\n");


    result = player.stop();
    result = player.stop();
    if (result != OBOE_OK) {
    if (result != AAUDIO_OK) {
        fprintf(stderr, "ERROR -  player.stop() returned %d\n", result);
        fprintf(stderr, "ERROR -  player.stop() returned %d\n", result);
        goto error;
        goto error;
    }
    }
    result = player.close();
    result = player.close();
    if (result != OBOE_OK) {
    if (result != AAUDIO_OK) {
        fprintf(stderr, "ERROR -  player.close() returned %d\n", result);
        fprintf(stderr, "ERROR -  player.close() returned %d\n", result);
        goto error;
        goto error;
    }
    }
@@ -313,7 +313,7 @@ int main(int argc, char **argv)
    return EXIT_SUCCESS;
    return EXIT_SUCCESS;
error:
error:
    player.close();
    player.close();
    printf("exiting - Oboe result = %d = %s\n", result, Oboe_convertResultToText(result));
    printf("exiting - AAudio result = %d = %s\n", result, AAudio_convertResultToText(result));
    return EXIT_FAILURE;
    return EXIT_FAILURE;
}
}
Loading