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