Fixed typo into invmem error message
[vlc.git] / src / control / media_discoverer.c
blobe4482de9e595ab59b22df7c20717366280eb2d72
1 /*****************************************************************************
2 * media_discoverer.c: libvlc new API media discoverer functions
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
5 * $Id$
7 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, 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(
70 p_mdis->p_libvlc_instance,
71 p_item, NULL );
73 /* If we have a category, that mean we have to group the items having
74 * that category in a media_list. */
75 if( psz_cat )
77 p_mlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, psz_cat );
79 if( p_mlist == kVLCDictionaryNotFound )
81 libvlc_media_t * p_catmd;
82 p_catmd = libvlc_media_new_as_node( p_mdis->p_libvlc_instance, psz_cat, NULL );
83 p_mlist = libvlc_media_subitems( p_catmd, NULL );
84 p_mlist->b_read_only = true;
86 /* Insert the newly created mlist in our dictionary */
87 vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist );
89 /* Insert the md into the root list */
90 libvlc_media_list_lock( p_mdis->p_mlist );
91 _libvlc_media_list_add_media( p_mdis->p_mlist, p_catmd, NULL );
92 libvlc_media_list_unlock( p_mdis->p_mlist );
94 /* We don't release the mlist cause the dictionary
95 * doesn't retain the object. But we release the md. */
96 libvlc_media_release( p_catmd );
99 else
101 libvlc_media_list_lock( p_mlist );
102 _libvlc_media_list_add_media( p_mlist, p_md, NULL );
103 libvlc_media_list_unlock( p_mlist );
107 /**************************************************************************
108 * services_discovery_item_removed (Private) (VLC event callback)
109 **************************************************************************/
111 static void services_discovery_item_removed( const vlc_event_t * p_event,
112 void * user_data )
114 input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
115 libvlc_media_t * p_md;
116 libvlc_media_discoverer_t * p_mdis = user_data;
118 int i, count = libvlc_media_list_count( p_mdis->p_mlist, NULL );
119 libvlc_media_list_lock( p_mdis->p_mlist );
120 for( i = 0; i < count; i++ )
122 p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i, NULL );
123 if( p_md->p_input_item == p_item )
125 _libvlc_media_list_remove_index( p_mdis->p_mlist, i, NULL );
126 break;
129 libvlc_media_list_unlock( p_mdis->p_mlist );
132 /**************************************************************************
133 * services_discovery_started (Private) (VLC event callback)
134 **************************************************************************/
136 static void services_discovery_started( const vlc_event_t * p_event,
137 void * user_data )
139 VLC_UNUSED(p_event);
140 libvlc_media_discoverer_t * p_mdis = user_data;
141 libvlc_event_t event;
142 p_mdis->running = true;
143 event.type = libvlc_MediaDiscovererStarted;
144 libvlc_event_send( p_mdis->p_event_manager, &event );
147 /**************************************************************************
148 * services_discovery_ended (Private) (VLC event callback)
149 **************************************************************************/
151 static void services_discovery_ended( const vlc_event_t * p_event,
152 void * user_data )
154 VLC_UNUSED(p_event);
155 libvlc_media_discoverer_t * p_mdis = user_data;
156 libvlc_event_t event;
157 p_mdis->running = false;
158 event.type = libvlc_MediaDiscovererEnded;
159 libvlc_event_send( p_mdis->p_event_manager, &event );
163 * Public libvlc functions
166 /**************************************************************************
167 * new (Public)
169 * Init an object.
170 **************************************************************************/
171 libvlc_media_discoverer_t *
172 libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
173 const char * psz_name,
174 libvlc_exception_t * p_e )
176 libvlc_media_discoverer_t * p_mdis;
178 p_mdis = malloc(sizeof(libvlc_media_discoverer_t));
179 if( !p_mdis )
181 libvlc_exception_raise( p_e );
182 libvlc_printerr( "Not enough memory" );
183 return NULL;
186 p_mdis->p_libvlc_instance = p_inst;
187 p_mdis->p_mlist = libvlc_media_list_new( p_inst, NULL );
188 p_mdis->p_mlist->b_read_only = true;
189 p_mdis->running = false;
191 vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
193 p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis,
194 p_inst, NULL );
196 libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
197 libvlc_MediaDiscovererStarted, NULL );
198 libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
199 libvlc_MediaDiscovererEnded, NULL );
201 p_mdis->p_sd = vlc_sd_Create( (vlc_object_t*)p_inst->p_libvlc_int );
203 if( !p_mdis->p_sd )
205 libvlc_media_list_release( p_mdis->p_mlist );
206 libvlc_exception_raise( p_e );
207 libvlc_printerr( "%s: no such discovery module found", psz_name );
208 free( p_mdis );
209 return NULL;
212 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
213 vlc_ServicesDiscoveryItemAdded,
214 services_discovery_item_added,
215 p_mdis );
216 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
217 vlc_ServicesDiscoveryItemRemoved,
218 services_discovery_item_removed,
219 p_mdis );
220 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
221 vlc_ServicesDiscoveryStarted,
222 services_discovery_started,
223 p_mdis );
224 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
225 vlc_ServicesDiscoveryEnded,
226 services_discovery_ended,
227 p_mdis );
229 /* Here we go */
230 if( !vlc_sd_Start( p_mdis->p_sd, psz_name ) )
232 libvlc_media_list_release( p_mdis->p_mlist );
233 libvlc_exception_raise( p_e );
234 libvlc_printerr( "%s: internal module error", psz_name );
235 free( p_mdis );
236 return NULL;
239 return p_mdis;
242 /**************************************************************************
243 * release (Public)
244 **************************************************************************/
245 void
246 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
248 int i;
250 libvlc_media_list_release( p_mdis->p_mlist );
251 vlc_sd_StopAndDestroy( p_mdis->p_sd );
253 /* Free catname_to_submedialist and all the mlist */
254 char ** all_keys = vlc_dictionary_all_keys( &p_mdis->catname_to_submedialist );
255 for( i = 0; all_keys[i]; i++ )
257 libvlc_media_list_t * p_catmlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, all_keys[i] );
258 libvlc_media_list_release( p_catmlist );
259 free( all_keys[i] );
261 free( all_keys );
263 vlc_dictionary_clear( &p_mdis->catname_to_submedialist, NULL, NULL );
264 libvlc_event_manager_release( p_mdis->p_event_manager );
266 free( p_mdis );
269 /**************************************************************************
270 * localized_name (Public)
271 **************************************************************************/
272 char *
273 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
275 return services_discovery_GetLocalizedName( p_mdis->p_sd );
278 /**************************************************************************
279 * media_list (Public)
280 **************************************************************************/
281 libvlc_media_list_t *
282 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
284 libvlc_media_list_retain( p_mdis->p_mlist );
285 return p_mdis->p_mlist;
288 /**************************************************************************
289 * event_manager (Public)
290 **************************************************************************/
291 libvlc_event_manager_t *
292 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
294 return p_mdis->p_event_manager;
298 /**************************************************************************
299 * running (Public)
300 **************************************************************************/
302 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
304 return p_mdis->running;