]> git.sesse.net Git - vlc/blob - src/control/media_discoverer.c
b861bd20604076bec2653444004f401b9b74ce2d
[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_descriptor_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_descriptor_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 = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, psz_cat );
55         if( p_mlist == kVLCDictionaryNotFound )
56         {
57             libvlc_media_descriptor_t * p_catmd;
58             p_catmd = libvlc_media_descriptor_new_as_node( p_mdis->p_libvlc_instance, psz_cat, NULL );
59             p_mlist = libvlc_media_descriptor_subitems( p_catmd, NULL );
60             p_mlist->b_read_only = VLC_TRUE;
61
62             /* Insert the newly created mlist in our dictionary */
63             vlc_dictionary_insert( &p_mdis->catname_to_submedialist, psz_cat, p_mlist );
64             
65             /* Insert the md into the root list */
66             _libvlc_media_list_add_media_descriptor( p_mdis->p_mlist, p_catmd, NULL );
67
68             /* We don't release the mlist cause the dictionary
69              * doesn't retain the object. But we release the md. */
70             libvlc_media_descriptor_release( p_catmd );
71         }
72     }
73
74     libvlc_media_list_lock( p_mdis->p_mlist );
75     _libvlc_media_list_add_media_descriptor( p_mlist, p_md, NULL );
76     libvlc_media_list_unlock( p_mdis->p_mlist );
77 }
78
79 /**************************************************************************
80  *       services_discovery_item_removed (Private) (VLC event callback)
81  **************************************************************************/
82
83 static void services_discovery_item_removed( const vlc_event_t * p_event,
84                                              void * user_data )
85 {
86     input_item_t * p_item = p_event->u.services_discovery_item_added.p_new_item;
87     libvlc_media_descriptor_t * p_md;
88     libvlc_media_discoverer_t * p_mdis = user_data;
89
90     int i, count = libvlc_media_list_count( p_mdis->p_mlist, NULL );
91     libvlc_media_list_lock( p_mdis->p_mlist );
92     for( i = 0; i < count; i++ )
93     {
94         p_md = libvlc_media_list_item_at_index( p_mdis->p_mlist, i, NULL );
95         if( p_md->p_input_item == p_item )
96         {
97             _libvlc_media_list_remove_index( p_mdis->p_mlist, i, NULL );
98             break;
99         }
100     }
101     libvlc_media_list_unlock( p_mdis->p_mlist );
102 }
103
104 /**************************************************************************
105  *       services_discovery_started (Private) (VLC event callback)
106  **************************************************************************/
107
108 static void services_discovery_started( const vlc_event_t * p_event,
109                                         void * user_data )
110 {
111     (void)p_event;
112     libvlc_media_discoverer_t * p_mdis = user_data;
113     libvlc_event_t event;
114     p_mdis->running = VLC_TRUE;
115     event.type = libvlc_MediaDiscovererStarted;
116     libvlc_event_send( p_mdis->p_event_manager, &event );
117 }
118
119 /**************************************************************************
120  *       services_discovery_ended (Private) (VLC event callback)
121  **************************************************************************/
122
123 static void services_discovery_ended( const vlc_event_t * p_event,
124                                       void * user_data )
125 {
126     (void)p_event;
127     libvlc_media_discoverer_t * p_mdis = user_data;
128     libvlc_event_t event;
129     p_mdis->running = VLC_FALSE;
130     event.type = libvlc_MediaDiscovererEnded;
131     libvlc_event_send( p_mdis->p_event_manager, &event );
132 }
133
134 /*
135  * Public libvlc functions
136  */
137
138 /**************************************************************************
139  *       new (Public)
140  *
141  * Init an object.
142  **************************************************************************/
143 libvlc_media_discoverer_t *
144 libvlc_media_discoverer_new_from_name( libvlc_instance_t * p_inst,
145                                        const char * psz_name,
146                                        libvlc_exception_t * p_e )
147 {
148     libvlc_media_discoverer_t * p_mdis;
149  
150     p_mdis = malloc(sizeof(libvlc_media_discoverer_t));
151     if( !p_mdis )
152     {
153         libvlc_exception_raise( p_e, "Not enough memory" );
154         return NULL;
155     }
156
157     p_mdis->p_libvlc_instance = p_inst;
158     p_mdis->p_mlist = libvlc_media_list_new( p_inst, NULL );
159     p_mdis->p_mlist->b_read_only = VLC_TRUE;
160     p_mdis->running = VLC_FALSE;
161
162     vlc_dictionary_init( &p_mdis->catname_to_submedialist, 0 );
163
164     p_mdis->p_event_manager = libvlc_event_manager_new( p_mdis,
165             p_inst, NULL );
166
167     libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
168             libvlc_MediaDiscovererStarted, NULL );
169     libvlc_event_manager_register_event_type( p_mdis->p_event_manager,
170             libvlc_MediaDiscovererEnded, NULL );
171
172     p_mdis->p_sd = services_discovery_Create( (vlc_object_t*)p_inst->p_libvlc_int, psz_name );
173
174     if( !p_mdis->p_sd )
175     {
176         free( p_mdis );
177         libvlc_exception_raise( p_e, "Can't find the services_discovery module named '%s'", psz_name );
178         return NULL;
179     }
180
181     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
182                       vlc_ServicesDiscoveryItemAdded,
183                       services_discovery_item_added,
184                       p_mdis );
185     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
186                       vlc_ServicesDiscoveryItemRemoved,
187                       services_discovery_item_removed,
188                       p_mdis );
189     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
190                       vlc_ServicesDiscoveryStarted,
191                       services_discovery_started,
192                       p_mdis );
193     vlc_event_attach( services_discovery_EventManager( p_mdis->p_sd ),
194                       vlc_ServicesDiscoveryEnded,
195                       services_discovery_ended,
196                       p_mdis );
197  
198     services_discovery_Start( p_mdis->p_sd );
199
200     /* Here we go */
201
202     return p_mdis;
203 }
204
205 /**************************************************************************
206  * release (Public)
207  **************************************************************************/
208 void
209 libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
210 {
211     int i;
212
213     libvlc_media_list_release( p_mdis->p_mlist );
214     services_discovery_Destroy( p_mdis->p_sd );
215
216     /* Free catname_to_submedialist and all the mlist */
217     char ** all_keys = vlc_dictionary_all_keys( &p_mdis->catname_to_submedialist );
218     for( i = 0; all_keys[i]; i++ )
219     {
220         libvlc_media_list_t * p_catmlist = vlc_dictionary_value_for_key( &p_mdis->catname_to_submedialist, all_keys[i] );
221         libvlc_media_list_release( p_catmlist );
222         free( all_keys[i] );
223     }
224     free( all_keys );
225
226     vlc_dictionary_clear( &p_mdis->catname_to_submedialist );
227
228     free( p_mdis );
229 }
230
231 /**************************************************************************
232  * localized_name (Public)
233  **************************************************************************/
234 char *
235 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t * p_mdis )
236 {
237     return services_discovery_GetLocalizedName( p_mdis->p_sd );
238 }
239
240 /**************************************************************************
241  * media_list (Public)
242  **************************************************************************/
243 libvlc_media_list_t *
244 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t * p_mdis )
245 {
246     libvlc_media_list_retain( p_mdis->p_mlist );
247     return p_mdis->p_mlist;
248 }
249
250 /**************************************************************************
251  * event_manager (Public)
252  **************************************************************************/
253 libvlc_event_manager_t *
254 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t * p_mdis )
255 {
256     return p_mdis->p_event_manager;
257 }
258
259
260 /**************************************************************************
261  * running (Public)
262  **************************************************************************/
263 vlc_bool_t
264 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t * p_mdis )
265 {
266     return p_mdis->running;
267 }
268