]> git.sesse.net Git - vlc/blob - lib/media_list.c
direct3d11: give enough room for the \0 in the string
[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 /*
48  * Private functions
49  */
50
51
52
53 /**************************************************************************
54  *       notify_item_addition (private)
55  *
56  * Do the appropriate action when an item is deleted.
57  **************************************************************************/
58 static void
59 notify_item_addition( libvlc_media_list_t * p_mlist,
60                       libvlc_media_t * p_md,
61                       int index,
62                       EventPlaceInTime event_status )
63 {
64     libvlc_event_t event;
65
66     /* Construct the event */
67     if( event_status == EventDidHappen )
68     {
69         event.type = libvlc_MediaListItemAdded;
70         event.u.media_list_item_added.item = p_md;
71         event.u.media_list_item_added.index = index;
72     }
73     else /* if( event_status == EventWillHappen ) */
74     {
75         event.type = libvlc_MediaListWillAddItem;
76         event.u.media_list_will_add_item.item = p_md;
77         event.u.media_list_will_add_item.index = index;
78     }
79
80     /* Send the event */
81     libvlc_event_send( p_mlist->p_event_manager, &event );
82 }
83
84 /**************************************************************************
85  *       notify_item_deletion (private)
86  *
87  * Do the appropriate action when an item is added.
88  **************************************************************************/
89 static void
90 notify_item_deletion( libvlc_media_list_t * p_mlist,
91                       libvlc_media_t * p_md,
92                       int index,
93                       EventPlaceInTime event_status )
94 {
95     libvlc_event_t event;
96
97     /* Construct the event */
98     if( event_status == EventDidHappen )
99     {
100         event.type = libvlc_MediaListItemDeleted;
101         event.u.media_list_item_deleted.item = p_md;
102         event.u.media_list_item_deleted.index = index;
103     }
104     else /* if( event_status == EventWillHappen ) */
105     {
106         event.type = libvlc_MediaListWillDeleteItem;
107         event.u.media_list_will_delete_item.item = p_md;
108         event.u.media_list_will_delete_item.index = index;
109     }
110
111     /* Send the event */
112     libvlc_event_send( p_mlist->p_event_manager, &event );
113 }
114
115 /* LibVLC internal */
116 void libvlc_media_list_internal_end_reached( libvlc_media_list_t * p_mlist )
117 {
118     libvlc_event_t event;
119
120     event.type = libvlc_MediaListEndReached;
121
122     /* Send the event */
123     libvlc_event_send( p_mlist->p_event_manager, &event );
124 }
125
126 /**************************************************************************
127  *       static mlist_is_writable (private)
128  **************************************************************************/
129 static inline
130 bool mlist_is_writable( libvlc_media_list_t *p_mlist )
131 {
132     if( !p_mlist||p_mlist->b_read_only )
133     {
134         /* We are read-only from user side */
135         libvlc_printerr( "Attempt to write a read-only media list" );
136         return false;
137     }
138     return true;
139 }
140
141 /*
142  * Public libvlc functions
143  */
144
145 /**************************************************************************
146  *       libvlc_media_list_new (Public)
147  *
148  * Init an object.
149  **************************************************************************/
150 libvlc_media_list_t *
151 libvlc_media_list_new( libvlc_instance_t * p_inst )
152 {
153     libvlc_media_list_t * p_mlist;
154
155     p_mlist = malloc(sizeof(libvlc_media_list_t));
156     if( unlikely(p_mlist == NULL) )
157     {
158         libvlc_printerr( "Not enough memory" );
159         return NULL;
160     }
161
162     p_mlist->p_libvlc_instance = p_inst;
163     p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst );
164     if( unlikely(p_mlist->p_event_manager == NULL) )
165     {
166         free(p_mlist);
167         return NULL;
168     }
169
170     p_mlist->b_read_only = false;
171
172     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
173             libvlc_MediaListItemAdded );
174     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
175             libvlc_MediaListWillAddItem );
176     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
177             libvlc_MediaListItemDeleted );
178     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
179             libvlc_MediaListWillDeleteItem );
180     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
181             libvlc_MediaListEndReached );
182
183     vlc_mutex_init( &p_mlist->object_lock );
184     vlc_mutex_init( &p_mlist->refcount_lock ); // FIXME: spinlock?
185
186     vlc_array_init( &p_mlist->items );
187     assert( p_mlist->items.i_count == 0 );
188     p_mlist->i_refcount = 1;
189     p_mlist->p_md = NULL;
190     p_mlist->p_internal_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_mutex_destroy( &p_mlist->refcount_lock );
228     vlc_array_clear( &p_mlist->items );
229
230     free( p_mlist );
231 }
232
233 /**************************************************************************
234  *       libvlc_media_list_retain (Public)
235  *
236  * Increase an object refcount.
237  **************************************************************************/
238 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
239 {
240     vlc_mutex_lock( &p_mlist->refcount_lock );
241     p_mlist->i_refcount++;
242     vlc_mutex_unlock( &p_mlist->refcount_lock );
243 }
244
245
246 /**************************************************************************
247  *       add_file_content (Public)
248  **************************************************************************/
249 int
250 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
251                                     const char * psz_uri )
252 {
253     input_item_t * p_input_item;
254     libvlc_media_t * p_md;
255
256     p_input_item = input_item_NewExt( psz_uri,
257                                          _("Media Library"), 0, NULL, 0, -1 );
258
259     if( !p_input_item )
260     {
261         libvlc_printerr( "Not enough memory" );
262         return -1;
263     }
264
265     p_md = libvlc_media_new_from_input_item( p_mlist->p_libvlc_instance,
266                                              p_input_item );
267     if( !p_md )
268     {
269         vlc_gc_decref( p_input_item );
270         return -1;
271     }
272
273     if( libvlc_media_list_add_media( p_mlist, p_md ) )
274     {
275 #warning Missing error handling!
276         /* printerr and leaks */
277         return -1;
278     }
279
280     input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item );
281     return 0;
282 }
283
284 /**************************************************************************
285  *       set_media (Public)
286  **************************************************************************/
287 void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
288                                   libvlc_media_t * p_md )
289
290 {
291     vlc_mutex_lock( &p_mlist->object_lock );
292     if( p_mlist->p_internal_md || !mlist_is_writable(p_mlist) )
293     {
294         vlc_mutex_unlock( &p_mlist->object_lock );
295         return;
296     }
297     libvlc_media_release( p_mlist->p_md );
298     libvlc_media_retain( p_md );
299     p_mlist->p_md = p_md;
300     vlc_mutex_unlock( &p_mlist->object_lock );
301 }
302
303 /**************************************************************************
304  *       media (Public)
305  *
306  * If this media_list comes is a media's subitems,
307  * This holds the corresponding media.
308  * This md is also seen as the information holder for the media_list.
309  * Indeed a media_list can have meta information through this
310  * media.
311  **************************************************************************/
312 libvlc_media_t *
313 libvlc_media_list_media( libvlc_media_list_t * p_mlist )
314 {
315     libvlc_media_t *p_md;
316
317     vlc_mutex_lock( &p_mlist->object_lock );
318     p_md = p_mlist->p_internal_md ? p_mlist->p_internal_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 held when entering.
330  **************************************************************************/
331 int libvlc_media_list_count( libvlc_media_list_t * p_mlist )
332 {
333     return vlc_array_count( &p_mlist->items );
334 }
335
336 /**************************************************************************
337  *       libvlc_media_list_add_media (Public)
338  *
339  * Lock should be held when entering.
340  **************************************************************************/
341 int libvlc_media_list_add_media( libvlc_media_list_t * p_mlist,
342                                  libvlc_media_t * p_md )
343 {
344     if( !mlist_is_writable(p_mlist) )
345         return -1;
346     libvlc_media_list_internal_add_media( p_mlist, p_md );
347     return 0;
348 }
349
350 /* LibVLC internal version */
351 void libvlc_media_list_internal_add_media( libvlc_media_list_t * p_mlist,
352                                            libvlc_media_t * p_md )
353 {
354     libvlc_media_retain( p_md );
355
356     notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
357                           EventWillHappen );
358     vlc_array_append( &p_mlist->items, p_md );
359     notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
360                           EventDidHappen );
361 }
362
363 /**************************************************************************
364  *       libvlc_media_list_insert_media (Public)
365  *
366  * Lock should be hold when entering.
367  **************************************************************************/
368 int libvlc_media_list_insert_media( libvlc_media_list_t * p_mlist,
369                                     libvlc_media_t * p_md,
370                                     int index )
371 {
372     if( !mlist_is_writable(p_mlist) )
373         return -1;
374     libvlc_media_list_internal_insert_media( p_mlist, p_md, index );
375     return 0;
376 }
377
378 /* LibVLC internal version */
379 void libvlc_media_list_internal_insert_media( libvlc_media_list_t * p_mlist,
380                                               libvlc_media_t * p_md,
381                                               int index )
382 {
383     libvlc_media_retain( p_md );
384
385     notify_item_addition( p_mlist, p_md, index, EventWillHappen );
386     vlc_array_insert( &p_mlist->items, p_md, index );
387     notify_item_addition( p_mlist, p_md, index, EventDidHappen );
388 }
389
390 /**************************************************************************
391  *       libvlc_media_list_remove_index (Public)
392  *
393  * Lock should be held when entering.
394  **************************************************************************/
395 int libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
396                                      int index )
397 {
398     if( !mlist_is_writable(p_mlist) )
399         return -1;
400     return libvlc_media_list_internal_remove_index( p_mlist, index );
401 }
402
403 /* LibVLC internal version */
404 int libvlc_media_list_internal_remove_index( libvlc_media_list_t * p_mlist,
405                                              int index )
406 {
407     libvlc_media_t * p_md;
408
409     if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
410     {
411         libvlc_printerr( "Index out of bounds" );
412         return -1;
413     }
414
415     p_md = vlc_array_item_at_index( &p_mlist->items, index );
416
417     notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
418     vlc_array_remove( &p_mlist->items, index );
419     notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
420
421     libvlc_media_release( p_md );
422     return 0;
423 }
424
425 /**************************************************************************
426  *       libvlc_media_list_item_at_index (Public)
427  *
428  * Lock should be held when entering.
429  **************************************************************************/
430 libvlc_media_t *
431 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
432                                  int index )
433 {
434     libvlc_media_t * p_md;
435
436     if( index < 0 || index >= vlc_array_count( &p_mlist->items ))
437     {
438         libvlc_printerr( "Index out of bounds" );
439         return NULL;
440     }
441
442     p_md = vlc_array_item_at_index( &p_mlist->items, index );
443     libvlc_media_retain( p_md );
444     return p_md;
445 }
446
447 /**************************************************************************
448  *       libvlc_media_list_index_of_item (Public)
449  *
450  * Lock should be held when entering.
451  * Warning: this function returns the first matching item.
452  **************************************************************************/
453 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
454                                      libvlc_media_t * p_searched_md )
455 {
456     libvlc_media_t * p_md;
457     int i;
458     for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
459     {
460         p_md = vlc_array_item_at_index( &p_mlist->items, i );
461         if( p_searched_md == p_md )
462             return i;
463     }
464     libvlc_printerr( "Media not found" );
465     return -1;
466 }
467
468 /**************************************************************************
469  *       libvlc_media_list_is_readonly (Public)
470  *
471  * This indicates if this media list is read-only from a user point of view
472  **************************************************************************/
473 int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
474 {
475     return p_mlist->b_read_only;
476 }
477
478 /**************************************************************************
479  *       libvlc_media_list_lock (Public)
480  *
481  * The lock must be held in access operations. It is never used in the
482  * Public method.
483  **************************************************************************/
484 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
485 {
486     vlc_mutex_lock( &p_mlist->object_lock );
487 }
488
489
490 /**************************************************************************
491  *       libvlc_media_list_unlock (Public)
492  *
493  * The lock must be held in access operations
494  **************************************************************************/
495 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
496 {
497     vlc_mutex_unlock( &p_mlist->object_lock );
498 }
499
500
501 /**************************************************************************
502  *       libvlc_media_list_p_event_manager (Public)
503  *
504  * The p_event_manager is immutable, so you don't have to hold the lock
505  **************************************************************************/
506 libvlc_event_manager_t *
507 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist )
508 {
509     return p_mlist->p_event_manager;
510 }