Decklink: Fix swapped format arguments
[vlc.git] / lib / media.c
blob17deae436e2a9801c63febd73511f0f93073efcc
1 /*****************************************************************************
2 * media.c: Libvlc API media descripor management
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Pierre d'Herbemont <pdherbemont@videolan.org>
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.
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.
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
29 #include <errno.h>
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>
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>
42 #include "../src/libvlc.h"
44 #include "libvlc_internal.h"
45 #include "media_internal.h"
46 #include "media_list_internal.h"
48 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
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
78 static const libvlc_meta_t vlc_to_libvlc_meta[] =
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
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" );
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" );
126 static_assert(
127 MULTIVIEW_2D == (int) libvlc_video_multiview_2d &&
128 MULTIVIEW_STEREO_SBS == (int) libvlc_video_multiview_stereo_sbs &&
129 MULTIVIEW_STEREO_TB == (int) libvlc_video_multiview_stereo_tb &&
130 MULTIVIEW_STEREO_ROW == (int) libvlc_video_multiview_stereo_row &&
131 MULTIVIEW_STEREO_COL == (int) libvlc_video_multiview_stereo_col &&
132 MULTIVIEW_STEREO_FRAME == (int) libvlc_video_multiview_stereo_frame &&
133 MULTIVIEW_STEREO_CHECKERBOARD == (int) libvlc_video_multiview_stereo_checkerboard,
134 "Mismatch between libvlc_video_multiview_t and video_multiview_mode_t");
136 static libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md,
137 bool b_create )
139 libvlc_media_list_t *p_subitems = NULL;
141 vlc_mutex_lock( &p_md->subitems_lock );
142 if( p_md->p_subitems == NULL && b_create )
144 p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance );
145 if( p_md->p_subitems != NULL )
147 p_md->p_subitems->b_read_only = true;
148 p_md->p_subitems->p_internal_md = p_md;
151 p_subitems = p_md->p_subitems;
152 vlc_mutex_unlock( &p_md->subitems_lock );
153 return p_subitems;
156 static libvlc_media_t *input_item_add_subitem( libvlc_media_t *p_md,
157 input_item_t *item )
159 libvlc_media_t * p_md_child;
160 libvlc_media_list_t *p_subitems;
161 libvlc_event_t event;
163 p_md_child = libvlc_media_new_from_input_item( p_md->p_libvlc_instance,
164 item );
166 /* Add this to our media list */
167 p_subitems = media_get_subitems( p_md, true );
168 if( p_subitems != NULL )
170 libvlc_media_list_lock( p_subitems );
171 libvlc_media_list_internal_add_media( p_subitems, p_md_child );
172 libvlc_media_list_unlock( p_subitems );
175 /* Construct the event */
176 event.type = libvlc_MediaSubItemAdded;
177 event.u.media_subitem_added.new_child = p_md_child;
179 /* Send the event */
180 libvlc_event_send( &p_md->event_manager, &event );
181 return p_md_child;
184 static void input_item_add_subnode( libvlc_media_t *md,
185 input_item_node_t *node )
187 for( int i = 0; i < node->i_children; i++ )
189 input_item_node_t *child = node->pp_children[i];
190 libvlc_media_t *md_child = input_item_add_subitem( md, child->p_item );
192 if( md_child != NULL )
194 input_item_add_subnode( md_child, child );
195 libvlc_media_release( md_child );
200 /**************************************************************************
201 * input_item_subitemtree_added (Private) (vlc event Callback)
202 **************************************************************************/
203 static void input_item_subtree_added(input_item_t *item,
204 input_item_node_t *node,
205 void *user_data)
207 VLC_UNUSED(item);
208 libvlc_media_t * p_md = user_data;
209 libvlc_media_add_subtree(p_md, node);
212 void libvlc_media_add_subtree(libvlc_media_t *p_md, input_item_node_t *node)
214 /* FIXME FIXME FIXME
215 * Recursive function calls seem much simpler for this. But playlists are
216 * untrusted and can be arbitrarily deep (e.g. with XSPF). So recursion can
217 * potentially lead to plain old stack overflow. */
218 input_item_add_subnode( p_md, node );
220 /* Construct the event */
221 libvlc_event_t event;
222 event.type = libvlc_MediaSubItemTreeAdded;
223 event.u.media_subitemtree_added.item = p_md;
225 /* Send the event */
226 libvlc_event_send( &p_md->event_manager, &event );
229 /**************************************************************************
230 * input_item_meta_changed (Private) (vlc event Callback)
231 **************************************************************************/
232 static void input_item_meta_changed( const vlc_event_t *p_event,
233 void * user_data )
235 libvlc_media_t * p_md = user_data;
236 libvlc_event_t event;
238 /* Construct the event */
239 event.type = libvlc_MediaMetaChanged;
240 event.u.media_meta_changed.meta_type =
241 vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
243 /* Send the event */
244 libvlc_event_send( &p_md->event_manager, &event );
247 /**************************************************************************
248 * input_item_duration_changed (Private) (vlc event Callback)
249 **************************************************************************/
250 static void input_item_duration_changed( const vlc_event_t *p_event,
251 void * user_data )
253 libvlc_media_t * p_md = user_data;
254 libvlc_event_t event;
256 /* Construct the event */
257 event.type = libvlc_MediaDurationChanged;
258 event.u.media_duration_changed.new_duration =
259 from_mtime(p_event->u.input_item_duration_changed.new_duration);
261 /* Send the event */
262 libvlc_event_send( &p_md->event_manager, &event );
265 static void send_parsed_changed( libvlc_media_t *p_md,
266 libvlc_media_parsed_status_t new_status )
268 libvlc_event_t event;
270 vlc_mutex_lock( &p_md->parsed_lock );
271 if( p_md->parsed_status == new_status )
273 vlc_mutex_unlock( &p_md->parsed_lock );
274 return;
277 /* Legacy: notify libvlc_media_parse */
278 if( !p_md->is_parsed )
280 p_md->is_parsed = true;
281 vlc_cond_broadcast( &p_md->parsed_cond );
284 p_md->parsed_status = new_status;
285 if( p_md->parsed_status == libvlc_media_parsed_status_skipped )
286 p_md->has_asked_preparse = false;
288 vlc_mutex_unlock( &p_md->parsed_lock );
290 if( new_status == libvlc_media_parsed_status_done )
292 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, false );
293 if( p_subitems != NULL )
295 /* notify the media list */
296 libvlc_media_list_lock( p_subitems );
297 libvlc_media_list_internal_end_reached( p_subitems );
298 libvlc_media_list_unlock( p_subitems );
302 /* Construct the event */
303 event.type = libvlc_MediaParsedChanged;
304 event.u.media_parsed_changed.new_status = new_status;
306 /* Send the event */
307 libvlc_event_send( &p_md->event_manager, &event );
310 /**************************************************************************
311 * input_item_preparse_ended (Private) (vlc event Callback)
312 **************************************************************************/
313 static void input_item_preparse_ended(input_item_t *item,
314 enum input_item_preparse_status status,
315 void *user_data)
317 VLC_UNUSED(item);
318 libvlc_media_t * p_md = user_data;
319 libvlc_media_parsed_status_t new_status;
321 switch( status )
323 case ITEM_PREPARSE_SKIPPED:
324 new_status = libvlc_media_parsed_status_skipped;
325 break;
326 case ITEM_PREPARSE_FAILED:
327 new_status = libvlc_media_parsed_status_failed;
328 break;
329 case ITEM_PREPARSE_TIMEOUT:
330 new_status = libvlc_media_parsed_status_timeout;
331 break;
332 case ITEM_PREPARSE_DONE:
333 new_status = libvlc_media_parsed_status_done;
334 break;
335 default:
336 return;
338 send_parsed_changed( p_md, new_status );
341 /**************************************************************************
342 * Install event handler (Private)
343 **************************************************************************/
344 static void install_input_item_observer( libvlc_media_t *p_md )
346 vlc_event_attach( &p_md->p_input_item->event_manager,
347 vlc_InputItemMetaChanged,
348 input_item_meta_changed,
349 p_md );
350 vlc_event_attach( &p_md->p_input_item->event_manager,
351 vlc_InputItemDurationChanged,
352 input_item_duration_changed,
353 p_md );
356 /**************************************************************************
357 * Uninstall event handler (Private)
358 **************************************************************************/
359 static void uninstall_input_item_observer( libvlc_media_t *p_md )
361 vlc_event_detach( &p_md->p_input_item->event_manager,
362 vlc_InputItemMetaChanged,
363 input_item_meta_changed,
364 p_md );
365 vlc_event_detach( &p_md->p_input_item->event_manager,
366 vlc_InputItemDurationChanged,
367 input_item_duration_changed,
368 p_md );
371 /**************************************************************************
372 * Create a new media descriptor object from an input_item
373 * (libvlc internal)
374 * That's the generic constructor
375 **************************************************************************/
376 libvlc_media_t * libvlc_media_new_from_input_item(
377 libvlc_instance_t *p_instance,
378 input_item_t *p_input_item )
380 libvlc_media_t * p_md;
382 if (!p_input_item)
384 libvlc_printerr( "No input item given" );
385 return NULL;
388 p_md = calloc( 1, sizeof(libvlc_media_t) );
389 if( !p_md )
391 libvlc_printerr( "Not enough memory" );
392 return NULL;
395 p_md->p_libvlc_instance = p_instance;
396 p_md->p_input_item = p_input_item;
397 p_md->i_refcount = 1;
399 vlc_cond_init(&p_md->parsed_cond);
400 vlc_mutex_init(&p_md->parsed_lock);
401 vlc_mutex_init(&p_md->subitems_lock);
403 p_md->state = libvlc_NothingSpecial;
405 /* A media descriptor can be a playlist. When you open a playlist
406 * It can give a bunch of item to read. */
407 p_md->p_subitems = NULL;
409 libvlc_event_manager_init( &p_md->event_manager, p_md );
411 input_item_Hold( p_md->p_input_item );
413 install_input_item_observer( p_md );
415 libvlc_retain( p_instance );
416 return p_md;
419 /**************************************************************************
420 * Create a new media descriptor object
421 **************************************************************************/
422 libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
423 const char * psz_mrl )
425 input_item_t * p_input_item;
426 libvlc_media_t * p_md;
428 p_input_item = input_item_New( psz_mrl, NULL );
430 if (!p_input_item)
432 libvlc_printerr( "Not enough memory" );
433 return NULL;
436 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
438 /* The p_input_item is retained in libvlc_media_new_from_input_item */
439 input_item_Release( p_input_item );
441 return p_md;
444 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
445 const char *path )
447 char *mrl = vlc_path2uri( path, NULL );
448 if( unlikely(mrl == NULL) )
450 libvlc_printerr( "%s", vlc_strerror_c(errno) );
451 return NULL;
454 libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
455 free( mrl );
456 return m;
459 libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
461 char mrl[16];
462 snprintf( mrl, sizeof(mrl), "fd://%d", fd );
464 return libvlc_media_new_location( p_instance, mrl );
467 libvlc_media_t *libvlc_media_new_callbacks(libvlc_instance_t *p_instance,
468 libvlc_media_open_cb open_cb,
469 libvlc_media_read_cb read_cb,
470 libvlc_media_seek_cb seek_cb,
471 libvlc_media_close_cb close_cb,
472 void *opaque)
474 libvlc_media_t *m = libvlc_media_new_location(p_instance, "imem://");
475 if (unlikely(m == NULL))
476 return NULL;
478 assert(read_cb != NULL);
479 input_item_AddOpaque(m->p_input_item, "imem-data", opaque);
480 input_item_AddOpaque(m->p_input_item, "imem-open", open_cb);
481 input_item_AddOpaque(m->p_input_item, "imem-read", read_cb);
482 input_item_AddOpaque(m->p_input_item, "imem-seek", seek_cb);
483 input_item_AddOpaque(m->p_input_item, "imem-close", close_cb);
484 return m;
487 /**************************************************************************
488 * Create a new media descriptor object
489 **************************************************************************/
490 libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
491 const char * psz_name )
493 input_item_t * p_input_item;
494 libvlc_media_t * p_md;
495 libvlc_media_list_t * p_subitems;
497 p_input_item = input_item_New( INPUT_ITEM_URI_NOP, psz_name );
499 if (!p_input_item)
501 libvlc_printerr( "Not enough memory" );
502 return NULL;
505 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
506 input_item_Release( p_input_item );
508 p_subitems = media_get_subitems( p_md, true );
509 if( p_subitems == NULL) {
510 libvlc_media_release( p_md );
511 return NULL;
514 return p_md;
517 /**************************************************************************
518 * Add an option to the media descriptor,
519 * that will be used to determine how the media_player will read the
520 * media. This allow to use VLC advanced reading/streaming
521 * options in a per-media basis
523 * The options are detailled in vlc --long-help, for instance "--sout-all"
524 **************************************************************************/
525 void libvlc_media_add_option( libvlc_media_t * p_md,
526 const char * psz_option )
528 libvlc_media_add_option_flag( p_md, psz_option,
529 VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
532 /**************************************************************************
533 * Same as libvlc_media_add_option but with configurable flags.
534 **************************************************************************/
535 void libvlc_media_add_option_flag( libvlc_media_t * p_md,
536 const char * ppsz_option,
537 unsigned i_flags )
539 input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
542 /**************************************************************************
543 * Delete a media descriptor object
544 **************************************************************************/
545 void libvlc_media_release( libvlc_media_t *p_md )
547 if (!p_md)
548 return;
550 p_md->i_refcount--;
552 if( p_md->i_refcount > 0 )
553 return;
555 uninstall_input_item_observer( p_md );
557 /* Cancel asynchronous parsing (if any) */
558 libvlc_MetadataCancel( p_md->p_libvlc_instance->p_libvlc_int, p_md );
560 if( p_md->p_subitems )
561 libvlc_media_list_release( p_md->p_subitems );
563 input_item_Release( p_md->p_input_item );
565 vlc_cond_destroy( &p_md->parsed_cond );
566 vlc_mutex_destroy( &p_md->parsed_lock );
567 vlc_mutex_destroy( &p_md->subitems_lock );
569 /* Construct the event */
570 libvlc_event_t event;
571 event.type = libvlc_MediaFreed;
572 event.u.media_freed.md = p_md;
574 /* Send the event */
575 libvlc_event_send( &p_md->event_manager, &event );
577 libvlc_event_manager_destroy( &p_md->event_manager );
578 libvlc_release( p_md->p_libvlc_instance );
579 free( p_md );
582 /**************************************************************************
583 * Retain a media descriptor object
584 **************************************************************************/
585 void libvlc_media_retain( libvlc_media_t *p_md )
587 assert (p_md);
588 p_md->i_refcount++;
591 /**************************************************************************
592 * Duplicate a media descriptor object
593 **************************************************************************/
594 libvlc_media_t *
595 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
597 return libvlc_media_new_from_input_item(
598 p_md_orig->p_libvlc_instance, p_md_orig->p_input_item );
601 /**************************************************************************
602 * Get mrl from a media descriptor object
603 **************************************************************************/
604 char *
605 libvlc_media_get_mrl( libvlc_media_t * p_md )
607 assert( p_md );
608 return input_item_GetURI( p_md->p_input_item );
611 /**************************************************************************
612 * Getter for meta information
613 **************************************************************************/
615 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
617 char *psz_meta = NULL;
619 if( e_meta == libvlc_meta_NowPlaying )
621 psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
623 else
625 psz_meta = input_item_GetMeta( p_md->p_input_item,
626 libvlc_to_vlc_meta[e_meta] );
627 /* Should be integrated in core */
628 if( psz_meta == NULL && e_meta == libvlc_meta_Title
629 && p_md->p_input_item->psz_name != NULL )
630 psz_meta = strdup( p_md->p_input_item->psz_name );
632 return psz_meta;
635 /**************************************************************************
636 * Setter for meta information
637 **************************************************************************/
639 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
641 assert( p_md );
642 input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
645 int libvlc_media_save_meta( libvlc_media_t *p_md )
647 assert( p_md );
648 vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
649 return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
652 /**************************************************************************
653 * Getter for state information
654 * Can be error, playing, buffering, NothingSpecial.
655 **************************************************************************/
657 libvlc_state_t
658 libvlc_media_get_state( libvlc_media_t *p_md )
660 assert( p_md );
661 return p_md->state;
664 /**************************************************************************
665 * Setter for state information (LibVLC Internal)
666 **************************************************************************/
668 void
669 libvlc_media_set_state( libvlc_media_t *p_md,
670 libvlc_state_t state )
672 libvlc_event_t event;
674 p_md->state = state;
676 /* Construct the event */
677 event.type = libvlc_MediaStateChanged;
678 event.u.media_state_changed.new_state = state;
680 /* Send the event */
681 libvlc_event_send( &p_md->event_manager, &event );
684 /**************************************************************************
685 * subitems
686 **************************************************************************/
687 libvlc_media_list_t *
688 libvlc_media_subitems( libvlc_media_t * p_md )
690 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
691 if( p_subitems )
692 libvlc_media_list_retain( p_subitems );
693 return p_subitems;
696 /**************************************************************************
697 * Getter for statistics information
698 **************************************************************************/
699 int libvlc_media_get_stats( libvlc_media_t *p_md,
700 libvlc_media_stats_t *p_stats )
702 input_item_t *item = p_md->p_input_item;
704 if( !p_md->p_input_item )
705 return false;
707 vlc_mutex_lock( &item->lock );
709 input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
710 if( p_itm_stats == NULL )
712 vlc_mutex_unlock( &item->lock );
713 return false;
716 p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
717 p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
719 p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
720 p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
721 p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
722 p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
724 p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
725 p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
727 p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
728 p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
730 p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
731 p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
733 p_stats->i_sent_packets = 0;
734 p_stats->i_sent_bytes = 0;
735 p_stats->f_send_bitrate = 0.;
737 vlc_mutex_unlock( &item->lock );
738 return true;
741 /**************************************************************************
742 * event_manager
743 **************************************************************************/
744 libvlc_event_manager_t *
745 libvlc_media_event_manager( libvlc_media_t * p_md )
747 assert( p_md );
749 return &p_md->event_manager;
752 /**************************************************************************
753 * Get duration of media object (in ms)
754 **************************************************************************/
755 int64_t
756 libvlc_media_get_duration( libvlc_media_t * p_md )
758 assert( p_md );
760 if( !p_md->p_input_item )
762 libvlc_printerr( "No input item" );
763 return -1;
766 if (!input_item_IsPreparsed( p_md->p_input_item ))
767 return -1;
769 return from_mtime(input_item_GetDuration( p_md->p_input_item ));
772 static const input_preparser_callbacks_t input_preparser_callbacks = {
773 .on_preparse_ended = input_item_preparse_ended,
774 .on_subtree_added = input_item_subtree_added,
777 static int media_parse(libvlc_media_t *media, bool b_async,
778 libvlc_media_parse_flag_t parse_flag, int timeout)
780 bool needed;
782 vlc_mutex_lock(&media->parsed_lock);
783 needed = !media->has_asked_preparse;
784 media->has_asked_preparse = true;
785 if (needed)
786 media->is_parsed = false;
787 vlc_mutex_unlock(&media->parsed_lock);
789 if (needed)
791 libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
792 input_item_t *item = media->p_input_item;
793 input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
794 int ret;
796 /* Ignore libvlc_media_fetch_local flag since local art will be fetched
797 * by libvlc_MetadataRequest */
798 if (parse_flag & libvlc_media_fetch_network)
800 ret = libvlc_ArtRequest(libvlc, item,
801 META_REQUEST_OPTION_SCOPE_NETWORK,
802 NULL, NULL);
803 if (ret != VLC_SUCCESS)
804 return ret;
807 if (parse_flag & libvlc_media_parse_network)
808 parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
809 if (parse_flag & libvlc_media_do_interact)
810 parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
811 ret = libvlc_MetadataRequest(libvlc, item, parse_scope, &input_preparser_callbacks, media, timeout, media);
812 if (ret != VLC_SUCCESS)
813 return ret;
815 else
816 return VLC_EGENERIC;
818 if (!b_async)
820 vlc_mutex_lock(&media->parsed_lock);
821 while (!media->is_parsed)
822 vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
823 vlc_mutex_unlock(&media->parsed_lock);
825 return VLC_SUCCESS;
828 /**************************************************************************
829 * Parse the media and wait.
830 **************************************************************************/
831 void
832 libvlc_media_parse(libvlc_media_t *media)
834 media_parse( media, false, libvlc_media_fetch_local, -1 );
837 /**************************************************************************
838 * Parse the media but do not wait.
839 **************************************************************************/
840 void
841 libvlc_media_parse_async(libvlc_media_t *media)
843 media_parse( media, true, libvlc_media_fetch_local, -1 );
846 /**************************************************************************
847 * Parse the media asynchronously with options.
848 **************************************************************************/
850 libvlc_media_parse_with_options( libvlc_media_t *media,
851 libvlc_media_parse_flag_t parse_flag,
852 int timeout )
854 return media_parse( media, true, parse_flag, timeout ) == VLC_SUCCESS ? 0 : -1;
857 void
858 libvlc_media_parse_stop( libvlc_media_t *media )
860 libvlc_MetadataCancel( media->p_libvlc_instance->p_libvlc_int, media );
863 /**************************************************************************
864 * Get parsed status for media object.
865 **************************************************************************/
867 libvlc_media_is_parsed(libvlc_media_t *media)
869 bool parsed;
871 vlc_mutex_lock(&media->parsed_lock);
872 parsed = media->is_parsed;
873 vlc_mutex_unlock(&media->parsed_lock);
874 return parsed;
877 libvlc_media_parsed_status_t
878 libvlc_media_get_parsed_status(libvlc_media_t *media)
880 libvlc_media_parsed_status_t status;
882 vlc_mutex_lock(&media->parsed_lock);
883 status = media->parsed_status;
884 vlc_mutex_unlock(&media->parsed_lock);
885 return status;
888 /**************************************************************************
889 * Sets media descriptor's user_data. user_data is specialized data
890 * accessed by the host application, VLC.framework uses it as a pointer to
891 * an native object that references a libvlc_media_t pointer
892 **************************************************************************/
893 void
894 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
896 assert( p_md );
897 p_md->p_user_data = p_new_user_data;
900 /**************************************************************************
901 * Get media descriptor's user_data. user_data is specialized data
902 * accessed by the host application, VLC.framework uses it as a pointer to
903 * an native object that references a libvlc_media_t pointer
904 **************************************************************************/
905 void *
906 libvlc_media_get_user_data( libvlc_media_t * p_md )
908 assert( p_md );
909 return p_md->p_user_data;
912 unsigned
913 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
915 assert( p_md );
917 input_item_t *p_input_item = p_md->p_input_item;
918 vlc_mutex_lock( &p_input_item->lock );
920 const int i_es = p_input_item->i_es;
921 *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
923 if( !*pp_es ) /* no ES, or OOM */
925 vlc_mutex_unlock( &p_input_item->lock );
926 return 0;
929 /* Fill array */
930 for( int i = 0; i < i_es; i++ )
932 libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
933 if ( p_mes )
935 p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
936 sizeof(*p_mes->video)),
937 sizeof(*p_mes->subtitle)) );
939 if ( !p_mes || !p_mes->audio )
941 libvlc_media_tracks_release( *pp_es, i_es );
942 *pp_es = NULL;
943 free( p_mes );
944 vlc_mutex_unlock( &p_input_item->lock );
945 return 0;
947 (*pp_es)[i] = p_mes;
949 const es_format_t *p_es = p_input_item->es[i];
951 p_mes->i_codec = p_es->i_codec;
952 p_mes->i_original_fourcc = p_es->i_original_fourcc;
953 p_mes->i_id = p_es->i_id;
955 p_mes->i_profile = p_es->i_profile;
956 p_mes->i_level = p_es->i_level;
958 p_mes->i_bitrate = p_es->i_bitrate;
959 p_mes->psz_language = p_es->psz_language != NULL ? strdup(p_es->psz_language) : NULL;
960 p_mes->psz_description = p_es->psz_description != NULL ? strdup(p_es->psz_description) : NULL;
962 switch(p_es->i_cat)
964 case UNKNOWN_ES:
965 default:
966 p_mes->i_type = libvlc_track_unknown;
967 break;
968 case VIDEO_ES:
969 p_mes->i_type = libvlc_track_video;
970 p_mes->video->i_height = p_es->video.i_visible_height;
971 p_mes->video->i_width = p_es->video.i_visible_width;
972 p_mes->video->i_sar_num = p_es->video.i_sar_num;
973 p_mes->video->i_sar_den = p_es->video.i_sar_den;
974 p_mes->video->i_frame_rate_num = p_es->video.i_frame_rate;
975 p_mes->video->i_frame_rate_den = p_es->video.i_frame_rate_base;
977 assert( p_es->video.orientation >= ORIENT_TOP_LEFT &&
978 p_es->video.orientation <= ORIENT_RIGHT_BOTTOM );
979 p_mes->video->i_orientation = (int) p_es->video.orientation;
981 assert( ( p_es->video.projection_mode >= PROJECTION_MODE_RECTANGULAR &&
982 p_es->video.projection_mode <= PROJECTION_MODE_EQUIRECTANGULAR ) ||
983 ( p_es->video.projection_mode == PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD ) );
984 p_mes->video->i_projection = (int) p_es->video.projection_mode;
986 p_mes->video->pose.f_yaw = p_es->video.pose.yaw;
987 p_mes->video->pose.f_pitch = p_es->video.pose.pitch;
988 p_mes->video->pose.f_roll = p_es->video.pose.roll;
989 p_mes->video->pose.f_field_of_view = p_es->video.pose.fov;
991 assert( p_es->video.multiview_mode >= MULTIVIEW_2D &&
992 p_es->video.multiview_mode <= MULTIVIEW_STEREO_CHECKERBOARD );
993 p_mes->video->i_multiview = (int) p_es->video.multiview_mode;
994 break;
995 case AUDIO_ES:
996 p_mes->i_type = libvlc_track_audio;
997 p_mes->audio->i_channels = p_es->audio.i_channels;
998 p_mes->audio->i_rate = p_es->audio.i_rate;
999 break;
1000 case SPU_ES:
1001 p_mes->i_type = libvlc_track_text;
1002 p_mes->subtitle->psz_encoding = p_es->subs.psz_encoding != NULL ?
1003 strdup(p_es->subs.psz_encoding) : NULL;
1004 break;
1008 vlc_mutex_unlock( &p_input_item->lock );
1009 return i_es;
1012 /**************************************************************************
1013 * Get codec description from media elementary stream
1014 **************************************************************************/
1015 const char *
1016 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
1017 uint32_t i_codec )
1019 switch( i_type )
1021 case libvlc_track_audio:
1022 return vlc_fourcc_GetDescription( AUDIO_ES, i_codec );
1023 case libvlc_track_video:
1024 return vlc_fourcc_GetDescription( VIDEO_ES, i_codec );
1025 case libvlc_track_text:
1026 return vlc_fourcc_GetDescription( SPU_ES, i_codec );
1027 case libvlc_track_unknown:
1028 default:
1029 return vlc_fourcc_GetDescription( UNKNOWN_ES, i_codec );
1033 /**************************************************************************
1034 * Release media descriptor's elementary streams description array
1035 **************************************************************************/
1036 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
1038 for( unsigned i = 0; i < i_count; ++i )
1040 if ( !p_tracks[i] )
1041 continue;
1042 free( p_tracks[i]->psz_language );
1043 free( p_tracks[i]->psz_description );
1044 switch( p_tracks[i]->i_type )
1046 case libvlc_track_audio:
1047 break;
1048 case libvlc_track_video:
1049 break;
1050 case libvlc_track_text:
1051 free( p_tracks[i]->subtitle->psz_encoding );
1052 break;
1053 case libvlc_track_unknown:
1054 default:
1055 break;
1057 free( p_tracks[i]->audio );
1058 free( p_tracks[i] );
1060 free( p_tracks );
1063 /**************************************************************************
1064 * Get the media type of the media descriptor object
1065 **************************************************************************/
1066 libvlc_media_type_t libvlc_media_get_type( libvlc_media_t *p_md )
1068 assert( p_md );
1070 enum input_item_type_e i_type;
1071 input_item_t *p_input_item = p_md->p_input_item;
1073 vlc_mutex_lock( &p_input_item->lock );
1074 i_type = p_md->p_input_item->i_type;
1075 vlc_mutex_unlock( &p_input_item->lock );
1077 switch( i_type )
1079 case ITEM_TYPE_FILE:
1080 return libvlc_media_type_file;
1081 case ITEM_TYPE_NODE:
1082 case ITEM_TYPE_DIRECTORY:
1083 return libvlc_media_type_directory;
1084 case ITEM_TYPE_DISC:
1085 return libvlc_media_type_disc;
1086 case ITEM_TYPE_STREAM:
1087 return libvlc_media_type_stream;
1088 case ITEM_TYPE_PLAYLIST:
1089 return libvlc_media_type_playlist;
1090 default:
1091 return libvlc_media_type_unknown;
1095 int libvlc_media_slaves_add( libvlc_media_t *p_md,
1096 libvlc_media_slave_type_t i_type,
1097 unsigned int i_priority,
1098 const char *psz_uri )
1100 assert( p_md && psz_uri );
1101 input_item_t *p_input_item = p_md->p_input_item;
1103 enum slave_type i_input_slave_type;
1104 switch( i_type )
1106 case libvlc_media_slave_type_subtitle:
1107 i_input_slave_type = SLAVE_TYPE_SPU;
1108 break;
1109 case libvlc_media_slave_type_audio:
1110 i_input_slave_type = SLAVE_TYPE_AUDIO;
1111 break;
1112 default:
1113 vlc_assert_unreachable();
1114 return -1;
1117 enum slave_priority i_input_slave_priority;
1118 switch( i_priority )
1120 case 0:
1121 i_input_slave_priority = SLAVE_PRIORITY_MATCH_NONE;
1122 break;
1123 case 1:
1124 i_input_slave_priority = SLAVE_PRIORITY_MATCH_RIGHT;
1125 break;
1126 case 2:
1127 i_input_slave_priority = SLAVE_PRIORITY_MATCH_LEFT;
1128 break;
1129 case 3:
1130 i_input_slave_priority = SLAVE_PRIORITY_MATCH_ALL;
1131 break;
1132 default:
1133 case 4:
1134 i_input_slave_priority = SLAVE_PRIORITY_USER;
1135 break;
1138 input_item_slave_t *p_slave = input_item_slave_New( psz_uri,
1139 i_input_slave_type,
1140 i_input_slave_priority );
1141 if( p_slave == NULL )
1142 return -1;
1143 return input_item_AddSlave( p_input_item, p_slave ) == VLC_SUCCESS ? 0 : -1;
1146 void libvlc_media_slaves_clear( libvlc_media_t *p_md )
1148 assert( p_md );
1149 input_item_t *p_input_item = p_md->p_input_item;
1151 vlc_mutex_lock( &p_input_item->lock );
1152 for( int i = 0; i < p_input_item->i_slaves; i++ )
1153 input_item_slave_Delete( p_input_item->pp_slaves[i] );
1154 TAB_CLEAN( p_input_item->i_slaves, p_input_item->pp_slaves );
1155 vlc_mutex_unlock( &p_input_item->lock );
1158 unsigned int libvlc_media_slaves_get( libvlc_media_t *p_md,
1159 libvlc_media_slave_t ***ppp_slaves )
1161 assert( p_md && ppp_slaves );
1162 input_item_t *p_input_item = p_md->p_input_item;
1163 *ppp_slaves = NULL;
1165 vlc_mutex_lock( &p_input_item->lock );
1167 int i_count = p_input_item->i_slaves;
1168 if( i_count <= 0 )
1169 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1171 libvlc_media_slave_t **pp_slaves = calloc( i_count, sizeof(*pp_slaves) );
1172 if( pp_slaves == NULL )
1173 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1175 for( int i = 0; i < i_count; ++i )
1177 input_item_slave_t *p_item_slave = p_input_item->pp_slaves[i];
1178 assert( p_item_slave->i_priority >= SLAVE_PRIORITY_MATCH_NONE );
1180 /* also allocate psz_uri buffer at the end of the struct */
1181 libvlc_media_slave_t *p_slave = malloc( sizeof(*p_slave) +
1182 strlen( p_item_slave->psz_uri )
1183 + 1 );
1184 if( p_slave == NULL )
1186 libvlc_media_slaves_release(pp_slaves, i);
1187 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1189 p_slave->psz_uri = (char *) ((uint8_t *)p_slave) + sizeof(*p_slave);
1190 strcpy( p_slave->psz_uri, p_item_slave->psz_uri );
1192 switch( p_item_slave->i_type )
1194 case SLAVE_TYPE_SPU:
1195 p_slave->i_type = libvlc_media_slave_type_subtitle;
1196 break;
1197 case SLAVE_TYPE_AUDIO:
1198 p_slave->i_type = libvlc_media_slave_type_audio;
1199 break;
1200 default:
1201 vlc_assert_unreachable();
1204 switch( p_item_slave->i_priority )
1206 case SLAVE_PRIORITY_MATCH_NONE:
1207 p_slave->i_priority = 0;
1208 break;
1209 case SLAVE_PRIORITY_MATCH_RIGHT:
1210 p_slave->i_priority = 1;
1211 break;
1212 case SLAVE_PRIORITY_MATCH_LEFT:
1213 p_slave->i_priority = 2;
1214 break;
1215 case SLAVE_PRIORITY_MATCH_ALL:
1216 p_slave->i_priority = 3;
1217 break;
1218 case SLAVE_PRIORITY_USER:
1219 p_slave->i_priority = 4;
1220 break;
1221 default:
1222 vlc_assert_unreachable();
1224 pp_slaves[i] = p_slave;
1226 vlc_mutex_unlock( &p_input_item->lock );
1228 *ppp_slaves = pp_slaves;
1229 return i_count;
1232 void libvlc_media_slaves_release( libvlc_media_slave_t **pp_slaves,
1233 unsigned int i_count )
1235 if( i_count > 0 )
1237 assert( pp_slaves );
1238 for( unsigned int i = 0; i < i_count; ++i )
1239 free( pp_slaves[i] );
1241 free( pp_slaves );