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