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