Skip to content

Commit

Permalink
feat(whipinto/whepfrom): use cargo bin
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Oct 2, 2024
1 parent 846d0a0 commit 020e2c4
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 116 deletions.
6 changes: 2 additions & 4 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ path = "src/main.rs"
name = "liveman"
path = "src/liveman.rs"

[[bin]]
name = "whipinto"
path = "src/whipinto.rs"

[[bin]]
name = "whepfrom"
path = "src/whepfrom.rs"

[workspace]
members = [
".",
Expand Down Expand Up @@ -51,6 +59,8 @@ tracing = "0.1"
[dependencies]
liveion = { path = "liveion" }
liveman = { path = "liveman" }
whipinto = { path = "tools/whipinto" }
whepfrom = { path = "tools/whepfrom" }
signal = { path = "libs/signal" }
utils = { path = "libs/utils" }

Expand Down
2 changes: 0 additions & 2 deletions src/liveman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,3 @@ async fn main() {
liveman::server_up(cfg, listener, helper::shutdown_signal()).await;
info!("Server shutdown");
}


52 changes: 52 additions & 0 deletions src/whepfrom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use clap::{ArgAction, Parser};
use tracing::Level;

const SCHEME_RTSP_SERVER: &str = "rtsp-listen";

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Verbose mode [default: "warn", -v "info", -vv "debug", -vvv "trace"]
#[arg(short = 'v', action = ArgAction::Count, default_value_t = 0)]
verbose: u8,
/// rtsp://[username]:[password]@[ip]:[port]/[stream] Or <stream.sdp>
#[arg(short, long, default_value_t = format!("{}://0.0.0.0:8555", SCHEME_RTSP_SERVER))]
output: String,
/// Set Listener address
#[arg(long)]
host: Option<String>,
/// The WHEP server endpoint to POST SDP offer to. e.g.: https://example.com/whep/777
#[arg(short, long)]
whep: String,
/// Authentication token to use, will be sent in the HTTP Header as 'Bearer '
#[arg(short, long)]
token: Option<String>,
/// Run a command as childprocess
#[arg(long)]
command: Option<String>,
}

#[tokio::main]
async fn main() {
let args = Args::parse();

utils::set_log(format!(
"whepfrom={},webrtc=error",
match args.verbose {
0 => Level::WARN,
1 => Level::INFO,
2 => Level::DEBUG,
_ => Level::TRACE,
}
));

whepfrom::whepfrom(
args.output.clone(),
args.host.clone(),
args.whep.clone(),
args.token.clone(),
args.command.clone(),
)
.await
.unwrap();
}
52 changes: 52 additions & 0 deletions src/whipinto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use clap::{ArgAction, Parser};
use tracing::Level;

const SCHEME_RTSP_SERVER: &str = "rtsp-listen";

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Verbose mode [default: "warn", -v "info", -vv "debug", -vvv "trace"]
#[arg(short = 'v', action = ArgAction::Count, default_value_t = 0)]
verbose: u8,
/// rtsp://[username]:[password]@[ip]:[port]/[stream] Or <stream.sdp>
#[arg(short, long, default_value_t = format!("{}://0.0.0.0:8554", SCHEME_RTSP_SERVER))]
input: String,
/// Set Listener address
#[arg(long)]
host: Option<String>,
/// The WHIP server endpoint to POST SDP offer to. e.g.: https://example.com/whip/777
#[arg(short, long)]
whip: String,
/// Authentication token to use, will be sent in the HTTP Header as 'Bearer '
#[arg(short, long)]
token: Option<String>,
/// Run a command as childprocess
#[arg(long)]
command: Option<String>,
}

#[tokio::main]
async fn main() {
let args = Args::parse();

utils::set_log(format!(
"whipinto={},webrtc=error",
match args.verbose {
0 => Level::WARN,
1 => Level::INFO,
2 => Level::DEBUG,
_ => Level::TRACE,
}
));

whipinto::whipinto(
args.input.clone(),
args.host.clone(),
args.whip.clone(),
args.token.clone(),
args.command.clone(),
)
.await
.unwrap();
}
7 changes: 4 additions & 3 deletions tools/whepfrom/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
[package]
name = "whepfrom"
description = "WHEP to RTP tool"
description = "WHEP to RTP/RTSP tool"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true

[lib]
crate-type = ["lib"]

[dependencies]
cli = { path = "../../libs/cli" }
rtsp = { path = "../../libs/rtsp" }
libwish = { path = "../../libs/libwish" }
signal = { path = "../../libs/signal" }
utils = { path = "../../libs/utils" }

