Update NEWS with new supported codecs.
[vlc/solaris.git] / lib / media_list.c
blob749d3c5a2013013a9485b009c9b7bf4f9fa701b5
1 /*****************************************************************************
2 * media_list.c: libvlc new API media list functions
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
5 * $Id$
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
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 {
43 EventWillHappen,
44 EventDidHappen
45 } EventPlaceInTime;
47 //#define DEBUG_MEDIA_LIST
49 #ifdef DEBUG_MEDIA_LIST
50 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
51 #else
52 # define trace( ... )
53 #endif
56 * Private functions
61 /**************************************************************************
62 * notify_item_addition (private)
64 * Do the appropriate action when an item is deleted.
65 **************************************************************************/
66 static void
67 notify_item_addition( libvlc_media_list_t * p_mlist,
68 libvlc_media_t * p_md,
69 int index,
70 EventPlaceInTime event_status )
72 libvlc_event_t event;
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;
89 /* Send the event */
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 **************************************************************************/
98 static void
99 notify_item_deletion( libvlc_media_list_t * p_mlist,
100 libvlc_media_t * p_md,
101 int index,
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;
121 /* Send the event */
122 libvlc_event_send( p_mlist->p_event_manager, &event );
125 /**************************************************************************
126 * static mlist_is_writable (private)
127 **************************************************************************/
128 static inline
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" );
135 return false;
137 return true;
141 * Public libvlc functions
144 /**************************************************************************
145 * libvlc_media_list_new (Public)
147 * Init an object.
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" );
158 return NULL;
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) )
165 free(p_mlist);
166 return 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 assert( p_mlist->items.i_count == 0 );
187 p_mlist->i_refcount = 1;
188 p_mlist->p_md = NULL;
190 return p_mlist;
193 /**************************************************************************
194 * libvlc_media_list_release (Public)
196 * Release an object.
197 **************************************************************************/
198 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
200 libvlc_media_t * p_md;
201 int i;
203 vlc_mutex_lock( &p_mlist->refcount_lock );
204 p_mlist->i_refcount--;
205 if( p_mlist->i_refcount > 0 )
207 vlc_mutex_unlock( &p_mlist->refcount_lock );
208 return;
210 vlc_mutex_unlock( &p_mlist->refcount_lock );
212 /* Refcount null, time to free */
214 libvlc_event_manager_release( p_mlist->p_event_manager );
216 libvlc_media_release( p_mlist->p_md );
218 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
220 p_md = vlc_array_item_at_index( &p_mlist->items, i );
221 libvlc_media_release( p_md );
224 vlc_mutex_destroy( &p_mlist->object_lock );
225 vlc_array_clear( &p_mlist->items );
227 free( p_mlist );
230 /**************************************************************************
231 * libvlc_media_list_retain (Public)
233 * Increase an object refcount.
234 **************************************************************************/
235 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
237 vlc_mutex_lock( &p_mlist->refcount_lock );
238 p_mlist->i_refcount++;
239 vlc_mutex_unlock( &p_mlist->refcount_lock );
243 /**************************************************************************
244 * add_file_content (Public)
245 **************************************************************************/
247 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
248 const char * psz_uri )
250 input_item_t * p_input_item;
251 libvlc_media_t * p_md;
253 p_input_item = input_item_NewExt( psz_uri,
254 _("Media Library"), 0, NULL, 0, -1 );
256 if( !p_input_item )
258 libvlc_printerr( "Not enough memory" );
259 return -1;
262 p_md = libvlc_media_new_from_input_item( p_mlist->p_libvlc_instance,
263 p_input_item );
264 if( !p_md )
266 vlc_gc_decref( p_input_item );
267 return -1;
270 if( libvlc_media_list_add_media( p_mlist, p_md ) )
272 #warning Missing error handling!
273 /* printerr and leaks */
274 return -1;
277 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item );
278 return 0;
281 /**************************************************************************
282 * set_media (Public)
283 **************************************************************************/
284 void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
285 libvlc_media_t * p_md )
288 vlc_mutex_lock( &p_mlist->object_lock );
289 libvlc_media_release( p_mlist->p_md );
290 libvlc_media_retain( p_md );
291 p_mlist->p_md = p_md;
292 vlc_mutex_unlock( &p_mlist->object_lock );
295 /**************************************************************************
296 * media (Public)
298 * If this media_list comes is a media's subitems,
299 * This holds the corresponding media.
300 * This md is also seen as the information holder for the media_list.
301 * Indeed a media_list can have meta information through this
302 * media.
303 **************************************************************************/
304 libvlc_media_t *
305 libvlc_media_list_media( libvlc_media_list_t * p_mlist )
307 libvlc_media_t *p_md;
309 vlc_mutex_lock( &p_mlist->object_lock );
310 p_md = p_mlist->p_md;
311 if( p_md )
312 libvlc_media_retain( p_md );
313 vlc_mutex_unlock( &p_mlist->object_lock );
315 return p_md;
318 /**************************************************************************
319 * libvlc_media_list_count (Public)
321 * Lock should be held when entering.
322 **************************************************************************/
323 int libvlc_media_list_count( libvlc_media_list_t * p_mlist )
325 return vlc_array_count( &p_mlist->items );
328 /**************************************************************************
329 * libvlc_media_list_add_media (Public)
331 * Lock should be held when entering.
332 **************************************************************************/
333 int libvlc_media_list_add_media( libvlc_media_list_t * p_mlist,
334 libvlc_media_t * p_md )
336 if( !mlist_is_writable(p_mlist) )
337 return -1;
338 _libvlc_media_list_add_media( p_mlist, p_md );
339 return 0;
342 /* LibVLC internal version */
343 void _libvlc_media_list_add_media( libvlc_media_list_t * p_mlist,
344 libvlc_media_t * p_md )
346 libvlc_media_retain( p_md );
348 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
349 EventWillHappen );
350 vlc_array_append( &p_mlist->items, p_md );
351 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
352 EventDidHappen );
355 /**************************************************************************
356 * libvlc_media_list_insert_media (Public)
358 * Lock should be hold when entering.
359 **************************************************************************/
360 int libvlc_media_list_insert_media( libvlc_media_list_t * p_mlist,
361 libvlc_media_t * p_md,
362 int index )
364 if( !mlist_is_writable(p_mlist) )
365 return -1;
366 _libvlc_media_list_insert_media( p_mlist, p_md, index );
367 return 0;
370 /* LibVLC internal version */
371 void _libvlc_media_list_insert_media(
372 libvlc_media_list_t * p_mlist,
373 libvlc_media_t * p_md,
374 int index )
376 libvlc_media_retain( p_md );
378 notify_item_addition( p_mlist, p_md, index, EventWillHappen );
379 vlc_array_insert( &p_mlist->items, p_md, index );
380 notify_item_addition( p_mlist, p_md, index, EventDidHappen );
383 /**************************************************************************
384 * libvlc_media_list_remove_index (Public)
386 * Lock should be held when entering.
387 **************************************************************************/
388 int libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
389 int index )
391 if( !mlist_is_writable(p_mlist) )
392 return -1;
393 return _libvlc_media_list_remove_index( p_mlist, index );
396 /* LibVLC internal version */
397 int _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
398 int index )
400 libvlc_media_t * p_md;
402 if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
404 libvlc_printerr( "Index out of bounds" );
405 return -1;
408 p_md = vlc_array_item_at_index( &p_mlist->items, index );
410 notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
411 vlc_array_remove( &p_mlist->items, index );
412 notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
414 libvlc_media_release( p_md );
415 return 0;
418 /**************************************************************************
419 * libvlc_media_list_item_at_index (Public)
421 * Lock should be held when entering.
422 **************************************************************************/
423 libvlc_media_t *
424 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
425 int index )
427 libvlc_media_t * p_md;
429 if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
431 libvlc_printerr( "Index out of bounds" );
432 return NULL;
435 p_md = vlc_array_item_at_index( &p_mlist->items, index );
436 libvlc_media_retain( p_md );
437 return p_md;
440 /**************************************************************************
441 * libvlc_media_list_index_of_item (Public)
443 * Lock should be held when entering.
444 * Warning: this function returns the first matching item.
445 **************************************************************************/
446 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
447 libvlc_media_t * p_searched_md )
449 libvlc_media_t * p_md;
450 int i;
451 for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
453 p_md = vlc_array_item_at_index( &p_mlist->items, i );
454 if( p_searched_md == p_md )
455 return i;
457 libvlc_printerr( "Media not found" );
458 return -1;
461 /**************************************************************************
462 * libvlc_media_list_is_readonly (Public)
464 * This indicates if this media list is read-only from a user point of view
465 **************************************************************************/
466 int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
468 return p_mlist->b_read_only;
471 /**************************************************************************
472 * libvlc_media_list_lock (Public)
474 * The lock must be held in access operations. It is never used in the
475 * Public method.
476 **************************************************************************/
477 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
479 vlc_mutex_lock( &p_mlist->object_lock );
483 /**************************************************************************
484 * libvlc_media_list_unlock (Public)
486 * The lock must be held in access operations
487 **************************************************************************/
488 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
490 vlc_mutex_unlock( &p_mlist->object_lock );
494 /**************************************************************************
495 * libvlc_media_list_p_event_manager (Public)
497 * The p_event_manager is immutable, so you don't have to hold the lock
498 **************************************************************************/
499 libvlc_event_manager_t *
500 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist )
502 return p_mlist->p_event_manager;