ecd3c52680a4417f135a8990e02c9cb778411fc8
[vlc.git] / src / control / media_descriptor.c
1 /*****************************************************************************
2  * media_descriptor.c: Libvlc API media descriport 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
31 /**************************************************************************
32  * Preparse if not already done (Private)
33  **************************************************************************/
34 static void preparse_if_needed( libvlc_media_descriptor_t *p_media_desc )
35 {
36     /* XXX: need some locking here */
37     if (!p_media_desc->b_preparsed)
38     {
39         input_Preparse( p_media_desc->p_libvlc_instance->p_libvlc_int,
40                         p_media_desc->p_input_item );
41         p_media_desc->b_preparsed = VLC_TRUE;
42     }
43 }
44
45 /**************************************************************************
46  * Create a new media descriptor object
47  **************************************************************************/
48 libvlc_media_descriptor_t * libvlc_media_descriptor_new(
49                                    libvlc_instance_t *p_instance,
50                                    const char * psz_mrl,
51                                    libvlc_exception_t *p_e )
52 {
53     input_item_t * p_input_item;
54     libvlc_media_descriptor_t * p_media_desc;
55
56     p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, psz_mrl );
57
58     if (!p_input_item)
59         return NULL; /* XXX: throw an exception */
60
61     p_media_desc = malloc( sizeof(libvlc_media_descriptor_t) );
62     p_media_desc->p_libvlc_instance = p_instance;
63     p_media_desc->p_input_item      = p_input_item;
64     p_media_desc->b_preparsed       = VLC_FALSE;
65
66     return p_media_desc;
67 }
68
69 /**************************************************************************
70  * Create a new media descriptor object from an input_item
71  * (libvlc internal)
72  **************************************************************************/
73 libvlc_media_descriptor_t * libvlc_media_descriptor_new_from_input_item(
74                                    libvlc_instance_t *p_instance,
75                                    input_item_t *p_input_item )
76 {
77     libvlc_media_descriptor_t * p_media_desc;
78
79     if (!p_input_item)
80         return NULL; /* XXX: throw an exception */
81
82     p_media_desc = malloc( sizeof(libvlc_media_descriptor_t) );
83     p_media_desc->p_libvlc_instance = p_instance;
84     p_media_desc->p_input_item      = p_input_item;
85     p_media_desc->b_preparsed       = VLC_TRUE;
86
87     return p_media_desc;
88 }
89
90 /**************************************************************************
91  * Delete a media descriptor object
92  **************************************************************************/
93 void libvlc_media_descriptor_destroy( libvlc_media_descriptor_t *p_md )
94 {
95     if (!p_md)
96         return;
97
98     /* XXX: locking */
99     input_ItemClean( p_md->p_input_item );
100
101     free( p_md );
102 }
103
104 /**************************************************************************
105  * Delete a media descriptor object
106  **************************************************************************/
107 libvlc_media_descriptor_t *
108 libvlc_media_descriptor_duplicate( libvlc_media_descriptor_t *p_md_orig )
109 {
110     libvlc_media_descriptor_t * p_md;
111
112     p_md = malloc( sizeof(libvlc_media_descriptor_t) );
113     memcpy( p_md_orig, p_md, sizeof(libvlc_media_descriptor_t) );
114
115     return p_md;
116 }
117
118 /**************************************************************************
119  * Getter for meta information
120  **************************************************************************/
121 static const int meta_conversion[] =
122 {
123     [libvlc_meta_Title]  = 0, /* Offset in the vlc_meta_t structure */
124     [libvlc_meta_Artist] = 1
125 };
126
127 char * libvlc_media_descriptor_get_meta( libvlc_media_descriptor_t *p_meta_desc,
128                                          libvlc_meta_t e_meta,
129                                          libvlc_exception_t *p_e )
130 {
131     char ** ppsz_meta;
132
133     /* XXX: locking */
134
135     preparse_if_needed( p_meta_desc );
136
137     ppsz_meta = (char**)p_meta_desc->p_input_item->p_meta;
138
139     return strdup( ppsz_meta[ meta_conversion[e_meta] ] );
140 }