X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Ftheme.cpp;h=ade4e8fd2dc7f8f6359c9bea8acf05e5e7ccf748;hb=09886e5989fe0dc2516bcadeacae4269ac22f03a;hp=8050003b2025459651d20a99dea5f4d120347da3;hpb=2ba69dc78d091ad92427389147365f39760e0b1f;p=nageru diff --git a/nageru/theme.cpp b/nageru/theme.cpp index 8050003..ade4e8f 100644 --- a/nageru/theme.cpp +++ b/nageru/theme.cpp @@ -29,6 +29,7 @@ #include #include +#include "audio_mixer.h" #include "defs.h" #ifdef HAVE_CEF #include "cef_capture.h" @@ -38,6 +39,7 @@ #include "image_input.h" #include "input_state.h" #include "lua_utils.h" +#include "mainwindow.h" #include "pbo_frame_allocator.h" #include "scene.h" @@ -52,6 +54,9 @@ using namespace movit; extern Mixer *global_mixer; +constexpr unsigned Theme::MenuEntry::CHECKABLE; +constexpr unsigned Theme::MenuEntry::CHECKED; + Theme *get_theme_updata(lua_State* L) { luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA); @@ -116,6 +121,7 @@ Effect *instantiate_effect(EffectChain *chain, EffectType effect_type) case IDENTITY_EFFECT: return new IdentityEffect; case WHITE_BALANCE_EFFECT: + case AUTO_WHITE_BALANCE_EFFECT: return new WhiteBalanceEffect; case RESAMPLE_EFFECT: return new ResampleEffect; @@ -636,6 +642,20 @@ int InputStateInfo_get_height(lua_State* L) return 1; } +int InputStateInfo_get_frame_height(lua_State* L) +{ + assert(lua_gettop(L) == 2); + InputStateInfo *input_state_info = get_input_state_info(L, 1); + Theme *theme = get_theme_updata(L); + int signal_num = theme->map_signal(luaL_checknumber(L, 2)); + unsigned height = input_state_info->last_height[signal_num]; + if (input_state_info->last_interlaced[signal_num]) { + height *= 2; + } + lua_pushnumber(L, height); + return 1; +} + int InputStateInfo_get_interlaced(lua_State* L) { assert(lua_gettop(L) == 2); @@ -745,10 +765,10 @@ int InputStateInfo_get_human_readable_resolution(lua_State* L) string str; if (!input_state_info->last_is_connected[signal_num]) { str = "disconnected"; - } else if (input_state_info->last_height[signal_num]) { + } else if (input_state_info->last_height[signal_num] <= 0) { str = "no signal"; } else if (!input_state_info->last_has_signal[signal_num]) { - if (input_state_info->last_height[signal_num]) { + if (input_state_info->last_height[signal_num] == 525) { // Special mode for the USB3 cards. str = "no signal"; } else { @@ -846,6 +866,7 @@ const luaL_Reg Scene_funcs[] = { { "new", Scene_new }, { "__gc", Scene_gc }, { "add_input", Scene::add_input }, + { "add_auto_white_balance", Scene::add_auto_white_balance }, { "add_effect", Scene::add_effect }, { "add_optional_effect", Scene::add_optional_effect }, { "finalize", Scene::finalize }, @@ -858,6 +879,8 @@ const luaL_Reg Block_funcs[] = { { "enable", Block_enable }, { "enable_if", Block_enable_if }, { "disable", Block_disable }, + { "always_disable_if_disabled", Block_always_disable_if_disabled }, + { "promise_to_disable_if_enabled", Block_promise_to_disable_if_enabled }, { "set_int", Block_set_int }, { "set_float", Block_set_float }, { "set_vec3", Block_set_vec3 }, @@ -978,6 +1001,8 @@ const luaL_Reg LiftGammaGainEffect_funcs[] = { const luaL_Reg InputStateInfo_funcs[] = { { "get_width", InputStateInfo_get_width }, { "get_height", InputStateInfo_get_height }, + { "get_frame_width", InputStateInfo_get_width }, // Same as get_width(). + { "get_frame_height", InputStateInfo_get_frame_height }, { "get_interlaced", InputStateInfo_get_interlaced }, { "get_has_signal", InputStateInfo_get_has_signal }, { "get_is_connected", InputStateInfo_get_is_connected }, @@ -1212,6 +1237,7 @@ int call_num_channels(lua_State *L) if (lua_pcall(L, 0, 1, 0) != 0) { fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1)); + fprintf(stderr, "Try Nageru.set_num_channels(...) at the start of the script instead.\n"); abort(); } @@ -1223,9 +1249,176 @@ int call_num_channels(lua_State *L) } // namespace +int Nageru_set_channel_name(lua_State *L) +{ + // NOTE: m is already locked. + Theme *theme = get_theme_updata(L); + unsigned channel = luaL_checknumber(L, 1); + const string text = checkstdstring(L, 2); + theme->channel_names[channel] = text; + lua_pop(L, 2); + return 0; +} + +int Nageru_set_num_channels(lua_State *L) +{ + // NOTE: m is already locked. + Theme *theme = get_theme_updata(L); + if (theme->startup_finished) { + luaL_error(L, "set_num_channels() can only be called at startup."); + } + theme->num_channels = luaL_checknumber(L, 1); + lua_pop(L, 1); + return 0; +} + +int Nageru_set_channel_signal(lua_State *L) +{ + // NOTE: m is already locked. + Theme *theme = get_theme_updata(L); + if (theme->startup_finished) { + luaL_error(L, "set_channel_signal() can only be called at startup."); + } + unsigned channel = luaL_checknumber(L, 1); + int signal = luaL_checknumber(L, 2); + theme->channel_signals[channel] = signal; + lua_pop(L, 2); + return 0; +} + +int Nageru_set_supports_wb(lua_State *L) +{ + // NOTE: m is already locked. + Theme *theme = get_theme_updata(L); + if (theme->startup_finished) { + luaL_error(L, "set_supports_wb() can only be called at startup."); + } + unsigned channel = luaL_checknumber(L, 1); + bool supports_wb = checkbool(L, 2); + theme->channel_supports_wb[channel] = supports_wb; + lua_pop(L, 2); + return 0; +} + +// NOTE: There's a race condition in all of the audio functions; if the mapping +// is changed by the user underway, you might not be manipulating the bus you +// expect. (You should not get crashes, though.) There's not all that much we +// can do about it, short of locking the entire mixer while anything from the +// theme runs. + +int Nageru_get_num_audio_buses(lua_State *L) +{ + if (global_audio_mixer == nullptr) { + // The audio mixer isn't set up until we know how many FFmpeg inputs we have. + luaL_error(L, "Audio functions can not be called before the theme is done initializing."); + } + lua_pushinteger(L, global_audio_mixer->num_buses()); + return 1; +} + +int Nageru_get_audio_bus_name(lua_State *L) +{ + if (global_audio_mixer == nullptr) { + // The audio mixer isn't set up until we know how many FFmpeg inputs we have. + luaL_error(L, "Audio functions can not be called before the theme is done initializing."); + } + int bus_index = luaL_checknumber(L, 1); + InputMapping input_mapping = global_audio_mixer->get_input_mapping(); + if (bus_index < 0 || size_t(bus_index) >= input_mapping.buses.size()) { + // Doesn't fix the race, but fixes other out-of-bounds. + print_warning(L, "Theme called get_audio_bus_name() on nonexistent bus %d; returning nil.\n", bus_index); + lua_pushnil(L); + } else { + lua_pushstring(L, input_mapping.buses[bus_index].name.c_str()); + } + return 1; +} + +int Nageru_get_audio_bus_fader_level_db(lua_State *L) +{ + if (global_audio_mixer == nullptr) { + // The audio mixer isn't set up until we know how many FFmpeg inputs we have. + luaL_error(L, "Audio functions can not be called before the theme is done initializing."); + } + + int bus_index = luaL_checknumber(L, 1); + if (bus_index < 0 || size_t(bus_index) >= global_audio_mixer->num_buses()) { + // Doesn't fix the race, but fixes other out-of-bounds. + print_warning(L, "Theme called get_audio_bus_fader_level_db() on nonexistent bus %d; returning 0.0.\n", bus_index); + lua_pushnumber(L, 0.0); + } else { + lua_pushnumber(L, global_audio_mixer->get_fader_volume(bus_index)); + } + return 1; +} + +int Nageru_set_audio_bus_fader_level_db(lua_State *L) +{ + if (global_audio_mixer == nullptr || global_mainwindow == nullptr) { + // The audio mixer isn't set up until we know how many FFmpeg inputs we have. + luaL_error(L, "Audio functions can not be called before the theme is done initializing."); + } + + int bus_index = luaL_checknumber(L, 1); + if (bus_index < 0 || size_t(bus_index) >= global_audio_mixer->num_buses()) { + // Doesn't fix the race, but fixes other out-of-bounds. + print_warning(L, "Theme called set_audio_bus_fader_level_db() on nonexistent bus %d; ignoring.\n", bus_index); + return 0; + } + double level_db = luaL_checknumber(L, 2); + + // Go through the UI, so that it gets updated. + global_mainwindow->set_fader_absolute(bus_index, level_db); + return 0; +} + +int Nageru_get_audio_bus_mute(lua_State *L) +{ + if (global_audio_mixer == nullptr) { + // The audio mixer isn't set up until we know how many FFmpeg inputs we have. + luaL_error(L, "Audio functions can not be called before the theme is done initializing."); + } + + int bus_index = luaL_checknumber(L, 1); + if (bus_index < 0 || size_t(bus_index) >= global_audio_mixer->num_buses()) { + // Doesn't fix the race, but fixes other out-of-bounds. + print_warning(L, "Theme called get_audio_bus_mute() on nonexistent bus %d; returning false.\n", bus_index); + lua_pushboolean(L, false); + } else { + lua_pushboolean(L, global_audio_mixer->get_mute(bus_index)); + } + return 1; +} + +int Nageru_set_audio_bus_mute(lua_State *L) +{ + if (global_audio_mixer == nullptr || global_mainwindow == nullptr) { + // The audio mixer isn't set up until we know how many FFmpeg inputs we have. + luaL_error(L, "Audio functions can not be called before the theme is done initializing."); + } + + int bus_index = luaL_checknumber(L, 1); + if (bus_index < 0 || size_t(bus_index) >= global_audio_mixer->num_buses()) { + // Doesn't fix the race, but fixes other out-of-bounds. + print_warning(L, "Theme called set_audio_bus_mute() on nonexistent bus %d; ignoring.\n", bus_index); + return 0; + } + bool mute = checkbool(L, 2); + + // Go through the UI, so that it gets updated. + if (mute != global_audio_mixer->get_mute(bus_index)) { + global_mainwindow->toggle_mute(bus_index); + } + return 0; +} + Theme::Theme(const string &filename, const vector &search_dirs, ResourcePool *resource_pool, unsigned num_cards) : resource_pool(resource_pool), num_cards(num_cards), signal_to_card_mapping(global_flags.default_stream_mapping) { + // Defaults. + channel_names[0] = "Live"; + channel_names[1] = "Preview"; + L = luaL_newstate(); luaL_openlibs(L); @@ -1293,7 +1486,7 @@ Theme::Theme(const string &filename, const vector &search_dirs, Resource } // Set up the API we provide. - register_constants(); + register_globals(); register_class("Scene", Scene_funcs); register_class("Block", Block_funcs); register_class("EffectBlueprint", EffectBlueprint_funcs); @@ -1324,8 +1517,11 @@ Theme::Theme(const string &filename, const vector &search_dirs, Resource } assert(lua_gettop(L) == 0); - // Ask it for the number of channels. - num_channels = call_num_channels(L); + if (num_channels == -1) { + // Ask it for the number of channels. + num_channels = call_num_channels(L); + } + startup_finished = true; } Theme::~Theme() @@ -1334,12 +1530,14 @@ Theme::~Theme() lua_close(L); } -void Theme::register_constants() +void Theme::register_globals() { // Set Nageru.VIDEO_FORMAT_BGRA = bmusb::PixelFormat_8BitBGRA, etc. const vector> num_constants = { { "VIDEO_FORMAT_BGRA", bmusb::PixelFormat_8BitBGRA }, { "VIDEO_FORMAT_YCBCR", bmusb::PixelFormat_8BitYCbCrPlanar }, + { "CHECKABLE", MenuEntry::CHECKABLE }, + { "CHECKED", MenuEntry::CHECKED }, }; const vector> str_constants = { { "THEME_PATH", theme_path }, @@ -1358,6 +1556,26 @@ void Theme::register_constants() lua_settable(L, 1); // t[key] = value } + const luaL_Reg Nageru_funcs[] = { + // Channel information. + { "set_channel_name", Nageru_set_channel_name }, + { "set_num_channels", Nageru_set_num_channels }, + { "set_channel_signal", Nageru_set_channel_signal }, + { "set_supports_wb", Nageru_set_supports_wb }, + + // Audio. + { "get_num_audio_buses", Nageru_get_num_audio_buses }, + { "get_audio_bus_name", Nageru_get_audio_bus_name }, + { "get_audio_bus_fader_level_db", Nageru_get_audio_bus_fader_level_db }, + { "set_audio_bus_fader_level_db", Nageru_set_audio_bus_fader_level_db }, + { "get_audio_bus_mute", Nageru_get_audio_bus_mute }, + { "set_audio_bus_mute", Nageru_set_audio_bus_mute }, + + { nullptr, nullptr } + }; + lua_pushlightuserdata(L, this); + luaL_setfuncs(L, Nageru_funcs, 1); // for (name,f in funcs) { mt[name] = f, with upvalue {theme} } + lua_setglobal(L, "Nageru"); // Nageru = t assert(lua_gettop(L) == 0); } @@ -1484,7 +1702,23 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he string Theme::get_channel_name(unsigned channel) { lock_guard lock(m); + + // We never ask the legacy channel_name() about live and preview. + // The defaults are set in our constructor. + if (channel == 0 || channel == 1) { + return channel_names[channel]; + } + lua_getglobal(L, "channel_name"); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + if (channel_names.count(channel)) { + return channel_names[channel]; + } else { + return "(no title)"; + } + } + lua_pushnumber(L, channel); if (lua_pcall(L, 1, 1, 0) != 0) { fprintf(stderr, "error running function `channel_name': %s\n", lua_tostring(L, -1)); @@ -1493,6 +1727,7 @@ string Theme::get_channel_name(unsigned channel) const char *ret = lua_tostring(L, -1); if (ret == nullptr) { fprintf(stderr, "function `channel_name' returned nil for channel %d\n", channel); + fprintf(stderr, "Try Nageru.set_channel_name(channel, name) at the start of the script instead.\n"); abort(); } @@ -1506,9 +1741,19 @@ int Theme::get_channel_signal(unsigned channel) { lock_guard lock(m); lua_getglobal(L, "channel_signal"); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + if (channel_signals.count(channel)) { + return channel_signals[channel]; + } else { + return -1; + } + } + lua_pushnumber(L, channel); if (lua_pcall(L, 1, 1, 0) != 0) { fprintf(stderr, "error running function `channel_signal': %s\n", lua_tostring(L, -1)); + fprintf(stderr, "Try Nageru.set_channel_signal(channel, signal) at the start of the script instead.\n"); abort(); } @@ -1544,9 +1789,19 @@ bool Theme::get_supports_set_wb(unsigned channel) { lock_guard lock(m); lua_getglobal(L, "supports_set_wb"); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + if (channel_supports_wb.count(channel)) { + return channel_supports_wb[channel]; + } else { + return false; + } + } + lua_pushnumber(L, channel); if (lua_pcall(L, 1, 1, 0) != 0) { fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1)); + fprintf(stderr, "Try Nageru.set_supports_wb(channel, bool) at the start of the script instead.\n"); abort(); } @@ -1556,10 +1811,39 @@ bool Theme::get_supports_set_wb(unsigned channel) return ret; } -void Theme::set_wb(unsigned channel, double r, double g, double b) +void Theme::set_wb(unsigned channel, float r, float g, float b) { + int signal = get_channel_signal(channel); + lock_guard lock(m); + if (signal != -1) { + white_balance_for_signal[signal] = RGBTriplet{ r, g, b }; + } + + call_lua_wb_callback(channel, r, g, b); +} + +void Theme::set_wb_for_signal(int signal, float r, float g, float b) +{ + lock_guard lock(m); + white_balance_for_signal[signal] = RGBTriplet{ r, g, b }; + + for (const auto &channel_and_signal : channel_signals) { + if (channel_and_signal.second == signal) { + call_lua_wb_callback(channel_and_signal.first, r, g, b); + } + } +} + +void Theme::call_lua_wb_callback(unsigned channel, float r, float g, float b) +{ lua_getglobal(L, "set_wb"); + if (lua_isnil(L, -1)) { + // The function doesn't exist, to just ignore. We've stored the white balance, + // and most likely, it will be picked up by auto white balance instead. + lua_pop(L, 1); + return; + } lua_pushnumber(L, channel); lua_pushnumber(L, r); lua_pushnumber(L, g); @@ -1572,6 +1856,15 @@ void Theme::set_wb(unsigned channel, double r, double g, double b) assert(lua_gettop(L) == 0); } +RGBTriplet Theme::get_white_balance_for_signal(int signal) +{ + if (white_balance_for_signal.count(signal)) { + return white_balance_for_signal[signal]; + } else { + return RGBTriplet{ 1.0, 1.0, 1.0 }; + } +} + vector Theme::get_transition_names(float t) { lock_guard lock(m); @@ -1671,9 +1964,9 @@ void destroy(T &ref) Theme::MenuEntry::~MenuEntry() { if (is_submenu) { - luaL_unref(entry.L, LUA_REGISTRYINDEX, entry.lua_ref); - } else { destroy(submenu); + } else { + luaL_unref(entry.L, LUA_REGISTRYINDEX, entry.lua_ref); } } @@ -1689,6 +1982,13 @@ unique_ptr create_theme_menu_entry(lua_State *L, int index) const string text = checkstdstring(L, -1); lua_pop(L, 1); + unsigned flags = 0; + if (lua_objlen(L, -1) > 2) { + lua_rawgeti(L, -1, 3); + flags = luaL_checknumber(L, -1); + lua_pop(L, 1); + } + lua_rawgeti(L, index, 2); if (lua_istable(L, -1)) { vector> submenu = create_recursive_theme_menu(L); @@ -1697,7 +1997,7 @@ unique_ptr create_theme_menu_entry(lua_State *L, int index) } else { luaL_checktype(L, -1, LUA_TFUNCTION); int ref = luaL_ref(L, LUA_REGISTRYINDEX); - entry.reset(new Theme::MenuEntry{ text, L, ref }); + entry.reset(new Theme::MenuEntry{ text, L, ref, flags }); } return entry; } @@ -1725,9 +2025,7 @@ int Theme::set_theme_menu(lua_State *L) for (int i = 1; i <= num_elements; ++i) { root_menu.emplace_back(create_theme_menu_entry(L, i)); } - fprintf(stderr, "now creating a new one\n"); theme_menu.reset(new MenuEntry("", move(root_menu))); - fprintf(stderr, "DONE reset\n"); lua_pop(L, num_elements); assert(lua_gettop(L) == 0); @@ -1748,3 +2046,24 @@ void Theme::theme_menu_entry_clicked(int lua_ref) abort(); } } + +string Theme::format_status_line(const string &disk_space_left_text, double file_length_seconds) +{ + lock_guard lock(m); + lua_getglobal(L, "format_status_line"); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + return disk_space_left_text; + } + + lua_pushstring(L, disk_space_left_text.c_str()); + lua_pushnumber(L, file_length_seconds); + if (lua_pcall(L, 2, 1, 0) != 0) { + fprintf(stderr, "error running function format_status_line(): %s\n", lua_tostring(L, -1)); + abort(); + } + string text = checkstdstring(L, 1); + lua_pop(L, 1); + assert(lua_gettop(L) == 0); + return text; +}