1 /*****************************************************************************
2 * media_list.c: libvlc new API media list functions
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
27 #include "vlc_arrays.h"
29 typedef enum EventPlaceInTime {
40 /**************************************************************************
41 * notify_item_addition (private)
43 * Do the appropriate action when an item is deleted.
44 **************************************************************************/
46 notify_item_addition( libvlc_media_list_t * p_mlist,
47 libvlc_media_descriptor_t * p_md,
49 EventPlaceInTime event_status )
53 /* Construct the event */
54 if( event_status == EventDidHappen )
56 event.type = libvlc_MediaListItemAdded;
57 event.u.media_list_item_added.item = p_md;
58 event.u.media_list_item_added.index = index;
60 else /* if( event_status == EventWillHappen ) */
62 event.type = libvlc_MediaListWillAddItem;
63 event.u.media_list_will_add_item.item = p_md;
64 event.u.media_list_will_add_item.index = index;
68 libvlc_event_send( p_mlist->p_event_manager, &event );
71 /**************************************************************************
72 * notify_item_deletion (private)
74 * Do the appropriate action when an item is added.
75 **************************************************************************/
77 notify_item_deletion( libvlc_media_list_t * p_mlist,
78 libvlc_media_descriptor_t * p_md,
80 EventPlaceInTime event_status )
84 /* Construct the event */
85 if( event_status == EventDidHappen )
87 event.type = libvlc_MediaListItemDeleted;
88 event.u.media_list_item_deleted.item = p_md;
89 event.u.media_list_item_deleted.index = index;
91 else /* if( event_status == EventWillHappen ) */
93 event.type = libvlc_MediaListWillDeleteItem;
94 event.u.media_list_will_delete_item.item = p_md;
95 event.u.media_list_will_delete_item.index = index;
99 libvlc_event_send( p_mlist->p_event_manager, &event );
103 * Public libvlc functions
106 /**************************************************************************
107 * libvlc_media_list_new (Public)
110 **************************************************************************/
111 libvlc_media_list_t *
112 libvlc_media_list_new( libvlc_instance_t * p_inst,
113 libvlc_exception_t * p_e )
115 libvlc_media_list_t * p_mlist;
117 p_mlist = malloc(sizeof(libvlc_media_list_t));
122 p_mlist->p_libvlc_instance = p_inst;
123 p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
125 /* Code for that one should be handled in flat_media_list.c */
126 p_mlist->p_flat_mlist = NULL;
127 p_mlist->b_read_only = VLC_FALSE;
129 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
130 libvlc_MediaListItemAdded, p_e );
131 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
132 libvlc_MediaListWillAddItem, p_e );
133 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
134 libvlc_MediaListItemDeleted, p_e );
135 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
136 libvlc_MediaListWillDeleteItem, p_e );
138 if( libvlc_exception_raised( p_e ) )
140 libvlc_event_manager_release( p_mlist->p_event_manager );
145 vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
147 vlc_array_init( &p_mlist->items );
148 p_mlist->i_refcount = 1;
149 p_mlist->p_md = NULL;
154 /**************************************************************************
155 * libvlc_media_list_release (Public)
158 **************************************************************************/
159 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
161 libvlc_media_descriptor_t * p_md;
164 vlc_mutex_lock( &p_mlist->object_lock );
165 p_mlist->i_refcount--;
166 if( p_mlist->i_refcount > 0 )
168 vlc_mutex_unlock( &p_mlist->object_lock );
171 vlc_mutex_unlock( &p_mlist->object_lock );
173 /* Refcount null, time to free */
175 libvlc_event_manager_release( p_mlist->p_event_manager );
178 libvlc_media_descriptor_release( p_mlist->p_md );
180 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
182 p_md = vlc_array_item_at_index( &p_mlist->items, i );
183 libvlc_media_descriptor_release( p_md );
186 vlc_mutex_destroy( &p_mlist->object_lock );
187 vlc_array_clear( &p_mlist->items );
192 /**************************************************************************
193 * libvlc_media_list_retain (Public)
195 * Increase an object refcount.
196 **************************************************************************/
197 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
199 vlc_mutex_lock( &p_mlist->object_lock );
200 p_mlist->i_refcount++;
201 vlc_mutex_unlock( &p_mlist->object_lock );
205 /**************************************************************************
206 * add_file_content (Public)
207 **************************************************************************/
209 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
210 const char * psz_uri,
211 libvlc_exception_t * p_e )
213 input_item_t * p_input_item;
214 libvlc_media_descriptor_t * p_md;
216 p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
217 _("Media Library"), 0, NULL, -1 );
221 libvlc_exception_raise( p_e, "Can't create an input item" );
225 p_md = libvlc_media_descriptor_new_from_input_item(
226 p_mlist->p_libvlc_instance,
231 vlc_gc_decref( p_input_item );
235 libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
236 if( libvlc_exception_raised( p_e ) )
239 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
244 /**************************************************************************
245 * set_media_descriptor (Public)
246 **************************************************************************/
247 void libvlc_media_list_set_media_descriptor( libvlc_media_list_t * p_mlist,
248 libvlc_media_descriptor_t * p_md,
249 libvlc_exception_t * p_e)
253 vlc_mutex_lock( &p_mlist->object_lock );
255 libvlc_media_descriptor_release( p_mlist->p_md );
256 libvlc_media_descriptor_retain( p_md );
257 p_mlist->p_md = p_md;
258 vlc_mutex_unlock( &p_mlist->object_lock );
261 /**************************************************************************
262 * media_descriptor (Public)
264 * If this media_list comes is a media_descriptor's subitems,
265 * This holds the corresponding media_descriptor.
266 * This md is also seen as the information holder for the media_list.
267 * Indeed a media_list can have meta information through this
269 **************************************************************************/
270 libvlc_media_descriptor_t *
271 libvlc_media_list_media_descriptor( libvlc_media_list_t * p_mlist,
272 libvlc_exception_t * p_e)
274 libvlc_media_descriptor_t *p_md;
277 vlc_mutex_lock( &p_mlist->object_lock );
278 p_md = p_mlist->p_md;
280 libvlc_media_descriptor_retain( p_md );
281 vlc_mutex_unlock( &p_mlist->object_lock );
286 /**************************************************************************
287 * libvlc_media_list_count (Public)
289 * Lock should be hold when entering.
290 **************************************************************************/
291 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
292 libvlc_exception_t * p_e )
295 return vlc_array_count( &p_mlist->items );
298 /**************************************************************************
299 * libvlc_media_list_add_media_descriptor (Public)
301 * Lock should be hold when entering.
302 **************************************************************************/
303 void libvlc_media_list_add_media_descriptor(
304 libvlc_media_list_t * p_mlist,
305 libvlc_media_descriptor_t * p_md,
306 libvlc_exception_t * p_e )
308 if( p_mlist->b_read_only )
310 /* We are read only from user side */
311 libvlc_exception_raise( p_e, "Trying to write into a read-only media list." );
315 _libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
318 /* LibVLC internal version */
319 void _libvlc_media_list_add_media_descriptor(
320 libvlc_media_list_t * p_mlist,
321 libvlc_media_descriptor_t * p_md,
322 libvlc_exception_t * p_e )
325 libvlc_media_descriptor_retain( p_md );
327 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ), EventWillHappen );
328 vlc_array_append( &p_mlist->items, p_md );
329 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1, EventDidHappen );
332 /**************************************************************************
333 * libvlc_media_list_insert_media_descriptor (Public)
335 * Lock should be hold when entering.
336 **************************************************************************/
337 void libvlc_media_list_insert_media_descriptor(
338 libvlc_media_list_t * p_mlist,
339 libvlc_media_descriptor_t * p_md,
341 libvlc_exception_t * p_e )
343 if( p_mlist->b_read_only )
345 /* We are read only from user side */
346 libvlc_exception_raise( p_e, "Trying to write into a read-only media list." );
349 _libvlc_media_list_insert_media_descriptor( p_mlist, p_md, index, p_e );
352 /* LibVLC internal version */
353 void _libvlc_media_list_insert_media_descriptor(
354 libvlc_media_list_t * p_mlist,
355 libvlc_media_descriptor_t * p_md,
357 libvlc_exception_t * p_e )
360 libvlc_media_descriptor_retain( p_md );
362 notify_item_addition( p_mlist, p_md, index, EventWillHappen );
363 vlc_array_insert( &p_mlist->items, p_md, index );
364 notify_item_addition( p_mlist, p_md, index, EventDidHappen );
367 /**************************************************************************
368 * libvlc_media_list_remove_index (Public)
370 * Lock should be hold when entering.
371 **************************************************************************/
372 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
374 libvlc_exception_t * p_e )
376 if( p_mlist->b_read_only )
378 /* We are read only from user side */
379 libvlc_exception_raise( p_e, "Trying to write into a read-only media list." );
382 _libvlc_media_list_remove_index( p_mlist, index, p_e );
385 /* LibVLC internal version */
386 void _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
388 libvlc_exception_t * p_e )
390 libvlc_media_descriptor_t * p_md;
392 p_md = vlc_array_item_at_index( &p_mlist->items, index );
394 notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
395 vlc_array_remove( &p_mlist->items, index );
396 notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
398 libvlc_media_descriptor_release( p_md );
401 /**************************************************************************
402 * libvlc_media_list_item_at_index (Public)
404 * Lock should be hold when entering.
405 **************************************************************************/
406 libvlc_media_descriptor_t *
407 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
409 libvlc_exception_t * p_e )
411 libvlc_media_descriptor_t * p_md;
412 p_md = vlc_array_item_at_index( &p_mlist->items, index );
413 libvlc_media_descriptor_retain( p_md );
417 /**************************************************************************
418 * libvlc_media_list_index_of_item (Public)
420 * Lock should be hold when entering.
421 * Warning: this function would return the first matching item
422 **************************************************************************/
423 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
424 libvlc_media_descriptor_t * p_searched_md,
425 libvlc_exception_t * p_e )
427 libvlc_media_descriptor_t * p_md;
429 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
431 p_md = vlc_array_item_at_index( &p_mlist->items, i );
432 if( p_searched_md == p_md )
438 /**************************************************************************
439 * libvlc_media_list_is_readonly (Public)
441 * This indicates if this media list is read-only from a user point of view
442 **************************************************************************/
443 vlc_bool_t libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
445 return p_mlist->b_read_only;
448 /**************************************************************************
449 * libvlc_media_list_lock (Public)
451 * The lock must be held in access operations. It is never used in the
453 **************************************************************************/
454 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
456 vlc_mutex_lock( &p_mlist->object_lock );
460 /**************************************************************************
461 * libvlc_media_list_unlock (Public)
463 * The lock must be held in access operations
464 **************************************************************************/
465 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
467 vlc_mutex_unlock( &p_mlist->object_lock );
471 /**************************************************************************
472 * libvlc_media_list_p_event_manager (Public)
474 * The p_event_manager is immutable, so you don't have to hold the lock
475 **************************************************************************/
476 libvlc_event_manager_t *
477 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
478 libvlc_exception_t * p_e )
481 return p_mlist->p_event_manager;