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 )
42 return module_GetModulesNamesForCapability( p_super, "services_discovery" );
45 /***********************************************************************
47 ***********************************************************************/
48 services_discovery_t *
49 services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
51 services_discovery_t *p_sd = vlc_object_create( p_super, VLC_OBJECT_SD );
56 p_sd->psz_localized_name = NULL;
58 vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
59 vlc_event_manager_register_event_type( &p_sd->event_manager,
60 vlc_ServicesDiscoveryItemAdded );
61 vlc_event_manager_register_event_type( &p_sd->event_manager,
62 vlc_ServicesDiscoveryItemRemoved );
64 p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, VLC_TRUE );
66 if( p_sd->p_module == NULL )
68 msg_Err( p_super, "no suitable services discovery module" );
69 vlc_object_destroy( p_sd );
72 p_sd->psz_module = strdup( psz_module_name );
73 p_sd->b_die = VLC_FALSE; /* FIXME */
75 vlc_object_attach( p_sd, p_super );
79 /***********************************************************************
81 ***********************************************************************/
82 void services_discovery_Destroy ( services_discovery_t * p_sd )
84 vlc_event_manager_fini( &p_sd->event_manager );
86 free( p_sd->psz_module );
87 free( p_sd->psz_localized_name );
89 vlc_object_detach( p_sd );
90 vlc_object_destroy( p_sd );
93 /***********************************************************************
95 ***********************************************************************/
96 int services_discovery_Start ( services_discovery_t * p_sd )
98 if ((p_sd->pf_run != NULL)
99 && vlc_thread_create( p_sd, "services_discovery", RunSD,
100 VLC_THREAD_PRIORITY_LOW, VLC_FALSE))
102 msg_Err( p_sd, "cannot create services discovery thread" );
103 vlc_object_destroy( p_sd );
109 /***********************************************************************
111 ***********************************************************************/
112 void services_discovery_Stop ( services_discovery_t * p_sd )
114 vlc_object_kill( p_sd );
115 if( p_sd->pf_run ) vlc_thread_join( p_sd );
117 module_Unneed( p_sd, p_sd->p_module );
120 /***********************************************************************
122 ***********************************************************************/
124 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
126 return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
129 /***********************************************************************
131 ***********************************************************************/
133 services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
135 if(p_sd->psz_localized_name) free( p_sd->psz_localized_name );
136 p_sd->psz_localized_name = strdup(psz);
139 /***********************************************************************
141 ***********************************************************************/
142 vlc_event_manager_t *
143 services_discovery_EventManager ( services_discovery_t * p_sd )
145 return &p_sd->event_manager;
148 /***********************************************************************
150 ***********************************************************************/
152 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
153 const char * psz_category )
156 event.type = vlc_ServicesDiscoveryItemAdded;
157 event.u.services_discovery_item_added.p_new_item = p_item;
158 event.u.services_discovery_item_added.psz_category = psz_category;
160 vlc_event_send( &p_sd->event_manager, &event );
163 /***********************************************************************
165 ***********************************************************************/
167 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
170 event.type = vlc_ServicesDiscoveryItemRemoved;
171 event.u.services_discovery_item_removed.p_item = p_item;
173 vlc_event_send( &p_sd->event_manager, &event );
176 /***********************************************************************
178 ***********************************************************************/
179 static void RunSD( services_discovery_t *p_sd )
181 p_sd->pf_run( p_sd );
186 * Playlist - Services discovery bridge
189 /* A new item has been added to a certain sd */
190 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
192 input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
193 const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
194 playlist_item_t *p_new_item, * p_parent = user_data;
196 msg_Dbg( p_parent->p_playlist, "Adding %s in %s",
197 p_input->psz_name ? p_input->psz_name : "(null)",
198 psz_cat ? psz_cat : "(null)" );
200 /* If p_parent is in root category (this is clearly a hack) and we have a cat */
201 if( !EMPTY_STR(psz_cat) &&
202 p_parent->p_parent == p_parent->p_playlist->p_root_category )
205 playlist_item_t * p_cat;
206 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
209 p_cat = playlist_NodeCreate( p_parent->p_playlist, psz_cat,
211 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
216 p_new_item = playlist_NodeAddInput( p_parent->p_playlist, p_input, p_parent,
217 PLAYLIST_APPEND, PLAYLIST_END, VLC_FALSE );
218 p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
219 p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
222 /* A new item has been removed from a certain sd */
223 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
225 input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
226 playlist_item_t * p_parent = user_data;
228 playlist_DeleteInputInParent( p_parent->p_playlist, p_input->i_id,
229 p_parent, VLC_FALSE );
232 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modules )
234 const char *psz_parser = psz_modules ?: "";
235 int retval = VLC_SUCCESS;
239 struct playlist_archived_services_discovery_t * p_asd;
240 playlist_item_t * p_cat;
241 playlist_item_t * p_one;
243 while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
246 if( *psz_parser == '\0' )
249 const char *psz_next = strchr( psz_parser, ':' );
250 if( psz_next == NULL )
251 psz_next = psz_parser + strlen( psz_parser );
253 char psz_plugin[psz_next - psz_parser + 1];
254 memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
255 psz_plugin[sizeof (psz_plugin) - 1] = '\0';
256 psz_parser = psz_next;
258 /* Perform the addition */
259 msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
260 services_discovery_t *p_sd;
262 p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
266 char * psz = services_discovery_GetLocalizedName( p_sd );
269 playlist_NodesPairCreate( p_playlist, psz,
270 &p_cat, &p_one, VLC_FALSE );
275 /* No name, just add at the top of the playlist */
277 p_cat = p_playlist->p_root_category;
278 p_one = p_playlist->p_root_onelevel;
284 vlc_event_attach( services_discovery_EventManager( p_sd ),
285 vlc_ServicesDiscoveryItemAdded,
286 playlist_sd_item_added,
289 vlc_event_attach( services_discovery_EventManager( p_sd ),
290 vlc_ServicesDiscoveryItemAdded,
291 playlist_sd_item_added,
294 vlc_event_attach( services_discovery_EventManager( p_sd ),
295 vlc_ServicesDiscoveryItemRemoved,
296 playlist_sd_item_removed,
299 vlc_event_attach( services_discovery_EventManager( p_sd ),
300 vlc_ServicesDiscoveryItemRemoved,
301 playlist_sd_item_removed,
304 services_discovery_Start( p_sd );
307 TAB_APPEND( p_playlist->i_sd, p_playlist->pp_sd, p_sd );
314 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
315 const char *psz_module )
318 struct services_discovery_t *p_sd = NULL;
321 for( i = 0 ; i< p_playlist->i_sd ; i ++ )
323 if( !strcmp( psz_module, p_playlist->pp_sd[i]->psz_module ) )
325 p_sd = p_playlist->pp_sd[i];
326 REMOVE_ELEM( p_playlist->pp_sd, p_playlist->i_sd, i );
334 msg_Warn( p_playlist, "module %s is not loaded", psz_module );
338 services_discovery_Stop( p_sd );
340 vlc_event_detach( services_discovery_EventManager( p_sd ),
341 vlc_ServicesDiscoveryItemAdded,
342 playlist_sd_item_added,
345 vlc_event_detach( services_discovery_EventManager( p_sd ),
346 vlc_ServicesDiscoveryItemAdded,
347 playlist_sd_item_added,
350 vlc_event_detach( services_discovery_EventManager( p_sd ),
351 vlc_ServicesDiscoveryItemRemoved,
352 playlist_sd_item_removed,
355 vlc_event_detach( services_discovery_EventManager( p_sd ),
356 vlc_ServicesDiscoveryItemRemoved,
357 playlist_sd_item_removed,
360 /* Remove the sd playlist node if it exists */
362 if( p_sd->p_cat != p_playlist->p_root_category &&
363 p_sd->p_one != p_playlist->p_root_onelevel )
365 playlist_NodeDelete( p_playlist, p_sd->p_cat, VLC_TRUE, VLC_FALSE );
366 playlist_NodeDelete( p_playlist, p_sd->p_one, VLC_TRUE, VLC_FALSE );
370 services_discovery_Destroy( p_sd );
375 vlc_bool_t playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
376 const char *psz_module )
381 for( i = 0 ; i< p_playlist->i_sd ; i ++ )
383 if( !strcmp( psz_module, p_playlist->pp_sd[i]->psz_module ) )