libvlc: media: cancel preparsing when releasing a 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 static void send_parsed_changed( libvlc_media_t *p_md,
217                                  libvlc_media_parsed_status_t new_status )
218 {
219     libvlc_event_t event;
220
221     vlc_mutex_lock( &p_md->parsed_lock );
222     if( p_md->parsed_status == new_status )
223     {
224         vlc_mutex_unlock( &p_md->parsed_lock );
225         return;
226     }
227
228     /* Legacy: notify libvlc_media_parse */
229     if( !p_md->is_parsed )
230     {
231         p_md->is_parsed = true;
232         vlc_cond_broadcast( &p_md->parsed_cond );
233     }
234
235     p_md->parsed_status = new_status;
236     if( p_md->parsed_status == libvlc_media_parsed_status_skipped )
237         p_md->has_asked_preparse = false;
238
239     vlc_mutex_unlock( &p_md->parsed_lock );
240
241     if( new_status == libvlc_media_parsed_status_done )
242     {
243         libvlc_media_list_t *p_subitems = media_get_subitems( p_md, false );
244         if( p_subitems != NULL )
245         {
246             /* notify the media list */
247             libvlc_media_list_lock( p_subitems );
248             libvlc_media_list_internal_end_reached( p_subitems );
249             libvlc_media_list_unlock( p_subitems );
250         }
251     }
252
253     /* Construct the event */
254     event.type = libvlc_MediaParsedChanged;
255     event.u.media_parsed_changed.new_status = new_status;
256
257     /* Send the event */
258     libvlc_event_send( p_md->p_event_manager, &event );
259 }
260
261 /**************************************************************************
262  * input_item_preparse_ended (Private) (vlc event Callback)
263  **************************************************************************/
264 static void input_item_preparse_ended( const vlc_event_t * p_event,
265                                        void * user_data )
266 {
267     libvlc_media_t * p_md = user_data;
268     libvlc_media_parsed_status_t new_status;
269
270     switch( p_event->u.input_item_preparse_ended.new_status )
271     {
272         case ITEM_PREPARSE_SKIPPED:
273             new_status = libvlc_media_parsed_status_skipped;
274             break;
275         case ITEM_PREPARSE_FAILED:
276             new_status = libvlc_media_parsed_status_failed;
277             break;
278         case ITEM_PREPARSE_DONE:
279             new_status = libvlc_media_parsed_status_done;
280             break;
281         default:
282             return;
283     }
284     send_parsed_changed( p_md, new_status );
285 }
286
287 /**************************************************************************
288  * Install event handler (Private)
289  **************************************************************************/
290 static void install_input_item_observer( libvlc_media_t *p_md )
291 {
292     vlc_event_attach( &p_md->p_input_item->event_manager,
293                       vlc_InputItemSubItemAdded,
294                       input_item_subitem_added,
295                       p_md );
296     vlc_event_attach( &p_md->p_input_item->event_manager,
297                       vlc_InputItemMetaChanged,
298                       input_item_meta_changed,
299                       p_md );
300     vlc_event_attach( &p_md->p_input_item->event_manager,
301                       vlc_InputItemDurationChanged,
302                       input_item_duration_changed,
303                       p_md );
304     vlc_event_attach( &p_md->p_input_item->event_manager,
305                       vlc_InputItemSubItemTreeAdded,
306                       input_item_subitemtree_added,
307                       p_md );
308     vlc_event_attach( &p_md->p_input_item->event_manager,
309                       vlc_InputItemPreparseEnded,
310                       input_item_preparse_ended,
311                       p_md );
312 }
313
314 /**************************************************************************
315  * Uninstall event handler (Private)
316  **************************************************************************/
317 static void uninstall_input_item_observer( libvlc_media_t *p_md )
318 {
319     vlc_event_detach( &p_md->p_input_item->event_manager,
320                       vlc_InputItemSubItemAdded,
321                       input_item_subitem_added,
322                       p_md );
323     vlc_event_detach( &p_md->p_input_item->event_manager,
324                       vlc_InputItemMetaChanged,
325                       input_item_meta_changed,
326                       p_md );
327     vlc_event_detach( &p_md->p_input_item->event_manager,
328                       vlc_InputItemDurationChanged,
329                       input_item_duration_changed,
330                       p_md );
331     vlc_event_detach( &p_md->p_input_item->event_manager,
332                       vlc_InputItemSubItemTreeAdded,
333                       input_item_subitemtree_added,
334                       p_md );
335     vlc_event_detach( &p_md->p_input_item->event_manager,
336                       vlc_InputItemPreparseEnded,
337                       input_item_preparse_ended,
338                       p_md );
339 }
340
341 /**************************************************************************
342  * Create a new media descriptor object from an input_item
343  * (libvlc internal)
344  * That's the generic constructor
345  **************************************************************************/
346 libvlc_media_t * libvlc_media_new_from_input_item(
347                                    libvlc_instance_t *p_instance,
348                                    input_item_t *p_input_item )
349 {
350     libvlc_media_t * p_md;
351
352     if (!p_input_item)
353     {
354         libvlc_printerr( "No input item given" );
355         return NULL;
356     }
357
358     p_md = calloc( 1, sizeof(libvlc_media_t) );
359     if( !p_md )
360     {
361         libvlc_printerr( "Not enough memory" );
362         return NULL;
363     }
364
365     p_md->p_libvlc_instance = p_instance;
366     p_md->p_input_item      = p_input_item;
367     p_md->i_refcount        = 1;
368
369     vlc_cond_init(&p_md->parsed_cond);
370     vlc_mutex_init(&p_md->parsed_lock);
371     vlc_mutex_init(&p_md->subitems_lock);
372
373     p_md->state = libvlc_NothingSpecial;
374
375     /* A media descriptor can be a playlist. When you open a playlist
376      * It can give a bunch of item to read. */
377     p_md->p_subitems        = NULL;
378
379     p_md->p_event_manager = libvlc_event_manager_new( p_md );
380     if( unlikely(p_md->p_event_manager == NULL) )
381     {
382         free(p_md);
383         return NULL;
384     }
385
386     vlc_gc_incref( p_md->p_input_item );
387
388     install_input_item_observer( p_md );
389
390     libvlc_retain( p_instance );
391     return p_md;
392 }
393
394 /**************************************************************************
395  * Create a new media descriptor object
396  **************************************************************************/
397 libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
398                                            const char * psz_mrl )
399 {
400     input_item_t * p_input_item;
401     libvlc_media_t * p_md;
402
403     p_input_item = input_item_New( psz_mrl, NULL );
404
405     if (!p_input_item)
406     {
407         libvlc_printerr( "Not enough memory" );
408         return NULL;
409     }
410
411     p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
412
413     /* The p_input_item is retained in libvlc_media_new_from_input_item */
414     vlc_gc_decref( p_input_item );
415
416     return p_md;
417 }
418
419 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
420                                        const char *path )
421 {
422     char *mrl = vlc_path2uri( path, NULL );
423     if( unlikely(mrl == NULL) )
424     {
425         libvlc_printerr( "%s", vlc_strerror_c(errno) );
426         return NULL;
427     }
428
429     libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
430     free( mrl );
431     return m;
432 }
433
434 libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
435 {
436     char mrl[16];
437     snprintf( mrl, sizeof(mrl), "fd://%d", fd );
438
439     return libvlc_media_new_location( p_instance, mrl );
440 }
441
442 libvlc_media_t *libvlc_media_new_callbacks(libvlc_instance_t *p_instance,
443                                            libvlc_media_open_cb open_cb,
444                                            libvlc_media_read_cb read_cb,
445                                            libvlc_media_seek_cb seek_cb,
446                                            libvlc_media_close_cb close_cb,
447                                            void *opaque)
448 {
449     libvlc_media_t *m = libvlc_media_new_location(p_instance, "imem://");
450     if (unlikely(m == NULL))
451         return NULL;
452
453     assert(read_cb != NULL);
454     input_item_AddOpaque(m->p_input_item, "imem-data", opaque);
455     input_item_AddOpaque(m->p_input_item, "imem-open", open_cb);
456     input_item_AddOpaque(m->p_input_item, "imem-read", read_cb);
457     input_item_AddOpaque(m->p_input_item, "imem-seek", seek_cb);
458     input_item_AddOpaque(m->p_input_item, "imem-close", close_cb);
459     return m;
460 }
461
462 /**************************************************************************
463  * Create a new media descriptor object
464  **************************************************************************/
465 libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
466                                            const char * psz_name )
467 {
468     input_item_t * p_input_item;
469     libvlc_media_t * p_md;
470     libvlc_media_list_t * p_subitems;
471
472     p_input_item = input_item_New( "vlc://nop", psz_name );
473
474     if (!p_input_item)
475     {
476         libvlc_printerr( "Not enough memory" );
477         return NULL;
478     }
479
480     p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
481
482     p_subitems = media_get_subitems( p_md, true );
483     if( p_subitems == NULL) {
484         libvlc_media_release( p_md );
485         return NULL;
486     }
487
488     return p_md;
489 }
490
491 /**************************************************************************
492  * Add an option to the media descriptor,
493  * that will be used to determine how the media_player will read the
494  * media. This allow to use VLC advanced reading/streaming
495  * options in a per-media basis
496  *
497  * The options are detailled in vlc --long-help, for instance "--sout-all"
498  **************************************************************************/
499 void libvlc_media_add_option( libvlc_media_t * p_md,
500                               const char * psz_option )
501 {
502     libvlc_media_add_option_flag( p_md, psz_option,
503                           VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
504 }
505
506 /**************************************************************************
507  * Same as libvlc_media_add_option but with configurable flags.
508  **************************************************************************/
509 void libvlc_media_add_option_flag( libvlc_media_t * p_md,
510                                    const char * ppsz_option,
511                                    unsigned i_flags )
512 {
513     input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
514 }
515
516 /**************************************************************************
517  * Delete a media descriptor object
518  **************************************************************************/
519 void libvlc_media_release( libvlc_media_t *p_md )
520 {
521     if (!p_md)
522         return;
523
524     p_md->i_refcount--;
525
526     if( p_md->i_refcount > 0 )
527         return;
528
529     uninstall_input_item_observer( p_md );
530
531     /* Cancel asynchronous parsing (if any) */
532     if( p_md->p_input_item )
533         libvlc_MetadataCancel( p_md->p_libvlc_instance->p_libvlc_int, p_md );
534
535     if( p_md->p_subitems )
536         libvlc_media_list_release( p_md->p_subitems );
537
538     vlc_gc_decref( p_md->p_input_item );
539
540     vlc_cond_destroy( &p_md->parsed_cond );
541     vlc_mutex_destroy( &p_md->parsed_lock );
542     vlc_mutex_destroy( &p_md->subitems_lock );
543
544     /* Construct the event */
545     libvlc_event_t event;
546     event.type = libvlc_MediaFreed;
547     event.u.media_freed.md = p_md;
548
549     /* Send the event */
550     libvlc_event_send( p_md->p_event_manager, &event );
551
552     libvlc_event_manager_release( p_md->p_event_manager );
553     libvlc_release( p_md->p_libvlc_instance );
554     free( p_md );
555 }
556
557 /**************************************************************************
558  * Retain a media descriptor object
559  **************************************************************************/
560 void libvlc_media_retain( libvlc_media_t *p_md )
561 {
562     assert (p_md);
563     p_md->i_refcount++;
564 }
565
566 /**************************************************************************
567  * Duplicate a media descriptor object
568  **************************************************************************/
569 libvlc_media_t *
570 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
571 {
572     return libvlc_media_new_from_input_item(
573         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item );
574 }
575
576 /**************************************************************************
577  * Get mrl from a media descriptor object
578  **************************************************************************/
579 char *
580 libvlc_media_get_mrl( libvlc_media_t * p_md )
581 {
582     assert( p_md );
583     return input_item_GetURI( p_md->p_input_item );
584 }
585
586 /**************************************************************************
587  * Getter for meta information
588  **************************************************************************/
589
590 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
591 {
592     char *psz_meta = NULL;
593
594     if( e_meta == libvlc_meta_NowPlaying )
595     {
596         psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
597     }
598     else
599     {
600         psz_meta = input_item_GetMeta( p_md->p_input_item,
601                                              libvlc_to_vlc_meta[e_meta] );
602         /* Should be integrated in core */
603         if( psz_meta == NULL && e_meta == libvlc_meta_Title
604          && p_md->p_input_item->psz_name != NULL )
605             psz_meta = strdup( p_md->p_input_item->psz_name );
606     }
607     return psz_meta;
608 }
609
610 /**************************************************************************
611  * Setter for meta information
612  **************************************************************************/
613
614 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
615 {
616     assert( p_md );
617     input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
618 }
619
620 int libvlc_media_save_meta( libvlc_media_t *p_md )
621 {
622     assert( p_md );
623     vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
624     return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
625 }
626
627 /**************************************************************************
628  * Getter for state information
629  * Can be error, playing, buffering, NothingSpecial.
630  **************************************************************************/
631
632 libvlc_state_t
633 libvlc_media_get_state( libvlc_media_t *p_md )
634 {
635     assert( p_md );
636     return p_md->state;
637 }
638
639 /**************************************************************************
640  * Setter for state information (LibVLC Internal)
641  **************************************************************************/
642
643 void
644 libvlc_media_set_state( libvlc_media_t *p_md,
645                                    libvlc_state_t state )
646 {
647     libvlc_event_t event;
648
649     p_md->state = state;
650
651     /* Construct the event */
652     event.type = libvlc_MediaStateChanged;
653     event.u.media_state_changed.new_state = state;
654
655     /* Send the event */
656     libvlc_event_send( p_md->p_event_manager, &event );
657 }
658
659 /**************************************************************************
660  * subitems
661  **************************************************************************/
662 libvlc_media_list_t *
663 libvlc_media_subitems( libvlc_media_t * p_md )
664 {
665     libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
666     if( p_subitems )
667         libvlc_media_list_retain( p_subitems );
668     return p_subitems;
669 }
670
671 /**************************************************************************
672  * Getter for statistics information
673  **************************************************************************/
674 int libvlc_media_get_stats( libvlc_media_t *p_md,
675                             libvlc_media_stats_t *p_stats )
676 {
677     if( !p_md->p_input_item )
678         return false;
679
680     input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
681     vlc_mutex_lock( &p_itm_stats->lock );
682     p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
683     p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
684
685     p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
686     p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
687     p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
688     p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
689
690     p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
691     p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
692
693     p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
694     p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
695
696     p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
697     p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
698
699     p_stats->i_sent_packets = p_itm_stats->i_sent_packets;
700     p_stats->i_sent_bytes = p_itm_stats->i_sent_bytes;
701     p_stats->f_send_bitrate = p_itm_stats->f_send_bitrate;
702     vlc_mutex_unlock( &p_itm_stats->lock );
703     return true;
704 }
705
706 /**************************************************************************
707  * event_manager
708  **************************************************************************/
709 libvlc_event_manager_t *
710 libvlc_media_event_manager( libvlc_media_t * p_md )
711 {
712     assert( p_md );
713
714     return p_md->p_event_manager;
715 }
716
717 /**************************************************************************
718  * Get duration of media object (in ms)
719  **************************************************************************/
720 int64_t
721 libvlc_media_get_duration( libvlc_media_t * p_md )
722 {
723     assert( p_md );
724
725     if( !p_md->p_input_item )
726     {
727         libvlc_printerr( "No input item" );
728         return -1;
729     }
730
731     if (!input_item_IsPreparsed( p_md->p_input_item ))
732         return -1;
733
734     return from_mtime(input_item_GetDuration( p_md->p_input_item ));
735 }
736
737 static int media_parse(libvlc_media_t *media, bool b_async,
738                        libvlc_media_parse_flag_t parse_flag)
739 {
740     bool needed;
741
742     vlc_mutex_lock(&media->parsed_lock);
743     needed = !media->has_asked_preparse;
744     media->has_asked_preparse = true;
745     if (needed)
746         media->is_parsed = false;
747     vlc_mutex_unlock(&media->parsed_lock);
748
749     if (needed)
750     {
751         libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
752         input_item_t *item = media->p_input_item;
753         input_item_meta_request_option_t art_scope = META_REQUEST_OPTION_NONE;
754         input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
755         int ret;
756
757         if (parse_flag & libvlc_media_fetch_local)
758             art_scope |= META_REQUEST_OPTION_SCOPE_LOCAL;
759         if (parse_flag & libvlc_media_fetch_network)
760             art_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
761         if (art_scope != META_REQUEST_OPTION_NONE) {
762             ret = libvlc_ArtRequest(libvlc, item, art_scope);
763             if (ret != VLC_SUCCESS)
764                 return ret;
765         }
766
767         if (parse_flag & libvlc_media_parse_network)
768             parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
769         if (parse_flag & libvlc_media_do_interact)
770             parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
771         ret = libvlc_MetadataRequest(libvlc, item, parse_scope, 0, media);
772         if (ret != VLC_SUCCESS)
773             return ret;
774     }
775     else
776         return VLC_EGENERIC;
777
778     if (!b_async)
779     {
780         vlc_mutex_lock(&media->parsed_lock);
781         while (!media->is_parsed)
782             vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
783         vlc_mutex_unlock(&media->parsed_lock);
784     }
785     return VLC_SUCCESS;
786 }
787
788 /**************************************************************************
789  * Parse the media and wait.
790  **************************************************************************/
791 void
792 libvlc_media_parse(libvlc_media_t *media)
793 {
794     media_parse( media, false, libvlc_media_fetch_local );
795 }
796
797 /**************************************************************************
798  * Parse the media but do not wait.
799  **************************************************************************/
800 void
801 libvlc_media_parse_async(libvlc_media_t *media)
802 {
803     media_parse( media, true, libvlc_media_fetch_local );
804 }
805
806 /**************************************************************************
807  * Parse the media asynchronously with options.
808  **************************************************************************/
809 int
810 libvlc_media_parse_with_options( libvlc_media_t *media,
811                                  libvlc_media_parse_flag_t parse_flag )
812 {
813     return media_parse( media, true, parse_flag ) == VLC_SUCCESS ? 0 : -1;
814 }
815
816 /**************************************************************************
817  * Get parsed status for media object.
818  **************************************************************************/
819 int
820 libvlc_media_is_parsed(libvlc_media_t *media)
821 {
822     bool parsed;
823
824     vlc_mutex_lock(&media->parsed_lock);
825     parsed = media->is_parsed;
826     vlc_mutex_unlock(&media->parsed_lock);
827     return parsed;
828 }
829
830 libvlc_media_parsed_status_t
831 libvlc_media_get_parsed_status(libvlc_media_t *media)
832 {
833     libvlc_media_parsed_status_t status;
834
835     vlc_mutex_lock(&media->parsed_lock);
836     status = media->parsed_status;
837     vlc_mutex_unlock(&media->parsed_lock);
838     return status;
839 }
840
841 /**************************************************************************
842  * Sets media descriptor's user_data. user_data is specialized data
843  * accessed by the host application, VLC.framework uses it as a pointer to
844  * an native object that references a libvlc_media_t pointer
845  **************************************************************************/
846 void
847 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
848 {
849     assert( p_md );
850     p_md->p_user_data = p_new_user_data;
851 }
852
853 /**************************************************************************
854  * Get media descriptor's user_data. user_data is specialized data
855  * accessed by the host application, VLC.framework uses it as a pointer to
856  * an native object that references a libvlc_media_t pointer
857  **************************************************************************/
858 void *
859 libvlc_media_get_user_data( libvlc_media_t * p_md )
860 {
861     assert( p_md );
862     return p_md->p_user_data;
863 }
864
865 /**************************************************************************
866  * Get media descriptor's elementary streams description
867  **************************************************************************/
868 int
869 libvlc_media_get_tracks_info( libvlc_media_t *p_md, libvlc_media_track_info_t ** pp_es )
870 {
871     assert( p_md );
872
873     input_item_t *p_input_item = p_md->p_input_item;
874     vlc_mutex_lock( &p_input_item->lock );
875
876     const int i_es = p_input_item->i_es;
877     *pp_es = (i_es > 0) ? malloc( i_es * sizeof(libvlc_media_track_info_t) ) : NULL;
878
879     if( !*pp_es ) /* no ES, or OOM */
880     {
881         vlc_mutex_unlock( &p_input_item->lock );
882         return 0;
883     }
884
885     /* Fill array */
886     for( int i = 0; i < i_es; i++ )
887     {
888         libvlc_media_track_info_t *p_mes = *pp_es+i;
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_id = p_es->i_id;
893
894         p_mes->i_profile = p_es->i_profile;
895         p_mes->i_level = p_es->i_level;
896
897         switch(p_es->i_cat)
898         {
899         case UNKNOWN_ES:
900         default:
901             p_mes->i_type = libvlc_track_unknown;
902             break;
903         case VIDEO_ES:
904             p_mes->i_type = libvlc_track_video;
905             p_mes->u.video.i_height = p_es->video.i_visible_height;
906             p_mes->u.video.i_width = p_es->video.i_visible_width;
907             break;
908         case AUDIO_ES:
909             p_mes->i_type = libvlc_track_audio;
910             p_mes->u.audio.i_channels = p_es->audio.i_channels;
911             p_mes->u.audio.i_rate = p_es->audio.i_rate;
912             break;
913         case SPU_ES:
914             p_mes->i_type = libvlc_track_text;
915             break;
916         }
917     }
918
919     vlc_mutex_unlock( &p_input_item->lock );
920     return i_es;
921 }
922
923 unsigned
924 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
925 {
926     assert( p_md );
927
928     input_item_t *p_input_item = p_md->p_input_item;
929     vlc_mutex_lock( &p_input_item->lock );
930
931     const int i_es = p_input_item->i_es;
932     *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
933
934     if( !*pp_es ) /* no ES, or OOM */
935     {
936         vlc_mutex_unlock( &p_input_item->lock );
937         return 0;
938     }
939
940     /* Fill array */
941     for( int i = 0; i < i_es; i++ )
942     {
943         libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
944         if ( p_mes )
945         {
946             p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
947                                                sizeof(*p_mes->video)),
948                                                sizeof(*p_mes->subtitle)) );
949         }
950         if ( !p_mes || !p_mes->audio )
951         {
952             libvlc_media_tracks_release( *pp_es, i_es );
953             *pp_es = NULL;
954             free( p_mes );
955             vlc_mutex_unlock( &p_input_item->lock );
956             return 0;
957         }
958         (*pp_es)[i] = p_mes;
959
960         const es_format_t *p_es = p_input_item->es[i];
961
962         p_mes->i_codec = p_es->i_codec;
963         p_mes->i_original_fourcc = p_es->i_original_fourcc;
964         p_mes->i_id = p_es->i_id;
965
966         p_mes->i_profile = p_es->i_profile;
967         p_mes->i_level = p_es->i_level;
968
969         p_mes->i_bitrate = p_es->i_bitrate;
970         p_mes->psz_language = p_es->psz_language != NULL ? strdup(p_es->psz_language) : NULL;
971         p_mes->psz_description = p_es->psz_description != NULL ? strdup(p_es->psz_description) : NULL;
972
973         switch(p_es->i_cat)
974         {
975         case UNKNOWN_ES:
976         default:
977             p_mes->i_type = libvlc_track_unknown;
978             break;
979         case VIDEO_ES:
980             p_mes->i_type = libvlc_track_video;
981             p_mes->video->i_height = p_es->video.i_visible_height;
982             p_mes->video->i_width = p_es->video.i_visible_width;
983             p_mes->video->i_sar_num = p_es->video.i_sar_num;
984             p_mes->video->i_sar_den = p_es->video.i_sar_den;
985             p_mes->video->i_frame_rate_num = p_es->video.i_frame_rate;
986             p_mes->video->i_frame_rate_den = p_es->video.i_frame_rate_base;
987             break;
988         case AUDIO_ES:
989             p_mes->i_type = libvlc_track_audio;
990             p_mes->audio->i_channels = p_es->audio.i_channels;
991             p_mes->audio->i_rate = p_es->audio.i_rate;
992             break;
993         case SPU_ES:
994             p_mes->i_type = libvlc_track_text;
995             p_mes->subtitle->psz_encoding = p_es->subs.psz_encoding != NULL ?
996                                             strdup(p_es->subs.psz_encoding) : NULL;
997             break;
998         }
999     }
1000
1001     vlc_mutex_unlock( &p_input_item->lock );
1002     return i_es;
1003 }
1004
1005 /**************************************************************************
1006  * Get codec description from media elementary stream
1007  **************************************************************************/
1008 const char *
1009 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
1010                                     uint32_t i_codec )
1011 {
1012     switch( i_type )
1013     {
1014         case libvlc_track_audio:
1015             return vlc_fourcc_GetDescription( AUDIO_ES, i_codec );
1016         case libvlc_track_video:
1017             return vlc_fourcc_GetDescription( VIDEO_ES, i_codec );
1018         case libvlc_track_text:
1019             return vlc_fourcc_GetDescription( SPU_ES, i_codec );
1020         case libvlc_track_unknown:
1021         default:
1022             return vlc_fourcc_GetDescription( UNKNOWN_ES, i_codec );
1023     }
1024 }
1025
1026 /**************************************************************************
1027  * Release media descriptor's elementary streams description array
1028  **************************************************************************/
1029 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
1030 {
1031     for( unsigned i = 0; i < i_count; ++i )
1032     {
1033         if ( !p_tracks[i] )
1034             continue;
1035         free( p_tracks[i]->psz_language );
1036         free( p_tracks[i]->psz_description );
1037         switch( p_tracks[i]->i_type )
1038         {
1039         case libvlc_track_audio:
1040             break;
1041         case libvlc_track_video:
1042             break;
1043         case libvlc_track_text:
1044             free( p_tracks[i]->subtitle->psz_encoding );
1045             break;
1046         case libvlc_track_unknown:
1047         default:
1048             break;
1049         }
1050         free( p_tracks[i]->audio );
1051         free( p_tracks[i] );
1052     }
1053     free( p_tracks );
1054 }
1055
1056 /**************************************************************************
1057  * Get the media type of the media descriptor object
1058  **************************************************************************/
1059 libvlc_media_type_t libvlc_media_get_type( libvlc_media_t *p_md )
1060 {
1061     assert( p_md );
1062
1063     int i_type;
1064     input_item_t *p_input_item = p_md->p_input_item;
1065
1066     vlc_mutex_lock( &p_input_item->lock );
1067     i_type = p_md->p_input_item->i_type;
1068     vlc_mutex_unlock( &p_input_item->lock );
1069
1070     switch( i_type )
1071     {
1072     case ITEM_TYPE_FILE:
1073         return libvlc_media_type_file;
1074     case ITEM_TYPE_NODE:
1075     case ITEM_TYPE_DIRECTORY:
1076         return libvlc_media_type_directory;
1077     case ITEM_TYPE_DISC:
1078         return libvlc_media_type_disc;
1079     case ITEM_TYPE_STREAM:
1080         return libvlc_media_type_stream;
1081     case ITEM_TYPE_PLAYLIST:
1082         return libvlc_media_type_playlist;
1083     default:
1084         return libvlc_media_type_unknown;
1085     }
1086 }
1087
1088 int libvlc_media_slaves_add( libvlc_media_t *p_md,
1089                              libvlc_media_slave_type_t i_type,
1090                              unsigned int i_priority,
1091                              const char *psz_uri )
1092 {
1093     assert( p_md && psz_uri );
1094     input_item_t *p_input_item = p_md->p_input_item;
1095
1096     enum slave_type i_input_slave_type;
1097     switch( i_type )
1098     {
1099     case libvlc_media_slave_type_subtitle:
1100         i_input_slave_type = SLAVE_TYPE_SPU;
1101         break;
1102     case libvlc_media_slave_type_audio:
1103         i_input_slave_type = SLAVE_TYPE_AUDIO;
1104         break;
1105     default:
1106         vlc_assert_unreachable();
1107         return -1;
1108     }
1109
1110     enum slave_priority i_input_slave_priority;
1111     switch( i_priority )
1112     {
1113     case 0:
1114         i_input_slave_priority = SLAVE_PRIORITY_MATCH_NONE;
1115         break;
1116     case 1:
1117         i_input_slave_priority = SLAVE_PRIORITY_MATCH_RIGHT;
1118         break;
1119     case 2:
1120         i_input_slave_priority = SLAVE_PRIORITY_MATCH_LEFT;
1121         break;
1122     case 3:
1123         i_input_slave_priority = SLAVE_PRIORITY_MATCH_ALL;
1124         break;
1125     default:
1126     case 4:
1127         i_input_slave_priority = SLAVE_PRIORITY_USER;
1128         break;
1129     }
1130
1131     input_item_slave_t *p_slave = input_item_slave_New( psz_uri,
1132                                                       i_input_slave_type,
1133                                                       i_input_slave_priority );
1134     if( p_slave == NULL )
1135         return -1;
1136     return input_item_AddSlave( p_input_item, p_slave ) == VLC_SUCCESS ? 0 : -1;
1137 }
1138
1139 void libvlc_media_slaves_clear( libvlc_media_t *p_md )
1140 {
1141     assert( p_md );
1142     input_item_t *p_input_item = p_md->p_input_item;
1143
1144     vlc_mutex_lock( &p_input_item->lock );
1145     for( int i = 0; i < p_input_item->i_slaves; i++ )
1146         input_item_slave_Delete( p_input_item->pp_slaves[i] );
1147     TAB_CLEAN( p_input_item->i_slaves, p_input_item->pp_slaves );
1148     vlc_mutex_unlock( &p_input_item->lock );
1149 }
1150
1151 unsigned int libvlc_media_slaves_get( libvlc_media_t *p_md,
1152                                       libvlc_media_slave_t ***ppp_slaves )
1153 {
1154     assert( p_md && ppp_slaves );
1155     input_item_t *p_input_item = p_md->p_input_item;
1156
1157     vlc_mutex_lock( &p_input_item->lock );
1158
1159     int i_count = p_input_item->i_slaves;
1160     if( i_count <= 0 )
1161         return vlc_mutex_unlock( &p_input_item->lock ), 0;
1162
1163     libvlc_media_slave_t **pp_slaves = calloc( i_count, sizeof(*pp_slaves) );
1164     if( pp_slaves == NULL )
1165         return vlc_mutex_unlock( &p_input_item->lock ), 0;
1166
1167     for( int i = 0; i < i_count; ++i )
1168     {
1169         input_item_slave_t *p_item_slave = p_input_item->pp_slaves[i];
1170         assert( p_item_slave->i_priority >= SLAVE_PRIORITY_MATCH_NONE );
1171
1172         /* also allocate psz_uri buffer at the end of the struct */
1173         libvlc_media_slave_t *p_slave = malloc( sizeof(*p_slave) +
1174                                                 strlen( p_item_slave->psz_uri )
1175                                                 + 1 );
1176         if( p_slave == NULL )
1177         {
1178             libvlc_media_slaves_release(pp_slaves, i);
1179             return vlc_mutex_unlock( &p_input_item->lock ), 0;
1180         }
1181         p_slave->psz_uri = (char *) ((uint8_t *)p_slave) + sizeof(*p_slave);
1182         strcpy( p_slave->psz_uri, p_item_slave->psz_uri );
1183
1184         switch( p_item_slave->i_type )
1185         {
1186         case SLAVE_TYPE_SPU:
1187             p_slave->i_type = libvlc_media_slave_type_subtitle;
1188             break;
1189         case SLAVE_TYPE_AUDIO:
1190             p_slave->i_type = libvlc_media_slave_type_audio;
1191             break;
1192         default:
1193             vlc_assert_unreachable();
1194         }
1195
1196         switch( p_item_slave->i_priority )
1197         {
1198         case SLAVE_PRIORITY_MATCH_NONE:
1199             p_slave->i_priority = 0;
1200             break;
1201         case SLAVE_PRIORITY_MATCH_RIGHT:
1202             p_slave->i_priority = 1;
1203             break;
1204         case SLAVE_PRIORITY_MATCH_LEFT:
1205             p_slave->i_priority = 2;
1206             break;
1207         case SLAVE_PRIORITY_MATCH_ALL:
1208             p_slave->i_priority = 3;
1209             break;
1210         case SLAVE_PRIORITY_USER:
1211             p_slave->i_priority = 4;
1212             break;
1213         default:
1214             vlc_assert_unreachable();
1215         }
1216         pp_slaves[i] = p_slave;
1217     }
1218     vlc_mutex_unlock( &p_input_item->lock );
1219
1220     *ppp_slaves = pp_slaves;
1221     return i_count;
1222 }
1223
1224 void libvlc_media_slaves_release( libvlc_media_slave_t **pp_slaves,
1225                                   unsigned int i_count )
1226 {
1227     if( i_count > 0 )
1228     {
1229         assert( pp_slaves );
1230         for( unsigned int i = 0; i < i_count; ++i )
1231             free( pp_slaves[i] );
1232         free( pp_slaves );
1233     }
1234 }