#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
#include <fixmath.h>
#include <axsdk/axptz.h>
#define APP_NAME "axptzApp"
#define WRITE_TO_SYS_LOG
#ifdef WRITE_TO_SYS_LOG
#define LOGINFO(fmt, args...) \
do { \
syslog(LOG_INFO, fmt, ## args); \
printf(fmt, ## args); \
printf("\n"); \
} while(0)
#else
#define LOGINFO(fmt, args...)
#endif
#define FIXMATH_FRAC_BITS 16
static gint video_channel = 1;
static gboolean camera_is_moving = FALSE;
static GList *capabilities = NULL;
static fixed_t fx_zero = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t fx_two = fx_ftox(2.0f, FIXMATH_FRAC_BITS);
static fixed_t fx_four = fx_ftox(4.0f, FIXMATH_FRAC_BITS);
static fixed_t unitless_low_pan = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unitless_centre_pan = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unitless_high_pan = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unitless_low_tilt = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unitless_centre_tilt = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unitless_high_tilt = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unitless_centre_zoom = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unit_low_pan = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unit_centre_pan = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unit_high_pan = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unit_low_tilt = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unit_centre_tilt = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unit_high_tilt = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unit_centre_zoom = fx_itox(0, FIXMATH_FRAC_BITS);
static fixed_t unitless_pos_speed = fx_ftox(0.25f, FIXMATH_FRAC_BITS);
static fixed_t unitless_chosen_pan_position = fx_itox(0, FIXMATH_FRAC_BITS);
static gboolean get_ptz_move_capabilities(void)
{
GError *local_error = NULL;
if (!(capabilities =
&local_error))) {
g_error_free(local_error);
return FALSE;
}
return TRUE;
}
static gboolean is_capability_supported(const char *capability)
{
gboolean is_supported = FALSE;
GList *it = NULL;
if (capabilities) {
for (it = g_list_first(capabilities); it != NULL; it = g_list_next(it)) {
if (!(g_strcmp0((gchar *) it->data, capability))) {
is_supported = TRUE;
break;
}
}
}
return is_supported;
}
static gboolean wait_for_camera_movement_to_finish(void)
{
gboolean is_moving = TRUE;
gushort timer = 0;
gushort timeout = 10;
gushort sleep_time = 1;
GError *local_error = NULL;
(video_channel, &is_moving, &local_error))) {
g_error_free(local_error);
return FALSE;
}
while (is_moving && (timer < timeout)) {
(video_channel, &is_moving, &local_error))) {
g_error_free(local_error);
return FALSE;
}
sleep(sleep_time);
timer++;
}
if (is_moving) {
return FALSE;
} else {
return TRUE;
}
}
static gboolean
move_to_absolute_position(fixed_t pan_value,
fixed_t tilt_value,
gfloat speed,
{
GError *local_error = NULL;
(pan_tilt_space, pan_tilt_speed_space, zoom_space, &local_error))) {
pan_value,
tilt_value,
fx_ftox(speed,
FIXMATH_FRAC_BITS),
zoom_value,
AX_PTZ_MOVEMENT_NO_VALUE,
&local_error))) {
g_error_free(local_error);
return FALSE;
}
video_channel,
abs_movement,
NULL, &local_error))) {
g_error_free(local_error);
return FALSE;
}
g_error_free(local_error);
return FALSE;
}
} else {
g_error_free(local_error);
return FALSE;
}
} else {
g_error_free(local_error);
return FALSE;
}
return TRUE;
}
static gboolean
move_to_relative_position(fixed_t pan_value,
fixed_t tilt_value,
gfloat speed,
{
GError *local_error = NULL;
(pan_tilt_space, pan_tilt_speed_space, zoom_space, &local_error))) {
pan_value,
tilt_value,
fx_ftox(speed, FIXMATH_FRAC_BITS),
zoom_value,
AX_PTZ_MOVEMENT_NO_VALUE,
&local_error))) {
g_error_free(local_error);
return FALSE;
}
video_channel,
rel_movement,
NULL, &local_error))) {
g_error_free(local_error);
return FALSE;
}
g_error_free(local_error);
return FALSE;
}
} else {
g_error_free(local_error);
return FALSE;
}
} else {
g_error_free(local_error);
return FALSE;
}
return TRUE;
}
static gboolean
start_continous_movement(fixed_t pan_speed,
fixed_t tilt_speed,
fixed_t zoom_speed, gfloat timeout)
{
GError *local_error = NULL;
(pan_tilt_speed_space, &local_error))) {
pan_speed,
tilt_speed,
zoom_speed,
fx_ftox(timeout, FIXMATH_FRAC_BITS),
&local_error))) {
g_error_free(local_error);
return FALSE;
}
video_channel,
cont_movement,
NULL, &local_error))) {
g_error_free(local_error);
return FALSE;
}
g_error_free(local_error);
return FALSE;
}
} else {
g_error_free(local_error);
return FALSE;
}
} else {
g_error_free(local_error);
return FALSE;
}
return TRUE;
}
static gboolean stop_continous_movement(gboolean stop_pan_tilt,
gboolean stop_zoom)
{
GError *local_error = NULL;
video_channel,
stop_pan_tilt,
NULL, NULL, &local_error))) {
g_error_free(local_error);
return FALSE;
}
return TRUE;
}
int main(int argc, char **argv)
{
GList *it = NULL;
gint preset_number_0 = -1;
gint preset_number_1 = -1;
gint queue_pos = -1;
gint time_to_pos_one = -1;
gint poll_time = -1;
GError *local_error = NULL;
#ifdef WRITE_TO_SYS_LOG
openlog(APP_NAME, LOG_PID | LOG_CONS, LOG_USER);
#endif
LOGINFO("\'axptz library\' example application started...\n");
goto failure;
}
if (!(ax_ptz_control_queue_group =
goto failure;
}
if (!(get_ptz_move_capabilities())) {
goto failure;
}
&unitless_status,
&local_error))) {
goto failure;
}
LOGINFO("Now we got the current PTZ status.\n");
&unitless_limits,
&local_error))) {
unitless_low_pan =
fx_addx(fx_divx
(fx_subx
unitless_centre_pan =
fx_addx(fx_divx
(fx_subx
unitless_high_pan =
fx_divx(fx_subx
FIXMATH_FRAC_BITS));
unitless_low_tilt =
fx_addx(fx_divx
(fx_subx
unitless_centre_tilt =
fx_addx(fx_divx
(fx_subx
unitless_high_tilt =
fx_divx(fx_subx
FIXMATH_FRAC_BITS));
unitless_centre_zoom =
fx_addx(fx_divx
(fx_subx
} else {
goto failure;
}
&unit_limits, &local_error))) {
unit_low_pan =
fx_addx(fx_divx
(fx_subx
unit_centre_pan =
fx_addx(fx_divx
(fx_subx
unit_high_pan =
fx_divx(fx_subx
FIXMATH_FRAC_BITS));
unit_low_tilt =
fx_addx(fx_divx
(fx_subx
unit_centre_tilt =
fx_addx(fx_divx
(fx_subx
unit_high_tilt =
fx_divx(fx_subx
FIXMATH_FRAC_BITS));
unit_centre_zoom =
fx_addx(fx_divx
(fx_subx
} else {
goto failure;
}
LOGINFO("Now we got the current PTZ limits.\n");
if ((is_capability_supported("AX_PTZ_MOVE_ABS_PAN"))) {
if (!(move_to_absolute_position(unitless_centre_pan, AX_PTZ_MOVEMENT_NO_VALUE,
goto failure;
}
LOGINFO("Absolute movement to centre pan position.\n");
if (!(wait_for_camera_movement_to_finish())) {
goto failure;
}
}
if ((is_capability_supported("AX_PTZ_MOVE_REL_PAN"))) {
unitless_chosen_pan_position =
fx_divx((fx_subx(unitless_high_pan, unitless_centre_pan)), fx_two,
FIXMATH_FRAC_BITS);
if (!(move_to_relative_position(unitless_chosen_pan_position,
AX_PTZ_MOVEMENT_NO_VALUE,
0.5f,
goto failure;
}
LOGINFO("Relative movement to chosen pan position.\n");
if (!(wait_for_camera_movement_to_finish())) {
goto failure;
}
}
if ((is_capability_supported("AX_PTZ_MOVE_ABS_TILT"))) {
if (!(move_to_absolute_position(AX_PTZ_MOVEMENT_NO_VALUE,
unit_centre_tilt,
0.5f,
goto failure;
}
LOGINFO("Absolute movement to centre tilt position.\n");
if (!(wait_for_camera_movement_to_finish())) {
goto failure;
}
}
if ((is_capability_supported("AX_PTZ_MOVE_ABS_ZOOM"))) {
if (!(move_to_absolute_position(AX_PTZ_MOVEMENT_NO_VALUE,
AX_PTZ_MOVEMENT_NO_VALUE,
0.5f,
goto failure;
}
LOGINFO("Absolute movement to maximum zoom position.\n");
if (!(wait_for_camera_movement_to_finish())) {
goto failure;
}
if (!(move_to_absolute_position(AX_PTZ_MOVEMENT_NO_VALUE,
AX_PTZ_MOVEMENT_NO_VALUE,
0.5f,
goto failure;
}
LOGINFO("Absolute movement to minimum zoom position.\n");
if (!(wait_for_camera_movement_to_finish())) {
goto failure;
}
}
if ((is_capability_supported("AX_PTZ_MOVE_CONT_PAN"))) {
if (!(start_continous_movement(unitless_pos_speed,
fx_zero,
fx_zero, 1000.0f))) {
goto failure;
}
LOGINFO("Continous pan movement started.\n");
sleep(5);
(video_channel, &camera_is_moving, &local_error))) {
goto failure;
} else {
if (camera_is_moving) {
LOGINFO("Continous pan: Camera is moving.\n");
} else {
LOGINFO("Continous pan: Camera is not moving.\n");
}
}
if (!(stop_continous_movement(TRUE, FALSE))) {
goto failure;
}
sleep(1);
(video_channel, &camera_is_moving, &local_error))) {
goto failure;
} else {
if (camera_is_moving) {
LOGINFO("Continous pan: Camera is moving.\n");
} else {
LOGINFO("Continous pan: Camera is not moving.\n");
}
}
LOGINFO("Continous pan movement ended.\n");
}
video_channel, "Max_values",
&preset_number_0,
&local_error))) {
goto failure;
}
LOGINFO("Create preset %d\n", preset_number_0);
if ((is_capability_supported("AX_PTZ_MOVE_ABS_PAN") &&
is_capability_supported("AX_PTZ_MOVE_ABS_TILT")
&& is_capability_supported("AX_PTZ_MOVE_ABS_ZOOM"))) {
0.5f,
goto failure;
}
if (!(wait_for_camera_movement_to_finish())) {
goto failure;
}
}
video_channel, preset_number_0,
FALSE, &local_error))) {
goto failure;
}
LOGINFO("Set preset %d\n", preset_number_0);
video_channel, "Centre_values",
&preset_number_1,
&local_error))) {
goto failure;
}
LOGINFO("Create preset %d\n", preset_number_1);
if ((is_capability_supported("AX_PTZ_MOVE_ABS_PAN") &&
is_capability_supported("AX_PTZ_MOVE_ABS_TILT")
&& is_capability_supported("AX_PTZ_MOVE_ABS_ZOOM"))) {
if (!(move_to_absolute_position(unitless_centre_pan,
unitless_centre_tilt,
0.5f,
unitless_centre_zoom,
goto failure;
}
if (!(wait_for_camera_movement_to_finish())) {
goto failure;
}
}
video_channel, preset_number_1,
FALSE, &local_error))) {
goto failure;
}
LOGINFO("Set preset %d\n", preset_number_1);
video_channel,
preset_number_0,
fx_ftox(1.0f,
FIXMATH_FRAC_BITS),
NULL, &local_error))) {
goto failure;
}
LOGINFO("Goto preset %d\n", preset_number_0);
if (!(wait_for_camera_movement_to_finish())) {
goto failure;
}
sleep(2);
video_channel,
preset_number_1,
fx_ftox(1.0f,
FIXMATH_FRAC_BITS),
NULL, &local_error))) {
goto failure;
}
LOGINFO("Goto preset %d\n", preset_number_1);
if (!(wait_for_camera_movement_to_finish())) {
goto failure;
}
video_channel,
preset_number_0,
&local_error))) {
goto failure;
}
LOGINFO("Remove preset %d\n", preset_number_0);
video_channel,
preset_number_1,
&local_error))) {
goto failure;
}
LOGINFO("Remove preset %d\n", preset_number_1);
video_channel,
&queue_pos, &time_to_pos_one, &poll_time,
&local_error))) {
goto failure;
}
LOGINFO("Request AX_PTZ_CONTROL_QUEUE_QUERY_STATUS:\n");
LOGINFO("queue_pos = %d\n", queue_pos);
LOGINFO("time_to_pos_one = %d\n", time_to_pos_one);
LOGINFO("poll_time = %d\n", poll_time);
video_channel,
&queue_pos, &time_to_pos_one, &poll_time,
&local_error))) {
goto failure;
}
LOGINFO("Request AX_PTZ_CONTROL_QUEUE_GET:\n");
LOGINFO("queue_pos = %d\n", queue_pos);
LOGINFO("time_to_pos_one = %d\n", time_to_pos_one);
LOGINFO("poll_time = %d\n", poll_time);
video_channel,
&queue_pos, &time_to_pos_one, &poll_time,
&local_error))) {
goto failure;
}
LOGINFO("Request AX_PTZ_CONTROL_QUEUE_QUERY_STATUS:\n");
LOGINFO("queue_pos = %d\n", queue_pos);
LOGINFO("time_to_pos_one = %d\n", time_to_pos_one);
LOGINFO("poll_time = %d\n", poll_time);
video_channel,
&queue_pos, &time_to_pos_one, &poll_time,
&local_error))) {
goto failure;
}
LOGINFO("Request AX_PTZ_CONTROL_QUEUE_DROP:\n");
LOGINFO("queue_pos = %d\n", queue_pos);
LOGINFO("time_to_pos_one = %d\n", time_to_pos_one);
LOGINFO("poll_time = %d\n", poll_time);
video_channel,
&queue_pos, &time_to_pos_one, &poll_time,
&local_error))) {
goto failure;
}
LOGINFO("Request AX_PTZ_CONTROL_QUEUE_QUERY_STATUS:\n");
LOGINFO("queue_pos = %d\n", queue_pos);
LOGINFO("time_to_pos_one = %d\n", time_to_pos_one);
LOGINFO("poll_time = %d\n", poll_time);
goto failure;
}
LOGINFO("%s finished successfully...\n", APP_NAME);
if (local_error) {
g_error_free(local_error);
local_error = NULL;
}
if (capabilities) {
for (it = g_list_first(capabilities); it != NULL; it = g_list_next(it)) {
g_free((gchar *) it->data);
}
}
g_list_free(capabilities);
capabilities = NULL;
g_free(unitless_status);
unitless_status = NULL;
g_free(unitless_limits);
unitless_limits = NULL;
g_free(unit_limits);
unit_limits = NULL;
exit(EXIT_SUCCESS);
failure:
if (local_error && local_error->message) {
LOGINFO("ERROR: %s ended with errors:\n", APP_NAME);
LOGINFO("%s\n", local_error->message);
}
if (local_error) {
g_error_free(local_error);
local_error = NULL;
}
if (local_error) {
g_error_free(local_error);
local_error = NULL;
}
if (capabilities) {
for (it = g_list_first(capabilities); it != NULL; it = g_list_next(it)) {
g_free((gchar *) it->data);
}
}
g_list_free(capabilities);
capabilities = NULL;
g_free(unitless_status);
unitless_status = NULL;
g_free(unitless_limits);
unitless_limits = NULL;
g_free(unit_limits);
unit_limits = NULL;
#ifdef WRITE_TO_SYS_LOG
closelog();
#endif
exit(EXIT_FAILURE);
}