diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/cubeb-backend/src | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/cubeb-backend/src')
-rw-r--r-- | third_party/rust/cubeb-backend/src/capi.rs | 303 | ||||
-rw-r--r-- | third_party/rust/cubeb-backend/src/lib.rs | 17 | ||||
-rw-r--r-- | third_party/rust/cubeb-backend/src/log.rs | 73 | ||||
-rw-r--r-- | third_party/rust/cubeb-backend/src/ops.rs | 94 | ||||
-rw-r--r-- | third_party/rust/cubeb-backend/src/traits.rs | 60 |
5 files changed, 547 insertions, 0 deletions
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..33c13d18c1 --- /dev/null +++ b/third_party/rust/cubeb-backend/src/capi.rs @@ -0,0 +1,303 @@ +// Copyright © 2017 Mozilla Foundation +// +// This program is made available under an ISC-style license. See the +// accompanying file LICENSE for details + +use {ContextOps, StreamOps}; +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}; + +// 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_reset_default_device: + Some($crate::capi::capi_stream_reset_default_device::<$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>) + })); + +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 +} + +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() +} + +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 +} + +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 +} + +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 +} + +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 +} + +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 +} + +pub unsafe extern "C" fn capi_destroy<CTX>(c: *mut ffi::cubeb) { + let _: Box<CTX> = Box::from_raw(c as *mut _); +} + +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 +} + +pub unsafe extern "C" fn capi_stream_destroy<STM>(s: *mut ffi::cubeb_stream) { + let _ = Box::from_raw(s as *mut STM); +} + +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 +} + +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 +} + +pub unsafe extern "C" fn capi_stream_reset_default_device<STM: StreamOps>( + s: *mut ffi::cubeb_stream, +) -> c_int { + let stm = &mut *(s as *mut STM); + + _try!(stm.reset_default_device()); + ffi::CUBEB_OK +} + +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 +} + +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 +} + +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 +} + +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 +} + +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 + } +} + +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 +} + +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 +} + +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 +} + +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..495dd9318e --- /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] +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..ca297d0038 --- /dev/null +++ b/third_party/rust/cubeb-backend/src/log.rs @@ -0,0 +1,73 @@ +// Copyright © 2017-2018 Mozilla Foundation +// +// This program is made available under an ISC-style license. See the +// accompanying file LICENSE for details. + +#[macro_export] +macro_rules! cubeb_log_internal { + ($level: expr, $msg: expr) => { + #[allow(unused_unsafe)] + unsafe { + if $level <= $crate::ffi::g_cubeb_log_level.into() { + cubeb_log_internal!(__INTERNAL__ $msg); + } + } + }; + ($level: expr, $fmt: expr, $($arg: expr),+) => { + #[allow(unused_unsafe)] + unsafe { + if $level <= $crate::ffi::g_cubeb_log_level.into() { + cubeb_log_internal!(__INTERNAL__ format!($fmt, $($arg),*)); + } + } + }; + (__INTERNAL__ $msg: expr) => { + if let Some(log_callback) = $crate::ffi::g_cubeb_log_callback { + use std::io::Write; + + let mut buf = [0 as u8; 1024]; + let filename = std::path::Path::new(file!()) + .file_name() + .unwrap() + .to_str() + .unwrap(); + // 2 for ':', 1 for ' ', 1 for '\n', and 1 for converting `line!()` to number of digits + let len = filename.len() + ((line!() as f32).log10().trunc() as usize) + $msg.len() + 5; + debug_assert!(len < buf.len(), "log will be truncated"); + let _ = write!(&mut buf[..], "{}:{}: {}\n", filename, line!(), $msg); + let last = std::cmp::min(len, buf.len() - 1); + buf[last] = 0; + let cstr = unsafe { std::ffi::CStr::from_bytes_with_nul_unchecked(&buf[..=last]) }; + log_callback(cstr.as_ptr()); + } + } +} + +#[macro_export] +macro_rules! cubeb_logv { + ($msg: expr) => (cubeb_log_internal!($crate::LogLevel::Verbose, $msg)); + ($fmt: expr, $($arg: expr),+) => (cubeb_log_internal!($crate::LogLevel::Verbose, $fmt, $($arg),*)); +} + +#[macro_export] +macro_rules! cubeb_log { + ($msg: expr) => (cubeb_log_internal!($crate::LogLevel::Normal, $msg)); + ($fmt: expr, $($arg: expr),+) => (cubeb_log_internal!($crate::LogLevel::Normal, $fmt, $($arg),*)); +} + +#[cfg(test)] +mod tests { + #[test] + fn test_normal_logging() { + cubeb_log!("This is log at normal level"); + cubeb_log!("{} Formatted log", 1); + cubeb_log!("{} Formatted {} log {}", 1, 2, 3); + } + + #[test] + fn test_verbose_logging() { + cubeb_logv!("This is a log at verbose level"); + cubeb_logv!("{} Formatted log", 1); + cubeb_logv!("{} 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..9f51634cd0 --- /dev/null +++ b/third_party/rust/cubeb-backend/src/ops.rs @@ -0,0 +1,94 @@ +// 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)>, + 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_reset_default_device: + 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..afd13569c7 --- /dev/null +++ b/third_party/rust/cubeb-backend/src/traits.rs @@ -0,0 +1,60 @@ +// 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(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 reset_default_device(&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<()>; +} |