Skip to content

Commit

Permalink
add ini config env
Browse files Browse the repository at this point in the history
  • Loading branch information
BenLocal committed Jun 26, 2024
1 parent 4e422e3 commit b4a8f77
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
12 changes: 11 additions & 1 deletion examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use once_cell::sync::{Lazy, OnceCell};
use rszlm::{
event::EVENTS,
init::EnvInitBuilder,
init::{EnvIni, EnvInitBuilder},
player::ProxyPlayerBuilder,
server::{http_server_start, rtmp_server_start, rtsp_server_start, stop_all_server},
};
Expand Down Expand Up @@ -180,6 +180,16 @@ fn start_zlm_background(
.log_mask(0)
.thread_num(20)
.build();
{
let ini = EnvIni::global().lock().unwrap();
ini.set_option_int("protocol.hls_demand", 1);
ini.set_option_int("protocol.rtsp_demand", 1);
ini.set_option_int("protocol.rtmp_demand", 1);
ini.set_option_int("protocol.ts_demand", 1);
ini.set_option_int("protocol.fmp4_demand", 1);

println!("ini: {}", ini.dump());
}

http_server_start(8553, false);
rtsp_server_start(8554, false);
Expand Down
62 changes: 59 additions & 3 deletions src/init.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::sync::Mutex;

use once_cell::sync::Lazy;
use rszlm_sys::*;

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

pub struct EnvInitBuilder(mk_config);

Expand Down Expand Up @@ -68,8 +71,14 @@ impl EnvInitBuilder {
self
}

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

pub fn ini_by_file(mut self, path: &str) -> Self {
self.0.ini = const_str_to_ptr!(path);
self.0.ini_is_path = 1;
self
}

Expand Down Expand Up @@ -98,3 +107,50 @@ impl Default for EnvInitBuilder {
Self::new()
}
}

static EVN_INI: Lazy<Mutex<EnvIni>> = Lazy::new(|| Mutex::new(EnvIni(unsafe { mk_ini_default() })));

pub struct EnvIni(mk_ini);

impl EnvIni {
/// 创建ini配置对象
pub fn new() -> Self {
Self(unsafe { mk_ini_create() })
}

/// 创建ini配置对象
/// 全局默认ini配置,请勿用mk_ini_release释放它
///
pub fn global() -> &'static Mutex<EnvIni> {
&EVN_INI
}

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)) }
}

pub fn set_option_int(&self, key: &str, val: i32) {
unsafe { mk_ini_set_option_int(self.0, const_str_to_ptr!(key), 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))) }
}

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

pub fn dump(&self) -> String {
unsafe { const_ptr_to_string!(mk_ini_dump_string(self.0)) }
}
}

impl Drop for EnvIni {
fn drop(&mut self) {
unsafe { mk_ini_release(self.0) }
}
}

unsafe impl Send for EnvIni {}
unsafe impl Sync for EnvIni {}

0 comments on commit b4a8f77

Please sign in to comment.