libvlc: include config.h when needed
[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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <assert.h>
29
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_media.h>
32 #include <vlc/libvlc_media_list.h>
33 #include <vlc/libvlc_events.h>
34
35 #include <vlc_common.h>
36 #include <vlc_input.h>
37
38 #include "libvlc_internal.h"
39 #include "media_internal.h" // libvlc_media_new_from_input_item()
40 #include "media_list_internal.h"
41
42 typedef enum EventPlaceInTime {
43     EventWillHappen,
44     EventDidHappen
45 } EventPlaceInTime;
46
47 //#define DEBUG_MEDIA_LIST
48
49 #ifdef DEBUG_MEDIA_LIST
50 # define trace( fmt, ... ) printf( "%s(): " fmt, __FUNCTION__, ##__VA_ARGS__ )
51 #else
52 # define trace( ... )
53 #endif
54
55 /*
56  * Private functions
57  */
58
59
60
61 /**************************************************************************
62  *       notify_item_addition (private)
63  *
64  * Do the appropriate action when an item is deleted.
65  **************************************************************************/
66 static void
67 notify_item_addition( libvlc_media_list_t * p_mlist,
68                       libvlc_media_t * p_md,
69                       int index,
70                       EventPlaceInTime event_status )
71 {
72     libvlc_event_t event;
73
74     /* Construct the event */
75     if( event_status == EventDidHappen )
76     {
77         trace("item was added at index %d\n", index);
78         event.type = libvlc_MediaListItemAdded;
79         event.u.media_list_item_added.item = p_md;
80         event.u.media_list_item_added.index = index;
81     }
82     else /* if( event_status == EventWillHappen ) */
83     {
84         event.type = libvlc_MediaListWillAddItem;
85         event.u.media_list_will_add_item.item = p_md;
86         event.u.media_list_will_add_item.index = index;
87     }
88
89     /* Send the event */
90     libvlc_event_send( p_mlist->p_event_manager, &event );
91 }
92
93 /**************************************************************************
94  *       notify_item_deletion (private)
95  *
96  * Do the appropriate action when an item is added.
97  **************************************************************************/
98 static void
99 notify_item_deletion( libvlc_media_list_t * p_mlist,
100                       libvlc_media_t * p_md,
101                       int index,
102                       EventPlaceInTime event_status )
103 {
104     libvlc_event_t event;
105
106     /* Construct the event */
107     if( event_status == EventDidHappen )
108     {
109         trace("item at index %d was deleted\n", index);
110         event.type = libvlc_MediaListItemDeleted;
111         event.u.media_list_item_deleted.item = p_md;
112         event.u.media_list_item_deleted.index = index;
113     }
114     else /* if( event_status == EventWillHappen ) */
115     {
116         event.type = libvlc_MediaListWillDeleteItem;
117         event.u.media_list_will_delete_item.item = p_md;
118         event.u.media_list_will_delete_item.index = index;
119     }
120
121     /* Send the event */
122     libvlc_event_send( p_mlist->p_event_manager, &event );
123 }
124
125 /**************************************************************************
126  *       static mlist_is_writable (private)
127  *
128  * Raise exception and return 0 when the media_list instance is read-only,
129  * or else return 1.
130  **************************************************************************/
131 static inline
132 int mlist_is_writable( libvlc_media_list_t *p_mlist, libvlc_exception_t *p_e )
133 {
134     if( !p_mlist||p_mlist->b_read_only )
135     {
136         /* We are read-only from user side */
137         libvlc_exception_raise( p_e, "Cannot write to read-only media list." );
138         return 0;
139     }
140     return 1;
141 }
142
143 /*
144  * Public libvlc functions
145  */
146
147 /**************************************************************************
148  *       libvlc_media_list_new (Public)
149  *
150  * Init an object.
151  **************************************************************************/
152 libvlc_media_list_t *
153 libvlc_media_list_new( libvlc_instance_t * p_inst,
154                        libvlc_exception_t * p_e )
155 {
156     libvlc_media_list_t * p_mlist;
157
158     p_mlist = malloc(sizeof(libvlc_media_list_t));
159     if( !p_mlist )
160         return NULL;
161
162     p_mlist->p_libvlc_instance = p_inst;
163     p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
164
165     /* Code for that one should be handled in flat_media_list.c */
166     p_mlist->p_flat_mlist = NULL;
167     p_mlist->b_read_only = false;
168
169     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
170             libvlc_MediaListItemAdded, p_e );
171     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
172             libvlc_MediaListWillAddItem, p_e );
173     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
174             libvlc_MediaListItemDeleted, p_e );
175     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
176             libvlc_MediaListWillDeleteItem, p_e );
177
178     if( libvlc_exception_raised( p_e ) )
179     {
180         libvlc_event_manager_release( p_mlist->p_event_manager );
181         free( p_mlist );
182         return NULL;
183     }
184
185     vlc_mutex_init( &p_mlist->object_lock );
186     vlc_mutex_init( &p_mlist->refcount_lock ); // FIXME: spinlock?
187
188     vlc_array_init( &p_mlist->items );
189     p_mlist->i_refcount = 1;
190     p_mlist->p_md = NULL;
191
192     return p_mlist;
193 }
194
195 /**************************************************************************
196  *       libvlc_media_list_release (Public)
197  *
198  * Release an object.
199  **************************************************************************/
200 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
201 {
202     libvlc_media_t * p_md;
203     int i;
204
205     vlc_mutex_lock( &p_mlist->refcount_lock );
206     p_mlist->i_refcount--;
207     if( p_mlist->i_refcount > 0 )
208     {
209         vlc_mutex_unlock( &p_mlist->refcount_lock );
210         return;
211     }
212     vlc_mutex_unlock( &p_mlist->refcount_lock );
213
214     /* Refcount null, time to free */
215
216     libvlc_event_manager_release( p_mlist->p_event_manager );
217
218     libvlc_media_release( p_mlist->p_md );
219
220     for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
221     {
222         p_md = vlc_array_item_at_index( &p_mlist->items, i );
223         libvlc_media_release( p_md );
224     }
225
226     vlc_mutex_destroy( &p_mlist->object_lock );
227     vlc_array_clear( &p_mlist->items );
228
229     free( p_mlist );
230 }
231
232 /**************************************************************************
233  *       libvlc_media_list_retain (Public)
234  *
235  * Increase an object refcount.
236  **************************************************************************/
237 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
238 {
239     vlc_mutex_lock( &p_mlist->refcount_lock );
240     p_mlist->i_refcount++;
241     vlc_mutex_unlock( &p_mlist->refcount_lock );
242 }
243
244
245 /**************************************************************************
246  *       add_file_content (Public)
247  **************************************************************************/
248 void
249 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
250                                     const char * psz_uri,
251                                     libvlc_exception_t * p_e )
252 {
253     input_item_t * p_input_item;
254     libvlc_media_t * p_md;
255
256     p_input_item = input_item_NewExt(
257                            p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
258                                          _("Media Library"), 0, NULL, 0, -1 );
259
260     if( !p_input_item )
261     {
262         libvlc_exception_raise( p_e, "Can't create an input item" );
263         return;
264     }
265
266     p_md = libvlc_media_new_from_input_item(
267             p_mlist->p_libvlc_instance,
268             p_input_item, p_e );
269
270     if( !p_md )
271     {
272         vlc_gc_decref( p_input_item );
273         return;
274     }
275
276     libvlc_media_list_add_media( p_mlist, p_md, p_e );
277     if( libvlc_exception_raised( p_e ) )
278         return;
279
280     input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, true );
281
282     return;
283 }
284
285 /**************************************************************************
286  *       set_media (Public)
287  **************************************************************************/
288 void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
289                                              libvlc_media_t * p_md,
290                                              libvlc_exception_t * p_e)
291
292 {
293     VLC_UNUSED(p_e);
294     vlc_mutex_lock( &p_mlist->object_lock );
295     libvlc_media_release( p_mlist->p_md );
296     libvlc_media_retain( p_md );
297     p_mlist->p_md = p_md;
298     vlc_mutex_unlock( &p_mlist->object_lock );
299 }
300
301 /**************************************************************************
302  *       media (Public)
303  *
304  * If this media_list comes is a media's subitems,
305  * This holds the corresponding media.
306  * This md is also seen as the information holder for the media_list.
307  * Indeed a media_list can have meta information through this
308  * media.
309  **************************************************************************/
310 libvlc_media_t *
311 libvlc_media_list_media( libvlc_media_list_t * p_mlist,
312                                     libvlc_exception_t * p_e)
313 {
314     libvlc_media_t *p_md;
315     VLC_UNUSED(p_e);
316
317     vlc_mutex_lock( &p_mlist->object_lock );
318     p_md = p_mlist->p_md;
319     if( p_md )
320         libvlc_media_retain( p_md );
321     vlc_mutex_unlock( &p_mlist->object_lock );
322
323     return p_md;
324 }
325
326 /**************************************************************************
327  *       libvlc_media_list_count (Public)
328  *
329  * Lock should be hold when entering.
330  **************************************************************************/
331 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
332                              libvlc_exception_t * p_e )
333 {
334     VLC_UNUSED(p_e);
335     return vlc_array_count( &p_mlist->items );
336 }
337
338 /**************************************************************************
339  *       libvlc_media_list_add_media (Public)
340  *
341  * Lock should be held when entering.
342  **************************************************************************/
343 void libvlc_media_list_add_media(
344                                    libvlc_media_list_t * p_mlist,
345                                    libvlc_media_t * p_md,
346                                    libvlc_exception_t * p_e )
347 {
348     if( mlist_is_writable(p_mlist,p_e) )
349         _libvlc_media_list_add_media( p_mlist, p_md, p_e );
350 }
351
352 /* LibVLC internal version */
353 void _libvlc_media_list_add_media(
354                                    libvlc_media_list_t * p_mlist,
355                                    libvlc_media_t * p_md,
356                                    libvlc_exception_t * p_e )
357 {
358     VLC_UNUSED(p_e);
359     libvlc_media_retain( p_md );
360
361     notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
362                           EventWillHappen );
363     vlc_array_append( &p_mlist->items, p_md );
364     notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
365                           EventDidHappen );
366 }
367
368 /**************************************************************************
369  *       libvlc_media_list_insert_media (Public)
370  *
371  * Lock should be hold when entering.
372  **************************************************************************/
373 void libvlc_media_list_insert_media(
374                                    libvlc_media_list_t * p_mlist,
375                                    libvlc_media_t * p_md,
376                                    int index,
377                                    libvlc_exception_t * p_e )
378 {
379     if( mlist_is_writable(p_mlist,p_e) )
380         _libvlc_media_list_insert_media( p_mlist, p_md, index, p_e );
381 }
382
383 /* LibVLC internal version */
384 void _libvlc_media_list_insert_media(
385                                    libvlc_media_list_t * p_mlist,
386                                    libvlc_media_t * p_md,
387                                    int index,
388                                    libvlc_exception_t * p_e )
389 {
390     VLC_UNUSED(p_e);
391     libvlc_media_retain( p_md );
392
393     notify_item_addition( p_mlist, p_md, index, EventWillHappen );
394     vlc_array_insert( &p_mlist->items, p_md, index );
395     notify_item_addition( p_mlist, p_md, index, EventDidHappen );
396 }
397
398 /**************************************************************************
399  *       libvlc_media_list_remove_index (Public)
400  *
401  * Lock should be held when entering.
402  **************************************************************************/
403 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
404                                      int index,
405                                      libvlc_exception_t * p_e )
406 {
407     if( mlist_is_writable(p_mlist,p_e) )
408         _libvlc_media_list_remove_index( p_mlist, index, p_e );
409 }
410
411 /* LibVLC internal version */
412 void _libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
413                                      int index,
414                                      libvlc_exception_t * p_e )
415 {
416     libvlc_media_t * p_md;
417
418     if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
419     {
420         libvlc_exception_raise( p_e, "Index out of bounds");
421         return;
422     }
423
424     p_md = vlc_array_item_at_index( &p_mlist->items, index );
425
426     notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
427     vlc_array_remove( &p_mlist->items, index );
428     notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
429
430     libvlc_media_release( p_md );
431 }
432
433 /**************************************************************************
434  *       libvlc_media_list_item_at_index (Public)
435  *
436  * Lock should be held when entering.
437  **************************************************************************/
438 libvlc_media_t *
439 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
440                                  int index,
441                                  libvlc_exception_t * p_e )
442 {
443     libvlc_media_t * p_md;
444
445     if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
446     {
447         libvlc_exception_raise( p_e, "Index out of bounds");
448         return NULL;
449     }
450
451     p_md = vlc_array_item_at_index( &p_mlist->items, index );
452     libvlc_media_retain( p_md );
453     return p_md;
454 }
455
456 /**************************************************************************
457  *       libvlc_media_list_index_of_item (Public)
458  *
459  * Lock should be held when entering.
460  * Warning: this function returns the first matching item.
461  **************************************************************************/
462 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
463                                      libvlc_media_t * p_searched_md,
464                                      libvlc_exception_t * p_e )
465 {
466     VLC_UNUSED(p_e);
467
468     libvlc_media_t * p_md;
469     int i;
470     for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
471     {
472         p_md = vlc_array_item_at_index( &p_mlist->items, i );
473         if( p_searched_md == p_md )
474             return i;
475     }
476     return -1;
477 }
478
479 /**************************************************************************
480  *       libvlc_media_list_is_readonly (Public)
481  *
482  * This indicates if this media list is read-only from a user point of view
483  **************************************************************************/
484 int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
485 {
486     return p_mlist->b_read_only;
487 }
488
489 /**************************************************************************
490  *       libvlc_media_list_lock (Public)
491  *
492  * The lock must be held in access operations. It is never used in the
493  * Public method.
494  **************************************************************************/
495 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
496 {
497     vlc_mutex_lock( &p_mlist->object_lock );
498 }
499
500
501 /**************************************************************************
502  *       libvlc_media_list_unlock (Public)
503  *
504  * The lock must be held in access operations
505  **************************************************************************/
506 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
507 {
508     vlc_mutex_unlock( &p_mlist->object_lock );
509 }
510
511
512 /**************************************************************************
513  *       libvlc_media_list_p_event_manager (Public)
514  *
515  * The p_event_manager is immutable, so you don't have to hold the lock
516  **************************************************************************/
517 libvlc_event_manager_t *
518 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
519                                     libvlc_exception_t * p_e )
520 {
521     VLC_UNUSED(p_e);
522     return p_mlist->p_event_manager;
523 }