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