services_discovery.c 5.61 KB
Newer Older
1
2
3
4
5
/*****************************************************************************
 * services_discovery.c : Services discovery using lua scripts
 *****************************************************************************
 * Copyright (C) 2009 the VideoLAN team
 *
Sephiroth87's avatar
Sephiroth87 committed
6
 * Authors: Fabio Ritrovato <sephiroth87 at videolan dot org>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_services_discovery.h>
#include <vlc_playlist.h>

#include "vlc.h"
#include "libs.h"

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static void *Run( void * );

static const char * const ppsz_sd_options[] = { "sd", NULL };

/*****************************************************************************
 * Local structures
 *****************************************************************************/
struct services_discovery_sys_t
{
    lua_State *L;
    char *psz_filename;
    vlc_thread_t thread;
};
static const luaL_Reg p_reg[] = { { NULL, NULL } };

/*****************************************************************************
 * Open: initialize and create stuff
 *****************************************************************************/
int Open_LuaSD( vlc_object_t *p_this )
{
    services_discovery_t *p_sd = ( services_discovery_t * )p_this;
    services_discovery_sys_t *p_sys;
ivoire's avatar
ivoire committed
59
    lua_State *L = NULL;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    char *psz_name = strdup(p_sd->psz_name);

    if( !strcmp(p_sd->psz_name, "lua"))
    {
        // We want to load the module name "lua"
        // This module can be used to load lua script not registered
        // as builtin lua SD modules.
        config_ChainParse( p_sd, "lua-", ppsz_sd_options, p_sd->p_cfg );
        psz_name = var_CreateGetString( p_sd, "lua-sd" );
    }
    else
    {
        // We are loading a builtin lua sd module.
        psz_name = strdup(p_sd->psz_name);
    }
75
76

    if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
ivoire's avatar
ivoire committed
77
78
    {
        free( psz_name );
79
        return VLC_ENOMEM;
ivoire's avatar
ivoire committed
80
    }
81
    p_sd->p_sys = p_sys;
dionoea's avatar
dionoea committed
82
    p_sys->psz_filename = vlclua_find_file( p_this, "sd", psz_name );
83
84
85
86
87
    if( !p_sys->psz_filename )
    {
        msg_Err( p_sd, "Couldn't find lua services discovery script \"%s\".",
                 psz_name );
        free( psz_name );
Sephiroth87's avatar
Sephiroth87 committed
88
        goto error;
89
90
91
92
93
94
    }
    free( psz_name );
    L = luaL_newstate();
    if( !L )
    {
        msg_Err( p_sd, "Could not create new Lua State" );
Sephiroth87's avatar
Sephiroth87 committed
95
        goto error;
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    }
    luaL_openlibs( L );
    luaL_register( L, "vlc", p_reg );
    lua_pushlightuserdata( L, p_sd );
    lua_setfield( L, -2, "private" );
    luaopen_input( L );
    luaopen_msg( L );
    luaopen_misc( L );
    luaopen_net( L );
    luaopen_object( L );
    luaopen_playlist( L );
    luaopen_sd( L );
    luaopen_strings( L );
    luaopen_variables( L );
    luaopen_stream( L );
Sephiroth87's avatar
Sephiroth87 committed
111
112
    luaopen_gettext( L );
    luaopen_xml( L );
113
    lua_pop( L, 1 );
Sephiroth87's avatar
Sephiroth87 committed
114
115
116
117
118
119
    if( vlclua_add_modules_path( p_sd, L, p_sys->psz_filename ) )
    {
        msg_Warn( p_sd, "Error while setting the module search path for %s",
                  p_sys->psz_filename );
        goto error;
    }
120
121
122
123
124
125
    if( luaL_dofile( L, p_sys->psz_filename ) )
    {

        msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
                  lua_tostring( L, lua_gettop( L ) ) );
        lua_pop( L, 1 );
Sephiroth87's avatar
Sephiroth87 committed
126
        goto error;
127
128
129
130
    }
    p_sys->L = L;
    if( vlc_clone (&p_sd->p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW) )
    {
Sephiroth87's avatar
Sephiroth87 committed
131
        goto error;
132
133
    }
    return VLC_SUCCESS;
Sephiroth87's avatar
Sephiroth87 committed
134
error:
ivoire's avatar
ivoire committed
135
136
    if( L )
        lua_close( L );
Sephiroth87's avatar
Sephiroth87 committed
137
138
139
    free( p_sys->psz_filename );
    free( p_sys );
    return VLC_EGENERIC;
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
}

/*****************************************************************************
 * Close: cleanup
 *****************************************************************************/
void Close_LuaSD( vlc_object_t *p_this )
{
    services_discovery_t *p_sd = ( services_discovery_t * )p_this;

    vlc_join (p_sd->p_sys->thread, NULL);
    free( p_sd->p_sys->psz_filename );
    lua_close( p_sd->p_sys->L );
    free( p_sd->p_sys );
}

/*****************************************************************************
 * Run: Thread entry-point
 ****************************************************************************/
static void* Run( void *data )
{
    services_discovery_t *p_sd = ( services_discovery_t * )data;
    services_discovery_sys_t *p_sys = p_sd->p_sys;
    lua_State *L = p_sys->L;

    lua_getglobal( L, "main" );
    if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
    {
        msg_Err( p_sd, "Error while runing script %s, "
                  "function main(): %s", p_sys->psz_filename,
                  lua_tostring( L, lua_gettop( L ) ) );
        lua_pop( L, 1 );
        return NULL;
    }
Sephiroth87's avatar
Sephiroth87 committed
173
    msg_Dbg( p_sd, "LuaSD script loaded: %s", p_sd->psz_name );
174
175
    return NULL;
}