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 *vlc_sd_Create( vlc_object_t *p_super )
60 services_discovery_t *p_sd;
62 p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
63 "services discovery" );
67 vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
68 vlc_event_manager_register_event_type( &p_sd->event_manager,
69 vlc_ServicesDiscoveryItemAdded );
70 vlc_event_manager_register_event_type( &p_sd->event_manager,
71 vlc_ServicesDiscoveryItemRemoved );
72 vlc_event_manager_register_event_type( &p_sd->event_manager,
73 vlc_ServicesDiscoveryStarted );
74 vlc_event_manager_register_event_type( &p_sd->event_manager,
75 vlc_ServicesDiscoveryEnded );
77 vlc_object_attach( p_sd, p_super );
82 static void ObjectKillChildrens( vlc_object_t *p_obj )
86 vlc_object_kill( p_obj );
88 p_list = vlc_list_children( p_obj );
89 for( i = 0; i < p_list->i_count; i++ )
90 ObjectKillChildrens( p_list->p_values[i].p_object );
91 vlc_list_release( p_list );
94 /***********************************************************************
96 ***********************************************************************/
97 bool vlc_sd_Start ( services_discovery_t * p_sd, const char *module )
99 assert(!p_sd->p_module);
101 p_sd->p_module = module_need( p_sd, "services_discovery", module, true );
103 if( p_sd->p_module == NULL )
105 msg_Err( p_sd, "no suitable services discovery module" );
109 vlc_event_t event = {
110 .type = vlc_ServicesDiscoveryStarted
112 vlc_event_send( &p_sd->event_manager, &event );
116 /***********************************************************************
118 ***********************************************************************/
119 void vlc_sd_Stop ( services_discovery_t * p_sd )
121 vlc_event_t event = {
122 .type = vlc_ServicesDiscoveryEnded
125 ObjectKillChildrens( VLC_OBJECT(p_sd) );
127 vlc_event_send( &p_sd->event_manager, &event );
129 module_unneed( p_sd, p_sd->p_module );
130 p_sd->p_module = NULL;
133 /***********************************************************************
135 ***********************************************************************/
136 static void services_discovery_Destructor ( services_discovery_t * p_sd )
138 assert(!p_sd->p_module); /* Forgot to call Stop */
140 vlc_event_manager_fini( &p_sd->event_manager );
141 vlc_object_release( p_sd );
144 /***********************************************************************
146 ***********************************************************************/
148 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
150 return strdup( module_get_name( p_sd->p_module, true ) );
153 /***********************************************************************
155 ***********************************************************************/
156 vlc_event_manager_t *
157 services_discovery_EventManager ( services_discovery_t * p_sd )
159 return &p_sd->event_manager;
162 /***********************************************************************
164 ***********************************************************************/
166 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
167 const char * psz_category )
170 event.type = vlc_ServicesDiscoveryItemAdded;
171 event.u.services_discovery_item_added.p_new_item = p_item;
172 event.u.services_discovery_item_added.psz_category = psz_category;
174 vlc_event_send( &p_sd->event_manager, &event );
177 /***********************************************************************
179 ***********************************************************************/
181 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
184 event.type = vlc_ServicesDiscoveryItemRemoved;
185 event.u.services_discovery_item_removed.p_item = p_item;
187 vlc_event_send( &p_sd->event_manager, &event );
191 * Playlist - Services discovery bridge
194 /* A new item has been added to a certain sd */
195 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
197 input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
198 const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
199 playlist_item_t *p_new_item, * p_parent = user_data;
200 playlist_t * p_playlist = p_parent->p_playlist;
202 msg_Dbg( p_playlist, "Adding %s in %s",
203 p_input->psz_name ? p_input->psz_name : "(null)",
204 psz_cat ? psz_cat : "(null)" );
207 /* If p_parent is in root category (this is clearly a hack) and we have a cat */
208 if( !EMPTY_STR(psz_cat) &&
209 p_parent->p_parent == p_playlist->p_root_category )
212 playlist_item_t * p_cat;
213 p_cat = playlist_ChildSearchName( p_parent, psz_cat );
216 p_cat = playlist_NodeCreate( p_playlist, psz_cat,
218 p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
223 p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
224 PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
227 p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
228 p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
233 /* A new item has been removed from a certain sd */
234 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
236 input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
237 playlist_item_t * p_parent = user_data;
238 playlist_item_t * p_pl_item;
240 /* First make sure that if item is a node it will be deleted.
241 * XXX: Why don't we have a function to ensure that in the playlist code ? */
242 vlc_object_lock( p_parent->p_playlist );
243 p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
244 p_input->i_id, p_parent, false );
246 if( p_pl_item && p_pl_item->i_children > -1 )
248 playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
249 vlc_object_unlock( p_parent->p_playlist );
253 /* Delete the non-node item normally */
254 playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
255 p_parent, pl_Locked );
257 vlc_object_unlock( p_parent->p_playlist );
260 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modules )
262 const char *psz_parser = psz_modules ?: "";
263 int retval = VLC_SUCCESS;
267 struct playlist_services_discovery_support_t * p_sds;
268 playlist_item_t * p_cat;
269 playlist_item_t * p_one;
271 while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
274 if( *psz_parser == '\0' )
277 const char *psz_next = strchr( psz_parser, ':' );
278 if( psz_next == NULL )
279 psz_next = psz_parser + strlen( psz_parser );
281 char psz_plugin[psz_next - psz_parser + 1];
282 memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
283 psz_plugin[sizeof (psz_plugin) - 1] = '\0';
284 psz_parser = psz_next;
286 /* Perform the addition */
287 msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
288 services_discovery_t *p_sd;
290 p_sd = vlc_sd_Create( (vlc_object_t*)p_playlist );
294 vlc_event_attach( services_discovery_EventManager( p_sd ),
295 vlc_ServicesDiscoveryItemAdded,
296 playlist_sd_item_added,
299 vlc_event_attach( services_discovery_EventManager( p_sd ),
300 vlc_ServicesDiscoveryItemAdded,
301 playlist_sd_item_added,
304 vlc_event_attach( services_discovery_EventManager( p_sd ),
305 vlc_ServicesDiscoveryItemRemoved,
306 playlist_sd_item_removed,
309 vlc_event_attach( services_discovery_EventManager( p_sd ),
310 vlc_ServicesDiscoveryItemRemoved,
311 playlist_sd_item_removed,
314 if( !vlc_sd_Start( p_sd, psz_plugin ) )
316 vlc_sd_Destroy( p_sd );
320 char *psz = services_discovery_GetLocalizedName( p_sd );
323 playlist_NodesPairCreate( p_playlist, psz,
324 &p_cat, &p_one, false );
328 /* Free in playlist_ServicesDiscoveryRemove */
329 p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
333 /* We want tree-view for service directory */
334 p_one->p_input->b_prefers_tree = true;
336 p_sds->p_one = p_one;
337 p_sds->p_cat = p_cat;
340 TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
348 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
349 const char *psz_module )
351 struct playlist_services_discovery_support_t * p_sds = NULL;
355 for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
357 if( !strcmp( psz_module, module_get_object( pl_priv(p_playlist)->pp_sds[i]->p_sd->p_module ) ) )
359 p_sds = pl_priv(p_playlist)->pp_sds[i];
360 REMOVE_ELEM( pl_priv(p_playlist)->pp_sds, pl_priv(p_playlist)->i_sds, i );
366 if( !p_sds || !p_sds->p_sd )
368 msg_Warn( p_playlist, "module %s is not loaded", psz_module );
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_ServicesDiscoveryItemAdded,
379 playlist_sd_item_added,
382 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
383 vlc_ServicesDiscoveryItemRemoved,
384 playlist_sd_item_removed,
387 vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
388 vlc_ServicesDiscoveryItemRemoved,
389 playlist_sd_item_removed,
392 /* Remove the sd playlist node if it exists */
394 if( p_sds->p_cat != p_playlist->p_root_category &&
395 p_sds->p_one != p_playlist->p_root_onelevel )
397 playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
398 playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
402 vlc_sd_StopAndDestroy( p_sds->p_sd );
408 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
409 const char *psz_module )
414 for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
416 if( !strcmp( psz_module, module_get_object( pl_priv(p_playlist)->pp_sds[i]->p_sd->p_module ) ) )
426 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
428 while( pl_priv(p_playlist)->i_sds > 0 )
429 playlist_ServicesDiscoveryRemove( p_playlist,
430 module_get_object( pl_priv(p_playlist)->pp_sds[0]->p_sd->p_module ) );