packetizer: hevc_nal: retrieve source scan
[vlc.git] / lib / media.c
blob30e5bd55f4f40c28667adc0b050c6ac54306bf71
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 if( !p_md->p_input_item )
702 return false;
704 input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
705 vlc_mutex_lock( &p_itm_stats->lock );
706 p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
707 p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
709 p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
710 p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
711 p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
712 p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
714 p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
715 p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
717 p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
718 p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
720 p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
721 p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
723 p_stats->i_sent_packets = p_itm_stats->i_sent_packets;
724 p_stats->i_sent_bytes = p_itm_stats->i_sent_bytes;
725 p_stats->f_send_bitrate = p_itm_stats->f_send_bitrate;
726 vlc_mutex_unlock( &p_itm_stats->lock );
727 return true;
730 /**************************************************************************
731 * event_manager
732 **************************************************************************/
733 libvlc_event_manager_t *
734 libvlc_media_event_manager( libvlc_media_t * p_md )
736 assert( p_md );
738 return &p_md->event_manager;
741 /**************************************************************************
742 * Get duration of media object (in ms)
743 **************************************************************************/
744 int64_t
745 libvlc_media_get_duration( libvlc_media_t * p_md )
747 assert( p_md );
749 if( !p_md->p_input_item )
751 libvlc_printerr( "No input item" );
752 return -1;
755 if (!input_item_IsPreparsed( p_md->p_input_item ))
756 return -1;
758 return from_mtime(input_item_GetDuration( p_md->p_input_item ));
761 static int media_parse(libvlc_media_t *media, bool b_async,
762 libvlc_media_parse_flag_t parse_flag, int timeout)
764 bool needed;
766 vlc_mutex_lock(&media->parsed_lock);
767 needed = !media->has_asked_preparse;
768 media->has_asked_preparse = true;
769 if (needed)
770 media->is_parsed = false;
771 vlc_mutex_unlock(&media->parsed_lock);
773 if (needed)
775 libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
776 input_item_t *item = media->p_input_item;
777 input_item_meta_request_option_t art_scope = META_REQUEST_OPTION_NONE;
778 input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
779 int ret;
781 if (parse_flag & libvlc_media_fetch_local)
782 art_scope |= META_REQUEST_OPTION_SCOPE_LOCAL;
783 if (parse_flag & libvlc_media_fetch_network)
784 art_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
785 if (art_scope != META_REQUEST_OPTION_NONE) {
786 ret = libvlc_ArtRequest(libvlc, item, art_scope);
787 if (ret != VLC_SUCCESS)
788 return ret;
791 if (parse_flag & libvlc_media_parse_network)
792 parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
793 if (parse_flag & libvlc_media_do_interact)
794 parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
795 ret = libvlc_MetadataRequest(libvlc, item, parse_scope, timeout, media);
796 if (ret != VLC_SUCCESS)
797 return ret;
799 else
800 return VLC_EGENERIC;
802 if (!b_async)
804 vlc_mutex_lock(&media->parsed_lock);
805 while (!media->is_parsed)
806 vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
807 vlc_mutex_unlock(&media->parsed_lock);
809 return VLC_SUCCESS;
812 /**************************************************************************
813 * Parse the media and wait.
814 **************************************************************************/
815 void
816 libvlc_media_parse(libvlc_media_t *media)
818 media_parse( media, false, libvlc_media_fetch_local, -1 );
821 /**************************************************************************
822 * Parse the media but do not wait.
823 **************************************************************************/
824 void
825 libvlc_media_parse_async(libvlc_media_t *media)
827 media_parse( media, true, libvlc_media_fetch_local, -1 );
830 /**************************************************************************
831 * Parse the media asynchronously with options.
832 **************************************************************************/
834 libvlc_media_parse_with_options( libvlc_media_t *media,
835 libvlc_media_parse_flag_t parse_flag,
836 int timeout )
838 return media_parse( media, true, parse_flag, timeout ) == VLC_SUCCESS ? 0 : -1;
841 void
842 libvlc_media_parse_stop( libvlc_media_t *media )
844 libvlc_MetadataCancel( media->p_libvlc_instance->p_libvlc_int, media );
847 /**************************************************************************
848 * Get parsed status for media object.
849 **************************************************************************/
851 libvlc_media_is_parsed(libvlc_media_t *media)
853 bool parsed;
855 vlc_mutex_lock(&media->parsed_lock);
856 parsed = media->is_parsed;
857 vlc_mutex_unlock(&media->parsed_lock);
858 return parsed;
861 libvlc_media_parsed_status_t
862 libvlc_media_get_parsed_status(libvlc_media_t *media)
864 libvlc_media_parsed_status_t status;
866 vlc_mutex_lock(&media->parsed_lock);
867 status = media->parsed_status;
868 vlc_mutex_unlock(&media->parsed_lock);
869 return status;
872 /**************************************************************************
873 * Sets media descriptor's user_data. user_data is specialized data
874 * accessed by the host application, VLC.framework uses it as a pointer to
875 * an native object that references a libvlc_media_t pointer
876 **************************************************************************/
877 void
878 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
880 assert( p_md );
881 p_md->p_user_data = p_new_user_data;
884 /**************************************************************************
885 * Get media descriptor's user_data. user_data is specialized data
886 * accessed by the host application, VLC.framework uses it as a pointer to
887 * an native object that references a libvlc_media_t pointer
888 **************************************************************************/
889 void *
890 libvlc_media_get_user_data( libvlc_media_t * p_md )
892 assert( p_md );
893 return p_md->p_user_data;
896 /**************************************************************************
897 * Get media descriptor's elementary streams description
898 **************************************************************************/
900 libvlc_media_get_tracks_info( libvlc_media_t *p_md, libvlc_media_track_info_t ** pp_es )
902 assert( p_md );
904 input_item_t *p_input_item = p_md->p_input_item;
905 vlc_mutex_lock( &p_input_item->lock );
907 const int i_es = p_input_item->i_es;
908 *pp_es = (i_es > 0) ? malloc( i_es * sizeof(libvlc_media_track_info_t) ) : NULL;
910 if( !*pp_es ) /* no ES, or OOM */
912 vlc_mutex_unlock( &p_input_item->lock );
913 return 0;
916 /* Fill array */
917 for( int i = 0; i < i_es; i++ )
919 libvlc_media_track_info_t *p_mes = *pp_es+i;
920 const es_format_t *p_es = p_input_item->es[i];
922 p_mes->i_codec = p_es->i_codec;
923 p_mes->i_id = p_es->i_id;
925 p_mes->i_profile = p_es->i_profile;
926 p_mes->i_level = p_es->i_level;
928 switch(p_es->i_cat)
930 case UNKNOWN_ES:
931 default:
932 p_mes->i_type = libvlc_track_unknown;
933 break;
934 case VIDEO_ES:
935 p_mes->i_type = libvlc_track_video;
936 p_mes->u.video.i_height = p_es->video.i_visible_height;
937 p_mes->u.video.i_width = p_es->video.i_visible_width;
938 break;
939 case AUDIO_ES:
940 p_mes->i_type = libvlc_track_audio;
941 p_mes->u.audio.i_channels = p_es->audio.i_channels;
942 p_mes->u.audio.i_rate = p_es->audio.i_rate;
943 break;
944 case SPU_ES:
945 p_mes->i_type = libvlc_track_text;
946 break;
950 vlc_mutex_unlock( &p_input_item->lock );
951 return i_es;
954 unsigned
955 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
957 assert( p_md );
959 input_item_t *p_input_item = p_md->p_input_item;
960 vlc_mutex_lock( &p_input_item->lock );
962 const int i_es = p_input_item->i_es;
963 *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
965 if( !*pp_es ) /* no ES, or OOM */
967 vlc_mutex_unlock( &p_input_item->lock );
968 return 0;
971 /* Fill array */
972 for( int i = 0; i < i_es; i++ )
974 libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
975 if ( p_mes )
977 p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
978 sizeof(*p_mes->video)),
979 sizeof(*p_mes->subtitle)) );
981 if ( !p_mes || !p_mes->audio )
983 libvlc_media_tracks_release( *pp_es, i_es );
984 *pp_es = NULL;
985 free( p_mes );
986 vlc_mutex_unlock( &p_input_item->lock );
987 return 0;
989 (*pp_es)[i] = p_mes;
991 const es_format_t *p_es = p_input_item->es[i];
993 p_mes->i_codec = p_es->i_codec;
994 p_mes->i_original_fourcc = p_es->i_original_fourcc;
995 p_mes->i_id = p_es->i_id;
997 p_mes->i_profile = p_es->i_profile;
998 p_mes->i_level = p_es->i_level;
1000 p_mes->i_bitrate = p_es->i_bitrate;
1001 p_mes->psz_language = p_es->psz_language != NULL ? strdup(p_es->psz_language) : NULL;
1002 p_mes->psz_description = p_es->psz_description != NULL ? strdup(p_es->psz_description) : NULL;
1004 switch(p_es->i_cat)
1006 case UNKNOWN_ES:
1007 default:
1008 p_mes->i_type = libvlc_track_unknown;
1009 break;
1010 case VIDEO_ES:
1011 p_mes->i_type = libvlc_track_video;
1012 p_mes->video->i_height = p_es->video.i_visible_height;
1013 p_mes->video->i_width = p_es->video.i_visible_width;
1014 p_mes->video->i_sar_num = p_es->video.i_sar_num;
1015 p_mes->video->i_sar_den = p_es->video.i_sar_den;
1016 p_mes->video->i_frame_rate_num = p_es->video.i_frame_rate;
1017 p_mes->video->i_frame_rate_den = p_es->video.i_frame_rate_base;
1019 assert( p_es->video.orientation >= ORIENT_TOP_LEFT &&
1020 p_es->video.orientation <= ORIENT_RIGHT_BOTTOM );
1021 p_mes->video->i_orientation = (int) p_es->video.orientation;
1023 assert( ( p_es->video.projection_mode >= PROJECTION_MODE_RECTANGULAR &&
1024 p_es->video.projection_mode <= PROJECTION_MODE_EQUIRECTANGULAR ) ||
1025 ( p_es->video.projection_mode == PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD ) );
1026 p_mes->video->i_projection = (int) p_es->video.projection_mode;
1028 p_mes->video->pose.f_yaw = p_es->video.pose.yaw;
1029 p_mes->video->pose.f_pitch = p_es->video.pose.pitch;
1030 p_mes->video->pose.f_roll = p_es->video.pose.roll;
1031 p_mes->video->pose.f_field_of_view = p_es->video.pose.fov;
1032 break;
1033 case AUDIO_ES:
1034 p_mes->i_type = libvlc_track_audio;
1035 p_mes->audio->i_channels = p_es->audio.i_channels;
1036 p_mes->audio->i_rate = p_es->audio.i_rate;
1037 break;
1038 case SPU_ES:
1039 p_mes->i_type = libvlc_track_text;
1040 p_mes->subtitle->psz_encoding = p_es->subs.psz_encoding != NULL ?
1041 strdup(p_es->subs.psz_encoding) : NULL;
1042 break;
1046 vlc_mutex_unlock( &p_input_item->lock );
1047 return i_es;
1050 /**************************************************************************
1051 * Get codec description from media elementary stream
1052 **************************************************************************/
1053 const char *
1054 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
1055 uint32_t i_codec )
1057 switch( i_type )
1059 case libvlc_track_audio:
1060 return vlc_fourcc_GetDescription( AUDIO_ES, i_codec );
1061 case libvlc_track_video:
1062 return vlc_fourcc_GetDescription( VIDEO_ES, i_codec );
1063 case libvlc_track_text:
1064 return vlc_fourcc_GetDescription( SPU_ES, i_codec );
1065 case libvlc_track_unknown:
1066 default:
1067 return vlc_fourcc_GetDescription( UNKNOWN_ES, i_codec );
1071 /**************************************************************************
1072 * Release media descriptor's elementary streams description array
1073 **************************************************************************/
1074 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
1076 for( unsigned i = 0; i < i_count; ++i )
1078 if ( !p_tracks[i] )
1079 continue;
1080 free( p_tracks[i]->psz_language );
1081 free( p_tracks[i]->psz_description );
1082 switch( p_tracks[i]->i_type )
1084 case libvlc_track_audio:
1085 break;
1086 case libvlc_track_video:
1087 break;
1088 case libvlc_track_text:
1089 free( p_tracks[i]->subtitle->psz_encoding );
1090 break;
1091 case libvlc_track_unknown:
1092 default:
1093 break;
1095 free( p_tracks[i]->audio );
1096 free( p_tracks[i] );
1098 free( p_tracks );
1101 /**************************************************************************
1102 * Get the media type of the media descriptor object
1103 **************************************************************************/
1104 libvlc_media_type_t libvlc_media_get_type( libvlc_media_t *p_md )
1106 assert( p_md );
1108 int i_type;
1109 input_item_t *p_input_item = p_md->p_input_item;
1111 vlc_mutex_lock( &p_input_item->lock );
1112 i_type = p_md->p_input_item->i_type;
1113 vlc_mutex_unlock( &p_input_item->lock );
1115 switch( i_type )
1117 case ITEM_TYPE_FILE:
1118 return libvlc_media_type_file;
1119 case ITEM_TYPE_NODE:
1120 case ITEM_TYPE_DIRECTORY:
1121 return libvlc_media_type_directory;
1122 case ITEM_TYPE_DISC:
1123 return libvlc_media_type_disc;
1124 case ITEM_TYPE_STREAM:
1125 return libvlc_media_type_stream;
1126 case ITEM_TYPE_PLAYLIST:
1127 return libvlc_media_type_playlist;
1128 default:
1129 return libvlc_media_type_unknown;
1133 int libvlc_media_slaves_add( libvlc_media_t *p_md,
1134 libvlc_media_slave_type_t i_type,
1135 unsigned int i_priority,
1136 const char *psz_uri )
1138 assert( p_md && psz_uri );
1139 input_item_t *p_input_item = p_md->p_input_item;
1141 enum slave_type i_input_slave_type;
1142 switch( i_type )
1144 case libvlc_media_slave_type_subtitle:
1145 i_input_slave_type = SLAVE_TYPE_SPU;
1146 break;
1147 case libvlc_media_slave_type_audio:
1148 i_input_slave_type = SLAVE_TYPE_AUDIO;
1149 break;
1150 default:
1151 vlc_assert_unreachable();
1152 return -1;
1155 enum slave_priority i_input_slave_priority;
1156 switch( i_priority )
1158 case 0:
1159 i_input_slave_priority = SLAVE_PRIORITY_MATCH_NONE;
1160 break;
1161 case 1:
1162 i_input_slave_priority = SLAVE_PRIORITY_MATCH_RIGHT;
1163 break;
1164 case 2:
1165 i_input_slave_priority = SLAVE_PRIORITY_MATCH_LEFT;
1166 break;
1167 case 3:
1168 i_input_slave_priority = SLAVE_PRIORITY_MATCH_ALL;
1169 break;
1170 default:
1171 case 4:
1172 i_input_slave_priority = SLAVE_PRIORITY_USER;
1173 break;
1176 input_item_slave_t *p_slave = input_item_slave_New( psz_uri,
1177 i_input_slave_type,
1178 i_input_slave_priority );
1179 if( p_slave == NULL )
1180 return -1;
1181 return input_item_AddSlave( p_input_item, p_slave ) == VLC_SUCCESS ? 0 : -1;
1184 void libvlc_media_slaves_clear( libvlc_media_t *p_md )
1186 assert( p_md );
1187 input_item_t *p_input_item = p_md->p_input_item;
1189 vlc_mutex_lock( &p_input_item->lock );
1190 for( int i = 0; i < p_input_item->i_slaves; i++ )
1191 input_item_slave_Delete( p_input_item->pp_slaves[i] );
1192 TAB_CLEAN( p_input_item->i_slaves, p_input_item->pp_slaves );
1193 vlc_mutex_unlock( &p_input_item->lock );
1196 unsigned int libvlc_media_slaves_get( libvlc_media_t *p_md,
1197 libvlc_media_slave_t ***ppp_slaves )
1199 assert( p_md && ppp_slaves );
1200 input_item_t *p_input_item = p_md->p_input_item;
1201 *ppp_slaves = NULL;
1203 vlc_mutex_lock( &p_input_item->lock );
1205 int i_count = p_input_item->i_slaves;
1206 if( i_count <= 0 )
1207 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1209 libvlc_media_slave_t **pp_slaves = calloc( i_count, sizeof(*pp_slaves) );
1210 if( pp_slaves == NULL )
1211 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1213 for( int i = 0; i < i_count; ++i )
1215 input_item_slave_t *p_item_slave = p_input_item->pp_slaves[i];
1216 assert( p_item_slave->i_priority >= SLAVE_PRIORITY_MATCH_NONE );
1218 /* also allocate psz_uri buffer at the end of the struct */
1219 libvlc_media_slave_t *p_slave = malloc( sizeof(*p_slave) +
1220 strlen( p_item_slave->psz_uri )
1221 + 1 );
1222 if( p_slave == NULL )
1224 libvlc_media_slaves_release(pp_slaves, i);
1225 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1227 p_slave->psz_uri = (char *) ((uint8_t *)p_slave) + sizeof(*p_slave);
1228 strcpy( p_slave->psz_uri, p_item_slave->psz_uri );
1230 switch( p_item_slave->i_type )
1232 case SLAVE_TYPE_SPU:
1233 p_slave->i_type = libvlc_media_slave_type_subtitle;
1234 break;
1235 case SLAVE_TYPE_AUDIO:
1236 p_slave->i_type = libvlc_media_slave_type_audio;
1237 break;
1238 default:
1239 vlc_assert_unreachable();
1242 switch( p_item_slave->i_priority )
1244 case SLAVE_PRIORITY_MATCH_NONE:
1245 p_slave->i_priority = 0;
1246 break;
1247 case SLAVE_PRIORITY_MATCH_RIGHT:
1248 p_slave->i_priority = 1;
1249 break;
1250 case SLAVE_PRIORITY_MATCH_LEFT:
1251 p_slave->i_priority = 2;
1252 break;
1253 case SLAVE_PRIORITY_MATCH_ALL:
1254 p_slave->i_priority = 3;
1255 break;
1256 case SLAVE_PRIORITY_USER:
1257 p_slave->i_priority = 4;
1258 break;
1259 default:
1260 vlc_assert_unreachable();
1262 pp_slaves[i] = p_slave;
1264 vlc_mutex_unlock( &p_input_item->lock );
1266 *ppp_slaves = pp_slaves;
1267 return i_count;
1270 void libvlc_media_slaves_release( libvlc_media_slave_t **pp_slaves,
1271 unsigned int i_count )
1273 if( i_count > 0 )
1275 assert( pp_slaves );
1276 for( unsigned int i = 0; i < i_count; ++i )
1277 free( pp_slaves[i] );
1279 free( pp_slaves );