]> git.sesse.net Git - vlc/blob - src/control/media.c
Move LibVLC error messages to libvlc_printerr().
[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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc/libvlc.h>
29 #include <vlc/libvlc_media.h>
30 #include <vlc/libvlc_media_list.h> // For the subitems, here for convenience
31 #include <vlc/libvlc_events.h>
32
33 #include <vlc_common.h>
34 #include <vlc_input.h>
35 #include <vlc_meta.h>
36 #include <vlc_playlist.h> /* For the preparser */
37
38 #include "libvlc.h"
39
40 #include "libvlc_internal.h"
41 #include "media_internal.h"
42
43 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
44 {
45     [libvlc_meta_Title]        = vlc_meta_Title,
46     [libvlc_meta_Artist]       = vlc_meta_Artist,
47     [libvlc_meta_Genre]        = vlc_meta_Genre,
48     [libvlc_meta_Copyright]    = vlc_meta_Copyright,
49     [libvlc_meta_Album]        = vlc_meta_Album,
50     [libvlc_meta_TrackNumber]  = vlc_meta_TrackNumber,
51     [libvlc_meta_Description]  = vlc_meta_Description,
52     [libvlc_meta_Rating]       = vlc_meta_Rating,
53     [libvlc_meta_Date]         = vlc_meta_Date,
54     [libvlc_meta_Setting]      = vlc_meta_Setting,
55     [libvlc_meta_URL]          = vlc_meta_URL,
56     [libvlc_meta_Language]     = vlc_meta_Language,
57     [libvlc_meta_NowPlaying]   = vlc_meta_NowPlaying,
58     [libvlc_meta_Publisher]    = vlc_meta_Publisher,
59     [libvlc_meta_EncodedBy]    = vlc_meta_EncodedBy,
60     [libvlc_meta_ArtworkURL]   = vlc_meta_ArtworkURL,
61     [libvlc_meta_TrackID]      = vlc_meta_TrackID
62 };
63
64 static const libvlc_meta_t vlc_to_libvlc_meta[] =
65 {
66     [vlc_meta_Title]        = libvlc_meta_Title,
67     [vlc_meta_Artist]       = libvlc_meta_Artist,
68     [vlc_meta_Genre]        = libvlc_meta_Genre,
69     [vlc_meta_Copyright]    = libvlc_meta_Copyright,
70     [vlc_meta_Album]        = libvlc_meta_Album,
71     [vlc_meta_TrackNumber]  = libvlc_meta_TrackNumber,
72     [vlc_meta_Description]  = libvlc_meta_Description,
73     [vlc_meta_Rating]       = libvlc_meta_Rating,
74     [vlc_meta_Date]         = libvlc_meta_Date,
75     [vlc_meta_Setting]      = libvlc_meta_Setting,
76     [vlc_meta_URL]          = libvlc_meta_URL,
77     [vlc_meta_Language]     = libvlc_meta_Language,
78     [vlc_meta_NowPlaying]   = libvlc_meta_NowPlaying,
79     [vlc_meta_Publisher]    = libvlc_meta_Publisher,
80     [vlc_meta_EncodedBy]    = libvlc_meta_EncodedBy,
81     [vlc_meta_ArtworkURL]   = libvlc_meta_ArtworkURL,
82     [vlc_meta_TrackID]      = libvlc_meta_TrackID
83 };
84
85 /**************************************************************************
86  * input_item_subitem_added (Private) (vlc event Callback)
87  **************************************************************************/
88 static void input_item_subitem_added( const vlc_event_t *p_event,
89                                        void * user_data )
90 {
91     libvlc_media_t * p_md = user_data;
92     libvlc_media_t * p_md_child;
93     libvlc_event_t event;
94
95     p_md_child = libvlc_media_new_from_input_item(
96                 p_md->p_libvlc_instance,
97                 p_event->u.input_item_subitem_added.p_new_child, NULL );
98
99     /* Add this to our media list */
100     if( !p_md->p_subitems )
101     {
102         p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );
103         libvlc_media_list_set_media( p_md->p_subitems, p_md, NULL );
104     }
105     if( p_md->p_subitems )
106     {
107         libvlc_media_list_add_media( p_md->p_subitems, p_md_child, NULL );
108     }
109
110     /* Construct the event */
111     event.type = libvlc_MediaSubItemAdded;
112     event.u.media_subitem_added.new_child = p_md_child;
113
114     /* Send the event */
115     libvlc_event_send( p_md->p_event_manager, &event );
116     libvlc_media_release( p_md_child );
117 }
118
119 /**************************************************************************
120  * input_item_meta_changed (Private) (vlc event Callback)
121  **************************************************************************/
122 static void input_item_meta_changed( const vlc_event_t *p_event,
123                                      void * user_data )
124 {
125     libvlc_media_t * p_md = user_data;
126     libvlc_event_t event;
127
128     /* Construct the event */
129     event.type = libvlc_MediaMetaChanged;
130     event.u.media_meta_changed.meta_type =
131         vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
132
133     /* Send the event */
134     libvlc_event_send( p_md->p_event_manager, &event );
135 }
136
137 /**************************************************************************
138  * input_item_duration_changed (Private) (vlc event Callback)
139  **************************************************************************/
140 static void input_item_duration_changed( const vlc_event_t *p_event,
141                                          void * user_data )
142 {
143     libvlc_media_t * p_md = user_data;
144     libvlc_event_t event;
145
146     /* Construct the event */
147     event.type = libvlc_MediaDurationChanged;
148     event.u.media_duration_changed.new_duration = 
149         p_event->u.input_item_duration_changed.new_duration;
150
151     /* Send the event */
152     libvlc_event_send( p_md->p_event_manager, &event );
153 }
154
155 /**************************************************************************
156  * input_item_preparsed_changed (Private) (vlc event Callback)
157  **************************************************************************/
158 static void input_item_preparsed_changed( const vlc_event_t *p_event,
159                                           void * user_data )
160 {
161     libvlc_media_t * p_md = user_data;
162     libvlc_event_t event;
163
164     /* Construct the event */
165     event.type = libvlc_MediaPreparsedChanged;
166     event.u.media_preparsed_changed.new_status = 
167         p_event->u.input_item_preparsed_changed.new_status;
168
169     /* Send the event */
170     libvlc_event_send( p_md->p_event_manager, &event );
171 }
172
173 /**************************************************************************
174  * Install event handler (Private)
175  **************************************************************************/
176 static void install_input_item_observer( libvlc_media_t *p_md )
177 {
178     vlc_event_attach( &p_md->p_input_item->event_manager,
179                       vlc_InputItemSubItemAdded,
180                       input_item_subitem_added,
181                       p_md );
182     vlc_event_attach( &p_md->p_input_item->event_manager,
183                       vlc_InputItemMetaChanged,
184                       input_item_meta_changed,
185                       p_md );
186     vlc_event_attach( &p_md->p_input_item->event_manager,
187                       vlc_InputItemDurationChanged,
188                       input_item_duration_changed,
189                       p_md );
190     vlc_event_attach( &p_md->p_input_item->event_manager,
191                       vlc_InputItemPreparsedChanged,
192                       input_item_preparsed_changed,
193                       p_md );
194 }
195
196 /**************************************************************************
197  * Uninstall event handler (Private)
198  **************************************************************************/
199 static void uninstall_input_item_observer( libvlc_media_t *p_md )
200 {
201     vlc_event_detach( &p_md->p_input_item->event_manager,
202                       vlc_InputItemSubItemAdded,
203                       input_item_subitem_added,
204                       p_md );
205     vlc_event_detach( &p_md->p_input_item->event_manager,
206                       vlc_InputItemMetaChanged,
207                       input_item_meta_changed,
208                       p_md );
209     vlc_event_detach( &p_md->p_input_item->event_manager,
210                       vlc_InputItemDurationChanged,
211                       input_item_duration_changed,
212                       p_md );
213     vlc_event_detach( &p_md->p_input_item->event_manager,
214                       vlc_InputItemPreparsedChanged,
215                       input_item_preparsed_changed,
216                       p_md );
217 }
218
219 /**************************************************************************
220  * Preparse if not already done (Private)
221  **************************************************************************/
222 static void preparse_if_needed( libvlc_media_t *p_md )
223 {
224     /* XXX: need some locking here */
225     if (!p_md->b_preparsed)
226     {
227         playlist_PreparseEnqueue(
228                 libvlc_priv (p_md->p_libvlc_instance->p_libvlc_int)->p_playlist,
229                 p_md->p_input_item, pl_Unlocked );
230         p_md->b_preparsed = true;
231     }
232 }
233
234 /**************************************************************************
235  * Create a new media descriptor object from an input_item
236  * (libvlc internal)
237  * That's the generic constructor
238  **************************************************************************/
239 libvlc_media_t * libvlc_media_new_from_input_item(
240                                    libvlc_instance_t *p_instance,
241                                    input_item_t *p_input_item,
242                                    libvlc_exception_t *p_e )
243 {
244     libvlc_media_t * p_md;
245
246     if (!p_input_item)
247     {
248         libvlc_exception_raise( p_e );
249         libvlc_printerr( "No input item given" );
250         return NULL;
251     }
252
253     p_md = malloc( sizeof(libvlc_media_t) );
254     if( !p_md )
255     {
256         libvlc_exception_raise( p_e );
257         libvlc_printerr( "Not enough memory" );
258         return NULL;
259     }
260
261     p_md->p_libvlc_instance = p_instance;
262     p_md->p_input_item      = p_input_item;
263     p_md->b_preparsed       = false;
264     p_md->i_refcount        = 1;
265     p_md->p_user_data       = NULL;
266
267     p_md->state = libvlc_NothingSpecial;
268
269     /* A media descriptor can be a playlist. When you open a playlist
270      * It can give a bunch of item to read. */
271     p_md->p_subitems        = NULL;
272
273     p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance, p_e );
274     libvlc_event_manager_register_event_type( p_md->p_event_manager,
275         libvlc_MediaMetaChanged, p_e );
276     libvlc_event_manager_register_event_type( p_md->p_event_manager,
277         libvlc_MediaSubItemAdded, p_e );
278     libvlc_event_manager_register_event_type( p_md->p_event_manager,
279         libvlc_MediaFreed, p_e );
280     libvlc_event_manager_register_event_type( p_md->p_event_manager,
281         libvlc_MediaDurationChanged, p_e );
282     libvlc_event_manager_register_event_type( p_md->p_event_manager,
283         libvlc_MediaStateChanged, p_e );
284
285     vlc_gc_incref( p_md->p_input_item );
286
287     install_input_item_observer( p_md );
288
289     return p_md;
290 }
291
292 /**************************************************************************
293  * Create a new media descriptor object
294  **************************************************************************/
295 libvlc_media_t * libvlc_media_new(
296                                    libvlc_instance_t *p_instance,
297                                    const char * psz_mrl,
298                                    libvlc_exception_t *p_e )
299 {
300     input_item_t * p_input_item;
301     libvlc_media_t * p_md;
302
303     p_input_item = input_item_New( p_instance->p_libvlc_int, psz_mrl, NULL );
304
305     if (!p_input_item)
306     {
307         libvlc_exception_raise( p_e );
308         libvlc_printerr( "Not enough memory" );
309         return NULL;
310     }
311
312     p_md = libvlc_media_new_from_input_item( p_instance,
313                 p_input_item, p_e );
314
315     /* The p_input_item is retained in libvlc_media_new_from_input_item */
316     vlc_gc_decref( p_input_item );
317
318     return p_md;
319 }
320
321 /**************************************************************************
322  * Create a new media descriptor object
323  **************************************************************************/
324 libvlc_media_t * libvlc_media_new_as_node(
325                                    libvlc_instance_t *p_instance,
326                                    const char * psz_name,
327                                    libvlc_exception_t *p_e )
328 {
329     input_item_t * p_input_item;
330     libvlc_media_t * p_md;
331
332     p_input_item = input_item_New( p_instance->p_libvlc_int, "vlc://nop", psz_name );
333
334     if (!p_input_item)
335     {
336         libvlc_exception_raise( p_e );
337         libvlc_printerr( "Not enough memory" );
338         return NULL;
339     }
340
341     p_md = libvlc_media_new_from_input_item( p_instance,
342                 p_input_item, p_e );
343
344     p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );
345
346     return p_md;
347 }
348
349 /**************************************************************************
350  * Add an option to the media descriptor,
351  * that will be used to determine how the media_player will read the
352  * media. This allow to use VLC advanced reading/streaming
353  * options in a per-media basis
354  *
355  * The options are detailled in vlc --long-help, for instance "--sout-all"
356  **************************************************************************/
357 void libvlc_media_add_option(
358                                    libvlc_media_t * p_md,
359                                    const char * psz_option,
360                                    libvlc_exception_t *p_e )
361 {
362     VLC_UNUSED(p_e);
363     input_item_AddOption( p_md->p_input_item, psz_option,
364                           VLC_INPUT_OPTION_UNIQUE|VLC_INPUT_OPTION_TRUSTED );
365 }
366
367 /**************************************************************************
368  * Same as libvlc_media_add_option but with configurable flags.
369  **************************************************************************/
370 void libvlc_media_add_option_flag(
371                                    libvlc_media_t * p_md,
372                                    const char * ppsz_option,
373                                    libvlc_media_option_t i_flags,
374                                    libvlc_exception_t *p_e )
375 {
376     VLC_UNUSED(p_e);
377     input_item_AddOption( p_md->p_input_item, ppsz_option,
378                           i_flags );
379 }
380
381 /**************************************************************************
382  * Delete a media descriptor object
383  **************************************************************************/
384 void libvlc_media_release( libvlc_media_t *p_md )
385 {
386     if (!p_md)
387         return;
388
389     p_md->i_refcount--;
390
391     if( p_md->i_refcount > 0 )
392         return;
393
394     if( p_md->p_subitems )
395         libvlc_media_list_release( p_md->p_subitems );
396
397     uninstall_input_item_observer( p_md );
398     vlc_gc_decref( p_md->p_input_item );
399
400     /* Construct the event */
401     libvlc_event_t event;
402     event.type = libvlc_MediaFreed;
403     event.u.media_freed.md = p_md;
404
405     /* Send the event */
406     libvlc_event_send( p_md->p_event_manager, &event );
407
408     libvlc_event_manager_release( p_md->p_event_manager );
409
410     free( p_md );
411 }
412
413 /**************************************************************************
414  * Retain a media descriptor object
415  **************************************************************************/
416 void libvlc_media_retain( libvlc_media_t *p_md )
417 {
418     if (!p_md)
419         return;
420
421     p_md->i_refcount++;
422 }
423
424 /**************************************************************************
425  * Duplicate a media descriptor object
426  **************************************************************************/
427 libvlc_media_t *
428 libvlc_media_duplicate( libvlc_media_t *p_md_orig )
429 {
430     return libvlc_media_new_from_input_item(
431         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
432 }
433
434 /**************************************************************************
435  * Get mrl from a media descriptor object
436  **************************************************************************/
437 char *
438 libvlc_media_get_mrl( libvlc_media_t * p_md,
439                                  libvlc_exception_t * p_e )
440 {
441     VLC_UNUSED(p_e);
442     return input_item_GetURI( p_md->p_input_item );
443 }
444
445 /**************************************************************************
446  * Getter for meta information
447  **************************************************************************/
448
449 char * libvlc_media_get_meta( libvlc_media_t *p_md,
450                                          libvlc_meta_t e_meta,
451                                          libvlc_exception_t *p_e )
452 {
453     char * psz_meta;
454     VLC_UNUSED(p_e);
455
456     /* XXX: locking */
457
458     preparse_if_needed( p_md );
459
460     psz_meta = input_item_GetMeta( p_md->p_input_item,
461                                    libvlc_to_vlc_meta[e_meta] );
462
463     if( e_meta == libvlc_meta_ArtworkURL && !psz_meta )
464     {
465         playlist_AskForArtEnqueue(
466                 libvlc_priv(p_md->p_libvlc_instance->p_libvlc_int)->p_playlist,
467                 p_md->p_input_item, pl_Unlocked );
468     }
469
470     /* Should be integrated in core */
471     if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
472     {
473         free( psz_meta );
474         return strdup( p_md->p_input_item->psz_name );
475     }
476
477     return psz_meta;
478 }
479
480 /**************************************************************************
481  * Getter for state information
482  * Can be error, playing, buffering, NothingSpecial.
483  **************************************************************************/
484
485 libvlc_state_t
486 libvlc_media_get_state( libvlc_media_t *p_md,
487                                    libvlc_exception_t *p_e )
488 {
489     VLC_UNUSED(p_e);
490     return p_md->state;
491 }
492
493 /**************************************************************************
494  * Setter for state information (LibVLC Internal)
495  **************************************************************************/
496
497 void
498 libvlc_media_set_state( libvlc_media_t *p_md,
499                                    libvlc_state_t state,
500                                    libvlc_exception_t *p_e )
501 {
502     libvlc_event_t event;
503     VLC_UNUSED(p_e);
504
505     p_md->state = state;
506
507     /* Construct the event */
508     event.type = libvlc_MediaStateChanged;
509     event.u.media_state_changed.new_state = state;
510
511     /* Send the event */
512     libvlc_event_send( p_md->p_event_manager, &event );
513 }
514
515 /**************************************************************************
516  * subitems
517  **************************************************************************/
518 libvlc_media_list_t *
519 libvlc_media_subitems( libvlc_media_t * p_md,
520                                   libvlc_exception_t * p_e )
521 {
522     VLC_UNUSED(p_e);
523
524     if( p_md->p_subitems )
525         libvlc_media_list_retain( p_md->p_subitems );
526     return p_md->p_subitems;
527 }
528
529 /**************************************************************************
530  * event_manager
531  **************************************************************************/
532 libvlc_event_manager_t *
533 libvlc_media_event_manager( libvlc_media_t * p_md,
534                                        libvlc_exception_t * p_e )
535 {
536     VLC_UNUSED(p_e);
537
538     return p_md->p_event_manager;
539 }
540
541 /**************************************************************************
542  * Get duration of media object (in ms)
543  **************************************************************************/
544 int64_t
545 libvlc_media_get_duration( libvlc_media_t * p_md,
546                                       libvlc_exception_t * p_e )
547 {
548     VLC_UNUSED(p_e);
549
550     if( !p_md || !p_md->p_input_item)
551     {
552         libvlc_exception_raise( p_e );
553         libvlc_printerr( "No input item" );
554         return -1;
555     }
556
557     return input_item_GetDuration( p_md->p_input_item ) / 1000;
558 }
559
560 /**************************************************************************
561  * Get preparsed status for media object.
562  **************************************************************************/
563 int
564 libvlc_media_is_preparsed( libvlc_media_t * p_md,
565                                        libvlc_exception_t * p_e )
566 {
567     VLC_UNUSED(p_e);
568
569     if( !p_md || !p_md->p_input_item)
570     {
571         libvlc_exception_raise( p_e );
572         libvlc_printerr( "No input item" );
573         return false;
574     }
575
576     return input_item_IsPreparsed( p_md->p_input_item );
577 }
578
579 /**************************************************************************
580  * Sets media descriptor's user_data. user_data is specialized data 
581  * accessed by the host application, VLC.framework uses it as a pointer to 
582  * an native object that references a libvlc_media_t pointer
583  **************************************************************************/
584 void 
585 libvlc_media_set_user_data( libvlc_media_t * p_md,
586                                        void * p_new_user_data,
587                                        libvlc_exception_t * p_e )
588 {
589     VLC_UNUSED(p_e);
590
591     if( p_md )
592     {
593         p_md->p_user_data = p_new_user_data;
594     }
595 }
596
597 /**************************************************************************
598  * Get media descriptor's user_data. user_data is specialized data 
599  * accessed by the host application, VLC.framework uses it as a pointer to 
600  * an native object that references a libvlc_media_t pointer
601  **************************************************************************/
602 void *
603 libvlc_media_get_user_data( libvlc_media_t * p_md,
604                                        libvlc_exception_t * p_e )
605 {
606     VLC_UNUSED(p_e);
607     return p_md ? p_md->p_user_data : NULL;
608 }