summaryrefslogtreecommitdiffstats
path: root/third_party/rust/cubeb-backend
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/cubeb-backend')
-rw-r--r--third_party/rust/cubeb-backend/.cargo-checksum.json1
-rw-r--r--third_party/rust/cubeb-backend/Cargo.toml32
-rw-r--r--third_party/rust/cubeb-backend/LICENSE13
-rw-r--r--third_party/rust/cubeb-backend/src/capi.rs420
-rw-r--r--third_party/rust/cubeb-backend/src/lib.rs17
-rw-r--r--third_party/rust/cubeb-backend/src/log.rs143
-rw-r--r--third_party/rust/cubeb-backend/src/ops.rs97
-rw-r--r--third_party/rust/cubeb-backend/src/traits.rs61
-rw-r--r--third_party/rust/cubeb-backend/tests/test_capi.rs253
9 files changed, 1037 insertions, 0 deletions
diff --git a/third_party/rust/cubeb-backend/.cargo-checksum.json b/third_party/rust/cubeb-backend/.cargo-checksum.json
new file mode 100644
index 0000000000..249ded385e
--- /dev/null
+++ b/third_party/rust/cubeb-backend/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"Cargo.toml":"71fed70b173ae1349c1d6213a054f0e23e0b66b18f46a63abdccdccabd602221","LICENSE":"8c044baa5d883274736eeece0b955249076c2697b826e576fce59496235b2cf5","src/capi.rs":"edf8a65022d393d0214897986ca49428438b0b32f84e1380c962ecc835290e54","src/lib.rs":"4ce6db20f88487e99057ebeae0615fa1c99e82cfee0ab0825936da2e108fc73e","src/log.rs":"968b839a8a5a56fe42e372678fdd580e2777e1dba8c4f0bee6fbfe5e3f03b860","src/ops.rs":"981866ad12bf07e2f2b887ef9b5810364d2c48101d513879a06a8bf76984dc50","src/traits.rs":"ac35b2124876865b427f0e91026bb1102aadc96f1b763a639c495640cbd92650","tests/test_capi.rs":"27093e582f540c5baf50af5d1e5008c46573792853927b9ecfe62c117bfac303"},"package":"8f80631cc1edd2f810162c7b8a68e465e948ad4c76ba47f100968bc911944a80"} \ No newline at end of file
diff --git a/third_party/rust/cubeb-backend/Cargo.toml b/third_party/rust/cubeb-backend/Cargo.toml
new file mode 100644
index 0000000000..00bfada88c
--- /dev/null
+++ b/third_party/rust/cubeb-backend/Cargo.toml
@@ -0,0 +1,32 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g., crates.io) dependencies.
+#
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
+
+[package]
+name = "cubeb-backend"
+version = "0.10.3"
+authors = ["Dan Glastonbury <dglastonbury@mozilla.com>"]
+description = """
+Bindings to libcubeb internals to facilitate implementing cubeb backends in rust.
+"""
+homepage = "https://github.com/mozilla/cubeb-rs"
+keywords = ["cubeb"]
+categories = ["api-bindings"]
+license = "ISC"
+repository = "https://github.com/mozilla/cubeb-rs"
+
+[dependencies.cubeb-core]
+version = "0.10.3"
+
+[features]
+gecko-in-tree = ["cubeb-core/gecko-in-tree"]
+
+[badges.circle-ci]
+repository = "mozilla/cubeb-rs"
diff --git a/third_party/rust/cubeb-backend/LICENSE b/third_party/rust/cubeb-backend/LICENSE
new file mode 100644
index 0000000000..ec9718cf02
--- /dev/null
+++ b/third_party/rust/cubeb-backend/LICENSE
@@ -0,0 +1,13 @@
+Copyright © 2017 Mozilla Foundation
+
+Permission to use, copy, modify, and distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
diff --git a/third_party/rust/cubeb-backend/src/capi.rs b/third_party/rust/cubeb-backend/src/capi.rs
new file mode 100644
index 0000000000..a882420c88
--- /dev/null
+++ b/third_party/rust/cubeb-backend/src/capi.rs
@@ -0,0 +1,420 @@
+// Copyright © 2017 Mozilla Foundation
+//
+// This program is made available under an ISC-style license. See the
+// accompanying file LICENSE for details
+
+use cubeb_core::{ffi, DeviceCollectionRef, DeviceRef, DeviceType, StreamParams, StreamParamsRef};
+use std::ffi::CStr;
+use std::mem;
+use std::os::raw::{c_char, c_int, c_void};
+use {ContextOps, StreamOps};
+
+// Helper macro for unwrapping `Result` values from rust-api calls
+// while returning early with a c-api error code if the value of the
+// expression is `Err`.
+macro_rules! _try(
+ ($e:expr) => (match $e {
+ Ok(e) => e,
+ Err(e) => return e.raw_code()
+ })
+);
+
+macro_rules! as_opt_ref {
+ ($e:expr) => {
+ if $e.is_null() {
+ None
+ } else {
+ Some(StreamParamsRef::from_ptr($e))
+ }
+ };
+}
+
+#[macro_export]
+macro_rules! capi_new(
+ ($ctx:ident, $stm:ident) => (
+ Ops {
+ init: Some($crate::capi::capi_init::<$ctx>),
+ get_backend_id: Some($crate::capi::capi_get_backend_id::<$ctx>),
+ get_max_channel_count: Some($crate::capi::capi_get_max_channel_count::<$ctx>),
+ get_min_latency: Some($crate::capi::capi_get_min_latency::<$ctx>),
+ get_preferred_sample_rate: Some($crate::capi::capi_get_preferred_sample_rate::<$ctx>),
+ enumerate_devices: Some($crate::capi::capi_enumerate_devices::<$ctx>),
+ device_collection_destroy: Some($crate::capi::capi_device_collection_destroy::<$ctx>),
+ destroy: Some($crate::capi::capi_destroy::<$ctx>),
+ stream_init: Some($crate::capi::capi_stream_init::<$ctx>),
+ stream_destroy: Some($crate::capi::capi_stream_destroy::<$stm>),
+ stream_start: Some($crate::capi::capi_stream_start::<$stm>),
+ stream_stop: Some($crate::capi::capi_stream_stop::<$stm>),
+ stream_get_position: Some($crate::capi::capi_stream_get_position::<$stm>),
+ stream_get_latency: Some($crate::capi::capi_stream_get_latency::<$stm>),
+ stream_get_input_latency: Some($crate::capi::capi_stream_get_input_latency::<$stm>),
+ stream_set_volume: Some($crate::capi::capi_stream_set_volume::<$stm>),
+ stream_set_name: Some($crate::capi::capi_stream_set_name::<$stm>),
+ stream_get_current_device: Some($crate::capi::capi_stream_get_current_device::<$stm>),
+ stream_device_destroy: Some($crate::capi::capi_stream_device_destroy::<$stm>),
+ stream_register_device_changed_callback:
+ Some($crate::capi::capi_stream_register_device_changed_callback::<$stm>),
+ register_device_collection_changed:
+ Some($crate::capi::capi_register_device_collection_changed::<$ctx>)
+ }));
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `c` and `context` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_init<CTX: ContextOps>(
+ c: *mut *mut ffi::cubeb,
+ context_name: *const c_char,
+) -> c_int {
+ let anchor = &();
+ let context_name = opt_cstr(anchor, context_name);
+ let context = _try!(CTX::init(context_name));
+ *c = context.as_ptr();
+ // Leaking pointer across C FFI
+ mem::forget(context);
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `c` pointer.
+/// The caller should ensure that pointer is valid.
+pub unsafe extern "C" fn capi_get_backend_id<CTX: ContextOps>(c: *mut ffi::cubeb) -> *const c_char {
+ let ctx = &mut *(c as *mut CTX);
+ ctx.backend_id().as_ptr()
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `c` and `max_channels` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_get_max_channel_count<CTX: ContextOps>(
+ c: *mut ffi::cubeb,
+ max_channels: *mut u32,
+) -> c_int {
+ let ctx = &mut *(c as *mut CTX);
+
+ *max_channels = _try!(ctx.max_channel_count());
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `c` and `latency_frames` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_get_min_latency<CTX: ContextOps>(
+ c: *mut ffi::cubeb,
+ param: ffi::cubeb_stream_params,
+ latency_frames: *mut u32,
+) -> c_int {
+ let ctx = &mut *(c as *mut CTX);
+ let param = StreamParams::from(param);
+ *latency_frames = _try!(ctx.min_latency(param));
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `c` and `rate` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_get_preferred_sample_rate<CTX: ContextOps>(
+ c: *mut ffi::cubeb,
+ rate: *mut u32,
+) -> c_int {
+ let ctx = &mut *(c as *mut CTX);
+
+ *rate = _try!(ctx.preferred_sample_rate());
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `c` and `collection` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_enumerate_devices<CTX: ContextOps>(
+ c: *mut ffi::cubeb,
+ devtype: ffi::cubeb_device_type,
+ collection: *mut ffi::cubeb_device_collection,
+) -> c_int {
+ let ctx = &mut *(c as *mut CTX);
+ let devtype = DeviceType::from_bits_truncate(devtype);
+ let collection = DeviceCollectionRef::from_ptr(collection);
+ _try!(ctx.enumerate_devices(devtype, collection));
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `c` and `collection` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_device_collection_destroy<CTX: ContextOps>(
+ c: *mut ffi::cubeb,
+ collection: *mut ffi::cubeb_device_collection,
+) -> c_int {
+ let ctx = &mut *(c as *mut CTX);
+ let collection = DeviceCollectionRef::from_ptr_mut(collection);
+ _try!(ctx.device_collection_destroy(collection));
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `c` pointer.
+/// The caller should ensure that pointer is valid.
+pub unsafe extern "C" fn capi_destroy<CTX>(c: *mut ffi::cubeb) {
+ let _: Box<CTX> = Box::from_raw(c as *mut _);
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `c`, `s`, `stream_name`, `input_stream_params`,
+/// `output_stream_params`, `data_callback`, `state_callback`, and `user_ptr` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_stream_init<CTX: ContextOps>(
+ c: *mut ffi::cubeb,
+ s: *mut *mut ffi::cubeb_stream,
+ stream_name: *const c_char,
+ input_device: ffi::cubeb_devid,
+ input_stream_params: *mut ffi::cubeb_stream_params,
+ output_device: ffi::cubeb_devid,
+ output_stream_params: *mut ffi::cubeb_stream_params,
+ latency_frames: u32,
+ data_callback: ffi::cubeb_data_callback,
+ state_callback: ffi::cubeb_state_callback,
+ user_ptr: *mut c_void,
+) -> c_int {
+ let ctx = &mut *(c as *mut CTX);
+ let anchor = &(); // for lifetime of stream_name as CStr
+
+ let input_stream_params = as_opt_ref!(input_stream_params);
+ let output_stream_params = as_opt_ref!(output_stream_params);
+
+ let stream = _try!(ctx.stream_init(
+ opt_cstr(anchor, stream_name),
+ input_device,
+ input_stream_params,
+ output_device,
+ output_stream_params,
+ latency_frames,
+ data_callback,
+ state_callback,
+ user_ptr
+ ));
+ *s = stream.as_ptr();
+ // Leaking pointer across C FFI
+ mem::forget(stream);
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `s` pointer.
+/// The caller should ensure that pointer is valid.
+pub unsafe extern "C" fn capi_stream_destroy<STM>(s: *mut ffi::cubeb_stream) {
+ let _ = Box::from_raw(s as *mut STM);
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `s` pointer.
+/// The caller should ensure that pointer is valid.
+pub unsafe extern "C" fn capi_stream_start<STM: StreamOps>(s: *mut ffi::cubeb_stream) -> c_int {
+ let stm = &mut *(s as *mut STM);
+
+ _try!(stm.start());
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `s` pointer.
+/// The caller should ensure that pointer is valid.
+pub unsafe extern "C" fn capi_stream_stop<STM: StreamOps>(s: *mut ffi::cubeb_stream) -> c_int {
+ let stm = &mut *(s as *mut STM);
+
+ _try!(stm.stop());
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `s` and `position` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_stream_get_position<STM: StreamOps>(
+ s: *mut ffi::cubeb_stream,
+ position: *mut u64,
+) -> c_int {
+ let stm = &mut *(s as *mut STM);
+
+ *position = _try!(stm.position());
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `s` and `latency` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_stream_get_latency<STM: StreamOps>(
+ s: *mut ffi::cubeb_stream,
+ latency: *mut u32,
+) -> c_int {
+ let stm = &mut *(s as *mut STM);
+
+ *latency = _try!(stm.latency());
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `s` and `latency` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_stream_get_input_latency<STM: StreamOps>(
+ s: *mut ffi::cubeb_stream,
+ latency: *mut u32,
+) -> c_int {
+ let stm = &mut *(s as *mut STM);
+
+ *latency = _try!(stm.input_latency());
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `s` pointer.
+/// The caller should ensure that pointer is valid.
+pub unsafe extern "C" fn capi_stream_set_volume<STM: StreamOps>(
+ s: *mut ffi::cubeb_stream,
+ volume: f32,
+) -> c_int {
+ let stm = &mut *(s as *mut STM);
+
+ _try!(stm.set_volume(volume));
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `s` and `name` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_stream_set_name<STM: StreamOps>(
+ s: *mut ffi::cubeb_stream,
+ name: *const c_char,
+) -> c_int {
+ let stm = &mut *(s as *mut STM);
+ let anchor = &();
+ if let Some(name) = opt_cstr(anchor, name) {
+ _try!(stm.set_name(name));
+ ffi::CUBEB_OK
+ } else {
+ ffi::CUBEB_ERROR_INVALID_PARAMETER
+ }
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `s` and `device` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_stream_get_current_device<STM: StreamOps>(
+ s: *mut ffi::cubeb_stream,
+ device: *mut *mut ffi::cubeb_device,
+) -> i32 {
+ let stm = &mut *(s as *mut STM);
+
+ *device = _try!(stm.current_device()).as_ptr();
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `s` and `device` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_stream_device_destroy<STM: StreamOps>(
+ s: *mut ffi::cubeb_stream,
+ device: *mut ffi::cubeb_device,
+) -> c_int {
+ let stm = &mut *(s as *mut STM);
+ let device = DeviceRef::from_ptr(device);
+ let _ = stm.device_destroy(device);
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `s` and `device_changed_callback` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_stream_register_device_changed_callback<STM: StreamOps>(
+ s: *mut ffi::cubeb_stream,
+ device_changed_callback: ffi::cubeb_device_changed_callback,
+) -> c_int {
+ let stm = &mut *(s as *mut STM);
+
+ _try!(stm.register_device_changed_callback(device_changed_callback));
+ ffi::CUBEB_OK
+}
+
+/// # Safety
+///
+/// Entry point from C code.
+///
+/// This function is unsafe because it dereferences the given `s`, `collection_changed_callback`, and
+/// `user_ptr` pointers.
+/// The caller should ensure those pointers are valid.
+pub unsafe extern "C" fn capi_register_device_collection_changed<CTX: ContextOps>(
+ c: *mut ffi::cubeb,
+ devtype: ffi::cubeb_device_type,
+ collection_changed_callback: ffi::cubeb_device_collection_changed_callback,
+ user_ptr: *mut c_void,
+) -> i32 {
+ let ctx = &mut *(c as *mut CTX);
+ let devtype = DeviceType::from_bits_truncate(devtype);
+ _try!(ctx.register_device_collection_changed(devtype, collection_changed_callback, user_ptr));
+ ffi::CUBEB_OK
+}
+
+fn opt_cstr<T>(_anchor: &T, ptr: *const c_char) -> Option<&CStr> {
+ if ptr.is_null() {
+ None
+ } else {
+ Some(unsafe { CStr::from_ptr(ptr) })
+ }
+}
diff --git a/third_party/rust/cubeb-backend/src/lib.rs b/third_party/rust/cubeb-backend/src/lib.rs
new file mode 100644
index 0000000000..5b943e89c3
--- /dev/null
+++ b/third_party/rust/cubeb-backend/src/lib.rs
@@ -0,0 +1,17 @@
+// Copyright © 2017 Mozilla Foundation
+//
+// This program is made available under an ISC-style license. See the
+// accompanying file LICENSE for details.
+
+extern crate cubeb_core;
+
+pub mod capi;
+#[macro_use]
+pub mod log;
+mod ops;
+mod traits;
+
+// Re-export cubeb_core types
+pub use cubeb_core::*;
+pub use ops::Ops;
+pub use traits::{ContextOps, StreamOps};
diff --git a/third_party/rust/cubeb-backend/src/log.rs b/third_party/rust/cubeb-backend/src/log.rs
new file mode 100644
index 0000000000..c03d6b81be
--- /dev/null
+++ b/third_party/rust/cubeb-backend/src/log.rs
@@ -0,0 +1,143 @@
+// Copyright © 2017-2018 Mozilla Foundation
+//
+// This program is made available under an ISC-style license. See the
+// accompanying file LICENSE for details.
+
+use std::os::raw::c_char;
+
+/// Maximum length in bytes for a log message.
+/// Longer messages are silently truncated. See `write_str`.
+const LOG_LIMIT: usize = 1024;
+
+struct StaticCString<const N: usize> {
+ buf: [std::mem::MaybeUninit<u8>; N],
+ len: usize,
+}
+
+impl<const N: usize> StaticCString<N> {
+ fn new() -> Self {
+ StaticCString {
+ buf: unsafe { std::mem::MaybeUninit::uninit().assume_init() },
+ len: 0,
+ }
+ }
+
+ fn as_cstr(&self) -> &std::ffi::CStr {
+ unsafe {
+ std::ffi::CStr::from_bytes_with_nul_unchecked(std::slice::from_raw_parts(
+ self.buf.as_ptr().cast::<u8>(),
+ self.len,
+ ))
+ }
+ }
+}
+
+impl<const N: usize> std::fmt::Write for StaticCString<N> {
+ fn write_str(&mut self, s: &str) -> std::fmt::Result {
+ use std::convert::TryInto;
+ let s = s.as_bytes();
+ let end = s.len().min(N.checked_sub(1).unwrap() - self.len);
+ debug_assert_eq!(s.len(), end, "message truncated");
+ unsafe {
+ std::ptr::copy_nonoverlapping(
+ s[..end].as_ptr(),
+ self.buf
+ .as_mut_ptr()
+ .cast::<u8>()
+ .offset(self.len.try_into().unwrap()),
+ end,
+ )
+ };
+ self.len += end;
+ self.buf[self.len].write(0);
+ Ok(())
+ }
+}
+
+/// Formats `$file:line: $msg\n` into an on-stack buffer of size `LOG_LIMIT`,
+/// then calls `log_callback` with a pointer to the formatted message.
+pub fn cubeb_log_internal_buf_fmt(
+ log_callback: unsafe extern "C" fn(*const c_char, ...),
+ file: &str,
+ line: u32,
+ msg: std::fmt::Arguments,
+) {
+ let filename = std::path::Path::new(file)
+ .file_name()
+ .unwrap()
+ .to_str()
+ .unwrap();
+ let mut buf = StaticCString::<LOG_LIMIT>::new();
+ let _ = std::fmt::write(&mut buf, format_args!("{}:{}: {}\n", filename, line, msg));
+ unsafe {
+ log_callback(buf.as_cstr().as_ptr());
+ };
+}
+
+#[macro_export]
+macro_rules! cubeb_log_internal {
+ ($log_callback: expr, $level: expr, $fmt: expr, $($arg: expr),+) => {
+ #[allow(unused_unsafe)]
+ unsafe {
+ if $level <= $crate::ffi::cubeb_log_get_level().into() {
+ if let Some(log_callback) = $log_callback {
+ $crate::log::cubeb_log_internal_buf_fmt(log_callback, file!(), line!(), format_args!($fmt, $($arg),+));
+ }
+ }
+ }
+ };
+ ($log_callback: expr, $level: expr, $msg: expr) => {
+ cubeb_log_internal!($log_callback, $level, "{}", $msg);
+ };
+}
+
+#[macro_export]
+macro_rules! cubeb_log {
+ ($($arg: expr),+) => (cubeb_log_internal!($crate::ffi::cubeb_log_get_callback(), $crate::LogLevel::Normal, $($arg),+));
+}
+
+#[macro_export]
+macro_rules! cubeb_logv {
+ ($($arg: expr),+) => (cubeb_log_internal!($crate::ffi::cubeb_log_get_callback(), $crate::LogLevel::Verbose, $($arg),+));
+}
+
+#[macro_export]
+macro_rules! cubeb_alog {
+ ($($arg: expr),+) => (cubeb_log_internal!($crate::ffi::cubeb_async_log.into(), $crate::LogLevel::Normal, $($arg),+));
+}
+
+#[macro_export]
+macro_rules! cubeb_alogv {
+ ($($arg: expr),+) => (cubeb_log_internal!($crate::ffi::cubeb_async_log.into(), $crate::LogLevel::Verbose, $($arg),+));
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn test_normal_logging_sync() {
+ cubeb_log!("This is synchronous log output at normal level");
+ cubeb_log!("{} Formatted log", 1);
+ cubeb_log!("{} Formatted {} log {}", 1, 2, 3);
+ }
+
+ #[test]
+ fn test_verbose_logging_sync() {
+ cubeb_logv!("This is synchronous log output at verbose level");
+ cubeb_logv!("{} Formatted log", 1);
+ cubeb_logv!("{} Formatted {} log {}", 1, 2, 3);
+ }
+
+ #[test]
+ fn test_normal_logging_async() {
+ cubeb_alog!("This is asynchronous log output at normal level");
+ cubeb_alog!("{} Formatted log", 1);
+ cubeb_alog!("{} Formatted {} log {}", 1, 2, 3);
+ }
+
+ #[test]
+ fn test_verbose_logging_async() {
+ cubeb_alogv!("This is asynchronous log output at verbose level");
+ cubeb_alogv!("{} Formatted log", 1);
+ cubeb_alogv!("{} Formatted {} log {}", 1, 2, 3);
+ }
+}
diff --git a/third_party/rust/cubeb-backend/src/ops.rs b/third_party/rust/cubeb-backend/src/ops.rs
new file mode 100644
index 0000000000..0c53d44e9d
--- /dev/null
+++ b/third_party/rust/cubeb-backend/src/ops.rs
@@ -0,0 +1,97 @@
+// Copyright © 2017 Mozilla Foundation
+//
+// This program is made available under an ISC-style license. See the
+// accompanying file LICENSE for details.
+
+#![allow(non_camel_case_types)]
+
+use ffi;
+use std::os::raw::{c_char, c_float, c_int, c_uint, c_void};
+
+#[repr(C)]
+pub struct Ops {
+ pub init: Option<
+ unsafe extern "C" fn(context: *mut *mut ffi::cubeb, context_name: *const c_char) -> c_int,
+ >,
+ pub get_backend_id: Option<unsafe extern "C" fn(context: *mut ffi::cubeb) -> *const c_char>,
+ pub get_max_channel_count:
+ Option<unsafe extern "C" fn(context: *mut ffi::cubeb, max_channels: *mut c_uint) -> c_int>,
+ pub get_min_latency: Option<
+ unsafe extern "C" fn(
+ context: *mut ffi::cubeb,
+ params: ffi::cubeb_stream_params,
+ latency_ms: *mut c_uint,
+ ) -> c_int,
+ >,
+ pub get_preferred_sample_rate:
+ Option<unsafe extern "C" fn(context: *mut ffi::cubeb, rate: *mut u32) -> c_int>,
+ pub enumerate_devices: Option<
+ unsafe extern "C" fn(
+ context: *mut ffi::cubeb,
+ devtype: ffi::cubeb_device_type,
+ collection: *mut ffi::cubeb_device_collection,
+ ) -> c_int,
+ >,
+ pub device_collection_destroy: Option<
+ unsafe extern "C" fn(
+ context: *mut ffi::cubeb,
+ collection: *mut ffi::cubeb_device_collection,
+ ) -> c_int,
+ >,
+ pub destroy: Option<unsafe extern "C" fn(context: *mut ffi::cubeb)>,
+ #[allow(clippy::type_complexity)]
+ pub stream_init: Option<
+ unsafe extern "C" fn(
+ context: *mut ffi::cubeb,
+ stream: *mut *mut ffi::cubeb_stream,
+ stream_name: *const c_char,
+ input_device: ffi::cubeb_devid,
+ input_stream_params: *mut ffi::cubeb_stream_params,
+ output_device: ffi::cubeb_devid,
+ output_stream_params: *mut ffi::cubeb_stream_params,
+ latency: c_uint,
+ data_callback: ffi::cubeb_data_callback,
+ state_callback: ffi::cubeb_state_callback,
+ user_ptr: *mut c_void,
+ ) -> c_int,
+ >,
+ pub stream_destroy: Option<unsafe extern "C" fn(stream: *mut ffi::cubeb_stream)>,
+ pub stream_start: Option<unsafe extern "C" fn(stream: *mut ffi::cubeb_stream) -> c_int>,
+ pub stream_stop: Option<unsafe extern "C" fn(stream: *mut ffi::cubeb_stream) -> c_int>,
+ pub stream_get_position:
+ Option<unsafe extern "C" fn(stream: *mut ffi::cubeb_stream, position: *mut u64) -> c_int>,
+ pub stream_get_latency:
+ Option<unsafe extern "C" fn(stream: *mut ffi::cubeb_stream, latency: *mut u32) -> c_int>,
+ pub stream_get_input_latency:
+ Option<unsafe extern "C" fn(stream: *mut ffi::cubeb_stream, latency: *mut u32) -> c_int>,
+ pub stream_set_volume:
+ Option<unsafe extern "C" fn(stream: *mut ffi::cubeb_stream, volumes: c_float) -> c_int>,
+ pub stream_set_name:
+ Option<unsafe extern "C" fn(stream: *mut ffi::cubeb_stream, name: *const c_char) -> c_int>,
+ pub stream_get_current_device: Option<
+ unsafe extern "C" fn(
+ stream: *mut ffi::cubeb_stream,
+ device: *mut *mut ffi::cubeb_device,
+ ) -> c_int,
+ >,
+ pub stream_device_destroy: Option<
+ unsafe extern "C" fn(
+ stream: *mut ffi::cubeb_stream,
+ device: *mut ffi::cubeb_device,
+ ) -> c_int,
+ >,
+ pub stream_register_device_changed_callback: Option<
+ unsafe extern "C" fn(
+ stream: *mut ffi::cubeb_stream,
+ device_changed_callback: ffi::cubeb_device_changed_callback,
+ ) -> c_int,
+ >,
+ pub register_device_collection_changed: Option<
+ unsafe extern "C" fn(
+ context: *mut ffi::cubeb,
+ devtype: ffi::cubeb_device_type,
+ callback: ffi::cubeb_device_collection_changed_callback,
+ user_ptr: *mut c_void,
+ ) -> c_int,
+ >,
+}
diff --git a/third_party/rust/cubeb-backend/src/traits.rs b/third_party/rust/cubeb-backend/src/traits.rs
new file mode 100644
index 0000000000..c40cf3a7f2
--- /dev/null
+++ b/third_party/rust/cubeb-backend/src/traits.rs
@@ -0,0 +1,61 @@
+// Copyright © 2017 Mozilla Foundation
+//
+// This program is made available under an ISC-style license. See the
+// accompanying file LICENSE for details.
+
+use cubeb_core::{
+ Context, DeviceCollectionRef, DeviceId, DeviceRef, DeviceType, Result, Stream, StreamParams,
+ StreamParamsRef,
+};
+use ffi;
+use std::ffi::CStr;
+use std::os::raw::c_void;
+
+pub trait ContextOps {
+ fn init(context_name: Option<&CStr>) -> Result<Context>;
+ fn backend_id(&mut self) -> &CStr;
+ fn max_channel_count(&mut self) -> Result<u32>;
+ fn min_latency(&mut self, params: StreamParams) -> Result<u32>;
+ fn preferred_sample_rate(&mut self) -> Result<u32>;
+ fn enumerate_devices(
+ &mut self,
+ devtype: DeviceType,
+ collection: &DeviceCollectionRef,
+ ) -> Result<()>;
+ fn device_collection_destroy(&mut self, collection: &mut DeviceCollectionRef) -> Result<()>;
+ #[cfg_attr(feature = "cargo-clippy", allow(clippy::too_many_arguments))]
+ fn stream_init(
+ &mut self,
+ stream_name: Option<&CStr>,
+ input_device: DeviceId,
+ input_stream_params: Option<&StreamParamsRef>,
+ output_device: DeviceId,
+ output_stream_params: Option<&StreamParamsRef>,
+ latency_frames: u32,
+ data_callback: ffi::cubeb_data_callback,
+ state_callback: ffi::cubeb_state_callback,
+ user_ptr: *mut c_void,
+ ) -> Result<Stream>;
+ fn register_device_collection_changed(
+ &mut self,
+ devtype: DeviceType,
+ cb: ffi::cubeb_device_collection_changed_callback,
+ user_ptr: *mut c_void,
+ ) -> Result<()>;
+}
+
+pub trait StreamOps {
+ fn start(&mut self) -> Result<()>;
+ fn stop(&mut self) -> Result<()>;
+ fn position(&mut self) -> Result<u64>;
+ fn latency(&mut self) -> Result<u32>;
+ fn input_latency(&mut self) -> Result<u32>;
+ fn set_volume(&mut self, volume: f32) -> Result<()>;
+ fn set_name(&mut self, name: &CStr) -> Result<()>;
+ fn current_device(&mut self) -> Result<&DeviceRef>;
+ fn device_destroy(&mut self, device: &DeviceRef) -> Result<()>;
+ fn register_device_changed_callback(
+ &mut self,
+ device_changed_callback: ffi::cubeb_device_changed_callback,
+ ) -> Result<()>;
+}
diff --git a/third_party/rust/cubeb-backend/tests/test_capi.rs b/third_party/rust/cubeb-backend/tests/test_capi.rs
new file mode 100644
index 0000000000..be72f4abb6
--- /dev/null
+++ b/third_party/rust/cubeb-backend/tests/test_capi.rs
@@ -0,0 +1,253 @@
+// Copyright © 2017 Mozilla Foundation
+//
+// This program is made available under an ISC-style license. See the
+// accompanying file LICENSE for details
+
+#![cfg_attr(feature = "cargo-clippy", allow(clippy::float_cmp))]
+
+#[macro_use]
+extern crate cubeb_backend;
+
+use cubeb_backend::{
+ ffi, Context, ContextOps, DeviceCollectionRef, DeviceId, DeviceRef, DeviceType, Ops, Result,
+ Stream, StreamOps, StreamParams, StreamParamsRef,
+};
+use std::ffi::CStr;
+use std::os::raw::c_void;
+use std::ptr;
+
+pub const OPS: Ops = capi_new!(TestContext, TestStream);
+
+struct TestContext {
+ #[allow(dead_code)]
+ pub ops: *const Ops,
+}
+
+impl ContextOps for TestContext {
+ fn init(_context_name: Option<&CStr>) -> Result<Context> {
+ let ctx = Box::new(TestContext {
+ ops: &OPS as *const _,
+ });
+ Ok(unsafe { Context::from_ptr(Box::into_raw(ctx) as *mut _) })
+ }
+
+ fn backend_id(&mut self) -> &'static CStr {
+ unsafe { CStr::from_ptr(b"remote\0".as_ptr() as *const _) }
+ }
+ fn max_channel_count(&mut self) -> Result<u32> {
+ Ok(0u32)
+ }
+ fn min_latency(&mut self, _params: StreamParams) -> Result<u32> {
+ Ok(0u32)
+ }
+ fn preferred_sample_rate(&mut self) -> Result<u32> {
+ Ok(0u32)
+ }
+ fn enumerate_devices(
+ &mut self,
+ _devtype: DeviceType,
+ collection: &DeviceCollectionRef,
+ ) -> Result<()> {
+ let coll = unsafe { &mut *collection.as_ptr() };
+ coll.device = 0xDEAD_BEEF as *mut _;
+ coll.count = usize::max_value();
+ Ok(())
+ }
+ fn device_collection_destroy(&mut self, collection: &mut DeviceCollectionRef) -> Result<()> {
+ let coll = unsafe { &mut *collection.as_ptr() };
+ assert_eq!(coll.device, 0xDEAD_BEEF as *mut _);
+ assert_eq!(coll.count, usize::max_value());
+ coll.device = ptr::null_mut();
+ coll.count = 0;
+ Ok(())
+ }
+ fn stream_init(
+ &mut self,
+ _stream_name: Option<&CStr>,
+ _input_device: DeviceId,
+ _input_stream_params: Option<&StreamParamsRef>,
+ _output_device: DeviceId,
+ _output_stream_params: Option<&StreamParamsRef>,
+ _latency_frame: u32,
+ _data_callback: ffi::cubeb_data_callback,
+ _state_callback: ffi::cubeb_state_callback,
+ _user_ptr: *mut c_void,
+ ) -> Result<Stream> {
+ Ok(unsafe { Stream::from_ptr(0xDEAD_BEEF as *mut _) })
+ }
+ fn register_device_collection_changed(
+ &mut self,
+ _dev_type: DeviceType,
+ _collection_changed_callback: ffi::cubeb_device_collection_changed_callback,
+ _user_ptr: *mut c_void,
+ ) -> Result<()> {
+ Ok(())
+ }
+}
+
+struct TestStream {}
+
+impl StreamOps for TestStream {
+ fn start(&mut self) -> Result<()> {
+ Ok(())
+ }
+ fn stop(&mut self) -> Result<()> {
+ Ok(())
+ }
+ fn position(&mut self) -> Result<u64> {
+ Ok(0u64)
+ }
+ fn latency(&mut self) -> Result<u32> {
+ Ok(0u32)
+ }
+ fn input_latency(&mut self) -> Result<u32> {
+ Ok(0u32)
+ }
+ fn set_volume(&mut self, volume: f32) -> Result<()> {
+ assert_eq!(volume, 0.5);
+ Ok(())
+ }
+ fn set_name(&mut self, name: &CStr) -> Result<()> {
+ assert_eq!(name, CStr::from_bytes_with_nul(b"test\0").unwrap());
+ Ok(())
+ }
+ fn current_device(&mut self) -> Result<&DeviceRef> {
+ Ok(unsafe { DeviceRef::from_ptr(0xDEAD_BEEF as *mut _) })
+ }
+ fn device_destroy(&mut self, device: &DeviceRef) -> Result<()> {
+ assert_eq!(device.as_ptr(), 0xDEAD_BEEF as *mut _);
+ Ok(())
+ }
+ fn register_device_changed_callback(
+ &mut self,
+ _: ffi::cubeb_device_changed_callback,
+ ) -> Result<()> {
+ Ok(())
+ }
+}
+
+#[test]
+fn test_ops_context_init() {
+ let mut c: *mut ffi::cubeb = ptr::null_mut();
+ assert_eq!(
+ unsafe { OPS.init.unwrap()(&mut c, ptr::null()) },
+ ffi::CUBEB_OK
+ );
+ unsafe { OPS.destroy.unwrap()(c) }
+}
+
+#[test]
+fn test_ops_context_max_channel_count() {
+ let c: *mut ffi::cubeb = ptr::null_mut();
+ let mut max_channel_count = u32::max_value();
+ assert_eq!(
+ unsafe { OPS.get_max_channel_count.unwrap()(c, &mut max_channel_count) },
+ ffi::CUBEB_OK
+ );
+ assert_eq!(max_channel_count, 0);
+}
+
+#[test]
+fn test_ops_context_min_latency() {
+ let c: *mut ffi::cubeb = ptr::null_mut();
+ let params: ffi::cubeb_stream_params = unsafe { ::std::mem::zeroed() };
+ let mut latency = u32::max_value();
+ assert_eq!(
+ unsafe { OPS.get_min_latency.unwrap()(c, params, &mut latency) },
+ ffi::CUBEB_OK
+ );
+ assert_eq!(latency, 0);
+}
+
+#[test]
+fn test_ops_context_preferred_sample_rate() {
+ let c: *mut ffi::cubeb = ptr::null_mut();
+ let mut rate = u32::max_value();
+ assert_eq!(
+ unsafe { OPS.get_preferred_sample_rate.unwrap()(c, &mut rate) },
+ ffi::CUBEB_OK
+ );
+ assert_eq!(rate, 0);
+}
+
+#[test]
+fn test_ops_context_enumerate_devices() {
+ let c: *mut ffi::cubeb = ptr::null_mut();
+ let mut coll = ffi::cubeb_device_collection {
+ device: ptr::null_mut(),
+ count: 0,
+ };
+ assert_eq!(
+ unsafe { OPS.enumerate_devices.unwrap()(c, 0, &mut coll) },
+ ffi::CUBEB_OK
+ );
+ assert_eq!(coll.device, 0xDEAD_BEEF as *mut _);
+ assert_eq!(coll.count, usize::max_value())
+}
+
+#[test]
+fn test_ops_context_device_collection_destroy() {
+ let c: *mut ffi::cubeb = ptr::null_mut();
+ let mut coll = ffi::cubeb_device_collection {
+ device: 0xDEAD_BEEF as *mut _,
+ count: usize::max_value(),
+ };
+ assert_eq!(
+ unsafe { OPS.device_collection_destroy.unwrap()(c, &mut coll) },
+ ffi::CUBEB_OK
+ );
+ assert_eq!(coll.device, ptr::null_mut());
+ assert_eq!(coll.count, 0);
+}
+
+// stream_init: Some($crate::capi::capi_stream_init::<$ctx>),
+// stream_destroy: Some($crate::capi::capi_stream_destroy::<$stm>),
+// stream_start: Some($crate::capi::capi_stream_start::<$stm>),
+// stream_stop: Some($crate::capi::capi_stream_stop::<$stm>),
+// stream_get_position: Some($crate::capi::capi_stream_get_position::<$stm>),
+
+#[test]
+fn test_ops_stream_latency() {
+ let s: *mut ffi::cubeb_stream = ptr::null_mut();
+ let mut latency = u32::max_value();
+ assert_eq!(
+ unsafe { OPS.stream_get_latency.unwrap()(s, &mut latency) },
+ ffi::CUBEB_OK
+ );
+ assert_eq!(latency, 0);
+}
+
+#[test]
+fn test_ops_stream_set_volume() {
+ let s: *mut ffi::cubeb_stream = ptr::null_mut();
+ unsafe {
+ OPS.stream_set_volume.unwrap()(s, 0.5);
+ }
+}
+
+#[test]
+fn test_ops_stream_set_name() {
+ let s: *mut ffi::cubeb_stream = ptr::null_mut();
+ unsafe {
+ OPS.stream_set_name.unwrap()(s, CStr::from_bytes_with_nul(b"test\0").unwrap().as_ptr());
+ }
+}
+
+#[test]
+fn test_ops_stream_current_device() {
+ let s: *mut ffi::cubeb_stream = ptr::null_mut();
+ let mut device: *mut ffi::cubeb_device = ptr::null_mut();
+ assert_eq!(
+ unsafe { OPS.stream_get_current_device.unwrap()(s, &mut device) },
+ ffi::CUBEB_OK
+ );
+ assert_eq!(device, 0xDEAD_BEEF as *mut _);
+}
+
+#[test]
+fn test_ops_stream_device_destroy() {
+ let s: *mut ffi::cubeb_stream = ptr::null_mut();
+ unsafe {
+ OPS.stream_device_destroy.unwrap()(s, 0xDEAD_BEEF as *mut _);
+ }
+}