1use crate::Error;
5use std::{
6 ffi::{CStr, CString},
7 io,
8 os::unix::prelude::OsStrExt,
9 path::{Path, PathBuf},
10};
11use videostream_sys as ffi;
12
13pub struct Host {
20 ptr: *mut ffi::VSLHost,
21}
22
23impl Host {
24 pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
27 let path_str_c = CString::new(path.as_ref().as_os_str().as_bytes())?;
28 let ptr = vsl!(vsl_host_init(path_str_c.as_ptr()));
29 if ptr.is_null() {
30 let err = io::Error::last_os_error();
31 return Err(err.into());
32 }
33
34 Ok(Host { ptr })
35 }
36
37 pub fn path(&self) -> Result<PathBuf, Error> {
38 let path_str_c = vsl!(vsl_host_path(self.ptr));
39 if path_str_c.is_null() {
40 return Err(Error::NullPointer);
41 }
42
43 let path_str = unsafe { CStr::from_ptr(path_str_c).to_str()? };
44 Ok(PathBuf::from(path_str))
45 }
46
47 pub fn poll(&self) {}
48
49 pub fn process(&self) {}
50
51 pub fn sockets(&self) {}
52}
53
54impl Drop for Host {
55 fn drop(&mut self) {
56 if let Ok(lib) = ffi::init() {
57 unsafe {
58 lib.vsl_host_release(self.ptr);
59 }
60 }
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67 use std::path::PathBuf;
68
69 #[test]
70 fn test_host() {
71 let path = PathBuf::from("/tmp/test.vsl");
72 let host = Host::new(&path).unwrap();
73 assert_eq!(path, host.path().unwrap());
74 assert!(path.exists());
75 assert!(!path.is_file());
78 assert!(!path.is_dir());
79 assert!(!path.is_symlink());
80
81 }
89}