libvlc: add renderer_discoverer external API
[vlc.git] / lib / media_discoverer.c
1 /*****************************************************************************
2  * media_discoverer.c: libvlc new API media discoverer functions
3  *****************************************************************************
4  * Copyright (C) 2007 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <assert.h>
29
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_media.h>
32 #include <vlc/libvlc_media_list.h>
33 #include <vlc/libvlc_media_discoverer.h>
34 #include <vlc/libvlc_events.h>
35
36 #include <vlc_services_discovery.h>
37
38 #include "libvlc_internal.h"
39 #include "media_internal.h" // libvlc_media_new_from_input_item()
40 #include "media_list_internal.h" // libvlc_media_list_internal_add_media()
41
42 struct libvlc_media_discoverer_t
43 {
44     libvlc_event_manager_t * p_event_manager;
45     libvlc_instance_t *      p_libvlc_instance;
46     services_discovery_t *   p_sd;
47     libvlc_media_list_t *    p_mlist;
48     bool                     running;
49     vlc_dictionary_t         catname_to_submedialist;
50 };
51
52 /*
53  * Private functions
54  */
55
56 /**************************************************************************
57  *       services_discovery_item_added (Private) (VLC event callback)
58  **************************************************************************/
59
60 static void services_discovery_item_added( const vlc_event_t * p_event,
61                                            void * user_data )
62 {
63     input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
64     const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
65     libvlc_media_t * p_md;
66     libvlc_media_discoverer_t * p_mdis = user_data;
67     libvlc_media_list_t * p_mlist = p_mdis->p_mlist;
68
69     p_md = libvlc_media_new_from_input_item( p_mdis->p_libvlc_instance,
70                                              p_item );
71
72     /* If we have a category, that mean we have to group the items having
73      * that category in a media_list. */
74     if( psz_cat )
75     {
76         p_mlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, psz_cat );
77
78         if( p_mlist == kVLCDictionaryNotFound )
79         {
80             libvlc_media_t * p_catmd;
81             p_catmd = libvlc_media_new_as_node( p_mdis->p_libvlc_instance, psz_cat );
82             p_mlist = libvlc_media_subitems( p_catmd );
83             p_mlist->b_read_only = true;
84
85             /* Insert the newly created mlist in our dictionary */
86             vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist );
87
88             /* Insert the md into the root list */
89             libvlc_media_list_lock( p_mdis->p_mlist );
90             libvlc_media_list_internal_add_media( p_mdis->p_mlist, p_catmd );
91             libvlc_media_list_unlock( p_mdis->p_mlist );
92
93             /* We don't release the mlist cause the dictionary
94              * doesn't retain the object. But we release the md. */
95             libvlc_media_release( p_catmd );
96         }
97     }
98
99     libvlc_media_list_lock( p_mlist );
100     libvlc_media_list_internal_add_media( p_mlist, p_md );
101     libvlc_media_list_unlock( p_mlist );
102
103     libvlc_media_release( p_md );
104 }
105
106 /**************************************************************************
107  *       services_discovery_item_removed (Private) (VLC event callback)
108  **************************************************************************/
109
110 static void services_discovery_item_removed( const vlc_event_t * p_event,
111                                              void * user_data )
112 {
113     input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
114     libvlc_media_t * p_md;
115     libvlc_media_discoverer_t * p_mdis = user_data;
116
117     int i, count = libvlc_media_list_count( p_mdis->p_mlist );
118     libvlc_media_list_lock( p_mdis->p_mlist );
119     for( i = 0; i < count; i++ )
120     {
121         p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i );
122         assert(p_md != NULL);
123         if( p_md->p_input_item == p_item )
124         {
125             libvlc_media_list_internal_remove_index( p_mdis->p_mlist, i );
126             libvlc_media_release( p_md );
127             break;
128         }
129         libvlc_media_release( p_md );
130     }
131     libvlc_media_list_unlock( p_mdis->p_mlist );
132 }
133
134 /**************************************************************************
135  *       services_discovery_removeall (Private) (VLC event callback)
136  **************************************************************************/
137 static void services_discovery_removeall( const vlc_event_t * p_event,
138                                              void * user_data )
139 {
140     VLC_UNUSED(p_event);
141     libvlc_media_discoverer_t * p_mdis = user_data;
142
143     libvlc_media_list_lock( p_mdis->p_mlist );
144     for( int i = 0; i < libvlc_media_list_count( p_mdis->p_mlist ); i++ )
145     {
146         libvlc_media_list_internal_remove_index( p_mdis->p_mlist, i );
147     }
148     libvlc_media_list_unlock( p_mdis->p_mlist );
149 }
150
151 /*
152  * Public libvlc functions
153  */
154
155 /**************************************************************************
156  *       new (Public)
157  **************************************************************************/
158 libvlc_media_discoverer_t *
159 libvlc_media_discoverer_new( libvlc_instance_t * p_inst, const char * psz_name )
160 {
161     /* podcast SD is a hack and only works with custom playlist callbacks. */
162     if( !strncasecmp( psz_name, "podcast", 7 ) )
163         return NULL;
164
165     libvlc_media_discoverer_t *p_mdis = malloc(sizeof(*p_mdis));
166     if( unlikely(!p_mdis) )
167     {
168         libvlc_printerr( "Not enough memory" );
169         return NULL;
170     }
171
172     p_mdis->p_libvlc_instance = p_inst;
173     p_mdis->p_mlist = libvlc_media_list_new( p_inst );
174     p_mdis->p_mlist->b_read_only = true;
175     p_mdis->running = false;
176
177     vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
178
179     p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis );
180     if( unlikely(p_mdis->p_event_manager == NULL) )
181     {
182         free( p_mdis );
183         return NULL;
184     }
185
186     p_mdis->p_sd = vlc_sd_Create( (vlc_object_t*)p_inst->p_libvlc_int,
187                                   psz_name );
188     if( unlikely(p_mdis->p_sd == NULL) )
189     {
190         libvlc_printerr( "%s: no such discovery module found", psz_name );
191         libvlc_media_list_release( p_mdis->p_mlist );
192         libvlc_event_manager_release( p_mdis->p_event_manager );
193         free( p_mdis );
194         return NULL;
195     }
196
197     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
198                       vlc_ServicesDiscoveryItemAdded,
199                       services_discovery_item_added,
200                       p_mdis );
201     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
202                       vlc_ServicesDiscoveryItemRemoved,
203                       services_discovery_item_removed,
204                       p_mdis );
205     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
206                       vlc_ServicesDiscoveryItemRemoveAll,
207                       services_discovery_removeall,
208                       p_mdis );
209
210     libvlc_retain( p_inst );
211     return p_mdis;
212 }
213
214 /**************************************************************************
215  *       start (Public)
216  **************************************************************************/
217 LIBVLC_API int
218 libvlc_media_discoverer_start( libvlc_media_discoverer_t * p_mdis )
219 {
220     /* Here we go */
221     if (!vlc_sd_Start( p_mdis->p_sd ))
222         return -1;
223
224     p_mdis->running = true;
225     libvlc_event_t event;
226     event.type = libvlc_MediaDiscovererStarted;
227     libvlc_event_send( p_mdis->p_event_manager, &event );
228     return 0;
229 }
230
231 /**************************************************************************
232  *       stop (Public)
233  **************************************************************************/
234 LIBVLC_API void
235 libvlc_media_discoverer_stop( libvlc_media_discoverer_t * p_mdis )
236 {
237     p_mdis->running = false;
238
239     libvlc_media_list_t * p_mlist = p_mdis->p_mlist;
240     libvlc_media_list_lock( p_mlist );
241     libvlc_media_list_internal_end_reached( p_mlist );
242     libvlc_media_list_unlock( p_mlist );
243
244     libvlc_event_t event;
245     event.type = libvlc_MediaDiscovererEnded;
246     libvlc_event_send( p_mdis->p_event_manager, &event );
247
248     vlc_sd_Stop( p_mdis->p_sd );
249 }
250
251 /**************************************************************************
252  *       new_from_name (Public)
253  *
254  * \deprecated Use libvlc_media_discoverer_new and libvlc_media_discoverer_start
255  **************************************************************************/
256 libvlc_media_discoverer_t *
257 libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
258                                        const char * psz_name )
259 {
260     libvlc_media_discoverer_t *p_mdis = libvlc_media_discoverer_new( p_inst, psz_name );
261
262     if( !p_mdis )
263         return NULL;
264
265     if( libvlc_media_discoverer_start( p_mdis ) != 0)
266     {
267         libvlc_media_discoverer_release( p_mdis );
268         return NULL;
269     }
270
271     return p_mdis;
272 }
273
274 /**************************************************************************
275  * release (Public)
276  **************************************************************************/
277 void
278 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
279 {
280     int i;
281
282     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
283                      vlc_ServicesDiscoveryItemAdded,
284                      services_discovery_item_added,
285                      p_mdis );
286     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
287                      vlc_ServicesDiscoveryItemRemoved,
288                      services_discovery_item_removed,
289                      p_mdis );
290     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
291                      vlc_ServicesDiscoveryItemRemoveAll,
292                      services_discovery_removeall,
293                      p_mdis );
294
295     if( p_mdis->running )
296         libvlc_media_discoverer_stop( p_mdis );
297
298     vlc_sd_Destroy( p_mdis->p_sd );
299
300     libvlc_media_list_release( p_mdis->p_mlist );
301
302     /* Free catname_to_submedialist and all the mlist */
303     char ** all_keys = vlc_dictionary_all_keys( &p_mdis->catname_to_submedialist );
304     for( i = 0; all_keys[i]; i++ )
305     {
306         libvlc_media_list_t * p_catmlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, all_keys[i] );
307         libvlc_media_list_release( p_catmlist );
308         free( all_keys[i] );
309     }
310     free( all_keys );
311
312     vlc_dictionary_clear( &p_mdis->catname_to_submedialist, NULL, NULL );
313     libvlc_event_manager_release( p_mdis->p_event_manager );
314     libvlc_release( p_mdis->p_libvlc_instance );
315
316     free( p_mdis );
317 }
318
319 /**************************************************************************
320  * localized_name (Public)
321  **************************************************************************/
322 char *
323 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
324 {
325     return services_discovery_GetLocalizedName( p_mdis->p_sd );
326 }
327
328 /**************************************************************************
329  * media_list (Public)
330  **************************************************************************/
331 libvlc_media_list_t *
332 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
333 {
334     libvlc_media_list_retain( p_mdis->p_mlist );
335     return p_mdis->p_mlist;
336 }
337
338 /**************************************************************************
339  * event_manager (Public)
340  **************************************************************************/
341 libvlc_event_manager_t *
342 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
343 {
344     return p_mdis->p_event_manager;
345 }
346
347
348 /**************************************************************************
349  * running (Public)
350  **************************************************************************/
351 int
352 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
353 {
354     return p_mdis->running;
355 }
356
357 void
358 libvlc_media_discoverer_list_release( libvlc_media_discoverer_description_t **pp_services,
359                                       size_t i_count )
360 {
361     if( i_count > 0 )
362     {
363         for( size_t i = 0; i < i_count; ++i )
364         {
365             free( pp_services[i]->psz_name );
366             free( pp_services[i]->psz_longname );
367         }
368         free( *pp_services );
369         free( pp_services );
370     }
371 }
372
373 ssize_t
374 libvlc_media_discoverer_list_get( libvlc_instance_t *p_inst,
375                                   libvlc_media_discoverer_category_t i_cat,
376                                   libvlc_media_discoverer_description_t ***ppp_services )
377 {
378     assert( p_inst != NULL && ppp_services != NULL );
379
380     int i_core_cat;
381     switch( i_cat )
382     {
383     case libvlc_media_discoverer_devices:
384         i_core_cat = SD_CAT_DEVICES;
385         break;
386     case libvlc_media_discoverer_lan:
387         i_core_cat = SD_CAT_LAN;
388         break;
389     case libvlc_media_discoverer_podcasts:
390         i_core_cat = SD_CAT_INTERNET;
391         break;
392     case libvlc_media_discoverer_localdirs:
393         i_core_cat = SD_CAT_MYCOMPUTER;
394         break;
395     default:
396         vlc_assert_unreachable();
397         *ppp_services = NULL;
398         return -1;
399     }
400
401     /* Fetch all sd names, longnames and categories */
402     char **ppsz_names, **ppsz_longnames;
403     int *p_categories;
404     ppsz_names = vlc_sd_GetNames( p_inst->p_libvlc_int, &ppsz_longnames,
405                                   &p_categories );
406
407     if( ppsz_names == NULL )
408     {
409         *ppp_services = NULL;
410         return 0;
411     }
412
413     /* Count the number of sd matching our category (i_cat/i_core_cat) */
414     ssize_t i_nb_services = 0;
415     char **ppsz_name = ppsz_names;
416     int *p_category = p_categories;
417     for( ; *ppsz_name != NULL; ppsz_name++, p_category++ )
418     {
419         if( *p_category == i_core_cat )
420             i_nb_services++;
421     }
422
423     libvlc_media_discoverer_description_t **pp_services = NULL, *p_services = NULL;
424     if( i_nb_services > 0 )
425     {
426         /* Double alloc here, so that the caller iterates through pointers of
427          * struct instead of structs. This allows us to modify the struct
428          * without breaking the API. */
429
430         pp_services = malloc( i_nb_services
431                               * sizeof(libvlc_media_discoverer_description_t *) );
432         p_services = malloc( i_nb_services
433                              * sizeof(libvlc_media_discoverer_description_t) );
434         if( pp_services == NULL || p_services == NULL )
435         {
436             free( pp_services );
437             free( p_services );
438             pp_services = NULL;
439             p_services = NULL;
440             i_nb_services = -1;
441             /* Even if alloc fails, the next loop must be run in order to free
442              * names returned by vlc_sd_GetNames */
443         }
444     }
445
446     /* Fill output pp_services or free unused name, longname */
447     char **ppsz_longname = ppsz_longnames;
448     ppsz_name = ppsz_names;
449     p_category = p_categories;
450     unsigned int i_service_idx = 0;
451     libvlc_media_discoverer_description_t *p_service = p_services;
452     for( ; *ppsz_name != NULL; ppsz_name++, ppsz_longname++, p_category++ )
453     {
454         if( pp_services != NULL && *p_category == i_core_cat )
455         {
456             p_service->psz_name = *ppsz_name;
457             p_service->psz_longname = *ppsz_longname;
458             p_service->i_cat = i_cat;
459             pp_services[i_service_idx++] = p_service++;
460         }
461         else
462         {
463             free( *ppsz_name );
464             free( *ppsz_longname );
465         }
466     }
467     free( ppsz_names );
468     free( ppsz_longnames );
469     free( p_categories );
470
471     *ppp_services = pp_services;
472     return i_nb_services;
473 }