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 );
67 /*****************************************************************************
69 *****************************************************************************/
71 struct services_discovery_sys_t
73 playlist_item_t *p_node_one;
74 playlist_item_t *p_node_cat;
77 /*****************************************************************************
79 *****************************************************************************/
82 static void Run ( services_discovery_t *p_sd );
84 /*****************************************************************************
85 * Open: initialize and create stuff
86 *****************************************************************************/
87 static int Open( vlc_object_t *p_this )
89 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
90 services_discovery_sys_t *p_sys = (services_discovery_sys_t *)
91 malloc( sizeof( services_discovery_sys_t ) );
96 playlist_NodesPairCreate( pl_Get( p_sd ), _("Devices"),
97 &p_sys->p_node_cat, &p_sys->p_node_one,
103 /*****************************************************************************
105 *****************************************************************************/
106 static void Close( vlc_object_t *p_this )
108 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
109 services_discovery_sys_t *p_sys = p_sd->p_sys;
110 playlist_t *p_playlist = pl_Yield( p_sd );
111 playlist_NodeDelete( p_playlist, p_sys->p_node_one, VLC_TRUE,
113 playlist_NodeDelete( p_playlist, p_sys->p_node_cat, VLC_TRUE,
119 /*****************************************************************************
120 * Run: main UPnP thread
121 *****************************************************************************
122 * Processes UPnP events
123 *****************************************************************************/
124 class UPnPHandler : public MediaPlayer, public DeviceChangeListener,
125 /*public EventListener,*/ public SearchResponseListener
128 services_discovery_t *p_sd;
129 services_discovery_sys_t *p_sys;
131 Device *GetDeviceFromUSN( const string& usn )
133 return getDevice( usn.substr( 0, usn.find( "::" ) ).c_str() );
136 playlist_item_t *FindDeviceNode( Device *dev )
138 return playlist_ChildSearchName( p_sys->p_node, dev->getFriendlyName() );
141 playlist_item_t *FindDeviceNode( const string &usn )
143 return FindDeviceNode( GetDeviceFromUSN( usn ) );
146 playlist_item_t *AddDevice( Device *dev );
147 void AddDeviceContent( Device *dev );
148 void AddContent( playlist_item_t *p_parent, ContentNode *node );
149 void RemoveDevice( Device *dev );
151 /* CyberLink callbacks */
152 virtual void deviceAdded( Device *dev );
153 virtual void deviceRemoved( Device *dev );
155 virtual void deviceSearchResponseReceived( SSDPPacket *packet );
156 /*virtual void eventNotifyReceived( const char *uuid, long seq,
158 const char *value );*/
161 UPnPHandler( services_discovery_t *p_this )
162 : p_sd( p_this ), p_sys( p_this->p_sys )
164 addDeviceChangeListener( this );
165 addSearchResponseListener( this );
166 //addEventListener( this );
171 static void Run( services_discovery_t *p_sd )
173 UPnPHandler u( p_sd );
177 msg_Dbg( p_sd, "UPnP discovery started" );
178 /* read SAP packets */
179 while( !p_sd->b_die )
185 msg_Dbg( p_sd, "UPnP discovery stopped" );
189 playlist_item_t *UPnPHandler::AddDevice( Device *dev )
194 /* We are not interested in IGD devices or whatever (at the moment) */
195 if ( !dev->isDeviceType( MediaServer::DEVICE_TYPE ) )
198 playlist_item_t *p_item = FindDeviceNode( dev );
199 if ( p_item != NULL )
203 * Maybe one day, VLC API will make sensible use of the const keyword;
204 * That day, you will no longer need this strdup().
206 char *str = strdup( dev->getFriendlyName( ) );
208 p_item = playlist_NodeCreate( p_sys->p_playlist, str, p_sys->p_node_cat,0 );
209 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
210 msg_Dbg( p_sd, "device %s added", str );
216 void UPnPHandler::AddDeviceContent( Device *dev )
218 playlist_item_t *p_devnode = AddDevice( dev );
220 if( p_devnode == NULL )
223 AddContent( p_devnode, getContentDirectory( dev ) );
226 void UPnPHandler::AddContent( playlist_item_t *p_parent, ContentNode *node )
231 const char *title = node->getTitle();
235 msg_Dbg( p_sd, "title = %s", title );
237 if ( node->isItemNode() )
239 ItemNode *iNode = (ItemNode *)node;
240 input_item_t *p_input = input_ItemNew( p_sd, iNode->getResource(), title );
241 playlist_BothAddInput( p_sys->p_playlist, p_input, p_parent,
242 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 );