coreaudio: fix play of uninitialized data (loud CRACK)
[vlc.git] / lib / media.c
blob2175c04b98d35eb947be8ec8bf1c2b9418232113
1 /*****************************************************************************
2 * media.c: Libvlc API media descripor management
3 *****************************************************************************
4 * Copyright (C) 2007 VLC authors and VideoLAN
6 * Authors: Pierre d'Herbemont <pdherbemont@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <assert.h>
28 #include <errno.h>
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_picture.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_meta.h>
38 #include <vlc_url.h>
39 #include <vlc_thumbnailer.h>
41 #include "../src/libvlc.h"
43 #include "libvlc_internal.h"
44 #include "media_internal.h"
45 #include "media_list_internal.h"
46 #include "picture_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_assert(
127 MULTIVIEW_2D == (int) libvlc_video_multiview_2d &&
128 MULTIVIEW_STEREO_SBS == (int) libvlc_video_multiview_stereo_sbs &&
129 MULTIVIEW_STEREO_TB == (int) libvlc_video_multiview_stereo_tb &&
130 MULTIVIEW_STEREO_ROW == (int) libvlc_video_multiview_stereo_row &&
131 MULTIVIEW_STEREO_COL == (int) libvlc_video_multiview_stereo_col &&
132 MULTIVIEW_STEREO_FRAME == (int) libvlc_video_multiview_stereo_frame &&
133 MULTIVIEW_STEREO_CHECKERBOARD == (int) libvlc_video_multiview_stereo_checkerboard,
134 "Mismatch between libvlc_video_multiview_t and video_multiview_mode_t");
136 static libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md,
137 bool b_create )
139 libvlc_media_list_t *p_subitems = NULL;
141 vlc_mutex_lock( &p_md->subitems_lock );
142 if( p_md->p_subitems == NULL && b_create )
144 p_md->p_subitems = libvlc_media_list_new();
145 if( p_md->p_subitems != NULL )
147 p_md->p_subitems->b_read_only = true;
148 p_md->p_subitems->p_internal_md = p_md;
151 p_subitems = p_md->p_subitems;
152 vlc_mutex_unlock( &p_md->subitems_lock );
153 return p_subitems;
156 static libvlc_media_t *input_item_add_subitem( libvlc_media_t *p_md,
157 input_item_t *item )
159 libvlc_media_t * p_md_child;
160 libvlc_media_list_t *p_subitems;
161 libvlc_event_t event;
163 p_md_child = libvlc_media_new_from_input_item( p_md->p_libvlc_instance,
164 item );
166 /* Add this to our media list */
167 p_subitems = media_get_subitems( p_md, true );
168 if( p_subitems != NULL )
170 libvlc_media_list_lock( p_subitems );
171 libvlc_media_list_internal_add_media( p_subitems, p_md_child );
172 libvlc_media_list_unlock( p_subitems );
175 /* Construct the event */
176 event.type = libvlc_MediaSubItemAdded;
177 event.u.media_subitem_added.new_child = p_md_child;
179 /* Send the event */
180 libvlc_event_send( &p_md->event_manager, &event );
181 return p_md_child;
184 struct vlc_item_list
186 struct vlc_list node;
187 input_item_node_t *item;
188 libvlc_media_t *media;
191 static struct vlc_item_list *
192 wrap_item_in_list( libvlc_media_t *media, input_item_node_t *item )
194 struct vlc_item_list *node = malloc( sizeof *node );
195 if( node == NULL )
196 return NULL;
197 node->item = item;
198 node->media = media;
199 return node;
202 static void input_item_add_subnode( libvlc_media_t *md,
203 input_item_node_t *root )
205 struct vlc_list list;
206 vlc_list_init( &list );
208 /* Retain the media because we don't want the search algorithm to release
209 * it when its subitems get parsed. */
210 libvlc_media_retain(md);
212 struct vlc_item_list *node_root = wrap_item_in_list( md, root );
213 if( node_root == NULL )
215 libvlc_media_release(md);
216 goto error;
219 /* This is a depth-first search algorithm, so stash the root of the tree
220 * first, then stash its children and loop back on the last item in the
221 * list until the full subtree is parsed, and eventually the full tree is
222 * parsed. */
223 vlc_list_append( &node_root->node, &list );
225 while( !vlc_list_is_empty( &list ) )
227 /* Pop last item in the list. */
228 struct vlc_item_list *node =
229 vlc_list_last_entry_or_null( &list, struct vlc_item_list, node );
230 vlc_list_remove(&node->node);
232 for( int i = 0; i < node->item->i_children; i++ )
234 input_item_node_t *child = node->item->pp_children[i];
236 /* The media will be released when its children will be added to
237 * the list. */
238 libvlc_media_t *md_child = input_item_add_subitem( node->media, child->p_item );
239 if( md_child == NULL )
240 goto error;
242 struct vlc_item_list *submedia =
243 wrap_item_in_list( md_child, child );
244 if (submedia == NULL)
246 libvlc_media_release( md_child );
247 goto error;
250 /* Stash a request to parse this subtree. */
251 vlc_list_append( &submedia->node, &list );
254 libvlc_media_release( node->media );
255 free( node );
257 return;
259 error:
260 libvlc_printerr( "Not enough memory" );
262 struct vlc_item_list *node;
263 vlc_list_foreach( node, &list, node )
265 if( node->media != NULL )
266 libvlc_media_release( node->media );
267 free( node );
272 * \internal
273 * input_item_subitemtree_added (Private) (vlc event Callback)
275 static void input_item_subtree_added(input_item_t *item,
276 input_item_node_t *node,
277 void *user_data)
279 VLC_UNUSED(item);
280 libvlc_media_t * p_md = user_data;
281 libvlc_media_add_subtree(p_md, node);
284 void libvlc_media_add_subtree(libvlc_media_t *p_md, input_item_node_t *node)
286 input_item_add_subnode( p_md, node );
288 /* Construct the event */
289 libvlc_event_t event;
290 event.type = libvlc_MediaSubItemTreeAdded;
291 event.u.media_subitemtree_added.item = p_md;
293 /* Send the event */
294 libvlc_event_send( &p_md->event_manager, &event );
298 * \internal
299 * input_item_meta_changed (Private) (vlc event Callback)
301 static void input_item_meta_changed( const vlc_event_t *p_event,
302 void * user_data )
304 libvlc_media_t * p_md = user_data;
305 libvlc_event_t event;
307 /* Construct the event */
308 event.type = libvlc_MediaMetaChanged;
309 event.u.media_meta_changed.meta_type =
310 vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
312 /* Send the event */
313 libvlc_event_send( &p_md->event_manager, &event );
317 * \internal
318 * input_item_duration_changed (Private) (vlc event Callback)
320 static void input_item_duration_changed( const vlc_event_t *p_event,
321 void * user_data )
323 libvlc_media_t * p_md = user_data;
324 libvlc_event_t event;
326 /* Construct the event */
327 event.type = libvlc_MediaDurationChanged;
328 event.u.media_duration_changed.new_duration =
329 from_mtime(p_event->u.input_item_duration_changed.new_duration);
331 /* Send the event */
332 libvlc_event_send( &p_md->event_manager, &event );
335 static void send_parsed_changed( libvlc_media_t *p_md,
336 libvlc_media_parsed_status_t new_status )
338 libvlc_event_t event;
340 vlc_mutex_lock( &p_md->parsed_lock );
341 if( p_md->parsed_status == new_status )
343 vlc_mutex_unlock( &p_md->parsed_lock );
344 return;
347 /* Legacy: notify libvlc_media_parse */
348 if( !p_md->is_parsed )
350 p_md->is_parsed = true;
351 vlc_cond_broadcast( &p_md->parsed_cond );
354 p_md->parsed_status = new_status;
355 if( p_md->parsed_status != libvlc_media_parsed_status_done )
356 p_md->has_asked_preparse = false;
358 vlc_mutex_unlock( &p_md->parsed_lock );
360 if( new_status == libvlc_media_parsed_status_done )
362 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, false );
363 if( p_subitems != NULL )
365 /* notify the media list */
366 libvlc_media_list_lock( p_subitems );
367 libvlc_media_list_internal_end_reached( p_subitems );
368 libvlc_media_list_unlock( p_subitems );
372 /* Construct the event */
373 event.type = libvlc_MediaParsedChanged;
374 event.u.media_parsed_changed.new_status = new_status;
376 /* Send the event */
377 libvlc_event_send( &p_md->event_manager, &event );
381 * \internal
382 * input_item_preparse_ended (Private) (vlc event Callback)
384 static void input_item_preparse_ended(input_item_t *item,
385 enum input_item_preparse_status status,
386 void *user_data)
388 VLC_UNUSED(item);
389 libvlc_media_t * p_md = user_data;
390 libvlc_media_parsed_status_t new_status;
392 switch( status )
394 case ITEM_PREPARSE_SKIPPED:
395 new_status = libvlc_media_parsed_status_skipped;
396 break;
397 case ITEM_PREPARSE_FAILED:
398 new_status = libvlc_media_parsed_status_failed;
399 break;
400 case ITEM_PREPARSE_TIMEOUT:
401 new_status = libvlc_media_parsed_status_timeout;
402 break;
403 case ITEM_PREPARSE_DONE:
404 new_status = libvlc_media_parsed_status_done;
405 break;
406 default:
407 return;
409 send_parsed_changed( p_md, new_status );
413 * \internal
414 * Install event handler (Private)
416 static void install_input_item_observer( libvlc_media_t *p_md )
418 vlc_event_attach( &p_md->p_input_item->event_manager,
419 vlc_InputItemMetaChanged,
420 input_item_meta_changed,
421 p_md );
422 vlc_event_attach( &p_md->p_input_item->event_manager,
423 vlc_InputItemDurationChanged,
424 input_item_duration_changed,
425 p_md );
429 * \internal
430 * Uninstall event handler (Private)
432 static void uninstall_input_item_observer( libvlc_media_t *p_md )
434 vlc_event_detach( &p_md->p_input_item->event_manager,
435 vlc_InputItemMetaChanged,
436 input_item_meta_changed,
437 p_md );
438 vlc_event_detach( &p_md->p_input_item->event_manager,
439 vlc_InputItemDurationChanged,
440 input_item_duration_changed,
441 p_md );
445 * \internal
446 * Create a new media descriptor object from an input_item (Private)
448 * That's the generic constructor
450 libvlc_media_t * libvlc_media_new_from_input_item(
451 libvlc_instance_t *p_instance,
452 input_item_t *p_input_item )
454 libvlc_media_t * p_md;
456 if (!p_input_item)
458 libvlc_printerr( "No input item given" );
459 return NULL;
462 p_md = calloc( 1, sizeof(libvlc_media_t) );
463 if( !p_md )
465 libvlc_printerr( "Not enough memory" );
466 return NULL;
469 p_md->p_libvlc_instance = p_instance;
470 p_md->p_input_item = p_input_item;
471 p_md->i_refcount = 1;
473 vlc_cond_init(&p_md->parsed_cond);
474 vlc_mutex_init(&p_md->parsed_lock);
475 vlc_mutex_init(&p_md->subitems_lock);
477 p_md->state = libvlc_NothingSpecial;
479 /* A media descriptor can be a playlist. When you open a playlist
480 * It can give a bunch of item to read. */
481 p_md->p_subitems = NULL;
482 p_md->p_input_item->libvlc_owner = p_md;
484 libvlc_event_manager_init( &p_md->event_manager, p_md );
486 input_item_Hold( p_md->p_input_item );
488 install_input_item_observer( p_md );
490 libvlc_retain( p_instance );
491 return p_md;
494 // Create a media with a certain given media resource location
495 libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
496 const char * psz_mrl )
498 input_item_t * p_input_item;
499 libvlc_media_t * p_md;
501 p_input_item = input_item_New( psz_mrl, NULL );
503 if (!p_input_item)
505 libvlc_printerr( "Not enough memory" );
506 return NULL;
509 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
511 /* The p_input_item is retained in libvlc_media_new_from_input_item */
512 input_item_Release( p_input_item );
514 return p_md;
517 // Create a media for a certain file path
518 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
519 const char *path )
521 char *mrl = vlc_path2uri( path, NULL );
522 if( unlikely(mrl == NULL) )
524 libvlc_printerr( "%s", vlc_strerror_c(errno) );
525 return NULL;
528 libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
529 free( mrl );
530 return m;
533 // Create a media for an already open file descriptor
534 libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
536 char mrl[16];
537 snprintf( mrl, sizeof(mrl), "fd://%d", fd );
539 return libvlc_media_new_location( p_instance, mrl );
542 // Create a media with custom callbacks to read the data from
543 libvlc_media_t *libvlc_media_new_callbacks(libvlc_instance_t *p_instance,
544 libvlc_media_open_cb open_cb,
545 libvlc_media_read_cb read_cb,
546 libvlc_media_seek_cb seek_cb,
547 libvlc_media_close_cb close_cb,
548 void *opaque)
550 libvlc_media_t *m = libvlc_media_new_location(p_instance, "imem://");
551 if (unlikely(m == NULL))
552 return NULL;
554 assert(read_cb != NULL);
555 input_item_AddOpaque(m->p_input_item, "imem-data", opaque);
556 input_item_AddOpaque(m->p_input_item, "imem-open", open_cb);
557 input_item_AddOpaque(m->p_input_item, "imem-read", read_cb);
558 input_item_AddOpaque(m->p_input_item, "imem-seek", seek_cb);
559 input_item_AddOpaque(m->p_input_item, "imem-close", close_cb);
560 return m;
563 // Create a media as an empty node with a given name
564 libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
565 const char * psz_name )
567 input_item_t * p_input_item;
568 libvlc_media_t * p_md;
569 libvlc_media_list_t * p_subitems;
571 p_input_item = input_item_New( INPUT_ITEM_URI_NOP, psz_name );
573 if (!p_input_item)
575 libvlc_printerr( "Not enough memory" );
576 return NULL;
579 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
580 input_item_Release( p_input_item );
582 p_subitems = media_get_subitems( p_md, true );
583 if( p_subitems == NULL) {
584 libvlc_media_release( p_md );
585 return NULL;
588 return p_md;
591 // Add an option to the media descriptor
592 void libvlc_media_add_option( libvlc_media_t * p_md,
593 const char * psz_option )
595 libvlc_media_add_option_flag( p_md, psz_option,
596 VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
599 // Same as libvlc_media_add_option but with configurable flags
600 void libvlc_media_add_option_flag( libvlc_media_t * p_md,
601 const char * ppsz_option,
602 unsigned i_flags )
604 input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
607 // Delete a media descriptor object
608 void libvlc_media_release( libvlc_media_t *p_md )
610 if (!p_md)
611 return;
613 p_md->i_refcount--;
615 if( p_md->i_refcount > 0 )
616 return;
618 uninstall_input_item_observer( p_md );
620 /* Cancel asynchronous parsing (if any) */
621 libvlc_MetadataCancel( p_md->p_libvlc_instance->p_libvlc_int, p_md );
623 if( p_md->p_subitems )
624 libvlc_media_list_release( p_md->p_subitems );
626 input_item_Release( p_md->p_input_item );
628 /* Construct the event */
629 libvlc_event_t event;
630 event.type = libvlc_MediaFreed;
631 event.u.media_freed.md = p_md;
633 /* Send the event */
634 libvlc_event_send( &p_md->event_manager, &event );
636 libvlc_event_manager_destroy( &p_md->event_manager );
637 libvlc_release( p_md->p_libvlc_instance );
638 free( p_md );
641 // Retain a media descriptor object
642 void libvlc_media_retain( libvlc_media_t *p_md )
644 assert (p_md);
645 p_md->i_refcount++;
648 // Duplicate a media descriptor object
649 libvlc_media_t *
650 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
653 input_item_t *dup = input_item_Copy( p_md_orig->p_input_item );
654 if( dup == NULL )
655 return NULL;
656 return libvlc_media_new_from_input_item( p_md_orig->p_libvlc_instance, dup );
659 // Get mrl from a media descriptor object
660 char *
661 libvlc_media_get_mrl( libvlc_media_t * p_md )
663 assert( p_md );
664 return input_item_GetURI( p_md->p_input_item );
667 // Getter for meta information
668 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
670 char *psz_meta = NULL;
672 if( e_meta == libvlc_meta_NowPlaying )
674 psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
676 else
678 psz_meta = input_item_GetMeta( p_md->p_input_item,
679 libvlc_to_vlc_meta[e_meta] );
680 /* Should be integrated in core */
681 if( psz_meta == NULL && e_meta == libvlc_meta_Title
682 && p_md->p_input_item->psz_name != NULL )
683 psz_meta = strdup( p_md->p_input_item->psz_name );
685 return psz_meta;
688 // Set the meta of the media
689 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
691 assert( p_md );
692 input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
695 // Save the meta previously set
696 int libvlc_media_save_meta( libvlc_media_t *p_md )
698 assert( p_md );
699 vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
700 return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
703 // Getter for state information
704 libvlc_state_t
705 libvlc_media_get_state( libvlc_media_t *p_md )
707 assert( p_md );
708 return p_md->state;
711 // Setter for state information (LibVLC Internal)
712 void
713 libvlc_media_set_state( libvlc_media_t *p_md,
714 libvlc_state_t state )
716 libvlc_event_t event;
718 p_md->state = state;
720 /* Construct the event */
721 event.type = libvlc_MediaStateChanged;
722 event.u.media_state_changed.new_state = state;
724 /* Send the event */
725 libvlc_event_send( &p_md->event_manager, &event );
728 // Get subitems of media descriptor object.
729 libvlc_media_list_t *
730 libvlc_media_subitems( libvlc_media_t * p_md )
732 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
733 if( p_subitems )
734 libvlc_media_list_retain( p_subitems );
735 return p_subitems;
738 // Getter for statistics information
739 bool libvlc_media_get_stats(libvlc_media_t *p_md,
740 libvlc_media_stats_t *p_stats)
742 input_item_t *item = p_md->p_input_item;
744 if( !p_md->p_input_item )
745 return false;
747 vlc_mutex_lock( &item->lock );
749 input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
750 if( p_itm_stats == NULL )
752 vlc_mutex_unlock( &item->lock );
753 return false;
756 p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
757 p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
759 p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
760 p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
761 p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
762 p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
764 p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
765 p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
767 p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
768 p_stats->i_late_pictures = p_itm_stats->i_late_pictures;
769 p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
771 p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
772 p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
774 vlc_mutex_unlock( &item->lock );
775 return true;
778 // Get event manager from a media descriptor object
779 libvlc_event_manager_t *
780 libvlc_media_event_manager( libvlc_media_t * p_md )
782 assert( p_md );
784 return &p_md->event_manager;
787 // Get duration of media object (in ms)
788 int64_t
789 libvlc_media_get_duration( libvlc_media_t * p_md )
791 assert( p_md );
793 if( !p_md->p_input_item )
795 libvlc_printerr( "No input item" );
796 return -1;
799 if (!input_item_IsPreparsed( p_md->p_input_item ))
800 return -1;
802 return from_mtime(input_item_GetDuration( p_md->p_input_item ));
805 static const input_preparser_callbacks_t input_preparser_callbacks = {
806 .on_preparse_ended = input_item_preparse_ended,
807 .on_subtree_added = input_item_subtree_added,
810 static int media_parse(libvlc_media_t *media, bool b_async,
811 libvlc_media_parse_flag_t parse_flag, int timeout)
813 bool needed;
815 vlc_mutex_lock(&media->parsed_lock);
816 needed = !media->has_asked_preparse;
817 media->has_asked_preparse = true;
818 if (needed)
820 media->is_parsed = false;
821 media->parsed_status = 0;
823 vlc_mutex_unlock(&media->parsed_lock);
825 if (needed)
827 libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
828 input_item_t *item = media->p_input_item;
829 input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
830 int ret;
832 if (parse_flag & libvlc_media_parse_network)
833 parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
834 if (parse_flag & libvlc_media_fetch_local)
835 parse_scope |= META_REQUEST_OPTION_FETCH_LOCAL;
836 if (parse_flag & libvlc_media_fetch_network)
837 parse_scope |= META_REQUEST_OPTION_FETCH_NETWORK;
838 if (parse_flag & libvlc_media_do_interact)
839 parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
841 ret = libvlc_MetadataRequest(libvlc, item, parse_scope,
842 &input_preparser_callbacks, media,
843 timeout, media);
844 if (ret != VLC_SUCCESS)
845 return ret;
847 else
848 return VLC_EGENERIC;
850 if (!b_async)
852 vlc_mutex_lock(&media->parsed_lock);
853 while (!media->is_parsed)
854 vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
855 vlc_mutex_unlock(&media->parsed_lock);
857 return VLC_SUCCESS;
860 // Parse the media and wait
861 void
862 libvlc_media_parse(libvlc_media_t *media)
864 media_parse( media, false, libvlc_media_fetch_local, -1 );
867 // Parse the media but do not wait
868 void
869 libvlc_media_parse_async(libvlc_media_t *media)
871 media_parse( media, true, libvlc_media_fetch_local, -1 );
874 // Parse the media asynchronously with options
876 libvlc_media_parse_with_options( libvlc_media_t *media,
877 libvlc_media_parse_flag_t parse_flag,
878 int timeout )
880 return media_parse( media, true, parse_flag, timeout ) == VLC_SUCCESS ? 0 : -1;
883 // Stop parsing of the media
884 void
885 libvlc_media_parse_stop( libvlc_media_t *media )
887 libvlc_MetadataCancel( media->p_libvlc_instance->p_libvlc_int, media );
890 // Get parsed status for media object (deprecated)
891 bool libvlc_media_is_parsed(libvlc_media_t *media)
893 bool parsed;
895 vlc_mutex_lock(&media->parsed_lock);
896 parsed = media->is_parsed;
897 vlc_mutex_unlock(&media->parsed_lock);
898 return parsed;
901 // Get Parsed status for media descriptor object
902 libvlc_media_parsed_status_t
903 libvlc_media_get_parsed_status(libvlc_media_t *media)
905 libvlc_media_parsed_status_t status;
907 vlc_mutex_lock(&media->parsed_lock);
908 status = media->parsed_status;
909 vlc_mutex_unlock(&media->parsed_lock);
910 return status;
913 // Sets media descriptor's user_data
914 void
915 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
917 assert( p_md );
918 p_md->p_user_data = p_new_user_data;
921 // Get media descriptor's user_data
922 void *
923 libvlc_media_get_user_data( libvlc_media_t * p_md )
925 assert( p_md );
926 return p_md->p_user_data;
929 // Get media descriptor's elementary streams description
930 unsigned
931 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
933 assert( p_md );
935 input_item_t *p_input_item = p_md->p_input_item;
936 vlc_mutex_lock( &p_input_item->lock );
938 const int i_es = p_input_item->i_es;
939 *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
941 if( !*pp_es ) /* no ES, or OOM */
943 vlc_mutex_unlock( &p_input_item->lock );
944 return 0;
947 /* Fill array */
948 for( int i = 0; i < i_es; i++ )
950 libvlc_media_trackpriv_t *p_trackpriv = calloc( 1, sizeof(*p_trackpriv) );
951 if ( !p_trackpriv )
953 libvlc_media_tracks_release( *pp_es, i_es );
954 *pp_es = NULL;
955 vlc_mutex_unlock( &p_input_item->lock );
956 return 0;
958 libvlc_media_track_t *p_mes = &p_trackpriv->t;
959 (*pp_es)[i] = p_mes;
961 const es_format_t *p_es = p_input_item->es[i];
963 libvlc_media_trackpriv_from_es( p_trackpriv, p_es );
966 vlc_mutex_unlock( &p_input_item->lock );
967 return i_es;
970 libvlc_media_tracklist_t *
971 libvlc_media_get_tracklist( libvlc_media_t *p_md, libvlc_track_type_t type )
973 assert( p_md );
975 input_item_t *p_input_item = p_md->p_input_item;
977 vlc_mutex_lock( &p_input_item->lock );
978 libvlc_media_tracklist_t *list =
979 libvlc_media_tracklist_from_es_array( p_input_item->es,
980 p_input_item->i_es, type );
981 vlc_mutex_unlock( &p_input_item->lock );
983 return list;
986 // Get codec description from media elementary stream
987 const char *
988 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
989 uint32_t i_codec )
991 return vlc_fourcc_GetDescription( libvlc_track_type_to_escat( i_type),
992 i_codec );
995 // Release media descriptor's elementary streams description array
996 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
998 for( unsigned i = 0; i < i_count; ++i )
1000 if ( !p_tracks[i] )
1001 continue;
1002 libvlc_media_track_clean( p_tracks[i] );
1003 free( p_tracks[i] );
1005 free( p_tracks );
1008 // Get the media type of the media descriptor object
1009 libvlc_media_type_t libvlc_media_get_type( libvlc_media_t *p_md )
1011 assert( p_md );
1013 enum input_item_type_e i_type;
1014 input_item_t *p_input_item = p_md->p_input_item;
1016 vlc_mutex_lock( &p_input_item->lock );
1017 i_type = p_md->p_input_item->i_type;
1018 vlc_mutex_unlock( &p_input_item->lock );
1020 switch( i_type )
1022 case ITEM_TYPE_FILE:
1023 return libvlc_media_type_file;
1024 case ITEM_TYPE_NODE:
1025 case ITEM_TYPE_DIRECTORY:
1026 return libvlc_media_type_directory;
1027 case ITEM_TYPE_DISC:
1028 return libvlc_media_type_disc;
1029 case ITEM_TYPE_STREAM:
1030 return libvlc_media_type_stream;
1031 case ITEM_TYPE_PLAYLIST:
1032 return libvlc_media_type_playlist;
1033 default:
1034 return libvlc_media_type_unknown;
1038 struct libvlc_media_thumbnail_request_t
1040 libvlc_media_t *md;
1041 unsigned int width;
1042 unsigned int height;
1043 bool crop;
1044 libvlc_picture_type_t type;
1045 vlc_thumbnailer_request_t* req;
1048 static void media_on_thumbnail_ready( void* data, picture_t* thumbnail )
1050 libvlc_media_thumbnail_request_t *req = data;
1051 libvlc_media_t *p_media = req->md;
1052 libvlc_event_t event;
1053 event.type = libvlc_MediaThumbnailGenerated;
1054 libvlc_picture_t* pic = NULL;
1055 if ( thumbnail != NULL )
1056 pic = libvlc_picture_new( VLC_OBJECT(p_media->p_libvlc_instance->p_libvlc_int),
1057 thumbnail, req->type, req->width, req->height,
1058 req->crop );
1059 event.u.media_thumbnail_generated.p_thumbnail = pic;
1060 libvlc_event_send( &p_media->event_manager, &event );
1061 if ( pic != NULL )
1062 libvlc_picture_release( pic );
1065 // Start an asynchronous thumbnail generation
1066 libvlc_media_thumbnail_request_t*
1067 libvlc_media_thumbnail_request_by_time( libvlc_media_t *md, libvlc_time_t time,
1068 libvlc_thumbnailer_seek_speed_t speed,
1069 unsigned int width, unsigned int height,
1070 bool crop, libvlc_picture_type_t picture_type,
1071 libvlc_time_t timeout )
1073 assert( md );
1074 libvlc_priv_t *p_priv = libvlc_priv(md->p_libvlc_instance->p_libvlc_int);
1075 if( unlikely( p_priv->p_thumbnailer == NULL ) )
1076 return NULL;
1077 libvlc_media_thumbnail_request_t *req = malloc( sizeof( *req ) );
1078 if ( unlikely( req == NULL ) )
1079 return NULL;
1081 req->md = md;
1082 req->width = width;
1083 req->height = height;
1084 req->type = picture_type;
1085 req->crop = crop;
1086 libvlc_media_retain( md );
1087 req->req = vlc_thumbnailer_RequestByTime( p_priv->p_thumbnailer,
1088 VLC_TICK_FROM_MS( time ),
1089 speed == libvlc_media_thumbnail_seek_fast ?
1090 VLC_THUMBNAILER_SEEK_FAST : VLC_THUMBNAILER_SEEK_PRECISE,
1091 md->p_input_item,
1092 timeout > 0 ? VLC_TICK_FROM_MS( timeout ) : VLC_TICK_INVALID,
1093 media_on_thumbnail_ready, req );
1094 if ( req->req == NULL )
1096 free( req );
1097 libvlc_media_release( md );
1098 return NULL;
1100 return req;
1103 // Start an asynchronous thumbnail generation
1104 libvlc_media_thumbnail_request_t*
1105 libvlc_media_thumbnail_request_by_pos( libvlc_media_t *md, float pos,
1106 libvlc_thumbnailer_seek_speed_t speed,
1107 unsigned int width, unsigned int height,
1108 bool crop, libvlc_picture_type_t picture_type,
1109 libvlc_time_t timeout )
1111 assert( md );
1112 libvlc_priv_t *priv = libvlc_priv(md->p_libvlc_instance->p_libvlc_int);
1113 if( unlikely( priv->p_thumbnailer == NULL ) )
1114 return NULL;
1115 libvlc_media_thumbnail_request_t *req = malloc( sizeof( *req ) );
1116 if ( unlikely( req == NULL ) )
1117 return NULL;
1119 req->md = md;
1120 req->width = width;
1121 req->height = height;
1122 req->crop = crop;
1123 req->type = picture_type;
1124 libvlc_media_retain( md );
1125 req->req = vlc_thumbnailer_RequestByPos( priv->p_thumbnailer, pos,
1126 speed == libvlc_media_thumbnail_seek_fast ?
1127 VLC_THUMBNAILER_SEEK_FAST : VLC_THUMBNAILER_SEEK_PRECISE,
1128 md->p_input_item,
1129 timeout > 0 ? VLC_TICK_FROM_MS( timeout ) : VLC_TICK_INVALID,
1130 media_on_thumbnail_ready, req );
1131 if ( req->req == NULL )
1133 free( req );
1134 libvlc_media_release( md );
1135 return NULL;
1137 return req;
1140 // Cancel a thumbnail request
1141 void libvlc_media_thumbnail_request_cancel( libvlc_media_thumbnail_request_t *req )
1143 libvlc_priv_t *p_priv = libvlc_priv(req->md->p_libvlc_instance->p_libvlc_int);
1144 assert( p_priv->p_thumbnailer != NULL );
1145 vlc_thumbnailer_Cancel( p_priv->p_thumbnailer, req->req );
1148 // Destroy a thumbnail request
1149 void libvlc_media_thumbnail_request_destroy( libvlc_media_thumbnail_request_t *req )
1151 libvlc_media_release( req->md );
1152 free( req );
1155 // Add a slave to the media descriptor
1156 int libvlc_media_slaves_add( libvlc_media_t *p_md,
1157 libvlc_media_slave_type_t i_type,
1158 unsigned int i_priority,
1159 const char *psz_uri )
1161 assert( p_md && psz_uri );
1162 input_item_t *p_input_item = p_md->p_input_item;
1164 enum slave_type i_input_slave_type;
1165 switch( i_type )
1167 case libvlc_media_slave_type_subtitle:
1168 i_input_slave_type = SLAVE_TYPE_SPU;
1169 break;
1170 case libvlc_media_slave_type_audio:
1171 i_input_slave_type = SLAVE_TYPE_AUDIO;
1172 break;
1173 default:
1174 vlc_assert_unreachable();
1175 return -1;
1178 enum slave_priority i_input_slave_priority;
1179 switch( i_priority )
1181 case 0:
1182 i_input_slave_priority = SLAVE_PRIORITY_MATCH_NONE;
1183 break;
1184 case 1:
1185 i_input_slave_priority = SLAVE_PRIORITY_MATCH_RIGHT;
1186 break;
1187 case 2:
1188 i_input_slave_priority = SLAVE_PRIORITY_MATCH_LEFT;
1189 break;
1190 case 3:
1191 i_input_slave_priority = SLAVE_PRIORITY_MATCH_ALL;
1192 break;
1193 default:
1194 case 4:
1195 i_input_slave_priority = SLAVE_PRIORITY_USER;
1196 break;
1199 input_item_slave_t *p_slave = input_item_slave_New( psz_uri,
1200 i_input_slave_type,
1201 i_input_slave_priority );
1202 if( p_slave == NULL )
1203 return -1;
1204 return input_item_AddSlave( p_input_item, p_slave ) == VLC_SUCCESS ? 0 : -1;
1207 // Clear all slaves of the media descriptor
1208 void libvlc_media_slaves_clear( libvlc_media_t *p_md )
1210 assert( p_md );
1211 input_item_t *p_input_item = p_md->p_input_item;
1213 vlc_mutex_lock( &p_input_item->lock );
1214 for( int i = 0; i < p_input_item->i_slaves; i++ )
1215 input_item_slave_Delete( p_input_item->pp_slaves[i] );
1216 TAB_CLEAN( p_input_item->i_slaves, p_input_item->pp_slaves );
1217 vlc_mutex_unlock( &p_input_item->lock );
1220 // Get a media descriptor's slave list
1221 unsigned int libvlc_media_slaves_get( libvlc_media_t *p_md,
1222 libvlc_media_slave_t ***ppp_slaves )
1224 assert( p_md && ppp_slaves );
1225 input_item_t *p_input_item = p_md->p_input_item;
1226 *ppp_slaves = NULL;
1228 vlc_mutex_lock( &p_input_item->lock );
1230 int i_count = p_input_item->i_slaves;
1231 if( i_count <= 0 )
1232 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1234 libvlc_media_slave_t **pp_slaves = calloc( i_count, sizeof(*pp_slaves) );
1235 if( pp_slaves == NULL )
1236 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1238 for( int i = 0; i < i_count; ++i )
1240 input_item_slave_t *p_item_slave = p_input_item->pp_slaves[i];
1241 assert( p_item_slave->i_priority >= SLAVE_PRIORITY_MATCH_NONE );
1243 /* also allocate psz_uri buffer at the end of the struct */
1244 libvlc_media_slave_t *p_slave = malloc( sizeof(*p_slave) +
1245 strlen( p_item_slave->psz_uri )
1246 + 1 );
1247 if( p_slave == NULL )
1249 libvlc_media_slaves_release(pp_slaves, i);
1250 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1252 p_slave->psz_uri = (char *) ((uint8_t *)p_slave) + sizeof(*p_slave);
1253 strcpy( p_slave->psz_uri, p_item_slave->psz_uri );
1255 switch( p_item_slave->i_type )
1257 case SLAVE_TYPE_SPU:
1258 p_slave->i_type = libvlc_media_slave_type_subtitle;
1259 break;
1260 case SLAVE_TYPE_AUDIO:
1261 p_slave->i_type = libvlc_media_slave_type_audio;
1262 break;
1263 default:
1264 vlc_assert_unreachable();
1267 switch( p_item_slave->i_priority )
1269 case SLAVE_PRIORITY_MATCH_NONE:
1270 p_slave->i_priority = 0;
1271 break;
1272 case SLAVE_PRIORITY_MATCH_RIGHT:
1273 p_slave->i_priority = 1;
1274 break;
1275 case SLAVE_PRIORITY_MATCH_LEFT:
1276 p_slave->i_priority = 2;
1277 break;
1278 case SLAVE_PRIORITY_MATCH_ALL:
1279 p_slave->i_priority = 3;
1280 break;
1281 case SLAVE_PRIORITY_USER:
1282 p_slave->i_priority = 4;
1283 break;
1284 default:
1285 vlc_assert_unreachable();
1287 pp_slaves[i] = p_slave;
1289 vlc_mutex_unlock( &p_input_item->lock );
1291 *ppp_slaves = pp_slaves;
1292 return i_count;
1295 // Release a media descriptor's slave list
1296 void libvlc_media_slaves_release( libvlc_media_slave_t **pp_slaves,
1297 unsigned int i_count )
1299 if( i_count > 0 )
1301 assert( pp_slaves );
1302 for( unsigned int i = 0; i < i_count; ++i )
1303 free( pp_slaves[i] );
1305 free( pp_slaves );