Skip to content

Commit

Permalink
ptz: Move control limits to obs-ptz backend
Browse files Browse the repository at this point in the history
The control ranges depend on the model. Hence, the properties should be
provided by each backend. The plugin obs-ptz assumes SRG-120DH so that
the ranges can be fixed values.
  • Loading branch information
norihiro committed Aug 31, 2024
1 parent 3219799 commit 8309b4b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
19 changes: 6 additions & 13 deletions src/face-tracker-ptz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,16 @@ static void ftptz_update(void *data, obs_data_t *settings)
debug_data_open(&s->debug_data_error, &s->debug_data_error_last, settings, "debug_data_error");
debug_data_open(&s->debug_data_control, &s->debug_data_control_last, settings, "debug_data_control");

s->ptz_max_x = obs_data_get_int(settings, "ptz_max_x");
s->ptz_max_y = obs_data_get_int(settings, "ptz_max_y");
s->ptz_max_z = obs_data_get_int(settings, "ptz_max_z");

static const struct {
const char *old_name;
const char *new_name;
} renames[] = {
{ "ptz-obsptz-device_id", "ptz.obsptz.device_id" },
{ "ptz-viscaip-address", "ptz.visca-over-tcp.address" },
{ "ptz-viscaip-port", "ptz.visca-over-tcp.port" },
{ "ptz_max_x", "ptz.obsptz.max_x" },
{ "ptz_max_y", "ptz.obsptz.max_y" },
{ "ptz_max_z", "ptz.obsptz.max_z" },
{ nullptr, nullptr }
};
for (int i = 0; renames[i].old_name; i++) {
Expand Down Expand Up @@ -455,9 +454,6 @@ static obs_properties_t *ftptz_properties(void *data)
#endif // WITH_PTZ_TCP
obs_property_set_modified_callback(p, ptz_type_modified);

obs_properties_add_int_slider(pp, "ptz_max_x", "Max control (pan)", 0, PTZ_MAX_X, 1);
obs_properties_add_int_slider(pp, "ptz_max_y", "Max control (tilt)", 0, PTZ_MAX_Y, 1);
obs_properties_add_int_slider(pp, "ptz_max_z", "Max control (zoom)", 0, PTZ_MAX_Z, 1);
obs_properties_add_bool(pp, "invert_x", obs_module_text("Invert control (Pan)"));
obs_properties_add_bool(pp, "invert_y", obs_module_text("Invert control (Tilt)"));
obs_properties_add_bool(pp, "invert_z", obs_module_text("Invert control (Zoom)"));
Expand Down Expand Up @@ -514,9 +510,9 @@ static void ftptz_get_defaults(obs_data_t *settings)

obs_data_set_default_string(settings, "ptz-type", "obsptz");
obs_data_set_default_int(settings, "ptz.visca-over-tcp.port", 1259);
obs_data_set_default_int(settings, "ptz_max_x", PTZ_MAX_X);
obs_data_set_default_int(settings, "ptz_max_y", PTZ_MAX_Y);
obs_data_set_default_int(settings, "ptz_max_z", PTZ_MAX_Z);
obs_data_set_default_int(settings, "ptz.obsptz.max_x", PTZ_MAX_X);
obs_data_set_default_int(settings, "ptz.obsptz.max_y", PTZ_MAX_Y);
obs_data_set_default_int(settings, "ptz.obsptz.max_z", PTZ_MAX_Z);
}

static inline int pan_flt2raw(float x)
Expand Down Expand Up @@ -693,7 +689,6 @@ static void tick_filter(struct face_tracker_ptz *s, float second)
uf.v[i] = (e.v[i] + s->filter_int.v[i]) * second + (s->filter_lpf.v[i] - filter_lpf_prev.v[i]) * s->klpf.v[i];
}
s->face_found_last = s->face_found;
const int u_max[3] = {s->ptz_max_x, s->ptz_max_y, s->ptz_max_z};
const float kp_zoom = std::max(s->ptz_query[2], 1.0f);
const float kp[3] = {
s->kp_x / srwh / kp_zoom,
Expand All @@ -709,8 +704,6 @@ static void tick_filter(struct face_tracker_ptz *s, float second)
case 1: n = tilt_flt2raw(x); break;
default: n = zoom_flt2raw(x, n); break;
}
if (n < -u_max[i]) n = -u_max[i];
else if (n > +u_max[i]) n = +u_max[i];
s->u[i] = n;
}

Expand Down
1 change: 0 additions & 1 deletion src/face-tracker-ptz.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ struct face_tracker_ptz
char *debug_data_control_last;

char *ptz_type;
int ptz_max_x, ptz_max_y, ptz_max_z;

bool is_paused;
obs_hotkey_pair_id hotkey_pause;
Expand Down
17 changes: 17 additions & 0 deletions src/obsptz-backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#define debug(...) blog(LOG_INFO, __VA_ARGS__)

#define SAME_CNT_TH 4
#define PTZ_MAX_X 0x18
#define PTZ_MAX_Y 0x14
#define PTZ_MAX_Z 0x07

obsptz_backend::obsptz_backend()
{
Expand All @@ -20,6 +23,10 @@ obsptz_backend::~obsptz_backend()
void obsptz_backend::set_config(struct obs_data *data)
{
device_id = (int)obs_data_get_int(data, "device_id");

ptz_max_x = obs_data_get_int(data, "max_x");
ptz_max_y = obs_data_get_int(data, "max_y");
ptz_max_z = obs_data_get_int(data, "max_z");
}

bool obsptz_backend::can_send()
Expand Down Expand Up @@ -53,6 +60,9 @@ proc_handler_t *obsptz_backend::get_ptz_ph()

void obsptz_backend::set_pantilt_speed(int pan, int tilt)
{
pan = std::clamp(pan, -ptz_max_x, ptz_max_x);
tilt = std::clamp(tilt, -ptz_max_y, ptz_max_y);

if (pan==prev_pan && tilt==prev_tilt) {
if (same_pantilt_cnt > SAME_CNT_TH)
return;
Expand Down Expand Up @@ -82,6 +92,8 @@ void obsptz_backend::set_pantilt_speed(int pan, int tilt)

void obsptz_backend::set_zoom_speed(int zoom)
{
zoom = std::clamp(zoom, -ptz_max_z, ptz_max_z);

if (zoom==prev_zoom) {
if (same_zoom_cnt > SAME_CNT_TH)
return;
Expand Down Expand Up @@ -132,5 +144,10 @@ bool obsptz_backend::ptz_type_modified(obs_properties_t *pp, obs_data_t *)
return false;

obs_properties_add_int(pp, "ptz.obsptz.device_id", obs_module_text("Device ID"), 0, 99, 1);

obs_properties_add_int_slider(pp, "ptz.obsptz.max_x", "Max control (pan)", 0, PTZ_MAX_X, 1);
obs_properties_add_int_slider(pp, "ptz.obsptz.max_y", "Max control (tilt)", 0, PTZ_MAX_Y, 1);
obs_properties_add_int_slider(pp, "ptz.obsptz.max_z", "Max control (zoom)", 0, PTZ_MAX_Z, 1);

return true;
}
1 change: 1 addition & 0 deletions src/obsptz-backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class obsptz_backend : public ptz_backend
{
uint64_t available_ns = 0;
int device_id = -1;
int ptz_max_x = 0, ptz_max_y = 0, ptz_max_z = 0;
proc_handler_t *ptz_ph = NULL;
proc_handler_t *get_ptz_ph();
int prev_pan = 0;
Expand Down

0 comments on commit 8309b4b

Please sign in to comment.