| 1 |
// Copyright © 2017 Mozilla Foundation |
| 2 |
// |
| 3 |
// This program is made available under an ISC-style license. See the |
| 4 |
// accompanying file LICENSE for details |
| 5 |
|
| 6 |
#![cfg_attr(feature = "cargo-clippy", allow(clippy::float_cmp))] |
| 7 |
|
| 8 |
#[macro_use] |
| 9 |
extern crate cubeb_backend; |
| 10 |
|
| 11 |
use cubeb_backend::{ |
| 12 |
ffi, Context, ContextOps, DeviceCollectionRef, DeviceId, DeviceRef, DeviceType, |
| 13 |
InputProcessingParams, Ops, Result, Stream, StreamOps, StreamParams, StreamParamsRef, |
| 14 |
}; |
| 15 |
use std::ffi::CStr; |
| 16 |
use std::os::raw::c_void; |
| 17 |
use std::ptr; |
| 18 |
|
| 19 |
pub const OPS: Ops = capi_new!(TestContext, TestStream); |
| 20 |
|
| 21 |
struct TestContext { |
| 22 |
#[allow(dead_code)] |
| 23 |
pub ops: *const Ops, |
| 24 |
} |
| 25 |
|
| 26 |
impl ContextOps for TestContext { |
| 27 |
fn init(_context_name: Option<&CStr>) -> Result<Context> { |
| 28 |
let ctx = Box::new(TestContext { |
| 29 |
ops: &OPS as *const _, |
| 30 |
}); |
| 31 |
Ok(unsafe { Context::from_ptr(Box::into_raw(ctx) as *mut _) }) |
| 32 |
} |
| 33 |
|
| 34 |
fn backend_id(&mut self) -> &'static CStr { |
| 35 |
unsafe { CStr::from_ptr(b"remote\0".as_ptr() as *const _) } |
| 36 |
} |
| 37 |
fn max_channel_count(&mut self) -> Result<u32> { |
| 38 |
Ok(0u32) |
| 39 |
} |
| 40 |
fn min_latency(&mut self, _params: StreamParams) -> Result<u32> { |
| 41 |
Ok(0u32) |
| 42 |
} |
| 43 |
fn preferred_sample_rate(&mut self) -> Result<u32> { |
| 44 |
Ok(0u32) |
| 45 |
} |
| 46 |
fn supported_input_processing_params(&mut self) -> Result<InputProcessingParams> { |
| 47 |
Ok(InputProcessingParams::NONE) |
| 48 |
} |
| 49 |
fn enumerate_devices( |
| 50 |
&mut self, |
| 51 |
_devtype: DeviceType, |
| 52 |
collection: &DeviceCollectionRef, |
| 53 |
) -> Result<()> { |
| 54 |
let coll = unsafe { &mut *collection.as_ptr() }; |
| 55 |
coll.device = 0xDEAD_BEEF as *mut _; |
| 56 |
coll.count = usize::max_value(); |
| 57 |
Ok(()) |
| 58 |
} |
| 59 |
fn device_collection_destroy(&mut self, collection: &mut DeviceCollectionRef) -> Result<()> { |
| 60 |
let coll = unsafe { &mut *collection.as_ptr() }; |
| 61 |
assert_eq!(coll.device, 0xDEAD_BEEF as *mut _); |
| 62 |
assert_eq!(coll.count, usize::max_value()); |
| 63 |
coll.device = ptr::null_mut(); |
| 64 |
coll.count = 0; |
| 65 |
Ok(()) |
| 66 |
} |
| 67 |
fn stream_init( |
| 68 |
&mut self, |
| 69 |
_stream_name: Option<&CStr>, |
| 70 |
_input_device: DeviceId, |
| 71 |
_input_stream_params: Option<&StreamParamsRef>, |
| 72 |
_output_device: DeviceId, |
| 73 |
_output_stream_params: Option<&StreamParamsRef>, |
| 74 |
_latency_frame: u32, |
| 75 |
_data_callback: ffi::cubeb_data_callback, |
| 76 |
_state_callback: ffi::cubeb_state_callback, |
| 77 |
_user_ptr: *mut c_void, |
| 78 |
) -> Result<Stream> { |
| 79 |
Ok(unsafe { Stream::from_ptr(0xDEAD_BEEF as *mut _) }) |
| 80 |
} |
| 81 |
fn register_device_collection_changed( |
| 82 |
&mut self, |
| 83 |
_dev_type: DeviceType, |
| 84 |
_collection_changed_callback: ffi::cubeb_device_collection_changed_callback, |
| 85 |
_user_ptr: *mut c_void, |
| 86 |
) -> Result<()> { |
| 87 |
Ok(()) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
struct TestStream {} |
| 92 |
|
| 93 |
impl StreamOps for TestStream { |
| 94 |
fn start(&mut self) -> Result<()> { |
| 95 |
Ok(()) |
| 96 |
} |
| 97 |
fn stop(&mut self) -> Result<()> { |
| 98 |
Ok(()) |
| 99 |
} |
| 100 |
fn position(&mut self) -> Result<u64> { |
| 101 |
Ok(0u64) |
| 102 |
} |
| 103 |
fn latency(&mut self) -> Result<u32> { |
| 104 |
Ok(0u32) |
| 105 |
} |
| 106 |
fn input_latency(&mut self) -> Result<u32> { |
| 107 |
Ok(0u32) |
| 108 |
} |
| 109 |
fn set_volume(&mut self, volume: f32) -> Result<()> { |
| 110 |
assert_eq!(volume, 0.5); |
| 111 |
Ok(()) |
| 112 |
} |
| 113 |
fn set_name(&mut self, name: &CStr) -> Result<()> { |
| 114 |
assert_eq!(name, CStr::from_bytes_with_nul(b"test\0").unwrap()); |
| 115 |
Ok(()) |
| 116 |
} |
| 117 |
fn current_device(&mut self) -> Result<&DeviceRef> { |
| 118 |
Ok(unsafe { DeviceRef::from_ptr(0xDEAD_BEEF as *mut _) }) |
| 119 |
} |
| 120 |
fn set_input_mute(&mut self, mute: bool) -> Result<()> { |
| 121 |
assert_eq!(mute, true); |
| 122 |
Ok(()) |
| 123 |
} |
| 124 |
fn set_input_processing_params(&mut self, params: InputProcessingParams) -> Result<()> { |
| 125 |
assert_eq!(params, InputProcessingParams::ECHO_CANCELLATION); |
| 126 |
Ok(()) |
| 127 |
} |
| 128 |
fn device_destroy(&mut self, device: &DeviceRef) -> Result<()> { |
| 129 |
assert_eq!(device.as_ptr(), 0xDEAD_BEEF as *mut _); |
| 130 |
Ok(()) |
| 131 |
} |
| 132 |
fn register_device_changed_callback( |
| 133 |
&mut self, |
| 134 |
_: ffi::cubeb_device_changed_callback, |
| 135 |
) -> Result<()> { |
| 136 |
Ok(()) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
#[test] |
| 141 |
fn test_ops_context_init() { |
| 142 |
let mut c: *mut ffi::cubeb = ptr::null_mut(); |
| 143 |
assert_eq!( |
| 144 |
unsafe { OPS.init.unwrap()(&mut c, ptr::null()) }, |
| 145 |
ffi::CUBEB_OK |
| 146 |
); |
| 147 |
unsafe { OPS.destroy.unwrap()(c) } |
| 148 |
} |
| 149 |
|
| 150 |
#[test] |
| 151 |
fn test_ops_context_max_channel_count() { |
| 152 |
let c: *mut ffi::cubeb = ptr::null_mut(); |
| 153 |
let mut max_channel_count = u32::max_value(); |
| 154 |
assert_eq!( |
| 155 |
unsafe { OPS.get_max_channel_count.unwrap()(c, &mut max_channel_count) }, |
| 156 |
ffi::CUBEB_OK |
| 157 |
); |
| 158 |
assert_eq!(max_channel_count, 0); |
| 159 |
} |
| 160 |
|
| 161 |
#[test] |
| 162 |
fn test_ops_context_min_latency() { |
| 163 |
let c: *mut ffi::cubeb = ptr::null_mut(); |
| 164 |
let params: ffi::cubeb_stream_params = unsafe { ::std::mem::zeroed() }; |
| 165 |
let mut latency = u32::max_value(); |
| 166 |
assert_eq!( |
| 167 |
unsafe { OPS.get_min_latency.unwrap()(c, params, &mut latency) }, |
| 168 |
ffi::CUBEB_OK |
| 169 |
); |
| 170 |
assert_eq!(latency, 0); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn test_ops_context_preferred_sample_rate() { |
| 175 |
let c: *mut ffi::cubeb = ptr::null_mut(); |
| 176 |
let mut rate = u32::max_value(); |
| 177 |
assert_eq!( |
| 178 |
unsafe { OPS.get_preferred_sample_rate.unwrap()(c, &mut rate) }, |
| 179 |
ffi::CUBEB_OK |
| 180 |
); |
| 181 |
assert_eq!(rate, 0); |
| 182 |
} |
| 183 |
|
| 184 |
#[test] |
| 185 |
fn test_ops_context_supported_input_processing_params() { |
| 186 |
let c: *mut ffi::cubeb = ptr::null_mut(); |
| 187 |
let mut params: ffi::cubeb_input_processing_params = InputProcessingParams::all().bits(); |
| 188 |
assert_eq!( |
| 189 |
unsafe { OPS.get_supported_input_processing_params.unwrap()(c, &mut params) }, |
| 190 |
ffi::CUBEB_OK |
| 191 |
); |
| 192 |
assert_eq!(params, ffi::CUBEB_INPUT_PROCESSING_PARAM_NONE); |
| 193 |
} |
| 194 |
|
| 195 |
#[test] |
| 196 |
fn test_ops_context_enumerate_devices() { |
| 197 |
let c: *mut ffi::cubeb = ptr::null_mut(); |
| 198 |
let mut coll = ffi::cubeb_device_collection { |
| 199 |
device: ptr::null_mut(), |
| 200 |
count: 0, |
| 201 |
}; |
| 202 |
assert_eq!( |
| 203 |
unsafe { OPS.enumerate_devices.unwrap()(c, 0, &mut coll) }, |
| 204 |
ffi::CUBEB_OK |
| 205 |
); |
| 206 |
assert_eq!(coll.device, 0xDEAD_BEEF as *mut _); |
| 207 |
assert_eq!(coll.count, usize::max_value()) |
| 208 |
} |
| 209 |
|
| 210 |
#[test] |
| 211 |
fn test_ops_context_device_collection_destroy() { |
| 212 |
let c: *mut ffi::cubeb = ptr::null_mut(); |
| 213 |
let mut coll = ffi::cubeb_device_collection { |
| 214 |
device: 0xDEAD_BEEF as *mut _, |
| 215 |
count: usize::max_value(), |
| 216 |
}; |
| 217 |
assert_eq!( |
| 218 |
unsafe { OPS.device_collection_destroy.unwrap()(c, &mut coll) }, |
| 219 |
ffi::CUBEB_OK |
| 220 |
); |
| 221 |
assert_eq!(coll.device, ptr::null_mut()); |
| 222 |
assert_eq!(coll.count, 0); |
| 223 |
} |
| 224 |
|
| 225 |
// stream_init: Some($crate::capi::capi_stream_init::<$ctx>), |
| 226 |
// stream_destroy: Some($crate::capi::capi_stream_destroy::<$stm>), |
| 227 |
// stream_start: Some($crate::capi::capi_stream_start::<$stm>), |
| 228 |
// stream_stop: Some($crate::capi::capi_stream_stop::<$stm>), |
| 229 |
// stream_get_position: Some($crate::capi::capi_stream_get_position::<$stm>), |
| 230 |
|
| 231 |
#[test] |
| 232 |
fn test_ops_stream_latency() { |
| 233 |
let s: *mut ffi::cubeb_stream = ptr::null_mut(); |
| 234 |
let mut latency = u32::max_value(); |
| 235 |
assert_eq!( |
| 236 |
unsafe { OPS.stream_get_latency.unwrap()(s, &mut latency) }, |
| 237 |
ffi::CUBEB_OK |
| 238 |
); |
| 239 |
assert_eq!(latency, 0); |
| 240 |
} |
| 241 |
|
| 242 |
#[test] |
| 243 |
fn test_ops_stream_set_volume() { |
| 244 |
let s: *mut ffi::cubeb_stream = ptr::null_mut(); |
| 245 |
unsafe { |
| 246 |
OPS.stream_set_volume.unwrap()(s, 0.5); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn test_ops_stream_set_name() { |
| 252 |
let s: *mut ffi::cubeb_stream = ptr::null_mut(); |
| 253 |
unsafe { |
| 254 |
OPS.stream_set_name.unwrap()(s, CStr::from_bytes_with_nul(b"test\0").unwrap().as_ptr()); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
#[test] |
| 259 |
fn test_ops_stream_current_device() { |
| 260 |
let s: *mut ffi::cubeb_stream = ptr::null_mut(); |
| 261 |
let mut device: *mut ffi::cubeb_device = ptr::null_mut(); |
| 262 |
assert_eq!( |
| 263 |
unsafe { OPS.stream_get_current_device.unwrap()(s, &mut device) }, |
| 264 |
ffi::CUBEB_OK |
| 265 |
); |
| 266 |
assert_eq!(device, 0xDEAD_BEEF as *mut _); |
| 267 |
} |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn test_ops_stream_set_input_mute() { |
| 271 |
let s: *mut ffi::cubeb_stream = ptr::null_mut(); |
| 272 |
assert_eq!( |
| 273 |
unsafe { OPS.stream_set_input_mute.unwrap()(s, 1) }, |
| 274 |
ffi::CUBEB_OK |
| 275 |
); |
| 276 |
} |
| 277 |
|
| 278 |
#[test] |
| 279 |
fn test_ops_stream_set_input_processing_params() { |
| 280 |
let s: *mut ffi::cubeb_stream = ptr::null_mut(); |
| 281 |
assert_eq!( |
| 282 |
unsafe { |
| 283 |
OPS.stream_set_input_processing_params.unwrap()( |
| 284 |
s, |
| 285 |
ffi::CUBEB_INPUT_PROCESSING_PARAM_ECHO_CANCELLATION, |
| 286 |
) |
| 287 |
}, |
| 288 |
ffi::CUBEB_OK |
| 289 |
); |
| 290 |
} |
| 291 |
|
| 292 |
#[test] |
| 293 |
fn test_ops_stream_device_destroy() { |
| 294 |
let s: *mut ffi::cubeb_stream = ptr::null_mut(); |
| 295 |
unsafe { |
| 296 |
OPS.stream_device_destroy.unwrap()(s, 0xDEAD_BEEF as *mut _); |
| 297 |
} |
| 298 |
} |