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 *****************************************************************************/
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>
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
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" );
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" );
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
,
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
);
156 static libvlc_media_t
*input_item_add_subitem( libvlc_media_t
*p_md
,
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
,
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
;
180 libvlc_event_send( &p_md
->event_manager
, &event
);
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
,
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
)
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
;
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
,
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
];
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
,
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
);
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
);
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
;
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
,
318 libvlc_media_t
* p_md
= user_data
;
319 libvlc_media_parsed_status_t new_status
;
323 case ITEM_PREPARSE_SKIPPED
:
324 new_status
= libvlc_media_parsed_status_skipped
;
326 case ITEM_PREPARSE_FAILED
:
327 new_status
= libvlc_media_parsed_status_failed
;
329 case ITEM_PREPARSE_TIMEOUT
:
330 new_status
= libvlc_media_parsed_status_timeout
;
332 case ITEM_PREPARSE_DONE
:
333 new_status
= libvlc_media_parsed_status_done
;
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
,
350 vlc_event_attach( &p_md
->p_input_item
->event_manager
,
351 vlc_InputItemDurationChanged
,
352 input_item_duration_changed
,
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
,
365 vlc_event_detach( &p_md
->p_input_item
->event_manager
,
366 vlc_InputItemDurationChanged
,
367 input_item_duration_changed
,
371 /**************************************************************************
372 * Create a new media descriptor object from an input_item
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
;
384 libvlc_printerr( "No input item given" );
388 p_md
= calloc( 1, sizeof(libvlc_media_t
) );
391 libvlc_printerr( "Not enough memory" );
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
);
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
);
432 libvlc_printerr( "Not enough memory" );
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
);
444 libvlc_media_t
*libvlc_media_new_path( libvlc_instance_t
*p_instance
,
447 char *mrl
= vlc_path2uri( path
, NULL
);
448 if( unlikely(mrl
== NULL
) )
450 libvlc_printerr( "%s", vlc_strerror_c(errno
) );
454 libvlc_media_t
*m
= libvlc_media_new_location( p_instance
, mrl
);
459 libvlc_media_t
*libvlc_media_new_fd( libvlc_instance_t
*p_instance
, int fd
)
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
,
474 libvlc_media_t
*m
= libvlc_media_new_location(p_instance
, "imem://");
475 if (unlikely(m
== 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
);
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
);
501 libvlc_printerr( "Not enough memory" );
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
);
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
,
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
)
552 if( p_md
->i_refcount
> 0 )
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
;
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
);
582 /**************************************************************************
583 * Retain a media descriptor object
584 **************************************************************************/
585 void libvlc_media_retain( libvlc_media_t
*p_md
)
591 /**************************************************************************
592 * Duplicate a media descriptor object
593 **************************************************************************/
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 **************************************************************************/
605 libvlc_media_get_mrl( libvlc_media_t
* 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
);
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
);
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
)
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
)
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 **************************************************************************/
658 libvlc_media_get_state( libvlc_media_t
*p_md
)
664 /**************************************************************************
665 * Setter for state information (LibVLC Internal)
666 **************************************************************************/
669 libvlc_media_set_state( libvlc_media_t
*p_md
,
670 libvlc_state_t state
)
672 libvlc_event_t event
;
676 /* Construct the event */
677 event
.type
= libvlc_MediaStateChanged
;
678 event
.u
.media_state_changed
.new_state
= state
;
681 libvlc_event_send( &p_md
->event_manager
, &event
);
684 /**************************************************************************
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 );
692 libvlc_media_list_retain( 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
)
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
);
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
);
737 /**************************************************************************
739 **************************************************************************/
740 libvlc_event_manager_t
*
741 libvlc_media_event_manager( libvlc_media_t
* p_md
)
745 return &p_md
->event_manager
;
748 /**************************************************************************
749 * Get duration of media object (in ms)
750 **************************************************************************/
752 libvlc_media_get_duration( libvlc_media_t
* p_md
)
756 if( !p_md
->p_input_item
)
758 libvlc_printerr( "No input item" );
762 if (!input_item_IsPreparsed( p_md
->p_input_item
))
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
)
778 vlc_mutex_lock(&media
->parsed_lock
);
779 needed
= !media
->has_asked_preparse
;
780 media
->has_asked_preparse
= true;
783 media
->is_parsed
= false;
784 media
->parsed_status
= 0;
786 vlc_mutex_unlock(&media
->parsed_lock
);
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
;
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
,
807 if (ret
!= VLC_SUCCESS
)
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
);
823 /**************************************************************************
824 * Parse the media and wait.
825 **************************************************************************/
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 **************************************************************************/
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
,
849 return media_parse( media
, true, parse_flag
, timeout
) == VLC_SUCCESS
? 0 : -1;
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
)
865 vlc_mutex_lock(&media
->parsed_lock
);
866 parsed
= media
->is_parsed
;
867 vlc_mutex_unlock(&media
->parsed_lock
);
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
);
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 **************************************************************************/
888 libvlc_media_set_user_data( libvlc_media_t
* p_md
, void * p_new_user_data
)
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 **************************************************************************/
900 libvlc_media_get_user_data( libvlc_media_t
* p_md
)
903 return p_md
->p_user_data
;
907 libvlc_media_tracks_get( libvlc_media_t
*p_md
, libvlc_media_track_t
*** pp_es
)
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
);
924 for( int i
= 0; i
< i_es
; i
++ )
926 libvlc_media_track_t
*p_mes
= calloc( 1, sizeof(*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
);
938 vlc_mutex_unlock( &p_input_item
->lock
);
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
;
960 p_mes
->i_type
= libvlc_track_unknown
;
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
;
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
;
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
;
1002 vlc_mutex_unlock( &p_input_item
->lock
);
1006 /**************************************************************************
1007 * Get codec description from media elementary stream
1008 **************************************************************************/
1010 libvlc_media_get_codec_description( libvlc_track_type_t 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
:
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
)
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
:
1042 case libvlc_track_video
:
1044 case libvlc_track_text
:
1045 free( p_tracks
[i
]->subtitle
->psz_encoding
);
1047 case libvlc_track_unknown
:
1051 free( p_tracks
[i
]->audio
);
1052 free( p_tracks
[i
] );
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
)
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
);
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
;
1085 return libvlc_media_type_unknown
;
1089 struct libvlc_media_thumbnail_request_t
1093 unsigned int height
;
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
,
1110 event
.u
.media_thumbnail_generated
.p_thumbnail
= pic
;
1111 libvlc_event_send( &p_media
->event_manager
, &event
);
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
)
1124 libvlc_priv_t
*p_priv
= libvlc_priv(md
->p_libvlc_instance
->p_libvlc_int
);
1125 if( unlikely( p_priv
->p_thumbnailer
== NULL
) )
1127 libvlc_media_thumbnail_request_t
*req
= malloc( sizeof( *req
) );
1128 if ( unlikely( req
== NULL
) )
1133 req
->height
= height
;
1134 req
->type
= picture_type
;
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
,
1142 timeout
> 0 ? VLC_TICK_FROM_MS( timeout
) : VLC_TICK_INVALID
,
1143 media_on_thumbnail_ready
, req
);
1144 if ( req
->req
== NULL
)
1147 libvlc_media_release( md
);
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
)
1161 libvlc_priv_t
*priv
= libvlc_priv(md
->p_libvlc_instance
->p_libvlc_int
);
1162 if( unlikely( priv
->p_thumbnailer
== NULL
) )
1164 libvlc_media_thumbnail_request_t
*req
= malloc( sizeof( *req
) );
1165 if ( unlikely( req
== NULL
) )
1170 req
->height
= height
;
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
,
1178 timeout
> 0 ? VLC_TICK_FROM_MS( timeout
) : VLC_TICK_INVALID
,
1179 media_on_thumbnail_ready
, req
);
1180 if ( req
->req
== NULL
)
1183 libvlc_media_release( md
);
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
);
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
;
1213 case libvlc_media_slave_type_subtitle
:
1214 i_input_slave_type
= SLAVE_TYPE_SPU
;
1216 case libvlc_media_slave_type_audio
:
1217 i_input_slave_type
= SLAVE_TYPE_AUDIO
;
1220 vlc_assert_unreachable();
1224 enum slave_priority i_input_slave_priority
;
1225 switch( i_priority
)
1228 i_input_slave_priority
= SLAVE_PRIORITY_MATCH_NONE
;
1231 i_input_slave_priority
= SLAVE_PRIORITY_MATCH_RIGHT
;
1234 i_input_slave_priority
= SLAVE_PRIORITY_MATCH_LEFT
;
1237 i_input_slave_priority
= SLAVE_PRIORITY_MATCH_ALL
;
1241 i_input_slave_priority
= SLAVE_PRIORITY_USER
;
1245 input_item_slave_t
*p_slave
= input_item_slave_New( psz_uri
,
1247 i_input_slave_priority
);
1248 if( p_slave
== NULL
)
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
)
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
;
1272 vlc_mutex_lock( &p_input_item
->lock
);
1274 int i_count
= p_input_item
->i_slaves
;
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
)
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
;
1304 case SLAVE_TYPE_AUDIO
:
1305 p_slave
->i_type
= libvlc_media_slave_type_audio
;
1308 vlc_assert_unreachable();
1311 switch( p_item_slave
->i_priority
)
1313 case SLAVE_PRIORITY_MATCH_NONE
:
1314 p_slave
->i_priority
= 0;
1316 case SLAVE_PRIORITY_MATCH_RIGHT
:
1317 p_slave
->i_priority
= 1;
1319 case SLAVE_PRIORITY_MATCH_LEFT
:
1320 p_slave
->i_priority
= 2;
1322 case SLAVE_PRIORITY_MATCH_ALL
:
1323 p_slave
->i_priority
= 3;
1325 case SLAVE_PRIORITY_USER
:
1326 p_slave
->i_priority
= 4;
1329 vlc_assert_unreachable();
1331 pp_slaves
[i
] = p_slave
;
1333 vlc_mutex_unlock( &p_input_item
->lock
);
1335 *ppp_slaves
= pp_slaves
;
1339 void libvlc_media_slaves_release( libvlc_media_slave_t
**pp_slaves
,
1340 unsigned int i_count
)
1344 assert( pp_slaves
);
1345 for( unsigned int i
= 0; i
< i_count
; ++i
)
1346 free( pp_slaves
[i
] );