]> git.sesse.net Git - vlc/blob - modules/meta_engine/luameta.c
Implements ogg/vorbis & flac duration preparsing
[vlc] / modules / meta_engine / luameta.c
1 /*****************************************************************************
2  * luameta.c: Get meta/artwork using lua scripts
3  *****************************************************************************
4  * Copyright (C) 2006 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 #include <stdlib.h>                                      /* malloc(), free() */
32 #include <stdio.h>                                       /*asprintf()*/
33
34 #include <vlc/vlc.h>
35 #include <vlc_input.h>
36 #include <vlc_playlist.h>
37 #include <vlc_meta.h>
38 #include <vlc_url.h>
39 #include <vlc_strings.h>
40 #include <vlc_stream.h>
41 #include <vlc_charset.h>
42
43 #ifdef HAVE_SYS_STAT_H
44 #   include <sys/stat.h>
45 #endif
46
47 #include <lua.h>        /* Low level lua C API */
48 #include <lauxlib.h>    /* Higher level C API */
49 #include <lualib.h>     /* Lua libs */
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int FindArt( vlc_object_t * );
55 static int FindMeta( vlc_object_t *p_this );
56
57 /*****************************************************************************
58  * Module descriptor
59  *****************************************************************************/
60
61 vlc_module_begin();
62     set_shortname( N_( "Lua Meta" ) );
63     set_description( _("Fetch Artwork using lua scripts") );
64     set_capability( "meta fetcher", 10 );
65     set_callbacks( FindMeta, NULL );
66     add_submodule();
67         set_capability( "art finder", 10 );
68         set_callbacks( FindArt, NULL );
69 vlc_module_end();
70
71 /*****************************************************************************
72  * Lua function bridge
73  *****************************************************************************/
74 static vlc_object_t * vlclua_get_this( lua_State *p_state )
75 {
76     vlc_object_t * p_this;
77     lua_getglobal( p_state, "vlc" );
78     lua_getfield( p_state, lua_gettop( p_state ), "private" );
79     p_this = (vlc_object_t*)lua_topointer( p_state, lua_gettop( p_state ) );
80     lua_pop( p_state, 2 );
81     return p_this;
82 }
83
84 static int vlclua_stream_new( lua_State *p_state )
85 {
86     vlc_object_t * p_this = vlclua_get_this( p_state );
87     int i = lua_gettop( p_state );
88     stream_t * p_stream;
89     const char * psz_url;
90     if( !i ) return 0;
91     psz_url = lua_tostring( p_state, 1 );
92     lua_pop( p_state, i );
93     p_stream = stream_UrlNew( p_this, psz_url );
94     lua_pushlightuserdata( p_state, p_stream );
95     return 1;
96 }
97
98 static int vlclua_stream_read( lua_State *p_state )
99 {
100     int i = lua_gettop( p_state );
101     stream_t * p_stream;
102     int n;
103     byte_t *p_read;
104     int i_read;
105     if( !i ) return 0;
106     p_stream = (stream_t *)lua_topointer( p_state, 1 );
107     n = lua_tonumber( p_state, 2 );
108     lua_pop( p_state, i );
109     p_read = malloc( n );
110     if( !p_read ) return 0;
111     i_read = stream_Read( p_stream, p_read, n );
112     lua_pushlstring( p_state, (const char *)p_read, i_read );
113     free( p_read );
114     return 1;
115 }
116
117 static int vlclua_stream_readline( lua_State *p_state )
118 {
119     int i = lua_gettop( p_state );
120     stream_t * p_stream;
121     if( !i ) return 0;
122     p_stream = (stream_t *)lua_topointer( p_state, 1 );
123     lua_pop( p_state, i );
124     char *psz_line = stream_ReadLine( p_stream );
125     if( psz_line )
126     {
127         lua_pushstring( p_state, psz_line );
128         free( psz_line );
129     }
130     else
131     {
132         lua_pushnil( p_state );
133     }
134     return 1;
135 }
136
137 static int vlclua_stream_delete( lua_State *p_state )
138 {
139     int i = lua_gettop( p_state );
140     stream_t * p_stream;
141     if( !i ) return 0;
142     p_stream = (stream_t *)lua_topointer( p_state, 1 );
143     lua_pop( p_state, i );
144     stream_Delete( p_stream );
145     return 1;
146 }
147
148 static int vlclua_decode_uri( lua_State *p_state )
149 {
150     int i = lua_gettop( p_state );
151     if( !i ) return 0;
152     const char *psz_cstring = lua_tostring( p_state, 1 );
153     if( !psz_cstring ) return 0;
154     char *psz_string = strdup( psz_cstring );
155     lua_pop( p_state, i );
156     decode_URI( psz_string );
157     lua_pushstring( p_state, psz_string );
158     free( psz_string );
159     return 1;
160 }
161
162 static int vlclua_resolve_xml_special_chars( lua_State *p_state )
163 {
164     int i = lua_gettop( p_state );
165     if( !i ) return 0;
166     const char *psz_cstring = lua_tostring( p_state, 1 );
167     if( !psz_cstring ) return 0;
168     char *psz_string = strdup( psz_cstring );
169     lua_pop( p_state, i );
170     resolve_xml_special_chars( psz_string );
171     lua_pushstring( p_state, psz_string );
172     free( psz_string );
173     return 1;
174 }
175
176 static int vlclua_msg_dbg( lua_State *p_state )
177 {
178     vlc_object_t *p_this = vlclua_get_this( p_state );
179     int i = lua_gettop( p_state );
180     if( !i ) return 0;
181     const char *psz_cstring = lua_tostring( p_state, 1 );
182     if( !psz_cstring ) return 0;
183     msg_Dbg( p_this, "%s", psz_cstring );
184     return 0;
185 }
186 static int vlclua_msg_warn( lua_State *p_state )
187 {
188     vlc_object_t *p_this = vlclua_get_this( p_state );
189     int i = lua_gettop( p_state );
190     if( !i ) return 0;
191     const char *psz_cstring = lua_tostring( p_state, 1 );
192     if( !psz_cstring ) return 0;
193     msg_Warn( p_this, "%s", psz_cstring );
194     return 0;
195 }
196 static int vlclua_msg_err( lua_State *p_state )
197 {
198     vlc_object_t *p_this = vlclua_get_this( p_state );
199     int i = lua_gettop( p_state );
200     if( !i ) return 0;
201     const char *psz_cstring = lua_tostring( p_state, 1 );
202     if( !psz_cstring ) return 0;
203     msg_Err( p_this, "%s", psz_cstring );
204     return 0;
205 }
206 static int vlclua_msg_info( lua_State *p_state )
207 {
208     vlc_object_t *p_this = vlclua_get_this( p_state );
209     int i = lua_gettop( p_state );
210     if( !i ) return 0;
211     const char *psz_cstring = lua_tostring( p_state, 1 );
212     if( !psz_cstring ) return 0;
213     msg_Info( p_this, "%s", psz_cstring );
214     return 0;
215 }
216
217 /* Functions to register */
218 static luaL_Reg p_reg[] =
219 {
220     { "stream_new", vlclua_stream_new },
221     { "stream_read", vlclua_stream_read },
222     { "stream_readline", vlclua_stream_readline },
223     { "stream_delete", vlclua_stream_delete },
224     { "decode_uri", vlclua_decode_uri },
225     { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
226     { "msg_dbg", vlclua_msg_dbg },
227     { "msg_warn", vlclua_msg_warn },
228     { "msg_err", vlclua_msg_err },
229     { "msg_info", vlclua_msg_info },
230     { NULL, NULL }
231 };
232 /*****************************************************************************
233  *
234  *****************************************************************************/
235 static int file_select( const char *file )
236 {
237     int i = strlen( file );
238     return i > 4 && !strcmp( file+i-4, ".lua" );
239 }
240
241 static int file_compare( const char **a, const char **b )
242 {
243     return strcmp( *a, *b );
244 }
245
246 /*****************************************************************************
247  * Init lua
248  *****************************************************************************/
249 static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item )
250 {
251     lua_State * p_state = luaL_newstate();
252     if( !p_state )
253     {
254         msg_Err( p_this, "Could not create new Lua State" );
255         return NULL;
256     }
257     char *psz_meta;
258
259     /* Load Lua libraries */
260     luaL_openlibs( p_state ); /* XXX: Don't open all the libs? */
261     
262     luaL_register( p_state, "vlc", p_reg );
263     
264     lua_pushlightuserdata( p_state, p_this );
265     lua_setfield( p_state, lua_gettop( p_state ) - 1, "private" );
266    
267     psz_meta = input_item_GetName( p_item );
268     lua_pushstring( p_state, psz_meta );
269     lua_setfield( p_state, lua_gettop( p_state ) - 1, "name" );
270     free( psz_meta );
271    
272     psz_meta = input_item_GetArtist( p_item );
273     lua_pushstring( p_state, psz_meta );
274     lua_setfield( p_state, lua_gettop( p_state ) - 1, "artist" );
275     free( psz_meta );
276    
277     psz_meta = input_item_GetTitle( p_item ) ;
278     lua_pushstring( p_state, psz_meta );
279     lua_setfield( p_state, lua_gettop( p_state ) - 1, "title" );
280     free( psz_meta );
281    
282     psz_meta = input_item_GetAlbum( p_item );
283     lua_pushstring( p_state, psz_meta );
284     lua_setfield( p_state, lua_gettop( p_state ) - 1, "album" );
285     free( psz_meta );
286
287     psz_meta = input_item_GetArtURL( p_item );
288     lua_pushstring( p_state, psz_meta );
289     lua_setfield( p_state, lua_gettop( p_state ) - 1, "arturl" );
290     free( psz_meta );
291     /* XXX: all should be passed ( could use macro ) */
292
293     return p_state;
294 }
295
296 /*****************************************************************************
297  * Will execute func on all scripts in luadirname, and stop if func returns
298  * success.
299  *****************************************************************************/
300 static int vlclua_scripts_batch_execute( vlc_object_t *p_this,
301                                          const char * luadirname,
302                                          int (*func)(vlc_object_t *, const char *, lua_State *, void *),
303                                          lua_State * p_state,
304                                          void * user_data)
305 {
306     int i_ret = VLC_EGENERIC;
307
308     DIR   *dir           = NULL;
309     char **ppsz_filelist = NULL;
310     char **ppsz_fileend  = NULL;
311     char **ppsz_file;
312
313     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
314     char **ppsz_dir;
315
316     if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP CONFIG_DIR DIR_SEP "%s", p_this->p_libvlc->psz_homedir,
317             luadirname ) < 0 )
318         return VLC_ENOMEM;
319
320 #   if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
321     {
322         const char *psz_vlcpath = config_GetDataDir();
323         if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s", psz_vlcpath, luadirname )  < 0 )
324             return VLC_ENOMEM;
325
326         if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s", psz_vlcpath, luadirname )  < 0 )
327             return VLC_ENOMEM;
328     }
329 #   else
330     {
331 #   ifdef HAVE_SYS_STAT_H
332         struct stat stat_info;
333         if( ( utf8_stat( "share/luaplaylist", &stat_info ) == -1 )
334             || !S_ISDIR( stat_info.st_mode ) )
335         {
336             if( asprintf( &ppsz_dir_list[1], 
337                           DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
338                 return VLC_ENOMEM;
339         }
340         else
341 #   endif
342         {
343             if( asprintf( &ppsz_dir_list[1], 
344                           "share" DIR_SEP "%s", luadirname ) < 0 )
345                 return VLC_ENOMEM;
346         }
347     }
348 #   endif
349
350     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
351     {
352         int i_files;
353
354         if( ppsz_filelist )
355         {
356             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
357                  ppsz_file++ )
358                 free( *ppsz_file );
359             free( ppsz_filelist );
360             ppsz_filelist = NULL;
361         }
362
363         if( dir )
364         {
365             closedir( dir );
366         }
367
368         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
369         dir = utf8_opendir( *ppsz_dir );
370
371         if( !dir ) continue;
372         i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
373         if( i_files < 1 ) continue;
374         ppsz_fileend = ppsz_filelist + i_files;
375
376         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
377         {
378             char  *psz_filename;
379             asprintf( &psz_filename, "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file );
380             msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
381             
382             i_ret = func( p_this, psz_filename, p_state, user_data );
383             
384             free( psz_filename );
385
386             if( i_ret == VLC_SUCCESS ) break;
387         }
388         if( i_ret == VLC_SUCCESS ) break;
389     }
390
391     if( ppsz_filelist )
392     {
393         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
394              ppsz_file++ )
395             free( *ppsz_file );
396         free( ppsz_filelist );
397     }
398     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
399         free( *ppsz_dir );
400
401     if( dir ) closedir( dir );
402
403     return i_ret;
404 }
405
406 /*****************************************************************************
407  * Meta data setters utility.
408  *****************************************************************************/
409 static inline void read_meta_data( vlc_object_t *p_this,
410                                    lua_State *p_state, int o, int t,
411                                    input_item_t *p_input )
412 {
413     const char *psz_value;
414 #define TRY_META( a, b )                                    \
415     lua_getfield( p_state, o, a );                          \
416     if( lua_isstring( p_state, t ) )                        \
417     {                                                       \
418         psz_value = lua_tostring( p_state, t );             \
419         msg_Dbg( p_this, #b ": %s", psz_value );           \
420         input_item_Set ## b ( p_input, psz_value );   \
421     }                                                       \
422     lua_pop( p_state, 1 ); /* pop a */
423     TRY_META( "title", Title );
424     TRY_META( "artist", Artist );
425     TRY_META( "genre", Genre );
426     TRY_META( "copyright", Copyright );
427     TRY_META( "album", Album );
428     TRY_META( "tracknum", TrackNum );
429     TRY_META( "description", Description );
430     TRY_META( "rating", Rating );
431     TRY_META( "date", Date );
432     TRY_META( "setting", Setting );
433     TRY_META( "url", URL );
434     TRY_META( "language", Language );
435     TRY_META( "nowplaying", NowPlaying );
436     TRY_META( "publisher", Publisher );
437     TRY_META( "encodedby", EncodedBy );
438     TRY_META( "arturl", ArtURL );
439     TRY_META( "trackid", TrackID );
440 }
441
442 static inline void read_custom_meta_data( vlc_object_t *p_this,
443                                           lua_State *p_state, int o, int t,
444                                           input_item_t *p_input )
445 {
446     lua_getfield( p_state, o, "meta" );
447     if( lua_istable( p_state, t ) )
448     {
449         lua_pushnil( p_state );
450         while( lua_next( p_state, t ) )
451         {
452             if( !lua_isstring( p_state, t+1 ) )
453             {
454                 msg_Warn( p_this, "Custom meta data category name must be "
455                                    "a string" );
456             }
457             else if( !lua_istable( p_state, t+2 ) )
458             {
459                 msg_Warn( p_this, "Custom meta data category contents "
460                                    "must be a table" );
461             }
462             else
463             {
464                 const char *psz_meta_category = lua_tostring( p_state, t+1 );
465                 msg_Dbg( p_this, "Found custom meta data category: %s",
466                          psz_meta_category );
467                 lua_pushnil( p_state );
468                 while( lua_next( p_state, t+2 ) )
469                 {
470                     if( !lua_isstring( p_state, t+3 ) )
471                     {
472                         msg_Warn( p_this, "Custom meta category item name "
473                                            "must be a string." );
474                     }
475                     else if( !lua_isstring( p_state, t+4 ) )
476                     {
477                         msg_Warn( p_this, "Custom meta category item value "
478                                            "must be a string." );
479                     }
480                     else
481                     {
482                         const char *psz_meta_name =
483                             lua_tostring( p_state, t+3 );
484                         const char *psz_meta_value =
485                             lua_tostring( p_state, t+4 );
486                         msg_Dbg( p_this, "Custom meta %s, %s: %s",
487                                  psz_meta_category, psz_meta_name,
488                                  psz_meta_value );
489                         input_ItemAddInfo( p_input, psz_meta_category,
490                                            psz_meta_name, psz_meta_value );
491                     }
492                     lua_pop( p_state, 1 ); /* pop item */
493                 }
494             }
495             lua_pop( p_state, 1 ); /* pop category */
496         }
497     }
498     lua_pop( p_state, 1 ); /* pop "meta" */
499 }
500
501 /*****************************************************************************
502  * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
503  * pointed by psz_filename.
504  *****************************************************************************/
505 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
506                       lua_State * p_state, void * user_data )
507 {
508     int i_ret = VLC_EGENERIC;
509     input_item_t * p_input = user_data;
510     int s;
511
512     /* Ugly hack to delete previous versions of the fetchart()
513     * functions. */
514     lua_pushnil( p_state );
515     lua_setglobal( p_state, "fetch_art" );
516     
517     /* Load and run the script(s) */
518     if( luaL_dofile( p_state, psz_filename ) )
519     {
520         msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
521                   lua_tostring( p_state, lua_gettop( p_state ) ) );
522         lua_pop( p_state, 1 );
523         return VLC_EGENERIC;
524     }
525
526     lua_getglobal( p_state, "fetch_art" );
527
528     if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
529     {
530         msg_Warn( p_this, "Error while runing script %s, "
531                   "function fetch_art() not found", psz_filename );
532         lua_pop( p_state, 1 );
533         return VLC_EGENERIC;
534     }
535
536     if( lua_pcall( p_state, 0, 1, 0 ) )
537     {
538         msg_Warn( p_this, "Error while runing script %s, "
539                   "function fetch_art(): %s", psz_filename,
540                   lua_tostring( p_state, lua_gettop( p_state ) ) );
541         lua_pop( p_state, 1 );
542         return VLC_EGENERIC;
543     }
544
545     if((s = lua_gettop( p_state )))
546     {
547         const char * psz_value;
548
549         if( lua_isstring( p_state, s ) )
550         {
551             psz_value = lua_tostring( p_state, s );
552             if( psz_value && *psz_value != 0 )
553             {
554                 msg_Dbg( p_this, "setting arturl: %s", psz_value );
555                 input_item_SetArtURL ( p_input, psz_value );
556                 i_ret = VLC_SUCCESS;
557             }
558         }
559         else
560         {
561             msg_Err( p_this, "Lua playlist script %s: "
562                  "didn't return a string", psz_filename );
563         }
564     }
565     else
566     {
567         msg_Err( p_this, "Script went completely foobar" );
568     }
569
570     return i_ret;
571 }
572
573 /*****************************************************************************
574  * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
575  * pointed by psz_filename.
576  *****************************************************************************/
577 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
578                        lua_State * p_state, void * user_data )
579 {
580     input_item_t * p_input = user_data;
581     int t;
582
583     /* Ugly hack to delete previous versions of the fetchmeta()
584     * functions. */
585     lua_pushnil( p_state );
586     lua_setglobal( p_state, "fetch_meta" );
587     
588     /* Load and run the script(s) */
589     if( luaL_dofile( p_state, psz_filename ) )
590     {
591         msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
592                   lua_tostring( p_state, lua_gettop( p_state ) ) );
593         lua_pop( p_state, 1 );
594         return VLC_EGENERIC;
595     }
596     
597     lua_getglobal( p_state, "fetch_meta" );
598     
599     if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
600     {
601         msg_Warn( p_this, "Error while runing script %s, "
602                   "function fetch_meta() not found", psz_filename );
603         lua_pop( p_state, 1 );
604         return VLC_EGENERIC;
605     }
606     
607     if( lua_pcall( p_state, 0, 1, 0 ) )
608     {
609         msg_Warn( p_this, "Error while runing script %s, "
610                   "function fetch_meta(): %s", psz_filename,
611                   lua_tostring( p_state, lua_gettop( p_state ) ) );
612         lua_pop( p_state, 1 );
613         return VLC_EGENERIC;
614     }
615     
616
617     if((t = lua_gettop( p_state )))
618     {
619         if( lua_istable( p_state, t ) )
620         {
621             read_meta_data( p_this, p_state, t, t+1, p_input );
622             read_custom_meta_data( p_this, p_state, t, t+1, p_input );
623         }
624         else
625         {
626             msg_Err( p_this, "Lua playlist script %s: "
627                  "didn't return a table", psz_filename );
628         }
629     }
630     else
631     {
632         msg_Err( p_this, "Script went completely foobar" );
633     }
634
635     /* We tell the batch thing to continue, hence all script
636      * will get the change to add its meta. This behaviour could
637      * be changed. */
638     return VLC_EGENERIC;
639 }
640
641 /*****************************************************************************
642  * Module entry point for meta.
643  *****************************************************************************/
644 static int FindMeta( vlc_object_t *p_this )
645 {
646     meta_engine_t *p_me = (meta_engine_t *)p_this;
647     input_item_t *p_item = p_me->p_item;
648     lua_State *p_state = vlclua_meta_init( p_this, p_item );
649     
650     int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_meta, p_state, p_item );
651     lua_close( p_state );
652     return i_ret;
653 }
654
655 /*****************************************************************************
656  * Module entry point for art.
657  *****************************************************************************/
658 static int FindArt( vlc_object_t *p_this )
659 {
660     playlist_t *p_playlist = (playlist_t *)p_this;
661     input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
662     lua_State *p_state = vlclua_meta_init( p_this, p_item );
663     
664     int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_art, p_state, p_item );
665     lua_close( p_state );
666     return i_ret;
667 }
668