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 "playlist_internal.h"
32 #include "../libvlc.h"
34 static void RunSD( services_discovery_t *p_sd );
38 * Basically you just listen to Service discovery event through the
40 * That's how the playlist get's Service Discovery information
43 /***********************************************************************
45 ***********************************************************************/
46 char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
47 char ***pppsz_longnames )
49 return module_GetModulesNamesForCapability( p_super, "services_discovery",
53 /***********************************************************************
55 ***********************************************************************/
56 services_discovery_t *
57 services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
59 services_discovery_t *p_sd;
60 p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
61 "services discovery" );
66 p_sd->psz_localized_name = NULL;
68 vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
69 vlc_event_manager_register_event_type( &p_sd->event_manager,
70 vlc_ServicesDiscoveryItemAdded );
71 vlc_event_manager_register_event_type( &p_sd->event_manager,
72 vlc_ServicesDiscoveryItemRemoved );
73 vlc_event_manager_register_event_type( &p_sd->event_manager,
74 vlc_ServicesDiscoveryStarted );
75 vlc_event_manager_register_event_type( &p_sd->event_manager,
76 vlc_ServicesDiscoveryEnded );
78 p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, true );
80 if( p_sd->p_module == NULL )
82 msg_Err( p_super, "no suitable services discovery module" );
83 vlc_object_release( p_sd );
86 p_sd->psz_module = strdup( psz_module_name );
87 p_sd->b_die = false; /* FIXME */
89 vlc_object_attach( p_sd, p_super );
93 /***********************************************************************
95 ***********************************************************************/
96 void services_discovery_Destroy ( services_discovery_t * p_sd )
98 vlc_event_manager_fini( &p_sd->event_manager );
100 free( p_sd->psz_module );
101 free( p_sd->psz_localized_name );
103 vlc_object_detach( p_sd );
104 vlc_object_release( p_sd );
107 /***********************************************************************
109 ***********************************************************************/
110 int services_discovery_Start ( services_discovery_t * p_sd )
112 if ((p_sd->pf_run != NULL)
113 && vlc_thread_create( p_sd, "services_discovery", RunSD,
114 VLC_THREAD_PRIORITY_LOW, false))
116 msg_Err( p_sd, "cannot create services discovery thread" );
117 vlc_object_release( p_sd );
123 /***********************************************************************
125 ***********************************************************************/
126 static void ObjectKillChildrens( vlc_object_t *p_obj )
130 vlc_object_kill( p_obj );
132 p_list = vlc_list_children( p_obj );
133 for( i = 0; i < p_list->i_count; i++ )
134 ObjectKillChildrens( p_list->p_values[i].p_object );
135 vlc_list_release( p_list );
138 void services_discovery_Stop ( services_discovery_t * p_sd )
140 ObjectKillChildrens( VLC_OBJECT(p_sd) );
141 if( p_sd->pf_run ) vlc_thread_join( p_sd );
143 module_Unneed( p_sd, p_sd->p_module );
146 /***********************************************************************
148 ***********************************************************************/
150 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
152 return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
155 /***********************************************************************
157 ***********************************************************************/
159 services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
161 free( p_sd->psz_localized_name );
162 p_sd->psz_localized_name = strdup(psz);
165 /***********************************************************************
167 ***********************************************************************/
168 vlc_event_manager_t *
169 services_discovery_EventManager ( services_discovery_t * p_sd )
171 return &p_sd->event_manager;
174 /***********************************************************************
176 ***********************************************************************/
178 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
179 const char * psz_category )
182 event.type = vlc_ServicesDiscoveryItemAdded;
183 event.u.services_discovery_item_added.p_new_item = p_item;
184 event.u.services_discovery_item_added.psz_category = psz_category;
186 vlc_event_send( &p_sd->event_manager, &event );
189 /***********************************************************************
191 ***********************************************************************/
193 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
196 event.type = vlc_ServicesDiscoveryItemRemoved;
197 event.u.services_discovery_item_removed.p_item = p_item;
199 vlc_event_send( &p_sd->event_manager, &event );
202 /***********************************************************************
204 ***********************************************************************/
205 static void RunSD( services_discovery_t *p_sd )
209 event.type = vlc_ServicesDiscoveryStarted;
210 vlc_event_send( &p_sd->event_manager, &event );
212 p_sd->pf_run( p_sd );
214 event.type = vlc_ServicesDiscoveryEnded;
215 vlc_event_send( &p_sd->event_manager, &event );
220 * Playlist - Services discovery bridge
223 /* A new item has been added to a certain sd */
224 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
226 input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
227 const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
228 playlist_item_t *p_new_item, * p_parent = user_data;
229 playlist_t * p_playlist = p_parent->p_playlist;
231 msg_Dbg( p_playlist, "Adding %s in %s",
232 p_input->psz_name ? p_input->psz_name : "(null)",
233 psz_cat ? psz_cat : "(null)" );
236 /* If p_parent is in root category (this is clearly a hack) and we have a cat */
237 if( !EMPTY_STR(psz_cat) &&
238 p_parent->p_parent == p_playlist->p_root_category )
241 playlist_item_t * p_cat;
242 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
245 p_cat = playlist_NodeCreate( p_playlist, psz_cat,
247 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
252 p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
253 PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
256 p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
257 p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
262 /* A new item has been removed from a certain sd */
263 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
265 input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
266 playlist_item_t * p_parent = user_data;
267 playlist_item_t * p_pl_item;
269 /* First make sure that if item is a node it will be deleted.
270 * XXX: Why don't we have a function to ensure that in the playlist code ? */
271 vlc_object_lock( p_parent->p_playlist );
272 p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
273 p_input->i_id, p_parent, false );
275 if( p_pl_item && p_pl_item->i_children > -1 )
277 playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
278 vlc_object_unlock( p_parent->p_playlist );
282 /* Delete the non-node item normally */
283 playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
284 p_parent, pl_Locked );
286 vlc_object_unlock( p_parent->p_playlist );
289 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modules )
291 const char *psz_parser = psz_modules ?: "";
292 int retval = VLC_SUCCESS;
296 struct playlist_services_discovery_support_t * p_sds;
297 playlist_item_t * p_cat;
298 playlist_item_t * p_one;
300 while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
303 if( *psz_parser == '\0' )
306 const char *psz_next = strchr( psz_parser, ':' );
307 if( psz_next == NULL )
308 psz_next = psz_parser + strlen( psz_parser );
310 char psz_plugin[psz_next - psz_parser + 1];
311 memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
312 psz_plugin[sizeof (psz_plugin) - 1] = '\0';
313 psz_parser = psz_next;
315 /* Perform the addition */
316 msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
317 services_discovery_t *p_sd;
319 p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
323 char * psz = services_discovery_GetLocalizedName( p_sd );
326 playlist_NodesPairCreate( p_playlist, psz,
327 &p_cat, &p_one, false );
331 vlc_event_attach( services_discovery_EventManager( p_sd ),
332 vlc_ServicesDiscoveryItemAdded,
333 playlist_sd_item_added,
336 vlc_event_attach( services_discovery_EventManager( p_sd ),
337 vlc_ServicesDiscoveryItemAdded,
338 playlist_sd_item_added,
341 vlc_event_attach( services_discovery_EventManager( p_sd ),
342 vlc_ServicesDiscoveryItemRemoved,
343 playlist_sd_item_removed,
346 vlc_event_attach( services_discovery_EventManager( p_sd ),
347 vlc_ServicesDiscoveryItemRemoved,
348 playlist_sd_item_removed,
351 services_discovery_Start( p_sd );
353 /* Free in playlist_ServicesDiscoveryRemove */
354 p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
358 p_sds->p_one = p_one;
359 p_sds->p_cat = p_cat;
362 TAB_APPEND( p_playlist->i_sds, p_playlist->pp_sds, p_sds );
369 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
370 const char *psz_module )
372 struct playlist_services_discovery_support_t * p_sds = NULL;
376 for( i = 0 ; i< p_playlist->i_sds ; i ++ )
378 if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
380 p_sds = p_playlist->pp_sds[i];
381 REMOVE_ELEM( p_playlist->pp_sds, p_playlist->i_sds, i );
387 if( !p_sds || !p_sds->p_sd )
389 msg_Warn( p_playlist, "module %s is not loaded", psz_module );
393 services_discovery_Stop( p_sds->p_sd );
395 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
396 vlc_ServicesDiscoveryItemAdded,
397 playlist_sd_item_added,
400 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
401 vlc_ServicesDiscoveryItemAdded,
402 playlist_sd_item_added,
405 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
406 vlc_ServicesDiscoveryItemRemoved,
407 playlist_sd_item_removed,
410 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
411 vlc_ServicesDiscoveryItemRemoved,
412 playlist_sd_item_removed,
415 /* Remove the sd playlist node if it exists */
417 if( p_sds->p_cat != p_playlist->p_root_category &&
418 p_sds->p_one != p_playlist->p_root_onelevel )
420 playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
421 playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
425 services_discovery_Destroy( p_sds->p_sd );
431 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
432 const char *psz_module )
437 for( i = 0 ; i< p_playlist->i_sds ; i ++ )
439 if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
449 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
451 while( p_playlist->i_sds > 0 )
452 playlist_ServicesDiscoveryRemove( p_playlist,
453 p_playlist->pp_sds[0]->p_sd->psz_module );