lib: media: Fix memory leak on error case.
[vlc.git] / lib / media_list.c
blob2c5680a74cdb4a936658d2a08e456f78e5759fc3
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;
48 * Private functions
53 /**************************************************************************
54 * notify_item_addition (private)
56 * Do the appropriate action when an item is deleted.
57 **************************************************************************/
58 static void
59 notify_item_addition( libvlc_media_list_t * p_mlist,
60 libvlc_media_t * p_md,
61 int index,
62 EventPlaceInTime event_status )
64 libvlc_event_t event;
66 /* Construct the event */
67 if( event_status == EventDidHappen )
69 event.type = libvlc_MediaListItemAdded;
70 event.u.media_list_item_added.item = p_md;
71 event.u.media_list_item_added.index = index;
73 else /* if( event_status == EventWillHappen ) */
75 event.type = libvlc_MediaListWillAddItem;
76 event.u.media_list_will_add_item.item = p_md;
77 event.u.media_list_will_add_item.index = index;
80 /* Send the event */
81 libvlc_event_send( p_mlist->p_event_manager, &event );
84 /**************************************************************************
85 * notify_item_deletion (private)
87 * Do the appropriate action when an item is added.
88 **************************************************************************/
89 static void
90 notify_item_deletion( libvlc_media_list_t * p_mlist,
91 libvlc_media_t * p_md,
92 int index,
93 EventPlaceInTime event_status )
95 libvlc_event_t event;
97 /* Construct the event */
98 if( event_status == EventDidHappen )
100 event.type = libvlc_MediaListItemDeleted;
101 event.u.media_list_item_deleted.item = p_md;
102 event.u.media_list_item_deleted.index = index;
104 else /* if( event_status == EventWillHappen ) */
106 event.type = libvlc_MediaListWillDeleteItem;
107 event.u.media_list_will_delete_item.item = p_md;
108 event.u.media_list_will_delete_item.index = index;
111 /* Send the event */
112 libvlc_event_send( p_mlist->p_event_manager, &event );
115 /* LibVLC internal */
116 void libvlc_media_list_internal_end_reached( libvlc_media_list_t * p_mlist )
118 libvlc_event_t event;
120 event.type = libvlc_MediaListEndReached;
122 /* Send the event */
123 libvlc_event_send( p_mlist->p_event_manager, &event );
126 /**************************************************************************
127 * static mlist_is_writable (private)
128 **************************************************************************/
129 static inline
130 bool mlist_is_writable( libvlc_media_list_t *p_mlist )
132 if( !p_mlist||p_mlist->b_read_only )
134 /* We are read-only from user side */
135 libvlc_printerr( "Attempt to write a read-only media list" );
136 return false;
138 return true;
142 * Public libvlc functions
145 /**************************************************************************
146 * libvlc_media_list_new (Public)
148 * Init an object.
149 **************************************************************************/
150 libvlc_media_list_t *
151 libvlc_media_list_new( libvlc_instance_t * p_inst )
153 libvlc_media_list_t * p_mlist;
155 p_mlist = malloc(sizeof(libvlc_media_list_t));
156 if( unlikely(p_mlist == NULL) )
158 libvlc_printerr( "Not enough memory" );
159 return NULL;
162 p_mlist->p_libvlc_instance = p_inst;
163 p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist );
164 if( unlikely(p_mlist->p_event_manager == NULL) )
166 free(p_mlist);
167 return NULL;
170 p_mlist->b_read_only = false;
172 vlc_mutex_init( &p_mlist->object_lock );
173 vlc_mutex_init( &p_mlist->refcount_lock ); // FIXME: spinlock?
175 vlc_array_init( &p_mlist->items );
176 assert( p_mlist->items.i_count == 0 );
177 p_mlist->i_refcount = 1;
178 p_mlist->p_md = NULL;
179 p_mlist->p_internal_md = NULL;
181 libvlc_retain( p_inst );
182 return p_mlist;
185 /**************************************************************************
186 * libvlc_media_list_release (Public)
188 * Release an object.
189 **************************************************************************/
190 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
192 vlc_mutex_lock( &p_mlist->refcount_lock );
193 p_mlist->i_refcount--;
194 if( p_mlist->i_refcount > 0 )
196 vlc_mutex_unlock( &p_mlist->refcount_lock );
197 return;
199 vlc_mutex_unlock( &p_mlist->refcount_lock );
201 /* Refcount null, time to free */
203 libvlc_event_manager_release( p_mlist->p_event_manager );
205 libvlc_media_release( p_mlist->p_md );
207 for ( int i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
209 libvlc_media_t* 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_mutex_destroy( &p_mlist->refcount_lock );
215 vlc_array_clear( &p_mlist->items );
217 libvlc_release( p_mlist->p_libvlc_instance );
218 free( p_mlist );
221 /**************************************************************************
222 * libvlc_media_list_retain (Public)
224 * Increase an object refcount.
225 **************************************************************************/
226 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
228 vlc_mutex_lock( &p_mlist->refcount_lock );
229 p_mlist->i_refcount++;
230 vlc_mutex_unlock( &p_mlist->refcount_lock );
234 /**************************************************************************
235 * add_file_content (Public)
236 **************************************************************************/
238 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
239 const char * psz_uri )
241 input_item_t * p_input_item;
242 libvlc_media_t * p_md;
244 p_input_item = input_item_New( psz_uri, _("Media Library") );
246 if( !p_input_item )
248 libvlc_printerr( "Not enough memory" );
249 return -1;
252 p_md = libvlc_media_new_from_input_item( p_mlist->p_libvlc_instance,
253 p_input_item );
254 if( !p_md )
256 vlc_gc_decref( p_input_item );
257 return -1;
260 if( libvlc_media_list_add_media( p_mlist, p_md ) )
262 #warning Missing error handling!
263 /* printerr and leaks */
264 return -1;
267 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item );
268 return 0;
271 /**************************************************************************
272 * set_media (Public)
273 **************************************************************************/
274 void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
275 libvlc_media_t * p_md )
278 vlc_mutex_lock( &p_mlist->object_lock );
279 if( p_mlist->p_internal_md || !mlist_is_writable(p_mlist) )
281 vlc_mutex_unlock( &p_mlist->object_lock );
282 return;
284 libvlc_media_release( p_mlist->p_md );
285 libvlc_media_retain( p_md );
286 p_mlist->p_md = p_md;
287 vlc_mutex_unlock( &p_mlist->object_lock );
290 /**************************************************************************
291 * media (Public)
293 * If this media_list comes is a media's subitems,
294 * This holds the corresponding media.
295 * This md is also seen as the information holder for the media_list.
296 * Indeed a media_list can have meta information through this
297 * media.
298 **************************************************************************/
299 libvlc_media_t *
300 libvlc_media_list_media( libvlc_media_list_t * p_mlist )
302 libvlc_media_t *p_md;
304 vlc_mutex_lock( &p_mlist->object_lock );
305 p_md = p_mlist->p_internal_md ? p_mlist->p_internal_md : p_mlist->p_md;
306 if( p_md )
307 libvlc_media_retain( p_md );
308 vlc_mutex_unlock( &p_mlist->object_lock );
310 return p_md;
313 /**************************************************************************
314 * libvlc_media_list_count (Public)
316 * Lock should be held when entering.
317 **************************************************************************/
318 int libvlc_media_list_count( libvlc_media_list_t * p_mlist )
320 return vlc_array_count( &p_mlist->items );
323 /**************************************************************************
324 * libvlc_media_list_add_media (Public)
326 * Lock should be held when entering.
327 **************************************************************************/
328 int libvlc_media_list_add_media( libvlc_media_list_t * p_mlist,
329 libvlc_media_t * p_md )
331 if( !mlist_is_writable(p_mlist) )
332 return -1;
333 libvlc_media_list_internal_add_media( p_mlist, p_md );
334 return 0;
337 /* LibVLC internal version */
338 void libvlc_media_list_internal_add_media( libvlc_media_list_t * p_mlist,
339 libvlc_media_t * p_md )
341 libvlc_media_retain( p_md );
343 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
344 EventWillHappen );
345 vlc_array_append( &p_mlist->items, p_md );
346 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
347 EventDidHappen );
350 /**************************************************************************
351 * libvlc_media_list_insert_media (Public)
353 * Lock should be hold when entering.
354 **************************************************************************/
355 int libvlc_media_list_insert_media( libvlc_media_list_t * p_mlist,
356 libvlc_media_t * p_md,
357 int index )
359 if( !mlist_is_writable(p_mlist) )
360 return -1;
361 libvlc_media_list_internal_insert_media( p_mlist, p_md, index );
362 return 0;
365 /* LibVLC internal version */
366 void libvlc_media_list_internal_insert_media( libvlc_media_list_t * p_mlist,
367 libvlc_media_t * p_md,
368 int index )
370 libvlc_media_retain( p_md );
372 notify_item_addition( p_mlist, p_md, index, EventWillHappen );
373 vlc_array_insert( &p_mlist->items, p_md, index );
374 notify_item_addition( p_mlist, p_md, index, EventDidHappen );
377 /**************************************************************************
378 * libvlc_media_list_remove_index (Public)
380 * Lock should be held when entering.
381 **************************************************************************/
382 int libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
383 int index )
385 if( !mlist_is_writable(p_mlist) )
386 return -1;
387 return libvlc_media_list_internal_remove_index( p_mlist, index );
390 /* LibVLC internal version */
391 int libvlc_media_list_internal_remove_index( libvlc_media_list_t * p_mlist,
392 int index )
394 libvlc_media_t * p_md;
396 if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
398 libvlc_printerr( "Index out of bounds" );
399 return -1;
402 p_md = vlc_array_item_at_index( &p_mlist->items, index );
404 notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
405 vlc_array_remove( &p_mlist->items, index );
406 notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
408 libvlc_media_release( p_md );
409 return 0;
412 /**************************************************************************
413 * libvlc_media_list_item_at_index (Public)
415 * Lock should be held when entering.
416 **************************************************************************/
417 libvlc_media_t *
418 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
419 int index )
421 libvlc_media_t * p_md;
423 if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
425 libvlc_printerr( "Index out of bounds" );
426 return NULL;
429 p_md = vlc_array_item_at_index( &p_mlist->items, index );
430 libvlc_media_retain( p_md );
431 return p_md;
434 /**************************************************************************
435 * libvlc_media_list_index_of_item (Public)
437 * Lock should be held when entering.
438 * Warning: this function returns the first matching item.
439 **************************************************************************/
440 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
441 libvlc_media_t * p_searched_md )
443 int idx = vlc_array_index_of_item( &p_mlist->items, p_searched_md );
444 if( idx == -1 )
445 libvlc_printerr( "Media not found" );
447 return idx;
450 /**************************************************************************
451 * libvlc_media_list_is_readonly (Public)
453 * This indicates if this media list is read-only from a user point of view
454 **************************************************************************/
455 int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
457 return p_mlist->b_read_only;
460 /**************************************************************************
461 * libvlc_media_list_lock (Public)
463 * The lock must be held in access operations. It is never used in the
464 * Public method.
465 **************************************************************************/
466 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
468 vlc_mutex_lock( &p_mlist->object_lock );
472 /**************************************************************************
473 * libvlc_media_list_unlock (Public)
475 * The lock must be held in access operations
476 **************************************************************************/
477 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
479 vlc_mutex_unlock( &p_mlist->object_lock );
483 /**************************************************************************
484 * libvlc_media_list_p_event_manager (Public)
486 * The p_event_manager is immutable, so you don't have to hold the lock
487 **************************************************************************/
488 libvlc_event_manager_t *
489 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist )
491 return p_mlist->p_event_manager;