]> git.sesse.net Git - vlc/blob - modules/services_discovery/upnp_cc.cpp
Remove most stray semi-colons in module descriptions
[vlc] / modules / services_discovery / upnp_cc.cpp
1 /*****************************************************************************
2  * upnp_cc.cpp :  UPnP discovery module
3  *****************************************************************************
4  * Copyright (C) 2004-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: RĂ©mi Denis-Courmont <rem # videolan.org>
8  *
9  * Based on original wxWindows patch for VLC, and dependent on CyberLink
10  * UPnP library from :
11  *          Satoshi Konno <skonno@cybergarage.org>
12  *
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.
17  *
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.
22  *
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  *****************************************************************************/
27
28 /*****************************************************************************
29  * Includes
30  *****************************************************************************/
31
32 #include <cybergarage/upnp/media/player/MediaPlayer.h>
33
34 #undef PACKAGE_NAME
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
41 #include <vlc_playlist.h>
42
43 /* FIXME: thread-safety ?? */
44 /* FIXME: playlist locking */
45
46 /************************************************************************
47  * Macros and definitions
48  ************************************************************************/
49 using namespace std;
50 using namespace CyberLink;
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55
56 /* Callbacks */
57     static int  Open ( vlc_object_t * );
58     static void Close( vlc_object_t * );
59
60 vlc_module_begin ()
61     set_shortname( "UPnP")
62     set_description( N_("Universal Plug'n'Play discovery") )
63     set_category( CAT_PLAYLIST )
64     set_subcategory( SUBCAT_PLAYLIST_SD )
65
66     set_capability( "services_discovery", 0 )
67     set_callbacks( Open, Close )
68
69 vlc_module_end ()
70
71 /*****************************************************************************
72  * Run: main UPnP thread
73  *****************************************************************************
74  * Processes UPnP events
75  *****************************************************************************/
76 class UPnPHandler : public MediaPlayer, public DeviceChangeListener,
77                     /*public EventListener,*/ public SearchResponseListener
78 {
79     private:
80         services_discovery_t *p_sd;
81
82         Device *GetDeviceFromUSN( const string& usn )
83         {
84             return getDevice( usn.substr( 0, usn.find( "::" ) ).c_str() );
85         }
86
87         playlist_item_t *FindDeviceNode( Device *dev )
88         {
89             return playlist_ChildSearchName( p_sd->p_cat, dev->getFriendlyName() );
90         }
91
92         playlist_item_t *FindDeviceNode( const string &usn )
93         {
94             return FindDeviceNode( GetDeviceFromUSN( usn ) );
95         }
96
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 );
101
102         /* CyberLink callbacks */
103         virtual void deviceAdded( Device *dev );
104         virtual void deviceRemoved( Device *dev );
105
106         virtual void deviceSearchResponseReceived( SSDPPacket *packet );
107         /*virtual void eventNotifyReceived( const char *uuid, long seq,
108                                           const char *name,
109                                           const char *value );*/
110
111     public:
112         UPnPHandler( services_discovery_t *p_this )
113             : p_sd( p_this )
114         {
115             addDeviceChangeListener( this );
116             addSearchResponseListener( this );
117             //addEventListener( this );
118         }
119 };
120
121 /*****************************************************************************
122  * Open: initialize and create stuff
123  *****************************************************************************/
124 static int Open( vlc_object_t *p_this )
125 {
126     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
127
128     services_discovery_SetLocalizedName( p_sd, _("Devices") );
129
130     UPnPHandler *u = new UPnPHandler( p_sd );
131     u->start( );
132     msg_Dbg( p_sd, "upnp discovery started" );
133     p_sd->p_private = u;
134
135     return VLC_SUCCESS;
136 }
137
138
139 /*****************************************************************************
140  * Close:
141  *****************************************************************************/
142 static void Close( vlc_object_t *p_this )
143 {
144     UPnPHandler *u = (UPnPHandler *)p_this->p_private;
145     u->stop( );
146
147     msg_Dbg( p_this, "upnp discovery started" );
148 }
149
150
151 playlist_item_t *UPnPHandler::AddDevice( Device *dev )
152 {
153     if( dev == NULL )
154         return NULL;
155
156     /* We are not interested in IGD devices or whatever (at the moment) */
157     if ( !dev->isDeviceType( MediaServer::DEVICE_TYPE ) )
158         return NULL;
159
160     playlist_item_t *p_item = FindDeviceNode( dev );
161     if ( p_item != NULL )
162         return p_item;
163
164     /* FIXME:
165      * Maybe one day, VLC API will make sensible use of the const keyword;
166      * That day, you will no longer need this strdup().
167      */
168     char *str = strdup( dev->getFriendlyName( ) );
169
170     p_item = playlist_NodeCreate( p_playlist, str, p_sd->p_cat, 0, NULL );
171     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
172     msg_Dbg( p_sd, "device %s added", str );
173     free( str );
174
175     return p_item;
176 }
177
178 void UPnPHandler::AddDeviceContent( Device *dev )
179 {
180     playlist_item_t *p_devnode = AddDevice( dev );
181
182     if( p_devnode == NULL )
183         return;
184
185     AddContent( p_devnode, getContentDirectory( dev ) );
186 }
187
188 void UPnPHandler::AddContent( playlist_item_t *p_parent, ContentNode *node )
189 {
190     if( node == NULL )
191         return;
192
193     const char *title = node->getTitle();
194     if( title == NULL )
195         return;
196
197     msg_Dbg( p_sd, "title = %s", title );
198
199     if ( node->isItemNode() )
200     {
201         ItemNode *iNode = (ItemNode *)node;
202         input_item_t *p_input = input_item_New( p_sd, iNode->getResource(), title );
203         /* FIXME: playlist_AddInput() can fail */
204         playlist_BothAddInput( p_playlist, p_input, p_parent,
205                                PLAYLIST_APPEND, PLAYLIST_END, NULL, NULL,
206                                false );
207         vlc_gc_decref( p_input );
208     } else if ( node->isContainerNode() )
209     {
210         ContainerNode *conNode = (ContainerNode *)node;
211
212         char* p_name = strdup(title); /* See other comment on strdup */
213         playlist_item_t* p_node = playlist_NodeCreate( p_playlist, p_name,
214                                                        p_parent, 0, NULL );
215         free(p_name);
216
217         unsigned nContentNodes = conNode->getNContentNodes();
218
219         for( unsigned n = 0; n < nContentNodes; n++ )
220            AddContent( p_node, conNode->getContentNode( n ) );
221     }
222 }
223
224
225 void UPnPHandler::RemoveDevice( Device *dev )
226 {
227     playlist_item_t *p_item = FindDeviceNode( dev );
228
229     if( p_item != NULL )
230         playlist_NodeDelete( p_playlist, p_item, true, true );
231 }
232
233
234 void UPnPHandler::deviceAdded( Device *dev )
235 {
236     msg_Dbg( p_sd, "adding device" );
237     AddDeviceContent( dev );
238 }
239
240
241 void UPnPHandler::deviceRemoved( Device *dev )
242 {
243     msg_Dbg( p_sd, "removing device" );
244     RemoveDevice( dev );
245 }
246
247
248 void UPnPHandler::deviceSearchResponseReceived( SSDPPacket *packet )
249 {
250     if( !packet->isRootDevice() )
251         return;
252
253     string usn, nts, nt, udn;
254
255     packet->getUSN( usn );
256     packet->getNT( nt );
257     packet->getNTS( nts );
258     udn = usn.substr( 0, usn.find( "::" ) );
259
260     /* Remove existing root device before adding updated one */
261
262     Device *dev = GetDeviceFromUSN( usn );
263     RemoveDevice( dev );
264
265     if( !packet->isByeBye() )
266         AddDeviceContent( dev );
267 }
268
269 /*void UPnPHandler::eventNotifyReceived( const char *uuid, long seq,
270                                        const char *name, const char *value )
271 {
272     msg_Dbg( p_sd, "event notify received" );
273     msg_Dbg( p_sd, "uuid = %s, name = %s, value = %s", uuid, name, value );
274 }*/