1 /*****************************************************************************
2 * upnp_cc.cpp : UPnP discovery module
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
7 * Authors: RĂ©mi Denis-Courmont <rem # videolan.org>
9 * Based on original wxWindows patch for VLC, and dependant on CyberLink
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 /*****************************************************************************
30 *****************************************************************************/
31 #include <stdlib.h> /* malloc(), free() */
33 #include <cybergarage/upnp/media/player/MediaPlayer.h>
37 #include <vlc_playlist.h>
39 /* FIXME: thread-safety ?? */
40 /* FIXME: playlist locking */
42 /************************************************************************
43 * Macros and definitions
44 ************************************************************************/
46 using namespace CyberLink;
48 /*****************************************************************************
50 *****************************************************************************/
53 static int Open ( vlc_object_t * );
54 static void Close( vlc_object_t * );
57 set_shortname( "UPnP");
58 set_description( _("Universal Plug'n'Play discovery") );
59 set_category( CAT_PLAYLIST );
60 set_subcategory( SUBCAT_PLAYLIST_SD );
62 set_capability( "services_discovery", 0 );
63 set_callbacks( Open, Close );
68 /*****************************************************************************
70 *****************************************************************************/
72 struct services_discovery_sys_t
74 playlist_item_t *p_node_one;
75 playlist_item_t *p_node_cat;
78 /*****************************************************************************
80 *****************************************************************************/
83 static void Run ( services_discovery_t *p_sd );
85 /*****************************************************************************
86 * Open: initialize and create stuff
87 *****************************************************************************/
88 static int Open( vlc_object_t *p_this )
90 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
91 services_discovery_sys_t *p_sys = (services_discovery_sys_t *)
92 malloc( sizeof( services_discovery_sys_t ) );
97 playlist_NodesPairCreate( pl_Get( p_sd ), _("Devices"),
98 &p_sys->p_node_cat, &p_sys->p_node_one,
104 /*****************************************************************************
106 *****************************************************************************/
107 static void Close( vlc_object_t *p_this )
109 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
110 services_discovery_sys_t *p_sys = p_sd->p_sys;
111 playlist_t *p_playlist = pl_Yield( p_sd );
112 playlist_NodeDelete( p_playlist, p_sys->p_node_one, VLC_TRUE,
114 playlist_NodeDelete( p_playlist, p_sys->p_node_cat, VLC_TRUE,
120 /*****************************************************************************
121 * Run: main UPnP thread
122 *****************************************************************************
123 * Processes UPnP events
124 *****************************************************************************/
125 class UPnPHandler : public MediaPlayer, public DeviceChangeListener,
126 /*public EventListener,*/ public SearchResponseListener
129 services_discovery_t *p_sd;
130 services_discovery_sys_t *p_sys;
132 Device *GetDeviceFromUSN( const string& usn )
134 return getDevice( usn.substr( 0, usn.find( "::" ) ).c_str() );
137 playlist_item_t *FindDeviceNode( Device *dev )
139 return playlist_ChildSearchName( p_sys->p_node, dev->getFriendlyName() );
142 playlist_item_t *FindDeviceNode( const string &usn )
144 return FindDeviceNode( GetDeviceFromUSN( usn ) );
147 playlist_item_t *AddDevice( Device *dev );
148 void AddDeviceContent( Device *dev );
149 void AddContent( playlist_item_t *p_parent, ContentNode *node );
150 void RemoveDevice( Device *dev );
152 /* CyberLink callbacks */
153 virtual void deviceAdded( Device *dev );
154 virtual void deviceRemoved( Device *dev );
156 virtual void deviceSearchResponseReceived( SSDPPacket *packet );
157 /*virtual void eventNotifyReceived( const char *uuid, long seq,
159 const char *value );*/
162 UPnPHandler( services_discovery_t *p_this )
163 : p_sd( p_this ), p_sys( p_this->p_sys )
165 addDeviceChangeListener( this );
166 addSearchResponseListener( this );
167 //addEventListener( this );
172 static void Run( services_discovery_t *p_sd )
174 UPnPHandler u( p_sd );
178 msg_Dbg( p_sd, "UPnP discovery started" );
179 /* read SAP packets */
180 while( !p_sd->b_die )
186 msg_Dbg( p_sd, "UPnP discovery stopped" );
190 playlist_item_t *UPnPHandler::AddDevice( Device *dev )
195 /* We are not interested in IGD devices or whatever (at the moment) */
196 if ( !dev->isDeviceType( MediaServer::DEVICE_TYPE ) )
199 playlist_item_t *p_item = FindDeviceNode( dev );
200 if ( p_item != NULL )
204 * Maybe one day, VLC API will make sensible use of the const keyword;
205 * That day, you will no longer need this strdup().
207 char *str = strdup( dev->getFriendlyName( ) );
209 p_item = playlist_NodeCreate( p_sys->p_playlist, str, p_sys->p_node_cat,0 );
210 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
211 msg_Dbg( p_sd, "device %s added", str );
217 void UPnPHandler::AddDeviceContent( Device *dev )
219 playlist_item_t *p_devnode = AddDevice( dev );
221 if( p_devnode == NULL )
224 AddContent( p_devnode, getContentDirectory( dev ) );
227 void UPnPHandler::AddContent( playlist_item_t *p_parent, ContentNode *node )
232 const char *title = node->getTitle();
236 msg_Dbg( p_sd, "title = %s", title );
238 if ( node->isItemNode() )
240 ItemNode *iNode = (ItemNode *)node;
241 input_item_t *p_input = input_ItemNew( p_sd, iNode->getResource(), title );
242 playlist_BothAddInput( p_sys->p_playlist, p_input, p_parent,
243 PLAYLIST_APPEND, PLAYLIST_END, NULL, NULL );
244 } else if ( node->isContainerNode() )
246 ContainerNode *conNode = (ContainerNode *)node;
248 char* p_name = strdup(title); /* See other comment on strdup */
249 playlist_item_t* p_node = playlist_NodeCreate( p_sys->p_playlist,
250 p_name, p_parent, 0 );
253 unsigned nContentNodes = conNode->getNContentNodes();
255 for( unsigned n = 0; n < nContentNodes; n++ )
256 AddContent( p_node, conNode->getContentNode( n ) );
261 void UPnPHandler::RemoveDevice( Device *dev )
263 playlist_item_t *p_item = FindDeviceNode( dev );
266 playlist_NodeDelete( p_sys->p_playlist, p_item, VLC_TRUE, VLC_TRUE );
270 void UPnPHandler::deviceAdded( Device *dev )
272 msg_Dbg( p_sd, "adding device" );
273 AddDeviceContent( dev );
277 void UPnPHandler::deviceRemoved( Device *dev )
279 msg_Dbg( p_sd, "removing device" );
284 void UPnPHandler::deviceSearchResponseReceived( SSDPPacket *packet )
286 if( !packet->isRootDevice() )
289 string usn, nts, nt, udn;
291 packet->getUSN( usn );
293 packet->getNTS( nts );
294 udn = usn.substr( 0, usn.find( "::" ) );
296 /* Remove existing root device before adding updated one */
298 Device *dev = GetDeviceFromUSN( usn );
301 if( !packet->isByeBye() )
302 AddDeviceContent( dev );
305 /*void UPnPHandler::eventNotifyReceived( const char *uuid, long seq,
306 const char *name, const char *value )
308 msg_Dbg( p_sd, "event notify received" );
309 msg_Dbg( p_sd, "uuid = %s, name = %s, value = %s", uuid, name, value );