]> git.sesse.net Git - vlc/blob - src/control/media_list.c
control/media_list.c: Don't send MediaListItemChanged. Listen to the MediaDescriptor...
[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
35 /**************************************************************************
36  *       notify_item_addition (private)
37  *
38  * Do the appropriate action when an item is deleted.
39  **************************************************************************/
40 static void
41 notify_item_addition( libvlc_media_list_t * p_mlist,
42                       libvlc_media_descriptor_t * p_md,
43                       int index )
44 {
45     libvlc_event_t event;
46
47     /* Construct the event */
48     event.type = libvlc_MediaListItemAdded;
49     event.u.media_list_item_added.item = p_md;
50     event.u.media_list_item_added.index = index;
51
52     /* Send the event */
53     libvlc_event_send( p_mlist->p_event_manager, &event );
54 }
55
56 /**************************************************************************
57  *       notify_item_deletion (private)
58  *
59  * Do the appropriate action when an item is added.
60  **************************************************************************/
61 static void
62 notify_item_deletion( libvlc_media_list_t * p_mlist,
63                       libvlc_media_descriptor_t * p_md,
64                       int index )
65 {
66     libvlc_event_t event;
67
68     /* Construct the event */
69     event.type = libvlc_MediaListItemDeleted;
70     event.u.media_list_item_deleted.item = p_md;
71     event.u.media_list_item_deleted.index = index;
72
73     /* Send the event */
74     libvlc_event_send( p_mlist->p_event_manager, &event );
75 }
76
77 /*
78  * Public libvlc functions
79  */
80
81 /**************************************************************************
82  *       libvlc_media_list_new (Public)
83  *
84  * Init an object.
85  **************************************************************************/
86 libvlc_media_list_t *
87 libvlc_media_list_new( libvlc_instance_t * p_inst,
88                        libvlc_exception_t * p_e )
89 {
90     libvlc_media_list_t * p_mlist;
91
92     p_mlist = malloc(sizeof(libvlc_media_list_t));
93
94     if( !p_mlist )
95         return NULL;
96     
97     p_mlist->p_libvlc_instance = p_inst;
98     p_mlist->p_event_manager = libvlc_event_manager_new( p_mlist, p_inst, p_e );
99
100     /* Code for that one should be handled in flat_media_list.c */
101     p_mlist->p_flat_mlist = NULL;
102
103     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
104             libvlc_MediaListItemAdded, p_e );
105     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
106             libvlc_MediaListItemChanged, p_e );
107     libvlc_event_manager_register_event_type( p_mlist->p_event_manager,
108             libvlc_MediaListItemDeleted, p_e );
109
110     if( libvlc_exception_raised( p_e ) )
111     {
112         libvlc_event_manager_release( p_mlist->p_event_manager );
113         free( p_mlist );
114         return NULL;
115     }
116
117     vlc_mutex_init( p_inst->p_libvlc_int, &p_mlist->object_lock );
118     
119     ARRAY_INIT(p_mlist->items);
120     p_mlist->i_refcount = 1;
121     p_mlist->psz_name = NULL;
122
123     return p_mlist;
124 }
125
126 /**************************************************************************
127  *       libvlc_media_list_release (Public)
128  *
129  * Release an object.
130  **************************************************************************/
131 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
132 {
133     libvlc_media_descriptor_t * p_md;
134
135     vlc_mutex_lock( &p_mlist->object_lock );
136     p_mlist->i_refcount--;
137     if( p_mlist->i_refcount > 0 )
138     {
139         vlc_mutex_unlock( &p_mlist->object_lock );        
140         return;
141     }
142     vlc_mutex_unlock( &p_mlist->object_lock );        
143
144     /* Refcount null, time to free */
145
146     /* Handled in flat_media_list.c */
147     libvlc_media_list_flat_media_list_release( p_mlist );
148
149     libvlc_event_manager_release( p_mlist->p_event_manager );
150
151     FOREACH_ARRAY( p_md, p_mlist->items )
152         libvlc_media_descriptor_release( p_md );
153     FOREACH_END()
154  
155     free( p_mlist );
156 }
157
158 /**************************************************************************
159  *       libvlc_media_list_retain (Public)
160  *
161  * Increase an object refcount.
162  **************************************************************************/
163 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
164 {
165     vlc_mutex_lock( &p_mlist->object_lock );
166     p_mlist->i_refcount++;
167     vlc_mutex_unlock( &p_mlist->object_lock );
168 }
169
170
171 /**************************************************************************
172  *       add_file_content (Public)
173  **************************************************************************/
174 void
175 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
176                                     const char * psz_uri,
177                                     libvlc_exception_t * p_e )
178 {
179     input_item_t * p_input_item;
180     libvlc_media_descriptor_t * p_md;
181
182     p_input_item = input_ItemNewExt( p_mlist->p_libvlc_instance->p_libvlc_int, psz_uri,
183                                 _("Media Library"), 0, NULL, -1 );
184
185     if( !p_input_item )
186     {
187         libvlc_exception_raise( p_e, "Can't create an input item" );
188         return;
189     }
190
191     p_md = libvlc_media_descriptor_new_from_input_item(
192             p_mlist->p_libvlc_instance,
193             p_input_item, p_e );
194
195     if( !p_md )
196     {
197         vlc_gc_decref( p_input_item );
198         return;
199     }
200
201     libvlc_media_list_add_media_descriptor( p_mlist, p_md, p_e );
202     if( libvlc_exception_raised( p_e ) )
203         return;
204
205     input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item, VLC_TRUE );
206
207     return;
208 }
209
210 /**************************************************************************
211  *       set_name (Public)
212  **************************************************************************/
213 void libvlc_media_list_set_name( libvlc_media_list_t * p_mlist,
214                                  const char * psz_name,
215                                  libvlc_exception_t * p_e)
216
217 {
218     (void)p_e;
219     vlc_mutex_lock( &p_mlist->object_lock );
220     free( p_mlist->psz_name );
221     p_mlist->psz_name = psz_name ? strdup( psz_name ) : NULL;
222     vlc_mutex_unlock( &p_mlist->object_lock );
223 }
224
225 /**************************************************************************
226  *       name (Public)
227  **************************************************************************/
228 char * libvlc_media_list_name( libvlc_media_list_t * p_mlist,
229                                libvlc_exception_t * p_e)
230 {
231     char *ret;
232     (void)p_e;
233
234     vlc_mutex_lock( &p_mlist->object_lock );
235     ret = p_mlist->psz_name ? strdup( p_mlist->psz_name ) : NULL;
236     vlc_mutex_unlock( &p_mlist->object_lock );
237
238     return ret;
239 }
240
241 /**************************************************************************
242  *       libvlc_media_list_count (Public)
243  *
244  * Lock should be hold when entering.
245  **************************************************************************/
246 int libvlc_media_list_count( libvlc_media_list_t * p_mlist,
247                              libvlc_exception_t * p_e )
248 {
249     (void)p_e;
250     return p_mlist->items.i_size;
251 }
252
253 /**************************************************************************
254  *       libvlc_media_list_add_media_descriptor (Public)
255  *
256  * Lock should be hold when entering.
257  **************************************************************************/
258 void libvlc_media_list_add_media_descriptor( 
259                                    libvlc_media_list_t * p_mlist,
260                                    libvlc_media_descriptor_t * p_md,
261                                    libvlc_exception_t * p_e )
262 {
263     (void)p_e;
264     libvlc_media_descriptor_retain( p_md );
265     ARRAY_INSERT( p_mlist->items, p_md, p_mlist->items.i_size );
266     notify_item_addition( p_mlist, p_md, p_mlist->items.i_size-1 );
267 }
268
269 /**************************************************************************
270  *       libvlc_media_list_insert_media_descriptor (Public)
271  *
272  * Lock should be hold when entering.
273  **************************************************************************/
274 void libvlc_media_list_insert_media_descriptor( 
275                                    libvlc_media_list_t * p_mlist,
276                                    libvlc_media_descriptor_t * p_md,
277                                    int index,
278                                    libvlc_exception_t * p_e )
279 {
280     (void)p_e;
281     libvlc_media_descriptor_retain( p_md );
282
283     ARRAY_INSERT( p_mlist->items, p_md, index);
284     notify_item_addition( p_mlist, p_md, index );
285 }
286
287 /**************************************************************************
288  *       libvlc_media_list_remove_index (Public)
289  *
290  * Lock should be hold when entering.
291  **************************************************************************/
292 void libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
293                                      int index,
294                                      libvlc_exception_t * p_e )
295 {
296     libvlc_media_descriptor_t * p_md;
297
298     p_md = ARRAY_VAL( p_mlist->items, index );
299
300     ARRAY_REMOVE( p_mlist->items, index )
301     notify_item_deletion( p_mlist, p_md, index );
302
303     libvlc_media_descriptor_release( p_md );
304 }
305
306 /**************************************************************************
307  *       libvlc_media_list_item_at_index (Public)
308  *
309  * Lock should be hold when entering.
310  **************************************************************************/
311 libvlc_media_descriptor_t *
312 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
313                                  int index,
314                                  libvlc_exception_t * p_e )
315 {
316     libvlc_media_descriptor_t * p_md =  ARRAY_VAL( p_mlist->items, index );
317     libvlc_media_descriptor_retain( p_md );
318     return p_md;
319 }
320
321 /**************************************************************************
322  *       libvlc_media_list_index_of_item (Public)
323  *
324  * Lock should be hold when entering.
325  * Warning: this function would return the first matching item
326  **************************************************************************/
327 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
328                                      libvlc_media_descriptor_t * p_searched_md,
329                                      libvlc_exception_t * p_e )
330 {
331     libvlc_media_descriptor_t * p_md;
332     FOREACH_ARRAY( p_md, p_mlist->items )
333         if( p_searched_md == p_md )
334             return fe_idx; /* Once more, we hate macro for that */
335     FOREACH_END()
336     return -1;
337 }
338
339 /**************************************************************************
340  *       libvlc_media_list_lock (Public)
341  *
342  * The lock must be held in access operations. It is never used in the
343  * Public method.
344  **************************************************************************/
345 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
346 {
347     vlc_mutex_lock( &p_mlist->object_lock );
348 }
349
350
351 /**************************************************************************
352  *       libvlc_media_list_unlock (Public)
353  *
354  * The lock must be held in access operations
355  **************************************************************************/
356 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
357 {
358     vlc_mutex_unlock( &p_mlist->object_lock );
359 }
360
361
362
363 /**************************************************************************
364  *       libvlc_media_list_p_event_manager (Public)
365  *
366  * The p_event_manager is immutable, so you don't have to hold the lock
367  **************************************************************************/
368 libvlc_event_manager_t *
369 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist,
370                                     libvlc_exception_t * p_e )
371 {
372     (void)p_e;
373     return p_mlist->p_event_manager;
374 }