audiotrack: refactor
[vlc.git] / lib / media.c
blobdd4faf006662a77ecea850fa390dc6961bc43cf8
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 input_item_attachments_found( const vlc_event_t *p_event,
336 void * user_data )
338 libvlc_media_t * p_md = user_data;
339 libvlc_event_t event;
341 libvlc_picture_list_t* list = libvlc_picture_list_from_attachments(
342 p_event->u.input_item_attachments_found.attachments,
343 p_event->u.input_item_attachments_found.count );
344 if( !list )
345 return;
346 if( !libvlc_picture_list_count(list) )
348 libvlc_picture_list_destroy( list );
349 return;
352 /* Construct the event */
353 event.type = libvlc_MediaAttachedThumbnailsFound;
354 event.u.media_attached_thumbnails_found.thumbnails = list;
356 /* Send the event */
357 libvlc_event_send( &p_md->event_manager, &event );
359 libvlc_picture_list_destroy( list );
362 static void send_parsed_changed( libvlc_media_t *p_md,
363 libvlc_media_parsed_status_t new_status )
365 libvlc_event_t event;
367 vlc_mutex_lock( &p_md->parsed_lock );
368 if( p_md->parsed_status == new_status )
370 vlc_mutex_unlock( &p_md->parsed_lock );
371 return;
374 /* Legacy: notify libvlc_media_parse */
375 if( !p_md->is_parsed )
377 p_md->is_parsed = true;
378 vlc_cond_broadcast( &p_md->parsed_cond );
381 p_md->parsed_status = new_status;
382 if( p_md->parsed_status != libvlc_media_parsed_status_done )
383 p_md->has_asked_preparse = false;
385 vlc_mutex_unlock( &p_md->parsed_lock );
387 /* Construct the event */
388 event.type = libvlc_MediaParsedChanged;
389 event.u.media_parsed_changed.new_status = new_status;
391 /* Send the event */
392 libvlc_event_send( &p_md->event_manager, &event );
394 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, false );
395 if( p_subitems != NULL )
397 /* notify the media list */
398 libvlc_media_list_lock( p_subitems );
399 libvlc_media_list_internal_end_reached( p_subitems );
400 libvlc_media_list_unlock( p_subitems );
405 * \internal
406 * input_item_preparse_ended (Private) (vlc event Callback)
408 static void input_item_preparse_ended(input_item_t *item,
409 enum input_item_preparse_status status,
410 void *user_data)
412 VLC_UNUSED(item);
413 libvlc_media_t * p_md = user_data;
414 libvlc_media_parsed_status_t new_status;
416 switch( status )
418 case ITEM_PREPARSE_SKIPPED:
419 new_status = libvlc_media_parsed_status_skipped;
420 break;
421 case ITEM_PREPARSE_FAILED:
422 new_status = libvlc_media_parsed_status_failed;
423 break;
424 case ITEM_PREPARSE_TIMEOUT:
425 new_status = libvlc_media_parsed_status_timeout;
426 break;
427 case ITEM_PREPARSE_DONE:
428 new_status = libvlc_media_parsed_status_done;
429 break;
430 default:
431 return;
433 send_parsed_changed( p_md, new_status );
434 libvlc_media_release( p_md );
438 * \internal
439 * Install event handler (Private)
441 static void install_input_item_observer( libvlc_media_t *p_md )
443 vlc_event_attach( &p_md->p_input_item->event_manager,
444 vlc_InputItemMetaChanged,
445 input_item_meta_changed,
446 p_md );
447 vlc_event_attach( &p_md->p_input_item->event_manager,
448 vlc_InputItemDurationChanged,
449 input_item_duration_changed,
450 p_md );
451 vlc_event_attach( &p_md->p_input_item->event_manager,
452 vlc_InputItemAttachmentsFound,
453 input_item_attachments_found,
454 p_md );
458 * \internal
459 * Uninstall event handler (Private)
461 static void uninstall_input_item_observer( libvlc_media_t *p_md )
463 vlc_event_detach( &p_md->p_input_item->event_manager,
464 vlc_InputItemMetaChanged,
465 input_item_meta_changed,
466 p_md );
467 vlc_event_detach( &p_md->p_input_item->event_manager,
468 vlc_InputItemDurationChanged,
469 input_item_duration_changed,
470 p_md );
471 vlc_event_detach( &p_md->p_input_item->event_manager,
472 vlc_InputItemAttachmentsFound,
473 input_item_attachments_found,
474 p_md );
478 * \internal
479 * Create a new media descriptor object from an input_item (Private)
481 * That's the generic constructor
483 libvlc_media_t * libvlc_media_new_from_input_item(
484 libvlc_instance_t *p_instance,
485 input_item_t *p_input_item )
487 libvlc_media_t * p_md;
489 if (!p_input_item)
491 libvlc_printerr( "No input item given" );
492 return NULL;
495 p_md = calloc( 1, sizeof(libvlc_media_t) );
496 if( !p_md )
498 libvlc_printerr( "Not enough memory" );
499 return NULL;
502 p_md->p_libvlc_instance = p_instance;
503 p_md->p_input_item = p_input_item;
504 p_md->i_refcount = 1;
506 vlc_cond_init(&p_md->parsed_cond);
507 vlc_mutex_init(&p_md->parsed_lock);
508 vlc_mutex_init(&p_md->subitems_lock);
510 p_md->state = libvlc_NothingSpecial;
512 /* A media descriptor can be a playlist. When you open a playlist
513 * It can give a bunch of item to read. */
514 p_md->p_subitems = NULL;
515 p_md->p_input_item->libvlc_owner = p_md;
517 libvlc_event_manager_init( &p_md->event_manager, p_md );
519 input_item_Hold( p_md->p_input_item );
521 install_input_item_observer( p_md );
523 libvlc_retain( p_instance );
524 return p_md;
527 // Create a media with a certain given media resource location
528 libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
529 const char * psz_mrl )
531 input_item_t * p_input_item;
532 libvlc_media_t * p_md;
534 p_input_item = input_item_New( psz_mrl, NULL );
536 if (!p_input_item)
538 libvlc_printerr( "Not enough memory" );
539 return NULL;
542 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
544 /* The p_input_item is retained in libvlc_media_new_from_input_item */
545 input_item_Release( p_input_item );
547 return p_md;
550 // Create a media for a certain file path
551 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
552 const char *path )
554 char *mrl = vlc_path2uri( path, NULL );
555 if( unlikely(mrl == NULL) )
557 libvlc_printerr( "%s", vlc_strerror_c(errno) );
558 return NULL;
561 libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
562 free( mrl );
563 return m;
566 // Create a media for an already open file descriptor
567 libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
569 char mrl[16];
570 snprintf( mrl, sizeof(mrl), "fd://%d", fd );
572 return libvlc_media_new_location( p_instance, mrl );
575 // Create a media with custom callbacks to read the data from
576 libvlc_media_t *libvlc_media_new_callbacks(libvlc_instance_t *p_instance,
577 libvlc_media_open_cb open_cb,
578 libvlc_media_read_cb read_cb,
579 libvlc_media_seek_cb seek_cb,
580 libvlc_media_close_cb close_cb,
581 void *opaque)
583 libvlc_media_t *m = libvlc_media_new_location(p_instance, "imem://");
584 if (unlikely(m == NULL))
585 return NULL;
587 assert(read_cb != NULL);
588 input_item_AddOpaque(m->p_input_item, "imem-data", opaque);
589 input_item_AddOpaque(m->p_input_item, "imem-open", open_cb);
590 input_item_AddOpaque(m->p_input_item, "imem-read", read_cb);
591 input_item_AddOpaque(m->p_input_item, "imem-seek", seek_cb);
592 input_item_AddOpaque(m->p_input_item, "imem-close", close_cb);
593 return m;
596 // Create a media as an empty node with a given name
597 libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
598 const char * psz_name )
600 input_item_t * p_input_item;
601 libvlc_media_t * p_md;
602 libvlc_media_list_t * p_subitems;
604 p_input_item = input_item_New( INPUT_ITEM_URI_NOP, psz_name );
606 if (!p_input_item)
608 libvlc_printerr( "Not enough memory" );
609 return NULL;
612 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
613 input_item_Release( p_input_item );
615 p_subitems = media_get_subitems( p_md, true );
616 if( p_subitems == NULL) {
617 libvlc_media_release( p_md );
618 return NULL;
621 return p_md;
624 // Add an option to the media descriptor
625 void libvlc_media_add_option( libvlc_media_t * p_md,
626 const char * psz_option )
628 libvlc_media_add_option_flag( p_md, psz_option,
629 VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
632 // Same as libvlc_media_add_option but with configurable flags
633 void libvlc_media_add_option_flag( libvlc_media_t * p_md,
634 const char * ppsz_option,
635 unsigned i_flags )
637 input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
640 // Delete a media descriptor object
641 void libvlc_media_release( libvlc_media_t *p_md )
643 if (!p_md)
644 return;
646 p_md->i_refcount--;
648 if( p_md->i_refcount > 0 )
649 return;
651 uninstall_input_item_observer( p_md );
653 /* Cancel asynchronous parsing (if any) */
654 libvlc_MetadataCancel( p_md->p_libvlc_instance->p_libvlc_int, p_md );
656 if( p_md->p_subitems )
657 libvlc_media_list_release( p_md->p_subitems );
659 input_item_Release( p_md->p_input_item );
661 /* Construct the event */
662 libvlc_event_t event;
663 event.type = libvlc_MediaFreed;
664 event.u.media_freed.md = p_md;
666 /* Send the event */
667 libvlc_event_send( &p_md->event_manager, &event );
669 libvlc_event_manager_destroy( &p_md->event_manager );
670 libvlc_release( p_md->p_libvlc_instance );
671 free( p_md );
674 // Retain a media descriptor object
675 void libvlc_media_retain( libvlc_media_t *p_md )
677 assert (p_md);
678 p_md->i_refcount++;
681 // Duplicate a media descriptor object
682 libvlc_media_t *
683 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
686 input_item_t *dup = input_item_Copy( p_md_orig->p_input_item );
687 if( dup == NULL )
688 return NULL;
689 return libvlc_media_new_from_input_item( p_md_orig->p_libvlc_instance, dup );
692 // Get mrl from a media descriptor object
693 char *
694 libvlc_media_get_mrl( libvlc_media_t * p_md )
696 assert( p_md );
697 return input_item_GetURI( p_md->p_input_item );
700 // Getter for meta information
701 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
703 char *psz_meta = NULL;
705 if( e_meta == libvlc_meta_NowPlaying )
707 psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
709 else
711 psz_meta = input_item_GetMeta( p_md->p_input_item,
712 libvlc_to_vlc_meta[e_meta] );
713 /* Should be integrated in core */
714 if( psz_meta == NULL && e_meta == libvlc_meta_Title
715 && p_md->p_input_item->psz_name != NULL )
716 psz_meta = strdup( p_md->p_input_item->psz_name );
718 return psz_meta;
721 // Set the meta of the media
722 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
724 assert( p_md );
725 input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
728 // Save the meta previously set
729 int libvlc_media_save_meta( libvlc_media_t *p_md )
731 assert( p_md );
732 vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
733 return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
736 // Getter for state information
737 libvlc_state_t
738 libvlc_media_get_state( libvlc_media_t *p_md )
740 assert( p_md );
741 return p_md->state;
744 // Setter for state information (LibVLC Internal)
745 void
746 libvlc_media_set_state( libvlc_media_t *p_md,
747 libvlc_state_t state )
749 libvlc_event_t event;
751 p_md->state = state;
753 /* Construct the event */
754 event.type = libvlc_MediaStateChanged;
755 event.u.media_state_changed.new_state = state;
757 /* Send the event */
758 libvlc_event_send( &p_md->event_manager, &event );
761 // Get subitems of media descriptor object.
762 libvlc_media_list_t *
763 libvlc_media_subitems( libvlc_media_t * p_md )
765 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
766 if( p_subitems )
767 libvlc_media_list_retain( p_subitems );
768 return p_subitems;
771 // Getter for statistics information
772 bool libvlc_media_get_stats(libvlc_media_t *p_md,
773 libvlc_media_stats_t *p_stats)
775 input_item_t *item = p_md->p_input_item;
777 if( !p_md->p_input_item )
778 return false;
780 vlc_mutex_lock( &item->lock );
782 input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
783 if( p_itm_stats == NULL )
785 vlc_mutex_unlock( &item->lock );
786 return false;
789 p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
790 p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
792 p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
793 p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
794 p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
795 p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
797 p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
798 p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
800 p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
801 p_stats->i_late_pictures = p_itm_stats->i_late_pictures;
802 p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
804 p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
805 p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
807 vlc_mutex_unlock( &item->lock );
808 return true;
811 // Get event manager from a media descriptor object
812 libvlc_event_manager_t *
813 libvlc_media_event_manager( libvlc_media_t * p_md )
815 assert( p_md );
817 return &p_md->event_manager;
820 // Get duration of media object (in ms)
821 int64_t
822 libvlc_media_get_duration( libvlc_media_t * p_md )
824 assert( p_md );
826 if( !p_md->p_input_item )
828 libvlc_printerr( "No input item" );
829 return -1;
832 if (!input_item_IsPreparsed( p_md->p_input_item ))
833 return -1;
835 return from_mtime(input_item_GetDuration( p_md->p_input_item ));
838 static const input_preparser_callbacks_t input_preparser_callbacks = {
839 .on_preparse_ended = input_item_preparse_ended,
840 .on_subtree_added = input_item_subtree_added,
843 static int media_parse(libvlc_media_t *media, bool b_async,
844 libvlc_media_parse_flag_t parse_flag, int timeout)
846 bool needed;
848 vlc_mutex_lock(&media->parsed_lock);
849 needed = !media->has_asked_preparse;
850 media->has_asked_preparse = true;
851 if (needed)
853 media->is_parsed = false;
854 media->parsed_status = 0;
856 vlc_mutex_unlock(&media->parsed_lock);
858 if (needed)
860 libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
861 input_item_t *item = media->p_input_item;
862 input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
863 int ret;
865 if (parse_flag & libvlc_media_parse_network)
866 parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
867 if (parse_flag & libvlc_media_fetch_local)
868 parse_scope |= META_REQUEST_OPTION_FETCH_LOCAL;
869 if (parse_flag & libvlc_media_fetch_network)
870 parse_scope |= META_REQUEST_OPTION_FETCH_NETWORK;
871 if (parse_flag & libvlc_media_do_interact)
872 parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
874 libvlc_media_retain(media);
875 ret = libvlc_MetadataRequest(libvlc, item, parse_scope,
876 &input_preparser_callbacks, media,
877 timeout, media);
878 if (ret != VLC_SUCCESS)
880 libvlc_media_release(media);
881 return ret;
884 else
885 return VLC_EGENERIC;
887 if (!b_async)
889 vlc_mutex_lock(&media->parsed_lock);
890 while (!media->is_parsed)
891 vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
892 vlc_mutex_unlock(&media->parsed_lock);
894 return VLC_SUCCESS;
897 // Parse the media and wait
898 void
899 libvlc_media_parse(libvlc_media_t *media)
901 media_parse( media, false, libvlc_media_fetch_local, -1 );
904 // Parse the media but do not wait
905 void
906 libvlc_media_parse_async(libvlc_media_t *media)
908 media_parse( media, true, libvlc_media_fetch_local, -1 );
911 // Parse the media asynchronously with options
913 libvlc_media_parse_with_options( libvlc_media_t *media,
914 libvlc_media_parse_flag_t parse_flag,
915 int timeout )
917 return media_parse( media, true, parse_flag, timeout ) == VLC_SUCCESS ? 0 : -1;
920 // Stop parsing of the media
921 void
922 libvlc_media_parse_stop( libvlc_media_t *media )
924 libvlc_MetadataCancel( media->p_libvlc_instance->p_libvlc_int, media );
927 // Get parsed status for media object (deprecated)
928 bool libvlc_media_is_parsed(libvlc_media_t *media)
930 bool parsed;
932 vlc_mutex_lock(&media->parsed_lock);
933 parsed = media->is_parsed;
934 vlc_mutex_unlock(&media->parsed_lock);
935 return parsed;
938 // Get Parsed status for media descriptor object
939 libvlc_media_parsed_status_t
940 libvlc_media_get_parsed_status(libvlc_media_t *media)
942 libvlc_media_parsed_status_t status;
944 vlc_mutex_lock(&media->parsed_lock);
945 status = media->parsed_status;
946 vlc_mutex_unlock(&media->parsed_lock);
947 return status;
950 // Sets media descriptor's user_data
951 void
952 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
954 assert( p_md );
955 p_md->p_user_data = p_new_user_data;
958 // Get media descriptor's user_data
959 void *
960 libvlc_media_get_user_data( libvlc_media_t * p_md )
962 assert( p_md );
963 return p_md->p_user_data;
966 // Get media descriptor's elementary streams description
967 unsigned
968 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
970 assert( p_md );
972 input_item_t *p_input_item = p_md->p_input_item;
973 vlc_mutex_lock( &p_input_item->lock );
975 const int i_es = p_input_item->i_es;
976 *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
978 if( !*pp_es ) /* no ES, or OOM */
980 vlc_mutex_unlock( &p_input_item->lock );
981 return 0;
984 /* Fill array */
985 for( int i = 0; i < i_es; i++ )
987 libvlc_media_trackpriv_t *p_trackpriv = calloc( 1, sizeof(*p_trackpriv) );
988 if ( !p_trackpriv )
990 libvlc_media_tracks_release( *pp_es, i_es );
991 *pp_es = NULL;
992 vlc_mutex_unlock( &p_input_item->lock );
993 return 0;
995 libvlc_media_track_t *p_mes = &p_trackpriv->t;
996 (*pp_es)[i] = p_mes;
998 const es_format_t *p_es = p_input_item->es[i];
1000 libvlc_media_trackpriv_from_es( p_trackpriv, p_es );
1003 vlc_mutex_unlock( &p_input_item->lock );
1004 return i_es;
1007 libvlc_media_tracklist_t *
1008 libvlc_media_get_tracklist( libvlc_media_t *p_md, libvlc_track_type_t type )
1010 assert( p_md );
1012 input_item_t *p_input_item = p_md->p_input_item;
1014 vlc_mutex_lock( &p_input_item->lock );
1015 libvlc_media_tracklist_t *list =
1016 libvlc_media_tracklist_from_es_array( p_input_item->es,
1017 p_input_item->i_es, type );
1018 vlc_mutex_unlock( &p_input_item->lock );
1020 return list;
1023 // Get codec description from media elementary stream
1024 const char *
1025 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
1026 uint32_t i_codec )
1028 return vlc_fourcc_GetDescription( libvlc_track_type_to_escat( i_type),
1029 i_codec );
1032 // Release media descriptor's elementary streams description array
1033 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
1035 for( unsigned i = 0; i < i_count; ++i )
1037 if ( !p_tracks[i] )
1038 continue;
1039 libvlc_media_track_clean( p_tracks[i] );
1040 free( p_tracks[i] );
1042 free( p_tracks );
1045 // Get the media type of the media descriptor object
1046 libvlc_media_type_t libvlc_media_get_type( libvlc_media_t *p_md )
1048 assert( p_md );
1050 enum input_item_type_e i_type;
1051 input_item_t *p_input_item = p_md->p_input_item;
1053 vlc_mutex_lock( &p_input_item->lock );
1054 i_type = p_md->p_input_item->i_type;
1055 vlc_mutex_unlock( &p_input_item->lock );
1057 switch( i_type )
1059 case ITEM_TYPE_FILE:
1060 return libvlc_media_type_file;
1061 case ITEM_TYPE_NODE:
1062 case ITEM_TYPE_DIRECTORY:
1063 return libvlc_media_type_directory;
1064 case ITEM_TYPE_DISC:
1065 return libvlc_media_type_disc;
1066 case ITEM_TYPE_STREAM:
1067 return libvlc_media_type_stream;
1068 case ITEM_TYPE_PLAYLIST:
1069 return libvlc_media_type_playlist;
1070 default:
1071 return libvlc_media_type_unknown;
1075 struct libvlc_media_thumbnail_request_t
1077 libvlc_media_t *md;
1078 unsigned int width;
1079 unsigned int height;
1080 bool crop;
1081 libvlc_picture_type_t type;
1082 vlc_thumbnailer_request_t* req;
1085 static void media_on_thumbnail_ready( void* data, picture_t* thumbnail )
1087 libvlc_media_thumbnail_request_t *req = data;
1088 libvlc_media_t *p_media = req->md;
1089 libvlc_event_t event;
1090 event.type = libvlc_MediaThumbnailGenerated;
1091 libvlc_picture_t* pic = NULL;
1092 if ( thumbnail != NULL )
1093 pic = libvlc_picture_new( VLC_OBJECT(p_media->p_libvlc_instance->p_libvlc_int),
1094 thumbnail, req->type, req->width, req->height,
1095 req->crop );
1096 event.u.media_thumbnail_generated.p_thumbnail = pic;
1097 libvlc_event_send( &p_media->event_manager, &event );
1098 if ( pic != NULL )
1099 libvlc_picture_release( pic );
1102 // Start an asynchronous thumbnail generation
1103 libvlc_media_thumbnail_request_t*
1104 libvlc_media_thumbnail_request_by_time( libvlc_media_t *md, libvlc_time_t time,
1105 libvlc_thumbnailer_seek_speed_t speed,
1106 unsigned int width, unsigned int height,
1107 bool crop, libvlc_picture_type_t picture_type,
1108 libvlc_time_t timeout )
1110 assert( md );
1111 libvlc_priv_t *p_priv = libvlc_priv(md->p_libvlc_instance->p_libvlc_int);
1112 if( unlikely( p_priv->p_thumbnailer == NULL ) )
1113 return NULL;
1114 libvlc_media_thumbnail_request_t *req = malloc( sizeof( *req ) );
1115 if ( unlikely( req == NULL ) )
1116 return NULL;
1118 req->md = md;
1119 req->width = width;
1120 req->height = height;
1121 req->type = picture_type;
1122 req->crop = crop;
1123 libvlc_media_retain( md );
1124 req->req = vlc_thumbnailer_RequestByTime( p_priv->p_thumbnailer,
1125 VLC_TICK_FROM_MS( time ),
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 // Start an asynchronous thumbnail generation
1141 libvlc_media_thumbnail_request_t*
1142 libvlc_media_thumbnail_request_by_pos( libvlc_media_t *md, float pos,
1143 libvlc_thumbnailer_seek_speed_t speed,
1144 unsigned int width, unsigned int height,
1145 bool crop, libvlc_picture_type_t picture_type,
1146 libvlc_time_t timeout )
1148 assert( md );
1149 libvlc_priv_t *priv = libvlc_priv(md->p_libvlc_instance->p_libvlc_int);
1150 if( unlikely( priv->p_thumbnailer == NULL ) )
1151 return NULL;
1152 libvlc_media_thumbnail_request_t *req = malloc( sizeof( *req ) );
1153 if ( unlikely( req == NULL ) )
1154 return NULL;
1156 req->md = md;
1157 req->width = width;
1158 req->height = height;
1159 req->crop = crop;
1160 req->type = picture_type;
1161 libvlc_media_retain( md );
1162 req->req = vlc_thumbnailer_RequestByPos( priv->p_thumbnailer, pos,
1163 speed == libvlc_media_thumbnail_seek_fast ?
1164 VLC_THUMBNAILER_SEEK_FAST : VLC_THUMBNAILER_SEEK_PRECISE,
1165 md->p_input_item,
1166 timeout > 0 ? VLC_TICK_FROM_MS( timeout ) : VLC_TICK_INVALID,
1167 media_on_thumbnail_ready, req );
1168 if ( req->req == NULL )
1170 free( req );
1171 libvlc_media_release( md );
1172 return NULL;
1174 return req;
1177 // Cancel a thumbnail request
1178 void libvlc_media_thumbnail_request_cancel( libvlc_media_thumbnail_request_t *req )
1180 libvlc_priv_t *p_priv = libvlc_priv(req->md->p_libvlc_instance->p_libvlc_int);
1181 assert( p_priv->p_thumbnailer != NULL );
1182 vlc_thumbnailer_Cancel( p_priv->p_thumbnailer, req->req );
1185 // Destroy a thumbnail request
1186 void libvlc_media_thumbnail_request_destroy( libvlc_media_thumbnail_request_t *req )
1188 libvlc_media_release( req->md );
1189 free( req );
1192 // Add a slave to the media descriptor
1193 int libvlc_media_slaves_add( libvlc_media_t *p_md,
1194 libvlc_media_slave_type_t i_type,
1195 unsigned int i_priority,
1196 const char *psz_uri )
1198 assert( p_md && psz_uri );
1199 input_item_t *p_input_item = p_md->p_input_item;
1201 enum slave_type i_input_slave_type;
1202 switch( i_type )
1204 case libvlc_media_slave_type_subtitle:
1205 i_input_slave_type = SLAVE_TYPE_SPU;
1206 break;
1207 case libvlc_media_slave_type_audio:
1208 i_input_slave_type = SLAVE_TYPE_AUDIO;
1209 break;
1210 default:
1211 vlc_assert_unreachable();
1212 return -1;
1215 enum slave_priority i_input_slave_priority;
1216 switch( i_priority )
1218 case 0:
1219 i_input_slave_priority = SLAVE_PRIORITY_MATCH_NONE;
1220 break;
1221 case 1:
1222 i_input_slave_priority = SLAVE_PRIORITY_MATCH_RIGHT;
1223 break;
1224 case 2:
1225 i_input_slave_priority = SLAVE_PRIORITY_MATCH_LEFT;
1226 break;
1227 case 3:
1228 i_input_slave_priority = SLAVE_PRIORITY_MATCH_ALL;
1229 break;
1230 default:
1231 case 4:
1232 i_input_slave_priority = SLAVE_PRIORITY_USER;
1233 break;
1236 input_item_slave_t *p_slave = input_item_slave_New( psz_uri,
1237 i_input_slave_type,
1238 i_input_slave_priority );
1239 if( p_slave == NULL )
1240 return -1;
1241 return input_item_AddSlave( p_input_item, p_slave ) == VLC_SUCCESS ? 0 : -1;
1244 // Clear all slaves of the media descriptor
1245 void libvlc_media_slaves_clear( libvlc_media_t *p_md )
1247 assert( p_md );
1248 input_item_t *p_input_item = p_md->p_input_item;
1250 vlc_mutex_lock( &p_input_item->lock );
1251 for( int i = 0; i < p_input_item->i_slaves; i++ )
1252 input_item_slave_Delete( p_input_item->pp_slaves[i] );
1253 TAB_CLEAN( p_input_item->i_slaves, p_input_item->pp_slaves );
1254 vlc_mutex_unlock( &p_input_item->lock );
1257 // Get a media descriptor's slave list
1258 unsigned int libvlc_media_slaves_get( libvlc_media_t *p_md,
1259 libvlc_media_slave_t ***ppp_slaves )
1261 assert( p_md && ppp_slaves );
1262 input_item_t *p_input_item = p_md->p_input_item;
1263 *ppp_slaves = NULL;
1265 vlc_mutex_lock( &p_input_item->lock );
1267 int i_count = p_input_item->i_slaves;
1268 if( i_count <= 0 )
1269 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1271 libvlc_media_slave_t **pp_slaves = calloc( i_count, sizeof(*pp_slaves) );
1272 if( pp_slaves == NULL )
1273 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1275 for( int i = 0; i < i_count; ++i )
1277 input_item_slave_t *p_item_slave = p_input_item->pp_slaves[i];
1278 assert( p_item_slave->i_priority >= SLAVE_PRIORITY_MATCH_NONE );
1280 /* also allocate psz_uri buffer at the end of the struct */
1281 libvlc_media_slave_t *p_slave = malloc( sizeof(*p_slave) +
1282 strlen( p_item_slave->psz_uri )
1283 + 1 );
1284 if( p_slave == NULL )
1286 libvlc_media_slaves_release(pp_slaves, i);
1287 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1289 p_slave->psz_uri = (char *) ((uint8_t *)p_slave) + sizeof(*p_slave);
1290 strcpy( p_slave->psz_uri, p_item_slave->psz_uri );
1292 switch( p_item_slave->i_type )
1294 case SLAVE_TYPE_SPU:
1295 p_slave->i_type = libvlc_media_slave_type_subtitle;
1296 break;
1297 case SLAVE_TYPE_AUDIO:
1298 p_slave->i_type = libvlc_media_slave_type_audio;
1299 break;
1300 default:
1301 vlc_assert_unreachable();
1304 switch( p_item_slave->i_priority )
1306 case SLAVE_PRIORITY_MATCH_NONE:
1307 p_slave->i_priority = 0;
1308 break;
1309 case SLAVE_PRIORITY_MATCH_RIGHT:
1310 p_slave->i_priority = 1;
1311 break;
1312 case SLAVE_PRIORITY_MATCH_LEFT:
1313 p_slave->i_priority = 2;
1314 break;
1315 case SLAVE_PRIORITY_MATCH_ALL:
1316 p_slave->i_priority = 3;
1317 break;
1318 case SLAVE_PRIORITY_USER:
1319 p_slave->i_priority = 4;
1320 break;
1321 default:
1322 vlc_assert_unreachable();
1324 pp_slaves[i] = p_slave;
1326 vlc_mutex_unlock( &p_input_item->lock );
1328 *ppp_slaves = pp_slaves;
1329 return i_count;
1332 // Release a media descriptor's slave list
1333 void libvlc_media_slaves_release( libvlc_media_slave_t **pp_slaves,
1334 unsigned int i_count )
1336 if( i_count > 0 )
1338 assert( pp_slaves );
1339 for( unsigned int i = 0; i < i_count; ++i )
1340 free( pp_slaves[i] );
1342 free( pp_slaves );