Skip to content

Commit

Permalink
fix:whipinto and whepfrom mode sdp file path failed at windows (#247)
Browse files Browse the repository at this point in the history
* add:whipinto/whepfrom audio support

* add whipinto/whepfrom audio support

* fix cargo fmt

* fix cargo fmt

* Remove redundant test file

* fix whipinto single video/audio error

* delete useless files

* add whipinto/whepfrom aduio rtcp

* fix:whipinto and whepfrom mode sdp file path failed at windows

* refactor:apply unit tests and error handling

* refactor(libs/cli): create_child

---------

Co-authored-by: a-wing <[email protected]>
  • Loading branch information
Marsyew and a-wing authored Nov 5, 2024
1 parent d6ebe36 commit db2184b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 32 deletions.
31 changes: 17 additions & 14 deletions libs/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,21 @@ pub fn get_codec_type(codec: &RTCRtpCodecCapability) -> RTPCodecType {
}

pub fn create_child(command: Option<String>) -> Result<Option<Mutex<Child>>> {
let child = if let Some(command) = command {
let mut args = shellwords::split(&command)?;
Some(Mutex::new(
Command::new(args.remove(0))
.args(args)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()?,
))
} else {
None
};
Ok(child)
Ok(match command {
Some(command) => {
#[cfg(windows)]
let command = command.replace('\\', "/");

let mut args = shellwords::split(&command)?;
Some(Mutex::new(
Command::new(args.remove(0))
.args(args)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()?,
))
}
None => None,
})
}
31 changes: 22 additions & 9 deletions livetwo/src/whep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use sdp::{description::media::RangedPort, SessionDescription};
use std::{
fs::File,
io::{Cursor, Write},
path::Path,
sync::Arc,
time::Duration,
};
Expand Down Expand Up @@ -58,12 +59,13 @@ pub async fn from(
);
info!("=== Received Output: {} ===", target_url);

let mut host = match input
.host()
.unwrap_or_else(|| panic!("Invalid host for {}", input))
{
Host::Domain(_) | Host::Ipv4(_) => Ipv4Addr::UNSPECIFIED.to_string(),
Host::Ipv6(_) => Ipv6Addr::UNSPECIFIED.to_string(),
let mut host = match input.host() {
Some(Host::Domain(_)) | Some(Host::Ipv4(_)) => Ipv4Addr::UNSPECIFIED.to_string(),
Some(Host::Ipv6(_)) => Ipv6Addr::UNSPECIFIED.to_string(),
None => {
error!("Invalid host for {}, using default.", input);
Ipv4Addr::UNSPECIFIED.to_string()
}
};

if let Some(ref h) = set_host {
Expand Down Expand Up @@ -134,6 +136,12 @@ pub async fn from(

let mut reader = Cursor::new(filtered_sdp.as_bytes());
let mut session = SessionDescription::unmarshal(&mut reader).unwrap();
host = session
.clone()
.connection_information
.and_then(|conn_info| conn_info.address)
.map(|address| address.to_string())
.unwrap_or(Ipv4Addr::LOCALHOST.to_string());
for media in &mut session.media_descriptions {
if media.media_name.media == "video" {
if let Some(port) = media_info.video_rtp_client {
Expand All @@ -152,9 +160,14 @@ pub async fn from(
}
}
let sdp = session.marshal();
let file_path = input.path().strip_prefix('/').unwrap();
info!("SDP written to {}", file_path);
let mut file = File::create(file_path)?;

let file_path = Path::new(&target_url);
info!("SDP written to {:?}", file_path);
let mut file = File::options()
.write(true)
.create(true)
.truncate(true)
.open(file_path)?;
file.write_all(sdp.as_bytes())?;
}
debug!("media info : {:?}", media_info);
Expand Down
20 changes: 13 additions & 7 deletions livetwo/src/whip.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fs;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::path::Path;
use std::{sync::Arc, time::Duration, vec};

use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -53,12 +54,13 @@ pub async fn into(
);
info!("=== Received Input: {} ===", input);

let mut host = match input
.host()
.unwrap_or_else(|| panic!("Invalid host for {}", input))
{
Host::Domain(_) | Host::Ipv4(_) => Ipv4Addr::UNSPECIFIED.to_string(),
Host::Ipv6(_) => Ipv6Addr::UNSPECIFIED.to_string(),
let mut host = match input.host() {
Some(Host::Domain(_)) | Some(Host::Ipv4(_)) => Ipv4Addr::UNSPECIFIED.to_string(),
Some(Host::Ipv6(_)) => Ipv6Addr::UNSPECIFIED.to_string(),
None => {
eprintln!("Invalid host for {}, using default.", input);
Ipv4Addr::UNSPECIFIED.to_string()
}
};

if let Some(ref h) = set_host {
Expand Down Expand Up @@ -124,7 +126,11 @@ pub async fn into(
};
} else {
tokio::time::sleep(Duration::from_secs(1)).await;
let sdp = sdp_types::Session::parse(&fs::read(&target_url).unwrap()).unwrap();
let path = Path::new(&target_url);
let sdp = sdp_types::Session::parse(&fs::read(path).unwrap()).unwrap();
if let Some(connection_info) = &sdp.connection {
host.clone_from(&connection_info.connection_address);
}
let video_track = sdp.medias.iter().find(|md| md.media == "video");
let audio_track = sdp.medias.iter().find(|md| md.media == "audio");

Expand Down
2 changes: 0 additions & 2 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ async fn test_liveion_stream_create() {
assert_eq!(1, body.len());
}

#[cfg(not(windows))]
#[tokio::test]
async fn test_liveion_stream_connect() {
let cfg = liveion::config::Config::default();
Expand Down Expand Up @@ -208,7 +207,6 @@ a=rtpmap:96 VP8/90000
assert!(result.is_some());
}

#[cfg(not(windows))]
#[tokio::test]
async fn test_liveion_stream_ffmpeg() {
let cfg = liveion::config::Config::default();
Expand Down

0 comments on commit db2184b

Please sign in to comment.