aadee5736965e4006fcc53ba87eec3790c47ba15
[vlc.git] / src / playlist / services_discovery.c
1 /*****************************************************************************
2  * services_discovery.c : Manage playlist services_discovery modules
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 <vlc_modules.h>
34 #include "playlist_internal.h"
35 #include "../libvlc.h"
36
37 typedef struct
38 {
39     char *name;
40     char *longname;
41     int category;
42 } vlc_sd_probe_t;
43
44 int vlc_sd_probe_Add (vlc_probe_t *probe, const char *name,
45                       const char *longname, int category)
46 {
47     vlc_sd_probe_t names = { strdup(name), strdup(longname), category };
48
49     if (unlikely (names.name == NULL || names.longname == NULL
50                || vlc_probe_add (probe, &names, sizeof (names))))
51     {
52         free (names.name);
53         free (names.longname);
54         return VLC_ENOMEM;
55     }
56     return VLC_PROBE_CONTINUE;
57 }
58
59 #undef vlc_sd_GetNames
60
61 /**
62  * Gets the list of available services discovery plugins.
63  */
64 char **vlc_sd_GetNames (vlc_object_t *obj, char ***pppsz_longnames, int **pp_categories)
65 {
66     size_t count;
67     vlc_sd_probe_t *tab = vlc_probe (obj, "services probe", &count);
68
69     if (count == 0)
70     {
71         free (tab);
72         return NULL;
73     }
74
75     char **names = malloc (sizeof(char *) * (count + 1));
76     char **longnames = malloc (sizeof(char *) * (count + 1));
77     int *categories = malloc(sizeof(int) * (count + 1));
78
79     if (unlikely (names == NULL || longnames == NULL || categories == NULL))
80     {
81         free(names);
82         free(longnames);
83         free(categories);
84         free(tab);
85         return NULL;
86     }
87     for( size_t i = 0; i < count; i++ )
88     {
89         names[i] = tab[i].name;
90         longnames[i] = tab[i].longname;
91         categories[i] = tab[i].category;
92     }
93     free (tab);
94     names[count] = longnames[count] = NULL;
95     categories[count] = 0;
96     *pppsz_longnames = longnames;
97     if( pp_categories ) *pp_categories = categories;
98     else free( categories );
99     return names;
100 }
101
102
103 static void services_discovery_Destructor ( vlc_object_t *p_obj );
104
105 /*
106  * Services discovery
107  * Basically you just listen to Service discovery event through the
108  * sd's event manager.
109  * That's how the playlist get's Service Discovery information
110  */
111
112 /*******************************************************************//**
113  * Create a Service discovery
114  ***********************************************************************/
115 services_discovery_t *vlc_sd_Create( vlc_object_t *p_super,
116                                      const char *cfg )
117 {
118     services_discovery_t *p_sd;
119
120     p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), "services discovery" );
121     if( !p_sd )
122         return NULL;
123     free(config_ChainCreate( &p_sd->psz_name, &p_sd->p_cfg, cfg ));
124
125     vlc_event_manager_t *em = &p_sd->event_manager;
126     vlc_event_manager_init( em, p_sd );
127     vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryItemAdded);
128     vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryItemRemoved);
129
130     vlc_object_set_destructor( p_sd, services_discovery_Destructor );
131     return p_sd;
132 }
133
134 /*******************************************************************//**
135  * Start a Service Discovery
136  ***********************************************************************/
137 bool vlc_sd_Start ( services_discovery_t * p_sd )
138 {
139     assert(!p_sd->p_module);
140
141     p_sd->p_module = module_need( p_sd, "services_discovery",
142                                   p_sd->psz_name, true );
143     if( p_sd->p_module == NULL )
144     {
145         msg_Err( p_sd, "no suitable services discovery module" );
146         return false;
147     }
148
149     return true;
150 }
151
152 /*******************************************************************//**
153  * Stop a Service Discovery
154  ***********************************************************************/
155 void vlc_sd_Stop ( services_discovery_t * p_sd )
156 {
157     module_unneed( p_sd, p_sd->p_module );
158     p_sd->p_module = NULL;
159 }
160
161 /*******************************************************************//**
162  * Destroy a Service Discovery
163  ***********************************************************************/
164 void vlc_sd_Destroy( services_discovery_t *p_sd )
165 {
166     config_ChainDestroy( p_sd->p_cfg );
167     free( p_sd->psz_name );
168     vlc_object_release( p_sd );
169 }
170
171 /*******************************************************************//**
172  * Destructor of the Service Discovery
173  ***********************************************************************/
174 static void services_discovery_Destructor ( vlc_object_t *p_obj )
175 {
176     services_discovery_t * p_sd = (services_discovery_t *)p_obj;
177     assert(!p_sd->p_module); /* Forgot to call Stop */
178     vlc_event_manager_fini( &p_sd->event_manager );
179 }
180
181 /*******************************************************************//**
182  * Get the Localized Name
183  *
184  * This is useful for interfaces and libVLC
185  ***********************************************************************/
186 char *
187 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
188 {
189     if (p_sd->p_module == NULL)
190         return NULL;
191     return strdup( module_get_name( p_sd->p_module, true ) );
192 }
193
194 /*******************************************************************//**
195  * Getter for the EventManager
196  *
197  * You can receive event notification
198  * This is the preferred way to get new items
199  ***********************************************************************/
200 vlc_event_manager_t *
201 services_discovery_EventManager ( services_discovery_t * p_sd )
202 {
203     return &p_sd->event_manager;
204 }
205
206 /*******************************************************************//**
207  * Add an item to the Service Discovery listing
208  ***********************************************************************/
209 void
210 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
211                              const char * psz_category )
212 {
213     vlc_event_t event;
214     event.type = vlc_ServicesDiscoveryItemAdded;
215     event.u.services_discovery_item_added.p_new_item = p_item;
216     event.u.services_discovery_item_added.psz_category = psz_category;
217
218     vlc_event_send( &p_sd->event_manager, &event );
219 }
220
221 /*******************************************************************//**
222  * Remove an item from the Service Discovery listing
223  ***********************************************************************/
224 void
225 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
226 {
227     vlc_event_t event;
228     event.type = vlc_ServicesDiscoveryItemRemoved;
229     event.u.services_discovery_item_removed.p_item = p_item;
230
231     vlc_event_send( &p_sd->event_manager, &event );
232 }
233
234 /*
235  * Playlist - Services discovery bridge
236  */
237
238 struct vlc_sd_internal_t
239 {
240     /* the playlist items for category and onelevel */
241     playlist_item_t      *node;
242     services_discovery_t *sd; /**< Loaded service discovery modules */
243     char name[];
244 };
245
246  /* A new item has been added to a certain sd */
247 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
248 {
249     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
250     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
251     playlist_item_t * p_parent = user_data;
252     playlist_t * p_playlist = p_parent->p_playlist;
253
254     msg_Dbg( p_playlist, "Adding %s in %s",
255                 p_input->psz_name ? p_input->psz_name : "(null)",
256                 psz_cat ? psz_cat : "(null)" );
257
258     PL_LOCK;
259     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
260     if( !EMPTY_STR(psz_cat) )
261     {
262         /* */
263         playlist_item_t * p_cat;
264         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
265         if( !p_cat )
266         {
267             p_cat = playlist_NodeCreate( p_playlist, psz_cat,
268                                          p_parent, PLAYLIST_END, 0, NULL );
269             p_cat->i_flags |= PLAYLIST_RO_FLAG | PLAYLIST_SKIP_FLAG;
270         }
271         p_parent = p_cat;
272     }
273
274     playlist_NodeAddInput( p_playlist, p_input, p_parent,
275                            PLAYLIST_APPEND, PLAYLIST_END,
276                            pl_Locked );
277     PL_UNLOCK;
278 }
279
280  /* A new item has been removed from a certain sd */
281 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
282 {
283     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
284     playlist_item_t * p_sd_node = user_data;
285     playlist_t *p_playlist = p_sd_node->p_playlist;
286
287     PL_LOCK;
288     playlist_item_t *p_item =
289         playlist_ItemFindFromInputAndRoot( p_playlist, p_input,
290                                            p_sd_node, false );
291     if( !p_item )
292     {
293         PL_UNLOCK; return;
294     }
295     playlist_item_t *p_parent = p_item->p_parent;
296     /* if the item was added under a category and the category node
297        becomes empty, delete that node as well */
298     if( p_parent->i_children > 1 || p_parent == p_sd_node )
299         playlist_DeleteItem( p_playlist, p_item, true );
300     else
301         playlist_NodeDelete( p_playlist, p_parent, true, true );
302     PL_UNLOCK;
303 }
304
305 int playlist_ServicesDiscoveryAdd(playlist_t *playlist, const char *chain)
306 {
307     vlc_sd_internal_t *sds = malloc(sizeof (*sds) + strlen(chain) + 1);
308     if (unlikely(sds == NULL))
309         return VLC_ENOMEM;
310
311     /* Look for configuration chain "longname" */
312     const char *longname = "?";
313     config_chain_t *cfg;
314     char *name;
315
316     free(config_ChainCreate(&name, &cfg, chain));
317     msg_Dbg(playlist, "adding services_discovery %s...", name);
318
319     for (config_chain_t *p = cfg; p != NULL; p = p->p_next)
320         if (cfg->psz_name != NULL && !strcmp(cfg->psz_name, "longname"))
321         {
322             if (cfg->psz_value != NULL)
323                 longname = cfg->psz_value;
324             break;
325         }
326
327     playlist_Lock(playlist);
328     sds->node = playlist_NodeCreate(playlist, longname, playlist->p_root,
329                                     PLAYLIST_END,
330                                     PLAYLIST_RO_FLAG|PLAYLIST_SKIP_FLAG, NULL);
331     playlist_Unlock(playlist);
332
333     config_ChainDestroy(cfg);
334     free(name);
335
336     if (unlikely(sds->node == NULL))
337     {
338         free(sds);
339         return VLC_ENOMEM;
340     }
341
342     /* Perform the addition */
343     sds->sd = vlc_sd_Create(VLC_OBJECT(playlist), chain);
344     if (unlikely(sds->sd == NULL))
345     {
346         playlist_Lock(playlist);
347         playlist_NodeDelete(playlist, sds->node, true, false);
348         playlist_Unlock(playlist);
349         free(sds);
350         return VLC_ENOMEM;
351     }
352
353     vlc_event_manager_t *em = services_discovery_EventManager(sds->sd);
354     vlc_event_attach(em, vlc_ServicesDiscoveryItemAdded,
355                      playlist_sd_item_added, sds->node);
356     vlc_event_attach(em, vlc_ServicesDiscoveryItemRemoved,
357                      playlist_sd_item_removed, sds->node);
358
359     if (!vlc_sd_Start(sds->sd))
360     {
361         vlc_sd_Destroy(sds->sd);
362         playlist_Lock(playlist);
363         playlist_NodeDelete(playlist, sds->node, true, false);
364         playlist_Unlock(playlist);
365         free(sds);
366         return VLC_EGENERIC;
367     }
368
369     strcpy(sds->name, chain);
370
371     playlist_Lock(playlist);
372     TAB_APPEND(pl_priv(playlist)->i_sds, pl_priv(playlist)->pp_sds, sds);
373     playlist_Unlock(playlist);
374     return VLC_SUCCESS;
375 }
376
377 static void playlist_ServicesDiscoveryInternalRemove(playlist_t *playlist,
378                                                      vlc_sd_internal_t *sds)
379 {
380     assert(sds->sd != NULL);
381     vlc_sd_Stop(sds->sd);
382
383     vlc_event_detach(services_discovery_EventManager(sds->sd),
384                      vlc_ServicesDiscoveryItemAdded,
385                      playlist_sd_item_added, sds->node);
386     vlc_event_detach(services_discovery_EventManager(sds->sd),
387                      vlc_ServicesDiscoveryItemRemoved,
388                      playlist_sd_item_removed, sds->node);
389     vlc_sd_Destroy(sds->sd);
390
391     /* Remove the sd playlist node if it exists */
392     playlist_Lock(playlist);
393     playlist_NodeDelete(playlist, sds->node, true, false);
394     playlist_Unlock(playlist);
395
396     free(sds);
397 }
398
399
400 int playlist_ServicesDiscoveryRemove(playlist_t *playlist, const char *name)
401 {
402     playlist_private_t *priv = pl_priv(playlist);
403     vlc_sd_internal_t *sds = NULL;
404
405     playlist_Lock(playlist);
406     for (int i = 0; i < priv->i_sds; i++)
407     {
408         vlc_sd_internal_t *entry = priv->pp_sds[i];
409
410         if (!strcmp(name, entry->name))
411         {
412             REMOVE_ELEM(priv->pp_sds, priv->i_sds, i);
413             sds = entry;
414             break;
415         }
416     }
417     playlist_Unlock(playlist);
418
419     if (sds == NULL)
420     {
421         msg_Warn(playlist, "discovery %s is not loaded", name);
422         return VLC_EGENERIC;
423     }
424
425     playlist_ServicesDiscoveryInternalRemove(playlist, sds);
426     return VLC_SUCCESS;
427 }
428
429 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
430                                          const char *psz_name )
431 {
432     playlist_private_t *priv = pl_priv( p_playlist );
433     bool found = false;
434     PL_LOCK;
435
436     for( int i = 0; i < priv->i_sds; i++ )
437     {
438         vlc_sd_internal_t *sds = priv->pp_sds[i];
439
440         if (!strcmp(psz_name, sds->name))
441         {
442             found = true;
443             break;
444         }
445     }
446     PL_UNLOCK;
447     return found;
448 }
449
450 int playlist_ServicesDiscoveryControl( playlist_t *p_playlist, const char *psz_name, int i_control, ... )
451 {
452     playlist_private_t *priv = pl_priv( p_playlist );
453     int i_ret = VLC_EGENERIC;
454     int i;
455
456     PL_LOCK;
457     for( i = 0; i < priv->i_sds; i++ )
458     {
459         vlc_sd_internal_t *sds = priv->pp_sds[i];
460         if (!strcmp(psz_name, sds->name))
461         {
462             va_list args;
463             va_start( args, i_control );
464             i_ret = vlc_sd_control(sds->sd, i_control, args );
465             va_end( args );
466             break;
467         }
468     }
469
470     assert( i != priv->i_sds );
471     PL_UNLOCK;
472
473     return i_ret;
474 }
475
476 void playlist_ServicesDiscoveryKillAll(playlist_t *playlist)
477 {
478     playlist_private_t *priv = pl_priv(playlist);
479
480     for (int i = 0; i < priv->i_sds; i++)
481         playlist_ServicesDiscoveryInternalRemove(playlist, priv->pp_sds[i]);
482
483     TAB_CLEAN(priv->i_sds, priv->pp_sds);
484 }