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