qml: history.push changes view by default
[vlc.git] / lib / media_list.c
blobc4584c3a2085c0b22196f393c78251d1603bf112
1 /*****************************************************************************
2 * media_list.c: libvlc new API media list functions
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
6 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <assert.h>
29 #include <vlc/libvlc.h>
30 #include <vlc/libvlc_picture.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>
37 #include "libvlc_internal.h"
38 #include "media_internal.h" // libvlc_media_new_from_input_item()
39 #include "media_list_internal.h"
41 typedef enum EventPlaceInTime {
42 EventWillHappen,
43 EventDidHappen
44 } EventPlaceInTime;
47 * Private functions
52 /**************************************************************************
53 * notify_item_addition (private)
55 * Do the appropriate action when an item is deleted.
56 **************************************************************************/
57 static void
58 notify_item_addition( libvlc_media_list_t * p_mlist,
59 libvlc_media_t * p_md,
60 int index,
61 EventPlaceInTime event_status )
63 libvlc_event_t event;
65 /* Construct the event */
66 if( event_status == EventDidHappen )
68 event.type = libvlc_MediaListItemAdded;
69 event.u.media_list_item_added.item = p_md;
70 event.u.media_list_item_added.index = index;
72 else /* if( event_status == EventWillHappen ) */
74 event.type = libvlc_MediaListWillAddItem;
75 event.u.media_list_will_add_item.item = p_md;
76 event.u.media_list_will_add_item.index = index;
79 /* Send the event */
80 libvlc_event_send( &p_mlist->event_manager, &event );
83 /**************************************************************************
84 * notify_item_deletion (private)
86 * Do the appropriate action when an item is added.
87 **************************************************************************/
88 static void
89 notify_item_deletion( libvlc_media_list_t * p_mlist,
90 libvlc_media_t * p_md,
91 int index,
92 EventPlaceInTime event_status )
94 libvlc_event_t event;
96 /* Construct the event */
97 if( event_status == EventDidHappen )
99 event.type = libvlc_MediaListItemDeleted;
100 event.u.media_list_item_deleted.item = p_md;
101 event.u.media_list_item_deleted.index = index;
103 else /* if( event_status == EventWillHappen ) */
105 event.type = libvlc_MediaListWillDeleteItem;
106 event.u.media_list_will_delete_item.item = p_md;
107 event.u.media_list_will_delete_item.index = index;
110 /* Send the event */
111 libvlc_event_send( &p_mlist->event_manager, &event );
114 /* LibVLC internal */
115 void libvlc_media_list_internal_end_reached( libvlc_media_list_t * p_mlist )
117 libvlc_event_t event;
119 event.type = libvlc_MediaListEndReached;
121 /* Send the event */
122 libvlc_event_send( &p_mlist->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 libvlc_event_manager_init( &p_mlist->event_manager, p_mlist );
163 p_mlist->b_read_only = false;
165 vlc_mutex_init( &p_mlist->object_lock );
166 vlc_mutex_init( &p_mlist->refcount_lock ); // FIXME: spinlock?
168 vlc_array_init( &p_mlist->items );
169 assert( p_mlist->items.i_count == 0 );
170 p_mlist->i_refcount = 1;
171 p_mlist->p_md = NULL;
172 p_mlist->p_internal_md = NULL;
174 libvlc_retain( p_inst );
175 return p_mlist;
178 /**************************************************************************
179 * libvlc_media_list_release (Public)
181 * Release an object.
182 **************************************************************************/
183 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
185 vlc_mutex_lock( &p_mlist->refcount_lock );
186 p_mlist->i_refcount--;
187 if( p_mlist->i_refcount > 0 )
189 vlc_mutex_unlock( &p_mlist->refcount_lock );
190 return;
192 vlc_mutex_unlock( &p_mlist->refcount_lock );
194 /* Refcount null, time to free */
196 libvlc_event_manager_destroy( &p_mlist->event_manager );
197 libvlc_media_release( p_mlist->p_md );
199 for( size_t i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
201 libvlc_media_t* p_md = vlc_array_item_at_index( &p_mlist->items, i );
202 libvlc_media_release( p_md );
205 vlc_mutex_destroy( &p_mlist->object_lock );
206 vlc_mutex_destroy( &p_mlist->refcount_lock );
207 vlc_array_clear( &p_mlist->items );
209 libvlc_release( p_mlist->p_libvlc_instance );
210 free( p_mlist );
213 /**************************************************************************
214 * libvlc_media_list_retain (Public)
216 * Increase an object refcount.
217 **************************************************************************/
218 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
220 vlc_mutex_lock( &p_mlist->refcount_lock );
221 p_mlist->i_refcount++;
222 vlc_mutex_unlock( &p_mlist->refcount_lock );
225 /**************************************************************************
226 * set_media (Public)
227 **************************************************************************/
228 void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
229 libvlc_media_t * p_md )
232 vlc_mutex_lock( &p_mlist->object_lock );
233 if( p_mlist->p_internal_md || !mlist_is_writable(p_mlist) )
235 vlc_mutex_unlock( &p_mlist->object_lock );
236 return;
238 libvlc_media_release( p_mlist->p_md );
239 libvlc_media_retain( p_md );
240 p_mlist->p_md = p_md;
241 vlc_mutex_unlock( &p_mlist->object_lock );
244 /**************************************************************************
245 * media (Public)
247 * If this media_list comes is a media's subitems,
248 * This holds the corresponding media.
249 * This md is also seen as the information holder for the media_list.
250 * Indeed a media_list can have meta information through this
251 * media.
252 **************************************************************************/
253 libvlc_media_t *
254 libvlc_media_list_media( libvlc_media_list_t * p_mlist )
256 libvlc_media_t *p_md;
258 vlc_mutex_lock( &p_mlist->object_lock );
259 p_md = p_mlist->p_internal_md ? p_mlist->p_internal_md : p_mlist->p_md;
260 if( p_md )
261 libvlc_media_retain( p_md );
262 vlc_mutex_unlock( &p_mlist->object_lock );
264 return p_md;
267 /**************************************************************************
268 * libvlc_media_list_count (Public)
270 * Lock should be held when entering.
271 **************************************************************************/
272 int libvlc_media_list_count( libvlc_media_list_t * p_mlist )
274 return vlc_array_count( &p_mlist->items );
277 /**************************************************************************
278 * libvlc_media_list_add_media (Public)
280 * Lock should be held when entering.
281 **************************************************************************/
282 int libvlc_media_list_add_media( libvlc_media_list_t * p_mlist,
283 libvlc_media_t * p_md )
285 if( !mlist_is_writable(p_mlist) )
286 return -1;
287 libvlc_media_list_internal_add_media( p_mlist, p_md );
288 return 0;
291 /* LibVLC internal version */
292 void libvlc_media_list_internal_add_media( libvlc_media_list_t * p_mlist,
293 libvlc_media_t * p_md )
295 libvlc_media_retain( p_md );
297 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
298 EventWillHappen );
299 vlc_array_append_or_abort( &p_mlist->items, p_md );
300 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
301 EventDidHappen );
304 /**************************************************************************
305 * libvlc_media_list_insert_media (Public)
307 * Lock should be hold when entering.
308 **************************************************************************/
309 int libvlc_media_list_insert_media( libvlc_media_list_t * p_mlist,
310 libvlc_media_t * p_md,
311 int index )
313 if( !mlist_is_writable(p_mlist) )
314 return -1;
315 libvlc_media_list_internal_insert_media( p_mlist, p_md, index );
316 return 0;
319 /* LibVLC internal version */
320 void libvlc_media_list_internal_insert_media( libvlc_media_list_t * p_mlist,
321 libvlc_media_t * p_md,
322 int index )
324 libvlc_media_retain( p_md );
326 notify_item_addition( p_mlist, p_md, index, EventWillHappen );
327 vlc_array_insert_or_abort( &p_mlist->items, p_md, index );
328 notify_item_addition( p_mlist, p_md, index, EventDidHappen );
331 /**************************************************************************
332 * libvlc_media_list_remove_index (Public)
334 * Lock should be held when entering.
335 **************************************************************************/
336 int libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
337 int index )
339 if( !mlist_is_writable(p_mlist) )
340 return -1;
341 return libvlc_media_list_internal_remove_index( p_mlist, index );
344 /* LibVLC internal version */
345 int libvlc_media_list_internal_remove_index( libvlc_media_list_t * p_mlist,
346 int index )
348 libvlc_media_t * p_md;
350 if( (size_t) index >= vlc_array_count( &p_mlist->items ))
352 libvlc_printerr( "Index out of bounds" );
353 return -1;
356 p_md = vlc_array_item_at_index( &p_mlist->items, index );
358 notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
359 vlc_array_remove( &p_mlist->items, index );
360 notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
362 libvlc_media_release( p_md );
363 return 0;
366 /**************************************************************************
367 * libvlc_media_list_item_at_index (Public)
369 * Lock should be held when entering.
370 **************************************************************************/
371 libvlc_media_t *
372 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
373 int index )
375 libvlc_media_t * p_md;
377 if( (size_t) index >= vlc_array_count( &p_mlist->items ))
379 libvlc_printerr( "Index out of bounds" );
380 return NULL;
383 p_md = vlc_array_item_at_index( &p_mlist->items, index );
384 libvlc_media_retain( p_md );
385 return p_md;
388 /**************************************************************************
389 * libvlc_media_list_index_of_item (Public)
391 * Lock should be held when entering.
392 * Warning: this function returns the first matching item.
393 **************************************************************************/
394 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
395 libvlc_media_t * p_searched_md )
397 int idx = vlc_array_index_of_item( &p_mlist->items, p_searched_md );
398 if( idx == -1 )
399 libvlc_printerr( "Media not found" );
401 return idx;
404 /**************************************************************************
405 * libvlc_media_list_is_readonly (Public)
407 * This indicates if this media list is read-only from a user point of view
408 **************************************************************************/
409 bool libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
411 return p_mlist->b_read_only;
414 /**************************************************************************
415 * libvlc_media_list_lock (Public)
417 * The lock must be held in access operations. It is never used in the
418 * Public method.
419 **************************************************************************/
420 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
422 vlc_mutex_lock( &p_mlist->object_lock );
426 /**************************************************************************
427 * libvlc_media_list_unlock (Public)
429 * The lock must be held in access operations
430 **************************************************************************/
431 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
433 vlc_mutex_unlock( &p_mlist->object_lock );
437 /**************************************************************************
438 * libvlc_media_list_event_manager (Public)
440 * The p_event_manager is immutable, so you don't have to hold the lock
441 **************************************************************************/
442 libvlc_event_manager_t *
443 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist )
445 return &p_mlist->event_manager;