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 "playlist_internal.h"
33 #include "../libvlc.h"
36 static void services_discovery_Destructor ( services_discovery_t * p_sd );
40 * Basically you just listen to Service discovery event through the
42 * That's how the playlist get's Service Discovery information
45 /***********************************************************************
47 ***********************************************************************/
48 char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
49 char ***pppsz_longnames )
51 return module_GetModulesNamesForCapability( "services_discovery",
55 /***********************************************************************
57 ***********************************************************************/
58 services_discovery_t *
59 services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
61 services_discovery_t *p_sd;
62 assert( psz_module_name );
63 p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
64 "services discovery" );
68 p_sd->psz_module = strdup( psz_module_name );
70 vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
71 vlc_event_manager_register_event_type( &p_sd->event_manager,
72 vlc_ServicesDiscoveryItemAdded );
73 vlc_event_manager_register_event_type( &p_sd->event_manager,
74 vlc_ServicesDiscoveryItemRemoved );
75 vlc_event_manager_register_event_type( &p_sd->event_manager,
76 vlc_ServicesDiscoveryStarted );
77 vlc_event_manager_register_event_type( &p_sd->event_manager,
78 vlc_ServicesDiscoveryEnded );
80 vlc_object_attach( p_sd, p_super );
85 static void ObjectKillChildrens( vlc_object_t *p_obj )
89 vlc_object_kill( p_obj );
91 p_list = vlc_list_children( p_obj );
92 for( i = 0; i < p_list->i_count; i++ )
93 ObjectKillChildrens( p_list->p_values[i].p_object );
94 vlc_list_release( p_list );
97 /***********************************************************************
99 ***********************************************************************/
100 bool services_discovery_Start ( services_discovery_t * p_sd )
102 assert(!p_sd->p_module);
104 p_sd->p_module = module_need( p_sd, "services_discovery", p_sd->psz_module, true );
106 if( p_sd->p_module == NULL )
108 msg_Err( p_sd, "no suitable services discovery module" );
112 vlc_event_t event = {
113 .type = vlc_ServicesDiscoveryStarted
115 vlc_event_send( &p_sd->event_manager, &event );
119 /***********************************************************************
121 ***********************************************************************/
122 void services_discovery_Stop ( services_discovery_t * p_sd )
124 vlc_event_t event = {
125 .type = vlc_ServicesDiscoveryEnded
128 ObjectKillChildrens( VLC_OBJECT(p_sd) );
130 vlc_event_send( &p_sd->event_manager, &event );
132 module_unneed( p_sd, p_sd->p_module );
133 p_sd->p_module = NULL;
136 /***********************************************************************
138 ***********************************************************************/
139 static void services_discovery_Destructor ( services_discovery_t * p_sd )
141 assert(!p_sd->p_module); /* Forgot to call Stop */
143 vlc_event_manager_fini( &p_sd->event_manager );
144 free( p_sd->psz_module );
145 vlc_object_release( p_sd );
148 /***********************************************************************
150 ***********************************************************************/
152 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
154 return strdup( module_get_name( p_sd->p_module, true ) );
157 /***********************************************************************
159 ***********************************************************************/
160 vlc_event_manager_t *
161 services_discovery_EventManager ( services_discovery_t * p_sd )
163 return &p_sd->event_manager;
166 /***********************************************************************
168 ***********************************************************************/
170 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
171 const char * psz_category )
174 event.type = vlc_ServicesDiscoveryItemAdded;
175 event.u.services_discovery_item_added.p_new_item = p_item;
176 event.u.services_discovery_item_added.psz_category = psz_category;
178 vlc_event_send( &p_sd->event_manager, &event );
181 /***********************************************************************
183 ***********************************************************************/
185 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
188 event.type = vlc_ServicesDiscoveryItemRemoved;
189 event.u.services_discovery_item_removed.p_item = p_item;
191 vlc_event_send( &p_sd->event_manager, &event );
195 * Playlist - Services discovery bridge
198 /* A new item has been added to a certain sd */
199 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
201 input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
202 const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
203 playlist_item_t *p_new_item, * p_parent = user_data;
204 playlist_t * p_playlist = p_parent->p_playlist;
206 msg_Dbg( p_playlist, "Adding %s in %s",
207 p_input->psz_name ? p_input->psz_name : "(null)",
208 psz_cat ? psz_cat : "(null)" );
211 /* If p_parent is in root category (this is clearly a hack) and we have a cat */
212 if( !EMPTY_STR(psz_cat) &&
213 p_parent->p_parent == p_playlist->p_root_category )
216 playlist_item_t * p_cat;
217 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
220 p_cat = playlist_NodeCreate( p_playlist, psz_cat,
222 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
227 p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
228 PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
231 p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
232 p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
237 /* A new item has been removed from a certain sd */
238 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
240 input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
241 playlist_item_t * p_parent = user_data;
242 playlist_item_t * p_pl_item;
244 /* First make sure that if item is a node it will be deleted.
245 * XXX: Why don't we have a function to ensure that in the playlist code ? */
246 vlc_object_lock( p_parent->p_playlist );
247 p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
248 p_input->i_id, p_parent, false );
250 if( p_pl_item && p_pl_item->i_children > -1 )
252 playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
253 vlc_object_unlock( p_parent->p_playlist );
257 /* Delete the non-node item normally */
258 playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
259 p_parent, pl_Locked );
261 vlc_object_unlock( p_parent->p_playlist );
264 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modules )
266 const char *psz_parser = psz_modules ?: "";
267 int retval = VLC_SUCCESS;
271 struct playlist_services_discovery_support_t * p_sds;
272 playlist_item_t * p_cat;
273 playlist_item_t * p_one;
275 while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
278 if( *psz_parser == '\0' )
281 const char *psz_next = strchr( psz_parser, ':' );
282 if( psz_next == NULL )
283 psz_next = psz_parser + strlen( psz_parser );
285 char psz_plugin[psz_next - psz_parser + 1];
286 memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
287 psz_plugin[sizeof (psz_plugin) - 1] = '\0';
288 psz_parser = psz_next;
290 /* Perform the addition */
291 msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
292 services_discovery_t *p_sd;
294 p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
298 vlc_event_attach( services_discovery_EventManager( p_sd ),
299 vlc_ServicesDiscoveryItemAdded,
300 playlist_sd_item_added,
303 vlc_event_attach( services_discovery_EventManager( p_sd ),
304 vlc_ServicesDiscoveryItemAdded,
305 playlist_sd_item_added,
308 vlc_event_attach( services_discovery_EventManager( p_sd ),
309 vlc_ServicesDiscoveryItemRemoved,
310 playlist_sd_item_removed,
313 vlc_event_attach( services_discovery_EventManager( p_sd ),
314 vlc_ServicesDiscoveryItemRemoved,
315 playlist_sd_item_removed,
318 bool ret = services_discovery_Start( p_sd );
321 vlc_object_release( p_sd );
325 char *psz = services_discovery_GetLocalizedName( p_sd );
328 playlist_NodesPairCreate( p_playlist, psz,
329 &p_cat, &p_one, false );
333 /* Free in playlist_ServicesDiscoveryRemove */
334 p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
338 /* We want tree-view for service directory */
339 p_one->p_input->b_prefers_tree = true;
341 p_sds->p_one = p_one;
342 p_sds->p_cat = p_cat;
345 TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
353 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
354 const char *psz_module )
356 struct playlist_services_discovery_support_t * p_sds = NULL;
360 for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
362 if( !strcmp( psz_module, pl_priv(p_playlist)->pp_sds[i]->p_sd->psz_module ) )
364 p_sds = pl_priv(p_playlist)->pp_sds[i];
365 REMOVE_ELEM( pl_priv(p_playlist)->pp_sds, pl_priv(p_playlist)->i_sds, i );
371 if( !p_sds || !p_sds->p_sd )
373 msg_Warn( p_playlist, "module %s is not loaded", psz_module );
377 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
378 vlc_ServicesDiscoveryItemAdded,
379 playlist_sd_item_added,
382 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
383 vlc_ServicesDiscoveryItemAdded,
384 playlist_sd_item_added,
387 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
388 vlc_ServicesDiscoveryItemRemoved,
389 playlist_sd_item_removed,
392 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
393 vlc_ServicesDiscoveryItemRemoved,
394 playlist_sd_item_removed,
397 /* Remove the sd playlist node if it exists */
399 if( p_sds->p_cat != p_playlist->p_root_category &&
400 p_sds->p_one != p_playlist->p_root_onelevel )
402 playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
403 playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
407 services_discovery_StopAndRelease( p_sds->p_sd );
413 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
414 const char *psz_module )
419 for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
421 if( !strcmp( psz_module, pl_priv(p_playlist)->pp_sds[i]->p_sd->psz_module ) )
431 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
433 while( pl_priv(p_playlist)->i_sds > 0 )
434 playlist_ServicesDiscoveryRemove( p_playlist,
435 pl_priv(p_playlist)->pp_sds[0]->p_sd->psz_module );