vlc_image: constify ImageReadUrl parameter
[vlc.git] / lib / media.c
blob6786f7edcb66d6e736fc3b269cccd764b40cde36
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_picture.h>
33 #include <vlc/libvlc_media.h>
34 #include <vlc/libvlc_media_list.h> // For the subitems, here for convenience
35 #include <vlc/libvlc_events.h>
37 #include <vlc_common.h>
38 #include <vlc_input.h>
39 #include <vlc_meta.h>
40 #include <vlc_playlist_legacy.h> /* For the preparser */
41 #include <vlc_url.h>
42 #include <vlc_thumbnailer.h>
44 #include "../src/libvlc.h"
46 #include "libvlc_internal.h"
47 #include "media_internal.h"
48 #include "media_list_internal.h"
49 #include "picture_internal.h"
51 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
53 [libvlc_meta_Title] = vlc_meta_Title,
54 [libvlc_meta_Artist] = vlc_meta_Artist,
55 [libvlc_meta_Genre] = vlc_meta_Genre,
56 [libvlc_meta_Copyright] = vlc_meta_Copyright,
57 [libvlc_meta_Album] = vlc_meta_Album,
58 [libvlc_meta_TrackNumber] = vlc_meta_TrackNumber,
59 [libvlc_meta_Description] = vlc_meta_Description,
60 [libvlc_meta_Rating] = vlc_meta_Rating,
61 [libvlc_meta_Date] = vlc_meta_Date,
62 [libvlc_meta_Setting] = vlc_meta_Setting,
63 [libvlc_meta_URL] = vlc_meta_URL,
64 [libvlc_meta_Language] = vlc_meta_Language,
65 [libvlc_meta_NowPlaying] = vlc_meta_NowPlaying,
66 [libvlc_meta_Publisher] = vlc_meta_Publisher,
67 [libvlc_meta_EncodedBy] = vlc_meta_EncodedBy,
68 [libvlc_meta_ArtworkURL] = vlc_meta_ArtworkURL,
69 [libvlc_meta_TrackID] = vlc_meta_TrackID,
70 [libvlc_meta_TrackTotal] = vlc_meta_TrackTotal,
71 [libvlc_meta_Director] = vlc_meta_Director,
72 [libvlc_meta_Season] = vlc_meta_Season,
73 [libvlc_meta_Episode] = vlc_meta_Episode,
74 [libvlc_meta_ShowName] = vlc_meta_ShowName,
75 [libvlc_meta_Actors] = vlc_meta_Actors,
76 [libvlc_meta_AlbumArtist] = vlc_meta_AlbumArtist,
77 [libvlc_meta_DiscNumber] = vlc_meta_DiscNumber,
78 [libvlc_meta_DiscTotal] = vlc_meta_DiscTotal
81 static const libvlc_meta_t vlc_to_libvlc_meta[] =
83 [vlc_meta_Title] = libvlc_meta_Title,
84 [vlc_meta_Artist] = libvlc_meta_Artist,
85 [vlc_meta_Genre] = libvlc_meta_Genre,
86 [vlc_meta_Copyright] = libvlc_meta_Copyright,
87 [vlc_meta_Album] = libvlc_meta_Album,
88 [vlc_meta_TrackNumber] = libvlc_meta_TrackNumber,
89 [vlc_meta_Description] = libvlc_meta_Description,
90 [vlc_meta_Rating] = libvlc_meta_Rating,
91 [vlc_meta_Date] = libvlc_meta_Date,
92 [vlc_meta_Setting] = libvlc_meta_Setting,
93 [vlc_meta_URL] = libvlc_meta_URL,
94 [vlc_meta_Language] = libvlc_meta_Language,
95 [vlc_meta_NowPlaying] = libvlc_meta_NowPlaying,
96 [vlc_meta_ESNowPlaying] = libvlc_meta_NowPlaying,
97 [vlc_meta_Publisher] = libvlc_meta_Publisher,
98 [vlc_meta_EncodedBy] = libvlc_meta_EncodedBy,
99 [vlc_meta_ArtworkURL] = libvlc_meta_ArtworkURL,
100 [vlc_meta_TrackID] = libvlc_meta_TrackID,
101 [vlc_meta_TrackTotal] = libvlc_meta_TrackTotal,
102 [vlc_meta_Director] = libvlc_meta_Director,
103 [vlc_meta_Season] = libvlc_meta_Season,
104 [vlc_meta_Episode] = libvlc_meta_Episode,
105 [vlc_meta_ShowName] = libvlc_meta_ShowName,
106 [vlc_meta_Actors] = libvlc_meta_Actors,
107 [vlc_meta_AlbumArtist] = libvlc_meta_AlbumArtist,
108 [vlc_meta_DiscNumber] = libvlc_meta_DiscNumber,
109 [vlc_meta_DiscTotal] = libvlc_meta_DiscTotal
112 static_assert(
113 ORIENT_TOP_LEFT == (int) libvlc_video_orient_top_left &&
114 ORIENT_TOP_RIGHT == (int) libvlc_video_orient_top_right &&
115 ORIENT_BOTTOM_LEFT == (int) libvlc_video_orient_bottom_left &&
116 ORIENT_BOTTOM_RIGHT == (int) libvlc_video_orient_bottom_right &&
117 ORIENT_LEFT_TOP == (int) libvlc_video_orient_left_top &&
118 ORIENT_LEFT_BOTTOM == (int) libvlc_video_orient_left_bottom &&
119 ORIENT_RIGHT_TOP == (int) libvlc_video_orient_right_top &&
120 ORIENT_RIGHT_BOTTOM == (int) libvlc_video_orient_right_bottom,
121 "Mismatch between libvlc_video_orient_t and video_orientation_t" );
123 static_assert(
124 PROJECTION_MODE_RECTANGULAR == (int) libvlc_video_projection_rectangular &&
125 PROJECTION_MODE_EQUIRECTANGULAR == (int) libvlc_video_projection_equirectangular &&
126 PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD == (int) libvlc_video_projection_cubemap_layout_standard,
127 "Mismatch between libvlc_video_projection_t and video_projection_mode_t" );
129 static_assert(
130 MULTIVIEW_2D == (int) libvlc_video_multiview_2d &&
131 MULTIVIEW_STEREO_SBS == (int) libvlc_video_multiview_stereo_sbs &&
132 MULTIVIEW_STEREO_TB == (int) libvlc_video_multiview_stereo_tb &&
133 MULTIVIEW_STEREO_ROW == (int) libvlc_video_multiview_stereo_row &&
134 MULTIVIEW_STEREO_COL == (int) libvlc_video_multiview_stereo_col &&
135 MULTIVIEW_STEREO_FRAME == (int) libvlc_video_multiview_stereo_frame &&
136 MULTIVIEW_STEREO_CHECKERBOARD == (int) libvlc_video_multiview_stereo_checkerboard,
137 "Mismatch between libvlc_video_multiview_t and video_multiview_mode_t");
139 static libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md,
140 bool b_create )
142 libvlc_media_list_t *p_subitems = NULL;
144 vlc_mutex_lock( &p_md->subitems_lock );
145 if( p_md->p_subitems == NULL && b_create )
147 p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance );
148 if( p_md->p_subitems != NULL )
150 p_md->p_subitems->b_read_only = true;
151 p_md->p_subitems->p_internal_md = p_md;
154 p_subitems = p_md->p_subitems;
155 vlc_mutex_unlock( &p_md->subitems_lock );
156 return p_subitems;
159 static libvlc_media_t *input_item_add_subitem( libvlc_media_t *p_md,
160 input_item_t *item )
162 libvlc_media_t * p_md_child;
163 libvlc_media_list_t *p_subitems;
164 libvlc_event_t event;
166 p_md_child = libvlc_media_new_from_input_item( p_md->p_libvlc_instance,
167 item );
169 /* Add this to our media list */
170 p_subitems = media_get_subitems( p_md, true );
171 if( p_subitems != NULL )
173 libvlc_media_list_lock( p_subitems );
174 libvlc_media_list_internal_add_media( p_subitems, p_md_child );
175 libvlc_media_list_unlock( p_subitems );
178 /* Construct the event */
179 event.type = libvlc_MediaSubItemAdded;
180 event.u.media_subitem_added.new_child = p_md_child;
182 /* Send the event */
183 libvlc_event_send( &p_md->event_manager, &event );
184 return p_md_child;
187 static void input_item_add_subnode( libvlc_media_t *md,
188 input_item_node_t *node )
190 for( int i = 0; i < node->i_children; i++ )
192 input_item_node_t *child = node->pp_children[i];
193 libvlc_media_t *md_child = input_item_add_subitem( md, child->p_item );
195 if( md_child != NULL )
197 input_item_add_subnode( md_child, child );
198 libvlc_media_release( md_child );
203 /**************************************************************************
204 * input_item_subitemtree_added (Private) (vlc event Callback)
205 **************************************************************************/
206 static void input_item_subtree_added(input_item_t *item,
207 input_item_node_t *node,
208 void *user_data)
210 VLC_UNUSED(item);
211 libvlc_media_t * p_md = user_data;
212 libvlc_media_add_subtree(p_md, node);
215 void libvlc_media_add_subtree(libvlc_media_t *p_md, input_item_node_t *node)
217 /* FIXME FIXME FIXME
218 * Recursive function calls seem much simpler for this. But playlists are
219 * untrusted and can be arbitrarily deep (e.g. with XSPF). So recursion can
220 * potentially lead to plain old stack overflow. */
221 input_item_add_subnode( p_md, node );
223 /* Construct the event */
224 libvlc_event_t event;
225 event.type = libvlc_MediaSubItemTreeAdded;
226 event.u.media_subitemtree_added.item = p_md;
228 /* Send the event */
229 libvlc_event_send( &p_md->event_manager, &event );
232 /**************************************************************************
233 * input_item_meta_changed (Private) (vlc event Callback)
234 **************************************************************************/
235 static void input_item_meta_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_MediaMetaChanged;
243 event.u.media_meta_changed.meta_type =
244 vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
246 /* Send the event */
247 libvlc_event_send( &p_md->event_manager, &event );
250 /**************************************************************************
251 * input_item_duration_changed (Private) (vlc event Callback)
252 **************************************************************************/
253 static void input_item_duration_changed( const vlc_event_t *p_event,
254 void * user_data )
256 libvlc_media_t * p_md = user_data;
257 libvlc_event_t event;
259 /* Construct the event */
260 event.type = libvlc_MediaDurationChanged;
261 event.u.media_duration_changed.new_duration =
262 from_mtime(p_event->u.input_item_duration_changed.new_duration);
264 /* Send the event */
265 libvlc_event_send( &p_md->event_manager, &event );
268 static void send_parsed_changed( libvlc_media_t *p_md,
269 libvlc_media_parsed_status_t new_status )
271 libvlc_event_t event;
273 vlc_mutex_lock( &p_md->parsed_lock );
274 if( p_md->parsed_status == new_status )
276 vlc_mutex_unlock( &p_md->parsed_lock );
277 return;
280 /* Legacy: notify libvlc_media_parse */
281 if( !p_md->is_parsed )
283 p_md->is_parsed = true;
284 vlc_cond_broadcast( &p_md->parsed_cond );
287 p_md->parsed_status = new_status;
288 if( p_md->parsed_status == libvlc_media_parsed_status_skipped )
289 p_md->has_asked_preparse = false;
291 vlc_mutex_unlock( &p_md->parsed_lock );
293 if( new_status == libvlc_media_parsed_status_done )
295 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, false );
296 if( p_subitems != NULL )
298 /* notify the media list */
299 libvlc_media_list_lock( p_subitems );
300 libvlc_media_list_internal_end_reached( p_subitems );
301 libvlc_media_list_unlock( p_subitems );
305 /* Construct the event */
306 event.type = libvlc_MediaParsedChanged;
307 event.u.media_parsed_changed.new_status = new_status;
309 /* Send the event */
310 libvlc_event_send( &p_md->event_manager, &event );
313 /**************************************************************************
314 * input_item_preparse_ended (Private) (vlc event Callback)
315 **************************************************************************/
316 static void input_item_preparse_ended(input_item_t *item,
317 enum input_item_preparse_status status,
318 void *user_data)
320 VLC_UNUSED(item);
321 libvlc_media_t * p_md = user_data;
322 libvlc_media_parsed_status_t new_status;
324 switch( status )
326 case ITEM_PREPARSE_SKIPPED:
327 new_status = libvlc_media_parsed_status_skipped;
328 break;
329 case ITEM_PREPARSE_FAILED:
330 new_status = libvlc_media_parsed_status_failed;
331 break;
332 case ITEM_PREPARSE_TIMEOUT:
333 new_status = libvlc_media_parsed_status_timeout;
334 break;
335 case ITEM_PREPARSE_DONE:
336 new_status = libvlc_media_parsed_status_done;
337 break;
338 default:
339 return;
341 send_parsed_changed( p_md, new_status );
344 /**************************************************************************
345 * Install event handler (Private)
346 **************************************************************************/
347 static void install_input_item_observer( libvlc_media_t *p_md )
349 vlc_event_attach( &p_md->p_input_item->event_manager,
350 vlc_InputItemMetaChanged,
351 input_item_meta_changed,
352 p_md );
353 vlc_event_attach( &p_md->p_input_item->event_manager,
354 vlc_InputItemDurationChanged,
355 input_item_duration_changed,
356 p_md );
359 /**************************************************************************
360 * Uninstall event handler (Private)
361 **************************************************************************/
362 static void uninstall_input_item_observer( libvlc_media_t *p_md )
364 vlc_event_detach( &p_md->p_input_item->event_manager,
365 vlc_InputItemMetaChanged,
366 input_item_meta_changed,
367 p_md );
368 vlc_event_detach( &p_md->p_input_item->event_manager,
369 vlc_InputItemDurationChanged,
370 input_item_duration_changed,
371 p_md );
374 /**************************************************************************
375 * Create a new media descriptor object from an input_item
376 * (libvlc internal)
377 * That's the generic constructor
378 **************************************************************************/
379 libvlc_media_t * libvlc_media_new_from_input_item(
380 libvlc_instance_t *p_instance,
381 input_item_t *p_input_item )
383 libvlc_media_t * p_md;
385 if (!p_input_item)
387 libvlc_printerr( "No input item given" );
388 return NULL;
391 p_md = calloc( 1, sizeof(libvlc_media_t) );
392 if( !p_md )
394 libvlc_printerr( "Not enough memory" );
395 return NULL;
398 p_md->p_libvlc_instance = p_instance;
399 p_md->p_input_item = p_input_item;
400 p_md->i_refcount = 1;
402 vlc_cond_init(&p_md->parsed_cond);
403 vlc_mutex_init(&p_md->parsed_lock);
404 vlc_mutex_init(&p_md->subitems_lock);
406 p_md->state = libvlc_NothingSpecial;
408 /* A media descriptor can be a playlist. When you open a playlist
409 * It can give a bunch of item to read. */
410 p_md->p_subitems = NULL;
412 libvlc_event_manager_init( &p_md->event_manager, p_md );
414 input_item_Hold( p_md->p_input_item );
416 install_input_item_observer( p_md );
418 libvlc_retain( p_instance );
419 return p_md;
422 /**************************************************************************
423 * Create a new media descriptor object
424 **************************************************************************/
425 libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
426 const char * psz_mrl )
428 input_item_t * p_input_item;
429 libvlc_media_t * p_md;
431 p_input_item = input_item_New( psz_mrl, NULL );
433 if (!p_input_item)
435 libvlc_printerr( "Not enough memory" );
436 return NULL;
439 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
441 /* The p_input_item is retained in libvlc_media_new_from_input_item */
442 input_item_Release( p_input_item );
444 return p_md;
447 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
448 const char *path )
450 char *mrl = vlc_path2uri( path, NULL );
451 if( unlikely(mrl == NULL) )
453 libvlc_printerr( "%s", vlc_strerror_c(errno) );
454 return NULL;
457 libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
458 free( mrl );
459 return m;
462 libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
464 char mrl[16];
465 snprintf( mrl, sizeof(mrl), "fd://%d", fd );
467 return libvlc_media_new_location( p_instance, mrl );
470 libvlc_media_t *libvlc_media_new_callbacks(libvlc_instance_t *p_instance,
471 libvlc_media_open_cb open_cb,
472 libvlc_media_read_cb read_cb,
473 libvlc_media_seek_cb seek_cb,
474 libvlc_media_close_cb close_cb,
475 void *opaque)
477 libvlc_media_t *m = libvlc_media_new_location(p_instance, "imem://");
478 if (unlikely(m == NULL))
479 return NULL;
481 assert(read_cb != NULL);
482 input_item_AddOpaque(m->p_input_item, "imem-data", opaque);
483 input_item_AddOpaque(m->p_input_item, "imem-open", open_cb);
484 input_item_AddOpaque(m->p_input_item, "imem-read", read_cb);
485 input_item_AddOpaque(m->p_input_item, "imem-seek", seek_cb);
486 input_item_AddOpaque(m->p_input_item, "imem-close", close_cb);
487 return m;
490 /**************************************************************************
491 * Create a new media descriptor object
492 **************************************************************************/
493 libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
494 const char * psz_name )
496 input_item_t * p_input_item;
497 libvlc_media_t * p_md;
498 libvlc_media_list_t * p_subitems;
500 p_input_item = input_item_New( INPUT_ITEM_URI_NOP, psz_name );
502 if (!p_input_item)
504 libvlc_printerr( "Not enough memory" );
505 return NULL;
508 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
509 input_item_Release( p_input_item );
511 p_subitems = media_get_subitems( p_md, true );
512 if( p_subitems == NULL) {
513 libvlc_media_release( p_md );
514 return NULL;
517 return p_md;
520 /**************************************************************************
521 * Add an option to the media descriptor,
522 * that will be used to determine how the media_player will read the
523 * media. This allow to use VLC advanced reading/streaming
524 * options in a per-media basis
526 * The options are detailled in vlc --long-help, for instance "--sout-all"
527 **************************************************************************/
528 void libvlc_media_add_option( libvlc_media_t * p_md,
529 const char * psz_option )
531 libvlc_media_add_option_flag( p_md, psz_option,
532 VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
535 /**************************************************************************
536 * Same as libvlc_media_add_option but with configurable flags.
537 **************************************************************************/
538 void libvlc_media_add_option_flag( libvlc_media_t * p_md,
539 const char * ppsz_option,
540 unsigned i_flags )
542 input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
545 /**************************************************************************
546 * Delete a media descriptor object
547 **************************************************************************/
548 void libvlc_media_release( libvlc_media_t *p_md )
550 if (!p_md)
551 return;
553 p_md->i_refcount--;
555 if( p_md->i_refcount > 0 )
556 return;
558 uninstall_input_item_observer( p_md );
560 /* Cancel asynchronous parsing (if any) */
561 libvlc_MetadataCancel( p_md->p_libvlc_instance->p_libvlc_int, p_md );
563 if( p_md->p_subitems )
564 libvlc_media_list_release( p_md->p_subitems );
566 input_item_Release( p_md->p_input_item );
568 vlc_cond_destroy( &p_md->parsed_cond );
569 vlc_mutex_destroy( &p_md->parsed_lock );
570 vlc_mutex_destroy( &p_md->subitems_lock );
572 /* Construct the event */
573 libvlc_event_t event;
574 event.type = libvlc_MediaFreed;
575 event.u.media_freed.md = p_md;
577 /* Send the event */
578 libvlc_event_send( &p_md->event_manager, &event );
580 libvlc_event_manager_destroy( &p_md->event_manager );
581 libvlc_release( p_md->p_libvlc_instance );
582 free( p_md );
585 /**************************************************************************
586 * Retain a media descriptor object
587 **************************************************************************/
588 void libvlc_media_retain( libvlc_media_t *p_md )
590 assert (p_md);
591 p_md->i_refcount++;
594 /**************************************************************************
595 * Duplicate a media descriptor object
596 **************************************************************************/
597 libvlc_media_t *
598 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
600 return libvlc_media_new_from_input_item(
601 p_md_orig->p_libvlc_instance, p_md_orig->p_input_item );
604 /**************************************************************************
605 * Get mrl from a media descriptor object
606 **************************************************************************/
607 char *
608 libvlc_media_get_mrl( libvlc_media_t * p_md )
610 assert( p_md );
611 return input_item_GetURI( p_md->p_input_item );
614 /**************************************************************************
615 * Getter for meta information
616 **************************************************************************/
618 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
620 char *psz_meta = NULL;
622 if( e_meta == libvlc_meta_NowPlaying )
624 psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
626 else
628 psz_meta = input_item_GetMeta( p_md->p_input_item,
629 libvlc_to_vlc_meta[e_meta] );
630 /* Should be integrated in core */
631 if( psz_meta == NULL && e_meta == libvlc_meta_Title
632 && p_md->p_input_item->psz_name != NULL )
633 psz_meta = strdup( p_md->p_input_item->psz_name );
635 return psz_meta;
638 /**************************************************************************
639 * Setter for meta information
640 **************************************************************************/
642 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
644 assert( p_md );
645 input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
648 int libvlc_media_save_meta( libvlc_media_t *p_md )
650 assert( p_md );
651 vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
652 return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
655 /**************************************************************************
656 * Getter for state information
657 * Can be error, playing, buffering, NothingSpecial.
658 **************************************************************************/
660 libvlc_state_t
661 libvlc_media_get_state( libvlc_media_t *p_md )
663 assert( p_md );
664 return p_md->state;
667 /**************************************************************************
668 * Setter for state information (LibVLC Internal)
669 **************************************************************************/
671 void
672 libvlc_media_set_state( libvlc_media_t *p_md,
673 libvlc_state_t state )
675 libvlc_event_t event;
677 p_md->state = state;
679 /* Construct the event */
680 event.type = libvlc_MediaStateChanged;
681 event.u.media_state_changed.new_state = state;
683 /* Send the event */
684 libvlc_event_send( &p_md->event_manager, &event );
687 /**************************************************************************
688 * subitems
689 **************************************************************************/
690 libvlc_media_list_t *
691 libvlc_media_subitems( libvlc_media_t * p_md )
693 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
694 if( p_subitems )
695 libvlc_media_list_retain( p_subitems );
696 return p_subitems;
699 /**************************************************************************
700 * Getter for statistics information
701 **************************************************************************/
702 int libvlc_media_get_stats( libvlc_media_t *p_md,
703 libvlc_media_stats_t *p_stats )
705 input_item_t *item = p_md->p_input_item;
707 if( !p_md->p_input_item )
708 return false;
710 vlc_mutex_lock( &item->lock );
712 input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
713 if( p_itm_stats == NULL )
715 vlc_mutex_unlock( &item->lock );
716 return false;
719 p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
720 p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
722 p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
723 p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
724 p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
725 p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
727 p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
728 p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
730 p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
731 p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
733 p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
734 p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
736 p_stats->i_sent_packets = 0;
737 p_stats->i_sent_bytes = 0;
738 p_stats->f_send_bitrate = 0.;
740 vlc_mutex_unlock( &item->lock );
741 return true;
744 /**************************************************************************
745 * event_manager
746 **************************************************************************/
747 libvlc_event_manager_t *
748 libvlc_media_event_manager( libvlc_media_t * p_md )
750 assert( p_md );
752 return &p_md->event_manager;
755 /**************************************************************************
756 * Get duration of media object (in ms)
757 **************************************************************************/
758 int64_t
759 libvlc_media_get_duration( libvlc_media_t * p_md )
761 assert( p_md );
763 if( !p_md->p_input_item )
765 libvlc_printerr( "No input item" );
766 return -1;
769 if (!input_item_IsPreparsed( p_md->p_input_item ))
770 return -1;
772 return from_mtime(input_item_GetDuration( p_md->p_input_item ));
775 static const input_preparser_callbacks_t input_preparser_callbacks = {
776 .on_preparse_ended = input_item_preparse_ended,
777 .on_subtree_added = input_item_subtree_added,
780 static int media_parse(libvlc_media_t *media, bool b_async,
781 libvlc_media_parse_flag_t parse_flag, int timeout)
783 bool needed;
785 vlc_mutex_lock(&media->parsed_lock);
786 needed = !media->has_asked_preparse;
787 media->has_asked_preparse = true;
788 if (needed)
789 media->is_parsed = false;
790 vlc_mutex_unlock(&media->parsed_lock);
792 if (needed)
794 libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
795 input_item_t *item = media->p_input_item;
796 input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
797 int ret;
799 /* Ignore libvlc_media_fetch_local flag since local art will be fetched
800 * by libvlc_MetadataRequest */
801 if (parse_flag & libvlc_media_fetch_network)
803 ret = libvlc_ArtRequest(libvlc, item,
804 META_REQUEST_OPTION_SCOPE_NETWORK,
805 NULL, NULL);
806 if (ret != VLC_SUCCESS)
807 return ret;
810 if (parse_flag & libvlc_media_parse_network)
811 parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
812 if (parse_flag & libvlc_media_do_interact)
813 parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
814 ret = libvlc_MetadataRequest(libvlc, item, parse_scope, &input_preparser_callbacks, media, timeout, media);
815 if (ret != VLC_SUCCESS)
816 return ret;
818 else
819 return VLC_EGENERIC;
821 if (!b_async)
823 vlc_mutex_lock(&media->parsed_lock);
824 while (!media->is_parsed)
825 vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
826 vlc_mutex_unlock(&media->parsed_lock);
828 return VLC_SUCCESS;
831 /**************************************************************************
832 * Parse the media and wait.
833 **************************************************************************/
834 void
835 libvlc_media_parse(libvlc_media_t *media)
837 media_parse( media, false, libvlc_media_fetch_local, -1 );
840 /**************************************************************************
841 * Parse the media but do not wait.
842 **************************************************************************/
843 void
844 libvlc_media_parse_async(libvlc_media_t *media)
846 media_parse( media, true, libvlc_media_fetch_local, -1 );
849 /**************************************************************************
850 * Parse the media asynchronously with options.
851 **************************************************************************/
853 libvlc_media_parse_with_options( libvlc_media_t *media,
854 libvlc_media_parse_flag_t parse_flag,
855 int timeout )
857 return media_parse( media, true, parse_flag, timeout ) == VLC_SUCCESS ? 0 : -1;
860 void
861 libvlc_media_parse_stop( libvlc_media_t *media )
863 libvlc_MetadataCancel( media->p_libvlc_instance->p_libvlc_int, media );
866 /**************************************************************************
867 * Get parsed status for media object.
868 **************************************************************************/
870 libvlc_media_is_parsed(libvlc_media_t *media)
872 bool parsed;
874 vlc_mutex_lock(&media->parsed_lock);
875 parsed = media->is_parsed;
876 vlc_mutex_unlock(&media->parsed_lock);
877 return parsed;
880 libvlc_media_parsed_status_t
881 libvlc_media_get_parsed_status(libvlc_media_t *media)
883 libvlc_media_parsed_status_t status;
885 vlc_mutex_lock(&media->parsed_lock);
886 status = media->parsed_status;
887 vlc_mutex_unlock(&media->parsed_lock);
888 return status;
891 /**************************************************************************
892 * Sets media descriptor's user_data. user_data is specialized data
893 * accessed by the host application, VLC.framework uses it as a pointer to
894 * an native object that references a libvlc_media_t pointer
895 **************************************************************************/
896 void
897 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
899 assert( p_md );
900 p_md->p_user_data = p_new_user_data;
903 /**************************************************************************
904 * Get media descriptor's user_data. user_data is specialized data
905 * accessed by the host application, VLC.framework uses it as a pointer to
906 * an native object that references a libvlc_media_t pointer
907 **************************************************************************/
908 void *
909 libvlc_media_get_user_data( libvlc_media_t * p_md )
911 assert( p_md );
912 return p_md->p_user_data;
915 unsigned
916 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
918 assert( p_md );
920 input_item_t *p_input_item = p_md->p_input_item;
921 vlc_mutex_lock( &p_input_item->lock );
923 const int i_es = p_input_item->i_es;
924 *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
926 if( !*pp_es ) /* no ES, or OOM */
928 vlc_mutex_unlock( &p_input_item->lock );
929 return 0;
932 /* Fill array */
933 for( int i = 0; i < i_es; i++ )
935 libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
936 if ( p_mes )
938 p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
939 sizeof(*p_mes->video)),
940 sizeof(*p_mes->subtitle)) );
942 if ( !p_mes || !p_mes->audio )
944 libvlc_media_tracks_release( *pp_es, i_es );
945 *pp_es = NULL;
946 free( p_mes );
947 vlc_mutex_unlock( &p_input_item->lock );
948 return 0;
950 (*pp_es)[i] = p_mes;
952 const es_format_t *p_es = p_input_item->es[i];
954 p_mes->i_codec = p_es->i_codec;
955 p_mes->i_original_fourcc = p_es->i_original_fourcc;
956 p_mes->i_id = p_es->i_id;
958 p_mes->i_profile = p_es->i_profile;
959 p_mes->i_level = p_es->i_level;
961 p_mes->i_bitrate = p_es->i_bitrate;
962 p_mes->psz_language = p_es->psz_language != NULL ? strdup(p_es->psz_language) : NULL;
963 p_mes->psz_description = p_es->psz_description != NULL ? strdup(p_es->psz_description) : NULL;
965 switch(p_es->i_cat)
967 case UNKNOWN_ES:
968 default:
969 p_mes->i_type = libvlc_track_unknown;
970 break;
971 case VIDEO_ES:
972 p_mes->i_type = libvlc_track_video;
973 p_mes->video->i_height = p_es->video.i_visible_height;
974 p_mes->video->i_width = p_es->video.i_visible_width;
975 p_mes->video->i_sar_num = p_es->video.i_sar_num;
976 p_mes->video->i_sar_den = p_es->video.i_sar_den;
977 p_mes->video->i_frame_rate_num = p_es->video.i_frame_rate;
978 p_mes->video->i_frame_rate_den = p_es->video.i_frame_rate_base;
980 assert( p_es->video.orientation >= ORIENT_TOP_LEFT &&
981 p_es->video.orientation <= ORIENT_RIGHT_BOTTOM );
982 p_mes->video->i_orientation = (int) p_es->video.orientation;
984 assert( ( p_es->video.projection_mode >= PROJECTION_MODE_RECTANGULAR &&
985 p_es->video.projection_mode <= PROJECTION_MODE_EQUIRECTANGULAR ) ||
986 ( p_es->video.projection_mode == PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD ) );
987 p_mes->video->i_projection = (int) p_es->video.projection_mode;
989 p_mes->video->pose.f_yaw = p_es->video.pose.yaw;
990 p_mes->video->pose.f_pitch = p_es->video.pose.pitch;
991 p_mes->video->pose.f_roll = p_es->video.pose.roll;
992 p_mes->video->pose.f_field_of_view = p_es->video.pose.fov;
994 assert( p_es->video.multiview_mode >= MULTIVIEW_2D &&
995 p_es->video.multiview_mode <= MULTIVIEW_STEREO_CHECKERBOARD );
996 p_mes->video->i_multiview = (int) p_es->video.multiview_mode;
997 break;
998 case AUDIO_ES:
999 p_mes->i_type = libvlc_track_audio;
1000 p_mes->audio->i_channels = p_es->audio.i_channels;
1001 p_mes->audio->i_rate = p_es->audio.i_rate;
1002 break;
1003 case SPU_ES:
1004 p_mes->i_type = libvlc_track_text;
1005 p_mes->subtitle->psz_encoding = p_es->subs.psz_encoding != NULL ?
1006 strdup(p_es->subs.psz_encoding) : NULL;
1007 break;
1011 vlc_mutex_unlock( &p_input_item->lock );
1012 return i_es;
1015 /**************************************************************************
1016 * Get codec description from media elementary stream
1017 **************************************************************************/
1018 const char *
1019 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
1020 uint32_t i_codec )
1022 switch( i_type )
1024 case libvlc_track_audio:
1025 return vlc_fourcc_GetDescription( AUDIO_ES, i_codec );
1026 case libvlc_track_video:
1027 return vlc_fourcc_GetDescription( VIDEO_ES, i_codec );
1028 case libvlc_track_text:
1029 return vlc_fourcc_GetDescription( SPU_ES, i_codec );
1030 case libvlc_track_unknown:
1031 default:
1032 return vlc_fourcc_GetDescription( UNKNOWN_ES, i_codec );
1036 /**************************************************************************
1037 * Release media descriptor's elementary streams description array
1038 **************************************************************************/
1039 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
1041 for( unsigned i = 0; i < i_count; ++i )
1043 if ( !p_tracks[i] )
1044 continue;
1045 free( p_tracks[i]->psz_language );
1046 free( p_tracks[i]->psz_description );
1047 switch( p_tracks[i]->i_type )
1049 case libvlc_track_audio:
1050 break;
1051 case libvlc_track_video:
1052 break;
1053 case libvlc_track_text:
1054 free( p_tracks[i]->subtitle->psz_encoding );
1055 break;
1056 case libvlc_track_unknown:
1057 default:
1058 break;
1060 free( p_tracks[i]->audio );
1061 free( p_tracks[i] );
1063 free( p_tracks );
1066 /**************************************************************************
1067 * Get the media type of the media descriptor object
1068 **************************************************************************/
1069 libvlc_media_type_t libvlc_media_get_type( libvlc_media_t *p_md )
1071 assert( p_md );
1073 enum input_item_type_e i_type;
1074 input_item_t *p_input_item = p_md->p_input_item;
1076 vlc_mutex_lock( &p_input_item->lock );
1077 i_type = p_md->p_input_item->i_type;
1078 vlc_mutex_unlock( &p_input_item->lock );
1080 switch( i_type )
1082 case ITEM_TYPE_FILE:
1083 return libvlc_media_type_file;
1084 case ITEM_TYPE_NODE:
1085 case ITEM_TYPE_DIRECTORY:
1086 return libvlc_media_type_directory;
1087 case ITEM_TYPE_DISC:
1088 return libvlc_media_type_disc;
1089 case ITEM_TYPE_STREAM:
1090 return libvlc_media_type_stream;
1091 case ITEM_TYPE_PLAYLIST:
1092 return libvlc_media_type_playlist;
1093 default:
1094 return libvlc_media_type_unknown;
1098 struct libvlc_media_thumbnail_request_t
1100 libvlc_media_t *md;
1101 unsigned int width;
1102 unsigned int height;
1103 libvlc_picture_type_t type;
1104 vlc_thumbnailer_request_t* req;
1107 static void media_on_thumbnail_ready( void* data, picture_t* thumbnail )
1109 libvlc_media_thumbnail_request_t *req = data;
1110 libvlc_media_t *p_media = req->md;
1111 libvlc_event_t event;
1112 event.type = libvlc_MediaThumbnailGenerated;
1113 libvlc_picture_t* pic = NULL;
1114 if ( thumbnail != NULL )
1115 pic = libvlc_picture_new( VLC_OBJECT(p_media->p_libvlc_instance->p_libvlc_int),
1116 thumbnail, req->type, req->width, req->height );
1117 event.u.media_thumbnail_generated.p_thumbnail = pic;
1118 libvlc_event_send( &p_media->event_manager, &event );
1119 if ( pic != NULL )
1120 libvlc_picture_release( pic );
1121 libvlc_media_release( p_media );
1122 free( req );
1125 libvlc_media_thumbnail_request_t*
1126 libvlc_media_thumbnail_request_by_time( libvlc_media_t *md, libvlc_time_t time,
1127 libvlc_thumbnailer_seek_speed_t speed,
1128 unsigned int width, unsigned int height,
1129 libvlc_picture_type_t picture_type,
1130 libvlc_time_t timeout )
1132 assert( md );
1133 libvlc_priv_t *p_priv = libvlc_priv(md->p_libvlc_instance->p_libvlc_int);
1134 if( unlikely( p_priv->p_thumbnailer == NULL ) )
1135 return NULL;
1136 libvlc_media_thumbnail_request_t *req = malloc( sizeof( *req ) );
1137 if ( unlikely( req == NULL ) )
1138 return NULL;
1140 req->md = md;
1141 req->width = width;
1142 req->height = height;
1143 req->type = picture_type;
1144 libvlc_media_retain( md );
1145 req->req = vlc_thumbnailer_RequestByTime( p_priv->p_thumbnailer,
1146 VLC_TICK_FROM_MS( time ),
1147 speed == libvlc_media_thumbnail_seek_fast ?
1148 VLC_THUMBNAILER_SEEK_FAST : VLC_THUMBNAILER_SEEK_PRECISE,
1149 md->p_input_item,
1150 timeout > 0 ? VLC_TICK_FROM_MS( timeout ) : VLC_TICK_INVALID,
1151 media_on_thumbnail_ready, req );
1152 if ( req->req == NULL )
1154 free( req );
1155 libvlc_media_release( md );
1156 return NULL;
1158 return req;
1161 libvlc_media_thumbnail_request_t*
1162 libvlc_media_thumbnail_request_by_pos( libvlc_media_t *md, float pos,
1163 libvlc_thumbnailer_seek_speed_t speed,
1164 unsigned int width, unsigned int height,
1165 libvlc_picture_type_t picture_type,
1166 libvlc_time_t timeout )
1168 assert( md );
1169 libvlc_priv_t *priv = libvlc_priv(md->p_libvlc_instance->p_libvlc_int);
1170 if( unlikely( priv->p_thumbnailer == NULL ) )
1171 return NULL;
1172 libvlc_media_thumbnail_request_t *req = malloc( sizeof( *req ) );
1173 if ( unlikely( req == NULL ) )
1174 return NULL;
1176 req->md = md;
1177 req->width = width;
1178 req->height = height;
1179 req->type = picture_type;
1180 libvlc_media_retain( md );
1181 req->req = vlc_thumbnailer_RequestByPos( priv->p_thumbnailer, pos,
1182 speed == libvlc_media_thumbnail_seek_fast ?
1183 VLC_THUMBNAILER_SEEK_FAST : VLC_THUMBNAILER_SEEK_PRECISE,
1184 md->p_input_item,
1185 timeout > 0 ? VLC_TICK_FROM_MS( timeout ) : VLC_TICK_INVALID,
1186 media_on_thumbnail_ready, req );
1187 if ( req->req == NULL )
1189 free( req );
1190 libvlc_media_release( md );
1191 return NULL;
1193 return req;
1196 void libvlc_media_thumbnail_cancel( libvlc_media_thumbnail_request_t *req )
1198 libvlc_priv_t *p_priv = libvlc_priv(req->md->p_libvlc_instance->p_libvlc_int);
1199 assert( p_priv->p_thumbnailer != NULL );
1200 vlc_thumbnailer_Cancel( p_priv->p_thumbnailer, req->req );
1201 libvlc_media_release( req->md );
1202 free( req );
1205 int libvlc_media_slaves_add( libvlc_media_t *p_md,
1206 libvlc_media_slave_type_t i_type,
1207 unsigned int i_priority,
1208 const char *psz_uri )
1210 assert( p_md && psz_uri );
1211 input_item_t *p_input_item = p_md->p_input_item;
1213 enum slave_type i_input_slave_type;
1214 switch( i_type )
1216 case libvlc_media_slave_type_subtitle:
1217 i_input_slave_type = SLAVE_TYPE_SPU;
1218 break;
1219 case libvlc_media_slave_type_audio:
1220 i_input_slave_type = SLAVE_TYPE_AUDIO;
1221 break;
1222 default:
1223 vlc_assert_unreachable();
1224 return -1;
1227 enum slave_priority i_input_slave_priority;
1228 switch( i_priority )
1230 case 0:
1231 i_input_slave_priority = SLAVE_PRIORITY_MATCH_NONE;
1232 break;
1233 case 1:
1234 i_input_slave_priority = SLAVE_PRIORITY_MATCH_RIGHT;
1235 break;
1236 case 2:
1237 i_input_slave_priority = SLAVE_PRIORITY_MATCH_LEFT;
1238 break;
1239 case 3:
1240 i_input_slave_priority = SLAVE_PRIORITY_MATCH_ALL;
1241 break;
1242 default:
1243 case 4:
1244 i_input_slave_priority = SLAVE_PRIORITY_USER;
1245 break;
1248 input_item_slave_t *p_slave = input_item_slave_New( psz_uri,
1249 i_input_slave_type,
1250 i_input_slave_priority );
1251 if( p_slave == NULL )
1252 return -1;
1253 return input_item_AddSlave( p_input_item, p_slave ) == VLC_SUCCESS ? 0 : -1;
1256 void libvlc_media_slaves_clear( libvlc_media_t *p_md )
1258 assert( p_md );
1259 input_item_t *p_input_item = p_md->p_input_item;
1261 vlc_mutex_lock( &p_input_item->lock );
1262 for( int i = 0; i < p_input_item->i_slaves; i++ )
1263 input_item_slave_Delete( p_input_item->pp_slaves[i] );
1264 TAB_CLEAN( p_input_item->i_slaves, p_input_item->pp_slaves );
1265 vlc_mutex_unlock( &p_input_item->lock );
1268 unsigned int libvlc_media_slaves_get( libvlc_media_t *p_md,
1269 libvlc_media_slave_t ***ppp_slaves )
1271 assert( p_md && ppp_slaves );
1272 input_item_t *p_input_item = p_md->p_input_item;
1273 *ppp_slaves = NULL;
1275 vlc_mutex_lock( &p_input_item->lock );
1277 int i_count = p_input_item->i_slaves;
1278 if( i_count <= 0 )
1279 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1281 libvlc_media_slave_t **pp_slaves = calloc( i_count, sizeof(*pp_slaves) );
1282 if( pp_slaves == NULL )
1283 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1285 for( int i = 0; i < i_count; ++i )
1287 input_item_slave_t *p_item_slave = p_input_item->pp_slaves[i];
1288 assert( p_item_slave->i_priority >= SLAVE_PRIORITY_MATCH_NONE );
1290 /* also allocate psz_uri buffer at the end of the struct */
1291 libvlc_media_slave_t *p_slave = malloc( sizeof(*p_slave) +
1292 strlen( p_item_slave->psz_uri )
1293 + 1 );
1294 if( p_slave == NULL )
1296 libvlc_media_slaves_release(pp_slaves, i);
1297 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1299 p_slave->psz_uri = (char *) ((uint8_t *)p_slave) + sizeof(*p_slave);
1300 strcpy( p_slave->psz_uri, p_item_slave->psz_uri );
1302 switch( p_item_slave->i_type )
1304 case SLAVE_TYPE_SPU:
1305 p_slave->i_type = libvlc_media_slave_type_subtitle;
1306 break;
1307 case SLAVE_TYPE_AUDIO:
1308 p_slave->i_type = libvlc_media_slave_type_audio;
1309 break;
1310 default:
1311 vlc_assert_unreachable();
1314 switch( p_item_slave->i_priority )
1316 case SLAVE_PRIORITY_MATCH_NONE:
1317 p_slave->i_priority = 0;
1318 break;
1319 case SLAVE_PRIORITY_MATCH_RIGHT:
1320 p_slave->i_priority = 1;
1321 break;
1322 case SLAVE_PRIORITY_MATCH_LEFT:
1323 p_slave->i_priority = 2;
1324 break;
1325 case SLAVE_PRIORITY_MATCH_ALL:
1326 p_slave->i_priority = 3;
1327 break;
1328 case SLAVE_PRIORITY_USER:
1329 p_slave->i_priority = 4;
1330 break;
1331 default:
1332 vlc_assert_unreachable();
1334 pp_slaves[i] = p_slave;
1336 vlc_mutex_unlock( &p_input_item->lock );
1338 *ppp_slaves = pp_slaves;
1339 return i_count;
1342 void libvlc_media_slaves_release( libvlc_media_slave_t **pp_slaves,
1343 unsigned int i_count )
1345 if( i_count > 0 )
1347 assert( pp_slaves );
1348 for( unsigned int i = 0; i < i_count; ++i )
1349 free( pp_slaves[i] );
1351 free( pp_slaves );