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

Update ndk and ndk-glue to 0.7 #47

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
26 changes: 16 additions & 10 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ jobs:
uses: android-actions/setup-android@v2
- name: Setup Android NDK
env:
NDK_VERSION: 21.3.6528147
NDK_VERSION: 21.4.7075529
TRIPLE: x86_64-linux-android
run: |
rm -rf $ANDROID_SDK_ROOT/ndk-bundle
sdkmanager --sdk_root=$ANDROID_SDK_ROOT "ndk;$NDK_VERSION" | grep -v = || true
ln -s $ANDROID_SDK_ROOT/ndk/$NDK_VERSION $ANDROID_SDK_ROOT/ndk-bundle
echo "$ANDROID_SDK_ROOT/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin" >> $GITHUB_PATH
sdkmanager --sdk_root=$ANDROID_SDK_ROOT --install "ndk;$NDK_VERSION" --channel=3 | grep -v = || true
echo "$ANDROID_SDK_ROOT/ndk/$NDK_VERSION/toolchains/llvm/prebuilt/linux-x86_64/bin" >> $GITHUB_PATH
for var in ANDROID_NDK ANDROID_NDK_HOME ANDROID_NDK_LATEST_HOME ANDROID_NDK_ROOT; do
echo "$var=$ANDROID_SDK_ROOT/ndk/$NDK_VERSION" >> $GITHUB_ENV
done
TRIPLE_ENV=$(echo $TRIPLE | tr '-' '_')
echo "CC_${TRIPLE_ENV}=${TRIPLE}21-clang" >> $GITHUB_ENV
echo "CXX_${TRIPLE_ENV}=${TRIPLE}21-clang++" >> $GITHUB_ENV
Expand Down Expand Up @@ -124,12 +125,13 @@ jobs:
uses: android-actions/setup-android@v2
- name: Setup Android NDK
env:
NDK_VERSION: 21.3.6528147
NDK_VERSION: 21.4.7075529
run: |
rm -rf $ANDROID_SDK_ROOT/ndk-bundle
sdkmanager --sdk_root=$ANDROID_SDK_ROOT "ndk;$NDK_VERSION" | grep -v = || true
ln -s $ANDROID_SDK_ROOT/ndk/$NDK_VERSION $ANDROID_SDK_ROOT/ndk-bundle
echo "$ANDROID_SDK_ROOT/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin" >> $GITHUB_PATH
sdkmanager --sdk_root=$ANDROID_SDK_ROOT --install "ndk;$NDK_VERSION" --channel=3 | grep -v = || true
echo "$ANDROID_SDK_ROOT/ndk/$NDK_VERSION/toolchains/llvm/prebuilt/linux-x86_64/bin" >> $GITHUB_PATH
for var in ANDROID_NDK ANDROID_NDK_HOME ANDROID_NDK_LATEST_HOME ANDROID_NDK_ROOT; do
echo "$var=$ANDROID_SDK_ROOT/ndk/$NDK_VERSION" >> $GITHUB_ENV
done
- name: Setup Rust ${{ matrix.rust }} [${{ matrix.target }}]
uses: actions-rs/toolchain@v1
with:
Expand Down Expand Up @@ -242,6 +244,10 @@ jobs:
path: ~/.cargo/bin/cargo-apk
key: ${{ runner.os }}-cargo-apk
- uses: Swatinem/rust-cache@v1
- name: Create signing key
run: |
echo ${{ secrets.APK_SIGN_KEY_DATA }} | base64 -d > demo/release.keystore
sed -i 's/keystore_password = "android"/keystore_password = "${{ secrets.APK_SIGN_KEY_SECRET }}"/' demo/Cargo.toml
- name: Build demo apk
uses: actions-rs/cargo@v1
with:
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ version = "0.4.5"
path = "sys"

[dependencies.ndk]
version = "0.6"
version = "0.7"
optional = true

[dependencies.ndk-context]
Expand Down Expand Up @@ -66,5 +66,5 @@ incremental = false
overflow-checks = false

[patch.crates-io]
winit = { git = "https://github.com/rust-windowing/winit" }
glutin = { git = "https://github.com/katyo/glutin", branch = "android-support" }
#winit = { git = "https://github.com/rust-windowing/winit" }
#glutin = { git = "https://github.com/katyo/glutin", branch = "android-support" }
8 changes: 6 additions & 2 deletions demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ optional = true
version = "0.1.0"

[dependencies.ndk-glue]
version = "0.6.0"
version = "0.7.0"
features = ["logger"]

[dependencies.android_logger]
Expand All @@ -32,7 +32,7 @@ version = "0.10"
version = "0.4"

[dependencies.glutin]
version = "0.24"
version = "0.29.1"

[features]
default = ["audio"]
Expand Down Expand Up @@ -75,3 +75,7 @@ name = "android.permission.WRITE_EXTERNAL_STORAGE"

[[package.metadata.android.permission]]
name = "android.permission.RECORD_AUDIO"

[package.metadata.android.signing.release]
path = "release.keystore"
keystore_password = "android"
80 changes: 58 additions & 22 deletions src/audio_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,7 @@ impl<T: RawAudioStream + RawAudioStreamBase> AudioStream for T {
}

