Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add recorder and rtp server #1

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod media;
pub mod obj;
pub mod player;
pub mod pusher;
pub mod recorder;
pub mod server;
#[cfg(feature = "webrtc")]
pub mod webrtc;
Expand Down
79 changes: 79 additions & 0 deletions src/recorder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use rszlm_sys::*;

use crate::const_str_to_ptr;

pub struct FlvRecorder(mk_flv_recorder);

impl FlvRecorder {
pub fn new() -> Self {
Self(unsafe { mk_flv_recorder_create() })
}

pub fn start(&self, vhost: &str, app: &str, stream: &str, file_path: &str) -> i32 {
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),
)
}
}
}

impl Drop for FlvRecorder {
fn drop(&mut self) {
unsafe { mk_flv_recorder_release(self.0) }
}
}

pub struct Recorder;

impl Recorder {
/// 是否正在录制
/// - typ:
/// - 0:hls
/// - 1:MP4
pub fn is_recording(typ: u32, vhost: &str, app: &str, stream: &str) -> bool {
unsafe {
mk_recorder_is_recording(
typ as i32,
const_str_to_ptr!(vhost),
const_str_to_ptr!(app),
const_str_to_ptr!(stream),
) == 1
}
}

pub fn start(
typ: u32,
vhost: &str,
app: &str,
stream: &str,
file_path: &str,
max_seconds: usize,
) -> i32 {
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),
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),
)
}
}
}
63 changes: 63 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use rszlm_sys::*;

use crate::{box_to_mut_void_ptr, const_ptr_to_string, const_str_to_ptr};

pub fn http_server_start(port: u16, ssl: bool) {
unsafe {
mk_http_server_start(port, ssl as i32);
Expand Down Expand Up @@ -35,3 +37,64 @@ pub fn stop_all_server() {
mk_stop_all_server();
}
}

pub struct RtpServer(mk_rtp_server);

impl RtpServer {
pub fn new(port: u16, tcp_mode: i32, stream_id: &str) -> Self {
Self(unsafe { mk_rtp_server_create(port, tcp_mode, const_str_to_ptr!(stream_id)) })
}

pub fn bind_port(&self) -> u16 {
unsafe { mk_rtp_server_port(self.0) }
}

pub fn on_detach(&self, cb: impl FnOnce() + 'static) {
let cb = Box::new(cb);
unsafe {
mk_rtp_server_set_on_detach(
self.0,
Some(on_rtp_server_detach),
box_to_mut_void_ptr!(cb),
)
}
}

pub fn connect(&self, url: &str, dst_port: u16, cb: impl FnOnce(i32, &str, i32) + 'static) {
let cb = Box::new(cb);
unsafe {
mk_rtp_server_connect(
self.0,
const_str_to_ptr!(url),
dst_port,
Some(on_rtp_server_connected),
box_to_mut_void_ptr!(cb),
)
}
}
}

extern "C" fn on_rtp_server_detach(user_data: *mut ::std::os::raw::c_void) {
unsafe {
let cb: &mut Box<dyn FnMut() + 'static> = std::mem::transmute(user_data);
cb();
};
}

extern "C" fn on_rtp_server_connected(
user_data: *mut ::std::os::raw::c_void,
err: ::std::os::raw::c_int,
what: *const ::std::os::raw::c_char,
sys_err: ::std::os::raw::c_int,
) {
unsafe {
let cb: &mut Box<dyn FnMut(i32, &str, i32) + 'static> = std::mem::transmute(user_data);
cb(err, const_ptr_to_string!(what).as_str(), sys_err);
};
}

impl Drop for RtpServer {
fn drop(&mut self) {
unsafe { mk_rtp_server_release(self.0) }
}
}