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