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

Gb28181 #91

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
77 changes: 54 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"protocol/hls",
"protocol/rtsp",
"protocol/webrtc",
"protocol/gb28181",
"library/bytesio",
"application/xiu",
"application/http-server",
Expand Down
2 changes: 1 addition & 1 deletion application/pprtmp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ log = "0.4.0"
env_logger = "0.10.0"
clap = "4.1.4"

rtmp = "0.4.0"
rtmp = "0.4.1"
streamhub = "0.1.0"

[dependencies.tokio]
Expand Down
1 change: 1 addition & 0 deletions application/xiu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ streamhub = { path = "../../library/streamhub/" }
rtmp = { path = "../../protocol/rtmp/" }
xrtsp = { path = "../../protocol/rtsp/" }
xwebrtc = { path = "../../protocol/webrtc/" }
gb28181 = { path = "../../protocol/gb28181/" }
httpflv = { path = "../../protocol/httpflv/" }
hls = { path = "../../protocol/hls/" }

Expand Down
21 changes: 18 additions & 3 deletions application/xiu/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Config {
pub rtmp: Option<RtmpConfig>,
pub rtsp: Option<RtspConfig>,
pub webrtc: Option<WebRTCConfig>,
pub gb28181: Option<GB28181Config>,
pub httpflv: Option<HttpFlvConfig>,
pub hls: Option<HlsConfig>,
pub httpapi: Option<HttpApiConfig>,
Expand All @@ -22,6 +23,7 @@ impl Config {
rtmp_port: usize,
rtsp_port: usize,
webrtc_port: usize,
gb28181_port: usize,
httpflv_port: usize,
hls_port: usize,
log_level: String,
Expand Down Expand Up @@ -53,6 +55,14 @@ impl Config {
});
}

let mut gb28181_config: Option<GB28181Config> = None;
if gb28181_port > 0 {
gb28181_config = Some(GB28181Config {
enabled: true,
api_port: gb28181_port,
});
}

