[svn r613] updated debian scripts for append info on osso media player desktop file
4 * @file gmyth/gmyth_upnp.c
6 * @brief <p> GMythUPnP allows that a MythTV frontend discovers a
7 * MythTV backend, using the UPnP architecture.
9 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
10 * @author Rosfran Lins Borges <rosfran.borges@indt.org.br>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "gmyth_upnp.h"
36 #include <arpa/inet.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
43 #include <gmyth/gmyth_backendinfo.h>
44 #include <gmyth/gmyth_socket.h>
45 #include <gmyth/gmyth_uri.h>
46 #include <gmyth/gmyth_debug.h>
48 #define GMYTH_UPNP_MAX_SEARCHS 10
50 static void gmyth_upnp_class_init (GMythUPnPClass *klass);
51 static void gmyth_upnp_init (GMythUPnP *object);
53 static void gmyth_upnp_dispose (GObject *object);
54 static void gmyth_upnp_finalize (GObject *object);
56 static gboolean gmyth_upnp_initialize ( GMythUPnP *gmyth_upnp, GMythBackendInfo *gmyth_backend_info );
58 static gboolean gmyth_upnp_print_cp_device_list( CgUpnpControlPoint* controlPt, gchar **udn,
59 GList **mythtv_servers_lst );
61 G_DEFINE_TYPE(GMythUPnP, gmyth_upnp, G_TYPE_OBJECT)
63 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
66 gmyth_upnp_class_init (GMythUPnPClass *klass)
68 GObjectClass *gobject_class;
70 gobject_class = (GObjectClass *) klass;
72 gobject_class->dispose = gmyth_upnp_dispose;
73 gobject_class->finalize = gmyth_upnp_finalize;
77 gmyth_upnp_init ( GMythUPnP *gmyth_upnp )
79 gmyth_upnp->upnp_dev_found = FALSE;
81 gmyth_upnp->uri = NULL;
82 gmyth_upnp->host = NULL;
84 gmyth_upnp->protocol = NULL;
86 gmyth_upnp->gmyth_backend_info = NULL;
88 gmyth_upnp->control_point = NULL;
92 /** Creates a new instance of GMythUPnP.
94 * @return a new instance of GMythUPnP.
97 gmyth_upnp_new ( GMythBackendInfo *gmyth_backend_info )
99 GMythUPnP *gmyth_upnp = GMYTH_UPNP (g_object_new (GMYTH_UPNP_TYPE, NULL));
101 gmyth_upnp->gmyth_backend_info = gmyth_backend_info;
103 if ( !gmyth_upnp_initialize ( gmyth_upnp, gmyth_backend_info ) )
110 gmyth_upnp_dispose (GObject *object)
112 GMythUPnP *gmyth_upnp = GMYTH_UPNP(object);
114 if ( gmyth_upnp->control_point != NULL ) {
115 cg_upnp_controlpoint_stop( gmyth_upnp->control_point );
116 cg_upnp_controlpoint_delete( gmyth_upnp->control_point );
117 gmyth_upnp->control_point = NULL;
120 if ( gmyth_upnp->uri != NULL ) {
121 g_free ( gmyth_upnp->uri );
122 gmyth_upnp->uri = NULL;
125 if ( gmyth_upnp->host != NULL ) {
126 g_free ( gmyth_upnp->host );
127 gmyth_upnp->host = NULL;
130 if ( gmyth_upnp->protocol != NULL ) {
131 g_free ( gmyth_upnp->protocol );
132 gmyth_upnp->protocol = NULL;
135 if ( gmyth_upnp->mythtv_servers != NULL ) {
136 g_list_free( gmyth_upnp->mythtv_servers );
137 gmyth_upnp->mythtv_servers = NULL;
140 G_OBJECT_CLASS (gmyth_upnp_parent_class)->dispose (object);
144 gmyth_upnp_finalize (GObject *object)
146 g_signal_handlers_destroy (object);
148 G_OBJECT_CLASS (gmyth_upnp_parent_class)->finalize (object);
152 * Create a control point and start it.
155 gmyth_upnp_initialize ( GMythUPnP *gmyth_upnp, GMythBackendInfo *gmyth_backend_info )
158 gboolean ret = FALSE;
160 GMythURI* uri = NULL;
162 guint iter_count = GMYTH_UPNP_MAX_SEARCHS;
164 g_return_val_if_fail( gmyth_backend_info != NULL, FALSE );
166 /* Create the cybergarage control point */
167 gmyth_upnp->control_point = cg_upnp_controlpoint_new();
168 /* cg_upnp_controlpoint_setdevicelistener( gmyth_upnp->control_point, device_listener ); */
170 /* Start the control point */
171 if ( cg_upnp_controlpoint_start( gmyth_upnp->control_point ) == FALSE)
173 gmyth_debug( "Unable to start UPnP control point!!!" );
178 gmyth_debug( "Control point started." );
181 while ( gmyth_upnp->upnp_dev_found == FALSE && ( --iter_count > 0 ) ) {
183 gmyth_debug( "UPnP MythTV Client control point is searching MythTV AV Device server...\n" );
185 if ( gmyth_upnp->control_point != NULL )
186 cg_upnp_controlpoint_search ( gmyth_upnp->control_point,
187 "urn:schemas-upnp-org:service:ContentDirectory:1" );
189 /* just to avoid clinkc pthread concurrency faults */
192 /* discover if it was found */
193 gmyth_upnp->upnp_dev_found = gmyth_upnp_print_cp_device_list( gmyth_upnp->control_point, &gmyth_upnp->udn,
194 &gmyth_upnp->mythtv_servers );
198 if ( gmyth_upnp->upnp_dev_found ) {
200 gmyth_debug( "Found UPnP MythTV AV Device...\n" );
202 if ( g_list_first( gmyth_upnp->mythtv_servers ) != NULL && (g_list_first( gmyth_upnp->mythtv_servers ))->data != NULL )
204 gmyth_upnp->uri = (gchar*) (g_list_first( gmyth_upnp->mythtv_servers ))->data;
205 uri = gmyth_uri_new_with_value( gmyth_upnp->uri );
207 gmyth_upnp->host = gmyth_uri_get_host( uri );
208 gmyth_upnp->port = gmyth_uri_get_port( uri );
209 gmyth_upnp->protocol = gmyth_uri_get_protocol( uri );
211 /* sets all the discovered data from the UPnP remote device, like host name, IP address, port,... */
212 if ( NULL != gmyth_upnp->host )
213 gmyth_backend_info_set_hostname ( gmyth_upnp->gmyth_backend_info, gmyth_upnp->host );
215 if ( gmyth_upnp->port > 0 )
216 gmyth_backend_info_set_port ( gmyth_upnp->gmyth_backend_info, gmyth_upnp->port );
226 g_object_unref( uri );
235 * Prints the Control Point's device list
238 gmyth_upnp_print_cp_device_list( CgUpnpControlPoint* controlPt, gchar **udn, GList **mythtv_servers_lst )
241 g_return_val_if_fail( mythtv_servers_lst != NULL, FALSE );
242 g_return_val_if_fail( controlPt != NULL, FALSE );
244 gchar* mythtvFriendlyName = "Myth";
245 /* begin assertion about the size of discovered devices */
246 gint numDevices = cg_upnp_controlpoint_getndevices(controlPt);
248 //CgUpnpDeviceList *devList = NULL;
249 CgUpnpDevice *childDev;
250 gchar *devName = NULL, *dev_url = NULL;
251 gboolean upnp_dev_found = FALSE;
253 gmyth_debug( "UPnP MythTV AV Device list size = %d\n", numDevices );
255 for ( childDev = cg_upnp_controlpoint_getdevices(controlPt); childDev != NULL;
256 childDev = cg_upnp_device_next(childDev) ) {
257 devName = cg_upnp_device_getfriendlyname(childDev);
258 dev_url = cg_upnp_device_getlocationfromssdppacket( childDev );
259 gmyth_debug( "Device's friendly name = %s, and device's URL = %s\n", devName, dev_url );
260 if ( ( upnp_dev_found = ( g_strstr_len( devName, strlen( devName ), mythtvFriendlyName ) != NULL ) ) == TRUE )
262 *udn = cg_upnp_device_getudn( childDev );
263 *mythtv_servers_lst = g_list_append( *mythtv_servers_lst, dev_url );
268 if ( upnp_dev_found == TRUE ) {
269 gmyth_debug( "MythTV AV Device found, from a total of %d devices.\n", cntDevs );
270 } else if ( numDevices == cntDevs ) {
271 gmyth_debug( "MythTV AV Device not found, from a total of %d devices.\n", cntDevs );
273 gmyth_debug( "Control Point's MythTV AV Device count is wrong: iterated over %d devices, but there are %d registered devices.\n", cntDevs, numDevices );
276 return upnp_dev_found;
280 /** Gets the GMythBackendInfo host object associated to this upnp.
282 * @return The string host object currently valid or NULL if the settings
286 gmyth_upnp_get_host ( GMythUPnP *gmyth_upnp )
289 if ( NULL == gmyth_upnp || NULL == gmyth_upnp->host )
291 gmyth_debug ("[%s] GMythUPnP host field not initialized\n", __FUNCTION__);
295 return gmyth_upnp->host;
298 /** Gets the GMythBackendInfo port object associated to this upnp.
300 * @return The string object currently valid or NULL if the port number.
303 gmyth_upnp_get_port( GMythUPnP *gmyth_upnp )
306 if ( NULL == gmyth_upnp || gmyth_upnp->port <= 0 )
308 gmyth_debug ("[%s] GMythUPnP host field not initialized\n", __FUNCTION__);
312 return gmyth_upnp->port;
315 /** Gets the UPnP AV devices server's list associated to this upnp.
317 * @return The GList* containing all the URI values for each recognized UPnP device,
318 * or NULL if it couldn't recognize any MythTV AV device.
321 gmyth_upnp_get_servers ( GMythUPnP *gmyth_upnp )
324 if ( NULL == gmyth_upnp || NULL == gmyth_upnp->mythtv_servers )
326 gmyth_debug ("[%s] GMythUPnP has no MythTV servers recognized.\n", __FUNCTION__);
330 return gmyth_upnp->mythtv_servers;
333 /** Gets the GMythBackendInfo object associated to this upnp.
335 * @return The GMythBackendInfo object currently valid or NULL if the settings
339 gmyth_upnp_get_backend_info ( GMythUPnP *gmyth_upnp )
342 if ( NULL == gmyth_upnp || NULL == gmyth_upnp->gmyth_backend_info )
344 gmyth_debug ("[%s] GMythUPnP not initialized\n", __FUNCTION__);
348 return gmyth_upnp->gmyth_backend_info;