]> git.sesse.net Git - vlc/blob - src/playlist/services_discovery.c
simplify service discoveries
[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 "playlist_internal.h"
32 #include "../libvlc.h"
33
34 /*
35  * Services discovery
36  * Basically you just listen to Service discovery event through the
37  * sd's event manager.
38  * That's how the playlist get's Service Discovery information
39  */
40
41 /***********************************************************************
42  * GetServicesNames
43  ***********************************************************************/
44 char ** __services_discovery_GetServicesNames( vlc_object_t * p_super,
45                                                char ***pppsz_longnames )
46 {
47     return module_GetModulesNamesForCapability( p_super, "services_discovery",
48                                                 pppsz_longnames );
49 }
50
51 /***********************************************************************
52  * Create
53  ***********************************************************************/
54 services_discovery_t *
55 services_discovery_Create ( vlc_object_t * p_super, const char * psz_module_name )
56 {
57     services_discovery_t *p_sd;
58     p_sd = vlc_custom_create( p_super, sizeof( *p_sd ), VLC_OBJECT_GENERIC,
59                               "services discovery" );
60     if( !p_sd )
61         return NULL;
62
63     p_sd->psz_localized_name = 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     p_sd->p_module = module_Need( p_sd, "services_discovery", psz_module_name, true );
76
77     if( p_sd->p_module == NULL )
78     {
79         msg_Err( p_super, "no suitable services discovery module" );
80         vlc_object_release( p_sd );
81         return NULL;
82     }
83     p_sd->psz_module = strdup( psz_module_name );
84     p_sd->b_die = false; /* FIXME */
85
86     vlc_object_attach( p_sd, p_super );
87
88     vlc_event_t event = {
89         .type = vlc_ServicesDiscoveryStarted
90     };
91     vlc_event_send( &p_sd->event_manager, &event );
92     return p_sd;
93 }
94
95 static void ObjectKillChildrens( vlc_object_t *p_obj )
96 {
97     vlc_list_t *p_list;
98     int i;
99     vlc_object_kill( p_obj );
100
101     p_list = vlc_list_children( p_obj );
102     for( i = 0; i < p_list->i_count; i++ )
103         ObjectKillChildrens( p_list->p_values[i].p_object );
104     vlc_list_release( p_list );
105 }
106
107 /***********************************************************************
108  * Destroy
109  ***********************************************************************/
110 void services_discovery_Destroy ( services_discovery_t * p_sd )
111 {
112     vlc_event_t event = {
113         .type = vlc_ServicesDiscoveryEnded
114     };
115
116     ObjectKillChildrens( VLC_OBJECT(p_sd) );
117
118     vlc_event_send( &p_sd->event_manager, &event );
119     module_Unneed( p_sd, p_sd->p_module );
120
121     vlc_event_manager_fini( &p_sd->event_manager );
122
123     free( p_sd->psz_module );
124     free( p_sd->psz_localized_name );
125
126     vlc_object_release( p_sd );
127 }
128
129 /***********************************************************************
130  * GetLocalizedName
131  ***********************************************************************/
132 char *
133 services_discovery_GetLocalizedName ( services_discovery_t * p_sd )
134 {
135     return p_sd->psz_localized_name ? strdup( p_sd->psz_localized_name ) : NULL;
136 }
137
138 /***********************************************************************
139  * SetLocalizedName
140  ***********************************************************************/
141 void
142 services_discovery_SetLocalizedName ( services_discovery_t * p_sd, const char *psz )
143 {
144     free( p_sd->psz_localized_name );
145     p_sd->psz_localized_name = strdup(psz);
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     vlc_object_lock( p_parent->p_playlist );
238     p_pl_item = playlist_ItemFindFromInputAndRoot( p_parent->p_playlist,
239             p_input->i_id, p_parent, false );
240
241     if( p_pl_item && p_pl_item->i_children > -1 )
242     {
243         playlist_NodeDelete( p_parent->p_playlist, p_pl_item, true, false );
244         vlc_object_unlock( p_parent->p_playlist );
245         return;
246     }
247
248     /* Delete the non-node item normally */
249     playlist_DeleteFromInputInParent( p_parent->p_playlist, p_input->i_id,
250                                       p_parent, pl_Locked );
251
252     vlc_object_unlock( p_parent->p_playlist );
253 }
254
255 int playlist_ServicesDiscoveryAdd( playlist_t *p_playlist,  const char *psz_modules )
256 {
257     const char *psz_parser = psz_modules ?: "";
258     int retval = VLC_SUCCESS;
259
260     for (;;)
261     {
262         struct playlist_services_discovery_support_t * p_sds;
263         playlist_item_t * p_cat;
264         playlist_item_t * p_one;
265
266         while( *psz_parser == ' ' || *psz_parser == ':' || *psz_parser == ',' )
267             psz_parser++;
268
269         if( *psz_parser == '\0' )
270             break;
271
272         const char *psz_next = strchr( psz_parser, ':' );
273         if( psz_next == NULL )
274             psz_next = psz_parser + strlen( psz_parser );
275
276         char psz_plugin[psz_next - psz_parser + 1];
277         memcpy (psz_plugin, psz_parser, sizeof (psz_plugin) - 1);
278         psz_plugin[sizeof (psz_plugin) - 1] = '\0';
279         psz_parser = psz_next;
280
281         /* Perform the addition */
282         msg_Dbg( p_playlist, "Add services_discovery %s", psz_plugin );
283         services_discovery_t *p_sd;
284
285         p_sd = services_discovery_Create( (vlc_object_t*)p_playlist, psz_plugin );
286         if( !p_sd )
287             continue;
288
289         char * psz = services_discovery_GetLocalizedName( p_sd );
290         assert( psz );
291         PL_LOCK;
292         playlist_NodesPairCreate( p_playlist, psz,
293                 &p_cat, &p_one, false );
294         PL_UNLOCK;
295         free( psz );
296
297         vlc_event_attach( services_discovery_EventManager( p_sd ),
298                           vlc_ServicesDiscoveryItemAdded,
299                           playlist_sd_item_added,
300                           p_one );
301
302         vlc_event_attach( services_discovery_EventManager( p_sd ),
303                           vlc_ServicesDiscoveryItemAdded,
304                           playlist_sd_item_added,
305                           p_cat );
306
307         vlc_event_attach( services_discovery_EventManager( p_sd ),
308                           vlc_ServicesDiscoveryItemRemoved,
309                           playlist_sd_item_removed,
310                           p_one );
311
312         vlc_event_attach( services_discovery_EventManager( p_sd ),
313                           vlc_ServicesDiscoveryItemRemoved,
314                           playlist_sd_item_removed,
315                           p_cat );
316
317         /* Free in playlist_ServicesDiscoveryRemove */
318         p_sds = malloc( sizeof(struct playlist_services_discovery_support_t) );
319         if( !p_sds )
320             return VLC_ENOMEM;
321
322         /* We want tree-view for service directory */
323         p_one->p_input->b_prefers_tree = true;
324         p_sds->p_sd = p_sd;
325         p_sds->p_one = p_one;
326         p_sds->p_cat = p_cat;
327
328         PL_LOCK;
329         TAB_APPEND( p_playlist->i_sds, p_playlist->pp_sds, p_sds );
330         PL_UNLOCK;
331     }
332
333     return retval;
334 }
335
336 int playlist_ServicesDiscoveryRemove( playlist_t * p_playlist,
337                                        const char *psz_module )
338 {
339     struct playlist_services_discovery_support_t * p_sds = NULL;
340     int i;
341
342     PL_LOCK;
343     for( i = 0 ; i< p_playlist->i_sds ; i ++ )
344     {
345         if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_module ) )
346         {
347             p_sds = p_playlist->pp_sds[i];
348             REMOVE_ELEM( p_playlist->pp_sds, p_playlist->i_sds, i );
349             break;
350         }
351     }
352     PL_UNLOCK;
353
354     if( !p_sds || !p_sds->p_sd )
355     {
356         msg_Warn( p_playlist, "module %s is not loaded", psz_module );
357         return VLC_EGENERIC;
358     }
359
360     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
361                         vlc_ServicesDiscoveryItemAdded,
362                         playlist_sd_item_added,
363                         p_sds->p_one );
364
365     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
366                         vlc_ServicesDiscoveryItemAdded,
367                         playlist_sd_item_added,
368                         p_sds->p_cat );
369
370     vlc_event_detach( services_discovery_EventManager( p_sds->p_sd ),
371                         vlc_ServicesDiscoveryItemRemoved,
372                         playlist_sd_item_removed,
373                         p_sds->p_one );
374
375     vlc_event_detach( services_discovery_EventManager( p_sds->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     services_discovery_Destroy( p_sds->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< p_playlist->i_sds ; i ++ )
403     {
404         if( !strcmp( psz_module, p_playlist->pp_sds[i]->p_sd->psz_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( p_playlist->i_sds > 0 )
417         playlist_ServicesDiscoveryRemove( p_playlist,
418                                      p_playlist->pp_sds[0]->p_sd->psz_module );
419 }
420