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