]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
487a7c68b86a631b083c7f0b23df54d2cba478ff
[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( vlc_object_t *p_this );
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     vlc_event_t event = {
113         .type = vlc_ServicesDiscoveryStarted
114     };
115     vlc_event_send( &p_sd->event_manager, &event );
116
117     if ((p_sd->pf_run != NULL)
118         && vlc_thread_create( p_sd, "services_discovery", RunSD,
119                               VLC_THREAD_PRIORITY_LOW, false))
120     {
121         msg_Err( p_sd, "cannot create services discovery thread" );
122         vlc_object_release( p_sd );
123         return VLC_EGENERIC;
124     }
125     return VLC_SUCCESS;
126 }
127
128 /***********************************************************************
129  * Stop
130  ***********************************************************************/
131 static void ObjectKillChildrens( vlc_object_t *p_obj )
132 {
133     vlc_list_t *p_list;
134     int i;
135     vlc_object_kill( p_obj );
136
137     p_list = vlc_list_children( p_obj );
138     for( i = 0; i < p_list->i_count; i++ )
139         ObjectKillChildrens( p_list->p_values[i].p_object );
140     vlc_list_release( p_list );
141 }
142
143 void services_discovery_Stop ( services_discovery_t * p_sd )
144 {
145     vlc_event_t event = {
146         .type = vlc_ServicesDiscoveryEnded
147     };
148
149     ObjectKillChildrens( VLC_OBJECT(p_sd) );
150     if( p_sd->pf_run ) vlc_thread_join( p_sd );
151
152     vlc_event_send( &p_sd->event_manager, &event );
153     module_Unneed( p_sd, p_sd->p_module );
154 }
155
156 /***********************************************************************
157  * GetLocalizedName
158  ***********************************************************************/
159 char *
160 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
161 {
162     return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
163 }
164
165 /***********************************************************************
166  * SetLocalizedName
167  ***********************************************************************/
168 void
169 services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
170 {
171     free( p_sd->psz_localized_name );
172     p_sd->psz_localized_name = strdup(psz);
173 }
174
175 /***********************************************************************
176  * EventManager
177  ***********************************************************************/
178 vlc_event_manager_t *
179 services_discovery_EventManager ( services_discovery_t * p_sd )
180 {
181     return &p_sd->event_manager;
182 }
183
184 /***********************************************************************
185  * AddItem
186  ***********************************************************************/
187 void
188 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
189                              const char * psz_category )
190 {
191     vlc_event_t event;
192     event.type = vlc_ServicesDiscoveryItemAdded;
193     event.u.services_discovery_item_added.p_new_item = p_item;
194     event.u.services_discovery_item_added.psz_category = psz_category;
195
196     vlc_event_send( &p_sd->event_manager, &event );
197 }
198
199 /***********************************************************************
200  * RemoveItem
201  ***********************************************************************/
202 void
203 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
204 {
205     vlc_event_t event;
206     event.type = vlc_ServicesDiscoveryItemRemoved;
207     event.u.services_discovery_item_removed.p_item = p_item;
208
209     vlc_event_send( &p_sd->event_manager, &event );
210 }
211
212 /***********************************************************************
213  * RunSD (Private)
214  ***********************************************************************/
215 static void* RunSD( vlc_object_t *p_this )
216 {
217     services_discovery_t *p_sd = (services_discovery_t *)p_this;
218     p_sd->pf_run( p_sd );
219     return NULL;
220 }
221
222 /*
223  * Playlist - Services discovery bridge
224  */
225
226  /* A new item has been added to a certain sd */
227 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
228 {
229     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
230     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
231     playlist_item_t *p_new_item, * p_parent = user_data;
232     playlist_t * p_playlist = p_parent->p_playlist;
233
234     msg_Dbg( p_playlist, "Adding %s in %s",
235                 p_input->psz_name ? p_input->psz_name : "(null)",
236                 psz_cat ? psz_cat : "(null)" );
237
238     PL_LOCK;
239     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
240     if( !EMPTY_STR(psz_cat) &&
241         p_parent->p_parent == p_playlist->p_root_category )
242     {
243         /* */
244         playlist_item_t * p_cat;
245         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
246         if( !p_cat )
247         {
248             p_cat = playlist_NodeCreate( p_playlist, psz_cat,
249                                          p_parent, 0, NULL );
250             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
251         }
252         p_parent = p_cat;
253     }
254
255     p_new_item = playlist_NodeAddInput( p_playlist, p_input, p_parent,
256                                         PLAYLIST_APPEND, PLAYLIST_END, pl_Locked );
257     if( p_new_item )
258     {
259         p_new_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
260         p_new_item->i_flags &= ~PLAYLIST_SAVE_FLAG;
261     }
262     PL_UNLOCK;
263 }
264
265  /* A new item has been removed from a certain sd */
266 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
267 {
268     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
269     playlist_item_t * p_parent = user_data;
270     playlist_item_t * p_pl_item;
271
272     /* First make sure that if item is a node it will be deleted.
273      * XXX: Why don't we have a function to ensure that in the playlist code ? */
274     vlc_object_lock( p_parent->p_playlist );
275     p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
276             p_input->i_id, p_parent, false );
277
278     if( p_pl_item && p_pl_item->i_children > -1 )
279     {
280         playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
281         vlc_object_unlock( p_parent->p_playlist );
282         return;
283     }
284
285     /* Delete the non-node item normally */
286     playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
287                                       p_parent, pl_Locked );
288
289     vlc_object_unlock( p_parent->p_playlist );
290 }
291
292 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,  const char *psz_modules )
293 {
294     const char *psz_parser = psz_modules ?: "";
295     int retval = VLC_SUCCESS;
296
297     for (;;)
298     {
299         struct playlist_services_discovery_support_t * p_sds;
300         playlist_item_t * p_cat;
301         playlist_item_t * p_one;
302
303         while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
304             psz_parser++;
305
306         if( *psz_parser == '\0' )
307             break;
308
309         const char *psz_next = strchr( psz_parser, ':' );
310         if( psz_next == NULL )
311             psz_next = psz_parser + strlen( psz_parser );
312
313         char psz_plugin[psz_next - psz_parser + 1];
314         memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
315         psz_plugin[sizeof (psz_plugin) - 1] = '\0';
316         psz_parser = psz_next;
317
318         /* Perform the addition */
319         msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
320         services_discovery_t *p_sd;
321
322         p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
323         if( !p_sd )
324             continue;
325
326         char * psz = services_discovery_GetLocalizedName( p_sd );
327         assert( psz );
328         PL_LOCK;
329         playlist_NodesPairCreate( p_playlist, psz,
330                 &p_cat, &p_one, false );
331         PL_UNLOCK;
332         free( psz );
333
334         vlc_event_attach( services_discovery_EventManager( p_sd ),
335                           vlc_ServicesDiscoveryItemAdded,
336                           playlist_sd_item_added,
337                           p_one );
338
339         vlc_event_attach( services_discovery_EventManager( p_sd ),
340                           vlc_ServicesDiscoveryItemAdded,
341                           playlist_sd_item_added,
342                           p_cat );
343
344         vlc_event_attach( services_discovery_EventManager( p_sd ),
345                           vlc_ServicesDiscoveryItemRemoved,
346                           playlist_sd_item_removed,
347                           p_one );
348
349         vlc_event_attach( services_discovery_EventManager( p_sd ),
350                           vlc_ServicesDiscoveryItemRemoved,
351                           playlist_sd_item_removed,
352                           p_cat );
353
354         services_discovery_Start( p_sd );
355
356         /* Free in playlist_ServicesDiscoveryRemove */
357         p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
358         if( !p_sds )
359             return VLC_ENOMEM;
360
361         /* We want tree-view for service directory */
362         p_one->p_input->b_prefers_tree = true;
363         p_sds->p_sd = p_sd;
364         p_sds->p_one = p_one;
365         p_sds->p_cat = p_cat;
366
367         PL_LOCK;
368         TAB_APPEND( p_playlist->i_sds, p_playlist->pp_sds, p_sds );
369         PL_UNLOCK;
370     }
371
372     return retval;
373 }
374
375 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
376                                        const char *psz_module )
377 {
378     struct playlist_services_discovery_support_t * p_sds = NULL;
379     int i;
380
381     PL_LOCK;
382     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
383     {
384         if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
385         {
386             p_sds = p_playlist->pp_sds[i];
387             REMOVE_ELEM( p_playlist->pp_sds, p_playlist->i_sds, i );
388             break;
389         }
390     }
391     PL_UNLOCK;
392
393     if( !p_sds || !p_sds->p_sd )
394     {
395         msg_Warn( p_playlist, "module %s is not loaded", psz_module );
396         return VLC_EGENERIC;
397     }
398
399     services_discovery_Stop( p_sds->p_sd );
400
401     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
402                         vlc_ServicesDiscoveryItemAdded,
403                         playlist_sd_item_added,
404                         p_sds->p_one );
405
406     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
407                         vlc_ServicesDiscoveryItemAdded,
408                         playlist_sd_item_added,
409                         p_sds->p_cat );
410
411     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
412                         vlc_ServicesDiscoveryItemRemoved,
413                         playlist_sd_item_removed,
414                         p_sds->p_one );
415
416     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
417                         vlc_ServicesDiscoveryItemRemoved,
418                         playlist_sd_item_removed,
419                         p_sds->p_cat );
420
421     /* Remove the sd playlist node if it exists */
422     PL_LOCK;
423     if( p_sds->p_cat != p_playlist->p_root_category &&
424         p_sds->p_one != p_playlist->p_root_onelevel )
425     {
426         playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
427         playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
428     }
429     PL_UNLOCK;
430
431     services_discovery_Destroy( p_sds->p_sd );
432     free( p_sds );
433
434     return VLC_SUCCESS;
435 }
436
437 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
438                                               const char *psz_module )
439 {
440     int i;
441     PL_LOCK;
442
443     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
444     {
445         if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
446         {
447             PL_UNLOCK;
448             return true;
449         }
450     }
451     PL_UNLOCK;
452     return false;
453 }
454
455 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
456 {
457     while( p_playlist->i_sds > 0 )
458         playlist_ServicesDiscoveryRemove( p_playlist,
459                                      p_playlist->pp_sds[0]->p_sd->psz_module );
460 }
461