mux: mp4: add mp41 brand for mpga/mp3
[vlc.git] / lib / media_discoverer.c
blob80ae82bc50377ad0bb53d9c87d30b75d2f4f2855
1 /*****************************************************************************
2 * media_discoverer.c: libvlc new API media discoverer functions
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
6 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <assert.h>
29 #include <vlc/libvlc.h>
30 #include <vlc/libvlc_picture.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>
36 #include <vlc_services_discovery.h>
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()
42 struct libvlc_media_discoverer_t
44 libvlc_instance_t * p_libvlc_instance;
45 services_discovery_t * p_sd;
46 libvlc_media_list_t * p_mlist;
47 vlc_dictionary_t catname_to_submedialist;
48 char name[];
52 * Private functions
55 /**************************************************************************
56 * services_discovery_item_added (Private) (VLC event callback)
57 **************************************************************************/
59 static void services_discovery_item_added( services_discovery_t *sd,
60 input_item_t *parent,
61 input_item_t *p_item,
62 const char *psz_cat )
64 libvlc_media_t * p_md;
65 libvlc_media_discoverer_t *p_mdis = sd->owner.sys;
66 libvlc_media_list_t * p_mlist = p_mdis->p_mlist;
68 p_md = libvlc_media_new_from_input_item( p_mdis->p_libvlc_instance,
69 p_item );
71 if( parent != NULL )
73 /* Flatten items list for now. TODO: tree support. */
75 else
76 /* If we have a category, that mean we have to group the items having
77 * that category in a media_list. */
78 if( psz_cat )
80 p_mlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, psz_cat );
82 if( p_mlist == kVLCDictionaryNotFound )
84 libvlc_media_t * p_catmd;
85 p_catmd = libvlc_media_new_as_node( p_mdis->p_libvlc_instance, psz_cat );
86 p_mlist = libvlc_media_subitems( p_catmd );
87 p_mlist->b_read_only = true;
89 /* Insert the newly created mlist in our dictionary */
90 vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist );
92 /* Insert the md into the root list */
93 libvlc_media_list_lock( p_mdis->p_mlist );
94 libvlc_media_list_internal_add_media( p_mdis->p_mlist, p_catmd );
95 libvlc_media_list_unlock( p_mdis->p_mlist );
97 /* We don't release the mlist cause the dictionary
98 * doesn't retain the object. But we release the md. */
99 libvlc_media_release( p_catmd );
103 libvlc_media_list_lock( p_mlist );
104 libvlc_media_list_internal_add_media( p_mlist, p_md );
105 libvlc_media_list_unlock( p_mlist );
107 libvlc_media_release( p_md );
110 /**************************************************************************
111 * services_discovery_item_removed (Private) (VLC event callback)
112 **************************************************************************/
114 static void services_discovery_item_removed( services_discovery_t *sd,
115 input_item_t *p_item )
117 libvlc_media_t * p_md;
118 libvlc_media_discoverer_t *p_mdis = sd->owner.sys;
120 int i, count = libvlc_media_list_count( p_mdis->p_mlist );
121 libvlc_media_list_lock( p_mdis->p_mlist );
122 for( i = 0; i < count; i++ )
124 p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i );
125 assert(p_md != NULL);
126 if( p_md->p_input_item == p_item )
128 libvlc_media_list_internal_remove_index( p_mdis->p_mlist, i );
129 libvlc_media_release( p_md );
130 break;
132 libvlc_media_release( p_md );
134 libvlc_media_list_unlock( p_mdis->p_mlist );
138 * Public libvlc functions
141 /**************************************************************************
142 * new (Public)
143 **************************************************************************/
144 libvlc_media_discoverer_t *
145 libvlc_media_discoverer_new( libvlc_instance_t * p_inst, const char * psz_name )
147 /* podcast SD is a hack and only works with custom playlist callbacks. */
148 if( !strncasecmp( psz_name, "podcast", 7 ) )
149 return NULL;
151 libvlc_media_discoverer_t *p_mdis;
153 p_mdis = malloc(sizeof(*p_mdis) + strlen(psz_name) + 1);
154 if( unlikely(p_mdis == NULL) )
156 libvlc_printerr( "Not enough memory" );
157 return NULL;
160 p_mdis->p_libvlc_instance = p_inst;
161 p_mdis->p_mlist = libvlc_media_list_new( p_inst );
162 p_mdis->p_mlist->b_read_only = true;
163 p_mdis->p_sd = NULL;
165 vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
167 libvlc_retain( p_inst );
168 strcpy( p_mdis->name, psz_name );
169 return p_mdis;
172 static const struct services_discovery_callbacks sd_cbs = {
173 .item_added = services_discovery_item_added,
174 .item_removed = services_discovery_item_removed,
177 /**************************************************************************
178 * start (Public)
179 **************************************************************************/
180 LIBVLC_API int
181 libvlc_media_discoverer_start( libvlc_media_discoverer_t * p_mdis )
183 struct services_discovery_owner_t owner = {
184 &sd_cbs,
185 p_mdis,
188 /* Here we go */
189 p_mdis->p_sd = vlc_sd_Create( (vlc_object_t *)p_mdis->p_libvlc_instance->p_libvlc_int,
190 p_mdis->name, &owner );
191 if( p_mdis->p_sd == NULL )
193 libvlc_printerr( "%s: no such discovery module found", p_mdis->name );
194 return -1;
197 return 0;
200 /**************************************************************************
201 * stop (Public)
202 **************************************************************************/
203 LIBVLC_API void
204 libvlc_media_discoverer_stop( libvlc_media_discoverer_t * p_mdis )
206 libvlc_media_list_t * p_mlist = p_mdis->p_mlist;
207 libvlc_media_list_lock( p_mlist );
208 libvlc_media_list_internal_end_reached( p_mlist );
209 libvlc_media_list_unlock( p_mlist );
211 vlc_sd_Destroy( p_mdis->p_sd );
212 p_mdis->p_sd = NULL;
215 /**************************************************************************
216 * release (Public)
217 **************************************************************************/
218 static void
219 MediaListDictValueRelease( void* mlist, void* obj )
221 libvlc_media_list_release( mlist );
222 (void)obj;
225 void
226 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
228 if( p_mdis->p_sd != NULL )
229 libvlc_media_discoverer_stop( p_mdis );
231 libvlc_media_list_release( p_mdis->p_mlist );
233 vlc_dictionary_clear( &p_mdis->catname_to_submedialist,
234 MediaListDictValueRelease, NULL );
236 libvlc_release( p_mdis->p_libvlc_instance );
238 free( p_mdis );
241 /**************************************************************************
242 * media_list (Public)
243 **************************************************************************/
244 libvlc_media_list_t *
245 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
247 libvlc_media_list_retain( p_mdis->p_mlist );
248 return p_mdis->p_mlist;
251 /**************************************************************************
252 * running (Public)
253 **************************************************************************/
255 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
257 return p_mdis->p_sd != NULL;
260 void
261 libvlc_media_discoverer_list_release( libvlc_media_discoverer_description_t **pp_services,
262 size_t i_count )
264 if( i_count > 0 )
266 for( size_t i = 0; i < i_count; ++i )
268 free( pp_services[i]->psz_name );
269 free( pp_services[i]->psz_longname );
271 free( *pp_services );
272 free( pp_services );
276 size_t
277 libvlc_media_discoverer_list_get( libvlc_instance_t *p_inst,
278 libvlc_media_discoverer_category_t i_cat,
279 libvlc_media_discoverer_description_t ***ppp_services )
281 assert( p_inst != NULL && ppp_services != NULL );
283 int i_core_cat;
284 switch( i_cat )
286 case libvlc_media_discoverer_devices:
287 i_core_cat = SD_CAT_DEVICES;
288 break;
289 case libvlc_media_discoverer_lan:
290 i_core_cat = SD_CAT_LAN;
291 break;
292 case libvlc_media_discoverer_podcasts:
293 i_core_cat = SD_CAT_INTERNET;
294 break;
295 case libvlc_media_discoverer_localdirs:
296 i_core_cat = SD_CAT_MYCOMPUTER;
297 break;
298 default:
299 vlc_assert_unreachable();
300 *ppp_services = NULL;
301 return 0;
304 /* Fetch all sd names, longnames and categories */
305 char **ppsz_names, **ppsz_longnames;
306 int *p_categories;
307 ppsz_names = vlc_sd_GetNames( p_inst->p_libvlc_int, &ppsz_longnames,
308 &p_categories );
310 if( ppsz_names == NULL )
312 *ppp_services = NULL;
313 return 0;
316 /* Count the number of sd matching our category (i_cat/i_core_cat) */
317 size_t i_nb_services = 0;
318 char **ppsz_name = ppsz_names;
319 int *p_category = p_categories;
320 for( ; *ppsz_name != NULL; ppsz_name++, p_category++ )
322 if( *p_category == i_core_cat )
323 i_nb_services++;
326 libvlc_media_discoverer_description_t **pp_services = NULL, *p_services = NULL;
327 if( i_nb_services > 0 )
329 /* Double alloc here, so that the caller iterates through pointers of
330 * struct instead of structs. This allows us to modify the struct
331 * without breaking the API. */
333 pp_services = malloc( i_nb_services
334 * sizeof(libvlc_media_discoverer_description_t *) );
335 p_services = malloc( i_nb_services
336 * sizeof(libvlc_media_discoverer_description_t) );
337 if( pp_services == NULL || p_services == NULL )
339 free( pp_services );
340 free( p_services );
341 pp_services = NULL;
342 p_services = NULL;
343 i_nb_services = 0;
344 /* Even if alloc fails, the next loop must be run in order to free
345 * names returned by vlc_sd_GetNames */
349 /* Fill output pp_services or free unused name, longname */
350 char **ppsz_longname = ppsz_longnames;
351 ppsz_name = ppsz_names;
352 p_category = p_categories;
353 unsigned int i_service_idx = 0;
354 libvlc_media_discoverer_description_t *p_service = p_services;
355 for( ; *ppsz_name != NULL; ppsz_name++, ppsz_longname++, p_category++ )
357 if( pp_services != NULL && *p_category == i_core_cat )
359 p_service->psz_name = *ppsz_name;
360 p_service->psz_longname = *ppsz_longname;
361 p_service->i_cat = i_cat;
362 pp_services[i_service_idx++] = p_service++;
364 else
366 free( *ppsz_name );
367 free( *ppsz_longname );
370 free( ppsz_names );
371 free( ppsz_longnames );
372 free( p_categories );
374 *ppp_services = pp_services;
375 return i_nb_services;