Skip to content

Commit

Permalink
oboe: refactor waitForStateChange for OpenSL ES
Browse files Browse the repository at this point in the history
Do not return ErrorTimeout is user passed timeout==0.

Related to #406
  • Loading branch information
Phil Burk committed Mar 28, 2019
1 parent be13441 commit 171a25c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
27 changes: 17 additions & 10 deletions src/opensles/AudioStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,23 +311,30 @@ int64_t AudioStreamOpenSLES::getFramesProcessedByServer() const {
Result AudioStreamOpenSLES::waitForStateChange(StreamState currentState,
StreamState *nextState,
int64_t timeoutNanoseconds) {
LOGD("AudioStreamOpenSLES::waitForStateChange()");

Result oboeResult = (timeoutNanoseconds <= 0) ? Result::OK : Result::ErrorTimeout;
int64_t durationNanos = 20 * kNanosPerMillisecond; // arbitrary
StreamState state = getState();

while (state == currentState && timeoutNanoseconds > 0){
while (true) {
const StreamState state = getState(); // this does not require a lock
if (nextState != nullptr) {
*nextState = state;
}
if (currentState != state) { // state changed?
oboeResult = Result::OK;
break;
}

// Did we timeout or did user ask for non-blocking?
if (timeoutNanoseconds <= 0) {
break;
}

if (durationNanos > timeoutNanoseconds){
durationNanos = timeoutNanoseconds;
}
AudioClock::sleepForNanos(durationNanos);
timeoutNanoseconds -= durationNanos;

state = getState();
}
if (nextState != nullptr) {
*nextState = state;
}

return (state == currentState) ? Result::ErrorTimeout : Result::OK;
return oboeResult;
}
2 changes: 1 addition & 1 deletion src/opensles/AudioStreamOpenSLES.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class AudioStreamOpenSLES : public AudioStreamBuffered {

uint8_t *mCallbackBuffer = nullptr;
int32_t mBytesPerCallback = oboe::kUnspecified;
StreamState mState = StreamState::Uninitialized;
std::atomic<StreamState> mState{StreamState::Uninitialized};

MonotonicCounter mPositionMillis; // for tracking OpenSL ES service position
};
Expand Down

0 comments on commit 171a25c

Please sign in to comment.