#include <utility>
#include <memory>
+#include "defs.h"
#include "image_input.h"
+#include "mixer.h"
namespace movit {
class ResourcePool;
} // namespace movit
-#define WIDTH 1280 // FIXME
-#define HEIGHT 720 // FIXME
-
using namespace std;
using namespace movit;
-namespace {
+extern Mixer *global_mixer;
-vector<LiveInputWrapper *> live_inputs;
+namespace {
class LuaRefWithDeleter {
public:
return 0;
}
+int LiveInputWrapper_get_width(lua_State* L)
+{
+ assert(lua_gettop(L) == 1);
+ LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
+ lua_pushnumber(L, input->get_width());
+ return 1;
+}
+
+int LiveInputWrapper_get_height(lua_State* L)
+{
+ assert(lua_gettop(L) == 1);
+ LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
+ lua_pushnumber(L, input->get_height());
+ return 1;
+}
+
int ImageInput_new(lua_State* L)
{
assert(lua_gettop(L) == 1);
const luaL_Reg LiveInputWrapper_funcs[] = {
{ "connect_signal", LiveInputWrapper_connect_signal },
+ // These are only valid during calls to get_chain and the setup chain callback.
+ { "get_width", LiveInputWrapper_get_width },
+ { "get_height", LiveInputWrapper_get_height },
{ NULL, NULL }
};
void LiveInputWrapper::connect_signal(int signal_num)
{
- theme->connect_signal(input, signal_num);
+ if (global_mixer == nullptr) {
+ // No data yet.
+ return;
+ }
+
+ signal_num = theme->map_signal(signal_num);
+
+ BufferedFrame frame = theme->input_state->buffered_frames[signal_num][0];
+ const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
+
+ input->set_texture_num(0, userdata->tex_y[frame.field_number]);
+ input->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
+ input->set_width(userdata->last_width[frame.field_number]);
+ input->set_height(userdata->last_height[frame.field_number]);
+}
+
+unsigned LiveInputWrapper::get_width() const
+{
+ if (last_connected_signal_num == -1) {
+ return 0;
+ }
+ BufferedFrame frame = theme->input_state->buffered_frames[last_connected_signal_num][0];
+ const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
+ return userdata->last_width[frame.field_number];
+}
+
+unsigned LiveInputWrapper::get_height() const
+{
+ if (last_connected_signal_num == -1) {
+ return 0;
+ }
+ BufferedFrame frame = theme->input_state->buffered_frames[last_connected_signal_num][0];
+ const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
+ return userdata->last_height[frame.field_number];
}
Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
{
assert(lua_gettop(L) == 0);
- luaL_newmetatable(L, class_name);
+ luaL_newmetatable(L, class_name); // mt = {}
lua_pushlightuserdata(L, this);
- luaL_setfuncs(L, funcs, 1);
+ luaL_setfuncs(L, funcs, 1); // for (name,f in funcs) { mt[name] = f, with upvalue {theme} }
lua_pushvalue(L, -1);
- lua_setfield(L, -2, "__index");
- lua_setglobal(L, class_name);
+ lua_setfield(L, -2, "__index"); // mt.__index = mt
+ lua_setglobal(L, class_name); // ClassName = mt
assert(lua_gettop(L) == 0);
}
-pair<EffectChain *, function<void()>>
-Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
+Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height, InputState input_state)
{
+ Chain chain;
+
unique_lock<mutex> lock(m);
assert(lua_gettop(L) == 0);
lua_getglobal(L, "get_chain"); /* function to be called */
lua_pushnumber(L, width);
lua_pushnumber(L, height);
+ this->input_state = &input_state;
if (lua_pcall(L, 4, 2, 0) != 0) {
fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
exit(1);
}
- EffectChain *chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
+ chain.chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
if (!lua_isfunction(L, -1)) {
fprintf(stderr, "Argument #-1 should be a function\n");
exit(1);
shared_ptr<LuaRefWithDeleter> funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX)));
lua_pop(L, 2);
assert(lua_gettop(L) == 0);
- return make_pair(chain, [this, funcref]{
+
+ chain.setup_chain = [this, funcref, input_state]{
unique_lock<mutex> lock(m);
+ this->input_state = &input_state;
+
// Set up state, including connecting signals.
lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
if (lua_pcall(L, 0, 0, 0) != 0) {
exit(1);
}
assert(lua_gettop(L) == 0);
- });
+ };
+
+ // TODO: Can we do better, e.g. by running setup_chain() and seeing what it references?
+ // Actually, setup_chain does maybe hold all the references we need now anyway?
+ for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
+ for (unsigned frame_num = 0; frame_num < FRAME_HISTORY_LENGTH; ++frame_num) {
+ chain.input_frames.push_back(input_state.buffered_frames[card_index][frame_num].frame);
+ }
+ }
+
+ return chain;
}
std::string Theme::get_channel_name(unsigned channel)
return ret;
}
-void Theme::connect_signal(YCbCrInput *input, int signal_num)
+int Theme::map_signal(int signal_num)
{
if (signal_num >= int(num_cards)) {
if (signals_warned_about.insert(signal_num).second) {
}
signal_num %= num_cards;
}
- input->set_texture_num(0, input_textures[signal_num].tex_y);
- input->set_texture_num(1, input_textures[signal_num].tex_cbcr);
+ return signal_num;
}
void Theme::transition_clicked(int transition_num, float t)