]> git.sesse.net Git - vlc/blob - src/control/media_descriptor.c
control/media_descriptor.c: Basic unamed tag support which will soon move to core.
[vlc] / src / control / media_descriptor.c
1 /*****************************************************************************
2  * media_descriptor.c: Libvlc API media descriport management
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Pierre d'Herbemont <pdherbemont@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <vlc/libvlc.h>
25 #include <vlc_input.h>
26 #include <vlc_meta.h>
27
28 #include "libvlc_internal.h"
29
30 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
31 {
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
49 };
50
51 static const libvlc_meta_t vlc_to_libvlc_meta[] =
52 {
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
70 };
71
72 struct tag_storage
73 {
74     char ** ppsz_tags;
75     int i_count;
76 };
77
78 /**************************************************************************
79  * input_item_subitem_added (Private) (vlc event Callback)
80  **************************************************************************/
81 static void input_item_subitem_added( const vlc_event_t *p_event,
82                                        void * user_data )
83 {
84     libvlc_media_descriptor_t * p_md = user_data;
85     libvlc_media_descriptor_t * p_md_child;
86     libvlc_event_t event;
87
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 );
91
92     /* Construct the event */
93     event.type = libvlc_MediaDescriptorSubItemAdded;
94     event.u.media_descriptor_subitem_added.new_child = p_md_child;
95
96     /* Send the event */
97     libvlc_event_send( p_md->p_event_manager, &event );
98     libvlc_media_descriptor_release( p_md_child );
99 }
100
101 /**************************************************************************
102  * input_item_meta_changed (Private) (vlc event Callback)
103  **************************************************************************/
104 static void input_item_meta_changed( const vlc_event_t *p_event,
105                                      void * user_data )
106 {
107     libvlc_media_descriptor_t * p_md = user_data;
108     libvlc_event_t event;
109
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];
114
115     /* Send the event */
116     libvlc_event_send( p_md->p_event_manager, &event );
117 }
118
119
120 /**************************************************************************
121  * Install event handler (Private)
122  **************************************************************************/
123 static void install_input_item_observer( libvlc_media_descriptor_t *p_md )
124 {
125     vlc_event_attach( &p_md->p_input_item->event_manager,
126                       vlc_InputItemSubItemAdded,
127                       input_item_subitem_added,
128                       p_md );
129     vlc_event_attach( &p_md->p_input_item->event_manager,
130                       vlc_InputItemMetaChanged,
131                       input_item_meta_changed,
132                       p_md );
133 }
134
135 /**************************************************************************
136  * Uninstall event handler (Private)
137  **************************************************************************/
138 static void uninstall_input_item_observer( libvlc_media_descriptor_t *p_md )
139 {
140     vlc_event_detach( &p_md->p_input_item->event_manager,
141                       vlc_InputItemSubItemAdded,
142                       input_item_subitem_added,
143                       p_md );
144     vlc_event_detach( &p_md->p_input_item->event_manager,
145                       vlc_InputItemMetaChanged,
146                       input_item_meta_changed,
147                       p_md );
148 }
149
150 /**************************************************************************
151  * Preparse if not already done (Private)
152  **************************************************************************/
153 static void preparse_if_needed( libvlc_media_descriptor_t *p_md )
154 {
155     /* XXX: need some locking here */
156     if (!p_md->b_preparsed)
157     {
158         input_Preparse( p_md->p_libvlc_instance->p_libvlc_int,
159                         p_md->p_input_item );
160         p_md->b_preparsed = VLC_TRUE;
161     }
162 }
163
164 /**************************************************************************
165  * Create a new media descriptor object from an input_item
166  * (libvlc internal)
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 )
173 {
174     libvlc_media_descriptor_t * p_md;
175
176     if (!p_input_item)
177     {
178         libvlc_exception_raise( p_e, "No input item given" );
179         return NULL;
180     }
181
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;
187
188     vlc_dictionary_init( &p_md->tags, 1 );
189
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 );
195
196     vlc_gc_incref( p_md->p_input_item );
197
198     install_input_item_observer( p_md );
199
200     return p_md;
201 }
202
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 )
210 {
211     input_item_t * p_input_item;
212     libvlc_media_descriptor_t * p_md;
213
214     p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, NULL );
215
216     if (!p_input_item)
217     {
218         libvlc_exception_raise( p_e, "Can't create md's input_item" );
219         return NULL;
220     }
221
222     p_md = libvlc_media_descriptor_new_from_input_item( p_instance,
223                 p_input_item, p_e );
224
225     return p_md;
226 }
227
228 /**************************************************************************
229  * Delete a media descriptor object
230  **************************************************************************/
231 void libvlc_media_descriptor_release( libvlc_media_descriptor_t *p_md )
232 {
233     int i;
234     if (!p_md)
235         return;
236
237     p_md->i_refcount--;
238
239     if( p_md->i_refcount > 0 )
240         return;
241
242     uninstall_input_item_observer( p_md );
243     vlc_gc_decref( p_md->p_input_item );
244
245     char ** all_keys = vlc_dictionary_all_keys( &p_md->tags );
246     for( i = 0; all_keys[i]; i++ )
247     {
248         int j;
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++ )
251         {
252             free( p_ts->ppsz_tags[j] );
253             free( p_ts->ppsz_tags );
254         }
255         free( p_ts );
256     }
257     vlc_dictionary_clear( &p_md->tags );
258     free( p_md );
259 }
260
261 /**************************************************************************
262  * Retain a media descriptor object
263  **************************************************************************/
264 void libvlc_media_descriptor_retain( libvlc_media_descriptor_t *p_md )
265 {
266     if (!p_md)
267         return;
268
269     p_md->i_refcount++;
270 }
271
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 )
277 {
278     return libvlc_media_descriptor_new_from_input_item(
279         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
280 }
281
282 /**************************************************************************
283  * Retain a media descriptor object
284  **************************************************************************/
285 char *
286 libvlc_media_descriptor_get_mrl( libvlc_media_descriptor_t * p_md,
287                                  libvlc_exception_t * p_e )
288 {
289     (void)p_e;
290     return strdup( p_md->p_input_item->psz_uri );
291 }
292
293 /**************************************************************************
294  * Getter for meta information
295  **************************************************************************/
296
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 )
300 {
301     char * psz_meta;
302
303     /* XXX: locking */
304
305     preparse_if_needed( p_md );
306
307     psz_meta = input_item_GetMeta( p_md->p_input_item,
308                                    libvlc_to_vlc_meta[e_meta] );
309
310     /* Should be integrated in core */
311     if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
312     {
313         free( psz_meta );
314         return strdup( p_md->p_input_item->psz_name );
315     }
316
317     return psz_meta;
318 }
319
320 /**************************************************************************
321  * Add a tag
322  **************************************************************************/
323 void libvlc_media_descriptor_add_tag( libvlc_media_descriptor_t *p_md,
324                                       const char * key,
325                                       const libvlc_tag_t tag,
326                                       libvlc_exception_t *p_e )
327 {
328     struct tag_storage * p_ts;
329
330     if( !tag || !key )
331         return;
332     
333     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
334
335     if( !p_ts )
336     {
337         p_ts = malloc(sizeof(struct tag_storage));
338         memset( p_ts, 0, sizeof(struct tag_storage) );
339     }
340     p_ts->i_count++;
341
342     if( !p_ts->ppsz_tags )
343         p_ts->ppsz_tags = malloc(sizeof(char*)*(p_ts->i_count));
344     else
345         p_ts->ppsz_tags = realloc(p_ts->ppsz_tags, sizeof(char*)*(p_ts->i_count));
346         
347     p_ts->ppsz_tags[p_ts->i_count-1] = strdup( tag );
348 }
349
350
351 /**************************************************************************
352  * Remove a tag
353  **************************************************************************/
354 void libvlc_media_descriptor_remove_tag( libvlc_media_descriptor_t *p_md,
355                                          const char * key,
356                                          const libvlc_tag_t tag,
357                                          libvlc_exception_t *p_e )
358 {
359     struct tag_storage * p_ts;
360     int i;
361
362     if( !tag || !key )
363         return;
364     
365     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
366
367     if( !p_ts )
368         return;
369
370     for( i = 0; i < p_ts->i_count; i++ )
371     {
372         if( !strcmp( p_ts->ppsz_tags[i], tag ) )
373         {
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 */
377             p_ts->i_count--;
378             return;
379         }
380     }
381 }
382
383 /**************************************************************************
384  * Get tags count
385  **************************************************************************/
386 int libvlc_media_descriptor_tags_count_for_key( libvlc_media_descriptor_t *p_md,
387                                                  const char * key,
388                                                  libvlc_exception_t *p_e )
389 {
390     struct tag_storage * p_ts;
391
392     if( !key )
393         return 0;
394     
395     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
396
397     if( !p_ts )
398         return 0;
399     return p_ts->i_count;
400 }
401
402 /**************************************************************************
403  * Get a tag
404  **************************************************************************/
405 libvlc_tag_t
406 libvlc_media_descriptor_tag_at_index_for_key( libvlc_media_descriptor_t *p_md,
407                                               int i,
408                                               const char * key,
409                                               libvlc_exception_t *p_e )
410 {
411     struct tag_storage * p_ts;
412
413     if( !key )
414         return NULL;
415     
416     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
417
418     if( !p_ts )
419         return NULL;
420     
421     return strdup( p_ts->ppsz_tags[i] );
422 }
423