1 /*****************************************************************************
2 * media_discoverer.c: libvlc new API media discoverer functions
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
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 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
27 #include "vlc_services_discovery.h"
33 /**************************************************************************
34 * services_discovery_item_added (Private) (VLC event callback)
35 **************************************************************************/
37 static void services_discovery_item_added( const vlc_event_t * p_event,
40 input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
41 const char * psz_cat = p_event->u.services_discovery_item_added.psz_category;
42 libvlc_media_descriptor_t * p_md;
43 libvlc_media_discoverer_t * p_mdis = user_data;
44 libvlc_media_list_t * p_mlist = p_mdis->p_mlist;
46 p_md = libvlc_media_descriptor_new_from_input_item(
47 p_mdis->p_libvlc_instance,
50 /* If we have a category, that mean we have to group the items having
51 * that category in a media_list. */
54 p_mlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, psz_cat );
55 if( p_mlist == kVLCDictionaryNotFound )
57 libvlc_media_descriptor_t * p_catmd;
58 p_catmd = libvlc_media_descriptor_new_as_node( p_mdis->p_libvlc_instance, psz_cat, NULL );
59 p_mlist = libvlc_media_descriptor_subitems( p_catmd, NULL );
60 p_mlist->b_read_only = VLC_TRUE;
62 /* Insert the newly created mlist in our dictionary */
63 vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist );
65 /* Insert the md into the root list */
66 _libvlc_media_list_add_media_descriptor( p_mdis->p_mlist, p_catmd, NULL );
68 /* We don't release the mlist cause the dictionary
69 * doesn't retain the object. But we release the md. */
70 libvlc_media_descriptor_release( p_catmd );
74 libvlc_media_list_lock( p_mdis->p_mlist );
75 _libvlc_media_list_add_media_descriptor( p_mlist, p_md, NULL );
76 libvlc_media_list_unlock( p_mdis->p_mlist );
79 /**************************************************************************
80 * services_discovery_item_removed (Private) (VLC event callback)
81 **************************************************************************/
83 static void services_discovery_item_removed( const vlc_event_t * p_event,
86 input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
87 libvlc_media_descriptor_t * p_md;
88 libvlc_media_discoverer_t * p_mdis = user_data;
90 int i, count = libvlc_media_list_count( p_mdis->p_mlist, NULL );
91 libvlc_media_list_lock( p_mdis->p_mlist );
92 for( i = 0; i < count; i++ )
94 p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i, NULL );
95 if( p_md->p_input_item == p_item )
97 _libvlc_media_list_remove_index( p_mdis->p_mlist, i, NULL );
101 libvlc_media_list_unlock( p_mdis->p_mlist );
104 /**************************************************************************
105 * services_discovery_started (Private) (VLC event callback)
106 **************************************************************************/
108 static void services_discovery_started( const vlc_event_t * p_event,
112 libvlc_media_discoverer_t * p_mdis = user_data;
113 libvlc_event_t event;
114 p_mdis->running = VLC_TRUE;
115 event.type = libvlc_MediaDiscovererStarted;
116 libvlc_event_send( p_mdis->p_event_manager, &event );
119 /**************************************************************************
120 * services_discovery_ended (Private) (VLC event callback)
121 **************************************************************************/
123 static void services_discovery_ended( const vlc_event_t * p_event,
127 libvlc_media_discoverer_t * p_mdis = user_data;
128 libvlc_event_t event;
129 p_mdis->running = VLC_FALSE;
130 event.type = libvlc_MediaDiscovererEnded;
131 libvlc_event_send( p_mdis->p_event_manager, &event );
135 * Public libvlc functions
138 /**************************************************************************
142 **************************************************************************/
143 libvlc_media_discoverer_t *
144 libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
145 const char * psz_name,
146 libvlc_exception_t * p_e )
148 libvlc_media_discoverer_t * p_mdis;
150 p_mdis = malloc(sizeof(libvlc_media_discoverer_t));
153 libvlc_exception_raise( p_e, "Not enough memory" );
157 p_mdis->p_libvlc_instance = p_inst;
158 p_mdis->p_mlist = libvlc_media_list_new( p_inst, NULL );
159 p_mdis->p_mlist->b_read_only = VLC_TRUE;
160 p_mdis->running = VLC_FALSE;
162 vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
164 p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis,
167 libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
168 libvlc_MediaDiscovererStarted, NULL );
169 libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
170 libvlc_MediaDiscovererEnded, NULL );
172 p_mdis->p_sd = services_discovery_Create( (vlc_object_t*)p_inst->p_libvlc_int, psz_name );
177 libvlc_exception_raise( p_e, "Can't find the services_discovery module named '%s'", psz_name );
181 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
182 vlc_ServicesDiscoveryItemAdded,
183 services_discovery_item_added,
185 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
186 vlc_ServicesDiscoveryItemRemoved,
187 services_discovery_item_removed,
189 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
190 vlc_ServicesDiscoveryStarted,
191 services_discovery_started,
193 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
194 vlc_ServicesDiscoveryEnded,
195 services_discovery_ended,
198 services_discovery_Start( p_mdis->p_sd );
205 /**************************************************************************
207 **************************************************************************/
209 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
213 libvlc_media_list_release( p_mdis->p_mlist );
214 services_discovery_Destroy( p_mdis->p_sd );
216 /* Free catname_to_submedialist and all the mlist */
217 char ** all_keys = vlc_dictionary_all_keys( &p_mdis->catname_to_submedialist );
218 for( i = 0; all_keys[i]; i++ )
220 libvlc_media_list_t * p_catmlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, all_keys[i] );
221 libvlc_media_list_release( p_catmlist );
226 vlc_dictionary_clear( &p_mdis->catname_to_submedialist );
231 /**************************************************************************
232 * localized_name (Public)
233 **************************************************************************/
235 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
237 return services_discovery_GetLocalizedName( p_mdis->p_sd );
240 /**************************************************************************
241 * media_list (Public)
242 **************************************************************************/
243 libvlc_media_list_t *
244 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
246 libvlc_media_list_retain( p_mdis->p_mlist );
247 return p_mdis->p_mlist;
250 /**************************************************************************
251 * event_manager (Public)
252 **************************************************************************/
253 libvlc_event_manager_t *
254 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
256 return p_mdis->p_event_manager;
260 /**************************************************************************
262 **************************************************************************/
264 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
266 return p_mdis->running;