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>
42 #include <vlc_services_discovery.h>
44 /* FIXME: thread-safety ?? */
45 /* FIXME: playlist locking */
47 /************************************************************************
48 * Macros and definitions
49 ************************************************************************/
51 using namespace CyberLink;
53 /*****************************************************************************
55 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
62 set_shortname( "UPnP")
63 set_description( N_("Universal Plug'n'Play discovery") )
64 set_category( CAT_PLAYLIST )
65 set_subcategory( SUBCAT_PLAYLIST_SD )
67 set_capability( "services_discovery", 0 )
68 set_callbacks( Open, Close )
72 /*****************************************************************************
73 * Run: main UPnP thread
74 *****************************************************************************
75 * Processes UPnP events
76 *****************************************************************************/
77 class UPnPHandler : public MediaPlayer, public DeviceChangeListener,
78 /*public EventListener,*/ public SearchResponseListener
81 services_discovery_t *p_sd;
83 Device *GetDeviceFromUSN( const string& usn )
85 return getDevice( usn.substr( 0, usn.find( "::" ) ).c_str() );
88 playlist_item_t *FindDeviceNode( Device *dev )
90 return playlist_ChildSearchName( p_sd->p_cat, dev->getFriendlyName() );
93 playlist_item_t *FindDeviceNode( const string &usn )
95 return FindDeviceNode( GetDeviceFromUSN( usn ) );
98 playlist_item_t *AddDevice( Device *dev );
99 void AddDeviceContent( Device *dev );
100 void AddContent( playlist_item_t *p_parent, ContentNode *node );
101 void RemoveDevice( Device *dev );
103 /* CyberLink callbacks */
104 virtual void deviceAdded( Device *dev );
105 virtual void deviceRemoved( Device *dev );
107 virtual void deviceSearchResponseReceived( SSDPPacket *packet );
108 /*virtual void eventNotifyReceived( const char *uuid, long seq,
110 const char *value );*/
113 UPnPHandler( services_discovery_t *p_this )
116 addDeviceChangeListener( this );
117 addSearchResponseListener( this );
118 //addEventListener( this );
122 /*****************************************************************************
123 * Open: initialize and create stuff
124 *****************************************************************************/
125 static int Open( vlc_object_t *p_this )
127 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
129 UPnPHandler *u = new UPnPHandler( p_sd );
131 msg_Dbg( p_sd, "upnp discovery started" );
138 /*****************************************************************************
140 *****************************************************************************/
141 static void Close( vlc_object_t *p_this )
143 UPnPHandler *u = (UPnPHandler *)p_this->p_private;
146 msg_Dbg( p_this, "upnp discovery started" );
150 playlist_item_t *UPnPHandler::AddDevice( Device *dev )
155 /* We are not interested in IGD devices or whatever (at the moment) */
156 if ( !dev->isDeviceType( MediaServer::DEVICE_TYPE ) )
159 playlist_item_t *p_item = FindDeviceNode( dev );
160 if ( p_item != NULL )
164 * Maybe one day, VLC API will make sensible use of the const keyword;
165 * That day, you will no longer need this strdup().
167 char *str = strdup( dev->getFriendlyName( ) );
169 p_item = playlist_NodeCreate( p_playlist, str, p_sd->p_cat, 0, NULL );
170 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
171 msg_Dbg( p_sd, "device %s added", str );
177 void UPnPHandler::AddDeviceContent( Device *dev )
179 playlist_item_t *p_devnode = AddDevice( dev );
181 if( p_devnode == NULL )
184 AddContent( p_devnode, getContentDirectory( dev ) );
187 void UPnPHandler::AddContent( playlist_item_t *p_parent, ContentNode *node )
192 const char *title = node->getTitle();
196 msg_Dbg( p_sd, "title = %s", title );
198 if ( node->isItemNode() )
200 ItemNode *iNode = (ItemNode *)node;
201 input_item_t *p_input = input_item_New( p_sd, iNode->getResource(), title );
202 /* FIXME: playlist_AddInput() can fail */
203 playlist_BothAddInput( p_playlist, p_input, p_parent,
204 PLAYLIST_APPEND, PLAYLIST_END, NULL, NULL,
206 vlc_gc_decref( p_input );
207 } else if ( node->isContainerNode() )
209 ContainerNode *conNode = (ContainerNode *)node;
211 char* p_name = strdup(title); /* See other comment on strdup */
212 playlist_item_t* p_node = playlist_NodeCreate( p_playlist, p_name,
216 unsigned nContentNodes = conNode->getNContentNodes();
218 for( unsigned n = 0; n < nContentNodes; n++ )
219 AddContent( p_node, conNode->getContentNode( n ) );
224 void UPnPHandler::RemoveDevice( Device *dev )
226 playlist_item_t *p_item = FindDeviceNode( dev );
229 playlist_NodeDelete( p_playlist, p_item, true, true );
233 void UPnPHandler::deviceAdded( Device *dev )
235 msg_Dbg( p_sd, "adding device" );
236 AddDeviceContent( dev );
240 void UPnPHandler::deviceRemoved( Device *dev )
242 msg_Dbg( p_sd, "removing device" );
247 void UPnPHandler::deviceSearchResponseReceived( SSDPPacket *packet )
249 if( !packet->isRootDevice() )
252 string usn, nts, nt, udn;
254 packet->getUSN( usn );
256 packet->getNTS( nts );
257 udn = usn.substr( 0, usn.find( "::" ) );
259 /* Remove existing root device before adding updated one */
261 Device *dev = GetDeviceFromUSN( usn );
264 if( !packet->isByeBye() )
265 AddDeviceContent( dev );
268 /*void UPnPHandler::eventNotifyReceived( const char *uuid, long seq,
269 const char *name, const char *value )
271 msg_Dbg( p_sd, "event notify received" );
272 msg_Dbg( p_sd, "uuid = %s, name = %s, value = %s", uuid, name, value );