1 /*****************************************************************************
2 * media_list.c: libvlc new API media list functions
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
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 *****************************************************************************/
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_picture.h>
32 #include <vlc/libvlc_media.h>
33 #include <vlc/libvlc_media_list.h>
34 #include <vlc/libvlc_events.h>
36 #include <vlc_common.h>
37 #include <vlc_input.h>
39 #include "libvlc_internal.h"
40 #include "media_internal.h" // libvlc_media_new_from_input_item()
41 #include "media_list_internal.h"
43 typedef enum EventPlaceInTime
{
54 /**************************************************************************
55 * notify_item_addition (private)
57 * Do the appropriate action when an item is deleted.
58 **************************************************************************/
60 notify_item_addition( libvlc_media_list_t
* p_mlist
,
61 libvlc_media_t
* p_md
,
63 EventPlaceInTime event_status
)
67 /* Construct the event */
68 if( event_status
== EventDidHappen
)
70 event
.type
= libvlc_MediaListItemAdded
;
71 event
.u
.media_list_item_added
.item
= p_md
;
72 event
.u
.media_list_item_added
.index
= index
;
74 else /* if( event_status == EventWillHappen ) */
76 event
.type
= libvlc_MediaListWillAddItem
;
77 event
.u
.media_list_will_add_item
.item
= p_md
;
78 event
.u
.media_list_will_add_item
.index
= index
;
82 libvlc_event_send( &p_mlist
->event_manager
, &event
);
85 /**************************************************************************
86 * notify_item_deletion (private)
88 * Do the appropriate action when an item is added.
89 **************************************************************************/
91 notify_item_deletion( libvlc_media_list_t
* p_mlist
,
92 libvlc_media_t
* p_md
,
94 EventPlaceInTime event_status
)
98 /* Construct the event */
99 if( event_status
== EventDidHappen
)
101 event
.type
= libvlc_MediaListItemDeleted
;
102 event
.u
.media_list_item_deleted
.item
= p_md
;
103 event
.u
.media_list_item_deleted
.index
= index
;
105 else /* if( event_status == EventWillHappen ) */
107 event
.type
= libvlc_MediaListWillDeleteItem
;
108 event
.u
.media_list_will_delete_item
.item
= p_md
;
109 event
.u
.media_list_will_delete_item
.index
= index
;
113 libvlc_event_send( &p_mlist
->event_manager
, &event
);
116 /* LibVLC internal */
117 void libvlc_media_list_internal_end_reached( libvlc_media_list_t
* p_mlist
)
119 libvlc_event_t event
;
121 event
.type
= libvlc_MediaListEndReached
;
124 libvlc_event_send( &p_mlist
->event_manager
, &event
);
127 /**************************************************************************
128 * static mlist_is_writable (private)
129 **************************************************************************/
131 bool mlist_is_writable( libvlc_media_list_t
*p_mlist
)
133 if( !p_mlist
||p_mlist
->b_read_only
)
135 /* We are read-only from user side */
136 libvlc_printerr( "Attempt to write a read-only media list" );
143 * Public libvlc functions
146 /**************************************************************************
147 * libvlc_media_list_new (Public)
150 **************************************************************************/
151 libvlc_media_list_t
*
152 libvlc_media_list_new( libvlc_instance_t
* p_inst
)
154 libvlc_media_list_t
* p_mlist
;
156 p_mlist
= malloc(sizeof(libvlc_media_list_t
));
157 if( unlikely(p_mlist
== NULL
) )
159 libvlc_printerr( "Not enough memory" );
163 p_mlist
->p_libvlc_instance
= p_inst
;
164 libvlc_event_manager_init( &p_mlist
->event_manager
, p_mlist
);
165 p_mlist
->b_read_only
= false;
167 vlc_mutex_init( &p_mlist
->object_lock
);
168 vlc_mutex_init( &p_mlist
->refcount_lock
); // FIXME: spinlock?
170 vlc_array_init( &p_mlist
->items
);
171 assert( p_mlist
->items
.i_count
== 0 );
172 p_mlist
->i_refcount
= 1;
173 p_mlist
->p_md
= NULL
;
174 p_mlist
->p_internal_md
= NULL
;
176 libvlc_retain( p_inst
);
180 /**************************************************************************
181 * libvlc_media_list_release (Public)
184 **************************************************************************/
185 void libvlc_media_list_release( libvlc_media_list_t
* p_mlist
)
187 vlc_mutex_lock( &p_mlist
->refcount_lock
);
188 p_mlist
->i_refcount
--;
189 if( p_mlist
->i_refcount
> 0 )
191 vlc_mutex_unlock( &p_mlist
->refcount_lock
);
194 vlc_mutex_unlock( &p_mlist
->refcount_lock
);
196 /* Refcount null, time to free */
198 libvlc_event_manager_destroy( &p_mlist
->event_manager
);
199 libvlc_media_release( p_mlist
->p_md
);
201 for( size_t i
= 0; i
< vlc_array_count( &p_mlist
->items
); i
++ )
203 libvlc_media_t
* p_md
= vlc_array_item_at_index( &p_mlist
->items
, i
);
204 libvlc_media_release( p_md
);
207 vlc_mutex_destroy( &p_mlist
->object_lock
);
208 vlc_mutex_destroy( &p_mlist
->refcount_lock
);
209 vlc_array_clear( &p_mlist
->items
);
211 libvlc_release( p_mlist
->p_libvlc_instance
);
215 /**************************************************************************
216 * libvlc_media_list_retain (Public)
218 * Increase an object refcount.
219 **************************************************************************/
220 void libvlc_media_list_retain( libvlc_media_list_t
* p_mlist
)
222 vlc_mutex_lock( &p_mlist
->refcount_lock
);
223 p_mlist
->i_refcount
++;
224 vlc_mutex_unlock( &p_mlist
->refcount_lock
);
228 /**************************************************************************
229 * add_file_content (Public)
230 **************************************************************************/
232 libvlc_media_list_add_file_content( libvlc_media_list_t
* p_mlist
,
233 const char * psz_uri
)
235 input_item_t
* p_input_item
;
236 libvlc_media_t
* p_md
;
238 p_input_item
= input_item_New( psz_uri
, _("Media Library") );
242 libvlc_printerr( "Not enough memory" );
246 p_md
= libvlc_media_new_from_input_item( p_mlist
->p_libvlc_instance
,
250 input_item_Release( p_input_item
);
254 if( libvlc_media_list_add_media( p_mlist
, p_md
) )
256 #warning Missing error handling!
257 /* printerr and leaks */
261 input_Read( p_mlist
->p_libvlc_instance
->p_libvlc_int
, p_input_item
,
266 /**************************************************************************
268 **************************************************************************/
269 void libvlc_media_list_set_media( libvlc_media_list_t
* p_mlist
,
270 libvlc_media_t
* p_md
)
273 vlc_mutex_lock( &p_mlist
->object_lock
);
274 if( p_mlist
->p_internal_md
|| !mlist_is_writable(p_mlist
) )
276 vlc_mutex_unlock( &p_mlist
->object_lock
);
279 libvlc_media_release( p_mlist
->p_md
);
280 libvlc_media_retain( p_md
);
281 p_mlist
->p_md
= p_md
;
282 vlc_mutex_unlock( &p_mlist
->object_lock
);
285 /**************************************************************************
288 * If this media_list comes is a media's subitems,
289 * This holds the corresponding media.
290 * This md is also seen as the information holder for the media_list.
291 * Indeed a media_list can have meta information through this
293 **************************************************************************/
295 libvlc_media_list_media( libvlc_media_list_t
* p_mlist
)
297 libvlc_media_t
*p_md
;
299 vlc_mutex_lock( &p_mlist
->object_lock
);
300 p_md
= p_mlist
->p_internal_md
? p_mlist
->p_internal_md
: p_mlist
->p_md
;
302 libvlc_media_retain( p_md
);
303 vlc_mutex_unlock( &p_mlist
->object_lock
);
308 /**************************************************************************
309 * libvlc_media_list_count (Public)
311 * Lock should be held when entering.
312 **************************************************************************/
313 int libvlc_media_list_count( libvlc_media_list_t
* p_mlist
)
315 return vlc_array_count( &p_mlist
->items
);
318 /**************************************************************************
319 * libvlc_media_list_add_media (Public)
321 * Lock should be held when entering.
322 **************************************************************************/
323 int libvlc_media_list_add_media( libvlc_media_list_t
* p_mlist
,
324 libvlc_media_t
* p_md
)
326 if( !mlist_is_writable(p_mlist
) )
328 libvlc_media_list_internal_add_media( p_mlist
, p_md
);
332 /* LibVLC internal version */
333 void libvlc_media_list_internal_add_media( libvlc_media_list_t
* p_mlist
,
334 libvlc_media_t
* p_md
)
336 libvlc_media_retain( p_md
);
338 notify_item_addition( p_mlist
, p_md
, vlc_array_count( &p_mlist
->items
),
340 vlc_array_append_or_abort( &p_mlist
->items
, p_md
);
341 notify_item_addition( p_mlist
, p_md
, vlc_array_count( &p_mlist
->items
)-1,
345 /**************************************************************************
346 * libvlc_media_list_insert_media (Public)
348 * Lock should be hold when entering.
349 **************************************************************************/
350 int libvlc_media_list_insert_media( libvlc_media_list_t
* p_mlist
,
351 libvlc_media_t
* p_md
,
354 if( !mlist_is_writable(p_mlist
) )
356 libvlc_media_list_internal_insert_media( p_mlist
, p_md
, index
);
360 /* LibVLC internal version */
361 void libvlc_media_list_internal_insert_media( libvlc_media_list_t
* p_mlist
,
362 libvlc_media_t
* p_md
,
365 libvlc_media_retain( p_md
);
367 notify_item_addition( p_mlist
, p_md
, index
, EventWillHappen
);
368 vlc_array_insert_or_abort( &p_mlist
->items
, p_md
, index
);
369 notify_item_addition( p_mlist
, p_md
, index
, EventDidHappen
);
372 /**************************************************************************
373 * libvlc_media_list_remove_index (Public)
375 * Lock should be held when entering.
376 **************************************************************************/
377 int libvlc_media_list_remove_index( libvlc_media_list_t
* p_mlist
,
380 if( !mlist_is_writable(p_mlist
) )
382 return libvlc_media_list_internal_remove_index( p_mlist
, index
);
385 /* LibVLC internal version */
386 int libvlc_media_list_internal_remove_index( libvlc_media_list_t
* p_mlist
,
389 libvlc_media_t
* p_md
;
391 if( (size_t) index
>= vlc_array_count( &p_mlist
->items
))
393 libvlc_printerr( "Index out of bounds" );
397 p_md
= vlc_array_item_at_index( &p_mlist
->items
, index
);
399 notify_item_deletion( p_mlist
, p_md
, index
, EventWillHappen
);
400 vlc_array_remove( &p_mlist
->items
, index
);
401 notify_item_deletion( p_mlist
, p_md
, index
, EventDidHappen
);
403 libvlc_media_release( p_md
);
407 /**************************************************************************
408 * libvlc_media_list_item_at_index (Public)
410 * Lock should be held when entering.
411 **************************************************************************/
413 libvlc_media_list_item_at_index( libvlc_media_list_t
* p_mlist
,
416 libvlc_media_t
* p_md
;
418 if( (size_t) index
>= vlc_array_count( &p_mlist
->items
))
420 libvlc_printerr( "Index out of bounds" );
424 p_md
= vlc_array_item_at_index( &p_mlist
->items
, index
);
425 libvlc_media_retain( p_md
);
429 /**************************************************************************
430 * libvlc_media_list_index_of_item (Public)
432 * Lock should be held when entering.
433 * Warning: this function returns the first matching item.
434 **************************************************************************/
435 int libvlc_media_list_index_of_item( libvlc_media_list_t
* p_mlist
,
436 libvlc_media_t
* p_searched_md
)
438 int idx
= vlc_array_index_of_item( &p_mlist
->items
, p_searched_md
);
440 libvlc_printerr( "Media not found" );
445 /**************************************************************************
446 * libvlc_media_list_is_readonly (Public)
448 * This indicates if this media list is read-only from a user point of view
449 **************************************************************************/
450 int libvlc_media_list_is_readonly( libvlc_media_list_t
* p_mlist
)
452 return p_mlist
->b_read_only
;
455 /**************************************************************************
456 * libvlc_media_list_lock (Public)
458 * The lock must be held in access operations. It is never used in the
460 **************************************************************************/
461 void libvlc_media_list_lock( libvlc_media_list_t
* p_mlist
)
463 vlc_mutex_lock( &p_mlist
->object_lock
);
467 /**************************************************************************
468 * libvlc_media_list_unlock (Public)
470 * The lock must be held in access operations
471 **************************************************************************/
472 void libvlc_media_list_unlock( libvlc_media_list_t
* p_mlist
)
474 vlc_mutex_unlock( &p_mlist
->object_lock
);
478 /**************************************************************************
479 * libvlc_media_list_event_manager (Public)
481 * The p_event_manager is immutable, so you don't have to hold the lock
482 **************************************************************************/
483 libvlc_event_manager_t
*
484 libvlc_media_list_event_manager( libvlc_media_list_t
* p_mlist
)
486 return &p_mlist
->event_manager
;