1 /*****************************************************************************
2 * media_discoverer.c: libvlc new API media discoverer functions
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
7 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
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>
36 #include <vlc_services_discovery.h>
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_internal_add_media()
42 struct libvlc_media_discoverer_t
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
;
49 vlc_dictionary_t catname_to_submedialist
;
56 /**************************************************************************
57 * services_discovery_item_added (Private) (VLC event callback)
58 **************************************************************************/
60 static void services_discovery_item_added( const vlc_event_t
* p_event
,
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
;
69 p_md
= libvlc_media_new_from_input_item( p_mdis
->p_libvlc_instance
,
72 /* If we have a category, that mean we have to group the items having
73 * that category in a media_list. */
76 p_mlist
= vlc_dictionary_value_for_key( &p_mdis
->catname_to_submedialist
, psz_cat
);
78 if( p_mlist
== kVLCDictionaryNotFound
)
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;
85 /* Insert the newly created mlist in our dictionary */
86 vlc_dictionary_insert( &p_mdis
->catname_to_submedialist
, psz_cat
, p_mlist
);
88 /* Insert the md into the root list */
89 libvlc_media_list_lock( p_mdis
->p_mlist
);
90 libvlc_media_list_internal_add_media( p_mdis
->p_mlist
, p_catmd
);
91 libvlc_media_list_unlock( p_mdis
->p_mlist
);
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
);
99 libvlc_media_list_lock( p_mlist
);
100 libvlc_media_list_internal_add_media( p_mlist
, p_md
);
101 libvlc_media_list_unlock( p_mlist
);
103 libvlc_media_release( p_md
);
106 /**************************************************************************
107 * services_discovery_item_removed (Private) (VLC event callback)
108 **************************************************************************/
110 static void services_discovery_item_removed( const vlc_event_t
* p_event
,
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
;
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
++ )
121 p_md
= libvlc_media_list_item_at_index( p_mdis
->p_mlist
, i
);
122 if( p_md
->p_input_item
== p_item
)
124 libvlc_media_list_internal_remove_index( p_mdis
->p_mlist
, i
);
128 libvlc_media_list_unlock( p_mdis
->p_mlist
);
131 /**************************************************************************
132 * services_discovery_removeall (Private) (VLC event callback)
133 **************************************************************************/
134 static void services_discovery_removeall( const vlc_event_t
* p_event
,
138 libvlc_media_discoverer_t
* p_mdis
= user_data
;
140 libvlc_media_list_lock( p_mdis
->p_mlist
);
141 for( int i
= 0; i
< libvlc_media_list_count( p_mdis
->p_mlist
); i
++ )
143 libvlc_media_list_internal_remove_index( p_mdis
->p_mlist
, i
);
145 libvlc_media_list_unlock( p_mdis
->p_mlist
);
148 /**************************************************************************
149 * services_discovery_started (Private) (VLC event callback)
150 **************************************************************************/
152 static void services_discovery_started( const vlc_event_t
* 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
);
163 /**************************************************************************
164 * services_discovery_ended (Private) (VLC event callback)
165 **************************************************************************/
167 static void services_discovery_ended( const vlc_event_t
* p_event
,
171 libvlc_media_discoverer_t
* p_mdis
= user_data
;
172 libvlc_media_list_t
* p_mlist
= p_mdis
->p_mlist
;
173 libvlc_event_t event
;
175 p_mdis
->running
= false;
177 libvlc_media_list_lock( p_mlist
);
178 libvlc_media_list_internal_end_reached( p_mlist
);
179 libvlc_media_list_unlock( p_mlist
);
181 event
.type
= libvlc_MediaDiscovererEnded
;
182 libvlc_event_send( p_mdis
->p_event_manager
, &event
);
186 * Public libvlc functions
189 /**************************************************************************
191 **************************************************************************/
192 libvlc_media_discoverer_t
*
193 libvlc_media_discoverer_new( libvlc_instance_t
* p_inst
, const char * psz_name
)
195 /* podcast SD is a hack and only works with custom playlist callbacks. */
196 if( !strncasecmp( psz_name
, "podcast", 7 ) )
199 libvlc_media_discoverer_t
*p_mdis
= malloc(sizeof(*p_mdis
));
200 if( unlikely(!p_mdis
) )
202 libvlc_printerr( "Not enough memory" );
206 p_mdis
->p_libvlc_instance
= p_inst
;
207 p_mdis
->p_mlist
= libvlc_media_list_new( p_inst
);
208 p_mdis
->p_mlist
->b_read_only
= true;
209 p_mdis
->running
= false;
211 vlc_dictionary_init( &p_mdis
->catname_to_submedialist
, 0 );
213 p_mdis
->p_event_manager
= libvlc_event_manager_new( p_mdis
);
214 if( unlikely(p_mdis
->p_event_manager
== NULL
) )
220 p_mdis
->p_sd
= vlc_sd_Create( (vlc_object_t
*)p_inst
->p_libvlc_int
,
222 if( unlikely(p_mdis
->p_sd
== NULL
) )
224 libvlc_printerr( "%s: no such discovery module found", psz_name
);
225 libvlc_media_list_release( p_mdis
->p_mlist
);
226 libvlc_event_manager_release( p_mdis
->p_event_manager
);
231 vlc_event_attach( services_discovery_EventManager( p_mdis
->p_sd
),
232 vlc_ServicesDiscoveryItemAdded
,
233 services_discovery_item_added
,
235 vlc_event_attach( services_discovery_EventManager( p_mdis
->p_sd
),
236 vlc_ServicesDiscoveryItemRemoved
,
237 services_discovery_item_removed
,
239 vlc_event_attach( services_discovery_EventManager( p_mdis
->p_sd
),
240 vlc_ServicesDiscoveryStarted
,
241 services_discovery_started
,
243 vlc_event_attach( services_discovery_EventManager( p_mdis
->p_sd
),
244 vlc_ServicesDiscoveryEnded
,
245 services_discovery_ended
,
247 vlc_event_attach( services_discovery_EventManager( p_mdis
->p_sd
),
248 vlc_ServicesDiscoveryItemRemoveAll
,
249 services_discovery_removeall
,
252 libvlc_retain( p_inst
);
256 /**************************************************************************
258 **************************************************************************/
260 libvlc_media_discoverer_start( libvlc_media_discoverer_t
* p_mdis
)
263 return vlc_sd_Start( p_mdis
->p_sd
) ? 0 : -1;
266 /**************************************************************************
268 **************************************************************************/
270 libvlc_media_discoverer_stop( libvlc_media_discoverer_t
* p_mdis
)
272 return vlc_sd_Stop( p_mdis
->p_sd
);
275 /**************************************************************************
276 * new_from_name (Public)
278 * \deprecated Use libvlc_media_discoverer_new and libvlc_media_discoverer_start
279 **************************************************************************/
280 libvlc_media_discoverer_t
*
281 libvlc_media_discoverer_new_from_name( libvlc_instance_t
* p_inst
,
282 const char * psz_name
)
284 libvlc_media_discoverer_t
*p_mdis
= libvlc_media_discoverer_new( p_inst
, psz_name
);
289 if( libvlc_media_discoverer_start( p_mdis
) != 0)
291 libvlc_media_discoverer_release( p_mdis
);
298 /**************************************************************************
300 **************************************************************************/
302 libvlc_media_discoverer_release( libvlc_media_discoverer_t
* p_mdis
)
306 vlc_event_detach( services_discovery_EventManager( p_mdis
->p_sd
),
307 vlc_ServicesDiscoveryItemAdded
,
308 services_discovery_item_added
,
310 vlc_event_detach( services_discovery_EventManager( p_mdis
->p_sd
),
311 vlc_ServicesDiscoveryItemRemoved
,
312 services_discovery_item_removed
,
314 vlc_event_detach( services_discovery_EventManager( p_mdis
->p_sd
),
315 vlc_ServicesDiscoveryStarted
,
316 services_discovery_started
,
318 vlc_event_detach( services_discovery_EventManager( p_mdis
->p_sd
),
319 vlc_ServicesDiscoveryEnded
,
320 services_discovery_ended
,
322 vlc_event_detach( services_discovery_EventManager( p_mdis
->p_sd
),
323 vlc_ServicesDiscoveryItemRemoveAll
,
324 services_discovery_removeall
,
327 libvlc_media_list_release( p_mdis
->p_mlist
);
329 if( p_mdis
->running
)
330 vlc_sd_Stop( p_mdis
->p_sd
);
332 vlc_sd_Destroy( p_mdis
->p_sd
);
334 /* Free catname_to_submedialist and all the mlist */
335 char ** all_keys
= vlc_dictionary_all_keys( &p_mdis
->catname_to_submedialist
);
336 for( i
= 0; all_keys
[i
]; i
++ )
338 libvlc_media_list_t
* p_catmlist
= vlc_dictionary_value_for_key( &p_mdis
->catname_to_submedialist
, all_keys
[i
] );
339 libvlc_media_list_release( p_catmlist
);
344 vlc_dictionary_clear( &p_mdis
->catname_to_submedialist
, NULL
, NULL
);
345 libvlc_event_manager_release( p_mdis
->p_event_manager
);
346 libvlc_release( p_mdis
->p_libvlc_instance
);
351 /**************************************************************************
352 * localized_name (Public)
353 **************************************************************************/
355 libvlc_media_discoverer_localized_name( libvlc_media_discoverer_t
* p_mdis
)
357 return services_discovery_GetLocalizedName( p_mdis
->p_sd
);
360 /**************************************************************************
361 * media_list (Public)
362 **************************************************************************/
363 libvlc_media_list_t
*
364 libvlc_media_discoverer_media_list( libvlc_media_discoverer_t
* p_mdis
)
366 libvlc_media_list_retain( p_mdis
->p_mlist
);
367 return p_mdis
->p_mlist
;
370 /**************************************************************************
371 * event_manager (Public)
372 **************************************************************************/
373 libvlc_event_manager_t
*
374 libvlc_media_discoverer_event_manager( libvlc_media_discoverer_t
* p_mdis
)
376 return p_mdis
->p_event_manager
;
380 /**************************************************************************
382 **************************************************************************/
384 libvlc_media_discoverer_is_running( libvlc_media_discoverer_t
* p_mdis
)
386 return p_mdis
->running
;