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