1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
// Copyright © 2017 Mozilla Foundation
//
// This program is made available under an ISC-style license. See the
// accompanying file LICENSE for details
#![allow(clippy::float_cmp)]
#[macro_use]
extern crate cubeb_backend;
use cubeb_backend::{
ffi, ContextOps, DeviceId, DeviceInfo, DeviceRef, DeviceType, InputProcessingParams, Ops,
Result, Stream, StreamOps, StreamParams, StreamParamsRef,
};
use std::ffi::CStr;
use std::mem::ManuallyDrop;
use std::os::raw::c_void;
use std::ptr;
use std::sync::OnceLock;
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<Box<Self>> {
Ok(Box::new(TestContext {
ops: &OPS as *const _,
}))
}
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 supported_input_processing_params(&mut self) -> Result<InputProcessingParams> {
Ok(InputProcessingParams::NONE)
}
fn enumerate_devices(&mut self, _devtype: DeviceType) -> Result<Box<[DeviceInfo]>> {
Ok(vec![DeviceInfo::default()].into_boxed_slice())
}
fn device_collection_destroy(&mut self, collection: Box<[DeviceInfo]>) -> Result<()> {
assert_eq!(collection.len(), 1);
assert_ne!(collection[0].as_ptr(), std::ptr::null_mut());
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 set_input_mute(&mut self, mute: bool) -> Result<()> {
assert_eq!(mute, true);
Ok(())
}
fn set_input_processing_params(&mut self, params: InputProcessingParams) -> Result<()> {
assert_eq!(params, InputProcessingParams::ECHO_CANCELLATION);
Ok(())
}
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
);
assert!(!c.is_null());
unsafe { OPS.destroy.unwrap()(c) }
}
#[test]
fn test_ops_context_max_channel_count() {
let c: *mut ffi::cubeb = get_ctx();
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 = get_ctx();
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 = get_ctx();
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_supported_input_processing_params() {
let c: *mut ffi::cubeb = get_ctx();
let mut params: ffi::cubeb_input_processing_params = InputProcessingParams::all().bits();
assert_eq!(
unsafe { OPS.get_supported_input_processing_params.unwrap()(c, &mut params) },
ffi::CUBEB_OK
);
assert_eq!(params, ffi::CUBEB_INPUT_PROCESSING_PARAM_NONE);
}
#[test]
fn test_ops_context_enumerate_devices() {
let c: *mut ffi::cubeb = get_ctx();
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_ne!(coll.device, std::ptr::null_mut());
assert_eq!(coll.count, 1)
}
#[test]
fn test_ops_context_device_collection_destroy() {
let c: *mut ffi::cubeb = get_ctx();
let mut device_infos = ManuallyDrop::new(Box::new([DeviceInfo::default().into()]));
let mut coll = ffi::cubeb_device_collection {
device: device_infos.as_mut_ptr(),
count: device_infos.len(),
};
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 = get_stream();
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 = get_stream();
unsafe {
OPS.stream_set_volume.unwrap()(s, 0.5);
}
}
#[test]
fn test_ops_stream_set_name() {
let s: *mut ffi::cubeb_stream = get_stream();
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 = get_stream();
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_set_input_mute() {
let s: *mut ffi::cubeb_stream = get_stream();
assert_eq!(
unsafe { OPS.stream_set_input_mute.unwrap()(s, 1) },
ffi::CUBEB_OK
);
}
#[test]
fn test_ops_stream_set_input_processing_params() {
let s: *mut ffi::cubeb_stream = get_stream();
assert_eq!(
unsafe {
OPS.stream_set_input_processing_params.unwrap()(
s,
ffi::CUBEB_INPUT_PROCESSING_PARAM_ECHO_CANCELLATION,
)
},
ffi::CUBEB_OK
);
}
fn get_ctx() -> *mut ffi::cubeb {
CONTEXT.get_or_init(TestContextPtr::new).ptr
}
static CONTEXT: OnceLock<TestContextPtr> = OnceLock::new();
struct TestContextPtr {
ptr: *mut ffi::cubeb,
}
// Safety: ffi::cubeb implementations are expected to be thread-safe.
unsafe impl Send for TestContextPtr {}
unsafe impl Sync for TestContextPtr {}
impl TestContextPtr {
fn new() -> Self {
let mut c: *mut ffi::cubeb = ptr::null_mut();
assert_eq!(
unsafe { OPS.init.unwrap()(&mut c, ptr::null()) },
ffi::CUBEB_OK
);
assert!(!c.is_null());
TestContextPtr { ptr: c }
}
}
impl Drop for TestContextPtr {
fn drop(&mut self) {
unsafe { OPS.destroy.unwrap()(self.ptr) }
}
}
fn get_stream() -> *mut ffi::cubeb_stream {
STREAM.get_or_init(TestStreamPtr::new).ptr
}
static STREAM: OnceLock<TestStreamPtr> = OnceLock::new();
struct TestStreamPtr {
ptr: *mut ffi::cubeb_stream,
}
// Safety: ffi::cubeb_stream implementations are expected to be thread-safe.
unsafe impl Send for TestStreamPtr {}
unsafe impl Sync for TestStreamPtr {}
impl TestStreamPtr {
fn new() -> Self {
let c: *mut ffi::cubeb = get_ctx();
let mut s: *mut ffi::cubeb_stream = ptr::null_mut();
assert_eq!(
unsafe {
OPS.stream_init.unwrap()(
c,
&mut s,
ptr::null(),
ptr::null(),
ptr::null_mut(),
ptr::null(),
ptr::null_mut(),
0,
None,
None,
ptr::null_mut(),
)
},
ffi::CUBEB_OK
);
assert!(!s.is_null());
TestStreamPtr { ptr: s }
}
}
impl Drop for TestStreamPtr {
fn drop(&mut self) {
unsafe { OPS.stream_destroy.unwrap()(self.ptr) }
}
}
|