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