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