Skip to content

Commit

Permalink
fix some error
Browse files Browse the repository at this point in the history
  • Loading branch information
BenLocal committed Jul 4, 2024
1 parent f280861 commit f1d19d3
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 78 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ workspace = { members = ["rszlm-sys"] }

[package]
name = "rszlm"
version = "0.1.6"
version = "0.1.7"
edition = "2021"
authors = ["shiben. <benshi0v0@gmail.com>"]
description = "ZLMediaKit rust api"
Expand Down
2 changes: 1 addition & 1 deletion examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ fn start_zlm_background(
}));
} else {
let _ = tx_clone.blocking_send(ProxyMessageCmd::Start(StartProxyMessage {
source: "https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8".to_string(),
source: "rtsp://admin:iflytek@2030@192.168.0.14:554/t01/1".to_string(),
app,
stream,
}));
Expand Down
21 changes: 16 additions & 5 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rszlm_sys::*;
use std::{ffi::CString, sync::RwLock};

use crate::{
as_cstr_array, const_ptr_to_string, const_str_to_ptr,
const_ptr_to_string, const_str_to_ptr,
obj::{AuthInvoker, MediaInfo, MediaSource, Parser, RecordInfo, RtcTransport, SockInfo},
};

Expand Down Expand Up @@ -477,7 +477,8 @@ impl RtspAuthInvoker {
}

pub fn call(&self, pwd_or_md5: &str, encrypted: bool) {
unsafe { mk_rtsp_auth_invoker_do(self.0, encrypted as i32, const_str_to_ptr!(pwd_or_md5)) }
let pwd_or_md5 = const_str_to_ptr!(pwd_or_md5);
unsafe { mk_rtsp_auth_invoker_do(self.0, encrypted as i32, pwd_or_md5.as_ptr()) }
}
}

Expand Down Expand Up @@ -510,7 +511,8 @@ impl RtspGetRealmInvoker {
}

pub fn call(&self, realm: &str) {
unsafe { mk_rtsp_get_realm_invoker_do(self.0, const_str_to_ptr!(realm)) }
let realm = const_str_to_ptr!(realm);
unsafe { mk_rtsp_get_realm_invoker_do(self.0, realm.as_ptr()) }
}
}

Expand Down Expand Up @@ -585,9 +587,18 @@ pub struct HttpResponseInvoker(mk_http_response_invoker, bool);

