LGPL
[vlc.git] / lib / media_discoverer.c
1 /*****************************************************************************
2  * media_discoverer.c: libvlc new API media discoverer functions
3  *****************************************************************************
4  * Copyright (C) 2007 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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( p_mdis->p_libvlc_instance,
70                                              p_item );
71
72     /* If we have a category, that mean we have to group the items having
73      * that category in a media_list. */
74     if( psz_cat )
75     {
76         p_mlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, psz_cat );
77
78         if( p_mlist == kVLCDictionaryNotFound )
79         {
80             libvlc_media_t * p_catmd;
81             p_catmd = libvlc_media_new_as_node( p_mdis->p_libvlc_instance, psz_cat );
82             p_mlist = libvlc_media_subitems( p_catmd );
83             p_mlist->b_read_only = true;
84
85             /* Insert the newly created mlist in our dictionary */
86             vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist );
87
88             /* Insert the md into the root list */
89             libvlc_media_list_lock( p_mdis->p_mlist );
90             _libvlc_media_list_add_media( p_mdis->p_mlist, p_catmd );
91             libvlc_media_list_unlock( p_mdis->p_mlist );
92
93             /* We don't release the mlist cause the dictionary
94              * doesn't retain the object. But we release the md. */
95             libvlc_media_release( p_catmd );
96         }
97     }
98
99     libvlc_media_list_lock( p_mlist );
100     _libvlc_media_list_add_media( p_mlist, p_md );
101     libvlc_media_list_unlock( p_mlist );
102
103     libvlc_media_release( p_md );
104 }
105
106 /**************************************************************************
107  *       services_discovery_item_removed (Private) (VLC event callback)
108  **************************************************************************/
109
110 static void services_discovery_item_removed( const vlc_event_t * p_event,
111                                              void * user_data )
112 {
113     input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
114     libvlc_media_t * p_md;
115     libvlc_media_discoverer_t * p_mdis = user_data;
116
117     int i, count = libvlc_media_list_count( p_mdis->p_mlist );
118     libvlc_media_list_lock( p_mdis->p_mlist );
119     for( i = 0; i < count; i++ )
120     {
121         p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i );
122         if( p_md->p_input_item == p_item )
123         {
124             _libvlc_media_list_remove_index( p_mdis->p_mlist, i );
125             break;
126         }
127     }
128     libvlc_media_list_unlock( p_mdis->p_mlist );
129 }
130
131 /**************************************************************************
132  *       services_discovery_started (Private) (VLC event callback)
133  **************************************************************************/
134
135 static void services_discovery_started( const vlc_event_t * p_event,
136                                         void * user_data )
137 {
138     VLC_UNUSED(p_event);
139     libvlc_media_discoverer_t * p_mdis = user_data;
140     libvlc_event_t event;
141     p_mdis->running = true;
142     event.type = libvlc_MediaDiscovererStarted;
143     libvlc_event_send( p_mdis->p_event_manager, &event );
144 }
145
146 /**************************************************************************
147  *       services_discovery_ended (Private) (VLC event callback)
148  **************************************************************************/
149
150 static void services_discovery_ended( const vlc_event_t * p_event,
151                                       void * user_data )
152 {
153     VLC_UNUSED(p_event);
154     libvlc_media_discoverer_t * p_mdis = user_data;
155     libvlc_event_t event;
156     p_mdis->running = false;
157     event.type = libvlc_MediaDiscovererEnded;
158     libvlc_event_send( p_mdis->p_event_manager, &event );
159 }
160
161 /*
162  * Public libvlc functions
163  */
164
165 /**************************************************************************
166  *       new (Public)
167  *
168  * Init an object.
169  **************************************************************************/
170 libvlc_media_discoverer_t *
171 libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
172                                        const char * psz_name )
173 {
174     libvlc_media_discoverer_t * p_mdis;
175
176     p_mdis = malloc(sizeof(libvlc_media_discoverer_t));
177     if( unlikely(!p_mdis) )
178     {
179         libvlc_printerr( "Not enough memory" );
180         return NULL;
181     }
182
183     p_mdis->p_libvlc_instance = p_inst;
184     p_mdis->p_mlist = libvlc_media_list_new( p_inst );
185     p_mdis->p_mlist->b_read_only = true;
186     p_mdis->running = false;
187
188     vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
189
190     p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis, p_inst );
191     if( unlikely(p_mdis->p_event_manager == NULL) )
192     {
193         free( p_mdis );
194         return NULL;
195     }
196
197     libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
198             libvlc_MediaDiscovererStarted );
199     libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
200             libvlc_MediaDiscovererEnded );
201
202     p_mdis->p_sd = vlc_sd_Create( (vlc_object_t*)p_inst->p_libvlc_int,
203                                   psz_name );
204     if( unlikely(p_mdis->p_sd == NULL) )
205     {
206         libvlc_printerr( "%s: no such discovery module found", psz_name );
207         libvlc_media_list_release( p_mdis->p_mlist );
208         libvlc_event_manager_release( p_mdis->p_event_manager );
209         free( p_mdis );
210         return NULL;
211     }
212
213     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
214                       vlc_ServicesDiscoveryItemAdded,
215                       services_discovery_item_added,
216                       p_mdis );
217     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
218                       vlc_ServicesDiscoveryItemRemoved,
219                       services_discovery_item_removed,
220                       p_mdis );
221     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
222                       vlc_ServicesDiscoveryStarted,
223                       services_discovery_started,
224                       p_mdis );
225     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
226                       vlc_ServicesDiscoveryEnded,
227                       services_discovery_ended,
228                       p_mdis );
229
230     /* Here we go */
231     if( !vlc_sd_Start( p_mdis->p_sd ) )
232     {
233         libvlc_printerr( "%s: internal module error",
234                          p_mdis->p_sd->psz_name );
235         libvlc_media_list_release( p_mdis->p_mlist );
236         libvlc_event_manager_release( p_mdis->p_event_manager );
237         free( p_mdis );
238         return NULL;
239     }
240
241     return p_mdis;
242 }
243
244 /**************************************************************************
245  * release (Public)
246  **************************************************************************/
247 void
248 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
249 {
250     int i;
251
252     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
253                      vlc_ServicesDiscoveryItemAdded,
254                      services_discovery_item_added,
255                      p_mdis );
256     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
257                      vlc_ServicesDiscoveryItemRemoved,
258                      services_discovery_item_removed,
259                      p_mdis );
260     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
261                      vlc_ServicesDiscoveryStarted,
262                      services_discovery_started,
263                      p_mdis );
264     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
265                      vlc_ServicesDiscoveryEnded,
266                      services_discovery_ended,
267                      p_mdis );
268
269     libvlc_media_list_release( p_mdis->p_mlist );
270
271     vlc_sd_StopAndDestroy( p_mdis->p_sd );
272
273     /* Free catname_to_submedialist and all the mlist */
274     char ** all_keys = vlc_dictionary_all_keys( &p_mdis->catname_to_submedialist );
275     for( i = 0; all_keys[i]; i++ )
276     {
277         libvlc_media_list_t * p_catmlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, all_keys[i] );
278         libvlc_media_list_release( p_catmlist );
279         free( all_keys[i] );
280     }
281     free( all_keys );
282
283     vlc_dictionary_clear( &p_mdis->catname_to_submedialist, NULL, NULL );
284     libvlc_event_manager_release( p_mdis->p_event_manager );
285
286     free( p_mdis );
287 }
288
289 /**************************************************************************
290  * localized_name (Public)
291  **************************************************************************/
292 char *
293 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
294 {
295     return services_discovery_GetLocalizedName( p_mdis->p_sd );
296 }
297
298 /**************************************************************************
299  * media_list (Public)
300  **************************************************************************/
301 libvlc_media_list_t *
302 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
303 {
304     libvlc_media_list_retain( p_mdis->p_mlist );
305     return p_mdis->p_mlist;
306 }
307
308 /**************************************************************************
309  * event_manager (Public)
310  **************************************************************************/
311 libvlc_event_manager_t *
312 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
313 {
314     return p_mdis->p_event_manager;
315 }
316
317
318 /**************************************************************************
319  * running (Public)
320  **************************************************************************/
321 int
322 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
323 {
324     return p_mdis->running;
325 }