direct3d11: pick the best swapchain colorspace for the source video
[vlc.git] / lib / media_discoverer.c
blobaf12a5e01d649dd08547d0b29982d93369e1ca44
1 /*****************************************************************************
2 * media_discoverer.c: libvlc new API media discoverer functions
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
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>
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_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 vlc_dictionary_t catname_to_submedialist;
49 char name[];
53 * Private functions
56 /**************************************************************************
57 * services_discovery_item_added (Private) (VLC event callback)
58 **************************************************************************/
60 static void services_discovery_item_added( services_discovery_t *sd,
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 we have a category, that mean we have to group the items having
72 * that category in a media_list. */
73 if( psz_cat )
75 p_mlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, psz_cat );
77 if( p_mlist == kVLCDictionaryNotFound )
79 libvlc_media_t * p_catmd;
80 p_catmd = libvlc_media_new_as_node( p_mdis->p_libvlc_instance, psz_cat );
81 p_mlist = libvlc_media_subitems( p_catmd );
82 p_mlist->b_read_only = true;
84 /* Insert the newly created mlist in our dictionary */
85 vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist );
87 /* Insert the md into the root list */
88 libvlc_media_list_lock( p_mdis->p_mlist );
89 libvlc_media_list_internal_add_media( p_mdis->p_mlist, p_catmd );
90 libvlc_media_list_unlock( p_mdis->p_mlist );
92 /* We don't release the mlist cause the dictionary
93 * doesn't retain the object. But we release the md. */
94 libvlc_media_release( p_catmd );
98 libvlc_media_list_lock( p_mlist );
99 libvlc_media_list_internal_add_media( p_mlist, p_md );
100 libvlc_media_list_unlock( p_mlist );
102 libvlc_media_release( p_md );
105 /**************************************************************************
106 * services_discovery_item_removed (Private) (VLC event callback)
107 **************************************************************************/
109 static void services_discovery_item_removed( services_discovery_t *sd,
110 input_item_t *p_item )
112 libvlc_media_t * p_md;
113 libvlc_media_discoverer_t *p_mdis = sd->owner.sys;
115 int i, count = libvlc_media_list_count( p_mdis->p_mlist );
116 libvlc_media_list_lock( p_mdis->p_mlist );
117 for( i = 0; i < count; i++ )
119 p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i );
120 assert(p_md != NULL);
121 if( p_md->p_input_item == p_item )
123 libvlc_media_list_internal_remove_index( p_mdis->p_mlist, i );
124 libvlc_media_release( p_md );
125 break;
127 libvlc_media_release( p_md );
129 libvlc_media_list_unlock( p_mdis->p_mlist );
133 * Public libvlc functions
136 /**************************************************************************
137 * new (Public)
138 **************************************************************************/
139 libvlc_media_discoverer_t *
140 libvlc_media_discoverer_new( libvlc_instance_t * p_inst, const char * psz_name )
142 /* podcast SD is a hack and only works with custom playlist callbacks. */
143 if( !strncasecmp( psz_name, "podcast", 7 ) )
144 return NULL;
146 libvlc_media_discoverer_t *p_mdis;
148 p_mdis = malloc(sizeof(*p_mdis) + strlen(psz_name) + 1);
149 if( unlikely(p_mdis == NULL) )
151 libvlc_printerr( "Not enough memory" );
152 return NULL;
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->p_sd = NULL;
160 vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
162 p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis );
163 if( unlikely(p_mdis->p_event_manager == NULL) )
165 free( p_mdis );
166 return NULL;
169 libvlc_retain( p_inst );
170 strcpy( p_mdis->name, psz_name );
171 return p_mdis;
174 /**************************************************************************
175 * start (Public)
176 **************************************************************************/
177 LIBVLC_API int
178 libvlc_media_discoverer_start( libvlc_media_discoverer_t * p_mdis )
180 struct services_discovery_owner_t owner = {
181 p_mdis,
182 services_discovery_item_added,
183 services_discovery_item_removed,
186 /* Here we go */
187 p_mdis->p_sd = vlc_sd_Create( (vlc_object_t *)p_mdis->p_libvlc_instance->p_libvlc_int,
188 p_mdis->name, &owner );
189 if( p_mdis->p_sd == NULL )
191 libvlc_printerr( "%s: no such discovery module found", p_mdis->name );
192 return -1;
195 libvlc_event_t event;
196 event.type = libvlc_MediaDiscovererStarted;
197 libvlc_event_send( p_mdis->p_event_manager, &event );
198 return 0;
201 /**************************************************************************
202 * stop (Public)
203 **************************************************************************/
204 LIBVLC_API void
205 libvlc_media_discoverer_stop( libvlc_media_discoverer_t * p_mdis )
207 libvlc_media_list_t * p_mlist = p_mdis->p_mlist;
208 libvlc_media_list_lock( p_mlist );
209 libvlc_media_list_internal_end_reached( p_mlist );
210 libvlc_media_list_unlock( p_mlist );
212 libvlc_event_t event;
213 event.type = libvlc_MediaDiscovererEnded;
214 libvlc_event_send( p_mdis->p_event_manager, &event );
216 vlc_sd_Destroy( p_mdis->p_sd );
217 p_mdis->p_sd = NULL;
220 /**************************************************************************
221 * new_from_name (Public)
223 * \deprecated Use libvlc_media_discoverer_new and libvlc_media_discoverer_start
224 **************************************************************************/
225 libvlc_media_discoverer_t *
226 libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
227 const char * psz_name )
229 libvlc_media_discoverer_t *p_mdis = libvlc_media_discoverer_new( p_inst, psz_name );
231 if( !p_mdis )
232 return NULL;
234 if( libvlc_media_discoverer_start( p_mdis ) != 0)
236 libvlc_media_discoverer_release( p_mdis );
237 return NULL;
240 return p_mdis;
243 /**************************************************************************
244 * release (Public)
245 **************************************************************************/
246 void
247 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
249 if( p_mdis->p_sd != NULL )
250 libvlc_media_discoverer_stop( p_mdis );
252 libvlc_media_list_release( p_mdis->p_mlist );
254 /* Free catname_to_submedialist and all the mlist */
255 char ** all_keys = vlc_dictionary_all_keys( &p_mdis->catname_to_submedialist );
256 for( int i = 0; all_keys[i]; i++ )
258 libvlc_media_list_t * p_catmlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, all_keys[i] );
259 libvlc_media_list_release( p_catmlist );
260 free( all_keys[i] );
262 free( all_keys );
264 vlc_dictionary_clear( &p_mdis->catname_to_submedialist, NULL, NULL );
265 libvlc_event_manager_release( p_mdis->p_event_manager );
266 libvlc_release( p_mdis->p_libvlc_instance );
268 free( p_mdis );
271 /**************************************************************************
272 * localized_name (Public)
273 **************************************************************************/
274 char *
275 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
277 if( p_mdis->p_sd == NULL || p_mdis->p_sd->description == NULL )
278 return NULL;
279 return strdup( p_mdis->p_sd->description );
282 /**************************************************************************
283 * media_list (Public)
284 **************************************************************************/
285 libvlc_media_list_t *
286 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
288 libvlc_media_list_retain( p_mdis->p_mlist );
289 return p_mdis->p_mlist;
292 /**************************************************************************
293 * event_manager (Public)
294 **************************************************************************/
295 libvlc_event_manager_t *
296 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
298 return p_mdis->p_event_manager;
302 /**************************************************************************
303 * running (Public)
304 **************************************************************************/
306 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
308 return p_mdis->p_sd != NULL;
311 void
312 libvlc_media_discoverer_list_release( libvlc_media_discoverer_description_t **pp_services,
313 size_t i_count )
315 if( i_count > 0 )
317 for( size_t i = 0; i < i_count; ++i )
319 free( pp_services[i]->psz_name );
320 free( pp_services[i]->psz_longname );
322 free( *pp_services );
323 free( pp_services );
327 size_t
328 libvlc_media_discoverer_list_get( libvlc_instance_t *p_inst,
329 libvlc_media_discoverer_category_t i_cat,
330 libvlc_media_discoverer_description_t ***ppp_services )
332 assert( p_inst != NULL && ppp_services != NULL );
334 int i_core_cat;
335 switch( i_cat )
337 case libvlc_media_discoverer_devices:
338 i_core_cat = SD_CAT_DEVICES;
339 break;
340 case libvlc_media_discoverer_lan:
341 i_core_cat = SD_CAT_LAN;
342 break;
343 case libvlc_media_discoverer_podcasts:
344 i_core_cat = SD_CAT_INTERNET;
345 break;
346 case libvlc_media_discoverer_localdirs:
347 i_core_cat = SD_CAT_MYCOMPUTER;
348 break;
349 default:
350 vlc_assert_unreachable();
351 *ppp_services = NULL;
352 return 0;
355 /* Fetch all sd names, longnames and categories */
356 char **ppsz_names, **ppsz_longnames;
357 int *p_categories;
358 ppsz_names = vlc_sd_GetNames( p_inst->p_libvlc_int, &ppsz_longnames,
359 &p_categories );
361 if( ppsz_names == NULL )
363 *ppp_services = NULL;
364 return 0;
367 /* Count the number of sd matching our category (i_cat/i_core_cat) */
368 size_t i_nb_services = 0;
369 char **ppsz_name = ppsz_names;
370 int *p_category = p_categories;
371 for( ; *ppsz_name != NULL; ppsz_name++, p_category++ )
373 if( *p_category == i_core_cat )
374 i_nb_services++;
377 libvlc_media_discoverer_description_t **pp_services = NULL, *p_services = NULL;
378 if( i_nb_services > 0 )
380 /* Double alloc here, so that the caller iterates through pointers of
381 * struct instead of structs. This allows us to modify the struct
382 * without breaking the API. */
384 pp_services = malloc( i_nb_services
385 * sizeof(libvlc_media_discoverer_description_t *) );
386 p_services = malloc( i_nb_services
387 * sizeof(libvlc_media_discoverer_description_t) );
388 if( pp_services == NULL || p_services == NULL )
390 free( pp_services );
391 free( p_services );
392 pp_services = NULL;
393 p_services = NULL;
394 i_nb_services = 0;
395 /* Even if alloc fails, the next loop must be run in order to free
396 * names returned by vlc_sd_GetNames */
400 /* Fill output pp_services or free unused name, longname */
401 char **ppsz_longname = ppsz_longnames;
402 ppsz_name = ppsz_names;
403 p_category = p_categories;
404 unsigned int i_service_idx = 0;
405 libvlc_media_discoverer_description_t *p_service = p_services;
406 for( ; *ppsz_name != NULL; ppsz_name++, ppsz_longname++, p_category++ )
408 if( pp_services != NULL && *p_category == i_core_cat )
410 p_service->psz_name = *ppsz_name;
411 p_service->psz_longname = *ppsz_longname;
412 p_service->i_cat = i_cat;
413 pp_services[i_service_idx++] = p_service++;
415 else
417 free( *ppsz_name );
418 free( *ppsz_longname );
421 free( ppsz_names );
422 free( ppsz_longnames );
423 free( p_categories );
425 *ppp_services = pp_services;
426 return i_nb_services;