]> git.sesse.net Git - vlc/blob - src/control/media_list.c
control/media_list.c: Don't forget to increase the md refcount where needed.
[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 /*
30  * Private functions
31  */
32
33 /**************************************************************************
34  *       notify_item_addition (private)
35  *
36  * Call parent playlist and send the appropriate event.
37  **************************************************************************/
38 static void
39 notify_item_addition( libvlc_media_list_t * p_mlist,
40                       libvlc_media_descriptor_t * p_md,
41                       int index )
42 {
43     libvlc_event_t event;
44
45     event.type = libvlc_MediaListItemAdded;
46     event.u.media_list_item_added.item = p_md;
47     event.u.media_list_item_added.index = index;
48     
49     libvlc_event_send( p_mlist->p_event_manager, &event );
50 }
51
52 /**************************************************************************
53  *       notify_item_deletion (private)
54  *
55  * Call parent playlist and send the appropriate event.
56  **************************************************************************/
57 static void
58 notify_item_deletion( libvlc_media_list_t * p_mlist,
59                       libvlc_media_descriptor_t * p_md,
60                       int index )
61 {
62     libvlc_event_t event;
63     
64     event.type = libvlc_MediaListItemDeleted;
65     event.u.media_list_item_deleted.item = p_md;
66     event.u.media_list_item_deleted.index = index;
67
68     libvlc_event_send( p_mlist->p_event_manager, &event );
69 }
70
71 /**************************************************************************
72  *       dynamic_list_propose_item (private) (Event Callback)
73  *
74  * This is called if the dynamic sublist's data provider adds a new item.
75  **************************************************************************/
76 static void
77 dynamic_list_propose_item( const libvlc_event_t * p_event, void * p_user_data )
78 {
79     libvlc_media_list_t * p_submlist = p_user_data;
80     libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_added.item;
81
82     //libvlc_media_descriptor_lock( p_md );
83     if( libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
84     {
85         libvlc_media_list_lock( p_submlist );
86         libvlc_media_list_add_media_descriptor( p_submlist, p_md, NULL );
87         libvlc_media_list_unlock( p_submlist );
88     }
89     //libvlc_media_descriptor_unlock( p_md );
90 }
91
92 /**************************************************************************
93  *       dynamic_list_remove_item (private) (Event Callback)
94  *
95  * This is called if the dynamic sublist's data provider adds a new item.
96  **************************************************************************/
97 static void
98 dynamic_list_remove_item( const libvlc_event_t * p_event, void * p_user_data )
99 {
100     libvlc_media_list_t * p_submlist = p_user_data;
101     libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_deleted.item;
102
103     //libvlc_media_descriptor_lock( p_md );
104     if( libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
105     {
106         int i;
107         libvlc_media_list_lock( p_submlist );        
108         i = libvlc_media_list_index_of_item( p_submlist, p_md, NULL );
109         if ( i < 0 )
110         {
111             /* We've missed one item addition, that could happen especially
112              * if we add item in a threaded maner, so we just ignore */
113             libvlc_media_list_unlock( p_submlist );
114             //libvlc_media_descriptor_unlock( p_md );           
115             return;
116         }
117         libvlc_media_list_remove_index( p_submlist, i, NULL );
118         libvlc_media_list_unlock( p_submlist );
119     }
120     //libvlc_media_descriptor_unlock( p_md );
121 }
122
123 /**************************************************************************
124  *       dynamic_list_change_item (private) (Event Callback)
125  *
126  * This is called if the dynamic sublist's data provider adds a new item.
127  **************************************************************************/
128 static void
129 dynamic_list_change_item( const libvlc_event_t * p_event , void * p_user_data)
130 {
131     libvlc_media_list_t * p_submlist = p_user_data;
132     libvlc_media_descriptor_t * p_md = p_event->u.media_list_item_changed.item;
133     int index;
134
135     libvlc_media_list_lock( p_submlist );        
136     
137     index = libvlc_media_list_index_of_item( p_submlist, p_md, NULL );
138     if( index < 0 )
139     {
140         libvlc_media_list_unlock( p_submlist );     
141         return; /* Not found, no prob, just ignore */
142     }
143
144     //libvlc_media_descriptor_lock( p_md );
145     if( !libvlc_tag_query_match( p_submlist->p_query, p_md, NULL ) )
146         libvlc_media_list_remove_index( p_submlist, index, NULL );
147     //libvlc_media_descriptor_unlock( p_md );
148
149     libvlc_media_list_unlock( p_submlist );        
150 }
151
152 /*
153  * Public libvlc functions
154  */
155
156 /**************************************************************************
157  *       libvlc_media_list_new (Public)
158  *
159  * Init an object.
160  **************************************************************************/
161 libvlc_media_list_t *
162 libvlc_media_list_new( libvlc_instance_t * p_inst,
163                        libvlc_exception_t * p_e )
164 {
165     libvlc_media_list_t * p_mlist;
166
167     p_mlist = malloc(sizeof(libvlc_media_list_t));
168
169     if( !p_mlist )
170         return NULL;
171     
172     p_mlist->p_libvlc_instance = p_inst;
173     p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
174
175     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
176             libvlc_MediaListItemAdded, p_e );
177     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
178             libvlc_MediaListItemChanged, p_e );
179     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
180             libvlc_MediaListItemDeleted, p_e );
181
182     if( libvlc_exception_raised( p_e ) )
183     {
184         libvlc_event_manager_release( p_mlist->p_event_manager );
185         free( p_mlist );
186         return NULL;
187     }
188
189     vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
190     
191     ARRAY_INIT(p_mlist->items);
192     p_mlist->i_refcount = 1;
193     p_mlist->p_media_provider = NULL;
194
195     return p_mlist;
196 }
197
198 /**************************************************************************
199  *       libvlc_media_list_release (Public)
200  *
201  * Release an object.
202  **************************************************************************/
203 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
204 {
205     libvlc_media_descriptor_t * p_md;
206
207     vlc_mutex_lock( &p_mlist->object_lock );
208     p_mlist->i_refcount--;
209     if( p_mlist->i_refcount > 0 )
210     {
211         vlc_mutex_unlock( &p_mlist->object_lock );        
212         return;
213     }
214     vlc_mutex_unlock( &p_mlist->object_lock );        
215
216     /* Refcount null, time to free */
217     if( p_mlist->p_media_provider )
218         libvlc_media_list_release( p_mlist->p_media_provider );
219
220     if( p_mlist->p_query )
221         libvlc_tag_query_release( p_mlist->p_query );
222
223     libvlc_event_manager_release( p_mlist->p_event_manager );
224
225     FOREACH_ARRAY( p_md, p_mlist->items )
226         libvlc_media_descriptor_release( p_md );
227     FOREACH_END()
228  
229     free( p_mlist );
230 }
231 /**************************************************************************
232  *       libvlc_media_list_retain (Public)
233  *
234  * Increase an object refcount.
235  **************************************************************************/
236 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
237 {
238     vlc_mutex_lock( &p_mlist->object_lock );
239     p_mlist->i_refcount++;
240     vlc_mutex_unlock( &p_mlist->object_lock );
241 }
242
243 /**************************************************************************
244  *       libvlc_media_list_count (Public)
245  *
246  * Lock should be hold when entering.
247  **************************************************************************/
248 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
249                              libvlc_exception_t * p_e )
250 {
251     (void)p_e;
252     return p_mlist->items.i_size;
253 }
254
255 /**************************************************************************
256  *       libvlc_media_list_add_media_descriptor (Public)
257  *
258  * Lock should be hold when entering.
259  **************************************************************************/
260 void libvlc_media_list_add_media_descriptor( 
261                                    libvlc_media_list_t * p_mlist,
262                                    libvlc_media_descriptor_t * p_md,
263                                    libvlc_exception_t * p_e )
264 {
265     (void)p_e;
266     libvlc_media_descriptor_retain( p_md );
267     
268     ARRAY_APPEND( p_mlist->items, p_md );
269     notify_item_addition( p_mlist, p_md, p_mlist->items.i_size-1 );
270 }
271
272 /**************************************************************************
273  *       libvlc_media_list_insert_media_descriptor (Public)
274  *
275  * Lock should be hold when entering.
276  **************************************************************************/
277 void libvlc_media_list_insert_media_descriptor( 
278                                    libvlc_media_list_t * p_mlist,
279                                    libvlc_media_descriptor_t * p_md,
280                                    int index,
281                                    libvlc_exception_t * p_e )
282 {
283     (void)p_e;
284     libvlc_media_descriptor_retain( p_md );
285
286     ARRAY_INSERT( p_mlist->items, p_md, index);
287     notify_item_addition( p_mlist, p_md, index );
288 }
289
290 /**************************************************************************
291  *       libvlc_media_list_remove_index (Public)
292  *
293  * Lock should be hold when entering.
294  **************************************************************************/
295 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
296                                      int index,
297                                      libvlc_exception_t * p_e )
298 {
299     libvlc_media_descriptor_t * p_md;
300
301     p_md = ARRAY_VAL( p_mlist->items, index );
302
303     ARRAY_REMOVE( p_mlist->items, index )
304     notify_item_deletion( p_mlist, p_md, index );
305
306     libvlc_media_descriptor_release( p_md );
307 }
308
309 /**************************************************************************
310  *       libvlc_media_list_item_at_index (Public)
311  *
312  * Lock should be hold when entering.
313  **************************************************************************/
314 libvlc_media_descriptor_t *
315 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
316                                  int index,
317                                  libvlc_exception_t * p_e )
318 {
319     libvlc_media_descriptor_t * p_md =  ARRAY_VAL( p_mlist->items, index );
320     libvlc_media_descriptor_retain( p_md );
321     return p_md;
322 }
323
324 /**************************************************************************
325  *       libvlc_media_list_index_of_item (Public)
326  *
327  * Lock should be hold when entering.
328  * Warning: this function would return the first matching item
329  **************************************************************************/
330 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
331                                      libvlc_media_descriptor_t * p_searched_md,
332                                      libvlc_exception_t * p_e )
333 {
334     libvlc_media_descriptor_t * p_md;
335     FOREACH_ARRAY( p_md, p_mlist->items )
336         if( p_searched_md == p_md )
337             return fe_idx; /* Once more, we hate macro for that */
338     FOREACH_END()
339     return -1;
340 }
341
342 /**************************************************************************
343  *       libvlc_media_list_lock (Public)
344  *
345  * The lock must be held in access operations. It is never used in the
346  * Public method.
347  **************************************************************************/
348 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
349 {
350     vlc_mutex_lock( &p_mlist->object_lock );
351 }
352
353
354 /**************************************************************************
355  *       libvlc_media_list_unlock (Public)
356  *
357  * The lock must be held in access operations
358  **************************************************************************/
359 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
360 {
361     vlc_mutex_unlock( &p_mlist->object_lock );
362 }
363
364
365
366 /**************************************************************************
367  *       libvlc_media_list_p_event_manager (Public)
368  *
369  * The p_event_manager is immutable, so you don't have to hold the lock
370  **************************************************************************/
371 libvlc_event_manager_t *
372 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
373                                     libvlc_exception_t * p_e )
374 {
375     (void)p_e;
376     return p_mlist->p_event_manager;
377 }
378
379 /**************************************************************************
380  *       libvlc_media_list_dynamic_sublist (Public)
381  *
382  * Lock should be hold when entering.
383  **************************************************************************/
384 libvlc_media_list_t *
385 libvlc_media_list_dynamic_sublist( libvlc_media_list_t * p_mlist,
386                                    libvlc_tag_query_t * p_query,
387                                    libvlc_exception_t * p_e )
388 {
389     libvlc_media_list_t * p_submlist;
390     libvlc_event_manager_t * p_em;
391     int count, i;
392
393     (void)p_e;
394
395     p_submlist = libvlc_media_list_new( p_mlist->p_libvlc_instance, p_e );
396     if( !p_submlist )
397     {
398         if( !libvlc_exception_raised( p_e ) )
399             libvlc_exception_raise( p_e, "Can't get the new media_list" );
400         return NULL;
401     }
402
403     /* We have a query */
404     libvlc_tag_query_retain( p_query );
405     p_submlist->p_query = p_query;
406
407     /* We have a media provider */
408     libvlc_media_list_retain( p_mlist );
409     p_submlist->p_media_provider = p_mlist;
410
411
412     libvlc_media_list_lock( p_submlist );        
413     
414     count = libvlc_media_list_count( p_mlist, p_e );
415
416     /* This should be running in a thread, a good plan to achieve that
417      * move all the dynamic code to libvlc_tag_query. */
418     for( i = 0; i < count; i++ )
419     {
420         libvlc_media_descriptor_t * p_md;
421         p_md = libvlc_media_list_item_at_index( p_mlist, i, p_e );
422         if( libvlc_tag_query_match( p_query, p_md, NULL ) )
423             libvlc_media_list_add_media_descriptor( p_submlist, p_md, p_e );
424     }
425
426     /* And we will listen to its event, so we can update p_submlist
427      * accordingly */
428     p_em = libvlc_media_list_event_manager( p_mlist, p_e );
429     libvlc_event_attach( p_em, libvlc_MediaListItemAdded,
430                          dynamic_list_propose_item, p_submlist, p_e );
431     libvlc_event_attach( p_em, libvlc_MediaListItemDeleted,
432                          dynamic_list_remove_item, p_submlist, p_e );
433     libvlc_event_attach( p_em, libvlc_MediaListItemChanged,
434                          dynamic_list_change_item, p_submlist, p_e );
435
436     libvlc_media_list_unlock( p_submlist );        
437
438     return p_submlist;
439 }
440