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 *****************************************************************************/
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_media.h>
32 #include <vlc/libvlc_media_list.h>
33 #include <vlc/libvlc_events.h>
35 #include <vlc_common.h>
36 #include <vlc_input.h>
38 #include "libvlc_internal.h"
39 #include "media_internal.h" // libvlc_media_new_from_input_item()
40 #include "media_list_internal.h"
42 typedef enum EventPlaceInTime {
47 //#define DEBUG_MEDIA_LIST
49 #ifdef DEBUG_MEDIA_LIST
50 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
61 /**************************************************************************
62 * notify_item_addition (private)
64 * Do the appropriate action when an item is deleted.
65 **************************************************************************/
67 notify_item_addition( libvlc_media_list_t * p_mlist,
68 libvlc_media_t * p_md,
70 EventPlaceInTime event_status )
74 /* Construct the event */
75 if( event_status == EventDidHappen )
77 trace("item was added at index %d\n", index);
78 event.type = libvlc_MediaListItemAdded;
79 event.u.media_list_item_added.item = p_md;
80 event.u.media_list_item_added.index = index;
82 else /* if( event_status == EventWillHappen ) */
84 event.type = libvlc_MediaListWillAddItem;
85 event.u.media_list_will_add_item.item = p_md;
86 event.u.media_list_will_add_item.index = index;
90 libvlc_event_send( p_mlist->p_event_manager, &event );
93 /**************************************************************************
94 * notify_item_deletion (private)
96 * Do the appropriate action when an item is added.
97 **************************************************************************/
99 notify_item_deletion( libvlc_media_list_t * p_mlist,
100 libvlc_media_t * p_md,
102 EventPlaceInTime event_status )
104 libvlc_event_t event;
106 /* Construct the event */
107 if( event_status == EventDidHappen )
109 trace("item at index %d was deleted\n", index);
110 event.type = libvlc_MediaListItemDeleted;
111 event.u.media_list_item_deleted.item = p_md;
112 event.u.media_list_item_deleted.index = index;
114 else /* if( event_status == EventWillHappen ) */
116 event.type = libvlc_MediaListWillDeleteItem;
117 event.u.media_list_will_delete_item.item = p_md;
118 event.u.media_list_will_delete_item.index = index;
122 libvlc_event_send( p_mlist->p_event_manager, &event );
125 /**************************************************************************
126 * static mlist_is_writable (private)
127 **************************************************************************/
129 bool mlist_is_writable( libvlc_media_list_t *p_mlist )
131 if( !p_mlist||p_mlist->b_read_only )
133 /* We are read-only from user side */
134 libvlc_printerr( "Attempt to write a read-only media list" );
141 * Public libvlc functions
144 /**************************************************************************
145 * libvlc_media_list_new (Public)
148 **************************************************************************/
149 libvlc_media_list_t *
150 libvlc_media_list_new( libvlc_instance_t * p_inst )
152 libvlc_media_list_t * p_mlist;
154 p_mlist = malloc(sizeof(libvlc_media_list_t));
155 if( unlikely(p_mlist == NULL) )
157 libvlc_printerr( "Not enough memory" );
161 p_mlist->p_libvlc_instance = p_inst;
162 p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst );
163 if( unlikely(p_mlist->p_event_manager == NULL) )
169 /* Code for that one should be handled in flat_media_list.c */
170 p_mlist->p_flat_mlist = NULL;
171 p_mlist->b_read_only = false;
173 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
174 libvlc_MediaListItemAdded );
175 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
176 libvlc_MediaListWillAddItem );
177 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
178 libvlc_MediaListItemDeleted );
179 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
180 libvlc_MediaListWillDeleteItem );
182 vlc_mutex_init( &p_mlist->object_lock );
183 vlc_mutex_init( &p_mlist->refcount_lock ); // FIXME: spinlock?
185 vlc_array_init( &p_mlist->items );
186 p_mlist->i_refcount = 1;
187 p_mlist->p_md = NULL;
192 /**************************************************************************
193 * libvlc_media_list_release (Public)
196 **************************************************************************/
197 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
199 libvlc_media_t * p_md;
202 vlc_mutex_lock( &p_mlist->refcount_lock );
203 p_mlist->i_refcount--;
204 if( p_mlist->i_refcount > 0 )
206 vlc_mutex_unlock( &p_mlist->refcount_lock );
209 vlc_mutex_unlock( &p_mlist->refcount_lock );
211 /* Refcount null, time to free */
213 libvlc_event_manager_release( p_mlist->p_event_manager );
215 libvlc_media_release( p_mlist->p_md );
217 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
219 p_md = vlc_array_item_at_index( &p_mlist->items, i );
220 libvlc_media_release( p_md );
223 vlc_mutex_destroy( &p_mlist->object_lock );
224 vlc_array_clear( &p_mlist->items );
229 /**************************************************************************
230 * libvlc_media_list_retain (Public)
232 * Increase an object refcount.
233 **************************************************************************/
234 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
236 vlc_mutex_lock( &p_mlist->refcount_lock );
237 p_mlist->i_refcount++;
238 vlc_mutex_unlock( &p_mlist->refcount_lock );
242 /**************************************************************************
243 * add_file_content (Public)
244 **************************************************************************/
246 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
247 const char * psz_uri,
248 libvlc_exception_t * p_e )
250 input_item_t * p_input_item;
251 libvlc_media_t * p_md;
253 p_input_item = input_item_NewExt(
254 p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
255 _("Media Library"), 0, NULL, 0, -1 );
259 libvlc_exception_raise( p_e );
260 libvlc_printerr( "Not enough memory" );
264 p_md = libvlc_media_new_from_input_item( p_mlist->p_libvlc_instance,
268 vlc_gc_decref( p_input_item );
272 if( libvlc_media_list_add_media( p_mlist, p_md ) )
274 libvlc_exception_raise( p_e );
278 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item );
283 /**************************************************************************
285 **************************************************************************/
286 void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
287 libvlc_media_t * p_md )
290 vlc_mutex_lock( &p_mlist->object_lock );
291 libvlc_media_release( p_mlist->p_md );
292 libvlc_media_retain( p_md );
293 p_mlist->p_md = p_md;
294 vlc_mutex_unlock( &p_mlist->object_lock );
297 /**************************************************************************
300 * If this media_list comes is a media's subitems,
301 * This holds the corresponding media.
302 * This md is also seen as the information holder for the media_list.
303 * Indeed a media_list can have meta information through this
305 **************************************************************************/
307 libvlc_media_list_media( libvlc_media_list_t * p_mlist )
309 libvlc_media_t *p_md;
311 vlc_mutex_lock( &p_mlist->object_lock );
312 p_md = p_mlist->p_md;
314 libvlc_media_retain( p_md );
315 vlc_mutex_unlock( &p_mlist->object_lock );
320 /**************************************************************************
321 * libvlc_media_list_count (Public)
323 * Lock should be held when entering.
324 **************************************************************************/
325 int libvlc_media_list_count( libvlc_media_list_t * p_mlist )
327 return vlc_array_count( &p_mlist->items );
330 /**************************************************************************
331 * libvlc_media_list_add_media (Public)
333 * Lock should be held when entering.
334 **************************************************************************/
335 int libvlc_media_list_add_media( libvlc_media_list_t * p_mlist,
336 libvlc_media_t * p_md )
338 if( !mlist_is_writable(p_mlist) )
340 _libvlc_media_list_add_media( p_mlist, p_md );
344 /* LibVLC internal version */
345 void _libvlc_media_list_add_media( libvlc_media_list_t * p_mlist,
346 libvlc_media_t * p_md )
348 libvlc_media_retain( p_md );
350 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
352 vlc_array_append( &p_mlist->items, p_md );
353 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
357 /**************************************************************************
358 * libvlc_media_list_insert_media (Public)
360 * Lock should be hold when entering.
361 **************************************************************************/
362 int libvlc_media_list_insert_media( libvlc_media_list_t * p_mlist,
363 libvlc_media_t * p_md,
366 if( !mlist_is_writable(p_mlist) )
368 _libvlc_media_list_insert_media( p_mlist, p_md, index );
372 /* LibVLC internal version */
373 void _libvlc_media_list_insert_media(
374 libvlc_media_list_t * p_mlist,
375 libvlc_media_t * p_md,
378 libvlc_media_retain( p_md );
380 notify_item_addition( p_mlist, p_md, index, EventWillHappen );
381 vlc_array_insert( &p_mlist->items, p_md, index );
382 notify_item_addition( p_mlist, p_md, index, EventDidHappen );
385 /**************************************************************************
386 * libvlc_media_list_remove_index (Public)
388 * Lock should be held when entering.
389 **************************************************************************/
390 int libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
393 if( !mlist_is_writable(p_mlist) )
395 return _libvlc_media_list_remove_index( p_mlist, index );
398 /* LibVLC internal version */
399 int _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
402 libvlc_media_t * p_md;
404 if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
406 libvlc_printerr( "Index out of bounds" );
410 p_md = vlc_array_item_at_index( &p_mlist->items, index );
412 notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
413 vlc_array_remove( &p_mlist->items, index );
414 notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
416 libvlc_media_release( p_md );
420 /**************************************************************************
421 * libvlc_media_list_item_at_index (Public)
423 * Lock should be held when entering.
424 **************************************************************************/
426 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
429 libvlc_media_t * p_md;
431 if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
433 libvlc_printerr( "Index out of bounds" );
437 p_md = vlc_array_item_at_index( &p_mlist->items, index );
438 libvlc_media_retain( p_md );
442 /**************************************************************************
443 * libvlc_media_list_index_of_item (Public)
445 * Lock should be held when entering.
446 * Warning: this function returns the first matching item.
447 **************************************************************************/
448 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
449 libvlc_media_t * p_searched_md )
451 libvlc_media_t * p_md;
453 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
455 p_md = vlc_array_item_at_index( &p_mlist->items, i );
456 if( p_searched_md == p_md )
462 /**************************************************************************
463 * libvlc_media_list_is_readonly (Public)
465 * This indicates if this media list is read-only from a user point of view
466 **************************************************************************/
467 int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
469 return p_mlist->b_read_only;
472 /**************************************************************************
473 * libvlc_media_list_lock (Public)
475 * The lock must be held in access operations. It is never used in the
477 **************************************************************************/
478 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
480 vlc_mutex_lock( &p_mlist->object_lock );
484 /**************************************************************************
485 * libvlc_media_list_unlock (Public)
487 * The lock must be held in access operations
488 **************************************************************************/
489 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
491 vlc_mutex_unlock( &p_mlist->object_lock );
495 /**************************************************************************
496 * libvlc_media_list_p_event_manager (Public)
498 * The p_event_manager is immutable, so you don't have to hold the lock
499 **************************************************************************/
500 libvlc_event_manager_t *
501 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist )
503 return p_mlist->p_event_manager;