Qt: remove a suspicious delete
[vlc.git] / lib / media_discoverer.c
blobb126c95fa346d3e6821dbd647f5bf581ccc1d063
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_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 bool running;
49 vlc_dictionary_t catname_to_submedialist;
53 * Private functions
56 /**************************************************************************
57 * services_discovery_item_added (Private) (VLC event callback)
58 **************************************************************************/
60 static void services_discovery_item_added( const vlc_event_t * p_event,
61 void * user_data )
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;
69 p_md = libvlc_media_new_from_input_item( p_mdis->p_libvlc_instance,
70 p_item );
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 )
76 p_mlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, psz_cat );
78 if( p_mlist == kVLCDictionaryNotFound )
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;
85 /* Insert the newly created mlist in our dictionary */
86 vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist );
88 /* Insert the md into the root list */
89 libvlc_media_list_lock( p_mdis->p_mlist );
90 _libvlc_media_list_add_media( p_mdis->p_mlist, p_catmd );
91 libvlc_media_list_unlock( p_mdis->p_mlist );
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 );
99 libvlc_media_list_lock( p_mlist );
100 _libvlc_media_list_add_media( p_mlist, p_md );
101 libvlc_media_list_unlock( p_mlist );
103 libvlc_media_release( p_md );
106 /**************************************************************************
107 * services_discovery_item_removed (Private) (VLC event callback)
108 **************************************************************************/
110 static void services_discovery_item_removed( const vlc_event_t * p_event,
111 void * user_data )
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;
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++ )
121 p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i );
122 if( p_md->p_input_item == p_item )
124 _libvlc_media_list_remove_index( p_mdis->p_mlist, i );
125 break;
128 libvlc_media_list_unlock( p_mdis->p_mlist );
131 /**************************************************************************
132 * services_discovery_removeall (Private) (VLC event callback)
133 **************************************************************************/
134 static void services_discovery_removeall( const vlc_event_t * p_event,
135 void * user_data )
137 VLC_UNUSED(p_event);
138 libvlc_media_discoverer_t * p_mdis = user_data;
140 libvlc_media_list_lock( p_mdis->p_mlist );
141 for( int i = 0; i < libvlc_media_list_count( p_mdis->p_mlist ); i++ )
143 _libvlc_media_list_remove_index( p_mdis->p_mlist, i );
145 libvlc_media_list_unlock( p_mdis->p_mlist );
148 /**************************************************************************
149 * services_discovery_started (Private) (VLC event callback)
150 **************************************************************************/
152 static void services_discovery_started( const vlc_event_t * p_event,
153 void * user_data )
155 VLC_UNUSED(p_event);
156 libvlc_media_discoverer_t * p_mdis = user_data;
157 libvlc_event_t event;
158 p_mdis->running = true;
159 event.type = libvlc_MediaDiscovererStarted;
160 libvlc_event_send( p_mdis->p_event_manager, &event );
163 /**************************************************************************
164 * services_discovery_ended (Private) (VLC event callback)
165 **************************************************************************/
167 static void services_discovery_ended( const vlc_event_t * p_event,
168 void * user_data )
170 VLC_UNUSED(p_event);
171 libvlc_media_discoverer_t * p_mdis = user_data;
172 libvlc_event_t event;
173 p_mdis->running = false;
174 event.type = libvlc_MediaDiscovererEnded;
175 libvlc_event_send( p_mdis->p_event_manager, &event );
179 * Public libvlc functions
182 /**************************************************************************
183 * new (Public)
185 * Init an object.
186 **************************************************************************/
187 libvlc_media_discoverer_t *
188 libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
189 const char * psz_name )
191 /* podcast SD is a hack and only works with custom playlist callbacks. */
192 if( !strncasecmp( psz_name, "podcast", 7 ) )
193 return NULL;
195 libvlc_media_discoverer_t *p_mdis = malloc(sizeof(*p_mdis));
196 if( unlikely(!p_mdis) )
198 libvlc_printerr( "Not enough memory" );
199 return NULL;
202 p_mdis->p_libvlc_instance = p_inst;
203 p_mdis->p_mlist = libvlc_media_list_new( p_inst );
204 p_mdis->p_mlist->b_read_only = true;
205 p_mdis->running = false;
207 vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
209 p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis, p_inst );
210 if( unlikely(p_mdis->p_event_manager == NULL) )
212 free( p_mdis );
213 return NULL;
216 libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
217 libvlc_MediaDiscovererStarted );
218 libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
219 libvlc_MediaDiscovererEnded );
221 p_mdis->p_sd = vlc_sd_Create( (vlc_object_t*)p_inst->p_libvlc_int,
222 psz_name );
223 if( unlikely(p_mdis->p_sd == NULL) )
225 libvlc_printerr( "%s: no such discovery module found", psz_name );
226 libvlc_media_list_release( p_mdis->p_mlist );
227 libvlc_event_manager_release( p_mdis->p_event_manager );
228 free( p_mdis );
229 return NULL;
232 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
233 vlc_ServicesDiscoveryItemAdded,
234 services_discovery_item_added,
235 p_mdis );
236 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
237 vlc_ServicesDiscoveryItemRemoved,
238 services_discovery_item_removed,
239 p_mdis );
240 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
241 vlc_ServicesDiscoveryStarted,
242 services_discovery_started,
243 p_mdis );
244 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
245 vlc_ServicesDiscoveryEnded,
246 services_discovery_ended,
247 p_mdis );
248 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
249 vlc_ServicesDiscoveryItemRemoveAll,
250 services_discovery_removeall,
251 p_mdis );
253 /* Here we go */
254 if( !vlc_sd_Start( p_mdis->p_sd ) )
256 libvlc_printerr( "%s: internal module error",
257 p_mdis->p_sd->psz_name );
258 libvlc_media_list_release( p_mdis->p_mlist );
259 libvlc_event_manager_release( p_mdis->p_event_manager );
260 free( p_mdis );
261 return NULL;
264 return p_mdis;
267 /**************************************************************************
268 * release (Public)
269 **************************************************************************/
270 void
271 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
273 int i;
275 vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
276 vlc_ServicesDiscoveryItemAdded,
277 services_discovery_item_added,
278 p_mdis );
279 vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
280 vlc_ServicesDiscoveryItemRemoved,
281 services_discovery_item_removed,
282 p_mdis );
283 vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
284 vlc_ServicesDiscoveryStarted,
285 services_discovery_started,
286 p_mdis );
287 vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
288 vlc_ServicesDiscoveryEnded,
289 services_discovery_ended,
290 p_mdis );
291 vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
292 vlc_ServicesDiscoveryItemRemoveAll,
293 services_discovery_removeall,
294 p_mdis );
296 libvlc_media_list_release( p_mdis->p_mlist );
298 vlc_sd_StopAndDestroy( p_mdis->p_sd );
300 /* Free catname_to_submedialist and all the mlist */
301 char ** all_keys = vlc_dictionary_all_keys( &p_mdis->catname_to_submedialist );
302 for( i = 0; all_keys[i]; i++ )
304 libvlc_media_list_t * p_catmlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, all_keys[i] );
305 libvlc_media_list_release( p_catmlist );
306 free( all_keys[i] );
308 free( all_keys );
310 vlc_dictionary_clear( &p_mdis->catname_to_submedialist, NULL, NULL );
311 libvlc_event_manager_release( p_mdis->p_event_manager );
313 free( p_mdis );
316 /**************************************************************************
317 * localized_name (Public)
318 **************************************************************************/
319 char *
320 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
322 return services_discovery_GetLocalizedName( p_mdis->p_sd );
325 /**************************************************************************
326 * media_list (Public)
327 **************************************************************************/
328 libvlc_media_list_t *
329 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
331 libvlc_media_list_retain( p_mdis->p_mlist );
332 return p_mdis->p_mlist;
335 /**************************************************************************
336 * event_manager (Public)
337 **************************************************************************/
338 libvlc_event_manager_t *
339 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
341 return p_mdis->p_event_manager;
345 /**************************************************************************
346 * running (Public)
347 **************************************************************************/
349 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
351 return p_mdis->running;