Simple: use ITU-R BS.775-2 matrices for 5.x downmixing
[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_removeall (Private) (VLC event callback)
133  **************************************************************************/
134 static void services_discovery_removeall( 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
140     libvlc_media_list_lock( p_mdis->p_mlist );
141     for( int i = 0; i < libvlc_media_list_count( p_mdis->p_mlist ); i++ )
142     {
143         _libvlc_media_list_remove_index( p_mdis->p_mlist, i );
144     }
145     libvlc_media_list_unlock( p_mdis->p_mlist );
146 }
147
148 /**************************************************************************
149  *       services_discovery_started (Private) (VLC event callback)
150  **************************************************************************/
151
152 static void services_discovery_started( const vlc_event_t * p_event,
153                                         void * user_data )
154 {
155     VLC_UNUSED(p_event);
156     libvlc_media_discoverer_t * p_mdis = user_data;
157     libvlc_event_t event;
158     p_mdis->running = true;
159     event.type = libvlc_MediaDiscovererStarted;
160     libvlc_event_send( p_mdis->p_event_manager, &event );
161 }
162
163 /**************************************************************************
164  *       services_discovery_ended (Private) (VLC event callback)
165  **************************************************************************/
166
167 static void services_discovery_ended( const vlc_event_t * p_event,
168                                       void * user_data )
169 {
170     VLC_UNUSED(p_event);
171     libvlc_media_discoverer_t * p_mdis = user_data;
172     libvlc_event_t event;
173     p_mdis->running = false;
174     event.type = libvlc_MediaDiscovererEnded;
175     libvlc_event_send( p_mdis->p_event_manager, &event );
176 }
177
178 /*
179  * Public libvlc functions
180  */
181
182 /**************************************************************************
183  *       new (Public)
184  *
185  * Init an object.
186  **************************************************************************/
187 libvlc_media_discoverer_t *
188 libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
189                                        const char * psz_name )
190 {
191     libvlc_media_discoverer_t * p_mdis;
192
193     p_mdis = malloc(sizeof(libvlc_media_discoverer_t));
194     if( unlikely(!p_mdis) )
195     {
196         libvlc_printerr( "Not enough memory" );
197         return NULL;
198     }
199
200     p_mdis->p_libvlc_instance = p_inst;
201     p_mdis->p_mlist = libvlc_media_list_new( p_inst );
202     p_mdis->p_mlist->b_read_only = true;
203     p_mdis->running = false;
204
205     vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
206
207     p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis, p_inst );
208     if( unlikely(p_mdis->p_event_manager == NULL) )
209     {
210         free( p_mdis );
211         return NULL;
212     }
213
214     libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
215             libvlc_MediaDiscovererStarted );
216     libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
217             libvlc_MediaDiscovererEnded );
218
219     p_mdis->p_sd = vlc_sd_Create( (vlc_object_t*)p_inst->p_libvlc_int,
220                                   psz_name );
221     if( unlikely(p_mdis->p_sd == NULL) )
222     {
223         libvlc_printerr( "%s: no such discovery module found", psz_name );
224         libvlc_media_list_release( p_mdis->p_mlist );
225         libvlc_event_manager_release( p_mdis->p_event_manager );
226         free( p_mdis );
227         return NULL;
228     }
229
230     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
231                       vlc_ServicesDiscoveryItemAdded,
232                       services_discovery_item_added,
233                       p_mdis );
234     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
235                       vlc_ServicesDiscoveryItemRemoved,
236                       services_discovery_item_removed,
237                       p_mdis );
238     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
239                       vlc_ServicesDiscoveryStarted,
240                       services_discovery_started,
241                       p_mdis );
242     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
243                       vlc_ServicesDiscoveryEnded,
244                       services_discovery_ended,
245                       p_mdis );
246     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
247                       vlc_ServicesDiscoveryItemRemoveAll,
248                       services_discovery_removeall,
249                       p_mdis );
250
251     /* Here we go */
252     if( !vlc_sd_Start( p_mdis->p_sd ) )
253     {
254         libvlc_printerr( "%s: internal module error",
255                          p_mdis->p_sd->psz_name );
256         libvlc_media_list_release( p_mdis->p_mlist );
257         libvlc_event_manager_release( p_mdis->p_event_manager );
258         free( p_mdis );
259         return NULL;
260     }
261
262     return p_mdis;
263 }
264
265 /**************************************************************************
266  * release (Public)
267  **************************************************************************/
268 void
269 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
270 {
271     int i;
272
273     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
274                      vlc_ServicesDiscoveryItemAdded,
275                      services_discovery_item_added,
276                      p_mdis );
277     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
278                      vlc_ServicesDiscoveryItemRemoved,
279                      services_discovery_item_removed,
280                      p_mdis );
281     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
282                      vlc_ServicesDiscoveryStarted,
283                      services_discovery_started,
284                      p_mdis );
285     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
286                      vlc_ServicesDiscoveryEnded,
287                      services_discovery_ended,
288                      p_mdis );
289     vlc_event_detach( services_discovery_EventManager( p_mdis->p_sd ),
290                      vlc_ServicesDiscoveryItemRemoveAll,
291                      services_discovery_removeall,
292                      p_mdis );
293
294     libvlc_media_list_release( p_mdis->p_mlist );
295
296     vlc_sd_StopAndDestroy( p_mdis->p_sd );
297
298     /* Free catname_to_submedialist and all the mlist */
299     char ** all_keys = vlc_dictionary_all_keys( &p_mdis->catname_to_submedialist );
300     for( i = 0; all_keys[i]; i++ )
301     {
302         libvlc_media_list_t * p_catmlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, all_keys[i] );
303         libvlc_media_list_release( p_catmlist );
304         free( all_keys[i] );
305     }
306     free( all_keys );
307
308     vlc_dictionary_clear( &p_mdis->catname_to_submedialist, NULL, NULL );
309     libvlc_event_manager_release( p_mdis->p_event_manager );
310
311     free( p_mdis );
312 }
313
314 /**************************************************************************
315  * localized_name (Public)
316  **************************************************************************/
317 char *
318 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
319 {
320     return services_discovery_GetLocalizedName( p_mdis->p_sd );
321 }
322
323 /**************************************************************************
324  * media_list (Public)
325  **************************************************************************/
326 libvlc_media_list_t *
327 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
328 {
329     libvlc_media_list_retain( p_mdis->p_mlist );
330     return p_mdis->p_mlist;
331 }
332
333 /**************************************************************************
334  * event_manager (Public)
335  **************************************************************************/
336 libvlc_event_manager_t *
337 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
338 {
339     return p_mdis->p_event_manager;
340 }
341
342
343 /**************************************************************************
344  * running (Public)
345  **************************************************************************/
346 int
347 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
348 {
349     return p_mdis->running;
350 }