document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //============================================================================
  2. // Name        : UPnPClient3.cpp
  3. // Author      : Majik
  4. //============================================================================
  5.  
  6. #include <glib-2.0/glib.h>
  7. #include <libgupnp/gupnp-control-point.h>
  8. #include <libgupnp-av/gupnp-av.h>
  9.  
  10. #define MEDIA_RENDERER "urn:schemas-upnp-org:device:MediaRenderer:1"
  11. #define AV_TRANSPORT "urn:schemas-upnp-org:service:AVTransport"
  12.  
  13. static GMainLoop *main_loop;
  14. static GHashTable *renderers;
  15.  
  16. /*
  17.  * Utility function to print out discovered device pairs
  18.  */
  19. static void device_info(const char *udn, GUPnPDeviceProxy *renderer) {
  20.         g_print("Device: %s\\n", gupnp_device_info_get_friendly_name(GUPNP_DEVICE_INFO(renderer)));
  21. }
  22.  
  23. /* This is our callback method to terminate the main loop
  24.  * after the timeout has expired
  25.  */
  26. static gboolean main_loop_timeout(void *data)
  27. {
  28.   /* Print out devices before exit */
  29.         g_print("\\n\\nDiscovered Devices\\n");
  30.     g_hash_table_foreach(renderers, (GHFunc)device_info, NULL);
  31.     g_print("\\n\\n");
  32.     g_main_loop_quit (main_loop);
  33.     return 0;
  34. }
  35.  
  36. /* This is our callback method to handle new devices
  37.  * which have been discovered.
  38.  */
  39. static void device_proxy_available_cb(GUPnPControlPoint *cp,
  40.                             GUPnPDeviceProxy *renderer)
  41. {
  42.   /*
  43.    * Add device to list of current devices
  44.    */
  45.     g_print("Device added: %s\\n", gupnp_device_info_get_friendly_name(GUPNP_DEVICE_INFO(renderer)));
  46.     g_hash_table_insert(renderers,
  47.                   const_cast<char*>(gupnp_device_info_get_udn(GUPNP_DEVICE_INFO(renderer))),
  48.                   renderer);
  49. }
  50.  
  51. /* This is our callback method to handle devices
  52.  * which are removed from the network
  53.  */
  54. static void device_proxy_unavailable_cb(GUPnPControlPoint *cp,
  55.                             GUPnPDeviceProxy *renderer)
  56. {
  57.   /*
  58.    * Remove device from list of current devices
  59.    */
  60.     g_print("Device removed: %s\\n", gupnp_device_info_get_friendly_name(GUPNP_DEVICE_INFO(renderer)));
  61.     g_hash_table_remove(renderers,
  62.                   const_cast<char*>(gupnp_device_info_get_udn(GUPNP_DEVICE_INFO(renderer))));
  63. }
  64.  
  65. /*
  66.  * This is the main program
  67.  */
  68. int main (int argc, char **argv)
  69. {
  70.   GUPnPContext *context;
  71.   GUPnPControlPoint *cp;
  72.  
  73.   /* Required initialisation */
  74.   g_thread_init (NULL);
  75.   g_type_init ();
  76.  
  77.   /* Create a new GHashTable to store the discovered devices in */
  78.   renderers = g_hash_table_new(g_str_hash, g_str_equal);
  79.  
  80.   /* Create a new GUPnP Context.  By here we are using the default GLib main
  81.      context, and connecting to the current machine\'s default IP on an
  82.      automatically generated port. */
  83.   context = gupnp_context_new (NULL, NULL, 0, NULL);
  84.  
  85.   /* Create a Control Point targeting UPnP AV MediaRenderer devices */
  86.   cp = gupnp_control_point_new(context, MEDIA_RENDERER);
  87.  
  88.   /* The device-proxy-available signal is emitted when any devices which match
  89.      our target are found, so connect to it */
  90.   g_signal_connect (cp,
  91.                     "device-proxy-available",
  92.                     G_CALLBACK (device_proxy_available_cb),
  93.                     NULL);
  94.   /* The device-proxy-unavailable signal is emitted when any devices which match
  95.       our target are removed, so connect to it */
  96.   g_signal_connect (cp,
  97.                     "device-proxy-unavailable",
  98.                     G_CALLBACK (device_proxy_unavailable_cb),
  99.                     NULL);
  100.  
  101.   /* Tell the Control Point to start searching */
  102.   gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
  103.  
  104.   /* Set a timeout of 2 seconds to finish processing */
  105.   g_timeout_add_seconds (2,main_loop_timeout, NULL);
  106.  
  107.   /* Enter the main loop. This will start the search and result in callbacks to
  108.      device_proxy_available_cb. */
  109.   main_loop = g_main_loop_new (NULL, FALSE);
  110.   g_main_loop_run (main_loop);
  111.  
  112.   /* Clean up */
  113.   g_main_loop_unref (main_loop);
  114.   g_object_unref (cp);
  115.   g_object_unref (context);
  116.  
  117.   return 0;
  118. }
');