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