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