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