libvlc: fix libvlc_media_slaves_release with a count of 0
[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     if( p_md->p_subitems )
532         libvlc_media_list_release( p_md->p_subitems );
533
534     vlc_gc_decref( p_md->p_input_item );
535
536     vlc_cond_destroy( &p_md->parsed_cond );
537     vlc_mutex_destroy( &p_md->parsed_lock );
538     vlc_mutex_destroy( &p_md->subitems_lock );
539
540     /* Construct the event */
541     libvlc_event_t event;
542     event.type = libvlc_MediaFreed;
543     event.u.media_freed.md = p_md;
544
545     /* Send the event */
546     libvlc_event_send( p_md->p_event_manager, &event );
547
548     libvlc_event_manager_release( p_md->p_event_manager );
549     libvlc_release( p_md->p_libvlc_instance );
550     free( p_md );
551 }
552
553 /**************************************************************************
554  * Retain a media descriptor object
555  **************************************************************************/
556 void libvlc_media_retain( libvlc_media_t *p_md )
557 {
558     assert (p_md);
559     p_md->i_refcount++;
560 }
561
562 /**************************************************************************
563  * Duplicate a media descriptor object
564  **************************************************************************/
565 libvlc_media_t *
566 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
567 {
568     return libvlc_media_new_from_input_item(
569         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item );
570 }
571
572 /**************************************************************************
573  * Get mrl from a media descriptor object
574  **************************************************************************/
575 char *
576 libvlc_media_get_mrl( libvlc_media_t * p_md )
577 {
578     assert( p_md );
579     return input_item_GetURI( p_md->p_input_item );
580 }
581
582 /**************************************************************************
583  * Getter for meta information
584  **************************************************************************/
585
586 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
587 {
588     char *psz_meta = NULL;
589
590     if( e_meta == libvlc_meta_NowPlaying )
591     {
592         psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
593     }
594     else
595     {
596         psz_meta = input_item_GetMeta( p_md->p_input_item,
597                                              libvlc_to_vlc_meta[e_meta] );
598         /* Should be integrated in core */
599         if( psz_meta == NULL && e_meta == libvlc_meta_Title
600          && p_md->p_input_item->psz_name != NULL )
601             psz_meta = strdup( p_md->p_input_item->psz_name );
602     }
603     return psz_meta;
604 }
605
606 /**************************************************************************
607  * Setter for meta information
608  **************************************************************************/
609
610 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
611 {
612     assert( p_md );
613     input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
614 }
615
616 int libvlc_media_save_meta( libvlc_media_t *p_md )
617 {
618     assert( p_md );
619     vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
620     return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
621 }
622
623 /**************************************************************************
624  * Getter for state information
625  * Can be error, playing, buffering, NothingSpecial.
626  **************************************************************************/
627
628 libvlc_state_t
629 libvlc_media_get_state( libvlc_media_t *p_md )
630 {
631     assert( p_md );
632     return p_md->state;
633 }
634
635 /**************************************************************************
636  * Setter for state information (LibVLC Internal)
637  **************************************************************************/
638
639 void
640 libvlc_media_set_state( libvlc_media_t *p_md,
641                                    libvlc_state_t state )
642 {
643     libvlc_event_t event;
644
645     p_md->state = state;
646
647     /* Construct the event */
648     event.type = libvlc_MediaStateChanged;
649     event.u.media_state_changed.new_state = state;
650
651     /* Send the event */
652     libvlc_event_send( p_md->p_event_manager, &event );
653 }
654
655 /**************************************************************************
656  * subitems
657  **************************************************************************/
658 libvlc_media_list_t *
659 libvlc_media_subitems( libvlc_media_t * p_md )
660 {
661     libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
662     if( p_subitems )
663         libvlc_media_list_retain( p_subitems );
664     return p_subitems;
665 }
666
667 /**************************************************************************
668  * Getter for statistics information
669  **************************************************************************/
670 int libvlc_media_get_stats( libvlc_media_t *p_md,
671                             libvlc_media_stats_t *p_stats )
672 {
673     if( !p_md->p_input_item )
674         return false;
675
676     input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
677     vlc_mutex_lock( &p_itm_stats->lock );
678     p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
679     p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
680
681     p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
682     p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
683     p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
684     p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
685
686     p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
687     p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
688
689     p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
690     p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
691
692     p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
693     p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
694
695     p_stats->i_sent_packets = p_itm_stats->i_sent_packets;
696     p_stats->i_sent_bytes = p_itm_stats->i_sent_bytes;
697     p_stats->f_send_bitrate = p_itm_stats->f_send_bitrate;
698     vlc_mutex_unlock( &p_itm_stats->lock );
699     return true;
700 }
701
702 /**************************************************************************
703  * event_manager
704  **************************************************************************/
705 libvlc_event_manager_t *
706 libvlc_media_event_manager( libvlc_media_t * p_md )
707 {
708     assert( p_md );
709
710     return p_md->p_event_manager;
711 }
712
713 /**************************************************************************
714  * Get duration of media object (in ms)
715  **************************************************************************/
716 int64_t
717 libvlc_media_get_duration( libvlc_media_t * p_md )
718 {
719     assert( p_md );
720
721     if( !p_md->p_input_item )
722     {
723         libvlc_printerr( "No input item" );
724         return -1;
725     }
726
727     if (!input_item_IsPreparsed( p_md->p_input_item ))
728         return -1;
729
730     return from_mtime(input_item_GetDuration( p_md->p_input_item ));
731 }
732
733 static int media_parse(libvlc_media_t *media, bool b_async,
734                        libvlc_media_parse_flag_t parse_flag)
735 {
736     bool needed;
737
738     vlc_mutex_lock(&media->parsed_lock);
739     needed = !media->has_asked_preparse;
740     media->has_asked_preparse = true;
741     if (needed)
742         media->is_parsed = false;
743     vlc_mutex_unlock(&media->parsed_lock);
744
745     if (needed)
746     {
747         libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
748         input_item_t *item = media->p_input_item;
749         input_item_meta_request_option_t art_scope = META_REQUEST_OPTION_NONE;
750         input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
751         int ret;
752
753         if (parse_flag & libvlc_media_fetch_local)
754             art_scope |= META_REQUEST_OPTION_SCOPE_LOCAL;
755         if (parse_flag & libvlc_media_fetch_network)
756             art_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
757         if (art_scope != META_REQUEST_OPTION_NONE) {
758             ret = libvlc_ArtRequest(libvlc, item, art_scope);
759             if (ret != VLC_SUCCESS)
760                 return ret;
761         }
762
763         if (parse_flag & libvlc_media_parse_network)
764             parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
765         if (parse_flag & libvlc_media_do_interact)
766             parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
767         ret = libvlc_MetaRequest(libvlc, item, parse_scope);
768         if (ret != VLC_SUCCESS)
769             return ret;
770     }
771     else
772         return VLC_EGENERIC;
773
774     if (!b_async)
775     {
776         vlc_mutex_lock(&media->parsed_lock);
777         while (!media->is_parsed)
778             vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
779         vlc_mutex_unlock(&media->parsed_lock);
780     }
781     return VLC_SUCCESS;
782 }
783
784 /**************************************************************************
785  * Parse the media and wait.
786  **************************************************************************/
787 void
788 libvlc_media_parse(libvlc_media_t *media)
789 {
790     media_parse( media, false, libvlc_media_fetch_local );
791 }
792
793 /**************************************************************************
794  * Parse the media but do not wait.
795  **************************************************************************/
796 void
797 libvlc_media_parse_async(libvlc_media_t *media)
798 {
799     media_parse( media, true, libvlc_media_fetch_local );
800 }
801
802 /**************************************************************************
803  * Parse the media asynchronously with options.
804  **************************************************************************/
805 int
806 libvlc_media_parse_with_options( libvlc_media_t *media,
807                                  libvlc_media_parse_flag_t parse_flag )
808 {
809     return media_parse( media, true, parse_flag ) == VLC_SUCCESS ? 0 : -1;
810 }
811
812 /**************************************************************************
813  * Get parsed status for media object.
814  **************************************************************************/
815 int
816 libvlc_media_is_parsed(libvlc_media_t *media)
817 {
818     bool parsed;
819
820     vlc_mutex_lock(&media->parsed_lock);
821     parsed = media->is_parsed;
822     vlc_mutex_unlock(&media->parsed_lock);
823     return parsed;
824 }
825
826 libvlc_media_parsed_status_t
827 libvlc_media_get_parsed_status(libvlc_media_t *media)
828 {
829     libvlc_media_parsed_status_t status;
830
831     vlc_mutex_lock(&media->parsed_lock);
832     status = media->parsed_status;
833     vlc_mutex_unlock(&media->parsed_lock);
834     return status;
835 }
836
837 /**************************************************************************
838  * Sets media descriptor's user_data. user_data is specialized data
839  * accessed by the host application, VLC.framework uses it as a pointer to
840  * an native object that references a libvlc_media_t pointer
841  **************************************************************************/
842 void
843 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
844 {
845     assert( p_md );
846     p_md->p_user_data = p_new_user_data;
847 }
848
849 /**************************************************************************
850  * Get media descriptor's user_data. user_data is specialized data
851  * accessed by the host application, VLC.framework uses it as a pointer to
852  * an native object that references a libvlc_media_t pointer
853  **************************************************************************/
854 void *
855 libvlc_media_get_user_data( libvlc_media_t * p_md )
856 {
857     assert( p_md );
858     return p_md->p_user_data;
859 }
860
861 /**************************************************************************
862  * Get media descriptor's elementary streams description
863  **************************************************************************/
864 int
865 libvlc_media_get_tracks_info( libvlc_media_t *p_md, libvlc_media_track_info_t ** pp_es )
866 {
867     assert( p_md );
868
869     input_item_t *p_input_item = p_md->p_input_item;
870     vlc_mutex_lock( &p_input_item->lock );
871
872     const int i_es = p_input_item->i_es;
873     *pp_es = (i_es > 0) ? malloc( i_es * sizeof(libvlc_media_track_info_t) ) : NULL;
874
875     if( !*pp_es ) /* no ES, or OOM */
876     {
877         vlc_mutex_unlock( &p_input_item->lock );
878         return 0;
879     }
880
881     /* Fill array */
882     for( int i = 0; i < i_es; i++ )
883     {
884         libvlc_media_track_info_t *p_mes = *pp_es+i;
885         const es_format_t *p_es = p_input_item->es[i];
886
887         p_mes->i_codec = p_es->i_codec;
888         p_mes->i_id = p_es->i_id;
889
890         p_mes->i_profile = p_es->i_profile;
891         p_mes->i_level = p_es->i_level;
892
893         switch(p_es->i_cat)
894         {
895         case UNKNOWN_ES:
896         default:
897             p_mes->i_type = libvlc_track_unknown;
898             break;
899         case VIDEO_ES:
900             p_mes->i_type = libvlc_track_video;
901             p_mes->u.video.i_height = p_es->video.i_visible_height;
902             p_mes->u.video.i_width = p_es->video.i_visible_width;
903             break;
904         case AUDIO_ES:
905             p_mes->i_type = libvlc_track_audio;
906             p_mes->u.audio.i_channels = p_es->audio.i_channels;
907             p_mes->u.audio.i_rate = p_es->audio.i_rate;
908             break;
909         case SPU_ES:
910             p_mes->i_type = libvlc_track_text;
911             break;
912         }
913     }
914
915     vlc_mutex_unlock( &p_input_item->lock );
916     return i_es;
917 }
918
919 unsigned
920 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
921 {
922     assert( p_md );
923
924     input_item_t *p_input_item = p_md->p_input_item;
925     vlc_mutex_lock( &p_input_item->lock );
926
927     const int i_es = p_input_item->i_es;
928     *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
929
930     if( !*pp_es ) /* no ES, or OOM */
931     {
932         vlc_mutex_unlock( &p_input_item->lock );
933         return 0;
934     }
935
936     /* Fill array */
937     for( int i = 0; i < i_es; i++ )
938     {
939         libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
940         if ( p_mes )
941         {
942             p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
943                                                sizeof(*p_mes->video)),
944                                                sizeof(*p_mes->subtitle)) );
945         }
946         if ( !p_mes || !p_mes->audio )
947         {
948             libvlc_media_tracks_release( *pp_es, i_es );
949             *pp_es = NULL;
950             free( p_mes );
951             vlc_mutex_unlock( &p_input_item->lock );
952             return 0;
953         }
954         (*pp_es)[i] = p_mes;
955
956         const es_format_t *p_es = p_input_item->es[i];
957
958         p_mes->i_codec = p_es->i_codec;
959         p_mes->i_original_fourcc = p_es->i_original_fourcc;
960         p_mes->i_id = p_es->i_id;
961
962         p_mes->i_profile = p_es->i_profile;
963         p_mes->i_level = p_es->i_level;
964
965         p_mes->i_bitrate = p_es->i_bitrate;
966         p_mes->psz_language = p_es->psz_language != NULL ? strdup(p_es->psz_language) : NULL;
967         p_mes->psz_description = p_es->psz_description != NULL ? strdup(p_es->psz_description) : NULL;
968
969         switch(p_es->i_cat)
970         {
971         case UNKNOWN_ES:
972         default:
973             p_mes->i_type = libvlc_track_unknown;
974             break;
975         case VIDEO_ES:
976             p_mes->i_type = libvlc_track_video;
977             p_mes->video->i_height = p_es->video.i_visible_height;
978             p_mes->video->i_width = p_es->video.i_visible_width;
979             p_mes->video->i_sar_num = p_es->video.i_sar_num;
980             p_mes->video->i_sar_den = p_es->video.i_sar_den;
981             p_mes->video->i_frame_rate_num = p_es->video.i_frame_rate;
982             p_mes->video->i_frame_rate_den = p_es->video.i_frame_rate_base;
983             break;
984         case AUDIO_ES:
985             p_mes->i_type = libvlc_track_audio;
986             p_mes->audio->i_channels = p_es->audio.i_channels;
987             p_mes->audio->i_rate = p_es->audio.i_rate;
988             break;
989         case SPU_ES:
990             p_mes->i_type = libvlc_track_text;
991             p_mes->subtitle->psz_encoding = p_es->subs.psz_encoding != NULL ?
992                                             strdup(p_es->subs.psz_encoding) : NULL;
993             break;
994         }
995     }
996
997     vlc_mutex_unlock( &p_input_item->lock );
998     return i_es;
999 }
1000
1001 /**************************************************************************
1002  * Get codec description from media elementary stream
1003  **************************************************************************/
1004 const char *
1005 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
1006                                     uint32_t i_codec )
1007 {
1008     switch( i_type )
1009     {
1010         case libvlc_track_audio:
1011             return vlc_fourcc_GetDescription( AUDIO_ES, i_codec );
1012         case libvlc_track_video:
1013             return vlc_fourcc_GetDescription( VIDEO_ES, i_codec );
1014         case libvlc_track_text:
1015             return vlc_fourcc_GetDescription( SPU_ES, i_codec );
1016         case libvlc_track_unknown:
1017         default:
1018             return vlc_fourcc_GetDescription( UNKNOWN_ES, i_codec );
1019     }
1020 }
1021
1022 /**************************************************************************
1023  * Release media descriptor's elementary streams description array
1024  **************************************************************************/
1025 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
1026 {
1027     for( unsigned i = 0; i < i_count; ++i )
1028     {
1029         if ( !p_tracks[i] )
1030             continue;
1031         free( p_tracks[i]->psz_language );
1032         free( p_tracks[i]->psz_description );
1033         switch( p_tracks[i]->i_type )
1034         {
1035         case libvlc_track_audio:
1036             break;
1037         case libvlc_track_video:
1038             break;
1039         case libvlc_track_text:
1040             free( p_tracks[i]->subtitle->psz_encoding );
1041             break;
1042         case libvlc_track_unknown:
1043         default:
1044             break;
1045         }
1046         free( p_tracks[i]->audio );
1047         free( p_tracks[i] );
1048     }
1049     free( p_tracks );
1050 }
1051
1052 /**************************************************************************
1053  * Get the media type of the media descriptor object
1054  **************************************************************************/
1055 libvlc_media_type_t libvlc_media_get_type( libvlc_media_t *p_md )
1056 {
1057     assert( p_md );
1058
1059     int i_type;
1060     input_item_t *p_input_item = p_md->p_input_item;
1061
1062     vlc_mutex_lock( &p_input_item->lock );
1063     i_type = p_md->p_input_item->i_type;
1064     vlc_mutex_unlock( &p_input_item->lock );
1065
1066     switch( i_type )
1067     {
1068     case ITEM_TYPE_FILE:
1069         return libvlc_media_type_file;
1070     case ITEM_TYPE_NODE:
1071     case ITEM_TYPE_DIRECTORY:
1072         return libvlc_media_type_directory;
1073     case ITEM_TYPE_DISC:
1074         return libvlc_media_type_disc;
1075     case ITEM_TYPE_STREAM:
1076         return libvlc_media_type_stream;
1077     case ITEM_TYPE_PLAYLIST:
1078         return libvlc_media_type_playlist;
1079     default:
1080         return libvlc_media_type_unknown;
1081     }
1082 }
1083
1084 int libvlc_media_slaves_add( libvlc_media_t *p_md,
1085                              libvlc_media_slave_type_t i_type,
1086                              unsigned int i_priority,
1087                              const char *psz_uri )
1088 {
1089     assert( p_md && psz_uri );
1090     input_item_t *p_input_item = p_md->p_input_item;
1091
1092     enum slave_type i_input_slave_type;
1093     switch( i_type )
1094     {
1095     case libvlc_media_slave_type_subtitle:
1096         i_input_slave_type = SLAVE_TYPE_SPU;
1097         break;
1098     case libvlc_media_slave_type_audio:
1099         i_input_slave_type = SLAVE_TYPE_AUDIO;
1100         break;
1101     default:
1102         vlc_assert_unreachable();
1103         return -1;
1104     }
1105
1106     enum slave_priority i_input_slave_priority;
1107     switch( i_priority )
1108     {
1109     case 0:
1110         i_input_slave_priority = SLAVE_PRIORITY_MATCH_NONE;
1111         break;
1112     case 1:
1113         i_input_slave_priority = SLAVE_PRIORITY_MATCH_RIGHT;
1114         break;
1115     case 2:
1116         i_input_slave_priority = SLAVE_PRIORITY_MATCH_LEFT;
1117         break;
1118     case 3:
1119         i_input_slave_priority = SLAVE_PRIORITY_MATCH_ALL;
1120         break;
1121     default:
1122     case 4:
1123         i_input_slave_priority = SLAVE_PRIORITY_USER;
1124         break;
1125     }
1126
1127     input_item_slave_t *p_slave = input_item_slave_New( psz_uri,
1128                                                       i_input_slave_type,
1129                                                       i_input_slave_priority );
1130     if( p_slave == NULL )
1131         return -1;
1132     return input_item_AddSlave( p_input_item, p_slave ) == VLC_SUCCESS ? 0 : -1;
1133 }
1134
1135 void libvlc_media_slaves_clear( libvlc_media_t *p_md )
1136 {
1137     assert( p_md );
1138     input_item_t *p_input_item = p_md->p_input_item;
1139
1140     vlc_mutex_lock( &p_input_item->lock );
1141     for( int i = 0; i < p_input_item->i_slaves; i++ )
1142         input_item_slave_Delete( p_input_item->pp_slaves[i] );
1143     TAB_CLEAN( p_input_item->i_slaves, p_input_item->pp_slaves );
1144     vlc_mutex_unlock( &p_input_item->lock );
1145 }
1146
1147 unsigned int libvlc_media_slaves_get( libvlc_media_t *p_md,
1148                                       libvlc_media_slave_t ***ppp_slaves )
1149 {
1150     assert( p_md && ppp_slaves );
1151     input_item_t *p_input_item = p_md->p_input_item;
1152
1153     vlc_mutex_lock( &p_input_item->lock );
1154
1155     int i_count = p_input_item->i_slaves;
1156     if( i_count <= 0 )
1157         return vlc_mutex_unlock( &p_input_item->lock ), 0;
1158
1159     libvlc_media_slave_t **pp_slaves = calloc( i_count, sizeof(*pp_slaves) );
1160     if( pp_slaves == NULL )
1161         return vlc_mutex_unlock( &p_input_item->lock ), 0;
1162
1163     for( int i = 0; i < i_count; ++i )
1164     {
1165         input_item_slave_t *p_item_slave = p_input_item->pp_slaves[i];
1166         assert( p_item_slave->i_priority >= SLAVE_PRIORITY_MATCH_NONE );
1167
1168         /* also allocate psz_uri buffer at the end of the struct */
1169         libvlc_media_slave_t *p_slave = malloc( sizeof(*p_slave) +
1170                                                 strlen( p_item_slave->psz_uri )
1171                                                 + 1 );
1172         if( p_slave == NULL )
1173         {
1174             libvlc_media_slaves_release(pp_slaves, i);
1175             return vlc_mutex_unlock( &p_input_item->lock ), 0;
1176         }
1177         p_slave->psz_uri = (char *) ((uint8_t *)p_slave) + sizeof(*p_slave);
1178         strcpy( p_slave->psz_uri, p_item_slave->psz_uri );
1179
1180         switch( p_item_slave->i_type )
1181         {
1182         case SLAVE_TYPE_SPU:
1183             p_slave->i_type = libvlc_media_slave_type_subtitle;
1184             break;
1185         case SLAVE_TYPE_AUDIO:
1186             p_slave->i_type = libvlc_media_slave_type_audio;
1187             break;
1188         default:
1189             vlc_assert_unreachable();
1190         }
1191
1192         switch( p_item_slave->i_priority )
1193         {
1194         case SLAVE_PRIORITY_MATCH_NONE:
1195             p_slave->i_priority = 0;
1196             break;
1197         case SLAVE_PRIORITY_MATCH_RIGHT:
1198             p_slave->i_priority = 1;
1199             break;
1200         case SLAVE_PRIORITY_MATCH_LEFT:
1201             p_slave->i_priority = 2;
1202             break;
1203         case SLAVE_PRIORITY_MATCH_ALL:
1204             p_slave->i_priority = 3;
1205             break;
1206         case SLAVE_PRIORITY_USER:
1207             p_slave->i_priority = 4;
1208             break;
1209         default:
1210             vlc_assert_unreachable();
1211         }
1212         pp_slaves[i] = p_slave;
1213     }
1214     vlc_mutex_unlock( &p_input_item->lock );
1215
1216     *ppp_slaves = pp_slaves;
1217     return i_count;
1218 }
1219
1220 void libvlc_media_slaves_release( libvlc_media_slave_t **pp_slaves,
1221                                   unsigned int i_count )
1222 {
1223     if( i_count > 0 )
1224     {
1225         assert( pp_slaves );
1226         for( unsigned int i = 0; i < i_count; ++i )
1227             free( pp_slaves[i] );
1228         free( pp_slaves );
1229     }
1230 }