]> git.sesse.net Git - vlc/blob - src/control/media.c
dca579a3d6fffa61ed48e7ce3366efe1dc5b1980
[vlc] / src / control / media.c
1 /*****************************************************************************
2  * media.c: Libvlc API media descripor management
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Pierre d'Herbemont <pdherbemont@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <assert.h>
29
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_media.h>
32 #include <vlc/libvlc_media_list.h> // For the subitems, here for convenience
33 #include <vlc/libvlc_events.h>
34
35 #include <vlc_common.h>
36 #include <vlc_input.h>
37 #include <vlc_meta.h>
38 #include <vlc_playlist.h> /* For the preparser */
39 #include <vlc_url.h>
40
41 #include "libvlc.h"
42
43 #include "libvlc_internal.h"
44 #include "media_internal.h"
45
46 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
47 {
48     [libvlc_meta_Title]        = vlc_meta_Title,
49     [libvlc_meta_Artist]       = vlc_meta_Artist,
50     [libvlc_meta_Genre]        = vlc_meta_Genre,
51     [libvlc_meta_Copyright]    = vlc_meta_Copyright,
52     [libvlc_meta_Album]        = vlc_meta_Album,
53     [libvlc_meta_TrackNumber]  = vlc_meta_TrackNumber,
54     [libvlc_meta_Description]  = vlc_meta_Description,
55     [libvlc_meta_Rating]       = vlc_meta_Rating,
56     [libvlc_meta_Date]         = vlc_meta_Date,
57     [libvlc_meta_Setting]      = vlc_meta_Setting,
58     [libvlc_meta_URL]          = vlc_meta_URL,
59     [libvlc_meta_Language]     = vlc_meta_Language,
60     [libvlc_meta_NowPlaying]   = vlc_meta_NowPlaying,
61     [libvlc_meta_Publisher]    = vlc_meta_Publisher,
62     [libvlc_meta_EncodedBy]    = vlc_meta_EncodedBy,
63     [libvlc_meta_ArtworkURL]   = vlc_meta_ArtworkURL,
64     [libvlc_meta_TrackID]      = vlc_meta_TrackID
65 };
66
67 static const libvlc_meta_t vlc_to_libvlc_meta[] =
68 {
69     [vlc_meta_Title]        = libvlc_meta_Title,
70     [vlc_meta_Artist]       = libvlc_meta_Artist,
71     [vlc_meta_Genre]        = libvlc_meta_Genre,
72     [vlc_meta_Copyright]    = libvlc_meta_Copyright,
73     [vlc_meta_Album]        = libvlc_meta_Album,
74     [vlc_meta_TrackNumber]  = libvlc_meta_TrackNumber,
75     [vlc_meta_Description]  = libvlc_meta_Description,
76     [vlc_meta_Rating]       = libvlc_meta_Rating,
77     [vlc_meta_Date]         = libvlc_meta_Date,
78     [vlc_meta_Setting]      = libvlc_meta_Setting,
79     [vlc_meta_URL]          = libvlc_meta_URL,
80     [vlc_meta_Language]     = libvlc_meta_Language,
81     [vlc_meta_NowPlaying]   = libvlc_meta_NowPlaying,
82     [vlc_meta_Publisher]    = libvlc_meta_Publisher,
83     [vlc_meta_EncodedBy]    = libvlc_meta_EncodedBy,
84     [vlc_meta_ArtworkURL]   = libvlc_meta_ArtworkURL,
85     [vlc_meta_TrackID]      = libvlc_meta_TrackID
86 };
87
88 /**************************************************************************
89  * input_item_subitem_added (Private) (vlc event Callback)
90  **************************************************************************/
91 static void input_item_subitem_added( const vlc_event_t *p_event,
92                                        void * user_data )
93 {
94     libvlc_media_t * p_md = user_data;
95     libvlc_media_t * p_md_child;
96     libvlc_event_t event;
97
98     p_md_child = libvlc_media_new_from_input_item(
99                 p_md->p_libvlc_instance,
100                 p_event->u.input_item_subitem_added.p_new_child );
101
102     /* Add this to our media list */
103     if( !p_md->p_subitems )
104     {
105         p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance );
106         libvlc_media_list_set_media( p_md->p_subitems, p_md );
107     }
108     if( p_md->p_subitems )
109     {
110         libvlc_media_list_add_media( p_md->p_subitems, p_md_child );
111     }
112
113     /* Construct the event */
114     event.type = libvlc_MediaSubItemAdded;
115     event.u.media_subitem_added.new_child = p_md_child;
116
117     /* Send the event */
118     libvlc_event_send( p_md->p_event_manager, &event );
119     libvlc_media_release( p_md_child );
120 }
121
122 /**************************************************************************
123  * input_item_meta_changed (Private) (vlc event Callback)
124  **************************************************************************/
125 static void input_item_meta_changed( const vlc_event_t *p_event,
126                                      void * user_data )
127 {
128     libvlc_media_t * p_md = user_data;
129     libvlc_event_t event;
130
131     /* Construct the event */
132     event.type = libvlc_MediaMetaChanged;
133     event.u.media_meta_changed.meta_type =
134         vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
135
136     /* Send the event */
137     libvlc_event_send( p_md->p_event_manager, &event );
138 }
139
140 /**************************************************************************
141  * input_item_duration_changed (Private) (vlc event Callback)
142  **************************************************************************/
143 static void input_item_duration_changed( const vlc_event_t *p_event,
144                                          void * user_data )
145 {
146     libvlc_media_t * p_md = user_data;
147     libvlc_event_t event;
148
149     /* Construct the event */
150     event.type = libvlc_MediaDurationChanged;
151     event.u.media_duration_changed.new_duration =
152         from_mtime(p_event->u.input_item_duration_changed.new_duration);
153
154     /* Send the event */
155     libvlc_event_send( p_md->p_event_manager, &event );
156 }
157
158 /**************************************************************************
159  * input_item_preparsed_changed (Private) (vlc event Callback)
160  **************************************************************************/
161 static void input_item_preparsed_changed(const vlc_event_t *p_event,
162                                          void * user_data)
163 {
164     libvlc_media_t *media = user_data;
165     libvlc_event_t event;
166
167     /* Eventually notify libvlc_media_parse() */
168     vlc_mutex_lock(&media->parsed_lock);
169     media->is_parsed = true;
170     vlc_cond_broadcast(&media->parsed_cond);
171     vlc_mutex_unlock(&media->parsed_lock);
172
173
174     /* Construct the event */
175     event.type = libvlc_MediaPreparsedChanged;
176     event.u.media_preparsed_changed.new_status =
177         p_event->u.input_item_preparsed_changed.new_status;
178
179     /* Send the event */
180     libvlc_event_send(media->p_event_manager, &event);
181 }
182
183 /**************************************************************************
184  * Install event handler (Private)
185  **************************************************************************/
186 static void install_input_item_observer( libvlc_media_t *p_md )
187 {
188     vlc_event_attach( &p_md->p_input_item->event_manager,
189                       vlc_InputItemSubItemAdded,
190                       input_item_subitem_added,
191                       p_md );
192     vlc_event_attach( &p_md->p_input_item->event_manager,
193                       vlc_InputItemMetaChanged,
194                       input_item_meta_changed,
195                       p_md );
196     vlc_event_attach( &p_md->p_input_item->event_manager,
197                       vlc_InputItemDurationChanged,
198                       input_item_duration_changed,
199                       p_md );
200     vlc_event_attach( &p_md->p_input_item->event_manager,
201                       vlc_InputItemPreparsedChanged,
202                       input_item_preparsed_changed,
203                       p_md );
204 }
205
206 /**************************************************************************
207  * Uninstall event handler (Private)
208  **************************************************************************/
209 static void uninstall_input_item_observer( libvlc_media_t *p_md )
210 {
211     vlc_event_detach( &p_md->p_input_item->event_manager,
212                       vlc_InputItemSubItemAdded,
213                       input_item_subitem_added,
214                       p_md );
215     vlc_event_detach( &p_md->p_input_item->event_manager,
216                       vlc_InputItemMetaChanged,
217                       input_item_meta_changed,
218                       p_md );
219     vlc_event_detach( &p_md->p_input_item->event_manager,
220                       vlc_InputItemDurationChanged,
221                       input_item_duration_changed,
222                       p_md );
223     vlc_event_detach( &p_md->p_input_item->event_manager,
224                       vlc_InputItemPreparsedChanged,
225                       input_item_preparsed_changed,
226                       p_md );
227 }
228
229 /**************************************************************************
230  * Preparse if not already done (Private)
231  **************************************************************************/
232 static void preparse_if_needed( libvlc_media_t *p_md )
233 {
234     /* XXX: need some locking here */
235     if (!p_md->has_asked_preparse)
236     {
237         playlist_PreparseEnqueue(
238                 libvlc_priv (p_md->p_libvlc_instance->p_libvlc_int)->p_playlist,
239                 p_md->p_input_item );
240         p_md->has_asked_preparse = true;
241     }
242 }
243
244 /**************************************************************************
245  * Create a new media descriptor object from an input_item
246  * (libvlc internal)
247  * That's the generic constructor
248  **************************************************************************/
249 libvlc_media_t * libvlc_media_new_from_input_item(
250                                    libvlc_instance_t *p_instance,
251                                    input_item_t *p_input_item )
252 {
253     libvlc_media_t * p_md;
254
255     if (!p_input_item)
256     {
257         libvlc_printerr( "No input item given" );
258         return NULL;
259     }
260
261     p_md = calloc( 1, sizeof(libvlc_media_t) );
262     if( !p_md )
263     {
264         libvlc_printerr( "Not enough memory" );
265         return NULL;
266     }
267
268     p_md->p_libvlc_instance = p_instance;
269     p_md->p_input_item      = p_input_item;
270     p_md->i_refcount        = 1;
271
272     vlc_cond_init(&p_md->parsed_cond);
273     vlc_mutex_init(&p_md->parsed_lock);
274
275     p_md->state = libvlc_NothingSpecial;
276
277     /* A media descriptor can be a playlist. When you open a playlist
278      * It can give a bunch of item to read. */
279     p_md->p_subitems        = NULL;
280
281     p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance );
282     if( unlikely(p_md->p_event_manager == NULL) )
283     {
284         free(p_md);
285         return NULL;
286     }
287     libvlc_event_manager_register_event_type( p_md->p_event_manager,
288         libvlc_MediaMetaChanged );
289     libvlc_event_manager_register_event_type( p_md->p_event_manager,
290         libvlc_MediaSubItemAdded );
291     libvlc_event_manager_register_event_type( p_md->p_event_manager,
292         libvlc_MediaFreed );
293     libvlc_event_manager_register_event_type( p_md->p_event_manager,
294         libvlc_MediaDurationChanged );
295     libvlc_event_manager_register_event_type( p_md->p_event_manager,
296         libvlc_MediaStateChanged );
297     libvlc_event_manager_register_event_type( p_md->p_event_manager,
298         libvlc_MediaPreparsedChanged );
299
300     vlc_gc_incref( p_md->p_input_item );
301
302     install_input_item_observer( p_md );
303
304     return p_md;
305 }
306
307 /**************************************************************************
308  * Create a new media descriptor object
309  **************************************************************************/
310 libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
311                                            const char * psz_mrl )
312 {
313     input_item_t * p_input_item;
314     libvlc_media_t * p_md;
315
316     p_input_item = input_item_New( p_instance->p_libvlc_int, psz_mrl, NULL );
317
318     if (!p_input_item)
319     {
320         libvlc_printerr( "Not enough memory" );
321         return NULL;
322     }
323
324     p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
325
326     /* The p_input_item is retained in libvlc_media_new_from_input_item */
327     vlc_gc_decref( p_input_item );
328
329     return p_md;
330 }
331
332 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
333                                        const char *path )
334 {
335     char *mrl = make_URI( path );
336     if( unlikely(mrl == NULL) )
337     {
338         libvlc_printerr( "Not enough memory" );
339         return NULL;
340     }
341
342     libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
343     free( mrl );
344     return m;
345 }
346
347 /**************************************************************************
348  * Create a new media descriptor object
349  **************************************************************************/
350 libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
351                                            const char * psz_name )
352 {
353     input_item_t * p_input_item;
354     libvlc_media_t * p_md;
355
356     p_input_item = input_item_New( p_instance->p_libvlc_int, "vlc://nop", psz_name );
357
358     if (!p_input_item)
359     {
360         libvlc_printerr( "Not enough memory" );
361         return NULL;
362     }
363
364     p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
365
366     p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance );
367
368     return p_md;
369 }
370
371 /**************************************************************************
372  * Add an option to the media descriptor,
373  * that will be used to determine how the media_player will read the
374  * media. This allow to use VLC advanced reading/streaming
375  * options in a per-media basis
376  *
377  * The options are detailled in vlc --long-help, for instance "--sout-all"
378  **************************************************************************/
379 void libvlc_media_add_option( libvlc_media_t * p_md,
380                               const char * psz_option )
381 {
382     libvlc_media_add_option_flag( p_md, psz_option,
383                           VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
384 }
385
386 /**************************************************************************
387  * Same as libvlc_media_add_option but with configurable flags.
388  **************************************************************************/
389 void libvlc_media_add_option_flag( libvlc_media_t * p_md,
390                                    const char * ppsz_option,
391                                    unsigned i_flags )
392 {
393     input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
394 }
395
396 /**************************************************************************
397  * Delete a media descriptor object
398  **************************************************************************/
399 void libvlc_media_release( libvlc_media_t *p_md )
400 {
401     if (!p_md)
402         return;
403
404     p_md->i_refcount--;
405
406     if( p_md->i_refcount > 0 )
407         return;
408
409     if( p_md->p_subitems )
410         libvlc_media_list_release( p_md->p_subitems );
411
412     uninstall_input_item_observer( p_md );
413     vlc_gc_decref( p_md->p_input_item );
414
415     /* Construct the event */
416     libvlc_event_t event;
417     event.type = libvlc_MediaFreed;
418     event.u.media_freed.md = p_md;
419
420     /* Send the event */
421     libvlc_event_send( p_md->p_event_manager, &event );
422
423     libvlc_event_manager_release( p_md->p_event_manager );
424
425     free( p_md );
426 }
427
428 /**************************************************************************
429  * Retain a media descriptor object
430  **************************************************************************/
431 void libvlc_media_retain( libvlc_media_t *p_md )
432 {
433     assert (p_md);
434     p_md->i_refcount++;
435 }
436
437 /**************************************************************************
438  * Duplicate a media descriptor object
439  **************************************************************************/
440 libvlc_media_t *
441 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
442 {
443     return libvlc_media_new_from_input_item(
444         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item );
445 }
446
447 /**************************************************************************
448  * Get mrl from a media descriptor object
449  **************************************************************************/
450 char *
451 libvlc_media_get_mrl( libvlc_media_t * p_md )
452 {
453     assert( p_md );
454     return input_item_GetURI( p_md->p_input_item );
455 }
456
457 /**************************************************************************
458  * Getter for meta information
459  **************************************************************************/
460
461 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
462 {
463     char * psz_meta;
464
465     assert( p_md );
466     /* XXX: locking */
467
468     preparse_if_needed( p_md );
469
470     psz_meta = input_item_GetMeta( p_md->p_input_item,
471                                    libvlc_to_vlc_meta[e_meta] );
472
473     if( e_meta == libvlc_meta_ArtworkURL && !psz_meta && !p_md->has_asked_art )
474     {
475         p_md->has_asked_art = true;
476         playlist_AskForArtEnqueue(
477                 libvlc_priv(p_md->p_libvlc_instance->p_libvlc_int)->p_playlist,
478                 p_md->p_input_item );
479     }
480
481     /* Should be integrated in core */
482     if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
483     {
484         free( psz_meta );
485         return strdup( p_md->p_input_item->psz_name );
486     }
487
488     return psz_meta;
489 }
490
491 /**************************************************************************
492  * Setter for meta information
493  **************************************************************************/
494
495 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
496 {
497     assert( p_md );
498     input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
499 }
500
501 int libvlc_media_save_meta( libvlc_media_t *p_md )
502 {
503     assert( p_md );
504     vlc_object_t *p_obj = VLC_OBJECT(libvlc_priv(
505                             p_md->p_libvlc_instance->p_libvlc_int)->p_playlist);
506     return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
507 }
508
509 /**************************************************************************
510  * Getter for state information
511  * Can be error, playing, buffering, NothingSpecial.
512  **************************************************************************/
513
514 libvlc_state_t
515 libvlc_media_get_state( libvlc_media_t *p_md )
516 {
517     assert( p_md );
518     return p_md->state;
519 }
520
521 /**************************************************************************
522  * Setter for state information (LibVLC Internal)
523  **************************************************************************/
524
525 void
526 libvlc_media_set_state( libvlc_media_t *p_md,
527                                    libvlc_state_t state )
528 {
529     libvlc_event_t event;
530
531     p_md->state = state;
532
533     /* Construct the event */
534     event.type = libvlc_MediaStateChanged;
535     event.u.media_state_changed.new_state = state;
536
537     /* Send the event */
538     libvlc_event_send( p_md->p_event_manager, &event );
539 }
540
541 /**************************************************************************
542  * subitems
543  **************************************************************************/
544 libvlc_media_list_t *
545 libvlc_media_subitems( libvlc_media_t * p_md )
546 {
547     if( p_md->p_subitems )
548         libvlc_media_list_retain( p_md->p_subitems );
549     return p_md->p_subitems;
550 }
551
552 /**************************************************************************
553  * Getter for statistics information
554  **************************************************************************/
555 int libvlc_media_get_stats( libvlc_media_t *p_md,
556                             libvlc_media_stats_t *p_stats )
557 {
558     if( !p_md->p_input_item )
559         return false;
560
561     input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
562     vlc_mutex_lock( &p_itm_stats->lock );
563     p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
564     p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
565
566     p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
567     p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
568     p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
569     p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
570
571     p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
572     p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
573
574     p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
575     p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
576
577     p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
578     p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
579
580     p_stats->i_sent_packets = p_itm_stats->i_sent_packets;
581     p_stats->i_sent_bytes = p_itm_stats->i_sent_bytes;
582     p_stats->f_send_bitrate = p_itm_stats->f_send_bitrate;
583     vlc_mutex_unlock( &p_itm_stats->lock );
584     return true;
585 }
586
587 /**************************************************************************
588  * event_manager
589  **************************************************************************/
590 libvlc_event_manager_t *
591 libvlc_media_event_manager( libvlc_media_t * p_md )
592 {
593     assert( p_md );
594
595     return p_md->p_event_manager;
596 }
597
598 /**************************************************************************
599  * Get duration of media object (in ms)
600  **************************************************************************/
601 int64_t
602 libvlc_media_get_duration( libvlc_media_t * p_md )
603 {
604     assert( p_md );
605
606     if( !p_md->p_input_item )
607     {
608         libvlc_printerr( "No input item" );
609         return -1;
610     }
611
612     preparse_if_needed( p_md );
613
614     if (!input_item_IsPreparsed( p_md->p_input_item ))
615         return -1;
616
617     return from_mtime(input_item_GetDuration( p_md->p_input_item ));
618 }
619
620 /**************************************************************************
621  * Parse the media.
622  **************************************************************************/
623 void
624 libvlc_media_parse(libvlc_media_t *media)
625 {
626     preparse_if_needed(media);
627
628     vlc_mutex_lock(&media->parsed_lock);
629     while (!media->is_parsed)
630         vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
631     vlc_mutex_unlock(&media->parsed_lock);
632 }
633
634 /**************************************************************************
635  * Parse the media.
636  **************************************************************************/
637 void
638 libvlc_media_parse_async(libvlc_media_t *media)
639 {
640     preparse_if_needed(media);
641 }
642
643 /**************************************************************************
644  * Get preparsed status for media object.
645  **************************************************************************/
646 int
647 libvlc_media_is_preparsed( libvlc_media_t * p_md )
648 {
649     assert( p_md );
650
651     if( !p_md->p_input_item )
652         return false;
653
654     return input_item_IsPreparsed( p_md->p_input_item );
655 }
656
657 /**************************************************************************
658  * Sets media descriptor's user_data. user_data is specialized data
659  * accessed by the host application, VLC.framework uses it as a pointer to
660  * an native object that references a libvlc_media_t pointer
661  **************************************************************************/
662 void
663 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
664 {
665     assert( p_md );
666     p_md->p_user_data = p_new_user_data;
667 }
668
669 /**************************************************************************
670  * Get media descriptor's user_data. user_data is specialized data
671  * accessed by the host application, VLC.framework uses it as a pointer to
672  * an native object that references a libvlc_media_t pointer
673  **************************************************************************/
674 void *
675 libvlc_media_get_user_data( libvlc_media_t * p_md )
676 {
677     assert( p_md );
678     return p_md->p_user_data;
679 }
680
681 /**************************************************************************
682  * Get media descriptor's elementary streams description
683  **************************************************************************/
684 int
685 libvlc_media_get_tracks_info( libvlc_media_t *p_md, libvlc_media_track_info_t ** pp_es )
686 {
687     assert( p_md );
688
689     input_item_t *p_input_item = p_md->p_input_item;
690     vlc_mutex_lock( &p_input_item->lock );
691
692     const int i_es = p_input_item->i_es;
693     *pp_es = (i_es > 0) ? malloc( i_es * sizeof(libvlc_media_track_info_t) ) : NULL;
694
695     if( !pp_es ) /* no ES, or OOM */
696     {
697         vlc_mutex_unlock( &p_input_item->lock );
698         return 0;
699     }
700
701     /* Fill array */
702     for( int i = 0; i < i_es; i++ )
703     {
704         libvlc_media_track_info_t *p_mes = *pp_es+i;
705         const es_format_t *p_es = p_input_item->es[i];
706
707         p_mes->i_channels = p_mes->i_rate = 0;
708         p_mes->i_width = p_mes->i_height = 0;
709
710
711         p_mes->i_codec = p_es->i_codec;
712         p_mes->i_id = p_es->i_id;
713
714         p_mes->i_profile = p_es->i_profile;
715         p_mes->i_level = p_es->i_level;
716
717         switch(p_es->i_cat)
718         {
719         case UNKNOWN_ES:
720         default:
721             p_mes->i_type = libvlc_track_unknown;
722             break;
723         case VIDEO_ES:
724             p_mes->i_type = libvlc_track_video;
725             p_mes->i_height = p_es->video.i_height;
726             p_mes->i_width = p_es->video.i_width;
727             break;
728         case AUDIO_ES:
729             p_mes->i_type = libvlc_track_audio;
730             p_mes->i_channels = p_es->audio.i_channels;
731             p_mes->i_rate = p_es->audio.i_rate;
732             break;
733         case SPU_ES:
734             p_mes->i_type = libvlc_track_text;
735             break;
736         }
737     }
738
739     vlc_mutex_unlock( &p_input_item->lock );
740     return i_es;
741 }