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