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