<div dir="ltr"><div dir="ltr">On Tue, Sep 15, 2020 at 1:54 PM Alexandre Janniaux <<a href="mailto:ajanni@videolabs.io">ajanni@videolabs.io</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
I have a few additional points :)<br>
<br>
On Sat, Sep 12, 2020 at 01:03:45AM +0530, Kartik Ohri wrote:<br>
> The API is written in two layers. vlccore-sys<br>
> crate contains one to one bindings of libvlc<br>
> functions. vlccore-rs crate wraps vlccore-sys and<br>
> exposes a Rust idiomatic API for writing modules<br>
> in rust. As of now, the API exposes only a subset<br>
> of the vlc_stream API. More functionality can be<br>
> added to Rust API as required in the future.<br>
> ---<br>
>  .gitignore                    |   2 +-<br>
>  <a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a>                  |  13 ++++<br>
>  src/vlccore-rs/Cargo.toml     |  10 +++<br>
>  src/vlccore-rs/src/<a href="http://lib.rs" rel="noreferrer" target="_blank">lib.rs</a>     |   1 +<br>
>  src/vlccore-rs/src/<a href="http://stream.rs" rel="noreferrer" target="_blank">stream.rs</a>  | 120 ++++++++++++++++++++++++++++++++++<br>
>  src/vlccore-sys/Cargo.toml    |  10 +++<br>
>  src/vlccore-sys/src/<a href="http://lib.rs" rel="noreferrer" target="_blank">lib.rs</a>    |   2 +<br>
>  src/vlccore-sys/src/<a href="http://stream.rs" rel="noreferrer" target="_blank">stream.rs</a> |  24 +++++++<br>
>  8 files changed, 181 insertions(+), 1 deletion(-)<br>
>  create mode 100644 src/vlccore-rs/Cargo.toml<br>
>  create mode 100644 src/vlccore-rs/src/<a href="http://lib.rs" rel="noreferrer" target="_blank">lib.rs</a><br>
>  create mode 100644 src/vlccore-rs/src/<a href="http://stream.rs" rel="noreferrer" target="_blank">stream.rs</a><br>
>  create mode 100644 src/vlccore-sys/Cargo.toml<br>
>  create mode 100644 src/vlccore-sys/src/<a href="http://lib.rs" rel="noreferrer" target="_blank">lib.rs</a><br>
>  create mode 100644 src/vlccore-sys/src/<a href="http://stream.rs" rel="noreferrer" target="_blank">stream.rs</a><br>
><br>
> diff --git a/.gitignore b/.gitignore<br>
> index fc368212c8..6c049a80b1 100644<br>
> --- a/.gitignore<br>
> +++ b/.gitignore<br>
> @@ -46,7 +46,7 @@ wxvlc<br>
>  vlc_install_dir/*<br>
>  plugins.dat<br>
>  patches/*<br>
> -<br>
> +**/Cargo.lock<br>
>  include/vlc/libvlc_version.h<br>
><br>
>  # Ignore build dirs<br>
> diff --git a/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a> b/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
> index 99e4669942..53d56a725c 100644<br>
> --- a/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
> +++ b/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
> @@ -1896,6 +1896,19 @@ AS_IF([test "${enable_sout}" != "no"], [<br>
>  ])<br>
>  AM_CONDITIONAL([ENABLE_SOUT], [test "${enable_sout}" != "no"])<br>
><br>
> +dnl Rust Modules<br>
> +AC_ARG_ENABLE([rust],<br>
> +    AS_HELP_STRING([--enable-rust], [enable building Rust modules (default disabled)]))<br>
> +AS_IF([test "${enable_rust}" = "yes"],<br>
> +      [AC_DEFINE(ENABLE_RUST, 1, [Define to 1 for building rust modules.])])<br>
> +AM_CONDITIONAL([BUILD_RUST], [test "${enable_rust}" = "yes"])<br>
> +if test "${enable_rust}" = "yes"<br>
> +then<br>
> +    AC_CHECK_PROG(CARGO, [cargo], [yes], [no])<br>
> +    AS_IF([test "x$CARGO" = "xno"],<br>
> +          AC_MSG_ERROR([cargo not found. cargo is required to build rust modules]))<br>
> +fi<br>
> +<br>
>  dnl Lua modules<br>
>  AC_ARG_ENABLE([lua],<br>
>    AS_HELP_STRING([--disable-lua],<br>
> diff --git a/src/vlccore-rs/Cargo.toml b/src/vlccore-rs/Cargo.toml<br>
> new file mode 100644<br>
> index 0000000000..108c849f80<br>
> --- /dev/null<br>
> +++ b/src/vlccore-rs/Cargo.toml<br>
> @@ -0,0 +1,10 @@<br>
> +[package]<br>
> +name = "vlccore-rs"<br>
> +version = "0.1.0"<br>
> +authors = ["Kartik Ohri <<a href="mailto:kartikohri13@gmail.com" target="_blank">kartikohri13@gmail.com</a>>"]<br>
> +edition = "2018"<br>
> +license = "LGPL-2.1-or-later"<br>
> +<br>
> +[dependencies]<br>
> +libc = "0.2"<br>
> +vlccore-sys = {path="../vlccore-sys"}<br>
> \ No newline at end of file<br>
> diff --git a/src/vlccore-rs/src/<a href="http://lib.rs" rel="noreferrer" target="_blank">lib.rs</a> b/src/vlccore-rs/src/<a href="http://lib.rs" rel="noreferrer" target="_blank">lib.rs</a><br>
> new file mode 100644<br>
> index 0000000000..baf29e06ad<br>
> --- /dev/null<br>
> +++ b/src/vlccore-rs/src/<a href="http://lib.rs" rel="noreferrer" target="_blank">lib.rs</a><br>
> @@ -0,0 +1 @@<br>
> +pub mod stream;<br>
> diff --git a/src/vlccore-rs/src/<a href="http://stream.rs" rel="noreferrer" target="_blank">stream.rs</a> b/src/vlccore-rs/src/<a href="http://stream.rs" rel="noreferrer" target="_blank">stream.rs</a><br>
> new file mode 100644<br>
> index 0000000000..0725a8fe6e<br>
> --- /dev/null<br>
> +++ b/src/vlccore-rs/src/<a href="http://stream.rs" rel="noreferrer" target="_blank">stream.rs</a><br>
> @@ -0,0 +1,120 @@<br>
> +use libc::{c_char, c_void};<br>
> +use std::ffi::{CStr, CString};<br>
> +use std::io::{Error, ErrorKind, Read, Result, Seek, SeekFrom};<br>
> +use std::ptr::null_mut;<br>
> +use vlccore_sys::stream as ffi;<br>
> +<br>
> +pub struct Stream {<br>
> +    stream: *mut ffi::stream_t,<br>
> +}<br>
> +<br>
> +impl Read for Stream {<br>
> +    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {<br>
> +        let bytes_read: isize = unsafe {<br>
> +            ffi::vlc_stream_Read(self.stream, buf.as_mut_ptr() as *mut c_void, buf.len())<br>
> +        };<br>
> +        if bytes_read < 0 {<br>
> +            Err(Error::new(<br>
> +                ErrorKind::Other,<br>
> +                "Error while reading from stream",<br>
> +            ))<br>
> +        } else {<br>
> +            Ok(bytes_read as usize)<br>
> +        }<br>
> +    }<br>
> +}<br>
> +<br>
> +impl Seek for Stream {<br>
> +    fn seek(&mut self, pos: SeekFrom) -> Result<u64> {<br>
> +        let offset = match pos {<br>
> +            SeekFrom::Start(n) => n,<br>
> +            SeekFrom::Current(n) => {<br>
> +                let len = self.position();<br>
> +                if n >= 0 {<br>
> +                    len.checked_add(n as u64)<br>
> +                } else {<br>
> +                    len.checked_sub(n.wrapping_neg() as u64)<br>
> +                }<br>
> +                .ok_or(Error::new(ErrorKind::Other, "Position Overflow"))?<br>
> +            }<br>
> +            SeekFrom::End(n) => self.len().and_then(|position| {<br>
> +                if n >= 0 {<br>
> +                    position.checked_add(n as u64)<br>
> +                } else {<br>
> +                    position.checked_sub(n.wrapping_neg() as u64)<br>
> +                }<br>
> +                .ok_or(Error::new(ErrorKind::Other, "Position Overflow"))<br>
> +            })?,<br>
> +        };<br>
> +<br>
> +        let result = unsafe { ffi::vlc_stream_Seek(self.stream, offset) };<br>
> +        if result != 0 {<br>
> +            return Err(Error::new(ErrorKind::Other, "Could not seek stream"));<br>
> +        }<br>
> +        Ok(offset)<br>
> +    }<br>
> +}<br>
> +<br>
> +impl Drop for Stream {<br>
> +    fn drop(&mut self) {<br>
> +        unsafe { ffi::vlc_stream_Delete(self.stream) }<br>
> +    }<br>
> +}<br>
> +<br>
> +impl From<*mut ffi::stream_t> for Stream {<br>
> +    fn from(stream: *mut ffi::stream_t) -> Self {<br>
> +        Stream { stream }<br>
> +    }<br>
> +}<br>
> +<br>
> +impl Stream {<br>
> +    pub fn len(&self) -> std::result::Result<u64, Error> {<br>
> +        let size: u64 = 0;<br>
> +        const STREAM_GET_SIZE: i32 = 6;<br>
> +        let result = unsafe { ffi::vlc_stream_vaControl(self.stream, STREAM_GET_SIZE, &size) };<br>
> +        if result != 0 {<br>
> +            return Err(Error::new(<br>
> +                ErrorKind::Other,<br>
> +                "Could not determine stream size",<br>
> +            ));<br>
> +        }<br>
> +        Ok(size)<br>
> +    }<br>
> +<br>
> +    pub fn position(&self) -> u64 {<br>
> +        unsafe { ffi::vlc_stream_Tell(self.stream) }<br>
> +    }<br>
> +<br>
> +    pub fn eof(&self) -> Result<bool> {<br>
> +        Ok(unsafe { ffi::vlc_stream_Eof(self.stream) })<br>
> +    }<br>
> +<br>
> +    pub fn mimetype(&self) -> Option<String> {<br>
> +        self.contenttype().map(|content_type| {<br>
> +            let index = content_type.chars().position(|c| c == ';').unwrap();<br>
> +            let mime_type: String = content_type[..index].to_owned();<br>
> +            mime_type<br>
> +        })<br>
<br>
Since you don't chain map, you can probably just use early return here.<br>
<br>
    pub fn mimetype(&self) -> Option<String> {<br>
        let contenttype = self.contenttype()?;<br>
        let index = content_type.chars().position(|c| c == ';')?;<br>
        Some(content_type[..index].to_owned())<br>
    }<br>
<br></blockquote><div>Will fix! </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> +    }<br>
> +<br>
> +    pub fn contenttype(&self) -> Option<String> {<br>
> +        // FIXME: STREAM_GET_CONTENT_TYPE does not have a fixed value<br>
> +        const STREAM_GET_CONTENT_TYPE: i32 = 0;<br>
> +        let result: *mut c_char = null_mut();<br>
> +        unsafe {<br>
> +            if ffi::vlc_stream_vaControl(self.stream, STREAM_GET_CONTENT_TYPE, result) != 0 {<br>
> +                return None;<br>
> +            } else if result.is_null() {<br>
> +                return None;<br>
> +            }<br>
> +            // FIXME: Free the C pointer received but retain copy of data in Rust<br>
> +            CStr::from_ptr(result).to_str().map(|s| s.to_string()).ok()<br>
> +        }<br>
> +    }<br>
> +<br>
> +    pub fn new_with_url(&mut self, url: &str) -> Self {<br>
> +        let c_url = CString::new(url).unwrap().as_ptr();<br>
> +        let stream = unsafe { ffi::vlc_stream_NewURL(self.stream, c_url) };<br>
> +        Stream { stream }<br>
> +    }<br>
> +}<br>
> diff --git a/src/vlccore-sys/Cargo.toml b/src/vlccore-sys/Cargo.toml<br>
> new file mode 100644<br>
> index 0000000000..0dde7b2e7d<br>
> --- /dev/null<br>
> +++ b/src/vlccore-sys/Cargo.toml<br>
> @@ -0,0 +1,10 @@<br>
> +[package]<br>
> +name = "vlccore-sys"<br>
> +version = "0.1.0"<br>
> +authors = ["Kartik Ohri <<a href="mailto:kartikohri13@gmail.com" target="_blank">kartikohri13@gmail.com</a>>"]<br>
> +edition = "2018"<br>
> +license = "LGPL-2.1-or-later"<br>
> +<br>
> +[dependencies]<br>
> +libc = "0.2"<br>
> +<br>
> diff --git a/src/vlccore-sys/src/<a href="http://lib.rs" rel="noreferrer" target="_blank">lib.rs</a> b/src/vlccore-sys/src/<a href="http://lib.rs" rel="noreferrer" target="_blank">lib.rs</a><br>
> new file mode 100644<br>
> index 0000000000..42a75452cd<br>
> --- /dev/null<br>
> +++ b/src/vlccore-sys/src/<a href="http://lib.rs" rel="noreferrer" target="_blank">lib.rs</a><br>
> @@ -0,0 +1,2 @@<br>
> +#![allow(non_camel_case_types)]<br>
> +pub mod stream;<br>
> diff --git a/src/vlccore-sys/src/<a href="http://stream.rs" rel="noreferrer" target="_blank">stream.rs</a> b/src/vlccore-sys/src/<a href="http://stream.rs" rel="noreferrer" target="_blank">stream.rs</a><br>
> new file mode 100644<br>
> index 0000000000..c9e8d5f0b3<br>
> --- /dev/null<br>
> +++ b/src/vlccore-sys/src/<a href="http://stream.rs" rel="noreferrer" target="_blank">stream.rs</a><br>
> @@ -0,0 +1,24 @@<br>
> +use libc::{c_char, c_int, c_void, size_t, ssize_t};<br>
> +<br>
> +#[repr(C)]<br>
> +pub struct stream_t {<br>
> +    _private: [u8; 0],<br>
> +}<br>
> +<br>
> +pub type vlc_object_t = stream_t;<br>
> +<br>
> +extern "C" {<br>
> +    pub fn vlc_stream_Read(s: *mut stream_t, buf: *mut c_void, len: size_t) -> ssize_t;<br>
> +<br>
> +    pub fn vlc_stream_Tell(s: *const stream_t) -> u64;<br>
> +<br>
> +    pub fn vlc_stream_Eof(s: *const stream_t) -> bool;<br>
> +<br>
> +    pub fn vlc_stream_Seek(s: *mut stream_t, offset: u64) -> c_int;<br>
> +<br>
> +    pub fn vlc_stream_vaControl(s: *mut stream_t, query: c_int, ...) -> c_int;<br>
<br>
After checking [1], you probably need the following code instead:<br>
<br>
    extern "C" {<br>
        pub unsafe fn vlc_stream_vaControl(s: *mut stream_t, query: c_int, ap: VaList) -> c_int;<br>
    }<br>
    pub unsafe fn vlc_stream_Control(s: *mut stream_t, query: c_int, mut args: ...) -> c_int {<br>
        vlc_stream_vaControl(s, query, args)<br>
    }<br>
<br>
Otherwise, as far as I understand this document, va_start/va_end might not be called correctly.<br>
I haven't checked in details though, and it needs the #![feature(c_variadic)] attribute.<br>
<br>
[1] <a href="https://github.com/rust-lang/rfcs/blob/26197104b7bb9a5a35db243d639aee6e46d35d75/text/2137-variadic.md" rel="noreferrer" target="_blank">https://github.com/rust-lang/rfcs/blob/26197104b7bb9a5a35db243d639aee6e46d35d75/text/2137-variadic.md</a><br>
<br></blockquote><div> </div><div>You are probably right. In that case, the RFC is not yet stabilized so should we drop this part till then from the patch ?</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> +<br>
> +    pub fn vlc_stream_Delete(s: *mut stream_t);<br>
> +<br>
> +    pub fn vlc_stream_NewURL(obj: *mut vlc_object_t, url: *const c_char) -> *mut stream_t;<br>
> +}<br>
> --<br>
> 2.25.1<br>
><br>
> _______________________________________________<br>
> vlc-devel mailing list<br>
> To unsubscribe or modify your subscription options:<br>
> <a href="https://mailman.videolan.org/listinfo/vlc-devel" rel="noreferrer" target="_blank">https://mailman.videolan.org/listinfo/vlc-devel</a><br>
<br>
<br>
Regards,<br>
--<br>
Alexandre Janniaux<br>
Videolabs<br>
_______________________________________________<br>
vlc-devel mailing list<br>
To unsubscribe or modify your subscription options:<br>
<a href="https://mailman.videolan.org/listinfo/vlc-devel" rel="noreferrer" target="_blank">https://mailman.videolan.org/listinfo/vlc-devel</a></blockquote></div></div>