Skip to content

Commit

Permalink
Merge pull request #14 from gabrielmagno/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
gabrielmagno authored Aug 20, 2023
2 parents 503a2e8 + c21ce4c commit 638a40c
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "crab-dlna"
version = "0.1.1"
version = "0.1.2"
authors = ["Gabriel Magno <[email protected]>"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/gabrielmagno/crab-dlna"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ use crab_dlna::{
Render,
RenderSpec,
MediaStreamingServer,
STREAMING_PORT_DEFAULT,
get_local_ip,
infer_subtitle_from_video,
Error,
Expand All @@ -114,12 +115,14 @@ async fn main() -> Result<(), Error> {
let render_spec = RenderSpec::Query(discover_timeout_secs, "Kodi".to_string());
let render = Render::new(render_spec).await?;
let host_ip = get_local_ip().await?;
let host_port = STREAMING_PORT_DEFAULT;
let video_path = PathBuf::from("/home/crab/Videos/my_video.mp4");
let inferred_subtitle_path = infer_subtitle_from_video(&video_path);
let media_streaming_server = MediaStreamingServer::new(
&video_path,
&inferred_subtitle_path,
&host_ip,
&host_port,
)?;
play(render, media_streaming_server).await
}
Expand Down
4 changes: 3 additions & 1 deletion examples/play.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crab_dlna::{
get_local_ip, infer_subtitle_from_video, play, Error, MediaStreamingServer, Render, RenderSpec,
STREAMING_PORT_DEFAULT,
};
use std::path::PathBuf;

Expand All @@ -9,9 +10,10 @@ async fn main() -> Result<(), Error> {
let render_spec = RenderSpec::Query(discover_timeout_secs, "Kodi".to_string());
let render = Render::new(render_spec).await?;
let host_ip = get_local_ip().await?;
let host_port = STREAMING_PORT_DEFAULT;
let video_path = PathBuf::from("/home/crab/Videos/my_video.mp4");
let inferred_subtitle_path = infer_subtitle_from_video(&video_path);
let media_streaming_server =
MediaStreamingServer::new(&video_path, &inferred_subtitle_path, &host_ip)?;
MediaStreamingServer::new(&video_path, &inferred_subtitle_path, &host_ip, &host_port)?;
play(render, media_streaming_server).await
}
11 changes: 9 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::{
devices::{Render, RenderSpec},
dlna,
error::Result,
streaming::{get_local_ip, infer_subtitle_from_video, MediaStreamingServer},
streaming::{
get_local_ip, infer_subtitle_from_video, MediaStreamingServer, STREAMING_PORT_DEFAULT,
},
};
use clap::{Args, Parser, Subcommand};
use log::info;
Expand Down Expand Up @@ -83,6 +85,10 @@ struct Play {
#[clap(short = 'H', long = "host")]
host: Option<String>,

/// The port to be used to host and serve the files
#[clap(short = 'P', long = "port", default_value_t=STREAMING_PORT_DEFAULT)]
port: u32,

/// Specify the device where to play through a query (scan devices before playing)
#[clap(short = 'q', long = "query-device")]
device_query: Option<String>,
Expand Down Expand Up @@ -127,6 +133,7 @@ impl Play {
info!("Building media streaming server");
let local_host_ip = get_local_ip().await?;
let host_ip = self.host.as_ref().unwrap_or(&local_host_ip);
let host_port = self.port;

let subtitle = match &self.no_subtitle {
false => self
Expand All @@ -136,7 +143,7 @@ impl Play {
true => None,
};

MediaStreamingServer::new(&self.file_video, &subtitle, host_ip)
MediaStreamingServer::new(&self.file_video, &subtitle, host_ip, &host_port)
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ impl Render {

let mut renders = Vec::new();

while let Some(device) = devices
.try_next()
.await
.map_err(Error::DevicesNextDeviceError)?
{
debug!("Found device: {}", format_device!(device));
match Self::from_device(device).await {
Some(render) => {
renders.push(render);
while let Some(result) = devices.next().await {
match result {
Ok(device) => {
debug!("Found device: {}", format_device!(device));
if let Some(render) = Self::from_device(device).await {
renders.push(render);
};
}
Err(e) => {
debug!("A device returned error while discovering it: {}", e);
}
None => {}
};
}
}

Ok(renders)
Expand Down
4 changes: 0 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use std::fmt;
pub enum Error {
/// An error occurred while discovering devices
DevicesDiscoverFail(rupnp::Error),
/// An error occurred while iterating over discovered devices
DevicesNextDeviceError(rupnp::Error),
/// An error occurred while parsing a device URL
DevicesUrlParseError(String),
/// An error occurred while parsing and creating a device
Expand All @@ -34,7 +32,6 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::DevicesDiscoverFail(err) => write!(f, "Failed to discover devices: {}", err),
Error::DevicesNextDeviceError(err) => write!(f, "Failed to get next device: {}", err),
Error::DevicesUrlParseError(url) => write!(f, "Failed to parse URL '{}'", url),
Error::DevicesCreateError(url, err) => write!(
f,
Expand Down Expand Up @@ -77,7 +74,6 @@ impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::DevicesDiscoverFail(err) => Some(err),
Error::DevicesNextDeviceError(err) => Some(err),
Error::DevicesCreateError(_, err) => Some(err),
Error::StreamingRemoteRenderConnectFail(_, err) => Some(err),
Error::StreamingIdentifyLocalAddressError(err) => Some(err),
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use crab_dlna::{
Render,
RenderSpec,
MediaStreamingServer,
STREAMING_PORT_DEFAULT,
get_local_ip,
infer_subtitle_from_video,
Error,
Expand All @@ -53,12 +54,14 @@ async fn main() -> Result<(), Error> {
let render_spec = RenderSpec::Query(discover_timeout_secs, "Kodi".to_string());
let render = Render::new(render_spec).await?;
let host_ip = get_local_ip().await?;
let host_port = STREAMING_PORT_DEFAULT;
let video_path = PathBuf::from("/home/crab/Videos/my_video.mp4");
let inferred_subtitle_path = infer_subtitle_from_video(&video_path);
let media_streaming_server = MediaStreamingServer::new(
&video_path,
&inferred_subtitle_path,
&host_ip,
&host_port,
)?;
play(render, media_streaming_server).await
}
Expand Down Expand Up @@ -97,4 +100,6 @@ mod error;
pub use devices::{Render, RenderSpec};
pub use dlna::play;
pub use error::Error;
pub use streaming::{get_local_ip, infer_subtitle_from_video, MediaStreamingServer};
pub use streaming::{
get_local_ip, infer_subtitle_from_video, MediaStreamingServer, STREAMING_PORT_DEFAULT,
};
6 changes: 4 additions & 2 deletions src/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use slugify::slugify;
use std::net::SocketAddr;
use warp::Filter;

const STREAMING_PORT: u32 = 9000;
/// Default port to use for the streaming server
pub const STREAMING_PORT_DEFAULT: u32 = 9000;

/// A media file to stream
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -41,8 +42,9 @@ impl MediaStreamingServer {
video_path: &std::path::Path,
subtitle_path: &Option<std::path::PathBuf>,
host_ip: &String,
host_port: &u32,
) -> Result<Self> {
let server_addr_str = format!("{}:{}", host_ip, STREAMING_PORT);
let server_addr_str = format!("{}:{}", host_ip, host_port);
let server_addr: SocketAddr = server_addr_str
.parse()
.map_err(|_| Error::StreamingHostParseError(server_addr_str))?;
Expand Down

0 comments on commit 638a40c

Please sign in to comment.