]> git.sesse.net Git - vlc/blob - modules/demux/playlist/luaplaylist.c
* luaplaylist.c: add support for meta data info. Playlist items now use .path and...
[vlc] / modules / demux / playlist / luaplaylist.c
1 /*****************************************************************************
2  * luaplaylist.c :  Lua playlist demux module
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod 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  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc_demux.h>
29 #include <vlc_url.h>
30 #include <vlc_strings.h>
31 #include <vlc_charset.h>
32
33 #include <errno.h>                                                 /* ENOMEM */
34 #include "playlist.h"
35
36 #ifdef HAVE_SYS_STAT_H
37 #   include <sys/stat.h>
38 #endif
39
40 #include <lua.h>        /* Low level lua C API */
41 #include <lauxlib.h>    /* Higher level C API */
42 #include <lualib.h>     /* Lua libs */
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static int E_(Import_LuaPlaylist)( vlc_object_t *p_this );
48 static void E_(Close_LuaPlaylist)( vlc_object_t *p_this );
49
50 static int Demux( demux_t *p_demux );
51 static int Control( demux_t *p_demux, int i_query, va_list args );
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56 vlc_module_begin();
57     add_shortcut( "lua" );
58     set_category( CAT_INPUT );
59     set_subcategory( SUBCAT_INPUT_DEMUX );
60
61     set_shortname( _("Lua Playlist") );
62     set_description( _("Lua Playlist Parser Interface") );
63     set_capability( "demux2", 0 );
64     set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
65 vlc_module_end();
66
67 /*****************************************************************************
68  *
69  *****************************************************************************/
70 struct demux_sys_t
71 {
72     lua_State *p_state;
73 };
74
75 static demux_t *vlclua_get_demux( lua_State *p_state )
76 {
77     demux_t *p_demux;
78     lua_getglobal( p_state, "vlc" );
79     lua_getfield( p_state, lua_gettop( p_state ), "private" );
80     p_demux = (demux_t*)lua_topointer( p_state, lua_gettop( p_state ) );
81     lua_pop( p_state, 2 );
82     return p_demux;
83 }
84
85 static int vlclua_peek( lua_State *p_state )
86 {
87     demux_t *p_demux = vlclua_get_demux( p_state );
88     int i = lua_gettop( p_state );
89     int n;
90     byte_t *p_peek;
91     int i_peek;
92     if( !i ) return 0;
93     n = lua_tonumber( p_state, 1 );
94     lua_pop( p_state, i );
95     i_peek = stream_Peek( p_demux->s, &p_peek, n );
96     lua_pushlstring( p_state, (const char *)p_peek, i_peek );
97     return 1;
98 }
99
100 static int vlclua_read( lua_State *p_state )
101 {
102     demux_t *p_demux = vlclua_get_demux( p_state );
103     int i = lua_gettop( p_state );
104     int n;
105     byte_t *p_read;
106     int i_read;
107     if( !i ) return 0;
108     n = lua_tonumber( p_state, 1 );
109     lua_pop( p_state, i );
110     i_read = stream_Read( p_demux->s, &p_read, n );
111     lua_pushlstring( p_state, (const char *)p_read, i_read );
112     return 1;
113 }
114
115 static int vlclua_readline( lua_State *p_state )
116 {
117     demux_t *p_demux = vlclua_get_demux( p_state );
118     char *psz_line = stream_ReadLine( p_demux->s );
119     if( psz_line )
120     {
121         lua_pushstring( p_state, psz_line );
122         free( psz_line );
123     }
124     else
125     {
126         lua_pushnil( p_state );
127     }
128     return 1;
129 }
130
131 static int vlclua_decode_uri( lua_State *p_state )
132 {
133     int i = lua_gettop( p_state );
134     if( !i ) return 0;
135     const char *psz_cstring = lua_tostring( p_state, 1 );
136     if( !psz_cstring ) return 0;
137     char *psz_string = strdup( psz_cstring );
138     lua_pop( p_state, i );
139     decode_URI( psz_string );
140     lua_pushstring( p_state, psz_string );
141     free( psz_string );
142     return 1;
143 }
144
145 static int vlclua_resolve_xml_special_chars( lua_State *p_state )
146 {
147     int i = lua_gettop( p_state );
148     if( !i ) return 0;
149     const char *psz_cstring = lua_tostring( p_state, 1 );
150     if( !psz_cstring ) return 0;
151     char *psz_string = strdup( psz_cstring );
152     lua_pop( p_state, i );
153     resolve_xml_special_chars( psz_string );
154     lua_pushstring( p_state, psz_string );
155     free( psz_string );
156     return 1;
157 }
158
159 static int file_select( const char *file )
160 {
161     int i = strlen( file );
162     return i > 4 && !strcmp( file+i-4, ".lua" );
163 }
164
165 static int file_compare( const char **a, const char **b )
166 {
167     return strcmp( *a, *b );
168 }
169
170 /*****************************************************************************
171  * Import_LuaPlaylist: main import function
172  *****************************************************************************/
173 int E_(Import_LuaPlaylist)( vlc_object_t *p_this )
174 {
175     demux_t *p_demux = (demux_t *)p_this;
176     lua_State *p_state;
177     int i_ret = VLC_EGENERIC;
178     char **ppsz_filelist = NULL;
179     char **ppsz_fileend = NULL;
180     char **ppsz_file;
181     DIR *dir;
182     char *psz_filename = NULL;
183     int i_files;
184     char *psz_dir;
185
186 #   if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
187     {
188         char *psz_vlcpath = p_intf->p_libvlc_global->psz_vlcpath;
189         psz_dir = malloc( strlen( psz_vlcpath ) + strlen( "/share/luaplaylist" ) + 1 );
190         if( !psz_src ) return VLC_ENOMEM;
191 #       if defined( WIN32 )
192             sprintf( psz_src, "%s/luaplaylist", psz_vlcpath );
193 #       else
194             sprintf( psz_src, "%s/share/luaplaylist", psz_vlcpath );
195 #       endif
196     }
197 #   else
198     {
199 #   ifdef HAVE_SYS_STAT_H
200         struct stat stat_info;
201         if( ( utf8_stat( "share/luaplaylist", &stat_info ) == -1 )
202             || !S_ISDIR( stat_info.st_mode ) )
203         {
204             psz_dir = strdup( DATA_PATH "/luaplaylist" );
205         }
206         else
207 #   endif
208         {
209             psz_dir = strdup( "share/luaplaylist" );
210         }
211     }
212 #   endif
213
214     static luaL_Reg p_reg[] =
215     {
216         { "peek", vlclua_peek },
217         { "read", vlclua_read },
218         { "readline", vlclua_readline },
219         { "decode_uri", vlclua_decode_uri },
220         { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars }
221     };
222
223     p_demux->p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
224     if( !p_demux->p_sys )
225     {
226         free( psz_dir );
227         return VLC_ENOMEM;
228     }
229
230     p_demux->pf_control = Control;
231     p_demux->pf_demux = Demux;
232
233     /* Initialise Lua state structure */
234     p_state = luaL_newstate();
235     if( !p_state )
236     {
237         msg_Err( p_demux, "Could not create new Lua State" );
238         free( psz_dir );
239         free( p_demux->p_sys );
240         return VLC_EGENERIC;
241     }
242     p_demux->p_sys->p_state = p_state;
243
244     /* Load Lua libraries */
245     luaL_openlibs( p_state ); /* FIXME: Don't open all the libs? */
246
247     luaL_register( p_state, "vlc", p_reg );
248     lua_pushlightuserdata( p_state, p_demux );
249     lua_setfield( p_state, lua_gettop( p_state ) - 1, "private" );
250     lua_pushstring( p_state, p_demux->psz_path );
251     lua_setfield( p_state, lua_gettop( p_state ) - 1, "path" );
252     lua_pushstring( p_state, p_demux->psz_access );
253     lua_setfield( p_state, lua_gettop( p_state ) - 1, "access" );
254
255     lua_pop( p_state, 1 );
256
257     dir = utf8_opendir( psz_dir );
258     if( !dir ) goto error;
259     i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
260     if( i_files < 1 ) goto error;
261     ppsz_fileend = ppsz_filelist + i_files;
262
263     for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
264     {
265         free( psz_filename ); psz_filename = NULL;
266         asprintf( &psz_filename, "%s/%s", psz_dir, *ppsz_file );
267         msg_Dbg( p_demux, "Trying Lua playlist script %s", psz_filename );
268
269         /* Ugly hack to delete previous versions of the probe() and parse()
270          * functions. */
271         lua_pushnil( p_state );
272         lua_pushnil( p_state );
273         lua_setglobal( p_state, "probe" );
274         lua_setglobal( p_state, "parse" );
275
276         /* Load and run the script(s) */
277         if( luaL_dofile( p_state, psz_filename ) )
278         {
279             msg_Warn( p_demux, "Error loading script %s: %s", psz_filename,
280                       lua_tostring( p_state, lua_gettop( p_state ) ) );
281             lua_pop( p_state, 1 );
282             continue;
283         }
284
285         lua_getglobal( p_state, "probe" );
286
287         if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
288         {
289             msg_Warn( p_demux, "Error while runing script %s, "
290                       "function probe() not found", psz_filename );
291             lua_pop( p_state, 1 );
292             continue;
293         }
294
295         if( lua_pcall( p_state, 0, 1, 0 ) )
296         {
297             msg_Warn( p_demux, "Error while runing script %s, "
298                       "function probe(): %s", psz_filename,
299                       lua_tostring( p_state, lua_gettop( p_state ) ) );
300             lua_pop( p_state, 1 );
301             continue;
302         }
303
304         if( lua_gettop( p_state ) )
305         {
306             if( lua_toboolean( p_state, 1 ) )
307             {
308                 msg_Dbg( p_demux, "Lua playlist script %s's "
309                          "probe() function was successful", psz_filename );
310                 i_ret = VLC_SUCCESS;
311             }
312             lua_pop( p_state, 1 );
313
314             if( i_ret == VLC_SUCCESS ) break;
315         }
316     }
317
318     error:
319         free( psz_filename );
320
321         if( ppsz_filelist )
322         {
323             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
324                  ppsz_file++ )
325                 free( *ppsz_file );
326             free( ppsz_filelist );
327         }
328
329         if( dir ) closedir( dir );
330         free( psz_dir );
331         if( i_ret != VLC_SUCCESS )
332             E_(Close_LuaPlaylist)( p_this );
333         return i_ret;
334 }
335
336 /*****************************************************************************
337  * Deactivate: frees unused data
338  *****************************************************************************/
339 void E_(Close_LuaPlaylist)( vlc_object_t *p_this )
340 {
341     demux_t *p_demux = (demux_t *)p_this;
342     lua_close( p_demux->p_sys->p_state );
343     free( p_demux->p_sys );
344 }
345
346 static int Demux( demux_t *p_demux )
347 {
348     input_item_t *p_input;
349     lua_State *p_state = p_demux->p_sys->p_state;
350     char psz_filename[] = "FIXME";
351
352     INIT_PLAYLIST_STUFF;
353
354     lua_getglobal( p_state, "parse" );
355
356     if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
357     {
358         msg_Warn( p_demux, "Error while runing script %s, "
359                   "function parse() not found", psz_filename );
360         E_(Close_LuaPlaylist)( VLC_OBJECT( p_demux ) );
361         return VLC_EGENERIC;
362     }
363
364     if( lua_pcall( p_state, 0, 1, 0 ) )
365     {
366         msg_Warn( p_demux, "Error while runing script %s, "
367                   "function parse(): %s", psz_filename,
368                   lua_tostring( p_state, lua_gettop( p_state ) ) );
369         E_(Close_LuaPlaylist)( VLC_OBJECT( p_demux ) );
370         return VLC_EGENERIC;
371     }
372
373     if( lua_gettop( p_state ) )
374     {
375         int t = lua_gettop( p_state );
376         if( lua_istable( p_state, t ) )
377         {
378             lua_pushnil( p_state );
379             while( lua_next( p_state, t ) )
380             {
381                 if( lua_istable( p_state, t+2 ) )
382                 {
383                     const char *psz_url = NULL;
384                     const char *psz_title = NULL;
385                     lua_getfield( p_state, t+2, "path" );
386                     if( lua_isstring( p_state, t+3 ) )
387                     {
388                         psz_url = lua_tostring( p_state, t+3 );
389                         printf("Path: %s\n", psz_url );
390                         lua_getfield( p_state, t+2, "name" );
391                         if( lua_isstring( p_state, t+4 ) )
392                         {
393                             psz_title = lua_tostring( p_state, t+4 );
394                             printf("Name: %s\n", psz_title );
395                         }
396                         else
397                         {
398                             psz_title = psz_url;
399                         }
400                         p_input = input_ItemNewExt( p_playlist, psz_url,
401                                                     psz_title, 0, NULL, -1 );
402                         p_input->p_meta = vlc_meta_New();
403
404 #define TRY_META( a, b ) \
405                         lua_getfield( p_state, t+2, a ); \
406                         if( lua_isstring( p_state, t+5 ) ) \
407                         { \
408                             psz_title = lua_tostring( p_state, t+5 ); \
409                             printf( #b ": %s\n", psz_title ); \
410                             vlc_meta_Set ## b ( p_input->p_meta, psz_title ); \
411                         } \
412                         lua_pop( p_state, 1 ); /* pop a */
413                         TRY_META( "title", Title );
414                         TRY_META( "artist", Artist );
415                         TRY_META( "genre", Genre );
416                         TRY_META( "copyright", Copyright );
417                         TRY_META( "album", Album );
418                         TRY_META( "tracknum", Tracknum );
419                         TRY_META( "description", Description );
420                         TRY_META( "rating", Rating );
421                         TRY_META( "date", Date );
422                         TRY_META( "setting", Setting );
423                         TRY_META( "url", URL );
424                         TRY_META( "language", Language );
425                         TRY_META( "nowplaying", NowPlaying );
426                         TRY_META( "publisher", Publisher );
427                         TRY_META( "encodedby", EncodedBy );
428                         TRY_META( "arturl", ArtURL );
429                         TRY_META( "trackid", TrackID );
430
431                         playlist_BothAddInput(
432                             p_playlist, p_input,
433                             p_item_in_category,
434                             PLAYLIST_APPEND | PLAYLIST_SPREPARSE,
435                             PLAYLIST_END, NULL, NULL, VLC_FALSE );
436                         lua_pop( p_state, 1 ); /* pop "name" */
437                     }
438                     else
439                     {
440                         printf("Path isn't a string\n");
441                     }
442                     lua_pop( p_state, 1 ); /* pop "path" */
443                 }
444                 else
445                 {
446                     printf("This isn't a table !!!\n" );
447                 }
448                 lua_pop( p_state, 1 ); /* pop the value, keep the key for
449                                         * the next lua_next() call */
450             }
451             lua_pop( p_state, 1 ); /* pop the last key */
452         }
453         else
454         {
455             msg_Warn( p_demux, "Script didn't return a table" );
456         }
457     }
458     else
459     {
460         msg_Err( p_demux, "Script went completely foobar" );
461     }
462
463     HANDLE_PLAY_AND_RELEASE;
464
465     return -1; /* Needed for correct operation of go back */
466 }
467
468 static int Control( demux_t *p_demux, int i_query, va_list args )
469 {
470     return VLC_EGENERIC;
471 }