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 *****************************************************************************/
24 #include "vlc_playlist.h"
25 #include "vlc_events.h"
26 #include "playlist_internal.h"
28 static void RunSD( services_discovery_t *p_sd );
32 * Basically you just listen to Service discovery event through the
34 * That's how the playlist get's Service Discovery information
37 /***********************************************************************
39 ***********************************************************************/
40 char ** services_discovery_GetServicesNames( vlc_object_t * p_super,
41 char ***pppsz_longnames )
43 return module_GetModulesNamesForCapability( p_super, "services_discovery",
47 /***********************************************************************
49 ***********************************************************************/
50 services_discovery_t *
51 services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
53 services_discovery_t *p_sd = vlc_object_create( p_super, VLC_OBJECT_SD );
58 p_sd->psz_localized_name = NULL;
60 vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
61 vlc_event_manager_register_event_type( &p_sd->event_manager,
62 vlc_ServicesDiscoveryItemAdded );
63 vlc_event_manager_register_event_type( &p_sd->event_manager,
64 vlc_ServicesDiscoveryItemRemoved );
66 p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, VLC_TRUE );
68 if( p_sd->p_module == NULL )
70 msg_Err( p_super, "no suitable services discovery module" );
71 vlc_object_destroy( p_sd );
74 p_sd->psz_module = strdup( psz_module_name );
75 p_sd->b_die = VLC_FALSE; /* FIXME */
77 vlc_object_attach( p_sd, p_super );
81 /***********************************************************************
83 ***********************************************************************/
84 void services_discovery_Destroy ( services_discovery_t * p_sd )
86 vlc_event_manager_fini( &p_sd->event_manager );
88 free( p_sd->psz_module );
89 free( p_sd->psz_localized_name );
91 vlc_object_detach( p_sd );
92 vlc_object_destroy( p_sd );
95 /***********************************************************************
97 ***********************************************************************/
98 int services_discovery_Start ( services_discovery_t * p_sd )
100 if ((p_sd->pf_run != NULL)
101 && vlc_thread_create( p_sd, "services_discovery", RunSD,
102 VLC_THREAD_PRIORITY_LOW, VLC_FALSE))
104 msg_Err( p_sd, "cannot create services discovery thread" );
105 vlc_object_destroy( p_sd );
111 /***********************************************************************
113 ***********************************************************************/
114 void services_discovery_Stop ( services_discovery_t * p_sd )
116 vlc_object_kill( p_sd );
117 if( p_sd->pf_run ) vlc_thread_join( p_sd );
119 module_Unneed( p_sd, p_sd->p_module );
122 /***********************************************************************
124 ***********************************************************************/
126 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
128 return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
131 /***********************************************************************
133 ***********************************************************************/
135 services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
137 if(p_sd->psz_localized_name) free( p_sd->psz_localized_name );
138 p_sd->psz_localized_name = strdup(psz);
141 /***********************************************************************
143 ***********************************************************************/
144 vlc_event_manager_t *
145 services_discovery_EventManager ( services_discovery_t * p_sd )
147 return &p_sd->event_manager;
150 /***********************************************************************
152 ***********************************************************************/
154 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
155 const char * psz_category )
158 event.type = vlc_ServicesDiscoveryItemAdded;
159 event.u.services_discovery_item_added.p_new_item = p_item;
160 event.u.services_discovery_item_added.psz_category = psz_category;
162 vlc_event_send( &p_sd->event_manager, &event );
165 /***********************************************************************
167 ***********************************************************************/
169 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
172 event.type = vlc_ServicesDiscoveryItemRemoved;
173 event.u.services_discovery_item_removed.p_item = p_item;
175 vlc_event_send( &p_sd->event_manager, &event );
178 /***********************************************************************
180 ***********************************************************************/
181 static void RunSD( services_discovery_t *p_sd )
183 p_sd->pf_run( p_sd );
188 * Playlist - Services discovery bridge
191 /* A new item has been added to a certain sd */
192 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
194 input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
195 const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
196 playlist_item_t *p_new_item, * p_parent = user_data;
198 msg_Dbg( p_parent->p_playlist, "Adding %s in %s",
199 p_input->psz_name ? p_input->psz_name : "(null)",
200 psz_cat ? psz_cat : "(null)" );
202 /* If p_parent is in root category (this is clearly a hack) and we have a cat */
203 if( !EMPTY_STR(psz_cat) &&
204 p_parent->p_parent == p_parent->p_playlist->p_root_category )
207 playlist_item_t * p_cat;
208 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
211 p_cat = playlist_NodeCreate( p_parent->p_playlist, psz_cat,
213 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
218 p_new_item = playlist_NodeAddInput( p_parent->p_playlist, p_input, p_parent,
219 PLAYLIST_APPEND, PLAYLIST_END, VLC_FALSE );
220 p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
221 p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
224 /* A new item has been removed from a certain sd */
225 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
227 input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
228 playlist_item_t * p_parent = user_data;
229 playlist_item_t * p_pl_item;
231 /* First make sure that if item is a node it will be deleted.
232 * XXX: Why don't we have a function to ensure that in the playlist code ? */
233 vlc_object_lock( p_parent->p_playlist );
234 p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
235 p_input->i_id, p_parent, VLC_FALSE );
237 if( p_pl_item && p_pl_item->i_children > -1 )
239 playlist_NodeDelete( p_parent->p_playlist, p_pl_item, VLC_TRUE, VLC_FALSE );
240 vlc_object_unlock( p_parent->p_playlist );
243 vlc_object_unlock( p_parent->p_playlist );
245 /* Delete the non-node item normally */
246 playlist_DeleteInputInParent( p_parent->p_playlist, p_input->i_id,
247 p_parent, VLC_FALSE );
250 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modules )
252 const char *psz_parser = psz_modules ?: "";
253 int retval = VLC_SUCCESS;
257 struct playlist_services_discovery_support_t * p_sds;
258 playlist_item_t * p_cat;
259 playlist_item_t * p_one;
261 while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
264 if( *psz_parser == '\0' )
267 const char *psz_next = strchr( psz_parser, ':' );
268 if( psz_next == NULL )
269 psz_next = psz_parser + strlen( psz_parser );
271 char psz_plugin[psz_next - psz_parser + 1];
272 memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
273 psz_plugin[sizeof (psz_plugin) - 1] = '\0';
274 psz_parser = psz_next;
276 /* Perform the addition */
277 msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
278 services_discovery_t *p_sd;
280 p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
284 char * psz = services_discovery_GetLocalizedName( p_sd );
287 playlist_NodesPairCreate( p_playlist, psz,
288 &p_cat, &p_one, VLC_FALSE );
293 /* No name, just add at the top of the playlist */
295 p_cat = p_playlist->p_root_category;
296 p_one = p_playlist->p_root_onelevel;
300 vlc_event_attach( services_discovery_EventManager( p_sd ),
301 vlc_ServicesDiscoveryItemAdded,
302 playlist_sd_item_added,
305 vlc_event_attach( services_discovery_EventManager( p_sd ),
306 vlc_ServicesDiscoveryItemAdded,
307 playlist_sd_item_added,
310 vlc_event_attach( services_discovery_EventManager( p_sd ),
311 vlc_ServicesDiscoveryItemRemoved,
312 playlist_sd_item_removed,
315 vlc_event_attach( services_discovery_EventManager( p_sd ),
316 vlc_ServicesDiscoveryItemRemoved,
317 playlist_sd_item_removed,
320 services_discovery_Start( p_sd );
322 /* Free in playlist_ServicesDiscoveryRemove */
323 p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
326 msg_Err( p_playlist, "No more memory" );
330 p_sds->p_one = p_one;
331 p_sds->p_cat = p_cat;
334 TAB_APPEND( p_playlist->i_sds, p_playlist->pp_sds, p_sds );
341 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
342 const char *psz_module )
344 struct playlist_services_discovery_support_t * p_sds = NULL;
348 for( i = 0 ; i< p_playlist->i_sds ; i ++ )
350 if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
352 p_sds = p_playlist->pp_sds[i];
353 REMOVE_ELEM( p_playlist->pp_sds, p_playlist->i_sds, i );
359 if( !p_sds || !p_sds->p_sd )
361 msg_Warn( p_playlist, "module %s is not loaded", psz_module );
365 services_discovery_Stop( p_sds->p_sd );
367 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
368 vlc_ServicesDiscoveryItemAdded,
369 playlist_sd_item_added,
372 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
373 vlc_ServicesDiscoveryItemAdded,
374 playlist_sd_item_added,
377 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
378 vlc_ServicesDiscoveryItemRemoved,
379 playlist_sd_item_removed,
382 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
383 vlc_ServicesDiscoveryItemRemoved,
384 playlist_sd_item_removed,
387 /* Remove the sd playlist node if it exists */
389 if( p_sds->p_cat != p_playlist->p_root_category &&
390 p_sds->p_one != p_playlist->p_root_onelevel )
392 playlist_NodeDelete( p_playlist, p_sds->p_cat, VLC_TRUE, VLC_FALSE );
393 playlist_NodeDelete( p_playlist, p_sds->p_one, VLC_TRUE, VLC_FALSE );
397 services_discovery_Destroy( p_sds->p_sd );
402 vlc_bool_t playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
403 const char *psz_module )
408 for( i = 0 ; i< p_playlist->i_sds ; i ++ )
410 if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )