libvlc: don't release media_list while it's used by media
[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     [libvlc_meta_DiscTotal]    = vlc_meta_DiscTotal
76 };
77
78 static const libvlc_meta_t vlc_to_libvlc_meta[] =
79 {
80     [vlc_meta_Title]        = libvlc_meta_Title,
81     [vlc_meta_Artist]       = libvlc_meta_Artist,
82     [vlc_meta_Genre]        = libvlc_meta_Genre,
83     [vlc_meta_Copyright]    = libvlc_meta_Copyright,
84     [vlc_meta_Album]        = libvlc_meta_Album,
85     [vlc_meta_TrackNumber]  = libvlc_meta_TrackNumber,
86     [vlc_meta_Description]  = libvlc_meta_Description,
87     [vlc_meta_Rating]       = libvlc_meta_Rating,
88     [vlc_meta_Date]         = libvlc_meta_Date,
89     [vlc_meta_Setting]      = libvlc_meta_Setting,
90     [vlc_meta_URL]          = libvlc_meta_URL,
91     [vlc_meta_Language]     = libvlc_meta_Language,
92     [vlc_meta_NowPlaying]   = libvlc_meta_NowPlaying,
93     [vlc_meta_ESNowPlaying] = libvlc_meta_NowPlaying,
94     [vlc_meta_Publisher]    = libvlc_meta_Publisher,
95     [vlc_meta_EncodedBy]    = libvlc_meta_EncodedBy,
96     [vlc_meta_ArtworkURL]   = libvlc_meta_ArtworkURL,
97     [vlc_meta_TrackID]      = libvlc_meta_TrackID,
98     [vlc_meta_TrackTotal]   = libvlc_meta_TrackTotal,
99     [vlc_meta_Director]     = libvlc_meta_Director,
100     [vlc_meta_Season]       = libvlc_meta_Season,
101     [vlc_meta_Episode]      = libvlc_meta_Episode,
102     [vlc_meta_ShowName]     = libvlc_meta_ShowName,
103     [vlc_meta_Actors]       = libvlc_meta_Actors,
104     [vlc_meta_AlbumArtist]  = libvlc_meta_AlbumArtist,
105     [vlc_meta_DiscNumber]   = libvlc_meta_DiscNumber,
106     [vlc_meta_DiscTotal]    = libvlc_meta_DiscTotal
107 };
108
109 static libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md,
110                                                 bool b_create )
111 {
112     libvlc_media_list_t *p_subitems = NULL;
113
114     vlc_mutex_lock( &p_md->subitems_lock );
115     if( p_md->p_subitems == NULL && b_create )
116     {
117         p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance );
118         if( p_md->p_subitems != NULL )
119         {
120             p_md->p_subitems->b_read_only = true;
121             p_md->p_subitems->p_internal_md = p_md;
122         }
123     }
124     p_subitems = p_md->p_subitems;
125     vlc_mutex_unlock( &p_md->subitems_lock );
126     return p_subitems;
127 }
128
129 /**************************************************************************
130  * input_item_subitem_added (Private) (vlc event Callback)
131  **************************************************************************/
132 static void input_item_subitem_added( const vlc_event_t *p_event,
133                                        void * user_data )
134 {
135     libvlc_media_t * p_md = user_data;
136     libvlc_media_t * p_md_child;
137     libvlc_media_list_t *p_subitems;
138     libvlc_event_t event;
139
140     p_md_child = libvlc_media_new_from_input_item(
141                 p_md->p_libvlc_instance,
142                 p_event->u.input_item_subitem_added.p_new_child );
143
144     /* Add this to our media list */
145     p_subitems = media_get_subitems( p_md, true );
146     if( p_subitems != NULL )
147     {
148         libvlc_media_list_lock( p_subitems );
149         libvlc_media_list_internal_add_media( p_subitems, p_md_child );
150         libvlc_media_list_unlock( p_subitems );
151     }
152
153     /* Construct the event */
154     event.type = libvlc_MediaSubItemAdded;
155     event.u.media_subitem_added.new_child = p_md_child;
156
157     /* Send the event */
158     libvlc_event_send( p_md->p_event_manager, &event );
159     libvlc_media_release( p_md_child );
160 }
161
162 /**************************************************************************
163  * input_item_subitemtree_added (Private) (vlc event Callback)
164  **************************************************************************/
165 static void input_item_subitemtree_added( const vlc_event_t * p_event,
166                                           void * user_data )
167 {
168     VLC_UNUSED( p_event );
169     libvlc_media_t * p_md = user_data;
170     libvlc_event_t event;
171
172     /* Construct the event */
173     event.type = libvlc_MediaSubItemTreeAdded;
174     event.u.media_subitemtree_added.item = p_md;
175
176     /* Send the event */
177     libvlc_event_send( p_md->p_event_manager, &event );
178 }
179
180 /**************************************************************************
181  * input_item_meta_changed (Private) (vlc event Callback)
182  **************************************************************************/
183 static void input_item_meta_changed( const vlc_event_t *p_event,
184                                      void * user_data )
185 {
186     libvlc_media_t * p_md = user_data;
187     libvlc_event_t event;
188
189     /* Construct the event */
190     event.type = libvlc_MediaMetaChanged;
191     event.u.media_meta_changed.meta_type =
192         vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
193
194     /* Send the event */
195     libvlc_event_send( p_md->p_event_manager, &event );
196 }
197
198 /**************************************************************************
199  * input_item_duration_changed (Private) (vlc event Callback)
200  **************************************************************************/
201 static void input_item_duration_changed( const vlc_event_t *p_event,
202                                          void * user_data )
203 {
204     libvlc_media_t * p_md = user_data;
205     libvlc_event_t event;
206
207     /* Construct the event */
208     event.type = libvlc_MediaDurationChanged;
209     event.u.media_duration_changed.new_duration =
210         from_mtime(p_event->u.input_item_duration_changed.new_duration);
211
212     /* Send the event */
213     libvlc_event_send( p_md->p_event_manager, &event );
214 }
215
216 /**************************************************************************
217  * input_item_preparsed_changed (Private) (vlc event Callback)
218  **************************************************************************/
219 static void input_item_preparsed_changed(const vlc_event_t *p_event,
220                                          void * user_data)
221 {
222     libvlc_media_t *media = user_data;
223     libvlc_event_t event;
224
225     /* Eventually notify libvlc_media_parse() */
226     vlc_mutex_lock(&media->parsed_lock);
227     media->is_parsed = true;
228     vlc_cond_broadcast(&media->parsed_cond);
229     vlc_mutex_unlock(&media->parsed_lock);
230
231
232     /* Construct the event */
233     event.type = libvlc_MediaParsedChanged;
234     event.u.media_parsed_changed.new_status =
235         p_event->u.input_item_preparsed_changed.new_status;
236
237     /* Send the event */
238     libvlc_event_send(media->p_event_manager, &event);
239 }
240
241 /**************************************************************************
242  * input_item_preparse_ended (Private) (vlc event Callback)
243  **************************************************************************/
244 static void input_item_preparse_ended( const vlc_event_t * p_event,
245                                        void * user_data )
246 {
247     VLC_UNUSED( p_event );
248     libvlc_media_t * p_md = user_data;
249     libvlc_media_list_t *p_subitems = media_get_subitems( p_md, false );
250
251     if( p_subitems != NULL )
252     {
253         /* notify the media list */
254         libvlc_media_list_lock( p_subitems );
255         libvlc_media_list_internal_end_reached( p_subitems );
256         libvlc_media_list_unlock( p_subitems );
257     }
258 }
259
260 /**************************************************************************
261  * Install event handler (Private)
262  **************************************************************************/
263 static void install_input_item_observer( libvlc_media_t *p_md )
264 {
265     vlc_event_attach( &p_md->p_input_item->event_manager,
266                       vlc_InputItemSubItemAdded,
267                       input_item_subitem_added,
268                       p_md );
269     vlc_event_attach( &p_md->p_input_item->event_manager,
270                       vlc_InputItemMetaChanged,
271                       input_item_meta_changed,
272                       p_md );
273     vlc_event_attach( &p_md->p_input_item->event_manager,
274                       vlc_InputItemDurationChanged,
275                       input_item_duration_changed,
276                       p_md );
277     vlc_event_attach( &p_md->p_input_item->event_manager,
278                       vlc_InputItemPreparsedChanged,
279                       input_item_preparsed_changed,
280                       p_md );
281     vlc_event_attach( &p_md->p_input_item->event_manager,
282                       vlc_InputItemSubItemTreeAdded,
283                       input_item_subitemtree_added,
284                       p_md );
285     vlc_event_attach( &p_md->p_input_item->event_manager,
286                       vlc_InputItemPreparseEnded,
287                       input_item_preparse_ended,
288                       p_md );
289 }
290
291 /**************************************************************************
292  * Uninstall event handler (Private)
293  **************************************************************************/
294 static void uninstall_input_item_observer( libvlc_media_t *p_md )
295 {
296     vlc_event_detach( &p_md->p_input_item->event_manager,
297                       vlc_InputItemSubItemAdded,
298                       input_item_subitem_added,
299                       p_md );
300     vlc_event_detach( &p_md->p_input_item->event_manager,
301                       vlc_InputItemMetaChanged,
302                       input_item_meta_changed,
303                       p_md );
304     vlc_event_detach( &p_md->p_input_item->event_manager,
305                       vlc_InputItemDurationChanged,
306                       input_item_duration_changed,
307                       p_md );
308     vlc_event_detach( &p_md->p_input_item->event_manager,
309                       vlc_InputItemPreparsedChanged,
310                       input_item_preparsed_changed,
311                       p_md );
312     vlc_event_detach( &p_md->p_input_item->event_manager,
313                       vlc_InputItemSubItemTreeAdded,
314                       input_item_subitemtree_added,
315                       p_md );
316     vlc_event_detach( &p_md->p_input_item->event_manager,
317                       vlc_InputItemPreparseEnded,
318                       input_item_preparse_ended,
319                       p_md );
320 }
321
322 /**************************************************************************
323  * Create a new media descriptor object from an input_item
324  * (libvlc internal)
325  * That's the generic constructor
326  **************************************************************************/
327 libvlc_media_t * libvlc_media_new_from_input_item(
328                                    libvlc_instance_t *p_instance,
329                                    input_item_t *p_input_item )
330 {
331     libvlc_media_t * p_md;
332
333     if (!p_input_item)
334     {
335         libvlc_printerr( "No input item given" );
336         return NULL;
337     }
338
339     p_md = calloc( 1, sizeof(libvlc_media_t) );
340     if( !p_md )
341     {
342         libvlc_printerr( "Not enough memory" );
343         return NULL;
344     }
345
346     p_md->p_libvlc_instance = p_instance;
347     p_md->p_input_item      = p_input_item;
348     p_md->i_refcount        = 1;
349
350     vlc_cond_init(&p_md->parsed_cond);
351     vlc_mutex_init(&p_md->parsed_lock);
352     vlc_mutex_init(&p_md->subitems_lock);
353
354     p_md->state = libvlc_NothingSpecial;
355
356     /* A media descriptor can be a playlist. When you open a playlist
357      * It can give a bunch of item to read. */
358     p_md->p_subitems        = NULL;
359
360     p_md->p_event_manager = libvlc_event_manager_new( p_md );
361     if( unlikely(p_md->p_event_manager == NULL) )
362     {
363         free(p_md);
364         return NULL;
365     }
366
367     vlc_gc_incref( p_md->p_input_item );
368
369     install_input_item_observer( p_md );
370
371     libvlc_retain( p_instance );
372     return p_md;
373 }
374
375 /**************************************************************************
376  * Create a new media descriptor object
377  **************************************************************************/
378 libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
379                                            const char * psz_mrl )
380 {
381     input_item_t * p_input_item;
382     libvlc_media_t * p_md;
383
384     p_input_item = input_item_New( psz_mrl, NULL );
385
386     if (!p_input_item)
387     {
388         libvlc_printerr( "Not enough memory" );
389         return NULL;
390     }
391
392     p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
393
394     /* The p_input_item is retained in libvlc_media_new_from_input_item */
395     vlc_gc_decref( p_input_item );
396
397     return p_md;
398 }
399
400 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
401                                        const char *path )
402 {
403     char *mrl = vlc_path2uri( path, NULL );
404     if( unlikely(mrl == NULL) )
405     {
406         libvlc_printerr( "%s", vlc_strerror_c(errno) );
407         return NULL;
408     }
409
410     libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
411     free( mrl );
412     return m;
413 }
414
415 libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
416 {
417     char mrl[16];
418     snprintf( mrl, sizeof(mrl), "fd://%d", fd );
419
420     return libvlc_media_new_location( p_instance, mrl );
421 }
422
423 libvlc_media_t *libvlc_media_new_callbacks(libvlc_instance_t *p_instance,
424                                            libvlc_media_open_cb open_cb,
425                                            libvlc_media_read_cb read_cb,
426                                            libvlc_media_seek_cb seek_cb,
427                                            libvlc_media_close_cb close_cb,
428                                            void *opaque)
429 {
430     libvlc_media_t *m = libvlc_media_new_location(p_instance, "imem://");
431     if (unlikely(m == NULL))
432         return NULL;
433
434     assert(read_cb != NULL);
435     input_item_AddOpaque(m->p_input_item, "imem-data", opaque);
436     input_item_AddOpaque(m->p_input_item, "imem-open", open_cb);
437     input_item_AddOpaque(m->p_input_item, "imem-read", read_cb);
438     input_item_AddOpaque(m->p_input_item, "imem-seek", seek_cb);
439     input_item_AddOpaque(m->p_input_item, "imem-close", close_cb);
440     return m;
441 }
442
443 /**************************************************************************
444  * Create a new media descriptor object
445  **************************************************************************/
446 libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
447                                            const char * psz_name )
448 {
449     input_item_t * p_input_item;
450     libvlc_media_t * p_md;
451     libvlc_media_list_t * p_subitems;
452
453     p_input_item = input_item_New( "vlc://nop", psz_name );
454
455     if (!p_input_item)
456     {
457         libvlc_printerr( "Not enough memory" );
458         return NULL;
459     }
460
461     p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
462
463     p_subitems = media_get_subitems( p_md, true );
464     if( p_subitems == NULL) {
465         libvlc_media_release( p_md );
466         return NULL;
467     }
468
469     return p_md;
470 }
471
472 /**************************************************************************
473  * Add an option to the media descriptor,
474  * that will be used to determine how the media_player will read the
475  * media. This allow to use VLC advanced reading/streaming
476  * options in a per-media basis
477  *
478  * The options are detailled in vlc --long-help, for instance "--sout-all"
479  **************************************************************************/
480 void libvlc_media_add_option( libvlc_media_t * p_md,
481                               const char * psz_option )
482 {
483     libvlc_media_add_option_flag( p_md, psz_option,
484                           VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
485 }
486
487 /**************************************************************************
488  * Same as libvlc_media_add_option but with configurable flags.
489  **************************************************************************/
490 void libvlc_media_add_option_flag( libvlc_media_t * p_md,
491                                    const char * ppsz_option,
492                                    unsigned i_flags )
493 {
494     input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
495 }
496
497 /**************************************************************************
498  * Delete a media descriptor object
499  **************************************************************************/
500 void libvlc_media_release( libvlc_media_t *p_md )
501 {
502     if (!p_md)
503         return;
504
505     p_md->i_refcount--;
506
507     if( p_md->i_refcount > 0 )
508         return;
509
510     uninstall_input_item_observer( p_md );
511
512     if( p_md->p_subitems )
513         libvlc_media_list_release( p_md->p_subitems );
514
515     vlc_gc_decref( p_md->p_input_item );
516
517     vlc_cond_destroy( &p_md->parsed_cond );
518     vlc_mutex_destroy( &p_md->parsed_lock );
519     vlc_mutex_destroy( &p_md->subitems_lock );
520
521     /* Construct the event */
522     libvlc_event_t event;
523     event.type = libvlc_MediaFreed;
524     event.u.media_freed.md = p_md;
525
526     /* Send the event */
527     libvlc_event_send( p_md->p_event_manager, &event );
528
529     libvlc_event_manager_release( p_md->p_event_manager );
530     libvlc_release( p_md->p_libvlc_instance );
531     free( p_md );
532 }
533
534 /**************************************************************************
535  * Retain a media descriptor object
536  **************************************************************************/
537 void libvlc_media_retain( libvlc_media_t *p_md )
538 {
539     assert (p_md);
540     p_md->i_refcount++;
541 }
542
543 /**************************************************************************
544  * Duplicate a media descriptor object
545  **************************************************************************/
546 libvlc_media_t *
547 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
548 {
549     return libvlc_media_new_from_input_item(
550         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item );
551 }
552
553 /**************************************************************************
554  * Get mrl from a media descriptor object
555  **************************************************************************/
556 char *
557 libvlc_media_get_mrl( libvlc_media_t * p_md )
558 {
559     assert( p_md );
560     return input_item_GetURI( p_md->p_input_item );
561 }
562
563 /**************************************************************************
564  * Getter for meta information
565  **************************************************************************/
566
567 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
568 {
569     char *psz_meta = NULL;
570
571     if( e_meta == libvlc_meta_NowPlaying )
572     {
573         psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
574     }
575     else
576     {
577         psz_meta = input_item_GetMeta( p_md->p_input_item,
578                                              libvlc_to_vlc_meta[e_meta] );
579         /* Should be integrated in core */
580         if( psz_meta == NULL && e_meta == libvlc_meta_Title
581          && p_md->p_input_item->psz_name != NULL )
582             psz_meta = strdup( p_md->p_input_item->psz_name );
583     }
584     return psz_meta;
585 }
586
587 /**************************************************************************
588  * Setter for meta information
589  **************************************************************************/
590
591 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
592 {
593     assert( p_md );
594     input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
595 }
596
597 int libvlc_media_save_meta( libvlc_media_t *p_md )
598 {
599     assert( p_md );
600     vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
601     return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
602 }
603
604 /**************************************************************************
605  * Getter for state information
606  * Can be error, playing, buffering, NothingSpecial.
607  **************************************************************************/
608
609 libvlc_state_t
610 libvlc_media_get_state( libvlc_media_t *p_md )
611 {
612     assert( p_md );
613     return p_md->state;
614 }
615
616 /**************************************************************************
617  * Setter for state information (LibVLC Internal)
618  **************************************************************************/
619
620 void
621 libvlc_media_set_state( libvlc_media_t *p_md,
622                                    libvlc_state_t state )
623 {
624     libvlc_event_t event;
625
626     p_md->state = state;
627
628     /* Construct the event */
629     event.type = libvlc_MediaStateChanged;
630     event.u.media_state_changed.new_state = state;
631
632     /* Send the event */
633     libvlc_event_send( p_md->p_event_manager, &event );
634 }
635
636 /**************************************************************************
637  * subitems
638  **************************************************************************/
639 libvlc_media_list_t *
640 libvlc_media_subitems( libvlc_media_t * p_md )
641 {
642     libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
643     if( p_subitems )
644         libvlc_media_list_retain( p_subitems );
645     return p_subitems;
646 }
647
648 /**************************************************************************
649  * Getter for statistics information
650  **************************************************************************/
651 int libvlc_media_get_stats( libvlc_media_t *p_md,
652                             libvlc_media_stats_t *p_stats )
653 {
654     if( !p_md->p_input_item )
655         return false;
656
657     input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
658     vlc_mutex_lock( &p_itm_stats->lock );
659     p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
660     p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
661
662     p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
663     p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
664     p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
665     p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
666
667     p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
668     p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
669
670     p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
671     p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
672
673     p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
674     p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
675
676     p_stats->i_sent_packets = p_itm_stats->i_sent_packets;
677     p_stats->i_sent_bytes = p_itm_stats->i_sent_bytes;
678     p_stats->f_send_bitrate = p_itm_stats->f_send_bitrate;
679     vlc_mutex_unlock( &p_itm_stats->lock );
680     return true;
681 }
682
683 /**************************************************************************
684  * event_manager
685  **************************************************************************/
686 libvlc_event_manager_t *
687 libvlc_media_event_manager( libvlc_media_t * p_md )
688 {
689     assert( p_md );
690
691     return p_md->p_event_manager;
692 }
693
694 /**************************************************************************
695  * Get duration of media object (in ms)
696  **************************************************************************/
697 int64_t
698 libvlc_media_get_duration( libvlc_media_t * p_md )
699 {
700     assert( p_md );
701
702     if( !p_md->p_input_item )
703     {
704         libvlc_printerr( "No input item" );
705         return -1;
706     }
707
708     if (!input_item_IsPreparsed( p_md->p_input_item ))
709         return -1;
710
711     return from_mtime(input_item_GetDuration( p_md->p_input_item ));
712 }
713
714 static int media_parse(libvlc_media_t *media, bool b_async,
715                        libvlc_media_parse_flag_t parse_flag)
716 {
717     bool needed;
718
719     vlc_mutex_lock(&media->parsed_lock);
720     needed = !media->has_asked_preparse;
721     media->has_asked_preparse = true;
722     vlc_mutex_unlock(&media->parsed_lock);
723
724     if (needed)
725     {
726         libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
727         input_item_t *item = media->p_input_item;
728         input_item_meta_request_option_t art_scope = META_REQUEST_OPTION_NONE;
729         input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
730         int ret;
731
732         if (parse_flag & libvlc_media_fetch_local)
733             art_scope |= META_REQUEST_OPTION_SCOPE_LOCAL;
734         if (parse_flag & libvlc_media_fetch_network)
735             art_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
736         if (art_scope != META_REQUEST_OPTION_NONE) {
737             ret = libvlc_ArtRequest(libvlc, item, art_scope);
738             if (ret != VLC_SUCCESS)
739                 return ret;
740         }
741
742         if (parse_flag & libvlc_media_parse_network)
743             parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
744         ret = libvlc_MetaRequest(libvlc, item, parse_scope);
745         if (ret != VLC_SUCCESS)
746             return ret;
747     }
748     else
749         return VLC_EGENERIC;
750
751     if (!b_async)
752     {
753         vlc_mutex_lock(&media->parsed_lock);
754         while (!media->is_parsed)
755             vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
756         vlc_mutex_unlock(&media->parsed_lock);
757     }
758     return VLC_SUCCESS;
759 }
760
761 /**************************************************************************
762  * Parse the media and wait.
763  **************************************************************************/
764 void
765 libvlc_media_parse(libvlc_media_t *media)
766 {
767     media_parse( media, false, libvlc_media_fetch_local );
768 }
769
770 /**************************************************************************
771  * Parse the media but do not wait.
772  **************************************************************************/
773 void
774 libvlc_media_parse_async(libvlc_media_t *media)
775 {
776     media_parse( media, true, libvlc_media_fetch_local );
777 }
778
779 /**************************************************************************
780  * Parse the media asynchronously with options.
781  **************************************************************************/
782 int
783 libvlc_media_parse_with_options( libvlc_media_t *media,
784                                  libvlc_media_parse_flag_t parse_flag )
785 {
786     return media_parse( media, true, parse_flag ) == VLC_SUCCESS ? 0 : -1;
787 }
788
789 /**************************************************************************
790  * Get parsed status for media object.
791  **************************************************************************/
792 int
793 libvlc_media_is_parsed(libvlc_media_t *media)
794 {
795     bool parsed;
796
797     vlc_mutex_lock(&media->parsed_lock);
798     parsed = media->is_parsed;
799     vlc_mutex_unlock(&media->parsed_lock);
800     return parsed;
801 }
802
803 /**************************************************************************
804  * Sets media descriptor's user_data. user_data is specialized data
805  * accessed by the host application, VLC.framework uses it as a pointer to
806  * an native object that references a libvlc_media_t pointer
807  **************************************************************************/
808 void
809 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
810 {
811     assert( p_md );
812     p_md->p_user_data = p_new_user_data;
813 }
814
815 /**************************************************************************
816  * Get media descriptor's user_data. user_data is specialized data
817  * accessed by the host application, VLC.framework uses it as a pointer to
818  * an native object that references a libvlc_media_t pointer
819  **************************************************************************/
820 void *
821 libvlc_media_get_user_data( libvlc_media_t * p_md )
822 {
823     assert( p_md );
824     return p_md->p_user_data;
825 }
826
827 /**************************************************************************
828  * Get media descriptor's elementary streams description
829  **************************************************************************/
830 int
831 libvlc_media_get_tracks_info( libvlc_media_t *p_md, libvlc_media_track_info_t ** pp_es )
832 {
833     assert( p_md );
834
835     input_item_t *p_input_item = p_md->p_input_item;
836     vlc_mutex_lock( &p_input_item->lock );
837
838     const int i_es = p_input_item->i_es;
839     *pp_es = (i_es > 0) ? malloc( i_es * sizeof(libvlc_media_track_info_t) ) : NULL;
840
841     if( !*pp_es ) /* no ES, or OOM */
842     {
843         vlc_mutex_unlock( &p_input_item->lock );
844         return 0;
845     }
846
847     /* Fill array */
848     for( int i = 0; i < i_es; i++ )
849     {
850         libvlc_media_track_info_t *p_mes = *pp_es+i;
851         const es_format_t *p_es = p_input_item->es[i];
852
853         p_mes->i_codec = p_es->i_codec;
854         p_mes->i_id = p_es->i_id;
855
856         p_mes->i_profile = p_es->i_profile;
857         p_mes->i_level = p_es->i_level;
858
859         switch(p_es->i_cat)
860         {
861         case UNKNOWN_ES:
862         default:
863             p_mes->i_type = libvlc_track_unknown;
864             break;
865         case VIDEO_ES:
866             p_mes->i_type = libvlc_track_video;
867             p_mes->u.video.i_height = p_es->video.i_visible_height;
868             p_mes->u.video.i_width = p_es->video.i_visible_width;
869             break;
870         case AUDIO_ES:
871             p_mes->i_type = libvlc_track_audio;
872             p_mes->u.audio.i_channels = p_es->audio.i_channels;
873             p_mes->u.audio.i_rate = p_es->audio.i_rate;
874             break;
875         case SPU_ES:
876             p_mes->i_type = libvlc_track_text;
877             break;
878         }
879     }
880
881     vlc_mutex_unlock( &p_input_item->lock );
882     return i_es;
883 }
884
885 unsigned
886 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
887 {
888     assert( p_md );
889
890     input_item_t *p_input_item = p_md->p_input_item;
891     vlc_mutex_lock( &p_input_item->lock );
892
893     const int i_es = p_input_item->i_es;
894     *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
895
896     if( !*pp_es ) /* no ES, or OOM */
897     {
898         vlc_mutex_unlock( &p_input_item->lock );
899         return 0;
900     }
901
902     /* Fill array */
903     for( int i = 0; i < i_es; i++ )
904     {
905         libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
906         if ( p_mes )
907         {
908             p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
909                                                sizeof(*p_mes->video)),
910                                                sizeof(*p_mes->subtitle)) );
911         }
912         if ( !p_mes || !p_mes->audio )
913         {
914             libvlc_media_tracks_release( *pp_es, i_es );
915             *pp_es = NULL;
916             free( p_mes );
917             vlc_mutex_unlock( &p_input_item->lock );
918             return 0;
919         }
920         (*pp_es)[i] = p_mes;
921
922         const es_format_t *p_es = p_input_item->es[i];
923
924         p_mes->i_codec = p_es->i_codec;
925         p_mes->i_original_fourcc = p_es->i_original_fourcc;
926         p_mes->i_id = p_es->i_id;
927
928         p_mes->i_profile = p_es->i_profile;
929         p_mes->i_level = p_es->i_level;
930
931         p_mes->i_bitrate = p_es->i_bitrate;
932         p_mes->psz_language = p_es->psz_language != NULL ? strdup(p_es->psz_language) : NULL;
933         p_mes->psz_description = p_es->psz_description != NULL ? strdup(p_es->psz_description) : NULL;
934
935         switch(p_es->i_cat)
936         {
937         case UNKNOWN_ES:
938         default:
939             p_mes->i_type = libvlc_track_unknown;
940             break;
941         case VIDEO_ES:
942             p_mes->i_type = libvlc_track_video;
943             p_mes->video->i_height = p_es->video.i_visible_height;
944             p_mes->video->i_width = p_es->video.i_visible_width;
945             p_mes->video->i_sar_num = p_es->video.i_sar_num;
946             p_mes->video->i_sar_den = p_es->video.i_sar_den;
947             p_mes->video->i_frame_rate_num = p_es->video.i_frame_rate;
948             p_mes->video->i_frame_rate_den = p_es->video.i_frame_rate_base;
949             break;
950         case AUDIO_ES:
951             p_mes->i_type = libvlc_track_audio;
952             p_mes->audio->i_channels = p_es->audio.i_channels;
953             p_mes->audio->i_rate = p_es->audio.i_rate;
954             break;
955         case SPU_ES:
956             p_mes->i_type = libvlc_track_text;
957             p_mes->subtitle->psz_encoding = p_es->subs.psz_encoding != NULL ?
958                                             strdup(p_es->subs.psz_encoding) : NULL;
959             break;
960         }
961     }
962
963     vlc_mutex_unlock( &p_input_item->lock );
964     return i_es;
965 }
966
967 /**************************************************************************
968  * Get codec description from media elementary stream
969  **************************************************************************/
970 const char *
971 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
972                                     uint32_t i_codec )
973 {
974     switch( i_type )
975     {
976         case libvlc_track_audio:
977             return vlc_fourcc_GetDescription( AUDIO_ES, i_codec );
978         case libvlc_track_video:
979             return vlc_fourcc_GetDescription( VIDEO_ES, i_codec );
980         case libvlc_track_text:
981             return vlc_fourcc_GetDescription( SPU_ES, i_codec );
982         case libvlc_track_unknown:
983         default:
984             return vlc_fourcc_GetDescription( UNKNOWN_ES, i_codec );
985     }
986 }
987
988 /**************************************************************************
989  * Release media descriptor's elementary streams description array
990  **************************************************************************/
991 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
992 {
993     for( unsigned i = 0; i < i_count; ++i )
994     {
995         if ( !p_tracks[i] )
996             continue;
997         free( p_tracks[i]->psz_language );
998         free( p_tracks[i]->psz_description );
999         switch( p_tracks[i]->i_type )
1000         {
1001         case libvlc_track_audio:
1002             break;
1003         case libvlc_track_video:
1004             break;
1005         case libvlc_track_text:
1006             free( p_tracks[i]->subtitle->psz_encoding );
1007             break;
1008         case libvlc_track_unknown:
1009         default:
1010             break;
1011         }
1012         free( p_tracks[i]->audio );
1013         free( p_tracks[i] );
1014     }
1015     free( p_tracks );
1016 }
1017
1018 /**************************************************************************
1019  * Get the media type of the media descriptor object
1020  **************************************************************************/
1021 libvlc_media_type_t libvlc_media_get_type( libvlc_media_t *p_md )
1022 {
1023     assert( p_md );
1024
1025     int i_type;
1026     input_item_t *p_input_item = p_md->p_input_item;
1027
1028     vlc_mutex_lock( &p_input_item->lock );
1029     i_type = p_md->p_input_item->i_type;
1030     vlc_mutex_unlock( &p_input_item->lock );
1031
1032     switch( i_type )
1033     {
1034     case ITEM_TYPE_FILE:
1035         return libvlc_media_type_file;
1036     case ITEM_TYPE_NODE:
1037     case ITEM_TYPE_DIRECTORY:
1038         return libvlc_media_type_directory;
1039     case ITEM_TYPE_DISC:
1040         return libvlc_media_type_disc;
1041     case ITEM_TYPE_STREAM:
1042         return libvlc_media_type_stream;
1043     case ITEM_TYPE_PLAYLIST:
1044         return libvlc_media_type_playlist;
1045     default:
1046         return libvlc_media_type_unknown;
1047     }
1048 }