crab_dlna/
lib.rs

1#![warn(missing_docs)]
2
3/*!
4crab-dlna is a minimal UPnP/DLNA media streamer.
5
6It allows you to play a local video file in your TV (or any other DLNA compatible device).
7
8This crate provides both a library and a command line tool.
9
10# Features
11 - Searching available DLNA devices in the local network
12 - Streaming audio
13 - Streaming video, with subtitle support
14
15# Example: discover and list devices
16
17crab-dlna provides a function to discover a list devices in the network.
18
19```rust
20use crab_dlna::Render;
21
22#[tokio::main]
23async fn main() {
24    let discover_timeout_secs = 5;
25    let renders_discovered = Render::discover(discover_timeout_secs).await.unwrap();
26    for render in renders_discovered {
27        println!("{}", render);
28    }
29}
30```
31
32# Example: play a video in a render
33
34We can specify a DLNA device render trough a query string,
35and then play a certain video in it, automatically detecting
36the subtitle file.
37
38```rust,no_run
39use std::path::PathBuf;
40use crab_dlna::{
41    Render,
42    RenderSpec,
43    MediaStreamingServer,
44    STREAMING_PORT_DEFAULT,
45    get_local_ip,
46    infer_subtitle_from_video,
47    Error,
48    play,
49};
50
51#[tokio::main]
52async fn main() -> Result<(), Error> {
53    let discover_timeout_secs = 5;
54    let render_spec = RenderSpec::Query(discover_timeout_secs, "Kodi".to_string());
55    let render = Render::new(render_spec).await?;
56    let host_ip = get_local_ip().await?;
57    let host_port = STREAMING_PORT_DEFAULT;
58    let video_path = PathBuf::from("/home/crab/Videos/my_video.mp4");
59    let inferred_subtitle_path = infer_subtitle_from_video(&video_path);
60    let media_streaming_server = MediaStreamingServer::new(
61        &video_path,
62        &inferred_subtitle_path,
63        &host_ip,
64        &host_port,
65    )?;
66    play(render, media_streaming_server).await
67}
68```
69
70# Technical Details
71
72crab-dlna is basically a one-file DLNA MediaServer and a self DLNA MediaController.
73
74How does `list` work?
751. Issue an SSDP M-Search broadcast message in the network
762. Capture the responses and register the devices
773. Filter only devices that provide [UPnP's AVTransport service](http://www.upnp.org/specs/av/UPnP-av-AVTransport-v3-Service-20101231.pdf)
78
79How does `play` work?
801. Setup an HTTP server to provide the media files to be streamed (including subtitles)
812. Send a `SetAVTransportURI` message to the device, specifying the HTTP URLs of the media files
823. Send a `Play` message to the device
83*/
84
85/// Discovery of render devices in the network
86mod devices;
87
88/// Streaming of media files
89mod streaming;
90
91/// Handling of the DLNA protocol
92mod dlna;
93
94/// Command line interface
95pub mod cli;
96
97/// Definition of the errors
98mod error;
99
100pub use devices::{Render, RenderSpec};
101pub use dlna::play;
102pub use error::Error;
103pub use streaming::{
104    get_local_ip, infer_subtitle_from_video, MediaStreamingServer, STREAMING_PORT_DEFAULT,
105};