]> git.sesse.net Git - vlc/blob - src/control/media_descriptor.c
6c7ecb9d4201e31348c6fe1786aad79cc0ab444e
[vlc] / 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     {
92         p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );
93         libvlc_media_list_set_media_descriptor( p_md->p_subitems, p_md, NULL );
94     }
95     if( p_md->p_subitems )
96     {
97         libvlc_media_list_add_media_descriptor( p_md->p_subitems, p_md_child, NULL );
98     }
99
100     /* Construct the event */
101     event.type = libvlc_MediaDescriptorSubItemAdded;
102     event.u.media_descriptor_subitem_added.new_child = p_md_child;
103
104     /* Send the event */
105     libvlc_event_send( p_md->p_event_manager, &event );
106     libvlc_media_descriptor_release( p_md_child );
107 }
108
109 /**************************************************************************
110  * input_item_meta_changed (Private) (vlc event Callback)
111  **************************************************************************/
112 static void input_item_meta_changed( const vlc_event_t *p_event,
113                                      void * user_data )
114 {
115     libvlc_media_descriptor_t * p_md = user_data;
116     libvlc_event_t event;
117
118     /* Construct the event */
119     event.type = libvlc_MediaDescriptorMetaChanged;
120     event.u.media_descriptor_meta_changed.meta_type =
121         vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
122
123     /* Send the event */
124     libvlc_event_send( p_md->p_event_manager, &event );
125 }
126
127 /**************************************************************************
128  * input_item_duration_changed (Private) (vlc event Callback)
129  **************************************************************************/
130 static void input_item_duration_changed( const vlc_event_t *p_event,
131                                          void * user_data )
132 {
133     libvlc_media_descriptor_t * p_md = user_data;
134     libvlc_event_t event;
135
136     /* Construct the event */
137     event.type = libvlc_MediaDescriptorDurationChanged;
138     event.u.media_descriptor_duration_changed.new_duration = 
139         p_event->u.input_item_duration_changed.new_duration;
140
141     /* Send the event */
142     libvlc_event_send( p_md->p_event_manager, &event );
143 }
144
145 /**************************************************************************
146  * input_item_preparsed_changed (Private) (vlc event Callback)
147  **************************************************************************/
148 static void input_item_preparsed_changed( const vlc_event_t *p_event,
149                                           void * user_data )
150 {
151     libvlc_media_descriptor_t * p_md = user_data;
152     libvlc_event_t event;
153
154     /* Construct the event */
155     event.type = libvlc_MediaDescriptorPreparsedChanged;
156     event.u.media_descriptor_preparsed_changed.new_status = 
157         p_event->u.input_item_preparsed_changed.new_status;
158
159     /* Send the event */
160     libvlc_event_send( p_md->p_event_manager, &event );
161 }
162
163 /**************************************************************************
164  * Install event handler (Private)
165  **************************************************************************/
166 static void install_input_item_observer( libvlc_media_descriptor_t *p_md )
167 {
168     vlc_event_attach( &p_md->p_input_item->event_manager,
169                       vlc_InputItemSubItemAdded,
170                       input_item_subitem_added,
171                       p_md );
172     vlc_event_attach( &p_md->p_input_item->event_manager,
173                       vlc_InputItemMetaChanged,
174                       input_item_meta_changed,
175                       p_md );
176     vlc_event_attach( &p_md->p_input_item->event_manager,
177                       vlc_InputItemDurationChanged,
178                       input_item_duration_changed,
179                       p_md );
180     vlc_event_attach( &p_md->p_input_item->event_manager,
181                       vlc_InputItemPreparsedChanged,
182                       input_item_preparsed_changed,
183                       p_md );
184 }
185
186 /**************************************************************************
187  * Uninstall event handler (Private)
188  **************************************************************************/
189 static void uninstall_input_item_observer( libvlc_media_descriptor_t *p_md )
190 {
191     vlc_event_detach( &p_md->p_input_item->event_manager,
192                       vlc_InputItemSubItemAdded,
193                       input_item_subitem_added,
194                       p_md );
195     vlc_event_detach( &p_md->p_input_item->event_manager,
196                       vlc_InputItemMetaChanged,
197                       input_item_meta_changed,
198                       p_md );
199     vlc_event_detach( &p_md->p_input_item->event_manager,
200                       vlc_InputItemDurationChanged,
201                       input_item_duration_changed,
202                       p_md );
203     vlc_event_detach( &p_md->p_input_item->event_manager,
204                       vlc_InputItemPreparsedChanged,
205                       input_item_preparsed_changed,
206                       p_md );
207 }
208
209 /**************************************************************************
210  * Preparse if not already done (Private)
211  **************************************************************************/
212 static void preparse_if_needed( libvlc_media_descriptor_t *p_md )
213 {
214     /* XXX: need some locking here */
215     if (!p_md->b_preparsed)
216     {
217         playlist_PreparseEnqueue(
218                 p_md->p_libvlc_instance->p_libvlc_int->p_playlist,
219                 p_md->p_input_item );
220         playlist_AskForArtEnqueue(
221                 p_md->p_libvlc_instance->p_libvlc_int->p_playlist,
222                 p_md->p_input_item );
223
224         p_md->b_preparsed = VLC_TRUE;
225     }
226 }
227
228 /**************************************************************************
229  * Create a new media descriptor object from an input_item
230  * (libvlc internal)
231  * That's the generic constructor
232  **************************************************************************/
233 libvlc_media_descriptor_t * libvlc_media_descriptor_new_from_input_item(
234                                    libvlc_instance_t *p_instance,
235                                    input_item_t *p_input_item,
236                                    libvlc_exception_t *p_e )
237 {
238     libvlc_media_descriptor_t * p_md;
239
240     if (!p_input_item)
241     {
242         libvlc_exception_raise( p_e, "No input item given" );
243         return NULL;
244     }
245
246     p_md = malloc( sizeof(libvlc_media_descriptor_t) );
247     p_md->p_libvlc_instance = p_instance;
248     p_md->p_input_item      = p_input_item;
249     p_md->b_preparsed       = VLC_FALSE;
250     p_md->i_refcount        = 1;
251     p_md->p_user_data       = NULL; // VLC.framework hook
252
253     /* A media descriptor can be a playlist. When you open a playlist
254      * It can give a bunch of item to read. */
255     p_md->p_subitems        = NULL;
256
257     vlc_dictionary_init( &p_md->tags, 1 );
258
259     p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance, p_e );
260     libvlc_event_manager_register_event_type( p_md->p_event_manager,
261         libvlc_MediaDescriptorMetaChanged, p_e );
262     libvlc_event_manager_register_event_type( p_md->p_event_manager,
263         libvlc_MediaDescriptorSubItemAdded, p_e );
264     libvlc_event_manager_register_event_type( p_md->p_event_manager,
265         libvlc_MediaDescriptorFreed, p_e );
266     libvlc_event_manager_register_event_type( p_md->p_event_manager,
267         libvlc_MediaDescriptorDurationChanged, p_e );
268
269     vlc_gc_incref( p_md->p_input_item );
270
271     install_input_item_observer( p_md );
272
273     return p_md;
274 }
275
276 /**************************************************************************
277  * Create a new media descriptor object
278  **************************************************************************/
279 libvlc_media_descriptor_t * libvlc_media_descriptor_new(
280                                    libvlc_instance_t *p_instance,
281                                    const char * psz_mrl,
282                                    libvlc_exception_t *p_e )
283 {
284     input_item_t * p_input_item;
285     libvlc_media_descriptor_t * p_md;
286
287     p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, NULL );
288
289     if (!p_input_item)
290     {
291         libvlc_exception_raise( p_e, "Can't create md's input_item" );
292         return NULL;
293     }
294
295     p_md = libvlc_media_descriptor_new_from_input_item( p_instance,
296                 p_input_item, p_e );
297
298     return p_md;
299 }
300
301 /**************************************************************************
302  * Delete a media descriptor object
303  **************************************************************************/
304 void libvlc_media_descriptor_release( libvlc_media_descriptor_t *p_md )
305 {
306     int i;
307     if (!p_md)
308         return;
309
310     p_md->i_refcount--;
311
312     if( p_md->i_refcount > 0 )
313         return;
314
315     if( p_md->p_subitems )
316         libvlc_media_list_release( p_md->p_subitems );
317
318     uninstall_input_item_observer( p_md );
319     vlc_gc_decref( p_md->p_input_item );
320
321     /* Construct the event */
322     libvlc_event_t event;
323     event.type = libvlc_MediaDescriptorFreed;
324     event.u.media_descriptor_freed.md = p_md;
325
326     /* Send the event */
327     libvlc_event_send( p_md->p_event_manager, &event );
328
329     libvlc_event_manager_release( p_md->p_event_manager );
330
331     char ** all_keys = vlc_dictionary_all_keys( &p_md->tags );
332     for( i = 0; all_keys[i]; i++ )
333     {
334         int j;
335         struct libvlc_tags_storage_t * p_ts = vlc_dictionary_value_for_key( &p_md->tags, all_keys[i] );
336         for( j = 0; j < p_ts->i_count; j++ )
337         {
338             free( p_ts->ppsz_tags[j] );
339             free( p_ts->ppsz_tags );
340         }
341         free( p_ts );
342     }
343     vlc_dictionary_clear( &p_md->tags );
344     free( p_md );
345 }
346
347 /**************************************************************************
348  * Retain a media descriptor object
349  **************************************************************************/
350 void libvlc_media_descriptor_retain( libvlc_media_descriptor_t *p_md )
351 {
352     if (!p_md)
353         return;
354
355     p_md->i_refcount++;
356 }
357
358 /**************************************************************************
359  * Duplicate a media descriptor object
360  **************************************************************************/
361 libvlc_media_descriptor_t *
362 libvlc_media_descriptor_duplicate( libvlc_media_descriptor_t *p_md_orig )
363 {
364     return libvlc_media_descriptor_new_from_input_item(
365         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
366 }
367
368 /**************************************************************************
369  * Retain a media descriptor object
370  **************************************************************************/
371 char *
372 libvlc_media_descriptor_get_mrl( libvlc_media_descriptor_t * p_md,
373                                  libvlc_exception_t * p_e )
374 {
375     (void)p_e;
376     return input_item_GetURI( p_md->p_input_item );
377 }
378
379 /**************************************************************************
380  * Getter for meta information
381  **************************************************************************/
382
383 char * libvlc_media_descriptor_get_meta( libvlc_media_descriptor_t *p_md,
384                                          libvlc_meta_t e_meta,
385                                          libvlc_exception_t *p_e )
386 {
387     char * psz_meta;
388
389     /* XXX: locking */
390
391     preparse_if_needed( p_md );
392
393     psz_meta = input_item_GetMeta( p_md->p_input_item,
394                                    libvlc_to_vlc_meta[e_meta] );
395
396     /* Should be integrated in core */
397     if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
398     {
399         free( psz_meta );
400         return strdup( p_md->p_input_item->psz_name );
401     }
402
403     return psz_meta;
404 }
405
406 /**************************************************************************
407  * Add a tag
408  **************************************************************************/
409 void libvlc_media_descriptor_add_tag( libvlc_media_descriptor_t *p_md,
410                                       const char * key,
411                                       const libvlc_tag_t tag,
412                                       libvlc_exception_t *p_e )
413 {
414     struct libvlc_tags_storage_t * p_ts;
415
416     if( !tag || !key )
417         return;
418  
419     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
420
421     if( !p_ts )
422     {
423         p_ts = malloc(sizeof(struct libvlc_tags_storage_t));
424         memset( p_ts, 0, sizeof(struct libvlc_tags_storage_t) );
425     }
426     p_ts->i_count++;
427
428     if( !p_ts->ppsz_tags )
429         p_ts->ppsz_tags = malloc(sizeof(char*)*(p_ts->i_count));
430     else
431         p_ts->ppsz_tags = realloc(p_ts->ppsz_tags, sizeof(char*)*(p_ts->i_count));
432  
433     p_ts->ppsz_tags[p_ts->i_count-1] = strdup( tag );
434 }
435
436
437 /**************************************************************************
438  * Remove a tag
439  **************************************************************************/
440 void libvlc_media_descriptor_remove_tag( libvlc_media_descriptor_t *p_md,
441                                          const char * key,
442                                          const libvlc_tag_t tag,
443                                          libvlc_exception_t *p_e )
444 {
445     struct libvlc_tags_storage_t * p_ts;
446     int i;
447
448     if( !tag || !key )
449         return;
450  
451     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
452
453     if( !p_ts )
454         return;
455
456     for( i = 0; i < p_ts->i_count; i++ )
457     {
458         if( !strcmp( p_ts->ppsz_tags[i], tag ) )
459         {
460             free( p_ts->ppsz_tags[i] );
461             memcpy( p_ts->ppsz_tags + i + 1, p_ts->ppsz_tags + i, (p_ts->i_count - i - 2)*sizeof(char*) );
462             /* Don't dealloc, the memory will be regain if we add a new tag */
463             p_ts->i_count--;
464             return;
465         }
466     }
467 }
468
469 /**************************************************************************
470  * Get tags count
471  **************************************************************************/
472 int libvlc_media_descriptor_tags_count_for_key( libvlc_media_descriptor_t *p_md,
473                                                  const char * key,
474                                                  libvlc_exception_t *p_e )
475 {
476     struct libvlc_tags_storage_t * p_ts;
477
478     if( !key )
479         return 0;
480  
481     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
482
483     if( !p_ts )
484         return 0;
485     return p_ts->i_count;
486 }
487
488 /**************************************************************************
489  * Get a tag
490  **************************************************************************/
491 libvlc_tag_t
492 libvlc_media_descriptor_tag_at_index_for_key( libvlc_media_descriptor_t *p_md,
493                                               int i,
494                                               const char * key,
495                                               libvlc_exception_t *p_e )
496 {
497     struct libvlc_tags_storage_t * p_ts;
498
499     if( !key )
500         return NULL;
501  
502     p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
503
504     if( !p_ts )
505         return NULL;
506  
507     return strdup( p_ts->ppsz_tags[i] );
508 }
509
510 /**************************************************************************
511  * subitems
512  **************************************************************************/
513 libvlc_media_list_t *
514 libvlc_media_descriptor_subitems( libvlc_media_descriptor_t * p_md,
515                                   libvlc_exception_t * p_e )
516 {
517     if( p_md->p_subitems )
518         libvlc_media_list_retain( p_md->p_subitems );
519     return p_md->p_subitems;
520 }
521
522 /**************************************************************************
523  * event_manager
524  **************************************************************************/
525 libvlc_event_manager_t *
526 libvlc_media_descriptor_event_manager( libvlc_media_descriptor_t * p_md,
527                                        libvlc_exception_t * p_e )
528 {
529     return p_md->p_event_manager;
530 }
531
532 /**************************************************************************
533  * Get duration of media_descriptor object.
534  **************************************************************************/
535 vlc_int64_t
536 libvlc_media_descriptor_get_duration( libvlc_media_descriptor_t * p_md,
537                                       libvlc_exception_t * p_e )
538 {
539     if( p_md && p_md->p_input_item)
540     {
541         return input_item_GetDuration( p_md->p_input_item );
542     }
543     else
544     {
545         return -1;
546     }
547 }
548
549 /**************************************************************************
550  * Get preparsed status for media_descriptor object.
551  **************************************************************************/
552 vlc_bool_t
553 libvlc_media_descriptor_is_preparsed( libvlc_media_descriptor_t * p_md,
554                                        libvlc_exception_t * p_e )
555 {
556     if( p_md && p_md->p_input_item)
557     {
558         return input_item_IsPreparsed( p_md->p_input_item );
559     }
560     else
561     {
562         return VLC_FALSE;
563     }
564 }
565
566 /**************************************************************************
567  * Sets media descriptor's user_data. user_data is specialized data 
568  * accessed by the host application, VLC.framework uses it as a pointer to 
569  * an native object that references a libvlc_media_descriptor_t pointer
570  **************************************************************************/
571 void 
572 libvlc_media_descriptor_set_user_data( libvlc_media_descriptor_t * p_md,
573                                        void * p_new_user_data,
574                                        libvlc_exception_t * p_e )
575 {
576     if( p_md )
577     {
578         p_md->p_user_data = p_new_user_data;
579     }
580 }
581
582 /**************************************************************************
583  * Get media descriptor's user_data. user_data is specialized data 
584  * accessed by the host application, VLC.framework uses it as a pointer to 
585  * an native object that references a libvlc_media_descriptor_t pointer
586  **************************************************************************/
587 void *
588 libvlc_media_descriptor_get_user_data( libvlc_media_descriptor_t * p_md,
589                                        libvlc_exception_t * p_e )
590 {
591     if( p_md )
592     {
593         return p_md->p_user_data;
594     }
595     else
596     {
597         return NULL;
598     }
599 }
600