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"
35 /**************************************************************************
36 * notify_item_addition (private)
38 * Do the appropriate action when an item is deleted.
39 **************************************************************************/
41 notify_item_addition( libvlc_media_list_t * p_mlist,
42 libvlc_media_descriptor_t * p_md,
47 /* Construct the event */
48 event.type = libvlc_MediaListItemAdded;
49 event.u.media_list_item_added.item = p_md;
50 event.u.media_list_item_added.index = index;
53 libvlc_event_send( p_mlist->p_event_manager, &event );
56 /**************************************************************************
57 * notify_item_deletion (private)
59 * Do the appropriate action when an item is added.
60 **************************************************************************/
62 notify_item_deletion( libvlc_media_list_t * p_mlist,
63 libvlc_media_descriptor_t * p_md,
68 /* Construct the event */
69 event.type = libvlc_MediaListItemDeleted;
70 event.u.media_list_item_deleted.item = p_md;
71 event.u.media_list_item_deleted.index = index;
74 libvlc_event_send( p_mlist->p_event_manager, &event );
77 /**************************************************************************
78 * media_descriptor_changed (private) (libvlc Event Callback )
80 * An item has changed.
81 **************************************************************************/
83 media_descriptor_changed( const libvlc_event_t * p_event, void * user_data )
85 libvlc_media_list_t * p_mlist = user_data;
86 libvlc_media_descriptor_t * p_md = p_event->p_obj;
89 /* Construct the new media list event */
90 event.type = libvlc_MediaListItemChanged;
91 event.u.media_list_item_changed.item = p_md;
93 /* XXX: this is not good, but there is a solution in the pipeline */
94 event.u.media_list_item_changed.index =
95 libvlc_media_list_index_of_item( p_mlist, p_md, NULL );
98 libvlc_event_send( p_mlist->p_event_manager, &event );
101 /**************************************************************************
102 * media_descriptor_subitem_added (private) (libvlc Event Callback )
104 * An item (which is a playlist) has gained sub child.
105 **************************************************************************/
107 media_descriptor_subitem_added( const libvlc_event_t * p_event, void * user_data )
109 libvlc_media_list_t * p_mlist = user_data;
110 libvlc_media_descriptor_t * p_new_md;
112 p_new_md = p_event->u.media_descriptor_subitem_added.new_child;
114 /* For now, just add the new item to this media list */
115 libvlc_media_list_add_media_descriptor( p_mlist, p_new_md, NULL );
118 /**************************************************************************
119 * install_media_descriptor_observer (private)
121 * Do the appropriate action when an item is deleted.
122 **************************************************************************/
124 install_media_descriptor_observer( libvlc_media_list_t * p_mlist,
125 libvlc_media_descriptor_t * p_md )
127 libvlc_event_attach( p_md->p_event_manager,
128 libvlc_MediaDescriptorMetaChanged,
129 media_descriptor_changed,
131 libvlc_event_attach( p_md->p_event_manager,
132 libvlc_MediaDescriptorSubItemAdded,
133 media_descriptor_subitem_added,
137 /**************************************************************************
138 * uninstall_media_descriptor_observer (private)
140 * Do the appropriate action when an item is deleted.
141 **************************************************************************/
143 uninstall_media_descriptor_observer( libvlc_media_list_t * p_mlist,
144 libvlc_media_descriptor_t * p_md )
146 libvlc_event_detach( p_md->p_event_manager,
147 libvlc_MediaDescriptorMetaChanged,
148 media_descriptor_changed,
150 libvlc_event_detach( p_md->p_event_manager,
151 libvlc_MediaDescriptorSubItemAdded,
152 media_descriptor_subitem_added,
156 /**************************************************************************
157 * dynamic_list_propose_item (private) (Event Callback)
159 * This is called if the dynamic sublist's data provider adds a new item.
160 **************************************************************************/
162 dynamic_list_propose_item( const libvlc_event_t * p_event, void * p_user_data )
164 libvlc_media_list_t * p_submlist = p_user_data;
165 libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
167 //libvlc_media_descriptor_lock( p_md );
168 if( libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
170 libvlc_media_list_lock( p_submlist );
171 libvlc_media_list_add_media_descriptor( p_submlist, p_md, NULL );
172 libvlc_media_list_unlock( p_submlist );
174 //libvlc_media_descriptor_unlock( p_md );
177 /**************************************************************************
178 * dynamic_list_remove_item (private) (Event Callback)
180 * This is called if the dynamic sublist's data provider adds a new item.
181 **************************************************************************/
183 dynamic_list_remove_item( const libvlc_event_t * p_event, void * p_user_data )
185 libvlc_media_list_t * p_submlist = p_user_data;
186 libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_deleted.item;
188 //libvlc_media_descriptor_lock( p_md );
189 if( libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
192 libvlc_media_list_lock( p_submlist );
193 i = libvlc_media_list_index_of_item( p_submlist, p_md, NULL );
196 /* We've missed one item addition, that could happen especially
197 * if we add item in a threaded maner, so we just ignore */
198 libvlc_media_list_unlock( p_submlist );
199 //libvlc_media_descriptor_unlock( p_md );
202 libvlc_media_list_remove_index( p_submlist, i, NULL );
203 libvlc_media_list_unlock( p_submlist );
205 //libvlc_media_descriptor_unlock( p_md );
208 /**************************************************************************
209 * dynamic_list_change_item (private) (Event Callback)
211 * This is called if the dynamic sublist's data provider adds a new item.
212 **************************************************************************/
214 dynamic_list_change_item( const libvlc_event_t * p_event , void * p_user_data)
216 libvlc_media_list_t * p_submlist = p_user_data;
217 libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_changed.item;
220 libvlc_media_list_lock( p_submlist );
222 index = libvlc_media_list_index_of_item( p_submlist, p_md, NULL );
225 libvlc_media_list_unlock( p_submlist );
226 return; /* Not found, no prob, just ignore */
229 //libvlc_media_descriptor_lock( p_md );
230 if( !libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
231 libvlc_media_list_remove_index( p_submlist, index, NULL );
232 //libvlc_media_descriptor_unlock( p_md );
234 libvlc_media_list_unlock( p_submlist );
238 * Public libvlc functions
241 /**************************************************************************
242 * libvlc_media_list_new (Public)
245 **************************************************************************/
246 libvlc_media_list_t *
247 libvlc_media_list_new( libvlc_instance_t * p_inst,
248 libvlc_exception_t * p_e )
250 libvlc_media_list_t * p_mlist;
252 p_mlist = malloc(sizeof(libvlc_media_list_t));
257 p_mlist->p_libvlc_instance = p_inst;
258 p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
260 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
261 libvlc_MediaListItemAdded, p_e );
262 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
263 libvlc_MediaListItemChanged, p_e );
264 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
265 libvlc_MediaListItemDeleted, p_e );
267 if( libvlc_exception_raised( p_e ) )
269 libvlc_event_manager_release( p_mlist->p_event_manager );
274 vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
276 ARRAY_INIT(p_mlist->items);
277 p_mlist->i_refcount = 1;
278 p_mlist->p_media_provider = NULL;
279 p_mlist->psz_name = NULL;
284 /**************************************************************************
285 * libvlc_media_list_release (Public)
288 **************************************************************************/
289 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
291 libvlc_media_descriptor_t * p_md;
293 vlc_mutex_lock( &p_mlist->object_lock );
294 p_mlist->i_refcount--;
295 if( p_mlist->i_refcount > 0 )
297 vlc_mutex_unlock( &p_mlist->object_lock );
300 vlc_mutex_unlock( &p_mlist->object_lock );
302 /* Refcount null, time to free */
303 if( p_mlist->p_media_provider )
304 libvlc_media_list_release( p_mlist->p_media_provider );
306 if( p_mlist->p_query )
307 libvlc_tag_query_release( p_mlist->p_query );
309 libvlc_event_manager_release( p_mlist->p_event_manager );
311 FOREACH_ARRAY( p_md, p_mlist->items )
312 uninstall_media_descriptor_observer( p_mlist, p_md );
313 libvlc_media_descriptor_release( p_md );
319 /**************************************************************************
320 * libvlc_media_list_retain (Public)
322 * Increase an object refcount.
323 **************************************************************************/
324 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
326 vlc_mutex_lock( &p_mlist->object_lock );
327 p_mlist->i_refcount++;
328 vlc_mutex_unlock( &p_mlist->object_lock );
332 /**************************************************************************
333 * add_file_content (Public)
334 **************************************************************************/
336 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
337 const char * psz_uri,
338 libvlc_exception_t * p_e )
340 input_item_t * p_input_item;
341 libvlc_media_descriptor_t * p_md;
343 p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
344 _("Media Library"), 0, NULL, -1 );
348 libvlc_exception_raise( p_e, "Can't create an input item" );
352 p_md = libvlc_media_descriptor_new_from_input_item(
353 p_mlist->p_libvlc_instance,
358 vlc_gc_decref( p_input_item );
362 libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
363 if( libvlc_exception_raised( p_e ) )
366 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
371 /**************************************************************************
373 **************************************************************************/
374 void libvlc_media_list_set_name( libvlc_media_list_t * p_mlist,
375 const char * psz_name,
376 libvlc_exception_t * p_e)
380 vlc_mutex_lock( &p_mlist->object_lock );
381 free( p_mlist->psz_name );
382 p_mlist->psz_name = psz_name ? strdup( psz_name ) : NULL;
383 vlc_mutex_unlock( &p_mlist->object_lock );
386 /**************************************************************************
388 **************************************************************************/
389 char * libvlc_media_list_name( libvlc_media_list_t * p_mlist,
390 libvlc_exception_t * p_e)
395 vlc_mutex_lock( &p_mlist->object_lock );
396 ret = p_mlist->psz_name ? strdup( p_mlist->psz_name ) : NULL;
397 vlc_mutex_unlock( &p_mlist->object_lock );
402 /**************************************************************************
403 * libvlc_media_list_count (Public)
405 * Lock should be hold when entering.
406 **************************************************************************/
407 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
408 libvlc_exception_t * p_e )
411 return p_mlist->items.i_size;
414 /**************************************************************************
415 * libvlc_media_list_add_media_descriptor (Public)
417 * Lock should be hold when entering.
418 **************************************************************************/
419 void libvlc_media_list_add_media_descriptor(
420 libvlc_media_list_t * p_mlist,
421 libvlc_media_descriptor_t * p_md,
422 libvlc_exception_t * p_e )
425 libvlc_media_descriptor_retain( p_md );
426 ARRAY_APPEND( p_mlist->items, p_md );
427 notify_item_addition( p_mlist, p_md, p_mlist->items.i_size-1 );
428 install_media_descriptor_observer( p_mlist, p_md );
431 /**************************************************************************
432 * libvlc_media_list_insert_media_descriptor (Public)
434 * Lock should be hold when entering.
435 **************************************************************************/
436 void libvlc_media_list_insert_media_descriptor(
437 libvlc_media_list_t * p_mlist,
438 libvlc_media_descriptor_t * p_md,
440 libvlc_exception_t * p_e )
443 libvlc_media_descriptor_retain( p_md );
445 ARRAY_INSERT( p_mlist->items, p_md, index);
446 notify_item_addition( p_mlist, p_md, index );
447 install_media_descriptor_observer( p_mlist, p_md );
450 /**************************************************************************
451 * libvlc_media_list_remove_index (Public)
453 * Lock should be hold when entering.
454 **************************************************************************/
455 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
457 libvlc_exception_t * p_e )
459 libvlc_media_descriptor_t * p_md;
461 p_md = ARRAY_VAL( p_mlist->items, index );
463 uninstall_media_descriptor_observer( p_mlist, p_md );
465 ARRAY_REMOVE( p_mlist->items, index )
466 notify_item_deletion( p_mlist, p_md, index );
468 libvlc_media_descriptor_release( p_md );
471 /**************************************************************************
472 * libvlc_media_list_item_at_index (Public)
474 * Lock should be hold when entering.
475 **************************************************************************/
476 libvlc_media_descriptor_t *
477 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
479 libvlc_exception_t * p_e )
481 libvlc_media_descriptor_t * p_md = ARRAY_VAL( p_mlist->items, index );
482 libvlc_media_descriptor_retain( p_md );
486 /**************************************************************************
487 * libvlc_media_list_index_of_item (Public)
489 * Lock should be hold when entering.
490 * Warning: this function would return the first matching item
491 **************************************************************************/
492 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
493 libvlc_media_descriptor_t * p_searched_md,
494 libvlc_exception_t * p_e )
496 libvlc_media_descriptor_t * p_md;
497 FOREACH_ARRAY( p_md, p_mlist->items )
498 if( p_searched_md == p_md )
499 return fe_idx; /* Once more, we hate macro for that */
504 /**************************************************************************
505 * libvlc_media_list_lock (Public)
507 * The lock must be held in access operations. It is never used in the
509 **************************************************************************/
510 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
512 vlc_mutex_lock( &p_mlist->object_lock );
516 /**************************************************************************
517 * libvlc_media_list_unlock (Public)
519 * The lock must be held in access operations
520 **************************************************************************/
521 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
523 vlc_mutex_unlock( &p_mlist->object_lock );
528 /**************************************************************************
529 * libvlc_media_list_p_event_manager (Public)
531 * The p_event_manager is immutable, so you don't have to hold the lock
532 **************************************************************************/
533 libvlc_event_manager_t *
534 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
535 libvlc_exception_t * p_e )
538 return p_mlist->p_event_manager;
541 /**************************************************************************
542 * libvlc_media_list_dynamic_sublist (Public)
544 * Lock should be hold when entering.
545 **************************************************************************/
546 libvlc_media_list_t *
547 libvlc_media_list_dynamic_sublist( libvlc_media_list_t * p_mlist,
548 libvlc_tag_query_t * p_query,
549 libvlc_exception_t * p_e )
551 libvlc_media_list_t * p_submlist;
552 libvlc_event_manager_t * p_em;
557 p_submlist = libvlc_media_list_new( p_mlist->p_libvlc_instance, p_e );
560 if( !libvlc_exception_raised( p_e ) )
561 libvlc_exception_raise( p_e, "Can't get the new media_list" );
565 /* We have a query */
566 libvlc_tag_query_retain( p_query );
567 p_submlist->p_query = p_query;
569 /* We have a media provider */
570 libvlc_media_list_retain( p_mlist );
571 p_submlist->p_media_provider = p_mlist;
574 libvlc_media_list_lock( p_submlist );
576 count = libvlc_media_list_count( p_mlist, p_e );
578 /* This should be running in a thread, a good plan to achieve that
579 * move all the dynamic code to libvlc_tag_query. */
580 for( i = 0; i < count; i++ )
582 libvlc_media_descriptor_t * p_md;
583 p_md = libvlc_media_list_item_at_index( p_mlist, i, p_e );
584 if( libvlc_tag_query_match( p_query, p_md, NULL ) )
585 libvlc_media_list_add_media_descriptor( p_submlist, p_md, p_e );
588 /* And we will listen to its event, so we can update p_submlist
590 p_em = libvlc_media_list_event_manager( p_mlist, p_e );
591 libvlc_event_attach( p_em, libvlc_MediaListItemAdded,
592 dynamic_list_propose_item, p_submlist, p_e );
593 libvlc_event_attach( p_em, libvlc_MediaListItemDeleted,
594 dynamic_list_remove_item, p_submlist, p_e );
595 libvlc_event_attach( p_em, libvlc_MediaListItemChanged,
596 dynamic_list_change_item, p_submlist, p_e );
598 libvlc_media_list_unlock( p_submlist );