1 /*****************************************************************************
2 * media_list.c: libvlc new API media list functions
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
27 #include "vlc_arrays.h"
29 typedef enum EventPlaceInTime
{
34 //#define DEBUG_MEDIA_LIST
36 #ifdef DEBUG_MEDIA_LIST
37 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
48 /**************************************************************************
49 * notify_item_addition (private)
51 * Do the appropriate action when an item is deleted.
52 **************************************************************************/
54 notify_item_addition( libvlc_media_list_t
* p_mlist
,
55 libvlc_media_t
* p_md
,
57 EventPlaceInTime event_status
)
61 /* Construct the event */
62 if( event_status
== EventDidHappen
)
64 trace("item was added at index %d\n", index
);
65 event
.type
= libvlc_MediaListItemAdded
;
66 event
.u
.media_list_item_added
.item
= p_md
;
67 event
.u
.media_list_item_added
.index
= index
;
69 else /* if( event_status == EventWillHappen ) */
71 event
.type
= libvlc_MediaListWillAddItem
;
72 event
.u
.media_list_will_add_item
.item
= p_md
;
73 event
.u
.media_list_will_add_item
.index
= index
;
77 libvlc_event_send( p_mlist
->p_event_manager
, &event
);
80 /**************************************************************************
81 * notify_item_deletion (private)
83 * Do the appropriate action when an item is added.
84 **************************************************************************/
86 notify_item_deletion( libvlc_media_list_t
* p_mlist
,
87 libvlc_media_t
* p_md
,
89 EventPlaceInTime event_status
)
93 /* Construct the event */
94 if( event_status
== EventDidHappen
)
96 trace("item at index %d was deleted\n", index
);
97 event
.type
= libvlc_MediaListItemDeleted
;
98 event
.u
.media_list_item_deleted
.item
= p_md
;
99 event
.u
.media_list_item_deleted
.index
= index
;
101 else /* if( event_status == EventWillHappen ) */
103 event
.type
= libvlc_MediaListWillDeleteItem
;
104 event
.u
.media_list_will_delete_item
.item
= p_md
;
105 event
.u
.media_list_will_delete_item
.index
= index
;
109 libvlc_event_send( p_mlist
->p_event_manager
, &event
);
113 * Public libvlc functions
116 /**************************************************************************
117 * libvlc_media_list_new (Public)
120 **************************************************************************/
121 libvlc_media_list_t
*
122 libvlc_media_list_new( libvlc_instance_t
* p_inst
,
123 libvlc_exception_t
* p_e
)
125 libvlc_media_list_t
* p_mlist
;
127 p_mlist
= malloc(sizeof(libvlc_media_list_t
));
131 p_mlist
->p_libvlc_instance
= p_inst
;
132 p_mlist
->p_event_manager
= libvlc_event_manager_new( p_mlist
, p_inst
, p_e
);
134 /* Code for that one should be handled in flat_media_list.c */
135 p_mlist
->p_flat_mlist
= NULL
;
136 p_mlist
->b_read_only
= false;
138 libvlc_event_manager_register_event_type( p_mlist
->p_event_manager
,
139 libvlc_MediaListItemAdded
, p_e
);
140 libvlc_event_manager_register_event_type( p_mlist
->p_event_manager
,
141 libvlc_MediaListWillAddItem
, p_e
);
142 libvlc_event_manager_register_event_type( p_mlist
->p_event_manager
,
143 libvlc_MediaListItemDeleted
, p_e
);
144 libvlc_event_manager_register_event_type( p_mlist
->p_event_manager
,
145 libvlc_MediaListWillDeleteItem
, p_e
);
147 if( libvlc_exception_raised( p_e
) )
149 libvlc_event_manager_release( p_mlist
->p_event_manager
);
154 vlc_mutex_init( &p_mlist
->object_lock
);
156 vlc_array_init( &p_mlist
->items
);
157 p_mlist
->i_refcount
= 1;
158 p_mlist
->p_md
= NULL
;
163 /**************************************************************************
164 * libvlc_media_list_release (Public)
167 **************************************************************************/
168 void libvlc_media_list_release( libvlc_media_list_t
* p_mlist
)
170 libvlc_media_t
* p_md
;
173 vlc_mutex_lock( &p_mlist
->object_lock
);
174 p_mlist
->i_refcount
--;
175 if( p_mlist
->i_refcount
> 0 )
177 vlc_mutex_unlock( &p_mlist
->object_lock
);
180 vlc_mutex_unlock( &p_mlist
->object_lock
);
182 /* Refcount null, time to free */
184 libvlc_event_manager_release( p_mlist
->p_event_manager
);
187 libvlc_media_release( p_mlist
->p_md
);
189 for ( i
= 0; i
< vlc_array_count( &p_mlist
->items
); i
++ )
191 p_md
= vlc_array_item_at_index( &p_mlist
->items
, i
);
192 libvlc_media_release( p_md
);
195 vlc_mutex_destroy( &p_mlist
->object_lock
);
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_mutex_lock( &p_mlist
->object_lock
);
209 p_mlist
->i_refcount
++;
210 vlc_mutex_unlock( &p_mlist
->object_lock
);
214 /**************************************************************************
215 * add_file_content (Public)
216 **************************************************************************/
218 libvlc_media_list_add_file_content( libvlc_media_list_t
* p_mlist
,
219 const char * psz_uri
,
220 libvlc_exception_t
* p_e
)
222 input_item_t
* p_input_item
;
223 libvlc_media_t
* p_md
;
225 p_input_item
= input_item_NewExt( p_mlist
->p_libvlc_instance
->p_libvlc_int
, psz_uri
,
226 _("Media Library"), 0, NULL
, -1 );
230 libvlc_exception_raise( p_e
, "Can't create an input item" );
234 p_md
= libvlc_media_new_from_input_item(
235 p_mlist
->p_libvlc_instance
,
240 vlc_gc_decref( p_input_item
);
244 libvlc_media_list_add_media( p_mlist
, p_md
, p_e
);
245 if( libvlc_exception_raised( p_e
) )
248 input_Read( p_mlist
->p_libvlc_instance
->p_libvlc_int
, p_input_item
, true );
253 /**************************************************************************
255 **************************************************************************/
256 void libvlc_media_list_set_media( libvlc_media_list_t
* p_mlist
,
257 libvlc_media_t
* p_md
,
258 libvlc_exception_t
* p_e
)
262 vlc_mutex_lock( &p_mlist
->object_lock
);
264 libvlc_media_release( p_mlist
->p_md
);
265 libvlc_media_retain( p_md
);
266 p_mlist
->p_md
= p_md
;
267 vlc_mutex_unlock( &p_mlist
->object_lock
);
270 /**************************************************************************
273 * If this media_list comes is a media's subitems,
274 * This holds the corresponding media.
275 * This md is also seen as the information holder for the media_list.
276 * Indeed a media_list can have meta information through this
278 **************************************************************************/
280 libvlc_media_list_media( libvlc_media_list_t
* p_mlist
,
281 libvlc_exception_t
* p_e
)
283 libvlc_media_t
*p_md
;
286 vlc_mutex_lock( &p_mlist
->object_lock
);
287 p_md
= p_mlist
->p_md
;
289 libvlc_media_retain( p_md
);
290 vlc_mutex_unlock( &p_mlist
->object_lock
);
295 /**************************************************************************
296 * libvlc_media_list_count (Public)
298 * Lock should be hold when entering.
299 **************************************************************************/
300 int libvlc_media_list_count( libvlc_media_list_t
* p_mlist
,
301 libvlc_exception_t
* p_e
)
304 return vlc_array_count( &p_mlist
->items
);
307 /**************************************************************************
308 * libvlc_media_list_add_media (Public)
310 * Lock should be hold when entering.
311 **************************************************************************/
312 void libvlc_media_list_add_media(
313 libvlc_media_list_t
* p_mlist
,
314 libvlc_media_t
* p_md
,
315 libvlc_exception_t
* p_e
)
317 if( p_mlist
->b_read_only
)
319 /* We are read only from user side */
320 libvlc_exception_raise( p_e
, "Trying to write into a read-only media list." );
324 _libvlc_media_list_add_media( p_mlist
, p_md
, p_e
);
327 /* LibVLC internal version */
328 void _libvlc_media_list_add_media(
329 libvlc_media_list_t
* p_mlist
,
330 libvlc_media_t
* p_md
,
331 libvlc_exception_t
* p_e
)
334 libvlc_media_retain( p_md
);
336 notify_item_addition( p_mlist
, p_md
, vlc_array_count( &p_mlist
->items
), EventWillHappen
);
337 vlc_array_append( &p_mlist
->items
, p_md
);
338 notify_item_addition( p_mlist
, p_md
, vlc_array_count( &p_mlist
->items
)-1, EventDidHappen
);
341 /**************************************************************************
342 * libvlc_media_list_insert_media (Public)
344 * Lock should be hold when entering.
345 **************************************************************************/
346 void libvlc_media_list_insert_media(
347 libvlc_media_list_t
* p_mlist
,
348 libvlc_media_t
* p_md
,
350 libvlc_exception_t
* p_e
)
352 if( p_mlist
->b_read_only
)
354 /* We are read only from user side */
355 libvlc_exception_raise( p_e
, "Trying to write into a read-only media list." );
358 _libvlc_media_list_insert_media( p_mlist
, p_md
, index
, p_e
);
361 /* LibVLC internal version */
362 void _libvlc_media_list_insert_media(
363 libvlc_media_list_t
* p_mlist
,
364 libvlc_media_t
* p_md
,
366 libvlc_exception_t
* p_e
)
369 libvlc_media_retain( p_md
);
371 notify_item_addition( p_mlist
, p_md
, index
, EventWillHappen
);
372 vlc_array_insert( &p_mlist
->items
, p_md
, index
);
373 notify_item_addition( p_mlist
, p_md
, index
, EventDidHappen
);
376 /**************************************************************************
377 * libvlc_media_list_remove_index (Public)
379 * Lock should be hold when entering.
380 **************************************************************************/
381 void libvlc_media_list_remove_index( libvlc_media_list_t
* p_mlist
,
383 libvlc_exception_t
* p_e
)
385 if( p_mlist
->b_read_only
)
387 /* We are read only from user side */
388 libvlc_exception_raise( p_e
, "Trying to write into a read-only media list." );
391 _libvlc_media_list_remove_index( p_mlist
, index
, p_e
);
394 /* LibVLC internal version */
395 void _libvlc_media_list_remove_index( libvlc_media_list_t
* p_mlist
,
397 libvlc_exception_t
* p_e
)
400 libvlc_media_t
* p_md
;
402 if( index
< 0 || index
>= vlc_array_count( &p_mlist
->items
))
404 libvlc_exception_raise( p_e
, "Index out of bounds exception");
408 p_md
= vlc_array_item_at_index( &p_mlist
->items
, index
);
410 notify_item_deletion( p_mlist
, p_md
, index
, EventWillHappen
);
411 vlc_array_remove( &p_mlist
->items
, index
);
412 notify_item_deletion( p_mlist
, p_md
, index
, EventDidHappen
);
414 libvlc_media_release( p_md
);
417 /**************************************************************************
418 * libvlc_media_list_item_at_index (Public)
420 * Lock should be hold when entering.
421 **************************************************************************/
423 libvlc_media_list_item_at_index( libvlc_media_list_t
* p_mlist
,
425 libvlc_exception_t
* p_e
)
429 if( index
< 0 || index
>= vlc_array_count( &p_mlist
->items
))
431 libvlc_exception_raise( p_e
, "Index out of bounds exception");
435 libvlc_media_t
* p_md
;
436 p_md
= vlc_array_item_at_index( &p_mlist
->items
, index
);
437 libvlc_media_retain( p_md
);
441 /**************************************************************************
442 * libvlc_media_list_index_of_item (Public)
444 * Lock should be hold when entering.
445 * Warning: this function would return the first matching item
446 **************************************************************************/
447 int libvlc_media_list_index_of_item( libvlc_media_list_t
* p_mlist
,
448 libvlc_media_t
* p_searched_md
,
449 libvlc_exception_t
* p_e
)
453 libvlc_media_t
* p_md
;
455 for ( i
= 0; i
< vlc_array_count( &p_mlist
->items
); i
++ )
457 p_md
= vlc_array_item_at_index( &p_mlist
->items
, i
);
458 if( p_searched_md
== p_md
)
464 /**************************************************************************
465 * libvlc_media_list_is_readonly (Public)
467 * This indicates if this media list is read-only from a user point of view
468 **************************************************************************/
469 int libvlc_media_list_is_readonly( libvlc_media_list_t
* p_mlist
)
471 return p_mlist
->b_read_only
;
474 /**************************************************************************
475 * libvlc_media_list_lock (Public)
477 * The lock must be held in access operations. It is never used in the
479 **************************************************************************/
480 void libvlc_media_list_lock( libvlc_media_list_t
* p_mlist
)
482 vlc_mutex_lock( &p_mlist
->object_lock
);
486 /**************************************************************************
487 * libvlc_media_list_unlock (Public)
489 * The lock must be held in access operations
490 **************************************************************************/
491 void libvlc_media_list_unlock( libvlc_media_list_t
* p_mlist
)
493 vlc_mutex_unlock( &p_mlist
->object_lock
);
497 /**************************************************************************
498 * libvlc_media_list_p_event_manager (Public)
500 * The p_event_manager is immutable, so you don't have to hold the lock
501 **************************************************************************/
502 libvlc_event_manager_t
*
503 libvlc_media_list_event_manager( libvlc_media_list_t
* p_mlist
,
504 libvlc_exception_t
* p_e
)
507 return p_mlist
->p_event_manager
;