1 /*****************************************************************************
2 * media.c: Libvlc API media descripor management
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
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 *****************************************************************************/
31 #include <vlc/libvlc.h>
32 #include <vlc/libvlc_media.h>
33 #include <vlc/libvlc_media_list.h> // For the subitems, here for convenience
34 #include <vlc/libvlc_events.h>
36 #include <vlc_common.h>
37 #include <vlc_input.h>
39 #include <vlc_playlist.h> /* For the preparser */
42 #include "../src/libvlc.h"
44 #include "libvlc_internal.h"
45 #include "media_internal.h"
46 #include "media_list_internal.h"
48 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
50 [libvlc_meta_Title] = vlc_meta_Title,
51 [libvlc_meta_Artist] = vlc_meta_Artist,
52 [libvlc_meta_Genre] = vlc_meta_Genre,
53 [libvlc_meta_Copyright] = vlc_meta_Copyright,
54 [libvlc_meta_Album] = vlc_meta_Album,
55 [libvlc_meta_TrackNumber] = vlc_meta_TrackNumber,
56 [libvlc_meta_Description] = vlc_meta_Description,
57 [libvlc_meta_Rating] = vlc_meta_Rating,
58 [libvlc_meta_Date] = vlc_meta_Date,
59 [libvlc_meta_Setting] = vlc_meta_Setting,
60 [libvlc_meta_URL] = vlc_meta_URL,
61 [libvlc_meta_Language] = vlc_meta_Language,
62 [libvlc_meta_NowPlaying] = vlc_meta_NowPlaying,
63 [libvlc_meta_Publisher] = vlc_meta_Publisher,
64 [libvlc_meta_EncodedBy] = vlc_meta_EncodedBy,
65 [libvlc_meta_ArtworkURL] = vlc_meta_ArtworkURL,
66 [libvlc_meta_TrackID] = vlc_meta_TrackID,
67 [libvlc_meta_TrackTotal] = vlc_meta_TrackTotal,
68 [libvlc_meta_Director] = vlc_meta_Director,
69 [libvlc_meta_Season] = vlc_meta_Season,
70 [libvlc_meta_Episode] = vlc_meta_Episode,
71 [libvlc_meta_ShowName] = vlc_meta_ShowName,
72 [libvlc_meta_Actors] = vlc_meta_Actors,
73 [libvlc_meta_AlbumArtist] = vlc_meta_AlbumArtist,
74 [libvlc_meta_DiscNumber] = vlc_meta_DiscNumber
77 static const libvlc_meta_t vlc_to_libvlc_meta[] =
79 [vlc_meta_Title] = libvlc_meta_Title,
80 [vlc_meta_Artist] = libvlc_meta_Artist,
81 [vlc_meta_Genre] = libvlc_meta_Genre,
82 [vlc_meta_Copyright] = libvlc_meta_Copyright,
83 [vlc_meta_Album] = libvlc_meta_Album,
84 [vlc_meta_TrackNumber] = libvlc_meta_TrackNumber,
85 [vlc_meta_Description] = libvlc_meta_Description,
86 [vlc_meta_Rating] = libvlc_meta_Rating,
87 [vlc_meta_Date] = libvlc_meta_Date,
88 [vlc_meta_Setting] = libvlc_meta_Setting,
89 [vlc_meta_URL] = libvlc_meta_URL,
90 [vlc_meta_Language] = libvlc_meta_Language,
91 [vlc_meta_NowPlaying] = libvlc_meta_NowPlaying,
92 [vlc_meta_ESNowPlaying] = libvlc_meta_NowPlaying,
93 [vlc_meta_Publisher] = libvlc_meta_Publisher,
94 [vlc_meta_EncodedBy] = libvlc_meta_EncodedBy,
95 [vlc_meta_ArtworkURL] = libvlc_meta_ArtworkURL,
96 [vlc_meta_TrackID] = libvlc_meta_TrackID,
97 [vlc_meta_TrackTotal] = libvlc_meta_TrackTotal,
98 [vlc_meta_Director] = libvlc_meta_Director,
99 [vlc_meta_Season] = libvlc_meta_Season,
100 [vlc_meta_Episode] = libvlc_meta_Episode,
101 [vlc_meta_ShowName] = libvlc_meta_ShowName,
102 [vlc_meta_Actors] = libvlc_meta_Actors,
103 [vlc_meta_AlbumArtist] = libvlc_meta_AlbumArtist,
104 [vlc_meta_DiscNumber] = libvlc_meta_DiscNumber
107 static libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md )
109 libvlc_media_list_t *p_subitems = NULL;
111 vlc_mutex_lock( &p_md->subitems_lock );
112 if( p_md->p_subitems == NULL )
114 p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance );
115 if( p_md->p_subitems != NULL )
117 p_md->p_subitems->b_read_only = true;
118 p_md->p_subitems->p_internal_md = p_md;
121 p_subitems = p_md->p_subitems;
122 vlc_mutex_unlock( &p_md->subitems_lock );
126 /**************************************************************************
127 * input_item_subitem_added (Private) (vlc event Callback)
128 **************************************************************************/
129 static void input_item_subitem_added( const vlc_event_t *p_event,
132 libvlc_media_t * p_md = user_data;
133 libvlc_media_t * p_md_child;
134 libvlc_media_list_t *p_subitems;
135 libvlc_event_t event;
137 p_md_child = libvlc_media_new_from_input_item(
138 p_md->p_libvlc_instance,
139 p_event->u.input_item_subitem_added.p_new_child );
141 /* Add this to our media list */
142 p_subitems = media_get_subitems( p_md );
143 if( p_subitems != NULL )
145 libvlc_media_list_lock( p_subitems );
146 libvlc_media_list_internal_add_media( p_subitems, p_md_child );
147 libvlc_media_list_unlock( p_subitems );
150 /* Construct the event */
151 event.type = libvlc_MediaSubItemAdded;
152 event.u.media_subitem_added.new_child = p_md_child;
155 libvlc_event_send( p_md->p_event_manager, &event );
156 libvlc_media_release( p_md_child );
159 /**************************************************************************
160 * input_item_subitemtree_added (Private) (vlc event Callback)
161 **************************************************************************/
162 static void input_item_subitemtree_added( const vlc_event_t * p_event,
165 VLC_UNUSED( p_event );
166 libvlc_media_t * p_md = user_data;
167 libvlc_media_list_t *p_subitems;
168 libvlc_event_t event;
170 /* notify the media list */
171 p_subitems = media_get_subitems( p_md );
172 if( p_subitems != NULL )
174 libvlc_media_list_lock( p_subitems );
175 libvlc_media_list_internal_end_reached( p_subitems );
176 libvlc_media_list_unlock( p_subitems );
179 /* Construct the event */
180 event.type = libvlc_MediaSubItemTreeAdded;
181 event.u.media_subitemtree_added.item = p_md;
184 libvlc_event_send( p_md->p_event_manager, &event );
187 /**************************************************************************
188 * input_item_meta_changed (Private) (vlc event Callback)
189 **************************************************************************/
190 static void input_item_meta_changed( const vlc_event_t *p_event,
193 libvlc_media_t * p_md = user_data;
194 libvlc_event_t event;
196 /* Construct the event */
197 event.type = libvlc_MediaMetaChanged;
198 event.u.media_meta_changed.meta_type =
199 vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
202 libvlc_event_send( p_md->p_event_manager, &event );
205 /**************************************************************************
206 * input_item_duration_changed (Private) (vlc event Callback)
207 **************************************************************************/
208 static void input_item_duration_changed( const vlc_event_t *p_event,
211 libvlc_media_t * p_md = user_data;
212 libvlc_event_t event;
214 /* Construct the event */
215 event.type = libvlc_MediaDurationChanged;
216 event.u.media_duration_changed.new_duration =
217 from_mtime(p_event->u.input_item_duration_changed.new_duration);
220 libvlc_event_send( p_md->p_event_manager, &event );
223 /**************************************************************************
224 * input_item_preparsed_changed (Private) (vlc event Callback)
225 **************************************************************************/
226 static void input_item_preparsed_changed(const vlc_event_t *p_event,
229 libvlc_media_t *media = user_data;
230 libvlc_event_t event;
232 /* Eventually notify libvlc_media_parse() */
233 vlc_mutex_lock(&media->parsed_lock);
234 media->is_parsed = true;
235 vlc_cond_broadcast(&media->parsed_cond);
236 vlc_mutex_unlock(&media->parsed_lock);
239 /* Construct the event */
240 event.type = libvlc_MediaParsedChanged;
241 event.u.media_parsed_changed.new_status =
242 p_event->u.input_item_preparsed_changed.new_status;
245 libvlc_event_send(media->p_event_manager, &event);
248 /**************************************************************************
249 * Install event handler (Private)
250 **************************************************************************/
251 static void install_input_item_observer( libvlc_media_t *p_md )
253 vlc_event_attach( &p_md->p_input_item->event_manager,
254 vlc_InputItemSubItemAdded,
255 input_item_subitem_added,
257 vlc_event_attach( &p_md->p_input_item->event_manager,
258 vlc_InputItemMetaChanged,
259 input_item_meta_changed,
261 vlc_event_attach( &p_md->p_input_item->event_manager,
262 vlc_InputItemDurationChanged,
263 input_item_duration_changed,
265 vlc_event_attach( &p_md->p_input_item->event_manager,
266 vlc_InputItemPreparsedChanged,
267 input_item_preparsed_changed,
269 vlc_event_attach( &p_md->p_input_item->event_manager,
270 vlc_InputItemSubItemTreeAdded,
271 input_item_subitemtree_added,
275 /**************************************************************************
276 * Uninstall event handler (Private)
277 **************************************************************************/
278 static void uninstall_input_item_observer( libvlc_media_t *p_md )
280 vlc_event_detach( &p_md->p_input_item->event_manager,
281 vlc_InputItemSubItemAdded,
282 input_item_subitem_added,
284 vlc_event_detach( &p_md->p_input_item->event_manager,
285 vlc_InputItemMetaChanged,
286 input_item_meta_changed,
288 vlc_event_detach( &p_md->p_input_item->event_manager,
289 vlc_InputItemDurationChanged,
290 input_item_duration_changed,
292 vlc_event_detach( &p_md->p_input_item->event_manager,
293 vlc_InputItemPreparsedChanged,
294 input_item_preparsed_changed,
296 vlc_event_detach( &p_md->p_input_item->event_manager,
297 vlc_InputItemSubItemTreeAdded,
298 input_item_subitemtree_added,
302 /**************************************************************************
303 * Create a new media descriptor object from an input_item
305 * That's the generic constructor
306 **************************************************************************/
307 libvlc_media_t * libvlc_media_new_from_input_item(
308 libvlc_instance_t *p_instance,
309 input_item_t *p_input_item )
311 libvlc_media_t * p_md;
315 libvlc_printerr( "No input item given" );
319 p_md = calloc( 1, sizeof(libvlc_media_t) );
322 libvlc_printerr( "Not enough memory" );
326 p_md->p_libvlc_instance = p_instance;
327 p_md->p_input_item = p_input_item;
328 p_md->i_refcount = 1;
330 vlc_cond_init(&p_md->parsed_cond);
331 vlc_mutex_init(&p_md->parsed_lock);
332 vlc_mutex_init(&p_md->subitems_lock);
334 p_md->state = libvlc_NothingSpecial;
336 /* A media descriptor can be a playlist. When you open a playlist
337 * It can give a bunch of item to read. */
338 p_md->p_subitems = NULL;
340 p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance );
341 if( unlikely(p_md->p_event_manager == NULL) )
347 libvlc_event_manager_t *em = p_md->p_event_manager;
348 libvlc_event_manager_register_event_type(em, libvlc_MediaMetaChanged);
349 libvlc_event_manager_register_event_type(em, libvlc_MediaSubItemAdded);
350 libvlc_event_manager_register_event_type(em, libvlc_MediaFreed);
351 libvlc_event_manager_register_event_type(em, libvlc_MediaDurationChanged);
352 libvlc_event_manager_register_event_type(em, libvlc_MediaStateChanged);
353 libvlc_event_manager_register_event_type(em, libvlc_MediaParsedChanged);
354 libvlc_event_manager_register_event_type(em, libvlc_MediaSubItemTreeAdded);
356 vlc_gc_incref( p_md->p_input_item );
358 install_input_item_observer( p_md );
363 /**************************************************************************
364 * Create a new media descriptor object
365 **************************************************************************/
366 libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
367 const char * psz_mrl )
369 input_item_t * p_input_item;
370 libvlc_media_t * p_md;
372 p_input_item = input_item_New( psz_mrl, NULL );
376 libvlc_printerr( "Not enough memory" );
380 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
382 /* The p_input_item is retained in libvlc_media_new_from_input_item */
383 vlc_gc_decref( p_input_item );
388 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
391 char *mrl = vlc_path2uri( path, NULL );
392 if( unlikely(mrl == NULL) )
394 libvlc_printerr( "%s", vlc_strerror_c(errno) );
398 libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
403 libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
406 snprintf( mrl, sizeof(mrl), "fd://%d", fd );
408 return libvlc_media_new_location( p_instance, mrl );
411 /**************************************************************************
412 * Create a new media descriptor object
413 **************************************************************************/
414 libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
415 const char * psz_name )
417 input_item_t * p_input_item;
418 libvlc_media_t * p_md;
419 libvlc_media_list_t * p_subitems;
421 p_input_item = input_item_New( "vlc://nop", psz_name );
425 libvlc_printerr( "Not enough memory" );
429 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
431 p_subitems = media_get_subitems( p_md );
432 if( p_subitems == NULL) {
433 libvlc_media_release( p_md );
440 /**************************************************************************
441 * Add an option to the media descriptor,
442 * that will be used to determine how the media_player will read the
443 * media. This allow to use VLC advanced reading/streaming
444 * options in a per-media basis
446 * The options are detailled in vlc --long-help, for instance "--sout-all"
447 **************************************************************************/
448 void libvlc_media_add_option( libvlc_media_t * p_md,
449 const char * psz_option )
451 libvlc_media_add_option_flag( p_md, psz_option,
452 VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
455 /**************************************************************************
456 * Same as libvlc_media_add_option but with configurable flags.
457 **************************************************************************/
458 void libvlc_media_add_option_flag( libvlc_media_t * p_md,
459 const char * ppsz_option,
462 input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
465 /**************************************************************************
466 * Delete a media descriptor object
467 **************************************************************************/
468 void libvlc_media_release( libvlc_media_t *p_md )
475 if( p_md->i_refcount > 0 )
478 if( p_md->p_subitems )
479 libvlc_media_list_release( p_md->p_subitems );
481 uninstall_input_item_observer( p_md );
482 vlc_gc_decref( p_md->p_input_item );
484 vlc_cond_destroy( &p_md->parsed_cond );
485 vlc_mutex_destroy( &p_md->parsed_lock );
486 vlc_mutex_destroy( &p_md->subitems_lock );
488 /* Construct the event */
489 libvlc_event_t event;
490 event.type = libvlc_MediaFreed;
491 event.u.media_freed.md = p_md;
494 libvlc_event_send( p_md->p_event_manager, &event );
496 libvlc_event_manager_release( p_md->p_event_manager );
501 /**************************************************************************
502 * Retain a media descriptor object
503 **************************************************************************/
504 void libvlc_media_retain( libvlc_media_t *p_md )
510 /**************************************************************************
511 * Duplicate a media descriptor object
512 **************************************************************************/
514 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
516 return libvlc_media_new_from_input_item(
517 p_md_orig->p_libvlc_instance, p_md_orig->p_input_item );
520 /**************************************************************************
521 * Get mrl from a media descriptor object
522 **************************************************************************/
524 libvlc_media_get_mrl( libvlc_media_t * p_md )
527 return input_item_GetURI( p_md->p_input_item );
530 /**************************************************************************
531 * Getter for meta information
532 **************************************************************************/
534 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
536 char *psz_meta = NULL;
538 if( e_meta == libvlc_meta_NowPlaying )
540 psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
544 psz_meta = input_item_GetMeta( p_md->p_input_item,
545 libvlc_to_vlc_meta[e_meta] );
546 /* Should be integrated in core */
547 if( psz_meta == NULL && e_meta == libvlc_meta_Title
548 && p_md->p_input_item->psz_name != NULL )
549 psz_meta = strdup( p_md->p_input_item->psz_name );
554 /**************************************************************************
555 * Setter for meta information
556 **************************************************************************/
558 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
561 input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
564 int libvlc_media_save_meta( libvlc_media_t *p_md )
567 vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
568 return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
571 /**************************************************************************
572 * Getter for state information
573 * Can be error, playing, buffering, NothingSpecial.
574 **************************************************************************/
577 libvlc_media_get_state( libvlc_media_t *p_md )
583 /**************************************************************************
584 * Setter for state information (LibVLC Internal)
585 **************************************************************************/
588 libvlc_media_set_state( libvlc_media_t *p_md,
589 libvlc_state_t state )
591 libvlc_event_t event;
595 /* Construct the event */
596 event.type = libvlc_MediaStateChanged;
597 event.u.media_state_changed.new_state = state;
600 libvlc_event_send( p_md->p_event_manager, &event );
603 /**************************************************************************
605 **************************************************************************/
606 libvlc_media_list_t *
607 libvlc_media_subitems( libvlc_media_t * p_md )
609 libvlc_media_list_t *p_subitems = media_get_subitems( p_md );
611 libvlc_media_list_retain( p_subitems );
615 /**************************************************************************
616 * Getter for statistics information
617 **************************************************************************/
618 int libvlc_media_get_stats( libvlc_media_t *p_md,
619 libvlc_media_stats_t *p_stats )
621 if( !p_md->p_input_item )
624 input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
625 vlc_mutex_lock( &p_itm_stats->lock );
626 p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
627 p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
629 p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
630 p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
631 p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
632 p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
634 p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
635 p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
637 p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
638 p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
640 p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
641 p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
643 p_stats->i_sent_packets = p_itm_stats->i_sent_packets;
644 p_stats->i_sent_bytes = p_itm_stats->i_sent_bytes;
645 p_stats->f_send_bitrate = p_itm_stats->f_send_bitrate;
646 vlc_mutex_unlock( &p_itm_stats->lock );
650 /**************************************************************************
652 **************************************************************************/
653 libvlc_event_manager_t *
654 libvlc_media_event_manager( libvlc_media_t * p_md )
658 return p_md->p_event_manager;
661 /**************************************************************************
662 * Get duration of media object (in ms)
663 **************************************************************************/
665 libvlc_media_get_duration( libvlc_media_t * p_md )
669 if( !p_md->p_input_item )
671 libvlc_printerr( "No input item" );
675 if (!input_item_IsPreparsed( p_md->p_input_item ))
678 return from_mtime(input_item_GetDuration( p_md->p_input_item ));
681 static int media_parse(libvlc_media_t *media, bool b_async,
682 libvlc_media_parse_flag_t parse_flag)
686 vlc_mutex_lock(&media->parsed_lock);
687 needed = !media->has_asked_preparse;
688 media->has_asked_preparse = true;
689 vlc_mutex_unlock(&media->parsed_lock);
693 libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
694 input_item_t *item = media->p_input_item;
695 input_item_meta_request_option_t art_scope = META_REQUEST_OPTION_NONE;
696 input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
699 if (parse_flag & libvlc_media_fetch_local)
700 art_scope |= META_REQUEST_OPTION_SCOPE_LOCAL;
701 if (parse_flag & libvlc_media_fetch_network)
702 art_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
703 if (art_scope != META_REQUEST_OPTION_NONE) {
704 ret = libvlc_ArtRequest(libvlc, item, art_scope);
705 if (ret != VLC_SUCCESS)
709 if (parse_flag & libvlc_media_parse_network)
710 parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
711 ret = libvlc_MetaRequest(libvlc, item, parse_scope);
712 if (ret != VLC_SUCCESS)
720 vlc_mutex_lock(&media->parsed_lock);
721 while (!media->is_parsed)
722 vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
723 vlc_mutex_unlock(&media->parsed_lock);
728 /**************************************************************************
729 * Parse the media and wait.
730 **************************************************************************/
732 libvlc_media_parse(libvlc_media_t *media)
734 media_parse( media, false, libvlc_media_fetch_local );
737 /**************************************************************************
738 * Parse the media but do not wait.
739 **************************************************************************/
741 libvlc_media_parse_async(libvlc_media_t *media)
743 media_parse( media, true, libvlc_media_fetch_local );
746 /**************************************************************************
747 * Parse the media asynchronously with options.
748 **************************************************************************/
750 libvlc_media_parse_with_options( libvlc_media_t *media,
751 libvlc_media_parse_flag_t parse_flag )
753 return media_parse( media, true, parse_flag ) == VLC_SUCCESS ? 0 : -1;
756 /**************************************************************************
757 * Get parsed status for media object.
758 **************************************************************************/
760 libvlc_media_is_parsed(libvlc_media_t *media)
764 vlc_mutex_lock(&media->parsed_lock);
765 parsed = media->is_parsed;
766 vlc_mutex_unlock(&media->parsed_lock);
770 /**************************************************************************
771 * Sets media descriptor's user_data. user_data is specialized data
772 * accessed by the host application, VLC.framework uses it as a pointer to
773 * an native object that references a libvlc_media_t pointer
774 **************************************************************************/
776 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
779 p_md->p_user_data = p_new_user_data;
782 /**************************************************************************
783 * Get media descriptor's user_data. user_data is specialized data
784 * accessed by the host application, VLC.framework uses it as a pointer to
785 * an native object that references a libvlc_media_t pointer
786 **************************************************************************/
788 libvlc_media_get_user_data( libvlc_media_t * p_md )
791 return p_md->p_user_data;
794 /**************************************************************************
795 * Get media descriptor's elementary streams description
796 **************************************************************************/
798 libvlc_media_get_tracks_info( libvlc_media_t *p_md, libvlc_media_track_info_t ** pp_es )
802 input_item_t *p_input_item = p_md->p_input_item;
803 vlc_mutex_lock( &p_input_item->lock );
805 const int i_es = p_input_item->i_es;
806 *pp_es = (i_es > 0) ? malloc( i_es * sizeof(libvlc_media_track_info_t) ) : NULL;
808 if( !*pp_es ) /* no ES, or OOM */
810 vlc_mutex_unlock( &p_input_item->lock );
815 for( int i = 0; i < i_es; i++ )
817 libvlc_media_track_info_t *p_mes = *pp_es+i;
818 const es_format_t *p_es = p_input_item->es[i];
820 p_mes->i_codec = p_es->i_codec;
821 p_mes->i_id = p_es->i_id;
823 p_mes->i_profile = p_es->i_profile;
824 p_mes->i_level = p_es->i_level;
830 p_mes->i_type = libvlc_track_unknown;
833 p_mes->i_type = libvlc_track_video;
834 p_mes->u.video.i_height = p_es->video.i_height;
835 p_mes->u.video.i_width = p_es->video.i_width;
838 p_mes->i_type = libvlc_track_audio;
839 p_mes->u.audio.i_channels = p_es->audio.i_channels;
840 p_mes->u.audio.i_rate = p_es->audio.i_rate;
843 p_mes->i_type = libvlc_track_text;
848 vlc_mutex_unlock( &p_input_item->lock );
853 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
857 input_item_t *p_input_item = p_md->p_input_item;
858 vlc_mutex_lock( &p_input_item->lock );
860 const int i_es = p_input_item->i_es;
861 *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
863 if( !*pp_es ) /* no ES, or OOM */
865 vlc_mutex_unlock( &p_input_item->lock );
870 for( int i = 0; i < i_es; i++ )
872 libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
875 p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
876 sizeof(*p_mes->video)),
877 sizeof(*p_mes->subtitle)) );
879 if ( !p_mes || !p_mes->audio )
881 libvlc_media_tracks_release( *pp_es, i_es );
884 vlc_mutex_unlock( &p_input_item->lock );
889 const es_format_t *p_es = p_input_item->es[i];
891 p_mes->i_codec = p_es->i_codec;
892 p_mes->i_original_fourcc = p_es->i_original_fourcc;
893 p_mes->i_id = p_es->i_id;
895 p_mes->i_profile = p_es->i_profile;
896 p_mes->i_level = p_es->i_level;
898 p_mes->i_bitrate = p_es->i_bitrate;
899 p_mes->psz_language = p_es->psz_language != NULL ? strdup(p_es->psz_language) : NULL;
900 p_mes->psz_description = p_es->psz_description != NULL ? strdup(p_es->psz_description) : NULL;
906 p_mes->i_type = libvlc_track_unknown;
909 p_mes->i_type = libvlc_track_video;
910 p_mes->video->i_height = p_es->video.i_height;
911 p_mes->video->i_width = p_es->video.i_width;
912 p_mes->video->i_sar_num = p_es->video.i_sar_num;
913 p_mes->video->i_sar_den = p_es->video.i_sar_den;
914 p_mes->video->i_frame_rate_num = p_es->video.i_frame_rate;
915 p_mes->video->i_frame_rate_den = p_es->video.i_frame_rate_base;
918 p_mes->i_type = libvlc_track_audio;
919 p_mes->audio->i_channels = p_es->audio.i_channels;
920 p_mes->audio->i_rate = p_es->audio.i_rate;
923 p_mes->i_type = libvlc_track_text;
924 p_mes->subtitle->psz_encoding = p_es->subs.psz_encoding != NULL ?
925 strdup(p_es->subs.psz_encoding) : NULL;
930 vlc_mutex_unlock( &p_input_item->lock );
934 /**************************************************************************
935 * Get codec description from media elementary stream
936 **************************************************************************/
938 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
943 case libvlc_track_audio:
944 return vlc_fourcc_GetDescription( AUDIO_ES, i_codec );
945 case libvlc_track_video:
946 return vlc_fourcc_GetDescription( VIDEO_ES, i_codec );
947 case libvlc_track_text:
948 return vlc_fourcc_GetDescription( SPU_ES, i_codec );
949 case libvlc_track_unknown:
951 return vlc_fourcc_GetDescription( UNKNOWN_ES, i_codec );
955 /**************************************************************************
956 * Release media descriptor's elementary streams description array
957 **************************************************************************/
958 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
960 for( unsigned i = 0; i < i_count; ++i )
964 free( p_tracks[i]->psz_language );
965 free( p_tracks[i]->psz_description );
966 switch( p_tracks[i]->i_type )
968 case libvlc_track_audio:
970 case libvlc_track_video:
972 case libvlc_track_text:
973 free( p_tracks[i]->subtitle->psz_encoding );
975 case libvlc_track_unknown:
979 free( p_tracks[i]->audio );