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 *****************************************************************************/
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>
36 #include <vlc_atomic.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
{
53 /**************************************************************************
54 * notify_item_addition (private)
56 * Do the appropriate action when an item is deleted.
57 **************************************************************************/
59 notify_item_addition( libvlc_media_list_t
* p_mlist
,
60 libvlc_media_t
* p_md
,
62 EventPlaceInTime event_status
)
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
;
81 libvlc_event_send( &p_mlist
->event_manager
, &event
);
84 /**************************************************************************
85 * notify_item_deletion (private)
87 * Do the appropriate action when an item is added.
88 **************************************************************************/
90 notify_item_deletion( libvlc_media_list_t
* p_mlist
,
91 libvlc_media_t
* p_md
,
93 EventPlaceInTime event_status
)
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
;
112 libvlc_event_send( &p_mlist
->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
;
123 libvlc_event_send( &p_mlist
->event_manager
, &event
);
126 /**************************************************************************
127 * static mlist_is_writable (private)
128 **************************************************************************/
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" );
142 * Public libvlc functions
145 /**************************************************************************
146 * libvlc_media_list_new (Public)
149 **************************************************************************/
150 libvlc_media_list_t
*libvlc_media_list_new(void)
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" );
161 libvlc_event_manager_init( &p_mlist
->event_manager
, p_mlist
);
162 p_mlist
->b_read_only
= false;
164 vlc_mutex_init( &p_mlist
->object_lock
);
165 vlc_atomic_rc_init( &p_mlist
->rc
);
167 vlc_array_init( &p_mlist
->items
);
168 assert( p_mlist
->items
.i_count
== 0 );
169 p_mlist
->p_md
= NULL
;
170 p_mlist
->p_internal_md
= NULL
;
175 /**************************************************************************
176 * libvlc_media_list_release (Public)
179 **************************************************************************/
180 void libvlc_media_list_release( libvlc_media_list_t
* p_mlist
)
182 if( !vlc_atomic_rc_dec( &p_mlist
->rc
) )
185 /* Refcount null, time to free */
187 libvlc_event_manager_destroy( &p_mlist
->event_manager
);
188 libvlc_media_release( p_mlist
->p_md
);
190 for( size_t i
= 0; i
< vlc_array_count( &p_mlist
->items
); i
++ )
192 libvlc_media_t
* p_md
= vlc_array_item_at_index( &p_mlist
->items
, i
);
193 libvlc_media_release( p_md
);
196 vlc_array_clear( &p_mlist
->items
);
201 /**************************************************************************
202 * libvlc_media_list_retain (Public)
204 * Increase an object refcount.
205 **************************************************************************/
206 void libvlc_media_list_retain( libvlc_media_list_t
* p_mlist
)
208 vlc_atomic_rc_inc( &p_mlist
->rc
);
211 /**************************************************************************
213 **************************************************************************/
214 void libvlc_media_list_set_media( libvlc_media_list_t
* p_mlist
,
215 libvlc_media_t
* p_md
)
218 vlc_mutex_lock( &p_mlist
->object_lock
);
219 if( p_mlist
->p_internal_md
|| !mlist_is_writable(p_mlist
) )
221 vlc_mutex_unlock( &p_mlist
->object_lock
);
224 libvlc_media_release( p_mlist
->p_md
);
225 libvlc_media_retain( p_md
);
226 p_mlist
->p_md
= p_md
;
227 vlc_mutex_unlock( &p_mlist
->object_lock
);
230 /**************************************************************************
233 * If this media_list comes is a media's subitems,
234 * This holds the corresponding media.
235 * This md is also seen as the information holder for the media_list.
236 * Indeed a media_list can have meta information through this
238 **************************************************************************/
240 libvlc_media_list_media( libvlc_media_list_t
* p_mlist
)
242 libvlc_media_t
*p_md
;
244 vlc_mutex_lock( &p_mlist
->object_lock
);
245 p_md
= p_mlist
->p_internal_md
? p_mlist
->p_internal_md
: p_mlist
->p_md
;
247 libvlc_media_retain( p_md
);
248 vlc_mutex_unlock( &p_mlist
->object_lock
);
253 /**************************************************************************
254 * libvlc_media_list_count (Public)
256 * Lock should be held when entering.
257 **************************************************************************/
258 int libvlc_media_list_count( libvlc_media_list_t
* p_mlist
)
260 return vlc_array_count( &p_mlist
->items
);
263 /**************************************************************************
264 * libvlc_media_list_add_media (Public)
266 * Lock should be held when entering.
267 **************************************************************************/
268 int libvlc_media_list_add_media( libvlc_media_list_t
* p_mlist
,
269 libvlc_media_t
* p_md
)
271 if( !mlist_is_writable(p_mlist
) )
273 libvlc_media_list_internal_add_media( p_mlist
, p_md
);
277 /* LibVLC internal version */
278 void libvlc_media_list_internal_add_media( libvlc_media_list_t
* p_mlist
,
279 libvlc_media_t
* p_md
)
281 libvlc_media_retain( p_md
);
283 notify_item_addition( p_mlist
, p_md
, vlc_array_count( &p_mlist
->items
),
285 vlc_array_append_or_abort( &p_mlist
->items
, p_md
);
286 notify_item_addition( p_mlist
, p_md
, vlc_array_count( &p_mlist
->items
)-1,
290 /**************************************************************************
291 * libvlc_media_list_insert_media (Public)
293 * Lock should be hold when entering.
294 **************************************************************************/
295 int libvlc_media_list_insert_media( libvlc_media_list_t
* p_mlist
,
296 libvlc_media_t
* p_md
,
299 if( !mlist_is_writable(p_mlist
) )
301 libvlc_media_list_internal_insert_media( p_mlist
, p_md
, index
);
305 /* LibVLC internal version */
306 void libvlc_media_list_internal_insert_media( libvlc_media_list_t
* p_mlist
,
307 libvlc_media_t
* p_md
,
310 libvlc_media_retain( p_md
);
312 notify_item_addition( p_mlist
, p_md
, index
, EventWillHappen
);
313 vlc_array_insert_or_abort( &p_mlist
->items
, p_md
, index
);
314 notify_item_addition( p_mlist
, p_md
, index
, EventDidHappen
);
317 /**************************************************************************
318 * libvlc_media_list_remove_index (Public)
320 * Lock should be held when entering.
321 **************************************************************************/
322 int libvlc_media_list_remove_index( libvlc_media_list_t
* p_mlist
,
325 if( !mlist_is_writable(p_mlist
) )
327 return libvlc_media_list_internal_remove_index( p_mlist
, index
);
330 /* LibVLC internal version */
331 int libvlc_media_list_internal_remove_index( libvlc_media_list_t
* p_mlist
,
334 libvlc_media_t
* p_md
;
336 if( (size_t) index
>= vlc_array_count( &p_mlist
->items
))
338 libvlc_printerr( "Index out of bounds" );
342 p_md
= vlc_array_item_at_index( &p_mlist
->items
, index
);
344 notify_item_deletion( p_mlist
, p_md
, index
, EventWillHappen
);
345 vlc_array_remove( &p_mlist
->items
, index
);
346 notify_item_deletion( p_mlist
, p_md
, index
, EventDidHappen
);
348 libvlc_media_release( p_md
);
352 /**************************************************************************
353 * libvlc_media_list_item_at_index (Public)
355 * Lock should be held when entering.
356 **************************************************************************/
358 libvlc_media_list_item_at_index( libvlc_media_list_t
* p_mlist
,
361 libvlc_media_t
* p_md
;
363 if( (size_t) index
>= vlc_array_count( &p_mlist
->items
))
365 libvlc_printerr( "Index out of bounds" );
369 p_md
= vlc_array_item_at_index( &p_mlist
->items
, index
);
370 libvlc_media_retain( p_md
);
374 /**************************************************************************
375 * libvlc_media_list_index_of_item (Public)
377 * Lock should be held when entering.
378 * Warning: this function returns the first matching item.
379 **************************************************************************/
380 int libvlc_media_list_index_of_item( libvlc_media_list_t
* p_mlist
,
381 libvlc_media_t
* p_searched_md
)
383 int idx
= vlc_array_index_of_item( &p_mlist
->items
, p_searched_md
);
385 libvlc_printerr( "Media not found" );
390 /**************************************************************************
391 * libvlc_media_list_is_readonly (Public)
393 * This indicates if this media list is read-only from a user point of view
394 **************************************************************************/
395 bool libvlc_media_list_is_readonly( libvlc_media_list_t
* p_mlist
)
397 return p_mlist
->b_read_only
;
400 /**************************************************************************
401 * libvlc_media_list_lock (Public)
403 * The lock must be held in access operations. It is never used in the
405 **************************************************************************/
406 void libvlc_media_list_lock( libvlc_media_list_t
* p_mlist
)
408 vlc_mutex_lock( &p_mlist
->object_lock
);
412 /**************************************************************************
413 * libvlc_media_list_unlock (Public)
415 * The lock must be held in access operations
416 **************************************************************************/
417 void libvlc_media_list_unlock( libvlc_media_list_t
* p_mlist
)
419 vlc_mutex_unlock( &p_mlist
->object_lock
);
423 /**************************************************************************
424 * libvlc_media_list_event_manager (Public)
426 * The p_event_manager is immutable, so you don't have to hold the lock
427 **************************************************************************/
428 libvlc_event_manager_t
*
429 libvlc_media_list_event_manager( libvlc_media_list_t
* p_mlist
)
431 return &p_mlist
->event_manager
;