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