control/media_descriptor.c: Use the default input name.
[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 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
31 {
32     [libvlc_meta_Title]        = vlc_meta_Title,
33     [libvlc_meta_Artist]       = vlc_meta_Artist,
34     [libvlc_meta_Genre]        = vlc_meta_Genre,
35     [libvlc_meta_Copyright]    = vlc_meta_Copyright,
36     [libvlc_meta_Album]        = vlc_meta_Album,
37     [libvlc_meta_TrackNumber]  = vlc_meta_TrackNumber,
38     [libvlc_meta_Description]  = vlc_meta_Description,
39     [libvlc_meta_Rating]       = vlc_meta_Rating,
40     [libvlc_meta_Date]         = vlc_meta_Date,
41     [libvlc_meta_Setting]      = vlc_meta_Setting,
42     [libvlc_meta_URL]          = vlc_meta_URL,
43     [libvlc_meta_Language]     = vlc_meta_Language,
44     [libvlc_meta_NowPlaying]   = vlc_meta_NowPlaying,
45     [libvlc_meta_Publisher]    = vlc_meta_Publisher,
46     [libvlc_meta_EncodedBy]    = vlc_meta_EncodedBy,
47     [libvlc_meta_ArtworkURL]   = vlc_meta_ArtworkURL,
48     [libvlc_meta_TrackID]      = vlc_meta_TrackID
49 };
50
51 static const libvlc_meta_t vlc_to_libvlc_meta[] =
52 {
53     [vlc_meta_Title]        = libvlc_meta_Title,
54     [vlc_meta_Artist]       = libvlc_meta_Artist,
55     [vlc_meta_Genre]        = libvlc_meta_Genre,
56     [vlc_meta_Copyright]    = libvlc_meta_Copyright,
57     [vlc_meta_Album]        = libvlc_meta_Album,
58     [vlc_meta_TrackNumber]  = libvlc_meta_TrackNumber,
59     [vlc_meta_Description]  = libvlc_meta_Description,
60     [vlc_meta_Rating]       = libvlc_meta_Rating,
61     [vlc_meta_Date]         = libvlc_meta_Date,
62     [vlc_meta_Setting]      = libvlc_meta_Setting,
63     [vlc_meta_URL]          = libvlc_meta_URL,
64     [vlc_meta_Language]     = libvlc_meta_Language,
65     [vlc_meta_NowPlaying]   = libvlc_meta_NowPlaying,
66     [vlc_meta_Publisher]    = libvlc_meta_Publisher,
67     [vlc_meta_EncodedBy]    = libvlc_meta_EncodedBy,
68     [vlc_meta_ArtworkURL]   = libvlc_meta_ArtworkURL,
69     [vlc_meta_TrackID]      = libvlc_meta_TrackID
70 };
71
72 /**************************************************************************
73  * input_item_subitem_added (Private) (vlc event Callback)
74  **************************************************************************/
75 static void input_item_subitem_added( const vlc_event_t *p_event,
76                                      void * user_data )
77 {
78     libvlc_media_descriptor_t * p_md = user_data;
79     libvlc_media_descriptor_t * p_md_child;
80     libvlc_event_t event;
81
82     p_md_child = libvlc_media_descriptor_new_from_input_item(
83                 p_md->p_libvlc_instance, 
84                 p_event->u.input_item_subitem_added.p_new_child, NULL );
85
86     /* Construct the event */
87     event.type = libvlc_MediaDescriptorSubItemAdded;
88     event.u.media_descriptor_subitem_added.new_child = p_md_child;
89
90     /* Send the event */
91     libvlc_event_send( p_md->p_event_manager, &event );
92     libvlc_media_descriptor_release( p_md_child );
93 }
94
95 /**************************************************************************
96  * input_item_meta_changed (Private) (vlc event Callback)
97  **************************************************************************/
98 static void input_item_meta_changed( const vlc_event_t *p_event,
99                                      void * user_data )
100 {
101     libvlc_media_descriptor_t * p_md = user_data;
102     libvlc_event_t event;
103
104     /* Construct the event */
105     event.type = libvlc_MediaDescriptorMetaChanged;
106     event.u.media_descriptor_meta_changed.meta_type =
107         vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
108
109     /* Send the event */
110     libvlc_event_send( p_md->p_event_manager, &event );
111 }
112
113
114 /**************************************************************************
115  * Install event handler (Private)
116  **************************************************************************/
117 static void install_input_item_observer( libvlc_media_descriptor_t *p_md )
118 {
119     vlc_event_attach( &p_md->p_input_item->event_manager,
120                       vlc_InputItemSubItemAdded,
121                       input_item_subitem_added,
122                       p_md );
123     vlc_event_attach( &p_md->p_input_item->event_manager,
124                       vlc_InputItemMetaChanged,
125                       input_item_meta_changed,
126                       p_md );
127 }
128
129 /**************************************************************************
130  * Uninstall event handler (Private)
131  **************************************************************************/
132 static void uninstall_input_item_observer( libvlc_media_descriptor_t *p_md )
133 {
134     vlc_event_detach( &p_md->p_input_item->event_manager,
135                       vlc_InputItemSubItemAdded,
136                       input_item_subitem_added,
137                       p_md );
138     vlc_event_detach( &p_md->p_input_item->event_manager,
139                       vlc_InputItemMetaChanged,
140                       input_item_meta_changed,
141                       p_md );
142 }
143
144 /**************************************************************************
145  * Preparse if not already done (Private)
146  **************************************************************************/
147 static void preparse_if_needed( libvlc_media_descriptor_t *p_md )
148 {
149     /* XXX: need some locking here */
150     if (!p_md->b_preparsed)
151     {
152         input_Preparse( p_md->p_libvlc_instance->p_libvlc_int,
153                         p_md->p_input_item );
154         p_md->b_preparsed = VLC_TRUE;
155     }
156 }
157
158 /**************************************************************************
159  * Create a new media descriptor object from an input_item
160  * (libvlc internal)
161  * That's the generic constructor
162  **************************************************************************/
163 libvlc_media_descriptor_t * libvlc_media_descriptor_new_from_input_item(
164                                    libvlc_instance_t *p_instance,
165                                    input_item_t *p_input_item,
166                                    libvlc_exception_t *p_e )
167 {
168     libvlc_media_descriptor_t * p_md;
169
170     if (!p_input_item)
171     {
172         libvlc_exception_raise( p_e, "No input item given" );
173         return NULL;
174     }
175
176     p_md = malloc( sizeof(libvlc_media_descriptor_t) );
177     p_md->p_libvlc_instance = p_instance;
178     p_md->p_input_item      = p_input_item;
179     p_md->b_preparsed       = VLC_TRUE;
180     p_md->i_refcount        = 1;
181     p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance, p_e );
182
183     libvlc_event_manager_register_event_type( p_md->p_event_manager, 
184         libvlc_MediaDescriptorMetaChanged, p_e );
185     libvlc_event_manager_register_event_type( p_md->p_event_manager, 
186         libvlc_MediaDescriptorSubItemAdded, p_e );
187
188     vlc_gc_incref( p_md->p_input_item );
189
190     install_input_item_observer( p_md );
191
192     return p_md;
193 }
194
195 /**************************************************************************
196  * Create a new media descriptor object
197  **************************************************************************/
198 libvlc_media_descriptor_t * libvlc_media_descriptor_new(
199                                    libvlc_instance_t *p_instance,
200                                    const char * psz_mrl,
201                                    libvlc_exception_t *p_e )
202 {
203     input_item_t * p_input_item;
204     libvlc_media_descriptor_t * p_md;
205
206     p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, NULL );
207
208     if (!p_input_item)
209     {
210         libvlc_exception_raise( p_e, "Can't create md's input_item" );
211         return NULL;
212     }
213
214     p_md = libvlc_media_descriptor_new_from_input_item( p_instance,
215                 p_input_item, p_e );
216
217     return p_md;
218 }
219
220 /**************************************************************************
221  * Delete a media descriptor object
222  **************************************************************************/
223 void libvlc_media_descriptor_release( libvlc_media_descriptor_t *p_md )
224 {
225     if (!p_md)
226         return;
227
228     p_md->i_refcount--;
229
230     if( p_md->i_refcount > 0 )
231         return;
232
233     uninstall_input_item_observer( p_md );
234     vlc_gc_decref( p_md->p_input_item );
235
236     free( p_md );
237 }
238
239 /**************************************************************************
240  * Retain a media descriptor object
241  **************************************************************************/
242 void libvlc_media_descriptor_retain( libvlc_media_descriptor_t *p_md )
243 {
244     if (!p_md)
245         return;
246
247     p_md->i_refcount++;
248 }
249
250 /**************************************************************************
251  * Duplicate a media descriptor object
252  **************************************************************************/
253 libvlc_media_descriptor_t *
254 libvlc_media_descriptor_duplicate( libvlc_media_descriptor_t *p_md_orig )
255 {
256     return libvlc_media_descriptor_new_from_input_item(
257         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
258 }
259
260 /**************************************************************************
261  * Retain a media descriptor object
262  **************************************************************************/
263 char *
264 libvlc_media_descriptor_get_mrl( libvlc_media_descriptor_t * p_md,
265                                  libvlc_exception_t * p_e )
266 {
267     (void)p_e;
268     return strdup( p_md->p_input_item->psz_uri );
269 }
270
271 /**************************************************************************
272  * Getter for meta information
273  **************************************************************************/
274
275 char * libvlc_media_descriptor_get_meta( libvlc_media_descriptor_t *p_md,
276                                          libvlc_meta_t e_meta,
277                                          libvlc_exception_t *p_e )
278 {
279     char * psz_meta;
280
281     /* XXX: locking */
282
283     preparse_if_needed( p_md );
284
285     psz_meta = input_item_GetMeta( p_md->p_input_item,
286                                    libvlc_to_vlc_meta[e_meta] );
287
288     /* Should be integrated in core */
289     if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
290     {
291         free( psz_meta );
292         return strdup( p_md->p_input_item->psz_name );
293     }
294
295     return psz_meta;
296 }
297
298 /**************************************************************************
299  * Getter for meta information
300  **************************************************************************/
301 typedef char * libvlc_tag_t;
302 void libvlc_media_descriptor_set_tag( libvlc_media_descriptor_t *p_md,
303                                         const libvlc_tag_t tag,
304                                         libvlc_exception_t *p_e )
305 {
306
307 }
308