]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
playlist/services_discovery.c: Handle removal of a service.
[vlc] / src / playlist / services_discovery.c
1 /*****************************************************************************
2  * services_discovery.c : Manage playlist services_discovery modules
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 #include <vlc/vlc.h>
24 #include "vlc_playlist.h"
25 #include "vlc_events.h"
26 #include "playlist_internal.h"
27
28 static void RunSD( services_discovery_t *p_sd );
29
30 /*
31  * Services discovery
32  * Basically you just listen to Service discovery event through the
33  * sd's event manager.
34  * That's how the playlist get's Service Discovery information
35  */
36
37 /***********************************************************************
38  * Create
39  ***********************************************************************/
40 services_discovery_t *
41 services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
42 {
43     services_discovery_t *p_sd = vlc_object_create( p_super, VLC_OBJECT_SD );
44     if( !p_sd )
45         return NULL;
46     
47     p_sd->pf_run = NULL;
48     p_sd->psz_localized_name = NULL;
49
50     vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
51     vlc_event_manager_register_event_type( &p_sd->event_manager, 
52             vlc_ServicesDiscoveryItemAdded );
53     vlc_event_manager_register_event_type( &p_sd->event_manager, 
54             vlc_ServicesDiscoveryItemRemoved );
55
56     p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, 0 );
57     
58     if( p_sd->p_module == NULL )
59     {
60         msg_Err( p_super, "no suitable services discovery module" );
61         vlc_object_destroy( p_sd );
62         return NULL;
63     }
64     p_sd->psz_module = strdup( psz_module_name );
65     p_sd->b_die = VLC_FALSE; /* FIXME */
66
67     return p_sd;
68 }
69
70 /***********************************************************************
71  * Destroy
72  ***********************************************************************/
73 void services_discovery_Destroy ( services_discovery_t * p_sd )
74 {
75     vlc_object_kill( p_sd );
76     if( p_sd->pf_run ) vlc_thread_join( p_sd );
77
78     free( p_sd->psz_module );
79     module_Unneed( p_sd, p_sd->p_module );
80
81     vlc_event_manager_fini( &p_sd->event_manager );
82     free( p_sd->psz_localized_name );
83
84     vlc_object_destroy( p_sd );
85 }
86
87 /***********************************************************************
88  * Start
89  ***********************************************************************/
90 int services_discovery_Start ( services_discovery_t * p_sd )
91 {
92     if ((p_sd->pf_run != NULL)
93         && vlc_thread_create( p_sd, "services_discovery", RunSD,
94                               VLC_THREAD_PRIORITY_LOW, VLC_FALSE))
95     {
96         msg_Err( p_sd, "cannot create services discovery thread" );
97         vlc_object_destroy( p_sd );
98         return VLC_EGENERIC;
99     }
100     return VLC_SUCCESS;
101 }
102
103 /***********************************************************************
104  * GetLocalizedName
105  ***********************************************************************/
106 char *
107 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
108 {
109     return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
110 }
111
112 /***********************************************************************
113  * SetLocalizedName
114  ***********************************************************************/
115 void
116 services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
117 {
118     if(p_sd->psz_localized_name) free( p_sd->psz_localized_name );
119     p_sd->psz_localized_name = strdup(psz);
120 }
121
122 /***********************************************************************
123  * EventManager
124  ***********************************************************************/
125 vlc_event_manager_t *
126 services_discovery_EventManager ( services_discovery_t * p_sd )
127 {
128     return &p_sd->event_manager;
129 }
130
131 /***********************************************************************
132  * AddItem
133  ***********************************************************************/
134 void
135 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
136                              const char * psz_category )
137 {
138     vlc_event_t event;
139     event.type = vlc_ServicesDiscoveryItemAdded;
140     event.u.services_discovery_item_added.p_new_item = p_item;
141     event.u.services_discovery_item_added.psz_category = psz_category;
142
143     vlc_event_send( &p_sd->event_manager, &event );
144 }
145
146 /***********************************************************************
147  * RemoveItem
148  ***********************************************************************/
149 void
150 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
151 {
152     vlc_event_t event;
153     event.type = vlc_ServicesDiscoveryItemRemoved;
154     event.u.services_discovery_item_removed.p_item = p_item;
155
156     vlc_event_send( &p_sd->event_manager, &event );
157 }
158
159 /***********************************************************************
160  * RunSD (Private)
161  ***********************************************************************/
162 static void RunSD( services_discovery_t *p_sd )
163 {
164     p_sd->pf_run( p_sd );
165     return;
166 }
167
168 /*
169  * Playlist - Services discovery bridge
170  */
171  
172  /* A new item has been added to a certain sd */
173 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
174 {
175     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
176     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
177     playlist_item_t *p_new_item, * p_parent = user_data;
178
179     msg_Dbg( p_parent->p_playlist, "Adding %s in %s", p_input->psz_name, psz_cat );
180
181     /* If p_child is in root category (this is clearly a hack) and we have a cat*/
182     if( !EMPTY_STR(psz_cat) &&
183         p_parent == playlist_ItemToNode( p_parent->p_playlist, p_parent, VLC_FALSE ) )
184     {
185         /* */
186         playlist_item_t * p_cat;
187         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
188         if( !p_cat )
189         {
190             p_cat = playlist_NodeCreate( p_parent->p_playlist, psz_cat,
191                                          p_parent, 0 );
192             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
193         }
194         p_parent = p_cat;
195     }
196         
197     p_new_item = playlist_NodeAddInput( p_parent->p_playlist, p_input, p_parent,
198                                         PLAYLIST_APPEND, PLAYLIST_END, VLC_FALSE );
199     p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
200     p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
201 }
202
203  /* A new item has been removed from a certain sd */
204 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
205 {
206     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
207     playlist_item_t * p_parent = user_data;
208     printf(" Removind %s\n", p_input->psz_name );
209     playlist_DeleteInputInParent( p_parent->p_playlist, p_input->i_id,
210                                   p_parent, VLC_FALSE );
211 }
212
213 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,  const char *psz_modules )
214 {
215     const char *psz_parser = psz_modules ?: "";
216     int retval = VLC_SUCCESS;
217
218     for (;;)
219     {
220         struct playlist_archived_services_discovery_t * p_asd;
221         playlist_item_t * p_cat;
222         playlist_item_t * p_one;
223
224         while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
225             psz_parser++;
226
227         if( *psz_parser == '\0' )
228             break;
229
230         const char *psz_next = strchr( psz_parser, ':' );
231         if( psz_next == NULL )
232             psz_next = psz_parser + strlen( psz_parser );
233
234         char psz_plugin[psz_next - psz_parser + 1];
235         memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
236         psz_plugin[sizeof (psz_plugin) - 1] = '\0';
237         psz_parser = psz_next;
238
239         /* Perform the addition */
240         msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
241         services_discovery_t *p_sd;
242
243         p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
244         if( !p_sd )
245             continue;
246
247         char * psz = services_discovery_GetLocalizedName( p_sd );
248         if( psz )
249         {
250             playlist_NodesPairCreate( p_playlist, psz,
251                     &p_cat, &p_one, VLC_FALSE );
252             free( psz );
253         }
254         else
255         {
256             /* No name, just add at the top of the playlist */
257             PL_LOCK;
258             p_cat = p_playlist->p_root_category;
259             p_one = p_playlist->p_root_onelevel;
260             PL_UNLOCK;
261         }
262
263         vlc_event_attach( services_discovery_EventManager( p_sd ),
264                           vlc_ServicesDiscoveryItemAdded,
265                           playlist_sd_item_added,
266                           p_one );
267
268         vlc_event_attach( services_discovery_EventManager( p_sd ),
269                           vlc_ServicesDiscoveryItemAdded,
270                           playlist_sd_item_added,
271                           p_cat );
272
273         vlc_event_attach( services_discovery_EventManager( p_sd ),
274                           vlc_ServicesDiscoveryItemRemoved,
275                           playlist_sd_item_removed,
276                           p_one );
277
278         vlc_event_attach( services_discovery_EventManager( p_sd ),
279                           vlc_ServicesDiscoveryItemRemoved,
280                           playlist_sd_item_removed,
281                           p_cat );
282
283         services_discovery_Start( p_sd );
284
285         /* Free in playlist_ServicesDiscoveryRemove */
286         p_asd = malloc( sizeof(struct playlist_archived_services_discovery_t) );
287         p_asd->p_sd = p_sd;
288         p_asd->p_one = p_one;
289         p_asd->p_cat = p_cat;
290         
291         PL_LOCK;
292         TAB_APPEND( p_playlist->i_asds, p_playlist->pp_asds, p_asd );
293         PL_UNLOCK;
294     }
295
296     return retval;
297 }
298
299 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
300                                        const char *psz_module )
301 {
302     int i;
303     struct playlist_archived_services_discovery_t *p_asd = NULL;
304
305     PL_LOCK;
306     for( i = 0 ; i< p_playlist->i_asds ; i ++ )
307     {
308         if( !strcmp( psz_module, p_playlist->pp_asds[i]->p_sd->psz_module ) )
309         {
310             p_asd = p_playlist->pp_asds[i];
311             REMOVE_ELEM( p_playlist->pp_asds, p_playlist->i_asds, i );
312             break;
313         }
314     }
315     PL_UNLOCK;
316
317     if( p_asd && p_asd->p_sd )
318     {
319         services_discovery_Destroy( p_asd->p_sd );
320
321         vlc_event_detach( services_discovery_EventManager( p_asd->p_sd ),
322                           vlc_ServicesDiscoveryItemAdded,
323                           playlist_sd_item_added,
324                           p_asd->p_one );
325         
326         vlc_event_detach( services_discovery_EventManager( p_asd->p_sd ),
327                           vlc_ServicesDiscoveryItemAdded,
328                           playlist_sd_item_added,
329                           p_asd->p_cat );
330         
331         vlc_event_detach( services_discovery_EventManager( p_asd->p_sd ),
332                           vlc_ServicesDiscoveryItemRemoved,
333                           playlist_sd_item_removed,
334                           p_asd->p_one );
335         
336         vlc_event_detach( services_discovery_EventManager( p_asd->p_sd ),
337                           vlc_ServicesDiscoveryItemRemoved,
338                           playlist_sd_item_removed,
339                           p_asd->p_cat );
340         
341         /* Remove the sd playlist node if it exists */
342         PL_LOCK;
343         if( p_asd->p_cat != p_playlist->p_root_category && 
344             p_asd->p_one != p_playlist->p_root_onelevel )
345         {
346             playlist_NodeDelete( p_playlist, p_asd->p_cat, VLC_TRUE, VLC_FALSE );
347             playlist_NodeDelete( p_playlist, p_asd->p_one, VLC_TRUE, VLC_FALSE );
348         }
349         PL_UNLOCK;
350
351         free( p_asd );
352     }
353     else
354     {
355         msg_Warn( p_playlist, "module %s is not loaded", psz_module );
356         return VLC_EGENERIC;
357     }
358     return VLC_SUCCESS;
359 }
360
361 vlc_bool_t playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
362                                               const char *psz_module )
363 {
364     int i;
365     PL_LOCK;
366
367     for( i = 0 ; i< p_playlist->i_asds ; i ++ )
368     {
369         if( !strcmp( psz_module, p_playlist->pp_asds[i]->p_sd->psz_module ) )
370         {
371             PL_UNLOCK;
372             return VLC_TRUE;
373         }
374     }
375     PL_UNLOCK;
376     return VLC_FALSE;
377 }
378