]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
a0e5a7498d8c04c9248ab7b2412727d751d0ade7
[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 <vlc_services_discovery.h>
32 #include <vlc_probe.h>
33 #include "playlist_internal.h"
34 #include "../libvlc.h"
35
36 typedef struct
37 {
38     char *name;
39     char *longname;
40 } vlc_sd_probe_t;
41
42 int vlc_sd_probe_Add (vlc_probe_t *probe, const char *name,
43                       const char *longname)
44 {
45     vlc_sd_probe_t names = { strdup(name), strdup(longname) };
46
47     if (unlikely (names.name == NULL || names.longname == NULL
48                || vlc_probe_add (probe, &names, sizeof (names))))
49     {
50         free (names.name);
51         free (names.longname);
52         return VLC_ENOMEM;
53     }
54     return VLC_PROBE_CONTINUE;
55 }
56
57 #undef vlc_sd_GetNames
58
59 /**
60  * Gets the list of available services discovery plugins.
61  */
62 char **vlc_sd_GetNames (vlc_object_t *obj, char ***pppsz_longnames)
63 {
64     size_t count;
65     vlc_sd_probe_t *tab = vlc_probe (obj, "services probe", &count);
66
67     if (count == 0)
68     {
69         free (tab);
70         return NULL;
71     }
72
73     char **names = malloc (sizeof(char *) * (count + 1));
74     char **longnames = malloc (sizeof(char *) * (count + 1));
75
76     if (unlikely (names == NULL || longnames == NULL))
77         abort();
78     for( size_t i = 0; i < count; i++ )
79     {
80         names[i] = tab[i].name;
81         longnames[i] = tab[i].longname;
82     }
83     free (tab);
84     names[count] = longnames[count] = NULL;
85     *pppsz_longnames = longnames;
86     return names;
87 }
88
89
90 struct vlc_sd_internal_t
91 {
92     /* the playlist items for category and onelevel */
93     playlist_item_t      *p_node;
94     services_discovery_t *p_sd; /**< Loaded service discovery modules */
95     char                 *psz_name;
96 };
97
98 static void services_discovery_Destructor ( vlc_object_t *p_obj );
99
100 /*
101  * Services discovery
102  * Basically you just listen to Service discovery event through the
103  * sd's event manager.
104  * That's how the playlist get's Service Discovery information
105  */
106
107 /***********************************************************************
108  * Create
109  ***********************************************************************/
110 services_discovery_t *vlc_sd_Create( vlc_object_t *p_super )
111 {
112     services_discovery_t *p_sd;
113
114     p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
115                               "services discovery" );
116     if( !p_sd )
117         return NULL;
118
119     vlc_event_manager_init( &p_sd->event_manager, p_sd, (vlc_object_t *)p_sd );
120     vlc_event_manager_register_event_type( &p_sd->event_manager,
121             vlc_ServicesDiscoveryItemAdded );
122     vlc_event_manager_register_event_type( &p_sd->event_manager,
123             vlc_ServicesDiscoveryItemRemoved );
124     vlc_event_manager_register_event_type( &p_sd->event_manager,
125             vlc_ServicesDiscoveryStarted );
126     vlc_event_manager_register_event_type( &p_sd->event_manager,
127             vlc_ServicesDiscoveryEnded );
128
129     vlc_object_set_destructor( p_sd, services_discovery_Destructor );
130     vlc_object_attach( p_sd, p_super );
131
132     return p_sd;
133 }
134
135 /***********************************************************************
136  * Stop
137  ***********************************************************************/
138 bool vlc_sd_Start ( services_discovery_t * p_sd, const char *module )
139 {
140     assert(!p_sd->p_module);
141
142     p_sd->p_module = module_need( p_sd, "services_discovery", module, true );
143
144     if( p_sd->p_module == NULL )
145     {
146         msg_Err( p_sd, "no suitable services discovery module" );
147         return false;
148     }
149         
150     vlc_event_t event = {
151         .type = vlc_ServicesDiscoveryStarted
152     };
153     vlc_event_send( &p_sd->event_manager, &event );
154     return true;
155 }
156                           
157 /***********************************************************************
158  * Stop
159  ***********************************************************************/
160 void vlc_sd_Stop ( services_discovery_t * p_sd )
161 {
162     vlc_event_t event = {
163         .type = vlc_ServicesDiscoveryEnded
164     };
165     
166     vlc_event_send( &p_sd->event_manager, &event );
167
168     module_unneed( p_sd, p_sd->p_module );
169     p_sd->p_module = NULL;
170 }
171
172 /***********************************************************************
173  * Destructor
174  ***********************************************************************/
175 static void services_discovery_Destructor ( vlc_object_t *p_obj )
176 {
177     services_discovery_t * p_sd = (services_discovery_t *)p_obj;
178     assert(!p_sd->p_module); /* Forgot to call Stop */
179     vlc_event_manager_fini( &p_sd->event_manager );
180 }
181
182 /***********************************************************************
183  * GetLocalizedName
184  ***********************************************************************/
185 char *
186 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
187 {
188     return strdup( module_get_name( p_sd->p_module, true ) );
189 }
190
191 /***********************************************************************
192  * EventManager
193  ***********************************************************************/
194 vlc_event_manager_t *
195 services_discovery_EventManager ( services_discovery_t * p_sd )
196 {
197     return &p_sd->event_manager;
198 }
199
200 /***********************************************************************
201  * AddItem
202  ***********************************************************************/
203 void
204 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
205                              const char * psz_category )
206 {
207     vlc_event_t event;
208     event.type = vlc_ServicesDiscoveryItemAdded;
209     event.u.services_discovery_item_added.p_new_item = p_item;
210     event.u.services_discovery_item_added.psz_category = psz_category;
211
212     vlc_event_send( &p_sd->event_manager, &event );
213 }
214
215 /***********************************************************************
216  * RemoveItem
217  ***********************************************************************/
218 void
219 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
220 {
221     vlc_event_t event;
222     event.type = vlc_ServicesDiscoveryItemRemoved;
223     event.u.services_discovery_item_removed.p_item = p_item;
224
225     vlc_event_send( &p_sd->event_manager, &event );
226 }
227
228 /*
229  * Playlist - Services discovery bridge
230  */
231
232  /* A new item has been added to a certain sd */
233 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
234 {
235     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
236     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
237     playlist_item_t * p_parent = user_data;
238     playlist_t * p_playlist = p_parent->p_playlist;
239
240     msg_Dbg( p_playlist, "Adding %s in %s",
241                 p_input->psz_name ? p_input->psz_name : "(null)",
242                 psz_cat ? psz_cat : "(null)" );
243
244     PL_LOCK;
245     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
246     if( !EMPTY_STR(psz_cat) )
247     {
248         /* */
249         playlist_item_t * p_cat;
250         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
251         if( !p_cat )
252         {
253             p_cat = playlist_NodeCreate( p_playlist, psz_cat,
254                                          p_parent, 0, NULL );
255             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
256         }
257         p_parent = p_cat;
258     }
259
260     playlist_NodeAddInput( p_playlist, p_input, p_parent,
261                            PLAYLIST_APPEND, PLAYLIST_END,
262                            pl_Locked );
263     PL_UNLOCK;
264 }
265
266  /* A new item has been removed from a certain sd */
267 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
268 {
269     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
270     playlist_item_t * p_parent = user_data;
271     playlist_DeleteFromInput( p_parent->p_playlist, p_input, false );
272 }
273
274 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_module )
275 {
276     /* Perform the addition */
277     msg_Dbg( p_playlist, "adding services_discovery %s...", psz_module );
278
279     services_discovery_t *p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist) );
280     if( !p_sd )
281         return VLC_ENOMEM;
282
283     module_t *m = module_find_by_shortcut( psz_module );
284     if( !m )
285     {
286         msg_Err( p_playlist, "No such module: %s", psz_module );
287         vlc_sd_Destroy( p_sd );
288         return VLC_EGENERIC;
289     }
290
291     /* Free in playlist_ServicesDiscoveryRemove */
292     vlc_sd_internal_t * p_sds = malloc( sizeof(*p_sds) );
293     if( !p_sds )
294     {
295         vlc_sd_Destroy( p_sd );
296         module_release( m );
297         return VLC_ENOMEM;
298     }
299
300     playlist_item_t *p_node;
301
302     PL_LOCK;
303     p_node = playlist_NodeCreate( p_playlist, module_get_name( m, true ),
304                                   p_playlist->p_root, 0, NULL );
305     PL_UNLOCK;
306     module_release( m );
307
308     vlc_event_attach( services_discovery_EventManager( p_sd ),
309                       vlc_ServicesDiscoveryItemAdded,
310                       playlist_sd_item_added, p_node );
311
312     vlc_event_attach( services_discovery_EventManager( p_sd ),
313                       vlc_ServicesDiscoveryItemRemoved,
314                       playlist_sd_item_removed, p_node );
315
316     if( !vlc_sd_Start( p_sd, psz_module ) )
317     {
318         vlc_sd_Destroy( p_sd );
319         free( p_sds );
320         return VLC_EGENERIC;
321     }
322
323     /* We want tree-view for service directory */
324     p_node->p_input->b_prefers_tree = true;
325     p_sds->p_sd = p_sd;
326     p_sds->p_node = p_node;
327     p_sds->psz_name = strdup( psz_module );
328
329     PL_LOCK;
330     TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
331     PL_UNLOCK;
332
333     return VLC_SUCCESS;
334 }
335
336 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
337                                       const char *psz_name )
338 {
339     playlist_private_t *priv = pl_priv( p_playlist );
340     vlc_sd_internal_t * p_sds = NULL;
341
342     PL_LOCK;
343     for( int i = 0; i < priv->i_sds; i++ )
344     {
345         if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) )
346         {
347             p_sds = priv->pp_sds[i];
348             REMOVE_ELEM( priv->pp_sds, priv->i_sds, i );
349             break;
350         }
351     }
352     PL_UNLOCK;
353
354     if( !p_sds )
355     {
356         msg_Warn( p_playlist, "discovery %s is not loaded", psz_name );
357         return VLC_EGENERIC;
358     }
359
360     services_discovery_t *p_sd = p_sds->p_sd;
361     assert( p_sd );
362
363     vlc_sd_Stop( p_sd );
364
365     vlc_event_detach( services_discovery_EventManager( p_sd ),
366                         vlc_ServicesDiscoveryItemAdded,
367                         playlist_sd_item_added,
368                         p_sds->p_node );
369
370     vlc_event_detach( services_discovery_EventManager( p_sd ),
371                         vlc_ServicesDiscoveryItemRemoved,
372                         playlist_sd_item_removed,
373                         p_sds->p_node );
374
375     /* Remove the sd playlist node if it exists */
376     PL_LOCK;
377     playlist_NodeDelete( p_playlist, p_sds->p_node, true, false );
378     PL_UNLOCK;
379
380     vlc_sd_Destroy( p_sd );
381     free( p_sds->psz_name );
382     free( p_sds );
383
384     return VLC_SUCCESS;
385 }
386
387 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
388                                          const char *psz_name )
389 {
390     playlist_private_t *priv = pl_priv( p_playlist );
391     bool found = false;
392     PL_LOCK;
393
394     for( int i = 0; i < priv->i_sds; i++ )
395     {
396         vlc_sd_internal_t *sd = priv->pp_sds[i];
397
398         if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
399         {
400             found = true;
401             break;
402         }
403     }
404     PL_UNLOCK;
405     return found;
406 }
407
408 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
409 {
410     playlist_private_t *priv = pl_priv( p_playlist );
411
412     while( priv->i_sds > 0 )
413         playlist_ServicesDiscoveryRemove( p_playlist,
414                                           priv->pp_sds[0]->psz_name );
415 }