codecleanup: de-static-inline a bunch of input_item functions.
[vlc/pdherbemont.git] / modules / services_discovery / upnp_cc.cpp
blob7dfe82d0ebbcb130e433fc027f4134c7f17c5064
1 /*****************************************************************************
2 * upnp_cc.cpp : UPnP discovery module
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
5 * $Id$
7 * Authors: RĂ©mi Denis-Courmont <rem # videolan.org>
9 * Based on original wxWindows patch for VLC, and dependent on CyberLink
10 * UPnP library from :
11 * Satoshi Konno <skonno@cybergarage.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 /*****************************************************************************
29 * Includes
30 *****************************************************************************/
32 #include <cybergarage/upnp/media/player/MediaPlayer.h>
34 #undef PACKAGE_NAME
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_playlist.h>
43 /* FIXME: thread-safety ?? */
44 /* FIXME: playlist locking */
46 /************************************************************************
47 * Macros and definitions
48 ************************************************************************/
49 using namespace std;
50 using namespace CyberLink;
52 /*****************************************************************************
53 * Module descriptor
54 *****************************************************************************/
56 /* Callbacks */
57 static int Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
60 vlc_module_begin();
61 set_shortname( "UPnP");
62 set_description( N_("Universal Plug'n'Play discovery") );
63 set_category( CAT_PLAYLIST );
64 set_subcategory( SUBCAT_PLAYLIST_SD );
66 set_capability( "services_discovery", 0 );
67 set_callbacks( Open, Close );
69 vlc_module_end();
71 /*****************************************************************************
72 * Local prototypes
73 *****************************************************************************/
75 /* Main functions */
76 static void Run ( services_discovery_t *p_sd );
78 /*****************************************************************************
79 * Open: initialize and create stuff
80 *****************************************************************************/
81 static int Open( vlc_object_t *p_this )
83 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
85 p_sd->pf_run = Run;
87 services_discovery_SetLocalizedName( p_sd, _("Devices") );
89 return VLC_SUCCESS;
93 /*****************************************************************************
94 * Close:
95 *****************************************************************************/
96 static void Close( vlc_object_t *p_this )
100 /*****************************************************************************
101 * Run: main UPnP thread
102 *****************************************************************************
103 * Processes UPnP events
104 *****************************************************************************/
105 class UPnPHandler : public MediaPlayer, public DeviceChangeListener,
106 /*public EventListener,*/ public SearchResponseListener
108 private:
109 services_discovery_t *p_sd;
111 Device *GetDeviceFromUSN( const string& usn )
113 return getDevice( usn.substr( 0, usn.find( "::" ) ).c_str() );
116 playlist_item_t *FindDeviceNode( Device *dev )
118 return playlist_ChildSearchName( p_sd->p_cat, dev->getFriendlyName() );
121 playlist_item_t *FindDeviceNode( const string &usn )
123 return FindDeviceNode( GetDeviceFromUSN( usn ) );
126 playlist_item_t *AddDevice( Device *dev );
127 void AddDeviceContent( Device *dev );
128 void AddContent( playlist_item_t *p_parent, ContentNode *node );
129 void RemoveDevice( Device *dev );
131 /* CyberLink callbacks */
132 virtual void deviceAdded( Device *dev );
133 virtual void deviceRemoved( Device *dev );
135 virtual void deviceSearchResponseReceived( SSDPPacket *packet );
136 /*virtual void eventNotifyReceived( const char *uuid, long seq,
137 const char *name,
138 const char *value );*/
140 public:
141 UPnPHandler( services_discovery_t *p_this )
142 : p_sd( p_this )
144 addDeviceChangeListener( this );
145 addSearchResponseListener( this );
146 //addEventListener( this );
151 static void Run( services_discovery_t *p_sd )
153 UPnPHandler u( p_sd );
155 u.start();
157 msg_Dbg( p_sd, "UPnP discovery started" );
158 /* read SAP packets */
159 while( vlc_object_alive (p_sd) )
161 msleep( 500 );
164 u.stop();
165 msg_Dbg( p_sd, "UPnP discovery stopped" );
169 playlist_item_t *UPnPHandler::AddDevice( Device *dev )
171 if( dev == NULL )
172 return NULL;
174 /* We are not interested in IGD devices or whatever (at the moment) */
175 if ( !dev->isDeviceType( MediaServer::DEVICE_TYPE ) )
176 return NULL;
178 playlist_item_t *p_item = FindDeviceNode( dev );
179 if ( p_item != NULL )
180 return p_item;
182 /* FIXME:
183 * Maybe one day, VLC API will make sensible use of the const keyword;
184 * That day, you will no longer need this strdup().
186 char *str = strdup( dev->getFriendlyName( ) );
188 p_item = playlist_NodeCreate( p_playlist, str, p_sd->p_cat, 0, NULL );
189 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
190 msg_Dbg( p_sd, "device %s added", str );
191 free( str );
193 return p_item;
196 void UPnPHandler::AddDeviceContent( Device *dev )
198 playlist_item_t *p_devnode = AddDevice( dev );
200 if( p_devnode == NULL )
201 return;
203 AddContent( p_devnode, getContentDirectory( dev ) );
206 void UPnPHandler::AddContent( playlist_item_t *p_parent, ContentNode *node )
208 if( node == NULL )
209 return;
211 const char *title = node->getTitle();
212 if( title == NULL )
213 return;
215 msg_Dbg( p_sd, "title = %s", title );
217 if ( node->isItemNode() )
219 ItemNode *iNode = (ItemNode *)node;
220 input_item_t *p_input = input_item_New( p_sd, iNode->getResource(), title );
221 /* FIXME: playlist_AddInput() can fail */
222 playlist_BothAddInput( p_playlist, p_input, p_parent,
223 PLAYLIST_APPEND, PLAYLIST_END, NULL, NULL,
224 false );
225 vlc_gc_decref( p_input );
226 } else if ( node->isContainerNode() )
228 ContainerNode *conNode = (ContainerNode *)node;
230 char* p_name = strdup(title); /* See other comment on strdup */
231 playlist_item_t* p_node = playlist_NodeCreate( p_playlist, p_name,
232 p_parent, 0, NULL );
233 free(p_name);
235 unsigned nContentNodes = conNode->getNContentNodes();
237 for( unsigned n = 0; n < nContentNodes; n++ )
238 AddContent( p_node, conNode->getContentNode( n ) );
243 void UPnPHandler::RemoveDevice( Device *dev )
245 playlist_item_t *p_item = FindDeviceNode( dev );
247 if( p_item != NULL )
248 playlist_NodeDelete( p_playlist, p_item, true, true );
252 void UPnPHandler::deviceAdded( Device *dev )
254 msg_Dbg( p_sd, "adding device" );
255 AddDeviceContent( dev );
259 void UPnPHandler::deviceRemoved( Device *dev )
261 msg_Dbg( p_sd, "removing device" );
262 RemoveDevice( dev );
266 void UPnPHandler::deviceSearchResponseReceived( SSDPPacket *packet )
268 if( !packet->isRootDevice() )
269 return;
271 string usn, nts, nt, udn;
273 packet->getUSN( usn );
274 packet->getNT( nt );
275 packet->getNTS( nts );
276 udn = usn.substr( 0, usn.find( "::" ) );
278 /* Remove existing root device before adding updated one */
280 Device *dev = GetDeviceFromUSN( usn );
281 RemoveDevice( dev );
283 if( !packet->isByeBye() )
284 AddDeviceContent( dev );
287 /*void UPnPHandler::eventNotifyReceived( const char *uuid, long seq,
288 const char *name, const char *value )
290 msg_Dbg( p_sd, "event notify received" );
291 msg_Dbg( p_sd, "uuid = %s, name = %s, value = %s", uuid, name, value );