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_changed,
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_changed,
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;
283 /**************************************************************************
284 * libvlc_media_list_release (Public)
287 **************************************************************************/
288 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
290 libvlc_media_descriptor_t * p_md;
292 vlc_mutex_lock( &p_mlist->object_lock );
293 p_mlist->i_refcount--;
294 if( p_mlist->i_refcount > 0 )
296 vlc_mutex_unlock( &p_mlist->object_lock );
299 vlc_mutex_unlock( &p_mlist->object_lock );
301 /* Refcount null, time to free */
302 if( p_mlist->p_media_provider )
303 libvlc_media_list_release( p_mlist->p_media_provider );
305 if( p_mlist->p_query )
306 libvlc_tag_query_release( p_mlist->p_query );
308 libvlc_event_manager_release( p_mlist->p_event_manager );
310 FOREACH_ARRAY( p_md, p_mlist->items )
311 uninstall_media_descriptor_observer( p_mlist, p_md );
312 libvlc_media_descriptor_release( p_md );
317 /**************************************************************************
318 * libvlc_media_list_retain (Public)
320 * Increase an object refcount.
321 **************************************************************************/
322 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
324 vlc_mutex_lock( &p_mlist->object_lock );
325 p_mlist->i_refcount++;
326 vlc_mutex_unlock( &p_mlist->object_lock );
329 /**************************************************************************
330 * libvlc_media_list_count (Public)
332 * Lock should be hold when entering.
333 **************************************************************************/
334 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
335 libvlc_exception_t * p_e )
338 return p_mlist->items.i_size;
341 /**************************************************************************
342 * libvlc_media_list_add_media_descriptor (Public)
344 * Lock should be hold when entering.
345 **************************************************************************/
346 void libvlc_media_list_add_media_descriptor(
347 libvlc_media_list_t * p_mlist,
348 libvlc_media_descriptor_t * p_md,
349 libvlc_exception_t * p_e )
352 libvlc_media_descriptor_retain( p_md );
353 ARRAY_APPEND( p_mlist->items, p_md );
354 notify_item_addition( p_mlist, p_md, p_mlist->items.i_size-1 );
355 install_media_descriptor_observer( p_mlist, p_md );
358 /**************************************************************************
359 * libvlc_media_list_insert_media_descriptor (Public)
361 * Lock should be hold when entering.
362 **************************************************************************/
363 void libvlc_media_list_insert_media_descriptor(
364 libvlc_media_list_t * p_mlist,
365 libvlc_media_descriptor_t * p_md,
367 libvlc_exception_t * p_e )
370 libvlc_media_descriptor_retain( p_md );
372 ARRAY_INSERT( p_mlist->items, p_md, index);
373 notify_item_addition( p_mlist, p_md, index );
374 install_media_descriptor_observer( p_mlist, p_md );
377 /**************************************************************************
378 * libvlc_media_list_remove_index (Public)
380 * Lock should be hold when entering.
381 **************************************************************************/
382 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
384 libvlc_exception_t * p_e )
386 libvlc_media_descriptor_t * p_md;
388 p_md = ARRAY_VAL( p_mlist->items, index );
390 uninstall_media_descriptor_observer( p_mlist, p_md );
392 ARRAY_REMOVE( p_mlist->items, index )
393 notify_item_deletion( p_mlist, p_md, index );
395 libvlc_media_descriptor_release( p_md );
398 /**************************************************************************
399 * libvlc_media_list_item_at_index (Public)
401 * Lock should be hold when entering.
402 **************************************************************************/
403 libvlc_media_descriptor_t *
404 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
406 libvlc_exception_t * p_e )
408 libvlc_media_descriptor_t * p_md = ARRAY_VAL( p_mlist->items, index );
409 libvlc_media_descriptor_retain( p_md );
413 /**************************************************************************
414 * libvlc_media_list_index_of_item (Public)
416 * Lock should be hold when entering.
417 * Warning: this function would return the first matching item
418 **************************************************************************/
419 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
420 libvlc_media_descriptor_t * p_searched_md,
421 libvlc_exception_t * p_e )
423 libvlc_media_descriptor_t * p_md;
424 FOREACH_ARRAY( p_md, p_mlist->items )
425 if( p_searched_md == p_md )
426 return fe_idx; /* Once more, we hate macro for that */
431 /**************************************************************************
432 * libvlc_media_list_lock (Public)
434 * The lock must be held in access operations. It is never used in the
436 **************************************************************************/
437 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
439 vlc_mutex_lock( &p_mlist->object_lock );
443 /**************************************************************************
444 * libvlc_media_list_unlock (Public)
446 * The lock must be held in access operations
447 **************************************************************************/
448 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
450 vlc_mutex_unlock( &p_mlist->object_lock );
455 /**************************************************************************
456 * libvlc_media_list_p_event_manager (Public)
458 * The p_event_manager is immutable, so you don't have to hold the lock
459 **************************************************************************/
460 libvlc_event_manager_t *
461 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
462 libvlc_exception_t * p_e )
465 return p_mlist->p_event_manager;
468 /**************************************************************************
469 * libvlc_media_list_dynamic_sublist (Public)
471 * Lock should be hold when entering.
472 **************************************************************************/
473 libvlc_media_list_t *
474 libvlc_media_list_dynamic_sublist( libvlc_media_list_t * p_mlist,
475 libvlc_tag_query_t * p_query,
476 libvlc_exception_t * p_e )
478 libvlc_media_list_t * p_submlist;
479 libvlc_event_manager_t * p_em;
484 p_submlist = libvlc_media_list_new( p_mlist->p_libvlc_instance, p_e );
487 if( !libvlc_exception_raised( p_e ) )
488 libvlc_exception_raise( p_e, "Can't get the new media_list" );
492 /* We have a query */
493 libvlc_tag_query_retain( p_query );
494 p_submlist->p_query = p_query;
496 /* We have a media provider */
497 libvlc_media_list_retain( p_mlist );
498 p_submlist->p_media_provider = p_mlist;
501 libvlc_media_list_lock( p_submlist );
503 count = libvlc_media_list_count( p_mlist, p_e );
505 /* This should be running in a thread, a good plan to achieve that
506 * move all the dynamic code to libvlc_tag_query. */
507 for( i = 0; i < count; i++ )
509 libvlc_media_descriptor_t * p_md;
510 p_md = libvlc_media_list_item_at_index( p_mlist, i, p_e );
511 if( libvlc_tag_query_match( p_query, p_md, NULL ) )
512 libvlc_media_list_add_media_descriptor( p_submlist, p_md, p_e );
515 /* And we will listen to its event, so we can update p_submlist
517 p_em = libvlc_media_list_event_manager( p_mlist, p_e );
518 libvlc_event_attach( p_em, libvlc_MediaListItemAdded,
519 dynamic_list_propose_item, p_submlist, p_e );
520 libvlc_event_attach( p_em, libvlc_MediaListItemDeleted,
521 dynamic_list_remove_item, p_submlist, p_e );
522 libvlc_event_attach( p_em, libvlc_MediaListItemChanged,
523 dynamic_list_change_item, p_submlist, p_e );
525 libvlc_media_list_unlock( p_submlist );