cli: move code
[vlc.git] / lib / media_list.c
blobc747778d47f451e32c822f4f7ea170ac8c3c6f1f
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 *libvlc_media_list_new(void)
151 libvlc_media_list_t * p_mlist;
153 p_mlist = malloc(sizeof(libvlc_media_list_t));
154 if( unlikely(p_mlist == NULL) )
156 libvlc_printerr( "Not enough memory" );
157 return NULL;
160 libvlc_event_manager_init( &p_mlist->event_manager, p_mlist );
161 p_mlist->b_read_only = false;
163 vlc_mutex_init( &p_mlist->object_lock );
164 vlc_mutex_init( &p_mlist->refcount_lock ); // FIXME: spinlock?
166 vlc_array_init( &p_mlist->items );
167 assert( p_mlist->items.i_count == 0 );
168 p_mlist->i_refcount = 1;
169 p_mlist->p_md = NULL;
170 p_mlist->p_internal_md = NULL;
172 return p_mlist;
175 /**************************************************************************
176 * libvlc_media_list_release (Public)
178 * Release an object.
179 **************************************************************************/
180 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
182 vlc_mutex_lock( &p_mlist->refcount_lock );
183 p_mlist->i_refcount--;
184 if( p_mlist->i_refcount > 0 )
186 vlc_mutex_unlock( &p_mlist->refcount_lock );
187 return;
189 vlc_mutex_unlock( &p_mlist->refcount_lock );
191 /* Refcount null, time to free */
193 libvlc_event_manager_destroy( &p_mlist->event_manager );
194 libvlc_media_release( p_mlist->p_md );
196 for( size_t i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
198 libvlc_media_t* p_md = vlc_array_item_at_index( &p_mlist->items, i );
199 libvlc_media_release( p_md );
202 vlc_array_clear( &p_mlist->items );
204 free( p_mlist );
207 /**************************************************************************
208 * libvlc_media_list_retain (Public)
210 * Increase an object refcount.
211 **************************************************************************/
212 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
214 vlc_mutex_lock( &p_mlist->refcount_lock );
215 p_mlist->i_refcount++;
216 vlc_mutex_unlock( &p_mlist->refcount_lock );
219 /**************************************************************************
220 * set_media (Public)
221 **************************************************************************/
222 void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
223 libvlc_media_t * p_md )
226 vlc_mutex_lock( &p_mlist->object_lock );
227 if( p_mlist->p_internal_md || !mlist_is_writable(p_mlist) )
229 vlc_mutex_unlock( &p_mlist->object_lock );
230 return;
232 libvlc_media_release( p_mlist->p_md );
233 libvlc_media_retain( p_md );
234 p_mlist->p_md = p_md;
235 vlc_mutex_unlock( &p_mlist->object_lock );
238 /**************************************************************************
239 * media (Public)
241 * If this media_list comes is a media's subitems,
242 * This holds the corresponding media.
243 * This md is also seen as the information holder for the media_list.
244 * Indeed a media_list can have meta information through this
245 * media.
246 **************************************************************************/
247 libvlc_media_t *
248 libvlc_media_list_media( libvlc_media_list_t * p_mlist )
250 libvlc_media_t *p_md;
252 vlc_mutex_lock( &p_mlist->object_lock );
253 p_md = p_mlist->p_internal_md ? p_mlist->p_internal_md : p_mlist->p_md;
254 if( p_md )
255 libvlc_media_retain( p_md );
256 vlc_mutex_unlock( &p_mlist->object_lock );
258 return p_md;
261 /**************************************************************************
262 * libvlc_media_list_count (Public)
264 * Lock should be held when entering.
265 **************************************************************************/
266 int libvlc_media_list_count( libvlc_media_list_t * p_mlist )
268 return vlc_array_count( &p_mlist->items );
271 /**************************************************************************
272 * libvlc_media_list_add_media (Public)
274 * Lock should be held when entering.
275 **************************************************************************/
276 int libvlc_media_list_add_media( libvlc_media_list_t * p_mlist,
277 libvlc_media_t * p_md )
279 if( !mlist_is_writable(p_mlist) )
280 return -1;
281 libvlc_media_list_internal_add_media( p_mlist, p_md );
282 return 0;
285 /* LibVLC internal version */
286 void libvlc_media_list_internal_add_media( libvlc_media_list_t * p_mlist,
287 libvlc_media_t * p_md )
289 libvlc_media_retain( p_md );
291 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
292 EventWillHappen );
293 vlc_array_append_or_abort( &p_mlist->items, p_md );
294 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
295 EventDidHappen );
298 /**************************************************************************
299 * libvlc_media_list_insert_media (Public)
301 * Lock should be hold when entering.
302 **************************************************************************/
303 int libvlc_media_list_insert_media( libvlc_media_list_t * p_mlist,
304 libvlc_media_t * p_md,
305 int index )
307 if( !mlist_is_writable(p_mlist) )
308 return -1;
309 libvlc_media_list_internal_insert_media( p_mlist, p_md, index );
310 return 0;
313 /* LibVLC internal version */
314 void libvlc_media_list_internal_insert_media( libvlc_media_list_t * p_mlist,
315 libvlc_media_t * p_md,
316 int index )
318 libvlc_media_retain( p_md );
320 notify_item_addition( p_mlist, p_md, index, EventWillHappen );
321 vlc_array_insert_or_abort( &p_mlist->items, p_md, index );
322 notify_item_addition( p_mlist, p_md, index, EventDidHappen );
325 /**************************************************************************
326 * libvlc_media_list_remove_index (Public)
328 * Lock should be held when entering.
329 **************************************************************************/
330 int libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
331 int index )
333 if( !mlist_is_writable(p_mlist) )
334 return -1;
335 return libvlc_media_list_internal_remove_index( p_mlist, index );
338 /* LibVLC internal version */
339 int libvlc_media_list_internal_remove_index( libvlc_media_list_t * p_mlist,
340 int index )
342 libvlc_media_t * p_md;
344 if( (size_t) index >= vlc_array_count( &p_mlist->items ))
346 libvlc_printerr( "Index out of bounds" );
347 return -1;
350 p_md = vlc_array_item_at_index( &p_mlist->items, index );
352 notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
353 vlc_array_remove( &p_mlist->items, index );
354 notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
356 libvlc_media_release( p_md );
357 return 0;
360 /**************************************************************************
361 * libvlc_media_list_item_at_index (Public)
363 * Lock should be held when entering.
364 **************************************************************************/
365 libvlc_media_t *
366 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
367 int index )
369 libvlc_media_t * p_md;
371 if( (size_t) index >= vlc_array_count( &p_mlist->items ))
373 libvlc_printerr( "Index out of bounds" );
374 return NULL;
377 p_md = vlc_array_item_at_index( &p_mlist->items, index );
378 libvlc_media_retain( p_md );
379 return p_md;
382 /**************************************************************************
383 * libvlc_media_list_index_of_item (Public)
385 * Lock should be held when entering.
386 * Warning: this function returns the first matching item.
387 **************************************************************************/
388 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
389 libvlc_media_t * p_searched_md )
391 int idx = vlc_array_index_of_item( &p_mlist->items, p_searched_md );
392 if( idx == -1 )
393 libvlc_printerr( "Media not found" );
395 return idx;
398 /**************************************************************************
399 * libvlc_media_list_is_readonly (Public)
401 * This indicates if this media list is read-only from a user point of view
402 **************************************************************************/
403 bool libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
405 return p_mlist->b_read_only;
408 /**************************************************************************
409 * libvlc_media_list_lock (Public)
411 * The lock must be held in access operations. It is never used in the
412 * Public method.
413 **************************************************************************/
414 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
416 vlc_mutex_lock( &p_mlist->object_lock );
420 /**************************************************************************
421 * libvlc_media_list_unlock (Public)
423 * The lock must be held in access operations
424 **************************************************************************/
425 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
427 vlc_mutex_unlock( &p_mlist->object_lock );
431 /**************************************************************************
432 * libvlc_media_list_event_manager (Public)
434 * The p_event_manager is immutable, so you don't have to hold the lock
435 **************************************************************************/
436 libvlc_event_manager_t *
437 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist )
439 return &p_mlist->event_manager;