Implementing of Cling to search and play media on a UPnP renderer
Posted By : Shubham Saxena | 24-Dec-2014
In the previous blog, I briefly described about the cling and its configuration. Here I am pasting a part of my code that I have implemented to search a cling UPnP renderer and and used its AVTransport service to play a video on it. For the purpose of testing, you can take any renderer device which is WiFi enabled like any Smart TV or SoftDMA(although it is slow).
It starts with making a thread.
Thread clientThread = new Thread(new notMain());
clientThread.setDaemon(false);
clientThread.start();
Search Devices:
In run function, I called the search function with the type of service i want to search for. For instance, here I am sreaching for "AVTransport" service that is a service provided by a renderer.
public void run() {
System.out.println("run method started");
try {
upnpService = new UpnpServiceImpl(new MyUpnpServiceConfiguration());
upnpService.getRegistry().addListener(this);
UDAServiceType udaType = new UDAServiceType("AVTransport");
upnpService.getControlPoint().search(new UDAServiceTypeHeader(udaType));
System.out.println("new STAllHeader() "+ new UDAServiceTypeHeader(udaType));
System.out.println("upnpService " + upnpService);
} catch (Exception ex) {
System.err.println("Exception occured: " + ex);
System.exit(1);
}
}
Call the function that play video:
Then to play a video on any of the searched renderer, you can override the deviceAdded function. Here in the code below, I get the device UUID which id the universal id. Then I checked the deviceUuid(the code is not included but deviceUuid is just a string type variable) if it get matched with the searched devices then from all the services of that renderer device, AVTransport service is set to be executed with the setContent function call.
public void deviceAdded(Registry registry, Device device) {
super.deviceAdded(registry, device);
System.out.println("test "+device.getIdentity().getUdn().toString().contains(deviceUuid));
if (device.getIdentity().getUdn().toString().contains(deviceUuid)) {
Service[] services = device.findServices();
for (Service service : services) {
if ("AVTransport".equals(service.getServiceType().getType())) {
System.out.println("device.getDisplayString(): "
+ device.getDisplayString());
setContent(service);
}
}
}
}
Play function:
In the setContent function, I have provided the service that we have passed in the last function, the videoURL and the metaData for the video to be played in the AVTransport class object.
Note: The metaData that I have mentioned is very much compatible with the devices like smart TV.
public void setContent(final Service service) {
System.out.println("setContent started");
ActionCallback setAVTransportURIAction = new SetAVTransportURI(
service,
videoURL,
"<DIDL-Lite xmlns='urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/' xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:upnp='urn:schemas-upnp-org:metadata-1-0/upnp/' xmlns:dlna='urn:schemas-dlna-org:metadata-1-0/'><item id='sample' parentID='0' restricted='0'><dc:title>"+videoTitle+"</dc:title><dc:creator>Mohit</dc:creator><upnp:genre>No Genre</upnp:genre><res protocolInfo='http-get:*:video/mpeg:DLNA.ORG_FLAGS=01700000000000000000000000000000;DLNA.ORG_CI=0;DLNA.ORG_OP=01'>"+videoURL+"</res><upnp:class>object.item.videoItem</upnp:class></item></DIDL-Lite>") {
@Override
public void failure(ActionInvocation invocation,
UpnpResponse operation, String defaultMsg) {
System.out.println(" " + defaultMsg);
}
@Override
public void success(ActionInvocation invocation) {
ActionCallback playAction = getPlayAction(service);
playAction.setControlPoint(upnpService.getControlPoint());
playAction.run();
}
};
setAVTransportURIAction.setControlPoint(upnpService.getControlPoint());
setAVTransportURIAction.run();
}
Hope the description would help you in some way else 2 user manuals can help you in implementing the cling are:
1: https://github.com/4thline
2: https://github.com/4thline/cling
Thanks!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shubham Saxena
Shubham is a Groovy/Grails Developer and has good exposure on FFMPEG. He has worked on development of various SaaS applications.