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