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 {
34 //#define DEBUG_MEDIA_LIST
36 #ifdef DEBUG_MEDIA_LIST
37 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
48 /**************************************************************************
49 * notify_item_addition (private)
51 * Do the appropriate action when an item is deleted.
52 **************************************************************************/
54 notify_item_addition( libvlc_media_list_t * p_mlist,
55 libvlc_media_t * p_md,
57 EventPlaceInTime event_status )
61 /* Construct the event */
62 if( event_status == EventDidHappen )
64 trace("item was added at index %d\n", index);
65 event.type = libvlc_MediaListItemAdded;
66 event.u.media_list_item_added.item = p_md;
67 event.u.media_list_item_added.index = index;
69 else /* if( event_status == EventWillHappen ) */
71 event.type = libvlc_MediaListWillAddItem;
72 event.u.media_list_will_add_item.item = p_md;
73 event.u.media_list_will_add_item.index = index;
77 libvlc_event_send( p_mlist->p_event_manager, &event );
80 /**************************************************************************
81 * notify_item_deletion (private)
83 * Do the appropriate action when an item is added.
84 **************************************************************************/
86 notify_item_deletion( libvlc_media_list_t * p_mlist,
87 libvlc_media_t * p_md,
89 EventPlaceInTime event_status )
93 /* Construct the event */
94 if( event_status == EventDidHappen )
96 trace("item at index %d was deleted\n", index);
97 event.type = libvlc_MediaListItemDeleted;
98 event.u.media_list_item_deleted.item = p_md;
99 event.u.media_list_item_deleted.index = index;
101 else /* if( event_status == EventWillHappen ) */
103 event.type = libvlc_MediaListWillDeleteItem;
104 event.u.media_list_will_delete_item.item = p_md;
105 event.u.media_list_will_delete_item.index = index;
109 libvlc_event_send( p_mlist->p_event_manager, &event );
112 /**************************************************************************
113 * static mlist_is_writable (private)
115 * Raise exception and return 0 when the media_list instance is read-only,
117 **************************************************************************/
119 int mlist_is_writable( libvlc_media_list_t *p_mlist, libvlc_exception_t *p_e )
121 if( !p_mlist||p_mlist->b_read_only )
123 /* We are read-only from user side */
124 libvlc_exception_raise( p_e, "Cannot write to read-only media list." );
131 * Public libvlc functions
134 /**************************************************************************
135 * libvlc_media_list_new (Public)
138 **************************************************************************/
139 libvlc_media_list_t *
140 libvlc_media_list_new( libvlc_instance_t * p_inst,
141 libvlc_exception_t * p_e )
143 libvlc_media_list_t * p_mlist;
145 p_mlist = malloc(sizeof(libvlc_media_list_t));
149 p_mlist->p_libvlc_instance = p_inst;
150 p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
152 /* Code for that one should be handled in flat_media_list.c */
153 p_mlist->p_flat_mlist = NULL;
154 p_mlist->b_read_only = false;
156 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
157 libvlc_MediaListItemAdded, p_e );
158 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
159 libvlc_MediaListWillAddItem, p_e );
160 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
161 libvlc_MediaListItemDeleted, p_e );
162 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
163 libvlc_MediaListWillDeleteItem, p_e );
165 if( libvlc_exception_raised( p_e ) )
167 libvlc_event_manager_release( p_mlist->p_event_manager );
172 vlc_mutex_init( &p_mlist->object_lock );
174 vlc_array_init( &p_mlist->items );
175 p_mlist->i_refcount = 1;
176 p_mlist->p_md = NULL;
181 /**************************************************************************
182 * libvlc_media_list_release (Public)
185 **************************************************************************/
186 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
188 libvlc_media_t * p_md;
191 vlc_mutex_lock( &p_mlist->object_lock );
192 p_mlist->i_refcount--;
193 if( p_mlist->i_refcount > 0 )
195 vlc_mutex_unlock( &p_mlist->object_lock );
198 vlc_mutex_unlock( &p_mlist->object_lock );
200 /* Refcount null, time to free */
202 libvlc_event_manager_release( p_mlist->p_event_manager );
205 libvlc_media_release( p_mlist->p_md );
207 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
209 p_md = vlc_array_item_at_index( &p_mlist->items, i );
210 libvlc_media_release( p_md );
213 vlc_mutex_destroy( &p_mlist->object_lock );
214 vlc_array_clear( &p_mlist->items );
219 /**************************************************************************
220 * libvlc_media_list_retain (Public)
222 * Increase an object refcount.
223 **************************************************************************/
224 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
226 vlc_mutex_lock( &p_mlist->object_lock );
227 p_mlist->i_refcount++;
228 vlc_mutex_unlock( &p_mlist->object_lock );
232 /**************************************************************************
233 * add_file_content (Public)
234 **************************************************************************/
236 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
237 const char * psz_uri,
238 libvlc_exception_t * p_e )
240 input_item_t * p_input_item;
241 libvlc_media_t * p_md;
243 p_input_item = input_item_NewExt(
244 p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
245 _("Media Library"), 0, NULL, 0, -1 );
249 libvlc_exception_raise( p_e, "Can't create an input item" );
253 p_md = libvlc_media_new_from_input_item(
254 p_mlist->p_libvlc_instance,
259 vlc_gc_decref( p_input_item );
263 libvlc_media_list_add_media( p_mlist, p_md, p_e );
264 if( libvlc_exception_raised( p_e ) )
267 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, true );
272 /**************************************************************************
274 **************************************************************************/
275 void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
276 libvlc_media_t * p_md,
277 libvlc_exception_t * p_e)
281 vlc_mutex_lock( &p_mlist->object_lock );
283 libvlc_media_release( p_mlist->p_md );
284 libvlc_media_retain( p_md );
285 p_mlist->p_md = p_md;
286 vlc_mutex_unlock( &p_mlist->object_lock );
289 /**************************************************************************
292 * If this media_list comes is a media's subitems,
293 * This holds the corresponding media.
294 * This md is also seen as the information holder for the media_list.
295 * Indeed a media_list can have meta information through this
297 **************************************************************************/
299 libvlc_media_list_media( libvlc_media_list_t * p_mlist,
300 libvlc_exception_t * p_e)
302 libvlc_media_t *p_md;
305 vlc_mutex_lock( &p_mlist->object_lock );
306 p_md = p_mlist->p_md;
308 libvlc_media_retain( p_md );
309 vlc_mutex_unlock( &p_mlist->object_lock );
314 /**************************************************************************
315 * libvlc_media_list_count (Public)
317 * Lock should be hold when entering.
318 **************************************************************************/
319 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
320 libvlc_exception_t * p_e )
323 return vlc_array_count( &p_mlist->items );
326 /**************************************************************************
327 * libvlc_media_list_add_media (Public)
329 * Lock should be held when entering.
330 **************************************************************************/
331 void libvlc_media_list_add_media(
332 libvlc_media_list_t * p_mlist,
333 libvlc_media_t * p_md,
334 libvlc_exception_t * p_e )
336 if( mlist_is_writable(p_mlist,p_e) )
337 _libvlc_media_list_add_media( p_mlist, p_md, p_e );
340 /* LibVLC internal version */
341 void _libvlc_media_list_add_media(
342 libvlc_media_list_t * p_mlist,
343 libvlc_media_t * p_md,
344 libvlc_exception_t * p_e )
347 libvlc_media_retain( p_md );
349 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
351 vlc_array_append( &p_mlist->items, p_md );
352 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
356 /**************************************************************************
357 * libvlc_media_list_insert_media (Public)
359 * Lock should be hold when entering.
360 **************************************************************************/
361 void libvlc_media_list_insert_media(
362 libvlc_media_list_t * p_mlist,
363 libvlc_media_t * p_md,
365 libvlc_exception_t * p_e )
367 if( mlist_is_writable(p_mlist,p_e) )
368 _libvlc_media_list_insert_media( p_mlist, p_md, index, p_e );
371 /* LibVLC internal version */
372 void _libvlc_media_list_insert_media(
373 libvlc_media_list_t * p_mlist,
374 libvlc_media_t * p_md,
376 libvlc_exception_t * p_e )
379 libvlc_media_retain( p_md );
381 notify_item_addition( p_mlist, p_md, index, EventWillHappen );
382 vlc_array_insert( &p_mlist->items, p_md, index );
383 notify_item_addition( p_mlist, p_md, index, EventDidHappen );
386 /**************************************************************************
387 * libvlc_media_list_remove_index (Public)
389 * Lock should be held when entering.
390 **************************************************************************/
391 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
393 libvlc_exception_t * p_e )
395 if( mlist_is_writable(p_mlist,p_e) )
396 _libvlc_media_list_remove_index( p_mlist, index, p_e );
399 /* LibVLC internal version */
400 void _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
402 libvlc_exception_t * p_e )
404 libvlc_media_t * p_md;
406 if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
408 libvlc_exception_raise( p_e, "Index out of bounds");
412 p_md = vlc_array_item_at_index( &p_mlist->items, index );
414 notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
415 vlc_array_remove( &p_mlist->items, index );
416 notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
418 libvlc_media_release( p_md );
421 /**************************************************************************
422 * libvlc_media_list_item_at_index (Public)
424 * Lock should be held when entering.
425 **************************************************************************/
427 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
429 libvlc_exception_t * p_e )
431 libvlc_media_t * p_md;
433 if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
435 libvlc_exception_raise( p_e, "Index out of bounds");
439 p_md = vlc_array_item_at_index( &p_mlist->items, index );
440 libvlc_media_retain( p_md );
444 /**************************************************************************
445 * libvlc_media_list_index_of_item (Public)
447 * Lock should be held when entering.
448 * Warning: this function returns the first matching item.
449 **************************************************************************/
450 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
451 libvlc_media_t * p_searched_md,
452 libvlc_exception_t * p_e )
456 libvlc_media_t * p_md;
458 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
460 p_md = vlc_array_item_at_index( &p_mlist->items, i );
461 if( p_searched_md == p_md )
467 /**************************************************************************
468 * libvlc_media_list_is_readonly (Public)
470 * This indicates if this media list is read-only from a user point of view
471 **************************************************************************/
472 int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
474 return p_mlist->b_read_only;
477 /**************************************************************************
478 * libvlc_media_list_lock (Public)
480 * The lock must be held in access operations. It is never used in the
482 **************************************************************************/
483 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
485 vlc_mutex_lock( &p_mlist->object_lock );
489 /**************************************************************************
490 * libvlc_media_list_unlock (Public)
492 * The lock must be held in access operations
493 **************************************************************************/
494 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
496 vlc_mutex_unlock( &p_mlist->object_lock );
500 /**************************************************************************
501 * libvlc_media_list_p_event_manager (Public)
503 * The p_event_manager is immutable, so you don't have to hold the lock
504 **************************************************************************/
505 libvlc_event_manager_t *
506 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
507 libvlc_exception_t * p_e )
510 return p_mlist->p_event_manager;