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