upnp_play – Play a file on a mediaplayer
This is a very simple a straight forward UPNP play function. It searches a file on a mediaserver and plays it on the mediaplayer. It does not include search of mediaplayer/server URLs as the UPNP discovery take some time. Use the script below to find the correct urls.
upnp_play.php - Play media on UPNP device
upnp_play.php
<?php
$mediaserver_url = 'http://192.168.1.1:49200/ctl/ContentDir';
$mediaplayer_url = 'http://192.168.1.22:9020/AVTransport/Control';
// From http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/
function do_post_request($url, $data, $optional_headers = null)
{
$params = <a href="http://www.php.net/array">array</a>('http' => <a href="http://www.php.net/array">array</a>( 'method' => 'POST', 'content' => $data));
if ($optional_headers !== null) { $params['http']['header'] = $optional_headers; }
$ctx = <a href="http://www.php.net/stream_context_create">stream_context_create</a>($params);
$fp = <a href="http://www.php.net/fopen">fopen</a>($url, 'rb', false, $ctx);
//$fp = @fopen($url, 'rb', false, $ctx);
//if (!$fp) { throw new Exception("Problem with $url, $php_errormsg");}
$response = <a href="http://www.php.net/stream_get_contents">stream_get_contents</a>($fp);
//$response = @stream_get_contents($fp);
//if ($response === false) { throw new Exception("Problem reading data from $url, $php_errormsg"); }
return $response;
}
function upnp_play_film($mediaserver_url, $mediaplayer_url, $film)
{
// Get nasename to be searched
$info = <a href="http://www.php.net/pathinfo">pathinfo</a>($film);
$film = @<a href="http://www.php.net/basename">basename</a>($film,'.'.$info['extension']);
// Search on the mediaserver
$upnp_search = '<?xml version="1.0" encoding="utf-8"?><s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><ns0:Search xmlns:ns0="urn:schemas-upnp-org:service:ContentDirectory:1"><ContainerID>0</ContainerID><SearchCriteria>dc:title="%s"</SearchCriteria><Filter></Filter><StartingIndex>0</StartingIndex><RequestedCount>0</RequestedCount><SortCriteria /></ns0:Search></s:Body></s:Envelope>';
$upnp_search_headers = 'SOAPACTION: "urn:schemas-upnp-org:service:ContentDirectory:1#Search"ncontent-type: text/xml ;charset="utf-8"nconnection: close';
$ret = do_post_request($mediaserver_url, <a href="http://www.php.net/sprintf">sprintf</a>($upnp_search,<a href="http://www.php.net/htmlspecialchars">htmlspecialchars</a>($film)),$upnp_search_headers);
// Extract meta and url
$r = <a href="http://www.php.net/simplexml_load_string">simplexml_load_string</a>($ret);
$res = $r->xpath("//Result");
$r = <a href="http://www.php.net/simplexml_load_string">simplexml_load_string</a>($res[0]);
$film_meta = <a href="http://www.php.net/ltrim">ltrim</a>(<a href="http://www.php.net/str_replace">str_replace</a>('<?xml version="1.0"?>','',$r->asXML()));
$film_url = $r->item->res;
// Play on the mediaplayer
if (<a href="http://www.php.net/strlen">strlen</a>($film_url) > 4)
{
// SetAVTransportURI
$upnp_setav = '<?xml version="1.0" encoding="utf-8"?><s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><ns0:SetAVTransportURI xmlns:ns0="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><CurrentURI>%s</CurrentURI><CurrentURIMetaData>%s</CurrentURIMetaData></ns0:SetAVTransportURI></s:Body></s:Envelope>';
$upnp_setav_headers = 'SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI"ncontent-type: text/xml ;charset="utf-8"nconnection: close';
$ret = do_post_request($mediaplayer_url, <a href="http://www.php.net/sprintf">sprintf</a>($upnp_setav,<a href="http://www.php.net/htmlspecialchars">htmlspecialchars</a>($film_url),<a href="http://www.php.net/htmlspecialchars">htmlspecialchars</a>($film_meta)),$upnp_setav_headers);
// Play
$upnp_play = '<?xml version="1.0" encoding="utf-8"?><s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><ns0:Play xmlns:ns0="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><Speed>1</Speed></ns0:Play></s:Body></s:Envelope>';
$upnp_play_headers = 'SOAPACTION: "urn:schemas-upnp-org:service:AVTransport:1#Play"ncontent-type: text/xml ;charset="utf-8"nconnection: close';
$ret = do_post_request($mediaplayer_url, $upnp_play,$upnp_play_headers);
}
return $film_url;
}
$film = "Your film"; // Full name, without path, and without extension
print upnp_play_film($mediaserver_url, $mediaplayer_url, $film);
?>
upnp_list – Discover UPNP Devices
Discover UPNP devices on your network (and small tutorial for gupnp).
upnp_list.php
<table>
<?php
$aimRenderer['name'] = "BboxTV"; // urn:schemas-upnp-org:service:AVTransport:1
$aimRenderer['udn'] = "uuid:56076f6e-6b79-4d65-6435-e8be81811ddd";
$aimRenderer['Service'] = 0;
$aimServer['name'] = "Server"; // urn:schemas-upnp-org:service:ContentDirectory:1
$aimServer['udn'] = "uuid:4d696e69-444c-164e-9d41-001e8c6f8837";
$aimServer['Service'] = 0;
function device_proxy_available_cb($proxy, $arg)
{
global $context,$cp,$aimRenderer, $aimServer;
$info = gupnp_device_info_get($proxy);
<a href="http://www.php.net/printf">printf</a>("<tr>");
<a href="http://www.php.net/printf">printf</a>("<td>%s</td>", $info['device_type']);
<a href="http://www.php.net/printf">printf</a>("<td><a href='%s'>%s</a></td>", $info['location'],$info['friendly_name']);
<a href="http://www.php.net/printf">printf</a>("<td>%s</td>", $info['udn']);
<a href="http://www.php.net/printf">printf</a>("</tr>n");
<a href="http://www.php.net/flush">flush</a>();
if ($aimRenderer == $info['friendly_name'])
{
<a href="http://www.php.net/printf">printf</a>("Found Renderer !");
}
if ($aimServer == $info['friendly_name'])
{
<a href="http://www.php.net/printf">printf</a>("Found Server !");
$aimServerService = gupnp_device_info_get_service($proxy,"urn:schemas-upnp-org:service:ContentDirectory:1");
}
if (($aimRendererService) && ($aimServerService))
{
<a href="http://www.php.net/printf">printf</a>("Stopn");
gupnp_control_point_browse_stop($cp);
}
}
function service_proxy_available_cb($proxy, $arg)
{
global $context,$cp,$aimRenderer, $aimServer;
$info = gupnp_service_info_get($proxy);
//var_dump($info);
<a href="http://www.php.net/printf">printf</a>("<tr>");
<a href="http://www.php.net/printf">printf</a>("<td>%s</td>", $info['service_type']);
<a href="http://www.php.net/printf">printf</a>("<td><a href='%s'>%s</a></td>", $info['location'],$info['friendly_name']);
<a href="http://www.php.net/printf">printf</a>("<td>%s</td>", $info['udn']);
<a href="http://www.php.net/printf">printf</a>("</tr>n");
<a href="http://www.php.net/flush">flush</a>();
if (($info['udn'] == $aimRenderer['udn']) && ($info['service_type'] == "urn:schemas-upnp-org:service:AVTransport:1"))
{
<a href="http://www.php.net/printf">printf</a>("Found Renderer !");
$aimRenderer['service'] = $proxy;
}
if (($info['udn'] == $aimServer['udn']) && ($info['service_type'] == "urn:schemas-upnp-org:service:ContentDirectory:1"))
{
<a href="http://www.php.net/printf">printf</a>("Found Server !");
$aimServer['service'] = $proxy;
}
if (($aimRenderer['service']) && ($aimServer['service']))
{
<a href="http://www.php.net/printf">printf</a>("Stopn");
gupnp_control_point_browse_stop($cp);
}
}
$context = gupnp_context_new();
if (!$context) {
<a href="http://www.php.net/printf">printf</a>("Error creating the GUPnP contextn");
<a href="http://www.php.net/exit">exit</a>(-1);
}
$cp = gupnp_control_point_new($context, "ssdp:all");
gupnp_control_point_callback_set($cp, GUPNP_SIGNAL_DEVICE_PROXY_AVAILABLE, 'device_proxy_available_cb');
gupnp_control_point_callback_set($cp, GUPNP_SIGNAL_SERVICE_PROXY_AVAILABLE, 'service_proxy_available_cb');
gupnp_context_timeout_add($context, 5000, "gupnp_control_point_browse_stop", $cp);
gupnp_control_point_browse_start($cp);
?>
</table>
Fini !
Comments are powered by Github. You must authenticate to GitHub before commenting. Create an account if you haven't one, it only takes 1 minute!
Les commentaires sont hébergés sur GitHub. Il vous faudra vous authentifier sur GitHub pour pouvoir commenter. Si vous n'avez pas de compte, une minute suffit à en créer un !