]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
demux: avi: improve broken index offset heuristic (fix #14120)
[vlc] / 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         abort();
81     for( size_t i = 0; i < count; i++ )
82     {
83         names[i] = tab[i].name;
84         longnames[i] = tab[i].longname;
85         categories[i] = tab[i].category;
86     }
87     free (tab);
88     names[count] = longnames[count] = NULL;
89     categories[count] = 0;
90     *pppsz_longnames = longnames;
91     if( pp_categories ) *pp_categories = categories;
92     else free( categories );
93     return names;
94 }
95
96
97 static void services_discovery_Destructor ( vlc_object_t *p_obj );
98
99 /*
100  * Services discovery
101  * Basically you just listen to Service discovery event through the
102  * sd's event manager.
103  * That's how the playlist get's Service Discovery information
104  */
105
106 /*******************************************************************//**
107  * Create a Service discovery
108  ***********************************************************************/
109 services_discovery_t *vlc_sd_Create( vlc_object_t *p_super,
110                                      const char *cfg )
111 {
112     services_discovery_t *p_sd;
113
114     p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), "services discovery" );
115     if( !p_sd )
116         return NULL;
117     free(config_ChainCreate( &p_sd->psz_name, &p_sd->p_cfg, cfg ));
118
119     vlc_event_manager_t *em = &p_sd->event_manager;
120     vlc_event_manager_init( em, p_sd );
121     vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryItemAdded);
122     vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryItemRemoved);
123     vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryItemRemoveAll);
124     vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryStarted);
125     vlc_event_manager_register_event_type(em, vlc_ServicesDiscoveryEnded);
126
127     vlc_object_set_destructor( p_sd, services_discovery_Destructor );
128     return p_sd;
129 }
130
131 /*******************************************************************//**
132  * Start a Service Discovery
133  ***********************************************************************/
134 bool vlc_sd_Start ( services_discovery_t * p_sd )
135 {
136     assert(!p_sd->p_module);
137
138     p_sd->p_module = module_need( p_sd, "services_discovery",
139                                   p_sd->psz_name, true );
140     if( p_sd->p_module == NULL )
141     {
142         msg_Err( p_sd, "no suitable services discovery module" );
143         return false;
144     }
145
146     vlc_event_t event = {
147         .type = vlc_ServicesDiscoveryStarted
148     };
149     vlc_event_send( &p_sd->event_manager, &event );
150     return true;
151 }
152
153 /*******************************************************************//**
154  * Stop a Service Discovery
155  ***********************************************************************/
156 void vlc_sd_Stop ( services_discovery_t * p_sd )
157 {
158     vlc_event_t event = {
159         .type = vlc_ServicesDiscoveryEnded
160     };
161
162     vlc_event_send( &p_sd->event_manager, &event );
163
164     module_unneed( p_sd, p_sd->p_module );
165     p_sd->p_module = NULL;
166 }
167
168 /*******************************************************************//**
169  * Destroy a Service Discovery
170  ***********************************************************************/
171 void vlc_sd_Destroy( services_discovery_t *p_sd )
172 {
173     config_ChainDestroy( p_sd->p_cfg );
174     free( p_sd->psz_name );
175     vlc_object_release( p_sd );
176 }
177
178 /*******************************************************************//**
179  * Destructor of the Service Discovery
180  ***********************************************************************/
181 static void services_discovery_Destructor ( vlc_object_t *p_obj )
182 {
183     services_discovery_t * p_sd = (services_discovery_t *)p_obj;
184     assert(!p_sd->p_module); /* Forgot to call Stop */
185     vlc_event_manager_fini( &p_sd->event_manager );
186 }
187
188 /*******************************************************************//**
189  * Get the Localized Name
190  *
191  * This is useful for interfaces and libVLC
192  ***********************************************************************/
193 char *
194 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
195 {
196     return strdup( module_get_name( p_sd->p_module, true ) );
197 }
198
199 /*******************************************************************//**
200  * Getter for the EventManager
201  *
202  * You can receive event notification
203  * This is the preferred way to get new items
204  ***********************************************************************/
205 vlc_event_manager_t *
206 services_discovery_EventManager ( services_discovery_t * p_sd )
207 {
208     return &p_sd->event_manager;
209 }
210
211 /*******************************************************************//**
212  * Remove all items from the Service Discovery listing
213  ***********************************************************************/
214 void
215 services_discovery_RemoveAll ( services_discovery_t * p_sd )
216 {
217     vlc_event_t event;
218     event.type = vlc_ServicesDiscoveryItemRemoveAll;
219
220     vlc_event_send( &p_sd->event_manager, &event );
221 }
222
223 /*******************************************************************//**
224  * Add an item to the Service Discovery listing
225  ***********************************************************************/
226 void
227 services_discovery_AddItem ( services_discovery_t * p_sd, input_item_t * p_item,
228                              const char * psz_category )
229 {
230     vlc_event_t event;
231     event.type = vlc_ServicesDiscoveryItemAdded;
232     event.u.services_discovery_item_added.p_new_item = p_item;
233     event.u.services_discovery_item_added.psz_category = psz_category;
234
235     vlc_event_send( &p_sd->event_manager, &event );
236 }
237
238 /*******************************************************************//**
239  * Remove an item from the Service Discovery listing
240  ***********************************************************************/
241 void
242 services_discovery_RemoveItem ( services_discovery_t * p_sd, input_item_t * p_item )
243 {
244     vlc_event_t event;
245     event.type = vlc_ServicesDiscoveryItemRemoved;
246     event.u.services_discovery_item_removed.p_item = p_item;
247
248     vlc_event_send( &p_sd->event_manager, &event );
249 }
250
251 /*
252  * Playlist - Services discovery bridge
253  */
254
255 struct vlc_sd_internal_t
256 {
257     /* the playlist items for category and onelevel */
258     playlist_item_t      *p_node;
259     services_discovery_t *p_sd; /**< Loaded service discovery modules */
260     char                 *psz_name;
261 };
262
263  /* A new item has been added to a certain sd */
264 static void playlist_sd_item_added( const vlc_event_t * p_event, void * user_data )
265 {
266     input_item_t * p_input = p_event->u.services_discovery_item_added.p_new_item;
267     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
268     playlist_item_t * p_parent = user_data;
269     playlist_t * p_playlist = p_parent->p_playlist;
270
271     msg_Dbg( p_playlist, "Adding %s in %s",
272                 p_input->psz_name ? p_input->psz_name : "(null)",
273                 psz_cat ? psz_cat : "(null)" );
274
275     PL_LOCK;
276     /* If p_parent is in root category (this is clearly a hack) and we have a cat */
277     if( !EMPTY_STR(psz_cat) )
278     {
279         /* */
280         playlist_item_t * p_cat;
281         p_cat = playlist_ChildSearchName( p_parent, psz_cat );
282         if( !p_cat )
283         {
284             p_cat = playlist_NodeCreate( p_playlist, psz_cat,
285                                          p_parent, PLAYLIST_END, 0, NULL );
286             p_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
287         }
288         p_parent = p_cat;
289     }
290
291     playlist_NodeAddInput( p_playlist, p_input, p_parent,
292                            PLAYLIST_APPEND, PLAYLIST_END,
293                            pl_Locked );
294     PL_UNLOCK;
295 }
296
297  /* A new item has been removed from a certain sd */
298 static void playlist_sd_item_removed( const vlc_event_t * p_event, void * user_data )
299 {
300     input_item_t * p_input = p_event->u.services_discovery_item_removed.p_item;
301     playlist_item_t * p_sd_node = user_data;
302     playlist_t *p_playlist = p_sd_node->p_playlist;
303
304     PL_LOCK;
305     playlist_item_t *p_item =
306         playlist_ItemFindFromInputAndRoot( p_playlist, p_input,
307                                            p_sd_node, false );
308     if( !p_item )
309     {
310         PL_UNLOCK; return;
311     }
312     playlist_item_t *p_parent = p_item->p_parent;
313     /* if the item was added under a category and the category node
314        becomes empty, delete that node as well */
315     if( p_parent->i_children > 1 || p_parent == p_sd_node )
316         playlist_DeleteItem( p_playlist, p_item, true );
317     else
318         playlist_NodeDelete( p_playlist, p_parent, true, true );
319     PL_UNLOCK;
320 }
321
322 /* A request to remove all ideas from SD */
323 static void playlist_sd_item_removeall( const vlc_event_t * p_event, void * user_data )
324 {
325     VLC_UNUSED(p_event);
326     playlist_item_t* p_sd_node = user_data;
327     if( p_sd_node == NULL ) return;
328     playlist_t* p_playlist = p_sd_node->p_playlist;
329     PL_LOCK;
330     playlist_NodeEmpty( p_playlist, p_sd_node, true );
331     PL_UNLOCK;
332 }
333
334 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,
335                                    const char *psz_name )
336 {
337     /* Perform the addition */
338     services_discovery_t *p_sd;
339
340     msg_Dbg( p_playlist, "adding services_discovery %s...", psz_name );
341     p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist), psz_name );
342     if( !p_sd )
343         return VLC_ENOMEM;
344
345     /* Free in playlist_ServicesDiscoveryRemove */
346     vlc_sd_internal_t * p_sds = malloc( sizeof(*p_sds) );
347     if( !p_sds )
348     {
349         vlc_sd_Destroy( p_sd );
350         return VLC_ENOMEM;
351     }
352
353     playlist_item_t *p_node;
354
355     /* Look for configuration chain "longname" */
356     const char *psz_longname = "?";
357     if( p_sd->p_cfg )
358     {
359         config_chain_t *cfg = p_sd->p_cfg;
360         while( cfg )
361         {
362             if( cfg->psz_name && !strcmp( cfg->psz_name, "longname" ) )
363             {
364                 psz_longname = cfg->psz_value;
365                 break;
366             }
367             cfg = cfg->p_next;
368         }
369     }
370
371     PL_LOCK;
372     p_node = playlist_NodeCreate( p_playlist, psz_longname,
373                                   p_playlist->p_root, PLAYLIST_END, 0, NULL );
374     PL_UNLOCK;
375
376     vlc_event_manager_t *em = services_discovery_EventManager( p_sd );
377     vlc_event_attach( em, vlc_ServicesDiscoveryItemAdded,
378                       playlist_sd_item_added, p_node );
379
380     vlc_event_attach( em, vlc_ServicesDiscoveryItemRemoved,
381                       playlist_sd_item_removed, p_node );
382
383     vlc_event_attach( em, vlc_ServicesDiscoveryItemRemoveAll,
384                       playlist_sd_item_removeall, p_node );
385
386     if( !vlc_sd_Start( p_sd ) )
387     {
388         vlc_sd_Destroy( p_sd );
389         free( p_sds );
390         return VLC_EGENERIC;
391     }
392
393     p_sds->p_sd = p_sd;
394     p_sds->p_node = p_node;
395     p_sds->psz_name = strdup( psz_name );
396
397     PL_LOCK;
398     TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
399     PL_UNLOCK;
400
401     return VLC_SUCCESS;
402 }
403
404 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
405                                       const char *psz_name )
406 {
407     playlist_private_t *priv = pl_priv( p_playlist );
408     vlc_sd_internal_t * p_sds = NULL;
409
410     PL_LOCK;
411     for( int i = 0; i < priv->i_sds; i++ )
412     {
413         if( !strcmp( psz_name, priv->pp_sds[i]->psz_name ) )
414         {
415             p_sds = priv->pp_sds[i];
416             REMOVE_ELEM( priv->pp_sds, priv->i_sds, i );
417             break;
418         }
419     }
420     PL_UNLOCK;
421
422     if( !p_sds )
423     {
424         msg_Warn( p_playlist, "discovery %s is not loaded", psz_name );
425         return VLC_EGENERIC;
426     }
427
428     services_discovery_t *p_sd = p_sds->p_sd;
429     assert( p_sd );
430
431     vlc_sd_Stop( p_sd );
432
433     vlc_event_detach( services_discovery_EventManager( p_sd ),
434                         vlc_ServicesDiscoveryItemAdded,
435                         playlist_sd_item_added,
436                         p_sds->p_node );
437
438     vlc_event_detach( services_discovery_EventManager( p_sd ),
439                         vlc_ServicesDiscoveryItemRemoved,
440                         playlist_sd_item_removed,
441                         p_sds->p_node );
442
443     /* Remove the sd playlist node if it exists */
444     PL_LOCK;
445     playlist_NodeDelete( p_playlist, p_sds->p_node, true, false );
446     PL_UNLOCK;
447
448     vlc_sd_Destroy( p_sd );
449     free( p_sds->psz_name );
450     free( p_sds );
451
452     return VLC_SUCCESS;
453 }
454
455 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
456                                          const char *psz_name )
457 {
458     playlist_private_t *priv = pl_priv( p_playlist );
459     bool found = false;
460     PL_LOCK;
461
462     for( int i = 0; i < priv->i_sds; i++ )
463     {
464         vlc_sd_internal_t *sd = priv->pp_sds[i];
465
466         if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
467         {
468             found = true;
469             break;
470         }
471     }
472     PL_UNLOCK;
473     return found;
474 }
475
476 int playlist_ServicesDiscoveryControl( playlist_t *p_playlist, const char *psz_name, int i_control, ... )
477 {
478     playlist_private_t *priv = pl_priv( p_playlist );
479     int i_ret = VLC_EGENERIC;
480     int i;
481
482     PL_LOCK;
483     for( i = 0; i < priv->i_sds; i++ )
484     {
485         vlc_sd_internal_t *sd = priv->pp_sds[i];
486         if( sd->psz_name && !strcmp( psz_name, sd->psz_name ) )
487         {
488             va_list args;
489             va_start( args, i_control );
490             i_ret = vlc_sd_control( sd->p_sd, i_control, args );
491             va_end( args );
492             break;
493         }
494     }
495
496     assert( i != priv->i_sds );
497     PL_UNLOCK;
498
499     return i_ret;
500 }
501
502 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
503 {
504     playlist_private_t *priv = pl_priv( p_playlist );
505
506     while( priv->i_sds > 0 )
507         playlist_ServicesDiscoveryRemove( p_playlist,
508                                           priv->pp_sds[0]->psz_name );
509 }