Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions apps/OboeTester/app/src/main/cpp/TestColdStartLatency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,34 @@ int32_t TestColdStartLatency::open(bool useInput, bool useLowLatency, bool useMm
}

int32_t TestColdStartLatency::start() {
std::shared_ptr<oboe::AudioStream> stream = mStream;
if (!stream) return (int32_t)Result::ErrorNull;
mBeginStartNanos = AudioClock::getNanoseconds();
Result result = mStream->requestStart();
Result result = stream->requestStart();
int64_t endStartNanos = AudioClock::getNanoseconds();
int64_t actualDurationNanos = endStartNanos - mBeginStartNanos;
mStartTimeMicros = actualDurationNanos / NANOS_PER_MICROSECOND;
return (int32_t) result;
}

int32_t TestColdStartLatency::close() {
if (!mStream) {
std::shared_ptr<oboe::AudioStream> stream = mStream;
if (!stream) {
return (int32_t)Result::OK;
}
Result result1 = mStream->requestStop();
Result result2 = mStream->close();
Result result1 = stream->requestStop();
Result result2 = stream->close();
return (int32_t)((result1 != Result::OK) ? result1 : result2);
}

int32_t TestColdStartLatency::getColdStartTimeMicros() {
std::shared_ptr<oboe::AudioStream> stream = mStream;
if (!stream) return -1;

int64_t position;
int64_t timestampNanos;
if (mStream->getDirection() == Direction::Output) {
auto result = mStream->getTimestamp(CLOCK_MONOTONIC);
if (stream->getDirection() == Direction::Output) {
auto result = stream->getTimestamp(CLOCK_MONOTONIC);
if (!result) {
return -1; // ERROR
}
Expand All @@ -90,10 +96,10 @@ int32_t TestColdStartLatency::getColdStartTimeMicros() {
position = frameTimestamp.position;
timestampNanos = frameTimestamp.timestamp;
} else {
position = mStream->getFramesRead();
position = stream->getFramesRead();
timestampNanos = AudioClock::getNanoseconds();
}
double sampleRate = (double) mStream->getSampleRate();
double sampleRate = (double) stream->getSampleRate();

int64_t elapsedNanos = NANOS_PER_SECOND * (position / sampleRate);
int64_t timeOfFrameZero = timestampNanos - elapsedNanos;
Expand All @@ -102,8 +108,30 @@ int32_t TestColdStartLatency::getColdStartTimeMicros() {
}

void TestColdStartLatency::waitForValidTimestamp() {
while (!mStream->getTimestamp(CLOCK_MONOTONIC)) {
usleep(kPollPeriodMillis * 1000);
std::shared_ptr<oboe::AudioStream> stream = mStream;
if (!stream) return;

int32_t timeoutCount = 0;
const int32_t kMaxTimeoutCount = 3000 / kPollPeriodMillis; // 3 seconds timeout

if (stream->getDirection() == Direction::Output) {
while (!stream->getTimestamp(CLOCK_MONOTONIC)) {
oboe::StreamState state = stream->getState();
if (state != oboe::StreamState::Starting && state != oboe::StreamState::Started) {
break;
}
usleep(kPollPeriodMillis * 1000);
if (++timeoutCount > kMaxTimeoutCount) break;
}
} else {
while (stream->getFramesRead() == 0) {
oboe::StreamState state = stream->getState();
if (state != oboe::StreamState::Starting && state != oboe::StreamState::Started) {
break;
}
usleep(kPollPeriodMillis * 1000);
if (++timeoutCount > kMaxTimeoutCount) break;
}
}
}

Expand Down
Loading