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