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