]> git.sesse.net Git - vlc/blob - modules/misc/lua/vlc.c
Port the http interface to the Lua Interface Module framework.
[vlc] / modules / misc / lua / vlc.c
1 /*****************************************************************************
2  * vlc.c: Generic lua inteface functions
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifndef  _GNU_SOURCE
29 #   define  _GNU_SOURCE
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_meta.h>
34 #include <vlc_charset.h>
35 #include <vlc_aout.h>
36
37 #include <lua.h>        /* Low level lua C API */
38 #include <lauxlib.h>    /* Higher level C API */
39 #include <lualib.h>     /* Lua libs */
40
41 #include "vlc.h"
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46
47 #define INTF_TEXT N_("Lua interface")
48 #define INTF_LONGTEXT N_("Lua interface module to load")
49
50 #define CONFIG_TEXT N_("Lua inteface configuration")
51 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
52
53 vlc_module_begin();
54     add_shortcut( "luameta" );
55     set_shortname( N_( "Lua Meta" ) );
56     set_description( _("Fetch metadata using lua scripts") );
57     set_capability( "meta fetcher", 10 );
58     set_callbacks( E_(FindMeta), NULL );
59     add_submodule();
60         set_shortname( N_( "Lua Art" ) );
61         set_description( _("Fetch artwork using lua scripts") );
62         set_capability( "art finder", 10 );
63         set_callbacks( E_(FindArt), NULL );
64     add_submodule();
65         add_shortcut( "luaplaylist" );
66         set_category( CAT_INPUT );
67         set_subcategory( SUBCAT_INPUT_DEMUX );
68         set_shortname( _("Lua Playlist") );
69         set_description( _("Lua Playlist Parser Interface") );
70         set_capability( "demux2", 9 );
71         set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
72     add_submodule();
73         add_shortcut( "luaintf" );
74         add_shortcut( "luarc" );
75         /* add_shortcut( "rc" ); */
76         add_shortcut( "luahotkeys" );
77         /* add_shortcut( "hotkeys" ); */
78         add_shortcut( "luatelnet" );
79         /* add_shortcut( "telnet" ); */
80         set_description( _("Lua Interface Module") );
81         set_capability( "interface", 0 );
82         add_string( "lua-intf", "dummy", NULL,
83                     INTF_TEXT, INTF_LONGTEXT, VLC_FALSE );
84         add_string( "lua-config", "", NULL,
85                     CONFIG_TEXT, CONFIG_LONGTEXT, VLC_FALSE );
86         set_callbacks( E_(Open_LuaIntf), E_(Close_LuaIntf) );
87 vlc_module_end();
88
89 /*****************************************************************************
90  * Internal lua<->vlc utils
91  *****************************************************************************/
92 vlc_object_t * vlclua_get_this( lua_State *L )
93 {
94     vlc_object_t * p_this;
95     lua_getglobal( L, "vlc" );
96     lua_getfield( L, -1, "private" );
97     p_this = (vlc_object_t*)lua_topointer( L, lua_gettop( L ) );
98     lua_pop( L, 2 );
99     return p_this;
100 }
101
102 /*****************************************************************************
103  * VLC error code translation
104  *****************************************************************************/
105 int vlclua_push_ret( lua_State *L, int i_error )
106 {
107     lua_pushnumber( L, i_error );
108     lua_pushstring( L, vlc_error( i_error ) );
109     return 2;
110 }
111
112 /*****************************************************************************
113  * Get the VLC version string
114  *****************************************************************************/
115 int vlclua_version( lua_State *L )
116 {
117     lua_pushstring( L, VLC_Version() );
118     return 1;
119 }
120
121 /*****************************************************************************
122  * Get the VLC copyright
123  *****************************************************************************/
124 int vlclua_copyright( lua_State *L )
125 {
126     lua_pushstring( L, COPYRIGHT_MESSAGE );
127     return 1;
128 }
129
130 /*****************************************************************************
131  * Get the VLC license msg/disclaimer
132  *****************************************************************************/
133 int vlclua_license( lua_State *L )
134 {
135     lua_pushstring( L, LICENSE_MSG );
136     return 1;
137 }
138
139 /*****************************************************************************
140  * Quit VLC
141  *****************************************************************************/
142 int vlclua_quit( lua_State *L )
143 {
144     vlc_object_t *p_this = vlclua_get_this( L );
145     /* The rc.c code also stops the playlist ... not sure if this is needed
146      * though. */
147     vlc_object_kill( p_this->p_libvlc );
148     return 0;
149 }
150
151 /*****************************************************************************
152  * Global properties getters
153  *****************************************************************************/
154 int vlclua_datadir( lua_State *L )
155 {
156     lua_pushstring( L, config_GetDataDir() );
157     return 1;
158 }
159 int vlclua_homedir( lua_State *L )
160 {
161     lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_homedir );
162     return 1;
163 }
164 int vlclua_configdir( lua_State *L )
165 {
166     lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_configdir );
167     return 1;
168 }
169 int vlclua_cachedir( lua_State *L )
170 {
171     lua_pushstring( L, vlclua_get_this( L )->p_libvlc->psz_cachedir );
172     return 1;
173 }
174 int vlclua_datadir_list( lua_State *L )
175 {
176     const char *psz_dirname = luaL_checkstring( L, 1 );
177     vlc_object_t *p_this = vlclua_get_this( L );
178     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
179     char **ppsz_dir = ppsz_dir_list;
180     int i = 1;
181
182     if( vlclua_dir_list( p_this, psz_dirname, ppsz_dir_list ) != VLC_SUCCESS )
183         return 0;
184     lua_newtable( L );
185     for( ; *ppsz_dir; ppsz_dir++ )
186     {
187         lua_pushstring( L, *ppsz_dir );
188         lua_rawseti( L, -2, i );
189         i ++;
190     }
191     return 1;
192 }
193
194 /*****************************************************************************
195  * Volume related
196  *****************************************************************************/
197 int vlclua_volume_set( lua_State *L )
198 {
199     vlc_object_t *p_this = vlclua_get_this( L );
200     int i_volume = luaL_checkint( L, 1 );
201     /* Do we need to check that i_volume is in the AOUT_VOLUME_MIN->MAX range?*/
202     return vlclua_push_ret( L, aout_VolumeSet( p_this, i_volume ) );
203 }
204
205 int vlclua_volume_get( lua_State *L )
206 {
207     vlc_object_t *p_this = vlclua_get_this( L );
208     audio_volume_t i_volume;
209     if( aout_VolumeGet( p_this, &i_volume ) == VLC_SUCCESS )
210         lua_pushnumber( L, i_volume );
211     else
212         lua_pushnil( L );
213     return 1;
214 }
215
216 int vlclua_volume_up( lua_State *L )
217 {
218     audio_volume_t i_volume;
219     aout_VolumeUp( vlclua_get_this( L ),
220                    luaL_optint( L, 1, 1 ),
221                    &i_volume );
222     lua_pushnumber( L, i_volume );
223     return 1;
224 }
225
226 int vlclua_volume_down( lua_State *L )
227 {
228     audio_volume_t i_volume;
229     aout_VolumeDown( vlclua_get_this( L ),
230                      luaL_optint( L, 1, 1 ),
231                      &i_volume );
232     lua_pushnumber( L, i_volume );
233     return 1;
234 }
235
236 /*****************************************************************************
237  * Stream handling
238  *****************************************************************************/
239 int vlclua_stream_new( lua_State *L )
240 {
241     vlc_object_t * p_this = vlclua_get_this( L );
242     stream_t * p_stream;
243     const char * psz_url;
244     psz_url = luaL_checkstring( L, -1 );
245     p_stream = stream_UrlNew( p_this, psz_url );
246     if( !p_stream )
247         return luaL_error( L, "Error when opening url: `%s'", psz_url );
248     lua_pushlightuserdata( L, p_stream );
249     return 1;
250 }
251
252 int vlclua_stream_read( lua_State *L )
253 {
254     stream_t * p_stream;
255     int n;
256     byte_t *p_read;
257     int i_read;
258     p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
259     n = luaL_checkint( L, 2 );
260     p_read = malloc( n );
261     if( !p_read ) return vlclua_error( L );
262     i_read = stream_Read( p_stream, p_read, n );
263     lua_pushlstring( L, (const char *)p_read, i_read );
264     free( p_read );
265     return 1;
266 }
267
268 int vlclua_stream_readline( lua_State *L )
269 {
270     stream_t * p_stream;
271     p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
272     char *psz_line = stream_ReadLine( p_stream );
273     if( psz_line )
274     {
275         lua_pushstring( L, psz_line );
276         free( psz_line );
277     }
278     else
279         lua_pushnil( L );
280     return 1;
281 }
282
283 int vlclua_stream_delete( lua_State *L )
284 {
285     stream_t * p_stream;
286     p_stream = (stream_t *)luaL_checklightuserdata( L, 1 );
287     stream_Delete( p_stream );
288     return 0;
289 }
290
291 /*****************************************************************************
292  * String transformations
293  *****************************************************************************/
294 int vlclua_decode_uri( lua_State *L )
295 {
296     int i_top = lua_gettop( L );
297     int i;
298     for( i = 1; i <= i_top; i++ )
299     {
300         const char *psz_cstring = luaL_checkstring( L, 1 );
301         char *psz_string = strdup( psz_cstring );
302         lua_remove( L, 1 ); /* remove elements to prevent being limited by
303                              * the stack's size (this function will work with
304                              * up to (stack size - 1) arguments */
305         decode_URI( psz_string );
306         lua_pushstring( L, psz_string );
307         free( psz_string );
308     }
309     return i_top;
310 }
311
312 int vlclua_resolve_xml_special_chars( lua_State *L )
313 {
314     int i_top = lua_gettop( L );
315     int i;
316     for( i = 1; i <= i_top; i++ )
317     {
318         const char *psz_cstring = luaL_checkstring( L, 1 );
319         char *psz_string = strdup( psz_cstring );
320         lua_remove( L, 1 ); /* remove elements to prevent being limited by
321                              * the stack's size (this function will work with
322                              * up to (stack size - 1) arguments */
323         resolve_xml_special_chars( psz_string );
324         lua_pushstring( L, psz_string );
325         free( psz_string );
326     }
327     return i_top;
328 }
329
330 int vlclua_convert_xml_special_chars( lua_State *L )
331 {
332     int i_top = lua_gettop( L );
333     int i;
334     for( i = 1; i <= i_top; i++ )
335     {
336         char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
337         lua_remove( L, 1 );
338         lua_pushstring( L, psz_string );
339         free( psz_string );
340     }
341     return i_top;
342 }
343
344 /*****************************************************************************
345  * Messaging facilities
346  *****************************************************************************/
347 int vlclua_msg_dbg( lua_State *L )
348 {
349     int i_top = lua_gettop( L );
350     vlc_object_t *p_this = vlclua_get_this( L );
351     int i;
352     for( i = 1; i <= i_top; i++ )
353         msg_Dbg( p_this, "%s", luaL_checkstring( L, 1 ) );
354     return 0;
355 }
356 int vlclua_msg_warn( lua_State *L )
357 {
358     int i_top = lua_gettop( L );
359     vlc_object_t *p_this = vlclua_get_this( L );
360     int i;
361     for( i = 1; i <= i_top; i++ )
362         msg_Warn( p_this, "%s", luaL_checkstring( L, i ) );
363     return 0;
364 }
365 int vlclua_msg_err( lua_State *L )
366 {
367     int i_top = lua_gettop( L );
368     vlc_object_t *p_this = vlclua_get_this( L );
369     int i;
370     for( i = 1; i <= i_top; i++ )
371         msg_Err( p_this, "%s", luaL_checkstring( L, i ) );
372     return 0;
373 }
374 int vlclua_msg_info( lua_State *L )
375 {
376     int i_top = lua_gettop( L );
377     vlc_object_t *p_this = vlclua_get_this( L );
378     int i;
379     for( i = 1; i <= i_top; i++ )
380         msg_Info( p_this, "%s", luaL_checkstring( L, i ) );
381     return 0;
382 }
383
384 /*****************************************************************************
385  *
386  *****************************************************************************/
387 static int file_select( const char *file )
388 {
389     int i = strlen( file );
390     return i > 4 && !strcmp( file+i-4, ".lua" );
391 }
392
393 static int file_compare( const char **a, const char **b )
394 {
395     return strcmp( *a, *b );
396 }
397
398 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
399                      char **ppsz_dir_list )
400 {
401     if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP "%s",
402                    p_this->p_libvlc->psz_datadir, luadirname ) < 0 )
403         return VLC_ENOMEM;
404
405 #   if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
406     {
407         const char *psz_vlcpath = config_GetDataDir();
408         if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s",
409                       psz_vlcpath, luadirname )  < 0 )
410             return VLC_ENOMEM;
411
412         if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s",
413                       psz_vlcpath, luadirname )  < 0 )
414             return VLC_ENOMEM;
415     }
416 #   else
417     if( asprintf( &ppsz_dir_list[1],
418                   "share" DIR_SEP "%s", luadirname ) < 0 )
419         return VLC_ENOMEM;
420
421 #   ifdef HAVE_SYS_STAT_H
422     {
423         struct stat stat_info;
424         if( ( utf8_stat( ppsz_dir_list[1], &stat_info ) == -1 )
425             || !S_ISDIR( stat_info.st_mode ) )
426         {
427             free(ppsz_dir_list[1]);
428             if( asprintf( &ppsz_dir_list[1],
429                           DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
430                 return VLC_ENOMEM;
431         }
432     }
433 #   endif
434 #   endif
435     return VLC_SUCCESS;
436 }
437
438 /*****************************************************************************
439  * Will execute func on all scripts in luadirname, and stop if func returns
440  * success.
441  *****************************************************************************/
442 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
443                                   const char * luadirname,
444                                   int (*func)(vlc_object_t *, const char *, lua_State *, void *),
445                                   lua_State * L,
446                                   void * user_data)
447 {
448     int i_ret = VLC_EGENERIC;
449
450     DIR   *dir           = NULL;
451     char **ppsz_filelist = NULL;
452     char **ppsz_fileend  = NULL;
453     char **ppsz_file;
454
455     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
456     char **ppsz_dir;
457
458     i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
459     if( i_ret != VLC_SUCCESS )
460         return i_ret;
461     i_ret = VLC_EGENERIC;
462
463
464     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
465     {
466         int i_files;
467
468         if( ppsz_filelist )
469         {
470             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
471                  ppsz_file++ )
472                 free( *ppsz_file );
473             free( ppsz_filelist );
474             ppsz_filelist = NULL;
475         }
476
477         if( dir )
478         {
479             closedir( dir );
480         }
481
482         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
483         dir = utf8_opendir( *ppsz_dir );
484
485         if( !dir ) continue;
486         i_files = utf8_loaddir( dir, &ppsz_filelist, file_select,
487                                 file_compare );
488         if( i_files < 1 ) continue;
489         ppsz_fileend = ppsz_filelist + i_files;
490
491         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
492         {
493             char  *psz_filename;
494             if( asprintf( &psz_filename,
495                           "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
496                 return VLC_ENOMEM;
497             msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
498
499             i_ret = func( p_this, psz_filename, L, user_data );
500
501             free( psz_filename );
502
503             if( i_ret == VLC_SUCCESS ) break;
504         }
505         if( i_ret == VLC_SUCCESS ) break;
506     }
507
508     if( ppsz_filelist )
509     {
510         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
511              ppsz_file++ )
512             free( *ppsz_file );
513         free( ppsz_filelist );
514     }
515     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
516         free( *ppsz_dir );
517
518     if( dir ) closedir( dir );
519
520     return i_ret;
521 }
522
523
524 /*****************************************************************************
525  * Meta data setters utility.
526  * Playlist item table should be on top of the stack when these are called
527  *****************************************************************************/
528 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
529                               input_item_t *p_input )
530 {
531 #define TRY_META( a, b )                                        \
532     lua_getfield( L, -1, a );                                   \
533     if( lua_isstring( L, -1 ) )                                 \
534     {                                                           \
535         char *psz_value = strdup( lua_tostring( L, -1 ) );      \
536         EnsureUTF8( psz_value );                                \
537         msg_Dbg( p_this, #b ": %s", psz_value );                \
538         input_item_Set ## b ( p_input, psz_value );             \
539         free( psz_value );                                      \
540     }                                                           \
541     lua_pop( L, 1 ); /* pop a */
542     TRY_META( "title", Title );
543     TRY_META( "artist", Artist );
544     TRY_META( "genre", Genre );
545     TRY_META( "copyright", Copyright );
546     TRY_META( "album", Album );
547     TRY_META( "tracknum", TrackNum );
548     TRY_META( "description", Description );
549     TRY_META( "rating", Rating );
550     TRY_META( "date", Date );
551     TRY_META( "setting", Setting );
552     TRY_META( "url", URL );
553     TRY_META( "language", Language );
554     TRY_META( "nowplaying", NowPlaying );
555     TRY_META( "publisher", Publisher );
556     TRY_META( "encodedby", EncodedBy );
557     TRY_META( "arturl", ArtURL );
558     TRY_META( "trackid", TrackID );
559 }
560
561 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
562                                      input_item_t *p_input )
563 {
564     /* ... item */
565     lua_getfield( L, -1, "meta" );
566     /* ... item meta */
567     if( lua_istable( L, -1 ) )
568     {
569         lua_pushnil( L );
570         /* ... item meta nil */
571         while( lua_next( L, -2 ) )
572         {
573             /* ... item meta key value */
574             if( !lua_isstring( L, -2 ) )
575             {
576                 msg_Warn( p_this, "Custom meta data category name must be "
577                                    "a string" );
578             }
579             else if( !lua_istable( L, -1 ) )
580             {
581                 msg_Warn( p_this, "Custom meta data category contents "
582                                    "must be a table" );
583             }
584             else
585             {
586                 const char *psz_meta_category = lua_tostring( L, -2 );
587                 msg_Dbg( p_this, "Found custom meta data category: %s",
588                          psz_meta_category );
589                 lua_pushnil( L );
590                 /* ... item meta key value nil */
591                 while( lua_next( L, -2 ) )
592                 {
593                     /* ... item meta key value key2 value2 */
594                     if( !lua_isstring( L, -2 ) )
595                     {
596                         msg_Warn( p_this, "Custom meta category item name "
597                                            "must be a string." );
598                     }
599                     else if( !lua_isstring( L, -1 ) )
600                     {
601                         msg_Warn( p_this, "Custom meta category item value "
602                                            "must be a string." );
603                     }
604                     else
605                     {
606                         const char *psz_meta_name =
607                             lua_tostring( L, -2 );
608                         const char *psz_meta_value =
609                             lua_tostring( L, -1 );
610                         msg_Dbg( p_this, "Custom meta %s, %s: %s",
611                                  psz_meta_category, psz_meta_name,
612                                  psz_meta_value );
613                         input_ItemAddInfo( p_input, psz_meta_category,
614                                            psz_meta_name, psz_meta_value );
615                     }
616                     lua_pop( L, 1 ); /* pop item */
617                     /* ... item meta key value key2 */
618                 }
619                 /* ... item meta key value */
620             }
621             lua_pop( L, 1 ); /* pop category */
622             /* ... item meta key */
623         }
624         /* ... item meta */
625     }
626     lua_pop( L, 1 ); /* pop "meta" */
627     /* ... item -> back to original stack */
628 }
629
630 /*****************************************************************************
631  * Playlist utilities
632  ****************************************************************************/
633 /**
634  * Playlist item table should be on top of the stack when this is called
635  */
636 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
637                             int *pi_options, char ***pppsz_options )
638 {
639     lua_getfield( L, -1, "options" );
640     if( lua_istable( L, -1 ) )
641     {
642         lua_pushnil( L );
643         while( lua_next( L, -2 ) )
644         {
645             if( lua_isstring( L, -1 ) )
646             {
647                 char *psz_option = strdup( lua_tostring( L, -1 ) );
648                 msg_Dbg( p_this, "Option: %s", psz_option );
649                 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
650                              psz_option );
651             }
652             else
653             {
654                 msg_Warn( p_this, "Option should be a string" );
655             }
656             lua_pop( L, 1 ); /* pop option */
657         }
658     }
659     lua_pop( L, 1 ); /* pop "options" */
660 }
661
662 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
663                                     playlist_t *p_playlist,
664                                     input_item_t *p_parent, vlc_bool_t b_play )
665 {
666     int i_count = 0;
667
668     /* playlist */
669     if( lua_istable( L, -1 ) )
670     {
671         lua_pushnil( L );
672         /* playlist nil */
673         while( lua_next( L, -2 ) )
674         {
675             /* playlist key item */
676             /* <Parse playlist item> */
677             if( lua_istable( L, -1 ) )
678             {
679                 lua_getfield( L, -1, "path" );
680                 /* playlist key item path */
681                 if( lua_isstring( L, -1 ) )
682                 {
683                     const char   *psz_path     = NULL;
684                     const char   *psz_name     = NULL;
685                     char        **ppsz_options = NULL;
686                     int           i_options    = 0;
687                     mtime_t       i_duration   = -1;
688                     input_item_t *p_input;
689
690                     /* Read path and name */
691                     psz_path = lua_tostring( L, -1 );
692                     msg_Dbg( p_this, "Path: %s", psz_path );
693                     lua_getfield( L, -2, "name" );
694                     /* playlist key item path name */
695                     if( lua_isstring( L, -1 ) )
696                     {
697                         psz_name = lua_tostring( L, -1 );
698                         msg_Dbg( p_this, "Name: %s", psz_name );
699                     }
700                     else
701                     {
702                         if( !lua_isnil( L, -1 ) )
703                             msg_Warn( p_this, "Playlist item name should be a string." );
704                         psz_name = psz_path;
705                     }
706
707                     /* Read duration */
708                     lua_getfield( L, -3, "duration" );
709                     /* playlist key item path name duration */
710                     if( lua_isnumber( L, -1 ) )
711                     {
712                         i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
713                     }
714                     else if( !lua_isnil( L, -1 ) )
715                     {
716                         msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
717                     }
718                     lua_pop( L, 1 ); /* pop "duration" */
719
720                     /* playlist key item path name */
721
722                     /* Read options: item must be on top of stack */
723                     lua_pushvalue( L, -3 );
724                     /* playlist key item path name item */
725                     vlclua_read_options( p_this, L, &i_options, &ppsz_options );
726
727                     /* Create input item */
728                     p_input = input_ItemNewExt( p_playlist, psz_path,
729                                                 psz_name, i_options,
730                                                 (const char **)ppsz_options,
731                                                 i_duration );
732                     lua_pop( L, 3 ); /* pop "path name item" */
733                     /* playlist key item */
734
735                     /* Read meta data: item must be on top of stack */
736                     vlclua_read_meta_data( p_this, L, p_input );
737
738                     /* Read custom meta data: item must be on top of stack*/
739                     vlclua_read_custom_meta_data( p_this, L, p_input );
740
741                     /* Append item to playlist */
742                     if( p_parent ) /* Add to node */
743                         input_ItemAddSubItem( p_parent, p_input );
744                     else if( b_play ) /* Play */
745                         playlist_AddInput( p_playlist, p_input,
746                                            PLAYLIST_APPEND | PLAYLIST_GO,
747                                            PLAYLIST_END, VLC_TRUE, VLC_FALSE );
748                     else /* Enqueue */
749                         playlist_AddInput( p_playlist, p_input,
750                                            PLAYLIST_APPEND | PLAYLIST_PREPARSE,
751                                            PLAYLIST_END, VLC_TRUE, VLC_FALSE );
752                     i_count ++; /* increment counter */
753
754                     while( i_options > 0 )
755                         free( ppsz_options[--i_options] );
756                     free( ppsz_options );
757                 }
758                 else
759                 {
760                     lua_pop( L, 1 ); /* pop "path" */
761                     msg_Warn( p_this,
762                              "Playlist item's path should be a string" );
763                 }
764                 /* playlist key item */
765             }
766             else
767             {
768                 msg_Warn( p_this, "Playlist item should be a table" );
769             }
770             /* <Parse playlist item> */
771             lua_pop( L, 1 ); /* pop the value, keep the key for
772                               * the next lua_next() call */
773             /* playlist key */
774         }
775         /* playlist */
776     }
777     else
778     {
779         msg_Warn( p_this, "Playlist should be a table." );
780     }
781     return i_count;
782 }