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