libvlc: add libvlc_media_parse_with_options
[vlc.git] / 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 #include <errno.h>
30
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>
35
36 #include <vlc_common.h>
37 #include <vlc_input.h>
38 #include <vlc_meta.h>
39 #include <vlc_playlist.h> /* For the preparser */
40 #include <vlc_url.h>
41
42 #include "../src/libvlc.h"
43
44 #include "libvlc_internal.h"
45 #include "media_internal.h"
46 #include "media_list_internal.h"
47
48 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
49 {
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 };
76
77 static const libvlc_meta_t vlc_to_libvlc_meta[] =
78 {
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
105 };
106
107 static libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md )
108 {
109     libvlc_media_list_t *p_subitems = NULL;
110
111     vlc_mutex_lock( &p_md->subitems_lock );
112     if( p_md->p_subitems == NULL )
113     {
114         p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance );
115         if( p_md->p_subitems != NULL )
116         {
117             p_md->p_subitems->b_read_only = true;
118             p_md->p_subitems->p_internal_md = p_md;
119         }
120     }
121     p_subitems = p_md->p_subitems;
122     vlc_mutex_unlock( &p_md->subitems_lock );
123     return p_subitems;
124 }
125
126 /**************************************************************************
127  * input_item_subitem_added (Private) (vlc event Callback)
128  **************************************************************************/
129 static void input_item_subitem_added( const vlc_event_t *p_event,
130                                        void * user_data )
131 {
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;
136
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 );
140
141     /* Add this to our media list */
142     p_subitems = media_get_subitems( p_md );
143     if( p_subitems != NULL )
144     {
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 );
148     }
149
150     /* Construct the event */
151     event.type = libvlc_MediaSubItemAdded;
152     event.u.media_subitem_added.new_child = p_md_child;
153
154     /* Send the event */
155     libvlc_event_send( p_md->p_event_manager, &event );
156     libvlc_media_release( p_md_child );
157 }
158
159 /**************************************************************************
160  * input_item_subitemtree_added (Private) (vlc event Callback)
161  **************************************************************************/
162 static void input_item_subitemtree_added( const vlc_event_t * p_event,
163                                           void * user_data )
164 {
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;
169
170     /* notify the media list */
171     p_subitems = media_get_subitems( p_md );
172     if( p_subitems != NULL )
173     {
174         libvlc_media_list_lock( p_subitems );
175         libvlc_media_list_internal_end_reached( p_subitems );
176         libvlc_media_list_unlock( p_subitems );
177     }
178
179     /* Construct the event */
180     event.type = libvlc_MediaSubItemTreeAdded;
181     event.u.media_subitemtree_added.item = p_md;
182
183     /* Send the event */
184     libvlc_event_send( p_md->p_event_manager, &event );
185 }
186
187 /**************************************************************************
188  * input_item_meta_changed (Private) (vlc event Callback)
189  **************************************************************************/
190 static void input_item_meta_changed( const vlc_event_t *p_event,
191                                      void * user_data )
192 {
193     libvlc_media_t * p_md = user_data;
194     libvlc_event_t event;
195
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];
200
201     /* Send the event */
202     libvlc_event_send( p_md->p_event_manager, &event );
203 }
204
205 /**************************************************************************
206  * input_item_duration_changed (Private) (vlc event Callback)
207  **************************************************************************/
208 static void input_item_duration_changed( const vlc_event_t *p_event,
209                                          void * user_data )
210 {
211     libvlc_media_t * p_md = user_data;
212     libvlc_event_t event;
213
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);
218
219     /* Send the event */
220     libvlc_event_send( p_md->p_event_manager, &event );
221 }
222
223 /**************************************************************************
224  * input_item_preparsed_changed (Private) (vlc event Callback)
225  **************************************************************************/
226 static void input_item_preparsed_changed(const vlc_event_t *p_event,
227                                          void * user_data)
228 {
229     libvlc_media_t *media = user_data;
230     libvlc_event_t event;
231
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);
237
238
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;
243
244     /* Send the event */
245     libvlc_event_send(media->p_event_manager, &event);
246 }
247
248 /**************************************************************************
249  * Install event handler (Private)
250  **************************************************************************/
251 static void install_input_item_observer( libvlc_media_t *p_md )
252 {
253     vlc_event_attach( &p_md->p_input_item->event_manager,
254                       vlc_InputItemSubItemAdded,
255                       input_item_subitem_added,
256                       p_md );
257     vlc_event_attach( &p_md->p_input_item->event_manager,
258                       vlc_InputItemMetaChanged,
259                       input_item_meta_changed,
260                       p_md );
261     vlc_event_attach( &p_md->p_input_item->event_manager,
262                       vlc_InputItemDurationChanged,
263                       input_item_duration_changed,
264                       p_md );
265     vlc_event_attach( &p_md->p_input_item->event_manager,
266                       vlc_InputItemPreparsedChanged,
267                       input_item_preparsed_changed,
268                       p_md );
269     vlc_event_attach( &p_md->p_input_item->event_manager,
270                       vlc_InputItemSubItemTreeAdded,
271                       input_item_subitemtree_added,
272                       p_md );
273 }
274
275 /**************************************************************************
276  * Uninstall event handler (Private)
277  **************************************************************************/
278 static void uninstall_input_item_observer( libvlc_media_t *p_md )
279 {
280     vlc_event_detach( &p_md->p_input_item->event_manager,
281                       vlc_InputItemSubItemAdded,
282                       input_item_subitem_added,
283                       p_md );
284     vlc_event_detach( &p_md->p_input_item->event_manager,
285                       vlc_InputItemMetaChanged,
286                       input_item_meta_changed,
287                       p_md );
288     vlc_event_detach( &p_md->p_input_item->event_manager,
289                       vlc_InputItemDurationChanged,
290                       input_item_duration_changed,
291                       p_md );
292     vlc_event_detach( &p_md->p_input_item->event_manager,
293                       vlc_InputItemPreparsedChanged,
294                       input_item_preparsed_changed,
295                       p_md );
296     vlc_event_detach( &p_md->p_input_item->event_manager,
297                       vlc_InputItemSubItemTreeAdded,
298                       input_item_subitemtree_added,
299                       p_md );
300 }
301
302 /**************************************************************************
303  * Create a new media descriptor object from an input_item
304  * (libvlc internal)
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 )
310 {
311     libvlc_media_t * p_md;
312
313     if (!p_input_item)
314     {
315         libvlc_printerr( "No input item given" );
316         return NULL;
317     }
318
319     p_md = calloc( 1, sizeof(libvlc_media_t) );
320     if( !p_md )
321     {
322         libvlc_printerr( "Not enough memory" );
323         return NULL;
324     }
325
326     p_md->p_libvlc_instance = p_instance;
327     p_md->p_input_item      = p_input_item;
328     p_md->i_refcount        = 1;
329
330     vlc_cond_init(&p_md->parsed_cond);
331     vlc_mutex_init(&p_md->parsed_lock);
332     vlc_mutex_init(&p_md->subitems_lock);
333
334     p_md->state = libvlc_NothingSpecial;
335
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;
339
340     p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance );
341     if( unlikely(p_md->p_event_manager == NULL) )
342     {
343         free(p_md);
344         return NULL;
345     }
346
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);
355
356     vlc_gc_incref( p_md->p_input_item );
357
358     install_input_item_observer( p_md );
359
360     return p_md;
361 }
362
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 )
368 {
369     input_item_t * p_input_item;
370     libvlc_media_t * p_md;
371
372     p_input_item = input_item_New( psz_mrl, NULL );
373
374     if (!p_input_item)
375     {
376         libvlc_printerr( "Not enough memory" );
377         return NULL;
378     }
379
380     p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
381
382     /* The p_input_item is retained in libvlc_media_new_from_input_item */
383     vlc_gc_decref( p_input_item );
384
385     return p_md;
386 }
387
388 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
389                                        const char *path )
390 {
391     char *mrl = vlc_path2uri( path, NULL );
392     if( unlikely(mrl == NULL) )
393     {
394         libvlc_printerr( "%s", vlc_strerror_c(errno) );
395         return NULL;
396     }
397
398     libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
399     free( mrl );
400     return m;
401 }
402
403 libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
404 {
405     char mrl[16];
406     snprintf( mrl, sizeof(mrl), "fd://%d", fd );
407
408     return libvlc_media_new_location( p_instance, mrl );
409 }
410
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 )
416 {
417     input_item_t * p_input_item;
418     libvlc_media_t * p_md;
419     libvlc_media_list_t * p_subitems;
420
421     p_input_item = input_item_New( "vlc://nop", psz_name );
422
423     if (!p_input_item)
424     {
425         libvlc_printerr( "Not enough memory" );
426         return NULL;
427     }
428
429     p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
430
431     p_subitems = media_get_subitems( p_md );
432     if( p_subitems == NULL) {
433         libvlc_media_release( p_md );
434         return NULL;
435     }
436
437     return p_md;
438 }
439
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
445  *
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 )
450 {
451     libvlc_media_add_option_flag( p_md, psz_option,
452                           VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
453 }
454
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,
460                                    unsigned i_flags )
461 {
462     input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
463 }
464
465 /**************************************************************************
466  * Delete a media descriptor object
467  **************************************************************************/
468 void libvlc_media_release( libvlc_media_t *p_md )
469 {
470     if (!p_md)
471         return;
472
473     p_md->i_refcount--;
474
475     if( p_md->i_refcount > 0 )
476         return;
477
478     if( p_md->p_subitems )
479         libvlc_media_list_release( p_md->p_subitems );
480
481     uninstall_input_item_observer( p_md );
482     vlc_gc_decref( p_md->p_input_item );
483
484     vlc_cond_destroy( &p_md->parsed_cond );
485     vlc_mutex_destroy( &p_md->parsed_lock );
486     vlc_mutex_destroy( &p_md->subitems_lock );
487
488     /* Construct the event */
489     libvlc_event_t event;
490     event.type = libvlc_MediaFreed;
491     event.u.media_freed.md = p_md;
492
493     /* Send the event */
494     libvlc_event_send( p_md->p_event_manager, &event );
495
496     libvlc_event_manager_release( p_md->p_event_manager );
497
498     free( p_md );
499 }
500
501 /**************************************************************************
502  * Retain a media descriptor object
503  **************************************************************************/
504 void libvlc_media_retain( libvlc_media_t *p_md )
505 {
506     assert (p_md);
507     p_md->i_refcount++;
508 }
509
510 /**************************************************************************
511  * Duplicate a media descriptor object
512  **************************************************************************/
513 libvlc_media_t *
514 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
515 {
516     return libvlc_media_new_from_input_item(
517         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item );
518 }
519
520 /**************************************************************************
521  * Get mrl from a media descriptor object
522  **************************************************************************/
523 char *
524 libvlc_media_get_mrl( libvlc_media_t * p_md )
525 {
526     assert( p_md );
527     return input_item_GetURI( p_md->p_input_item );
528 }
529
530 /**************************************************************************
531  * Getter for meta information
532  **************************************************************************/
533
534 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
535 {
536     char *psz_meta = NULL;
537
538     if( e_meta == libvlc_meta_NowPlaying )
539     {
540         psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
541     }
542     else
543     {
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 );
550     }
551     return psz_meta;
552 }
553
554 /**************************************************************************
555  * Setter for meta information
556  **************************************************************************/
557
558 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
559 {
560     assert( p_md );
561     input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
562 }
563
564 int libvlc_media_save_meta( libvlc_media_t *p_md )
565 {
566     assert( 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;
569 }
570
571 /**************************************************************************
572  * Getter for state information
573  * Can be error, playing, buffering, NothingSpecial.
574  **************************************************************************/
575
576 libvlc_state_t
577 libvlc_media_get_state( libvlc_media_t *p_md )
578 {
579     assert( p_md );
580     return p_md->state;
581 }
582
583 /**************************************************************************
584  * Setter for state information (LibVLC Internal)
585  **************************************************************************/
586
587 void
588 libvlc_media_set_state( libvlc_media_t *p_md,
589                                    libvlc_state_t state )
590 {
591     libvlc_event_t event;
592
593     p_md->state = state;
594
595     /* Construct the event */
596     event.type = libvlc_MediaStateChanged;
597     event.u.media_state_changed.new_state = state;
598
599     /* Send the event */
600     libvlc_event_send( p_md->p_event_manager, &event );
601 }
602
603 /**************************************************************************
604  * subitems
605  **************************************************************************/
606 libvlc_media_list_t *
607 libvlc_media_subitems( libvlc_media_t * p_md )
608 {
609     libvlc_media_list_t *p_subitems = media_get_subitems( p_md );
610     if( p_subitems )
611         libvlc_media_list_retain( p_subitems );
612     return p_subitems;
613 }
614
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 )
620 {
621     if( !p_md->p_input_item )
622         return false;
623
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;
628
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;
633
634     p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
635     p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
636
637     p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
638     p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
639
640     p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
641     p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
642
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 );
647     return true;
648 }
649
650 /**************************************************************************
651  * event_manager
652  **************************************************************************/
653 libvlc_event_manager_t *
654 libvlc_media_event_manager( libvlc_media_t * p_md )
655 {
656     assert( p_md );
657
658     return p_md->p_event_manager;
659 }
660
661 /**************************************************************************
662  * Get duration of media object (in ms)
663  **************************************************************************/
664 int64_t
665 libvlc_media_get_duration( libvlc_media_t * p_md )
666 {
667     assert( p_md );
668
669     if( !p_md->p_input_item )
670     {
671         libvlc_printerr( "No input item" );
672         return -1;
673     }
674
675     if (!input_item_IsPreparsed( p_md->p_input_item ))
676         return -1;
677
678     return from_mtime(input_item_GetDuration( p_md->p_input_item ));
679 }
680
681 static int media_parse(libvlc_media_t *media, bool b_async,
682                        libvlc_media_parse_flag_t parse_flag)
683 {
684     bool needed;
685
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);
690
691     if (needed)
692     {
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;
697         int ret;
698
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)
706                 return ret;
707         }
708
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)
713             return ret;
714     }
715     else
716         return VLC_EGENERIC;
717
718     if (!b_async)
719     {
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);
724     }
725     return VLC_SUCCESS;
726 }
727
728 /**************************************************************************
729  * Parse the media and wait.
730  **************************************************************************/
731 void
732 libvlc_media_parse(libvlc_media_t *media)
733 {
734     media_parse( media, false, libvlc_media_fetch_local );
735 }
736
737 /**************************************************************************
738  * Parse the media but do not wait.
739  **************************************************************************/
740 void
741 libvlc_media_parse_async(libvlc_media_t *media)
742 {
743     media_parse( media, true, libvlc_media_fetch_local );
744 }
745
746 /**************************************************************************
747  * Parse the media asynchronously with options.
748  **************************************************************************/
749 int
750 libvlc_media_parse_with_options( libvlc_media_t *media,
751                                  libvlc_media_parse_flag_t parse_flag )
752 {
753     return media_parse( media, true, parse_flag ) == VLC_SUCCESS ? 0 : -1;
754 }
755
756 /**************************************************************************
757  * Get parsed status for media object.
758  **************************************************************************/
759 int
760 libvlc_media_is_parsed(libvlc_media_t *media)
761 {
762     bool parsed;
763
764     vlc_mutex_lock(&media->parsed_lock);
765     parsed = media->is_parsed;
766     vlc_mutex_unlock(&media->parsed_lock);
767     return parsed;
768 }
769
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  **************************************************************************/
775 void
776 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
777 {
778     assert( p_md );
779     p_md->p_user_data = p_new_user_data;
780 }
781
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  **************************************************************************/
787 void *
788 libvlc_media_get_user_data( libvlc_media_t * p_md )
789 {
790     assert( p_md );
791     return p_md->p_user_data;
792 }
793
794 /**************************************************************************
795  * Get media descriptor's elementary streams description
796  **************************************************************************/
797 int
798 libvlc_media_get_tracks_info( libvlc_media_t *p_md, libvlc_media_track_info_t ** pp_es )
799 {
800     assert( p_md );
801
802     input_item_t *p_input_item = p_md->p_input_item;
803     vlc_mutex_lock( &p_input_item->lock );
804
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;
807
808     if( !*pp_es ) /* no ES, or OOM */
809     {
810         vlc_mutex_unlock( &p_input_item->lock );
811         return 0;
812     }
813
814     /* Fill array */
815     for( int i = 0; i < i_es; i++ )
816     {
817         libvlc_media_track_info_t *p_mes = *pp_es+i;
818         const es_format_t *p_es = p_input_item->es[i];
819
820         p_mes->i_codec = p_es->i_codec;
821         p_mes->i_id = p_es->i_id;
822
823         p_mes->i_profile = p_es->i_profile;
824         p_mes->i_level = p_es->i_level;
825
826         switch(p_es->i_cat)
827         {
828         case UNKNOWN_ES:
829         default:
830             p_mes->i_type = libvlc_track_unknown;
831             break;
832         case VIDEO_ES:
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;
836             break;
837         case AUDIO_ES:
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;
841             break;
842         case SPU_ES:
843             p_mes->i_type = libvlc_track_text;
844             break;
845         }
846     }
847
848     vlc_mutex_unlock( &p_input_item->lock );
849     return i_es;
850 }
851
852 unsigned
853 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
854 {
855     assert( p_md );
856
857     input_item_t *p_input_item = p_md->p_input_item;
858     vlc_mutex_lock( &p_input_item->lock );
859
860     const int i_es = p_input_item->i_es;
861     *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
862
863     if( !*pp_es ) /* no ES, or OOM */
864     {
865         vlc_mutex_unlock( &p_input_item->lock );
866         return 0;
867     }
868
869     /* Fill array */
870     for( int i = 0; i < i_es; i++ )
871     {
872         libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
873         if ( p_mes )
874         {
875             p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
876                                                sizeof(*p_mes->video)),
877                                                sizeof(*p_mes->subtitle)) );
878         }
879         if ( !p_mes || !p_mes->audio )
880         {
881             libvlc_media_tracks_release( *pp_es, i_es );
882             *pp_es = NULL;
883             free( p_mes );
884             vlc_mutex_unlock( &p_input_item->lock );
885             return 0;
886         }
887         (*pp_es)[i] = p_mes;
888
889         const es_format_t *p_es = p_input_item->es[i];
890
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;
894
895         p_mes->i_profile = p_es->i_profile;
896         p_mes->i_level = p_es->i_level;
897
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;
901
902         switch(p_es->i_cat)
903         {
904         case UNKNOWN_ES:
905         default:
906             p_mes->i_type = libvlc_track_unknown;
907             break;
908         case VIDEO_ES:
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;
916             break;
917         case AUDIO_ES:
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;
921             break;
922         case SPU_ES:
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;
926             break;
927         }
928     }
929
930     vlc_mutex_unlock( &p_input_item->lock );
931     return i_es;
932 }
933
934 /**************************************************************************
935  * Get codec description from media elementary stream
936  **************************************************************************/
937 const char *
938 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
939                                     uint32_t i_codec )
940 {
941     switch( i_type )
942     {
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:
950         default:
951             return vlc_fourcc_GetDescription( UNKNOWN_ES, i_codec );
952     }
953 }
954
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 )
959 {
960     for( unsigned i = 0; i < i_count; ++i )
961     {
962         if ( !p_tracks[i] )
963             continue;
964         free( p_tracks[i]->psz_language );
965         free( p_tracks[i]->psz_description );
966         switch( p_tracks[i]->i_type )
967         {
968         case libvlc_track_audio:
969             break;
970         case libvlc_track_video:
971             break;
972         case libvlc_track_text:
973             free( p_tracks[i]->subtitle->psz_encoding );
974             break;
975         case libvlc_track_unknown:
976         default:
977             break;
978         }
979         free( p_tracks[i]->audio );
980         free( p_tracks[i] );
981     }
982     free( p_tracks );
983 }