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 <vlc/libvlc.h>
25 #include <vlc_input.h>
28 #include "libvlc_internal.h"
30 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
32 [libvlc_meta_Title] = vlc_meta_Title,
33 [libvlc_meta_Artist] = vlc_meta_Artist,
34 [libvlc_meta_Genre] = vlc_meta_Genre,
35 [libvlc_meta_Copyright] = vlc_meta_Copyright,
36 [libvlc_meta_Album] = vlc_meta_Album,
37 [libvlc_meta_TrackNumber] = vlc_meta_TrackNumber,
38 [libvlc_meta_Description] = vlc_meta_Description,
39 [libvlc_meta_Rating] = vlc_meta_Rating,
40 [libvlc_meta_Date] = vlc_meta_Date,
41 [libvlc_meta_Setting] = vlc_meta_Setting,
42 [libvlc_meta_URL] = vlc_meta_URL,
43 [libvlc_meta_Language] = vlc_meta_Language,
44 [libvlc_meta_NowPlaying] = vlc_meta_NowPlaying,
45 [libvlc_meta_Publisher] = vlc_meta_Publisher,
46 [libvlc_meta_EncodedBy] = vlc_meta_EncodedBy,
47 [libvlc_meta_ArtworkURL] = vlc_meta_ArtworkURL,
48 [libvlc_meta_TrackID] = vlc_meta_TrackID
51 static const libvlc_meta_t vlc_to_libvlc_meta[] =
53 [vlc_meta_Title] = libvlc_meta_Title,
54 [vlc_meta_Artist] = libvlc_meta_Artist,
55 [vlc_meta_Genre] = libvlc_meta_Genre,
56 [vlc_meta_Copyright] = libvlc_meta_Copyright,
57 [vlc_meta_Album] = libvlc_meta_Album,
58 [vlc_meta_TrackNumber] = libvlc_meta_TrackNumber,
59 [vlc_meta_Description] = libvlc_meta_Description,
60 [vlc_meta_Rating] = libvlc_meta_Rating,
61 [vlc_meta_Date] = libvlc_meta_Date,
62 [vlc_meta_Setting] = libvlc_meta_Setting,
63 [vlc_meta_URL] = libvlc_meta_URL,
64 [vlc_meta_Language] = libvlc_meta_Language,
65 [vlc_meta_NowPlaying] = libvlc_meta_NowPlaying,
66 [vlc_meta_Publisher] = libvlc_meta_Publisher,
67 [vlc_meta_EncodedBy] = libvlc_meta_EncodedBy,
68 [vlc_meta_ArtworkURL] = libvlc_meta_ArtworkURL,
69 [vlc_meta_TrackID] = libvlc_meta_TrackID
72 /**************************************************************************
73 * input_item_subitem_added (Private) (vlc event Callback)
74 **************************************************************************/
75 static void input_item_subitem_added( const vlc_event_t *p_event,
78 libvlc_media_descriptor_t * p_md = user_data;
79 libvlc_media_descriptor_t * p_md_child;
82 p_md_child = libvlc_media_descriptor_new_from_input_item(
83 p_md->p_libvlc_instance,
84 p_event->u.input_item_subitem_added.p_new_child, NULL );
86 /* Add this to our media list */
87 if( !p_md->p_subitems )
88 p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );
89 if( !p_md->p_subitems )
91 libvlc_media_list_add_media_descriptor( p_md->p_subitems, p_md_child, NULL );
94 /* Construct the event */
95 event.type = libvlc_MediaDescriptorSubItemAdded;
96 event.u.media_descriptor_subitem_added.new_child = p_md_child;
99 libvlc_event_send( p_md->p_event_manager, &event );
100 libvlc_media_descriptor_release( p_md_child );
103 /**************************************************************************
104 * input_item_meta_changed (Private) (vlc event Callback)
105 **************************************************************************/
106 static void input_item_meta_changed( const vlc_event_t *p_event,
109 libvlc_media_descriptor_t * p_md = user_data;
110 libvlc_event_t event;
112 /* Construct the event */
113 event.type = libvlc_MediaDescriptorMetaChanged;
114 event.u.media_descriptor_meta_changed.meta_type =
115 vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
118 libvlc_event_send( p_md->p_event_manager, &event );
122 /**************************************************************************
123 * Install event handler (Private)
124 **************************************************************************/
125 static void install_input_item_observer( libvlc_media_descriptor_t *p_md )
127 vlc_event_attach( &p_md->p_input_item->event_manager,
128 vlc_InputItemSubItemAdded,
129 input_item_subitem_added,
131 vlc_event_attach( &p_md->p_input_item->event_manager,
132 vlc_InputItemMetaChanged,
133 input_item_meta_changed,
137 /**************************************************************************
138 * Uninstall event handler (Private)
139 **************************************************************************/
140 static void uninstall_input_item_observer( libvlc_media_descriptor_t *p_md )
142 vlc_event_detach( &p_md->p_input_item->event_manager,
143 vlc_InputItemSubItemAdded,
144 input_item_subitem_added,
146 vlc_event_detach( &p_md->p_input_item->event_manager,
147 vlc_InputItemMetaChanged,
148 input_item_meta_changed,
152 /**************************************************************************
153 * Preparse if not already done (Private)
154 **************************************************************************/
155 static void preparse_if_needed( libvlc_media_descriptor_t *p_md )
157 /* XXX: need some locking here */
158 if (!p_md->b_preparsed)
160 playlist_PreparseEnqueue(
161 p_md->p_libvlc_instance->p_libvlc_int->p_playlist,
162 p_md->p_input_item );
163 playlist_AskForArtEnqueue(
164 p_md->p_libvlc_instance->p_libvlc_int->p_playlist,
165 p_md->p_input_item );
167 p_md->b_preparsed = VLC_TRUE;
171 /**************************************************************************
172 * Create a new media descriptor object from an input_item
174 * That's the generic constructor
175 **************************************************************************/
176 libvlc_media_descriptor_t * libvlc_media_descriptor_new_from_input_item(
177 libvlc_instance_t *p_instance,
178 input_item_t *p_input_item,
179 libvlc_exception_t *p_e )
181 libvlc_media_descriptor_t * p_md;
185 libvlc_exception_raise( p_e, "No input item given" );
189 p_md = malloc( sizeof(libvlc_media_descriptor_t) );
190 p_md->p_libvlc_instance = p_instance;
191 p_md->p_input_item = p_input_item;
192 p_md->b_preparsed = VLC_TRUE;
193 p_md->i_refcount = 1;
195 /* A media descriptor can be a playlist. When you open a playlist
196 * It can give a bunch of item to read. */
197 p_md->p_subitems = NULL;
199 vlc_dictionary_init( &p_md->tags, 1 );
201 p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance, p_e );
202 libvlc_event_manager_register_event_type( p_md->p_event_manager,
203 libvlc_MediaDescriptorMetaChanged, p_e );
204 libvlc_event_manager_register_event_type( p_md->p_event_manager,
205 libvlc_MediaDescriptorSubItemAdded, p_e );
207 vlc_gc_incref( p_md->p_input_item );
209 install_input_item_observer( p_md );
214 /**************************************************************************
215 * Create a new media descriptor object
216 **************************************************************************/
217 libvlc_media_descriptor_t * libvlc_media_descriptor_new(
218 libvlc_instance_t *p_instance,
219 const char * psz_mrl,
220 libvlc_exception_t *p_e )
222 input_item_t * p_input_item;
223 libvlc_media_descriptor_t * p_md;
225 p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, NULL );
229 libvlc_exception_raise( p_e, "Can't create md's input_item" );
233 p_md = libvlc_media_descriptor_new_from_input_item( p_instance,
239 /**************************************************************************
240 * Delete a media descriptor object
241 **************************************************************************/
242 void libvlc_media_descriptor_release( libvlc_media_descriptor_t *p_md )
250 if( p_md->i_refcount > 0 )
253 libvlc_media_list_release( p_md->p_subitems );
255 uninstall_input_item_observer( p_md );
256 vlc_gc_decref( p_md->p_input_item );
258 char ** all_keys = vlc_dictionary_all_keys( &p_md->tags );
259 for( i = 0; all_keys[i]; i++ )
262 struct libvlc_tags_storage_t * p_ts = vlc_dictionary_value_for_key( &p_md->tags, all_keys[i] );
263 for( j = 0; j < p_ts->i_count; j++ )
265 free( p_ts->ppsz_tags[j] );
266 free( p_ts->ppsz_tags );
270 vlc_dictionary_clear( &p_md->tags );
274 /**************************************************************************
275 * Retain a media descriptor object
276 **************************************************************************/
277 void libvlc_media_descriptor_retain( libvlc_media_descriptor_t *p_md )
285 /**************************************************************************
286 * Duplicate a media descriptor object
287 **************************************************************************/
288 libvlc_media_descriptor_t *
289 libvlc_media_descriptor_duplicate( libvlc_media_descriptor_t *p_md_orig )
291 return libvlc_media_descriptor_new_from_input_item(
292 p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
295 /**************************************************************************
296 * Retain a media descriptor object
297 **************************************************************************/
299 libvlc_media_descriptor_get_mrl( libvlc_media_descriptor_t * p_md,
300 libvlc_exception_t * p_e )
303 return strdup( p_md->p_input_item->psz_uri );
306 /**************************************************************************
307 * Getter for meta information
308 **************************************************************************/
310 char * libvlc_media_descriptor_get_meta( libvlc_media_descriptor_t *p_md,
311 libvlc_meta_t e_meta,
312 libvlc_exception_t *p_e )
318 preparse_if_needed( p_md );
320 psz_meta = input_item_GetMeta( p_md->p_input_item,
321 libvlc_to_vlc_meta[e_meta] );
323 /* Should be integrated in core */
324 if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
327 return strdup( p_md->p_input_item->psz_name );
333 /**************************************************************************
335 **************************************************************************/
336 void libvlc_media_descriptor_add_tag( libvlc_media_descriptor_t *p_md,
338 const libvlc_tag_t tag,
339 libvlc_exception_t *p_e )
341 struct libvlc_tags_storage_t * p_ts;
346 p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
350 p_ts = malloc(sizeof(struct libvlc_tags_storage_t));
351 memset( p_ts, 0, sizeof(struct libvlc_tags_storage_t) );
355 if( !p_ts->ppsz_tags )
356 p_ts->ppsz_tags = malloc(sizeof(char*)*(p_ts->i_count));
358 p_ts->ppsz_tags = realloc(p_ts->ppsz_tags, sizeof(char*)*(p_ts->i_count));
360 p_ts->ppsz_tags[p_ts->i_count-1] = strdup( tag );
364 /**************************************************************************
366 **************************************************************************/
367 void libvlc_media_descriptor_remove_tag( libvlc_media_descriptor_t *p_md,
369 const libvlc_tag_t tag,
370 libvlc_exception_t *p_e )
372 struct libvlc_tags_storage_t * p_ts;
378 p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
383 for( i = 0; i < p_ts->i_count; i++ )
385 if( !strcmp( p_ts->ppsz_tags[i], tag ) )
387 free( p_ts->ppsz_tags[i] );
388 memcpy( p_ts->ppsz_tags + i + 1, p_ts->ppsz_tags + i, (p_ts->i_count - i - 2)*sizeof(char*) );
389 /* Don't dealloc, the memory will be regain if we add a new tag */
396 /**************************************************************************
398 **************************************************************************/
399 int libvlc_media_descriptor_tags_count_for_key( libvlc_media_descriptor_t *p_md,
401 libvlc_exception_t *p_e )
403 struct libvlc_tags_storage_t * p_ts;
408 p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
412 return p_ts->i_count;
415 /**************************************************************************
417 **************************************************************************/
419 libvlc_media_descriptor_tag_at_index_for_key( libvlc_media_descriptor_t *p_md,
422 libvlc_exception_t *p_e )
424 struct libvlc_tags_storage_t * p_ts;
429 p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
434 return strdup( p_ts->ppsz_tags[i] );