Fortunes about QT and "hearing" video codecs
[vlc.git] / lib / media_list.c
blob37c7586b0e8d58bad732e1a8e1689aa246857065
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_picture.h>
32 #include <vlc/libvlc_media.h>
33 #include <vlc/libvlc_media_list.h>
34 #include <vlc/libvlc_events.h>
36 #include <vlc_common.h>
37 #include <vlc_input.h>
39 #include "libvlc_internal.h"
40 #include "media_internal.h" // libvlc_media_new_from_input_item()
41 #include "media_list_internal.h"
43 typedef enum EventPlaceInTime {
44 EventWillHappen,
45 EventDidHappen
46 } EventPlaceInTime;
49 * Private functions
54 /**************************************************************************
55 * notify_item_addition (private)
57 * Do the appropriate action when an item is deleted.
58 **************************************************************************/
59 static void
60 notify_item_addition( libvlc_media_list_t * p_mlist,
61 libvlc_media_t * p_md,
62 int index,
63 EventPlaceInTime event_status )
65 libvlc_event_t event;
67 /* Construct the event */
68 if( event_status == EventDidHappen )
70 event.type = libvlc_MediaListItemAdded;
71 event.u.media_list_item_added.item = p_md;
72 event.u.media_list_item_added.index = index;
74 else /* if( event_status == EventWillHappen ) */
76 event.type = libvlc_MediaListWillAddItem;
77 event.u.media_list_will_add_item.item = p_md;
78 event.u.media_list_will_add_item.index = index;
81 /* Send the event */
82 libvlc_event_send( &p_mlist->event_manager, &event );
85 /**************************************************************************
86 * notify_item_deletion (private)
88 * Do the appropriate action when an item is added.
89 **************************************************************************/
90 static void
91 notify_item_deletion( libvlc_media_list_t * p_mlist,
92 libvlc_media_t * p_md,
93 int index,
94 EventPlaceInTime event_status )
96 libvlc_event_t event;
98 /* Construct the event */
99 if( event_status == EventDidHappen )
101 event.type = libvlc_MediaListItemDeleted;
102 event.u.media_list_item_deleted.item = p_md;
103 event.u.media_list_item_deleted.index = index;
105 else /* if( event_status == EventWillHappen ) */
107 event.type = libvlc_MediaListWillDeleteItem;
108 event.u.media_list_will_delete_item.item = p_md;
109 event.u.media_list_will_delete_item.index = index;
112 /* Send the event */
113 libvlc_event_send( &p_mlist->event_manager, &event );
116 /* LibVLC internal */
117 void libvlc_media_list_internal_end_reached( libvlc_media_list_t * p_mlist )
119 libvlc_event_t event;
121 event.type = libvlc_MediaListEndReached;
123 /* Send the event */
124 libvlc_event_send( &p_mlist->event_manager, &event );
127 /**************************************************************************
128 * static mlist_is_writable (private)
129 **************************************************************************/
130 static inline
131 bool mlist_is_writable( libvlc_media_list_t *p_mlist )
133 if( !p_mlist||p_mlist->b_read_only )
135 /* We are read-only from user side */
136 libvlc_printerr( "Attempt to write a read-only media list" );
137 return false;
139 return true;
143 * Public libvlc functions
146 /**************************************************************************
147 * libvlc_media_list_new (Public)
149 * Init an object.
150 **************************************************************************/
151 libvlc_media_list_t *
152 libvlc_media_list_new( libvlc_instance_t * p_inst )
154 libvlc_media_list_t * p_mlist;
156 p_mlist = malloc(sizeof(libvlc_media_list_t));
157 if( unlikely(p_mlist == NULL) )
159 libvlc_printerr( "Not enough memory" );
160 return NULL;
163 p_mlist->p_libvlc_instance = p_inst;
164 libvlc_event_manager_init( &p_mlist->event_manager, p_mlist );
165 p_mlist->b_read_only = false;
167 vlc_mutex_init( &p_mlist->object_lock );
168 vlc_mutex_init( &p_mlist->refcount_lock ); // FIXME: spinlock?
170 vlc_array_init( &p_mlist->items );
171 assert( p_mlist->items.i_count == 0 );
172 p_mlist->i_refcount = 1;
173 p_mlist->p_md = NULL;
174 p_mlist->p_internal_md = NULL;
176 libvlc_retain( p_inst );
177 return p_mlist;
180 /**************************************************************************
181 * libvlc_media_list_release (Public)
183 * Release an object.
184 **************************************************************************/
185 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
187 vlc_mutex_lock( &p_mlist->refcount_lock );
188 p_mlist->i_refcount--;
189 if( p_mlist->i_refcount > 0 )
191 vlc_mutex_unlock( &p_mlist->refcount_lock );
192 return;
194 vlc_mutex_unlock( &p_mlist->refcount_lock );
196 /* Refcount null, time to free */
198 libvlc_event_manager_destroy( &p_mlist->event_manager );
199 libvlc_media_release( p_mlist->p_md );
201 for( size_t i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
203 libvlc_media_t* p_md = vlc_array_item_at_index( &p_mlist->items, i );
204 libvlc_media_release( p_md );
207 vlc_mutex_destroy( &p_mlist->object_lock );
208 vlc_mutex_destroy( &p_mlist->refcount_lock );
209 vlc_array_clear( &p_mlist->items );
211 libvlc_release( p_mlist->p_libvlc_instance );
212 free( p_mlist );
215 /**************************************************************************
216 * libvlc_media_list_retain (Public)
218 * Increase an object refcount.
219 **************************************************************************/
220 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
222 vlc_mutex_lock( &p_mlist->refcount_lock );
223 p_mlist->i_refcount++;
224 vlc_mutex_unlock( &p_mlist->refcount_lock );
228 /**************************************************************************
229 * add_file_content (Public)
230 **************************************************************************/
232 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
233 const char * psz_uri )
235 input_item_t * p_input_item;
236 libvlc_media_t * p_md;
238 p_input_item = input_item_New( psz_uri, _("Media Library") );
240 if( !p_input_item )
242 libvlc_printerr( "Not enough memory" );
243 return -1;
246 p_md = libvlc_media_new_from_input_item( p_mlist->p_libvlc_instance,
247 p_input_item );
248 if( !p_md )
250 input_item_Release( p_input_item );
251 return -1;
254 if( libvlc_media_list_add_media( p_mlist, p_md ) )
256 #warning Missing error handling!
257 /* printerr and leaks */
258 return -1;
261 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item,
262 NULL, NULL );
263 return 0;
266 /**************************************************************************
267 * set_media (Public)
268 **************************************************************************/
269 void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
270 libvlc_media_t * p_md )
273 vlc_mutex_lock( &p_mlist->object_lock );
274 if( p_mlist->p_internal_md || !mlist_is_writable(p_mlist) )
276 vlc_mutex_unlock( &p_mlist->object_lock );
277 return;
279 libvlc_media_release( p_mlist->p_md );
280 libvlc_media_retain( p_md );
281 p_mlist->p_md = p_md;
282 vlc_mutex_unlock( &p_mlist->object_lock );
285 /**************************************************************************
286 * media (Public)
288 * If this media_list comes is a media's subitems,
289 * This holds the corresponding media.
290 * This md is also seen as the information holder for the media_list.
291 * Indeed a media_list can have meta information through this
292 * media.
293 **************************************************************************/
294 libvlc_media_t *
295 libvlc_media_list_media( libvlc_media_list_t * p_mlist )
297 libvlc_media_t *p_md;
299 vlc_mutex_lock( &p_mlist->object_lock );
300 p_md = p_mlist->p_internal_md ? p_mlist->p_internal_md : p_mlist->p_md;
301 if( p_md )
302 libvlc_media_retain( p_md );
303 vlc_mutex_unlock( &p_mlist->object_lock );
305 return p_md;
308 /**************************************************************************
309 * libvlc_media_list_count (Public)
311 * Lock should be held when entering.
312 **************************************************************************/
313 int libvlc_media_list_count( libvlc_media_list_t * p_mlist )
315 return vlc_array_count( &p_mlist->items );
318 /**************************************************************************
319 * libvlc_media_list_add_media (Public)
321 * Lock should be held when entering.
322 **************************************************************************/
323 int libvlc_media_list_add_media( libvlc_media_list_t * p_mlist,
324 libvlc_media_t * p_md )
326 if( !mlist_is_writable(p_mlist) )
327 return -1;
328 libvlc_media_list_internal_add_media( p_mlist, p_md );
329 return 0;
332 /* LibVLC internal version */
333 void libvlc_media_list_internal_add_media( libvlc_media_list_t * p_mlist,
334 libvlc_media_t * p_md )
336 libvlc_media_retain( p_md );
338 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
339 EventWillHappen );
340 vlc_array_append_or_abort( &p_mlist->items, p_md );
341 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
342 EventDidHappen );
345 /**************************************************************************
346 * libvlc_media_list_insert_media (Public)
348 * Lock should be hold when entering.
349 **************************************************************************/
350 int libvlc_media_list_insert_media( libvlc_media_list_t * p_mlist,
351 libvlc_media_t * p_md,
352 int index )
354 if( !mlist_is_writable(p_mlist) )
355 return -1;
356 libvlc_media_list_internal_insert_media( p_mlist, p_md, index );
357 return 0;
360 /* LibVLC internal version */
361 void libvlc_media_list_internal_insert_media( libvlc_media_list_t * p_mlist,
362 libvlc_media_t * p_md,
363 int index )
365 libvlc_media_retain( p_md );
367 notify_item_addition( p_mlist, p_md, index, EventWillHappen );
368 vlc_array_insert_or_abort( &p_mlist->items, p_md, index );
369 notify_item_addition( p_mlist, p_md, index, EventDidHappen );
372 /**************************************************************************
373 * libvlc_media_list_remove_index (Public)
375 * Lock should be held when entering.
376 **************************************************************************/
377 int libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
378 int index )
380 if( !mlist_is_writable(p_mlist) )
381 return -1;
382 return libvlc_media_list_internal_remove_index( p_mlist, index );
385 /* LibVLC internal version */
386 int libvlc_media_list_internal_remove_index( libvlc_media_list_t * p_mlist,
387 int index )
389 libvlc_media_t * p_md;
391 if( (size_t) index >= vlc_array_count( &p_mlist->items ))
393 libvlc_printerr( "Index out of bounds" );
394 return -1;
397 p_md = vlc_array_item_at_index( &p_mlist->items, index );
399 notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
400 vlc_array_remove( &p_mlist->items, index );
401 notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
403 libvlc_media_release( p_md );
404 return 0;
407 /**************************************************************************
408 * libvlc_media_list_item_at_index (Public)
410 * Lock should be held when entering.
411 **************************************************************************/
412 libvlc_media_t *
413 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
414 int index )
416 libvlc_media_t * p_md;
418 if( (size_t) index >= vlc_array_count( &p_mlist->items ))
420 libvlc_printerr( "Index out of bounds" );
421 return NULL;
424 p_md = vlc_array_item_at_index( &p_mlist->items, index );
425 libvlc_media_retain( p_md );
426 return p_md;
429 /**************************************************************************
430 * libvlc_media_list_index_of_item (Public)
432 * Lock should be held when entering.
433 * Warning: this function returns the first matching item.
434 **************************************************************************/
435 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
436 libvlc_media_t * p_searched_md )
438 int idx = vlc_array_index_of_item( &p_mlist->items, p_searched_md );
439 if( idx == -1 )
440 libvlc_printerr( "Media not found" );
442 return idx;
445 /**************************************************************************
446 * libvlc_media_list_is_readonly (Public)
448 * This indicates if this media list is read-only from a user point of view
449 **************************************************************************/
450 int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
452 return p_mlist->b_read_only;
455 /**************************************************************************
456 * libvlc_media_list_lock (Public)
458 * The lock must be held in access operations. It is never used in the
459 * Public method.
460 **************************************************************************/
461 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
463 vlc_mutex_lock( &p_mlist->object_lock );
467 /**************************************************************************
468 * libvlc_media_list_unlock (Public)
470 * The lock must be held in access operations
471 **************************************************************************/
472 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
474 vlc_mutex_unlock( &p_mlist->object_lock );
478 /**************************************************************************
479 * libvlc_media_list_event_manager (Public)
481 * The p_event_manager is immutable, so you don't have to hold the lock
482 **************************************************************************/
483 libvlc_event_manager_t *
484 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist )
486 return &p_mlist->event_manager;