let mut httpflv_config: Option<HttpFlvConfig> = None;
if httpflv_port > 0 {
httpflv_config = Some(HttpFlvConfig {
Expand All @@ -79,6 +89,7 @@ impl Config {
rtmp: rtmp_config,
rtsp: rtsp_config,
webrtc: webrtc_config,
gb28181: gb28181_config,
httpflv: httpflv_config,
hls: hls_config,
httpapi: None,
Expand Down Expand Up @@ -121,6 +132,12 @@ pub struct WebRTCConfig {
pub port: usize,
}

#[derive(Debug, Deserialize, Clone)]
pub struct GB28181Config {
pub enabled: bool,
pub api_port: usize,
}

#[derive(Debug, Deserialize, Clone)]
pub struct HttpFlvConfig {
pub enabled: bool,
Expand Down Expand Up @@ -184,9 +201,7 @@ fn test_toml_parse() {
Err(err) => println!("{}", err),
}

let str = fs::read_to_string(
"./src/config/config.toml",
);
let str = fs::read_to_string("./src/config/config.toml");

match str {
Ok(val) => {
Expand Down
22 changes: 21 additions & 1 deletion application/xiu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ async fn main() -> Result<()> {
.value_parser(value_parser!(usize))
.conflicts_with("config_file_path"),
)
.arg(
Arg::new("gb28181")
.long("gb28181")
.short('g')
.value_name("port")
.help("Specify the gb28181 listening port.(e.g.:3000)")
.value_parser(value_parser!(usize))
.conflicts_with("config_file_path"),
)
.arg(
Arg::new("httpflv")
.long("httpflv")
Expand Down Expand Up @@ -119,8 +128,13 @@ async fn main() -> Result<()> {
let rtmp_port_o = matches.get_one::<usize>("rtmp");
let rtsp_port_o = matches.get_one::<usize>("rtsp");
let webrtc_port_o = matches.get_one::<usize>("webrtc");
let gb28181_port_o = matches.get_one::<usize>("gb28181");

if rtmp_port_o.is_none() && rtsp_port_o.is_none() && webrtc_port_o.is_none() {
if rtmp_port_o.is_none()
&& rtsp_port_o.is_none()
&& webrtc_port_o.is_none()
&& gb28181_port_o.is_none()
{
println!("If you do not specify the config Options, you must enable at least one protocol from RTSP and RTMP.");
return Ok(());
}
Expand All @@ -140,6 +154,11 @@ async fn main() -> Result<()> {
None => 0,
};

let gb28181_port = match gb28181_port_o {
Some(val) => *val,
None => 0,
};

let httpflv_port = match matches.get_one::<usize>("httpflv") {
Some(val) => *val,
None => 0,
Expand All @@ -157,6 +176,7 @@ async fn main() -> Result<()> {
rtmp_port,
rtsp_port,
webrtc_port,
gb28181_port,
httpflv_port,
hls_port,
log_level,
Expand Down
31 changes: 29 additions & 2 deletions application/xiu/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
super::config::Config,
//https://rustcc.cn/article?id=6dcbf032-0483-4980-8bfe-c64a7dfb33c7
anyhow::Result,
gb28181::api_service,
hls::remuxer::HlsRemuxer,
hls::server as hls_server,
httpflv::server as httpflv_server,
Expand Down Expand Up @@ -50,6 +51,7 @@ impl Service {
self.start_rtmp(&mut stream_hub).await?;
self.start_rtsp(&mut stream_hub).await?;
self.start_webrtc(&mut stream_hub).await?;
self.start_gb28181(&mut stream_hub).await?;
self.start_http_api_server(&mut stream_hub).await?;
self.start_rtmp_remuxer(&mut stream_hub).await?;

Expand Down Expand Up @@ -158,14 +160,21 @@ impl Service {
}

async fn start_rtmp_remuxer(&mut self, stream_hub: &mut StreamsHub) -> Result<()> {
//The remuxer now is used for rtsp2rtmp, so both rtsp/rtmp cfg need to be enabled.
//The remuxer now is used for rtsp2rtmp/gb281812rtmp, so both rtsp(or gb28181)/rtmp cfg need to be enabled.
let mut rtsp_enabled = false;
if let Some(rtsp_cfg_value) = &self.cfg.rtsp {
if rtsp_cfg_value.enabled {
rtsp_enabled = true;
}
}
if !rtsp_enabled {

let mut gb28181_enabled = false;
if let Some(gb28181_cfg_value) = &self.cfg.gb28181 {
if gb28181_cfg_value.enabled {
gb28181_enabled = true;
}
}
if !rtsp_enabled && !gb28181_enabled {
return Ok(());
}

Expand Down Expand Up @@ -240,6 +249,24 @@ impl Service {
Ok(())
}

async fn start_gb28181(&mut self, stream_hub: &mut StreamsHub) -> Result<()> {
let gb28181_cfg = &self.cfg.gb28181;

if let Some(gb28181_cfg_value) = gb28181_cfg {
if !gb28181_cfg_value.enabled {
return Ok(());
}

let producer = stream_hub.get_hub_event_sender();
let listen_port = gb28181_cfg_value.api_port;
tokio::spawn(async move {
api_service::run(producer, listen_port).await;
});
}

Ok(())
}

async fn start_httpflv(&mut self, stream_hub: &mut StreamsHub) -> Result<()> {
let httpflv_cfg = &self.cfg.httpflv;

Expand Down
8 changes: 8 additions & 0 deletions library/bytesio/src/bits_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ impl BitsReader {
Ok((self.cur_byte >> self.cur_bit_left) & 0x01)
}

pub fn advance_bit(&mut self) -> Result<u8, BitError> {
if self.cur_bit_left == 0 {
self.cur_byte = self.reader.read_u8()?;
self.cur_bit_left = 8;
}
Ok((self.cur_byte >> self.cur_bit_left) & 0x01)
}

pub fn read_n_bits(&mut self, n: usize) -> Result<u64, BitError> {
let mut result: u64 = 0;
for _ in 0..n {
Expand Down
Loading