| 1 |
// Copyright © 2017-2018 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 backend::*; |
| 7 |
use cubeb_backend::{ |
| 8 |
ffi, log_enabled, Context, ContextOps, DeviceCollectionRef, DeviceId, DeviceType, Error, |
| 9 |
InputProcessingParams, Ops, Result, Stream, StreamParams, StreamParamsRef, |
| 10 |
}; |
| 11 |
use pulse::{self, ProplistExt}; |
| 12 |
use pulse_ffi::*; |
| 13 |
use semver; |
| 14 |
use std::cell::RefCell; |
| 15 |
use std::default::Default; |
| 16 |
use std::ffi::{CStr, CString}; |
| 17 |
use std::mem; |
| 18 |
use std::os::raw::c_void; |
| 19 |
use std::ptr; |
| 20 |
|
| 21 |
#[derive(Debug)] |
| 22 |
pub struct DefaultInfo { |
| 23 |
pub sample_spec: pulse::SampleSpec, |
| 24 |
pub channel_map: pulse::ChannelMap, |
| 25 |
pub flags: pulse::SinkFlags, |
| 26 |
} |
| 27 |
|
| 28 |
pub const PULSE_OPS: Ops = capi_new!(PulseContext, PulseStream); |
| 29 |
|
| 30 |
#[repr(C)] |
| 31 |
#[derive(Debug)] |
| 32 |
pub struct PulseContext { |
| 33 |
_ops: *const Ops, |
| 34 |
pub mainloop: pulse::ThreadedMainloop, |
| 35 |
pub context: Option<pulse::Context>, |
| 36 |
pub default_sink_info: Option<DefaultInfo>, |
| 37 |
pub context_name: Option<CString>, |
| 38 |
pub input_collection_changed_callback: ffi::cubeb_device_collection_changed_callback, |
| 39 |
pub input_collection_changed_user_ptr: *mut c_void, |
| 40 |
pub output_collection_changed_callback: ffi::cubeb_device_collection_changed_callback, |
| 41 |
pub output_collection_changed_user_ptr: *mut c_void, |
| 42 |
pub error: bool, |
| 43 |
pub version_2_0_0: bool, |
| 44 |
pub version_0_9_8: bool, |
| 45 |
#[cfg(feature = "pulse-dlopen")] |
| 46 |
pub libpulse: LibLoader, |
| 47 |
devids: RefCell<Intern>, |
| 48 |
} |
| 49 |
|
| 50 |
impl PulseContext { |
| 51 |
#[cfg(feature = "pulse-dlopen")] |
| 52 |
fn _new(name: Option<CString>) -> Result<Box<Self>> { |
| 53 |
let libpulse = unsafe { open() }; |
| 54 |
if libpulse.is_none() { |
| 55 |
cubeb_log!("libpulse not found"); |
| 56 |
return Err(Error::error()); |
| 57 |
} |
| 58 |
|
| 59 |
let ctx = Box::new(PulseContext { |
| 60 |
_ops: &PULSE_OPS, |
| 61 |
libpulse: libpulse.unwrap(), |
| 62 |
mainloop: pulse::ThreadedMainloop::new(), |
| 63 |
context: None, |
| 64 |
default_sink_info: None, |
| 65 |
context_name: name, |
| 66 |
input_collection_changed_callback: None, |
| 67 |
input_collection_changed_user_ptr: ptr::null_mut(), |
| 68 |
output_collection_changed_callback: None, |
| 69 |
output_collection_changed_user_ptr: ptr::null_mut(), |
| 70 |
error: true, |
| 71 |
version_0_9_8: false, |
| 72 |
version_2_0_0: false, |
| 73 |
devids: RefCell::new(Intern::new()), |
| 74 |
}); |
| 75 |
|
| 76 |
Ok(ctx) |
| 77 |
} |
| 78 |
|
| 79 |
#[cfg(not(feature = "pulse-dlopen"))] |
| 80 |
fn _new(name: Option<CString>) -> Result<Box<Self>> { |
| 81 |
Ok(Box::new(PulseContext { |
| 82 |
_ops: &PULSE_OPS, |
| 83 |
mainloop: pulse::ThreadedMainloop::new(), |
| 84 |
context: None, |
| 85 |
default_sink_info: None, |
| 86 |
context_name: name, |
| 87 |
input_collection_changed_callback: None, |
| 88 |
input_collection_changed_user_ptr: ptr::null_mut(), |
| 89 |
output_collection_changed_callback: None, |
| 90 |
output_collection_changed_user_ptr: ptr::null_mut(), |
| 91 |
error: true, |
| 92 |
version_0_9_8: false, |
| 93 |
version_2_0_0: false, |
| 94 |
devids: RefCell::new(Intern::new()), |
| 95 |
})) |
| 96 |
} |
| 97 |
|
| 98 |
fn server_info_cb(context: &pulse::Context, info: Option<&pulse::ServerInfo>, u: *mut c_void) { |
| 99 |
fn sink_info_cb(_: &pulse::Context, i: *const pulse::SinkInfo, eol: i32, u: *mut c_void) { |
| 100 |
let ctx = unsafe { &mut *(u as *mut PulseContext) }; |
| 101 |
if eol == 0 { |
| 102 |
let info = unsafe { &*i }; |
| 103 |
let flags = pulse::SinkFlags::from_bits_truncate(info.flags); |
| 104 |
ctx.default_sink_info = Some(DefaultInfo { |
| 105 |
sample_spec: info.sample_spec, |
| 106 |
channel_map: info.channel_map, |
| 107 |
flags, |
| 108 |
}); |
| 109 |
} |
| 110 |
ctx.mainloop.signal(); |
| 111 |
} |
| 112 |
|
| 113 |
if let Some(info) = info { |
| 114 |
let _ = context.get_sink_info_by_name( |
| 115 |
try_cstr_from(info.default_sink_name), |
| 116 |
sink_info_cb, |
| 117 |
u, |
| 118 |
); |
| 119 |
} else { |
| 120 |
// If info is None, then an error occured. |
| 121 |
let ctx = unsafe { &mut *(u as *mut PulseContext) }; |
| 122 |
ctx.mainloop.signal(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
fn new(name: Option<&CStr>) -> Result<Box<Self>> { |
| 127 |
let name = name.map(|s| s.to_owned()); |
| 128 |
let mut ctx = PulseContext::_new(name)?; |
| 129 |
|
| 130 |
if ctx.mainloop.start().is_err() { |
| 131 |
ctx.destroy(); |
| 132 |
cubeb_log!("Error: couldn't start pulse's mainloop"); |
| 133 |
return Err(Error::error()); |
| 134 |
} |
| 135 |
|
| 136 |
if ctx.context_init().is_err() { |
| 137 |
ctx.destroy(); |
| 138 |
cubeb_log!("Error: couldn't init pulse's context"); |
| 139 |
return Err(Error::error()); |
| 140 |
} |
| 141 |
|
| 142 |
ctx.mainloop.lock(); |
| 143 |
/* server_info_callback performs a second async query, |
| 144 |
* which is responsible for initializing default_sink_info |
| 145 |
* and signalling the mainloop to end the wait. */ |
| 146 |
let user_data: *mut c_void = ctx.as_mut() as *mut _ as *mut _; |
| 147 |
if let Some(ref context) = ctx.context { |
| 148 |
if let Ok(o) = context.get_server_info(PulseContext::server_info_cb, user_data) { |
| 149 |
ctx.operation_wait(None, &o); |
| 150 |
} |
| 151 |
} |
| 152 |
ctx.mainloop.unlock(); |
| 153 |
|
| 154 |
/* Update `default_sink_info` when the default device changes. */ |
| 155 |
if let Err(e) = ctx.subscribe_notifications(pulse::SubscriptionMask::SERVER) { |
| 156 |
cubeb_log!("subscribe_notifications ignored failure: {}", e); |
| 157 |
} |
| 158 |
|
| 159 |
// Return the result. |
| 160 |
Ok(ctx) |
| 161 |
} |
| 162 |
|
| 163 |
pub fn destroy(&mut self) { |
| 164 |
self.context_destroy(); |
| 165 |
|
| 166 |
assert!( |
| 167 |
self.input_collection_changed_callback.is_none() |
| 168 |
&& self.output_collection_changed_callback.is_none() |
| 169 |
); |
| 170 |
|
| 171 |
if !self.mainloop.is_null() { |
| 172 |
self.mainloop.stop(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
fn subscribe_notifications(&mut self, mask: pulse::SubscriptionMask) -> Result<()> { |
| 177 |
fn update_collection( |
| 178 |
_: &pulse::Context, |
| 179 |
event: pulse::SubscriptionEvent, |
| 180 |
index: u32, |
| 181 |
user_data: *mut c_void, |
| 182 |
) { |
| 183 |
let ctx = unsafe { &mut *(user_data as *mut PulseContext) }; |
| 184 |
|
| 185 |
let (f, t) = (event.event_facility(), event.event_type()); |
| 186 |
if (f == pulse::SubscriptionEventFacility::Source) |
| 187 |
| (f == pulse::SubscriptionEventFacility::Sink) |
| 188 |
{ |
| 189 |
if (t == pulse::SubscriptionEventType::Remove) |
| 190 |
| (t == pulse::SubscriptionEventType::New) |
| 191 |
{ |
| 192 |
if log_enabled() { |
| 193 |
let op = if t == pulse::SubscriptionEventType::New { |
| 194 |
"Adding" |
| 195 |
} else { |
| 196 |
"Removing" |
| 197 |
}; |
| 198 |
let dev = if f == pulse::SubscriptionEventFacility::Sink { |
| 199 |
"sink" |
| 200 |
} else { |
| 201 |
"source " |
| 202 |
}; |
| 203 |
cubeb_log!("{} {} index {}", op, dev, index); |
| 204 |
} |
| 205 |
|
| 206 |
if f == pulse::SubscriptionEventFacility::Source { |
| 207 |
unsafe { |
| 208 |
ctx.input_collection_changed_callback.unwrap()( |
| 209 |
ctx as *mut _ as *mut _, |
| 210 |
ctx.input_collection_changed_user_ptr, |
| 211 |
); |
| 212 |
} |
| 213 |
} |
| 214 |
if f == pulse::SubscriptionEventFacility::Sink { |
| 215 |
unsafe { |
| 216 |
ctx.output_collection_changed_callback.unwrap()( |
| 217 |
ctx as *mut _ as *mut _, |
| 218 |
ctx.output_collection_changed_user_ptr, |
| 219 |
); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} else if (f == pulse::SubscriptionEventFacility::Server) |
| 224 |
&& (t == pulse::SubscriptionEventType::Change) |
| 225 |
{ |
| 226 |
cubeb_log!("Server changed {}", index as i32); |
| 227 |
let user_data: *mut c_void = ctx as *mut _ as *mut _; |
| 228 |
if let Some(ref context) = ctx.context { |
| 229 |
if let Err(e) = context.get_server_info(PulseContext::server_info_cb, user_data) |
| 230 |
{ |
| 231 |
cubeb_log!("Error: get_server_info ignored failure: {}", e); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
fn success(_: &pulse::Context, success: i32, user_data: *mut c_void) { |
| 238 |
let ctx = unsafe { &*(user_data as *mut PulseContext) }; |
| 239 |
if success != 1 { |
| 240 |
cubeb_log!("subscribe_success ignored failure: {}", success); |
| 241 |
} |
| 242 |
ctx.mainloop.signal(); |
| 243 |
} |
| 244 |
|
| 245 |
let user_data: *mut c_void = self as *const _ as *mut _; |
| 246 |
if let Some(ref context) = self.context { |
| 247 |
self.mainloop.lock(); |
| 248 |
|
| 249 |
context.set_subscribe_callback(update_collection, user_data); |
| 250 |
|
| 251 |
if let Ok(o) = context.subscribe(mask, success, self as *const _ as *mut _) { |
| 252 |
self.operation_wait(None, &o); |
| 253 |
} else { |
| 254 |
self.mainloop.unlock(); |
| 255 |
cubeb_log!("Error: context subscribe failed"); |
| 256 |
return Err(Error::error()); |
| 257 |
} |
| 258 |
|
| 259 |
self.mainloop.unlock(); |
| 260 |
} |
| 261 |
|
| 262 |
Ok(()) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
impl ContextOps for PulseContext { |
| 267 |
fn init(context_name: Option<&CStr>) -> Result<Context> { |
| 268 |
let ctx = PulseContext::new(context_name)?; |
| 269 |
Ok(unsafe { Context::from_ptr(Box::into_raw(ctx) as *mut _) }) |
| 270 |
} |
| 271 |
|
| 272 |
fn backend_id(&mut self) -> &'static CStr { |
| 273 |
unsafe { CStr::from_ptr(b"pulse-rust\0".as_ptr() as *const _) } |
| 274 |
} |
| 275 |
|
| 276 |
fn max_channel_count(&mut self) -> Result<u32> { |
| 277 |
match self.default_sink_info { |
| 278 |
Some(ref info) => Ok(u32::from(info.channel_map.channels)), |
| 279 |
None => { |
| 280 |
cubeb_log!("Error: couldn't get the max channel count"); |
| 281 |
Err(Error::error()) |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
fn min_latency(&mut self, params: StreamParams) -> Result<u32> { |
| 287 |
// According to PulseAudio developers, this is a safe minimum. |
| 288 |
Ok(25 * params.rate() / 1000) |
| 289 |
} |
| 290 |
|
| 291 |
fn preferred_sample_rate(&mut self) -> Result<u32> { |
| 292 |
match self.default_sink_info { |
| 293 |
Some(ref info) => Ok(info.sample_spec.rate), |
| 294 |
None => { |
| 295 |
cubeb_log!("Error: Couldn't get the preferred sample rate"); |
| 296 |
Err(Error::error()) |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
fn supported_input_processing_params(&mut self) -> Result<InputProcessingParams> { |
| 302 |
Ok(InputProcessingParams::NONE) |
| 303 |
} |
| 304 |
|
| 305 |
fn enumerate_devices( |
| 306 |
&mut self, |
| 307 |
devtype: DeviceType, |
| 308 |
collection: &DeviceCollectionRef, |
| 309 |
) -> Result<()> { |
| 310 |
fn add_output_device( |
| 311 |
_: &pulse::Context, |
| 312 |
i: *const pulse::SinkInfo, |
| 313 |
eol: i32, |
| 314 |
user_data: *mut c_void, |
| 315 |
) { |
| 316 |
let list_data = unsafe { &mut *(user_data as *mut PulseDevListData) }; |
| 317 |
let ctx = list_data.context; |
| 318 |
|
| 319 |
if eol != 0 { |
| 320 |
ctx.mainloop.signal(); |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
debug_assert!(!i.is_null()); |
| 325 |
debug_assert!(!user_data.is_null()); |
| 326 |
|
| 327 |
let info = unsafe { &*i }; |
| 328 |
|
| 329 |
let group_id = match info.proplist().gets("sysfs.path") { |
| 330 |
Some(p) => p.to_owned().into_raw(), |
| 331 |
_ => ptr::null_mut(), |
| 332 |
}; |
| 333 |
|
| 334 |
let vendor_name = match info.proplist().gets("device.vendor.name") { |
| 335 |
Some(p) => p.to_owned().into_raw(), |
| 336 |
_ => ptr::null_mut(), |
| 337 |
}; |
| 338 |
|
| 339 |
let info_name = unsafe { CStr::from_ptr(info.name) }; |
| 340 |
let info_description = unsafe { CStr::from_ptr(info.description) }.to_owned(); |
| 341 |
|
| 342 |
let preferred = if *info_name == *list_data.default_sink_name { |
| 343 |
ffi::CUBEB_DEVICE_PREF_ALL |
| 344 |
} else { |
| 345 |
ffi::CUBEB_DEVICE_PREF_NONE |
| 346 |
}; |
| 347 |
|
| 348 |
let device_id = ctx.devids.borrow_mut().add(info_name); |
| 349 |
let friendly_name = info_description.into_raw(); |
| 350 |
let devinfo = ffi::cubeb_device_info { |
| 351 |
device_id, |
| 352 |
devid: device_id as ffi::cubeb_devid, |
| 353 |
friendly_name, |
| 354 |
group_id, |
| 355 |
vendor_name, |
| 356 |
device_type: ffi::CUBEB_DEVICE_TYPE_OUTPUT, |
| 357 |
state: ctx.state_from_port(info.active_port), |
| 358 |
preferred, |
| 359 |
format: ffi::CUBEB_DEVICE_FMT_ALL, |
| 360 |
default_format: pulse_format_to_cubeb_format(info.sample_spec.format), |
| 361 |
max_channels: u32::from(info.channel_map.channels), |
| 362 |
min_rate: 1, |
| 363 |
max_rate: PA_RATE_MAX, |
| 364 |
default_rate: info.sample_spec.rate, |
| 365 |
latency_lo: 0, |
| 366 |
latency_hi: 0, |
| 367 |
}; |
| 368 |
list_data.devinfo.push(devinfo); |
| 369 |
} |
| 370 |
|
| 371 |
fn add_input_device( |
| 372 |
_: &pulse::Context, |
| 373 |
i: *const pulse::SourceInfo, |
| 374 |
eol: i32, |
| 375 |
user_data: *mut c_void, |
| 376 |
) { |
| 377 |
let list_data = unsafe { &mut *(user_data as *mut PulseDevListData) }; |
| 378 |
let ctx = list_data.context; |
| 379 |
|
| 380 |
if eol != 0 { |
| 381 |
ctx.mainloop.signal(); |
| 382 |
return; |
| 383 |
} |
| 384 |
|
| 385 |
debug_assert!(!user_data.is_null()); |
| 386 |
debug_assert!(!i.is_null()); |
| 387 |
|
| 388 |
let info = unsafe { &*i }; |
| 389 |
|
| 390 |
let group_id = match info.proplist().gets("sysfs.path") { |
| 391 |
Some(p) => p.to_owned().into_raw(), |
| 392 |
_ => ptr::null_mut(), |
| 393 |
}; |
| 394 |
|
| 395 |
let vendor_name = match info.proplist().gets("device.vendor.name") { |
| 396 |
Some(p) => p.to_owned().into_raw(), |
| 397 |
_ => ptr::null_mut(), |
| 398 |
}; |
| 399 |
|
| 400 |
let info_name = unsafe { CStr::from_ptr(info.name) }; |
| 401 |
let info_description = unsafe { CStr::from_ptr(info.description) }.to_owned(); |
| 402 |
|
| 403 |
let preferred = if *info_name == *list_data.default_source_name { |
| 404 |
ffi::CUBEB_DEVICE_PREF_ALL |
| 405 |
} else { |
| 406 |
ffi::CUBEB_DEVICE_PREF_NONE |
| 407 |
}; |
| 408 |
|
| 409 |
let device_id = ctx.devids.borrow_mut().add(info_name); |
| 410 |
let friendly_name = info_description.into_raw(); |
| 411 |
let devinfo = ffi::cubeb_device_info { |
| 412 |
device_id, |
| 413 |
devid: device_id as ffi::cubeb_devid, |
| 414 |
friendly_name, |
| 415 |
group_id, |
| 416 |
vendor_name, |
| 417 |
device_type: ffi::CUBEB_DEVICE_TYPE_INPUT, |
| 418 |
state: ctx.state_from_port(info.active_port), |
| 419 |
preferred, |
| 420 |
format: ffi::CUBEB_DEVICE_FMT_ALL, |
| 421 |
default_format: pulse_format_to_cubeb_format(info.sample_spec.format), |
| 422 |
max_channels: u32::from(info.channel_map.channels), |
| 423 |
min_rate: 1, |
| 424 |
max_rate: PA_RATE_MAX, |
| 425 |
default_rate: info.sample_spec.rate, |
| 426 |
latency_lo: 0, |
| 427 |
latency_hi: 0, |
| 428 |
}; |
| 429 |
|
| 430 |
list_data.devinfo.push(devinfo); |
| 431 |
} |
| 432 |
|
| 433 |
fn default_device_names( |
| 434 |
_: &pulse::Context, |
| 435 |
info: Option<&pulse::ServerInfo>, |
| 436 |
user_data: *mut c_void, |
| 437 |
) { |
| 438 |
let list_data = unsafe { &mut *(user_data as *mut PulseDevListData) }; |
| 439 |
|
| 440 |
if let Some(info) = info { |
| 441 |
list_data.default_sink_name = super::try_cstr_from(info.default_sink_name) |
| 442 |
.map(|s| s.to_owned()) |
| 443 |
.unwrap_or_default(); |
| 444 |
list_data.default_source_name = super::try_cstr_from(info.default_source_name) |
| 445 |
.map(|s| s.to_owned()) |
| 446 |
.unwrap_or_default(); |
| 447 |
} |
| 448 |
|
| 449 |
list_data.context.mainloop.signal(); |
| 450 |
} |
| 451 |
|
| 452 |
let mut user_data = PulseDevListData::new(self); |
| 453 |
|
| 454 |
if let Some(ref context) = self.context { |
| 455 |
self.mainloop.lock(); |
| 456 |
|
| 457 |
if let Ok(o) = |
| 458 |
context.get_server_info(default_device_names, &mut user_data as *mut _ as *mut _) |
| 459 |
{ |
| 460 |
self.operation_wait(None, &o); |
| 461 |
} |
| 462 |
|
| 463 |
if devtype.contains(DeviceType::OUTPUT) { |
| 464 |
if let Ok(o) = context |
| 465 |
.get_sink_info_list(add_output_device, &mut user_data as *mut _ as *mut _) |
| 466 |
{ |
| 467 |
self.operation_wait(None, &o); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
if devtype.contains(DeviceType::INPUT) { |
| 472 |
if let Ok(o) = context |
| 473 |
.get_source_info_list(add_input_device, &mut user_data as *mut _ as *mut _) |
| 474 |
{ |
| 475 |
self.operation_wait(None, &o); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
self.mainloop.unlock(); |
| 480 |
} |
| 481 |
|
| 482 |
// Extract the array of cubeb_device_info from |
| 483 |
// PulseDevListData and convert it into C representation. |
| 484 |
let mut tmp = Vec::new(); |
| 485 |
mem::swap(&mut user_data.devinfo, &mut tmp); |
| 486 |
let mut devices = tmp.into_boxed_slice(); |
| 487 |
let coll = unsafe { &mut *collection.as_ptr() }; |
| 488 |
coll.device = devices.as_mut_ptr(); |
| 489 |
coll.count = devices.len(); |
| 490 |
|
| 491 |
// Giving away the memory owned by devices. Don't free it! |
| 492 |
mem::forget(devices); |
| 493 |
Ok(()) |
| 494 |
} |
| 495 |
|
| 496 |
fn device_collection_destroy(&mut self, collection: &mut DeviceCollectionRef) -> Result<()> { |
| 497 |
debug_assert!(!collection.as_ptr().is_null()); |
| 498 |
unsafe { |
| 499 |
let coll = &mut *collection.as_ptr(); |
| 500 |
let mut devices = Vec::from_raw_parts(coll.device, coll.count, coll.count); |
| 501 |
for dev in &mut devices { |
| 502 |
if !dev.group_id.is_null() { |
| 503 |
let _ = CString::from_raw(dev.group_id as *mut _); |
| 504 |
} |
| 505 |
if !dev.vendor_name.is_null() { |
| 506 |
let _ = CString::from_raw(dev.vendor_name as *mut _); |
| 507 |
} |
| 508 |
if !dev.friendly_name.is_null() { |
| 509 |
let _ = CString::from_raw(dev.friendly_name as *mut _); |
| 510 |
} |
| 511 |
} |
| 512 |
coll.device = ptr::null_mut(); |
| 513 |
coll.count = 0; |
| 514 |
} |
| 515 |
Ok(()) |
| 516 |
} |
| 517 |
|
| 518 |
#[allow(clippy::too_many_arguments)] |
| 519 |
fn stream_init( |
| 520 |
&mut self, |
| 521 |
stream_name: Option<&CStr>, |
| 522 |
input_device: DeviceId, |
| 523 |
input_stream_params: Option<&StreamParamsRef>, |
| 524 |
output_device: DeviceId, |
| 525 |
output_stream_params: Option<&StreamParamsRef>, |
| 526 |
latency_frames: u32, |
| 527 |
data_callback: ffi::cubeb_data_callback, |
| 528 |
state_callback: ffi::cubeb_state_callback, |
| 529 |
user_ptr: *mut c_void, |
| 530 |
) -> Result<Stream> { |
| 531 |
if self.error { |
| 532 |
self.context_init()?; |
| 533 |
} |
| 534 |
|
| 535 |
let stm = PulseStream::new( |
| 536 |
self, |
| 537 |
stream_name, |
| 538 |
input_device, |
| 539 |
input_stream_params, |
| 540 |
output_device, |
| 541 |
output_stream_params, |
| 542 |
latency_frames, |
| 543 |
data_callback, |
| 544 |
state_callback, |
| 545 |
user_ptr, |
| 546 |
)?; |
| 547 |
Ok(unsafe { Stream::from_ptr(Box::into_raw(stm) as *mut _) }) |
| 548 |
} |
| 549 |
|
| 550 |
fn register_device_collection_changed( |
| 551 |
&mut self, |
| 552 |
devtype: DeviceType, |
| 553 |
cb: ffi::cubeb_device_collection_changed_callback, |
| 554 |
user_ptr: *mut c_void, |
| 555 |
) -> Result<()> { |
| 556 |
if devtype.contains(DeviceType::INPUT) { |
| 557 |
self.input_collection_changed_callback = cb; |
| 558 |
self.input_collection_changed_user_ptr = user_ptr; |
| 559 |
} |
| 560 |
if devtype.contains(DeviceType::OUTPUT) { |
| 561 |
self.output_collection_changed_callback = cb; |
| 562 |
self.output_collection_changed_user_ptr = user_ptr; |
| 563 |
} |
| 564 |
|
| 565 |
let mut mask = pulse::SubscriptionMask::empty(); |
| 566 |
if self.input_collection_changed_callback.is_some() { |
| 567 |
mask |= pulse::SubscriptionMask::SOURCE; |
| 568 |
} |
| 569 |
if self.output_collection_changed_callback.is_some() { |
| 570 |
mask |= pulse::SubscriptionMask::SINK; |
| 571 |
} |
| 572 |
/* Default device changed, this is always registered in order to update the |
| 573 |
* `default_sink_info` when the default device changes. */ |
| 574 |
mask |= pulse::SubscriptionMask::SERVER; |
| 575 |
|
| 576 |
self.subscribe_notifications(mask) |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
impl Drop for PulseContext { |
| 581 |
fn drop(&mut self) { |
| 582 |
self.destroy(); |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
impl PulseContext { |
| 587 |
/* Initialize PulseAudio Context */ |
| 588 |
fn context_init(&mut self) -> Result<()> { |
| 589 |
fn error_state(c: &pulse::Context, u: *mut c_void) { |
| 590 |
let ctx = unsafe { &mut *(u as *mut PulseContext) }; |
| 591 |
if !c.get_state().is_good() { |
| 592 |
ctx.error = true; |
| 593 |
} |
| 594 |
ctx.mainloop.signal(); |
| 595 |
} |
| 596 |
|
| 597 |
if self.context.is_some() { |
| 598 |
debug_assert!(self.error); |
| 599 |
self.context_destroy(); |
| 600 |
} |
| 601 |
|
| 602 |
self.context = { |
| 603 |
let name = self.context_name.as_ref().map(|s| s.as_ref()); |
| 604 |
pulse::Context::new(&self.mainloop.get_api(), name) |
| 605 |
}; |
| 606 |
|
| 607 |
let context_ptr: *mut c_void = self as *mut _ as *mut _; |
| 608 |
if self.context.is_none() { |
| 609 |
cubeb_log!("Error: couldn't create pulse's context"); |
| 610 |
return Err(Error::error()); |
| 611 |
} |
| 612 |
|
| 613 |
self.mainloop.lock(); |
| 614 |
let connected = if let Some(ref context) = self.context { |
| 615 |
context.set_state_callback(error_state, context_ptr); |
| 616 |
context |
| 617 |
.connect(None, pulse::ContextFlags::empty(), ptr::null()) |
| 618 |
.is_ok() |
| 619 |
} else { |
| 620 |
false |
| 621 |
}; |
| 622 |
|
| 623 |
if !connected || !self.wait_until_context_ready() { |
| 624 |
self.mainloop.unlock(); |
| 625 |
self.context_destroy(); |
| 626 |
cubeb_log!("Error: error while waiting for pulse's context to be ready"); |
| 627 |
return Err(Error::error()); |
| 628 |
} |
| 629 |
|
| 630 |
self.mainloop.unlock(); |
| 631 |
|
| 632 |
let version_str = unsafe { CStr::from_ptr(pulse::library_version()) }; |
| 633 |
if let Ok(version) = semver::Version::parse(&version_str.to_string_lossy()) { |
| 634 |
self.version_0_9_8 = |
| 635 |
version >= semver::Version::parse("0.9.8").expect("Failed to parse version"); |
| 636 |
self.version_2_0_0 = |
| 637 |
version >= semver::Version::parse("2.0.0").expect("Failed to parse version"); |
| 638 |
} |
| 639 |
|
| 640 |
self.error = false; |
| 641 |
|
| 642 |
Ok(()) |
| 643 |
} |
| 644 |
|
| 645 |
fn context_destroy(&mut self) { |
| 646 |
fn drain_complete(_: &pulse::Context, u: *mut c_void) { |
| 647 |
let ctx = unsafe { &*(u as *mut PulseContext) }; |
| 648 |
ctx.mainloop.signal(); |
| 649 |
} |
| 650 |
|
| 651 |
let context_ptr: *mut c_void = self as *mut _ as *mut _; |
| 652 |
if let Some(ctx) = self.context.take() { |
| 653 |
self.mainloop.lock(); |
| 654 |
if let Ok(o) = ctx.drain(drain_complete, context_ptr) { |
| 655 |
self.operation_wait(None, &o); |
| 656 |
} |
| 657 |
ctx.clear_state_callback(); |
| 658 |
ctx.disconnect(); |
| 659 |
ctx.unref(); |
| 660 |
self.mainloop.unlock(); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
pub fn operation_wait<'a, S>(&self, s: S, o: &pulse::Operation) -> bool |
| 665 |
where |
| 666 |
S: Into<Option<&'a pulse::Stream>>, |
| 667 |
{ |
| 668 |
let stream = s.into(); |
| 669 |
while o.get_state() == PA_OPERATION_RUNNING { |
| 670 |
self.mainloop.wait(); |
| 671 |
if let Some(ref context) = self.context { |
| 672 |
if !context.get_state().is_good() { |
| 673 |
return false; |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
if let Some(stm) = stream { |
| 678 |
if !stm.get_state().is_good() { |
| 679 |
return false; |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
true |
| 685 |
} |
| 686 |
|
| 687 |
pub fn wait_until_context_ready(&self) -> bool { |
| 688 |
if let Some(ref context) = self.context { |
| 689 |
loop { |
| 690 |
let state = context.get_state(); |
| 691 |
if !state.is_good() { |
| 692 |
return false; |
| 693 |
} |
| 694 |
if state == pulse::ContextState::Ready { |
| 695 |
break; |
| 696 |
} |
| 697 |
self.mainloop.wait(); |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
true |
| 702 |
} |
| 703 |
|
| 704 |
fn state_from_port(&self, i: *const pa_port_info) -> ffi::cubeb_device_state { |
| 705 |
if !i.is_null() { |
| 706 |
let info = unsafe { *i }; |
| 707 |
if self.version_2_0_0 && info.available == PA_PORT_AVAILABLE_NO { |
| 708 |
ffi::CUBEB_DEVICE_STATE_UNPLUGGED |
| 709 |
} else { |
| 710 |
ffi::CUBEB_DEVICE_STATE_ENABLED |
| 711 |
} |
| 712 |
} else { |
| 713 |
ffi::CUBEB_DEVICE_STATE_ENABLED |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
struct PulseDevListData<'a> { |
| 719 |
default_sink_name: CString, |
| 720 |
default_source_name: CString, |
| 721 |
devinfo: Vec<ffi::cubeb_device_info>, |
| 722 |
context: &'a PulseContext, |
| 723 |
} |
| 724 |
|
| 725 |
impl<'a> PulseDevListData<'a> { |
| 726 |
pub fn new<'b>(context: &'b PulseContext) -> Self |
| 727 |
where |
| 728 |
'b: 'a, |
| 729 |
{ |
| 730 |
PulseDevListData { |
| 731 |
default_sink_name: CString::default(), |
| 732 |
default_source_name: CString::default(), |
| 733 |
devinfo: Vec::new(), |
| 734 |
context, |
| 735 |
} |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
impl<'a> Drop for PulseDevListData<'a> { |
| 740 |
fn drop(&mut self) { |
| 741 |
for elem in &mut self.devinfo { |
| 742 |
let _ = unsafe { Box::from_raw(elem) }; |
| 743 |
} |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
fn pulse_format_to_cubeb_format(format: pa_sample_format_t) -> ffi::cubeb_device_fmt { |
| 748 |
match format { |
| 749 |
PA_SAMPLE_S16LE => ffi::CUBEB_DEVICE_FMT_S16LE, |
| 750 |
PA_SAMPLE_S16BE => ffi::CUBEB_DEVICE_FMT_S16BE, |
| 751 |
PA_SAMPLE_FLOAT32LE => ffi::CUBEB_DEVICE_FMT_F32LE, |
| 752 |
PA_SAMPLE_FLOAT32BE => ffi::CUBEB_DEVICE_FMT_F32BE, |
| 753 |
// Unsupported format, return F32NE |
| 754 |
_ => ffi::CUBEB_DEVICE_FMT_F32NE, |
| 755 |
} |
| 756 |
} |