forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia_list.c
432 lines (370 loc) · 13.8 KB
/
media_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*****************************************************************************
* media_list.c: libvlc new API media list functions
*****************************************************************************
* Copyright (C) 2007 VLC authors and VideoLAN
*
* Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <vlc/libvlc.h>
#include <vlc/libvlc_picture.h>
#include <vlc/libvlc_media.h>
#include <vlc/libvlc_media_list.h>
#include <vlc/libvlc_events.h>
#include <vlc_common.h>
#include <vlc_atomic.h>
#include "libvlc_internal.h"
#include "media_internal.h" // libvlc_media_new_from_input_item()
#include "media_list_internal.h"
typedef enum EventPlaceInTime {
EventWillHappen,
EventDidHappen
} EventPlaceInTime;
/*
* Private functions
*/
/**************************************************************************
* notify_item_addition (private)
*
* Do the appropriate action when an item is deleted.
**************************************************************************/
static void
notify_item_addition( libvlc_media_list_t * p_mlist,
libvlc_media_t * p_md,
int index,
EventPlaceInTime event_status )
{
libvlc_event_t event;
/* Construct the event */
if( event_status == EventDidHappen )
{
event.type = libvlc_MediaListItemAdded;
event.u.media_list_item_added.item = p_md;
event.u.media_list_item_added.index = index;
}
else /* if( event_status == EventWillHappen ) */
{
event.type = libvlc_MediaListWillAddItem;
event.u.media_list_will_add_item.item = p_md;
event.u.media_list_will_add_item.index = index;
}
/* Send the event */
libvlc_event_send( &p_mlist->event_manager, &event );
}
/**************************************************************************
* notify_item_deletion (private)
*
* Do the appropriate action when an item is added.
**************************************************************************/
static void
notify_item_deletion( libvlc_media_list_t * p_mlist,
libvlc_media_t * p_md,
int index,
EventPlaceInTime event_status )
{
libvlc_event_t event;
/* Construct the event */
if( event_status == EventDidHappen )
{
event.type = libvlc_MediaListItemDeleted;
event.u.media_list_item_deleted.item = p_md;
event.u.media_list_item_deleted.index = index;
}
else /* if( event_status == EventWillHappen ) */
{
event.type = libvlc_MediaListWillDeleteItem;
event.u.media_list_will_delete_item.item = p_md;
event.u.media_list_will_delete_item.index = index;
}
/* Send the event */
libvlc_event_send( &p_mlist->event_manager, &event );
}
/* LibVLC internal */
void libvlc_media_list_internal_end_reached( libvlc_media_list_t * p_mlist )
{
libvlc_event_t event;
event.type = libvlc_MediaListEndReached;
/* Send the event */
libvlc_event_send( &p_mlist->event_manager, &event );
}
/**************************************************************************
* static mlist_is_writable (private)
**************************************************************************/
static inline
bool mlist_is_writable( libvlc_media_list_t *p_mlist )
{
if( !p_mlist||p_mlist->b_read_only )
{
/* We are read-only from user side */
libvlc_printerr( "Attempt to write a read-only media list" );
return false;
}
return true;
}
/*
* Public libvlc functions
*/
/**************************************************************************
* libvlc_media_list_new (Public)
*
* Init an object.
**************************************************************************/
libvlc_media_list_t *libvlc_media_list_new(void)
{
libvlc_media_list_t * p_mlist;
p_mlist = malloc(sizeof(libvlc_media_list_t));
if( unlikely(p_mlist == NULL) )
{
libvlc_printerr( "Not enough memory" );
return NULL;
}
libvlc_event_manager_init( &p_mlist->event_manager, p_mlist );
p_mlist->b_read_only = false;
vlc_mutex_init( &p_mlist->object_lock );
vlc_atomic_rc_init( &p_mlist->rc );
vlc_array_init( &p_mlist->items );
assert( p_mlist->items.i_count == 0 );
p_mlist->p_md = NULL;
p_mlist->p_internal_md = NULL;
return p_mlist;
}
/**************************************************************************
* libvlc_media_list_release (Public)
*
* Release an object.
**************************************************************************/
void libvlc_media_list_release( libvlc_media_list_t * p_mlist )
{
if( !vlc_atomic_rc_dec( &p_mlist->rc ) )
return;
/* Refcount null, time to free */
libvlc_event_manager_destroy( &p_mlist->event_manager );
libvlc_media_release( p_mlist->p_md );
for( size_t i = 0; i < vlc_array_count( &p_mlist->items ); i++ )
{
libvlc_media_t* p_md = vlc_array_item_at_index( &p_mlist->items, i );
libvlc_media_release( p_md );
}
vlc_array_clear( &p_mlist->items );
free( p_mlist );
}
/**************************************************************************
* libvlc_media_list_retain (Public)
*
* Increase an object refcount.
**************************************************************************/
void libvlc_media_list_retain( libvlc_media_list_t * p_mlist )
{
vlc_atomic_rc_inc( &p_mlist->rc );
}
/**************************************************************************
* set_media (Public)
**************************************************************************/
void libvlc_media_list_set_media( libvlc_media_list_t * p_mlist,
libvlc_media_t * p_md )
{
vlc_mutex_lock( &p_mlist->object_lock );
if( p_mlist->p_internal_md || !mlist_is_writable(p_mlist) )
{
vlc_mutex_unlock( &p_mlist->object_lock );
return;
}
libvlc_media_release( p_mlist->p_md );
libvlc_media_retain( p_md );
p_mlist->p_md = p_md;
vlc_mutex_unlock( &p_mlist->object_lock );
}
/**************************************************************************
* media (Public)
*
* If this media_list comes is a media's subitems,
* This holds the corresponding media.
* This md is also seen as the information holder for the media_list.
* Indeed a media_list can have meta information through this
* media.
**************************************************************************/
libvlc_media_t *
libvlc_media_list_media( libvlc_media_list_t * p_mlist )
{
libvlc_media_t *p_md;
vlc_mutex_lock( &p_mlist->object_lock );
p_md = p_mlist->p_internal_md ? p_mlist->p_internal_md : p_mlist->p_md;
if( p_md )
libvlc_media_retain( p_md );
vlc_mutex_unlock( &p_mlist->object_lock );
return p_md;
}
/**************************************************************************
* libvlc_media_list_count (Public)
*
* Lock should be held when entering.
**************************************************************************/
int libvlc_media_list_count( libvlc_media_list_t * p_mlist )
{
return vlc_array_count( &p_mlist->items );
}
/**************************************************************************
* libvlc_media_list_add_media (Public)
*
* Lock should be held when entering.
**************************************************************************/
int libvlc_media_list_add_media( libvlc_media_list_t * p_mlist,
libvlc_media_t * p_md )
{
if( !mlist_is_writable(p_mlist) )
return -1;
libvlc_media_list_internal_add_media( p_mlist, p_md );
return 0;
}
/* LibVLC internal version */
void libvlc_media_list_internal_add_media( libvlc_media_list_t * p_mlist,
libvlc_media_t * p_md )
{
libvlc_media_retain( p_md );
notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items ),
EventWillHappen );
vlc_array_append_or_abort( &p_mlist->items, p_md );
notify_item_addition( p_mlist, p_md, vlc_array_count( &p_mlist->items )-1,
EventDidHappen );
}
/**************************************************************************
* libvlc_media_list_insert_media (Public)
*
* Lock should be hold when entering.
**************************************************************************/
int libvlc_media_list_insert_media( libvlc_media_list_t * p_mlist,
libvlc_media_t * p_md,
int index )
{
if( !mlist_is_writable(p_mlist) )
return -1;
libvlc_media_list_internal_insert_media( p_mlist, p_md, index );
return 0;
}
/* LibVLC internal version */
void libvlc_media_list_internal_insert_media( libvlc_media_list_t * p_mlist,
libvlc_media_t * p_md,
int index )
{
libvlc_media_retain( p_md );
notify_item_addition( p_mlist, p_md, index, EventWillHappen );
vlc_array_insert_or_abort( &p_mlist->items, p_md, index );
notify_item_addition( p_mlist, p_md, index, EventDidHappen );
}
/**************************************************************************
* libvlc_media_list_remove_index (Public)
*
* Lock should be held when entering.
**************************************************************************/
int libvlc_media_list_remove_index( libvlc_media_list_t * p_mlist,
int index )
{
if( !mlist_is_writable(p_mlist) )
return -1;
return libvlc_media_list_internal_remove_index( p_mlist, index );
}
/* LibVLC internal version */
int libvlc_media_list_internal_remove_index( libvlc_media_list_t * p_mlist,
int index )
{
libvlc_media_t * p_md;
if( (size_t) index >= vlc_array_count( &p_mlist->items ))
{
libvlc_printerr( "Index out of bounds" );
return -1;
}
p_md = vlc_array_item_at_index( &p_mlist->items, index );
notify_item_deletion( p_mlist, p_md, index, EventWillHappen );
vlc_array_remove( &p_mlist->items, index );
notify_item_deletion( p_mlist, p_md, index, EventDidHappen );
libvlc_media_release( p_md );
return 0;
}
/**************************************************************************
* libvlc_media_list_item_at_index (Public)
*
* Lock should be held when entering.
**************************************************************************/
libvlc_media_t *
libvlc_media_list_item_at_index( libvlc_media_list_t * p_mlist,
int index )
{
libvlc_media_t * p_md;
if( (size_t) index >= vlc_array_count( &p_mlist->items ))
{
libvlc_printerr( "Index out of bounds" );
return NULL;
}
p_md = vlc_array_item_at_index( &p_mlist->items, index );
libvlc_media_retain( p_md );
return p_md;
}
/**************************************************************************
* libvlc_media_list_index_of_item (Public)
*
* Lock should be held when entering.
* Warning: this function returns the first matching item.
**************************************************************************/
int libvlc_media_list_index_of_item( libvlc_media_list_t * p_mlist,
libvlc_media_t * p_searched_md )
{
int idx = vlc_array_index_of_item( &p_mlist->items, p_searched_md );
if( idx == -1 )
libvlc_printerr( "Media not found" );
return idx;
}
/**************************************************************************
* libvlc_media_list_is_readonly (Public)
*
* This indicates if this media list is read-only from a user point of view
**************************************************************************/
bool libvlc_media_list_is_readonly( libvlc_media_list_t * p_mlist )
{
return p_mlist->b_read_only;
}
/**************************************************************************
* libvlc_media_list_lock (Public)
*
* The lock must be held in access operations. It is never used in the
* Public method.
**************************************************************************/
void libvlc_media_list_lock( libvlc_media_list_t * p_mlist )
{
vlc_mutex_lock( &p_mlist->object_lock );
}
/**************************************************************************
* libvlc_media_list_unlock (Public)
*
* The lock must be held in access operations
**************************************************************************/
void libvlc_media_list_unlock( libvlc_media_list_t * p_mlist )
{
vlc_mutex_unlock( &p_mlist->object_lock );
}
/**************************************************************************
* libvlc_media_list_event_manager (Public)
*
* The p_event_manager is immutable, so you don't have to hold the lock
**************************************************************************/
libvlc_event_manager_t *
libvlc_media_list_event_manager( libvlc_media_list_t * p_mlist )
{
return &p_mlist->event_manager;
}