document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //============================================================================
  2. // Name        : UPnPClient5.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. #define RUN_TIME 1
  13.  
  14. static GMainLoop *main_loop;
  15. static GHashTable *renderers;
  16.  
  17. /*
  18.  * Utility function to pause AVT Transport on device
  19.  */
  20. static void device_info(const char *udn, GUPnPDeviceProxy *renderer) {
  21.         GUPnPServiceProxy *av_transport;
  22.         char *friendly_name;
  23.         const char *action = "Pause";
  24.         GError *error;
  25.         error = NULL;
  26.         gboolean success;
  27.  
  28.         /* Get AVTransport service for device */
  29.         av_transport = GUPNP_SERVICE_PROXY (gupnp_device_info_get_service(GUPNP_DEVICE_INFO(renderer), AV_TRANSPORT));
  30.         friendly_name = gupnp_device_info_get_friendly_name(GUPNP_DEVICE_INFO(renderer));
  31.  
  32.         success = gupnp_service_proxy_send_action (av_transport, action, &error,
  33.                         "InstanceID", G_TYPE_UINT, 0,
  34.                         NULL);
  35. }
  36.  
  37.  
  38. /* This is our callback method to terminate the main loop
  39.  * after the timeout has expired
  40.  */
  41. static gboolean main_loop_timeout(void *data)
  42. {
  43.         /* Set all players to pause */
  44.         g_hash_table_foreach(renderers, (GHFunc)device_info, NULL);
  45.     g_main_loop_quit (main_loop);
  46.     return 0;
  47. }
  48.  
  49. /* This is our callback method to handle new devices
  50.  * which have been discovered.
  51.  */
  52. static void device_proxy_available_cb(GUPnPControlPoint *cp,
  53.                             GUPnPDeviceProxy *renderer)
  54. {
  55.        
  56.   /*
  57.    * Add device to list of current devices
  58.    */
  59.             g_hash_table_insert(renderers,
  60.                   const_cast<char*>(gupnp_device_info_get_udn(GUPNP_DEVICE_INFO(renderer))),
  61.                   renderer);
  62. }
  63.  
  64. /* This is our callback method to handle devices
  65.  * which are removed from the network
  66.  */
  67. static void device_proxy_unavailable_cb(GUPnPControlPoint *cp,
  68.                             GUPnPDeviceProxy *renderer)
  69. {
  70.   /*
  71.    * Remove device from list of current devices
  72.    */
  73.             g_hash_table_remove(renderers,
  74.                   const_cast<char*>(gupnp_device_info_get_udn(GUPNP_DEVICE_INFO(renderer))));
  75. }
  76.  
  77.  
  78. /*
  79.  * This is the main program
  80.  */
  81. int main (int argc, char **argv)
  82. {
  83.   GUPnPContext *context;
  84.   GUPnPControlPoint *cp;
  85.  
  86.   /* Required initialisation */
  87.   g_thread_init (NULL);
  88.   g_type_init ();
  89.  
  90.   /* Create a new GHashTable to store the discovered devices in */
  91.   renderers = g_hash_table_new(g_str_hash, g_str_equal);
  92.  
  93.   /* Create a new GUPnP Context.  By here we are using the default GLib main
  94.      context, and connecting to the current machine\'s default IP on an
  95.      automatically generated port. */
  96.   context = gupnp_context_new (NULL, NULL, 0, NULL);
  97.  
  98.   /* Create a Control Point targeting UPnP AV MediaRenderer devices */
  99.   cp = gupnp_control_point_new(context, MEDIA_RENDERER);
  100.  
  101.   /* The device-proxy-available signal is emitted when any devices which match
  102.      our target are found, so connect to it */
  103.   g_signal_connect (cp,
  104.                     "device-proxy-available",
  105.                     G_CALLBACK (device_proxy_available_cb),
  106.                     NULL);
  107.   /* The device-proxy-unavailable signal is emitted when any devices which match
  108.       our target are removed, so connect to it */
  109.   g_signal_connect (cp,
  110.                     "device-proxy-unavailable",
  111.                     G_CALLBACK (device_proxy_unavailable_cb),
  112.                     NULL);
  113.  
  114.   /* Tell the Control Point to start searching */
  115.   gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
  116.  
  117.   /* Set a timeout of 2 seconds to finish processing */
  118.   g_timeout_add_seconds (RUN_TIME,main_loop_timeout, NULL);
  119.  
  120.   /* Enter the main loop. This will start the search and result in callbacks to
  121.      device_proxy_available_cb. */
  122.   main_loop = g_main_loop_new (NULL, FALSE);
  123.   g_main_loop_run (main_loop);
  124.  
  125.   /* Clean up */
  126.   g_main_loop_unref (main_loop);
  127.   g_object_unref (cp);
  128.   g_object_unref (context);
  129.  
  130.   return 0;
  131. }
');