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

Implement getFramesRead for OpenSL ES OUTPUT #89

Merged
merged 2 commits into from
May 22, 2018
Merged

Implement getFramesRead for OpenSL ES OUTPUT #89

merged 2 commits into from
May 22, 2018

Conversation

philburk
Copy link
Collaborator

and getFramesWritten for OpenSL ES INPUT.
Query position in milliseconds from OpenSL ES.
Use MonotonicCounter to build 64-bit frame counter from
32-bit internal counter.

Fixes Issue #81

and getFramesWritten for OpenSL ES INPUT.
Query position in milliseconds from OpenSL ES.
Use MonotonicCounter to build 64-bit frame counter from
32-bit internal counter.

Fixes Issue #81
}

private:
int64_t mCounter64 = 0;
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 should be atomic.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Atomicity isn't for free--there are costs on synchronizing CPU caches. Also, in update32 we seem to update both 32-bit and 64-bit counters in one transaction. This will require making the whole pair to be atomic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is there an std::atomic way to do that or do you just mean to use a mutex?

Copy link
Collaborator

@mnaganov mnaganov May 21, 2018

Choose a reason for hiding this comment

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

Make a struct and put it into std::atomic. Depending on different conditions it may use a mutex internally, you can check that with is_lock_free function: http://en.cppreference.com/w/cpp/atomic/atomic/is_lock_free

For two counters, it should usually be lock-free, unless it's a 32-bit ARM CPU and the fields alignment is wrong.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mikhail and I talked. The caller is only updating the internal data from one thread at a time. So I will document that this is not thread-safe and require caller to guarantee synchronization.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

}
return result;
}

int64_t AudioInputStreamOpenSLES::getFramesWritten() const {
int64_t millis64 = mPositionMillis.get();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Extract common function and call from OutputStream

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

mPositionMillis.reset32(); // OpenSL ES resets its millisecond position when stopped.
int64_t framesWritten = getFramesWritten();
if (framesWritten >= 0) {
setFramesRead(framesWritten);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is supposed to prevent drift but does not seem to be working.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is no longer drifting.

@@ -0,0 +1,110 @@
/*
* Copyright 2016 The Android Open Source Project
Copy link
Collaborator

Choose a reason for hiding this comment

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

2018

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The code was written in 2016. I copied it from inside AAudio.

/**
* Round 64-bit counter up to a multiple of the period.
*
* @param period might be, for example, a buffer capacity
Copy link
Collaborator

Choose a reason for hiding this comment

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

Specify that it has to be a positive integer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

}

private:
int64_t mCounter64 = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Atomicity isn't for free--there are costs on synchronizing CPU caches. Also, in update32 we seem to update both 32-bit and 64-bit counters in one transaction. This will require making the whole pair to be atomic.


int64_t AudioStreamOpenSLES::getFramesProcessedByServer() const {
int64_t millis64 = mPositionMillis.get();
int64_t framesRead = millis64 * getSampleRate() / kMillisPerSecond;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

change to framesProcessed

@philburk philburk merged commit 1f042c0 into master May 22, 2018
@philburk philburk deleted the framesread branch May 22, 2018 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants