]> git.sesse.net Git - vlc/blob - lib/media_list.c
libvlc: add libvlc_MediaListEndReached event
[vlc] / lib / media_list.c
1 /*****************************************************************************
2  * media_list.c: libvlc new API media list functions
3  *****************************************************************************
4  * Copyright (C) 2007 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 /* LibVLC internal */
126 void libvlc_media_list_internal_end_reached( libvlc_media_list_t * p_mlist )
127 {
128     libvlc_event_t event;
129
130     event.type = libvlc_MediaListEndReached;
131
132     /* Send the event */
133     libvlc_event_send( p_mlist->p_event_manager, &event );
134 }
135
136 /**************************************************************************
137  *       static mlist_is_writable (private)
138  **************************************************************************/
139 static inline
140 bool mlist_is_writable( libvlc_media_list_t *p_mlist )
141 {
142     if( !p_mlist||p_mlist->b_read_only )
143     {
144         /* We are read-only from user side */
145         libvlc_printerr( "Attempt to write a read-only media list" );
146         return false;
147     }
148     return true;
149 }
150
151 /*
152  * Public libvlc functions
153  */
154
155 /**************************************************************************
156  *       libvlc_media_list_new (Public)
157  *
158  * Init an object.
159  **************************************************************************/
160 libvlc_media_list_t *
161 libvlc_media_list_new( libvlc_instance_t * p_inst )
162 {
163     libvlc_media_list_t * p_mlist;
164
165     p_mlist = malloc(sizeof(libvlc_media_list_t));
166     if( unlikely(p_mlist == NULL) )
167     {
168         libvlc_printerr( "Not enough memory" );
169         return NULL;
170     }
171
172     p_mlist->p_libvlc_instance = p_inst;
173     p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst );
174     if( unlikely(p_mlist->p_event_manager == NULL) )
175     {
176         free(p_mlist);
177         return NULL;
178     }
179
180     p_mlist->b_read_only = false;
181
182     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
183             libvlc_MediaListItemAdded );
184     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
185             libvlc_MediaListWillAddItem );
186     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
187             libvlc_MediaListItemDeleted );
188     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
189             libvlc_MediaListWillDeleteItem );
190     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
191             libvlc_MediaListEndReached );
192
193     vlc_mutex_init( &p_mlist->object_lock );
194     vlc_mutex_init( &p_mlist->refcount_lock ); // FIXME: spinlock?
195
196     vlc_array_init( &p_mlist->items );
197     assert( p_mlist->items.i_count == 0 );
198     p_mlist->i_refcount = 1;
199     p_mlist->p_md = NULL;
200     p_mlist->p_internal_md = NULL;
201
202     return p_mlist;
203 }
204
205 /**************************************************************************
206  *       libvlc_media_list_release (Public)
207  *
208  * Release an object.
209  **************************************************************************/
210 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
211 {
212     libvlc_media_t * p_md;
213     int i;
214
215     vlc_mutex_lock( &p_mlist->refcount_lock );
216     p_mlist->i_refcount--;
217     if( p_mlist->i_refcount > 0 )
218     {
219         vlc_mutex_unlock( &p_mlist->refcount_lock );
220         return;
221     }
222     vlc_mutex_unlock( &p_mlist->refcount_lock );
223
224     /* Refcount null, time to free */
225
226     libvlc_event_manager_release( p_mlist->p_event_manager );
227
228     libvlc_media_release( p_mlist->p_md );
229
230     for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
231     {
232         p_md = vlc_array_item_at_index( &p_mlist->items, i );
233         libvlc_media_release( p_md );
234     }
235
236     vlc_mutex_destroy( &p_mlist->object_lock );
237     vlc_mutex_destroy( &p_mlist->refcount_lock );
238     vlc_array_clear( &p_mlist->items );
239
240     free( p_mlist );
241 }
242
243 /**************************************************************************
244  *       libvlc_media_list_retain (Public)
245  *
246  * Increase an object refcount.
247  **************************************************************************/
248 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
249 {
250     vlc_mutex_lock( &p_mlist->refcount_lock );
251     p_mlist->i_refcount++;
252     vlc_mutex_unlock( &p_mlist->refcount_lock );
253 }
254
255
256 /**************************************************************************
257  *       add_file_content (Public)
258  **************************************************************************/
259 int
260 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
261                                     const char * psz_uri )
262 {
263     input_item_t * p_input_item;
264     libvlc_media_t * p_md;
265
266     p_input_item = input_item_NewExt( psz_uri,
267                                          _("Media Library"), 0, NULL, 0, -1 );
268
269     if( !p_input_item )
270     {
271         libvlc_printerr( "Not enough memory" );
272         return -1;
273     }
274
275     p_md = libvlc_media_new_from_input_item( p_mlist->p_libvlc_instance,
276                                              p_input_item );
277     if( !p_md )
278     {
279         vlc_gc_decref( p_input_item );
280         return -1;
281     }
282
283     if( libvlc_media_list_add_media( p_mlist, p_md ) )
284     {
285 #warning Missing error handling!
286         /* printerr and leaks */
287         return -1;
288     }
289
290     input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item );
291     return 0;
292 }
293
294 /**************************************************************************
295  *       set_media (Public)
296  **************************************************************************/
297 void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
298                                   libvlc_media_t * p_md )
299
300 {
301     vlc_mutex_lock( &p_mlist->object_lock );
302     if( p_mlist->p_internal_md || !mlist_is_writable(p_mlist) )
303     {
304         vlc_mutex_unlock( &p_mlist->object_lock );
305         return;
306     }
307     libvlc_media_release( p_mlist->p_md );
308     libvlc_media_retain( p_md );
309     p_mlist->p_md = p_md;
310     vlc_mutex_unlock( &p_mlist->object_lock );
311 }
312
313 /**************************************************************************
314  *       media (Public)
315  *
316  * If this media_list comes is a media's subitems,
317  * This holds the corresponding media.
318  * This md is also seen as the information holder for the media_list.
319  * Indeed a media_list can have meta information through this
320  * media.
321  **************************************************************************/
322 libvlc_media_t *
323 libvlc_media_list_media( libvlc_media_list_t * p_mlist )
324 {
325     libvlc_media_t *p_md;
326
327     vlc_mutex_lock( &p_mlist->object_lock );
328     p_md = p_mlist->p_internal_md ? p_mlist->p_internal_md : p_mlist->p_md;
329     if( p_md )
330         libvlc_media_retain( p_md );
331     vlc_mutex_unlock( &p_mlist->object_lock );
332
333     return p_md;
334 }
335
336 /**************************************************************************
337  *       libvlc_media_list_count (Public)
338  *
339  * Lock should be held when entering.
340  **************************************************************************/
341 int libvlc_media_list_count( libvlc_media_list_t * p_mlist )
342 {
343     return vlc_array_count( &p_mlist->items );
344 }
345
346 /**************************************************************************
347  *       libvlc_media_list_add_media (Public)
348  *
349  * Lock should be held when entering.
350  **************************************************************************/
351 int libvlc_media_list_add_media( libvlc_media_list_t * p_mlist,
352                                  libvlc_media_t * p_md )
353 {
354     if( !mlist_is_writable(p_mlist) )
355         return -1;
356     libvlc_media_list_internal_add_media( p_mlist, p_md );
357     return 0;
358 }
359
360 /* LibVLC internal version */
361 void libvlc_media_list_internal_add_media( libvlc_media_list_t * p_mlist,
362                                            libvlc_media_t * p_md )
363 {
364     libvlc_media_retain( p_md );
365
366     notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
367                           EventWillHappen );
368     vlc_array_append( &p_mlist->items, p_md );
369     notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
370                           EventDidHappen );
371 }
372
373 /**************************************************************************
374  *       libvlc_media_list_insert_media (Public)
375  *
376  * Lock should be hold when entering.
377  **************************************************************************/
378 int libvlc_media_list_insert_media( libvlc_media_list_t * p_mlist,
379                                     libvlc_media_t * p_md,
380                                     int index )
381 {
382     if( !mlist_is_writable(p_mlist) )
383         return -1;
384     libvlc_media_list_internal_insert_media( p_mlist, p_md, index );
385     return 0;
386 }
387
388 /* LibVLC internal version */
389 void libvlc_media_list_internal_insert_media( libvlc_media_list_t * p_mlist,
390                                               libvlc_media_t * p_md,
391                                               int index )
392 {
393     libvlc_media_retain( p_md );
394
395     notify_item_addition( p_mlist, p_md, index, EventWillHappen );
396     vlc_array_insert( &p_mlist->items, p_md, index );
397     notify_item_addition( p_mlist, p_md, index, EventDidHappen );
398 }
399
400 /**************************************************************************
401  *       libvlc_media_list_remove_index (Public)
402  *
403  * Lock should be held when entering.
404  **************************************************************************/
405 int libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
406                                      int index )
407 {
408     if( !mlist_is_writable(p_mlist) )
409         return -1;
410     return libvlc_media_list_internal_remove_index( p_mlist, index );
411 }
412
413 /* LibVLC internal version */
414 int libvlc_media_list_internal_remove_index( libvlc_media_list_t * p_mlist,
415                                              int index )
416 {
417     libvlc_media_t * p_md;
418
419     if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
420     {
421         libvlc_printerr( "Index out of bounds" );
422         return -1;
423     }
424
425     p_md = vlc_array_item_at_index( &p_mlist->items, index );
426
427     notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
428     vlc_array_remove( &p_mlist->items, index );
429     notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
430
431     libvlc_media_release( p_md );
432     return 0;
433 }
434
435 /**************************************************************************
436  *       libvlc_media_list_item_at_index (Public)
437  *
438  * Lock should be held when entering.
439  **************************************************************************/
440 libvlc_media_t *
441 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
442                                  int index )
443 {
444     libvlc_media_t * p_md;
445
446     if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
447     {
448         libvlc_printerr( "Index out of bounds" );
449         return NULL;
450     }
451
452     p_md = vlc_array_item_at_index( &p_mlist->items, index );
453     libvlc_media_retain( p_md );
454     return p_md;
455 }
456
457 /**************************************************************************
458  *       libvlc_media_list_index_of_item (Public)
459  *
460  * Lock should be held when entering.
461  * Warning: this function returns the first matching item.
462  **************************************************************************/
463 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
464                                      libvlc_media_t * p_searched_md )
465 {
466     libvlc_media_t * p_md;
467     int i;
468     for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
469     {
470         p_md = vlc_array_item_at_index( &p_mlist->items, i );
471         if( p_searched_md == p_md )
472             return i;
473     }
474     libvlc_printerr( "Media not found" );
475     return -1;
476 }
477
478 /**************************************************************************
479  *       libvlc_media_list_is_readonly (Public)
480  *
481  * This indicates if this media list is read-only from a user point of view
482  **************************************************************************/
483 int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
484 {
485     return p_mlist->b_read_only;
486 }
487
488 /**************************************************************************
489  *       libvlc_media_list_lock (Public)
490  *
491  * The lock must be held in access operations. It is never used in the
492  * Public method.
493  **************************************************************************/
494 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
495 {
496     vlc_mutex_lock( &p_mlist->object_lock );
497 }
498
499
500 /**************************************************************************
501  *       libvlc_media_list_unlock (Public)
502  *
503  * The lock must be held in access operations
504  **************************************************************************/
505 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
506 {
507     vlc_mutex_unlock( &p_mlist->object_lock );
508 }
509
510
511 /**************************************************************************
512  *       libvlc_media_list_p_event_manager (Public)
513  *
514  * The p_event_manager is immutable, so you don't have to hold the lock
515  **************************************************************************/
516 libvlc_event_manager_t *
517 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist )
518 {
519     return p_mlist->p_event_manager;
520 }