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