]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
sd: Make most members private.
[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  * Destructor
190  ***********************************************************************/
191 static void services_discovery_Destructor ( vlc_object_t *p_obj )
192 {
193     services_discovery_t * p_sd = (services_discovery_t *)p_obj;
194     assert(!p_sd->p->p_module); /* Forgot to call Stop */
195     vlc_event_manager_fini( &p_sd->p->event_manager );
196 }
197
198 /***********************************************************************
199  * GetLocalizedName
200  ***********************************************************************/
201 char *
202 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
203 {
204     return strdup( module_get_name( p_sd->p->p_module, true ) );
205 }
206
207 /***********************************************************************
208  * EventManager
209  ***********************************************************************/
210 vlc_event_manager_t *
211 services_discovery_EventManager ( services_discovery_t * p_sd )
212 {
213     return &p_sd->p->event_manager;
214 }
215
216 /***********************************************************************
217  * AddItem
218  ***********************************************************************/
219 void
220 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
221                              const char * psz_category )
222 {
223     vlc_event_t event;
224     event.type = vlc_ServicesDiscoveryItemAdded;
225     event.u.services_discovery_item_added.p_new_item = p_item;
226     event.u.services_discovery_item_added.psz_category = psz_category;
227
228     vlc_event_send( &p_sd->p->event_manager, &event );
229 }
230
231 /***********************************************************************
232  * RemoveItem
233  ***********************************************************************/
234 void
235 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
236 {
237     vlc_event_t event;
238     event.type = vlc_ServicesDiscoveryItemRemoved;
239     event.u.services_discovery_item_removed.p_item = p_item;
240
241     vlc_event_send( &p_sd->p->event_manager, &event );
242 }
243
244 /*
245  * Playlist - Services discovery bridge
246  */
247
248 struct vlc_playlist_sd_t
249 {
250     /* the playlist items for category and onelevel */
251     playlist_item_t      *p_node;
252     services_discovery_t *p_sd; /**< Loaded service discovery modules */
253     char                 *psz_name;
254 };
255
256  /* A new item has been added to a certain sd */
257 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
258 {
259     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
260     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
261     playlist_item_t * p_parent = user_data;
262     playlist_t * p_playlist = p_parent->p_playlist;
263
264     msg_Dbg( p_playlist, "Adding %s in %s",
265                 p_input->psz_name ? p_input->psz_name : "(null)",
266                 psz_cat ? psz_cat : "(null)" );
267
268     PL_LOCK;
269     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
270     if( !EMPTY_STR(psz_cat) )
271     {
272         /* */
273         playlist_item_t * p_cat;
274         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
275         if( !p_cat )
276         {
277             p_cat = playlist_NodeCreate( p_playlist, psz_cat,
278                                          p_parent, 0, NULL );
279             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
280         }
281         p_parent = p_cat;
282     }
283
284     playlist_NodeAddInput( p_playlist, p_input, p_parent,
285                            PLAYLIST_APPEND, PLAYLIST_END,
286                            pl_Locked );
287     PL_UNLOCK;
288 }
289
290  /* A new item has been removed from a certain sd */
291 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
292 {
293     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
294     playlist_item_t * p_sd_node = user_data;
295     playlist_t *p_playlist = p_sd_node->p_playlist;
296
297     PL_LOCK;
298     playlist_item_t *p_item =
299         playlist_ItemFindFromInputAndRoot( p_playlist, p_input,
300                                            p_sd_node, false );
301     if( !p_item )
302     {
303         PL_UNLOCK; return;
304     }
305     playlist_item_t *p_parent = p_item->p_parent;
306     /* if the item was added under a category and the category node
307        becomes empty, delete that node as well */
308     if( p_parent->i_children > 1 || p_parent == p_sd_node )
309         playlist_DeleteItem( p_playlist, p_item, true );
310     else
311         playlist_NodeDelete( p_playlist, p_parent, true, true );
312     PL_UNLOCK;
313 }
314
315 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,
316                                    const char *psz_name )
317 {
318     /* Perform the addition */
319     services_discovery_t *p_sd;
320
321     msg_Dbg( p_playlist, "adding services_discovery %s...", psz_name );
322     p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist), psz_name );
323     if( !p_sd )
324         return VLC_ENOMEM;
325
326     /* Free in playlist_ServicesDiscoveryRemove */
327     vlc_playlist_sd_t * p_sds = malloc( sizeof(*p_sds) );
328     if( !p_sds )
329     {
330         vlc_sd_Destroy( p_sd );
331         return VLC_ENOMEM;
332     }
333
334     playlist_item_t *p_node;
335
336     /* Look for configuration chain "longname" */
337     const char *psz_longname = "?";
338     if( p_sd->p->p_cfg )
339     {
340         config_chain_t *cfg = p_sd->p->p_cfg;
341         while( cfg )
342         {
343             if( cfg->psz_name && !strcmp( cfg->psz_name, "longname" ) )
344             {
345                 psz_longname = cfg->psz_value;
346                 break;
347             }
348             cfg = cfg->p_next;
349         }
350     }
351
352     PL_LOCK;
353     p_node = playlist_NodeCreate( p_playlist, psz_longname,
354                                   p_playlist->p_root, 0, NULL );
355     PL_UNLOCK;
356
357     vlc_event_manager_t *em = services_discovery_EventManager( p_sd );
358     vlc_event_attach( em, vlc_ServicesDiscoveryItemAdded,
359                       playlist_sd_item_added, p_node );
360
361     vlc_event_attach( em, vlc_ServicesDiscoveryItemRemoved,
362                       playlist_sd_item_removed, p_node );
363
364     if( !vlc_sd_Start( p_sd ) )
365     {
366         vlc_sd_Destroy( p_sd );
367         free( p_sds );
368         return VLC_EGENERIC;
369     }
370
371     /* We want tree-view for service directory */
372     p_node->p_input->b_prefers_tree = true;
373     p_sds->p_sd = p_sd;
374     p_sds->p_node = p_node;
375     p_sds->psz_name = strdup( psz_name );
376
377     PL_LOCK;
378     TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
379     PL_UNLOCK;
380
381     return VLC_SUCCESS;
382 }
383
384 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
385                                       const char *psz_name )
386 {
387     playlist_private_t *priv = pl_priv( p_playlist );
388     vlc_playlist_sd_t * p_sds = NULL;
389
390     PL_LOCK;
391     for( int i = 0; i < priv->i_sds; i++ )
392     {
393         if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) )
394         {
395             p_sds = priv->pp_sds[i];
396             REMOVE_ELEM( priv->pp_sds, priv->i_sds, i );
397             break;
398         }
399     }
400     PL_UNLOCK;
401
402     if( !p_sds )
403     {
404         msg_Warn( p_playlist, "discovery %s is not loaded", psz_name );
405         return VLC_EGENERIC;
406     }
407
408     services_discovery_t *p_sd = p_sds->p_sd;
409     assert( p_sd );
410
411     vlc_sd_Stop( p_sd );
412
413     vlc_event_detach( services_discovery_EventManager( p_sd ),
414                         vlc_ServicesDiscoveryItemAdded,
415                         playlist_sd_item_added,
416                         p_sds->p_node );
417
418     vlc_event_detach( services_discovery_EventManager( p_sd ),
419                         vlc_ServicesDiscoveryItemRemoved,
420                         playlist_sd_item_removed,
421                         p_sds->p_node );
422
423     /* Remove the sd playlist node if it exists */
424     PL_LOCK;
425     playlist_NodeDelete( p_playlist, p_sds->p_node, true, false );
426     PL_UNLOCK;
427
428     vlc_sd_Destroy( p_sd );
429     free( p_sds->psz_name );
430     free( p_sds );
431
432     return VLC_SUCCESS;
433 }
434
435 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
436                                          const char *psz_name )
437 {
438     playlist_private_t *priv = pl_priv( p_playlist );
439     bool found = false;
440     PL_LOCK;
441
442     for( int i = 0; i < priv->i_sds; i++ )
443     {
444         vlc_playlist_sd_t *sd = priv->pp_sds[i];
445
446         if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
447         {
448             found = true;
449             break;
450         }
451     }
452     PL_UNLOCK;
453     return found;
454 }
455
456 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
457 {
458     playlist_private_t *priv = pl_priv( p_playlist );
459
460     while( priv->i_sds > 0 )
461         playlist_ServicesDiscoveryRemove( p_playlist,
462                                           priv->pp_sds[0]->psz_name );
463 }