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