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