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