mediacodec: also set quirks from MediaCodec_GetName()
[vlc.git] / lib / media.c
blobf045c729d24917aab21bddeb5cb5bfe8fdebf468
1 /*****************************************************************************
2 * media.c: Libvlc API media descripor management
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
6 * Authors: Pierre d'Herbemont <pdherbemont@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <assert.h>
28 #include <errno.h>
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_picture.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_meta.h>
38 #include <vlc_url.h>
39 #include <vlc_thumbnailer.h>
41 #include "../src/libvlc.h"
43 #include "libvlc_internal.h"
44 #include "media_internal.h"
45 #include "media_list_internal.h"
46 #include "picture_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_done )
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 bool 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 vlc_mutex_unlock( &item->lock );
734 return true;
737 /**************************************************************************
738 * event_manager
739 **************************************************************************/
740 libvlc_event_manager_t *
741 libvlc_media_event_manager( libvlc_media_t * p_md )
743 assert( p_md );
745 return &p_md->event_manager;
748 /**************************************************************************
749 * Get duration of media object (in ms)
750 **************************************************************************/
751 int64_t
752 libvlc_media_get_duration( libvlc_media_t * p_md )
754 assert( p_md );
756 if( !p_md->p_input_item )
758 libvlc_printerr( "No input item" );
759 return -1;
762 if (!input_item_IsPreparsed( p_md->p_input_item ))
763 return -1;
765 return from_mtime(input_item_GetDuration( p_md->p_input_item ));
768 static const input_preparser_callbacks_t input_preparser_callbacks = {
769 .on_preparse_ended = input_item_preparse_ended,
770 .on_subtree_added = input_item_subtree_added,
773 static int media_parse(libvlc_media_t *media, bool b_async,
774 libvlc_media_parse_flag_t parse_flag, int timeout)
776 bool needed;
778 vlc_mutex_lock(&media->parsed_lock);
779 needed = !media->has_asked_preparse;
780 media->has_asked_preparse = true;
781 if (needed)
783 media->is_parsed = false;
784 media->parsed_status = 0;
786 vlc_mutex_unlock(&media->parsed_lock);
788 if (needed)
790 libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
791 input_item_t *item = media->p_input_item;
792 input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
793 int ret;
795 if (parse_flag & libvlc_media_parse_network)
796 parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
797 if (parse_flag & libvlc_media_fetch_local)
798 parse_scope |= META_REQUEST_OPTION_FETCH_LOCAL;
799 if (parse_flag & libvlc_media_fetch_network)
800 parse_scope |= META_REQUEST_OPTION_FETCH_NETWORK;
801 if (parse_flag & libvlc_media_do_interact)
802 parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
804 ret = libvlc_MetadataRequest(libvlc, item, parse_scope,
805 &input_preparser_callbacks, media,
806 timeout, media);
807 if (ret != VLC_SUCCESS)
808 return ret;
810 else
811 return VLC_EGENERIC;
813 if (!b_async)
815 vlc_mutex_lock(&media->parsed_lock);
816 while (!media->is_parsed)
817 vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
818 vlc_mutex_unlock(&media->parsed_lock);
820 return VLC_SUCCESS;
823 /**************************************************************************
824 * Parse the media and wait.
825 **************************************************************************/
826 void
827 libvlc_media_parse(libvlc_media_t *media)
829 media_parse( media, false, libvlc_media_fetch_local, -1 );
832 /**************************************************************************
833 * Parse the media but do not wait.
834 **************************************************************************/
835 void
836 libvlc_media_parse_async(libvlc_media_t *media)
838 media_parse( media, true, libvlc_media_fetch_local, -1 );
841 /**************************************************************************
842 * Parse the media asynchronously with options.
843 **************************************************************************/
845 libvlc_media_parse_with_options( libvlc_media_t *media,
846 libvlc_media_parse_flag_t parse_flag,
847 int timeout )
849 return media_parse( media, true, parse_flag, timeout ) == VLC_SUCCESS ? 0 : -1;
852 void
853 libvlc_media_parse_stop( libvlc_media_t *media )
855 libvlc_MetadataCancel( media->p_libvlc_instance->p_libvlc_int, media );
858 /**************************************************************************
859 * Get parsed status for media object.
860 **************************************************************************/
861 bool libvlc_media_is_parsed(libvlc_media_t *media)
863 bool parsed;
865 vlc_mutex_lock(&media->parsed_lock);
866 parsed = media->is_parsed;
867 vlc_mutex_unlock(&media->parsed_lock);
868 return parsed;
871 libvlc_media_parsed_status_t
872 libvlc_media_get_parsed_status(libvlc_media_t *media)
874 libvlc_media_parsed_status_t status;
876 vlc_mutex_lock(&media->parsed_lock);
877 status = media->parsed_status;
878 vlc_mutex_unlock(&media->parsed_lock);
879 return status;
882 /**************************************************************************
883 * Sets media descriptor's user_data. user_data is specialized data
884 * accessed by the host application, VLC.framework uses it as a pointer to
885 * an native object that references a libvlc_media_t pointer
886 **************************************************************************/
887 void
888 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
890 assert( p_md );
891 p_md->p_user_data = p_new_user_data;
894 /**************************************************************************
895 * Get media descriptor's user_data. user_data is specialized data
896 * accessed by the host application, VLC.framework uses it as a pointer to
897 * an native object that references a libvlc_media_t pointer
898 **************************************************************************/
899 void *
900 libvlc_media_get_user_data( libvlc_media_t * p_md )
902 assert( p_md );
903 return p_md->p_user_data;
906 unsigned
907 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
909 assert( p_md );
911 input_item_t *p_input_item = p_md->p_input_item;
912 vlc_mutex_lock( &p_input_item->lock );
914 const int i_es = p_input_item->i_es;
915 *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
917 if( !*pp_es ) /* no ES, or OOM */
919 vlc_mutex_unlock( &p_input_item->lock );
920 return 0;
923 /* Fill array */
924 for( int i = 0; i < i_es; i++ )
926 libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
927 if ( p_mes )
929 p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
930 sizeof(*p_mes->video)),
931 sizeof(*p_mes->subtitle)) );
933 if ( !p_mes || !p_mes->audio )
935 libvlc_media_tracks_release( *pp_es, i_es );
936 *pp_es = NULL;
937 free( p_mes );
938 vlc_mutex_unlock( &p_input_item->lock );
939 return 0;
941 (*pp_es)[i] = p_mes;
943 const es_format_t *p_es = p_input_item->es[i];
945 p_mes->i_codec = p_es->i_codec;
946 p_mes->i_original_fourcc = p_es->i_original_fourcc;
947 p_mes->i_id = p_es->i_id;
949 p_mes->i_profile = p_es->i_profile;
950 p_mes->i_level = p_es->i_level;
952 p_mes->i_bitrate = p_es->i_bitrate;
953 p_mes->psz_language = p_es->psz_language != NULL ? strdup(p_es->psz_language) : NULL;
954 p_mes->psz_description = p_es->psz_description != NULL ? strdup(p_es->psz_description) : NULL;
956 switch(p_es->i_cat)
958 case UNKNOWN_ES:
959 default:
960 p_mes->i_type = libvlc_track_unknown;
961 break;
962 case VIDEO_ES:
963 p_mes->i_type = libvlc_track_video;
964 p_mes->video->i_height = p_es->video.i_visible_height;
965 p_mes->video->i_width = p_es->video.i_visible_width;
966 p_mes->video->i_sar_num = p_es->video.i_sar_num;
967 p_mes->video->i_sar_den = p_es->video.i_sar_den;
968 p_mes->video->i_frame_rate_num = p_es->video.i_frame_rate;
969 p_mes->video->i_frame_rate_den = p_es->video.i_frame_rate_base;
971 assert( p_es->video.orientation >= ORIENT_TOP_LEFT &&
972 p_es->video.orientation <= ORIENT_RIGHT_BOTTOM );
973 p_mes->video->i_orientation = (int) p_es->video.orientation;
975 assert( ( p_es->video.projection_mode >= PROJECTION_MODE_RECTANGULAR &&
976 p_es->video.projection_mode <= PROJECTION_MODE_EQUIRECTANGULAR ) ||
977 ( p_es->video.projection_mode == PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD ) );
978 p_mes->video->i_projection = (int) p_es->video.projection_mode;
980 p_mes->video->pose.f_yaw = p_es->video.pose.yaw;
981 p_mes->video->pose.f_pitch = p_es->video.pose.pitch;
982 p_mes->video->pose.f_roll = p_es->video.pose.roll;
983 p_mes->video->pose.f_field_of_view = p_es->video.pose.fov;
985 assert( p_es->video.multiview_mode >= MULTIVIEW_2D &&
986 p_es->video.multiview_mode <= MULTIVIEW_STEREO_CHECKERBOARD );
987 p_mes->video->i_multiview = (int) p_es->video.multiview_mode;
988 break;
989 case AUDIO_ES:
990 p_mes->i_type = libvlc_track_audio;
991 p_mes->audio->i_channels = p_es->audio.i_channels;
992 p_mes->audio->i_rate = p_es->audio.i_rate;
993 break;
994 case SPU_ES:
995 p_mes->i_type = libvlc_track_text;
996 p_mes->subtitle->psz_encoding = p_es->subs.psz_encoding != NULL ?
997 strdup(p_es->subs.psz_encoding) : NULL;
998 break;
1002 vlc_mutex_unlock( &p_input_item->lock );
1003 return i_es;
1006 /**************************************************************************
1007 * Get codec description from media elementary stream
1008 **************************************************************************/
1009 const char *
1010 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
1011 uint32_t i_codec )
1013 switch( i_type )
1015 case libvlc_track_audio:
1016 return vlc_fourcc_GetDescription( AUDIO_ES, i_codec );
1017 case libvlc_track_video:
1018 return vlc_fourcc_GetDescription( VIDEO_ES, i_codec );
1019 case libvlc_track_text:
1020 return vlc_fourcc_GetDescription( SPU_ES, i_codec );
1021 case libvlc_track_unknown:
1022 default:
1023 return vlc_fourcc_GetDescription( UNKNOWN_ES, i_codec );
1027 /**************************************************************************
1028 * Release media descriptor's elementary streams description array
1029 **************************************************************************/
1030 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
1032 for( unsigned i = 0; i < i_count; ++i )
1034 if ( !p_tracks[i] )
1035 continue;
1036 free( p_tracks[i]->psz_language );
1037 free( p_tracks[i]->psz_description );
1038 switch( p_tracks[i]->i_type )
1040 case libvlc_track_audio:
1041 break;
1042 case libvlc_track_video:
1043 break;
1044 case libvlc_track_text:
1045 free( p_tracks[i]->subtitle->psz_encoding );
1046 break;
1047 case libvlc_track_unknown:
1048 default:
1049 break;
1051 free( p_tracks[i]->audio );
1052 free( p_tracks[i] );
1054 free( p_tracks );
1057 /**************************************************************************
1058 * Get the media type of the media descriptor object
1059 **************************************************************************/
1060 libvlc_media_type_t libvlc_media_get_type( libvlc_media_t *p_md )
1062 assert( p_md );
1064 enum input_item_type_e i_type;
1065 input_item_t *p_input_item = p_md->p_input_item;
1067 vlc_mutex_lock( &p_input_item->lock );
1068 i_type = p_md->p_input_item->i_type;
1069 vlc_mutex_unlock( &p_input_item->lock );
1071 switch( i_type )
1073 case ITEM_TYPE_FILE:
1074 return libvlc_media_type_file;
1075 case ITEM_TYPE_NODE:
1076 case ITEM_TYPE_DIRECTORY:
1077 return libvlc_media_type_directory;
1078 case ITEM_TYPE_DISC:
1079 return libvlc_media_type_disc;
1080 case ITEM_TYPE_STREAM:
1081 return libvlc_media_type_stream;
1082 case ITEM_TYPE_PLAYLIST:
1083 return libvlc_media_type_playlist;
1084 default:
1085 return libvlc_media_type_unknown;
1089 struct libvlc_media_thumbnail_request_t
1091 libvlc_media_t *md;
1092 unsigned int width;
1093 unsigned int height;
1094 bool crop;
1095 libvlc_picture_type_t type;
1096 vlc_thumbnailer_request_t* req;
1099 static void media_on_thumbnail_ready( void* data, picture_t* thumbnail )
1101 libvlc_media_thumbnail_request_t *req = data;
1102 libvlc_media_t *p_media = req->md;
1103 libvlc_event_t event;
1104 event.type = libvlc_MediaThumbnailGenerated;
1105 libvlc_picture_t* pic = NULL;
1106 if ( thumbnail != NULL )
1107 pic = libvlc_picture_new( VLC_OBJECT(p_media->p_libvlc_instance->p_libvlc_int),
1108 thumbnail, req->type, req->width, req->height,
1109 req->crop );
1110 event.u.media_thumbnail_generated.p_thumbnail = pic;
1111 libvlc_event_send( &p_media->event_manager, &event );
1112 if ( pic != NULL )
1113 libvlc_picture_release( pic );
1116 libvlc_media_thumbnail_request_t*
1117 libvlc_media_thumbnail_request_by_time( libvlc_media_t *md, libvlc_time_t time,
1118 libvlc_thumbnailer_seek_speed_t speed,
1119 unsigned int width, unsigned int height,
1120 bool crop, libvlc_picture_type_t picture_type,
1121 libvlc_time_t timeout )
1123 assert( md );
1124 libvlc_priv_t *p_priv = libvlc_priv(md->p_libvlc_instance->p_libvlc_int);
1125 if( unlikely( p_priv->p_thumbnailer == NULL ) )
1126 return NULL;
1127 libvlc_media_thumbnail_request_t *req = malloc( sizeof( *req ) );
1128 if ( unlikely( req == NULL ) )
1129 return NULL;
1131 req->md = md;
1132 req->width = width;
1133 req->height = height;
1134 req->type = picture_type;
1135 req->crop = crop;
1136 libvlc_media_retain( md );
1137 req->req = vlc_thumbnailer_RequestByTime( p_priv->p_thumbnailer,
1138 VLC_TICK_FROM_MS( time ),
1139 speed == libvlc_media_thumbnail_seek_fast ?
1140 VLC_THUMBNAILER_SEEK_FAST : VLC_THUMBNAILER_SEEK_PRECISE,
1141 md->p_input_item,
1142 timeout > 0 ? VLC_TICK_FROM_MS( timeout ) : VLC_TICK_INVALID,
1143 media_on_thumbnail_ready, req );
1144 if ( req->req == NULL )
1146 free( req );
1147 libvlc_media_release( md );
1148 return NULL;
1150 return req;
1153 libvlc_media_thumbnail_request_t*
1154 libvlc_media_thumbnail_request_by_pos( libvlc_media_t *md, float pos,
1155 libvlc_thumbnailer_seek_speed_t speed,
1156 unsigned int width, unsigned int height,
1157 bool crop, libvlc_picture_type_t picture_type,
1158 libvlc_time_t timeout )
1160 assert( md );
1161 libvlc_priv_t *priv = libvlc_priv(md->p_libvlc_instance->p_libvlc_int);
1162 if( unlikely( priv->p_thumbnailer == NULL ) )
1163 return NULL;
1164 libvlc_media_thumbnail_request_t *req = malloc( sizeof( *req ) );
1165 if ( unlikely( req == NULL ) )
1166 return NULL;
1168 req->md = md;
1169 req->width = width;
1170 req->height = height;
1171 req->crop = crop;
1172 req->type = picture_type;
1173 libvlc_media_retain( md );
1174 req->req = vlc_thumbnailer_RequestByPos( priv->p_thumbnailer, pos,
1175 speed == libvlc_media_thumbnail_seek_fast ?
1176 VLC_THUMBNAILER_SEEK_FAST : VLC_THUMBNAILER_SEEK_PRECISE,
1177 md->p_input_item,
1178 timeout > 0 ? VLC_TICK_FROM_MS( timeout ) : VLC_TICK_INVALID,
1179 media_on_thumbnail_ready, req );
1180 if ( req->req == NULL )
1182 free( req );
1183 libvlc_media_release( md );
1184 return NULL;
1186 return req;
1189 void libvlc_media_thumbnail_request_cancel( libvlc_media_thumbnail_request_t *req )
1191 libvlc_priv_t *p_priv = libvlc_priv(req->md->p_libvlc_instance->p_libvlc_int);
1192 assert( p_priv->p_thumbnailer != NULL );
1193 vlc_thumbnailer_Cancel( p_priv->p_thumbnailer, req->req );
1196 void libvlc_media_thumbnail_request_destroy( libvlc_media_thumbnail_request_t *req )
1198 libvlc_media_release( req->md );
1199 free( req );
1202 int libvlc_media_slaves_add( libvlc_media_t *p_md,
1203 libvlc_media_slave_type_t i_type,
1204 unsigned int i_priority,
1205 const char *psz_uri )
1207 assert( p_md && psz_uri );
1208 input_item_t *p_input_item = p_md->p_input_item;
1210 enum slave_type i_input_slave_type;
1211 switch( i_type )
1213 case libvlc_media_slave_type_subtitle:
1214 i_input_slave_type = SLAVE_TYPE_SPU;
1215 break;
1216 case libvlc_media_slave_type_audio:
1217 i_input_slave_type = SLAVE_TYPE_AUDIO;
1218 break;
1219 default:
1220 vlc_assert_unreachable();
1221 return -1;
1224 enum slave_priority i_input_slave_priority;
1225 switch( i_priority )
1227 case 0:
1228 i_input_slave_priority = SLAVE_PRIORITY_MATCH_NONE;
1229 break;
1230 case 1:
1231 i_input_slave_priority = SLAVE_PRIORITY_MATCH_RIGHT;
1232 break;
1233 case 2:
1234 i_input_slave_priority = SLAVE_PRIORITY_MATCH_LEFT;
1235 break;
1236 case 3:
1237 i_input_slave_priority = SLAVE_PRIORITY_MATCH_ALL;
1238 break;
1239 default:
1240 case 4:
1241 i_input_slave_priority = SLAVE_PRIORITY_USER;
1242 break;
1245 input_item_slave_t *p_slave = input_item_slave_New( psz_uri,
1246 i_input_slave_type,
1247 i_input_slave_priority );
1248 if( p_slave == NULL )
1249 return -1;
1250 return input_item_AddSlave( p_input_item, p_slave ) == VLC_SUCCESS ? 0 : -1;
1253 void libvlc_media_slaves_clear( libvlc_media_t *p_md )
1255 assert( p_md );
1256 input_item_t *p_input_item = p_md->p_input_item;
1258 vlc_mutex_lock( &p_input_item->lock );
1259 for( int i = 0; i < p_input_item->i_slaves; i++ )
1260 input_item_slave_Delete( p_input_item->pp_slaves[i] );
1261 TAB_CLEAN( p_input_item->i_slaves, p_input_item->pp_slaves );
1262 vlc_mutex_unlock( &p_input_item->lock );
1265 unsigned int libvlc_media_slaves_get( libvlc_media_t *p_md,
1266 libvlc_media_slave_t ***ppp_slaves )
1268 assert( p_md && ppp_slaves );
1269 input_item_t *p_input_item = p_md->p_input_item;
1270 *ppp_slaves = NULL;
1272 vlc_mutex_lock( &p_input_item->lock );
1274 int i_count = p_input_item->i_slaves;
1275 if( i_count <= 0 )
1276 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1278 libvlc_media_slave_t **pp_slaves = calloc( i_count, sizeof(*pp_slaves) );
1279 if( pp_slaves == NULL )
1280 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1282 for( int i = 0; i < i_count; ++i )
1284 input_item_slave_t *p_item_slave = p_input_item->pp_slaves[i];
1285 assert( p_item_slave->i_priority >= SLAVE_PRIORITY_MATCH_NONE );
1287 /* also allocate psz_uri buffer at the end of the struct */
1288 libvlc_media_slave_t *p_slave = malloc( sizeof(*p_slave) +
1289 strlen( p_item_slave->psz_uri )
1290 + 1 );
1291 if( p_slave == NULL )
1293 libvlc_media_slaves_release(pp_slaves, i);
1294 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1296 p_slave->psz_uri = (char *) ((uint8_t *)p_slave) + sizeof(*p_slave);
1297 strcpy( p_slave->psz_uri, p_item_slave->psz_uri );
1299 switch( p_item_slave->i_type )
1301 case SLAVE_TYPE_SPU:
1302 p_slave->i_type = libvlc_media_slave_type_subtitle;
1303 break;
1304 case SLAVE_TYPE_AUDIO:
1305 p_slave->i_type = libvlc_media_slave_type_audio;
1306 break;
1307 default:
1308 vlc_assert_unreachable();
1311 switch( p_item_slave->i_priority )
1313 case SLAVE_PRIORITY_MATCH_NONE:
1314 p_slave->i_priority = 0;
1315 break;
1316 case SLAVE_PRIORITY_MATCH_RIGHT:
1317 p_slave->i_priority = 1;
1318 break;
1319 case SLAVE_PRIORITY_MATCH_LEFT:
1320 p_slave->i_priority = 2;
1321 break;
1322 case SLAVE_PRIORITY_MATCH_ALL:
1323 p_slave->i_priority = 3;
1324 break;
1325 case SLAVE_PRIORITY_USER:
1326 p_slave->i_priority = 4;
1327 break;
1328 default:
1329 vlc_assert_unreachable();
1331 pp_slaves[i] = p_slave;
1333 vlc_mutex_unlock( &p_input_item->lock );
1335 *ppp_slaves = pp_slaves;
1336 return i_count;
1339 void libvlc_media_slaves_release( libvlc_media_slave_t **pp_slaves,
1340 unsigned int i_count )
1342 if( i_count > 0 )
1344 assert( pp_slaves );
1345 for( unsigned int i = 0; i < i_count; ++i )
1346 free( pp_slaves[i] );
1348 free( pp_slaves );