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 ***********************************************************************/
191 static void services_discovery_Destructor ( vlc_object_t *p_obj )
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 );
198 /***********************************************************************
200 ***********************************************************************/
202 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
204 return strdup( module_get_name( p_sd->p->p_module, true ) );
207 /***********************************************************************
209 ***********************************************************************/
210 vlc_event_manager_t *
211 services_discovery_EventManager ( services_discovery_t * p_sd )
213 return &p_sd->p->event_manager;
216 /***********************************************************************
218 ***********************************************************************/
220 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
221 const char * psz_category )
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;
228 vlc_event_send( &p_sd->p->event_manager, &event );
231 /***********************************************************************
233 ***********************************************************************/
235 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
238 event.type = vlc_ServicesDiscoveryItemRemoved;
239 event.u.services_discovery_item_removed.p_item = p_item;
241 vlc_event_send( &p_sd->p->event_manager, &event );
245 * Playlist - Services discovery bridge
248 struct vlc_playlist_sd_t
250 /* the playlist items for category and onelevel */
251 playlist_item_t *p_node;
252 services_discovery_t *p_sd; /**< Loaded service discovery modules */
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 )
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;
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)" );
269 /* If p_parent is in root category (this is clearly a hack) and we have a cat */
270 if( !EMPTY_STR(psz_cat) )
273 playlist_item_t * p_cat;
274 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
277 p_cat = playlist_NodeCreate( p_playlist, psz_cat,
279 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
284 playlist_NodeAddInput( p_playlist, p_input, p_parent,
285 PLAYLIST_APPEND, PLAYLIST_END,
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 )
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;
298 playlist_item_t *p_item =
299 playlist_ItemFindFromInputAndRoot( p_playlist, p_input,
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 );
311 playlist_NodeDelete( p_playlist, p_parent, true, true );
315 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,
316 const char *psz_name )
318 /* Perform the addition */
319 services_discovery_t *p_sd;
321 msg_Dbg( p_playlist, "adding services_discovery %s...", psz_name );
322 p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist), psz_name );
326 /* Free in playlist_ServicesDiscoveryRemove */
327 vlc_playlist_sd_t * p_sds = malloc( sizeof(*p_sds) );
330 vlc_sd_Destroy( p_sd );
334 playlist_item_t *p_node;
336 /* Look for configuration chain "longname" */
337 const char *psz_longname = "?";
340 config_chain_t *cfg = p_sd->p->p_cfg;
343 if( cfg->psz_name && !strcmp( cfg->psz_name, "longname" ) )
345 psz_longname = cfg->psz_value;
353 p_node = playlist_NodeCreate( p_playlist, psz_longname,
354 p_playlist->p_root, 0, NULL );
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 );
361 vlc_event_attach( em, vlc_ServicesDiscoveryItemRemoved,
362 playlist_sd_item_removed, p_node );
364 if( !vlc_sd_Start( p_sd ) )
366 vlc_sd_Destroy( p_sd );
371 /* We want tree-view for service directory */
372 p_node->p_input->b_prefers_tree = true;
374 p_sds->p_node = p_node;
375 p_sds->psz_name = strdup( psz_name );
378 TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
384 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
385 const char *psz_name )
387 playlist_private_t *priv = pl_priv( p_playlist );
388 vlc_playlist_sd_t * p_sds = NULL;
391 for( int i = 0; i < priv->i_sds; i++ )
393 if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) )
395 p_sds = priv->pp_sds[i];
396 REMOVE_ELEM( priv->pp_sds, priv->i_sds, i );
404 msg_Warn( p_playlist, "discovery %s is not loaded", psz_name );
408 services_discovery_t *p_sd = p_sds->p_sd;
413 vlc_event_detach( services_discovery_EventManager( p_sd ),
414 vlc_ServicesDiscoveryItemAdded,
415 playlist_sd_item_added,
418 vlc_event_detach( services_discovery_EventManager( p_sd ),
419 vlc_ServicesDiscoveryItemRemoved,
420 playlist_sd_item_removed,
423 /* Remove the sd playlist node if it exists */
425 playlist_NodeDelete( p_playlist, p_sds->p_node, true, false );
428 vlc_sd_Destroy( p_sd );
429 free( p_sds->psz_name );
435 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
436 const char *psz_name )
438 playlist_private_t *priv = pl_priv( p_playlist );
442 for( int i = 0; i < priv->i_sds; i++ )
444 vlc_playlist_sd_t *sd = priv->pp_sds[i];
446 if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
456 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
458 playlist_private_t *priv = pl_priv( p_playlist );
460 while( priv->i_sds > 0 )
461 playlist_ServicesDiscoveryRemove( p_playlist,
462 priv->pp_sds[0]->psz_name );