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 * install_media_descriptor_observer (private)
104 * Do the appropriate action when an item is deleted.
105 **************************************************************************/
107 install_media_descriptor_observer( libvlc_media_list_t * p_mlist,
108 libvlc_media_descriptor_t * p_md )
110 libvlc_event_attach( p_md->p_event_manager,
111 libvlc_MediaDescriptorMetaChanged,
112 media_descriptor_changed,
116 /**************************************************************************
117 * uninstall_media_descriptor_observer (private)
119 * Do the appropriate action when an item is deleted.
120 **************************************************************************/
122 uninstall_media_descriptor_observer( libvlc_media_list_t * p_mlist,
123 libvlc_media_descriptor_t * p_md )
125 libvlc_event_detach( p_md->p_event_manager,
126 libvlc_MediaDescriptorMetaChanged,
127 media_descriptor_changed,
131 /**************************************************************************
132 * dynamic_list_propose_item (private) (Event Callback)
134 * This is called if the dynamic sublist's data provider adds a new item.
135 **************************************************************************/
137 dynamic_list_propose_item( const libvlc_event_t * p_event, void * p_user_data )
139 libvlc_media_list_t * p_submlist = p_user_data;
140 libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
142 //libvlc_media_descriptor_lock( p_md );
143 if( libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
145 libvlc_media_list_lock( p_submlist );
146 libvlc_media_list_add_media_descriptor( p_submlist, p_md, NULL );
147 libvlc_media_list_unlock( p_submlist );
149 //libvlc_media_descriptor_unlock( p_md );
152 /**************************************************************************
153 * dynamic_list_remove_item (private) (Event Callback)
155 * This is called if the dynamic sublist's data provider adds a new item.
156 **************************************************************************/
158 dynamic_list_remove_item( const libvlc_event_t * p_event, void * p_user_data )
160 libvlc_media_list_t * p_submlist = p_user_data;
161 libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_deleted.item;
163 //libvlc_media_descriptor_lock( p_md );
164 if( libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
167 libvlc_media_list_lock( p_submlist );
168 i = libvlc_media_list_index_of_item( p_submlist, p_md, NULL );
171 /* We've missed one item addition, that could happen especially
172 * if we add item in a threaded maner, so we just ignore */
173 libvlc_media_list_unlock( p_submlist );
174 //libvlc_media_descriptor_unlock( p_md );
177 libvlc_media_list_remove_index( p_submlist, i, NULL );
178 libvlc_media_list_unlock( p_submlist );
180 //libvlc_media_descriptor_unlock( p_md );
183 /**************************************************************************
184 * dynamic_list_change_item (private) (Event Callback)
186 * This is called if the dynamic sublist's data provider adds a new item.
187 **************************************************************************/
189 dynamic_list_change_item( const libvlc_event_t * p_event , void * p_user_data)
191 libvlc_media_list_t * p_submlist = p_user_data;
192 libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_changed.item;
195 libvlc_media_list_lock( p_submlist );
197 index = libvlc_media_list_index_of_item( p_submlist, p_md, NULL );
200 libvlc_media_list_unlock( p_submlist );
201 return; /* Not found, no prob, just ignore */
204 //libvlc_media_descriptor_lock( p_md );
205 if( !libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
206 libvlc_media_list_remove_index( p_submlist, index, NULL );
207 //libvlc_media_descriptor_unlock( p_md );
209 libvlc_media_list_unlock( p_submlist );
213 * Public libvlc functions
216 /**************************************************************************
217 * libvlc_media_list_new (Public)
220 **************************************************************************/
221 libvlc_media_list_t *
222 libvlc_media_list_new( libvlc_instance_t * p_inst,
223 libvlc_exception_t * p_e )
225 libvlc_media_list_t * p_mlist;
227 p_mlist = malloc(sizeof(libvlc_media_list_t));
232 p_mlist->p_libvlc_instance = p_inst;
233 p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
235 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
236 libvlc_MediaListItemAdded, p_e );
237 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
238 libvlc_MediaListItemChanged, p_e );
239 libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
240 libvlc_MediaListItemDeleted, p_e );
242 if( libvlc_exception_raised( p_e ) )
244 libvlc_event_manager_release( p_mlist->p_event_manager );
249 vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
251 ARRAY_INIT(p_mlist->items);
252 p_mlist->i_refcount = 1;
253 p_mlist->p_media_provider = NULL;
258 /**************************************************************************
259 * libvlc_media_list_release (Public)
262 **************************************************************************/
263 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
265 libvlc_media_descriptor_t * p_md;
267 vlc_mutex_lock( &p_mlist->object_lock );
268 p_mlist->i_refcount--;
269 if( p_mlist->i_refcount > 0 )
271 vlc_mutex_unlock( &p_mlist->object_lock );
274 vlc_mutex_unlock( &p_mlist->object_lock );
276 /* Refcount null, time to free */
277 if( p_mlist->p_media_provider )
278 libvlc_media_list_release( p_mlist->p_media_provider );
280 if( p_mlist->p_query )
281 libvlc_tag_query_release( p_mlist->p_query );
283 libvlc_event_manager_release( p_mlist->p_event_manager );
285 FOREACH_ARRAY( p_md, p_mlist->items )
286 uninstall_media_descriptor_observer( p_mlist, p_md );
287 libvlc_media_descriptor_release( p_md );
292 /**************************************************************************
293 * libvlc_media_list_retain (Public)
295 * Increase an object refcount.
296 **************************************************************************/
297 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
299 vlc_mutex_lock( &p_mlist->object_lock );
300 p_mlist->i_refcount++;
301 vlc_mutex_unlock( &p_mlist->object_lock );
304 /**************************************************************************
305 * libvlc_media_list_count (Public)
307 * Lock should be hold when entering.
308 **************************************************************************/
309 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
310 libvlc_exception_t * p_e )
313 return p_mlist->items.i_size;
316 /**************************************************************************
317 * libvlc_media_list_add_media_descriptor (Public)
319 * Lock should be hold when entering.
320 **************************************************************************/
321 void libvlc_media_list_add_media_descriptor(
322 libvlc_media_list_t * p_mlist,
323 libvlc_media_descriptor_t * p_md,
324 libvlc_exception_t * p_e )
327 libvlc_media_descriptor_retain( p_md );
328 ARRAY_APPEND( p_mlist->items, p_md );
329 notify_item_addition( p_mlist, p_md, p_mlist->items.i_size-1 );
330 install_media_descriptor_observer( p_mlist, p_md );
333 /**************************************************************************
334 * libvlc_media_list_insert_media_descriptor (Public)
336 * Lock should be hold when entering.
337 **************************************************************************/
338 void libvlc_media_list_insert_media_descriptor(
339 libvlc_media_list_t * p_mlist,
340 libvlc_media_descriptor_t * p_md,
342 libvlc_exception_t * p_e )
345 libvlc_media_descriptor_retain( p_md );
347 ARRAY_INSERT( p_mlist->items, p_md, index);
348 notify_item_addition( p_mlist, p_md, index );
349 install_media_descriptor_observer( p_mlist, p_md );
352 /**************************************************************************
353 * libvlc_media_list_remove_index (Public)
355 * Lock should be hold when entering.
356 **************************************************************************/
357 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
359 libvlc_exception_t * p_e )
361 libvlc_media_descriptor_t * p_md;
363 p_md = ARRAY_VAL( p_mlist->items, index );
365 uninstall_media_descriptor_observer( p_mlist, p_md );
367 ARRAY_REMOVE( p_mlist->items, index )
368 notify_item_deletion( p_mlist, p_md, index );
370 libvlc_media_descriptor_release( p_md );
373 /**************************************************************************
374 * libvlc_media_list_item_at_index (Public)
376 * Lock should be hold when entering.
377 **************************************************************************/
378 libvlc_media_descriptor_t *
379 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
381 libvlc_exception_t * p_e )
383 libvlc_media_descriptor_t * p_md = ARRAY_VAL( p_mlist->items, index );
384 libvlc_media_descriptor_retain( p_md );
388 /**************************************************************************
389 * libvlc_media_list_index_of_item (Public)
391 * Lock should be hold when entering.
392 * Warning: this function would return the first matching item
393 **************************************************************************/
394 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
395 libvlc_media_descriptor_t * p_searched_md,
396 libvlc_exception_t * p_e )
398 libvlc_media_descriptor_t * p_md;
399 FOREACH_ARRAY( p_md, p_mlist->items )
400 if( p_searched_md == p_md )
401 return fe_idx; /* Once more, we hate macro for that */
406 /**************************************************************************
407 * libvlc_media_list_lock (Public)
409 * The lock must be held in access operations. It is never used in the
411 **************************************************************************/
412 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
414 vlc_mutex_lock( &p_mlist->object_lock );
418 /**************************************************************************
419 * libvlc_media_list_unlock (Public)
421 * The lock must be held in access operations
422 **************************************************************************/
423 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
425 vlc_mutex_unlock( &p_mlist->object_lock );
430 /**************************************************************************
431 * libvlc_media_list_p_event_manager (Public)
433 * The p_event_manager is immutable, so you don't have to hold the lock
434 **************************************************************************/
435 libvlc_event_manager_t *
436 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
437 libvlc_exception_t * p_e )
440 return p_mlist->p_event_manager;
443 /**************************************************************************
444 * libvlc_media_list_dynamic_sublist (Public)
446 * Lock should be hold when entering.
447 **************************************************************************/
448 libvlc_media_list_t *
449 libvlc_media_list_dynamic_sublist( libvlc_media_list_t * p_mlist,
450 libvlc_tag_query_t * p_query,
451 libvlc_exception_t * p_e )
453 libvlc_media_list_t * p_submlist;
454 libvlc_event_manager_t * p_em;
459 p_submlist = libvlc_media_list_new( p_mlist->p_libvlc_instance, p_e );
462 if( !libvlc_exception_raised( p_e ) )
463 libvlc_exception_raise( p_e, "Can't get the new media_list" );
467 /* We have a query */
468 libvlc_tag_query_retain( p_query );
469 p_submlist->p_query = p_query;
471 /* We have a media provider */
472 libvlc_media_list_retain( p_mlist );
473 p_submlist->p_media_provider = p_mlist;
476 libvlc_media_list_lock( p_submlist );
478 count = libvlc_media_list_count( p_mlist, p_e );
480 /* This should be running in a thread, a good plan to achieve that
481 * move all the dynamic code to libvlc_tag_query. */
482 for( i = 0; i < count; i++ )
484 libvlc_media_descriptor_t * p_md;
485 p_md = libvlc_media_list_item_at_index( p_mlist, i, p_e );
486 if( libvlc_tag_query_match( p_query, p_md, NULL ) )
487 libvlc_media_list_add_media_descriptor( p_submlist, p_md, p_e );
490 /* And we will listen to its event, so we can update p_submlist
492 p_em = libvlc_media_list_event_manager( p_mlist, p_e );
493 libvlc_event_attach( p_em, libvlc_MediaListItemAdded,
494 dynamic_list_propose_item, p_submlist, p_e );
495 libvlc_event_attach( p_em, libvlc_MediaListItemDeleted,
496 dynamic_list_remove_item, p_submlist, p_e );
497 libvlc_event_attach( p_em, libvlc_MediaListItemChanged,
498 dynamic_list_change_item, p_submlist, p_e );
500 libvlc_media_list_unlock( p_submlist );