]> git.sesse.net Git - vlc/blob - src/control/media.c
update module LIST file.
[vlc] / src / control / media.c
1 /*****************************************************************************
2  * media.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 "libvlc_internal.h"
25
26 #include <vlc/libvlc.h>
27 #include <vlc_input.h>
28 #include <vlc_meta.h>
29
30 /* For the preparser */
31 #include <vlc_playlist.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_t * p_md = user_data;
82     libvlc_media_t * p_md_child;
83     libvlc_event_t event;
84
85     p_md_child = libvlc_media_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( p_md->p_subitems, p_md, NULL );
94     }
95     if( p_md->p_subitems )
96     {
97         libvlc_media_list_add_media( p_md->p_subitems, p_md_child, NULL );
98     }
99
100     /* Construct the event */
101     event.type = libvlc_MediaSubItemAdded;
102     event.u.media_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_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_t * p_md = user_data;
116     libvlc_event_t event;
117
118     /* Construct the event */
119     event.type = libvlc_MediaMetaChanged;
120     event.u.media_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_t * p_md = user_data;
134     libvlc_event_t event;
135
136     /* Construct the event */
137     event.type = libvlc_MediaDurationChanged;
138     event.u.media_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_t * p_md = user_data;
152     libvlc_event_t event;
153
154     /* Construct the event */
155     event.type = libvlc_MediaPreparsedChanged;
156     event.u.media_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_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_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_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         p_md->b_preparsed = VLC_TRUE;
221     }
222 }
223
224 /**************************************************************************
225  * Create a new media descriptor object from an input_item
226  * (libvlc internal)
227  * That's the generic constructor
228  **************************************************************************/
229 libvlc_media_t * libvlc_media_new_from_input_item(
230                                    libvlc_instance_t *p_instance,
231                                    input_item_t *p_input_item,
232                                    libvlc_exception_t *p_e )
233 {
234     libvlc_media_t * p_md;
235
236     if (!p_input_item)
237     {
238         libvlc_exception_raise( p_e, "No input item given" );
239         return NULL;
240     }
241
242     p_md = malloc( sizeof(libvlc_media_t) );
243     p_md->p_libvlc_instance = p_instance;
244     p_md->p_input_item      = p_input_item;
245     p_md->b_preparsed       = VLC_FALSE;
246     p_md->i_refcount        = 1;
247     p_md->p_user_data       = NULL;
248
249     p_md->state = libvlc_NothingSpecial;
250
251     /* A media descriptor can be a playlist. When you open a playlist
252      * It can give a bunch of item to read. */
253     p_md->p_subitems        = NULL;
254
255     p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance, p_e );
256     libvlc_event_manager_register_event_type( p_md->p_event_manager,
257         libvlc_MediaMetaChanged, p_e );
258     libvlc_event_manager_register_event_type( p_md->p_event_manager,
259         libvlc_MediaSubItemAdded, p_e );
260     libvlc_event_manager_register_event_type( p_md->p_event_manager,
261         libvlc_MediaFreed, p_e );
262     libvlc_event_manager_register_event_type( p_md->p_event_manager,
263         libvlc_MediaDurationChanged, p_e );
264     libvlc_event_manager_register_event_type( p_md->p_event_manager,
265         libvlc_MediaStateChanged, p_e );
266
267     vlc_gc_incref( p_md->p_input_item );
268
269     install_input_item_observer( p_md );
270
271     return p_md;
272 }
273
274 /**************************************************************************
275  * Create a new media descriptor object
276  **************************************************************************/
277 libvlc_media_t * libvlc_media_new(
278                                    libvlc_instance_t *p_instance,
279                                    const char * psz_mrl,
280                                    libvlc_exception_t *p_e )
281 {
282     input_item_t * p_input_item;
283     libvlc_media_t * p_md;
284
285     p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, NULL );
286
287     if (!p_input_item)
288     {
289         libvlc_exception_raise( p_e, "Can't create md's input_item" );
290         return NULL;
291     }
292
293     p_md = libvlc_media_new_from_input_item( p_instance,
294                 p_input_item, p_e );
295
296     /* The p_input_item is retained in libvlc_media_new_from_input_item */
297     vlc_gc_decref( p_input_item );
298     
299     return p_md;
300 }
301
302 /**************************************************************************
303  * Create a new media descriptor object
304  **************************************************************************/
305 libvlc_media_t * libvlc_media_new_as_node(
306                                    libvlc_instance_t *p_instance,
307                                    const char * psz_name,
308                                    libvlc_exception_t *p_e )
309 {
310     input_item_t * p_input_item;
311     libvlc_media_t * p_md;
312
313     p_input_item = input_ItemNew( p_instance->p_libvlc_int, "vlc://nop", psz_name );
314
315     if (!p_input_item)
316     {
317         libvlc_exception_raise( p_e, "Can't create md's input_item" );
318         return NULL;
319     }
320
321     p_md = libvlc_media_new_from_input_item( p_instance,
322                 p_input_item, p_e );
323
324     p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );
325
326     return p_md;
327 }
328
329 /**************************************************************************
330  * Add an option to the media descriptor,
331  * that will be used to determine how the media_player will read the
332  * media. This allow to use VLC advanced reading/streaming
333  * options in a per-media basis
334  *
335  * The options are detailled in vlc --long-help, for instance "--sout-all"
336  **************************************************************************/
337 void libvlc_media_add_option(
338                                    libvlc_media_t * p_md,
339                                    const char * ppsz_option,
340                                    libvlc_exception_t *p_e )
341 {
342     (void)p_e;
343     input_ItemAddOpt( p_md->p_input_item, ppsz_option,
344                       VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
345 }
346
347 /**************************************************************************
348  * Delete a media descriptor object
349  **************************************************************************/
350 void libvlc_media_release( libvlc_media_t *p_md )
351 {
352     if (!p_md)
353         return;
354
355     p_md->i_refcount--;
356
357     if( p_md->i_refcount > 0 )
358         return;
359
360     if( p_md->p_subitems )
361         libvlc_media_list_release( p_md->p_subitems );
362
363     uninstall_input_item_observer( p_md );
364     vlc_gc_decref( p_md->p_input_item );
365
366     /* Construct the event */
367     libvlc_event_t event;
368     event.type = libvlc_MediaFreed;
369     event.u.media_freed.md = p_md;
370
371     /* Send the event */
372     libvlc_event_send( p_md->p_event_manager, &event );
373
374     libvlc_event_manager_release( p_md->p_event_manager );
375
376     free( p_md );
377 }
378
379 /**************************************************************************
380  * Retain a media descriptor object
381  **************************************************************************/
382 void libvlc_media_retain( libvlc_media_t *p_md )
383 {
384     if (!p_md)
385         return;
386
387     p_md->i_refcount++;
388 }
389
390 /**************************************************************************
391  * Duplicate a media descriptor object
392  **************************************************************************/
393 libvlc_media_t *
394 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
395 {
396     return libvlc_media_new_from_input_item(
397         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
398 }
399
400 /**************************************************************************
401  * Retain a media descriptor object
402  **************************************************************************/
403 char *
404 libvlc_media_get_mrl( libvlc_media_t * p_md,
405                                  libvlc_exception_t * p_e )
406 {
407     (void)p_e;
408     return input_item_GetURI( p_md->p_input_item );
409 }
410
411 /**************************************************************************
412  * Getter for meta information
413  **************************************************************************/
414
415 char * libvlc_media_get_meta( libvlc_media_t *p_md,
416                                          libvlc_meta_t e_meta,
417                                          libvlc_exception_t *p_e )
418 {
419     VLC_UNUSED(p_e);
420
421     char * psz_meta;
422
423     /* XXX: locking */
424
425     preparse_if_needed( p_md );
426
427     psz_meta = input_item_GetMeta( p_md->p_input_item,
428                                    libvlc_to_vlc_meta[e_meta] );
429     
430     if( e_meta == libvlc_meta_ArtworkURL && !psz_meta )
431     {
432         playlist_AskForArtEnqueue(
433                 p_md->p_libvlc_instance->p_libvlc_int->p_playlist,
434                 p_md->p_input_item );
435     }
436
437     /* Should be integrated in core */
438     if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
439     {
440         free( psz_meta );
441         return strdup( p_md->p_input_item->psz_name );
442     }
443
444     return psz_meta;
445 }
446
447 /**************************************************************************
448  * Getter for state information
449  * Can be error, playing, buffering, NothingSpecial.
450  **************************************************************************/
451
452 libvlc_state_t
453 libvlc_media_get_state( libvlc_media_t *p_md,
454                                    libvlc_exception_t *p_e )
455 {
456     (void)p_e;
457     return p_md->state;
458 }
459
460 /**************************************************************************
461  * Setter for state information (LibVLC Internal)
462  **************************************************************************/
463
464 void
465 libvlc_media_set_state( libvlc_media_t *p_md,
466                                    libvlc_state_t state,
467                                    libvlc_exception_t *p_e )
468 {
469     (void)p_e;
470     libvlc_event_t event;
471
472     p_md->state = state;
473
474     /* Construct the event */
475     event.type = libvlc_MediaStateChanged;
476     event.u.media_state_changed.new_state = state;
477
478     /* Send the event */
479     libvlc_event_send( p_md->p_event_manager, &event );
480 }
481
482 /**************************************************************************
483  * subitems
484  **************************************************************************/
485 libvlc_media_list_t *
486 libvlc_media_subitems( libvlc_media_t * p_md,
487                                   libvlc_exception_t * p_e )
488 {
489     VLC_UNUSED(p_e);
490
491     if( p_md->p_subitems )
492         libvlc_media_list_retain( p_md->p_subitems );
493     return p_md->p_subitems;
494 }
495
496 /**************************************************************************
497  * event_manager
498  **************************************************************************/
499 libvlc_event_manager_t *
500 libvlc_media_event_manager( libvlc_media_t * p_md,
501                                        libvlc_exception_t * p_e )
502 {
503     VLC_UNUSED(p_e);
504
505     return p_md->p_event_manager;
506 }
507
508 /**************************************************************************
509  * Get duration of media object.
510  **************************************************************************/
511 vlc_int64_t
512 libvlc_media_get_duration( libvlc_media_t * p_md,
513                                       libvlc_exception_t * p_e )
514 {
515     VLC_UNUSED(p_e);
516
517     if( p_md && p_md->p_input_item)
518     {
519         return input_item_GetDuration( p_md->p_input_item );
520     }
521     else
522     {
523         return -1;
524     }
525 }
526
527 /**************************************************************************
528  * Get preparsed status for media object.
529  **************************************************************************/
530 int
531 libvlc_media_is_preparsed( libvlc_media_t * p_md,
532                                        libvlc_exception_t * p_e )
533 {
534     VLC_UNUSED(p_e);
535
536     if( p_md && p_md->p_input_item)
537     {
538         return input_item_IsPreparsed( p_md->p_input_item );
539     }
540     else
541     {
542         return VLC_FALSE;
543     }
544 }
545
546 /**************************************************************************
547  * Sets media descriptor's user_data. user_data is specialized data 
548  * accessed by the host application, VLC.framework uses it as a pointer to 
549  * an native object that references a libvlc_media_t pointer
550  **************************************************************************/
551 void 
552 libvlc_media_set_user_data( libvlc_media_t * p_md,
553                                        void * p_new_user_data,
554                                        libvlc_exception_t * p_e )
555 {
556     VLC_UNUSED(p_e);
557
558     if( p_md )
559     {
560         p_md->p_user_data = p_new_user_data;
561     }
562 }
563
564 /**************************************************************************
565  * Get media descriptor's user_data. user_data is specialized data 
566  * accessed by the host application, VLC.framework uses it as a pointer to 
567  * an native object that references a libvlc_media_t pointer
568  **************************************************************************/
569 void *
570 libvlc_media_get_user_data( libvlc_media_t * p_md,
571                                        libvlc_exception_t * p_e )
572 {
573     VLC_UNUSED(p_e);
574
575     if( p_md )
576     {
577         return p_md->p_user_data;
578     }
579     else
580     {
581         return NULL;
582     }
583 }
584