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 dependent 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>
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 ************************************************************************/
50 using namespace CyberLink;
52 /*****************************************************************************
54 *****************************************************************************/
57 static int Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
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 )
71 /*****************************************************************************
72 * Run: main UPnP thread
73 *****************************************************************************
74 * Processes UPnP events
75 *****************************************************************************/
76 class UPnPHandler : public MediaPlayer, public DeviceChangeListener,
77 /*public EventListener,*/ public SearchResponseListener
80 services_discovery_t *p_sd;
82 Device *GetDeviceFromUSN( const string& usn )
84 return getDevice( usn.substr( 0, usn.find( "::" ) ).c_str() );
87 playlist_item_t *FindDeviceNode( Device *dev )
89 return playlist_ChildSearchName( p_sd->p_cat, dev->getFriendlyName() );
92 playlist_item_t *FindDeviceNode( const string &usn )
94 return FindDeviceNode( GetDeviceFromUSN( usn ) );
97 playlist_item_t *AddDevice( Device *dev );
98 void AddDeviceContent( Device *dev );
99 void AddContent( playlist_item_t *p_parent, ContentNode *node );
100 void RemoveDevice( Device *dev );
102 /* CyberLink callbacks */
103 virtual void deviceAdded( Device *dev );
104 virtual void deviceRemoved( Device *dev );
106 virtual void deviceSearchResponseReceived( SSDPPacket *packet );
107 /*virtual void eventNotifyReceived( const char *uuid, long seq,
109 const char *value );*/
112 UPnPHandler( services_discovery_t *p_this )
115 addDeviceChangeListener( this );
116 addSearchResponseListener( this );
117 //addEventListener( this );
121 /*****************************************************************************
122 * Open: initialize and create stuff
123 *****************************************************************************/
124 static int Open( vlc_object_t *p_this )
126 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
128 UPnPHandler *u = new UPnPHandler( p_sd );
130 msg_Dbg( p_sd, "upnp discovery started" );
137 /*****************************************************************************
139 *****************************************************************************/
140 static void Close( vlc_object_t *p_this )
142 UPnPHandler *u = (UPnPHandler *)p_this->p_private;
145 msg_Dbg( p_this, "upnp discovery started" );
149 playlist_item_t *UPnPHandler::AddDevice( Device *dev )
154 /* We are not interested in IGD devices or whatever (at the moment) */
155 if ( !dev->isDeviceType( MediaServer::DEVICE_TYPE ) )
158 playlist_item_t *p_item = FindDeviceNode( dev );
159 if ( p_item != NULL )
163 * Maybe one day, VLC API will make sensible use of the const keyword;
164 * That day, you will no longer need this strdup().
166 char *str = strdup( dev->getFriendlyName( ) );
168 p_item = playlist_NodeCreate( p_playlist, str, p_sd->p_cat, 0, NULL );
169 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
170 msg_Dbg( p_sd, "device %s added", str );
176 void UPnPHandler::AddDeviceContent( Device *dev )
178 playlist_item_t *p_devnode = AddDevice( dev );
180 if( p_devnode == NULL )
183 AddContent( p_devnode, getContentDirectory( dev ) );
186 void UPnPHandler::AddContent( playlist_item_t *p_parent, ContentNode *node )
191 const char *title = node->getTitle();
195 msg_Dbg( p_sd, "title = %s", title );
197 if ( node->isItemNode() )
199 ItemNode *iNode = (ItemNode *)node;
200 input_item_t *p_input = input_item_New( p_sd, iNode->getResource(), title );
201 /* FIXME: playlist_AddInput() can fail */
202 playlist_BothAddInput( p_playlist, p_input, p_parent,
203 PLAYLIST_APPEND, PLAYLIST_END, NULL, NULL,
205 vlc_gc_decref( p_input );
206 } else if ( node->isContainerNode() )
208 ContainerNode *conNode = (ContainerNode *)node;
210 char* p_name = strdup(title); /* See other comment on strdup */
211 playlist_item_t* p_node = playlist_NodeCreate( p_playlist, p_name,
215 unsigned nContentNodes = conNode->getNContentNodes();
217 for( unsigned n = 0; n < nContentNodes; n++ )
218 AddContent( p_node, conNode->getContentNode( n ) );
223 void UPnPHandler::RemoveDevice( Device *dev )
225 playlist_item_t *p_item = FindDeviceNode( dev );
228 playlist_NodeDelete( p_playlist, p_item, true, true );
232 void UPnPHandler::deviceAdded( Device *dev )
234 msg_Dbg( p_sd, "adding device" );
235 AddDeviceContent( dev );
239 void UPnPHandler::deviceRemoved( Device *dev )
241 msg_Dbg( p_sd, "removing device" );
246 void UPnPHandler::deviceSearchResponseReceived( SSDPPacket *packet )
248 if( !packet->isRootDevice() )
251 string usn, nts, nt, udn;
253 packet->getUSN( usn );
255 packet->getNTS( nts );
256 udn = usn.substr( 0, usn.find( "::" ) );
258 /* Remove existing root device before adding updated one */
260 Device *dev = GetDeviceFromUSN( usn );
263 if( !packet->isByeBye() )
264 AddDeviceContent( dev );
267 /*void UPnPHandler::eventNotifyReceived( const char *uuid, long seq,
268 const char *name, const char *value )
270 msg_Dbg( p_sd, "event notify received" );
271 msg_Dbg( p_sd, "uuid = %s, name = %s, value = %s", uuid, name, value );