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