fn close(&mut self) -> Status {
wrap_status(unsafe {
ffi::oboe_AudioStream_close(self._raw_stream_mut() as *mut _ as *mut c_void)
})
wrap_status(unsafe { ffi::oboe_AudioStream_close1(self._raw_stream_mut()) })
}

fn start_with_timeout(&mut self, timeout_nanoseconds: i64) -> Status {
Expand Down Expand Up @@ -537,24 +535,30 @@ pub(crate) fn audio_stream_fmt<T: AudioStreamSafe>(
'\n'.fmt(f)
}

#[repr(transparent)]
struct AudioStreamHandle(*mut ffi::oboe_AudioStream);
struct AudioStreamHandle(*mut ffi::oboe_AudioStream, *mut c_void);

impl From<*mut ffi::oboe_AudioStream> for AudioStreamHandle {
fn from(raw: *mut ffi::oboe_AudioStream) -> Self {
Self(raw)
impl AudioStreamHandle {
fn new(raw: *mut ffi::oboe_AudioStream, shared_ptr: *mut c_void) -> Self {
Self(raw, shared_ptr)
}
}

impl Default for AudioStreamHandle {
fn default() -> Self {
Self(null_mut())
/// SAFETY: `self.0` and `self.1` must be valid pointers.
pub(crate) unsafe fn delete(&mut self) {
assert!(!self.0.is_null());
assert!(!self.1.is_null());

// The error callback could be holding a shared_ptr, so don't delete AudioStream
// directly, but only its shared_ptr.
ffi::oboe_AudioStream_deleteShared(self.1);

self.0 = null_mut();
self.1 = null_mut();
}
}

impl Drop for AudioStreamHandle {
fn drop(&mut self) {
unsafe { ffi::oboe_AudioStream_delete(self.0) }
impl Default for AudioStreamHandle {
fn default() -> Self {
Self(null_mut(), null_mut())
}
}

Expand Down Expand Up @@ -625,9 +629,6 @@ impl<'s> RawAudioOutputStream for AudioStreamRef<'s, Output> {}
*/
pub struct AudioStreamAsync<D, F> {
raw: AudioStreamHandle,

// Needed to keep callback alive
#[allow(dead_code)]
callback: AudioCallbackWrapper<D, F>,
}

Expand All @@ -638,17 +639,37 @@ impl<D, F> fmt::Debug for AudioStreamAsync<D, F> {
}

impl<D, F> AudioStreamAsync<D, F> {
pub(crate) fn wrap_raw(
// SAFETY: `raw`, `shared_ptr` and `callback` must be valid.
pub(crate) unsafe fn wrap_raw(
raw: *mut ffi::oboe_AudioStream,
shared_ptr: *mut c_void,
callback: AudioCallbackWrapper<D, F>,
) -> Self {
Self {
raw: raw.into(),
raw: AudioStreamHandle(raw, shared_ptr),
callback,
}
}
}

impl<D, F> Drop for AudioStreamAsync<D, F> {
fn drop(&mut self) {
// SAFETY: As long as the conditions on Self::wrap_raw are guaranteed on the creation of
// self, this is safe.
unsafe {
self.close();
self.raw.delete();

// NOTE: Currently there is no safe way to delete the AudioStreamCallback, so we are
// leaking it here.
// see https://github.com/google/oboe/issues/1610 and https://github.com/google/oboe/issues/1603

// replace this by `self.callback.delete()` when a fix upstream appear.
let _ = &self.callback;
}
}
}

impl<D, T> RawAudioStreamBase for AudioStreamAsync<D, T> {
fn _raw_base(&self) -> &ffi::oboe_AudioStreamBase {
unsafe { &*ffi::oboe_AudioStream_getBase(self.raw.0) }
Expand Down Expand Up @@ -688,14 +709,29 @@ impl<D, F> fmt::Debug for AudioStreamSync<D, F> {
}

impl<D, F> AudioStreamSync<D, F> {
pub(crate) fn wrap_raw(raw: *mut ffi::oboe_AudioStream) -> Self {
// SAFETY: `raw`, `shared_ptr` must be valid, because they will be deleted on drop.
pub(crate) unsafe fn wrap_raw(
raw: *mut ffi::oboe_AudioStream,
shared_ptr: *mut c_void,
) -> Self {
Self {
raw: raw.into(),
raw: AudioStreamHandle::new(raw, shared_ptr),
_phantom: PhantomData,
}
}
}

impl<D, F> Drop for AudioStreamSync<D, F> {
fn drop(&mut self) {
// SAFETY: As long as the conditions on Self::wrap_raw are guaranteed on the creation of
// self, this is safe.
unsafe {
self.close();
self.raw.delete();
}
}
}

impl<D, T> RawAudioStreamBase for AudioStreamSync<D, T> {
fn _raw_base(&self) -> &ffi::oboe_AudioStreamBase {
unsafe { &*ffi::oboe_AudioStream_getBase(self.raw.0) }
Expand Down
Loading