anyhow = { workspace = true }
clap = { workspace = true, features = ["derive"] }
tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true }
webrtc = { workspace = true }
Expand Down
58 changes: 12 additions & 46 deletions tools/whepfrom/src/main.rs → tools/whepfrom/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::{anyhow, Result};
use clap::{ArgAction, Parser};
use cli::create_child;
use core::net::{Ipv4Addr, Ipv6Addr};
use portpicker::pick_unused_port;
Expand All @@ -16,7 +15,7 @@ use tokio::{
net::UdpSocket,
sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
};
use tracing::{debug, error, info, trace, warn, Level};
use tracing::{debug, error, info, trace, warn};
use url::{Host, Url};
use webrtc::ice_transport::ice_credential_type::RTCIceCredentialType;
use webrtc::peer_connection::sdp::session_description::RTCSessionDescription;
Expand All @@ -43,60 +42,30 @@ const SCHEME_RTSP_SERVER: &str = "rtsp-listen";
const _SCHEME_RTSP_CLIENT: &str = "rtsp";
const SCHEME_RTP_SDP: &str = "sdp";

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Verbose mode [default: "warn", -v "info", -vv "debug", -vvv "trace"]
#[arg(short = 'v', action = ArgAction::Count, default_value_t = 0)]
verbose: u8,
/// rtsp://[username]:[password]@[ip]:[port]/[stream] Or <stream.sdp>
#[arg(short, long, default_value_t = format!("{}://0.0.0.0:8555", SCHEME_RTSP_SERVER))]
output: String,
/// Set Listener address
#[arg(long)]
host: Option<String>,
/// The WHEP server endpoint to POST SDP offer to. e.g.: https://example.com/whep/777
#[arg(short, long)]
whep: String,
/// Authentication token to use, will be sent in the HTTP Header as 'Bearer '
#[arg(short, long)]
pub async fn whepfrom(
target_url: String,
set_host: Option<String>,
whep_url: String,
token: Option<String>,
/// Run a command as childprocess
#[arg(long)]
command: Option<String>,
}

#[tokio::main]
async fn main() -> Result<()> {
let args = Args::parse();

utils::set_log(format!(
"whepfrom={},webrtc=error",
match args.verbose {
0 => Level::WARN,
1 => Level::INFO,
2 => Level::DEBUG,
_ => Level::TRACE,
}
));

let input = Url::parse(&args.output).unwrap_or(
) -> Result<()> {
let input = Url::parse(&target_url).unwrap_or(
Url::parse(&format!(
"{}://{}:0/{}",
SCHEME_RTP_SDP,
Ipv4Addr::UNSPECIFIED,
args.output
target_url
))
.unwrap(),
);
info!("=== Received Output: {} ===", args.output);
info!("=== Received Output: {} ===", target_url);

let mut host = match input.host().unwrap() {
Host::Domain(_) | Host::Ipv4(_) => Ipv4Addr::UNSPECIFIED.to_string(),
Host::Ipv6(_) => Ipv6Addr::UNSPECIFIED.to_string(),
};

if let Some(ref h) = args.host {
if let Some(ref h) = set_host {
debug!("=== Specified set host, using {} ===", h);
host.clone_from(h);
}
Expand All @@ -107,10 +76,7 @@ async fn main() -> Result<()> {
let (audio_send, audio_recv) = unbounded_channel::<Vec<u8>>();
let codec_info = Arc::new(tokio::sync::Mutex::new(rtsp::CodecInfo::new()));

let mut client = Client::new(
args.whep.clone(),
Client::get_auth_header_map(args.token.clone()),
);
let mut client = Client::new(whep_url.clone(), Client::get_auth_header_map(token.clone()));

let (peer, answer) = webrtc_start(
&mut client,
Expand Down Expand Up @@ -203,7 +169,7 @@ async fn main() -> Result<()> {
media_info.audio_rtp_server,
));

let child = Arc::new(create_child(args.command)?);
let child = Arc::new(create_child(command)?);
defer!({
if let Some(child) = child.as_ref() {
if let Ok(mut child) = child.lock() {
Expand Down
9 changes: 5 additions & 4 deletions tools/whipinto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
[package]
name = "whipinto"
description = "RTP to WHIP tool"
description = "RTP/RTSP to WHIP tool"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true

[lib]
crate-type = ["lib"]

[dependencies]
cli = { path = "../../libs/cli" }
rtsp = { path = "../../libs/rtsp" }
libwish = { path = "../../libs/libwish" }
signal = { path = "../../libs/signal" }
utils = { path = "../../libs/utils" }

anyhow = { workspace = true }
clap = { workspace = true, features = ["derive"] }
tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true }
webrtc = { workspace = true }
Expand All @@ -26,4 +27,4 @@ url = "2.5.2"
portpicker = "0.1.1"
rtsp-types = "0.1.2"
sdp-types = "0.1"
sdp = "0.6.2"
sdp = "0.6.2"
Loading

0 comments on commit 020e2c4

Please sign in to comment.