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>
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
{
52 /**************************************************************************
53 * notify_item_addition (private)
55 * Do the appropriate action when an item is deleted.
56 **************************************************************************/
58 notify_item_addition( libvlc_media_list_t
* p_mlist
,
59 libvlc_media_t
* p_md
,
61 EventPlaceInTime event_status
)
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
;
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 **************************************************************************/
89 notify_item_deletion( libvlc_media_list_t
* p_mlist
,
90 libvlc_media_t
* p_md
,
92 EventPlaceInTime event_status
)
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
;
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
;
122 libvlc_event_send( &p_mlist
->event_manager
, &event
);
125 /**************************************************************************
126 * static mlist_is_writable (private)
127 **************************************************************************/
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" );
141 * Public libvlc functions
144 /**************************************************************************
145 * libvlc_media_list_new (Public)
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" );
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
);
178 /**************************************************************************
179 * libvlc_media_list_release (Public)
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
);
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_array_clear( &p_mlist
->items
);
207 libvlc_release( p_mlist
->p_libvlc_instance
);
211 /**************************************************************************
212 * libvlc_media_list_retain (Public)
214 * Increase an object refcount.
215 **************************************************************************/
216 void libvlc_media_list_retain( libvlc_media_list_t
* p_mlist
)
218 vlc_mutex_lock( &p_mlist
->refcount_lock
);
219 p_mlist
->i_refcount
++;
220 vlc_mutex_unlock( &p_mlist
->refcount_lock
);
223 /**************************************************************************
225 **************************************************************************/
226 void libvlc_media_list_set_media( libvlc_media_list_t
* p_mlist
,
227 libvlc_media_t
* p_md
)
230 vlc_mutex_lock( &p_mlist
->object_lock
);
231 if( p_mlist
->p_internal_md
|| !mlist_is_writable(p_mlist
) )
233 vlc_mutex_unlock( &p_mlist
->object_lock
);
236 libvlc_media_release( p_mlist
->p_md
);
237 libvlc_media_retain( p_md
);
238 p_mlist
->p_md
= p_md
;
239 vlc_mutex_unlock( &p_mlist
->object_lock
);
242 /**************************************************************************
245 * If this media_list comes is a media's subitems,
246 * This holds the corresponding media.
247 * This md is also seen as the information holder for the media_list.
248 * Indeed a media_list can have meta information through this
250 **************************************************************************/
252 libvlc_media_list_media( libvlc_media_list_t
* p_mlist
)
254 libvlc_media_t
*p_md
;
256 vlc_mutex_lock( &p_mlist
->object_lock
);
257 p_md
= p_mlist
->p_internal_md
? p_mlist
->p_internal_md
: p_mlist
->p_md
;
259 libvlc_media_retain( p_md
);
260 vlc_mutex_unlock( &p_mlist
->object_lock
);
265 /**************************************************************************
266 * libvlc_media_list_count (Public)
268 * Lock should be held when entering.
269 **************************************************************************/
270 int libvlc_media_list_count( libvlc_media_list_t
* p_mlist
)
272 return vlc_array_count( &p_mlist
->items
);
275 /**************************************************************************
276 * libvlc_media_list_add_media (Public)
278 * Lock should be held when entering.
279 **************************************************************************/
280 int libvlc_media_list_add_media( libvlc_media_list_t
* p_mlist
,
281 libvlc_media_t
* p_md
)
283 if( !mlist_is_writable(p_mlist
) )
285 libvlc_media_list_internal_add_media( p_mlist
, p_md
);
289 /* LibVLC internal version */
290 void libvlc_media_list_internal_add_media( libvlc_media_list_t
* p_mlist
,
291 libvlc_media_t
* p_md
)
293 libvlc_media_retain( p_md
);
295 notify_item_addition( p_mlist
, p_md
, vlc_array_count( &p_mlist
->items
),
297 vlc_array_append_or_abort( &p_mlist
->items
, p_md
);
298 notify_item_addition( p_mlist
, p_md
, vlc_array_count( &p_mlist
->items
)-1,
302 /**************************************************************************
303 * libvlc_media_list_insert_media (Public)
305 * Lock should be hold when entering.
306 **************************************************************************/
307 int libvlc_media_list_insert_media( libvlc_media_list_t
* p_mlist
,
308 libvlc_media_t
* p_md
,
311 if( !mlist_is_writable(p_mlist
) )
313 libvlc_media_list_internal_insert_media( p_mlist
, p_md
, index
);
317 /* LibVLC internal version */
318 void libvlc_media_list_internal_insert_media( libvlc_media_list_t
* p_mlist
,
319 libvlc_media_t
* p_md
,
322 libvlc_media_retain( p_md
);
324 notify_item_addition( p_mlist
, p_md
, index
, EventWillHappen
);
325 vlc_array_insert_or_abort( &p_mlist
->items
, p_md
, index
);
326 notify_item_addition( p_mlist
, p_md
, index
, EventDidHappen
);
329 /**************************************************************************
330 * libvlc_media_list_remove_index (Public)
332 * Lock should be held when entering.
333 **************************************************************************/
334 int libvlc_media_list_remove_index( libvlc_media_list_t
* p_mlist
,
337 if( !mlist_is_writable(p_mlist
) )
339 return libvlc_media_list_internal_remove_index( p_mlist
, index
);
342 /* LibVLC internal version */
343 int libvlc_media_list_internal_remove_index( libvlc_media_list_t
* p_mlist
,
346 libvlc_media_t
* p_md
;
348 if( (size_t) index
>= vlc_array_count( &p_mlist
->items
))
350 libvlc_printerr( "Index out of bounds" );
354 p_md
= vlc_array_item_at_index( &p_mlist
->items
, index
);
356 notify_item_deletion( p_mlist
, p_md
, index
, EventWillHappen
);
357 vlc_array_remove( &p_mlist
->items
, index
);
358 notify_item_deletion( p_mlist
, p_md
, index
, EventDidHappen
);
360 libvlc_media_release( p_md
);
364 /**************************************************************************
365 * libvlc_media_list_item_at_index (Public)
367 * Lock should be held when entering.
368 **************************************************************************/
370 libvlc_media_list_item_at_index( libvlc_media_list_t
* p_mlist
,
373 libvlc_media_t
* p_md
;
375 if( (size_t) index
>= vlc_array_count( &p_mlist
->items
))
377 libvlc_printerr( "Index out of bounds" );
381 p_md
= vlc_array_item_at_index( &p_mlist
->items
, index
);
382 libvlc_media_retain( p_md
);
386 /**************************************************************************
387 * libvlc_media_list_index_of_item (Public)
389 * Lock should be held when entering.
390 * Warning: this function returns the first matching item.
391 **************************************************************************/
392 int libvlc_media_list_index_of_item( libvlc_media_list_t
* p_mlist
,
393 libvlc_media_t
* p_searched_md
)
395 int idx
= vlc_array_index_of_item( &p_mlist
->items
, p_searched_md
);
397 libvlc_printerr( "Media not found" );
402 /**************************************************************************
403 * libvlc_media_list_is_readonly (Public)
405 * This indicates if this media list is read-only from a user point of view
406 **************************************************************************/
407 bool libvlc_media_list_is_readonly( libvlc_media_list_t
* p_mlist
)
409 return p_mlist
->b_read_only
;
412 /**************************************************************************
413 * libvlc_media_list_lock (Public)
415 * The lock must be held in access operations. It is never used in the
417 **************************************************************************/
418 void libvlc_media_list_lock( libvlc_media_list_t
* p_mlist
)
420 vlc_mutex_lock( &p_mlist
->object_lock
);
424 /**************************************************************************
425 * libvlc_media_list_unlock (Public)
427 * The lock must be held in access operations
428 **************************************************************************/
429 void libvlc_media_list_unlock( libvlc_media_list_t
* p_mlist
)
431 vlc_mutex_unlock( &p_mlist
->object_lock
);
435 /**************************************************************************
436 * libvlc_media_list_event_manager (Public)
438 * The p_event_manager is immutable, so you don't have to hold the lock
439 **************************************************************************/
440 libvlc_event_manager_t
*
441 libvlc_media_list_event_manager( libvlc_media_list_t
* p_mlist
)
443 return &p_mlist
->event_manager
;