| 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 |
use crate::PlatformHandle; |
| 7 |
use crate::PlatformHandleType; |
| 8 |
use crate::INVALID_HANDLE_VALUE; |
| 9 |
#[cfg(target_os = "linux")] |
| 10 |
use audio_thread_priority::RtPriorityThreadInfo; |
| 11 |
use cubeb::ffi; |
| 12 |
use serde_derive::Deserialize; |
| 13 |
use serde_derive::Serialize; |
| 14 |
use std::ffi::{CStr, CString}; |
| 15 |
use std::os::raw::{c_char, c_int, c_uint}; |
| 16 |
use std::ptr; |
| 17 |
|
| 18 |
#[derive(Debug, Serialize, Deserialize)] |
| 19 |
pub struct Device { |
| 20 |
#[serde(with = "serde_bytes")] |
| 21 |
pub output_name: Option<Vec<u8>>, |
| 22 |
#[serde(with = "serde_bytes")] |
| 23 |
pub input_name: Option<Vec<u8>>, |
| 24 |
} |
| 25 |
|
| 26 |
impl<'a> From<&'a cubeb::DeviceRef> for Device { |
| 27 |
fn from(info: &'a cubeb::DeviceRef) -> Self { |
| 28 |
Self { |
| 29 |
output_name: info.output_name_bytes().map(|s| s.to_vec()), |
| 30 |
input_name: info.input_name_bytes().map(|s| s.to_vec()), |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
impl From<ffi::cubeb_device> for Device { |
| 36 |
fn from(info: ffi::cubeb_device) -> Self { |
| 37 |
Self { |
| 38 |
output_name: dup_str(info.output_name), |
| 39 |
input_name: dup_str(info.input_name), |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl From<Device> for ffi::cubeb_device { |
| 45 |
fn from(info: Device) -> Self { |
| 46 |
Self { |
| 47 |
output_name: opt_str(info.output_name), |
| 48 |
input_name: opt_str(info.input_name), |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
#[derive(Debug, Serialize, Deserialize)] |
| 54 |
pub struct DeviceInfo { |
| 55 |
pub devid: usize, |
| 56 |
#[serde(with = "serde_bytes")] |
| 57 |
pub device_id: Option<Vec<u8>>, |
| 58 |
#[serde(with = "serde_bytes")] |
| 59 |
pub friendly_name: Option<Vec<u8>>, |
| 60 |
#[serde(with = "serde_bytes")] |
| 61 |
pub group_id: Option<Vec<u8>>, |
| 62 |
#[serde(with = "serde_bytes")] |
| 63 |
pub vendor_name: Option<Vec<u8>>, |
| 64 |
|
| 65 |
pub device_type: ffi::cubeb_device_type, |
| 66 |
pub state: ffi::cubeb_device_state, |
| 67 |
pub preferred: ffi::cubeb_device_pref, |
| 68 |
|
| 69 |
pub format: ffi::cubeb_device_fmt, |
| 70 |
pub default_format: ffi::cubeb_device_fmt, |
| 71 |
pub max_channels: u32, |
| 72 |
pub default_rate: u32, |
| 73 |
pub max_rate: u32, |
| 74 |
pub min_rate: u32, |
| 75 |
|
| 76 |
pub latency_lo: u32, |
| 77 |
pub latency_hi: u32, |
| 78 |
} |
| 79 |
|
| 80 |
impl<'a> From<&'a cubeb::DeviceInfoRef> for DeviceInfo { |
| 81 |
fn from(info: &'a cubeb::DeviceInfoRef) -> Self { |
| 82 |
let info = unsafe { &*info.as_ptr() }; |
| 83 |
DeviceInfo { |
| 84 |
devid: info.devid as _, |
| 85 |
device_id: dup_str(info.device_id), |
| 86 |
friendly_name: dup_str(info.friendly_name), |
| 87 |
group_id: dup_str(info.group_id), |
| 88 |
vendor_name: dup_str(info.vendor_name), |
| 89 |
|
| 90 |
device_type: info.device_type, |
| 91 |
state: info.state, |
| 92 |
preferred: info.preferred, |
| 93 |
|
| 94 |
format: info.format, |
| 95 |
default_format: info.default_format, |
| 96 |
max_channels: info.max_channels, |
| 97 |
default_rate: info.default_rate, |
| 98 |
max_rate: info.max_rate, |
| 99 |
min_rate: info.min_rate, |
| 100 |
|
| 101 |
latency_lo: info.latency_lo, |
| 102 |
latency_hi: info.latency_hi, |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl From<DeviceInfo> for ffi::cubeb_device_info { |
| 108 |
fn from(info: DeviceInfo) -> Self { |
| 109 |
ffi::cubeb_device_info { |
| 110 |
devid: info.devid as _, |
| 111 |
device_id: opt_str(info.device_id), |
| 112 |
friendly_name: opt_str(info.friendly_name), |
| 113 |
group_id: opt_str(info.group_id), |
| 114 |
vendor_name: opt_str(info.vendor_name), |
| 115 |
|
| 116 |
device_type: info.device_type, |
| 117 |
state: info.state, |
| 118 |
preferred: info.preferred, |
| 119 |
|
| 120 |
format: info.format, |
| 121 |
default_format: info.default_format, |
| 122 |
max_channels: info.max_channels, |
| 123 |
default_rate: info.default_rate, |
| 124 |
max_rate: info.max_rate, |
| 125 |
min_rate: info.min_rate, |
| 126 |
|
| 127 |
latency_lo: info.latency_lo, |
| 128 |
latency_hi: info.latency_hi, |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
#[repr(C)] |
| 134 |
#[derive(Clone, Copy, Debug, Deserialize, Serialize)] |
| 135 |
pub struct StreamParams { |
| 136 |
pub format: ffi::cubeb_sample_format, |
| 137 |
pub rate: c_uint, |
| 138 |
pub channels: c_uint, |
| 139 |
pub layout: ffi::cubeb_channel_layout, |
| 140 |
pub prefs: ffi::cubeb_stream_prefs, |
| 141 |
} |
| 142 |
|
| 143 |
impl From<&cubeb::StreamParamsRef> for StreamParams { |
| 144 |
fn from(x: &cubeb::StreamParamsRef) -> StreamParams { |
| 145 |
unsafe { *(x.as_ptr() as *mut StreamParams) } |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
#[derive(Clone, Debug, Serialize, Deserialize)] |
| 150 |
pub struct StreamCreateParams { |
| 151 |
#[serde(with = "serde_bytes")] |
| 152 |
pub stream_name: Option<Vec<u8>>, |
| 153 |
pub input_device: usize, |
| 154 |
pub input_stream_params: Option<StreamParams>, |
| 155 |
pub output_device: usize, |
| 156 |
pub output_stream_params: Option<StreamParams>, |
| 157 |
pub latency_frames: u32, |
| 158 |
} |
| 159 |
|
| 160 |
fn dup_str(s: *const c_char) -> Option<Vec<u8>> { |
| 161 |
if s.is_null() { |
| 162 |
None |
| 163 |
} else { |
| 164 |
let vec: Vec<u8> = unsafe { CStr::from_ptr(s) }.to_bytes().to_vec(); |
| 165 |
Some(vec) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
fn opt_str(v: Option<Vec<u8>>) -> *mut c_char { |
| 170 |
match v { |
| 171 |
Some(v) => match CString::new(v) { |
| 172 |
Ok(s) => s.into_raw(), |
| 173 |
Err(_) => { |
| 174 |
debug!("Failed to convert bytes to CString"); |
| 175 |
ptr::null_mut() |
| 176 |
} |
| 177 |
}, |
| 178 |
None => ptr::null_mut(), |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
#[derive(Debug, Serialize, Deserialize)] |
| 183 |
pub struct StreamCreate { |
| 184 |
pub token: usize, |
| 185 |
pub shm_handle: SerializableHandle, |
| 186 |
pub shm_area_size: usize, |
| 187 |
} |
| 188 |
|
| 189 |
#[derive(Debug, Serialize, Deserialize)] |
| 190 |
pub struct RegisterDeviceCollectionChanged { |
| 191 |
pub platform_handle: SerializableHandle, |
| 192 |
} |
| 193 |
|
| 194 |
// Client -> Server messages. |
| 195 |
// TODO: Callbacks should be different messages types so |
| 196 |
// ServerConn::process_msg doesn't have a catch-all case. |
| 197 |
#[derive(Debug, Serialize, Deserialize)] |
| 198 |
pub enum ServerMessage { |
| 199 |
ClientConnect(u32), |
| 200 |
ClientDisconnect, |
| 201 |
|
| 202 |
ContextGetBackendId, |
| 203 |
ContextGetMaxChannelCount, |
| 204 |
ContextGetMinLatency(StreamParams), |
| 205 |
ContextGetPreferredSampleRate, |
| 206 |
ContextGetSupportedInputProcessingParams, |
| 207 |
ContextGetDeviceEnumeration(ffi::cubeb_device_type), |
| 208 |
ContextSetupDeviceCollectionCallback, |
| 209 |
ContextRegisterDeviceCollectionChanged(ffi::cubeb_device_type, bool), |
| 210 |
|
| 211 |
StreamCreate(StreamCreateParams), |
| 212 |
StreamInit(usize), |
| 213 |
StreamDestroy(usize), |
| 214 |
|
| 215 |
StreamStart(usize), |
| 216 |
StreamStop(usize), |
| 217 |
StreamGetPosition(usize), |
| 218 |
StreamGetLatency(usize), |
| 219 |
StreamGetInputLatency(usize), |
| 220 |
StreamSetVolume(usize, f32), |
| 221 |
StreamSetName(usize, CString), |
| 222 |
StreamGetCurrentDevice(usize), |
| 223 |
StreamSetInputMute(usize, bool), |
| 224 |
StreamSetInputProcessingParams(usize, ffi::cubeb_input_processing_params), |
| 225 |
StreamRegisterDeviceChangeCallback(usize, bool), |
| 226 |
|
| 227 |
#[cfg(target_os = "linux")] |
| 228 |
PromoteThreadToRealTime([u8; std::mem::size_of::<RtPriorityThreadInfo>()]), |
| 229 |
} |
| 230 |
|
| 231 |
// Server -> Client messages. |
| 232 |
// TODO: Streams need id. |
| 233 |
#[derive(Debug, Serialize, Deserialize)] |
| 234 |
pub enum ClientMessage { |
| 235 |
ClientConnected, |
| 236 |
ClientDisconnected, |
| 237 |
|
| 238 |
ContextBackendId(String), |
| 239 |
ContextMaxChannelCount(u32), |
| 240 |
ContextMinLatency(u32), |
| 241 |
ContextPreferredSampleRate(u32), |
| 242 |
ContextSupportedInputProcessingParams(ffi::cubeb_input_processing_params), |
| 243 |
ContextEnumeratedDevices(Vec<DeviceInfo>), |
| 244 |
ContextSetupDeviceCollectionCallback(RegisterDeviceCollectionChanged), |
| 245 |
ContextRegisteredDeviceCollectionChanged, |
| 246 |
|
| 247 |
StreamCreated(StreamCreate), |
| 248 |
StreamInitialized(SerializableHandle), |
| 249 |
StreamDestroyed, |
| 250 |
|
| 251 |
StreamStarted, |
| 252 |
StreamStopped, |
| 253 |
StreamPosition(u64), |
| 254 |
StreamLatency(u32), |
| 255 |
StreamInputLatency(u32), |
| 256 |
StreamVolumeSet, |
| 257 |
StreamNameSet, |
| 258 |
StreamCurrentDevice(Device), |
| 259 |
StreamInputMuteSet, |
| 260 |
StreamInputProcessingParamsSet, |
| 261 |
StreamRegisterDeviceChangeCallback, |
| 262 |
|
| 263 |
#[cfg(target_os = "linux")] |
| 264 |
ThreadPromoted, |
| 265 |
|
| 266 |
Error(c_int), |
| 267 |
} |
| 268 |
|
| 269 |
#[derive(Debug, Deserialize, Serialize)] |
| 270 |
pub enum CallbackReq { |
| 271 |
Data { |
| 272 |
nframes: isize, |
| 273 |
input_frame_size: usize, |
| 274 |
output_frame_size: usize, |
| 275 |
}, |
| 276 |
State(ffi::cubeb_state), |
| 277 |
DeviceChange, |
| 278 |
} |
| 279 |
|
| 280 |
#[derive(Debug, Deserialize, Serialize)] |
| 281 |
pub enum CallbackResp { |
| 282 |
Data(isize), |
| 283 |
State, |
| 284 |
DeviceChange, |
| 285 |
Error(c_int), |
| 286 |
} |
| 287 |
|
| 288 |
#[derive(Debug, Deserialize, Serialize)] |
| 289 |
pub enum DeviceCollectionReq { |
| 290 |
DeviceChange(ffi::cubeb_device_type), |
| 291 |
} |
| 292 |
|
| 293 |
#[derive(Debug, Deserialize, Serialize)] |
| 294 |
pub enum DeviceCollectionResp { |
| 295 |
DeviceChange, |
| 296 |
} |
| 297 |
|
| 298 |
// Represents a platform handle in various transitional states during serialization and remoting. |
| 299 |
// The process of serializing and remoting handles and the ownership during various states differs |
| 300 |
// between Windows and Unix. SerializableHandle changes during IPC as follows: |
| 301 |
// |
| 302 |
// 1. Created in the initial state `Owned`, with a valid `target_pid`. |
| 303 |
// 2. Ownership is transferred out for processing during IPC send, becoming `Empty` temporarily. |
| 304 |
// See `AssociateHandleForMessage::take_handle`. |
| 305 |
// - Windows: DuplicateHandle transfers the handle to the remote process. |
| 306 |
// This produces a new handle value in the local process representing the remote handle. |
| 307 |
// This value must be sent to the remote, so `AssociateHandleForMessage::set_remote_handle` |
| 308 |
// is used to transform the handle into a `SerializableValue`. |
| 309 |
// - Unix: sendmsg transfers the handle to the remote process. The handle is left `Empty`. |
| 310 |
// (Note: this occurs later, when the serialized message buffer is sent) |
| 311 |
// 3. Message containing `SerializableValue` or `Empty` (depending on handle processing in step 2) |
| 312 |
// is serialized and sent via IPC. |
| 313 |
// 4. Message received and deserialized in target process. |
| 314 |
// - Windows: `AssociateHandleForMessage::set_local_handle converts the received `SerializableValue` into `Owned`, ready for use. |
| 315 |
// - Unix: Handle (with a new value in the target process) is received out-of-band via `recvmsg` |
| 316 |
// and converted to `Owned` via `AssociateHandleForMessage::set_local_handle`. |
| 317 |
#[derive(Debug)] |
| 318 |
pub enum SerializableHandle { |
| 319 |
// Owned handle, with optional target_pid on sending side. |
| 320 |
Owned(PlatformHandle, Option<u32>), |
| 321 |
// Transitional IPC states: |
| 322 |
SerializableValue(PlatformHandleType), // Windows |
| 323 |
Empty, // Unix |
| 324 |
} |
| 325 |
|
| 326 |
// PlatformHandle is non-Send and contains a pointer (HANDLE) on Windows. |
| 327 |
#[allow(clippy::non_send_fields_in_send_ty)] |
| 328 |
unsafe impl Send for SerializableHandle {} |
| 329 |
|
| 330 |
impl SerializableHandle { |
| 331 |
pub fn new(handle: PlatformHandle, target_pid: u32) -> SerializableHandle { |
| 332 |
SerializableHandle::Owned(handle, Some(target_pid)) |
| 333 |
} |
| 334 |
|
| 335 |
// Called on the receiving side to take ownership of the handle. |
| 336 |
pub fn take_handle(&mut self) -> PlatformHandle { |
| 337 |
match std::mem::replace(self, SerializableHandle::Empty) { |
| 338 |
SerializableHandle::Owned(handle, target_pid) => { |
| 339 |
assert!(target_pid.is_none()); |
| 340 |
handle |
| 341 |
} |
| 342 |
_ => panic!("take_handle called in invalid state"), |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
// Called on the sending side to take ownership of the handle for |
| 347 |
// handling platform-specific remoting. |
| 348 |
fn take_handle_for_send(&mut self) -> RemoteHandle { |
| 349 |
match std::mem::replace(self, SerializableHandle::Empty) { |
| 350 |
SerializableHandle::Owned(handle, target_pid) => unsafe { |
| 351 |
RemoteHandle::new( |
| 352 |
handle.into_raw(), |
| 353 |
target_pid.expect("target process required"), |
| 354 |
) |
| 355 |
}, |
| 356 |
_ => panic!("take_handle_for_send called in invalid state"), |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
fn new_owned(handle: PlatformHandleType) -> SerializableHandle { |
| 361 |
SerializableHandle::Owned(PlatformHandle::new(handle), None) |
| 362 |
} |
| 363 |
|
| 364 |
#[cfg(windows)] |
| 365 |
fn make_owned(&mut self) { |
| 366 |
if let SerializableHandle::SerializableValue(handle) = self { |
| 367 |
*self = SerializableHandle::new_owned(*handle); |
| 368 |
} else { |
| 369 |
panic!("make_owned called in invalid state") |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
fn new_serializable_value(handle: PlatformHandleType) -> SerializableHandle { |
| 374 |
SerializableHandle::SerializableValue(handle) |
| 375 |
} |
| 376 |
|
| 377 |
fn get_serializable_value(&self) -> PlatformHandleType { |
| 378 |
match *self { |
| 379 |
SerializableHandle::SerializableValue(handle) => handle, |
| 380 |
SerializableHandle::Empty => INVALID_HANDLE_VALUE, |
| 381 |
_ => panic!("get_remote_handle called in invalid state"), |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
// Raw handle values are serialized as i64. Additional handling external to (de)serialization is required during IPC |
| 387 |
// send/receive to convert these raw values into valid handles. |
| 388 |
impl serde::Serialize for SerializableHandle { |
| 389 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 390 |
where |
| 391 |
S: serde::Serializer, |
| 392 |
{ |
| 393 |
let handle = self.get_serializable_value(); |
| 394 |
serializer.serialize_i64(handle as i64) |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
impl<'de> serde::Deserialize<'de> for SerializableHandle { |
| 399 |
fn deserialize<D>(deserializer: D) -> Result<SerializableHandle, D::Error> |
| 400 |
where |
| 401 |
D: serde::Deserializer<'de>, |
| 402 |
{ |
| 403 |
deserializer.deserialize_i64(SerializableHandleVisitor) |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
struct SerializableHandleVisitor; |
| 408 |
impl serde::de::Visitor<'_> for SerializableHandleVisitor { |
| 409 |
type Value = SerializableHandle; |
| 410 |
|
| 411 |
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 412 |
formatter.write_str("an integer between -2^63 and 2^63") |
| 413 |
} |
| 414 |
|
| 415 |
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E> |
| 416 |
where |
| 417 |
E: serde::de::Error, |
| 418 |
{ |
| 419 |
Ok(SerializableHandle::new_serializable_value( |
| 420 |
value as PlatformHandleType, |
| 421 |
)) |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
// Represents a PlatformHandle in-flight between processes. |
| 426 |
// On Unix platforms, this is just a plain owned Handle, closed on drop. |
| 427 |
// On Windows, `RemoteHandle` also retains ownership of the `target_handle` |
| 428 |
// in the `target` process. Once the handle has been successfully sent |
| 429 |
// to the remote, the sender should call `mark_sent()` to relinquish |
| 430 |
// ownership of `target_handle` in the remote. |
| 431 |
#[derive(Debug)] |
| 432 |
pub struct RemoteHandle { |
| 433 |
pub(crate) handle: PlatformHandleType, |
| 434 |
#[cfg(windows)] |
| 435 |
pub(crate) target: u32, |
| 436 |
#[cfg(windows)] |
| 437 |
pub(crate) target_handle: Option<PlatformHandleType>, |
| 438 |
} |
| 439 |
|
| 440 |
impl RemoteHandle { |
| 441 |
#[allow(clippy::missing_safety_doc)] |
| 442 |
pub unsafe fn new(handle: PlatformHandleType, _target: u32) -> Self { |
| 443 |
RemoteHandle { |
| 444 |
handle, |
| 445 |
#[cfg(windows)] |
| 446 |
target: _target, |
| 447 |
#[cfg(windows)] |
| 448 |
target_handle: None, |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
#[cfg(windows)] |
| 453 |
pub fn mark_sent(&mut self) { |
| 454 |
self.target_handle.take(); |
| 455 |
} |
| 456 |
|
| 457 |
#[cfg(windows)] |
| 458 |
#[allow(clippy::missing_safety_doc)] |
| 459 |
pub unsafe fn send_to_target(&mut self) -> std::io::Result<PlatformHandleType> { |
| 460 |
let target_handle = crate::duplicate_platform_handle(self.handle, Some(self.target))?; |
| 461 |
self.target_handle = Some(target_handle); |
| 462 |
Ok(target_handle) |
| 463 |
} |
| 464 |
|
| 465 |
#[cfg(unix)] |
| 466 |
#[allow(clippy::missing_safety_doc)] |
| 467 |
pub unsafe fn take(self) -> PlatformHandleType { |
| 468 |
let h = self.handle; |
| 469 |
std::mem::forget(self); |
| 470 |
h |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
impl Drop for RemoteHandle { |
| 475 |
fn drop(&mut self) { |
| 476 |
unsafe { |
| 477 |
crate::close_platform_handle(self.handle); |
| 478 |
} |
| 479 |
#[cfg(windows)] |
| 480 |
unsafe { |
| 481 |
if let Some(target_handle) = self.target_handle { |
| 482 |
if let Err(e) = crate::close_target_handle(target_handle, self.target) { |
| 483 |
trace!("RemoteHandle failed to close target handle: {:?}", e); |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
unsafe impl Send for RemoteHandle {} |
| 491 |
|
| 492 |
pub trait AssociateHandleForMessage { |
| 493 |
// True if this item has an associated handle attached for remoting. |
| 494 |
fn has_associated_handle(&self) -> bool { |
| 495 |
false |
| 496 |
} |
| 497 |
|
| 498 |
// Take ownership of the associated handle, leaving the item's |
| 499 |
// associated handle empty. |
| 500 |
fn take_handle(&mut self) -> RemoteHandle { |
| 501 |
panic!("take_handle called on item without associated handle"); |
| 502 |
} |
| 503 |
|
| 504 |
#[allow(clippy::missing_safety_doc)] |
| 505 |
// Replace an empty associated handle with a non-owning serializable value |
| 506 |
// indicating the value of the handle in the remote process. |
| 507 |
#[cfg(windows)] |
| 508 |
unsafe fn set_remote_handle(&mut self, _: PlatformHandleType) { |
| 509 |
panic!("set_remote_handle called on item without associated handle"); |
| 510 |
} |
| 511 |
|
| 512 |
#[allow(clippy::missing_safety_doc)] |
| 513 |
// Replace a serialized associated handle value with an owned local handle. |
| 514 |
#[cfg(windows)] |
| 515 |
unsafe fn set_local_handle(&mut self) { |
| 516 |
panic!("set_local_handle called on item without associated handle"); |
| 517 |
} |
| 518 |
|
| 519 |
#[allow(clippy::missing_safety_doc)] |
| 520 |
// Replace an empty associated handle with an owned local handle. |
| 521 |
#[cfg(unix)] |
| 522 |
unsafe fn set_local_handle(&mut self, _: PlatformHandleType) { |
| 523 |
panic!("set_local_handle called on item without associated handle"); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
impl AssociateHandleForMessage for ClientMessage { |
| 528 |
fn has_associated_handle(&self) -> bool { |
| 529 |
matches!( |
| 530 |
*self, |
| 531 |
ClientMessage::StreamCreated(_) |
| 532 |
| ClientMessage::StreamInitialized(_) |
| 533 |
| ClientMessage::ContextSetupDeviceCollectionCallback(_) |
| 534 |
) |
| 535 |
} |
| 536 |
|
| 537 |
fn take_handle(&mut self) -> RemoteHandle { |
| 538 |
match *self { |
| 539 |
ClientMessage::StreamCreated(ref mut data) => data.shm_handle.take_handle_for_send(), |
| 540 |
ClientMessage::StreamInitialized(ref mut data) => data.take_handle_for_send(), |
| 541 |
ClientMessage::ContextSetupDeviceCollectionCallback(ref mut data) => { |
| 542 |
data.platform_handle.take_handle_for_send() |
| 543 |
} |
| 544 |
_ => panic!("take_handle called on item without associated handle"), |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
#[cfg(windows)] |
| 549 |
unsafe fn set_remote_handle(&mut self, handle: PlatformHandleType) { |
| 550 |
match *self { |
| 551 |
ClientMessage::StreamCreated(ref mut data) => { |
| 552 |
data.shm_handle = SerializableHandle::new_serializable_value(handle); |
| 553 |
} |
| 554 |
ClientMessage::StreamInitialized(ref mut data) => { |
| 555 |
*data = SerializableHandle::new_serializable_value(handle); |
| 556 |
} |
| 557 |
ClientMessage::ContextSetupDeviceCollectionCallback(ref mut data) => { |
| 558 |
data.platform_handle = SerializableHandle::new_serializable_value(handle); |
| 559 |
} |
| 560 |
_ => panic!("set_remote_handle called on item without associated handle"), |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
#[cfg(windows)] |
| 565 |
unsafe fn set_local_handle(&mut self) { |
| 566 |
match *self { |
| 567 |
ClientMessage::StreamCreated(ref mut data) => data.shm_handle.make_owned(), |
| 568 |
ClientMessage::StreamInitialized(ref mut data) => data.make_owned(), |
| 569 |
ClientMessage::ContextSetupDeviceCollectionCallback(ref mut data) => { |
| 570 |
data.platform_handle.make_owned() |
| 571 |
} |
| 572 |
_ => panic!("set_local_handle called on item without associated handle"), |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
#[cfg(unix)] |
| 577 |
unsafe fn set_local_handle(&mut self, handle: PlatformHandleType) { |
| 578 |
match *self { |
| 579 |
ClientMessage::StreamCreated(ref mut data) => { |
| 580 |
data.shm_handle = SerializableHandle::new_owned(handle); |
| 581 |
} |
| 582 |
ClientMessage::StreamInitialized(ref mut data) => { |
| 583 |
*data = SerializableHandle::new_owned(handle); |
| 584 |
} |
| 585 |
ClientMessage::ContextSetupDeviceCollectionCallback(ref mut data) => { |
| 586 |
data.platform_handle = SerializableHandle::new_owned(handle); |
| 587 |
} |
| 588 |
_ => panic!("set_local_handle called on item without associated handle"), |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
impl AssociateHandleForMessage for ServerMessage {} |
| 594 |
|
| 595 |
impl AssociateHandleForMessage for DeviceCollectionReq {} |
| 596 |
impl AssociateHandleForMessage for DeviceCollectionResp {} |
| 597 |
|
| 598 |
impl AssociateHandleForMessage for CallbackReq {} |
| 599 |
impl AssociateHandleForMessage for CallbackResp {} |
| 600 |
|
| 601 |
#[cfg(test)] |
| 602 |
mod test { |
| 603 |
use super::StreamParams; |
| 604 |
use cubeb::ffi; |
| 605 |
use std::mem; |
| 606 |
|
| 607 |
#[test] |
| 608 |
fn stream_params_size_check() { |
| 609 |
assert_eq!( |
| 610 |
mem::size_of::<StreamParams>(), |
| 611 |
mem::size_of::<ffi::cubeb_stream_params>() |
| 612 |
) |
| 613 |
} |
| 614 |
|
| 615 |
#[test] |
| 616 |
fn stream_params_from() { |
| 617 |
let raw = ffi::cubeb_stream_params { |
| 618 |
format: ffi::CUBEB_SAMPLE_FLOAT32BE, |
| 619 |
rate: 96_000, |
| 620 |
channels: 32, |
| 621 |
layout: ffi::CUBEB_LAYOUT_3F1_LFE, |
| 622 |
prefs: ffi::CUBEB_STREAM_PREF_LOOPBACK, |
| 623 |
}; |
| 624 |
let wrapped = ::cubeb::StreamParams::from(raw); |
| 625 |
let params = StreamParams::from(wrapped.as_ref()); |
| 626 |
assert_eq!(params.format, raw.format); |
| 627 |
assert_eq!(params.rate, raw.rate); |
| 628 |
assert_eq!(params.channels, raw.channels); |
| 629 |
assert_eq!(params.layout, raw.layout); |
| 630 |
assert_eq!(params.prefs, raw.prefs); |
| 631 |
} |
| 632 |
} |