ce56a014d70c96d2fed9d41be010b9378865cf73
[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  * Public libvlc functions
136  */
137
138 /**************************************************************************
139  *       new (Public)
140  **************************************************************************/
141 libvlc_media_discoverer_t *
142 libvlc_media_discoverer_new( libvlc_instance_t * p_inst, const char * psz_name )
143 {
144     /* podcast SD is a hack and only works with custom playlist callbacks. */
145     if( !strncasecmp( psz_name, "podcast", 7 ) )
146         return NULL;
147
148     libvlc_media_discoverer_t *p_mdis = malloc(sizeof(*p_mdis));
149     if( unlikely(!p_mdis) )
150     {
151         libvlc_printerr( "Not enough memory" );
152         return NULL;
153     }
154
155     p_mdis->p_libvlc_instance = p_inst;
156     p_mdis->p_mlist = libvlc_media_list_new( p_inst );
157     p_mdis->p_mlist->b_read_only = true;
158     p_mdis->running = false;
159
160     vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
161
162     p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis );
163     if( unlikely(p_mdis->p_event_manager == NULL) )
164     {
165         free( p_mdis );
166         return NULL;
167     }
168
169     p_mdis->p_sd = vlc_sd_Create( (vlc_object_t*)p_inst->p_libvlc_int,
170                                   psz_name );
171     if( unlikely(p_mdis->p_sd == NULL) )
172     {
173         libvlc_printerr( "%s: no such discovery module found", psz_name );
174         libvlc_media_list_release( p_mdis->p_mlist );
175         libvlc_event_manager_release( p_mdis->p_event_manager );
176         free( p_mdis );
177         return NULL;
178     }
179
180     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
181                       vlc_ServicesDiscoveryItemAdded,
182                       services_discovery_item_added,
183                       p_mdis );
184     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
185                       vlc_ServicesDiscoveryItemRemoved,
186                       services_discovery_item_removed,
187                       p_mdis );
188
189     libvlc_retain( p_inst );
190     return p_mdis;
191 }
192
193 /**************************************************************************
194  *       start (Public)
195  **************************************************************************/
196 LIBVLC_API int
197 libvlc_media_discoverer_start( libvlc_media_discoverer_t * p_mdis )
198 {
199     /* Here we go */
200     if (!vlc_sd_Start( p_mdis->p_sd ))
201         return -1;
202
203     p_mdis->running = true;
204     libvlc_event_t event;
205     event.type = libvlc_MediaDiscovererStarted;
206     libvlc_event_send( p_mdis->p_event_manager, &event );
207     return 0;
208 }
209
210 /**************************************************************************
211  *       stop (Public)
212  **************************************************************************/
213 LIBVLC_API void
214 libvlc_media_discoverer_stop( libvlc_media_discoverer_t * p_mdis )
215 {
216     p_mdis->running = false;
217
218     libvlc_media_list_t * p_mlist = p_mdis->p_mlist;
219     libvlc_media_list_lock( p_mlist );
220     libvlc_media_list_internal_end_reached( p_mlist );
221     libvlc_media_list_unlock( p_mlist );
222
223     libvlc_event_t event;
224     event.type = libvlc_MediaDiscovererEnded;
225     libvlc_event_send( p_mdis->p_event_manager, &event );
226
227     vlc_sd_Stop( p_mdis->p_sd );
228 }
229
230 /**************************************************************************
231  *       new_from_name (Public)
232  *
233  * \deprecated Use libvlc_media_discoverer_new and libvlc_media_discoverer_start
234  **************************************************************************/
235 libvlc_media_discoverer_t *
236 libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
237                                        const char * psz_name )
238 {
239     libvlc_media_discoverer_t *p_mdis = libvlc_media_discoverer_new( p_inst, psz_name );
240
241     if( !p_mdis )
242         return NULL;
243
244     if( libvlc_media_discoverer_start( p_mdis ) != 0)
245     {
246         libvlc_media_discoverer_release( p_mdis );
247         return NULL;
248     }
249
250     return p_mdis;
251 }
252
253 /**************************************************************************
254  * release (Public)
255  **************************************************************************/
256 void
257 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
258 {
259     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
260                      vlc_ServicesDiscoveryItemAdded,
261                      services_discovery_item_added,
262                      p_mdis );
263     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
264                      vlc_ServicesDiscoveryItemRemoved,
265                      services_discovery_item_removed,
266                      p_mdis );
267
268     if( p_mdis->running )
269         libvlc_media_discoverer_stop( p_mdis );
270
271     vlc_sd_Destroy( p_mdis->p_sd );
272
273     libvlc_media_list_release( p_mdis->p_mlist );
274
275     /* Free catname_to_submedialist and all the mlist */
276     char ** all_keys = vlc_dictionary_all_keys( &p_mdis->catname_to_submedialist );
277     for( int i = 0; all_keys[i]; i++ )
278     {
279         libvlc_media_list_t * p_catmlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, all_keys[i] );
280         libvlc_media_list_release( p_catmlist );
281         free( all_keys[i] );
282     }
283     free( all_keys );
284
285     vlc_dictionary_clear( &p_mdis->catname_to_submedialist, NULL, NULL );
286     libvlc_event_manager_release( p_mdis->p_event_manager );
287     libvlc_release( p_mdis->p_libvlc_instance );
288
289     free( p_mdis );
290 }
291
292 /**************************************************************************
293  * localized_name (Public)
294  **************************************************************************/
295 char *
296 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
297 {
298     return services_discovery_GetLocalizedName( p_mdis->p_sd );
299 }
300
301 /**************************************************************************
302  * media_list (Public)
303  **************************************************************************/
304 libvlc_media_list_t *
305 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
306 {
307     libvlc_media_list_retain( p_mdis->p_mlist );
308     return p_mdis->p_mlist;
309 }
310
311 /**************************************************************************
312  * event_manager (Public)
313  **************************************************************************/
314 libvlc_event_manager_t *
315 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
316 {
317     return p_mdis->p_event_manager;
318 }
319
320
321 /**************************************************************************
322  * running (Public)
323  **************************************************************************/
324 int
325 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
326 {
327     return p_mdis->running;
328 }
329
330 void
331 libvlc_media_discoverer_list_release( libvlc_media_discoverer_description_t **pp_services,
332                                       size_t i_count )
333 {
334     if( i_count > 0 )
335     {
336         for( size_t i = 0; i < i_count; ++i )
337         {
338             free( pp_services[i]->psz_name );
339             free( pp_services[i]->psz_longname );
340         }
341         free( *pp_services );
342         free( pp_services );
343     }
344 }
345
346 size_t
347 libvlc_media_discoverer_list_get( libvlc_instance_t *p_inst,
348                                   libvlc_media_discoverer_category_t i_cat,
349                                   libvlc_media_discoverer_description_t ***ppp_services )
350 {
351     assert( p_inst != NULL && ppp_services != NULL );
352
353     int i_core_cat;
354     switch( i_cat )
355     {
356     case libvlc_media_discoverer_devices:
357         i_core_cat = SD_CAT_DEVICES;
358         break;
359     case libvlc_media_discoverer_lan:
360         i_core_cat = SD_CAT_LAN;
361         break;
362     case libvlc_media_discoverer_podcasts:
363         i_core_cat = SD_CAT_INTERNET;
364         break;
365     case libvlc_media_discoverer_localdirs:
366         i_core_cat = SD_CAT_MYCOMPUTER;
367         break;
368     default:
369         vlc_assert_unreachable();
370         *ppp_services = NULL;
371         return 0;
372     }
373
374     /* Fetch all sd names, longnames and categories */
375     char **ppsz_names, **ppsz_longnames;
376     int *p_categories;
377     ppsz_names = vlc_sd_GetNames( p_inst->p_libvlc_int, &ppsz_longnames,
378                                   &p_categories );
379
380     if( ppsz_names == NULL )
381     {
382         *ppp_services = NULL;
383         return 0;
384     }
385
386     /* Count the number of sd matching our category (i_cat/i_core_cat) */
387     size_t i_nb_services = 0;
388     char **ppsz_name = ppsz_names;
389     int *p_category = p_categories;
390     for( ; *ppsz_name != NULL; ppsz_name++, p_category++ )
391     {
392         if( *p_category == i_core_cat )
393             i_nb_services++;
394     }
395
396     libvlc_media_discoverer_description_t **pp_services = NULL, *p_services = NULL;
397     if( i_nb_services > 0 )
398     {
399         /* Double alloc here, so that the caller iterates through pointers of
400          * struct instead of structs. This allows us to modify the struct
401          * without breaking the API. */
402
403         pp_services = malloc( i_nb_services
404                               * sizeof(libvlc_media_discoverer_description_t *) );
405         p_services = malloc( i_nb_services
406                              * sizeof(libvlc_media_discoverer_description_t) );
407         if( pp_services == NULL || p_services == NULL )
408         {
409             free( pp_services );
410             free( p_services );
411             pp_services = NULL;
412             p_services = NULL;
413             i_nb_services = 0;
414             /* Even if alloc fails, the next loop must be run in order to free
415              * names returned by vlc_sd_GetNames */
416         }
417     }
418
419     /* Fill output pp_services or free unused name, longname */
420     char **ppsz_longname = ppsz_longnames;
421     ppsz_name = ppsz_names;
422     p_category = p_categories;
423     unsigned int i_service_idx = 0;
424     libvlc_media_discoverer_description_t *p_service = p_services;
425     for( ; *ppsz_name != NULL; ppsz_name++, ppsz_longname++, p_category++ )
426     {
427         if( pp_services != NULL && *p_category == i_core_cat )
428         {
429             p_service->psz_name = *ppsz_name;
430             p_service->psz_longname = *ppsz_longname;
431             p_service->i_cat = i_cat;
432             pp_services[i_service_idx++] = p_service++;
433         }
434         else
435         {
436             free( *ppsz_name );
437             free( *ppsz_longname );
438         }
439     }
440     free( ppsz_names );
441     free( ppsz_longnames );
442     free( p_categories );
443
444     *ppp_services = pp_services;
445     return i_nb_services;
446 }