1 /*****************************************************************************
2 * media_descriptor.c: Libvlc API media descriport 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
78 /**************************************************************************
79 * input_item_subitem_added (Private) (vlc event Callback)
80 **************************************************************************/
81 static void input_item_subitem_added( const vlc_event_t *p_event,
84 libvlc_media_descriptor_t * p_md = user_data;
85 libvlc_media_descriptor_t * p_md_child;
88 p_md_child = libvlc_media_descriptor_new_from_input_item(
89 p_md->p_libvlc_instance,
90 p_event->u.input_item_subitem_added.p_new_child, NULL );
92 /* Construct the event */
93 event.type = libvlc_MediaDescriptorSubItemAdded;
94 event.u.media_descriptor_subitem_added.new_child = p_md_child;
97 libvlc_event_send( p_md->p_event_manager, &event );
98 libvlc_media_descriptor_release( p_md_child );
101 /**************************************************************************
102 * input_item_meta_changed (Private) (vlc event Callback)
103 **************************************************************************/
104 static void input_item_meta_changed( const vlc_event_t *p_event,
107 libvlc_media_descriptor_t * p_md = user_data;
108 libvlc_event_t event;
110 /* Construct the event */
111 event.type = libvlc_MediaDescriptorMetaChanged;
112 event.u.media_descriptor_meta_changed.meta_type =
113 vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
116 libvlc_event_send( p_md->p_event_manager, &event );
120 /**************************************************************************
121 * Install event handler (Private)
122 **************************************************************************/
123 static void install_input_item_observer( libvlc_media_descriptor_t *p_md )
125 vlc_event_attach( &p_md->p_input_item->event_manager,
126 vlc_InputItemSubItemAdded,
127 input_item_subitem_added,
129 vlc_event_attach( &p_md->p_input_item->event_manager,
130 vlc_InputItemMetaChanged,
131 input_item_meta_changed,
135 /**************************************************************************
136 * Uninstall event handler (Private)
137 **************************************************************************/
138 static void uninstall_input_item_observer( libvlc_media_descriptor_t *p_md )
140 vlc_event_detach( &p_md->p_input_item->event_manager,
141 vlc_InputItemSubItemAdded,
142 input_item_subitem_added,
144 vlc_event_detach( &p_md->p_input_item->event_manager,
145 vlc_InputItemMetaChanged,
146 input_item_meta_changed,
150 /**************************************************************************
151 * Preparse if not already done (Private)
152 **************************************************************************/
153 static void preparse_if_needed( libvlc_media_descriptor_t *p_md )
155 /* XXX: need some locking here */
156 if (!p_md->b_preparsed)
158 input_Preparse( p_md->p_libvlc_instance->p_libvlc_int,
159 p_md->p_input_item );
160 p_md->b_preparsed = VLC_TRUE;
164 /**************************************************************************
165 * Create a new media descriptor object from an input_item
167 * That's the generic constructor
168 **************************************************************************/
169 libvlc_media_descriptor_t * libvlc_media_descriptor_new_from_input_item(
170 libvlc_instance_t *p_instance,
171 input_item_t *p_input_item,
172 libvlc_exception_t *p_e )
174 libvlc_media_descriptor_t * p_md;
178 libvlc_exception_raise( p_e, "No input item given" );
182 p_md = malloc( sizeof(libvlc_media_descriptor_t) );
183 p_md->p_libvlc_instance = p_instance;
184 p_md->p_input_item = p_input_item;
185 p_md->b_preparsed = VLC_TRUE;
186 p_md->i_refcount = 1;
188 vlc_dictionary_init( &p_md->tags, 1 );
190 p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance, p_e );
191 libvlc_event_manager_register_event_type( p_md->p_event_manager,
192 libvlc_MediaDescriptorMetaChanged, p_e );
193 libvlc_event_manager_register_event_type( p_md->p_event_manager,
194 libvlc_MediaDescriptorSubItemAdded, p_e );
196 vlc_gc_incref( p_md->p_input_item );
198 install_input_item_observer( p_md );
203 /**************************************************************************
204 * Create a new media descriptor object
205 **************************************************************************/
206 libvlc_media_descriptor_t * libvlc_media_descriptor_new(
207 libvlc_instance_t *p_instance,
208 const char * psz_mrl,
209 libvlc_exception_t *p_e )
211 input_item_t * p_input_item;
212 libvlc_media_descriptor_t * p_md;
214 p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, NULL );
218 libvlc_exception_raise( p_e, "Can't create md's input_item" );
222 p_md = libvlc_media_descriptor_new_from_input_item( p_instance,
228 /**************************************************************************
229 * Delete a media descriptor object
230 **************************************************************************/
231 void libvlc_media_descriptor_release( libvlc_media_descriptor_t *p_md )
239 if( p_md->i_refcount > 0 )
242 uninstall_input_item_observer( p_md );
243 vlc_gc_decref( p_md->p_input_item );
245 char ** all_keys = vlc_dictionary_all_keys( &p_md->tags );
246 for( i = 0; all_keys[i]; i++ )
249 struct tag_storage * p_ts = vlc_dictionary_value_for_key( &p_md->tags, all_keys[i] );
250 for( j = 0; j < p_ts->i_count; j++ )
252 free( p_ts->ppsz_tags[j] );
253 free( p_ts->ppsz_tags );
257 vlc_dictionary_clear( &p_md->tags );
261 /**************************************************************************
262 * Retain a media descriptor object
263 **************************************************************************/
264 void libvlc_media_descriptor_retain( libvlc_media_descriptor_t *p_md )
272 /**************************************************************************
273 * Duplicate a media descriptor object
274 **************************************************************************/
275 libvlc_media_descriptor_t *
276 libvlc_media_descriptor_duplicate( libvlc_media_descriptor_t *p_md_orig )
278 return libvlc_media_descriptor_new_from_input_item(
279 p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
282 /**************************************************************************
283 * Retain a media descriptor object
284 **************************************************************************/
286 libvlc_media_descriptor_get_mrl( libvlc_media_descriptor_t * p_md,
287 libvlc_exception_t * p_e )
290 return strdup( p_md->p_input_item->psz_uri );
293 /**************************************************************************
294 * Getter for meta information
295 **************************************************************************/
297 char * libvlc_media_descriptor_get_meta( libvlc_media_descriptor_t *p_md,
298 libvlc_meta_t e_meta,
299 libvlc_exception_t *p_e )
305 preparse_if_needed( p_md );
307 psz_meta = input_item_GetMeta( p_md->p_input_item,
308 libvlc_to_vlc_meta[e_meta] );
310 /* Should be integrated in core */
311 if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
314 return strdup( p_md->p_input_item->psz_name );
320 /**************************************************************************
322 **************************************************************************/
323 void libvlc_media_descriptor_add_tag( libvlc_media_descriptor_t *p_md,
325 const libvlc_tag_t tag,
326 libvlc_exception_t *p_e )
328 struct tag_storage * p_ts;
333 p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
337 p_ts = malloc(sizeof(struct tag_storage));
338 memset( p_ts, 0, sizeof(struct tag_storage) );
342 if( !p_ts->ppsz_tags )
343 p_ts->ppsz_tags = malloc(sizeof(char*)*(p_ts->i_count));
345 p_ts->ppsz_tags = realloc(p_ts->ppsz_tags, sizeof(char*)*(p_ts->i_count));
347 p_ts->ppsz_tags[p_ts->i_count-1] = strdup( tag );
351 /**************************************************************************
353 **************************************************************************/
354 void libvlc_media_descriptor_remove_tag( libvlc_media_descriptor_t *p_md,
356 const libvlc_tag_t tag,
357 libvlc_exception_t *p_e )
359 struct tag_storage * p_ts;
365 p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
370 for( i = 0; i < p_ts->i_count; i++ )
372 if( !strcmp( p_ts->ppsz_tags[i], tag ) )
374 free( p_ts->ppsz_tags[i] );
375 memcpy( p_ts->ppsz_tags + i + 1, p_ts->ppsz_tags + i, (p_ts->i_count - i - 2)*sizeof(char*) );
376 /* Don't dealloc, the memory will be regain if we add a new tag */
383 /**************************************************************************
385 **************************************************************************/
386 int libvlc_media_descriptor_tags_count_for_key( libvlc_media_descriptor_t *p_md,
388 libvlc_exception_t *p_e )
390 struct tag_storage * p_ts;
395 p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
399 return p_ts->i_count;
402 /**************************************************************************
404 **************************************************************************/
406 libvlc_media_descriptor_tag_at_index_for_key( libvlc_media_descriptor_t *p_md,
409 libvlc_exception_t *p_e )
411 struct tag_storage * p_ts;
416 p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
421 return strdup( p_ts->ppsz_tags[i] );