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 #undef vlc_sd_GetNames
61 * Gets the list of available services discovery plugins.
63 char **vlc_sd_GetNames (vlc_object_t *obj, char ***pppsz_longnames, int **pp_categories)
66 vlc_sd_probe_t *tab = vlc_probe (obj, "services probe", &count);
74 char **names = malloc (sizeof(char *) * (count + 1));
75 char **longnames = malloc (sizeof(char *) * (count + 1));
76 int *categories = malloc(sizeof(int) * (count + 1));
78 if (unlikely (names == NULL || longnames == NULL || categories == NULL))
80 for( size_t i = 0; i < count; i++ )
82 names[i] = tab[i].name;
83 longnames[i] = tab[i].longname;
84 categories[i] = tab[i].category;
87 names[count] = longnames[count] = NULL;
88 categories[count] = 0;
89 *pppsz_longnames = longnames;
90 if( pp_categories ) *pp_categories = categories;
91 else free( categories );
96 struct vlc_sd_internal_t
98 /* the playlist items for category and onelevel */
99 playlist_item_t *p_node;
100 services_discovery_t *p_sd; /**< Loaded service discovery modules */
104 static void services_discovery_Destructor ( vlc_object_t *p_obj );
108 * Basically you just listen to Service discovery event through the
109 * sd's event manager.
110 * That's how the playlist get's Service Discovery information
113 /***********************************************************************
115 ***********************************************************************/
116 services_discovery_t *vlc_sd_Create( vlc_object_t *p_super,
119 services_discovery_t *p_sd;
121 p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
122 "services discovery" );
125 free(config_ChainCreate( &p_sd->psz_name, &p_sd->p_cfg, cfg ));
127 vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
128 vlc_event_manager_register_event_type( &p_sd->event_manager,
129 vlc_ServicesDiscoveryItemAdded );
130 vlc_event_manager_register_event_type( &p_sd->event_manager,
131 vlc_ServicesDiscoveryItemRemoved );
132 vlc_event_manager_register_event_type( &p_sd->event_manager,
133 vlc_ServicesDiscoveryStarted );
134 vlc_event_manager_register_event_type( &p_sd->event_manager,
135 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_module = module_need( p_sd, "services_discovery",
151 p_sd->psz_name, true );
152 if( p_sd->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->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->event_manager, &event );
176 module_unneed( p_sd, p_sd->p_module );
177 p_sd->p_module = NULL;
180 void vlc_sd_Destroy( services_discovery_t *p_sd )
182 config_ChainDestroy( p_sd->p_cfg );
183 free( p_sd->psz_name );
184 vlc_object_release( p_sd );
187 /***********************************************************************
189 ***********************************************************************/
190 static void services_discovery_Destructor ( vlc_object_t *p_obj )
192 services_discovery_t * p_sd = (services_discovery_t *)p_obj;
193 assert(!p_sd->p_module); /* Forgot to call Stop */
194 vlc_event_manager_fini( &p_sd->event_manager );
197 /***********************************************************************
199 ***********************************************************************/
201 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
203 return strdup( module_get_name( p_sd->p_module, true ) );
206 /***********************************************************************
208 ***********************************************************************/
209 vlc_event_manager_t *
210 services_discovery_EventManager ( services_discovery_t * p_sd )
212 return &p_sd->event_manager;
215 /***********************************************************************
217 ***********************************************************************/
219 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
220 const char * psz_category )
223 event.type = vlc_ServicesDiscoveryItemAdded;
224 event.u.services_discovery_item_added.p_new_item = p_item;
225 event.u.services_discovery_item_added.psz_category = psz_category;
227 vlc_event_send( &p_sd->event_manager, &event );
230 /***********************************************************************
232 ***********************************************************************/
234 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
237 event.type = vlc_ServicesDiscoveryItemRemoved;
238 event.u.services_discovery_item_removed.p_item = p_item;
240 vlc_event_send( &p_sd->event_manager, &event );
244 * Playlist - Services discovery bridge
247 /* A new item has been added to a certain sd */
248 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
250 input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
251 const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
252 playlist_item_t * p_parent = user_data;
253 playlist_t * p_playlist = p_parent->p_playlist;
255 msg_Dbg( p_playlist, "Adding %s in %s",
256 p_input->psz_name ? p_input->psz_name : "(null)",
257 psz_cat ? psz_cat : "(null)" );
260 /* If p_parent is in root category (this is clearly a hack) and we have a cat */
261 if( !EMPTY_STR(psz_cat) )
264 playlist_item_t * p_cat;
265 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
268 p_cat = playlist_NodeCreate( p_playlist, psz_cat,
270 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
275 playlist_NodeAddInput( p_playlist, p_input, p_parent,
276 PLAYLIST_APPEND, PLAYLIST_END,
281 /* A new item has been removed from a certain sd */
282 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
284 input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
285 playlist_item_t * p_parent = user_data;
286 playlist_DeleteFromInput( p_parent->p_playlist, p_input, false );
289 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,
290 const char *psz_name )
292 /* Perform the addition */
293 services_discovery_t *p_sd;
295 msg_Dbg( p_playlist, "adding services_discovery %s...", psz_name );
296 p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist), psz_name );
300 /* Free in playlist_ServicesDiscoveryRemove */
301 vlc_sd_internal_t * p_sds = malloc( sizeof(*p_sds) );
304 vlc_sd_Destroy( p_sd );
308 playlist_item_t *p_node;
310 /* Look for configuration chain "longname" */
311 const char *psz_longname = "?";
314 config_chain_t *cfg = p_sd->p_cfg;
317 if( cfg->psz_name && !strcmp( cfg->psz_name, "longname" ) )
319 psz_longname = cfg->psz_value;
327 p_node = playlist_NodeCreate( p_playlist, psz_longname,
328 p_playlist->p_root, 0, NULL );
331 vlc_event_attach( services_discovery_EventManager( p_sd ),
332 vlc_ServicesDiscoveryItemAdded,
333 playlist_sd_item_added, p_node );
335 vlc_event_attach( services_discovery_EventManager( p_sd ),
336 vlc_ServicesDiscoveryItemRemoved,
337 playlist_sd_item_removed, p_node );
339 if( !vlc_sd_Start( p_sd ) )
341 vlc_sd_Destroy( p_sd );
346 /* We want tree-view for service directory */
347 p_node->p_input->b_prefers_tree = true;
349 p_sds->p_node = p_node;
350 p_sds->psz_name = strdup( psz_name );
353 TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
359 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
360 const char *psz_name )
362 playlist_private_t *priv = pl_priv( p_playlist );
363 vlc_sd_internal_t * p_sds = NULL;
366 for( int i = 0; i < priv->i_sds; i++ )
368 if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) )
370 p_sds = priv->pp_sds[i];
371 REMOVE_ELEM( priv->pp_sds, priv->i_sds, i );
379 msg_Warn( p_playlist, "discovery %s is not loaded", psz_name );
383 services_discovery_t *p_sd = p_sds->p_sd;
388 vlc_event_detach( services_discovery_EventManager( p_sd ),
389 vlc_ServicesDiscoveryItemAdded,
390 playlist_sd_item_added,
393 vlc_event_detach( services_discovery_EventManager( p_sd ),
394 vlc_ServicesDiscoveryItemRemoved,
395 playlist_sd_item_removed,
398 /* Remove the sd playlist node if it exists */
400 playlist_NodeDelete( p_playlist, p_sds->p_node, true, false );
403 vlc_sd_Destroy( p_sd );
404 free( p_sds->psz_name );
410 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
411 const char *psz_name )
413 playlist_private_t *priv = pl_priv( p_playlist );
417 for( int i = 0; i < priv->i_sds; i++ )
419 vlc_sd_internal_t *sd = priv->pp_sds[i];
421 if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
431 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
433 playlist_private_t *priv = pl_priv( p_playlist );
435 while( priv->i_sds > 0 )
436 playlist_ServicesDiscoveryRemove( p_playlist,
437 priv->pp_sds[0]->psz_name );