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