src/control/media_descriptor.c: Forward vlc_InputItemMetaChanged events.
[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_meta_changed (Private) (vlc event Callback)
74  **************************************************************************/
75 static void input_item_meta_changed( const vlc_event_t *p_event,
76                                      void * user_data )
77 {
78     libvlc_media_descriptor_t * p_md = user_data;
79     libvlc_event_t event;
80
81     /* Construct the event */
82     event.type = libvlc_MediaDescriptorMetaChanged;
83     event.u.media_descriptor_meta_changed.meta_type =
84         vlc_to_libvlc_meta[p_event->u.input_item_meta_changed.meta_type];
85
86     /* Send the event */
87     libvlc_event_send( p_md->p_event_manager, &event );
88 }
89
90
91 /**************************************************************************
92  * Install event handler (Private)
93  **************************************************************************/
94 static void install_input_item_observer( libvlc_media_descriptor_t *p_md )
95 {
96     vlc_event_attach( &p_md->p_input_item->event_manager,
97                       vlc_InputItemMetaChanged,
98                       input_item_meta_changed,
99                       p_md );
100 }
101
102 /**************************************************************************
103  * Uninstall event handler (Private)
104  **************************************************************************/
105 static void uninstall_input_item_observer( libvlc_media_descriptor_t *p_md )
106 {
107     vlc_event_detach( &p_md->p_input_item->event_manager,
108                       vlc_InputItemMetaChanged,
109                       input_item_meta_changed,
110                       p_md );
111 }
112
113 /**************************************************************************
114  * Preparse if not already done (Private)
115  **************************************************************************/
116 static void preparse_if_needed( libvlc_media_descriptor_t *p_md )
117 {
118     /* XXX: need some locking here */
119     if (!p_md->b_preparsed)
120     {
121         input_Preparse( p_md->p_libvlc_instance->p_libvlc_int,
122                         p_md->p_input_item );
123         p_md->b_preparsed = VLC_TRUE;
124     }
125 }
126
127 /**************************************************************************
128  * Create a new media descriptor object from an input_item
129  * (libvlc internal)
130  * That's the generic constructor
131  **************************************************************************/
132 libvlc_media_descriptor_t * libvlc_media_descriptor_new_from_input_item(
133                                    libvlc_instance_t *p_instance,
134                                    input_item_t *p_input_item,
135                                    libvlc_exception_t *p_e )
136 {
137     libvlc_media_descriptor_t * p_md;
138
139     if (!p_input_item)
140     {
141         libvlc_exception_raise( p_e, "No input item given" );
142         return NULL;
143     }
144
145     p_md = malloc( sizeof(libvlc_media_descriptor_t) );
146     p_md->p_libvlc_instance = p_instance;
147     p_md->p_input_item      = p_input_item;
148     p_md->b_preparsed       = VLC_TRUE;
149     p_md->i_refcount        = 1;
150     p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance, p_e );
151     libvlc_event_manager_register_event_type( p_md->p_event_manager, 
152         libvlc_MediaDescriptorMetaChanged, p_e );
153     vlc_gc_incref( p_md->p_input_item );
154
155     install_input_item_observer( p_md );
156
157     return p_md;
158 }
159
160 /**************************************************************************
161  * Create a new media descriptor object
162  **************************************************************************/
163 libvlc_media_descriptor_t * libvlc_media_descriptor_new(
164                                    libvlc_instance_t *p_instance,
165                                    const char * psz_mrl,
166                                    libvlc_exception_t *p_e )
167 {
168     input_item_t * p_input_item;
169     libvlc_media_descriptor_t * p_md;
170
171     p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, psz_mrl );
172
173     if (!p_input_item)
174     {
175         libvlc_exception_raise( p_e, "Can't create md's input_item" );
176         return NULL;
177     }
178
179     p_md = libvlc_media_descriptor_new_from_input_item( p_instance,
180                 p_input_item, p_e );
181
182     return p_md;
183 }
184
185 /**************************************************************************
186  * Delete a media descriptor object
187  **************************************************************************/
188 void libvlc_media_descriptor_release( libvlc_media_descriptor_t *p_md )
189 {
190     if (!p_md)
191         return;
192
193     p_md->i_refcount--;
194
195     if( p_md->i_refcount > 0 )
196         return;
197
198     uninstall_input_item_observer( p_md );
199     vlc_gc_decref( p_md->p_input_item );
200
201     free( p_md );
202 }
203
204 /**************************************************************************
205  * Retain a media descriptor object
206  **************************************************************************/
207 void libvlc_media_descriptor_retain( libvlc_media_descriptor_t *p_md )
208 {
209     if (!p_md)
210         return;
211
212     p_md->i_refcount++;
213 }
214
215 /**************************************************************************
216  * Duplicate a media descriptor object
217  **************************************************************************/
218 libvlc_media_descriptor_t *
219 libvlc_media_descriptor_duplicate( libvlc_media_descriptor_t *p_md_orig )
220 {
221     return libvlc_media_descriptor_new_from_input_item(
222         p_md_orig->p_libvlc_instance, p_md_orig->p_input_item, NULL );
223 }
224
225 /**************************************************************************
226  * Retain a media descriptor object
227  **************************************************************************/
228 char *
229 libvlc_media_descriptor_get_mrl( libvlc_media_descriptor_t * p_md,
230                                  libvlc_exception_t * p_e )
231 {
232     (void)p_e;
233     return strdup( p_md->p_input_item->psz_uri );
234 }
235
236 /**************************************************************************
237  * Getter for meta information
238  **************************************************************************/
239
240 char * libvlc_media_descriptor_get_meta( libvlc_media_descriptor_t *p_md,
241                                          libvlc_meta_t e_meta,
242                                          libvlc_exception_t *p_e )
243 {
244     const char * psz_meta;
245
246     /* XXX: locking */
247
248     preparse_if_needed( p_md );
249
250     psz_meta = input_item_GetMeta( p_md->p_input_item,
251                                    libvlc_to_vlc_meta[e_meta] );
252
253     /* Should be integrated in core */
254     if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
255         return strdup( p_md->p_input_item->psz_name );
256
257     if( !psz_meta )
258         return NULL;
259
260     return strdup( psz_meta );
261 }
262