Skip to content

Instantly share code, notes, and snippets.

@djg
Created March 2, 2018 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djg/c88ac72b549660ccbaa83e226f96dfdf to your computer and use it in GitHub Desktop.
Save djg/c88ac72b549660ccbaa83e226f96dfdf to your computer and use it in GitHub Desktop.
Pulse Audio Callbacks
type ServerInfoCB = FnMut(&ContextRef, Option<&ServerInfoRef>) + Send + Sync + 'static;
pub fn get_server_info<CB>(&self, cb: CB) -> Result<Operation>
where
CB: FnMut(&ContextRef, Option<&SourceInfo>) + Send + Sync + 'static,
{
let userdata = Box::into_raw(Box::new(cb)) as *mut c_void;
let cb: ffi::pa_server_info_cb_t = Some(c_get_server_info_cb);
op_or_err!(
self,
ffi::pa_context_get_server_info(self.as_ptr(), cb, userdata)
)
}
unsafe extern "C" fn c_get_server_info_cb(
c: *mut ffi::pa_context,
i: *const ffi::pa_server_info,
userdata: *mut c_void,
) {
if panic::catch_unwind(|| {
let ctx = ContextRef::from_ptr(c);
let info = if i.is_null() {
None
} else {
Some(ServerInfoRef::from_ptr(i as *mut _))
};
let cb = Box::from_raw(userdata as *mut ServerInfoCB);
cb(ctx, info);
}).is_err()
{
process::abort()
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment