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"
42 int vlc_sd_probe_Add (vlc_probe_t *probe, const char *name,
45 vlc_sd_probe_t names = { strdup(name), strdup(longname) };
47 if (unlikely (names.name == NULL || names.longname == NULL
48 || vlc_probe_add (probe, &names, sizeof (names))))
51 free (names.longname);
54 return VLC_PROBE_CONTINUE;
57 #undef vlc_sd_GetNames
60 * Gets the list of available services discovery plugins.
62 char **vlc_sd_GetNames (vlc_object_t *obj, char ***pppsz_longnames)
65 vlc_sd_probe_t *tab = vlc_probe (obj, "services probe", &count);
73 char **names = malloc (sizeof(char *) * (count + 1));
74 char **longnames = malloc (sizeof(char *) * (count + 1));
76 if (unlikely (names == NULL || longnames == NULL))
78 for( size_t i = 0; i < count; i++ )
80 names[i] = tab[i].name;
81 longnames[i] = tab[i].longname;
84 names[count] = longnames[count] = NULL;
85 *pppsz_longnames = longnames;
90 struct vlc_sd_internal_t
92 /* the playlist items for category and onelevel */
93 playlist_item_t *p_cat;
94 playlist_item_t *p_one;
95 services_discovery_t *p_sd; /**< Loaded service discovery modules */
99 static void services_discovery_Destructor ( vlc_object_t *p_obj );
103 * Basically you just listen to Service discovery event through the
104 * sd's event manager.
105 * That's how the playlist get's Service Discovery information
108 /***********************************************************************
110 ***********************************************************************/
111 services_discovery_t *vlc_sd_Create( vlc_object_t *p_super )
113 services_discovery_t *p_sd;
115 p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
116 "services discovery" );
120 vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
121 vlc_event_manager_register_event_type( &p_sd->event_manager,
122 vlc_ServicesDiscoveryItemAdded );
123 vlc_event_manager_register_event_type( &p_sd->event_manager,
124 vlc_ServicesDiscoveryItemRemoved );
125 vlc_event_manager_register_event_type( &p_sd->event_manager,
126 vlc_ServicesDiscoveryStarted );
127 vlc_event_manager_register_event_type( &p_sd->event_manager,
128 vlc_ServicesDiscoveryEnded );
130 vlc_object_set_destructor( p_sd, services_discovery_Destructor );
131 vlc_object_attach( p_sd, p_super );
136 /***********************************************************************
138 ***********************************************************************/
139 bool vlc_sd_Start ( services_discovery_t * p_sd, const char *module )
141 assert(!p_sd->p_module);
143 p_sd->p_module = module_need( p_sd, "services_discovery", module, true );
145 if( p_sd->p_module == NULL )
147 msg_Err( p_sd, "no suitable services discovery module" );
151 vlc_event_t event = {
152 .type = vlc_ServicesDiscoveryStarted
154 vlc_event_send( &p_sd->event_manager, &event );
158 /***********************************************************************
160 ***********************************************************************/
161 void vlc_sd_Stop ( services_discovery_t * p_sd )
163 vlc_event_t event = {
164 .type = vlc_ServicesDiscoveryEnded
167 vlc_event_send( &p_sd->event_manager, &event );
169 module_unneed( p_sd, p_sd->p_module );
170 p_sd->p_module = NULL;
173 /***********************************************************************
175 ***********************************************************************/
176 static void services_discovery_Destructor ( vlc_object_t *p_obj )
178 services_discovery_t * p_sd = (services_discovery_t *)p_obj;
179 assert(!p_sd->p_module); /* Forgot to call Stop */
180 vlc_event_manager_fini( &p_sd->event_manager );
183 /***********************************************************************
185 ***********************************************************************/
187 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
189 return strdup( module_get_name( p_sd->p_module, true ) );
192 /***********************************************************************
194 ***********************************************************************/
195 vlc_event_manager_t *
196 services_discovery_EventManager ( services_discovery_t * p_sd )
198 return &p_sd->event_manager;
201 /***********************************************************************
203 ***********************************************************************/
205 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
206 const char * psz_category )
209 event.type = vlc_ServicesDiscoveryItemAdded;
210 event.u.services_discovery_item_added.p_new_item = p_item;
211 event.u.services_discovery_item_added.psz_category = psz_category;
213 vlc_event_send( &p_sd->event_manager, &event );
216 /***********************************************************************
218 ***********************************************************************/
220 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
223 event.type = vlc_ServicesDiscoveryItemRemoved;
224 event.u.services_discovery_item_removed.p_item = p_item;
226 vlc_event_send( &p_sd->event_manager, &event );
230 * Playlist - Services discovery bridge
233 /* A new item has been added to a certain sd */
234 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
236 input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
237 const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
238 playlist_item_t * p_parent = user_data;
239 playlist_t * p_playlist = p_parent->p_playlist;
241 msg_Dbg( p_playlist, "Adding %s in %s",
242 p_input->psz_name ? p_input->psz_name : "(null)",
243 psz_cat ? psz_cat : "(null)" );
246 /* If p_parent is in root category (this is clearly a hack) and we have a cat */
247 if( !EMPTY_STR(psz_cat) &&
248 p_parent->p_parent == p_playlist->p_root_category )
251 playlist_item_t * p_cat;
252 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
255 p_cat = playlist_NodeCreate( p_playlist, psz_cat,
257 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
262 playlist_BothAddInput( p_playlist, p_input, p_parent,
263 PLAYLIST_APPEND, PLAYLIST_END,
264 NULL, NULL, pl_Locked );
268 /* A new item has been removed from a certain sd */
269 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
271 input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
272 playlist_item_t * p_parent = user_data;
273 playlist_DeleteFromInput( p_parent->p_playlist, p_input, false );
276 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_module )
278 /* Perform the addition */
279 msg_Dbg( p_playlist, "adding services_discovery %s...", psz_module );
281 services_discovery_t *p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist) );
285 module_t *m = module_find_by_shortcut( psz_module );
288 msg_Err( p_playlist, "No such module: %s", psz_module );
289 vlc_sd_Destroy( p_sd );
293 /* Free in playlist_ServicesDiscoveryRemove */
294 vlc_sd_internal_t * p_sds = malloc( sizeof(*p_sds) );
297 vlc_sd_Destroy( p_sd );
302 playlist_item_t * p_cat;
303 playlist_item_t * p_one;
306 playlist_NodesPairCreate( p_playlist, module_get_name( m, true ),
307 &p_cat, &p_one, false );
311 vlc_event_attach( services_discovery_EventManager( p_sd ),
312 vlc_ServicesDiscoveryItemAdded,
313 playlist_sd_item_added, p_cat );
315 vlc_event_attach( services_discovery_EventManager( p_sd ),
316 vlc_ServicesDiscoveryItemRemoved,
317 playlist_sd_item_removed, p_cat );
319 if( !vlc_sd_Start( p_sd, psz_module ) )
321 vlc_sd_Destroy( p_sd );
326 /* We want tree-view for service directory */
327 p_one->p_input->b_prefers_tree = true;
329 p_sds->p_one = p_one;
330 p_sds->p_cat = p_cat;
331 p_sds->psz_name = strdup( psz_module );
334 TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
340 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
341 const char *psz_name )
343 playlist_private_t *priv = pl_priv( p_playlist );
344 vlc_sd_internal_t * p_sds = NULL;
347 for( int i = 0; i < priv->i_sds; i++ )
349 if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) )
351 p_sds = priv->pp_sds[i];
352 REMOVE_ELEM( priv->pp_sds, priv->i_sds, i );
360 msg_Warn( p_playlist, "discovery %s is not loaded", psz_name );
364 services_discovery_t *p_sd = p_sds->p_sd;
369 vlc_event_detach( services_discovery_EventManager( p_sd ),
370 vlc_ServicesDiscoveryItemAdded,
371 playlist_sd_item_added,
374 vlc_event_detach( services_discovery_EventManager( p_sd ),
375 vlc_ServicesDiscoveryItemRemoved,
376 playlist_sd_item_removed,
379 /* Remove the sd playlist node if it exists */
381 if( p_sds->p_cat != p_playlist->p_root_category &&
382 p_sds->p_one != p_playlist->p_root_onelevel )
384 playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
385 playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
389 vlc_sd_Destroy( p_sd );
390 free( p_sds->psz_name );
396 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
397 const char *psz_name )
399 playlist_private_t *priv = pl_priv( p_playlist );
403 for( int i = 0; i < priv->i_sds; i++ )
405 vlc_sd_internal_t *sd = priv->pp_sds[i];
407 if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
417 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
419 playlist_private_t *priv = pl_priv( p_playlist );
421 while( priv->i_sds > 0 )
422 playlist_ServicesDiscoveryRemove( p_playlist,
423 priv->pp_sds[0]->psz_name );