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 = kVLCDictionaryNotFound;
55 p_mlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, psz_cat );
57 if( p_mlist == kVLCDictionaryNotFound )
59 libvlc_media_descriptor_t * p_catmd;
60 p_catmd = libvlc_media_descriptor_new_as_node( p_mdis->p_libvlc_instance, psz_cat, NULL );
61 p_mlist = libvlc_media_descriptor_subitems( p_catmd, NULL );
62 p_mlist->b_read_only = VLC_TRUE;
64 /* Insert the newly created mlist in our dictionary */
65 __vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist, 0 );
67 /* Insert the md into the root list */
68 libvlc_media_list_lock( p_mdis->p_mlist );
69 _libvlc_media_list_add_media_descriptor( p_mdis->p_mlist, p_catmd, NULL );
70 libvlc_media_list_unlock( p_mdis->p_mlist );
72 /* We don't release the mlist cause the dictionary
73 * doesn't retain the object. But we release the md. */
74 libvlc_media_descriptor_release( p_catmd );
78 libvlc_media_list_lock( p_mlist );
79 _libvlc_media_list_add_media_descriptor( p_mlist, p_md, NULL );
80 libvlc_media_list_unlock( p_mlist );
83 /**************************************************************************
84 * services_discovery_item_removed (Private) (VLC event callback)
85 **************************************************************************/
87 static void services_discovery_item_removed( const vlc_event_t * p_event,
90 input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
91 libvlc_media_descriptor_t * p_md;
92 libvlc_media_discoverer_t * p_mdis = user_data;
94 int i, count = libvlc_media_list_count( p_mdis->p_mlist, NULL );
95 libvlc_media_list_lock( p_mdis->p_mlist );
96 for( i = 0; i < count; i++ )
98 p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i, NULL );
99 if( p_md->p_input_item == p_item )
101 _libvlc_media_list_remove_index( p_mdis->p_mlist, i, NULL );
105 libvlc_media_list_unlock( p_mdis->p_mlist );
108 /**************************************************************************
109 * services_discovery_started (Private) (VLC event callback)
110 **************************************************************************/
112 static void services_discovery_started( const vlc_event_t * p_event,
116 libvlc_media_discoverer_t * p_mdis = user_data;
117 libvlc_event_t event;
118 p_mdis->running = VLC_TRUE;
119 event.type = libvlc_MediaDiscovererStarted;
120 libvlc_event_send( p_mdis->p_event_manager, &event );
123 /**************************************************************************
124 * services_discovery_ended (Private) (VLC event callback)
125 **************************************************************************/
127 static void services_discovery_ended( const vlc_event_t * p_event,
131 libvlc_media_discoverer_t * p_mdis = user_data;
132 libvlc_event_t event;
133 p_mdis->running = VLC_FALSE;
134 event.type = libvlc_MediaDiscovererEnded;
135 libvlc_event_send( p_mdis->p_event_manager, &event );
139 * Public libvlc functions
142 /**************************************************************************
146 **************************************************************************/
147 libvlc_media_discoverer_t *
148 libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
149 const char * psz_name,
150 libvlc_exception_t * p_e )
152 libvlc_media_discoverer_t * p_mdis;
154 p_mdis = malloc(sizeof(libvlc_media_discoverer_t));
157 libvlc_exception_raise( p_e, "Not enough memory" );
161 p_mdis->p_libvlc_instance = p_inst;
162 p_mdis->p_mlist = libvlc_media_list_new( p_inst, NULL );
163 p_mdis->p_mlist->b_read_only = VLC_TRUE;
164 p_mdis->running = VLC_FALSE;
166 vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
168 p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis,
171 libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
172 libvlc_MediaDiscovererStarted, NULL );
173 libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
174 libvlc_MediaDiscovererEnded, NULL );
176 p_mdis->p_sd = services_discovery_Create( (vlc_object_t*)p_inst->p_libvlc_int, psz_name );
181 libvlc_exception_raise( p_e, "Can't find the services_discovery module named '%s'", psz_name );
185 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
186 vlc_ServicesDiscoveryItemAdded,
187 services_discovery_item_added,
189 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
190 vlc_ServicesDiscoveryItemRemoved,
191 services_discovery_item_removed,
193 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
194 vlc_ServicesDiscoveryStarted,
195 services_discovery_started,
197 vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
198 vlc_ServicesDiscoveryEnded,
199 services_discovery_ended,
202 services_discovery_Start( p_mdis->p_sd );
209 /**************************************************************************
211 **************************************************************************/
213 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
217 libvlc_media_list_release( p_mdis->p_mlist );
218 services_discovery_Destroy( p_mdis->p_sd );
220 /* Free catname_to_submedialist and all the mlist */
221 char ** all_keys = vlc_dictionary_all_keys( &p_mdis->catname_to_submedialist );
222 for( i = 0; all_keys[i]; i++ )
224 libvlc_media_list_t * p_catmlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, all_keys[i] );
225 libvlc_media_list_release( p_catmlist );
230 vlc_dictionary_clear( &p_mdis->catname_to_submedialist );
235 /**************************************************************************
236 * localized_name (Public)
237 **************************************************************************/
239 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
241 return services_discovery_GetLocalizedName( p_mdis->p_sd );
244 /**************************************************************************
245 * media_list (Public)
246 **************************************************************************/
247 libvlc_media_list_t *
248 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
250 libvlc_media_list_retain( p_mdis->p_mlist );
251 return p_mdis->p_mlist;
254 /**************************************************************************
255 * event_manager (Public)
256 **************************************************************************/
257 libvlc_event_manager_t *
258 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
260 return p_mdis->p_event_manager;
264 /**************************************************************************
266 **************************************************************************/
268 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
270 return p_mdis->running;