]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
Run SD threads (if any) with cancellation
[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 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26 #include <assert.h>
27
28 #include <vlc_common.h>
29 #include "vlc_playlist.h"
30 #include "vlc_events.h"
31 #include "playlist_internal.h"
32 #include "../libvlc.h"
33
34 static void* RunSD( vlc_object_t *p_this );
35
36 /*
37  * Services discovery
38  * Basically you just listen to Service discovery event through the
39  * sd's event manager.
40  * That's how the playlist get's Service Discovery information
41  */
42
43 /***********************************************************************
44  * GetServicesNames
45  ***********************************************************************/
46 char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
47                                                char ***pppsz_longnames )
48 {
49     return module_GetModulesNamesForCapability( p_super, "services_discovery",
50                                                 pppsz_longnames );
51 }
52
53 /***********************************************************************
54  * Create
55  ***********************************************************************/
56 services_discovery_t *
57 services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
58 {
59     services_discovery_t *p_sd;
60     p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
61                               "services discovery" );
62     if( !p_sd )
63         return NULL;
64
65     p_sd->pf_run = NULL;
66     p_sd->psz_localized_name = NULL;
67
68     vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
69     vlc_event_manager_register_event_type( &p_sd->event_manager,
70             vlc_ServicesDiscoveryItemAdded );
71     vlc_event_manager_register_event_type( &p_sd->event_manager,
72             vlc_ServicesDiscoveryItemRemoved );
73     vlc_event_manager_register_event_type( &p_sd->event_manager,
74             vlc_ServicesDiscoveryStarted );
75     vlc_event_manager_register_event_type( &p_sd->event_manager,
76             vlc_ServicesDiscoveryEnded );
77
78     p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, true );
79
80     if( p_sd->p_module == NULL )
81     {
82         msg_Err( p_super, "no suitable services discovery module" );
83         vlc_object_release( p_sd );
84         return NULL;
85     }
86     p_sd->psz_module = strdup( psz_module_name );
87     p_sd->b_die = false; /* FIXME */
88
89     vlc_object_attach( p_sd, p_super );
90     return p_sd;
91 }
92
93 /***********************************************************************
94  * Destroy
95  ***********************************************************************/
96 void services_discovery_Destroy ( services_discovery_t * p_sd )
97 {
98     vlc_event_manager_fini( &p_sd->event_manager );
99
100     free( p_sd->psz_module );
101     free( p_sd->psz_localized_name );
102
103     vlc_object_detach( p_sd );
104     vlc_object_release( p_sd );
105 }
106
107 /***********************************************************************
108  * Start
109  ***********************************************************************/
110 int services_discovery_Start ( services_discovery_t * p_sd )
111 {
112     if ((p_sd->pf_run != NULL)
113         && vlc_thread_create( p_sd, "services_discovery", RunSD,
114                               VLC_THREAD_PRIORITY_LOW, false))
115     {
116         msg_Err( p_sd, "cannot create services discovery thread" );
117         vlc_object_release( p_sd );
118         return VLC_EGENERIC;
119     }
120     return VLC_SUCCESS;
121 }
122
123 /***********************************************************************
124  * Stop
125  ***********************************************************************/
126 static void ObjectKillChildrens( vlc_object_t *p_obj )
127 {
128     vlc_list_t *p_list;
129     int i;
130     vlc_object_kill( p_obj );
131
132     p_list = vlc_list_children( p_obj );
133     for( i = 0; i < p_list->i_count; i++ )
134         ObjectKillChildrens( p_list->p_values[i].p_object );
135     vlc_list_release( p_list );
136 }
137
138 void services_discovery_Stop ( services_discovery_t * p_sd )
139 {
140     ObjectKillChildrens( VLC_OBJECT(p_sd) );
141     if( p_sd->pf_run ) vlc_thread_join( p_sd );
142
143     module_Unneed( p_sd, p_sd->p_module );
144 }
145
146 /***********************************************************************
147  * GetLocalizedName
148  ***********************************************************************/
149 char *
150 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
151 {
152     return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
153 }
154
155 /***********************************************************************
156  * SetLocalizedName
157  ***********************************************************************/
158 void
159 services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
160 {
161     free( p_sd->psz_localized_name );
162     p_sd->psz_localized_name = strdup(psz);
163 }
164
165 /***********************************************************************
166  * EventManager
167  ***********************************************************************/
168 vlc_event_manager_t *
169 services_discovery_EventManager ( services_discovery_t * p_sd )
170 {
171     return &p_sd->event_manager;
172 }
173
174 /***********************************************************************
175  * AddItem
176  ***********************************************************************/
177 void
178 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
179                              const char * psz_category )
180 {
181     vlc_event_t event;
182     event.type = vlc_ServicesDiscoveryItemAdded;
183     event.u.services_discovery_item_added.p_new_item = p_item;
184     event.u.services_discovery_item_added.psz_category = psz_category;
185
186     vlc_event_send( &p_sd->event_manager, &event );
187 }
188
189 /***********************************************************************
190  * RemoveItem
191  ***********************************************************************/
192 void
193 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
194 {
195     vlc_event_t event;
196     event.type = vlc_ServicesDiscoveryItemRemoved;
197     event.u.services_discovery_item_removed.p_item = p_item;
198
199     vlc_event_send( &p_sd->event_manager, &event );
200 }
201
202 /***********************************************************************
203  * RunSD (Private)
204  ***********************************************************************/
205 static void* RunSD( vlc_object_t *p_this )
206 {
207     services_discovery_t *p_sd = (services_discovery_t *)p_this;
208     vlc_event_t event = {
209         .type = vlc_ServicesDiscoveryStarted
210     };
211     int canc = vlc_savecancel ();
212     vlc_event_send( &p_sd->event_manager, &event );
213     vlc_restorecancel (canc);
214
215     p_sd->pf_run( p_sd );
216
217     canc = vlc_savecancel ();
218     event.type = vlc_ServicesDiscoveryEnded;
219     vlc_event_send( &p_sd->event_manager, &event );
220     vlc_restorecancel (canc);
221     return NULL;
222 }
223
224 /*
225  * Playlist - Services discovery bridge
226  */
227
228  /* A new item has been added to a certain sd */
229 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
230 {
231     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
232     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
233     playlist_item_t *p_new_item, * p_parent = user_data;
234     playlist_t * p_playlist = p_parent->p_playlist;
235
236     msg_Dbg( p_playlist, "Adding %s in %s",
237                 p_input->psz_name ? p_input->psz_name : "(null)",
238                 psz_cat ? psz_cat : "(null)" );
239
240     PL_LOCK;
241     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
242     if( !EMPTY_STR(psz_cat) &&
243         p_parent->p_parent == p_playlist->p_root_category )
244     {
245         /* */
246         playlist_item_t * p_cat;
247         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
248         if( !p_cat )
249         {
250             p_cat = playlist_NodeCreate( p_playlist, psz_cat,
251                                          p_parent, 0, NULL );
252             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
253         }
254         p_parent = p_cat;
255     }
256
257     p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
258                                         PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
259     if( p_new_item )
260     {
261         p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
262         p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
263     }
264     PL_UNLOCK;
265 }
266
267  /* A new item has been removed from a certain sd */
268 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
269 {
270     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
271     playlist_item_t * p_parent = user_data;
272     playlist_item_t * p_pl_item;
273
274     /* First make sure that if item is a node it will be deleted.
275      * XXX: Why don't we have a function to ensure that in the playlist code ? */
276     vlc_object_lock( p_parent->p_playlist );
277     p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
278             p_input->i_id, p_parent, false );
279
280     if( p_pl_item && p_pl_item->i_children > -1 )
281     {
282         playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
283         vlc_object_unlock( p_parent->p_playlist );
284         return;
285     }
286
287     /* Delete the non-node item normally */
288     playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
289                                       p_parent, pl_Locked );
290
291     vlc_object_unlock( p_parent->p_playlist );
292 }
293
294 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,  const char *psz_modules )
295 {
296     const char *psz_parser = psz_modules ?: "";
297     int retval = VLC_SUCCESS;
298
299     for (;;)
300     {
301         struct playlist_services_discovery_support_t * p_sds;
302         playlist_item_t * p_cat;
303         playlist_item_t * p_one;
304
305         while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
306             psz_parser++;
307
308         if( *psz_parser == '\0' )
309             break;
310
311         const char *psz_next = strchr( psz_parser, ':' );
312         if( psz_next == NULL )
313             psz_next = psz_parser + strlen( psz_parser );
314
315         char psz_plugin[psz_next - psz_parser + 1];
316         memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
317         psz_plugin[sizeof (psz_plugin) - 1] = '\0';
318         psz_parser = psz_next;
319
320         /* Perform the addition */
321         msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
322         services_discovery_t *p_sd;
323
324         p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
325         if( !p_sd )
326             continue;
327
328         char * psz = services_discovery_GetLocalizedName( p_sd );
329         assert( psz );
330         PL_LOCK;
331         playlist_NodesPairCreate( p_playlist, psz,
332                 &p_cat, &p_one, false );
333         PL_UNLOCK;
334         free( psz );
335
336         vlc_event_attach( services_discovery_EventManager( p_sd ),
337                           vlc_ServicesDiscoveryItemAdded,
338                           playlist_sd_item_added,
339                           p_one );
340
341         vlc_event_attach( services_discovery_EventManager( p_sd ),
342                           vlc_ServicesDiscoveryItemAdded,
343                           playlist_sd_item_added,
344                           p_cat );
345
346         vlc_event_attach( services_discovery_EventManager( p_sd ),
347                           vlc_ServicesDiscoveryItemRemoved,
348                           playlist_sd_item_removed,
349                           p_one );
350
351         vlc_event_attach( services_discovery_EventManager( p_sd ),
352                           vlc_ServicesDiscoveryItemRemoved,
353                           playlist_sd_item_removed,
354                           p_cat );
355
356         services_discovery_Start( p_sd );
357
358         /* Free in playlist_ServicesDiscoveryRemove */
359         p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
360         if( !p_sds )
361             return VLC_ENOMEM;
362
363         /* We want tree-view for service directory */
364         p_one->p_input->b_prefers_tree = true;
365         p_sds->p_sd = p_sd;
366         p_sds->p_one = p_one;
367         p_sds->p_cat = p_cat;
368
369         PL_LOCK;
370         TAB_APPEND( p_playlist->i_sds, p_playlist->pp_sds, p_sds );
371         PL_UNLOCK;
372     }
373
374     return retval;
375 }
376
377 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
378                                        const char *psz_module )
379 {
380     struct playlist_services_discovery_support_t * p_sds = NULL;
381     int i;
382
383     PL_LOCK;
384     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
385     {
386         if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
387         {
388             p_sds = p_playlist->pp_sds[i];
389             REMOVE_ELEM( p_playlist->pp_sds, p_playlist->i_sds, i );
390             break;
391         }
392     }
393     PL_UNLOCK;
394
395     if( !p_sds || !p_sds->p_sd )
396     {
397         msg_Warn( p_playlist, "module %s is not loaded", psz_module );
398         return VLC_EGENERIC;
399     }
400
401     services_discovery_Stop( p_sds->p_sd );
402
403     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
404                         vlc_ServicesDiscoveryItemAdded,
405                         playlist_sd_item_added,
406                         p_sds->p_one );
407
408     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
409                         vlc_ServicesDiscoveryItemAdded,
410                         playlist_sd_item_added,
411                         p_sds->p_cat );
412
413     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
414                         vlc_ServicesDiscoveryItemRemoved,
415                         playlist_sd_item_removed,
416                         p_sds->p_one );
417
418     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
419                         vlc_ServicesDiscoveryItemRemoved,
420                         playlist_sd_item_removed,
421                         p_sds->p_cat );
422
423     /* Remove the sd playlist node if it exists */
424     PL_LOCK;
425     if( p_sds->p_cat != p_playlist->p_root_category &&
426         p_sds->p_one != p_playlist->p_root_onelevel )
427     {
428         playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
429         playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
430     }
431     PL_UNLOCK;
432
433     services_discovery_Destroy( p_sds->p_sd );
434     free( p_sds );
435
436     return VLC_SUCCESS;
437 }
438
439 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
440                                               const char *psz_module )
441 {
442     int i;
443     PL_LOCK;
444
445     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
446     {
447         if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
448         {
449             PL_UNLOCK;
450             return true;
451         }
452     }
453     PL_UNLOCK;
454     return false;
455 }
456
457 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
458 {
459     while( p_playlist->i_sds > 0 )
460         playlist_ServicesDiscoveryRemove( p_playlist,
461                                      p_playlist->pp_sds[0]->p_sd->psz_module );
462 }
463