use std;
use capi;
use std::os::raw::{c_char, c_void};
use std::ffi::CString;
use std::ptr::{null, null_mut};
use super::{ContextInternal, Context};
pub use capi::pa_ext_stream_restore_info as InfoInternal;
#[repr(C)]
pub struct Info {
pub name: *const c_char,
pub channel_map: ::channelmap::Map,
pub volume: ::volume::CVolume,
pub device: *const c_char,
pub mute: i32,
}
pub struct StreamRestore {
pub context: *mut ContextInternal,
weak: bool,
}
impl Context {
pub fn stream_restore(&self) -> StreamRestore {
unsafe { capi::pa_context_ref(self.ptr) };
StreamRestore::from_raw(self.ptr)
}
}
pub type TestCb = extern "C" fn(c: *mut ContextInternal, version: u32,
userdata: *mut c_void);
pub type ReadCb = extern "C" fn(c: *mut ContextInternal,
info: *const InfoInternal, eol: i32, userdata: *mut c_void);
pub type SubscribeCb = extern "C" fn(c: *mut ContextInternal,
userdata: *mut c_void);
impl StreamRestore {
pub fn from_raw(context: *mut ContextInternal) -> Self {
Self { context: context, weak: false }
}
pub fn from_raw_weak(context: *mut ContextInternal) -> Self {
Self { context: context, weak: true }
}
pub fn test(&self, cb: (TestCb, *mut c_void)) -> Option<::operation::Operation> {
let ptr = unsafe { capi::pa_ext_stream_restore_test(self.context, Some(cb.0), cb.1) };
if ptr.is_null() {
return None;
}
Some(::operation::Operation::from_raw(ptr))
}
pub fn read(&self, cb: (ReadCb, *mut c_void)) -> Option<::operation::Operation> {
let ptr = unsafe { capi::pa_ext_stream_restore_read(self.context, Some(cb.0), cb.1) };
if ptr.is_null() {
return None;
}
Some(::operation::Operation::from_raw(ptr))
}
pub fn write(&self, mode: ::proplist::UpdateMode, data: &[&Info], apply_immediately: bool,
cb: (::context::ContextSuccessCb, *mut c_void)) -> Option<::operation::Operation>
{
let ptr = unsafe {
capi::pa_ext_stream_restore_write(self.context, mode,
std::mem::transmute(data.as_ptr()), data.len() as u32, apply_immediately as i32,
Some(cb.0), cb.1)
};
if ptr.is_null() {
return None;
}
Some(::operation::Operation::from_raw(ptr))
}
pub fn delete(&self, streams: &[&str], cb: (::context::ContextSuccessCb, *mut c_void)
) -> Option<::operation::Operation>
{
let mut c_streams: Vec<CString> = Vec::with_capacity(streams.len());
for stream in streams {
c_streams.push(CString::new(stream.clone()).unwrap());
}
let mut c_stream_ptrs: Vec<*const c_char> = Vec::with_capacity(c_streams.len() + 1);
for c_stream in c_streams {
c_stream_ptrs.push(c_stream.as_ptr());
}
c_stream_ptrs.push(null());
let ptr = unsafe { capi::pa_ext_stream_restore_delete(self.context, c_stream_ptrs.as_ptr(),
Some(cb.0), cb.1) };
if ptr.is_null() {
return None;
}
Some(::operation::Operation::from_raw(ptr))
}
pub fn subscribe(&self, enable: bool, cb: (::context::ContextSuccessCb, *mut c_void)
) -> Option<::operation::Operation>
{
let ptr = unsafe { capi::pa_ext_stream_restore_subscribe(self.context, enable as i32,
Some(cb.0), cb.1) };
if ptr.is_null() {
return None;
}
Some(::operation::Operation::from_raw(ptr))
}
pub fn set_subscribe_cb(&self, cb: (SubscribeCb, *mut c_void)) {
unsafe { capi::pa_ext_stream_restore_set_subscribe_cb(self.context, Some(cb.0), cb.1); }
}
}
impl Drop for StreamRestore {
fn drop(&mut self) {
if !self.weak {
unsafe { capi::pa_context_unref(self.context) };
}
self.context = null_mut::<ContextInternal>();
}
}