1 /*****************************************************************************
2 * media.c: Libvlc API media descripor management
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Pierre d'Herbemont <pdherbemont@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
30 #include <vlc/libvlc.h>
31 #include <vlc/libvlc_media.h>
32 #include <vlc/libvlc_media_list.h> // For the subitems, here for convenience
33 #include <vlc/libvlc_events.h>
35 #include <vlc_common.h>
36 #include <vlc_input.h>
38 #include <vlc_playlist.h> /* For the preparser */
42 #include "libvlc_internal.h"
43 #include "media_internal.h"
45 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
47 [libvlc_meta_Title] = vlc_meta_Title,
48 [libvlc_meta_Artist] = vlc_meta_Artist,
49 [libvlc_meta_Genre] = vlc_meta_Genre,
50 [libvlc_meta_Copyright] = vlc_meta_Copyright,
51 [libvlc_meta_Album] = vlc_meta_Album,
52 [libvlc_meta_TrackNumber] = vlc_meta_TrackNumber,
53 [libvlc_meta_Description] = vlc_meta_Description,
54 [libvlc_meta_Rating] = vlc_meta_Rating,
55 [libvlc_meta_Date] = vlc_meta_Date,
56 [libvlc_meta_Setting] = vlc_meta_Setting,
57 [libvlc_meta_URL] = vlc_meta_URL,
58 [libvlc_meta_Language] = vlc_meta_Language,
59 [libvlc_meta_NowPlaying] = vlc_meta_NowPlaying,
60 [libvlc_meta_Publisher] = vlc_meta_Publisher,
61 [libvlc_meta_EncodedBy] = vlc_meta_EncodedBy,
62 [libvlc_meta_ArtworkURL] = vlc_meta_ArtworkURL,
63 [libvlc_meta_TrackID] = vlc_meta_TrackID
66 static const libvlc_meta_t vlc_to_libvlc_meta[] =
68 [vlc_meta_Title] = libvlc_meta_Title,
69 [vlc_meta_Artist] = libvlc_meta_Artist,
70 [vlc_meta_Genre] = libvlc_meta_Genre,
71 [vlc_meta_Copyright] = libvlc_meta_Copyright,
72 [vlc_meta_Album] = libvlc_meta_Album,
73 [vlc_meta_TrackNumber] = libvlc_meta_TrackNumber,
74 [vlc_meta_Description] = libvlc_meta_Description,
75 [vlc_meta_Rating] = libvlc_meta_Rating,
76 [vlc_meta_Date] = libvlc_meta_Date,
77 [vlc_meta_Setting] = libvlc_meta_Setting,
78 [vlc_meta_URL] = libvlc_meta_URL,
79 [vlc_meta_Language] = libvlc_meta_Language,
80 [vlc_meta_NowPlaying] = libvlc_meta_NowPlaying,
81 [vlc_meta_Publisher] = libvlc_meta_Publisher,
82 [vlc_meta_EncodedBy] = libvlc_meta_EncodedBy,
83 [vlc_meta_ArtworkURL] = libvlc_meta_ArtworkURL,
84 [vlc_meta_TrackID] = libvlc_meta_TrackID
87 /**************************************************************************
88 * input_item_subitem_added (Private) (vlc event Callback)
89 **************************************************************************/
90 static void input_item_subitem_added( const vlc_event_t *p_event,
93 libvlc_media_t * p_md = user_data;
94 libvlc_media_t * p_md_child;
97 p_md_child = libvlc_media_new_from_input_item(
98 p_md->p_libvlc_instance,
99 p_event->u.input_item_subitem_added.p_new_child, NULL );
101 /* Add this to our media list */
102 if( !p_md->p_subitems )
104 p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );
105 libvlc_media_list_set_media( p_md->p_subitems, p_md, NULL );
107 if( p_md->p_subitems )
109 libvlc_media_list_add_media( p_md->p_subitems, p_md_child, NULL );
112 /* Construct the event */
113 event.type = libvlc_MediaSubItemAdded;
114 event.u.media_subitem_added.new_child = p_md_child;
117 libvlc_event_send( p_md->p_event_manager, &event );
118 libvlc_media_release( p_md_child );
121 /**************************************************************************
122 * input_item_meta_changed (Private) (vlc event Callback)
123 **************************************************************************/
124 static void input_item_meta_changed( const vlc_event_t *p_event,
127 libvlc_media_t * p_md = user_data;
128 libvlc_event_t event;
130 /* Construct the event */
131 event.type = libvlc_MediaMetaChanged;
132 event.u.media_meta_changed.meta_type =
133 vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
136 libvlc_event_send( p_md->p_event_manager, &event );
139 /**************************************************************************
140 * input_item_duration_changed (Private) (vlc event Callback)
141 **************************************************************************/
142 static void input_item_duration_changed( const vlc_event_t *p_event,
145 libvlc_media_t * p_md = user_data;
146 libvlc_event_t event;
148 /* Construct the event */
149 event.type = libvlc_MediaDurationChanged;
150 event.u.media_duration_changed.new_duration =
151 from_mtime(p_event->u.input_item_duration_changed.new_duration);
154 libvlc_event_send( p_md->p_event_manager, &event );
157 /**************************************************************************
158 * input_item_preparsed_changed (Private) (vlc event Callback)
159 **************************************************************************/
160 static void input_item_preparsed_changed( const vlc_event_t *p_event,
163 libvlc_media_t * p_md = user_data;
164 libvlc_event_t event;
166 /* Construct the event */
167 event.type = libvlc_MediaPreparsedChanged;
168 event.u.media_preparsed_changed.new_status =
169 p_event->u.input_item_preparsed_changed.new_status;
172 libvlc_event_send( p_md->p_event_manager, &event );
175 /**************************************************************************
176 * Install event handler (Private)
177 **************************************************************************/
178 static void install_input_item_observer( libvlc_media_t *p_md )
180 vlc_event_attach( &p_md->p_input_item->event_manager,
181 vlc_InputItemSubItemAdded,
182 input_item_subitem_added,
184 vlc_event_attach( &p_md->p_input_item->event_manager,
185 vlc_InputItemMetaChanged,
186 input_item_meta_changed,
188 vlc_event_attach( &p_md->p_input_item->event_manager,
189 vlc_InputItemDurationChanged,
190 input_item_duration_changed,
192 vlc_event_attach( &p_md->p_input_item->event_manager,
193 vlc_InputItemPreparsedChanged,
194 input_item_preparsed_changed,
198 /**************************************************************************
199 * Uninstall event handler (Private)
200 **************************************************************************/
201 static void uninstall_input_item_observer( libvlc_media_t *p_md )
203 vlc_event_detach( &p_md->p_input_item->event_manager,
204 vlc_InputItemSubItemAdded,
205 input_item_subitem_added,
207 vlc_event_detach( &p_md->p_input_item->event_manager,
208 vlc_InputItemMetaChanged,
209 input_item_meta_changed,
211 vlc_event_detach( &p_md->p_input_item->event_manager,
212 vlc_InputItemDurationChanged,
213 input_item_duration_changed,
215 vlc_event_detach( &p_md->p_input_item->event_manager,
216 vlc_InputItemPreparsedChanged,
217 input_item_preparsed_changed,
221 /**************************************************************************
222 * Preparse if not already done (Private)
223 **************************************************************************/
224 static void preparse_if_needed( libvlc_media_t *p_md )
226 /* XXX: need some locking here */
227 if (!p_md->b_preparsed)
229 playlist_PreparseEnqueue(
230 libvlc_priv (p_md->p_libvlc_instance->p_libvlc_int)->p_playlist,
231 p_md->p_input_item, pl_Unlocked );
232 p_md->b_preparsed = true;
236 /**************************************************************************
237 * Create a new media descriptor object from an input_item
239 * That's the generic constructor
240 **************************************************************************/
241 libvlc_media_t * libvlc_media_new_from_input_item(
242 libvlc_instance_t *p_instance,
243 input_item_t *p_input_item,
244 libvlc_exception_t *p_e )
246 libvlc_media_t * p_md;
250 libvlc_exception_raise( p_e );
251 libvlc_printerr( "No input item given" );
255 p_md = calloc( 1, sizeof(libvlc_media_t) );
258 libvlc_exception_raise( p_e );
259 libvlc_printerr( "Not enough memory" );
263 p_md->p_libvlc_instance = p_instance;
264 p_md->p_input_item = p_input_item;
265 p_md->i_refcount = 1;
267 p_md->state = libvlc_NothingSpecial;
269 /* A media descriptor can be a playlist. When you open a playlist
270 * It can give a bunch of item to read. */
271 p_md->p_subitems = NULL;
273 p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance, p_e );
274 libvlc_event_manager_register_event_type( p_md->p_event_manager,
275 libvlc_MediaMetaChanged, p_e );
276 libvlc_event_manager_register_event_type( p_md->p_event_manager,
277 libvlc_MediaSubItemAdded, p_e );
278 libvlc_event_manager_register_event_type( p_md->p_event_manager,
279 libvlc_MediaFreed, p_e );
280 libvlc_event_manager_register_event_type( p_md->p_event_manager,
281 libvlc_MediaDurationChanged, p_e );
282 libvlc_event_manager_register_event_type( p_md->p_event_manager,
283 libvlc_MediaStateChanged, p_e );
285 vlc_gc_incref( p_md->p_input_item );
287 install_input_item_observer( p_md );
292 /**************************************************************************
293 * Create a new media descriptor object
294 **************************************************************************/
295 libvlc_media_t * libvlc_media_new(
296 libvlc_instance_t *p_instance,
297 const char * psz_mrl,
298 libvlc_exception_t *p_e )
300 input_item_t * p_input_item;
301 libvlc_media_t * p_md;
303 p_input_item = input_item_New( p_instance->p_libvlc_int, psz_mrl, NULL );
307 libvlc_exception_raise( p_e );
308 libvlc_printerr( "Not enough memory" );
312 p_md = libvlc_media_new_from_input_item( p_instance,
315 /* The p_input_item is retained in libvlc_media_new_from_input_item */
316 vlc_gc_decref( p_input_item );
321 /**************************************************************************
322 * Create a new media descriptor object
323 **************************************************************************/
324 libvlc_media_t * libvlc_media_new_as_node(
325 libvlc_instance_t *p_instance,
326 const char * psz_name,
327 libvlc_exception_t *p_e )
329 input_item_t * p_input_item;
330 libvlc_media_t * p_md;
332 p_input_item = input_item_New( p_instance->p_libvlc_int, "vlc://nop", psz_name );
336 libvlc_exception_raise( p_e );
337 libvlc_printerr( "Not enough memory" );
341 p_md = libvlc_media_new_from_input_item( p_instance,
344 p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );
349 /**************************************************************************
350 * Add an option to the media descriptor,
351 * that will be used to determine how the media_player will read the
352 * media. This allow to use VLC advanced reading/streaming
353 * options in a per-media basis
355 * The options are detailled in vlc --long-help, for instance "--sout-all"
356 **************************************************************************/
357 void libvlc_media_add_option(
358 libvlc_media_t * p_md,
359 const char * psz_option )
361 input_item_AddOption( p_md->p_input_item, psz_option,
362 VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
365 /**************************************************************************
366 * Same as libvlc_media_add_option but with configurable flags.
367 **************************************************************************/
368 void libvlc_media_add_option_flag(
369 libvlc_media_t * p_md,
370 const char * ppsz_option,
371 libvlc_media_option_t i_flags )
373 input_item_AddOption( p_md->p_input_item, ppsz_option,
377 /**************************************************************************
378 * Delete a media descriptor object
379 **************************************************************************/
380 void libvlc_media_release( libvlc_media_t *p_md )
387 if( p_md->i_refcount > 0 )
390 if( p_md->p_subitems )
391 libvlc_media_list_release( p_md->p_subitems );
393 uninstall_input_item_observer( p_md );
394 vlc_gc_decref( p_md->p_input_item );
396 /* Construct the event */
397 libvlc_event_t event;
398 event.type = libvlc_MediaFreed;
399 event.u.media_freed.md = p_md;
402 libvlc_event_send( p_md->p_event_manager, &event );
404 libvlc_event_manager_release( p_md->p_event_manager );
409 /**************************************************************************
410 * Retain a media descriptor object
411 **************************************************************************/
412 void libvlc_media_retain( libvlc_media_t *p_md )
418 /**************************************************************************
419 * Duplicate a media descriptor object
420 **************************************************************************/
422 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
424 return libvlc_media_new_from_input_item(
425 p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
428 /**************************************************************************
429 * Get mrl from a media descriptor object
430 **************************************************************************/
432 libvlc_media_get_mrl( libvlc_media_t * p_md )
435 return input_item_GetURI( p_md->p_input_item );
438 /**************************************************************************
439 * Getter for meta information
440 **************************************************************************/
442 char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
449 preparse_if_needed( p_md );
451 psz_meta = input_item_GetMeta( p_md->p_input_item,
452 libvlc_to_vlc_meta[e_meta] );
454 if( e_meta == libvlc_meta_ArtworkURL && !psz_meta && !p_md->has_asked_art )
456 p_md->has_asked_art = true;
457 playlist_AskForArtEnqueue(
458 libvlc_priv(p_md->p_libvlc_instance->p_libvlc_int)->p_playlist,
459 p_md->p_input_item, pl_Unlocked );
462 /* Should be integrated in core */
463 if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
466 return strdup( p_md->p_input_item->psz_name );
472 /**************************************************************************
473 * Setter for meta information
474 **************************************************************************/
476 void libvlc_media_set_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta, const char *psz_value )
479 input_item_SetMeta( p_md->p_input_item, libvlc_to_vlc_meta[e_meta], psz_value );
482 int libvlc_media_save_meta( libvlc_media_t *p_md )
485 vlc_object_t *p_obj = VLC_OBJECT(libvlc_priv(
486 p_md->p_libvlc_instance->p_libvlc_int)->p_playlist);
487 return input_item_WriteMeta( p_obj, p_md->p_input_item ) == VLC_SUCCESS;
490 /**************************************************************************
491 * Getter for state information
492 * Can be error, playing, buffering, NothingSpecial.
493 **************************************************************************/
496 libvlc_media_get_state( libvlc_media_t *p_md )
502 /**************************************************************************
503 * Setter for state information (LibVLC Internal)
504 **************************************************************************/
507 libvlc_media_set_state( libvlc_media_t *p_md,
508 libvlc_state_t state )
510 libvlc_event_t event;
514 /* Construct the event */
515 event.type = libvlc_MediaStateChanged;
516 event.u.media_state_changed.new_state = state;
519 libvlc_event_send( p_md->p_event_manager, &event );
522 /**************************************************************************
524 **************************************************************************/
525 libvlc_media_list_t *
526 libvlc_media_subitems( libvlc_media_t * p_md )
528 if( p_md->p_subitems )
529 libvlc_media_list_retain( p_md->p_subitems );
530 return p_md->p_subitems;
533 /**************************************************************************
534 * Getter for statistics information
535 **************************************************************************/
536 int libvlc_media_get_stats( libvlc_media_t *p_md,
537 libvlc_media_stats_t *p_stats )
539 if( !p_md->p_input_item )
542 input_stats_t *p_itm_stats = p_md->p_input_item->p_stats;
543 vlc_mutex_lock( &p_itm_stats->lock );
544 p_stats->i_read_bytes = p_itm_stats->i_read_bytes;
545 p_stats->f_input_bitrate = p_itm_stats->f_input_bitrate;
547 p_stats->i_demux_read_bytes = p_itm_stats->i_demux_read_bytes;
548 p_stats->f_demux_bitrate = p_itm_stats->f_demux_bitrate;
549 p_stats->i_demux_corrupted = p_itm_stats->i_demux_corrupted;
550 p_stats->i_demux_discontinuity = p_itm_stats->i_demux_discontinuity;
552 p_stats->i_decoded_video = p_itm_stats->i_decoded_video;
553 p_stats->i_decoded_audio = p_itm_stats->i_decoded_audio;
555 p_stats->i_displayed_pictures = p_itm_stats->i_displayed_pictures;
556 p_stats->i_lost_pictures = p_itm_stats->i_lost_pictures;
558 p_stats->i_played_abuffers = p_itm_stats->i_played_abuffers;
559 p_stats->i_lost_abuffers = p_itm_stats->i_lost_abuffers;
561 p_stats->i_sent_packets = p_itm_stats->i_sent_packets;
562 p_stats->i_sent_bytes = p_itm_stats->i_sent_bytes;
563 p_stats->f_send_bitrate = p_itm_stats->f_send_bitrate;
564 vlc_mutex_unlock( &p_itm_stats->lock );
568 /**************************************************************************
570 **************************************************************************/
571 libvlc_event_manager_t *
572 libvlc_media_event_manager( libvlc_media_t * p_md )
576 return p_md->p_event_manager;
579 /**************************************************************************
580 * Get duration of media object (in ms)
581 **************************************************************************/
583 libvlc_media_get_duration( libvlc_media_t * p_md, libvlc_exception_t *p_e )
587 if( !p_md->p_input_item )
589 libvlc_exception_raise( p_e );
590 libvlc_printerr( "No input item" );
594 preparse_if_needed( p_md );
596 if (!input_item_IsPreparsed( p_md->p_input_item ))
599 return from_mtime(input_item_GetDuration( p_md->p_input_item ));
602 /**************************************************************************
603 * Get preparsed status for media object.
604 **************************************************************************/
606 libvlc_media_is_preparsed( libvlc_media_t * p_md )
610 if( !p_md->p_input_item )
613 return input_item_IsPreparsed( p_md->p_input_item );
616 /**************************************************************************
617 * Sets media descriptor's user_data. user_data is specialized data
618 * accessed by the host application, VLC.framework uses it as a pointer to
619 * an native object that references a libvlc_media_t pointer
620 **************************************************************************/
622 libvlc_media_set_user_data( libvlc_media_t * p_md, void * p_new_user_data )
625 p_md->p_user_data = p_new_user_data;
628 /**************************************************************************
629 * Get media descriptor's user_data. user_data is specialized data
630 * accessed by the host application, VLC.framework uses it as a pointer to
631 * an native object that references a libvlc_media_t pointer
632 **************************************************************************/
634 libvlc_media_get_user_data( libvlc_media_t * p_md )
637 return p_md->p_user_data;
640 /**************************************************************************
641 * Get media descriptor's elementary streams description
642 **************************************************************************/
644 libvlc_media_get_es( libvlc_media_t *p_md, libvlc_media_es_t ** pp_es )
648 input_item_t *p_input_item = p_md->p_input_item;
649 vlc_mutex_lock( &p_input_item->lock );
651 const int i_es = p_input_item->i_es;
652 *pp_es = (i_es > 0) ? malloc( i_es * sizeof(libvlc_media_es_t) ) : NULL;
654 if( !pp_es ) /* no ES, or OOM */
656 vlc_mutex_unlock( &p_input_item->lock );
661 for( int i = 0; i < i_es; i++ )
663 libvlc_media_es_t *p_mes = *pp_es+i;
664 const es_format_t *p_es = p_input_item->es[i];
666 p_mes->i_channels = p_mes->i_rate = 0;
667 p_mes->i_width = p_mes->i_height = 0;
670 p_mes->i_codec = p_es->i_codec;
672 p_mes->i_profile = p_es->i_profile;
673 p_mes->i_level = p_es->i_level;
679 p_mes->i_type = libvlc_es_unknown;
682 p_mes->i_type = libvlc_es_video;
683 p_mes->i_height = p_es->video.i_height;
684 p_mes->i_width = p_es->video.i_width;
687 p_mes->i_type = libvlc_es_audio;
688 p_mes->i_channels = p_es->audio.i_channels;
689 p_mes->i_rate = p_es->audio.i_rate;
692 p_mes->i_type = libvlc_es_text;
697 vlc_mutex_unlock( &p_input_item->lock );