Javascript: DLNA client
Asked Answered
F

3

6

I am planning to write a DLNA (upnp) client in javascript. I would like to know if this is possible in the first place or not.

If yes, then where can do I start? What do I need to know to begin? Links to any documentation and tutorials will be highly appreciated. I've tried Googling, but didn't come across much helpful content.

I just need a prod in the right direction.

Thanks! :)

Fanjet answered 9/9, 2012 at 11:17 Comment(2)
First you need to research what DLNA is, then break it down to it's individual requirements, then see if it's possible with JavaScript. My guess is 'no.' But I'm unsure.Hon
Likely accessible via activeX which is not JavaScriptBhang
C
13

The best place for you to start is the UPnP device architecture doc in the docs bundle from the UPnP forum. This splits the protocol into a number of areas:

  • Discovery. This requires the ability to send multicast UDP packets and receive unicast UDP. You cannot do this from JavaScript so would require a native helper app to cover this portion if you want to search a network and offer to control any devices found on it. Alternatively, you can skip this section if you somehow already know the address of your target device.
  • Description. Given an address for a device, fetch (http get) an xml overview of its capabilities. You can do this easily from JavaScript.
  • Control. Instruct a given device to carry out given actions. Implemented using http post and soap. You can do this easily from JavaScript.
  • Eventing. A mechanism to be informed of changes in device state. Requires that you run a tcp server so cannot be done from JavaScript. Fortunately, this is often optional as most device services are designed to allow clients to poll state getters as an alternative to eventing. So, you can do this from JavaScript, albeit that your app will be less efficient than a native one.
  • Presentation. Some devices provide a web app allowing their control. This is hosted in a browser so will use JavaScript and is a good example that the sort of control app you want to write is possible.

In summary then, a JavaScript UPnP client is possible only if you can use native code to handle device discovery. If you decide to try this, open source UPnP stacks exist to handle most of the work of discovery for you.

Chema answered 10/9, 2012 at 8:9 Comment(6)
From my personal experience, eventing is better not to rely on for the real world devices (at least media renderers). Eventing is even disabled by UPnP spec for certain values which expectably have a high cadence and/or amount of data, like AVTransport.RelativeTimePosition. But the need for HTTPMU (multicast HTTP over UDP) stands. +1 for comprehensive overview.Defilade
Wow... I cannot thank you enough! This gave me a really great insight to UPnP. Thanks a lot for your detailed explanation. :) I'll be starting work on this project soon. :)Fanjet
I dont think control can occur from Javascript in the Browser. It would be a CORS request and unless the Upnp server is configured to handle this, it would just fail. Through the node terminal however it can easily work.Frustration
@Frustration The browser (or its IP) would be both the domain that requests the UPnP presentation page and that executes POST requests to invoke actions on the UPnP device. Many UPnP devices offer a presentation page that works in exactly this way.Chema
@Chema Because they are configured to Access-Control-Allow-Origin: *. But not all. Not the majority. There are workarounds like starting a server which acts gateway between clients and devices. Do you know how exactly it works?Frustration
Granted, 11 years is an awfully long time in computer years, but sending Multicast and receiving unicast UDP packets is no longer a deal breaker. Was it really impossible in 2012?Skew
F
4

Take a look at Plug.Play.js - A JavaScript API for communicating with Universal Plug and Play (UPnP) Services obtained via the W3C Network Service Discovery API

https://github.com/rexboy7/plug.play.js

And ssdp.js - (Simple Service Discovery Protocol) A Network Service Discovery API implementation based on the W3C Raw Socket API

https://github.com/schien/ssdp.js

And here is a sample implementation of a DLNA client using the above: https://github.com/mozilla-b2g/gaia/tree/master/tv_apps/dlna-player

Furcula answered 16/7, 2015 at 21:44 Comment(0)
C
1

EDIT: Based on Firefox OS.

Looking around about this topic and based on André Fiedler answer I found that the libraries he posted about resides on UDPSocket from MDN webarchive.

In the main page you can see the discovery example:

var SSDP_PORT = 1900;
var SSDP_ADDRESS = "239.255.255.250";
var SSDP_DISCOVER_MX = 2;
var SEARCH_TARGET = "urn:schemas-upnp-org:service:ContentDirectory:1";

var SSDP_DISCOVER_PACKET =
    "M-SEARCH * HTTP/1.1\r\n" +
    "HOST: " + SSDP_ADDRESS + ":" + SSDP_PORT + "\r\n" +
    "MAN: \"ssdp:discover\"\r\n" +
    "MX: " + SSDP_DISCOVER_MX + "\r\n" +
    "ST: " + SEARCH_TARGET + "\r\n" +
    "\r\n";

var searchSocket = new UDPSocket({
    loopback: true
});

searchSocket.joinMulticastGroup(SSDP_ADDRESS);

searchSocket.onmessage = function (e) {

    var msg = String.fromCharCode.apply(null, new Uint8Array(e.data));
    
    console.log(msg);
};

searchSocket.opened.then(function() {
    
    searchSocket.send(SSDP_DISCOVER_PACKET, SSDP_ADDRESS, SSDP_PORT);
    
    setTimeout(function () { searchSocket.close(); }, SSDP_DISCOVER_MX * 1000);
});
Corso answered 2/5, 2016 at 1:0 Comment(1)
current UPDSocket MDN link is broken and it points to this bug: bugzilla.mozilla.org/show_bug.cgi?id=745283Corso

© 2022 - 2024 — McMap. All rights reserved.