]> git.sesse.net Git - vlc/blob - src/control/media_list.c
control/media_list.c: Don't forget to register_event_type for WillAdd/WillDelete.
[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
128     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
129             libvlc_MediaListItemAdded, p_e );
130     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
131             libvlc_MediaListWillAddItem, p_e );
132     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
133             libvlc_MediaListItemDeleted, p_e );
134     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
135             libvlc_MediaListWillDeleteItem, p_e );
136
137     if( libvlc_exception_raised( p_e ) )
138     {
139         libvlc_event_manager_release( p_mlist->p_event_manager );
140         free( p_mlist );
141         return NULL;
142     }
143
144     vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
145  
146     vlc_array_init( &p_mlist->items );
147     p_mlist->i_refcount = 1;
148     p_mlist->p_md = NULL;
149
150     return p_mlist;
151 }
152
153 /**************************************************************************
154  *       libvlc_media_list_release (Public)
155  *
156  * Release an object.
157  **************************************************************************/
158 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
159 {
160     libvlc_media_descriptor_t * p_md;
161     int i;
162
163     vlc_mutex_lock( &p_mlist->object_lock );
164     p_mlist->i_refcount--;
165     if( p_mlist->i_refcount > 0 )
166     {
167         vlc_mutex_unlock( &p_mlist->object_lock );
168         return;
169     }
170     vlc_mutex_unlock( &p_mlist->object_lock );
171
172     /* Refcount null, time to free */
173
174     libvlc_event_manager_release( p_mlist->p_event_manager );
175
176     if( p_mlist->p_md )
177         libvlc_media_descriptor_release( p_mlist->p_md );
178
179     for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
180     {
181         p_md = vlc_array_item_at_index( &p_mlist->items, i );
182         libvlc_media_descriptor_release( p_md );
183     }
184
185     vlc_mutex_destroy( &p_mlist->object_lock );
186     vlc_array_clear( &p_mlist->items );
187
188     free( p_mlist );
189 }
190
191 /**************************************************************************
192  *       libvlc_media_list_retain (Public)
193  *
194  * Increase an object refcount.
195  **************************************************************************/
196 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
197 {
198     vlc_mutex_lock( &p_mlist->object_lock );
199     p_mlist->i_refcount++;
200     vlc_mutex_unlock( &p_mlist->object_lock );
201 }
202
203
204 /**************************************************************************
205  *       add_file_content (Public)
206  **************************************************************************/
207 void
208 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
209                                     const char * psz_uri,
210                                     libvlc_exception_t * p_e )
211 {
212     input_item_t * p_input_item;
213     libvlc_media_descriptor_t * p_md;
214
215     p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
216                                 _("Media Library"), 0, NULL, -1 );
217
218     if( !p_input_item )
219     {
220         libvlc_exception_raise( p_e, "Can't create an input item" );
221         return;
222     }
223
224     p_md = libvlc_media_descriptor_new_from_input_item(
225             p_mlist->p_libvlc_instance,
226             p_input_item, p_e );
227
228     if( !p_md )
229     {
230         vlc_gc_decref( p_input_item );
231         return;
232     }
233
234     libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
235     if( libvlc_exception_raised( p_e ) )
236         return;
237
238     input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
239
240     return;
241 }
242
243 /**************************************************************************
244  *       set_media_descriptor (Public)
245  **************************************************************************/
246 void libvlc_media_list_set_media_descriptor( libvlc_media_list_t * p_mlist,
247                                              libvlc_media_descriptor_t * p_md,
248                                              libvlc_exception_t * p_e)
249
250 {
251     (void)p_e;
252     vlc_mutex_lock( &p_mlist->object_lock );
253     if( p_mlist->p_md )
254         libvlc_media_descriptor_release( p_mlist->p_md );
255     libvlc_media_descriptor_retain( p_md );
256     p_mlist->p_md = p_md;
257     vlc_mutex_unlock( &p_mlist->object_lock );
258 }
259
260 /**************************************************************************
261  *       media_descriptor (Public)
262  *
263  * If this media_list comes is a media_descriptor's subitems,
264  * This holds the corresponding media_descriptor.
265  * This md is also seen as the information holder for the media_list.
266  * Indeed a media_list can have meta information through this
267  * media_descriptor.
268  **************************************************************************/
269 libvlc_media_descriptor_t *
270 libvlc_media_list_media_descriptor( libvlc_media_list_t * p_mlist,
271                                     libvlc_exception_t * p_e)
272 {
273     libvlc_media_descriptor_t *p_md;
274     (void)p_e;
275
276     vlc_mutex_lock( &p_mlist->object_lock );
277     p_md = p_mlist->p_md;
278     if( p_md )
279         libvlc_media_descriptor_retain( p_md );
280     vlc_mutex_unlock( &p_mlist->object_lock );
281
282     return p_md;
283 }
284
285 /**************************************************************************
286  *       libvlc_media_list_count (Public)
287  *
288  * Lock should be hold when entering.
289  **************************************************************************/
290 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
291                              libvlc_exception_t * p_e )
292 {
293     (void)p_e;
294     return vlc_array_count( &p_mlist->items );
295 }
296
297 /**************************************************************************
298  *       libvlc_media_list_add_media_descriptor (Public)
299  *
300  * Lock should be hold when entering.
301  **************************************************************************/
302 void libvlc_media_list_add_media_descriptor(
303                                    libvlc_media_list_t * p_mlist,
304                                    libvlc_media_descriptor_t * p_md,
305                                    libvlc_exception_t * p_e )
306 {
307     (void)p_e;
308     libvlc_media_descriptor_retain( p_md );
309
310     notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ), EventWillHappen );
311     vlc_array_append( &p_mlist->items, p_md );
312     notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1, EventDidHappen );
313 }
314
315 /**************************************************************************
316  *       libvlc_media_list_insert_media_descriptor (Public)
317  *
318  * Lock should be hold when entering.
319  **************************************************************************/
320 void libvlc_media_list_insert_media_descriptor(
321                                    libvlc_media_list_t * p_mlist,
322                                    libvlc_media_descriptor_t * p_md,
323                                    int index,
324                                    libvlc_exception_t * p_e )
325 {
326     (void)p_e;
327     libvlc_media_descriptor_retain( p_md );
328
329     notify_item_addition( p_mlist, p_md, index, EventWillHappen );
330     vlc_array_insert( &p_mlist->items, p_md, index );
331     notify_item_addition( p_mlist, p_md, index, EventDidHappen );
332 }
333
334 /**************************************************************************
335  *       libvlc_media_list_remove_index (Public)
336  *
337  * Lock should be hold when entering.
338  **************************************************************************/
339 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
340                                      int index,
341                                      libvlc_exception_t * p_e )
342 {
343     libvlc_media_descriptor_t * p_md;
344
345     p_md = vlc_array_item_at_index( &p_mlist->items, index );
346
347     notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
348     vlc_array_remove( &p_mlist->items, index );
349     notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
350
351     libvlc_media_descriptor_release( p_md );
352 }
353
354 /**************************************************************************
355  *       libvlc_media_list_item_at_index (Public)
356  *
357  * Lock should be hold when entering.
358  **************************************************************************/
359 libvlc_media_descriptor_t *
360 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
361                                  int index,
362                                  libvlc_exception_t * p_e )
363 {
364     libvlc_media_descriptor_t * p_md;
365     p_md = vlc_array_item_at_index( &p_mlist->items, index );
366     libvlc_media_descriptor_retain( p_md );
367     return p_md;
368 }
369
370 /**************************************************************************
371  *       libvlc_media_list_index_of_item (Public)
372  *
373  * Lock should be hold when entering.
374  * Warning: this function would return the first matching item
375  **************************************************************************/
376 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
377                                      libvlc_media_descriptor_t * p_searched_md,
378                                      libvlc_exception_t * p_e )
379 {
380     libvlc_media_descriptor_t * p_md;
381     int i;
382     for ( i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
383     {
384         p_md = vlc_array_item_at_index( &p_mlist->items, i );
385         if( p_searched_md == p_md )
386             return i;
387     }
388     return -1;
389 }
390
391 /**************************************************************************
392  *       libvlc_media_list_lock (Public)
393  *
394  * The lock must be held in access operations. It is never used in the
395  * Public method.
396  **************************************************************************/
397 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
398 {
399     vlc_mutex_lock( &p_mlist->object_lock );
400 }
401
402
403 /**************************************************************************
404  *       libvlc_media_list_unlock (Public)
405  *
406  * The lock must be held in access operations
407  **************************************************************************/
408 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
409 {
410     vlc_mutex_unlock( &p_mlist->object_lock );
411 }
412
413
414
415 /**************************************************************************
416  *       libvlc_media_list_p_event_manager (Public)
417  *
418  * The p_event_manager is immutable, so you don't have to hold the lock
419  **************************************************************************/
420 libvlc_event_manager_t *
421 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
422                                     libvlc_exception_t * p_e )
423 {
424     (void)p_e;
425     return p_mlist->p_event_manager;
426 }