]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
Remove SD object type
[vlc] / src / playlist / services_discovery.c
1 /*****************************************************************************
2  * services_discovery.c : Manage playlist services_discovery modules
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26 #include <assert.h>
27
28 #include <vlc/vlc.h>
29 #include "vlc_playlist.h"
30 #include "vlc_events.h"
31 #include "playlist_internal.h"
32
33 static void RunSD( services_discovery_t *p_sd );
34
35 /*
36  * Services discovery
37  * Basically you just listen to Service discovery event through the
38  * sd's event manager.
39  * That's how the playlist get's Service Discovery information
40  */
41
42 /***********************************************************************
43  * GetServicesNames
44  ***********************************************************************/
45 char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
46                                                char ***pppsz_longnames )
47 {
48     return module_GetModulesNamesForCapability( p_super, "services_discovery",
49                                                 pppsz_longnames );
50 }
51
52 /***********************************************************************
53  * Create
54  ***********************************************************************/
55 services_discovery_t *
56 services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
57 {
58     services_discovery_t *p_sd;
59     p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
60                               "services discovery" );
61     if( !p_sd )
62         return NULL;
63
64     p_sd->pf_run = NULL;
65     p_sd->psz_localized_name = NULL;
66
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 );
76
77     p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, VLC_TRUE );
78
79     if( p_sd->p_module == NULL )
80     {
81         msg_Err( p_super, "no suitable services discovery module" );
82         vlc_object_release( p_sd );
83         return NULL;
84     }
85     p_sd->psz_module = strdup( psz_module_name );
86     p_sd->b_die = VLC_FALSE; /* FIXME */
87
88     vlc_object_attach( p_sd, p_super );
89     return p_sd;
90 }
91
92 /***********************************************************************
93  * Destroy
94  ***********************************************************************/
95 void services_discovery_Destroy ( services_discovery_t * p_sd )
96 {
97     vlc_event_manager_fini( &p_sd->event_manager );
98
99     free( p_sd->psz_module );
100     free( p_sd->psz_localized_name );
101
102     vlc_object_detach( p_sd );
103     vlc_object_release( p_sd );
104 }
105
106 /***********************************************************************
107  * Start
108  ***********************************************************************/
109 int services_discovery_Start ( services_discovery_t * p_sd )
110 {
111     if ((p_sd->pf_run != NULL)
112         && vlc_thread_create( p_sd, "services_discovery", RunSD,
113                               VLC_THREAD_PRIORITY_LOW, VLC_FALSE))
114     {
115         msg_Err( p_sd, "cannot create services discovery thread" );
116         vlc_object_release( p_sd );
117         return VLC_EGENERIC;
118     }
119     return VLC_SUCCESS;
120 }
121
122 /***********************************************************************
123  * Stop
124  ***********************************************************************/
125 void services_discovery_Stop ( services_discovery_t * p_sd )
126 {
127     vlc_object_kill( p_sd );
128     if( p_sd->pf_run ) vlc_thread_join( p_sd );
129
130     module_Unneed( p_sd, p_sd->p_module );
131 }
132
133 /***********************************************************************
134  * GetLocalizedName
135  ***********************************************************************/
136 char *
137 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
138 {
139     return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
140 }
141
142 /***********************************************************************
143  * SetLocalizedName
144  ***********************************************************************/
145 void
146 services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
147 {
148     free( p_sd->psz_localized_name );
149     p_sd->psz_localized_name = strdup(psz);
150 }
151
152 /***********************************************************************
153  * EventManager
154  ***********************************************************************/
155 vlc_event_manager_t *
156 services_discovery_EventManager ( services_discovery_t * p_sd )
157 {
158     return &p_sd->event_manager;
159 }
160
161 /***********************************************************************
162  * AddItem
163  ***********************************************************************/
164 void
165 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
166                              const char * psz_category )
167 {
168     vlc_event_t event;
169     event.type = vlc_ServicesDiscoveryItemAdded;
170     event.u.services_discovery_item_added.p_new_item = p_item;
171     event.u.services_discovery_item_added.psz_category = psz_category;
172
173     vlc_event_send( &p_sd->event_manager, &event );
174 }
175
176 /***********************************************************************
177  * RemoveItem
178  ***********************************************************************/
179 void
180 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
181 {
182     vlc_event_t event;
183     event.type = vlc_ServicesDiscoveryItemRemoved;
184     event.u.services_discovery_item_removed.p_item = p_item;
185
186     vlc_event_send( &p_sd->event_manager, &event );
187 }
188
189 /***********************************************************************
190  * RunSD (Private)
191  ***********************************************************************/
192 static void RunSD( services_discovery_t *p_sd )
193 {
194     vlc_event_t event;
195
196     event.type = vlc_ServicesDiscoveryStarted;
197     vlc_event_send( &p_sd->event_manager, &event );
198
199     p_sd->pf_run( p_sd );
200
201     event.type = vlc_ServicesDiscoveryEnded;
202     vlc_event_send( &p_sd->event_manager, &event );
203     return;
204 }
205
206 /*
207  * Playlist - Services discovery bridge
208  */
209
210  /* A new item has been added to a certain sd */
211 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
212 {
213     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
214     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
215     playlist_item_t *p_new_item, * p_parent = user_data;
216
217     msg_Dbg( p_parent->p_playlist, "Adding %s in %s",
218                 p_input->psz_name ? p_input->psz_name : "(null)",
219                 psz_cat ? psz_cat : "(null)" );
220
221     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
222     if( !EMPTY_STR(psz_cat) &&
223         p_parent->p_parent == p_parent->p_playlist->p_root_category )
224     {
225         /* */
226         playlist_item_t * p_cat;
227         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
228         if( !p_cat )
229         {
230             p_cat = playlist_NodeCreate( p_parent->p_playlist, psz_cat,
231                                          p_parent, 0, NULL );
232             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
233         }
234         p_parent = p_cat;
235     }
236
237     p_new_item = playlist_NodeAddInput( p_parent->p_playlist, p_input, p_parent,
238                                         PLAYLIST_APPEND, PLAYLIST_END, VLC_FALSE );
239     if( p_new_item )
240     {
241         p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
242         p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
243     }
244 }
245
246  /* A new item has been removed from a certain sd */
247 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
248 {
249     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
250     playlist_item_t * p_parent = user_data;
251     playlist_item_t * p_pl_item;
252
253     /* First make sure that if item is a node it will be deleted.
254      * XXX: Why don't we have a function to ensure that in the playlist code ? */
255     vlc_object_lock( p_parent->p_playlist );
256     p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
257             p_input->i_id, p_parent, VLC_FALSE );
258
259     if( p_pl_item && p_pl_item->i_children > -1 )
260     {
261         playlist_NodeDelete( p_parent->p_playlist, p_pl_item, VLC_TRUE, VLC_FALSE );
262         vlc_object_unlock( p_parent->p_playlist );
263         return;
264     }
265     vlc_object_unlock( p_parent->p_playlist );
266
267     /* Delete the non-node item normally */
268     playlist_DeleteInputInParent( p_parent->p_playlist, p_input->i_id,
269                                   p_parent, VLC_FALSE );
270 }
271
272 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,  const char *psz_modules )
273 {
274     const char *psz_parser = psz_modules ?: "";
275     int retval = VLC_SUCCESS;
276
277     for (;;)
278     {
279         struct playlist_services_discovery_support_t * p_sds;
280         playlist_item_t * p_cat;
281         playlist_item_t * p_one;
282
283         while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
284             psz_parser++;
285
286         if( *psz_parser == '\0' )
287             break;
288
289         const char *psz_next = strchr( psz_parser, ':' );
290         if( psz_next == NULL )
291             psz_next = psz_parser + strlen( psz_parser );
292
293         char psz_plugin[psz_next - psz_parser + 1];
294         memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
295         psz_plugin[sizeof (psz_plugin) - 1] = '\0';
296         psz_parser = psz_next;
297
298         /* Perform the addition */
299         msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
300         services_discovery_t *p_sd;
301
302         p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
303         if( !p_sd )
304             continue;
305
306         char * psz = services_discovery_GetLocalizedName( p_sd );
307         assert( psz );
308         playlist_NodesPairCreate( p_playlist, psz,
309                 &p_cat, &p_one, VLC_FALSE );
310         free( psz );
311
312         vlc_event_attach( services_discovery_EventManager( p_sd ),
313                           vlc_ServicesDiscoveryItemAdded,
314                           playlist_sd_item_added,
315                           p_one );
316
317         vlc_event_attach( services_discovery_EventManager( p_sd ),
318                           vlc_ServicesDiscoveryItemAdded,
319                           playlist_sd_item_added,
320                           p_cat );
321
322         vlc_event_attach( services_discovery_EventManager( p_sd ),
323                           vlc_ServicesDiscoveryItemRemoved,
324                           playlist_sd_item_removed,
325                           p_one );
326
327         vlc_event_attach( services_discovery_EventManager( p_sd ),
328                           vlc_ServicesDiscoveryItemRemoved,
329                           playlist_sd_item_removed,
330                           p_cat );
331
332         services_discovery_Start( p_sd );
333
334         /* Free in playlist_ServicesDiscoveryRemove */
335         p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
336         if( !p_sds )
337         {
338             msg_Err( p_playlist, "No more memory" );
339             return VLC_ENOMEM;
340         }
341         p_sds->p_sd = p_sd;
342         p_sds->p_one = p_one;
343         p_sds->p_cat = p_cat;
344
345         PL_LOCK;
346         TAB_APPEND( p_playlist->i_sds, p_playlist->pp_sds, p_sds );
347         PL_UNLOCK;
348     }
349
350     return retval;
351 }
352
353 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
354                                        const char *psz_module )
355 {
356     struct playlist_services_discovery_support_t * p_sds = NULL;
357     int i;
358
359     PL_LOCK;
360     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
361     {
362         if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
363         {
364             p_sds = p_playlist->pp_sds[i];
365             REMOVE_ELEM( p_playlist->pp_sds, p_playlist->i_sds, i );
366             break;
367         }
368     }
369     PL_UNLOCK;
370
371     if( !p_sds || !p_sds->p_sd )
372     {
373         msg_Warn( p_playlist, "module %s is not loaded", psz_module );
374         return VLC_EGENERIC;
375     }
376
377     services_discovery_Stop( p_sds->p_sd );
378
379     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
380                         vlc_ServicesDiscoveryItemAdded,
381                         playlist_sd_item_added,
382                         p_sds->p_one );
383
384     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
385                         vlc_ServicesDiscoveryItemAdded,
386                         playlist_sd_item_added,
387                         p_sds->p_cat );
388
389     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
390                         vlc_ServicesDiscoveryItemRemoved,
391                         playlist_sd_item_removed,
392                         p_sds->p_one );
393
394     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
395                         vlc_ServicesDiscoveryItemRemoved,
396                         playlist_sd_item_removed,
397                         p_sds->p_cat );
398
399     /* Remove the sd playlist node if it exists */
400     PL_LOCK;
401     if( p_sds->p_cat != p_playlist->p_root_category &&
402         p_sds->p_one != p_playlist->p_root_onelevel )
403     {
404         playlist_NodeDelete( p_playlist, p_sds->p_cat, VLC_TRUE, VLC_FALSE );
405         playlist_NodeDelete( p_playlist, p_sds->p_one, VLC_TRUE, VLC_FALSE );
406     }
407     PL_UNLOCK;
408
409     services_discovery_Destroy( p_sds->p_sd );
410
411     return VLC_SUCCESS;
412 }
413
414 vlc_bool_t playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
415                                               const char *psz_module )
416 {
417     int i;
418     PL_LOCK;
419
420     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
421     {
422         if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
423         {
424             PL_UNLOCK;
425             return VLC_TRUE;
426         }
427     }
428     PL_UNLOCK;
429     return VLC_FALSE;
430 }
431