use std::ptr::NonNull;
use crate::{Result, cstring, status_to_result, sys};
#[derive(Copy, Clone)]
pub struct Session {
raw: NonNull<sys::switch_core_session_t>,
}
impl Session {
pub unsafe fn from_raw(raw: *mut sys::switch_core_session_t) -> Option<Self> {
NonNull::new(raw).map(|raw| Self { raw })
}
#[inline]
pub fn as_ptr(self) -> *mut sys::switch_core_session_t {
self.raw.as_ptr()
}
pub fn answer(self) -> Result<()> {
let channel = unsafe { sys::switch_core_session_get_channel(self.raw.as_ptr()) };
let Some(channel) = NonNull::new(channel) else {
return Ok(());
};
let status = unsafe {
sys::switch_channel_perform_answer(
channel.as_ptr(),
c"fswtch-rs".as_ptr(),
c"Session::answer".as_ptr(),
line!() as _,
)
};
status_to_result(status)
}
pub fn sleep_ms(self, milliseconds: u32) -> Result<()> {
let status = unsafe {
sys::switch_ivr_sleep(
self.raw.as_ptr(),
milliseconds,
sys::switch_bool_t_SWITCH_FALSE,
std::ptr::null_mut(),
)
};
status_to_result(status)
}
pub fn play_file(self, path: impl AsRef<str>) -> Result<()> {
let path = cstring(path)?;
let status = unsafe {
sys::switch_ivr_play_file(
self.raw.as_ptr(),
std::ptr::null_mut(),
path.as_ptr(),
std::ptr::null_mut(),
)
};
status_to_result(status)
}
}