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