impl HttpResponseInvoker {
pub fn invoke(&self, code: i32, headers: Vec<String>, body: &str) {
let header_ptr = as_cstr_array(&headers);
// let header_ptr = as_cstr_array(headers);
let cstr_argv: Vec<_> = headers
.iter()
.map(|arg| std::ffi::CString::new(arg.as_str()).unwrap())
.collect();

let mut p_argv: Vec<_> = cstr_argv.iter().map(|arg| arg.as_ptr()).collect();
p_argv.push(std::ptr::null_mut());

let body = const_str_to_ptr!(body);
unsafe {
mk_http_response_invoker_do_string(self.0, code, header_ptr, const_str_to_ptr!(body))
mk_http_response_invoker_do_string(self.0, code, p_argv.as_mut_ptr(), body.as_ptr())
}
}
}
Expand Down
28 changes: 19 additions & 9 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ impl EnvInitBuilder {
/// 默认设置为NULL关闭日志输出至文件
///
pub fn log_file_path(mut self, log_file_path: &str) -> Self {
self.0.log_file_path = const_str_to_ptr!(log_file_path);
let log_file_path = const_str_to_ptr!(log_file_path);
self.0.log_file_path = log_file_path.into_raw();
self
}

Expand All @@ -77,12 +78,14 @@ impl EnvInitBuilder {
}

pub fn ini(mut self, ini_txt: &str) -> Self {
self.0.ini = const_str_to_ptr!(ini_txt);
let ini_txt = const_str_to_ptr!(ini_txt);
self.0.ini = ini_txt.into_raw();
self
}

pub fn ini_by_file(mut self, path: &str) -> Self {
self.0.ini = const_str_to_ptr!(path);
let path = const_str_to_ptr!(path);
self.0.ini = path.into_raw();
self.0.ini_is_path = 1;
self
}
Expand All @@ -93,12 +96,14 @@ impl EnvInitBuilder {
}

pub fn ssl(mut self, ssl: &str) -> Self {
self.0.ssl = const_str_to_ptr!(ssl);
let ssl = const_str_to_ptr!(ssl);
self.0.ssl = ssl.into_raw();
self
}

pub fn ssl_pwd(mut self, ssl_pwd: &str) -> Self {
self.0.ssl_pwd = const_str_to_ptr!(ssl_pwd);
let ssl_pwd = const_str_to_ptr!(ssl_pwd);
self.0.ssl_pwd = ssl_pwd.into_raw();
self
}

Expand Down Expand Up @@ -131,19 +136,24 @@ impl EnvIni {
}

pub fn set_option(&self, key: &str, val: &str) {
unsafe { mk_ini_set_option(self.0, const_str_to_ptr!(key), const_str_to_ptr!(val)) }
let val = const_str_to_ptr!(val);
let key = const_str_to_ptr!(key);
unsafe { mk_ini_set_option(self.0, key.as_ptr(), val.as_ptr()) }
}

pub fn set_option_int(&self, key: &str, val: i32) {
unsafe { mk_ini_set_option_int(self.0, const_str_to_ptr!(key), val) }
let key = const_str_to_ptr!(key);
unsafe { mk_ini_set_option_int(self.0, key.as_ptr(), val) }
}

pub fn get_option(&self, key: &str) -> String {
unsafe { const_ptr_to_string!(mk_ini_get_option(self.0, const_str_to_ptr!(key))) }
let key = const_str_to_ptr!(key);
unsafe { const_ptr_to_string!(mk_ini_get_option(self.0, key.as_ptr())) }
}

pub fn remove_option(&self, key: &str) -> bool {
unsafe { mk_ini_del_option(self.0, const_str_to_ptr!(key)) != 0 }
let key = const_str_to_ptr!(key);
unsafe { mk_ini_del_option(self.0, key.as_ptr()) != 0 }
}

pub fn dump(&self) -> String {
Expand Down
20 changes: 4 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ pub mod webrtc;
#[macro_export]
macro_rules! const_ptr_to_string {
($a:ident) => {
std::ffi::CStr::from_ptr($a).to_string_lossy().to_string()
std::ffi::CStr::from_ptr($a).to_string_lossy().into_owned()
};
($a:expr) => {
std::ffi::CStr::from_ptr($a).to_string_lossy().to_string()
std::ffi::CStr::from_ptr($a).to_string_lossy().into_owned()
};
($a:ident, $def:literal) => {
Ok(std::ffi::CStr::from_ptr(schema)
Expand All @@ -28,10 +28,10 @@ macro_rules! const_ptr_to_string {
#[macro_export]
macro_rules! const_str_to_ptr {
($a:ident) => {
std::ffi::CString::new($a).unwrap().into_raw()
std::ffi::CString::new($a).unwrap()
};
($a:expr) => {
std::ffi::CString::new($a).unwrap().into_raw()
std::ffi::CString::new($a).unwrap()
};
}

Expand All @@ -41,15 +41,3 @@ macro_rules! box_to_mut_void_ptr {
Box::into_raw(Box::new($a)) as *mut _
};
}

pub(crate) fn as_cstr_array<T: Into<Vec<u8>> + Clone>(
arr: &[T],
) -> *mut *const ::std::os::raw::c_char {
let mut tmp: Vec<_> = arr
.iter()
.map(|s| std::ffi::CString::new::<T>(s.to_owned()).unwrap())
.map(|f| f.as_ptr())
.collect::<Vec<_>>();
tmp.push(std::ptr::null_mut());
tmp.as_mut_ptr()
}
9 changes: 6 additions & 3 deletions src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ impl Media {
hls_enabled: bool,
mp4_enabled: bool,
) -> Self {
let vhost = const_str_to_ptr!(vhost);
let app = const_str_to_ptr!(app);
let stream = const_str_to_ptr!(stream);
unsafe {
mk_media_create(
const_str_to_ptr!(vhost),
const_str_to_ptr!(app),
const_str_to_ptr!(stream),
vhost.as_ptr(),
app.as_ptr(),
stream.as_ptr(),
duration,
hls_enabled as i32,
mp4_enabled as i32,
Expand Down
9 changes: 6 additions & 3 deletions src/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ impl AuthInvoker {
}

pub fn deny(&self, error: &str) {
unsafe { mk_auth_invoker_do(self.0, const_str_to_ptr!(error)) }
let error = const_str_to_ptr!(error);
unsafe { mk_auth_invoker_do(self.0, error.as_ptr()) }
}
}

Expand Down Expand Up @@ -285,11 +286,13 @@ impl Parser {
}

pub fn query(&self, key: &str) -> String {
unsafe { const_ptr_to_string!(mk_parser_get_url_param(self.0, const_str_to_ptr!(key))) }
let key = const_str_to_ptr!(key);
unsafe { const_ptr_to_string!(mk_parser_get_url_param(self.0, key.as_ptr())) }
}

pub fn header(&self, key: &str) -> String {
unsafe { const_ptr_to_string!(mk_parser_get_header(self.0, const_str_to_ptr!(key))) }
let key = const_str_to_ptr!(key);
unsafe { const_ptr_to_string!(mk_parser_get_header(self.0, key.as_ptr())) }
}

pub fn body(&self) -> String {
Expand Down
18 changes: 11 additions & 7 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ impl From<mk_proxy_player> for ProxyPlayer {

impl ProxyPlayer {
pub fn set_options(&self, key: &str, val: &str) {
unsafe {
mk_proxy_player_set_option(self.0, const_str_to_ptr!(key), const_str_to_ptr!(val))
}
let key = const_str_to_ptr!(key);
let val = const_str_to_ptr!(val);
unsafe { mk_proxy_player_set_option(self.0, key.as_ptr(), val.as_ptr()) }
}

pub fn play(&self, url: &str) {
unsafe { mk_proxy_player_play(self.0, const_str_to_ptr!(url)) };
let url = const_str_to_ptr!(url);
unsafe { mk_proxy_player_play(self.0, url.as_ptr()) };
}

pub fn total_reader_count(&self) -> i32 {
Expand Down Expand Up @@ -107,10 +108,13 @@ impl ProxyPlayerBuilder {

pub fn build(self) -> ProxyPlayer {
let tmp = ProxyPlayer(unsafe {
let vhost = const_str_to_ptr!(self.vhost);
let app = const_str_to_ptr!(self.app);
let stream = const_str_to_ptr!(self.stream);
mk_proxy_player_create(
const_str_to_ptr!(self.vhost),
const_str_to_ptr!(self.app),
const_str_to_ptr!(self.stream),
vhost.as_ptr(),
app.as_ptr(),
stream.as_ptr(),
self.hls_enabled as i32,
self.mp4_enabled as i32,
)
Expand Down
20 changes: 14 additions & 6 deletions src/pusher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ impl From<MediaSource> for Pusher {

impl Pusher {
pub fn set_options(&self, key: &str, val: &str) {
unsafe { mk_pusher_set_option(self.0, const_str_to_ptr!(key), const_str_to_ptr!(val)) }
let key = const_str_to_ptr!(key);
let val = const_str_to_ptr!(val);
unsafe { mk_pusher_set_option(self.0, key.as_ptr(), val.as_ptr()) }
}

pub fn publish(&self, url: &str) {
unsafe { mk_pusher_publish(self.0, const_str_to_ptr!(url)) }
let url = const_str_to_ptr!(url);
unsafe { mk_pusher_publish(self.0, url.as_ptr()) }
}

pub fn on_result(&self, cb: OnEventCallbackFn) {
Expand Down Expand Up @@ -74,12 +77,17 @@ impl PusherBuilder {
}

pub fn build(self) -> Pusher {
let vhost = const_str_to_ptr!(self.vhost);
let app = const_str_to_ptr!(self.app);
let stream = const_str_to_ptr!(self.stream);
let schema = const_str_to_ptr!(self.schema);

Pusher(unsafe {
mk_pusher_create(
const_str_to_ptr!(self.schema),
const_str_to_ptr!(self.vhost),
const_str_to_ptr!(self.app),
const_str_to_ptr!(self.stream),
schema.as_ptr(),
vhost.as_ptr(),
app.as_ptr(),
stream.as_ptr(),
)
})
}
Expand Down
47 changes: 25 additions & 22 deletions src/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ impl FlvRecorder {
}

pub fn start(&self, vhost: &str, app: &str, stream: &str, file_path: &str) -> i32 {
let vhost = const_str_to_ptr!(vhost);
let app = const_str_to_ptr!(app);
let stream = const_str_to_ptr!(stream);
let file_path = const_str_to_ptr!(file_path);
unsafe {
mk_flv_recorder_start(
self.0,
const_str_to_ptr!(vhost),
const_str_to_ptr!(app),
const_str_to_ptr!(stream),
const_str_to_ptr!(file_path),
vhost.as_ptr(),
app.as_ptr(),
stream.as_ptr(),
file_path.as_ptr(),
)
}
}
Expand All @@ -36,13 +40,11 @@ impl Recorder {
/// - 0:hls
/// - 1:MP4
pub fn is_recording(typ: u32, vhost: &str, app: &str, stream: &str) -> bool {
let vhost = const_str_to_ptr!(vhost);
let app = const_str_to_ptr!(app);
let stream = const_str_to_ptr!(stream);
unsafe {
mk_recorder_is_recording(
typ as i32,
const_str_to_ptr!(vhost),
const_str_to_ptr!(app),
const_str_to_ptr!(stream),
) == 1
mk_recorder_is_recording(typ as i32, vhost.as_ptr(), app.as_ptr(), stream.as_ptr()) == 1
}
}

Expand All @@ -54,26 +56,27 @@ impl Recorder {
file_path: &str,
max_seconds: usize,
) -> i32 {
let vhost = const_str_to_ptr!(vhost);
let app = const_str_to_ptr!(app);
let stream = const_str_to_ptr!(stream);
let file_path = const_str_to_ptr!(file_path);

unsafe {
mk_recorder_start(
typ as i32,
const_str_to_ptr!(vhost),
const_str_to_ptr!(app),
const_str_to_ptr!(stream),
const_str_to_ptr!(file_path),
vhost.as_ptr(),
app.as_ptr(),
stream.as_ptr(),
file_path.as_ptr(),
max_seconds as usize,
)
}
}

pub fn stop(typ: u32, vhost: &str, app: &str, stream: &str) -> i32 {
unsafe {
mk_recorder_stop(
typ as i32,
const_str_to_ptr!(vhost),
const_str_to_ptr!(app),
const_str_to_ptr!(stream),
)
}
let vhost = const_str_to_ptr!(vhost);
let app = const_str_to_ptr!(app);
let stream = const_str_to_ptr!(stream);
unsafe { mk_recorder_stop(typ as i32, vhost.as_ptr(), app.as_ptr(), stream.as_ptr()) }
}
}
Loading

0 comments on commit f1d19d3

Please sign in to comment.