Git Product home page Git Product logo

upnp-client-rs's Introduction

UPnP Client

License: MIT

This is a UPNP client library for Rust.

Usage

Add this to your Cargo.toml:

[dependencies]
upnp-client = "0.1"

Example

This example will print out all the devices found on the network.

use colored_json::prelude::*;
use futures_util::StreamExt;

use crate::discovery::discover_pnp_locations;

mod discovery;
mod types;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let devices = discover_pnp_locations().await?;
    tokio::pin!(devices);

    while let Some(device) = devices.next().await {
        let json = serde_json::to_string_pretty(&device)?;
        println!("{}", json.to_colored_json_auto()?);
    }

    Ok(())
}

Output:

{
  "device_type": "urn:schemas-upnp-org:device:MediaRenderer:1",
  "friendly_name": "Kodi (MacBook-Pro-de-Tsiry-4.local)",
  "location": "http://192.168.8.101:1825/",
  "manufacturer": "XBMC Foundation",
  "manufacturer_url": "http://kodi.tv/",
  "model_description": "Kodi - Media Renderer",
  "model_name": "Kodi",
  "model_number": "18.4 Git:20190831-3ade758ceb",
  "services": [
    {
      "control_url": "/AVTransport/d599320b-2d3b-e0d7-3224-dc1c4b074dae/control.xml",
      "event_sub_url": "/AVTransport/d599320b-2d3b-e0d7-3224-dc1c4b074dae/event.xml",
      "scpd_url": "/AVTransport/d599320b-2d3b-e0d7-3224-dc1c4b074dae/scpd.xml",
      "service_id": "urn:upnp-org:serviceId:AVTransport",
      "service_type": "urn:schemas-upnp-org:service:AVTransport:1"
    },
    {
      "control_url": "/ConnectionManager/d599320b-2d3b-e0d7-3224-dc1c4b074dae/control.xml",
      "event_sub_url": "/ConnectionManager/d599320b-2d3b-e0d7-3224-dc1c4b074dae/event.xml",
      "scpd_url": "/ConnectionManager/d599320b-2d3b-e0d7-3224-dc1c4b074dae/scpd.xml",
      "service_id": "urn:upnp-org:serviceId:ConnectionManager",
      "service_type": "urn:schemas-upnp-org:service:ConnectionManager:1"
    },
    {
      "control_url": "/RenderingControl/d599320b-2d3b-e0d7-3224-dc1c4b074dae/control.xml",
      "event_sub_url": "/RenderingControl/d599320b-2d3b-e0d7-3224-dc1c4b074dae/event.xml",
      "scpd_url": "/RenderingControl/d599320b-2d3b-e0d7-3224-dc1c4b074dae/scpd.xml",
      "service_id": "urn:upnp-org:serviceId:RenderingControl",
      "service_type": "urn:schemas-upnp-org:service:RenderingControl:1"
    }
  ]
}

Streaming

use futures_util::StreamExt;
use upnp_client::{
    device_client::DeviceClient,
    discovery::discover_pnp_locations,
    media_renderer::MediaRendererClient,
    types::{Device, LoadOptions, Metadata, ObjectClass},
};

const KODI_MEDIA_RENDERER: &str = "Kodi - Media Renderer";

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let devices = discover_pnp_locations().await?;
    tokio::pin!(devices);

    let mut kodi_device: Option<Device> = None;
    while let Some(device) = devices.next().await {
        // Select the first Kodi device found
        if device.model_description == Some(KODI_MEDIA_RENDERER.to_string()) {
            kodi_device = Some(device);
            break;
        }
    }

    let kodi_device = kodi_device.unwrap();
    let device_client = DeviceClient::new(&kodi_device.location)?.connect().await?;
    let media_renderer = MediaRendererClient::new(device_client);

    let options = LoadOptions {
        dlna_features: Some(
            "DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000"
                .to_string(),
        ),
        content_type: Some("video/mp4".to_string()),
        metadata: Some(Metadata {
            title: "Big Buck Bunny".to_string(),
            ..Default::default()
        }),
        autoplay: true,
        object_class: Some(ObjectClass::Video),
        ..Default::default()
    };

    let media_url =
        "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";

    media_renderer.load(media_url, options).await?;

    Ok(())
}

See the examples directory for more examples.

Features

  • Discover devices
  • Control Media Renderer device (Load, Play, Pause, Stop, Seek, etc.)
  • Browse Media Server device

References

License

MIT

upnp-client-rs's People

Contributors

seijikun avatar tsirysndr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

seijikun takov751

upnp-client-rs's Issues

