control/media_descriptor.c: Use playlist's fetcher to preparse the item.
[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 #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 /**************************************************************************
73  * input_item_subitem_added (Private) (vlc event Callback)
74  **************************************************************************/
75 static void input_item_subitem_added( const vlc_event_t *p_event,
76                                        void * user_data )
77 {
78     libvlc_media_descriptor_t * p_md = user_data;
79     libvlc_media_descriptor_t * p_md_child;
80     libvlc_event_t event;
81
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 );
85
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 )
90     {
91         libvlc_media_list_add_media_descriptor( p_md->p_subitems, p_md_child, NULL );
92     }
93
94     /* Construct the event */
95     event.type = libvlc_MediaDescriptorSubItemAdded;
96     event.u.media_descriptor_subitem_added.new_child = p_md_child;
97
98     /* Send the event */
99     libvlc_event_send( p_md->p_event_manager, &event );
100     libvlc_media_descriptor_release( p_md_child );
101 }
102
103 /**************************************************************************
104  * input_item_meta_changed (Private) (vlc event Callback)
105  **************************************************************************/
106 static void input_item_meta_changed( const vlc_event_t *p_event,
107                                      void * user_data )
108 {
109     libvlc_media_descriptor_t * p_md = user_data;
110     libvlc_event_t event;
111
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];
116
117     /* Send the event */
118     libvlc_event_send( p_md->p_event_manager, &event );
119 }
120
121
122 /**************************************************************************
123  * Install event handler (Private)
124  **************************************************************************/
125 static void install_input_item_observer( libvlc_media_descriptor_t *p_md )
126 {
127     vlc_event_attach( &p_md->p_input_item->event_manager,
128                       vlc_InputItemSubItemAdded,
129                       input_item_subitem_added,
130                       p_md );
131     vlc_event_attach( &p_md->p_input_item->event_manager,
132                       vlc_InputItemMetaChanged,
133                       input_item_meta_changed,
134                       p_md );
135 }
136
137 /**************************************************************************
138  * Uninstall event handler (Private)
139  **************************************************************************/
140 static void uninstall_input_item_observer( libvlc_media_descriptor_t *p_md )
141 {
142     vlc_event_detach( &p_md->p_input_item->event_manager,
143                       vlc_InputItemSubItemAdded,
144                       input_item_subitem_added,
145                       p_md );
146     vlc_event_detach( &p_md->p_input_item->event_manager,
147                       vlc_InputItemMetaChanged,
148                       input_item_meta_changed,
149                       p_md );
150 }
151
152 /**************************************************************************
153  * Preparse if not already done (Private)
154  **************************************************************************/
155 static void preparse_if_needed( libvlc_media_descriptor_t *p_md )
156 {
157     /* XXX: need some locking here */
158     if (!p_md->b_preparsed)
159     {
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 );
166
167         p_md->b_preparsed = VLC_TRUE;
168     }
169 }
170
171 /**************************************************************************
172  * Create a new media descriptor object from an input_item
173  * (libvlc internal)
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 )
180 {
181     libvlc_media_descriptor_t * p_md;
182
183     if (!p_input_item)
184     {
185         libvlc_exception_raise( p_e, "No input item given" );
186         return NULL;
187     }
188
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;
194
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;
198
199     vlc_dictionary_init( &p_md->tags, 1 );
200
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 );
206
207     vlc_gc_incref( p_md->p_input_item );
208
209     install_input_item_observer( p_md );
210
211     return p_md;
212 }
213
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 )
221 {
222     input_item_t * p_input_item;
223     libvlc_media_descriptor_t * p_md;
224
225     p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, NULL );
226
227     if (!p_input_item)
228     {
229         libvlc_exception_raise( p_e, "Can't create md's input_item" );
230         return NULL;
231     }
232
233     p_md = libvlc_media_descriptor_new_from_input_item( p_instance,
234                 p_input_item, p_e );
235
236     return p_md;
237 }
238
239 /**************************************************************************
240  * Delete a media descriptor object
241  **************************************************************************/
242 void libvlc_media_descriptor_release( libvlc_media_descriptor_t *p_md )
243 {
244     int i;
245     if (!p_md)
246         return;
247
248     p_md->i_refcount--;
249
250     if( p_md->i_refcount > 0 )
251         return;
252
253     libvlc_media_list_release( p_md->p_subitems );
254
255     uninstall_input_item_observer( p_md );
256     vlc_gc_decref( p_md->p_input_item );
257
258     char ** all_keys = vlc_dictionary_all_keys( &p_md->tags );
259     for( i = 0; all_keys[i]; i++ )
260     {
261         int j;
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++ )
264         {
265             free( p_ts->ppsz_tags[j] );
266             free( p_ts->ppsz_tags );
267         }
268         free( p_ts );
269     }
270     vlc_dictionary_clear( &p_md->tags );
271     free( p_md );
272 }
273
274 /**************************************************************************
275  * Retain a media descriptor object
276  **************************************************************************/
277 void libvlc_media_descriptor_retain( libvlc_media_descriptor_t *p_md )
278 {
279     if (!p_md)
280         return;
281
282     p_md->i_refcount++;
283 }
284
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 )
290 {
291     return libvlc_media_descriptor_new_from_input_item(
292         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
293 }
294
295 /**************************************************************************
296  * Retain a media descriptor object
297  **************************************************************************/
298 char *
299 libvlc_media_descriptor_get_mrl( libvlc_media_descriptor_t * p_md,
300                                  libvlc_exception_t * p_e )
301 {
302     (void)p_e;
303     return strdup( p_md->p_input_item->psz_uri );
304 }
305
306 /**************************************************************************
307  * Getter for meta information
308  **************************************************************************/
309
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 )
313 {
314     char * psz_meta;
315
316     /* XXX: locking */
317
318     preparse_if_needed( p_md );
319
320     psz_meta = input_item_GetMeta( p_md->p_input_item,
321                                    libvlc_to_vlc_meta[e_meta] );
322
323     /* Should be integrated in core */
324     if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
325     {
326         free( psz_meta );
327         return strdup( p_md->p_input_item->psz_name );
328     }
329
330     return psz_meta;
331 }
332
333 /**************************************************************************
334  * Add a tag
335  **************************************************************************/
336 void libvlc_media_descriptor_add_tag( libvlc_media_descriptor_t *p_md,
337                                       const char * key,
338                                       const libvlc_tag_t tag,
339                                       libvlc_exception_t *p_e )
340 {
341     struct libvlc_tags_storage_t * p_ts;
342
343     if( !tag || !key )
344         return;
345     
346     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
347
348     if( !p_ts )
349     {
350         p_ts = malloc(sizeof(struct libvlc_tags_storage_t));
351         memset( p_ts, 0, sizeof(struct libvlc_tags_storage_t) );
352     }
353     p_ts->i_count++;
354
355     if( !p_ts->ppsz_tags )
356         p_ts->ppsz_tags = malloc(sizeof(char*)*(p_ts->i_count));
357     else
358         p_ts->ppsz_tags = realloc(p_ts->ppsz_tags, sizeof(char*)*(p_ts->i_count));
359         
360     p_ts->ppsz_tags[p_ts->i_count-1] = strdup( tag );
361 }
362
363
364 /**************************************************************************
365  * Remove a tag
366  **************************************************************************/
367 void libvlc_media_descriptor_remove_tag( libvlc_media_descriptor_t *p_md,
368                                          const char * key,
369                                          const libvlc_tag_t tag,
370                                          libvlc_exception_t *p_e )
371 {
372     struct libvlc_tags_storage_t * p_ts;
373     int i;
374
375     if( !tag || !key )
376         return;
377     
378     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
379
380     if( !p_ts )
381         return;
382
383     for( i = 0; i < p_ts->i_count; i++ )
384     {
385         if( !strcmp( p_ts->ppsz_tags[i], tag ) )
386         {
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 */
390             p_ts->i_count--;
391             return;
392         }
393     }
394 }
395
396 /**************************************************************************
397  * Get tags count
398  **************************************************************************/
399 int libvlc_media_descriptor_tags_count_for_key( libvlc_media_descriptor_t *p_md,
400                                                  const char * key,
401                                                  libvlc_exception_t *p_e )
402 {
403     struct libvlc_tags_storage_t * p_ts;
404
405     if( !key )
406         return 0;
407     
408     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
409
410     if( !p_ts )
411         return 0;
412     return p_ts->i_count;
413 }
414
415 /**************************************************************************
416  * Get a tag
417  **************************************************************************/
418 libvlc_tag_t
419 libvlc_media_descriptor_tag_at_index_for_key( libvlc_media_descriptor_t *p_md,
420                                               int i,
421                                               const char * key,
422                                               libvlc_exception_t *p_e )
423 {
424     struct libvlc_tags_storage_t * p_ts;
425
426     if( !key )
427         return NULL;
428     
429     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
430
431     if( !p_ts )
432         return NULL;
433     
434     return strdup( p_ts->ppsz_tags[i] );
435 }
436