1 /*****************************************************************************
2 * media.c: Libvlc API media descripor management
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
5 * $Id: b909f324362d543926b24f76294afa10b4b9781a $
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 #include <errno.h>
30
31 #include <vlc/libvlc.h>
32 #include <vlc/libvlc_media.h>
33 #include <vlc/libvlc_media_list.h> // For the subitems, here for convenience
34 #include <vlc/libvlc_events.h>
35
36 #include <vlc_common.h>
37 #include <vlc_input.h>
38 #include <vlc_meta.h>
39 #include <vlc_playlist.h> /* For the preparser */
40 #include <vlc_url.h>
41
42 #include "../src/libvlc.h"
43
44 #include "libvlc_internal.h"
45 #include "media_internal.h"
46 #include "media_list_internal.h"
47
48 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
49 {
50 [libvlc_meta_Title] = vlc_meta_Title,
51 [libvlc_meta_Artist] = vlc_meta_Artist,
52 [libvlc_meta_Genre] = vlc_meta_Genre,
53 [libvlc_meta_Copyright] = vlc_meta_Copyright,
54 [libvlc_meta_Album] = vlc_meta_Album,
55 [libvlc_meta_TrackNumber] = vlc_meta_TrackNumber,
56 [libvlc_meta_Description] = vlc_meta_Description,
57 [libvlc_meta_Rating] = vlc_meta_Rating,
58 [libvlc_meta_Date] = vlc_meta_Date,
59 [libvlc_meta_Setting] = vlc_meta_Setting,
60 [libvlc_meta_URL] = vlc_meta_URL,
61 [libvlc_meta_Language] = vlc_meta_Language,
62 [libvlc_meta_NowPlaying] = vlc_meta_NowPlaying,
63 [libvlc_meta_Publisher] = vlc_meta_Publisher,
64 [libvlc_meta_EncodedBy] = vlc_meta_EncodedBy,
65 [libvlc_meta_ArtworkURL] = vlc_meta_ArtworkURL,
66 [libvlc_meta_TrackID] = vlc_meta_TrackID,
67 [libvlc_meta_TrackTotal] = vlc_meta_TrackTotal,
68 [libvlc_meta_Director] = vlc_meta_Director,
69 [libvlc_meta_Season] = vlc_meta_Season,
70 [libvlc_meta_Episode] = vlc_meta_Episode,
71 [libvlc_meta_ShowName] = vlc_meta_ShowName,
72 [libvlc_meta_Actors] = vlc_meta_Actors,
73 [libvlc_meta_AlbumArtist] = vlc_meta_AlbumArtist,
74 [libvlc_meta_DiscNumber] = vlc_meta_DiscNumber,
75 [libvlc_meta_DiscTotal] = vlc_meta_DiscTotal
76 };
77
78 static const libvlc_meta_t vlc_to_libvlc_meta[] =
79 {
80 [vlc_meta_Title] = libvlc_meta_Title,
81 [vlc_meta_Artist] = libvlc_meta_Artist,
82 [vlc_meta_Genre] = libvlc_meta_Genre,
83 [vlc_meta_Copyright] = libvlc_meta_Copyright,
84 [vlc_meta_Album] = libvlc_meta_Album,
85 [vlc_meta_TrackNumber] = libvlc_meta_TrackNumber,
86 [vlc_meta_Description] = libvlc_meta_Description,
87 [vlc_meta_Rating] = libvlc_meta_Rating,
88 [vlc_meta_Date] = libvlc_meta_Date,
89 [vlc_meta_Setting] = libvlc_meta_Setting,
90 [vlc_meta_URL] = libvlc_meta_URL,
91 [vlc_meta_Language] = libvlc_meta_Language,
92 [vlc_meta_NowPlaying] = libvlc_meta_NowPlaying,
93 [vlc_meta_ESNowPlaying] = libvlc_meta_NowPlaying,
94 [vlc_meta_Publisher] = libvlc_meta_Publisher,
95 [vlc_meta_EncodedBy] = libvlc_meta_EncodedBy,
96 [vlc_meta_ArtworkURL] = libvlc_meta_ArtworkURL,
97 [vlc_meta_TrackID] = libvlc_meta_TrackID,
98 [vlc_meta_TrackTotal] = libvlc_meta_TrackTotal,
99 [vlc_meta_Director] = libvlc_meta_Director,
100 [vlc_meta_Season] = libvlc_meta_Season,
101 [vlc_meta_Episode] = libvlc_meta_Episode,
102 [vlc_meta_ShowName] = libvlc_meta_ShowName,
103 [vlc_meta_Actors] = libvlc_meta_Actors,
104 [vlc_meta_AlbumArtist] = libvlc_meta_AlbumArtist,
105 [vlc_meta_DiscNumber] = libvlc_meta_DiscNumber,
106 [vlc_meta_DiscTotal] = libvlc_meta_DiscTotal
107 };
108
109 static_assert(
110 ORIENT_TOP_LEFT == (int) libvlc_video_orient_top_left &&
111 ORIENT_TOP_RIGHT == (int) libvlc_video_orient_top_right &&
112 ORIENT_BOTTOM_LEFT == (int) libvlc_video_orient_bottom_left &&
113 ORIENT_BOTTOM_RIGHT == (int) libvlc_video_orient_bottom_right &&
114 ORIENT_LEFT_TOP == (int) libvlc_video_orient_left_top &&
115 ORIENT_LEFT_BOTTOM == (int) libvlc_video_orient_left_bottom &&
116 ORIENT_RIGHT_TOP == (int) libvlc_video_orient_right_top &&
117 ORIENT_RIGHT_BOTTOM == (int) libvlc_video_orient_right_bottom,
118 "Mismatch between libvlc_video_orient_t and video_orientation_t" );
119
120 static_assert(
121 PROJECTION_MODE_RECTANGULAR == (int) libvlc_video_projection_rectangular &&
122 PROJECTION_MODE_EQUIRECTANGULAR == (int) libvlc_video_projection_equirectangular &&
123 PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD == (int) libvlc_video_projection_cubemap_layout_standard,
124 "Mismatch between libvlc_video_projection_t and video_projection_mode_t" );
125
media_get_subitems(libvlc_media_t * p_md,bool b_create)126 static libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md,
127 bool b_create )
128 {
129 libvlc_media_list_t *p_subitems = NULL;
130
131 vlc_mutex_lock( &p_md->subitems_lock );
132 if( p_md->p_subitems == NULL && b_create )
133 {
134 p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance );
135 if( p_md->p_subitems != NULL )
136 {
137 p_md->p_subitems->b_read_only = true;
138 p_md->p_subitems->p_internal_md = p_md;
139 }
140 }
141 p_subitems = p_md->p_subitems;
142 vlc_mutex_unlock( &p_md->subitems_lock );
143 return p_subitems;
144 }
145
input_item_add_subitem(libvlc_media_t * p_md,input_item_t * item)146 static libvlc_media_t *input_item_add_subitem( libvlc_media_t *p_md,
147 input_item_t *item )
148 {
149 libvlc_media_t * p_md_child;
150 libvlc_media_list_t *p_subitems;
151 libvlc_event_t event;
152
153 p_md_child = libvlc_media_new_from_input_item( p_md->p_libvlc_instance,
154 item );
155
156 /* Add this to our media list */
157 p_subitems = media_get_subitems( p_md, true );
158 if( p_subitems != NULL )
159 {
160 libvlc_media_list_lock( p_subitems );
161 libvlc_media_list_internal_add_media( p_subitems, p_md_child );
162 libvlc_media_list_unlock( p_subitems );
163 }
164
165 /* Construct the event */
166 event.type = libvlc_MediaSubItemAdded;
167 event.u.media_subitem_added.new_child = p_md_child;
168
169 /* Send the event */
170 libvlc_event_send( &p_md->event_manager, &event );
171 return p_md_child;
172 }
173
input_item_add_subnode(libvlc_media_t * md,input_item_node_t * node)174 static void input_item_add_subnode( libvlc_media_t *md,
175 input_item_node_t *node )
176 {
177 for( int i = 0; i < node->i_children; i++ )
178 {
179 input_item_node_t *child = node->pp_children[i];
180 libvlc_media_t *md_child = input_item_add_subitem( md, child->p_item );
181
182 if( md_child != NULL )
183 {
184 input_item_add_subnode( md_child, child );
185 libvlc_media_release( md_child );
186 }
187 }
188 }
189
190 /**************************************************************************
191 * input_item_subitemtree_added (Private) (vlc event Callback)
192 **************************************************************************/
input_item_subitemtree_added(const vlc_event_t * p_event,void * user_data)193 static void input_item_subitemtree_added( const vlc_event_t * p_event,
194 void * user_data )
195 {
196 libvlc_media_t * p_md = user_data;
197 libvlc_event_t event;
198 input_item_node_t *node = p_event->u.input_item_subitem_tree_added.p_root;
199
200 /* FIXME FIXME FIXME
201 * Recursive function calls seem much simpler for this. But playlists are
202 * untrusted and can be arbitrarily deep (e.g. with XSPF). So recursion can
203 * potentially lead to plain old stack overflow. */
204 input_item_add_subnode( p_md, node );
205
206 /* Construct the event */
207 event.type = libvlc_MediaSubItemTreeAdded;
208 event.u.media_subitemtree_added.item = p_md;
209
210 /* Send the event */
211 libvlc_event_send( &p_md->event_manager, &event );
212 }
213
214 /**************************************************************************
215 * input_item_meta_changed (Private) (vlc event Callback)
216 **************************************************************************/
input_item_meta_changed(const vlc_event_t * p_event,void * user_data)217 static void input_item_meta_changed( const vlc_event_t *p_event,
218 void * user_data )
219 {
220 libvlc_media_t * p_md = user_data;
221 libvlc_event_t event;
222
223 /* Construct the event */
224 event.type = libvlc_MediaMetaChanged;
225 event.u.media_meta_changed.meta_type =
226 vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
227
228 /* Send the event */
229 libvlc_event_send( &p_md->event_manager, &event );
230 }
231
232 /**************************************************************************
233 * input_item_duration_changed (Private) (vlc event Callback)
234 **************************************************************************/
input_item_duration_changed(const vlc_event_t * p_event,void * user_data)235 static void input_item_duration_changed( const vlc_event_t *p_event,
236 void * user_data )
237 {
238 libvlc_media_t * p_md = user_data;
239 libvlc_event_t event;
240
241 /* Construct the event */
242 event.type = libvlc_MediaDurationChanged;
243 event.u.media_duration_changed.new_duration =
244 from_mtime(p_event->u.input_item_duration_changed.new_duration);
245
246 /* Send the event */
247 libvlc_event_send( &p_md->event_manager, &event );
248 }
249
send_parsed_changed(libvlc_media_t * p_md,libvlc_media_parsed_status_t new_status)250 static void send_parsed_changed( libvlc_media_t *p_md,
251 libvlc_media_parsed_status_t new_status )
252 {
253 libvlc_event_t event;
254
255 vlc_mutex_lock( &p_md->parsed_lock );
256 if( p_md->parsed_status == new_status )
257 {
258 vlc_mutex_unlock( &p_md->parsed_lock );
259 return;
260 }
261
262 /* Legacy: notify libvlc_media_parse */
263 if( !p_md->is_parsed )
264 {
265 p_md->is_parsed = true;
266 vlc_cond_broadcast( &p_md->parsed_cond );
267 }
268
269 p_md->parsed_status = new_status;
270 if( p_md->parsed_status == libvlc_media_parsed_status_skipped )
271 p_md->has_asked_preparse = false;
272
273 vlc_mutex_unlock( &p_md->parsed_lock );
274
275 /* Construct the event */
276 event.type = libvlc_MediaParsedChanged;
277 event.u.media_parsed_changed.new_status = new_status;
278
279 /* Send the event */
280 libvlc_event_send( &p_md->event_manager, &event );
281
282 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, false );
283 if( p_subitems != NULL )
284 {
285 /* notify the media list */
286 libvlc_media_list_lock( p_subitems );
287 libvlc_media_list_internal_end_reached( p_subitems );
288 libvlc_media_list_unlock( p_subitems );
289 }
290 }
291
292 /**************************************************************************
293 * input_item_preparse_ended (Private) (vlc event Callback)
294 **************************************************************************/
input_item_preparse_ended(const vlc_event_t * p_event,void * user_data)295 static void input_item_preparse_ended( const vlc_event_t * p_event,
296 void * user_data )
297 {
298 libvlc_media_t * p_md = user_data;
299 libvlc_media_parsed_status_t new_status;
300
301 switch( p_event->u.input_item_preparse_ended.new_status )
302 {
303 case ITEM_PREPARSE_SKIPPED:
304 new_status = libvlc_media_parsed_status_skipped;
305 break;
306 case ITEM_PREPARSE_FAILED:
307 new_status = libvlc_media_parsed_status_failed;
308 break;
309 case ITEM_PREPARSE_TIMEOUT:
310 new_status = libvlc_media_parsed_status_timeout;
311 break;
312 case ITEM_PREPARSE_DONE:
313 new_status = libvlc_media_parsed_status_done;
314 break;
315 default:
316 return;
317 }
318 send_parsed_changed( p_md, new_status );
319 }
320
321 /**************************************************************************
322 * Install event handler (Private)
323 **************************************************************************/
install_input_item_observer(libvlc_media_t * p_md)324 static void install_input_item_observer( libvlc_media_t *p_md )
325 {
326 vlc_event_attach( &p_md->p_input_item->event_manager,
327 vlc_InputItemMetaChanged,
328 input_item_meta_changed,
329 p_md );
330 vlc_event_attach( &p_md->p_input_item->event_manager,
331 vlc_InputItemDurationChanged,
332 input_item_duration_changed,
333 p_md );
334 vlc_event_attach( &p_md->p_input_item->event_manager,
335 vlc_InputItemSubItemTreeAdded,
336 input_item_subitemtree_added,
337 p_md );
338 vlc_event_attach( &p_md->p_input_item->event_manager,
339 vlc_InputItemPreparseEnded,
340 input_item_preparse_ended,
341 p_md );
342 }
343
344 /**************************************************************************
345 * Uninstall event handler (Private)
346 **************************************************************************/
uninstall_input_item_observer(libvlc_media_t * p_md)347 static void uninstall_input_item_observer( libvlc_media_t *p_md )
348 {
349 vlc_event_detach( &p_md->p_input_item->event_manager,
350 vlc_InputItemMetaChanged,
351 input_item_meta_changed,
352 p_md );
353 vlc_event_detach( &p_md->p_input_item->event_manager,
354 vlc_InputItemDurationChanged,
355 input_item_duration_changed,
356 p_md );
357 vlc_event_detach( &p_md->p_input_item->event_manager,
358 vlc_InputItemSubItemTreeAdded,
359 input_item_subitemtree_added,
360 p_md );
361 vlc_event_detach( &p_md->p_input_item->event_manager,
362 vlc_InputItemPreparseEnded,
363 input_item_preparse_ended,
364 p_md );
365 }
366
367 /**************************************************************************
368 * Create a new media descriptor object from an input_item
369 * (libvlc internal)
370 * That's the generic constructor
371 **************************************************************************/
libvlc_media_new_from_input_item(libvlc_instance_t * p_instance,input_item_t * p_input_item)372 libvlc_media_t * libvlc_media_new_from_input_item(
373 libvlc_instance_t *p_instance,
374 input_item_t *p_input_item )
375 {
376 libvlc_media_t * p_md;
377
378 if (!p_input_item)
379 {
380 libvlc_printerr( "No input item given" );
381 return NULL;
382 }
383
384 p_md = calloc( 1, sizeof(libvlc_media_t) );
385 if( !p_md )
386 {
387 libvlc_printerr( "Not enough memory" );
388 return NULL;
389 }
390
391 p_md->p_libvlc_instance = p_instance;
392 p_md->p_input_item = p_input_item;
393 p_md->i_refcount = 1;
394
395 vlc_cond_init(&p_md->parsed_cond);
396 vlc_mutex_init(&p_md->parsed_lock);
397 vlc_mutex_init(&p_md->subitems_lock);
398
399 p_md->state = libvlc_NothingSpecial;
400
401 /* A media descriptor can be a playlist. When you open a playlist
402 * It can give a bunch of item to read. */
403 p_md->p_subitems = NULL;
404
405 libvlc_event_manager_init( &p_md->event_manager, p_md );
406
407 input_item_Hold( p_md->p_input_item );
408
409 install_input_item_observer( p_md );
410
411 libvlc_retain( p_instance );
412 return p_md;
413 }
414
415 /**************************************************************************
416 * Create a new media descriptor object
417 **************************************************************************/
libvlc_media_new_location(libvlc_instance_t * p_instance,const char * psz_mrl)418 libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
419 const char * psz_mrl )
420 {
421 input_item_t * p_input_item;
422 libvlc_media_t * p_md;
423
424 p_input_item = input_item_New( psz_mrl, NULL );
425
426 if (!p_input_item)
427 {
428 libvlc_printerr( "Not enough memory" );
429 return NULL;
430 }
431
432 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
433
434 /* The p_input_item is retained in libvlc_media_new_from_input_item */
435 input_item_Release( p_input_item );
436
437 return p_md;
438 }
439
libvlc_media_new_path(libvlc_instance_t * p_instance,const char * path)440 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
441 const char *path )
442 {
443 char *mrl = vlc_path2uri( path, NULL );
444 if( unlikely(mrl == NULL) )
445 {
446 libvlc_printerr( "%s", vlc_strerror_c(errno) );
447 return NULL;
448 }
449
450 libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
451 free( mrl );
452 return m;
453 }
454
libvlc_media_new_fd(libvlc_instance_t * p_instance,int fd)455 libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
456 {
457 char mrl[16];
458 snprintf( mrl, sizeof(mrl), "fd://%d", fd );
459
460 return libvlc_media_new_location( p_instance, mrl );
461 }
462
libvlc_media_new_callbacks(libvlc_instance_t * p_instance,libvlc_media_open_cb open_cb,libvlc_media_read_cb read_cb,libvlc_media_seek_cb seek_cb,libvlc_media_close_cb close_cb,void * opaque)463 libvlc_media_t *libvlc_media_new_callbacks(libvlc_instance_t *p_instance,
464 libvlc_media_open_cb open_cb,
465 libvlc_media_read_cb read_cb,
466 libvlc_media_seek_cb seek_cb,
467 libvlc_media_close_cb close_cb,
468 void *opaque)
469 {
470 libvlc_media_t *m = libvlc_media_new_location(p_instance, "imem://");
471 if (unlikely(m == NULL))
472 return NULL;
473
474 assert(read_cb != NULL);
475 input_item_AddOpaque(m->p_input_item, "imem-data", opaque);
476 input_item_AddOpaque(m->p_input_item, "imem-open", open_cb);
477 input_item_AddOpaque(m->p_input_item, "imem-read", read_cb);
478 input_item_AddOpaque(m->p_input_item, "imem-seek", seek_cb);
479 input_item_AddOpaque(m->p_input_item, "imem-close", close_cb);
480 return m;
481 }
482
483 /**************************************************************************
484 * Create a new media descriptor object
485 **************************************************************************/
libvlc_media_new_as_node(libvlc_instance_t * p_instance,const char * psz_name)486 libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
487 const char * psz_name )
488 {
489 input_item_t * p_input_item;
490 libvlc_media_t * p_md;
491 libvlc_media_list_t * p_subitems;
492
493 p_input_item = input_item_New( "vlc://nop", psz_name );
494
495 if (!p_input_item)
496 {
497 libvlc_printerr( "Not enough memory" );
498 return NULL;
499 }
500
501 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
502 input_item_Release( p_input_item );
503
504 p_subitems = media_get_subitems( p_md, true );
505 if( p_subitems == NULL) {
506 libvlc_media_release( p_md );
507 return NULL;
508 }
509
510 return p_md;
511 }
512
513 /**************************************************************************
514 * Add an option to the media descriptor,
515 * that will be used to determine how the media_player will read the
516 * media. This allow to use VLC advanced reading/streaming
517 * options in a per-media basis
518 *
519 * The options are detailled in vlc --long-help, for instance "--sout-all"
520 **************************************************************************/
libvlc_media_add_option(libvlc_media_t * p_md,const char * psz_option)521 void libvlc_media_add_option( libvlc_media_t * p_md,
522 const char * psz_option )
523 {
524 libvlc_media_add_option_flag( p_md, psz_option,
525 VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
526 }
527
528 /**************************************************************************
529 * Same as libvlc_media_add_option but with configurable flags.
530 **************************************************************************/
libvlc_media_add_option_flag(libvlc_media_t * p_md,const char * ppsz_option,unsigned i_flags)531 void libvlc_media_add_option_flag( libvlc_media_t * p_md,
532 const char * ppsz_option,
533 unsigned i_flags )
534 {
535 input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
536 }
537
538 /**************************************************************************
539 * Delete a media descriptor object
540 **************************************************************************/
libvlc_media_release(libvlc_media_t * p_md)541 void libvlc_media_release( libvlc_media_t *p_md )
542 {
543 if (!p_md)
544 return;
545
546 p_md->i_refcount--;
547
548 if( p_md->i_refcount > 0 )
549 return;
550
551 uninstall_input_item_observer( p_md );
552
553 /* Cancel asynchronous parsing (if any) */
554 libvlc_MetadataCancel( p_md->p_libvlc_instance->p_libvlc_int, p_md );
555
556 if( p_md->p_subitems )
557 libvlc_media_list_release( p_md->p_subitems );
558
559 input_item_Release( p_md->p_input_item );
560
561 vlc_cond_destroy( &p_md->parsed_cond );
562 vlc_mutex_destroy( &p_md->parsed_lock );
563 vlc_mutex_destroy( &p_md->subitems_lock );
564
565 /* Construct the event */
566 libvlc_event_t event;
567 event.type = libvlc_MediaFreed;
568 event.u.media_freed.md = p_md;
569
570 /* Send the event */
571 libvlc_event_send( &p_md->event_manager, &event );
572
573 libvlc_event_manager_destroy( &p_md->event_manager );
574 libvlc_release( p_md->p_libvlc_instance );
575 free( p_md );
576 }
577
578 /**************************************************************************
579 * Retain a media descriptor object
580 **************************************************************************/
libvlc_media_retain(libvlc_media_t * p_md)581 void libvlc_media_retain( libvlc_media_t *p_md )
582 {
583 assert (p_md);
584 p_md->i_refcount++;
585 }
586
587 /**************************************************************************
588 * Duplicate a media descriptor object
589 **************************************************************************/
590 libvlc_media_t *
libvlc_media_duplicate(libvlc_media_t * p_md_orig)591 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
592 {
593 return libvlc_media_new_from_input_item(
594 p_md_orig->p_libvlc_instance, p_md_orig->p_input_item );
595 }
596
597 /**************************************************************************
598 * Get mrl from a media descriptor object
599 **************************************************************************/
600 char *
libvlc_media_get_mrl(libvlc_media_t * p_md)601 libvlc_media_get_mrl( libvlc_media_t * p_md )
602 {
603 assert( p_md );
604 return input_item_GetURI( p_md->p_input_item );
605 }
606
607 /**************************************************************************
608 * Getter for meta information
609 **************************************************************************/
610
libvlc_media_get_meta(libvlc_media_t * p_md,libvlc_meta_t e_meta)611 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
612 {
613 char *psz_meta = NULL;
614
615 if( e_meta == libvlc_meta_NowPlaying )
616 {
617 psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
618 }
619 else
620 {
621 psz_meta = input_item_GetMeta( p_md->p_input_item,
622 libvlc_to_vlc_meta[e_meta] );
623 /* Should be integrated in core */
624 if( psz_meta == NULL && e_meta == libvlc_meta_Title
625 && p_md->p_input_item->psz_name != NULL )
626 psz_meta = strdup( p_md->p_input_item->psz_name );
627 }
628 return psz_meta;
629 }
630
631 /**************************************************************************
632 * Setter for meta information
633 **************************************************************************/
634
libvlc_media_set_meta(libvlc_media_t * p_md,libvlc_meta_t e_meta,const char * psz_value)635 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
636 {
637 assert( p_md );
638 input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
639 }
640
libvlc_media_save_meta(libvlc_media_t * p_md)641 int libvlc_media_save_meta( libvlc_media_t *p_md )
642 {
643 assert( p_md );
644 vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
645 return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
646 }
647
648 /**************************************************************************
649 * Getter for state information
650 * Can be error, playing, buffering, NothingSpecial.
651 **************************************************************************/
652
653 libvlc_state_t
libvlc_media_get_state(libvlc_media_t * p_md)654 libvlc_media_get_state( libvlc_media_t *p_md )
655 {
656 assert( p_md );
657 return p_md->state;
658 }
659
660 /**************************************************************************
661 * Setter for state information (LibVLC Internal)
662 **************************************************************************/
663
664 void
libvlc_media_set_state(libvlc_media_t * p_md,libvlc_state_t state)665 libvlc_media_set_state( libvlc_media_t *p_md,
666 libvlc_state_t state )
667 {
668 libvlc_event_t event;
669
670 p_md->state = state;
671
672 /* Construct the event */
673 event.type = libvlc_MediaStateChanged;
674 event.u.media_state_changed.new_state = state;
675
676 /* Send the event */
677 libvlc_event_send( &p_md->event_manager, &event );
678 }
679
680 /**************************************************************************
681 * subitems
682 **************************************************************************/
683 libvlc_media_list_t *
libvlc_media_subitems(libvlc_media_t * p_md)684 libvlc_media_subitems( libvlc_media_t * p_md )
685 {
686 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
687 if( p_subitems )
688 libvlc_media_list_retain( p_subitems );
689 return p_subitems;
690 }
691
692 /**************************************************************************
693 * Getter for statistics information
694 **************************************************************************/
libvlc_media_get_stats(libvlc_media_t * p_md,libvlc_media_stats_t * p_stats)695 int libvlc_media_get_stats( libvlc_media_t *p_md,
696 libvlc_media_stats_t *p_stats )
697 {
698 input_item_t *item = p_md->p_input_item;
699
700 if( !p_md->p_input_item )
701 return false;
702
703 vlc_mutex_lock( &item->lock );
704
705 input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
706 if( p_itm_stats == NULL )
707 {
708 vlc_mutex_unlock( &item->lock );
709 return false;
710 }
711
712 vlc_mutex_lock( &p_itm_stats->lock );
713 p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
714 p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
715
716 p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
717 p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
718 p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
719 p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
720
721 p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
722 p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
723
724 p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
725 p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
726
727 p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
728 p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
729
730 p_stats->i_sent_packets = p_itm_stats->i_sent_packets;
731 p_stats->i_sent_bytes = p_itm_stats->i_sent_bytes;
732 p_stats->f_send_bitrate = p_itm_stats->f_send_bitrate;
733 vlc_mutex_unlock( &p_itm_stats->lock );
734 vlc_mutex_unlock( &item->lock );
735 return true;
736 }
737
738 /**************************************************************************
739 * event_manager
740 **************************************************************************/
741 libvlc_event_manager_t *
libvlc_media_event_manager(libvlc_media_t * p_md)742 libvlc_media_event_manager( libvlc_media_t * p_md )
743 {
744 assert( p_md );
745
746 return &p_md->event_manager;
747 }
748
749 /**************************************************************************
750 * Get duration of media object (in ms)
751 **************************************************************************/
752 int64_t
libvlc_media_get_duration(libvlc_media_t * p_md)753 libvlc_media_get_duration( libvlc_media_t * p_md )
754 {
755 assert( p_md );
756
757 if( !p_md->p_input_item )
758 {
759 libvlc_printerr( "No input item" );
760 return -1;
761 }
762
763 if (!input_item_IsPreparsed( p_md->p_input_item ))
764 return -1;
765
766 return from_mtime(input_item_GetDuration( p_md->p_input_item ));
767 }
768
media_parse(libvlc_media_t * media,bool b_async,libvlc_media_parse_flag_t parse_flag,int timeout)769 static int media_parse(libvlc_media_t *media, bool b_async,
770 libvlc_media_parse_flag_t parse_flag, int timeout)
771 {
772 bool needed;
773
774 vlc_mutex_lock(&media->parsed_lock);
775 needed = !media->has_asked_preparse;
776 media->has_asked_preparse = true;
777 if (needed)
778 media->is_parsed = false;
779 vlc_mutex_unlock(&media->parsed_lock);
780
781 if (needed)
782 {
783 libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
784 input_item_t *item = media->p_input_item;
785 input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
786 int ret;
787
788 /* Ignore libvlc_media_fetch_local flag since local art will be fetched
789 * by libvlc_MetadataRequest */
790 if (parse_flag & libvlc_media_fetch_network)
791 {
792 ret = libvlc_ArtRequest(libvlc, item,
793 META_REQUEST_OPTION_SCOPE_NETWORK);
794 if (ret != VLC_SUCCESS)
795 return ret;
796 }
797
798 if (parse_flag & libvlc_media_parse_network)
799 parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
800 if (parse_flag & libvlc_media_do_interact)
801 parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
802 ret = libvlc_MetadataRequest(libvlc, item, parse_scope, timeout, media);
803 if (ret != VLC_SUCCESS)
804 return ret;
805 }
806 else
807 return VLC_EGENERIC;
808
809 if (!b_async)
810 {
811 vlc_mutex_lock(&media->parsed_lock);
812 while (!media->is_parsed)
813 vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
814 vlc_mutex_unlock(&media->parsed_lock);
815 }
816 return VLC_SUCCESS;
817 }
818
819 /**************************************************************************
820 * Parse the media and wait.
821 **************************************************************************/
822 void
libvlc_media_parse(libvlc_media_t * media)823 libvlc_media_parse(libvlc_media_t *media)
824 {
825 media_parse( media, false, libvlc_media_fetch_local, -1 );
826 }
827
828 /**************************************************************************
829 * Parse the media but do not wait.
830 **************************************************************************/
831 void
libvlc_media_parse_async(libvlc_media_t * media)832 libvlc_media_parse_async(libvlc_media_t *media)
833 {
834 media_parse( media, true, libvlc_media_fetch_local, -1 );
835 }
836
837 /**************************************************************************
838 * Parse the media asynchronously with options.
839 **************************************************************************/
840 int
libvlc_media_parse_with_options(libvlc_media_t * media,libvlc_media_parse_flag_t parse_flag,int timeout)841 libvlc_media_parse_with_options( libvlc_media_t *media,
842 libvlc_media_parse_flag_t parse_flag,
843 int timeout )
844 {
845 return media_parse( media, true, parse_flag, timeout ) == VLC_SUCCESS ? 0 : -1;
846 }
847
848 void
libvlc_media_parse_stop(libvlc_media_t * media)849 libvlc_media_parse_stop( libvlc_media_t *media )
850 {
851 libvlc_MetadataCancel( media->p_libvlc_instance->p_libvlc_int, media );
852 }
853
854 /**************************************************************************
855 * Get parsed status for media object.
856 **************************************************************************/
857 int
libvlc_media_is_parsed(libvlc_media_t * media)858 libvlc_media_is_parsed(libvlc_media_t *media)
859 {
860 bool parsed;
861
862 vlc_mutex_lock(&media->parsed_lock);
863 parsed = media->is_parsed;
864 vlc_mutex_unlock(&media->parsed_lock);
865 return parsed;
866 }
867
868 libvlc_media_parsed_status_t
libvlc_media_get_parsed_status(libvlc_media_t * media)869 libvlc_media_get_parsed_status(libvlc_media_t *media)
870 {
871 libvlc_media_parsed_status_t status;
872
873 vlc_mutex_lock(&media->parsed_lock);
874 status = media->parsed_status;
875 vlc_mutex_unlock(&media->parsed_lock);
876 return status;
877 }
878
879 /**************************************************************************
880 * Sets media descriptor's user_data. user_data is specialized data
881 * accessed by the host application, VLC.framework uses it as a pointer to
882 * an native object that references a libvlc_media_t pointer
883 **************************************************************************/
884 void
libvlc_media_set_user_data(libvlc_media_t * p_md,void * p_new_user_data)885 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
886 {
887 assert( p_md );
888 p_md->p_user_data = p_new_user_data;
889 }
890
891 /**************************************************************************
892 * Get media descriptor's user_data. user_data is specialized data
893 * accessed by the host application, VLC.framework uses it as a pointer to
894 * an native object that references a libvlc_media_t pointer
895 **************************************************************************/
896 void *
libvlc_media_get_user_data(libvlc_media_t * p_md)897 libvlc_media_get_user_data( libvlc_media_t * p_md )
898 {
899 assert( p_md );
900 return p_md->p_user_data;
901 }
902
903 /**************************************************************************
904 * Get media descriptor's elementary streams description
905 **************************************************************************/
906 int
libvlc_media_get_tracks_info(libvlc_media_t * p_md,libvlc_media_track_info_t ** pp_es)907 libvlc_media_get_tracks_info( libvlc_media_t *p_md, libvlc_media_track_info_t ** pp_es )
908 {
909 assert( p_md );
910
911 input_item_t *p_input_item = p_md->p_input_item;
912 vlc_mutex_lock( &p_input_item->lock );
913
914 const int i_es = p_input_item->i_es;
915 *pp_es = (i_es > 0) ? vlc_alloc( i_es, sizeof(libvlc_media_track_info_t) ) : NULL;
916
917 if( !*pp_es ) /* no ES, or OOM */
918 {
919 vlc_mutex_unlock( &p_input_item->lock );
920 return 0;
921 }
922
923 /* Fill array */
924 for( int i = 0; i < i_es; i++ )
925 {
926 libvlc_media_track_info_t *p_mes = *pp_es+i;
927 const es_format_t *p_es = p_input_item->es[i];
928
929 p_mes->i_codec = p_es->i_codec;
930 p_mes->i_id = p_es->i_id;
931
932 p_mes->i_profile = p_es->i_profile;
933 p_mes->i_level = p_es->i_level;
934
935 switch(p_es->i_cat)
936 {
937 case UNKNOWN_ES:
938 default:
939 p_mes->i_type = libvlc_track_unknown;
940 break;
941 case VIDEO_ES:
942 p_mes->i_type = libvlc_track_video;
943 p_mes->u.video.i_height = p_es->video.i_visible_height;
944 p_mes->u.video.i_width = p_es->video.i_visible_width;
945 break;
946 case AUDIO_ES:
947 p_mes->i_type = libvlc_track_audio;
948 p_mes->u.audio.i_channels = p_es->audio.i_channels;
949 p_mes->u.audio.i_rate = p_es->audio.i_rate;
950 break;
951 case SPU_ES:
952 p_mes->i_type = libvlc_track_text;
953 break;
954 }
955 }
956
957 vlc_mutex_unlock( &p_input_item->lock );
958 return i_es;
959 }
960
961 unsigned
libvlc_media_tracks_get(libvlc_media_t * p_md,libvlc_media_track_t *** pp_es)962 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
963 {
964 assert( p_md );
965
966 input_item_t *p_input_item = p_md->p_input_item;
967 vlc_mutex_lock( &p_input_item->lock );
968
969 const int i_es = p_input_item->i_es;
970 *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
971
972 if( !*pp_es ) /* no ES, or OOM */
973 {
974 vlc_mutex_unlock( &p_input_item->lock );
975 return 0;
976 }
977
978 /* Fill array */
979 for( int i = 0; i < i_es; i++ )
980 {
981 libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
982 if ( p_mes )
983 {
984 p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
985 sizeof(*p_mes->video)),
986 sizeof(*p_mes->subtitle)) );
987 }
988 if ( !p_mes || !p_mes->audio )
989 {
990 libvlc_media_tracks_release( *pp_es, i_es );
991 *pp_es = NULL;
992 free( p_mes );
993 vlc_mutex_unlock( &p_input_item->lock );
994 return 0;
995 }
996 (*pp_es)[i] = p_mes;
997
998 const es_format_t *p_es = p_input_item->es[i];
999
1000 p_mes->i_codec = p_es->i_codec;
1001 p_mes->i_original_fourcc = p_es->i_original_fourcc;
1002 p_mes->i_id = p_es->i_id;
1003
1004 p_mes->i_profile = p_es->i_profile;
1005 p_mes->i_level = p_es->i_level;
1006
1007 p_mes->i_bitrate = p_es->i_bitrate;
1008 p_mes->psz_language = p_es->psz_language != NULL ? strdup(p_es->psz_language) : NULL;
1009 p_mes->psz_description = p_es->psz_description != NULL ? strdup(p_es->psz_description) : NULL;
1010
1011 switch(p_es->i_cat)
1012 {
1013 case UNKNOWN_ES:
1014 default:
1015 p_mes->i_type = libvlc_track_unknown;
1016 break;
1017 case VIDEO_ES:
1018 p_mes->i_type = libvlc_track_video;
1019 p_mes->video->i_height = p_es->video.i_visible_height;
1020 p_mes->video->i_width = p_es->video.i_visible_width;
1021 p_mes->video->i_sar_num = p_es->video.i_sar_num;
1022 p_mes->video->i_sar_den = p_es->video.i_sar_den;
1023 p_mes->video->i_frame_rate_num = p_es->video.i_frame_rate;
1024 p_mes->video->i_frame_rate_den = p_es->video.i_frame_rate_base;
1025
1026 assert( p_es->video.orientation >= ORIENT_TOP_LEFT &&
1027 p_es->video.orientation <= ORIENT_RIGHT_BOTTOM );
1028 p_mes->video->i_orientation = (int) p_es->video.orientation;
1029
1030 assert( ( p_es->video.projection_mode >= PROJECTION_MODE_RECTANGULAR &&
1031 p_es->video.projection_mode <= PROJECTION_MODE_EQUIRECTANGULAR ) ||
1032 ( p_es->video.projection_mode == PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD ) );
1033 p_mes->video->i_projection = (int) p_es->video.projection_mode;
1034
1035 p_mes->video->pose.f_yaw = p_es->video.pose.yaw;
1036 p_mes->video->pose.f_pitch = p_es->video.pose.pitch;
1037 p_mes->video->pose.f_roll = p_es->video.pose.roll;
1038 p_mes->video->pose.f_field_of_view = p_es->video.pose.fov;
1039 break;
1040 case AUDIO_ES:
1041 p_mes->i_type = libvlc_track_audio;
1042 p_mes->audio->i_channels = p_es->audio.i_channels;
1043 p_mes->audio->i_rate = p_es->audio.i_rate;
1044 break;
1045 case SPU_ES:
1046 p_mes->i_type = libvlc_track_text;
1047 p_mes->subtitle->psz_encoding = p_es->subs.psz_encoding != NULL ?
1048 strdup(p_es->subs.psz_encoding) : NULL;
1049 break;
1050 }
1051 }
1052
1053 vlc_mutex_unlock( &p_input_item->lock );
1054 return i_es;
1055 }
1056
1057 /**************************************************************************
1058 * Get codec description from media elementary stream
1059 **************************************************************************/
1060 const char *
libvlc_media_get_codec_description(libvlc_track_type_t i_type,uint32_t i_codec)1061 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
1062 uint32_t i_codec )
1063 {
1064 switch( i_type )
1065 {
1066 case libvlc_track_audio:
1067 return vlc_fourcc_GetDescription( AUDIO_ES, i_codec );
1068 case libvlc_track_video:
1069 return vlc_fourcc_GetDescription( VIDEO_ES, i_codec );
1070 case libvlc_track_text:
1071 return vlc_fourcc_GetDescription( SPU_ES, i_codec );
1072 case libvlc_track_unknown:
1073 default:
1074 return vlc_fourcc_GetDescription( UNKNOWN_ES, i_codec );
1075 }
1076 }
1077
1078 /**************************************************************************
1079 * Release media descriptor's elementary streams description array
1080 **************************************************************************/
libvlc_media_tracks_release(libvlc_media_track_t ** p_tracks,unsigned i_count)1081 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
1082 {
1083 for( unsigned i = 0; i < i_count; ++i )
1084 {
1085 if ( !p_tracks[i] )
1086 continue;
1087 free( p_tracks[i]->psz_language );
1088 free( p_tracks[i]->psz_description );
1089 switch( p_tracks[i]->i_type )
1090 {
1091 case libvlc_track_audio:
1092 break;
1093 case libvlc_track_video:
1094 break;
1095 case libvlc_track_text:
1096 free( p_tracks[i]->subtitle->psz_encoding );
1097 break;
1098 case libvlc_track_unknown:
1099 default:
1100 break;
1101 }
1102 free( p_tracks[i]->audio );
1103 free( p_tracks[i] );
1104 }
1105 free( p_tracks );
1106 }
1107
1108 /**************************************************************************
1109 * Get the media type of the media descriptor object
1110 **************************************************************************/
libvlc_media_get_type(libvlc_media_t * p_md)1111 libvlc_media_type_t libvlc_media_get_type( libvlc_media_t *p_md )
1112 {
1113 assert( p_md );
1114
1115 int i_type;
1116 input_item_t *p_input_item = p_md->p_input_item;
1117
1118 vlc_mutex_lock( &p_input_item->lock );
1119 i_type = p_md->p_input_item->i_type;
1120 vlc_mutex_unlock( &p_input_item->lock );
1121
1122 switch( i_type )
1123 {
1124 case ITEM_TYPE_FILE:
1125 return libvlc_media_type_file;
1126 case ITEM_TYPE_NODE:
1127 case ITEM_TYPE_DIRECTORY:
1128 return libvlc_media_type_directory;
1129 case ITEM_TYPE_DISC:
1130 return libvlc_media_type_disc;
1131 case ITEM_TYPE_STREAM:
1132 return libvlc_media_type_stream;
1133 case ITEM_TYPE_PLAYLIST:
1134 return libvlc_media_type_playlist;
1135 default:
1136 return libvlc_media_type_unknown;
1137 }
1138 }
1139
libvlc_media_slaves_add(libvlc_media_t * p_md,libvlc_media_slave_type_t i_type,unsigned int i_priority,const char * psz_uri)1140 int libvlc_media_slaves_add( libvlc_media_t *p_md,
1141 libvlc_media_slave_type_t i_type,
1142 unsigned int i_priority,
1143 const char *psz_uri )
1144 {
1145 assert( p_md && psz_uri );
1146 input_item_t *p_input_item = p_md->p_input_item;
1147
1148 enum slave_type i_input_slave_type;
1149 switch( i_type )
1150 {
1151 case libvlc_media_slave_type_subtitle:
1152 i_input_slave_type = SLAVE_TYPE_SPU;
1153 break;
1154 case libvlc_media_slave_type_audio:
1155 i_input_slave_type = SLAVE_TYPE_AUDIO;
1156 break;
1157 default:
1158 vlc_assert_unreachable();
1159 return -1;
1160 }
1161
1162 enum slave_priority i_input_slave_priority;
1163 switch( i_priority )
1164 {
1165 case 0:
1166 i_input_slave_priority = SLAVE_PRIORITY_MATCH_NONE;
1167 break;
1168 case 1:
1169 i_input_slave_priority = SLAVE_PRIORITY_MATCH_RIGHT;
1170 break;
1171 case 2:
1172 i_input_slave_priority = SLAVE_PRIORITY_MATCH_LEFT;
1173 break;
1174 case 3:
1175 i_input_slave_priority = SLAVE_PRIORITY_MATCH_ALL;
1176 break;
1177 default:
1178 case 4:
1179 i_input_slave_priority = SLAVE_PRIORITY_USER;
1180 break;
1181 }
1182
1183 input_item_slave_t *p_slave = input_item_slave_New( psz_uri,
1184 i_input_slave_type,
1185 i_input_slave_priority );
1186 if( p_slave == NULL )
1187 return -1;
1188 return input_item_AddSlave( p_input_item, p_slave ) == VLC_SUCCESS ? 0 : -1;
1189 }
1190
libvlc_media_slaves_clear(libvlc_media_t * p_md)1191 void libvlc_media_slaves_clear( libvlc_media_t *p_md )
1192 {
1193 assert( p_md );
1194 input_item_t *p_input_item = p_md->p_input_item;
1195
1196 vlc_mutex_lock( &p_input_item->lock );
1197 for( int i = 0; i < p_input_item->i_slaves; i++ )
1198 input_item_slave_Delete( p_input_item->pp_slaves[i] );
1199 TAB_CLEAN( p_input_item->i_slaves, p_input_item->pp_slaves );
1200 vlc_mutex_unlock( &p_input_item->lock );
1201 }
1202
libvlc_media_slaves_get(libvlc_media_t * p_md,libvlc_media_slave_t *** ppp_slaves)1203 unsigned int libvlc_media_slaves_get( libvlc_media_t *p_md,
1204 libvlc_media_slave_t ***ppp_slaves )
1205 {
1206 assert( p_md && ppp_slaves );
1207 input_item_t *p_input_item = p_md->p_input_item;
1208 *ppp_slaves = NULL;
1209
1210 vlc_mutex_lock( &p_input_item->lock );
1211
1212 int i_count = p_input_item->i_slaves;
1213 if( i_count <= 0 )
1214 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1215
1216 libvlc_media_slave_t **pp_slaves = calloc( i_count, sizeof(*pp_slaves) );
1217 if( pp_slaves == NULL )
1218 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1219
1220 for( int i = 0; i < i_count; ++i )
1221 {
1222 input_item_slave_t *p_item_slave = p_input_item->pp_slaves[i];
1223 assert( p_item_slave->i_priority >= SLAVE_PRIORITY_MATCH_NONE );
1224
1225 /* also allocate psz_uri buffer at the end of the struct */
1226 libvlc_media_slave_t *p_slave = malloc( sizeof(*p_slave) +
1227 strlen( p_item_slave->psz_uri )
1228 + 1 );
1229 if( p_slave == NULL )
1230 {
1231 libvlc_media_slaves_release(pp_slaves, i);
1232 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1233 }
1234 p_slave->psz_uri = (char *) ((uint8_t *)p_slave) + sizeof(*p_slave);
1235 strcpy( p_slave->psz_uri, p_item_slave->psz_uri );
1236
1237 switch( p_item_slave->i_type )
1238 {
1239 case SLAVE_TYPE_SPU:
1240 p_slave->i_type = libvlc_media_slave_type_subtitle;
1241 break;
1242 case SLAVE_TYPE_AUDIO:
1243 p_slave->i_type = libvlc_media_slave_type_audio;
1244 break;
1245 default:
1246 vlc_assert_unreachable();
1247 }
1248
1249 switch( p_item_slave->i_priority )
1250 {
1251 case SLAVE_PRIORITY_MATCH_NONE:
1252 p_slave->i_priority = 0;
1253 break;
1254 case SLAVE_PRIORITY_MATCH_RIGHT:
1255 p_slave->i_priority = 1;
1256 break;
1257 case SLAVE_PRIORITY_MATCH_LEFT:
1258 p_slave->i_priority = 2;
1259 break;
1260 case SLAVE_PRIORITY_MATCH_ALL:
1261 p_slave->i_priority = 3;
1262 break;
1263 case SLAVE_PRIORITY_USER:
1264 p_slave->i_priority = 4;
1265 break;
1266 default:
1267 vlc_assert_unreachable();
1268 }
1269 pp_slaves[i] = p_slave;
1270 }
1271 vlc_mutex_unlock( &p_input_item->lock );
1272
1273 *ppp_slaves = pp_slaves;
1274 return i_count;
1275 }
1276
libvlc_media_slaves_release(libvlc_media_slave_t ** pp_slaves,unsigned int i_count)1277 void libvlc_media_slaves_release( libvlc_media_slave_t **pp_slaves,
1278 unsigned int i_count )
1279 {
1280 if( i_count > 0 )
1281 {
1282 assert( pp_slaves );
1283 for( unsigned int i = 0; i < i_count; ++i )
1284 free( pp_slaves[i] );
1285 }
1286 free( pp_slaves );
1287 }
1288