Browse directories

Hello there great project!

I am trying to browse files on a DLNA service and given this snippet

...
let device_client = DeviceClient::new(&device.location).expect("REASON").connect().await;
    let media_server_client = MediaServerClient::new(device_client.unwrap());
    let results = media_server_client
        .browse("1$4", "BrowseDirectChildren")
        .await.unwrap();
        println!("{:#?}", results);
...

When i browse 0,64,1,2,3 it does answers back as it should be, but when $ sign is in the id it's just won't. Am I missing something?

Parser panics when device has no serviceList

Thank you very much for this awesome library!
I was searching for a lib that I can use to send media urls to my Kodi instance...
Opened the crate page of upnp-client, scrolled down and was very surprised to see a code example of exactly what I need!

Unfortunately, we seem to have a device in our network that doesn't have a serviceList entry, which sometimes makes the parser crash in line 100.

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /home/seiji/.cargo/registry/src/github.com-1ecc6299db9ec823/upnp-client-0.1.7/src/parser.rs:100:33
stack backtrace:
   0: rust_begin_unwind
             at /rustc/f15f0ea73972786e426732c5b92ba9a904b866c4/library/std/src/panicking.rs:579:5
   1: core::panicking::panic_fmt
             at /rustc/f15f0ea73972786e426732c5b92ba9a904b866c4/library/core/src/panicking.rs:64:14
   2: core::panicking::panic
             at /rustc/f15f0ea73972786e426732c5b92ba9a904b866c4/library/core/src/panicking.rs:114:5
   3: core::option::Option<T>::unwrap
             at /rustc/f15f0ea73972786e426732c5b92ba9a904b866c4/library/core/src/option.rs:942:21
   4: upnp_client::parser::parse_services::{{closure}}
             at /home/seiji/.cargo/registry/src/github.com-1ecc6299db9ec823/upnp-client-0.1.7/src/parser.rs:100:20
   5: upnp_client::parser::parse_location::{{closure}}
             at /home/seiji/.cargo/registry/src/github.com-1ecc6299db9ec823/upnp-client-0.1.7/src/parser.rs:70:59
   6: upnp_client::discovery::discover_pnp_locations::{{closure}}
             at /home/seiji/.cargo/registry/src/github.com-1ecc6299db9ec823/upnp-client-0.1.7/src/discovery.rs:53:41
   7: <async_stream::async_stream::AsyncStream<T,U> as futures_core::stream::Stream>::poll_next
             at /home/seiji/.cargo/registry/src/github.com-1ecc6299db9ec823/async-stream-0.3.5/src/async_stream.rs:56:13
   8: <core::pin::Pin<P> as futures_core::stream::Stream>::poll_next
             at /home/seiji/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-core-0.3.28/src/stream.rs:120:9
   9: futures_util::stream::stream::StreamExt::poll_next_unpin
             at /home/seiji/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.28/src/stream/stream/mod.rs:1632:9
  10: <futures_util::stream::stream::next::Next<St> as core::future::future::Future>::poll
             at /home/seiji/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.28/src/stream/stream/next.rs:32:9
  11: my_upnp_test::main::{{closure}}
             at ./src/main.rs:17:44
   ...

This is the value of xml_root when it crashes:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
	<specVersion>
		<major>1</major>
		<minor>0</minor>
	</specVersion>
	<device>
		<deviceType>urn:schemas-upnp-org:device:WLANAccessPointDevice:1</deviceType>
		<friendlyName>NETGEAR47B64C</friendlyName>
		<manufacturer>NETGEAR</manufacturer>
		<manufacturerURL>https://www.netgear.com</manufacturerURL>
		<modelDescription>NETGEAR Dual Band Access Point</modelDescription>
		<modelName>WAX214</modelName>
		<modelNumber>WAX214</modelNumber>
		<modelURL>https://www.netgear.com</modelURL>
		<firmwareVersion>2.1.1.3</firmwareVersion>
		<insightMode>0</insightMode>
		<serialNumber>XXXXXXXXX</serialNumber>
		<UDN>uuid:919ba4ec-ec93-490f-b0e3-80CC9C47B64C</UDN>
		<presentationURL>http://xxxxxx/</presentationURL>
	</device>
</root>

Device discovery crashes upon invalid device response

At the moment, device discovery seems to tightly expect devices to follow the standard.
Though I've seen stuff like:

  • Random sections of the xml missing
  • The device just responding with nothing (xml is just empty string)

At the moment, these things crash the application - mostly in unwrap() calls.
Would it be possible to have the library catch and ignore these cases?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.