]> git.sesse.net Git - vlc/blob - src/control/media_discoverer.c
update module LIST file.
[vlc] / src / control / media_discoverer.c
1 /*****************************************************************************
2  * media_discoverer.c: libvlc new API media discoverer functions
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
26 #include <assert.h>
27 #include "vlc_services_discovery.h"
28
29 /*
30  * Private functions
31  */
32
33 /**************************************************************************
34  *       services_discovery_item_added (Private) (VLC event callback)
35  **************************************************************************/
36
37 static void services_discovery_item_added( const vlc_event_t * p_event,
38                                            void * user_data )
39 {
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_t * p_md;
43     libvlc_media_discoverer_t * p_mdis = user_data;
44     libvlc_media_list_t * p_mlist = p_mdis->p_mlist;
45
46     p_md = libvlc_media_new_from_input_item(
47             p_mdis->p_libvlc_instance,
48             p_item, NULL );
49
50     /* If we have a category, that mean we have to group the items having
51      * that category in a media_list. */
52     if( psz_cat )
53     {
54         p_mlist = kVLCDictionaryNotFound;
55         p_mlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, psz_cat );
56
57         if( p_mlist == kVLCDictionaryNotFound )
58         {
59             libvlc_media_t * p_catmd;
60             p_catmd = libvlc_media_new_as_node( p_mdis->p_libvlc_instance, psz_cat, NULL );
61             p_mlist = libvlc_media_subitems( p_catmd, NULL );
62             p_mlist->b_read_only = VLC_TRUE;
63
64             /* Insert the newly created mlist in our dictionary */
65             __vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist, 0 );
66             
67             /* Insert the md into the root list */
68             libvlc_media_list_lock( p_mdis->p_mlist );
69             _libvlc_media_list_add_media( p_mdis->p_mlist, p_catmd, NULL );
70             libvlc_media_list_unlock( p_mdis->p_mlist );
71
72             /* We don't release the mlist cause the dictionary
73              * doesn't retain the object. But we release the md. */
74             libvlc_media_release( p_catmd );
75         }
76     }
77
78     libvlc_media_list_lock( p_mlist );
79     _libvlc_media_list_add_media( p_mlist, p_md, NULL );
80     libvlc_media_list_unlock( p_mlist );
81 }
82
83 /**************************************************************************
84  *       services_discovery_item_removed (Private) (VLC event callback)
85  **************************************************************************/
86
87 static void services_discovery_item_removed( const vlc_event_t * p_event,
88                                              void * user_data )
89 {
90     input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
91     libvlc_media_t * p_md;
92     libvlc_media_discoverer_t * p_mdis = user_data;
93
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++ )
97     {
98         p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i, NULL );
99         if( p_md->p_input_item == p_item )
100         {
101             _libvlc_media_list_remove_index( p_mdis->p_mlist, i, NULL );
102             break;
103         }
104     }
105     libvlc_media_list_unlock( p_mdis->p_mlist );
106 }
107
108 /**************************************************************************
109  *       services_discovery_started (Private) (VLC event callback)
110  **************************************************************************/
111
112 static void services_discovery_started( const vlc_event_t * p_event,
113                                         void * user_data )
114 {
115     (void)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 );
121 }
122
123 /**************************************************************************
124  *       services_discovery_ended (Private) (VLC event callback)
125  **************************************************************************/
126
127 static void services_discovery_ended( const vlc_event_t * p_event,
128                                       void * user_data )
129 {
130     (void)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 );
136 }
137
138 /*
139  * Public libvlc functions
140  */
141
142 /**************************************************************************
143  *       new (Public)
144  *
145  * Init an object.
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 )
151 {
152     libvlc_media_discoverer_t * p_mdis;
153  
154     p_mdis = malloc(sizeof(libvlc_media_discoverer_t));
155     if( !p_mdis )
156     {
157         libvlc_exception_raise( p_e, "Not enough memory" );
158         return NULL;
159     }
160
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;
165
166     vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
167
168     p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis,
169             p_inst, NULL );
170
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 );
175
176     p_mdis->p_sd = services_discovery_Create( (vlc_object_t*)p_inst->p_libvlc_int, psz_name );
177
178     if( !p_mdis->p_sd )
179     {
180         free( p_mdis );
181         libvlc_exception_raise( p_e, "Can't find the services_discovery module named '%s'", psz_name );
182         return NULL;
183     }
184
185     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
186                       vlc_ServicesDiscoveryItemAdded,
187                       services_discovery_item_added,
188                       p_mdis );
189     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
190                       vlc_ServicesDiscoveryItemRemoved,
191                       services_discovery_item_removed,
192                       p_mdis );
193     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
194                       vlc_ServicesDiscoveryStarted,
195                       services_discovery_started,
196                       p_mdis );
197     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
198                       vlc_ServicesDiscoveryEnded,
199                       services_discovery_ended,
200                       p_mdis );
201
202     services_discovery_Start( p_mdis->p_sd );
203
204     /* Here we go */
205
206     return p_mdis;
207 }
208
209 /**************************************************************************
210  * release (Public)
211  **************************************************************************/
212 void
213 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
214 {
215     int i;
216
217     libvlc_media_list_release( p_mdis->p_mlist );
218     services_discovery_Destroy( p_mdis->p_sd );
219
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++ )
223     {
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 );
226         free( all_keys[i] );
227     }
228     free( all_keys );
229
230     vlc_dictionary_clear( &p_mdis->catname_to_submedialist );
231
232     free( p_mdis );
233 }
234
235 /**************************************************************************
236  * localized_name (Public)
237  **************************************************************************/
238 char *
239 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
240 {
241     return services_discovery_GetLocalizedName( p_mdis->p_sd );
242 }
243
244 /**************************************************************************
245  * media_list (Public)
246  **************************************************************************/
247 libvlc_media_list_t *
248 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
249 {
250     libvlc_media_list_retain( p_mdis->p_mlist );
251     return p_mdis->p_mlist;
252 }
253
254 /**************************************************************************
255  * event_manager (Public)
256  **************************************************************************/
257 libvlc_event_manager_t *
258 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
259 {
260     return p_mdis->p_event_manager;
261 }
262
263
264 /**************************************************************************
265  * running (Public)
266  **************************************************************************/
267 int
268 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
269 {
270     return p_mdis->running;
271 }
272