1 /*****************************************************************************
2 * services_discovery.c : Manage playlist services_discovery modules
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
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"
43 int vlc_sd_probe_Add (vlc_probe_t *probe, const char *name,
44 const char *longname, int category)
46 vlc_sd_probe_t names = { strdup(name), strdup(longname), category };
48 if (unlikely (names.name == NULL || names.longname == NULL
49 || vlc_probe_add (probe, &names, sizeof (names))))
52 free (names.longname);
55 return VLC_PROBE_CONTINUE;
58 struct vlc_sd_private_t
61 vlc_event_manager_t event_manager; /* Accessed through Setters for non class function */
63 config_chain_t *p_cfg;
67 #undef vlc_sd_GetNames
70 * Gets the list of available services discovery plugins.
72 char **vlc_sd_GetNames (vlc_object_t *obj, char ***pppsz_longnames, int **pp_categories)
75 vlc_sd_probe_t *tab = vlc_probe (obj, "services probe", &count);
83 char **names = malloc (sizeof(char *) * (count + 1));
84 char **longnames = malloc (sizeof(char *) * (count + 1));
85 int *categories = malloc(sizeof(int) * (count + 1));
87 if (unlikely (names == NULL || longnames == NULL || categories == NULL))
89 for( size_t i = 0; i < count; i++ )
91 names[i] = tab[i].name;
92 longnames[i] = tab[i].longname;
93 categories[i] = tab[i].category;
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 );
105 static void services_discovery_Destructor ( vlc_object_t *p_obj );
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
114 /***********************************************************************
116 ***********************************************************************/
117 services_discovery_t *vlc_sd_Create( vlc_object_t *p_super,
120 services_discovery_t *p_sd;
122 p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
123 "services discovery" );
126 p_sd->p = malloc(sizeof(struct vlc_sd_private_t));
128 free(config_ChainCreate( &p_sd->p->psz_name, &p_sd->p->p_cfg, cfg ));
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);
137 vlc_object_set_destructor( p_sd, services_discovery_Destructor );
138 vlc_object_attach( p_sd, p_super );
143 /***********************************************************************
145 ***********************************************************************/
146 bool vlc_sd_Start ( services_discovery_t * p_sd )
148 assert(!p_sd->p_module);
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 )
154 msg_Err( p_sd, "no suitable services discovery module" );
158 vlc_event_t event = {
159 .type = vlc_ServicesDiscoveryStarted
161 vlc_event_send( &p_sd->p->event_manager, &event );
165 /***********************************************************************
167 ***********************************************************************/
168 void vlc_sd_Stop ( services_discovery_t * p_sd )
170 vlc_event_t event = {
171 .type = vlc_ServicesDiscoveryEnded
174 vlc_event_send( &p_sd->p->event_manager, &event );
176 module_unneed( p_sd, p_sd->p->p_module );
177 p_sd->p->p_module = NULL;
180 void vlc_sd_Destroy( services_discovery_t *p_sd )
182 config_ChainDestroy( p_sd->p->p_cfg );
183 free( p_sd->p->psz_name );
185 vlc_object_release( p_sd );
188 /***********************************************************************
190 ***********************************************************************/
192 const char *vlc_sd_GetName( services_discovery_t *p_sd )
194 return p_sd->p->psz_name;
197 config_chain_t *vlc_sd_GetConfigChain( services_discovery_t *p_sd )
199 return p_sd->p->p_cfg;
202 /***********************************************************************
204 ***********************************************************************/
205 static void services_discovery_Destructor ( vlc_object_t *p_obj )
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 );
212 /***********************************************************************
214 ***********************************************************************/
216 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
218 return strdup( module_get_name( p_sd->p->p_module, true ) );
221 /***********************************************************************
223 ***********************************************************************/
224 vlc_event_manager_t *
225 services_discovery_EventManager ( services_discovery_t * p_sd )
227 return &p_sd->p->event_manager;
230 /***********************************************************************
232 ***********************************************************************/
234 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
235 const char * psz_category )
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;
242 vlc_event_send( &p_sd->p->event_manager, &event );
245 /***********************************************************************
247 ***********************************************************************/
249 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
252 event.type = vlc_ServicesDiscoveryItemRemoved;
253 event.u.services_discovery_item_removed.p_item = p_item;
255 vlc_event_send( &p_sd->p->event_manager, &event );
259 * Playlist - Services discovery bridge
262 struct vlc_playlist_sd_t
264 /* the playlist items for category and onelevel */
265 playlist_item_t *p_node;
266 services_discovery_t *p_sd; /**< Loaded service discovery modules */
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 )
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;
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)" );
283 /* If p_parent is in root category (this is clearly a hack) and we have a cat */
284 if( !EMPTY_STR(psz_cat) )
287 playlist_item_t * p_cat;
288 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
291 p_cat = playlist_NodeCreate( p_playlist, psz_cat,
293 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
298 playlist_NodeAddInput( p_playlist, p_input, p_parent,
299 PLAYLIST_APPEND, PLAYLIST_END,
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 )
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;
312 playlist_item_t *p_item =
313 playlist_ItemFindFromInputAndRoot( p_playlist, p_input,
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 );
325 playlist_NodeDelete( p_playlist, p_parent, true, true );
329 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,
330 const char *psz_name )
332 /* Perform the addition */
333 services_discovery_t *p_sd;
335 msg_Dbg( p_playlist, "adding services_discovery %s...", psz_name );
336 p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist), psz_name );
340 /* Free in playlist_ServicesDiscoveryRemove */
341 vlc_playlist_sd_t * p_sds = malloc( sizeof(*p_sds) );
344 vlc_sd_Destroy( p_sd );
348 playlist_item_t *p_node;
350 /* Look for configuration chain "longname" */
351 const char *psz_longname = "?";
354 config_chain_t *cfg = p_sd->p->p_cfg;
357 if( cfg->psz_name && !strcmp( cfg->psz_name, "longname" ) )
359 psz_longname = cfg->psz_value;
367 p_node = playlist_NodeCreate( p_playlist, psz_longname,
368 p_playlist->p_root, 0, NULL );
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 );
375 vlc_event_attach( em, vlc_ServicesDiscoveryItemRemoved,
376 playlist_sd_item_removed, p_node );
378 if( !vlc_sd_Start( p_sd ) )
380 vlc_sd_Destroy( p_sd );
385 /* We want tree-view for service directory */
386 p_node->p_input->b_prefers_tree = true;
388 p_sds->p_node = p_node;
389 p_sds->psz_name = strdup( psz_name );
392 TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
398 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
399 const char *psz_name )
401 playlist_private_t *priv = pl_priv( p_playlist );
402 vlc_playlist_sd_t * p_sds = NULL;
405 for( int i = 0; i < priv->i_sds; i++ )
407 if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) )
409 p_sds = priv->pp_sds[i];
410 REMOVE_ELEM( priv->pp_sds, priv->i_sds, i );
418 msg_Warn( p_playlist, "discovery %s is not loaded", psz_name );
422 services_discovery_t *p_sd = p_sds->p_sd;
427 vlc_event_detach( services_discovery_EventManager( p_sd ),
428 vlc_ServicesDiscoveryItemAdded,
429 playlist_sd_item_added,
432 vlc_event_detach( services_discovery_EventManager( p_sd ),
433 vlc_ServicesDiscoveryItemRemoved,
434 playlist_sd_item_removed,
437 /* Remove the sd playlist node if it exists */
439 playlist_NodeDelete( p_playlist, p_sds->p_node, true, false );
442 vlc_sd_Destroy( p_sd );
443 free( p_sds->psz_name );
449 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
450 const char *psz_name )
452 playlist_private_t *priv = pl_priv( p_playlist );
456 for( int i = 0; i < priv->i_sds; i++ )
458 vlc_playlist_sd_t *sd = priv->pp_sds[i];
460 if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
470 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
472 playlist_private_t *priv = pl_priv( p_playlist );
474 while( priv->i_sds > 0 )
475 playlist_ServicesDiscoveryRemove( p_playlist,
476 priv->pp_sds[0]->psz_name );