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