obs-source-factory: Add settings migration code

Previously sources had to manually implement migration code, which resulted in unresolvable regression issues due to the lack of version and commit tagging. With the new migration code, all sources automatically have this version and commit tagging at all times, and as such can now have a temporary regression fixed without the user needing to change any values manually.
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2020-04-05 06:02:03 +02:00 committed by Michael Fabian Dirks
parent 4b5754679e
commit cd5acd42f4
19 changed files with 89 additions and 85 deletions

View file

@ -197,14 +197,16 @@ bool blur::blur_instance::apply_mask_parameters(gs::effect effect, gs_texture_t*
return true;
}
inline void migrate_settings(obs_data_t* settings)
void blur::blur_instance::load(obs_data_t* settings)
{
obs_data_set_default_int(settings, S_VERSION, -1);
int64_t version = obs_data_get_int(settings, S_VERSION);
update(settings);
}
void filter::blur::blur_instance::migrate(obs_data_t* settings, std::uint64_t version)
{
// Now we use a fall-through switch to gradually upgrade each known version change.
switch (version) {
case -1:
case 0:
/// Blur Type
int64_t old_blur = obs_data_get_int(settings, "Filter.Blur.Type");
if (old_blur == 0) { // Box
@ -236,15 +238,10 @@ inline void migrate_settings(obs_data_t* settings)
obs_data_set_double(settings, ST_ANGLE, angle);
obs_data_unset_user_value(settings, "Filter.Blur.Directional.Angle");
}
obs_data_set_int(settings, S_VERSION, STREAMFX_VERSION);
}
void blur::blur_instance::update(obs_data_t* settings)
{
// Ensure backwards compatibility.
migrate_settings(settings);
{ // Blur Type
const char* blur_type = obs_data_get_string(settings, ST_TYPE);
const char* blur_subtype = obs_data_get_string(settings, ST_SUBTYPE);
@ -309,11 +306,6 @@ void blur::blur_instance::update(obs_data_t* settings)
}
}
void blur::blur_instance::load(obs_data_t* settings)
{
update(settings);
}
void blur::blur_instance::video_tick(float)
{
// Blur

View file

@ -99,8 +99,9 @@ namespace filter::blur {
~blur_instance();
public:
virtual void update(obs_data_t* settings) override;
virtual void load(obs_data_t* settings) override;
virtual void migrate(obs_data_t* settings, std::uint64_t version) override;
virtual void update(obs_data_t* settings) override;
virtual void video_tick(float time) override;
virtual void video_render(gs_effect_t* effect) override;

View file

@ -128,6 +128,8 @@ void color_grade::color_grade_instance::load(obs_data_t* data)
update(data);
}
void filter::color_grade::color_grade_instance::migrate(obs_data_t* data, std::uint64_t version) {}
void color_grade::color_grade_instance::update(obs_data_t* data)
{
_lift.x = static_cast<float_t>(obs_data_get_double(data, ST_LIFT_(RED)) / 100.0);

View file

@ -72,6 +72,7 @@ namespace filter::color_grade {
virtual ~color_grade_instance();
virtual void load(obs_data_t* data) override;
virtual void migrate(obs_data_t* data, std::uint64_t version) override;
virtual void update(obs_data_t* data) override;
virtual void video_tick(float time) override;

View file

@ -54,27 +54,20 @@ void displacement::displacement_instance::load(obs_data_t* settings)
update(settings);
}
inline void migrate_settings(obs_data_t* settings)
void filter::displacement::displacement_instance::migrate(obs_data_t* data, std::uint64_t version)
{
uint64_t version = static_cast<uint64_t>(obs_data_get_int(settings, S_VERSION));
switch (version & STREAMFX_MASK_COMPAT) {
case 0:
obs_data_set_double(settings, ST_SCALE, obs_data_get_double(settings, "Filter.Displacement.Scale") * 0.5);
obs_data_set_double(settings, ST_SCALE_TYPE,
obs_data_get_double(settings, "Filter.Displacement.Ratio") * 100.0);
obs_data_unset_user_value(settings, "Filter.Displacement.Ratio");
obs_data_set_double(data, ST_SCALE, obs_data_get_double(data, "Filter.Displacement.Scale") * 0.5);
obs_data_set_double(data, ST_SCALE_TYPE, obs_data_get_double(data, "Filter.Displacement.Ratio") * 100.0);
obs_data_unset_user_value(data, "Filter.Displacement.Ratio");
case STREAMFX_MAKE_VERSION(0, 8, 0, 0):
break;
}
obs_data_set_int(settings, S_VERSION, STREAMFX_VERSION);
}
void displacement::displacement_instance::update(obs_data_t* settings)
{
migrate_settings(settings);
_scale[0] = _scale[1] = static_cast<float_t>(obs_data_get_double(settings, ST_SCALE));
_scale_type = static_cast<float_t>(obs_data_get_double(settings, ST_SCALE_TYPE) / 100.0);

View file

@ -41,6 +41,7 @@ namespace filter::displacement {
virtual ~displacement_instance();
virtual void load(obs_data_t* settings) override;
virtual void migrate(obs_data_t* data, std::uint64_t version) override;
virtual void update(obs_data_t* settings) override;
virtual void video_tick(float_t) override;

View file

@ -74,6 +74,13 @@ dynamic_mask::dynamic_mask_instance::dynamic_mask_instance(obs_data_t* settings,
dynamic_mask::dynamic_mask_instance::~dynamic_mask_instance() {}
void dynamic_mask::dynamic_mask_instance::load(obs_data_t* settings)
{
update(settings);
}
void filter::dynamic_mask::dynamic_mask_instance::migrate(obs_data_t* data, std::uint64_t version) {}
void dynamic_mask::dynamic_mask_instance::update(obs_data_t* settings)
{
// Update source.
@ -135,11 +142,6 @@ void dynamic_mask::dynamic_mask_instance::update(obs_data_t* settings)
}
}
void dynamic_mask::dynamic_mask_instance::load(obs_data_t* settings)
{
update(settings);
}
void dynamic_mask::dynamic_mask_instance::save(obs_data_t* settings)
{
if (_input) {

View file

@ -65,8 +65,9 @@ namespace filter::dynamic_mask {
dynamic_mask_instance(obs_data_t* data, obs_source_t* self);
virtual ~dynamic_mask_instance();
virtual void update(obs_data_t* settings) override;
virtual void load(obs_data_t* settings) override;
virtual void migrate(obs_data_t* data, std::uint64_t version) override;
virtual void update(obs_data_t* settings) override;
virtual void save(obs_data_t* settings) override;
void input_renamed(obs::deprecated_source* src, std::string old_name, std::string new_name);

View file

@ -195,6 +195,8 @@ void filter::nvidia::face_tracking_instance::load(obs_data_t* data)
update(data);
}
void filter::nvidia::face_tracking_instance::migrate(obs_data_t* data, std::uint64_t version) {}
void filter::nvidia::face_tracking_instance::update(obs_data_t* data)
{
_cfg_roi_zoom = obs_data_get_double(data, SK_ROI_ZOOM) / 100.0;

View file

@ -101,6 +101,8 @@ namespace filter::nvidia {
virtual void load(obs_data_t* data) override;
virtual void migrate(obs_data_t* data, std::uint64_t version) override;
virtual void update(obs_data_t* data) override;
virtual void video_tick(float seconds) override;

View file

@ -119,6 +119,13 @@ sdf_effects::sdf_effects_instance::sdf_effects_instance(obs_data_t* settings, ob
sdf_effects::sdf_effects_instance::~sdf_effects_instance() {}
void sdf_effects::sdf_effects_instance::load(obs_data_t* settings)
{
update(settings);
}
void filter::sdf_effects::sdf_effects_instance::migrate(obs_data_t* data, std::uint64_t version) {}
void sdf_effects::sdf_effects_instance::update(obs_data_t* data)
{
{
@ -254,11 +261,6 @@ void sdf_effects::sdf_effects_instance::update(obs_data_t* data)
_sdf_threshold = float_t(obs_data_get_double(data, ST_SDF_THRESHOLD) / 100.0);
}
void sdf_effects::sdf_effects_instance::load(obs_data_t* settings)
{
update(settings);
}
void sdf_effects::sdf_effects_instance::video_tick(float)
{
uint32_t width = 1;

View file

@ -85,8 +85,9 @@ namespace filter::sdf_effects {
sdf_effects_instance(obs_data_t* settings, obs_source_t* self);
virtual ~sdf_effects_instance();
virtual void update(obs_data_t* settings) override;
virtual void load(obs_data_t* settings) override;
virtual void migrate(obs_data_t* data, std::uint64_t version) override;
virtual void update(obs_data_t* settings) override;
virtual void video_tick(float) override;
virtual void video_render(gs_effect_t*) override;

View file

@ -55,6 +55,8 @@ void filter::shader::shader_instance::load(obs_data_t* data)
update(data);
}
void filter::shader::shader_instance::migrate(obs_data_t* data, std::uint64_t version) {}
void filter::shader::shader_instance::update(obs_data_t* data)
{
_fx->update(data);

View file

@ -38,6 +38,7 @@ namespace filter::shader {
void properties(obs_properties_t* props);
virtual void load(obs_data_t* data) override;
virtual void migrate(obs_data_t* data, std::uint64_t version) override;
virtual void update(obs_data_t* data) override;
virtual void video_tick(float_t sec_since_last) override;

View file

@ -113,25 +113,20 @@ transform::transform_instance::~transform_instance()
_mipmap_texture.reset();
}
inline void migrate_settings(obs_data_t* settings)
{
uint64_t version = static_cast<uint64_t>(obs_data_get_int(settings, S_VERSION));
switch (version & STREAMFX_MASK_COMPAT) {
case 0:
obs_data_set_double(settings, ST_ROTATION_X, -obs_data_get_double(settings, ST_ROTATION_X));
obs_data_set_double(settings, ST_ROTATION_Y, -obs_data_get_double(settings, ST_ROTATION_Y));
}
obs_data_set_int(settings, S_VERSION, STREAMFX_VERSION);
}
void transform::transform_instance::load(obs_data_t* settings)
{
migrate_settings(settings);
update(settings);
}
void filter::transform::transform_instance::migrate(obs_data_t* data, std::uint64_t version)
{
switch (version & STREAMFX_MASK_COMPAT) {
case 0:
obs_data_set_double(data, ST_ROTATION_X, -obs_data_get_double(data, ST_ROTATION_X));
obs_data_set_double(data, ST_ROTATION_Y, -obs_data_get_double(data, ST_ROTATION_Y));
}
}
void transform::transform_instance::update(obs_data_t* settings)
{
// Camera

View file

@ -65,6 +65,7 @@ namespace filter::transform {
virtual ~transform_instance() override;
virtual void load(obs_data_t* settings) override;
virtual void migrate(obs_data_t* data, std::uint64_t version) override;
virtual void update(obs_data_t*) override;
virtual void video_tick(float) override;

View file

@ -37,9 +37,9 @@ namespace obs {
_info.destroy = _destroy;
_info.get_defaults2 = _get_defaults2;
_info.get_properties2 = _get_properties2;
_info.load = _load;
_info.update = _update;
_info.save = _save;
_info.load = _load;
_info.filter_remove = _filter_remove;
set_resolution_enabled(true);
@ -251,16 +251,6 @@ namespace obs {
return 0;
}
static void _update(void* data, obs_data_t* settings) noexcept
try {
if (data)
reinterpret_cast<_instance*>(data)->update(settings);
} catch (const std::exception& ex) {
LOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
} catch (...) {
LOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
}
static void _activate(void* data) noexcept
try {
if (data)
@ -357,20 +347,39 @@ namespace obs {
LOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
}
static void _save(void* data, obs_data_t* settings) noexcept
static void _load(void* data, obs_data_t* settings) noexcept
try {
if (data)
reinterpret_cast<_instance*>(data)->save(settings);
auto priv = reinterpret_cast<_instance*>(data);
if (priv) {
std::uint64_t version = static_cast<std::uint64_t>(obs_data_get_int(settings, S_VERSION));
priv->migrate(settings, version);
obs_data_set_int(settings, S_VERSION, static_cast<std::int64_t>(STREAMFX_VERSION));
obs_data_set_string(settings, S_COMMIT, STREAMFX_COMMIT);
priv->load(settings);
}
} catch (const std::exception& ex) {
LOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
} catch (...) {
LOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
}
static void _load(void* data, obs_data_t* settings) noexcept
static void _update(void* data, obs_data_t* settings) noexcept
try {
if (data)
reinterpret_cast<_instance*>(data)->load(settings);
reinterpret_cast<_instance*>(data)->update(settings);
} catch (const std::exception& ex) {
LOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
} catch (...) {
LOG_ERROR("Unexpected exception in function '%s'.", __FUNCTION_NAME__);
}
static void _save(void* data, obs_data_t* settings) noexcept
try {
if (data) {
reinterpret_cast<_instance*>(data)->save(settings);
obs_data_set_int(settings, S_VERSION, static_cast<std::int64_t>(STREAMFX_VERSION));
obs_data_set_string(settings, S_COMMIT, STREAMFX_COMMIT);
}
} catch (const std::exception& ex) {
LOG_ERROR("Unexpected exception in function '%s': %s.", __FUNCTION_NAME__, ex.what());
} catch (...) {
@ -534,8 +543,6 @@ namespace obs {
return 0;
}
virtual void update(obs_data_t* settings) {}
virtual void activate() {}
virtual void deactivate() {}
@ -560,10 +567,14 @@ namespace obs {
virtual void enum_active_sources(obs_source_enum_proc_t enum_callback, void* param) {}
virtual void save(obs_data_t* settings) {}
virtual void load(obs_data_t* settings) {}
virtual void migrate(obs_data_t* settings, std::uint64_t version) {}
virtual void update(obs_data_t* settings) {}
virtual void save(obs_data_t* settings) {}
virtual void mouse_click(const struct obs_mouse_event* event, int32_t type, bool mouse_up, uint32_t click_count)
{}

View file

@ -91,10 +91,13 @@ uint32_t mirror::mirror_instance::get_height()
return _source_size.second;
}
static void convert_config(obs_data_t* data)
void mirror::mirror_instance::load(obs_data_t* data)
{
uint64_t version = static_cast<uint64_t>(obs_data_get_int(data, S_VERSION));
update(data);
}
void source::mirror::mirror_instance::migrate(obs_data_t* data, std::uint64_t version)
{
switch (version) {
case 0:
obs_data_set_int(data, ST_SOURCE_AUDIO_LAYOUT, obs_data_get_int(data, "Source.Mirror.Audio.Layout"));
@ -102,15 +105,10 @@ static void convert_config(obs_data_t* data)
case STREAMFX_VERSION:
break;
}
obs_data_set_int(data, S_VERSION, STREAMFX_VERSION);
obs_data_set_string(data, S_COMMIT, STREAMFX_COMMIT);
}
void mirror::mirror_instance::update(obs_data_t* data)
{
convert_config(data);
// Audio
_audio_enabled = obs_data_get_bool(data, ST_SOURCE_AUDIO);
_audio_layout = static_cast<speaker_layout>(obs_data_get_int(data, ST_SOURCE_AUDIO_LAYOUT));
@ -119,11 +117,6 @@ void mirror::mirror_instance::update(obs_data_t* data)
acquire(obs_data_get_string(data, ST_SOURCE));
}
void mirror::mirror_instance::load(obs_data_t* data)
{
update(data);
}
void mirror::mirror_instance::save(obs_data_t* data)
{
if (_source) {

View file

@ -61,8 +61,9 @@ namespace source::mirror {
virtual uint32_t get_width() override;
virtual uint32_t get_height() override;
virtual void update(obs_data_t*) override;
virtual void load(obs_data_t*) override;
virtual void migrate(obs_data_t*, std::uint64_t) override;
virtual void update(obs_data_t*) override;
virtual void save(obs_data_t*) override;
virtual void video_tick(float) override;