e717ff9af5d86300a6aba5f1e4770c7d814fed41
[vlc.git] / src / control / media_list.c
1 /*****************************************************************************
2  * media_list.c: libvlc new API media list functions
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
26 #include <assert.h>
27 #include "vlc_arrays.h"
28
29 /*
30  * Private functions
31  */
32
33
34
35 /**************************************************************************
36  *       notify_item_addition (private)
37  *
38  * Do the appropriate action when an item is deleted.
39  **************************************************************************/
40 static void
41 notify_item_addition( libvlc_media_list_t * p_mlist,
42                       libvlc_media_descriptor_t * p_md,
43                       int index )
44 {
45     libvlc_event_t event;
46
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;
51
52     /* Send the event */
53     libvlc_event_send( p_mlist->p_event_manager, &event );
54 }
55
56 /**************************************************************************
57  *       notify_item_deletion (private)
58  *
59  * Do the appropriate action when an item is added.
60  **************************************************************************/
61 static void
62 notify_item_deletion( libvlc_media_list_t * p_mlist,
63                       libvlc_media_descriptor_t * p_md,
64                       int index )
65 {
66     libvlc_event_t event;
67
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;
72
73     /* Send the event */
74     libvlc_event_send( p_mlist->p_event_manager, &event );
75 }
76
77 /**************************************************************************
78  *       media_descriptor_changed (private) (libvlc Event Callback )
79  *
80  * An item has changed.
81  **************************************************************************/
82 static void
83 media_descriptor_changed( const libvlc_event_t * p_event, void * user_data )
84 {
85     libvlc_media_list_t * p_mlist = user_data;
86     libvlc_media_descriptor_t * p_md = p_event->p_obj;
87     libvlc_event_t event;
88     
89     /* Construct the new media list event */
90     event.type = libvlc_MediaListItemChanged;
91     event.u.media_list_item_changed.item = p_md;
92
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 );
96
97     /* Send the event */
98     libvlc_event_send( p_mlist->p_event_manager, &event );
99 }
100
101 /**************************************************************************
102  *       media_descriptor_subitem_added (private) (libvlc Event Callback )
103  *
104  * An item (which is a playlist) has gained sub child.
105  **************************************************************************/
106 static void
107 media_descriptor_subitem_added( const libvlc_event_t * p_event, void * user_data )
108 {
109     libvlc_media_list_t * p_mlist = user_data;
110     libvlc_media_descriptor_t * p_new_md;
111
112     p_new_md = p_event->u.media_descriptor_subitem_added.new_child;
113
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 );
116 }
117
118 /**************************************************************************
119  *       install_media_descriptor_observer (private)
120  *
121  * Do the appropriate action when an item is deleted.
122  **************************************************************************/
123 static void
124 install_media_descriptor_observer( libvlc_media_list_t * p_mlist,
125                                    libvlc_media_descriptor_t * p_md )
126 {
127     libvlc_event_attach( p_md->p_event_manager,
128                          libvlc_MediaDescriptorMetaChanged,
129                          media_descriptor_changed,
130                          p_mlist, NULL );
131     libvlc_event_attach( p_md->p_event_manager,
132                          libvlc_MediaDescriptorSubItemAdded,
133                          media_descriptor_subitem_added,
134                          p_mlist, NULL );
135 }
136
137 /**************************************************************************
138  *       uninstall_media_descriptor_observer (private)
139  *
140  * Do the appropriate action when an item is deleted.
141  **************************************************************************/
142 static void
143 uninstall_media_descriptor_observer( libvlc_media_list_t * p_mlist,
144                                      libvlc_media_descriptor_t * p_md )
145 {
146     libvlc_event_detach( p_md->p_event_manager,
147                          libvlc_MediaDescriptorMetaChanged,
148                          media_descriptor_changed,
149                          p_mlist, NULL );
150     libvlc_event_detach( p_md->p_event_manager,
151                          libvlc_MediaDescriptorSubItemAdded,
152                          media_descriptor_subitem_added,
153                          p_mlist, NULL );
154 }
155
156 /**************************************************************************
157  *       dynamic_list_propose_item (private) (Event Callback)
158  *
159  * This is called if the dynamic sublist's data provider adds a new item.
160  **************************************************************************/
161 static void
162 dynamic_list_propose_item( const libvlc_event_t * p_event, void * p_user_data )
163 {
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;
166
167     //libvlc_media_descriptor_lock( p_md );
168     if( libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
169     {
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 );
173     }
174     //libvlc_media_descriptor_unlock( p_md );
175 }
176
177 /**************************************************************************
178  *       dynamic_list_remove_item (private) (Event Callback)
179  *
180  * This is called if the dynamic sublist's data provider adds a new item.
181  **************************************************************************/
182 static void
183 dynamic_list_remove_item( const libvlc_event_t * p_event, void * p_user_data )
184 {
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;
187
188     //libvlc_media_descriptor_lock( p_md );
189     if( libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
190     {
191         int i;
192         libvlc_media_list_lock( p_submlist );        
193         i = libvlc_media_list_index_of_item( p_submlist, p_md, NULL );
194         if ( i < 0 )
195         {
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 );           
200             return;
201         }
202         libvlc_media_list_remove_index( p_submlist, i, NULL );
203         libvlc_media_list_unlock( p_submlist );
204     }
205     //libvlc_media_descriptor_unlock( p_md );
206 }
207
208 /**************************************************************************
209  *       dynamic_list_change_item (private) (Event Callback)
210  *
211  * This is called if the dynamic sublist's data provider adds a new item.
212  **************************************************************************/
213 static void
214 dynamic_list_change_item( const libvlc_event_t * p_event , void * p_user_data)
215 {
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;
218     int index;
219
220     libvlc_media_list_lock( p_submlist );        
221     
222     index = libvlc_media_list_index_of_item( p_submlist, p_md, NULL );
223     if( index < 0 )
224     {
225         libvlc_media_list_unlock( p_submlist );     
226         return; /* Not found, no prob, just ignore */
227     }
228
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 );
233
234     libvlc_media_list_unlock( p_submlist );        
235 }
236
237 /*
238  * Public libvlc functions
239  */
240
241 /**************************************************************************
242  *       libvlc_media_list_new (Public)
243  *
244  * Init an object.
245  **************************************************************************/
246 libvlc_media_list_t *
247 libvlc_media_list_new( libvlc_instance_t * p_inst,
248                        libvlc_exception_t * p_e )
249 {
250     libvlc_media_list_t * p_mlist;
251
252     p_mlist = malloc(sizeof(libvlc_media_list_t));
253
254     if( !p_mlist )
255         return NULL;
256     
257     p_mlist->p_libvlc_instance = p_inst;
258     p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
259
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 );
266
267     if( libvlc_exception_raised( p_e ) )
268     {
269         libvlc_event_manager_release( p_mlist->p_event_manager );
270         free( p_mlist );
271         return NULL;
272     }
273
274     vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
275     
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;
280
281     return p_mlist;
282 }
283
284 /**************************************************************************
285  *       libvlc_media_list_release (Public)
286  *
287  * Release an object.
288  **************************************************************************/
289 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
290 {
291     libvlc_media_descriptor_t * p_md;
292
293     vlc_mutex_lock( &p_mlist->object_lock );
294     p_mlist->i_refcount--;
295     if( p_mlist->i_refcount > 0 )
296     {
297         vlc_mutex_unlock( &p_mlist->object_lock );        
298         return;
299     }
300     vlc_mutex_unlock( &p_mlist->object_lock );        
301
302     /* Refcount null, time to free */
303     if( p_mlist->p_media_provider )
304         libvlc_media_list_release( p_mlist->p_media_provider );
305
306     if( p_mlist->p_query )
307         libvlc_tag_query_release( p_mlist->p_query );
308
309     libvlc_event_manager_release( p_mlist->p_event_manager );
310
311     FOREACH_ARRAY( p_md, p_mlist->items )
312         uninstall_media_descriptor_observer( p_mlist, p_md );
313         libvlc_media_descriptor_release( p_md );
314     FOREACH_END()
315  
316     free( p_mlist );
317 }
318
319 /**************************************************************************
320  *       libvlc_media_list_retain (Public)
321  *
322  * Increase an object refcount.
323  **************************************************************************/
324 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
325 {
326     vlc_mutex_lock( &p_mlist->object_lock );
327     p_mlist->i_refcount++;
328     vlc_mutex_unlock( &p_mlist->object_lock );
329 }
330
331
332 /**************************************************************************
333  *       add_file_content (Public)
334  **************************************************************************/
335 void
336 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
337                                     const char * psz_uri,
338                                     libvlc_exception_t * p_e )
339 {
340     input_item_t * p_input_item;
341     libvlc_media_descriptor_t * p_md;
342
343     p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
344                                 _("Media Library"), 0, NULL, -1 );
345
346     if( !p_input_item )
347     {
348         libvlc_exception_raise( p_e, "Can't create an input item" );
349         return;
350     }
351
352     p_md = libvlc_media_descriptor_new_from_input_item(
353             p_mlist->p_libvlc_instance,
354             p_input_item, p_e );
355
356     if( !p_md )
357     {
358         vlc_gc_decref( p_input_item );
359         return;
360     }
361
362     libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
363     if( libvlc_exception_raised( p_e ) )
364         return;
365
366     input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
367
368     return;
369 }
370
371 /**************************************************************************
372  *       set_name (Public)
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)
377
378 {
379     (void)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 );
384 }
385
386 /**************************************************************************
387  *       name (Public)
388  **************************************************************************/
389 char * libvlc_media_list_name( libvlc_media_list_t * p_mlist,
390                                libvlc_exception_t * p_e)
391 {
392     char *ret;
393     (void)p_e;
394
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 );
398
399     return ret;
400 }
401
402 /**************************************************************************
403  *       libvlc_media_list_count (Public)
404  *
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 )
409 {
410     (void)p_e;
411     return p_mlist->items.i_size;
412 }
413
414 /**************************************************************************
415  *       libvlc_media_list_add_media_descriptor (Public)
416  *
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 )
423 {
424     (void)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 );
429 }
430
431 /**************************************************************************
432  *       libvlc_media_list_insert_media_descriptor (Public)
433  *
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,
439                                    int index,
440                                    libvlc_exception_t * p_e )
441 {
442     (void)p_e;
443     libvlc_media_descriptor_retain( p_md );
444
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 );
448 }
449
450 /**************************************************************************
451  *       libvlc_media_list_remove_index (Public)
452  *
453  * Lock should be hold when entering.
454  **************************************************************************/
455 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
456                                      int index,
457                                      libvlc_exception_t * p_e )
458 {
459     libvlc_media_descriptor_t * p_md;
460
461     p_md = ARRAY_VAL( p_mlist->items, index );
462
463     uninstall_media_descriptor_observer( p_mlist, p_md );
464
465     ARRAY_REMOVE( p_mlist->items, index )
466     notify_item_deletion( p_mlist, p_md, index );
467
468     libvlc_media_descriptor_release( p_md );
469 }
470
471 /**************************************************************************
472  *       libvlc_media_list_item_at_index (Public)
473  *
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,
478                                  int index,
479                                  libvlc_exception_t * p_e )
480 {
481     libvlc_media_descriptor_t * p_md =  ARRAY_VAL( p_mlist->items, index );
482     libvlc_media_descriptor_retain( p_md );
483     return p_md;
484 }
485
486 /**************************************************************************
487  *       libvlc_media_list_index_of_item (Public)
488  *
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 )
495 {
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 */
500     FOREACH_END()
501     return -1;
502 }
503
504 /**************************************************************************
505  *       libvlc_media_list_lock (Public)
506  *
507  * The lock must be held in access operations. It is never used in the
508  * Public method.
509  **************************************************************************/
510 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
511 {
512     vlc_mutex_lock( &p_mlist->object_lock );
513 }
514
515
516 /**************************************************************************
517  *       libvlc_media_list_unlock (Public)
518  *
519  * The lock must be held in access operations
520  **************************************************************************/
521 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
522 {
523     vlc_mutex_unlock( &p_mlist->object_lock );
524 }
525
526
527
528 /**************************************************************************
529  *       libvlc_media_list_p_event_manager (Public)
530  *
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 )
536 {
537     (void)p_e;
538     return p_mlist->p_event_manager;
539 }
540
541 /**************************************************************************
542  *       libvlc_media_list_dynamic_sublist (Public)
543  *
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 )
550 {
551     libvlc_media_list_t * p_submlist;
552     libvlc_event_manager_t * p_em;
553     int count, i;
554
555     (void)p_e;
556
557     p_submlist = libvlc_media_list_new( p_mlist->p_libvlc_instance, p_e );
558     if( !p_submlist )
559     {
560         if( !libvlc_exception_raised( p_e ) )
561             libvlc_exception_raise( p_e, "Can't get the new media_list" );
562         return NULL;
563     }
564
565     /* We have a query */
566     libvlc_tag_query_retain( p_query );
567     p_submlist->p_query = p_query;
568
569     /* We have a media provider */
570     libvlc_media_list_retain( p_mlist );
571     p_submlist->p_media_provider = p_mlist;
572
573
574     libvlc_media_list_lock( p_submlist );        
575     
576     count = libvlc_media_list_count( p_mlist, p_e );
577
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++ )
581     {
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 );
586     }
587
588     /* And we will listen to its event, so we can update p_submlist
589      * accordingly */
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 );
597
598     libvlc_media_list_unlock( p_submlist );        
599
600     return p_submlist;
601 }