//============================================================================
// Name : UPnPClient3.cpp
// Author : Majik
//============================================================================
#include <glib-2.0/glib.h>
#include <libgupnp/gupnp-control-point.h>
#include <libgupnp-av/gupnp-av.h>
#define MEDIA_RENDERER "urn:schemas-upnp-org:device:MediaRenderer:1"
#define AV_TRANSPORT "urn:schemas-upnp-org:service:AVTransport"
static GMainLoop *main_loop;
static GHashTable *renderers;
/*
* Utility function to print out discovered device pairs
*/
static void device_info(const char *udn, GUPnPDeviceProxy *renderer) {
g_print("Device: %s\\n", gupnp_device_info_get_friendly_name(GUPNP_DEVICE_INFO(renderer)));
}
/* This is our callback method to terminate the main loop
* after the timeout has expired
*/
static gboolean main_loop_timeout(void *data)
{
/* Print out devices before exit */
g_print("\\n\\nDiscovered Devices\\n");
g_hash_table_foreach(renderers, (GHFunc)device_info, NULL);
g_print("\\n\\n");
g_main_loop_quit (main_loop);
return 0;
}
/* This is our callback method to handle new devices
* which have been discovered.
*/
static void device_proxy_available_cb(GUPnPControlPoint *cp,
GUPnPDeviceProxy *renderer)
{
/*
* Add device to list of current devices
*/
g_print("Device added: %s\\n", gupnp_device_info_get_friendly_name(GUPNP_DEVICE_INFO(renderer)));
g_hash_table_insert(renderers,
const_cast<char*>(gupnp_device_info_get_udn(GUPNP_DEVICE_INFO(renderer))),
renderer);
}
/* This is our callback method to handle devices
* which are removed from the network
*/
static void device_proxy_unavailable_cb(GUPnPControlPoint *cp,
GUPnPDeviceProxy *renderer)
{
/*
* Remove device from list of current devices
*/
g_print("Device removed: %s\\n", gupnp_device_info_get_friendly_name(GUPNP_DEVICE_INFO(renderer)));
g_hash_table_remove(renderers,
const_cast<char*>(gupnp_device_info_get_udn(GUPNP_DEVICE_INFO(renderer))));
}
/*
* This is the main program
*/
int main (int argc, char **argv)
{
GUPnPContext *context;
GUPnPControlPoint *cp;
/* Required initialisation */
g_thread_init (NULL);
g_type_init ();
/* Create a new GHashTable to store the discovered devices in */
renderers = g_hash_table_new(g_str_hash, g_str_equal);
/* Create a new GUPnP Context. By here we are using the default GLib main
context, and connecting to the current machine\'s default IP on an
automatically generated port. */
context = gupnp_context_new (NULL, NULL, 0, NULL);
/* Create a Control Point targeting UPnP AV MediaRenderer devices */
cp = gupnp_control_point_new(context, MEDIA_RENDERER);
/* The device-proxy-available signal is emitted when any devices which match
our target are found, so connect to it */
g_signal_connect (cp,
"device-proxy-available",
G_CALLBACK (device_proxy_available_cb),
NULL);
/* The device-proxy-unavailable signal is emitted when any devices which match
our target are removed, so connect to it */
g_signal_connect (cp,
"device-proxy-unavailable",
G_CALLBACK (device_proxy_unavailable_cb),
NULL);
/* Tell the Control Point to start searching */
gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);
/* Set a timeout of 2 seconds to finish processing */
g_timeout_add_seconds (2,main_loop_timeout, NULL);
/* Enter the main loop. This will start the search and result in callbacks to
device_proxy_available_cb. */
main_loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (main_loop);
/* Clean up */
g_main_loop_unref (main_loop);
g_object_unref (cp);
g_object_unref (context);
return 0;
}