]> git.sesse.net Git - vlc/blob - modules/demux/playlist/qtl.c
Add a bunch of helper functions/macros and start using them:
[vlc] / modules / demux / playlist / qtl.c
1 /*****************************************************************************
2  * qtl.c: QuickTime Media Link Importer
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -@t- videolan -Dot- 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 /*
25 See
26 http://developer.apple.com/documentation/QuickTime/QT6WhatsNew/Chap1/chapter_1_section_54.html
27 and
28 http://developer.apple.com/documentation/QuickTime/WhatsNewQT5/QT5NewChapt1/chapter_1_section_39.html
29
30 autoplay - true/false
31 controller - true/false
32 fullscreen - normal/double/half/current/full
33 href - url
34 kioskmode - true/false
35 loop - true/false/palindrome
36 movieid - integer
37 moviename - string
38 playeveryframe - true/false
39 qtnext - url
40 quitwhendone - true/false
41 src - url (required)
42 type - mime type
43 volume - 0 (mute) - 100 (max)
44
45 */
46
47 /*****************************************************************************
48  * Preamble
49  *****************************************************************************/
50 #include <ctype.h>                                              /* isspace() */
51
52 #include <vlc/vlc.h>
53 #include <vlc/input.h>
54 #include <vlc/intf.h>
55
56 #include "playlist.h"
57 #include "vlc_xml.h"
58
59 struct demux_sys_t
60 {
61     playlist_t *p_playlist;
62     playlist_item_t *p_current;
63     playlist_item_t *p_item_in_category;
64     int i_parent_id;
65
66     xml_t *p_xml;
67     xml_reader_t *p_xml_reader;
68 };
69
70 typedef enum { FULLSCREEN_NORMAL,
71                FULLSCREEN_DOUBLE,
72                FULLSCREEN_HALF,
73                FULLSCREEN_CURRENT,
74                FULLSCREEN_FULL } qtl_fullscreen_t;
75 char* ppsz_fullscreen[] = { "normal", "double", "half", "current", "full" };
76 typedef enum { LOOP_TRUE,
77                LOOP_FALSE,
78                LOOP_PALINDROME } qtl_loop_t;
79 char* ppsz_loop[] = { "true", "false", "palindrome" };
80
81 /*****************************************************************************
82  * Local prototypes
83  *****************************************************************************/
84 static int Demux( demux_t *p_demux);
85 static int Control( demux_t *p_demux, int i_query, va_list args );
86
87 /*****************************************************************************
88  * Import_QTL: main import function
89  *****************************************************************************/
90 int E_(Import_QTL)( vlc_object_t *p_this )
91 {
92     DEMUX_BY_EXTENSION_MSG( ".qtl", "using QuickTime Media Link reader" );
93     p_demux->p_sys->p_playlist = NULL;
94     p_demux->p_sys->p_xml = NULL;
95     p_demux->p_sys->p_xml_reader = NULL;
96     return VLC_SUCCESS;
97 }
98
99 /*****************************************************************************
100  * Deactivate: frees unused data
101  *****************************************************************************/
102 void E_(Close_QTL)( vlc_object_t *p_this )
103 {
104     demux_t *p_demux = (demux_t *)p_this;
105     demux_sys_t *p_sys = p_demux->p_sys;
106
107     if( p_sys->p_playlist )
108         vlc_object_release( p_sys->p_playlist );
109     if( p_sys->p_xml_reader )
110         xml_ReaderDelete( p_sys->p_xml, p_sys->p_xml_reader );
111     if( p_sys->p_xml )
112         xml_Delete( p_sys->p_xml );
113     free( p_sys );
114 }
115
116 #define FREE( a ) if( a ) free( a );
117
118 static int Demux( demux_t *p_demux )
119 {
120     demux_sys_t *p_sys = p_demux->p_sys;
121     xml_t *p_xml;
122     xml_reader_t *p_xml_reader;
123     char *psz_eltname = NULL;
124
125     /* List of all possible attributes. The only required one is "src" */
126     vlc_bool_t b_autoplay = VLC_FALSE;
127     vlc_bool_t b_controler = VLC_TRUE;
128     qtl_fullscreen_t fullscreen = VLC_FALSE;
129     char *psz_href = NULL;
130     vlc_bool_t b_kioskmode = VLC_FALSE;
131     qtl_loop_t loop = LOOP_FALSE;
132     int i_movieid = -1;
133     char *psz_moviename = NULL;
134     vlc_bool_t b_playeveryframe = VLC_FALSE;
135     char *psz_qtnext = NULL;
136     vlc_bool_t b_quitwhendone = VLC_FALSE;
137     char *psz_src = NULL;
138     char *psz_mimetype = NULL;
139     int i_volume = 100;
140
141     INIT_PLAYLIST_STUFF;
142
143     p_sys->p_playlist = p_playlist;
144     p_sys->p_current = p_current;
145     p_sys->i_parent_id = i_parent_id;
146     p_sys->p_item_in_category = p_item_in_category;
147
148     p_xml = p_sys->p_xml = xml_Create( p_demux );
149     if( !p_xml ) return -1;
150
151     p_xml_reader = xml_ReaderCreate( p_xml, p_demux->s );
152     if( !p_xml_reader ) return -1;
153     p_sys->p_xml_reader = p_xml_reader;
154
155     /* check root node */
156     if( xml_ReaderRead( p_xml_reader ) != 1 )
157     {
158         msg_Err( p_demux, "invalid file (no root node)" );
159         return -1;
160     }
161
162     if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
163         ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL ||
164         strcmp( psz_eltname, "embed" ) )
165     {
166         msg_Err( p_demux, "invalid root node %i, %s",
167                  xml_ReaderNodeType( p_xml_reader ), psz_eltname );
168         FREE( psz_eltname );
169
170         /* second line has <?quicktime tag ... so we try to skip it */
171         msg_Dbg( p_demux, "trying to read one more node" );
172         xml_ReaderRead( p_xml_reader );
173         if( xml_ReaderNodeType( p_xml_reader ) != XML_READER_STARTELEM ||
174             ( psz_eltname = xml_ReaderName( p_xml_reader ) ) == NULL ||
175             strcmp( psz_eltname, "embed" ) )
176         {
177             msg_Err( p_demux, "invalid root node %i, %s",
178                      xml_ReaderNodeType( p_xml_reader ), psz_eltname );
179             FREE( psz_eltname );
180             return -1;
181         }
182     }
183     FREE( psz_eltname );
184
185     while( xml_ReaderNextAttr( p_sys->p_xml_reader ) == VLC_SUCCESS )
186     {
187         char *psz_attrname = xml_ReaderName( p_sys->p_xml_reader );
188         char *psz_attrvalue = xml_ReaderValue( p_sys->p_xml_reader );
189
190         if( !psz_attrname || !psz_attrvalue )
191         {
192             FREE( psz_attrname );
193             FREE( psz_attrvalue );
194             return -1;
195         }
196
197         if( !strcmp( psz_attrname, "autoplay" ) )
198         {
199             if( !strcmp( psz_attrvalue, "true" ) )
200             {
201                 b_autoplay = VLC_TRUE;
202             }
203             else
204             {
205                 b_autoplay = VLC_FALSE;
206             }
207         }
208         else if( !strcmp( psz_attrname, "controler" ) )
209         {
210             if( !strcmp( psz_attrvalue, "false" ) )
211             {
212                 b_controler = VLC_FALSE;
213             }
214             else
215             {
216                 b_controler = VLC_TRUE;
217             }
218         }
219         else if( !strcmp( psz_attrname, "fullscreen" ) )
220         {
221             if( !strcmp( psz_attrvalue, "double" ) )
222             {
223                 fullscreen = FULLSCREEN_DOUBLE;
224             }
225             else if( !strcmp( psz_attrvalue, "half" ) )
226             {
227                 fullscreen = FULLSCREEN_HALF;
228             }
229             else if( !strcmp( psz_attrvalue, "current" ) )
230             {
231                 fullscreen = FULLSCREEN_CURRENT;
232             }
233             else if( !strcmp( psz_attrvalue, "full" ) )
234             {
235                 fullscreen = FULLSCREEN_FULL;
236             }
237             else
238             {
239                 fullscreen = FULLSCREEN_NORMAL;
240             }
241         }
242         else if( !strcmp( psz_attrname, "href" ) )
243         {
244             psz_href = psz_attrvalue;
245             psz_attrvalue = NULL;
246         }
247         else if( !strcmp( psz_attrname, "kioskmode" ) )
248         {
249             if( !strcmp( psz_attrvalue, "true" ) )
250             {
251                 b_kioskmode = VLC_TRUE;
252             }
253             else
254             {
255                 b_kioskmode = VLC_FALSE;
256             }
257         }
258         else if( !strcmp( psz_attrname, "loop" ) )
259         {
260             if( !strcmp( psz_attrvalue, "true" ) )
261             {
262                 loop = LOOP_TRUE;
263             }
264             else if( !strcmp( psz_attrvalue, "palindrome" ) )
265             {
266                 loop = LOOP_PALINDROME;
267             }
268             else
269             {
270                 loop = LOOP_FALSE;
271             }
272         }
273         else if( !strcmp( psz_attrname, "movieid" ) )
274         {
275             i_movieid = atoi( psz_attrvalue );
276         }
277         else if( !strcmp( psz_attrname, "moviename" ) )
278         {
279             psz_moviename = psz_attrvalue;
280             psz_attrvalue = NULL;
281         }
282         else if( !strcmp( psz_attrname, "playeveryframe" ) )
283         {
284             if( !strcmp( psz_attrvalue, "true" ) )
285             {
286                 b_playeveryframe = VLC_TRUE;
287             }
288             else
289             {
290                 b_playeveryframe = VLC_FALSE;
291             }
292         }
293         else if( !strcmp( psz_attrname, "qtnext" ) )
294         {
295             psz_qtnext = psz_attrvalue;
296             psz_attrvalue = NULL;
297         }
298         else if( !strcmp( psz_attrname, "quitwhendone" ) )
299         {
300             if( !strcmp( psz_attrvalue, "true" ) )
301             {
302                 b_quitwhendone = VLC_TRUE;
303             }
304             else
305             {
306                 b_quitwhendone = VLC_FALSE;
307             }
308         }
309         else if( !strcmp( psz_attrname, "src" ) )
310         {
311             psz_src = psz_attrvalue;
312             psz_attrvalue = NULL;
313         }
314         else if( !strcmp( psz_attrname, "mimetype" ) )
315         {
316             psz_mimetype = psz_attrvalue;
317             psz_attrvalue = NULL;
318         }
319         else if( !strcmp( psz_attrname, "volume" ) )
320         {
321             i_volume = atoi( psz_attrvalue );
322         }
323         else
324         {
325             msg_Dbg( p_demux, "Attribute %s with value %s isn't valid",
326                      psz_attrname, psz_attrvalue );
327         }
328         FREE( psz_attrname );
329         FREE( psz_attrvalue );
330     }
331
332     msg_Dbg( p_demux, "autoplay: %s (unused by VLC)",
333              b_autoplay==VLC_TRUE ? "true": "false" );
334     msg_Dbg( p_demux, "controler: %s (unused by VLC)",
335              b_controler==VLC_TRUE?"true": "false" );
336     msg_Dbg( p_demux, "fullscreen: %s (unused by VLC)",
337              ppsz_fullscreen[fullscreen] );
338     msg_Dbg( p_demux, "href: %s", psz_href );
339     msg_Dbg( p_demux, "kioskmode: %s (unused by VLC)",
340              b_kioskmode==VLC_TRUE?"true":"false" );
341     msg_Dbg( p_demux, "loop: %s (unused by VLC)", ppsz_loop[loop] );
342     msg_Dbg( p_demux, "movieid: %d (unused by VLC)", i_movieid );
343     msg_Dbg( p_demux, "moviename: %s", psz_moviename );
344     msg_Dbg( p_demux, "playeverframe: %s (unused by VLC)",
345              b_playeveryframe==VLC_TRUE?"true":"false" );
346     msg_Dbg( p_demux, "qtnext: %s", psz_qtnext );
347     msg_Dbg( p_demux, "quitwhendone: %s (unused by VLC)",
348              b_quitwhendone==VLC_TRUE?"true":"false" );
349     msg_Dbg( p_demux, "src: %s", psz_src );
350     msg_Dbg( p_demux, "mimetype: %s", psz_mimetype );
351     msg_Dbg( p_demux, "volume: %d (unused by VLC)", i_volume );
352
353
354     if( !psz_src )
355     {
356         msg_Err( p_demux, "Mandatory attribute 'src' not found" );
357     }
358     else
359     {
360         p_input = input_ItemNewExt( p_sys->p_playlist,
361                                 psz_src, psz_moviename, 0, NULL, -1 );
362 #define SADD_INFO( type, field ) if( field ) { vlc_input_item_AddInfo( \
363                     p_input, "QuickTime Media Link", _(type), "%s", field ) ; }
364         SADD_INFO( "href", psz_href );
365         SADD_INFO( "mime type", psz_mimetype );
366         playlist_AddWhereverNeeded( p_sys->p_playlist, p_input,
367                             p_sys->p_current, p_sys->p_item_in_category,
368                             (p_sys->i_parent_id > 0 ) ? VLC_TRUE: VLC_FALSE,
369                             PLAYLIST_APPEND );
370
371         if( psz_qtnext )
372         {
373             p_input = input_ItemNewExt( p_sys->p_playlist,
374                                         psz_qtnext, psz_qtnext, 0, NULL, -1 );
375             playlist_AddWhereverNeeded( p_sys->p_playlist, p_input,
376                             p_sys->p_current, p_sys->p_item_in_category,
377                             (p_sys->i_parent_id > 0 ) ? VLC_TRUE: VLC_FALSE,
378                             PLAYLIST_APPEND );
379         }
380     }
381
382     HANDLE_PLAY_AND_RELEASE;
383
384     p_sys->p_playlist = NULL;
385
386     FREE( psz_href );
387     FREE( psz_moviename );
388     FREE( psz_qtnext );
389     FREE( psz_src );
390     FREE( psz_mimetype );
391
392     return VLC_SUCCESS;
393 }
394
395 static int Control( demux_t *p_demux, int i_query, va_list args )
396 {
397     return VLC_EGENERIC;
398 }