]> git.sesse.net Git - vlc/blob - lib/media.c
Fix potential memleak (cid #1047282)
[vlc] / lib / media.c
1 /*****************************************************************************
2  * media.c: Libvlc API media descripor management
3  *****************************************************************************
4  * Copyright (C) 2007 VLC authors and VideoLAN
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 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.
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 Lesser General Public License for more details.
18  *
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  *****************************************************************************/
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 "../src/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_MediaParsedChanged;
176     event.u.media_parsed_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  * Create a new media descriptor object from an input_item
231  * (libvlc internal)
232  * That's the generic constructor
233  **************************************************************************/
234 libvlc_media_t * libvlc_media_new_from_input_item(
235                                    libvlc_instance_t *p_instance,
236                                    input_item_t *p_input_item )
237 {
238     libvlc_media_t * p_md;
239
240     if (!p_input_item)
241     {
242         libvlc_printerr( "No input item given" );
243         return NULL;
244     }
245
246     p_md = calloc( 1, sizeof(libvlc_media_t) );
247     if( !p_md )
248     {
249         libvlc_printerr( "Not enough memory" );
250         return NULL;
251     }
252
253     p_md->p_libvlc_instance = p_instance;
254     p_md->p_input_item      = p_input_item;
255     p_md->i_refcount        = 1;
256
257     vlc_cond_init(&p_md->parsed_cond);
258     vlc_mutex_init(&p_md->parsed_lock);
259
260     p_md->state = libvlc_NothingSpecial;
261
262     /* A media descriptor can be a playlist. When you open a playlist
263      * It can give a bunch of item to read. */
264     p_md->p_subitems        = NULL;
265
266     p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance );
267     if( unlikely(p_md->p_event_manager == NULL) )
268     {
269         free(p_md);
270         return NULL;
271     }
272
273     libvlc_event_manager_t *em = p_md->p_event_manager;
274     libvlc_event_manager_register_event_type(em, libvlc_MediaMetaChanged);
275     libvlc_event_manager_register_event_type(em, libvlc_MediaSubItemAdded);
276     libvlc_event_manager_register_event_type(em, libvlc_MediaFreed);
277     libvlc_event_manager_register_event_type(em, libvlc_MediaDurationChanged);
278     libvlc_event_manager_register_event_type(em, libvlc_MediaStateChanged);
279     libvlc_event_manager_register_event_type(em, libvlc_MediaParsedChanged);
280
281     vlc_gc_incref( p_md->p_input_item );
282
283     install_input_item_observer( p_md );
284
285     return p_md;
286 }
287
288 /**************************************************************************
289  * Create a new media descriptor object
290  **************************************************************************/
291 libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
292                                            const char * psz_mrl )
293 {
294     input_item_t * p_input_item;
295     libvlc_media_t * p_md;
296
297     p_input_item = input_item_New( psz_mrl, NULL );
298
299     if (!p_input_item)
300     {
301         libvlc_printerr( "Not enough memory" );
302         return NULL;
303     }
304
305     p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
306
307     /* The p_input_item is retained in libvlc_media_new_from_input_item */
308     vlc_gc_decref( p_input_item );
309
310     return p_md;
311 }
312
313 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
314                                        const char *path )
315 {
316     char *mrl = vlc_path2uri( path, NULL );
317     if( unlikely(mrl == NULL) )
318     {
319         libvlc_printerr( "Not enough memory" );
320         return NULL;
321     }
322
323     libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
324     free( mrl );
325     return m;
326 }
327
328 libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
329 {
330     char mrl[16];
331     snprintf( mrl, sizeof(mrl), "fd://%d", fd );
332
333     return libvlc_media_new_location( p_instance, mrl );
334 }
335
336 /**************************************************************************
337  * Create a new media descriptor object
338  **************************************************************************/
339 libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
340                                            const char * psz_name )
341 {
342     input_item_t * p_input_item;
343     libvlc_media_t * p_md;
344
345     p_input_item = input_item_New( "vlc://nop", psz_name );
346
347     if (!p_input_item)
348     {
349         libvlc_printerr( "Not enough memory" );
350         return NULL;
351     }
352
353     p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
354
355     p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance );
356
357     return p_md;
358 }
359
360 /**************************************************************************
361  * Add an option to the media descriptor,
362  * that will be used to determine how the media_player will read the
363  * media. This allow to use VLC advanced reading/streaming
364  * options in a per-media basis
365  *
366  * The options are detailled in vlc --long-help, for instance "--sout-all"
367  **************************************************************************/
368 void libvlc_media_add_option( libvlc_media_t * p_md,
369                               const char * psz_option )
370 {
371     libvlc_media_add_option_flag( p_md, psz_option,
372                           VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
373 }
374
375 /**************************************************************************
376  * Same as libvlc_media_add_option but with configurable flags.
377  **************************************************************************/
378 void libvlc_media_add_option_flag( libvlc_media_t * p_md,
379                                    const char * ppsz_option,
380                                    unsigned i_flags )
381 {
382     input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
383 }
384
385 /**************************************************************************
386  * Delete a media descriptor object
387  **************************************************************************/
388 void libvlc_media_release( libvlc_media_t *p_md )
389 {
390     if (!p_md)
391         return;
392
393     p_md->i_refcount--;
394
395     if( p_md->i_refcount > 0 )
396         return;
397
398     if( p_md->p_subitems )
399         libvlc_media_list_release( p_md->p_subitems );
400
401     uninstall_input_item_observer( p_md );
402     vlc_gc_decref( p_md->p_input_item );
403
404     vlc_cond_destroy( &p_md->parsed_cond );
405     vlc_mutex_destroy( &p_md->parsed_lock );
406
407     /* Construct the event */
408     libvlc_event_t event;
409     event.type = libvlc_MediaFreed;
410     event.u.media_freed.md = p_md;
411
412     /* Send the event */
413     libvlc_event_send( p_md->p_event_manager, &event );
414
415     libvlc_event_manager_release( p_md->p_event_manager );
416
417     free( p_md );
418 }
419
420 /**************************************************************************
421  * Retain a media descriptor object
422  **************************************************************************/
423 void libvlc_media_retain( libvlc_media_t *p_md )
424 {
425     assert (p_md);
426     p_md->i_refcount++;
427 }
428
429 /**************************************************************************
430  * Duplicate a media descriptor object
431  **************************************************************************/
432 libvlc_media_t *
433 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
434 {
435     return libvlc_media_new_from_input_item(
436         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item );
437 }
438
439 /**************************************************************************
440  * Get mrl from a media descriptor object
441  **************************************************************************/
442 char *
443 libvlc_media_get_mrl( libvlc_media_t * p_md )
444 {
445     assert( p_md );
446     return input_item_GetURI( p_md->p_input_item );
447 }
448
449 /**************************************************************************
450  * Getter for meta information
451  **************************************************************************/
452
453 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
454 {
455     char *psz_meta = input_item_GetMeta( p_md->p_input_item,
456                                          libvlc_to_vlc_meta[e_meta] );
457     /* Should be integrated in core */
458     if( psz_meta == NULL && e_meta == libvlc_meta_Title
459      && p_md->p_input_item->psz_name != NULL )
460         psz_meta = strdup( p_md->p_input_item->psz_name );
461
462     return psz_meta;
463 }
464
465 /**************************************************************************
466  * Setter for meta information
467  **************************************************************************/
468
469 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
470 {
471     assert( p_md );
472     input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
473 }
474
475 int libvlc_media_save_meta( libvlc_media_t *p_md )
476 {
477     assert( p_md );
478     vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
479     return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
480 }
481
482 /**************************************************************************
483  * Getter for state information
484  * Can be error, playing, buffering, NothingSpecial.
485  **************************************************************************/
486
487 libvlc_state_t
488 libvlc_media_get_state( libvlc_media_t *p_md )
489 {
490     assert( p_md );
491     return p_md->state;
492 }
493
494 /**************************************************************************
495  * Setter for state information (LibVLC Internal)
496  **************************************************************************/
497
498 void
499 libvlc_media_set_state( libvlc_media_t *p_md,
500                                    libvlc_state_t state )
501 {
502     libvlc_event_t event;
503
504     p_md->state = state;
505
506     /* Construct the event */
507     event.type = libvlc_MediaStateChanged;
508     event.u.media_state_changed.new_state = state;
509
510     /* Send the event */
511     libvlc_event_send( p_md->p_event_manager, &event );
512 }
513
514 /**************************************************************************
515  * subitems
516  **************************************************************************/
517 libvlc_media_list_t *
518 libvlc_media_subitems( libvlc_media_t * p_md )
519 {
520     if( p_md->p_subitems )
521         libvlc_media_list_retain( p_md->p_subitems );
522     return p_md->p_subitems;
523 }
524
525 /**************************************************************************
526  * Getter for statistics information
527  **************************************************************************/
528 int libvlc_media_get_stats( libvlc_media_t *p_md,
529                             libvlc_media_stats_t *p_stats )
530 {
531     if( !p_md->p_input_item )
532         return false;
533
534     input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
535     vlc_mutex_lock( &p_itm_stats->lock );
536     p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
537     p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
538
539     p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
540     p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
541     p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
542     p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
543
544     p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
545     p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
546
547     p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
548     p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
549
550     p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
551     p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
552
553     p_stats->i_sent_packets = p_itm_stats->i_sent_packets;
554     p_stats->i_sent_bytes = p_itm_stats->i_sent_bytes;
555     p_stats->f_send_bitrate = p_itm_stats->f_send_bitrate;
556     vlc_mutex_unlock( &p_itm_stats->lock );
557     return true;
558 }
559
560 /**************************************************************************
561  * event_manager
562  **************************************************************************/
563 libvlc_event_manager_t *
564 libvlc_media_event_manager( libvlc_media_t * p_md )
565 {
566     assert( p_md );
567
568     return p_md->p_event_manager;
569 }
570
571 /**************************************************************************
572  * Get duration of media object (in ms)
573  **************************************************************************/
574 int64_t
575 libvlc_media_get_duration( libvlc_media_t * p_md )
576 {
577     assert( p_md );
578
579     if( !p_md->p_input_item )
580     {
581         libvlc_printerr( "No input item" );
582         return -1;
583     }
584
585     if (!input_item_IsPreparsed( p_md->p_input_item ))
586         return -1;
587
588     return from_mtime(input_item_GetDuration( p_md->p_input_item ));
589 }
590
591 static int media_parse(libvlc_media_t *media)
592 {
593     /* TODO: fetcher and parser independent of playlist */
594 #warning FIXME: remove pl_Get
595     playlist_t *playlist = pl_Get(media->p_libvlc_instance->p_libvlc_int);
596
597     /* TODO: Fetch art on need basis. But how not to break compatibility? */
598     playlist_AskForArtEnqueue(playlist, media->p_input_item );
599     return playlist_PreparseEnqueue(playlist, media->p_input_item);
600 }
601
602 /**************************************************************************
603  * Parse the media and wait.
604  **************************************************************************/
605 void
606 libvlc_media_parse(libvlc_media_t *media)
607 {
608     vlc_mutex_lock(&media->parsed_lock);
609     if (!media->has_asked_preparse)
610     {
611         media->has_asked_preparse = true;
612         vlc_mutex_unlock(&media->parsed_lock);
613
614         if (media_parse(media))
615             /* Parse failed: do not wait! */
616             return;
617         vlc_mutex_lock(&media->parsed_lock);
618     }
619
620     while (!media->is_parsed)
621         vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
622     vlc_mutex_unlock(&media->parsed_lock);
623 }
624
625 /**************************************************************************
626  * Parse the media but do not wait.
627  **************************************************************************/
628 void
629 libvlc_media_parse_async(libvlc_media_t *media)
630 {
631     bool needed;
632
633     vlc_mutex_lock(&media->parsed_lock);
634     needed = !media->has_asked_preparse;
635     media->has_asked_preparse = true;
636     vlc_mutex_unlock(&media->parsed_lock);
637
638     if (needed)
639         media_parse(media);
640 }
641
642 /**************************************************************************
643  * Get parsed status for media object.
644  **************************************************************************/
645 int
646 libvlc_media_is_parsed(libvlc_media_t *media)
647 {
648     bool parsed;
649
650     vlc_mutex_lock(&media->parsed_lock);
651     parsed = media->is_parsed;
652     vlc_mutex_unlock(&media->parsed_lock);
653     return parsed;
654 }
655
656 /**************************************************************************
657  * Sets media descriptor's user_data. user_data is specialized data
658  * accessed by the host application, VLC.framework uses it as a pointer to
659  * an native object that references a libvlc_media_t pointer
660  **************************************************************************/
661 void
662 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
663 {
664     assert( p_md );
665     p_md->p_user_data = p_new_user_data;
666 }
667
668 /**************************************************************************
669  * Get media descriptor's user_data. user_data is specialized data
670  * accessed by the host application, VLC.framework uses it as a pointer to
671  * an native object that references a libvlc_media_t pointer
672  **************************************************************************/
673 void *
674 libvlc_media_get_user_data( libvlc_media_t * p_md )
675 {
676     assert( p_md );
677     return p_md->p_user_data;
678 }
679
680 /**************************************************************************
681  * Get media descriptor's elementary streams description
682  **************************************************************************/
683 int
684 libvlc_media_get_tracks_info( libvlc_media_t *p_md, libvlc_media_track_info_t ** pp_es )
685 {
686     assert( p_md );
687
688     input_item_t *p_input_item = p_md->p_input_item;
689     vlc_mutex_lock( &p_input_item->lock );
690
691     const int i_es = p_input_item->i_es;
692     *pp_es = (i_es > 0) ? malloc( i_es * sizeof(libvlc_media_track_info_t) ) : NULL;
693
694     if( !*pp_es ) /* no ES, or OOM */
695     {
696         vlc_mutex_unlock( &p_input_item->lock );
697         return 0;
698     }
699
700     /* Fill array */
701     for( int i = 0; i < i_es; i++ )
702     {
703         libvlc_media_track_info_t *p_mes = *pp_es+i;
704         const es_format_t *p_es = p_input_item->es[i];
705
706         p_mes->i_codec = p_es->i_codec;
707         p_mes->i_id = p_es->i_id;
708
709         p_mes->i_profile = p_es->i_profile;
710         p_mes->i_level = p_es->i_level;
711
712         switch(p_es->i_cat)
713         {
714         case UNKNOWN_ES:
715         default:
716             p_mes->i_type = libvlc_track_unknown;
717             break;
718         case VIDEO_ES:
719             p_mes->i_type = libvlc_track_video;
720             p_mes->u.video.i_height = p_es->video.i_height;
721             p_mes->u.video.i_width = p_es->video.i_width;
722             break;
723         case AUDIO_ES:
724             p_mes->i_type = libvlc_track_audio;
725             p_mes->u.audio.i_channels = p_es->audio.i_channels;
726             p_mes->u.audio.i_rate = p_es->audio.i_rate;
727             break;
728         case SPU_ES:
729             p_mes->i_type = libvlc_track_text;
730             break;
731         }
732     }
733
734     vlc_mutex_unlock( &p_input_item->lock );
735     return i_es;
736 }
737
738 unsigned
739 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
740 {
741     assert( p_md );
742
743     input_item_t *p_input_item = p_md->p_input_item;
744     vlc_mutex_lock( &p_input_item->lock );
745
746     const int i_es = p_input_item->i_es;
747     *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
748
749     if( !*pp_es ) /* no ES, or OOM */
750     {
751         vlc_mutex_unlock( &p_input_item->lock );
752         return 0;
753     }
754
755     /* Fill array */
756     for( int i = 0; i < i_es; i++ )
757     {
758         libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
759         if ( p_mes )
760         {
761             p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
762                                                sizeof(*p_mes->video)),
763                                                sizeof(*p_mes->subtitle)) );
764         }
765         if ( !p_mes || !p_mes->audio )
766         {
767             libvlc_media_tracks_release( *pp_es, i_es );
768             *pp_es = NULL;
769             free( p_mes );
770             return 0;
771         }
772         (*pp_es)[i] = p_mes;
773
774         const es_format_t *p_es = p_input_item->es[i];
775
776         p_mes->i_codec = p_es->i_codec;
777         p_mes->i_original_fourcc = p_es->i_original_fourcc;
778         p_mes->i_id = p_es->i_id;
779
780         p_mes->i_profile = p_es->i_profile;
781         p_mes->i_level = p_es->i_level;
782
783         p_mes->i_bitrate = p_es->i_bitrate;
784         p_mes->psz_language = p_es->psz_language != NULL ? strdup(p_es->psz_language) : NULL;
785         p_mes->psz_description = p_es->psz_description != NULL ? strdup(p_es->psz_description) : NULL;
786
787         switch(p_es->i_cat)
788         {
789         case UNKNOWN_ES:
790         default:
791             p_mes->i_type = libvlc_track_unknown;
792             break;
793         case VIDEO_ES:
794             p_mes->i_type = libvlc_track_video;
795             p_mes->video->i_height = p_es->video.i_height;
796             p_mes->video->i_width = p_es->video.i_width;
797             p_mes->video->i_sar_num = p_es->video.i_sar_num;
798             p_mes->video->i_sar_den = p_es->video.i_sar_den;
799             p_mes->video->i_frame_rate_num = p_es->video.i_frame_rate;
800             p_mes->video->i_frame_rate_den = p_es->video.i_frame_rate_base;
801             break;
802         case AUDIO_ES:
803             p_mes->i_type = libvlc_track_audio;
804             p_mes->audio->i_channels = p_es->audio.i_channels;
805             p_mes->audio->i_rate = p_es->audio.i_rate;
806             break;
807         case SPU_ES:
808             p_mes->i_type = libvlc_track_text;
809             p_mes->subtitle->psz_encoding = p_es->subs.psz_encoding != NULL ?
810                                             strdup(p_es->subs.psz_encoding) : NULL;
811             break;
812         }
813     }
814
815     vlc_mutex_unlock( &p_input_item->lock );
816     return i_es;
817 }
818
819
820 /**************************************************************************
821  * Release media descriptor's elementary streams description array
822  **************************************************************************/
823 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
824 {
825     for( unsigned i = 0; i < i_count; ++i )
826     {
827         if ( !p_tracks[i] )
828             continue;
829         free( p_tracks[i]->psz_language );
830         free( p_tracks[i]->psz_description );
831         switch( p_tracks[i]->i_type )
832         {
833         case libvlc_track_audio:
834             break;
835         case libvlc_track_video:
836             break;
837         case libvlc_track_text:
838             free( p_tracks[i]->subtitle->psz_encoding );
839             break;
840         case libvlc_track_unknown:
841         default:
842             break;
843         }
844         free( p_tracks[i]->audio );
845         free( p_tracks[i] );
846     }
847     free( p_tracks );
848 }