From d379d70a1cccd37659c1406543c62ad04525f36d Mon Sep 17 00:00:00 2001 From: Raniz Date: Mon, 25 Nov 2019 11:34:57 +0100 Subject: [PATCH] VLM as a separate module * Refactored VLM methods into a VLM trait in it's own module * Added more VLM methods --- src/core.rs | 69 ------------------------- src/lib.rs | 2 + src/vlm.rs | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 69 deletions(-) create mode 100644 src/vlm.rs diff --git a/src/core.rs b/src/core.rs index f1b21f0..2ef6c7c 100644 --- a/src/core.rs +++ b/src/core.rs @@ -81,75 +81,6 @@ impl Instance { else { Err(()) } } - pub fn add_broadcast(&self, name: &str, input: &str, output: &str, options: Option>, enabled: bool, loop_broadcast: bool,) -> Result<(), ()> { - let name= to_cstr(name); - let input = to_cstr(input); - let output = to_cstr(output); - let opts_c_ptr: Vec<*const c_char> ; - let opts_c: Vec; - let enabled = if enabled { 1 } else { 0 }; - let loop_broadcast = if loop_broadcast { 1 } else { 0 }; - if let Some(vec) = options { - opts_c = vec.into_iter() - .map(|x| CString::new(x).expect("Error: Unexpected null byte")).collect(); - opts_c_ptr = opts_c.iter().map(|x| x.as_ptr()).collect(); - } else { - opts_c_ptr = Vec::new(); - } - let result = unsafe { - if opts_c_ptr.is_empty() { - sys::libvlc_vlm_add_broadcast(self.ptr, name.as_ptr(), input.as_ptr(), output.as_ptr(), 0, ptr::null(), enabled, loop_broadcast) - } else { - sys::libvlc_vlm_add_broadcast(self.ptr, name.as_ptr(), input.as_ptr(), output.as_ptr(), opts_c_ptr.len() as i32, opts_c_ptr.as_ptr(), enabled, loop_broadcast) - } - }; - if result == 0 { Ok(()) } - else { Err(()) } - } - - pub fn add_vod(&self, name: &str, input: &str, mux: &str, options: Option>, enabled: bool) -> Result<(), ()> { - let name= to_cstr(name); - let input = to_cstr(input); - let mux = to_cstr(mux); - let opts_c_ptr: Vec<*const c_char> ; - let opts_c: Vec; - let enabled = if enabled { 1 } else { 0 }; - if let Some(vec) = options { - opts_c = vec.into_iter() - .map(|x| CString::new(x).expect("Error: Unexpected null byte")).collect(); - opts_c_ptr = opts_c.iter().map(|x| x.as_ptr()).collect(); - } else { - opts_c_ptr = Vec::new(); - } - let result = unsafe { - if opts_c_ptr.is_empty() { - sys::libvlc_vlm_add_vod(self.ptr, name.as_ptr(), input.as_ptr(), 0, ptr::null(), enabled, mux.as_ptr()) - } else { - sys::libvlc_vlm_add_vod(self.ptr, name.as_ptr(), input.as_ptr(), opts_c_ptr.len() as i32, opts_c_ptr.as_ptr(), enabled, mux.as_ptr()) - } - }; - if result == 0 { Ok(()) } - else { Err(()) } - } - - pub fn play_media(&self, name: &str) -> Result<(), ()> { - let name = to_cstr(name); - let result = unsafe { - sys::libvlc_vlm_play_media(self.ptr, name.as_ptr()) - }; - if result == 0 { Ok(()) } - else { Err(()) } - } - - pub fn stop_media(&self, name: &str) -> Result<(), ()> { - let name = to_cstr(name); - let result = unsafe { - sys::libvlc_vlm_stop_media(self.ptr, name.as_ptr()) - }; - if result == 0 { Ok(()) } - else { Err(()) } - } - /// Sets the application name. /// LibVLC passes this as the user agent string when a protocol requires it. pub fn set_user_agent(&self, name: &str, http: &str) { diff --git a/src/lib.rs b/src/lib.rs index 6825be0..4bf0ad4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,7 @@ mod media_library; mod enums; mod video; mod audio; +mod vlm; pub use enums::*; pub use core::*; @@ -24,3 +25,4 @@ pub use media_list::*; pub use media_library::*; pub use video::*; pub use audio::*; +pub use vlm::*; diff --git a/src/vlm.rs b/src/vlm.rs new file mode 100644 index 0000000..09b31e7 --- /dev/null +++ b/src/vlm.rs @@ -0,0 +1,146 @@ +use ::{sys, Instance}; +use std::ffi::CString; +use tools::{to_cstr, from_cstr}; +use std::os::raw::c_char; +use std::ptr; + +pub trait Vlm { + fn add_broadcast(&self, name: &str, input: &str, output: &str, options: Option>, enabled: bool, loop_broadcast: bool, ) -> Result<(), ()>; + + fn add_vod(&self, name: &str, input: &str, mux: &str, options: Option>, enabled: bool) -> Result<(), ()>; + + fn play_media(&self, name: &str) -> Result<(), ()>; + + fn pause_media(&self, name: &str) -> Result<(), ()>; + + fn stop_media(&self, name: &str) -> Result<(), ()>; + + fn get_media_instance_position(&self, name: &str, instance: i32) -> Result; + + fn get_media_instance_length(&self, name: &str, instance: i32) -> Result; + + fn get_media_instance_time(&self, name: &str, instance: i32) -> Result; + + fn get_media_instance_rate(&self, name: &str, instance: i32) -> Result; + + fn show_media(&self, name: &str) -> Result; +} + +impl Vlm for Instance { + fn add_broadcast(&self, name: &str, input: &str, output: &str, options: Option>, enabled: bool, loop_broadcast: bool, ) -> Result<(), ()> { + let name = to_cstr(name); + let input = to_cstr(input); + let output = to_cstr(output); + let opts_c_ptr: Vec<*const c_char>; + let opts_c: Vec; + let enabled = if enabled { 1 } else { 0 }; + let loop_broadcast = if loop_broadcast { 1 } else { 0 }; + if let Some(vec) = options { + opts_c = vec.into_iter() + .map(|x| CString::new(x).expect("Error: Unexpected null byte")).collect(); + opts_c_ptr = opts_c.iter().map(|x| x.as_ptr()).collect(); + } else { + opts_c_ptr = Vec::new(); + } + let result = unsafe { + if opts_c_ptr.is_empty() { + sys::libvlc_vlm_add_broadcast(self.ptr, name.as_ptr(), input.as_ptr(), output.as_ptr(), 0, ptr::null(), enabled, loop_broadcast) + } else { + sys::libvlc_vlm_add_broadcast(self.ptr, name.as_ptr(), input.as_ptr(), output.as_ptr(), opts_c_ptr.len() as i32, opts_c_ptr.as_ptr(), enabled, loop_broadcast) + } + }; + if result == 0 { Ok(()) } else { Err(()) } + } + + fn add_vod(&self, name: &str, input: &str, mux: &str, options: Option>, enabled: bool) -> Result<(), ()> { + let name = to_cstr(name); + let input = to_cstr(input); + let mux = to_cstr(mux); + let opts_c_ptr: Vec<*const c_char>; + let opts_c: Vec; + let enabled = if enabled { 1 } else { 0 }; + if let Some(vec) = options { + opts_c = vec.into_iter() + .map(|x| CString::new(x).expect("Error: Unexpected null byte")).collect(); + opts_c_ptr = opts_c.iter().map(|x| x.as_ptr()).collect(); + } else { + opts_c_ptr = Vec::new(); + } + let result = unsafe { + if opts_c_ptr.is_empty() { + sys::libvlc_vlm_add_vod(self.ptr, name.as_ptr(), input.as_ptr(), 0, ptr::null(), enabled, mux.as_ptr()) + } else { + sys::libvlc_vlm_add_vod(self.ptr, name.as_ptr(), input.as_ptr(), opts_c_ptr.len() as i32, opts_c_ptr.as_ptr(), enabled, mux.as_ptr()) + } + }; + if result == 0 { Ok(()) } else { Err(()) } + } + + fn play_media(&self, name: &str) -> Result<(), ()> { + let name = to_cstr(name); + let result = unsafe { + sys::libvlc_vlm_play_media(self.ptr, name.as_ptr()) + }; + if result == 0 { Ok(()) } else { Err(()) } + } + + fn pause_media(&self, name: &str) -> Result<(), ()> { + let name = to_cstr(name); + let result = unsafe { + sys::libvlc_vlm_pause_media(self.ptr, name.as_ptr()) + }; + if result == 0 { Ok(()) } else { Err(()) } + } + + fn stop_media(&self, name: &str) -> Result<(), ()> { + let name = to_cstr(name); + let result = unsafe { + sys::libvlc_vlm_stop_media(self.ptr, name.as_ptr()) + }; + if result == 0 { Ok(()) } else { Err(()) } + } + + fn get_media_instance_position(&self, name: &str, instance: i32) -> Result { + let name = to_cstr(name); + let result = unsafe { + sys::libvlc_vlm_get_media_instance_position(self.ptr, name.as_ptr(), instance) + }; + if result != -1f32 { Ok(result) } else { Err(()) } + } + + fn get_media_instance_length(&self, name: &str, instance: i32) -> Result { + let name = to_cstr(name); + let result = unsafe { + sys::libvlc_vlm_get_media_instance_length(self.ptr, name.as_ptr(), instance) + }; + if result != -1 { Ok(result) } else { Err(()) } + } + + fn get_media_instance_time(&self, name: &str, instance: i32) -> Result { + let name = to_cstr(name); + let result = unsafe { + sys::libvlc_vlm_get_media_instance_time(self.ptr, name.as_ptr(), instance) + }; + if result != -1 { Ok(result) } else { Err(()) } + } + + fn get_media_instance_rate(&self, name: &str, instance: i32) -> Result { + let name = to_cstr(name); + let result = unsafe { + sys::libvlc_vlm_get_media_instance_rate(self.ptr, name.as_ptr(), instance) + }; + if result != -1 { Ok(result) } else { Err(()) } + } + + fn show_media(&self, name: &str) -> Result { + let name = to_cstr(name); + let result = unsafe { + from_cstr(sys::libvlc_vlm_show_media(self.ptr, name.as_ptr())) + }; + if let Some(data) = result { + Ok(data.to_string()) + } else { + Err(()) + } + } +}