]> git.sesse.net Git - vlc/blob - src/control/media_descriptor.c
* Protect input item's meta through setters and getters. That allows tracking of...
[vlc] / 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_md )
35 {
36     /* XXX: need some locking here */
37     if (!p_md->b_preparsed)
38     {
39         input_Preparse( p_md->p_libvlc_instance->p_libvlc_int,
40                         p_md->p_input_item );
41         p_md->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_md;
55
56     p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, psz_mrl );
57
58     if (!p_input_item)
59     {
60         libvlc_exception_raise( p_e, "Can't create md's input_item" );
61         return NULL;
62     }
63
64     p_md = malloc( sizeof(libvlc_media_descriptor_t) );
65     p_md->p_libvlc_instance = p_instance;
66     p_md->p_input_item      = p_input_item;
67     p_md->b_preparsed       = VLC_FALSE;
68     p_md->i_refcount        = 1;
69  
70     vlc_gc_incref( p_md->p_input_item );
71
72     return p_md;
73 }
74
75 /**************************************************************************
76  * Create a new media descriptor object from an input_item
77  * (libvlc internal)
78  **************************************************************************/
79 libvlc_media_descriptor_t * libvlc_media_descriptor_new_from_input_item(
80                                    libvlc_instance_t *p_instance,
81                                    input_item_t *p_input_item,
82                                    libvlc_exception_t *p_e )
83 {
84     libvlc_media_descriptor_t * p_md;
85
86     if (!p_input_item)
87     {
88         libvlc_exception_raise( p_e, "No input item given" );
89         return NULL;
90     }
91
92     p_md = malloc( sizeof(libvlc_media_descriptor_t) );
93     p_md->p_libvlc_instance = p_instance;
94     p_md->p_input_item      = p_input_item;
95     p_md->b_preparsed       = VLC_TRUE;
96     p_md->i_refcount        = 1;
97
98     vlc_gc_incref( p_md->p_input_item );
99
100     return p_md;
101 }
102
103 /**************************************************************************
104  * Delete a media descriptor object
105  **************************************************************************/
106 void libvlc_media_descriptor_release( libvlc_media_descriptor_t *p_md )
107 {
108     if (!p_md)
109         return;
110
111     p_md->i_refcount--;
112
113     /* XXX: locking */
114     vlc_gc_decref( p_md->p_input_item );
115
116     if( p_md->i_refcount > 0 )
117         return;
118     
119     free( p_md );
120 }
121
122 /**************************************************************************
123  * Retain a media descriptor object
124  **************************************************************************/
125 void libvlc_media_descriptor_retain( libvlc_media_descriptor_t *p_md )
126 {
127     if (!p_md)
128         return;
129
130     p_md->i_refcount++;
131
132     /* XXX: locking */
133     vlc_gc_incref( p_md->p_input_item );
134 }
135
136 /**************************************************************************
137  * Duplicate a media descriptor object
138  **************************************************************************/
139 libvlc_media_descriptor_t *
140 libvlc_media_descriptor_duplicate( libvlc_media_descriptor_t *p_md_orig )
141 {
142     libvlc_media_descriptor_t * p_md;
143
144     p_md = malloc( sizeof(libvlc_media_descriptor_t) );
145     memcpy( p_md, p_md_orig, sizeof(libvlc_media_descriptor_t) );
146
147     vlc_gc_incref( p_md->p_input_item );
148
149     return p_md;
150 }
151
152 /**************************************************************************
153  * Retain a media descriptor object
154  **************************************************************************/
155 char *
156 libvlc_media_descriptor_get_mrl( libvlc_media_descriptor_t * p_md,
157                                  libvlc_exception_t * p_e )
158 {
159     (void)p_e;
160     return strdup( p_md->p_input_item->psz_uri );
161 }
162
163 /**************************************************************************
164  * Getter for meta information
165  **************************************************************************/
166 static const vlc_meta_type_t meta_conversion[] =
167 {
168     [libvlc_meta_Artist]       = vlc_meta_Artist,
169     [libvlc_meta_Genre]        = vlc_meta_Genre,
170     [libvlc_meta_Copyright]    = vlc_meta_Copyright,
171     [libvlc_meta_Album]        = vlc_meta_Album,
172     [libvlc_meta_TrackNumber]  = vlc_meta_TrackNumber,
173     [libvlc_meta_Description]  = vlc_meta_Description,
174     [libvlc_meta_Rating]       = vlc_meta_Rating,
175     [libvlc_meta_Date]         = vlc_meta_Date,
176     [libvlc_meta_Settings]     = vlc_meta_Setting,
177     [libvlc_meta_URL]          = vlc_meta_URL,
178     [libvlc_meta_Language]     = vlc_meta_Language,
179     [libvlc_meta_NowPlaying]   = vlc_meta_NowPlaying,
180     [libvlc_meta_Publisher]    = vlc_meta_Publisher,
181     [libvlc_meta_EncodedBy]    = vlc_meta_EncodedBy,
182     [libvlc_meta_ArtworkURL]   = vlc_meta_ArtworkURL,
183     [libvlc_meta_TrackID]      = vlc_meta_TrackID
184 };
185
186 char * libvlc_media_descriptor_get_meta( libvlc_media_descriptor_t *p_md,
187                                          libvlc_meta_t e_meta,
188                                          libvlc_exception_t *p_e )
189 {
190     const char * psz_meta;
191
192     /* XXX: locking */
193
194     preparse_if_needed( p_md );
195
196     psz_meta = input_item_GetMeta( p_md->p_input_item, meta_conversion[e_meta] );
197
198     if( !psz_meta )
199         return NULL;
200
201     return strdup( psz_meta );
202 }
203