]> git.sesse.net Git - vlc/blob - src/control/media_discoverer.c
libvlc: Fix media discoverer with category.
[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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <assert.h>
29
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>
35
36 #include <vlc_services_discovery.h>
37
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()
41
42 struct libvlc_media_discoverer_t
43 {
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;
50 };
51
52 /*
53  * Private functions
54  */
55
56 /**************************************************************************
57  *       services_discovery_item_added (Private) (VLC event callback)
58  **************************************************************************/
59
60 static void services_discovery_item_added( const vlc_event_t * p_event,
61                                            void * user_data )
62 {
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;
68
69     p_md = libvlc_media_new_from_input_item(
70             p_mdis->p_libvlc_instance,
71             p_item, NULL );
72
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 )
76     {
77         p_mlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, psz_cat );
78
79         if( p_mlist == kVLCDictionaryNotFound )
80         {
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 );
84             p_mlist->b_read_only = true;
85
86             /* Insert the newly created mlist in our dictionary */
87             vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist );
88
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 );
93
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 );
97         }
98     }
99
100     libvlc_media_list_lock( p_mlist );
101     _libvlc_media_list_add_media( p_mlist, p_md, NULL );
102     libvlc_media_list_unlock( p_mlist );
103 }
104
105 /**************************************************************************
106  *       services_discovery_item_removed (Private) (VLC event callback)
107  **************************************************************************/
108
109 static void services_discovery_item_removed( const vlc_event_t * p_event,
110                                              void * user_data )
111 {
112     input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
113     libvlc_media_t * p_md;
114     libvlc_media_discoverer_t * p_mdis = user_data;
115
116     int i, count = libvlc_media_list_count( p_mdis->p_mlist, NULL );
117     libvlc_media_list_lock( p_mdis->p_mlist );
118     for( i = 0; i < count; i++ )
119     {
120         p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i, NULL );
121         if( p_md->p_input_item == p_item )
122         {
123             _libvlc_media_list_remove_index( p_mdis->p_mlist, i, NULL );
124             break;
125         }
126     }
127     libvlc_media_list_unlock( p_mdis->p_mlist );
128 }
129
130 /**************************************************************************
131  *       services_discovery_started (Private) (VLC event callback)
132  **************************************************************************/
133
134 static void services_discovery_started( const vlc_event_t * p_event,
135                                         void * user_data )
136 {
137     VLC_UNUSED(p_event);
138     libvlc_media_discoverer_t * p_mdis = user_data;
139     libvlc_event_t event;
140     p_mdis->running = true;
141     event.type = libvlc_MediaDiscovererStarted;
142     libvlc_event_send( p_mdis->p_event_manager, &event );
143 }
144
145 /**************************************************************************
146  *       services_discovery_ended (Private) (VLC event callback)
147  **************************************************************************/
148
149 static void services_discovery_ended( const vlc_event_t * p_event,
150                                       void * user_data )
151 {
152     VLC_UNUSED(p_event);
153     libvlc_media_discoverer_t * p_mdis = user_data;
154     libvlc_event_t event;
155     p_mdis->running = false;
156     event.type = libvlc_MediaDiscovererEnded;
157     libvlc_event_send( p_mdis->p_event_manager, &event );
158 }
159
160 /*
161  * Public libvlc functions
162  */
163
164 /**************************************************************************
165  *       new (Public)
166  *
167  * Init an object.
168  **************************************************************************/
169 libvlc_media_discoverer_t *
170 libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
171                                        const char * psz_name,
172                                        libvlc_exception_t * p_e )
173 {
174     libvlc_media_discoverer_t * p_mdis;
175
176     p_mdis = malloc(sizeof(libvlc_media_discoverer_t));
177     if( !p_mdis )
178     {
179         libvlc_exception_raise( p_e );
180         libvlc_printerr( "Not enough memory" );
181         return NULL;
182     }
183
184     p_mdis->p_libvlc_instance = p_inst;
185     p_mdis->p_mlist = libvlc_media_list_new( p_inst, NULL );
186     p_mdis->p_mlist->b_read_only = true;
187     p_mdis->running = false;
188
189     vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
190
191     p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis,
192             p_inst, NULL );
193
194     libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
195             libvlc_MediaDiscovererStarted, NULL );
196     libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
197             libvlc_MediaDiscovererEnded, NULL );
198
199     p_mdis->p_sd = vlc_sd_Create( (vlc_object_t*)p_inst->p_libvlc_int );
200
201     if( !p_mdis->p_sd )
202     {
203         libvlc_media_list_release( p_mdis->p_mlist );
204         libvlc_exception_raise( p_e );
205         libvlc_printerr( "%s: no such discovery module found", psz_name );
206         free( p_mdis );
207         return NULL;
208     }
209
210     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
211                       vlc_ServicesDiscoveryItemAdded,
212                       services_discovery_item_added,
213                       p_mdis );
214     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
215                       vlc_ServicesDiscoveryItemRemoved,
216                       services_discovery_item_removed,
217                       p_mdis );
218     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
219                       vlc_ServicesDiscoveryStarted,
220                       services_discovery_started,
221                       p_mdis );
222     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
223                       vlc_ServicesDiscoveryEnded,
224                       services_discovery_ended,
225                       p_mdis );
226
227     /* Here we go */
228     if( !vlc_sd_Start( p_mdis->p_sd, psz_name ) )
229     {
230         libvlc_media_list_release( p_mdis->p_mlist );
231         libvlc_exception_raise( p_e );
232         libvlc_printerr( "%s: internal module error", psz_name );
233         free( p_mdis );
234         return NULL;
235     }
236
237     return p_mdis;
238 }
239
240 /**************************************************************************
241  * release (Public)
242  **************************************************************************/
243 void
244 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
245 {
246     int i;
247
248     libvlc_media_list_release( p_mdis->p_mlist );
249     vlc_sd_StopAndDestroy( p_mdis->p_sd );
250
251     /* Free catname_to_submedialist and all the mlist */
252     char ** all_keys = vlc_dictionary_all_keys( &p_mdis->catname_to_submedialist );
253     for( i = 0; all_keys[i]; i++ )
254     {
255         libvlc_media_list_t * p_catmlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, all_keys[i] );
256         libvlc_media_list_release( p_catmlist );
257         free( all_keys[i] );
258     }
259     free( all_keys );
260
261     vlc_dictionary_clear( &p_mdis->catname_to_submedialist, NULL, NULL );
262     libvlc_event_manager_release( p_mdis->p_event_manager );
263
264     free( p_mdis );
265 }
266
267 /**************************************************************************
268  * localized_name (Public)
269  **************************************************************************/
270 char *
271 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
272 {
273     return services_discovery_GetLocalizedName( p_mdis->p_sd );
274 }
275
276 /**************************************************************************
277  * media_list (Public)
278  **************************************************************************/
279 libvlc_media_list_t *
280 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
281 {
282     libvlc_media_list_retain( p_mdis->p_mlist );
283     return p_mdis->p_mlist;
284 }
285
286 /**************************************************************************
287  * event_manager (Public)
288  **************************************************************************/
289 libvlc_event_manager_t *
290 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
291 {
292     return p_mdis->p_event_manager;
293 }
294
295
296 /**************************************************************************
297  * running (Public)
298  **************************************************************************/
299 int
300 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
301 {
302     return p_mdis->running;
303 }