|
Packit |
3ff1e7 |
/* libquvi
|
|
Packit |
3ff1e7 |
* Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
|
|
Packit |
3ff1e7 |
*
|
|
Packit |
3ff1e7 |
* This file is part of libquvi <http://quvi.sourceforge.net/>.
|
|
Packit |
3ff1e7 |
*
|
|
Packit |
3ff1e7 |
* This library is free software: you can redistribute it and/or
|
|
Packit |
3ff1e7 |
* modify it under the terms of the GNU Affero General Public
|
|
Packit |
3ff1e7 |
* License as published by the Free Software Foundation, either
|
|
Packit |
3ff1e7 |
* version 3 of the License, or (at your option) any later version.
|
|
Packit |
3ff1e7 |
*
|
|
Packit |
3ff1e7 |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
3ff1e7 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
3ff1e7 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
3ff1e7 |
* GNU Affero General Public License for more details.
|
|
Packit |
3ff1e7 |
*
|
|
Packit |
3ff1e7 |
* You should have received a copy of the GNU Affero General
|
|
Packit |
3ff1e7 |
* Public License along with this library. If not, see
|
|
Packit |
3ff1e7 |
* <http://www.gnu.org/licenses/>.
|
|
Packit |
3ff1e7 |
*/
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
/*
|
|
Packit |
3ff1e7 |
* NOTE: The error messages produced in these functions are intended for
|
|
Packit |
3ff1e7 |
* developers. They would typically be seen when a new script is
|
|
Packit |
3ff1e7 |
* being developed or an old one is being maintained.
|
|
Packit |
3ff1e7 |
*
|
|
Packit |
3ff1e7 |
* These messages should be clear, indicating the actual error,
|
|
Packit |
3ff1e7 |
* minimizing the time spent on locating the problem in the script.
|
|
Packit |
3ff1e7 |
*/
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
#include "config.h"
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
#include <lauxlib.h>
|
|
Packit |
3ff1e7 |
#include <glib.h>
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
#include "quvi.h"
|
|
Packit |
3ff1e7 |
/* -- */
|
|
Packit |
3ff1e7 |
#include "_quvi_s.h"
|
|
Packit |
3ff1e7 |
#include "_quvi_media_s.h"
|
|
Packit |
3ff1e7 |
#include "_quvi_script_s.h"
|
|
Packit |
3ff1e7 |
/* -- */
|
|
Packit |
3ff1e7 |
#include "lua/setfield.h"
|
|
Packit |
3ff1e7 |
#include "lua/chk.h"
|
|
Packit |
3ff1e7 |
#include "lua/def.h"
|
|
Packit |
3ff1e7 |
#include "misc/re.h"
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
static const gchar script_func[] = "parse";
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
static void _foreach_video_property(lua_State *l, _quvi_media_t qm,
|
|
Packit |
3ff1e7 |
_quvi_media_stream_t qms)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
lua_pushnil(l);
|
|
Packit |
3ff1e7 |
while (lua_next(l, LI_KEY))
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
l_chk_assign_n(l, MSS_VIDEO_BITRATE_KBIT_S, &qms->video.bitrate_kbit_s);
|
|
Packit |
3ff1e7 |
l_chk_assign_s(l, MSS_VIDEO_ENCODING, qms->video.encoding, TRUE, FALSE);
|
|
Packit |
3ff1e7 |
l_chk_assign_n(l, MSS_VIDEO_HEIGHT, &qms->video.height);
|
|
Packit |
3ff1e7 |
l_chk_assign_n(l, MSS_VIDEO_WIDTH, &qms->video.width);
|
|
Packit |
3ff1e7 |
lua_pop(l, 1);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
static void _foreach_audio_property(lua_State *l, _quvi_media_t qm,
|
|
Packit |
3ff1e7 |
_quvi_media_stream_t qms)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
lua_pushnil(l);
|
|
Packit |
3ff1e7 |
while (lua_next(l, LI_KEY))
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
l_chk_assign_n(l, MSS_AUDIO_BITRATE_KBIT_S, &qms->audio.bitrate_kbit_s);
|
|
Packit |
3ff1e7 |
l_chk_assign_s(l, MSS_AUDIO_ENCODING, qms->audio.encoding, TRUE, FALSE);
|
|
Packit |
3ff1e7 |
lua_pop(l, 1);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
static void _foreach_flag_property(lua_State *l, _quvi_media_t qm,
|
|
Packit |
3ff1e7 |
_quvi_media_stream_t qms)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
lua_pushnil(l);
|
|
Packit |
3ff1e7 |
while (lua_next(l, LI_KEY))
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
l_chk_assign_b(l, MSS_FLAGS_BEST, &qms->flags.best);
|
|
Packit |
3ff1e7 |
lua_pop(l, 1);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
typedef void (*foreach_cb)(lua_State*, _quvi_media_t, _quvi_media_stream_t);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
static void _chk_stream_sublevel(const gchar *level, lua_State *l,
|
|
Packit |
3ff1e7 |
_quvi_media_t qm, _quvi_media_stream_t qms,
|
|
Packit |
3ff1e7 |
foreach_cb cb)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
if (lua_isstring(l, LI_KEY) && lua_istable(l, LI_VALUE))
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
if (g_strcmp0(lua_tostring(l, LI_KEY), level) == 0)
|
|
Packit |
3ff1e7 |
cb(l, qm, qms);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
static gpointer _media_stream_new()
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
_quvi_media_stream_t qms = g_new0(struct _quvi_media_stream_s, 1);
|
|
Packit |
3ff1e7 |
qms->video.encoding = g_string_new(NULL);
|
|
Packit |
3ff1e7 |
qms->audio.encoding = g_string_new(NULL);
|
|
Packit |
3ff1e7 |
qms->container = g_string_new(NULL);
|
|
Packit |
3ff1e7 |
qms->url = g_string_new(NULL);
|
|
Packit |
3ff1e7 |
qms->id = g_string_new(NULL);
|
|
Packit |
3ff1e7 |
return (qms);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
static void _has_stream_url(lua_State *l, _quvi_media_stream_t qms,
|
|
Packit |
3ff1e7 |
const gchar *script_path, const gint i)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
if (qms->url->len ==0)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
static const gchar *_E =
|
|
Packit |
3ff1e7 |
"%s: %s: must return a media stream URL in `qargs.%s[%d].%s'";
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
luaL_error(l, _E, script_path, script_func, MS_STREAMS, i, MSS_URL);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
static _quvi_media_stream_t _new_stream(lua_State *l, _quvi_media_t qm,
|
|
Packit |
3ff1e7 |
const gchar *script_path,
|
|
Packit |
3ff1e7 |
const gint i)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
_quvi_media_stream_t qms = _media_stream_new();
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
lua_pushnil(l);
|
|
Packit |
3ff1e7 |
while (lua_next(l, LI_KEY))
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
_chk_stream_sublevel(MSS_VIDEO, l, qm, qms, _foreach_video_property);
|
|
Packit |
3ff1e7 |
_chk_stream_sublevel(MSS_AUDIO, l, qm, qms, _foreach_audio_property);
|
|
Packit |
3ff1e7 |
_chk_stream_sublevel(MSS_FLAGS, l, qm, qms, _foreach_flag_property);
|
|
Packit |
3ff1e7 |
l_chk_assign_s(l, MSS_CONTAINER, qms->container, TRUE, FALSE);
|
|
Packit |
3ff1e7 |
l_chk_assign_s(l, MSS_URL, qms->url, TRUE, TRUE);
|
|
Packit |
3ff1e7 |
l_chk_assign_s(l, MSS_ID, qms->id, TRUE, FALSE);
|
|
Packit |
3ff1e7 |
lua_pop(l, 1);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
_has_stream_url(l, qms, script_path, i);
|
|
Packit |
3ff1e7 |
return (qms);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
static void _chk_stream_ids(lua_State *l, _quvi_media_t qm,
|
|
Packit |
3ff1e7 |
const gchar *script_path)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
_quvi_media_stream_t qms;
|
|
Packit |
3ff1e7 |
GSList *curr;
|
|
Packit |
3ff1e7 |
gint i;
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (g_slist_length(qm->streams) < 2)
|
|
Packit |
3ff1e7 |
return;
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
curr = qm->streams;
|
|
Packit |
3ff1e7 |
i = 1;
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
while (curr != NULL)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
qms = (_quvi_media_stream_t) curr->data;
|
|
Packit |
3ff1e7 |
if (qms->id->len ==0)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
g_warning("%s: %s: `qargs.%s[%d].%s' should not be empty; "
|
|
Packit |
3ff1e7 |
"each stream should have an ID when there are >1 "
|
|
Packit |
3ff1e7 |
"streams",
|
|
Packit |
3ff1e7 |
script_path, script_func, MS_STREAMS, i, MSS_ID);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
curr = g_slist_next(curr);
|
|
Packit |
3ff1e7 |
++i;
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
static void _foreach_stream(lua_State *l, _quvi_media_t qm,
|
|
Packit |
3ff1e7 |
const gchar *script_path)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
gint i = 0;
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
lua_pushnil(l);
|
|
Packit |
3ff1e7 |
while (lua_next(l, LI_KEY))
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
if (lua_istable(l, LI_VALUE))
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
_quvi_media_stream_t qms = _new_stream(l, qm, script_path, ++i);
|
|
Packit |
3ff1e7 |
qm->streams = g_slist_prepend(qm->streams, qms);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
lua_pop(l, 1);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
qm->streams = g_slist_reverse(qm->streams);
|
|
Packit |
3ff1e7 |
_chk_stream_ids(l, qm, script_path);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
/* Check for 'qargs.streams'. This is the only mandatory data. */
|
|
Packit |
3ff1e7 |
static void _chk_streams(lua_State *l, _quvi_media_t qm,
|
|
Packit |
3ff1e7 |
const gchar *script_path)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
lua_pushstring(l, MS_STREAMS);
|
|
Packit |
3ff1e7 |
lua_gettable(l, LI_KEY);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (lua_istable(l, LI_VALUE))
|
|
Packit |
3ff1e7 |
_foreach_stream(l, qm, script_path);
|
|
Packit |
3ff1e7 |
else
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
static const gchar *_E =
|
|
Packit |
3ff1e7 |
"%s: %s: must return a dictionary containing the `qargs.%s'";
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
luaL_error(l, _E, script_path, script_func, MS_STREAMS);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
lua_pop(l, 1);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (g_slist_length(qm->streams) ==0)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
luaL_error(l, "%s: %s: must return at least one media stream",
|
|
Packit |
3ff1e7 |
script_path, script_func);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
/* Check for optional media properties, e.g. title. */
|
|
Packit |
3ff1e7 |
static void _chk_optional(lua_State *l, _quvi_media_t qm)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
lua_pushnil(l);
|
|
Packit |
3ff1e7 |
while (lua_next(l, LI_KEY))
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
l_chk_assign_n(l, MS_START_TIME_MS, &qm->start_time_ms);
|
|
Packit |
3ff1e7 |
l_chk_assign_n(l, MS_DURATION_MS, &qm->duration_ms);
|
|
Packit |
3ff1e7 |
l_chk_assign_s(l, MS_THUMB_URL, qm->url.thumbnail, TRUE, TRUE);
|
|
Packit |
3ff1e7 |
l_chk_assign_s(l, MS_TITLE, qm->title, TRUE, FALSE);
|
|
Packit |
3ff1e7 |
l_chk_assign_s(l, MS_ID, qm->id, TRUE, FALSE);
|
|
Packit |
3ff1e7 |
lua_pop(l, 1);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
/* Check for the goto URL instruction. */
|
|
Packit |
3ff1e7 |
static gboolean _chk_goto_instr(lua_State *l, _quvi_media_t qm)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
lua_pushnil(l);
|
|
Packit |
3ff1e7 |
while (lua_next(l, LI_KEY))
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
l_chk_assign_s(l, MS_GOTO_URL, qm->url.redirect_to, TRUE, TRUE);
|
|
Packit |
3ff1e7 |
lua_pop(l, 1);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
return ((qm->url.redirect_to->len >0) ? TRUE:FALSE);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
extern QuviError l_exec_util_convert_entities(_quvi_media_t);
|
|
Packit |
3ff1e7 |
extern gint c_reset(_quvi_t);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
QuviError l_exec_media_script_parse(gpointer p, GSList *sl)
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
_quvi_script_t qs;
|
|
Packit |
3ff1e7 |
_quvi_media_t qm;
|
|
Packit |
3ff1e7 |
lua_State *l;
|
|
Packit |
3ff1e7 |
QuviError rc;
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
qm = (_quvi_media_t) p;
|
|
Packit |
3ff1e7 |
rc = QUVI_OK;
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
l = qm->handle.quvi->handle.lua;
|
|
Packit |
3ff1e7 |
c_reset(qm->handle.quvi);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
qs = (_quvi_script_t) sl->data;
|
|
Packit |
3ff1e7 |
lua_getglobal(l, script_func);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (!lua_isfunction(l, -1))
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
luaL_error(l, "%s: the function `%s' was not found",
|
|
Packit |
3ff1e7 |
qs->fpath->str, script_func);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
lua_newtable(l);
|
|
Packit |
3ff1e7 |
l_set_reg_userdata(l, USERDATA_QUVI_T, (gpointer) qm->handle.quvi);
|
|
Packit |
3ff1e7 |
l_setfield_s(l, MS_INPUT_URL, qm->url.input->str, -1);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (lua_pcall(l, 1, 1, 0))
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
g_string_assign(qm->handle.quvi->status.errmsg, lua_tostring(l, -1));
|
|
Packit |
3ff1e7 |
return (QUVI_ERROR_SCRIPT);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (!lua_istable(l, -1))
|
|
Packit |
3ff1e7 |
{
|
|
Packit |
3ff1e7 |
static const gchar *_E =
|
|
Packit |
3ff1e7 |
"%s: %s: must return a dictionary, this is typically the `qargs'";
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
luaL_error(l, _E, qs->fpath->str, script_func);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (_chk_goto_instr(l, qm) == FALSE)
|
|
Packit |
3ff1e7 |
_chk_streams(l, qm, qs->fpath->str);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
_chk_optional(l, qm);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
if (qm->title->len >0)
|
|
Packit |
3ff1e7 |
rc = l_exec_util_convert_entities(qm);
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
lua_pop(l, 1);
|
|
Packit |
3ff1e7 |
return (rc);
|
|
Packit |
3ff1e7 |
}
|
|
Packit |
3ff1e7 |
|
|
Packit |
3ff1e7 |
/* vim: set ts=2 sw=2 tw=72 expandtab: */
|