Fix typo
[vlc.git] / src / control / media_descriptor.c
1 /*****************************************************************************
2  * media_descriptor.c: Libvlc API media descripor 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 /* For the preparser */
29 #include <vlc_playlist.h>
30
31 #include "libvlc_internal.h"
32
33 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
34 {
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
52 };
53
54 static const libvlc_meta_t vlc_to_libvlc_meta[] =
55 {
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
73 };
74
75 /**************************************************************************
76  * input_item_subitem_added (Private) (vlc event Callback)
77  **************************************************************************/
78 static void input_item_subitem_added( const vlc_event_t *p_event,
79                                        void * user_data )
80 {
81     libvlc_media_descriptor_t * p_md = user_data;
82     libvlc_media_descriptor_t * p_md_child;
83     libvlc_event_t event;
84
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 );
88
89     /* Add this to our media list */
90     if( !p_md->p_subitems )
91         p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );
92     if( p_md->p_subitems )
93     {
94         libvlc_media_list_add_media_descriptor( p_md->p_subitems, p_md_child, NULL );
95     }
96
97     /* Construct the event */
98     event.type = libvlc_MediaDescriptorSubItemAdded;
99     event.u.media_descriptor_subitem_added.new_child = p_md_child;
100
101     /* Send the event */
102     libvlc_event_send( p_md->p_event_manager, &event );
103     libvlc_media_descriptor_release( p_md_child );
104 }
105
106 /**************************************************************************
107  * input_item_meta_changed (Private) (vlc event Callback)
108  **************************************************************************/
109 static void input_item_meta_changed( const vlc_event_t *p_event,
110                                      void * user_data )
111 {
112     libvlc_media_descriptor_t * p_md = user_data;
113     libvlc_event_t event;
114
115     /* Construct the event */
116     event.type = libvlc_MediaDescriptorMetaChanged;
117     event.u.media_descriptor_meta_changed.meta_type =
118         vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
119
120     /* Send the event */
121     libvlc_event_send( p_md->p_event_manager, &event );
122 }
123
124
125 /**************************************************************************
126  * Install event handler (Private)
127  **************************************************************************/
128 static void install_input_item_observer( libvlc_media_descriptor_t *p_md )
129 {
130     vlc_event_attach( &p_md->p_input_item->event_manager,
131                       vlc_InputItemSubItemAdded,
132                       input_item_subitem_added,
133                       p_md );
134     vlc_event_attach( &p_md->p_input_item->event_manager,
135                       vlc_InputItemMetaChanged,
136                       input_item_meta_changed,
137                       p_md );
138 }
139
140 /**************************************************************************
141  * Uninstall event handler (Private)
142  **************************************************************************/
143 static void uninstall_input_item_observer( libvlc_media_descriptor_t *p_md )
144 {
145     vlc_event_detach( &p_md->p_input_item->event_manager,
146                       vlc_InputItemSubItemAdded,
147                       input_item_subitem_added,
148                       p_md );
149     vlc_event_detach( &p_md->p_input_item->event_manager,
150                       vlc_InputItemMetaChanged,
151                       input_item_meta_changed,
152                       p_md );
153 }
154
155 /**************************************************************************
156  * Preparse if not already done (Private)
157  **************************************************************************/
158 static void preparse_if_needed( libvlc_media_descriptor_t *p_md )
159 {
160     /* XXX: need some locking here */
161     if (!p_md->b_preparsed)
162     {
163         playlist_PreparseEnqueue(
164                 p_md->p_libvlc_instance->p_libvlc_int->p_playlist,
165                 p_md->p_input_item );
166         playlist_AskForArtEnqueue(
167                 p_md->p_libvlc_instance->p_libvlc_int->p_playlist,
168                 p_md->p_input_item );
169
170         p_md->b_preparsed = VLC_TRUE;
171     }
172 }
173
174 /**************************************************************************
175  * Create a new media descriptor object from an input_item
176  * (libvlc internal)
177  * That's the generic constructor
178  **************************************************************************/
179 libvlc_media_descriptor_t * libvlc_media_descriptor_new_from_input_item(
180                                    libvlc_instance_t *p_instance,
181                                    input_item_t *p_input_item,
182                                    libvlc_exception_t *p_e )
183 {
184     libvlc_media_descriptor_t * p_md;
185
186     if (!p_input_item)
187     {
188         libvlc_exception_raise( p_e, "No input item given" );
189         return NULL;
190     }
191
192     p_md = malloc( sizeof(libvlc_media_descriptor_t) );
193     p_md->p_libvlc_instance = p_instance;
194     p_md->p_input_item      = p_input_item;
195     p_md->b_preparsed       = VLC_TRUE;
196     p_md->i_refcount        = 1;
197
198     /* A media descriptor can be a playlist. When you open a playlist
199      * It can give a bunch of item to read. */
200     p_md->p_subitems        = NULL;
201
202     vlc_dictionary_init( &p_md->tags, 1 );
203
204     p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance, p_e );
205     libvlc_event_manager_register_event_type( p_md->p_event_manager, 
206         libvlc_MediaDescriptorMetaChanged, p_e );
207     libvlc_event_manager_register_event_type( p_md->p_event_manager, 
208         libvlc_MediaDescriptorSubItemAdded, p_e );
209
210     vlc_gc_incref( p_md->p_input_item );
211
212     install_input_item_observer( p_md );
213
214     return p_md;
215 }
216
217 /**************************************************************************
218  * Create a new media descriptor object
219  **************************************************************************/
220 libvlc_media_descriptor_t * libvlc_media_descriptor_new(
221                                    libvlc_instance_t *p_instance,
222                                    const char * psz_mrl,
223                                    libvlc_exception_t *p_e )
224 {
225     input_item_t * p_input_item;
226     libvlc_media_descriptor_t * p_md;
227
228     p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, NULL );
229
230     if (!p_input_item)
231     {
232         libvlc_exception_raise( p_e, "Can't create md's input_item" );
233         return NULL;
234     }
235
236     p_md = libvlc_media_descriptor_new_from_input_item( p_instance,
237                 p_input_item, p_e );
238
239     return p_md;
240 }
241
242 /**************************************************************************
243  * Delete a media descriptor object
244  **************************************************************************/
245 void libvlc_media_descriptor_release( libvlc_media_descriptor_t *p_md )
246 {
247     int i;
248     if (!p_md)
249         return;
250
251     p_md->i_refcount--;
252
253     if( p_md->i_refcount > 0 )
254         return;
255
256     if( p_md->p_subitems )
257         libvlc_media_list_release( p_md->p_subitems );
258
259     uninstall_input_item_observer( p_md );
260     vlc_gc_decref( p_md->p_input_item );
261
262     char ** all_keys = vlc_dictionary_all_keys( &p_md->tags );
263     for( i = 0; all_keys[i]; i++ )
264     {
265         int j;
266         struct libvlc_tags_storage_t * p_ts = vlc_dictionary_value_for_key( &p_md->tags, all_keys[i] );
267         for( j = 0; j < p_ts->i_count; j++ )
268         {
269             free( p_ts->ppsz_tags[j] );
270             free( p_ts->ppsz_tags );
271         }
272         free( p_ts );
273     }
274     vlc_dictionary_clear( &p_md->tags );
275     free( p_md );
276 }
277
278 /**************************************************************************
279  * Retain a media descriptor object
280  **************************************************************************/
281 void libvlc_media_descriptor_retain( libvlc_media_descriptor_t *p_md )
282 {
283     if (!p_md)
284         return;
285
286     p_md->i_refcount++;
287 }
288
289 /**************************************************************************
290  * Duplicate a media descriptor object
291  **************************************************************************/
292 libvlc_media_descriptor_t *
293 libvlc_media_descriptor_duplicate( libvlc_media_descriptor_t *p_md_orig )
294 {
295     return libvlc_media_descriptor_new_from_input_item(
296         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
297 }
298
299 /**************************************************************************
300  * Retain a media descriptor object
301  **************************************************************************/
302 char *
303 libvlc_media_descriptor_get_mrl( libvlc_media_descriptor_t * p_md,
304                                  libvlc_exception_t * p_e )
305 {
306     (void)p_e;
307     return input_item_GetURI( p_md->p_input_item );
308 }
309
310 /**************************************************************************
311  * Getter for meta information
312  **************************************************************************/
313
314 char * libvlc_media_descriptor_get_meta( libvlc_media_descriptor_t *p_md,
315                                          libvlc_meta_t e_meta,
316                                          libvlc_exception_t *p_e )
317 {
318     char * psz_meta;
319
320     /* XXX: locking */
321
322     preparse_if_needed( p_md );
323
324     psz_meta = input_item_GetMeta( p_md->p_input_item,
325                                    libvlc_to_vlc_meta[e_meta] );
326
327     /* Should be integrated in core */
328     if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
329     {
330         free( psz_meta );
331         return strdup( p_md->p_input_item->psz_name );
332     }
333
334     return psz_meta;
335 }
336
337 /**************************************************************************
338  * Add a tag
339  **************************************************************************/
340 void libvlc_media_descriptor_add_tag( libvlc_media_descriptor_t *p_md,
341                                       const char * key,
342                                       const libvlc_tag_t tag,
343                                       libvlc_exception_t *p_e )
344 {
345     struct libvlc_tags_storage_t * p_ts;
346
347     if( !tag || !key )
348         return;
349     
350     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
351
352     if( !p_ts )
353     {
354         p_ts = malloc(sizeof(struct libvlc_tags_storage_t));
355         memset( p_ts, 0, sizeof(struct libvlc_tags_storage_t) );
356     }
357     p_ts->i_count++;
358
359     if( !p_ts->ppsz_tags )
360         p_ts->ppsz_tags = malloc(sizeof(char*)*(p_ts->i_count));
361     else
362         p_ts->ppsz_tags = realloc(p_ts->ppsz_tags, sizeof(char*)*(p_ts->i_count));
363         
364     p_ts->ppsz_tags[p_ts->i_count-1] = strdup( tag );
365 }
366
367
368 /**************************************************************************
369  * Remove a tag
370  **************************************************************************/
371 void libvlc_media_descriptor_remove_tag( libvlc_media_descriptor_t *p_md,
372                                          const char * key,
373                                          const libvlc_tag_t tag,
374                                          libvlc_exception_t *p_e )
375 {
376     struct libvlc_tags_storage_t * p_ts;
377     int i;
378
379     if( !tag || !key )
380         return;
381     
382     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
383
384     if( !p_ts )
385         return;
386
387     for( i = 0; i < p_ts->i_count; i++ )
388     {
389         if( !strcmp( p_ts->ppsz_tags[i], tag ) )
390         {
391             free( p_ts->ppsz_tags[i] );
392             memcpy( p_ts->ppsz_tags + i + 1, p_ts->ppsz_tags + i, (p_ts->i_count - i - 2)*sizeof(char*) );
393             /* Don't dealloc, the memory will be regain if we add a new tag */
394             p_ts->i_count--;
395             return;
396         }
397     }
398 }
399
400 /**************************************************************************
401  * Get tags count
402  **************************************************************************/
403 int libvlc_media_descriptor_tags_count_for_key( libvlc_media_descriptor_t *p_md,
404                                                  const char * key,
405                                                  libvlc_exception_t *p_e )
406 {
407     struct libvlc_tags_storage_t * p_ts;
408
409     if( !key )
410         return 0;
411     
412     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
413
414     if( !p_ts )
415         return 0;
416     return p_ts->i_count;
417 }
418
419 /**************************************************************************
420  * Get a tag
421  **************************************************************************/
422 libvlc_tag_t
423 libvlc_media_descriptor_tag_at_index_for_key( libvlc_media_descriptor_t *p_md,
424                                               int i,
425                                               const char * key,
426                                               libvlc_exception_t *p_e )
427 {
428     struct libvlc_tags_storage_t * p_ts;
429
430     if( !key )
431         return NULL;
432     
433     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
434
435     if( !p_ts )
436         return NULL;
437     
438     return strdup( p_ts->ppsz_tags[i] );
439 }
440
441 /**************************************************************************
442  * subitems
443  **************************************************************************/
444 libvlc_media_list_t *
445 libvlc_media_descriptor_subitems( libvlc_media_descriptor_t * p_md,
446                                   libvlc_exception_t * p_e )
447 {
448     if( p_md->p_subitems )
449         libvlc_media_list_retain( p_md->p_subitems );
450     return p_md->p_subitems;
451 }
452
453 /**************************************************************************
454  * event_manager
455  **************************************************************************/
456 libvlc_event_manager_t *
457 libvlc_media_descriptor_event_manager( libvlc_media_descriptor_t * p_md,
458                                        libvlc_exception_t * p_e )
459 {
460     return p_md->p_event_manager;
461 }