Update Changelogs
[vlc.git] / lib / media.c
blob9bac37a8dce9f7ee2f2a86fd8d561f9574959241
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>
40 #include <vlc_atomic.h>
42 #include "../src/libvlc.h"
44 #include "libvlc_internal.h"
45 #include "media_internal.h"
46 #include "media_list_internal.h"
47 #include "picture_internal.h"
49 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
51 [libvlc_meta_Title] = vlc_meta_Title,
52 [libvlc_meta_Artist] = vlc_meta_Artist,
53 [libvlc_meta_Genre] = vlc_meta_Genre,
54 [libvlc_meta_Copyright] = vlc_meta_Copyright,
55 [libvlc_meta_Album] = vlc_meta_Album,
56 [libvlc_meta_TrackNumber] = vlc_meta_TrackNumber,
57 [libvlc_meta_Description] = vlc_meta_Description,
58 [libvlc_meta_Rating] = vlc_meta_Rating,
59 [libvlc_meta_Date] = vlc_meta_Date,
60 [libvlc_meta_Setting] = vlc_meta_Setting,
61 [libvlc_meta_URL] = vlc_meta_URL,
62 [libvlc_meta_Language] = vlc_meta_Language,
63 [libvlc_meta_NowPlaying] = vlc_meta_NowPlaying,
64 [libvlc_meta_Publisher] = vlc_meta_Publisher,
65 [libvlc_meta_EncodedBy] = vlc_meta_EncodedBy,
66 [libvlc_meta_ArtworkURL] = vlc_meta_ArtworkURL,
67 [libvlc_meta_TrackID] = vlc_meta_TrackID,
68 [libvlc_meta_TrackTotal] = vlc_meta_TrackTotal,
69 [libvlc_meta_Director] = vlc_meta_Director,
70 [libvlc_meta_Season] = vlc_meta_Season,
71 [libvlc_meta_Episode] = vlc_meta_Episode,
72 [libvlc_meta_ShowName] = vlc_meta_ShowName,
73 [libvlc_meta_Actors] = vlc_meta_Actors,
74 [libvlc_meta_AlbumArtist] = vlc_meta_AlbumArtist,
75 [libvlc_meta_DiscNumber] = vlc_meta_DiscNumber,
76 [libvlc_meta_DiscTotal] = vlc_meta_DiscTotal
79 static const libvlc_meta_t vlc_to_libvlc_meta[] =
81 [vlc_meta_Title] = libvlc_meta_Title,
82 [vlc_meta_Artist] = libvlc_meta_Artist,
83 [vlc_meta_Genre] = libvlc_meta_Genre,
84 [vlc_meta_Copyright] = libvlc_meta_Copyright,
85 [vlc_meta_Album] = libvlc_meta_Album,
86 [vlc_meta_TrackNumber] = libvlc_meta_TrackNumber,
87 [vlc_meta_Description] = libvlc_meta_Description,
88 [vlc_meta_Rating] = libvlc_meta_Rating,
89 [vlc_meta_Date] = libvlc_meta_Date,
90 [vlc_meta_Setting] = libvlc_meta_Setting,
91 [vlc_meta_URL] = libvlc_meta_URL,
92 [vlc_meta_Language] = libvlc_meta_Language,
93 [vlc_meta_NowPlaying] = libvlc_meta_NowPlaying,
94 [vlc_meta_ESNowPlaying] = libvlc_meta_NowPlaying,
95 [vlc_meta_Publisher] = libvlc_meta_Publisher,
96 [vlc_meta_EncodedBy] = libvlc_meta_EncodedBy,
97 [vlc_meta_ArtworkURL] = libvlc_meta_ArtworkURL,
98 [vlc_meta_TrackID] = libvlc_meta_TrackID,
99 [vlc_meta_TrackTotal] = libvlc_meta_TrackTotal,
100 [vlc_meta_Director] = libvlc_meta_Director,
101 [vlc_meta_Season] = libvlc_meta_Season,
102 [vlc_meta_Episode] = libvlc_meta_Episode,
103 [vlc_meta_ShowName] = libvlc_meta_ShowName,
104 [vlc_meta_Actors] = libvlc_meta_Actors,
105 [vlc_meta_AlbumArtist] = libvlc_meta_AlbumArtist,
106 [vlc_meta_DiscNumber] = libvlc_meta_DiscNumber,
107 [vlc_meta_DiscTotal] = libvlc_meta_DiscTotal
110 static_assert(
111 ORIENT_TOP_LEFT == (int) libvlc_video_orient_top_left &&
112 ORIENT_TOP_RIGHT == (int) libvlc_video_orient_top_right &&
113 ORIENT_BOTTOM_LEFT == (int) libvlc_video_orient_bottom_left &&
114 ORIENT_BOTTOM_RIGHT == (int) libvlc_video_orient_bottom_right &&
115 ORIENT_LEFT_TOP == (int) libvlc_video_orient_left_top &&
116 ORIENT_LEFT_BOTTOM == (int) libvlc_video_orient_left_bottom &&
117 ORIENT_RIGHT_TOP == (int) libvlc_video_orient_right_top &&
118 ORIENT_RIGHT_BOTTOM == (int) libvlc_video_orient_right_bottom,
119 "Mismatch between libvlc_video_orient_t and video_orientation_t" );
121 static_assert(
122 PROJECTION_MODE_RECTANGULAR == (int) libvlc_video_projection_rectangular &&
123 PROJECTION_MODE_EQUIRECTANGULAR == (int) libvlc_video_projection_equirectangular &&
124 PROJECTION_MODE_CUBEMAP_LAYOUT_STANDARD == (int) libvlc_video_projection_cubemap_layout_standard,
125 "Mismatch between libvlc_video_projection_t and video_projection_mode_t" );
127 static_assert(
128 MULTIVIEW_2D == (int) libvlc_video_multiview_2d &&
129 MULTIVIEW_STEREO_SBS == (int) libvlc_video_multiview_stereo_sbs &&
130 MULTIVIEW_STEREO_TB == (int) libvlc_video_multiview_stereo_tb &&
131 MULTIVIEW_STEREO_ROW == (int) libvlc_video_multiview_stereo_row &&
132 MULTIVIEW_STEREO_COL == (int) libvlc_video_multiview_stereo_col &&
133 MULTIVIEW_STEREO_FRAME == (int) libvlc_video_multiview_stereo_frame &&
134 MULTIVIEW_STEREO_CHECKERBOARD == (int) libvlc_video_multiview_stereo_checkerboard,
135 "Mismatch between libvlc_video_multiview_t and video_multiview_mode_t");
137 static libvlc_media_list_t *media_get_subitems( libvlc_media_t * p_md,
138 bool b_create )
140 libvlc_media_list_t *p_subitems = NULL;
142 vlc_mutex_lock( &p_md->subitems_lock );
143 if( p_md->p_subitems == NULL && b_create )
145 p_md->p_subitems = libvlc_media_list_new();
146 if( p_md->p_subitems != NULL )
148 p_md->p_subitems->b_read_only = true;
149 p_md->p_subitems->p_internal_md = p_md;
152 p_subitems = p_md->p_subitems;
153 vlc_mutex_unlock( &p_md->subitems_lock );
154 return p_subitems;
157 static libvlc_media_t *input_item_add_subitem( libvlc_media_t *p_md,
158 input_item_t *item )
160 libvlc_media_t * p_md_child;
161 libvlc_media_list_t *p_subitems;
162 libvlc_event_t event;
164 p_md_child = libvlc_media_new_from_input_item( p_md->p_libvlc_instance,
165 item );
167 /* Add this to our media list */
168 p_subitems = media_get_subitems( p_md, true );
169 if( p_subitems != NULL )
171 libvlc_media_list_lock( p_subitems );
172 libvlc_media_list_internal_add_media( p_subitems, p_md_child );
173 libvlc_media_list_unlock( p_subitems );
176 /* Construct the event */
177 event.type = libvlc_MediaSubItemAdded;
178 event.u.media_subitem_added.new_child = p_md_child;
180 /* Send the event */
181 libvlc_event_send( &p_md->event_manager, &event );
182 return p_md_child;
185 struct vlc_item_list
187 struct vlc_list node;
188 input_item_node_t *item;
189 libvlc_media_t *media;
192 static struct vlc_item_list *
193 wrap_item_in_list( libvlc_media_t *media, input_item_node_t *item )
195 struct vlc_item_list *node = malloc( sizeof *node );
196 if( node == NULL )
197 return NULL;
198 node->item = item;
199 node->media = media;
200 return node;
203 static void input_item_add_subnode( libvlc_media_t *md,
204 input_item_node_t *root )
206 struct vlc_list list;
207 vlc_list_init( &list );
209 /* Retain the media because we don't want the search algorithm to release
210 * it when its subitems get parsed. */
211 libvlc_media_retain(md);
213 struct vlc_item_list *node_root = wrap_item_in_list( md, root );
214 if( node_root == NULL )
216 libvlc_media_release(md);
217 goto error;
220 /* This is a depth-first search algorithm, so stash the root of the tree
221 * first, then stash its children and loop back on the last item in the
222 * list until the full subtree is parsed, and eventually the full tree is
223 * parsed. */
224 vlc_list_append( &node_root->node, &list );
226 while( !vlc_list_is_empty( &list ) )
228 /* Pop last item in the list. */
229 struct vlc_item_list *node =
230 vlc_list_last_entry_or_null( &list, struct vlc_item_list, node );
231 vlc_list_remove(&node->node);
233 for( int i = 0; i < node->item->i_children; i++ )
235 input_item_node_t *child = node->item->pp_children[i];
237 /* The media will be released when its children will be added to
238 * the list. */
239 libvlc_media_t *md_child = input_item_add_subitem( node->media, child->p_item );
240 if( md_child == NULL )
241 goto error;
243 struct vlc_item_list *submedia =
244 wrap_item_in_list( md_child, child );
245 if (submedia == NULL)
247 libvlc_media_release( md_child );
248 goto error;
251 /* Stash a request to parse this subtree. */
252 vlc_list_append( &submedia->node, &list );
255 libvlc_media_release( node->media );
256 free( node );
258 return;
260 error:
261 libvlc_printerr( "Not enough memory" );
263 struct vlc_item_list *node;
264 vlc_list_foreach( node, &list, node )
266 if( node->media != NULL )
267 libvlc_media_release( node->media );
268 free( node );
273 * \internal
274 * input_item_subitemtree_added (Private) (vlc event Callback)
276 static void input_item_subtree_added(input_item_t *item,
277 input_item_node_t *node,
278 void *user_data)
280 VLC_UNUSED(item);
281 libvlc_media_t * p_md = user_data;
282 libvlc_media_add_subtree(p_md, node);
285 void libvlc_media_add_subtree(libvlc_media_t *p_md, input_item_node_t *node)
287 input_item_add_subnode( p_md, node );
289 /* Construct the event */
290 libvlc_event_t event;
291 event.type = libvlc_MediaSubItemTreeAdded;
292 event.u.media_subitemtree_added.item = p_md;
294 /* Send the event */
295 libvlc_event_send( &p_md->event_manager, &event );
299 * \internal
300 * input_item_meta_changed (Private) (vlc event Callback)
302 static void input_item_meta_changed( const vlc_event_t *p_event,
303 void * user_data )
305 libvlc_media_t * p_md = user_data;
306 libvlc_event_t event;
308 /* Construct the event */
309 event.type = libvlc_MediaMetaChanged;
310 event.u.media_meta_changed.meta_type =
311 vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
313 /* Send the event */
314 libvlc_event_send( &p_md->event_manager, &event );
318 * \internal
319 * input_item_duration_changed (Private) (vlc event Callback)
321 static void input_item_duration_changed( const vlc_event_t *p_event,
322 void * user_data )
324 libvlc_media_t * p_md = user_data;
325 libvlc_event_t event;
327 /* Construct the event */
328 event.type = libvlc_MediaDurationChanged;
329 event.u.media_duration_changed.new_duration =
330 from_mtime(p_event->u.input_item_duration_changed.new_duration);
332 /* Send the event */
333 libvlc_event_send( &p_md->event_manager, &event );
336 static void input_item_attachments_found( const vlc_event_t *p_event,
337 void * user_data )
339 libvlc_media_t * p_md = user_data;
340 libvlc_event_t event;
342 libvlc_picture_list_t* list = libvlc_picture_list_from_attachments(
343 p_event->u.input_item_attachments_found.attachments,
344 p_event->u.input_item_attachments_found.count );
345 if( !list )
346 return;
347 if( !libvlc_picture_list_count(list) )
349 libvlc_picture_list_destroy( list );
350 return;
353 /* Construct the event */
354 event.type = libvlc_MediaAttachedThumbnailsFound;
355 event.u.media_attached_thumbnails_found.thumbnails = list;
357 /* Send the event */
358 libvlc_event_send( &p_md->event_manager, &event );
360 libvlc_picture_list_destroy( list );
363 static void send_parsed_changed( libvlc_media_t *p_md,
364 libvlc_media_parsed_status_t new_status )
366 libvlc_event_t event;
368 vlc_mutex_lock( &p_md->parsed_lock );
369 if( p_md->parsed_status == new_status )
371 vlc_mutex_unlock( &p_md->parsed_lock );
372 return;
375 /* Legacy: notify libvlc_media_parse */
376 if( !p_md->is_parsed )
378 p_md->is_parsed = true;
379 vlc_cond_broadcast( &p_md->parsed_cond );
382 p_md->parsed_status = new_status;
383 if( p_md->parsed_status != libvlc_media_parsed_status_done )
384 p_md->has_asked_preparse = false;
386 vlc_mutex_unlock( &p_md->parsed_lock );
388 /* Construct the event */
389 event.type = libvlc_MediaParsedChanged;
390 event.u.media_parsed_changed.new_status = new_status;
392 /* Send the event */
393 libvlc_event_send( &p_md->event_manager, &event );
395 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, false );
396 if( p_subitems != NULL )
398 /* notify the media list */
399 libvlc_media_list_lock( p_subitems );
400 libvlc_media_list_internal_end_reached( p_subitems );
401 libvlc_media_list_unlock( p_subitems );
406 * \internal
407 * input_item_preparse_ended (Private) (vlc event Callback)
409 static void input_item_preparse_ended(input_item_t *item,
410 enum input_item_preparse_status status,
411 void *user_data)
413 VLC_UNUSED(item);
414 libvlc_media_t * p_md = user_data;
415 libvlc_media_parsed_status_t new_status;
417 switch( status )
419 case ITEM_PREPARSE_SKIPPED:
420 new_status = libvlc_media_parsed_status_skipped;
421 break;
422 case ITEM_PREPARSE_FAILED:
423 new_status = libvlc_media_parsed_status_failed;
424 break;
425 case ITEM_PREPARSE_TIMEOUT:
426 new_status = libvlc_media_parsed_status_timeout;
427 break;
428 case ITEM_PREPARSE_DONE:
429 new_status = libvlc_media_parsed_status_done;
430 break;
431 default:
432 return;
434 send_parsed_changed( p_md, new_status );
436 vlc_mutex_lock(&p_md->parsed_lock);
437 assert(p_md->worker_count > 0);
438 p_md->worker_count--;
439 vlc_cond_signal(&p_md->idle_cond);
440 vlc_mutex_unlock(&p_md->parsed_lock);
444 * \internal
445 * Install event handler (Private)
447 static void install_input_item_observer( libvlc_media_t *p_md )
449 vlc_event_attach( &p_md->p_input_item->event_manager,
450 vlc_InputItemMetaChanged,
451 input_item_meta_changed,
452 p_md );
453 vlc_event_attach( &p_md->p_input_item->event_manager,
454 vlc_InputItemDurationChanged,
455 input_item_duration_changed,
456 p_md );
457 vlc_event_attach( &p_md->p_input_item->event_manager,
458 vlc_InputItemAttachmentsFound,
459 input_item_attachments_found,
460 p_md );
464 * \internal
465 * Uninstall event handler (Private)
467 static void uninstall_input_item_observer( libvlc_media_t *p_md )
469 vlc_event_detach( &p_md->p_input_item->event_manager,
470 vlc_InputItemMetaChanged,
471 input_item_meta_changed,
472 p_md );
473 vlc_event_detach( &p_md->p_input_item->event_manager,
474 vlc_InputItemDurationChanged,
475 input_item_duration_changed,
476 p_md );
477 vlc_event_detach( &p_md->p_input_item->event_manager,
478 vlc_InputItemAttachmentsFound,
479 input_item_attachments_found,
480 p_md );
484 * \internal
485 * Create a new media descriptor object from an input_item (Private)
487 * That's the generic constructor
489 libvlc_media_t * libvlc_media_new_from_input_item(
490 libvlc_instance_t *p_instance,
491 input_item_t *p_input_item )
493 libvlc_media_t * p_md;
495 if (!p_input_item)
497 libvlc_printerr( "No input item given" );
498 return NULL;
501 p_md = calloc( 1, sizeof(libvlc_media_t) );
502 if( !p_md )
504 libvlc_printerr( "Not enough memory" );
505 return NULL;
508 p_md->p_libvlc_instance = p_instance;
509 p_md->p_input_item = p_input_item;
510 vlc_atomic_rc_init(&p_md->rc);
512 vlc_cond_init(&p_md->parsed_cond);
513 vlc_mutex_init(&p_md->parsed_lock);
514 vlc_mutex_init(&p_md->subitems_lock);
516 vlc_cond_init(&p_md->idle_cond);
517 p_md->worker_count = 0;
519 p_md->state = libvlc_NothingSpecial;
521 /* A media descriptor can be a playlist. When you open a playlist
522 * It can give a bunch of item to read. */
523 p_md->p_subitems = NULL;
524 p_md->p_input_item->libvlc_owner = p_md;
526 libvlc_event_manager_init( &p_md->event_manager, p_md );
528 input_item_Hold( p_md->p_input_item );
530 install_input_item_observer( p_md );
532 libvlc_retain( p_instance );
533 return p_md;
536 // Create a media with a certain given media resource location
537 libvlc_media_t *libvlc_media_new_location( libvlc_instance_t *p_instance,
538 const char * psz_mrl )
540 input_item_t * p_input_item;
541 libvlc_media_t * p_md;
543 p_input_item = input_item_New( psz_mrl, NULL );
545 if (!p_input_item)
547 libvlc_printerr( "Not enough memory" );
548 return NULL;
551 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
553 /* The p_input_item is retained in libvlc_media_new_from_input_item */
554 input_item_Release( p_input_item );
556 return p_md;
559 // Create a media for a certain file path
560 libvlc_media_t *libvlc_media_new_path( libvlc_instance_t *p_instance,
561 const char *path )
563 char *mrl = vlc_path2uri( path, NULL );
564 if( unlikely(mrl == NULL) )
566 libvlc_printerr( "%s", vlc_strerror_c(errno) );
567 return NULL;
570 libvlc_media_t *m = libvlc_media_new_location( p_instance, mrl );
571 free( mrl );
572 return m;
575 // Create a media for an already open file descriptor
576 libvlc_media_t *libvlc_media_new_fd( libvlc_instance_t *p_instance, int fd )
578 char mrl[16];
579 snprintf( mrl, sizeof(mrl), "fd://%d", fd );
581 return libvlc_media_new_location( p_instance, mrl );
584 // Create a media with custom callbacks to read the data from
585 libvlc_media_t *libvlc_media_new_callbacks(libvlc_instance_t *p_instance,
586 libvlc_media_open_cb open_cb,
587 libvlc_media_read_cb read_cb,
588 libvlc_media_seek_cb seek_cb,
589 libvlc_media_close_cb close_cb,
590 void *opaque)
592 libvlc_media_t *m = libvlc_media_new_location(p_instance, "imem://");
593 if (unlikely(m == NULL))
594 return NULL;
596 assert(read_cb != NULL);
597 input_item_AddOpaque(m->p_input_item, "imem-data", opaque);
598 input_item_AddOpaque(m->p_input_item, "imem-open", open_cb);
599 input_item_AddOpaque(m->p_input_item, "imem-read", read_cb);
600 input_item_AddOpaque(m->p_input_item, "imem-seek", seek_cb);
601 input_item_AddOpaque(m->p_input_item, "imem-close", close_cb);
602 return m;
605 // Create a media as an empty node with a given name
606 libvlc_media_t * libvlc_media_new_as_node( libvlc_instance_t *p_instance,
607 const char * psz_name )
609 input_item_t * p_input_item;
610 libvlc_media_t * p_md;
611 libvlc_media_list_t * p_subitems;
613 p_input_item = input_item_New( INPUT_ITEM_URI_NOP, psz_name );
615 if (!p_input_item)
617 libvlc_printerr( "Not enough memory" );
618 return NULL;
621 p_md = libvlc_media_new_from_input_item( p_instance, p_input_item );
622 input_item_Release( p_input_item );
624 p_subitems = media_get_subitems( p_md, true );
625 if( p_subitems == NULL) {
626 libvlc_media_release( p_md );
627 return NULL;
630 return p_md;
633 // Add an option to the media descriptor
634 void libvlc_media_add_option( libvlc_media_t * p_md,
635 const char * psz_option )
637 libvlc_media_add_option_flag( p_md, psz_option,
638 VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
641 // Same as libvlc_media_add_option but with configurable flags
642 void libvlc_media_add_option_flag( libvlc_media_t * p_md,
643 const char * ppsz_option,
644 unsigned i_flags )
646 input_item_AddOption( p_md->p_input_item, ppsz_option, i_flags );
649 // Delete a media descriptor object
650 void libvlc_media_release( libvlc_media_t *p_md )
652 if (!p_md)
653 return;
655 if( !vlc_atomic_rc_dec(&p_md->rc) )
656 return;
658 uninstall_input_item_observer( p_md );
660 /* Cancel asynchronous parsing (if any) */
661 libvlc_MetadataCancel( p_md->p_libvlc_instance->p_libvlc_int, p_md );
663 /* Wait for all async tasks to stop. */
664 vlc_mutex_lock( &p_md->parsed_lock );
665 while( p_md->worker_count > 0 )
666 vlc_cond_wait( &p_md->idle_cond, &p_md->parsed_lock );
667 vlc_mutex_unlock( &p_md->parsed_lock );
669 if( p_md->p_subitems )
670 libvlc_media_list_release( p_md->p_subitems );
672 input_item_Release( p_md->p_input_item );
674 libvlc_event_manager_destroy( &p_md->event_manager );
675 libvlc_release( p_md->p_libvlc_instance );
676 free( p_md );
679 // Retain a media descriptor object
680 void libvlc_media_retain( libvlc_media_t *p_md )
682 assert (p_md);
683 vlc_atomic_rc_inc( &p_md->rc );
686 // Duplicate a media descriptor object
687 libvlc_media_t *
688 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
691 input_item_t *dup = input_item_Copy( p_md_orig->p_input_item );
692 if( dup == NULL )
693 return NULL;
694 return libvlc_media_new_from_input_item( p_md_orig->p_libvlc_instance, dup );
697 // Get mrl from a media descriptor object
698 char *
699 libvlc_media_get_mrl( libvlc_media_t * p_md )
701 assert( p_md );
702 return input_item_GetURI( p_md->p_input_item );
705 // Getter for meta information
706 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
708 char *psz_meta = NULL;
710 if( e_meta == libvlc_meta_NowPlaying )
712 psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
714 else
716 psz_meta = input_item_GetMeta( p_md->p_input_item,
717 libvlc_to_vlc_meta[e_meta] );
718 /* Should be integrated in core */
719 if( psz_meta == NULL && e_meta == libvlc_meta_Title
720 && p_md->p_input_item->psz_name != NULL )
721 psz_meta = strdup( p_md->p_input_item->psz_name );
723 return psz_meta;
726 // Set the meta of the media
727 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
729 assert( p_md );
730 input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
733 // Save the meta previously set
734 int libvlc_media_save_meta( libvlc_media_t *p_md )
736 assert( p_md );
737 vlc_object_t *p_obj = VLC_OBJECT(p_md->p_libvlc_instance->p_libvlc_int);
738 return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
741 // Getter for state information
742 libvlc_state_t
743 libvlc_media_get_state( libvlc_media_t *p_md )
745 assert( p_md );
746 return p_md->state;
749 // Setter for state information (LibVLC Internal)
750 void
751 libvlc_media_set_state( libvlc_media_t *p_md,
752 libvlc_state_t state )
754 libvlc_event_t event;
756 p_md->state = state;
758 /* Construct the event */
759 event.type = libvlc_MediaStateChanged;
760 event.u.media_state_changed.new_state = state;
762 /* Send the event */
763 libvlc_event_send( &p_md->event_manager, &event );
766 // Get subitems of media descriptor object.
767 libvlc_media_list_t *
768 libvlc_media_subitems( libvlc_media_t * p_md )
770 libvlc_media_list_t *p_subitems = media_get_subitems( p_md, true );
771 if( p_subitems )
772 libvlc_media_list_retain( p_subitems );
773 return p_subitems;
776 // Getter for statistics information
777 bool libvlc_media_get_stats(libvlc_media_t *p_md,
778 libvlc_media_stats_t *p_stats)
780 input_item_t *item = p_md->p_input_item;
782 if( !p_md->p_input_item )
783 return false;
785 vlc_mutex_lock( &item->lock );
787 input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
788 if( p_itm_stats == NULL )
790 vlc_mutex_unlock( &item->lock );
791 return false;
794 p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
795 p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
797 p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
798 p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
799 p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
800 p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
802 p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
803 p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
805 p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
806 p_stats->i_late_pictures = p_itm_stats->i_late_pictures;
807 p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
809 p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
810 p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
812 vlc_mutex_unlock( &item->lock );
813 return true;
816 // Get event manager from a media descriptor object
817 libvlc_event_manager_t *
818 libvlc_media_event_manager( libvlc_media_t * p_md )
820 assert( p_md );
822 return &p_md->event_manager;
825 // Get duration of media object (in ms)
826 int64_t
827 libvlc_media_get_duration( libvlc_media_t * p_md )
829 assert( p_md );
831 if( !p_md->p_input_item )
833 libvlc_printerr( "No input item" );
834 return -1;
837 if (!input_item_IsPreparsed( p_md->p_input_item ))
838 return -1;
840 return from_mtime(input_item_GetDuration( p_md->p_input_item ));
843 static const input_preparser_callbacks_t input_preparser_callbacks = {
844 .on_preparse_ended = input_item_preparse_ended,
845 .on_subtree_added = input_item_subtree_added,
848 static int media_parse(libvlc_media_t *media, bool b_async,
849 libvlc_media_parse_flag_t parse_flag, int timeout)
851 bool needed;
853 vlc_mutex_lock(&media->parsed_lock);
854 needed = !media->has_asked_preparse;
855 media->has_asked_preparse = true;
856 if (needed)
858 media->is_parsed = false;
859 media->parsed_status = 0;
861 vlc_mutex_unlock(&media->parsed_lock);
863 if (needed)
865 libvlc_int_t *libvlc = media->p_libvlc_instance->p_libvlc_int;
866 input_item_t *item = media->p_input_item;
867 input_item_meta_request_option_t parse_scope = META_REQUEST_OPTION_SCOPE_LOCAL;
868 int ret;
870 if (parse_flag & libvlc_media_parse_network)
871 parse_scope |= META_REQUEST_OPTION_SCOPE_NETWORK;
872 if (parse_flag & libvlc_media_fetch_local)
873 parse_scope |= META_REQUEST_OPTION_FETCH_LOCAL;
874 if (parse_flag & libvlc_media_fetch_network)
875 parse_scope |= META_REQUEST_OPTION_FETCH_NETWORK;
876 if (parse_flag & libvlc_media_do_interact)
877 parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
879 /* Note: we cannot keep parsed_lock when calling libvlc_MetadataRequest
880 * because it might also be used to submit the state synchronously
881 * which would result in recursive lock. */
882 vlc_mutex_lock(&media->parsed_lock);
883 media->worker_count++;
884 vlc_mutex_unlock(&media->parsed_lock);
886 ret = libvlc_MetadataRequest(libvlc, item, parse_scope,
887 &input_preparser_callbacks, media,
888 timeout, media);
889 if (ret != VLC_SUCCESS)
891 vlc_mutex_lock(&media->parsed_lock);
892 assert(media->worker_count > 0);
893 media->worker_count--;
894 vlc_mutex_unlock(&media->parsed_lock);
895 return ret;
898 else
899 return VLC_EGENERIC;
901 if (!b_async)
903 vlc_mutex_lock(&media->parsed_lock);
904 while (!media->is_parsed)
905 vlc_cond_wait(&media->parsed_cond, &media->parsed_lock);
906 vlc_mutex_unlock(&media->parsed_lock);
908 return VLC_SUCCESS;
911 // Parse the media and wait
912 void
913 libvlc_media_parse(libvlc_media_t *media)
915 media_parse( media, false, libvlc_media_fetch_local, -1 );
918 // Parse the media but do not wait
919 void
920 libvlc_media_parse_async(libvlc_media_t *media)
922 media_parse( media, true, libvlc_media_fetch_local, -1 );
925 // Parse the media asynchronously with options
927 libvlc_media_parse_with_options( libvlc_media_t *media,
928 libvlc_media_parse_flag_t parse_flag,
929 int timeout )
931 return media_parse( media, true, parse_flag, timeout ) == VLC_SUCCESS ? 0 : -1;
934 // Stop parsing of the media
935 void
936 libvlc_media_parse_stop( libvlc_media_t *media )
938 libvlc_MetadataCancel( media->p_libvlc_instance->p_libvlc_int, media );
941 // Get parsed status for media object (deprecated)
942 bool libvlc_media_is_parsed(libvlc_media_t *media)
944 bool parsed;
946 vlc_mutex_lock(&media->parsed_lock);
947 parsed = media->is_parsed;
948 vlc_mutex_unlock(&media->parsed_lock);
949 return parsed;
952 // Get Parsed status for media descriptor object
953 libvlc_media_parsed_status_t
954 libvlc_media_get_parsed_status(libvlc_media_t *media)
956 libvlc_media_parsed_status_t status;
958 vlc_mutex_lock(&media->parsed_lock);
959 status = media->parsed_status;
960 vlc_mutex_unlock(&media->parsed_lock);
961 return status;
964 // Sets media descriptor's user_data
965 void
966 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
968 assert( p_md );
969 p_md->p_user_data = p_new_user_data;
972 // Get media descriptor's user_data
973 void *
974 libvlc_media_get_user_data( libvlc_media_t * p_md )
976 assert( p_md );
977 return p_md->p_user_data;
980 // Get media descriptor's elementary streams description
981 unsigned
982 libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t *** pp_es )
984 assert( p_md );
986 input_item_t *p_input_item = p_md->p_input_item;
987 vlc_mutex_lock( &p_input_item->lock );
989 const int i_es = p_input_item->i_es;
990 *pp_es = (i_es > 0) ? calloc( i_es, sizeof(**pp_es) ) : NULL;
992 if( !*pp_es ) /* no ES, or OOM */
994 vlc_mutex_unlock( &p_input_item->lock );
995 return 0;
998 /* Fill array */
999 for( int i = 0; i < i_es; i++ )
1001 libvlc_media_trackpriv_t *p_trackpriv = calloc( 1, sizeof(*p_trackpriv) );
1002 if ( !p_trackpriv )
1004 libvlc_media_tracks_release( *pp_es, i_es );
1005 *pp_es = NULL;
1006 vlc_mutex_unlock( &p_input_item->lock );
1007 return 0;
1009 libvlc_media_track_t *p_mes = &p_trackpriv->t;
1010 (*pp_es)[i] = p_mes;
1012 const es_format_t *p_es = p_input_item->es[i];
1014 libvlc_media_trackpriv_from_es( p_trackpriv, p_es );
1017 vlc_mutex_unlock( &p_input_item->lock );
1018 return i_es;
1021 libvlc_media_tracklist_t *
1022 libvlc_media_get_tracklist( libvlc_media_t *p_md, libvlc_track_type_t type )
1024 assert( p_md );
1026 input_item_t *p_input_item = p_md->p_input_item;
1028 vlc_mutex_lock( &p_input_item->lock );
1029 libvlc_media_tracklist_t *list =
1030 libvlc_media_tracklist_from_es_array( p_input_item->es,
1031 p_input_item->i_es, type );
1032 vlc_mutex_unlock( &p_input_item->lock );
1034 return list;
1037 // Get codec description from media elementary stream
1038 const char *
1039 libvlc_media_get_codec_description( libvlc_track_type_t i_type,
1040 uint32_t i_codec )
1042 return vlc_fourcc_GetDescription( libvlc_track_type_to_escat( i_type),
1043 i_codec );
1046 // Release media descriptor's elementary streams description array
1047 void libvlc_media_tracks_release( libvlc_media_track_t **p_tracks, unsigned i_count )
1049 for( unsigned i = 0; i < i_count; ++i )
1051 if ( !p_tracks[i] )
1052 continue;
1053 libvlc_media_track_clean( p_tracks[i] );
1054 free( p_tracks[i] );
1056 free( p_tracks );
1059 // Get the media type of the media descriptor object
1060 libvlc_media_type_t libvlc_media_get_type( libvlc_media_t *p_md )
1062 assert( p_md );
1064 enum input_item_type_e i_type;
1065 input_item_t *p_input_item = p_md->p_input_item;
1067 vlc_mutex_lock( &p_input_item->lock );
1068 i_type = p_md->p_input_item->i_type;
1069 vlc_mutex_unlock( &p_input_item->lock );
1071 switch( i_type )
1073 case ITEM_TYPE_FILE:
1074 return libvlc_media_type_file;
1075 case ITEM_TYPE_NODE:
1076 case ITEM_TYPE_DIRECTORY:
1077 return libvlc_media_type_directory;
1078 case ITEM_TYPE_DISC:
1079 return libvlc_media_type_disc;
1080 case ITEM_TYPE_STREAM:
1081 return libvlc_media_type_stream;
1082 case ITEM_TYPE_PLAYLIST:
1083 return libvlc_media_type_playlist;
1084 default:
1085 return libvlc_media_type_unknown;
1089 struct libvlc_media_thumbnail_request_t
1091 libvlc_media_t *md;
1092 unsigned int width;
1093 unsigned int height;
1094 bool crop;
1095 libvlc_picture_type_t type;
1096 vlc_thumbnailer_request_t* req;
1099 static void media_on_thumbnail_ready( void* data, picture_t* thumbnail )
1101 libvlc_media_thumbnail_request_t *req = data;
1102 libvlc_media_t *p_media = req->md;
1103 libvlc_event_t event;
1104 event.type = libvlc_MediaThumbnailGenerated;
1105 libvlc_picture_t* pic = NULL;
1106 if ( thumbnail != NULL )
1107 pic = libvlc_picture_new( VLC_OBJECT(p_media->p_libvlc_instance->p_libvlc_int),
1108 thumbnail, req->type, req->width, req->height,
1109 req->crop );
1110 event.u.media_thumbnail_generated.p_thumbnail = pic;
1111 libvlc_event_send( &p_media->event_manager, &event );
1112 if ( pic != NULL )
1113 libvlc_picture_release( pic );
1116 // Start an asynchronous thumbnail generation
1117 libvlc_media_thumbnail_request_t*
1118 libvlc_media_thumbnail_request_by_time( libvlc_media_t *md, libvlc_time_t time,
1119 libvlc_thumbnailer_seek_speed_t speed,
1120 unsigned int width, unsigned int height,
1121 bool crop, libvlc_picture_type_t picture_type,
1122 libvlc_time_t timeout )
1124 assert( md );
1125 libvlc_priv_t *p_priv = libvlc_priv(md->p_libvlc_instance->p_libvlc_int);
1126 if( unlikely( p_priv->p_thumbnailer == NULL ) )
1127 return NULL;
1128 libvlc_media_thumbnail_request_t *req = malloc( sizeof( *req ) );
1129 if ( unlikely( req == NULL ) )
1130 return NULL;
1132 req->md = md;
1133 req->width = width;
1134 req->height = height;
1135 req->type = picture_type;
1136 req->crop = crop;
1137 libvlc_media_retain( md );
1138 req->req = vlc_thumbnailer_RequestByTime( p_priv->p_thumbnailer,
1139 VLC_TICK_FROM_MS( time ),
1140 speed == libvlc_media_thumbnail_seek_fast ?
1141 VLC_THUMBNAILER_SEEK_FAST : VLC_THUMBNAILER_SEEK_PRECISE,
1142 md->p_input_item,
1143 timeout > 0 ? VLC_TICK_FROM_MS( timeout ) : VLC_TICK_INVALID,
1144 media_on_thumbnail_ready, req );
1145 if ( req->req == NULL )
1147 free( req );
1148 libvlc_media_release( md );
1149 return NULL;
1151 return req;
1154 // Start an asynchronous thumbnail generation
1155 libvlc_media_thumbnail_request_t*
1156 libvlc_media_thumbnail_request_by_pos( libvlc_media_t *md, float pos,
1157 libvlc_thumbnailer_seek_speed_t speed,
1158 unsigned int width, unsigned int height,
1159 bool crop, libvlc_picture_type_t picture_type,
1160 libvlc_time_t timeout )
1162 assert( md );
1163 libvlc_priv_t *priv = libvlc_priv(md->p_libvlc_instance->p_libvlc_int);
1164 if( unlikely( priv->p_thumbnailer == NULL ) )
1165 return NULL;
1166 libvlc_media_thumbnail_request_t *req = malloc( sizeof( *req ) );
1167 if ( unlikely( req == NULL ) )
1168 return NULL;
1170 req->md = md;
1171 req->width = width;
1172 req->height = height;
1173 req->crop = crop;
1174 req->type = picture_type;
1175 libvlc_media_retain( md );
1176 req->req = vlc_thumbnailer_RequestByPos( priv->p_thumbnailer, pos,
1177 speed == libvlc_media_thumbnail_seek_fast ?
1178 VLC_THUMBNAILER_SEEK_FAST : VLC_THUMBNAILER_SEEK_PRECISE,
1179 md->p_input_item,
1180 timeout > 0 ? VLC_TICK_FROM_MS( timeout ) : VLC_TICK_INVALID,
1181 media_on_thumbnail_ready, req );
1182 if ( req->req == NULL )
1184 free( req );
1185 libvlc_media_release( md );
1186 return NULL;
1188 return req;
1191 // Cancel a thumbnail request
1192 void libvlc_media_thumbnail_request_cancel( libvlc_media_thumbnail_request_t *req )
1194 libvlc_priv_t *p_priv = libvlc_priv(req->md->p_libvlc_instance->p_libvlc_int);
1195 assert( p_priv->p_thumbnailer != NULL );
1196 vlc_thumbnailer_Cancel( p_priv->p_thumbnailer, req->req );
1199 // Destroy a thumbnail request
1200 void libvlc_media_thumbnail_request_destroy( libvlc_media_thumbnail_request_t *req )
1202 libvlc_media_release( req->md );
1203 free( req );
1206 // Add a slave to the media descriptor
1207 int libvlc_media_slaves_add( libvlc_media_t *p_md,
1208 libvlc_media_slave_type_t i_type,
1209 unsigned int i_priority,
1210 const char *psz_uri )
1212 assert( p_md && psz_uri );
1213 input_item_t *p_input_item = p_md->p_input_item;
1215 enum slave_type i_input_slave_type;
1216 switch( i_type )
1218 case libvlc_media_slave_type_subtitle:
1219 i_input_slave_type = SLAVE_TYPE_SPU;
1220 break;
1221 case libvlc_media_slave_type_audio:
1222 i_input_slave_type = SLAVE_TYPE_AUDIO;
1223 break;
1224 default:
1225 vlc_assert_unreachable();
1226 return -1;
1229 enum slave_priority i_input_slave_priority;
1230 switch( i_priority )
1232 case 0:
1233 i_input_slave_priority = SLAVE_PRIORITY_MATCH_NONE;
1234 break;
1235 case 1:
1236 i_input_slave_priority = SLAVE_PRIORITY_MATCH_RIGHT;
1237 break;
1238 case 2:
1239 i_input_slave_priority = SLAVE_PRIORITY_MATCH_LEFT;
1240 break;
1241 case 3:
1242 i_input_slave_priority = SLAVE_PRIORITY_MATCH_ALL;
1243 break;
1244 default:
1245 case 4:
1246 i_input_slave_priority = SLAVE_PRIORITY_USER;
1247 break;
1250 input_item_slave_t *p_slave = input_item_slave_New( psz_uri,
1251 i_input_slave_type,
1252 i_input_slave_priority );
1253 if( p_slave == NULL )
1254 return -1;
1255 return input_item_AddSlave( p_input_item, p_slave ) == VLC_SUCCESS ? 0 : -1;
1258 // Clear all slaves of the media descriptor
1259 void libvlc_media_slaves_clear( libvlc_media_t *p_md )
1261 assert( p_md );
1262 input_item_t *p_input_item = p_md->p_input_item;
1264 vlc_mutex_lock( &p_input_item->lock );
1265 for( int i = 0; i < p_input_item->i_slaves; i++ )
1266 input_item_slave_Delete( p_input_item->pp_slaves[i] );
1267 TAB_CLEAN( p_input_item->i_slaves, p_input_item->pp_slaves );
1268 vlc_mutex_unlock( &p_input_item->lock );
1271 // Get a media descriptor's slave list
1272 unsigned int libvlc_media_slaves_get( libvlc_media_t *p_md,
1273 libvlc_media_slave_t ***ppp_slaves )
1275 assert( p_md && ppp_slaves );
1276 input_item_t *p_input_item = p_md->p_input_item;
1277 *ppp_slaves = NULL;
1279 vlc_mutex_lock( &p_input_item->lock );
1281 int i_count = p_input_item->i_slaves;
1282 if( i_count <= 0 )
1283 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1285 libvlc_media_slave_t **pp_slaves = calloc( i_count, sizeof(*pp_slaves) );
1286 if( pp_slaves == NULL )
1287 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1289 for( int i = 0; i < i_count; ++i )
1291 input_item_slave_t *p_item_slave = p_input_item->pp_slaves[i];
1292 assert( p_item_slave->i_priority >= SLAVE_PRIORITY_MATCH_NONE );
1294 /* also allocate psz_uri buffer at the end of the struct */
1295 libvlc_media_slave_t *p_slave = malloc( sizeof(*p_slave) +
1296 strlen( p_item_slave->psz_uri )
1297 + 1 );
1298 if( p_slave == NULL )
1300 libvlc_media_slaves_release(pp_slaves, i);
1301 return vlc_mutex_unlock( &p_input_item->lock ), 0;
1303 p_slave->psz_uri = (char *) ((uint8_t *)p_slave) + sizeof(*p_slave);
1304 strcpy( p_slave->psz_uri, p_item_slave->psz_uri );
1306 switch( p_item_slave->i_type )
1308 case SLAVE_TYPE_SPU:
1309 p_slave->i_type = libvlc_media_slave_type_subtitle;
1310 break;
1311 case SLAVE_TYPE_AUDIO:
1312 p_slave->i_type = libvlc_media_slave_type_audio;
1313 break;
1314 default:
1315 vlc_assert_unreachable();
1318 switch( p_item_slave->i_priority )
1320 case SLAVE_PRIORITY_MATCH_NONE:
1321 p_slave->i_priority = 0;
1322 break;
1323 case SLAVE_PRIORITY_MATCH_RIGHT:
1324 p_slave->i_priority = 1;
1325 break;
1326 case SLAVE_PRIORITY_MATCH_LEFT:
1327 p_slave->i_priority = 2;
1328 break;
1329 case SLAVE_PRIORITY_MATCH_ALL:
1330 p_slave->i_priority = 3;
1331 break;
1332 case SLAVE_PRIORITY_USER:
1333 p_slave->i_priority = 4;
1334 break;
1335 default:
1336 vlc_assert_unreachable();
1338 pp_slaves[i] = p_slave;
1340 vlc_mutex_unlock( &p_input_item->lock );
1342 *ppp_slaves = pp_slaves;
1343 return i_count;
1346 // Release a media descriptor's slave list
1347 void libvlc_media_slaves_release( libvlc_media_slave_t **pp_slaves,
1348 unsigned int i_count )
1350 if( i_count > 0 )
1352 assert( pp_slaves );
1353 for( unsigned int i = 0; i < i_count; ++i )
1354 free( pp_slaves[i] );
1356 free( pp_slaves );