1 /*****************************************************************************
2 * media_list.c: libvlc new API media list functions
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
5 * $Id: 7c93e41d5f67094e240c28d49633d455764f9970 $
6 *
7 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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 /*
48 * Private functions
49 */
50
51
52
53 /**************************************************************************
54 * notify_item_addition (private)
55 *
56 * Do the appropriate action when an item is deleted.
57 **************************************************************************/
58 static void
notify_item_addition(libvlc_media_list_t * p_mlist,libvlc_media_t * p_md,int index,EventPlaceInTime event_status)59 notify_item_addition( libvlc_media_list_t * p_mlist,
60 libvlc_media_t * p_md,
61 int index,
62 EventPlaceInTime event_status )
63 {
64 libvlc_event_t event;
65
66 /* Construct the event */
67 if( event_status == EventDidHappen )
68 {
69 event.type = libvlc_MediaListItemAdded;
70 event.u.media_list_item_added.item = p_md;
71 event.u.media_list_item_added.index = index;
72 }
73 else /* if( event_status == EventWillHappen ) */
74 {
75 event.type = libvlc_MediaListWillAddItem;
76 event.u.media_list_will_add_item.item = p_md;
77 event.u.media_list_will_add_item.index = index;
78 }
79
80 /* Send the event */
81 libvlc_event_send( &p_mlist->event_manager, &event );
82 }
83
84 /**************************************************************************
85 * notify_item_deletion (private)
86 *
87 * Do the appropriate action when an item is added.
88 **************************************************************************/
89 static void
notify_item_deletion(libvlc_media_list_t * p_mlist,libvlc_media_t * p_md,int index,EventPlaceInTime event_status)90 notify_item_deletion( libvlc_media_list_t * p_mlist,
91 libvlc_media_t * p_md,
92 int index,
93 EventPlaceInTime event_status )
94 {
95 libvlc_event_t event;
96
97 /* Construct the event */
98 if( event_status == EventDidHappen )
99 {
100 event.type = libvlc_MediaListItemDeleted;
101 event.u.media_list_item_deleted.item = p_md;
102 event.u.media_list_item_deleted.index = index;
103 }
104 else /* if( event_status == EventWillHappen ) */
105 {
106 event.type = libvlc_MediaListWillDeleteItem;
107 event.u.media_list_will_delete_item.item = p_md;
108 event.u.media_list_will_delete_item.index = index;
109 }
110
111 /* Send the event */
112 libvlc_event_send( &p_mlist->event_manager, &event );
113 }
114
115 /* LibVLC internal */
libvlc_media_list_internal_end_reached(libvlc_media_list_t * p_mlist)116 void libvlc_media_list_internal_end_reached( libvlc_media_list_t * p_mlist )
117 {
118 libvlc_event_t event;
119
120 event.type = libvlc_MediaListEndReached;
121
122 /* Send the event */
123 libvlc_event_send( &p_mlist->event_manager, &event );
124 }
125
126 /**************************************************************************
127 * static mlist_is_writable (private)
128 **************************************************************************/
129 static inline
mlist_is_writable(libvlc_media_list_t * p_mlist)130 bool mlist_is_writable( libvlc_media_list_t *p_mlist )
131 {
132 if( !p_mlist||p_mlist->b_read_only )
133 {
134 /* We are read-only from user side */
135 libvlc_printerr( "Attempt to write a read-only media list" );
136 return false;
137 }
138 return true;
139 }
140
141 /*
142 * Public libvlc functions
143 */
144
145 /**************************************************************************
146 * libvlc_media_list_new (Public)
147 *
148 * Init an object.
149 **************************************************************************/
150 libvlc_media_list_t *
libvlc_media_list_new(libvlc_instance_t * p_inst)151 libvlc_media_list_new( libvlc_instance_t * p_inst )
152 {
153 libvlc_media_list_t * p_mlist;
154
155 p_mlist = malloc(sizeof(libvlc_media_list_t));
156 if( unlikely(p_mlist == NULL) )
157 {
158 libvlc_printerr( "Not enough memory" );
159 return NULL;
160 }
161
162 p_mlist->p_libvlc_instance = p_inst;
163 libvlc_event_manager_init( &p_mlist->event_manager, p_mlist );
164 p_mlist->b_read_only = false;
165
166 vlc_mutex_init( &p_mlist->object_lock );
167 vlc_mutex_init( &p_mlist->refcount_lock ); // FIXME: spinlock?
168
169 vlc_array_init( &p_mlist->items );
170 assert( p_mlist->items.i_count == 0 );
171 p_mlist->i_refcount = 1;
172 p_mlist->p_md = NULL;
173 p_mlist->p_internal_md = NULL;
174
175 libvlc_retain( p_inst );
176 return p_mlist;
177 }
178
179 /**************************************************************************
180 * libvlc_media_list_release (Public)
181 *
182 * Release an object.
183 **************************************************************************/
libvlc_media_list_release(libvlc_media_list_t * p_mlist)184 void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
185 {
186 vlc_mutex_lock( &p_mlist->refcount_lock );
187 p_mlist->i_refcount--;
188 if( p_mlist->i_refcount > 0 )
189 {
190 vlc_mutex_unlock( &p_mlist->refcount_lock );
191 return;
192 }
193 vlc_mutex_unlock( &p_mlist->refcount_lock );
194
195 /* Refcount null, time to free */
196
197 libvlc_event_manager_destroy( &p_mlist->event_manager );
198 libvlc_media_release( p_mlist->p_md );
199
200 for( size_t i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
201 {
202 libvlc_media_t* p_md = vlc_array_item_at_index( &p_mlist->items, i );
203 libvlc_media_release( p_md );
204 }
205
206 vlc_mutex_destroy( &p_mlist->object_lock );
207 vlc_mutex_destroy( &p_mlist->refcount_lock );
208 vlc_array_clear( &p_mlist->items );
209
210 libvlc_release( p_mlist->p_libvlc_instance );
211 free( p_mlist );
212 }
213
214 /**************************************************************************
215 * libvlc_media_list_retain (Public)
216 *
217 * Increase an object refcount.
218 **************************************************************************/
libvlc_media_list_retain(libvlc_media_list_t * p_mlist)219 void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
220 {
221 vlc_mutex_lock( &p_mlist->refcount_lock );
222 p_mlist->i_refcount++;
223 vlc_mutex_unlock( &p_mlist->refcount_lock );
224 }
225
226
227 /**************************************************************************
228 * add_file_content (Public)
229 **************************************************************************/
230 int
libvlc_media_list_add_file_content(libvlc_media_list_t * p_mlist,const char * psz_uri)231 libvlc_media_list_add_file_content( libvlc_media_list_t * p_mlist,
232 const char * psz_uri )
233 {
234 input_item_t * p_input_item;
235 libvlc_media_t * p_md;
236
237 p_input_item = input_item_New( psz_uri, _("Media Library") );
238
239 if( !p_input_item )
240 {
241 libvlc_printerr( "Not enough memory" );
242 return -1;
243 }
244
245 p_md = libvlc_media_new_from_input_item( p_mlist->p_libvlc_instance,
246 p_input_item );
247 if( !p_md )
248 {
249 input_item_Release( p_input_item );
250 return -1;
251 }
252
253 if( libvlc_media_list_add_media( p_mlist, p_md ) )
254 {
255 #warning Missing error handling!
256 /* printerr and leaks */
257 return -1;
258 }
259
260 input_Read( p_mlist->p_libvlc_instance->p_libvlc_int, p_input_item );
261 return 0;
262 }
263
264 /**************************************************************************
265 * set_media (Public)
266 **************************************************************************/
libvlc_media_list_set_media(libvlc_media_list_t * p_mlist,libvlc_media_t * p_md)267 void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
268 libvlc_media_t * p_md )
269
270 {
271 vlc_mutex_lock( &p_mlist->object_lock );
272 if( p_mlist->p_internal_md || !mlist_is_writable(p_mlist) )
273 {
274 vlc_mutex_unlock( &p_mlist->object_lock );
275 return;
276 }
277 libvlc_media_release( p_mlist->p_md );
278 libvlc_media_retain( p_md );
279 p_mlist->p_md = p_md;
280 vlc_mutex_unlock( &p_mlist->object_lock );
281 }
282
283 /**************************************************************************
284 * media (Public)
285 *
286 * If this media_list comes is a media's subitems,
287 * This holds the corresponding media.
288 * This md is also seen as the information holder for the media_list.
289 * Indeed a media_list can have meta information through this
290 * media.
291 **************************************************************************/
292 libvlc_media_t *
libvlc_media_list_media(libvlc_media_list_t * p_mlist)293 libvlc_media_list_media( libvlc_media_list_t * p_mlist )
294 {
295 libvlc_media_t *p_md;
296
297 vlc_mutex_lock( &p_mlist->object_lock );
298 p_md = p_mlist->p_internal_md ? p_mlist->p_internal_md : p_mlist->p_md;
299 if( p_md )
300 libvlc_media_retain( p_md );
301 vlc_mutex_unlock( &p_mlist->object_lock );
302
303 return p_md;
304 }
305
306 /**************************************************************************
307 * libvlc_media_list_count (Public)
308 *
309 * Lock should be held when entering.
310 **************************************************************************/
libvlc_media_list_count(libvlc_media_list_t * p_mlist)311 int libvlc_media_list_count( libvlc_media_list_t * p_mlist )
312 {
313 return vlc_array_count( &p_mlist->items );
314 }
315
316 /**************************************************************************
317 * libvlc_media_list_add_media (Public)
318 *
319 * Lock should be held when entering.
320 **************************************************************************/
libvlc_media_list_add_media(libvlc_media_list_t * p_mlist,libvlc_media_t * p_md)321 int libvlc_media_list_add_media( libvlc_media_list_t * p_mlist,
322 libvlc_media_t * p_md )
323 {
324 if( !mlist_is_writable(p_mlist) )
325 return -1;
326 libvlc_media_list_internal_add_media( p_mlist, p_md );
327 return 0;
328 }
329
330 /* LibVLC internal version */
libvlc_media_list_internal_add_media(libvlc_media_list_t * p_mlist,libvlc_media_t * p_md)331 void libvlc_media_list_internal_add_media( libvlc_media_list_t * p_mlist,
332 libvlc_media_t * p_md )
333 {
334 libvlc_media_retain( p_md );
335
336 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
337 EventWillHappen );
338 vlc_array_append_or_abort( &p_mlist->items, p_md );
339 notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
340 EventDidHappen );
341 }
342
343 /**************************************************************************
344 * libvlc_media_list_insert_media (Public)
345 *
346 * Lock should be hold when entering.
347 **************************************************************************/
libvlc_media_list_insert_media(libvlc_media_list_t * p_mlist,libvlc_media_t * p_md,int index)348 int libvlc_media_list_insert_media( libvlc_media_list_t * p_mlist,
349 libvlc_media_t * p_md,
350 int index )
351 {
352 if( !mlist_is_writable(p_mlist) )
353 return -1;
354 libvlc_media_list_internal_insert_media( p_mlist, p_md, index );
355 return 0;
356 }
357
358 /* LibVLC internal version */
libvlc_media_list_internal_insert_media(libvlc_media_list_t * p_mlist,libvlc_media_t * p_md,int index)359 void libvlc_media_list_internal_insert_media( libvlc_media_list_t * p_mlist,
360 libvlc_media_t * p_md,
361 int index )
362 {
363 libvlc_media_retain( p_md );
364
365 notify_item_addition( p_mlist, p_md, index, EventWillHappen );
366 vlc_array_insert_or_abort( &p_mlist->items, p_md, index );
367 notify_item_addition( p_mlist, p_md, index, EventDidHappen );
368 }
369
370 /**************************************************************************
371 * libvlc_media_list_remove_index (Public)
372 *
373 * Lock should be held when entering.
374 **************************************************************************/
libvlc_media_list_remove_index(libvlc_media_list_t * p_mlist,int index)375 int libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
376 int index )
377 {
378 if( !mlist_is_writable(p_mlist) )
379 return -1;
380 return libvlc_media_list_internal_remove_index( p_mlist, index );
381 }
382
383 /* LibVLC internal version */
libvlc_media_list_internal_remove_index(libvlc_media_list_t * p_mlist,int index)384 int libvlc_media_list_internal_remove_index( libvlc_media_list_t * p_mlist,
385 int index )
386 {
387 libvlc_media_t * p_md;
388
389 if( (size_t) index >= vlc_array_count( &p_mlist->items ))
390 {
391 libvlc_printerr( "Index out of bounds" );
392 return -1;
393 }
394
395 p_md = vlc_array_item_at_index( &p_mlist->items, index );
396
397 notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
398 vlc_array_remove( &p_mlist->items, index );
399 notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
400
401 libvlc_media_release( p_md );
402 return 0;
403 }
404
405 /**************************************************************************
406 * libvlc_media_list_item_at_index (Public)
407 *
408 * Lock should be held when entering.
409 **************************************************************************/
410 libvlc_media_t *
libvlc_media_list_item_at_index(libvlc_media_list_t * p_mlist,int index)411 libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
412 int index )
413 {
414 libvlc_media_t * p_md;
415
416 if( (size_t) index >= vlc_array_count( &p_mlist->items ))
417 {
418 libvlc_printerr( "Index out of bounds" );
419 return NULL;
420 }
421
422 p_md = vlc_array_item_at_index( &p_mlist->items, index );
423 libvlc_media_retain( p_md );
424 return p_md;
425 }
426
427 /**************************************************************************
428 * libvlc_media_list_index_of_item (Public)
429 *
430 * Lock should be held when entering.
431 * Warning: this function returns the first matching item.
432 **************************************************************************/
libvlc_media_list_index_of_item(libvlc_media_list_t * p_mlist,libvlc_media_t * p_searched_md)433 int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
434 libvlc_media_t * p_searched_md )
435 {
436 int idx = vlc_array_index_of_item( &p_mlist->items, p_searched_md );
437 if( idx == -1 )
438 libvlc_printerr( "Media not found" );
439
440 return idx;
441 }
442
443 /**************************************************************************
444 * libvlc_media_list_is_readonly (Public)
445 *
446 * This indicates if this media list is read-only from a user point of view
447 **************************************************************************/
libvlc_media_list_is_readonly(libvlc_media_list_t * p_mlist)448 int libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
449 {
450 return p_mlist->b_read_only;
451 }
452
453 /**************************************************************************
454 * libvlc_media_list_lock (Public)
455 *
456 * The lock must be held in access operations. It is never used in the
457 * Public method.
458 **************************************************************************/
libvlc_media_list_lock(libvlc_media_list_t * p_mlist)459 void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
460 {
461 vlc_mutex_lock( &p_mlist->object_lock );
462 }
463
464
465 /**************************************************************************
466 * libvlc_media_list_unlock (Public)
467 *
468 * The lock must be held in access operations
469 **************************************************************************/
libvlc_media_list_unlock(libvlc_media_list_t * p_mlist)470 void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
471 {
472 vlc_mutex_unlock( &p_mlist->object_lock );
473 }
474
475
476 /**************************************************************************
477 * libvlc_media_list_event_manager (Public)
478 *
479 * The p_event_manager is immutable, so you don't have to hold the lock
480 **************************************************************************/
481 libvlc_event_manager_t *
libvlc_media_list_event_manager(libvlc_media_list_t * p_mlist)482 libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist )
483 {
484 return &p_mlist->event_manager;
485 }
486