]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
playlist: create root SD node before registering events
[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 /* FIXME: this function sucks by design. Trying to probe multiple SDs at the
234  * same time prevents proper error handling. We should really probe ONLY ONE
235  * SD at a time. */
236 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist, const char *psz_modules )
237 {
238     const char *psz_parser = psz_modules ?: "";
239
240     for (;;)
241     {
242         struct playlist_services_discovery_support_t * p_sds;
243         playlist_item_t * p_cat;
244         playlist_item_t * p_one;
245
246         while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
247             psz_parser++;
248
249         if( *psz_parser == '\0' )
250             break;
251
252         const char *psz_next = strchr( psz_parser, ':' );
253         if( psz_next == NULL )
254             psz_next = psz_parser + strlen( psz_parser );
255
256         char psz_plugin[psz_next - psz_parser + 1];
257         memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
258         psz_plugin[sizeof (psz_plugin) - 1] = '\0';
259         psz_parser = psz_next;
260
261         /* Perform the addition */
262         msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
263
264         services_discovery_t *p_sd = vlc_sd_Create( VLC_OBJECT(p_playlist) );
265         if( !p_sd )
266             return VLC_ENOMEM;
267
268         /* Free in playlist_ServicesDiscoveryRemove */
269         p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
270         if( !p_sds )
271         {
272             vlc_sd_Destroy( p_sd );
273             return VLC_ENOMEM;
274         }
275
276         module_t *m = module_find( psz_plugin );
277         if( !m )
278         {
279              msg_Err( p_playlist, "No such module: %s", psz_plugin );
280              return VLC_EGENERIC;
281         }
282
283         PL_LOCK;
284         playlist_NodesPairCreate( p_playlist, module_get_name( m, true ),
285                                   &p_cat, &p_one, false );
286         PL_UNLOCK;
287         module_release( m );
288
289         vlc_event_attach( services_discovery_EventManager( p_sd ),
290                           vlc_ServicesDiscoveryItemAdded,
291                           playlist_sd_item_added,
292                           p_one );
293         
294         vlc_event_attach( services_discovery_EventManager( p_sd ),
295                           vlc_ServicesDiscoveryItemAdded,
296                           playlist_sd_item_added,
297                           p_cat );
298
299         vlc_event_attach( services_discovery_EventManager( p_sd ),
300                           vlc_ServicesDiscoveryItemRemoved,
301                           playlist_sd_item_removed,
302                           p_one );
303
304         vlc_event_attach( services_discovery_EventManager( p_sd ),
305                           vlc_ServicesDiscoveryItemRemoved,
306                           playlist_sd_item_removed,
307                           p_cat );
308
309         if( !vlc_sd_Start( p_sd, psz_plugin ) )
310         {
311             vlc_sd_Destroy( p_sd );
312             free( p_sds );
313             return VLC_EGENERIC;
314         }
315
316         /* We want tree-view for service directory */
317         p_one->p_input->b_prefers_tree = true;
318         p_sds->p_sd = p_sd;
319         p_sds->p_one = p_one;
320         p_sds->p_cat = p_cat;
321
322         PL_LOCK;
323         TAB_APPEND( pl_priv(p_playlist)->i_sds, pl_priv(p_playlist)->pp_sds, p_sds );
324         PL_UNLOCK;
325
326     }
327
328     return VLC_SUCCESS;
329 }
330
331 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
332                                       const char *psz_module )
333 {
334     struct playlist_services_discovery_support_t * p_sds = NULL;
335     int i;
336
337     PL_LOCK;
338     for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
339     {
340         if( !strcmp( psz_module, module_get_object( pl_priv(p_playlist)->pp_sds[i]->p_sd->p_module ) ) )
341         {
342             p_sds = pl_priv(p_playlist)->pp_sds[i];
343             REMOVE_ELEM( pl_priv(p_playlist)->pp_sds, pl_priv(p_playlist)->i_sds, i );
344             break;
345         }
346     }
347     PL_UNLOCK;
348
349     if( !p_sds )
350     {
351         msg_Warn( p_playlist, "module %s is not loaded", psz_module );
352         return VLC_EGENERIC;
353     }
354
355     services_discovery_t *p_sd = p_sds->p_sd;
356     assert( p_sd );
357
358     vlc_sd_Stop( p_sd );
359
360     vlc_event_detach( services_discovery_EventManager( p_sd ),
361                         vlc_ServicesDiscoveryItemAdded,
362                         playlist_sd_item_added,
363                         p_sds->p_one );
364
365     vlc_event_detach( services_discovery_EventManager( p_sd ),
366                         vlc_ServicesDiscoveryItemAdded,
367                         playlist_sd_item_added,
368                         p_sds->p_cat );
369
370     vlc_event_detach( services_discovery_EventManager( p_sd ),
371                         vlc_ServicesDiscoveryItemRemoved,
372                         playlist_sd_item_removed,
373                         p_sds->p_one );
374
375     vlc_event_detach( services_discovery_EventManager( p_sd ),
376                         vlc_ServicesDiscoveryItemRemoved,
377                         playlist_sd_item_removed,
378                         p_sds->p_cat );
379
380     /* Remove the sd playlist node if it exists */
381     PL_LOCK;
382     if( p_sds->p_cat != p_playlist->p_root_category &&
383         p_sds->p_one != p_playlist->p_root_onelevel )
384     {
385         playlist_NodeDelete( p_playlist, p_sds->p_cat, true, false );
386         playlist_NodeDelete( p_playlist, p_sds->p_one, true, false );
387     }
388     PL_UNLOCK;
389
390     vlc_sd_Destroy( p_sd );
391     free( p_sds );
392
393     return VLC_SUCCESS;
394 }
395
396 bool playlist_IsServicesDiscoveryLoaded( playlist_t * p_playlist,
397                                               const char *psz_module )
398 {
399     int i;
400     PL_LOCK;
401
402     for( i = 0 ; i< pl_priv(p_playlist)->i_sds ; i ++ )
403     {
404         if( !strcmp( psz_module, module_get_object( pl_priv(p_playlist)->pp_sds[i]->p_sd->p_module ) ) )
405         {
406             PL_UNLOCK;
407             return true;
408         }
409     }
410     PL_UNLOCK;
411     return false;
412 }
413
414 void playlist_ServicesDiscoveryKillAll( playlist_t *p_playlist )
415 {
416     while( pl_priv(p_playlist)->i_sds > 0 )
417         playlist_ServicesDiscoveryRemove( p_playlist,
418                                           module_get_object( pl_priv(p_playlist)->pp_sds[0]->p_sd->p_module ) );
419 }