forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemux.c
328 lines (279 loc) · 8.66 KB
/
demux.c
1
2
3
4
5
6
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*****************************************************************************
* demux.c : Lua playlist demux module
*****************************************************************************
* Copyright (C) 2007-2008 the VideoLAN team
* $Id$
*
* Authors: Antoine Cellerier <dionoea at videolan tod org>
*
* 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.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <vlc_common.h>
#include <vlc_access.h>
#include "vlc.h"
#include "libs.h"
/*****************************************************************************
* Demux specific functions
*****************************************************************************/
struct vlclua_playlist
{
lua_State *L;
char *filename;
char *access;
const char *path;
};
static int vlclua_demux_peek( lua_State *L )
{
stream_t *s = (stream_t *)vlclua_get_this(L);
int n = luaL_checkint( L, 1 );
const uint8_t *p_peek;
ssize_t val = vlc_stream_Peek(s->s, &p_peek, n);
if (val > 0)
lua_pushlstring(L, (const char *)p_peek, val);
else
lua_pushnil( L );
return 1;
}
static int vlclua_demux_read( lua_State *L )
{
stream_t *s = (stream_t *)vlclua_get_this(L);
int n = luaL_checkint( L, 1 );
char *buf = malloc(n);
if (buf != NULL)
{
ssize_t val = vlc_stream_Read(s->s, buf, n);
if (val > 0)
lua_pushlstring(L, buf, val);
else
lua_pushnil( L );
free(buf);
}
else
lua_pushnil( L );
return 1;
}
static int vlclua_demux_readline( lua_State *L )
{
stream_t *s = (stream_t *)vlclua_get_this(L);
char *line = vlc_stream_ReadLine(s->s);
if (line != NULL)
{
lua_pushstring(L, line);
free(line);
}
else
lua_pushnil( L );
return 1;
}
/*****************************************************************************
*
*****************************************************************************/
/* Functions to register */
static const luaL_Reg p_reg[] =
{
{ "peek", vlclua_demux_peek },
{ NULL, NULL }
};
/* Functions to register for parse() function call only */
static const luaL_Reg p_reg_parse[] =
{
{ "read", vlclua_demux_read },
{ "readline", vlclua_demux_readline },
{ NULL, NULL }
};
/*****************************************************************************
* Called through lua_scripts_batch_execute to call 'probe' on
* the script pointed by psz_filename.
*****************************************************************************/
static int probe_luascript(vlc_object_t *obj, const char *filename,
const luabatch_context_t *ctx)
{
stream_t *s = (stream_t *)obj;
struct vlclua_playlist *sys = s->p_sys;
/* Initialise Lua state structure */
lua_State *L = luaL_newstate();
if( !L )
return VLC_ENOMEM;
sys->L = L;
/* Load Lua libraries */
luaL_openlibs( L ); /* FIXME: Don't open all the libs? */
vlclua_set_this(L, s);
luaL_register_namespace( L, "vlc", p_reg );
luaopen_msg( L );
luaopen_strings( L );
luaopen_stream( L );
luaopen_variables( L );
luaopen_xml( L );
if (sys->path != NULL)
lua_pushstring(L, sys->path);
else
lua_pushnil(L);
lua_setfield( L, -2, "path" );
if (sys->access != NULL)
lua_pushstring(L, sys->access);
else
lua_pushnil(L);
lua_setfield( L, -2, "access" );
lua_pop( L, 1 );
/* Setup the module search path */
if (vlclua_add_modules_path(L, filename))
{
msg_Warn(s, "error setting the module search path for %s", filename);
goto error;
}
/* Load and run the script(s) */
if (vlclua_dofile(VLC_OBJECT(s), L, filename))
{
msg_Warn(s, "error loading script %s: %s", filename,
lua_tostring(L, lua_gettop(L)));
goto error;
}
lua_getglobal( L, "probe" );
if( !lua_isfunction( L, -1 ) )
{
msg_Warn(s, "error running script %s: function %s(): %s",
filename, "probe", "not found");
goto error;
}
if( lua_pcall( L, 0, 1, 0 ) )
{
msg_Warn(s, "error running script %s: function %s(): %s",
filename, "probe", lua_tostring(L, lua_gettop(L)));
goto error;
}
if( lua_gettop( L ) )
{
if( lua_toboolean( L, 1 ) )
{
msg_Dbg(s, "Lua playlist script %s's "
"probe() function was successful", filename );
lua_pop( L, 1 );
sys->filename = strdup(filename);
return VLC_SUCCESS;
}
}
(void) ctx;
error:
lua_pop( L, 1 );
lua_close(sys->L);
return VLC_EGENERIC;
}
static int ReadDir(stream_t *s, input_item_node_t *node)
{
struct vlclua_playlist *sys = s->p_sys;
lua_State *L = sys->L;
luaL_register_namespace( L, "vlc", p_reg_parse );
lua_getglobal( L, "parse" );
if( !lua_isfunction( L, -1 ) )
{
msg_Warn(s, "error running script %s: function %s(): %s",
sys->filename, "parse", "not found");
return VLC_ENOITEM;
}
if( lua_pcall( L, 0, 1, 0 ) )
{
msg_Warn(s, "error running script %s: function %s(): %s",
sys->filename, "parse", lua_tostring(L, lua_gettop(L)));
return VLC_ENOITEM;
}
if (!lua_gettop(L))
{
msg_Err(s, "script went completely foobar");
return VLC_ENOITEM;
}
if (!lua_istable(L, -1))
{
msg_Warn(s, "Playlist should be a table.");
return VLC_ENOITEM;
}
lua_pushnil(L);
/* playlist nil */
while (lua_next(L, -2))
{
input_item_t *item = vlclua_read_input_item(VLC_OBJECT(s), L);
if (item != NULL)
{
/* copy the original URL to the meta data,
* if "URL" is still empty */
char *url = input_item_GetURL(item);
if (url == NULL && s->psz_url != NULL)
input_item_SetURL(item, s->psz_url);
free(url);
input_item_node_AppendItem(node, item);
input_item_Release(item);
}
/* pop the value, keep the key for the next lua_next() call */
lua_pop(L, 1);
}
/* playlist */
return VLC_SUCCESS;
}
/*****************************************************************************
* Import_LuaPlaylist: main import function
*****************************************************************************/
int Import_LuaPlaylist(vlc_object_t *obj)
{
if( lua_Disabled( obj ) )
return VLC_EGENERIC;
stream_t *s = (stream_t *)obj;
if( s->s->pf_readdir != NULL )
return VLC_EGENERIC;
struct vlclua_playlist *sys = malloc(sizeof (*sys));
if (unlikely(sys == NULL))
return VLC_ENOMEM;
s->p_sys = sys;
sys->access = NULL;
sys->path = NULL;
if (s->psz_url != NULL)
{ /* Backward compatibility hack: Lua scripts expect the URI scheme and
* the rest of the URI separately. */
const char *p = strstr(s->psz_url, "://");
if (p != NULL)
{
sys->access = strndup(s->psz_url, p - s->psz_url);
sys->path = p + 3;
}
}
int ret = vlclua_scripts_batch_execute(VLC_OBJECT(s), "playlist",
probe_luascript, NULL);
if (ret != VLC_SUCCESS)
{
free(sys->access);
free(sys);
return ret;
}
s->pf_readdir = ReadDir;
s->pf_control = access_vaDirectoryControlHelper;
return VLC_SUCCESS;
}
void Close_LuaPlaylist(vlc_object_t *obj)
{
stream_t *s = (stream_t *)obj;
struct vlclua_playlist *sys = s->p_sys;
free(sys->filename);
assert(sys->L != NULL);
lua_close(sys->L);
free(sys->access);
free(sys);
}