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