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