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 *****************************************************************************/
32 #include <cybergarage/upnp/media/player/MediaPlayer.h>
36 #include <vlc_playlist.h>
38 /* FIXME: thread-safety ?? */
39 /* FIXME: playlist locking */
41 /************************************************************************
42 * Macros and definitions
43 ************************************************************************/
45 using namespace CyberLink;
47 /*****************************************************************************
49 *****************************************************************************/
52 static int Open ( vlc_object_t * );
53 static void Close( vlc_object_t * );
56 set_shortname( "UPnP");
57 set_description( _("Universal Plug'n'Play discovery") );
58 set_category( CAT_PLAYLIST );
59 set_subcategory( SUBCAT_PLAYLIST_SD );
61 set_capability( "services_discovery", 0 );
62 set_callbacks( Open, Close );
66 /*****************************************************************************
68 *****************************************************************************/
71 static void Run ( services_discovery_t *p_sd );
73 /*****************************************************************************
74 * Open: initialize and create stuff
75 *****************************************************************************/
76 static int Open( vlc_object_t *p_this )
78 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
82 services_discovery_SetLocalizedName( p_sd, _("Devices") );
88 /*****************************************************************************
90 *****************************************************************************/
91 static void Close( vlc_object_t *p_this )
95 /*****************************************************************************
96 * Run: main UPnP thread
97 *****************************************************************************
98 * Processes UPnP events
99 *****************************************************************************/
100 class UPnPHandler : public MediaPlayer, public DeviceChangeListener,
101 /*public EventListener,*/ public SearchResponseListener
104 services_discovery_t *p_sd;
106 Device *GetDeviceFromUSN( const string& usn )
108 return getDevice( usn.substr( 0, usn.find( "::" ) ).c_str() );
111 playlist_item_t *FindDeviceNode( Device *dev )
113 return playlist_ChildSearchName( p_sd->p_cat, dev->getFriendlyName() );
116 playlist_item_t *FindDeviceNode( const string &usn )
118 return FindDeviceNode( GetDeviceFromUSN( usn ) );
121 playlist_item_t *AddDevice( Device *dev );
122 void AddDeviceContent( Device *dev );
123 void AddContent( playlist_item_t *p_parent, ContentNode *node );
124 void RemoveDevice( Device *dev );
126 /* CyberLink callbacks */
127 virtual void deviceAdded( Device *dev );
128 virtual void deviceRemoved( Device *dev );
130 virtual void deviceSearchResponseReceived( SSDPPacket *packet );
131 /*virtual void eventNotifyReceived( const char *uuid, long seq,
133 const char *value );*/
136 UPnPHandler( services_discovery_t *p_this )
139 addDeviceChangeListener( this );
140 addSearchResponseListener( this );
141 //addEventListener( this );
146 static void Run( services_discovery_t *p_sd )
148 UPnPHandler u( p_sd );
152 msg_Dbg( p_sd, "UPnP discovery started" );
153 /* read SAP packets */
154 while( !p_sd->b_die )
160 msg_Dbg( p_sd, "UPnP discovery stopped" );
164 playlist_item_t *UPnPHandler::AddDevice( Device *dev )
169 /* We are not interested in IGD devices or whatever (at the moment) */
170 if ( !dev->isDeviceType( MediaServer::DEVICE_TYPE ) )
173 playlist_item_t *p_item = FindDeviceNode( dev );
174 if ( p_item != NULL )
178 * Maybe one day, VLC API will make sensible use of the const keyword;
179 * That day, you will no longer need this strdup().
181 char *str = strdup( dev->getFriendlyName( ) );
183 p_item = playlist_NodeCreate( p_playlist, str, p_sd->p_cat,0 );
184 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
185 msg_Dbg( p_sd, "device %s added", str );
191 void UPnPHandler::AddDeviceContent( Device *dev )
193 playlist_item_t *p_devnode = AddDevice( dev );
195 if( p_devnode == NULL )
198 AddContent( p_devnode, getContentDirectory( dev ) );
201 void UPnPHandler::AddContent( playlist_item_t *p_parent, ContentNode *node )
206 const char *title = node->getTitle();
210 msg_Dbg( p_sd, "title = %s", title );
212 if ( node->isItemNode() )
214 ItemNode *iNode = (ItemNode *)node;
215 input_item_t *p_input = input_ItemNew( p_sd, iNode->getResource(), title );
216 playlist_BothAddInput( p_playlist, p_input, p_parent,
217 PLAYLIST_APPEND, PLAYLIST_END, NULL, NULL,
219 } else if ( node->isContainerNode() )
221 ContainerNode *conNode = (ContainerNode *)node;
223 char* p_name = strdup(title); /* See other comment on strdup */
224 playlist_item_t* p_node = playlist_NodeCreate( p_playlist,
225 p_name, p_parent, 0 );
228 unsigned nContentNodes = conNode->getNContentNodes();
230 for( unsigned n = 0; n < nContentNodes; n++ )
231 AddContent( p_node, conNode->getContentNode( n ) );
236 void UPnPHandler::RemoveDevice( Device *dev )
238 playlist_item_t *p_item = FindDeviceNode( dev );
241 playlist_NodeDelete( p_playlist, p_item, VLC_TRUE, VLC_TRUE );
245 void UPnPHandler::deviceAdded( Device *dev )
247 msg_Dbg( p_sd, "adding device" );
248 AddDeviceContent( dev );
252 void UPnPHandler::deviceRemoved( Device *dev )
254 msg_Dbg( p_sd, "removing device" );
259 void UPnPHandler::deviceSearchResponseReceived( SSDPPacket *packet )
261 if( !packet->isRootDevice() )
264 string usn, nts, nt, udn;
266 packet->getUSN( usn );
268 packet->getNTS( nts );
269 udn = usn.substr( 0, usn.find( "::" ) );
271 /* Remove existing root device before adding updated one */
273 Device *dev = GetDeviceFromUSN( usn );
276 if( !packet->isByeBye() )
277 AddDeviceContent( dev );
280 /*void UPnPHandler::eventNotifyReceived( const char *uuid, long seq,
281 const char *name, const char *value )
283 msg_Dbg( p_sd, "event notify received" );
284 msg_Dbg( p_sd, "uuid = %s, name = %s, value = %s", uuid, name, value );