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