1 /*****************************************************************************
2 * media_descriptor.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 *****************************************************************************/
24 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
27 #include <vlc_input.h>
30 /* For the preparser */
31 #include <vlc_playlist.h>
33 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
35 [libvlc_meta_Title] = vlc_meta_Title,
36 [libvlc_meta_Artist] = vlc_meta_Artist,
37 [libvlc_meta_Genre] = vlc_meta_Genre,
38 [libvlc_meta_Copyright] = vlc_meta_Copyright,
39 [libvlc_meta_Album] = vlc_meta_Album,
40 [libvlc_meta_TrackNumber] = vlc_meta_TrackNumber,
41 [libvlc_meta_Description] = vlc_meta_Description,
42 [libvlc_meta_Rating] = vlc_meta_Rating,
43 [libvlc_meta_Date] = vlc_meta_Date,
44 [libvlc_meta_Setting] = vlc_meta_Setting,
45 [libvlc_meta_URL] = vlc_meta_URL,
46 [libvlc_meta_Language] = vlc_meta_Language,
47 [libvlc_meta_NowPlaying] = vlc_meta_NowPlaying,
48 [libvlc_meta_Publisher] = vlc_meta_Publisher,
49 [libvlc_meta_EncodedBy] = vlc_meta_EncodedBy,
50 [libvlc_meta_ArtworkURL] = vlc_meta_ArtworkURL,
51 [libvlc_meta_TrackID] = vlc_meta_TrackID
54 static const libvlc_meta_t vlc_to_libvlc_meta[] =
56 [vlc_meta_Title] = libvlc_meta_Title,
57 [vlc_meta_Artist] = libvlc_meta_Artist,
58 [vlc_meta_Genre] = libvlc_meta_Genre,
59 [vlc_meta_Copyright] = libvlc_meta_Copyright,
60 [vlc_meta_Album] = libvlc_meta_Album,
61 [vlc_meta_TrackNumber] = libvlc_meta_TrackNumber,
62 [vlc_meta_Description] = libvlc_meta_Description,
63 [vlc_meta_Rating] = libvlc_meta_Rating,
64 [vlc_meta_Date] = libvlc_meta_Date,
65 [vlc_meta_Setting] = libvlc_meta_Setting,
66 [vlc_meta_URL] = libvlc_meta_URL,
67 [vlc_meta_Language] = libvlc_meta_Language,
68 [vlc_meta_NowPlaying] = libvlc_meta_NowPlaying,
69 [vlc_meta_Publisher] = libvlc_meta_Publisher,
70 [vlc_meta_EncodedBy] = libvlc_meta_EncodedBy,
71 [vlc_meta_ArtworkURL] = libvlc_meta_ArtworkURL,
72 [vlc_meta_TrackID] = libvlc_meta_TrackID
75 /**************************************************************************
76 * input_item_subitem_added (Private) (vlc event Callback)
77 **************************************************************************/
78 static void input_item_subitem_added( const vlc_event_t *p_event,
81 libvlc_media_descriptor_t * p_md = user_data;
82 libvlc_media_descriptor_t * p_md_child;
85 p_md_child = libvlc_media_descriptor_new_from_input_item(
86 p_md->p_libvlc_instance,
87 p_event->u.input_item_subitem_added.p_new_child, NULL );
89 /* Add this to our media list */
90 if( !p_md->p_subitems )
92 p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );
93 libvlc_media_list_set_media_descriptor( p_md->p_subitems, p_md, NULL );
95 if( p_md->p_subitems )
97 libvlc_media_list_add_media_descriptor( p_md->p_subitems, p_md_child, NULL );
100 /* Construct the event */
101 event.type = libvlc_MediaDescriptorSubItemAdded;
102 event.u.media_descriptor_subitem_added.new_child = p_md_child;
105 libvlc_event_send( p_md->p_event_manager, &event );
106 libvlc_media_descriptor_release( p_md_child );
109 /**************************************************************************
110 * input_item_meta_changed (Private) (vlc event Callback)
111 **************************************************************************/
112 static void input_item_meta_changed( const vlc_event_t *p_event,
115 libvlc_media_descriptor_t * p_md = user_data;
116 libvlc_event_t event;
118 /* Construct the event */
119 event.type = libvlc_MediaDescriptorMetaChanged;
120 event.u.media_descriptor_meta_changed.meta_type =
121 vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
124 libvlc_event_send( p_md->p_event_manager, &event );
127 /**************************************************************************
128 * input_item_duration_changed (Private) (vlc event Callback)
129 **************************************************************************/
130 static void input_item_duration_changed( const vlc_event_t *p_event,
133 libvlc_media_descriptor_t * p_md = user_data;
134 libvlc_event_t event;
136 /* Construct the event */
137 event.type = libvlc_MediaDescriptorDurationChanged;
138 event.u.media_descriptor_duration_changed.new_duration =
139 p_event->u.input_item_duration_changed.new_duration;
142 libvlc_event_send( p_md->p_event_manager, &event );
145 /**************************************************************************
146 * input_item_preparsed_changed (Private) (vlc event Callback)
147 **************************************************************************/
148 static void input_item_preparsed_changed( const vlc_event_t *p_event,
151 libvlc_media_descriptor_t * p_md = user_data;
152 libvlc_event_t event;
154 /* Construct the event */
155 event.type = libvlc_MediaDescriptorPreparsedChanged;
156 event.u.media_descriptor_preparsed_changed.new_status =
157 p_event->u.input_item_preparsed_changed.new_status;
160 libvlc_event_send( p_md->p_event_manager, &event );
163 /**************************************************************************
164 * Install event handler (Private)
165 **************************************************************************/
166 static void install_input_item_observer( libvlc_media_descriptor_t *p_md )
168 vlc_event_attach( &p_md->p_input_item->event_manager,
169 vlc_InputItemSubItemAdded,
170 input_item_subitem_added,
172 vlc_event_attach( &p_md->p_input_item->event_manager,
173 vlc_InputItemMetaChanged,
174 input_item_meta_changed,
176 vlc_event_attach( &p_md->p_input_item->event_manager,
177 vlc_InputItemDurationChanged,
178 input_item_duration_changed,
180 vlc_event_attach( &p_md->p_input_item->event_manager,
181 vlc_InputItemPreparsedChanged,
182 input_item_preparsed_changed,
186 /**************************************************************************
187 * Uninstall event handler (Private)
188 **************************************************************************/
189 static void uninstall_input_item_observer( libvlc_media_descriptor_t *p_md )
191 vlc_event_detach( &p_md->p_input_item->event_manager,
192 vlc_InputItemSubItemAdded,
193 input_item_subitem_added,
195 vlc_event_detach( &p_md->p_input_item->event_manager,
196 vlc_InputItemMetaChanged,
197 input_item_meta_changed,
199 vlc_event_detach( &p_md->p_input_item->event_manager,
200 vlc_InputItemDurationChanged,
201 input_item_duration_changed,
203 vlc_event_detach( &p_md->p_input_item->event_manager,
204 vlc_InputItemPreparsedChanged,
205 input_item_preparsed_changed,
209 /**************************************************************************
210 * Preparse if not already done (Private)
211 **************************************************************************/
212 static void preparse_if_needed( libvlc_media_descriptor_t *p_md )
214 /* XXX: need some locking here */
215 if (!p_md->b_preparsed)
217 playlist_PreparseEnqueue(
218 p_md->p_libvlc_instance->p_libvlc_int->p_playlist,
219 p_md->p_input_item );
220 p_md->b_preparsed = VLC_TRUE;
224 /**************************************************************************
225 * Create a new media descriptor object from an input_item
227 * That's the generic constructor
228 **************************************************************************/
229 libvlc_media_descriptor_t * libvlc_media_descriptor_new_from_input_item(
230 libvlc_instance_t *p_instance,
231 input_item_t *p_input_item,
232 libvlc_exception_t *p_e )
234 libvlc_media_descriptor_t * p_md;
238 libvlc_exception_raise( p_e, "No input item given" );
242 p_md = malloc( sizeof(libvlc_media_descriptor_t) );
243 p_md->p_libvlc_instance = p_instance;
244 p_md->p_input_item = p_input_item;
245 p_md->b_preparsed = VLC_FALSE;
246 p_md->i_refcount = 1;
247 p_md->p_user_data = NULL;
249 p_md->state = libvlc_NothingSpecial;
251 /* A media descriptor can be a playlist. When you open a playlist
252 * It can give a bunch of item to read. */
253 p_md->p_subitems = NULL;
255 vlc_dictionary_init( &p_md->tags, 1 );
257 p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance, p_e );
258 libvlc_event_manager_register_event_type( p_md->p_event_manager,
259 libvlc_MediaDescriptorMetaChanged, p_e );
260 libvlc_event_manager_register_event_type( p_md->p_event_manager,
261 libvlc_MediaDescriptorSubItemAdded, p_e );
262 libvlc_event_manager_register_event_type( p_md->p_event_manager,
263 libvlc_MediaDescriptorFreed, p_e );
264 libvlc_event_manager_register_event_type( p_md->p_event_manager,
265 libvlc_MediaDescriptorDurationChanged, p_e );
266 libvlc_event_manager_register_event_type( p_md->p_event_manager,
267 libvlc_MediaDescriptorStateChanged, p_e );
269 vlc_gc_incref( p_md->p_input_item );
271 install_input_item_observer( p_md );
276 /**************************************************************************
277 * Create a new media descriptor object
278 **************************************************************************/
279 libvlc_media_descriptor_t * libvlc_media_descriptor_new(
280 libvlc_instance_t *p_instance,
281 const char * psz_mrl,
282 libvlc_exception_t *p_e )
284 input_item_t * p_input_item;
285 libvlc_media_descriptor_t * p_md;
287 p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, NULL );
291 libvlc_exception_raise( p_e, "Can't create md's input_item" );
295 p_md = libvlc_media_descriptor_new_from_input_item( p_instance,
298 /* The p_input_item is retained in libvlc_media_descriptor_new_from_input_item */
299 vlc_gc_decref( p_input_item );
304 /**************************************************************************
305 * Create a new media descriptor object
306 **************************************************************************/
307 libvlc_media_descriptor_t * libvlc_media_descriptor_new_as_node(
308 libvlc_instance_t *p_instance,
309 const char * psz_name,
310 libvlc_exception_t *p_e )
312 input_item_t * p_input_item;
313 libvlc_media_descriptor_t * p_md;
315 p_input_item = input_ItemNew( p_instance->p_libvlc_int, "vlc:nop", psz_name );
319 libvlc_exception_raise( p_e, "Can't create md's input_item" );
323 p_md = libvlc_media_descriptor_new_from_input_item( p_instance,
326 p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );
331 /**************************************************************************
332 * Add an option to the media descriptor,
333 * that will be used to determine how the media_instance will read the
334 * media_descriptor. This allow to use VLC advanced reading/streaming
335 * options in a per-media basis
337 * The options are detailled in vlc --long-help, for instance "--sout-all"
338 **************************************************************************/
339 void libvlc_media_descriptor_add_option(
340 libvlc_media_descriptor_t * p_md,
341 const char * ppsz_option,
342 libvlc_exception_t *p_e )
345 input_ItemAddOptionNoDup( p_md->p_input_item, ppsz_option );
348 /**************************************************************************
349 * Delete a media descriptor object
350 **************************************************************************/
351 void libvlc_media_descriptor_release( libvlc_media_descriptor_t *p_md )
359 if( p_md->i_refcount > 0 )
362 if( p_md->p_subitems )
363 libvlc_media_list_release( p_md->p_subitems );
365 uninstall_input_item_observer( p_md );
366 vlc_gc_decref( p_md->p_input_item );
368 /* Construct the event */
369 libvlc_event_t event;
370 event.type = libvlc_MediaDescriptorFreed;
371 event.u.media_descriptor_freed.md = p_md;
374 libvlc_event_send( p_md->p_event_manager, &event );
376 libvlc_event_manager_release( p_md->p_event_manager );
378 char ** all_keys = vlc_dictionary_all_keys( &p_md->tags );
379 for( i = 0; all_keys[i]; i++ )
382 struct libvlc_tags_storage_t * p_ts = vlc_dictionary_value_for_key( &p_md->tags, all_keys[i] );
383 for( j = 0; j < p_ts->i_count; j++ )
385 free( p_ts->ppsz_tags[j] );
386 free( p_ts->ppsz_tags );
390 vlc_dictionary_clear( &p_md->tags );
394 /**************************************************************************
395 * Retain a media descriptor object
396 **************************************************************************/
397 void libvlc_media_descriptor_retain( libvlc_media_descriptor_t *p_md )
405 /**************************************************************************
406 * Duplicate a media descriptor object
407 **************************************************************************/
408 libvlc_media_descriptor_t *
409 libvlc_media_descriptor_duplicate( libvlc_media_descriptor_t *p_md_orig )
411 return libvlc_media_descriptor_new_from_input_item(
412 p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
415 /**************************************************************************
416 * Retain a media descriptor object
417 **************************************************************************/
419 libvlc_media_descriptor_get_mrl( libvlc_media_descriptor_t * p_md,
420 libvlc_exception_t * p_e )
423 return input_item_GetURI( p_md->p_input_item );
426 /**************************************************************************
427 * Getter for meta information
428 **************************************************************************/
430 char * libvlc_media_descriptor_get_meta( libvlc_media_descriptor_t *p_md,
431 libvlc_meta_t e_meta,
432 libvlc_exception_t *p_e )
440 preparse_if_needed( p_md );
442 psz_meta = input_item_GetMeta( p_md->p_input_item,
443 libvlc_to_vlc_meta[e_meta] );
445 if( e_meta == libvlc_meta_ArtworkURL && !psz_meta )
447 playlist_AskForArtEnqueue(
448 p_md->p_libvlc_instance->p_libvlc_int->p_playlist,
449 p_md->p_input_item );
452 /* Should be integrated in core */
453 if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
456 return strdup( p_md->p_input_item->psz_name );
462 /**************************************************************************
463 * Getter for state information
464 * Can be error, playing, buffering, NothingSpecial.
465 **************************************************************************/
468 libvlc_media_descriptor_get_state( libvlc_media_descriptor_t *p_md,
469 libvlc_exception_t *p_e )
475 /**************************************************************************
476 * Setter for state information (LibVLC Internal)
477 **************************************************************************/
480 libvlc_media_descriptor_set_state( libvlc_media_descriptor_t *p_md,
481 libvlc_state_t state,
482 libvlc_exception_t *p_e )
485 libvlc_event_t event;
489 /* Construct the event */
490 event.type = libvlc_MediaDescriptorStateChanged;
491 event.u.media_descriptor_state_changed.new_state = state;
494 libvlc_event_send( p_md->p_event_manager, &event );
497 /**************************************************************************
499 **************************************************************************/
500 void libvlc_media_descriptor_add_tag( libvlc_media_descriptor_t *p_md,
502 const libvlc_tag_t tag,
503 libvlc_exception_t *p_e )
507 struct libvlc_tags_storage_t * p_ts;
512 p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
516 p_ts = malloc(sizeof(struct libvlc_tags_storage_t));
517 memset( p_ts, 0, sizeof(struct libvlc_tags_storage_t) );
521 if( !p_ts->ppsz_tags )
522 p_ts->ppsz_tags = malloc(sizeof(char*)*(p_ts->i_count));
524 p_ts->ppsz_tags = realloc(p_ts->ppsz_tags, sizeof(char*)*(p_ts->i_count));
526 p_ts->ppsz_tags[p_ts->i_count-1] = strdup( tag );
530 /**************************************************************************
532 **************************************************************************/
533 void libvlc_media_descriptor_remove_tag( libvlc_media_descriptor_t *p_md,
535 const libvlc_tag_t tag,
536 libvlc_exception_t *p_e )
540 struct libvlc_tags_storage_t * p_ts;
546 p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
551 for( i = 0; i < p_ts->i_count; i++ )
553 if( !strcmp( p_ts->ppsz_tags[i], tag ) )
555 free( p_ts->ppsz_tags[i] );
556 memcpy( p_ts->ppsz_tags + i + 1, p_ts->ppsz_tags + i, (p_ts->i_count - i - 2)*sizeof(char*) );
557 /* Don't dealloc, the memory will be regain if we add a new tag */
564 /**************************************************************************
566 **************************************************************************/
567 int libvlc_media_descriptor_tags_count_for_key( libvlc_media_descriptor_t *p_md,
569 libvlc_exception_t *p_e )
573 struct libvlc_tags_storage_t * p_ts;
578 p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
582 return p_ts->i_count;
585 /**************************************************************************
587 **************************************************************************/
589 libvlc_media_descriptor_tag_at_index_for_key( libvlc_media_descriptor_t *p_md,
592 libvlc_exception_t *p_e )
596 struct libvlc_tags_storage_t * p_ts;
601 p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
606 return strdup( p_ts->ppsz_tags[i] );
609 /**************************************************************************
611 **************************************************************************/
612 libvlc_media_list_t *
613 libvlc_media_descriptor_subitems( libvlc_media_descriptor_t * p_md,
614 libvlc_exception_t * p_e )
618 if( p_md->p_subitems )
619 libvlc_media_list_retain( p_md->p_subitems );
620 return p_md->p_subitems;
623 /**************************************************************************
625 **************************************************************************/
626 libvlc_event_manager_t *
627 libvlc_media_descriptor_event_manager( libvlc_media_descriptor_t * p_md,
628 libvlc_exception_t * p_e )
632 return p_md->p_event_manager;
635 /**************************************************************************
636 * Get duration of media_descriptor object.
637 **************************************************************************/
639 libvlc_media_descriptor_get_duration( libvlc_media_descriptor_t * p_md,
640 libvlc_exception_t * p_e )
644 if( p_md && p_md->p_input_item)
646 return input_item_GetDuration( p_md->p_input_item );
654 /**************************************************************************
655 * Get preparsed status for media_descriptor object.
656 **************************************************************************/
658 libvlc_media_descriptor_is_preparsed( libvlc_media_descriptor_t * p_md,
659 libvlc_exception_t * p_e )
663 if( p_md && p_md->p_input_item)
665 return input_item_IsPreparsed( p_md->p_input_item );
673 /**************************************************************************
674 * Sets media descriptor's user_data. user_data is specialized data
675 * accessed by the host application, VLC.framework uses it as a pointer to
676 * an native object that references a libvlc_media_descriptor_t pointer
677 **************************************************************************/
679 libvlc_media_descriptor_set_user_data( libvlc_media_descriptor_t * p_md,
680 void * p_new_user_data,
681 libvlc_exception_t * p_e )
687 p_md->p_user_data = p_new_user_data;
691 /**************************************************************************
692 * Get media descriptor's user_data. user_data is specialized data
693 * accessed by the host application, VLC.framework uses it as a pointer to
694 * an native object that references a libvlc_media_descriptor_t pointer
695 **************************************************************************/
697 libvlc_media_descriptor_get_user_data( libvlc_media_descriptor_t * p_md,
698 libvlc_exception_t * p_e )
704 return p_md->p_user_data;