hw:d3d9: use d3d9_device_t instead of IDirect3DDevice9
[vlc.git] / lib / media.c
blobaf246c6c7e111b28aea419fec6bb61b27700a17d
1 /*****************************************************************************
2 * media.c: Libvlc API media descripor management
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Pierre d'Herbemont <pdherbemont@videolan.org>
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.
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.
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
29 #include <errno.h>
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>
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>
42 #include "../src/libvlc.h"
44 #include "libvlc_internal.h"
45 #include "media_internal.h"
46 #include "media_list_internal.h"
48 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
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
78 static const libvlc_meta_t vlc_to_libvlc_meta[] =
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
109 static_assert(
110 ORIENT_TOP_LEFT == (int) libvlc_video_orient_top_left &&
111 ORIENT_TOP_RIGHT == (int) libvlc_video_orient_top_right &&
112 ORIENT_BOTTOM_LEFT == (int) libvlc_video_orient_bottom_left &&
113 ORIENT_BOTTOM_RIGHT == (int) libvlc_video_orient_bottom_right &&
114 ORIENT_LEFT_TOP == (int) libvlc_video_orient_left_top &&
115 ORIENT_LEFT_BOTTOM == (int) libvlc_video_orient_left_bottom &&
116 ORIENT_RIGHT_TOP == (int) libvlc_video_orient_right_top &&
117 ORIENT_RIGHT_BOTTOM == (int) libvlc_video_orient_right_bottom,
118 "Mismatch between libvlc_video_orient_t and video_orientation_t" );
120 static_assert(
121 PROJECTION_MODE_RECTANGULAR == (int) libvlc_video_projection_rectangular &&
122 PROJECTION_MODE_EQUIRECTANGULAR == (int) libvlc_video_projection_equirectangular &&
123 PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD == (int) libvlc_video_projection_cubemap_layout_standard,
124 "Mismatch between libvlc_video_projection_t and video_projection_mode_t" );
126 static libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md,
127 bool b_create )
129 libvlc_media_list_t *p_subitems = NULL;
131 vlc_mutex_lock( &p_md->subitems_lock );
132 if( p_md->p_subitems == NULL && b_create )
134 p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance );
135 if( p_md->p_subitems != NULL )
137 p_md->p_subitems->b_read_only = true;
138 p_md->p_subitems->p_internal_md = p_md;
141 p_subitems = p_md->p_subitems;
142 vlc_mutex_unlock( &p_md->subitems_lock );
143 return p_subitems;
146 static libvlc_media_t *input_item_add_subitem( libvlc_media_t *p_md,
147 input_item_t *item )
149 libvlc_media_t * p_md_child;
150 libvlc_media_list_t *p_subitems;
151 libvlc_event_t event;
153 p_md_child = libvlc_media_new_from_input_item( p_md->p_libvlc_instance,
154 item );
156 /* Add this to our media list */
157 p_subitems = media_get_subitems( p_md, true );
158 if( p_subitems != NULL )
160 libvlc_media_list_lock( p_subitems );
161 libvlc_media_list_internal_add_media( p_subitems, p_md_child );
162 libvlc_media_list_unlock( p_subitems );
165 /* Construct the event */
166 event.type = libvlc_MediaSubItemAdded;
167 event.u.media_subitem_added.new_child = p_md_child;
169 /* Send the event */
170 libvlc_event_send( &p_md->event_manager, &event );
171 return p_md_child;
174 static void input_item_add_subnode( libvlc_media_t *md,
175 input_item_node_t *node )
177 for( int i = 0; i < node->i_children; i++ )
179 input_item_node_t *child = node->pp_children[i];
180 libvlc_media_t *md_child = input_item_add_subitem( md, child->p_item );
182 if( md_child != NULL )
184 input_item_add_subnode( md_child, child );
185 libvlc_media_release( md_child );
190 /**************************************************************************
191 * input_item_subitemtree_added (Private) (vlc event Callback)
192 **************************************************************************/
193 static void input_item_subitemtree_added( const vlc_event_t * p_event,
194 void * user_data )
196 libvlc_media_t * p_md = user_data;
197 libvlc_event_t event;
198 input_item_node_t *node = p_event->u.input_item_subitem_tree_added.p_root;
200 /* FIXME FIXME FIXME
201 * Recursive function calls seem much simpler for this. But playlists are
202 * untrusted and can be arbitrarily deep (e.g. with XSPF). So recursion can
203 * potentially lead to plain old stack overflow. */
204 input_item_add_subnode( p_md, node );
206 /* Construct the event */
207 event.type = libvlc_MediaSubItemTreeAdded;
208 event.u.media_subitemtree_added.item = p_md;
210 /* Send the event */
211 libvlc_event_send( &p_md->event_manager, &event );
214 /**************************************************************************
215 * input_item_meta_changed (Private) (vlc event Callback)
216 **************************************************************************/
217 static void input_item_meta_changed( const vlc_event_t *p_event,
218 void * user_data )
220 libvlc_media_t * p_md = user_data;
221 libvlc_event_t event;
223 /* Construct the event */
224 event.type = libvlc_MediaMetaChanged;
225 event.u.media_meta_changed.meta_type =
226 vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
228 /* Send the event */
229 libvlc_event_send( &p_md->event_manager, &event );
232 /**************************************************************************
233 * input_item_duration_changed (Private) (vlc event Callback)
234 **************************************************************************/
235 static void input_item_duration_changed( const vlc_event_t *p_event,
236 void * user_data )
238 libvlc_media_t * p_md = user_data;
239 libvlc_event_t event;
241 /* Construct the event */
242 event.type = libvlc_MediaDurationChanged;
243 event.u.media_duration_changed.new_duration =
244 from_mtime(p_event->u.input_item_duration_changed.new_duration);
246 /* Send the event */
247 libvlc_event_send( &p_md->event_manager, &event );
250 static void send_parsed_changed( libvlc_media_t *p_md,
251 libvlc_media_parsed_status_t new_status )
253 libvlc_event_t event;
255 vlc_mutex_lock( &p_md->parsed_lock );
256 if( p_md->parsed_status == new_status )
258 vlc_mutex_unlock( &p_md->parsed_lock );
259 return;
262 /* Legacy: notify libvlc_media_parse */
263 if( !p_md->is_parsed )
265 p_md->is_parsed = true;
266 vlc_cond_broadcast( &p_md->parsed_cond );
269 p_md->parsed_status = new_status;
270 if( p_md->parsed_status == libvlc_media_parsed_status_skipped )
271 p_md->has_asked_preparse = false;
273 vlc_mutex_unlock( &p_md->parsed_lock );
275 if( new_status == libvlc_media_parsed_status_done )
277 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, false );
278 if( p_subitems != NULL )
280 /* notify the media list */
281 libvlc_media_list_lock( p_subitems );
282 libvlc_media_list_internal_end_reached( p_subitems );
283 libvlc_media_list_unlock( p_subitems );
287 /* Construct the event */
288 event.type = libvlc_MediaParsedChanged;
289 event.u.media_parsed_changed.new_status = new_status;
291 /* Send the event */
292 libvlc_event_send( &p_md->event_manager, &event );
295 /**************************************************************************
296 * input_item_preparse_ended (Private) (vlc event Callback)
297 **************************************************************************/
298 static void input_item_preparse_ended( const vlc_event_t * p_event,
299 void * user_data )
301 libvlc_media_t * p_md = user_data;
302 libvlc_media_parsed_status_t new_status;
304 switch( p_event->u.input_item_preparse_ended.new_status )
306 case ITEM_PREPARSE_SKIPPED:
307 new_status = libvlc_media_parsed_status_skipped;
308 break;
309 case ITEM_PREPARSE_FAILED:
310 new_status = libvlc_media_parsed_status_failed;
311 break;
312 case ITEM_PREPARSE_TIMEOUT:
313 new_status = libvlc_media_parsed_status_timeout;
314 break;
315 case ITEM_PREPARSE_DONE:
316 new_status = libvlc_media_parsed_status_done;
317 break;
318 default:
319 return;
321 send_parsed_changed( p_md, new_status );
324 /**************************************************************************
325 * Install event handler (Private)
326 **************************************************************************/
327 static void install_input_item_observer( libvlc_media_t *p_md )
329 vlc_event_attach( &p_md->p_input_item->event_manager,
330 vlc_InputItemMetaChanged,
331 input_item_meta_changed,
332 p_md );
333 vlc_event_attach( &p_md->p_input_item->event_manager,
334 vlc_InputItemDurationChanged,
335 input_item_duration_changed,
336 p_md );
337 vlc_event_attach( &p_md->p_input_item->event_manager,
338 vlc_InputItemSubItemTreeAdded,
339 input_item_subitemtree_added,
340 p_md );
341 vlc_event_attach( &p_md->p_input_item->event_manager,
342 vlc_InputItemPreparseEnded,
343 input_item_preparse_ended,
344 p_md );
347 /**************************************************************************
348 * Uninstall event handler (Private)
349 **************************************************************************/
350 static void uninstall_input_item_observer( libvlc_media_t *p_md )
352 vlc_event_detach( &p_md->p_input_item->event_manager,
353 vlc_InputItemMetaChanged,
354 input_item_meta_changed,
355 p_md );
356 vlc_event_detach( &p_md->p_input_item->event_manager,
357 vlc_InputItemDurationChanged,
358 input_item_duration_changed,
359 p_md );
360 vlc_event_detach( &p_md->p_input_item->event_manager,
361 vlc_InputItemSubItemTreeAdded,
362 input_item_subitemtree_added,
363 p_md );
364 vlc_event_detach( &p_md->p_input_item->event_manager,
365 vlc_InputItemPreparseEnded,
366 input_item_preparse_ended,
367 p_md );
370 /**************************************************************************
371 * Create a new media descriptor object from an input_item
372 * (libvlc internal)
373 * That's the generic constructor
374 **************************************************************************/
375 libvlc_media_t * libvlc_media_new_from_input_item(
376 libvlc_instance_t *p_instance,
377 input_item_t *p_input_item )
379 libvlc_media_t * p_md;
381 if (!p_input_item)
383 libvlc_printerr( "No input item given" );
384 return NULL;
387 p_md = calloc( 1, sizeof(libvlc_media_t) );
388 if( !p_md )
390 libvlc_printerr( "Not enough memory" );
391 return NULL;
394 p_md->p_libvlc_instance = p_instance;
395 p_md->p_input_item = p_input_item;
396 p_md->i_refcount = 1;
398 vlc_cond_init(&p_md->parsed_cond);
399 vlc_mutex_init(&p_md->parsed_lock);
400 vlc_mutex_init(&p_md->subitems_lock);
402 p_md->state = libvlc_NothingSpecial;
404 /* A media descriptor can be a playlist. When you open a playlist
405 * It can give a bunch of item to read. */
406 p_md->p_subitems = NULL;
408 libvlc_event_manager_init( &p_md->event_manager, p_md );
410 input_item_Hold( p_md->p_input_item );
412 install_input_item_observer( p_md );
414 libvlc_retain( p_instance );
415 return p_md;
418 /**************************************************************************
419 * Create a new media descriptor object
420 **************************************************************************/
421 libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
422 const char * psz_mrl )
424 input_item_t * p_input_item;
425 libvlc_media_t * p_md;
427 p_input_item = input_item_New( psz_mrl, NULL );
429 if (!p_input_item)
431 libvlc_printerr( "Not enough memory" );
432 return NULL;
435 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
437 /* The p_input_item is retained in libvlc_media_new_from_input_item */
438 input_item_Release( p_input_item );
440 return p_md;
443 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
444 const char *path )
446 char *mrl = vlc_path2uri( path, NULL );
447 if( unlikely(mrl == NULL) )
449 libvlc_printerr( "%s", vlc_strerror_c(errno) );
450 return NULL;
453 libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
454 free( mrl );
455 return m;
458 libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
460 char mrl[16];
461 snprintf( mrl, sizeof(mrl), "fd://%d", fd );
463 return libvlc_media_new_location( p_instance, mrl );
466 libvlc_media_t *libvlc_media_new_callbacks(libvlc_instance_t *p_instance,
467 libvlc_media_open_cb open_cb,
468 libvlc_media_read_cb read_cb,
469 libvlc_media_seek_cb seek_cb,
470 libvlc_media_close_cb close_cb,
471 void *opaque)
473 libvlc_media_t *m = libvlc_media_new_location(p_instance, "imem://");
474 if (unlikely(m == NULL))
475 return NULL;
477 assert(read_cb != NULL);
478 input_item_AddOpaque(m->p_input_item, "imem-data", opaque);
479 input_item_AddOpaque(m->p_input_item, "imem-open", open_cb);
480 input_item_AddOpaque(m->p_input_item, "imem-read", read_cb);
481 input_item_AddOpaque(m->p_input_item, "imem-seek", seek_cb);
482 input_item_AddOpaque(m->p_input_item, "imem-close", close_cb);
483 return m;
486 /**************************************************************************
487 * Create a new media descriptor object
488 **************************************************************************/
489 libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
490 const char * psz_name )
492 input_item_t * p_input_item;
493 libvlc_media_t * p_md;
494 libvlc_media_list_t * p_subitems;
496 p_input_item = input_item_New( "vlc://nop", psz_name );
498 if (!p_input_item)
500 libvlc_printerr( "Not enough memory" );
501 return NULL;
504 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
505 input_item_Release( p_input_item );
507 p_subitems = media_get_subitems( p_md, true );
508 if( p_subitems == NULL) {
509 libvlc_media_release( p_md );
510 return NULL;
513 return p_md;
516 /**************************************************************************
517 * Add an option to the media descriptor,
518 * that will be used to determine how the media_player will read the
519 * media. This allow to use VLC advanced reading/streaming
520 * options in a per-media basis
522 * The options are detailled in vlc --long-help, for instance "--sout-all"
523 **************************************************************************/
524 void libvlc_media_add_option( libvlc_media_t * p_md,
525 const char * psz_option )
527 libvlc_media_add_option_flag( p_md, psz_option,
528 VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
531 /**************************************************************************
532 * Same as libvlc_media_add_option but with configurable flags.
533 **************************************************************************/
534 void libvlc_media_add_option_flag( libvlc_media_t * p_md,
535 const char * ppsz_option,
536 unsigned i_flags )
538 input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
541 /**************************************************************************
542 * Delete a media descriptor object
543 **************************************************************************/
544 void libvlc_media_release( libvlc_media_t *p_md )
546 if (!p_md)
547 return;
549 p_md->i_refcount--;
551 if( p_md->i_refcount > 0 )
552 return;
554 uninstall_input_item_observer( p_md );
556 /* Cancel asynchronous parsing (if any) */
557 libvlc_MetadataCancel( p_md->p_libvlc_instance->p_libvlc_int, p_md );
559 if( p_md->p_subitems )
560 libvlc_media_list_release( p_md->p_subitems );
562 input_item_Release( p_md->p_input_item );
564 vlc_cond_destroy( &p_md->parsed_cond );
565 vlc_mutex_destroy( &p_md->parsed_lock );
566 vlc_mutex_destroy( &p_md->subitems_lock );
568 /* Construct the event */
569 libvlc_event_t event;
570 event.type = libvlc_MediaFreed;
571 event.u.media_freed.md = p_md;
573 /* Send the event */
574 libvlc_event_send( &p_md->event_manager, &event );
576 libvlc_event_manager_destroy( &p_md->event_manager );
577 libvlc_release( p_md->p_libvlc_instance );
578 free( p_md );
581 /**************************************************************************
582 * Retain a media descriptor object
583 **************************************************************************/
584 void libvlc_media_retain( libvlc_media_t *p_md )
586 assert (p_md);
587 p_md->i_refcount++;
590 /**************************************************************************
591 * Duplicate a media descriptor object
592 **************************************************************************/
593 libvlc_media_t *
594 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
596 return libvlc_media_new_from_input_item(
597 p_md_orig->p_libvlc_instance, p_md_orig->p_input_item );
600 /**************************************************************************
601 * Get mrl from a media descriptor object
602 **************************************************************************/
603 char *
604 libvlc_media_get_mrl( libvlc_media_t * p_md )
606 assert( p_md );
607 return input_item_GetURI( p_md->p_input_item );
610 /**************************************************************************
611 * Getter for meta information
612 **************************************************************************/
614 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
616 char *psz_meta = NULL;
618 if( e_meta == libvlc_meta_NowPlaying )
620 psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
622 else
624 psz_meta = input_item_GetMeta( p_md->p_input_item,
625 libvlc_to_vlc_meta[e_meta] );
626 /* Should be integrated in core */
627 if( psz_meta == NULL && e_meta == libvlc_meta_Title
628 && p_md->p_input_item->psz_name != NULL )
629 psz_meta = strdup( p_md->p_input_item->psz_name );
631 return psz_meta;
634 /**************************************************************************
635 * Setter for meta information
636 **************************************************************************/
638 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
640 assert( p_md );
641 input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
644 int libvlc_media_save_meta( libvlc_media_t *p_md )
646 assert( p_md );
647 vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
648 return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
651 /**************************************************************************
652 * Getter for state information
653 * Can be error, playing, buffering, NothingSpecial.
654 **************************************************************************/
656 libvlc_state_t
657 libvlc_media_get_state( libvlc_media_t *p_md )
659 assert( p_md );
660 return p_md->state;
663 /**************************************************************************
664 * Setter for state information (LibVLC Internal)
665 **************************************************************************/
667 void
668 libvlc_media_set_state( libvlc_media_t *p_md,
669 libvlc_state_t state )
671 libvlc_event_t event;
673 p_md->state = state;
675 /* Construct the event */
676 event.type = libvlc_MediaStateChanged;
677 event.u.media_state_changed.new_state = state;
679 /* Send the event */
680 libvlc_event_send( &p_md->event_manager, &event );
683 /**************************************************************************
684 * subitems
685 **************************************************************************/
686 libvlc_media_list_t *
687 libvlc_media_subitems( libvlc_media_t * p_md )
689 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
690 if( p_subitems )
691 libvlc_media_list_retain( p_subitems );
692 return p_subitems;
695 /**************************************************************************
696 * Getter for statistics information
697 **************************************************************************/
698 int libvlc_media_get_stats( libvlc_media_t *p_md,
699 libvlc_media_stats_t *p_stats )
701 if( !p_md->p_input_item )
702 return false;
704 input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
705 vlc_mutex_lock( &p_itm_stats->lock );
706 p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
707 p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
709 p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
710 p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
711 p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
712 p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
714 p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
715 p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
717 p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
718 p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
720 p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
721 p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
723 p_stats->i_sent_packets = p_itm_stats->i_sent_packets;
724 p_stats->i_sent_bytes = p_itm_stats->i_sent_bytes;
725 p_stats->f_send_bitrate = p_itm_stats->f_send_bitrate;
726 vlc_mutex_unlock( &p_itm_stats->lock );
727 return true;
730 /**************************************************************************
731 * event_manager
732 **************************************************************************/
733 libvlc_event_manager_t *
734 libvlc_media_event_manager( libvlc_media_t * p_md )
736 assert( p_md );
738 return &p_md->event_manager;
741 /**************************************************************************
742 * Get duration of media object (in ms)
743 **************************************************************************/
744 int64_t
745 libvlc_media_get_duration( libvlc_media_t * p_md )
747 assert( p_md );
749 if( !p_md->p_input_item )
751 libvlc_printerr( "No input item" );
752 return -1;
755 if (!input_item_IsPreparsed( p_md->p_input_item ))
756 return -1;
758 return from_mtime(input_item_GetDuration( p_md->p_input_item ));
761 static int media_parse(libvlc_media_t *media, bool b_async,
762 libvlc_media_parse_flag_t parse_flag, int timeout)
764 bool needed;
766 vlc_mutex_lock(&media->parsed_lock);
767 needed = !media->has_asked_preparse;
768 media->has_asked_preparse = true;
769 if (needed)
770 media->is_parsed = false;
771 vlc_mutex_unlock(&media->parsed_lock);
773 if (needed)
775 libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
776 input_item_t *item = media->p_input_item;
777 input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
778 int ret;
780 /* Ignore libvlc_media_fetch_local flag since local art will be fetched
781 * by libvlc_MetadataRequest */
782 if (parse_flag & libvlc_media_fetch_network)
784 ret = libvlc_ArtRequest(libvlc, item,
785 META_REQUEST_OPTION_SCOPE_NETWORK);
786 if (ret != VLC_SUCCESS)
787 return ret;
790 if (parse_flag & libvlc_media_parse_network)
791 parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
792 if (parse_flag & libvlc_media_do_interact)
793 parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
794 ret = libvlc_MetadataRequest(libvlc, item, parse_scope, timeout, media);
795 if (ret != VLC_SUCCESS)
796 return ret;
798 else
799 return VLC_EGENERIC;
801 if (!b_async)
803 vlc_mutex_lock(&media->parsed_lock);
804 while (!media->is_parsed)
805 vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
806 vlc_mutex_unlock(&media->parsed_lock);
808 return VLC_SUCCESS;
811 /**************************************************************************
812 * Parse the media and wait.
813 **************************************************************************/
814 void
815 libvlc_media_parse(libvlc_media_t *media)
817 media_parse( media, false, libvlc_media_fetch_local, -1 );
820 /**************************************************************************
821 * Parse the media but do not wait.
822 **************************************************************************/
823 void
824 libvlc_media_parse_async(libvlc_media_t *media)
826 media_parse( media, true, libvlc_media_fetch_local, -1 );
829 /**************************************************************************
830 * Parse the media asynchronously with options.
831 **************************************************************************/
833 libvlc_media_parse_with_options( libvlc_media_t *media,
834 libvlc_media_parse_flag_t parse_flag,
835 int timeout )
837 return media_parse( media, true, parse_flag, timeout ) == VLC_SUCCESS ? 0 : -1;
840 void
841 libvlc_media_parse_stop( libvlc_media_t *media )
843 libvlc_MetadataCancel( media->p_libvlc_instance->p_libvlc_int, media );
846 /**************************************************************************
847 * Get parsed status for media object.
848 **************************************************************************/
850 libvlc_media_is_parsed(libvlc_media_t *media)
852 bool parsed;
854 vlc_mutex_lock(&media->parsed_lock);
855 parsed = media->is_parsed;
856 vlc_mutex_unlock(&media->parsed_lock);
857 return parsed;
860 libvlc_media_parsed_status_t
861 libvlc_media_get_parsed_status(libvlc_media_t *media)
863 libvlc_media_parsed_status_t status;
865 vlc_mutex_lock(&media->parsed_lock);
866 status = media->parsed_status;
867 vlc_mutex_unlock(&media->parsed_lock);
868 return status;
871 /**************************************************************************
872 * Sets media descriptor's user_data. user_data is specialized data
873 * accessed by the host application, VLC.framework uses it as a pointer to
874 * an native object that references a libvlc_media_t pointer
875 **************************************************************************/
876 void
877 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
879 assert( p_md );
880 p_md->p_user_data = p_new_user_data;
883 /**************************************************************************
884 * Get media descriptor's user_data. user_data is specialized data
885 * accessed by the host application, VLC.framework uses it as a pointer to
886 * an native object that references a libvlc_media_t pointer
887 **************************************************************************/
888 void *
889 libvlc_media_get_user_data( libvlc_media_t * p_md )
891 assert( p_md );
892 return p_md->p_user_data;
895 /**************************************************************************
896 * Get media descriptor's elementary streams description
897 **************************************************************************/
899 libvlc_media_get_tracks_info( libvlc_media_t *p_md, libvlc_media_track_info_t ** pp_es )
901 assert( p_md );
903 input_item_t *p_input_item = p_md->p_input_item;
904 vlc_mutex_lock( &p_input_item->lock );
906 const int i_es = p_input_item->i_es;
907 *pp_es = (i_es > 0) ? vlc_alloc( i_es, sizeof(libvlc_media_track_info_t) ) : NULL;
909 if( !*pp_es ) /* no ES, or OOM */
911 vlc_mutex_unlock( &p_input_item->lock );
912 return 0;
915 /* Fill array */
916 for( int i = 0; i < i_es; i++ )
918 libvlc_media_track_info_t *p_mes = *pp_es+i;
919 const es_format_t *p_es = p_input_item->es[i];
921 p_mes->i_codec = p_es->i_codec;
922 p_mes->i_id = p_es->i_id;
924 p_mes->i_profile = p_es->i_profile;
925 p_mes->i_level = p_es->i_level;
927 switch(p_es->i_cat)
929 case UNKNOWN_ES:
930 default:
931 p_mes->i_type = libvlc_track_unknown;
932 break;
933 case VIDEO_ES:
934 p_mes->i_type = libvlc_track_video;
935 p_mes->u.video.i_height = p_es->video.i_visible_height;
936 p_mes->u.video.i_width = p_es->video.i_visible_width;
937 break;
938 case AUDIO_ES:
939 p_mes->i_type = libvlc_track_audio;
940 p_mes->u.audio.i_channels = p_es->audio.i_channels;
941 p_mes->u.audio.i_rate = p_es->audio.i_rate;
942 break;
943 case SPU_ES:
944 p_mes->i_type = libvlc_track_text;
945 break;
949 vlc_mutex_unlock( &p_input_item->lock );
950 return i_es;
953 unsigned
954 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
956 assert( p_md );
958 input_item_t *p_input_item = p_md->p_input_item;
959 vlc_mutex_lock( &p_input_item->lock );
961 const int i_es = p_input_item->i_es;
962 *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
964 if( !*pp_es ) /* no ES, or OOM */
966 vlc_mutex_unlock( &p_input_item->lock );
967 return 0;
970 /* Fill array */
971 for( int i = 0; i < i_es; i++ )
973 libvlc_media_track_t *p_mes = calloc( 1, sizeof(*p_mes) );
974 if ( p_mes )
976 p_mes->audio = malloc( __MAX(__MAX(sizeof(*p_mes->audio),
977 sizeof(*p_mes->video)),
978 sizeof(*p_mes->subtitle)) );
980 if ( !p_mes || !p_mes->audio )
982 libvlc_media_tracks_release( *pp_es, i_es );
983 *pp_es = NULL;
984 free( p_mes );
985 vlc_mutex_unlock( &p_input_item->lock );
986 return 0;
988 (*pp_es)[i] = p_mes;
990 const es_format_t *p_es = p_input_item->es[i];
992 p_mes->i_codec = p_es->i_codec;
993 p_mes->i_original_fourcc = p_es->i_original_fourcc;
994 p_mes->i_id = p_es->i_id;
996 p_mes->i_profile = p_es->i_profile;
997 p_mes->i_level = p_es->i_level;
999 p_mes->i_bitrate = p_es->i_bitrate;
1000 p_mes->psz_language = p_es->psz_language != NULL ? strdup(p_es->psz_language) : NULL;
1001 p_mes->psz_description = p_es->psz_description != NULL ? strdup(p_es->psz_description) : NULL;
1003 switch(p_es->i_cat)
1005 case UNKNOWN_ES:
1006 default:
1007 p_mes->i_type = libvlc_track_unknown;
1008 break;
1009 case VIDEO_ES:
1010 p_mes->i_type = libvlc_track_video;
1011 p_mes->video->i_height = p_es->video.i_visible_height;
1012 p_mes->video->i_width = p_es->video.i_visible_width;
1013 p_mes->video->i_sar_num = p_es->video.i_sar_num;
1014 p_mes->video->i_sar_den = p_es->video.i_sar_den;
1015 p_mes->video->i_frame_rate_num = p_es->video.i_frame_rate;
1016 p_mes->video->i_frame_rate_den = p_es->video.i_frame_rate_base;
1018 assert( p_es->video.orientation >= ORIENT_TOP_LEFT &&
1019 p_es->video.orientation <= ORIENT_RIGHT_BOTTOM );
1020 p_mes->video->i_orientation = (int) p_es->video.orientation;
1022 assert( ( p_es->video.projection_mode >= PROJECTION_MODE_RECTANGULAR &&
1023 p_es->video.projection_mode <= PROJECTION_MODE_EQUIRECTANGULAR ) ||
1024 ( p_es->video.projection_mode == PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD ) );
1025 p_mes->video->i_projection = (int) p_es->video.projection_mode;
1027 p_mes->video->pose.f_yaw = p_es->video.pose.yaw;
1028 p_mes->video->pose.f_pitch = p_es->video.pose.pitch;
1029 p_mes->video->pose.f_roll = p_es->video.pose.roll;
1030 p_mes->video->pose.f_field_of_view = p_es->video.pose.fov;
1031 break;
1032 case AUDIO_ES:
1033 p_mes->i_type = libvlc_track_audio;
1034 p_mes->audio->i_channels = p_es->audio.i_channels;
1035 p_mes->audio->i_rate = p_es->audio.i_rate;
1036 break;
1037 case SPU_ES:
1038 p_mes->i_type = libvlc_track_text;
1039 p_mes->subtitle->psz_encoding = p_es->subs.psz_encoding != NULL ?
1040 strdup(p_es->subs.psz_encoding) : NULL;
1041 break;
1045 vlc_mutex_unlock( &p_input_item->lock );
1046 return i_es;
1049 /**************************************************************************
1050 * Get codec description from media elementary stream
1051 **************************************************************************/
1052 const char *
1053 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
1054 uint32_t i_codec )
1056 switch( i_type )
1058 case libvlc_track_audio:
1059 return vlc_fourcc_GetDescription( AUDIO_ES, i_codec );
1060 case libvlc_track_video:
1061 return vlc_fourcc_GetDescription( VIDEO_ES, i_codec );
1062 case libvlc_track_text:
1063 return vlc_fourcc_GetDescription( SPU_ES, i_codec );
1064 case libvlc_track_unknown:
1065 default:
1066 return vlc_fourcc_GetDescription( UNKNOWN_ES, i_codec );
1070 /**************************************************************************
1071 * Release media descriptor's elementary streams description array
1072 **************************************************************************/
1073 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
1075 for( unsigned i = 0; i < i_count; ++i )
1077 if ( !p_tracks[i] )
1078 continue;
1079 free( p_tracks[i]->psz_language );
1080 free( p_tracks[i]->psz_description );
1081 switch( p_tracks[i]->i_type )
1083 case libvlc_track_audio:
1084 break;
1085 case libvlc_track_video:
1086 break;
1087 case libvlc_track_text:
1088 free( p_tracks[i]->subtitle->psz_encoding );
1089 break;
1090 case libvlc_track_unknown:
1091 default:
1092 break;
1094 free( p_tracks[i]->audio );
1095 free( p_tracks[i] );
1097 free( p_tracks );
1100 /**************************************************************************
1101 * Get the media type of the media descriptor object
1102 **************************************************************************/
1103 libvlc_media_type_t libvlc_media_get_type( libvlc_media_t *p_md )
1105 assert( p_md );
1107 int i_type;
1108 input_item_t *p_input_item = p_md->p_input_item;
1110 vlc_mutex_lock( &p_input_item->lock );
1111 i_type = p_md->p_input_item->i_type;
1112 vlc_mutex_unlock( &p_input_item->lock );
1114 switch( i_type )
1116 case ITEM_TYPE_FILE:
1117 return libvlc_media_type_file;
1118 case ITEM_TYPE_NODE:
1119 case ITEM_TYPE_DIRECTORY:
1120 return libvlc_media_type_directory;
1121 case ITEM_TYPE_DISC:
1122 return libvlc_media_type_disc;
1123 case ITEM_TYPE_STREAM:
1124 return libvlc_media_type_stream;
1125 case ITEM_TYPE_PLAYLIST:
1126 return libvlc_media_type_playlist;
1127 default:
1128 return libvlc_media_type_unknown;
1132 int libvlc_media_slaves_add( libvlc_media_t *p_md,
1133 libvlc_media_slave_type_t i_type,
1134 unsigned int i_priority,
1135 const char *psz_uri )
1137 assert( p_md && psz_uri );
1138 input_item_t *p_input_item = p_md->p_input_item;
1140 enum slave_type i_input_slave_type;
1141 switch( i_type )
1143 case libvlc_media_slave_type_subtitle:
1144 i_input_slave_type = SLAVE_TYPE_SPU;
1145 break;
1146 case libvlc_media_slave_type_audio:
1147 i_input_slave_type = SLAVE_TYPE_AUDIO;
1148 break;
1149 default:
1150 vlc_assert_unreachable();
1151 return -1;
1154 enum slave_priority i_input_slave_priority;
1155 switch( i_priority )
1157 case 0:
1158 i_input_slave_priority = SLAVE_PRIORITY_MATCH_NONE;
1159 break;
1160 case 1:
1161 i_input_slave_priority = SLAVE_PRIORITY_MATCH_RIGHT;
1162 break;
1163 case 2:
1164 i_input_slave_priority = SLAVE_PRIORITY_MATCH_LEFT;
1165 break;
1166 case 3:
1167 i_input_slave_priority = SLAVE_PRIORITY_MATCH_ALL;
1168 break;
1169 default:
1170 case 4:
1171 i_input_slave_priority = SLAVE_PRIORITY_USER;
1172 break;
1175 input_item_slave_t *p_slave = input_item_slave_New( psz_uri,
1176 i_input_slave_type,
1177 i_input_slave_priority );
1178 if( p_slave == NULL )
1179 return -1;
1180 return input_item_AddSlave( p_input_item, p_slave ) == VLC_SUCCESS ? 0 : -1;
1183 void libvlc_media_slaves_clear( libvlc_media_t *p_md )
1185 assert( p_md );
1186 input_item_t *p_input_item = p_md->p_input_item;
1188 vlc_mutex_lock( &p_input_item->lock );
1189 for( int i = 0; i < p_input_item->i_slaves; i++ )
1190 input_item_slave_Delete( p_input_item->pp_slaves[i] );
1191 TAB_CLEAN( p_input_item->i_slaves, p_input_item->pp_slaves );
1192 vlc_mutex_unlock( &p_input_item->lock );
1195 unsigned int libvlc_media_slaves_get( libvlc_media_t *p_md,
1196 libvlc_media_slave_t ***ppp_slaves )
1198 assert( p_md && ppp_slaves );
1199 input_item_t *p_input_item = p_md->p_input_item;
1200 *ppp_slaves = NULL;
1202 vlc_mutex_lock( &p_input_item->lock );
1204 int i_count = p_input_item->i_slaves;
1205 if( i_count <= 0 )
1206 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1208 libvlc_media_slave_t **pp_slaves = calloc( i_count, sizeof(*pp_slaves) );
1209 if( pp_slaves == NULL )
1210 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1212 for( int i = 0; i < i_count; ++i )
1214 input_item_slave_t *p_item_slave = p_input_item->pp_slaves[i];
1215 assert( p_item_slave->i_priority >= SLAVE_PRIORITY_MATCH_NONE );
1217 /* also allocate psz_uri buffer at the end of the struct */
1218 libvlc_media_slave_t *p_slave = malloc( sizeof(*p_slave) +
1219 strlen( p_item_slave->psz_uri )
1220 + 1 );
1221 if( p_slave == NULL )
1223 libvlc_media_slaves_release(pp_slaves, i);
1224 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1226 p_slave->psz_uri = (char *) ((uint8_t *)p_slave) + sizeof(*p_slave);
1227 strcpy( p_slave->psz_uri, p_item_slave->psz_uri );
1229 switch( p_item_slave->i_type )
1231 case SLAVE_TYPE_SPU:
1232 p_slave->i_type = libvlc_media_slave_type_subtitle;
1233 break;
1234 case SLAVE_TYPE_AUDIO:
1235 p_slave->i_type = libvlc_media_slave_type_audio;
1236 break;
1237 default:
1238 vlc_assert_unreachable();
1241 switch( p_item_slave->i_priority )
1243 case SLAVE_PRIORITY_MATCH_NONE:
1244 p_slave->i_priority = 0;
1245 break;
1246 case SLAVE_PRIORITY_MATCH_RIGHT:
1247 p_slave->i_priority = 1;
1248 break;
1249 case SLAVE_PRIORITY_MATCH_LEFT:
1250 p_slave->i_priority = 2;
1251 break;
1252 case SLAVE_PRIORITY_MATCH_ALL:
1253 p_slave->i_priority = 3;
1254 break;
1255 case SLAVE_PRIORITY_USER:
1256 p_slave->i_priority = 4;
1257 break;
1258 default:
1259 vlc_assert_unreachable();
1261 pp_slaves[i] = p_slave;
1263 vlc_mutex_unlock( &p_input_item->lock );
1265 *ppp_slaves = pp_slaves;
1266 return i_count;
1269 void libvlc_media_slaves_release( libvlc_media_slave_t **pp_slaves,
1270 unsigned int i_count )
1272 if( i_count > 0 )
1274 assert( pp_slaves );
1275 for( unsigned int i = 0; i < i_count; ++i )
1276 free( pp_slaves[i] );
1278 free( pp_slaves );