Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oboe: add blocking read/write #55

Merged
merged 9 commits into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions include/oboe/AudioStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ class AudioStream : public AudioStreamBase {
*
* <pre><code>
* int64_t timeoutNanos = 500 * kNanosPerMillisecond; // arbitrary 1/2 second
* StreamState currentState = stream->getState(stream);
* while (currentState >= 0 && currentState != StreamState::Paused) {
* currentState = stream->waitForStateChange(
* stream, currentState, timeoutNanos);
* StreamState currentState = stream->getState();
* StreamState nextState = StreamState::Unknown;
* while (result == Result::OK && currentState != StreamState::Paused) {
* result = stream->waitForStateChange(
* currentState, &nextState, timeoutNanos);
* currentState = nextState;
* }
* </code></pre>
*
Expand Down Expand Up @@ -130,7 +132,7 @@ class AudioStream : public AudioStreamBase {
*
* @return the count or negative error.
*/
virtual int32_t getXRunCount() {
virtual int32_t getXRunCount() const {
return static_cast<int32_t>(Result::ErrorUnimplemented);
}

Expand All @@ -151,9 +153,9 @@ class AudioStream : public AudioStreamBase {
* This monotonic counter will never get reset.
* @return the number of frames written so far
*/
virtual int64_t getFramesWritten() { return mFramesWritten; }
virtual int64_t getFramesWritten() const { return mFramesWritten; }

virtual int64_t getFramesRead() { return static_cast<int64_t>(Result::ErrorUnimplemented); }
virtual int64_t getFramesRead() const { return mFramesRead; }

virtual Result getTimestamp(clockid_t clockId,
int64_t *framePosition,
Expand Down Expand Up @@ -197,6 +199,9 @@ class AudioStream : public AudioStreamBase {
virtual int64_t incrementFramesWritten(int32_t frames) {
return mFramesWritten += frames;
}
virtual int64_t incrementFramesRead(int32_t frames) {
return mFramesRead += frames;
}

/**
* Wait for a transition from one state to another.
Expand All @@ -208,7 +213,18 @@ class AudioStream : public AudioStreamBase {
StreamState endingState,
int64_t timeoutNanoseconds);

Result fireCallback(void *audioData, int numFrames);
/**
* Override this to provide a default for when the application did not specify a callback.
*
* @param audioData
* @param numFrames
* @return result
*/
virtual DataCallbackResult onDefaultCallback(void *audioData, int numFrames) {
return DataCallbackResult::Stop;
}

DataCallbackResult fireCallback(void *audioData, int numFrames);

virtual void setNativeFormat(AudioFormat format) {
mNativeFormat = format;
Expand All @@ -219,7 +235,9 @@ class AudioStream : public AudioStreamBase {
AudioFormat mNativeFormat = AudioFormat::Invalid;

private:
// TODO these should be atomic like in AAudio
int64_t mFramesWritten = 0;
int64_t mFramesRead = 0;
int mPreviousScheduler = -1;
};

Expand Down
23 changes: 16 additions & 7 deletions src/aaudio/AudioStreamAAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ static void oboe_aaudio_error_callback_proc(
}
}


namespace oboe {

/*
Expand Down Expand Up @@ -282,6 +281,18 @@ int32_t AudioStreamAAudio::write(const void *buffer,
}
}

int32_t AudioStreamAAudio::read(void *buffer,
int32_t numFrames,
int64_t timeoutNanoseconds)
{
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to "{" previous line.

AAudioStream *stream = mAAudioStream.load();
if (stream != nullptr) {
return mLibLoader->stream_read(mAAudioStream, buffer, numFrames, timeoutNanoseconds);
} else {
return static_cast<int32_t>(Result::ErrorNull);
}
}

Result AudioStreamAAudio::waitForStateChange(StreamState currentState,
StreamState *nextState,
int64_t timeoutNanoseconds)
Expand Down Expand Up @@ -339,17 +350,16 @@ int32_t AudioStreamAAudio::getFramesPerBurst()
}
}

int64_t AudioStreamAAudio::getFramesRead()
{
int64_t AudioStreamAAudio::getFramesRead() const {
AAudioStream *stream = mAAudioStream.load();
if (stream != nullptr) {
return mLibLoader->stream_getFramesRead(stream);
} else {
return static_cast<int32_t>(Result::ErrorNull);
}
}
int64_t AudioStreamAAudio::getFramesWritten()
{

int64_t AudioStreamAAudio::getFramesWritten() const {
AAudioStream *stream = mAAudioStream.load();
if (stream != nullptr) {
return mLibLoader->stream_getFramesWritten(stream);
Expand All @@ -358,8 +368,7 @@ int64_t AudioStreamAAudio::getFramesWritten()
}
}

int32_t AudioStreamAAudio::getXRunCount()
{
int32_t AudioStreamAAudio::getXRunCount() const {
AAudioStream *stream = mAAudioStream.load();
if (stream != nullptr) {
return mLibLoader->stream_getXRunCount(stream);
Expand Down
14 changes: 9 additions & 5 deletions src/aaudio/AudioStreamAAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,20 @@ class AudioStreamAAudio : public AudioStream {
Result requestStop() override;

int32_t write(const void *buffer,
int32_t numFrames,
int64_t timeoutNanoseconds) override;
int32_t numFrames,
int64_t timeoutNanoseconds) override;

int32_t read(void *buffer,
int32_t numFrames,
int64_t timeoutNanoseconds) override;

Result setBufferSizeInFrames(int32_t requestedFrames) override;
int32_t getBufferSizeInFrames() const override;
int32_t getFramesPerBurst() override;
int32_t getXRunCount() override;
int32_t getXRunCount() const override;

int64_t getFramesRead() override;
int64_t getFramesWritten() override;
int64_t getFramesRead() const override;
int64_t getFramesWritten() const override;

Result waitForStateChange(StreamState currentState,
StreamState *nextState,
Expand Down
28 changes: 13 additions & 15 deletions src/common/AudioStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ AudioStream::AudioStream(const AudioStreamBuilder &builder)
}

Result AudioStream::open() {
// TODO validate parameters or let underlyng API validate them?
// TODO validate parameters or let underlying API validate them?
return Result::OK;
}

Result AudioStream::fireCallback(void *audioData, int32_t numFrames)
{
DataCallbackResult AudioStream::fireCallback(void *audioData, int32_t numFrames) {
int scheduler = sched_getscheduler(0) & ~SCHED_RESET_ON_FORK; // for current thread
if (scheduler != mPreviousScheduler) {
LOGD("AudioStream::fireCallback() scheduler = %s",
Expand All @@ -45,21 +44,20 @@ Result AudioStream::fireCallback(void *audioData, int32_t numFrames)
);
mPreviousScheduler = scheduler;
}

DataCallbackResult result;
if (mStreamCallback == nullptr) {
return Result::ErrorNull;
result = onDefaultCallback(audioData, numFrames);
} else {
/**
* TODO: onAudioRead doesn't return an Result, it returns either Continue or Stop
* neither of which tells us whether an error occured. Figure out what to do here.
*/
/*Result result = mStreamCallback->onAudioReady(this, audioData, numFrames);
if (result == OBOE_OK) {
mFramesWritten += numFrames;
}*/
mStreamCallback->onAudioReady(this, audioData, numFrames);
mFramesWritten += numFrames;
return Result::OK;
result = mStreamCallback->onAudioReady(this, audioData, numFrames);
if (getDirection() == Direction::Input) {
incrementFramesRead(numFrames);
} else {
incrementFramesWritten(numFrames);
}
}

return result;
}

Result AudioStream::waitForStateTransition(StreamState startingState,
Expand Down
10 changes: 8 additions & 2 deletions src/fifo/FifoBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdint.h>
#include <time.h>
#include <memory.h>
#include <assert.h>

#include "common/OboeDebug.h"
#include "fifo/FifoControllerBase.h"
Expand All @@ -36,12 +37,15 @@ FifoBuffer::FifoBuffer(uint32_t bytesPerFrame, uint32_t capacityInFrames)
, mFramesUnderrunCount(0)
, mUnderrunCount(0)
{
assert(bytesPerFrame > 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a more elegant way we could handle these two assertions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. I added them because this is a constructor and the FIFO cannot work if the values are incorrect. If they are zero then the FIFO seems to work but doesn't. This took a while to debug. I generally don't like asserts but they seem justified here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes agreed, just checking they weren't left in for debugging purposes.

assert(capacityInFrames > 0);
mFifo = new FifoController(capacityInFrames, capacityInFrames);
// allocate buffer
int32_t bytesPerBuffer = bytesPerFrame * capacityInFrames;
mStorage = new uint8_t[bytesPerBuffer];
mStorageOwned = true;
LOGD("FifoProcessor: numFrames = %d, bytesPerFrame = %d", capacityInFrames, bytesPerFrame);
LOGD("FifoProcessor: capacityInFrames = %d, bytesPerFrame = %d",
capacityInFrames, bytesPerFrame);
}

FifoBuffer::FifoBuffer( uint32_t bytesPerFrame,
Expand All @@ -64,7 +68,8 @@ FifoBuffer::FifoBuffer( uint32_t bytesPerFrame,
writeIndexAddress);
mStorage = dataStorageAddress;
mStorageOwned = false;
LOGD("FifoProcessor: capacityInFrames = %d, bytesPerFrame = %d", capacityInFrames, bytesPerFrame);
LOGD("FifoProcessor: capacityInFrames = %d, bytesPerFrame = %d",
capacityInFrames, bytesPerFrame);
}

FifoBuffer::~FifoBuffer() {
Expand Down Expand Up @@ -173,6 +178,7 @@ int32_t FifoBuffer::readNow(void *buffer, int32_t numFrames) {
return framesRead;
}

// FIXME remove
int64_t FifoBuffer::getNextReadTime(int frameRate) {
if (mReadAtNanoseconds == 0) {
return 0;
Expand Down
4 changes: 3 additions & 1 deletion src/opensles/AudioInputStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Result AudioInputStreamOpenSLES::open() {
// configure audio sink
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, // locatorType
static_cast<SLuint32>(mBurstsPerBuffer)}; // numBuffers
static_cast<SLuint32>(kBufferQueueLength)}; // numBuffers

// Define the audio data format.
SLDataFormat_PCM format_pcm = {
Expand Down Expand Up @@ -139,6 +139,8 @@ Result AudioInputStreamOpenSLES::open() {
goto error;
}

allocateFifo();

return Result::OK;

error:
Expand Down
4 changes: 3 additions & 1 deletion src/opensles/AudioOutputStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Result AudioOutputStreamOpenSLES::open() {
// configure audio source
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, // locatorType
static_cast<SLuint32>(mBurstsPerBuffer)}; // numBuffers
static_cast<SLuint32>(kBufferQueueLength)}; // numBuffers

// Define the audio data format.
SLDataFormat_PCM format_pcm = {
Expand Down Expand Up @@ -141,6 +141,8 @@ Result AudioOutputStreamOpenSLES::open() {
goto error;
}

allocateFifo();

return Result::OK;
error:
return Result::ErrorInternal; // TODO convert error from SLES to OBOE
Expand Down
Loading