chroma: chain: add BGRA fallback
[vlc.git] / lib / media.c
blob58881e315f915d2fbc02f024d1ecde1bc07b407a
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 libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md,
127 bool b_create )
129 libvlc_media_list_t *p_subitems = NULL;
131 vlc_mutex_lock( &p_md->subitems_lock );
132 if( p_md->p_subitems == NULL && b_create )
134 p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance );
135 if( p_md->p_subitems != NULL )
137 p_md->p_subitems->b_read_only = true;
138 p_md->p_subitems->p_internal_md = p_md;
141 p_subitems = p_md->p_subitems;
142 vlc_mutex_unlock( &p_md->subitems_lock );
143 return p_subitems;
146 static libvlc_media_t *input_item_add_subitem( libvlc_media_t *p_md,
147 input_item_t *item )
149 libvlc_media_t * p_md_child;
150 libvlc_media_list_t *p_subitems;
151 libvlc_event_t event;
153 p_md_child = libvlc_media_new_from_input_item( p_md->p_libvlc_instance,
154 item );
156 /* Add this to our media list */
157 p_subitems = media_get_subitems( p_md, true );
158 if( p_subitems != NULL )
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 );
165 /* Construct the event */
166 event.type = libvlc_MediaSubItemAdded;
167 event.u.media_subitem_added.new_child = p_md_child;
169 /* Send the event */
170 libvlc_event_send( &p_md->event_manager, &event );
171 return p_md_child;
174 static void input_item_add_subnode( libvlc_media_t *md,
175 input_item_node_t *node )
177 for( int i = 0; i < node->i_children; i++ )
179 input_item_node_t *child = node->pp_children[i];
180 libvlc_media_t *md_child = input_item_add_subitem( md, child->p_item );
182 if( md_child != NULL )
184 input_item_add_subnode( md_child, child );
185 libvlc_media_release( md_child );
190 /**************************************************************************
191 * input_item_subitemtree_added (Private) (vlc event Callback)
192 **************************************************************************/
193 static void input_item_subitemtree_added( const vlc_event_t * p_event,
194 void * user_data )
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;
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 );
206 /* Construct the event */
207 event.type = libvlc_MediaSubItemTreeAdded;
208 event.u.media_subitemtree_added.item = p_md;
210 /* Send the event */
211 libvlc_event_send( &p_md->event_manager, &event );
214 /**************************************************************************
215 * input_item_meta_changed (Private) (vlc event Callback)
216 **************************************************************************/
217 static void input_item_meta_changed( const vlc_event_t *p_event,
218 void * user_data )
220 libvlc_media_t * p_md = user_data;
221 libvlc_event_t event;
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];
228 /* Send the event */
229 libvlc_event_send( &p_md->event_manager, &event );
232 /**************************************************************************
233 * input_item_duration_changed (Private) (vlc event Callback)
234 **************************************************************************/
235 static void input_item_duration_changed( const vlc_event_t *p_event,
236 void * user_data )
238 libvlc_media_t * p_md = user_data;
239 libvlc_event_t event;
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);
246 /* Send the event */
247 libvlc_event_send( &p_md->event_manager, &event );
250 static void send_parsed_changed( libvlc_media_t *p_md,
251 libvlc_media_parsed_status_t new_status )
253 libvlc_event_t event;
255 vlc_mutex_lock( &p_md->parsed_lock );
256 if( p_md->parsed_status == new_status )
258 vlc_mutex_unlock( &p_md->parsed_lock );
259 return;
262 /* Legacy: notify libvlc_media_parse */
263 if( !p_md->is_parsed )
265 p_md->is_parsed = true;
266 vlc_cond_broadcast( &p_md->parsed_cond );
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;
273 vlc_mutex_unlock( &p_md->parsed_lock );
275 if( new_status == libvlc_media_parsed_status_done )
277 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, false );
278 if( p_subitems != NULL )
280 /* notify the media list */
281 libvlc_media_list_lock( p_subitems );
282 libvlc_media_list_internal_end_reached( p_subitems );
283 libvlc_media_list_unlock( p_subitems );
287 /* Construct the event */
288 event.type = libvlc_MediaParsedChanged;
289 event.u.media_parsed_changed.new_status = new_status;
291 /* Send the event */
292 libvlc_event_send( &p_md->event_manager, &event );
295 /**************************************************************************
296 * input_item_preparse_ended (Private) (vlc event Callback)
297 **************************************************************************/
298 static void input_item_preparse_ended( const vlc_event_t * p_event,
299 void * user_data )
301 libvlc_media_t * p_md = user_data;
302 libvlc_media_parsed_status_t new_status;
304 switch( p_event->u.input_item_preparse_ended.new_status )
306 case ITEM_PREPARSE_SKIPPED:
307 new_status = libvlc_media_parsed_status_skipped;
308 break;
309 case ITEM_PREPARSE_FAILED:
310 new_status = libvlc_media_parsed_status_failed;
311 break;
312 case ITEM_PREPARSE_TIMEOUT:
313 new_status = libvlc_media_parsed_status_timeout;
314 break;
315 case ITEM_PREPARSE_DONE:
316 new_status = libvlc_media_parsed_status_done;
317 break;
318 default:
319 return;
321 send_parsed_changed( p_md, new_status );
324 /**************************************************************************
325 * Install event handler (Private)
326 **************************************************************************/
327 static void install_input_item_observer( libvlc_media_t *p_md )
329 vlc_event_attach( &p_md->p_input_item->event_manager,
330 vlc_InputItemMetaChanged,
331 input_item_meta_changed,
332 p_md );
333 vlc_event_attach( &p_md->p_input_item->event_manager,
334 vlc_InputItemDurationChanged,
335 input_item_duration_changed,
336 p_md );
337 vlc_event_attach( &p_md->p_input_item->event_manager,
338 vlc_InputItemSubItemTreeAdded,
339 input_item_subitemtree_added,
340 p_md );
341 vlc_event_attach( &p_md->p_input_item->event_manager,
342 vlc_InputItemPreparseEnded,
343 input_item_preparse_ended,
344 p_md );
347 /**************************************************************************
348 * Uninstall event handler (Private)
349 **************************************************************************/
350 static void uninstall_input_item_observer( libvlc_media_t *p_md )
352 vlc_event_detach( &p_md->p_input_item->event_manager,
353 vlc_InputItemMetaChanged,
354 input_item_meta_changed,
355 p_md );
356 vlc_event_detach( &p_md->p_input_item->event_manager,
357 vlc_InputItemDurationChanged,
358 input_item_duration_changed,
359 p_md );
360 vlc_event_detach( &p_md->p_input_item->event_manager,
361 vlc_InputItemSubItemTreeAdded,
362 input_item_subitemtree_added,
363 p_md );
364 vlc_event_detach( &p_md->p_input_item->event_manager,
365 vlc_InputItemPreparseEnded,
366 input_item_preparse_ended,
367 p_md );
370 /**************************************************************************
371 * Create a new media descriptor object from an input_item
372 * (libvlc internal)
373 * That's the generic constructor
374 **************************************************************************/
375 libvlc_media_t * libvlc_media_new_from_input_item(
376 libvlc_instance_t *p_instance,
377 input_item_t *p_input_item )
379 libvlc_media_t * p_md;
381 if (!p_input_item)
383 libvlc_printerr( "No input item given" );
384 return NULL;
387 p_md = calloc( 1, sizeof(libvlc_media_t) );
388 if( !p_md )
390 libvlc_printerr( "Not enough memory" );
391 return NULL;
394 p_md->p_libvlc_instance = p_instance;
395 p_md->p_input_item = p_input_item;
396 p_md->i_refcount = 1;
398 vlc_cond_init(&p_md->parsed_cond);
399 vlc_mutex_init(&p_md->parsed_lock);
400 vlc_mutex_init(&p_md->subitems_lock);
402 p_md->state = libvlc_NothingSpecial;
404 /* A media descriptor can be a playlist. When you open a playlist
405 * It can give a bunch of item to read. */
406 p_md->p_subitems = NULL;
408 libvlc_event_manager_init( &p_md->event_manager, p_md );
410 input_item_Hold( p_md->p_input_item );
412 install_input_item_observer( p_md );
414 libvlc_retain( p_instance );
415 return p_md;
418 /**************************************************************************
419 * Create a new media descriptor object
420 **************************************************************************/
421 libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
422 const char * psz_mrl )
424 input_item_t * p_input_item;
425 libvlc_media_t * p_md;
427 p_input_item = input_item_New( psz_mrl, NULL );
429 if (!p_input_item)
431 libvlc_printerr( "Not enough memory" );
432 return NULL;
435 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
437 /* The p_input_item is retained in libvlc_media_new_from_input_item */
438 input_item_Release( p_input_item );
440 return p_md;
443 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
444 const char *path )
446 char *mrl = vlc_path2uri( path, NULL );
447 if( unlikely(mrl == NULL) )
449 libvlc_printerr( "%s", vlc_strerror_c(errno) );
450 return NULL;
453 libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
454 free( mrl );
455 return m;
458 libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
460 char mrl[16];
461 snprintf( mrl, sizeof(mrl), "fd://%d", fd );
463 return libvlc_media_new_location( p_instance, mrl );
466 libvlc_media_t *libvlc_media_new_callbacks(libvlc_instance_t *p_instance,
467 libvlc_media_open_cb open_cb,
468 libvlc_media_read_cb read_cb,
469 libvlc_media_seek_cb seek_cb,
470 libvlc_media_close_cb close_cb,
471 void *opaque)
473 libvlc_media_t *m = libvlc_media_new_location(p_instance, "imem://");
474 if (unlikely(m == NULL))
475 return NULL;
477 assert(read_cb != NULL);
478 input_item_AddOpaque(m->p_input_item, "imem-data", opaque);
479 input_item_AddOpaque(m->p_input_item, "imem-open", open_cb);
480 input_item_AddOpaque(m->p_input_item, "imem-read", read_cb);
481 input_item_AddOpaque(m->p_input_item, "imem-seek", seek_cb);
482 input_item_AddOpaque(m->p_input_item, "imem-close", close_cb);
483 return m;
486 /**************************************************************************
487 * Create a new media descriptor object
488 **************************************************************************/
489 libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
490 const char * psz_name )
492 input_item_t * p_input_item;
493 libvlc_media_t * p_md;
494 libvlc_media_list_t * p_subitems;
496 p_input_item = input_item_New( "vlc://nop", psz_name );
498 if (!p_input_item)
500 libvlc_printerr( "Not enough memory" );
501 return NULL;
504 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
505 input_item_Release( p_input_item );
507 p_subitems = media_get_subitems( p_md, true );
508 if( p_subitems == NULL) {
509 libvlc_media_release( p_md );
510 return NULL;
513 return p_md;
516 /**************************************************************************
517 * Add an option to the media descriptor,
518 * that will be used to determine how the media_player will read the
519 * media. This allow to use VLC advanced reading/streaming
520 * options in a per-media basis
522 * The options are detailled in vlc --long-help, for instance "--sout-all"
523 **************************************************************************/
524 void libvlc_media_add_option( libvlc_media_t * p_md,
525 const char * psz_option )
527 libvlc_media_add_option_flag( p_md, psz_option,
528 VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
531 /**************************************************************************
532 * Same as libvlc_media_add_option but with configurable flags.
533 **************************************************************************/
534 void libvlc_media_add_option_flag( libvlc_media_t * p_md,
535 const char * ppsz_option,
536 unsigned i_flags )
538 input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
541 /**************************************************************************
542 * Delete a media descriptor object
543 **************************************************************************/
544 void libvlc_media_release( libvlc_media_t *p_md )
546 if (!p_md)
547 return;
549 p_md->i_refcount--;
551 if( p_md->i_refcount > 0 )
552 return;
554 uninstall_input_item_observer( p_md );
556 /* Cancel asynchronous parsing (if any) */
557 libvlc_MetadataCancel( p_md->p_libvlc_instance->p_libvlc_int, p_md );
559 if( p_md->p_subitems )
560 libvlc_media_list_release( p_md->p_subitems );
562 input_item_Release( p_md->p_input_item );
564 vlc_cond_destroy( &p_md->parsed_cond );
565 vlc_mutex_destroy( &p_md->parsed_lock );
566 vlc_mutex_destroy( &p_md->subitems_lock );
568 /* Construct the event */
569 libvlc_event_t event;
570 event.type = libvlc_MediaFreed;
571 event.u.media_freed.md = p_md;
573 /* Send the event */
574 libvlc_event_send( &p_md->event_manager, &event );
576 libvlc_event_manager_destroy( &p_md->event_manager );
577 libvlc_release( p_md->p_libvlc_instance );
578 free( p_md );
581 /**************************************************************************
582 * Retain a media descriptor object
583 **************************************************************************/
584 void libvlc_media_retain( libvlc_media_t *p_md )
586 assert (p_md);
587 p_md->i_refcount++;
590 /**************************************************************************
591 * Duplicate a media descriptor object
592 **************************************************************************/
593 libvlc_media_t *
594 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
596 return libvlc_media_new_from_input_item(
597 p_md_orig->p_libvlc_instance, p_md_orig->p_input_item );
600 /**************************************************************************
601 * Get mrl from a media descriptor object
602 **************************************************************************/
603 char *
604 libvlc_media_get_mrl( libvlc_media_t * p_md )
606 assert( p_md );
607 return input_item_GetURI( p_md->p_input_item );
610 /**************************************************************************
611 * Getter for meta information
612 **************************************************************************/
614 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
616 char *psz_meta = NULL;
618 if( e_meta == libvlc_meta_NowPlaying )
620 psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
622 else
624 psz_meta = input_item_GetMeta( p_md->p_input_item,
625 libvlc_to_vlc_meta[e_meta] );
626 /* Should be integrated in core */
627 if( psz_meta == NULL && e_meta == libvlc_meta_Title
628 && p_md->p_input_item->psz_name != NULL )
629 psz_meta = strdup( p_md->p_input_item->psz_name );
631 return psz_meta;
634 /**************************************************************************
635 * Setter for meta information
636 **************************************************************************/
638 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
640 assert( p_md );
641 input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
644 int libvlc_media_save_meta( libvlc_media_t *p_md )
646 assert( p_md );
647 vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
648 return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
651 /**************************************************************************
652 * Getter for state information
653 * Can be error, playing, buffering, NothingSpecial.
654 **************************************************************************/
656 libvlc_state_t
657 libvlc_media_get_state( libvlc_media_t *p_md )
659 assert( p_md );
660 return p_md->state;
663 /**************************************************************************
664 * Setter for state information (LibVLC Internal)
665 **************************************************************************/
667 void
668 libvlc_media_set_state( libvlc_media_t *p_md,
669 libvlc_state_t state )
671 libvlc_event_t event;
673 p_md->state = state;
675 /* Construct the event */
676 event.type = libvlc_MediaStateChanged;
677 event.u.media_state_changed.new_state = state;
679 /* Send the event */
680 libvlc_event_send( &p_md->event_manager, &event );
683 /**************************************************************************
684 * subitems
685 **************************************************************************/
686 libvlc_media_list_t *
687 libvlc_media_subitems( libvlc_media_t * p_md )
689 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
690 if( p_subitems )
691 libvlc_media_list_retain( p_subitems );
692 return p_subitems;
695 /**************************************************************************
696 * Getter for statistics information
697 **************************************************************************/
698 int libvlc_media_get_stats( libvlc_media_t *p_md,
699 libvlc_media_stats_t *p_stats )
701 input_item_t *item = p_md->p_input_item;
703 if( !p_md->p_input_item )
704 return false;
706 vlc_mutex_lock( &item->lock );
708 input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
709 if( p_itm_stats == NULL )
711 vlc_mutex_unlock( &item->lock );
712 return false;
715 p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
716 p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
718 p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
719 p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
720 p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
721 p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
723 p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
724 p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
726 p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
727 p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
729 p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
730 p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
732 p_stats->i_sent_packets = 0;
733 p_stats->i_sent_bytes = 0;
734 p_stats->f_send_bitrate = 0.;
736 vlc_mutex_unlock( &item->lock );
737 return true;
740 /**************************************************************************
741 * event_manager
742 **************************************************************************/
743 libvlc_event_manager_t *
744 libvlc_media_event_manager( libvlc_media_t * p_md )
746 assert( p_md );
748 return &p_md->event_manager;
751 /**************************************************************************
752 * Get duration of media object (in ms)
753 **************************************************************************/
754 int64_t
755 libvlc_media_get_duration( libvlc_media_t * p_md )
757 assert( p_md );
759 if( !p_md->p_input_item )
761 libvlc_printerr( "No input item" );
762 return -1;
765 if (!input_item_IsPreparsed( p_md->p_input_item ))
766 return -1;
768 return from_mtime(input_item_GetDuration( p_md->p_input_item ));
771 static int media_parse(libvlc_media_t *media, bool b_async,
772 libvlc_media_parse_flag_t parse_flag, int timeout)
774 bool needed;
776 vlc_mutex_lock(&media->parsed_lock);
777 needed = !media->has_asked_preparse;
778 media->has_asked_preparse = true;
779 if (needed)
780 media->is_parsed = false;
781 vlc_mutex_unlock(&media->parsed_lock);
783 if (needed)
785 libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
786 input_item_t *item = media->p_input_item;
787 input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
788 int ret;
790 /* Ignore libvlc_media_fetch_local flag since local art will be fetched
791 * by libvlc_MetadataRequest */
792 if (parse_flag & libvlc_media_fetch_network)
794 ret = libvlc_ArtRequest(libvlc, item,
795 META_REQUEST_OPTION_SCOPE_NETWORK);
796 if (ret != VLC_SUCCESS)
797 return ret;
800 if (parse_flag & libvlc_media_parse_network)
801 parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
802 if (parse_flag & libvlc_media_do_interact)
803 parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
804 ret = libvlc_MetadataRequest(libvlc, item, parse_scope, timeout, media);
805 if (ret != VLC_SUCCESS)
806 return ret;
808 else
809 return VLC_EGENERIC;
811 if (!b_async)
813 vlc_mutex_lock(&media->parsed_lock);
814 while (!media->is_parsed)
815 vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
816 vlc_mutex_unlock(&media->parsed_lock);
818 return VLC_SUCCESS;
821 /**************************************************************************
822 * Parse the media and wait.
823 **************************************************************************/
824 void
825 libvlc_media_parse(libvlc_media_t *media)
827 media_parse( media, false, libvlc_media_fetch_local, -1 );
830 /**************************************************************************
831 * Parse the media but do not wait.
832 **************************************************************************/
833 void
834 libvlc_media_parse_async(libvlc_media_t *media)
836 media_parse( media, true, libvlc_media_fetch_local, -1 );
839 /**************************************************************************
840 * Parse the media asynchronously with options.
841 **************************************************************************/
843 libvlc_media_parse_with_options( libvlc_media_t *media,
844 libvlc_media_parse_flag_t parse_flag,
845 int timeout )
847 return media_parse( media, true, parse_flag, timeout ) == VLC_SUCCESS ? 0 : -1;
850 void
851 libvlc_media_parse_stop( libvlc_media_t *media )
853 libvlc_MetadataCancel( media->p_libvlc_instance->p_libvlc_int, media );
856 /**************************************************************************
857 * Get parsed status for media object.
858 **************************************************************************/
860 libvlc_media_is_parsed(libvlc_media_t *media)
862 bool parsed;
864 vlc_mutex_lock(&media->parsed_lock);
865 parsed = media->is_parsed;
866 vlc_mutex_unlock(&media->parsed_lock);
867 return parsed;
870 libvlc_media_parsed_status_t
871 libvlc_media_get_parsed_status(libvlc_media_t *media)
873 libvlc_media_parsed_status_t status;
875 vlc_mutex_lock(&media->parsed_lock);
876 status = media->parsed_status;
877 vlc_mutex_unlock(&media->parsed_lock);
878 return status;
881 /**************************************************************************
882 * Sets media descriptor's user_data. user_data is specialized data
883 * accessed by the host application, VLC.framework uses it as a pointer to
884 * an native object that references a libvlc_media_t pointer
885 **************************************************************************/
886 void
887 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
889 assert( p_md );
890 p_md->p_user_data = p_new_user_data;
893 /**************************************************************************
894 * Get media descriptor's user_data. user_data is specialized data
895 * accessed by the host application, VLC.framework uses it as a pointer to
896 * an native object that references a libvlc_media_t pointer
897 **************************************************************************/
898 void *
899 libvlc_media_get_user_data( libvlc_media_t * p_md )
901 assert( p_md );
902 return p_md->p_user_data;
905 /**************************************************************************
906 * Get media descriptor's elementary streams description
907 **************************************************************************/
909 libvlc_media_get_tracks_info( libvlc_media_t *p_md, libvlc_media_track_info_t ** pp_es )
911 assert( p_md );
913 input_item_t *p_input_item = p_md->p_input_item;
914 vlc_mutex_lock( &p_input_item->lock );
916 const int i_es = p_input_item->i_es;
917 *pp_es = (i_es > 0) ? vlc_alloc( i_es, sizeof(libvlc_media_track_info_t) ) : NULL;
919 if( !*pp_es ) /* no ES, or OOM */
921 vlc_mutex_unlock( &p_input_item->lock );
922 return 0;
925 /* Fill array */
926 for( int i = 0; i < i_es; i++ )
928 libvlc_media_track_info_t *p_mes = *pp_es+i;
929 const es_format_t *p_es = p_input_item->es[i];
931 p_mes->i_codec = p_es->i_codec;
932 p_mes->i_id = p_es->i_id;
934 p_mes->i_profile = p_es->i_profile;
935 p_mes->i_level = p_es->i_level;
937 switch(p_es->i_cat)
939 case UNKNOWN_ES:
940 default:
941 p_mes->i_type = libvlc_track_unknown;
942 break;
943 case VIDEO_ES:
944 p_mes->i_type = libvlc_track_video;
945 p_mes->u.video.i_height = p_es->video.i_visible_height;
946 p_mes->u.video.i_width = p_es->video.i_visible_width;
947 break;
948 case AUDIO_ES:
949 p_mes->i_type = libvlc_track_audio;
950 p_mes->u.audio.i_channels = p_es->audio.i_channels;
951 p_mes->u.audio.i_rate = p_es->audio.i_rate;
952 break;
953 case SPU_ES:
954 p_mes->i_type = libvlc_track_text;
955 break;
959 vlc_mutex_unlock( &p_input_item->lock );
960 return i_es;
963 unsigned
964 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
966 assert( p_md );
968 input_item_t *p_input_item = p_md->p_input_item;
969 vlc_mutex_lock( &p_input_item->lock );
971 const int i_es = p_input_item->i_es;
972 *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
974 if( !*pp_es ) /* no ES, or OOM */
976 vlc_mutex_unlock( &p_input_item->lock );
977 return 0;
980 /* Fill array */
981 for( int i = 0; i < i_es; i++ )
983 libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
984 if ( p_mes )
986 p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
987 sizeof(*p_mes->video)),
988 sizeof(*p_mes->subtitle)) );
990 if ( !p_mes || !p_mes->audio )
992 libvlc_media_tracks_release( *pp_es, i_es );
993 *pp_es = NULL;
994 free( p_mes );
995 vlc_mutex_unlock( &p_input_item->lock );
996 return 0;
998 (*pp_es)[i] = p_mes;
1000 const es_format_t *p_es = p_input_item->es[i];
1002 p_mes->i_codec = p_es->i_codec;
1003 p_mes->i_original_fourcc = p_es->i_original_fourcc;
1004 p_mes->i_id = p_es->i_id;
1006 p_mes->i_profile = p_es->i_profile;
1007 p_mes->i_level = p_es->i_level;
1009 p_mes->i_bitrate = p_es->i_bitrate;
1010 p_mes->psz_language = p_es->psz_language != NULL ? strdup(p_es->psz_language) : NULL;
1011 p_mes->psz_description = p_es->psz_description != NULL ? strdup(p_es->psz_description) : NULL;
1013 switch(p_es->i_cat)
1015 case UNKNOWN_ES:
1016 default:
1017 p_mes->i_type = libvlc_track_unknown;
1018 break;
1019 case VIDEO_ES:
1020 p_mes->i_type = libvlc_track_video;
1021 p_mes->video->i_height = p_es->video.i_visible_height;
1022 p_mes->video->i_width = p_es->video.i_visible_width;
1023 p_mes->video->i_sar_num = p_es->video.i_sar_num;
1024 p_mes->video->i_sar_den = p_es->video.i_sar_den;
1025 p_mes->video->i_frame_rate_num = p_es->video.i_frame_rate;
1026 p_mes->video->i_frame_rate_den = p_es->video.i_frame_rate_base;
1028 assert( p_es->video.orientation >= ORIENT_TOP_LEFT &&
1029 p_es->video.orientation <= ORIENT_RIGHT_BOTTOM );
1030 p_mes->video->i_orientation = (int) p_es->video.orientation;
1032 assert( ( p_es->video.projection_mode >= PROJECTION_MODE_RECTANGULAR &&
1033 p_es->video.projection_mode <= PROJECTION_MODE_EQUIRECTANGULAR ) ||
1034 ( p_es->video.projection_mode == PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD ) );
1035 p_mes->video->i_projection = (int) p_es->video.projection_mode;
1037 p_mes->video->pose.f_yaw = p_es->video.pose.yaw;
1038 p_mes->video->pose.f_pitch = p_es->video.pose.pitch;
1039 p_mes->video->pose.f_roll = p_es->video.pose.roll;
1040 p_mes->video->pose.f_field_of_view = p_es->video.pose.fov;
1041 break;
1042 case AUDIO_ES:
1043 p_mes->i_type = libvlc_track_audio;
1044 p_mes->audio->i_channels = p_es->audio.i_channels;
1045 p_mes->audio->i_rate = p_es->audio.i_rate;
1046 break;
1047 case SPU_ES:
1048 p_mes->i_type = libvlc_track_text;
1049 p_mes->subtitle->psz_encoding = p_es->subs.psz_encoding != NULL ?
1050 strdup(p_es->subs.psz_encoding) : NULL;
1051 break;
1055 vlc_mutex_unlock( &p_input_item->lock );
1056 return i_es;
1059 /**************************************************************************
1060 * Get codec description from media elementary stream
1061 **************************************************************************/
1062 const char *
1063 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
1064 uint32_t i_codec )
1066 switch( i_type )
1068 case libvlc_track_audio:
1069 return vlc_fourcc_GetDescription( AUDIO_ES, i_codec );
1070 case libvlc_track_video:
1071 return vlc_fourcc_GetDescription( VIDEO_ES, i_codec );
1072 case libvlc_track_text:
1073 return vlc_fourcc_GetDescription( SPU_ES, i_codec );
1074 case libvlc_track_unknown:
1075 default:
1076 return vlc_fourcc_GetDescription( UNKNOWN_ES, i_codec );
1080 /**************************************************************************
1081 * Release media descriptor's elementary streams description array
1082 **************************************************************************/
1083 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
1085 for( unsigned i = 0; i < i_count; ++i )
1087 if ( !p_tracks[i] )
1088 continue;
1089 free( p_tracks[i]->psz_language );
1090 free( p_tracks[i]->psz_description );
1091 switch( p_tracks[i]->i_type )
1093 case libvlc_track_audio:
1094 break;
1095 case libvlc_track_video:
1096 break;
1097 case libvlc_track_text:
1098 free( p_tracks[i]->subtitle->psz_encoding );
1099 break;
1100 case libvlc_track_unknown:
1101 default:
1102 break;
1104 free( p_tracks[i]->audio );
1105 free( p_tracks[i] );
1107 free( p_tracks );
1110 /**************************************************************************
1111 * Get the media type of the media descriptor object
1112 **************************************************************************/
1113 libvlc_media_type_t libvlc_media_get_type( libvlc_media_t *p_md )
1115 assert( p_md );
1117 int i_type;
1118 input_item_t *p_input_item = p_md->p_input_item;
1120 vlc_mutex_lock( &p_input_item->lock );
1121 i_type = p_md->p_input_item->i_type;
1122 vlc_mutex_unlock( &p_input_item->lock );
1124 switch( i_type )
1126 case ITEM_TYPE_FILE:
1127 return libvlc_media_type_file;
1128 case ITEM_TYPE_NODE:
1129 case ITEM_TYPE_DIRECTORY:
1130 return libvlc_media_type_directory;
1131 case ITEM_TYPE_DISC:
1132 return libvlc_media_type_disc;
1133 case ITEM_TYPE_STREAM:
1134 return libvlc_media_type_stream;
1135 case ITEM_TYPE_PLAYLIST:
1136 return libvlc_media_type_playlist;
1137 default:
1138 return libvlc_media_type_unknown;
1142 int libvlc_media_slaves_add( libvlc_media_t *p_md,
1143 libvlc_media_slave_type_t i_type,
1144 unsigned int i_priority,
1145 const char *psz_uri )
1147 assert( p_md && psz_uri );
1148 input_item_t *p_input_item = p_md->p_input_item;
1150 enum slave_type i_input_slave_type;
1151 switch( i_type )
1153 case libvlc_media_slave_type_subtitle:
1154 i_input_slave_type = SLAVE_TYPE_SPU;
1155 break;
1156 case libvlc_media_slave_type_audio:
1157 i_input_slave_type = SLAVE_TYPE_AUDIO;
1158 break;
1159 default:
1160 vlc_assert_unreachable();
1161 return -1;
1164 enum slave_priority i_input_slave_priority;
1165 switch( i_priority )
1167 case 0:
1168 i_input_slave_priority = SLAVE_PRIORITY_MATCH_NONE;
1169 break;
1170 case 1:
1171 i_input_slave_priority = SLAVE_PRIORITY_MATCH_RIGHT;
1172 break;
1173 case 2:
1174 i_input_slave_priority = SLAVE_PRIORITY_MATCH_LEFT;
1175 break;
1176 case 3:
1177 i_input_slave_priority = SLAVE_PRIORITY_MATCH_ALL;
1178 break;
1179 default:
1180 case 4:
1181 i_input_slave_priority = SLAVE_PRIORITY_USER;
1182 break;
1185 input_item_slave_t *p_slave = input_item_slave_New( psz_uri,
1186 i_input_slave_type,
1187 i_input_slave_priority );
1188 if( p_slave == NULL )
1189 return -1;
1190 return input_item_AddSlave( p_input_item, p_slave ) == VLC_SUCCESS ? 0 : -1;
1193 void libvlc_media_slaves_clear( libvlc_media_t *p_md )
1195 assert( p_md );
1196 input_item_t *p_input_item = p_md->p_input_item;
1198 vlc_mutex_lock( &p_input_item->lock );
1199 for( int i = 0; i < p_input_item->i_slaves; i++ )
1200 input_item_slave_Delete( p_input_item->pp_slaves[i] );
1201 TAB_CLEAN( p_input_item->i_slaves, p_input_item->pp_slaves );
1202 vlc_mutex_unlock( &p_input_item->lock );
1205 unsigned int libvlc_media_slaves_get( libvlc_media_t *p_md,
1206 libvlc_media_slave_t ***ppp_slaves )
1208 assert( p_md && ppp_slaves );
1209 input_item_t *p_input_item = p_md->p_input_item;
1210 *ppp_slaves = NULL;
1212 vlc_mutex_lock( &p_input_item->lock );
1214 int i_count = p_input_item->i_slaves;
1215 if( i_count <= 0 )
1216 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1218 libvlc_media_slave_t **pp_slaves = calloc( i_count, sizeof(*pp_slaves) );
1219 if( pp_slaves == NULL )
1220 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1222 for( int i = 0; i < i_count; ++i )
1224 input_item_slave_t *p_item_slave = p_input_item->pp_slaves[i];
1225 assert( p_item_slave->i_priority >= SLAVE_PRIORITY_MATCH_NONE );
1227 /* also allocate psz_uri buffer at the end of the struct */
1228 libvlc_media_slave_t *p_slave = malloc( sizeof(*p_slave) +
1229 strlen( p_item_slave->psz_uri )
1230 + 1 );
1231 if( p_slave == NULL )
1233 libvlc_media_slaves_release(pp_slaves, i);
1234 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1236 p_slave->psz_uri = (char *) ((uint8_t *)p_slave) + sizeof(*p_slave);
1237 strcpy( p_slave->psz_uri, p_item_slave->psz_uri );
1239 switch( p_item_slave->i_type )
1241 case SLAVE_TYPE_SPU:
1242 p_slave->i_type = libvlc_media_slave_type_subtitle;
1243 break;
1244 case SLAVE_TYPE_AUDIO:
1245 p_slave->i_type = libvlc_media_slave_type_audio;
1246 break;
1247 default:
1248 vlc_assert_unreachable();
1251 switch( p_item_slave->i_priority )
1253 case SLAVE_PRIORITY_MATCH_NONE:
1254 p_slave->i_priority = 0;
1255 break;
1256 case SLAVE_PRIORITY_MATCH_RIGHT:
1257 p_slave->i_priority = 1;
1258 break;
1259 case SLAVE_PRIORITY_MATCH_LEFT:
1260 p_slave->i_priority = 2;
1261 break;
1262 case SLAVE_PRIORITY_MATCH_ALL:
1263 p_slave->i_priority = 3;
1264 break;
1265 case SLAVE_PRIORITY_USER:
1266 p_slave->i_priority = 4;
1267 break;
1268 default:
1269 vlc_assert_unreachable();
1271 pp_slaves[i] = p_slave;
1273 vlc_mutex_unlock( &p_input_item->lock );
1275 *ppp_slaves = pp_slaves;
1276 return i_count;
1279 void libvlc_media_slaves_release( libvlc_media_slave_t **pp_slaves,
1280 unsigned int i_count )
1282 if( i_count > 0 )
1284 assert( pp_slaves );
1285 for( unsigned int i = 0; i < i_count; ++i )
1286 free( pp_slaves[i] );
1288 free( pp_slaves );