document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //============================================================================
  2. // Name        : UPnPClient7.cpp
  3. // Author      : Majik
  4. // Purpose     : Play URL stream on specified Renderer
  5. //============================================================================
  6.  
  7. #include <string.h>
  8. #include <glib-2.0/glib.h>
  9. #include <libgupnp/gupnp-control-point.h>
  10. #include <libgupnp-av/gupnp-av.h>
  11.  
  12. #define MEDIA_RENDERER "urn:schemas-upnp-org:device:MediaRenderer:1"
  13. #define AV_TRANSPORT "urn:schemas-upnp-org:service:AVTransport"
  14.  
  15. static GMainLoop *main_loop;
  16. static GHashTable *renderers;
  17. static GUPnPLastChangeParser *lc_parser;
  18. char *rendererName;
  19. bool rendererFound = false;
  20. char *uri;
  21.  
  22. /* This is our callback method to terminate the main loop
  23.  * after the timeout has expired
  24.  */
  25. static gboolean main_loop_timeout(void *data)
  26. {
  27.         g_main_loop_quit (main_loop);
  28.         if (!rendererFound) {
  29.                 g_print("Renderer %s not found\\n", rendererName);
  30.         }
  31.         return 0;
  32. }
  33.  
  34.  
  35. /*
  36.  * Utility function to play URL on Media Renderer
  37.  */
  38. static void play_stream(const GUPnPDeviceProxy *renderer, const char *uri) {
  39.         GUPnPServiceProxy *av_transport;
  40.         char *friendly_name;
  41.         GError *error;
  42.         error = NULL;
  43.         char metadata[1000];
  44.  
  45.         /* Build metadata, this is very hacky but is good to show raw data */
  46.         char *metadata1 =
  47.                 "&lt;DIDL-Lite xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot;"
  48.                 " xmlns:upnp=&quot;urn:schemas-upnp-org:metadata-1-0/upnp/&quot;"
  49.                 " xmlns:r=&quot;urn:schemas-rinconnetworks-com:metadata-1-0/&quot;"
  50.                 " xmlns=&quot;urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/&quot;&gt;"
  51.                 "&lt;item id=&quot;-1&quot; parentID=&quot;-1&quot; restricted=&quot;true&quot;&gt;"
  52.                 "&lt;res protocolInfo=&quot;http-get:*:audio/mpeg:*&quot; &gt;";
  53.          char *metadata2 = "&lt;/res&gt;"
  54.                 "&lt;dc:title&gt;UPnPClient7 Test&lt;/dc:title&gt;"
  55.                 "&lt;upnp:class&gt;object.item.audioItem.musicTrack&lt;/upnp:class&gt;"
  56.                 "&lt;/item&gt;"
  57.                 "&lt;/DIDL-Lite&gt;";
  58.         /* metadata is metadata1 + uri + metadata2 */
  59.         strcat(metadata, metadata1);
  60.         strcat(metadata, uri);
  61.         strcat(metadata, metadata2);
  62.  
  63.         g_print("Trying to play stream...\\n");
  64.         /* Get AVTransport service for device */
  65.         av_transport = GUPNP_SERVICE_PROXY (gupnp_device_info_get_service(GUPNP_DEVICE_INFO(renderer), AV_TRANSPORT));
  66.         friendly_name = gupnp_device_info_get_friendly_name(GUPNP_DEVICE_INFO(renderer));
  67.  
  68.         if ( gupnp_service_proxy_send_action (av_transport, "SetAVTransportURI", &error,
  69.                                 "InstanceID", G_TYPE_UINT, 0,
  70.                                 "CurrentURI", G_TYPE_STRING, uri,
  71.                                 "CurrentURIMetaData", G_TYPE_STRING, metadata,
  72.                                 NULL,NULL) )
  73.         {
  74.                 if ( !gupnp_service_proxy_send_action (av_transport, "Play", &error,
  75.                                 "InstanceID", G_TYPE_UINT, 0,
  76.                                 "Speed", G_TYPE_UINT, 1,
  77.                                 NULL,NULL) )
  78.                 {
  79.                         g_warning("Could not start Renderer playing due to error: \'%s\'",error->message);
  80.                 }
  81.  
  82.         } else {
  83.                 g_warning("Could not set Transport URI due to error: \'%s\'",error->message);
  84.         }
  85.  
  86. }
  87.  
  88. /* This is our callback method to handle new devices
  89.  * which have been discovered.
  90.  */
  91. static void device_proxy_available_cb(GUPnPControlPoint *cp,
  92.                 GUPnPDeviceProxy *renderer)
  93. {
  94.         /* Add device to list of current devices */
  95.         g_hash_table_insert(renderers,
  96.                         const_cast<char*>(gupnp_device_info_get_udn(GUPNP_DEVICE_INFO(renderer))),
  97.                         renderer);
  98.         if ( strncmp( rendererName, gupnp_device_info_get_friendly_name(GUPNP_DEVICE_INFO(renderer)), strlen(rendererName) ) == 0 ) {
  99.                 rendererFound = true;
  100.                 // Do something here
  101.                 g_print("Renderer %s found\\n", rendererName);
  102.                 play_stream( renderer, uri);
  103.         }
  104.  
  105. }
  106.  
  107. /* This is our callback method to handle devices
  108.  * which are removed from the network
  109.  */
  110. static void device_proxy_unavailable_cb(GUPnPControlPoint *cp,
  111.                 GUPnPDeviceProxy *renderer)
  112. {
  113.         /* Remove device from list of current devices */
  114.         g_hash_table_remove(renderers,
  115.                         const_cast<char*>(gupnp_device_info_get_udn(GUPNP_DEVICE_INFO(renderer))));
  116. }
  117.  
  118.  
  119. /*
  120.  * This is the main program
  121.  */
  122. int main (int argc, char **argv)
  123. {
  124.         int run_time = 2;
  125.         GUPnPContext *context;
  126.         GUPnPControlPoint *cp;
  127.  
  128.          /* Check parameters - very basic check */
  129.         if ( argc != 3 )
  130.         {
  131.                 g_warning("Wrong number of parameters\\nUsage: UPnPClient7 <renderer Name> <uri>");
  132.                 return 0;
  133.         }
  134.         rendererName = argv[1];
  135.         uri= argv[2];
  136.  
  137.         /* Required initialisation */
  138.         g_thread_init (NULL);
  139.         g_type_init ();
  140.  
  141.         /* Create a new GHashTable to store the discovered devices in */
  142.         renderers = g_hash_table_new(g_str_hash, g_str_equal);
  143.  
  144.         /* Create a new parser to help with decoding the AV XML data */
  145.         lc_parser = gupnp_last_change_parser_new();
  146.  
  147.         /* Create a new GUPnP Context.  By here we are using the default GLib main
  148.          context, and connecting to the current machine\'s default IP on an
  149.          automatically generated port. */
  150.         context = gupnp_context_new (NULL, NULL, 0, NULL);
  151.  
  152.         /* Create a Control Point targeting UPnP AV MediaRenderer devices */
  153.         cp = gupnp_control_point_new(context, MEDIA_RENDERER);
  154.  
  155.         /* The device-proxy-available signal is emitted when any devices which match
  156.          our target are found, so connect to it */
  157.         g_signal_connect (cp,
  158.                         "device-proxy-available",
  159.                         G_CALLBACK (device_proxy_available_cb),
  160.                         NULL);
  161.         /* The device-proxy-unavailable signal is emitted when any devices which match
  162.           our target are removed, so connect to it */
  163.         g_signal_connect (cp,
  164.                         "device-proxy-unavailable",
  165.                         G_CALLBACK (device_proxy_unavailable_cb),
  166.                         NULL);
  167.  
  168.         /* Tell the Control Point to start searching */
  169.         gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
  170.  
  171.         /* Set a timeout to finish processing */
  172.         if (run_time >0) {
  173.                 g_timeout_add_seconds (run_time,main_loop_timeout, NULL);
  174.         }
  175.  
  176.         /* Enter the main loop. This will start the search and result in callbacks to
  177.          device_proxy_available_cb. */
  178.         main_loop = g_main_loop_new (NULL, FALSE);
  179.         g_main_loop_run (main_loop);
  180.  
  181.         /* Clean up */
  182.         g_main_loop_unref (main_loop);
  183.         g_object_unref (cp);
  184.         g_object_unref (context);
  185.  
  186.         return 0;
  187. }
');