Commit dfe2b31c authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Rewrite V4L2 controls to keep a list of them (fix #5269)

The V4L2 plug-in is a bit peculiar with dynamically generated object
variables. We need to keep track of which controls/variables we have,
so that we can remove the variables callbacks later. The only other
way to solve this bug, that I could think of, consisted of extending
the VLC variables subsystem (which would be worse in code freeze).

This rewrite also fixes a few other bugs:
 * Support menu with non-zero based minumum choice
 * Support menu with discontinuous choices range
 * Redumdant use the extended controls API as fallback
   (This only makes sense to set more than one control at a time,
    or to set 64-bits and string controls. VLC does none of that.)
 * Unused "controls-update" and "allcontrols" variables.
 * Skipping disabled, read-only and volatile controls.

Support for the legacy control enumeration API (pre-2.6.18 kernel) is
removed; and the code is now independent of the VLC object type (it
could easily be reused for say, a V4L2 video output).
parent fd1218a3
......@@ -73,6 +73,7 @@ void AccessClose( vlc_object_t *obj )
access_t *access = (access_t *)obj;
demux_sys_t *sys = (demux_sys_t *)access->p_sys;
ControlsDeinit( obj, sys->controls );
v4l2_close( sys->i_fd );
free( sys );
}
......
......@@ -28,19 +28,17 @@
#endif
#include "v4l2.h"
/* TODO: make callbacks independent of object type */
/* TODO: remove callbacks at exit */
#include <vlc_access.h>
#include <vlc_demux.h>
#include <ctype.h>
#include <sys/ioctl.h>
static const struct
typedef struct vlc_v4l2_ctrl_name
{
const char *psz_name;
unsigned int i_cid;
} controls[] =
const char name[20];
uint32_t cid;
} vlc_v4l2_ctrl_name_t;
/* NOTE: must be sorted by ID */
static const vlc_v4l2_ctrl_name_t controls[] =
{
{ "brightness", V4L2_CID_BRIGHTNESS },
{ "contrast", V4L2_CID_CONTRAST },
......@@ -65,609 +63,447 @@ static const struct
{ "vflip", V4L2_CID_VFLIP },
{ "hcenter", V4L2_CID_HCENTER },
{ "vcenter", V4L2_CID_VCENTER },
{ NULL, 0 }
/* TODO: add more standardized controls */
#define CTRL_CID_KNOWN(cid) \
((((uint32_t)cid) - V4L2_CID_BRIGHTNESS) \
<= (V4L2_CID_VCENTER - V4L2_CID_BRIGHTNESS))
};
static void name2var( unsigned char *name )
typedef struct vlc_v4l2_ctrl
{
int fd;
uint32_t id;
enum v4l2_ctrl_type type;
char name[32];
int32_t default_value;
struct vlc_v4l2_ctrl *next;
} vlc_v4l2_ctrl_t;
static int ControlSet (const vlc_v4l2_ctrl_t *c, int_fast32_t value)
{
for( ; *name; name++ )
*name = (*name == ' ') ? '_' : tolower( *name );
struct v4l2_control ctrl = {
.id = c->id,
.value = value,
};
if (v4l2_ioctl (c->fd, VIDIOC_S_CTRL, &ctrl) < 0)
return -1;
return 0;
}
/*****************************************************************************
* Issue user-class v4l2 controls
*****************************************************************************/
static int Control( vlc_object_t *p_obj, int i_fd,
const char *psz_name, int i_cid, int i_value )
static int ControlSetCallback (vlc_object_t *obj, const char *var,
vlc_value_t old, vlc_value_t cur, void *data)
{
struct v4l2_queryctrl queryctrl;
struct v4l2_control control;
struct v4l2_ext_control ext_control;
struct v4l2_ext_controls ext_controls;
if( i_value == -1 )
return VLC_SUCCESS;
memset( &queryctrl, 0, sizeof( queryctrl ) );
queryctrl.id = i_cid;
const vlc_v4l2_ctrl_t *ctrl = data;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
|| queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
if (ControlSet (ctrl, cur.i_int))
{
msg_Dbg( p_obj, "%s (%x) control is not supported.", psz_name, i_cid );
msg_Err (obj, "cannot set control %s: %m", var);
return VLC_EGENERIC;
}
(void) old;
return VLC_SUCCESS;
}
memset( &control, 0, sizeof( control ) );
memset( &ext_control, 0, sizeof( ext_control ) );
memset( &ext_controls, 0, sizeof( ext_controls ) );
control.id = i_cid;
ext_control.id = i_cid;
ext_controls.ctrl_class = V4L2_CTRL_ID2CLASS( i_cid );
ext_controls.count = 1;
ext_controls.controls = &ext_control;
int i_ret = -1;
if( i_value >= queryctrl.minimum && i_value <= queryctrl.maximum )
{
ext_control.value = i_value;
if( v4l2_ioctl( i_fd, VIDIOC_S_EXT_CTRLS, &ext_controls ) < 0 )
{
control.value = i_value;
if( v4l2_ioctl( i_fd, VIDIOC_S_CTRL, &control ) < 0 )
{
msg_Err( p_obj, "unable to set %s (%x) to %d (%m)",
psz_name, i_cid, i_value );
return VLC_EGENERIC;
}
i_ret = v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control );
}
else
{
i_ret = v4l2_ioctl( i_fd, VIDIOC_G_EXT_CTRLS, &ext_controls );
control.value = ext_control.value;
}
}
if( i_ret >= 0 )
{
vlc_value_t val;
msg_Dbg( p_obj, "video %s: %d", psz_name, control.value );
switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
static void ControlsReset (vlc_v4l2_ctrl_t *list)
{
while (list != NULL)
{
case VLC_VAR_BOOL:
val.b_bool = control.value;
var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
var_TriggerCallback( p_obj, "controls-update" );
break;
case VLC_VAR_INTEGER:
val.i_int = control.value;
var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL );
var_TriggerCallback( p_obj, "controls-update" );
break;
}
if (list->type != V4L2_CTRL_TYPE_BUTTON)
ControlSet (list, list->default_value);
list = list->next;
}
return VLC_SUCCESS;
}
static int DemuxControlCallback( vlc_object_t *p_this,
const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
void *p_data )
static int ControlsResetCallback (vlc_object_t *obj, const char *var,
vlc_value_t old, vlc_value_t cur, void *data)
{
(void)oldval;
demux_t *p_demux = (demux_t*)p_this;
demux_sys_t *p_sys = p_demux->p_sys;
int i_cid = (long int)p_data;
int i_fd = p_sys->i_fd;
if( i_fd < 0 )
return VLC_EGENERIC;
Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
return VLC_EGENERIC;
ControlsReset (data);
(void) obj; (void) var; (void) old; (void) cur;
return VLC_SUCCESS;
}
static int AccessControlCallback( vlc_object_t *p_this,
const char *psz_var, vlc_value_t oldval, vlc_value_t newval,
void *p_data )
static void ControlsSetFromString (vlc_object_t *obj,
const vlc_v4l2_ctrl_t *list)
{
(void)oldval;
access_t *p_access = (access_t *)p_this;
demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
int i_cid = (long int)p_data;
int i_fd = p_sys->i_fd;
if( i_fd < 0 )
return VLC_EGENERIC;
char *buf = var_InheritString (obj, CFG_PREFIX"set-ctrls");
if (buf == NULL)
return;
Control( p_this, i_fd, psz_var, i_cid, newval.i_int );
char *p = buf;
if (*p == '{')
p++;
return VLC_EGENERIC;
}
char *end = strchr (p, '}');
if (end != NULL)
*end = '\0';
next:
while (p != NULL && *p)
{
const char *name, *value;
/**
* Resets all user-class V4L2 controls to their default value
*/
static int ControlReset( vlc_object_t *p_obj, int i_fd )
{
struct v4l2_queryctrl queryctrl;
int i_cid;
memset( &queryctrl, 0, sizeof( queryctrl ) );
p += strspn (p, ", ");
name = p;
end = strchr (p, ',');
if (end != NULL)
*(end++) = '\0';
p = end; /* next name/value pair */
queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
end = strchr (name, '=');
if (end == NULL)
{
/* Extended control API supported */
queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
{
if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS
|| V4L2_CTRL_ID2CLASS( queryctrl.id ) == V4L2_CTRL_CLASS_MPEG )
{
queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
/* TODO? support button controls that way? */
msg_Err (obj, "syntax error in \"%s\": missing '='", name);
continue;
}
struct v4l2_control control;
memset( &control, 0, sizeof( control ) );
control.id = queryctrl.id;
if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
&& queryctrl.default_value != control.value )
{
int i;
for( i = 0; controls[i].psz_name != NULL; i++ )
if( controls[i].i_cid == queryctrl.id ) break;
name2var( queryctrl.name );
Control( p_obj, i_fd,
controls[i].psz_name ? controls[i].psz_name
: (const char *)queryctrl.name,
queryctrl.id, queryctrl.default_value );
}
queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
}
}
else
{
*(end++) = '\0';
value = end;
/* public controls */
for( i_cid = V4L2_CID_BASE;
i_cid < V4L2_CID_LASTP1;
i_cid ++ )
long val = strtol (value, &end, 0);
if (*end)
{
queryctrl.id = i_cid;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
{
struct v4l2_control control;
if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
msg_Err (obj, "syntax error in \"%s\": not an integer", value);
continue;
memset( &control, 0, sizeof( control ) );
control.id = queryctrl.id;
if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
&& queryctrl.default_value != control.value )
{
int i;
for( i = 0; controls[i].psz_name != NULL; i++ )
if( controls[i].i_cid == queryctrl.id ) break;
name2var( queryctrl.name );
Control( p_obj, i_fd,
controls[i].psz_name ? controls[i].psz_name
: (const char *)queryctrl.name,
queryctrl.id, queryctrl.default_value );
}
}
}
/* private controls */
for( i_cid = V4L2_CID_PRIVATE_BASE;
;
i_cid ++ )
{
queryctrl.id = i_cid;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
for (const vlc_v4l2_ctrl_t *c = list; c != NULL; c = c->next)
if (!strcasecmp (name, c->name))
{
struct v4l2_control control;
if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
continue;
memset( &control, 0, sizeof( control ) );
control.id = queryctrl.id;
if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0
&& queryctrl.default_value != control.value )
{
name2var( queryctrl.name );
Control( p_obj, i_fd, (const char *)queryctrl.name,
queryctrl.id, queryctrl.default_value );
}
}
else
break;
ControlSet (c, val);
goto next;
}
msg_Err (obj, "control \"%s\" not available", name);
}
return VLC_SUCCESS;
free (buf);
}
static int DemuxControlResetCallback( vlc_object_t *p_this,
const char *psz_var, vlc_value_t oldval, vlc_value_t newval, void *p_data )
static int cidcmp (const void *a, const void *b)
{
(void)psz_var; (void)oldval; (void)newval; (void)p_data;
demux_t *p_demux = (demux_t*)p_this;
demux_sys_t *p_sys = p_demux->p_sys;
int i_fd = p_sys->i_fd;
if( i_fd < 0 )
return VLC_EGENERIC;
ControlReset( p_this, i_fd );
const uint32_t *id = a;
const vlc_v4l2_ctrl_name_t *name = b;
return VLC_EGENERIC;
return (int32_t)(*id - name->cid);
}
static int AccessControlResetCallback( vlc_object_t *p_this,
const char *psz_var, vlc_value_t oldval, vlc_value_t newval, void *p_data )
/**
* Creates a VLC-V4L2 control structure:
* In particular, determines a name suitable for a VLC object variable.
* \param query V4L2 control query structure [IN]
* \return NULL on error
*/
static vlc_v4l2_ctrl_t *ControlCreate (int fd,
const struct v4l2_queryctrl *query)
{
(void)psz_var; (void)oldval; (void)newval; (void)p_data;
access_t *p_access = (access_t *)p_this;
demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys;
int i_fd = p_sys->i_fd;
if( i_fd < 0 )
return VLC_EGENERIC;
ControlReset( p_this, i_fd );
vlc_v4l2_ctrl_t *ctrl = malloc (sizeof (*ctrl));
if (unlikely(ctrl == NULL))
return NULL;
ctrl->fd = fd;
ctrl->id = query->id;
ctrl->type = query->type;
/* Search for a well-known control */
const vlc_v4l2_ctrl_name_t *known;
known = bsearch (&query->id, controls, sizeof (controls) / sizeof (*known),
sizeof (*known), cidcmp);
if (known != NULL)
strcpy (ctrl->name, known->name);
else
/* Fallback to automatically-generated control name */
{
size_t i;
for (i = 0; query->name[i]; i++)
{
unsigned char c = query->name[i];
if (c == ' ')
c = '_';
if (c < 128)
c = tolower (c);
ctrl->name[i] = c;
}
ctrl->name[i] = '\0';
}
return VLC_EGENERIC;
ctrl->default_value = query->default_value;
return ctrl;
}
/*****************************************************************************
* Print a user-class v4l2 control's details, create the relevant variable,
* change the value if needed.
*****************************************************************************/
static void ControlListPrint( vlc_object_t *p_obj, int i_fd,
struct v4l2_queryctrl queryctrl,
bool b_reset, bool b_demux )
{
struct v4l2_querymenu querymenu;
unsigned int i_mid;
int i;
int i_val;
#ifndef V4L2_CTRL_FLAG_VOLATILE
# define V4L2_CTRL_FLAG_VOLATILE 0x0080
# warning Please update V4L2 kernel headers!
#endif
char *psz_name;
vlc_value_t val, val2;
#define CTRL_FLAGS_IGNORE \
(V4L2_CTRL_FLAG_DISABLED /* not implemented at all */ \
|V4L2_CTRL_FLAG_READ_ONLY /* value is constant */ \
|V4L2_CTRL_FLAG_VOLATILE /* value is (variable but) read-only */)
if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED )
msg_Dbg( p_obj, " control is busy" );
if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
msg_Dbg( p_obj, " control is read-only" );
static vlc_v4l2_ctrl_t *ControlAddInteger (vlc_object_t *obj, int fd,
const struct v4l2_queryctrl *query)
{
msg_Dbg (obj, " integer %s (%08"PRIX32")", query->name, query->id);
if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
return NULL;
for( i = 0; controls[i].psz_name != NULL; i++ )
if( controls[i].i_cid == queryctrl.id ) break;
vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
if (unlikely(c == NULL))
return NULL;
if( controls[i].psz_name )
{
psz_name = strdup( controls[i].psz_name );
char psz_cfg_name[40];
sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name );
i_val = var_CreateGetInteger( p_obj, psz_cfg_name );
var_Destroy( p_obj, psz_cfg_name );
}
else
if (var_Create (obj, c->name, VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND))
{
psz_name = strdup( (const char *)queryctrl.name );
name2var( (unsigned char *)psz_name );
i_val = -1;
free (c);
return NULL;
}
switch( queryctrl.type )
{
case V4L2_CTRL_TYPE_INTEGER:
msg_Dbg( p_obj, " integer control" );
msg_Dbg( p_obj,
" valid values: %d to %d by steps of %d",
queryctrl.minimum, queryctrl.maximum,
queryctrl.step );
var_Create( p_obj, psz_name,
VLC_VAR_INTEGER | VLC_VAR_HASMIN | VLC_VAR_HASMAX
| VLC_VAR_HASSTEP | VLC_VAR_ISCOMMAND );
val.i_int = queryctrl.minimum;
var_Change( p_obj, psz_name, VLC_VAR_SETMIN, &val, NULL );
val.i_int = queryctrl.maximum;
var_Change( p_obj, psz_name, VLC_VAR_SETMAX, &val, NULL );
val.i_int = queryctrl.step;
var_Change( p_obj, psz_name, VLC_VAR_SETSTEP, &val, NULL );
break;
case V4L2_CTRL_TYPE_BOOLEAN:
msg_Dbg( p_obj, " boolean control" );
var_Create( p_obj, psz_name,
VLC_VAR_BOOL | VLC_VAR_ISCOMMAND );
break;
case V4L2_CTRL_TYPE_MENU:
msg_Dbg( p_obj, " menu control" );
var_Create( p_obj, psz_name,
VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
| VLC_VAR_ISCOMMAND );
memset( &querymenu, 0, sizeof( querymenu ) );
for( i_mid = queryctrl.minimum;
i_mid <= (unsigned)queryctrl.maximum;
i_mid++ )
{
querymenu.index = i_mid;
querymenu.id = queryctrl.id;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYMENU, &querymenu ) >= 0 )
{
msg_Dbg( p_obj, " %d: %s",
querymenu.index, querymenu.name );
val.i_int = querymenu.index;
val2.psz_string = (char *)querymenu.name;
var_Change( p_obj, psz_name,
VLC_VAR_ADDCHOICE, &val, &val2 );
}
}
break;
case V4L2_CTRL_TYPE_BUTTON:
msg_Dbg( p_obj, " button control" );
var_Create( p_obj, psz_name,
VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
break;
case V4L2_CTRL_TYPE_CTRL_CLASS:
msg_Dbg( p_obj, " control class" );
var_Create( p_obj, psz_name, VLC_VAR_VOID );
break;
default:
msg_Dbg( p_obj, " unknown control type (FIXME)" );
/* FIXME */
break;
}
vlc_value_t val;
struct v4l2_control ctrl = { .id = query->id };
switch( queryctrl.type )
{
case V4L2_CTRL_TYPE_INTEGER:
case V4L2_CTRL_TYPE_BOOLEAN:
case V4L2_CTRL_TYPE_MENU:
if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
{
struct v4l2_control control;
msg_Dbg( p_obj, " default value: %d",
queryctrl.default_value );
memset( &control, 0, sizeof( control ) );
control.id = queryctrl.id;
if( v4l2_ioctl( i_fd, VIDIOC_G_CTRL, &control ) >= 0 )
{
msg_Dbg( p_obj, " current value: %d", control.value );
}
if( i_val == -1 )
{
i_val = control.value;
if( b_reset && queryctrl.default_value != control.value )
{
msg_Dbg( p_obj, " reset value to default" );
Control( p_obj, i_fd, psz_name,
queryctrl.id, queryctrl.default_value );
}
msg_Dbg (obj, " current: %3"PRId32", default: %3"PRId32,
ctrl.value, query->default_value);
val.i_int = ctrl.value;
var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
}
else
val.i_int = query->minimum;
var_Change (obj, c->name, VLC_VAR_SETMIN, &val, NULL);
val.i_int = query->maximum;
var_Change (obj, c->name, VLC_VAR_SETMAX, &val, NULL);
if (query->step != 1)
{
Control( p_obj, i_fd, psz_name, queryctrl.id, i_val );
}
}
break;
default:
break;
val.i_int = query->step;
var_Change (obj, c->name, VLC_VAR_SETSTEP, &val, NULL);
}
val.i_int = query->default_value;
var_Change (obj, c->name, VLC_VAR_SETDEFAULT, &val, NULL);
return c;
}
static vlc_v4l2_ctrl_t *ControlAddBoolean (vlc_object_t *obj, int fd,
const struct v4l2_queryctrl *query)
{
msg_Dbg (obj, " boolean %s (%08"PRIX32")", query->name, query->id);
if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
return NULL;
val.psz_string = (char *)queryctrl.name;
var_Change( p_obj, psz_name, VLC_VAR_SETTEXT, &val, NULL );
val.i_int = queryctrl.id;
val2.psz_string = (char *)psz_name;
var_Change( p_obj, "allcontrols", VLC_VAR_ADDCHOICE, &val, &val2 );
/* bad things happen changing MPEG mid-stream
* so don't add to Ext Settings GUI */
if( V4L2_CTRL_ID2CLASS( queryctrl.id ) != V4L2_CTRL_CLASS_MPEG )
var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE )
vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
if (unlikely(c == NULL))
return NULL;
if (var_Create (obj, c->name, VLC_VAR_BOOL | VLC_VAR_ISCOMMAND))
{
case VLC_VAR_BOOL:
var_SetBool( p_obj, psz_name, i_val );
break;
case VLC_VAR_INTEGER:
var_SetInteger( p_obj, psz_name, i_val );
break;
case VLC_VAR_VOID:
break;
default:
msg_Warn( p_obj, "FIXME: %s %s %d", __FILE__, __func__,
__LINE__ );
break;
free (c);
return NULL;
}
if( b_demux )
var_AddCallback( p_obj, psz_name,
DemuxControlCallback, (void*)(intptr_t)queryctrl.id );
else
var_AddCallback( p_obj, psz_name,
AccessControlCallback, (void*)(intptr_t)queryctrl.id );
vlc_value_t val;
struct v4l2_control ctrl = { .id = query->id };
free( psz_name );
if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
{
msg_Dbg (obj, " current: %s, default: %s",
ctrl.value ? " true" : "false",
query->default_value ? " true" : "false");
val.b_bool = ctrl.value;
var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
}
val.b_bool = query->default_value;
var_Change (obj, c->name, VLC_VAR_SETDEFAULT, &val, NULL);
return c;
}
static void SetAvailControlsByString( vlc_object_t *p_obj, int i_fd )
static vlc_v4l2_ctrl_t *ControlAddMenu (vlc_object_t *obj, int fd,
const struct v4l2_queryctrl *query)
{
char *ctrls = var_InheritString( p_obj, CFG_PREFIX"set-ctrls" );
if( ctrls == NULL )
return;
msg_Dbg (obj, " menu %s (%08"PRIX32")", query->name, query->id);
if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
return NULL;
vlc_value_t val, text;
vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
if (unlikely(c == NULL))
return NULL;
if( var_Change( p_obj, "allcontrols", VLC_VAR_GETCHOICES, &val, &text ) )
if (var_Create (obj, c->name, VLC_VAR_INTEGER | VLC_VAR_HASCHOICE
| VLC_VAR_ISCOMMAND))
{
msg_Err( p_obj, "Oops, can't find 'allcontrols' variable." );
free( ctrls );
return;
free (c);
return NULL;
}
char *p = ctrls;
if( *p == '{' )
p++;
vlc_value_t val;
struct v4l2_control ctrl = { .id = query->id };
while( p != NULL && *p && *p != '}' )
if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
{
p += strspn( p, ", " );
const char *name = p;
char *end = strchr( p, ',' );
if( end == NULL )
end = strchr( p, '}' );
if( end != NULL )
*(end++) = '\0';
msg_Dbg (obj, " current: %"PRId32", default: %"PRId32,
ctrl.value, query->default_value);
val.i_int = ctrl.value;
var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
}
val.b_bool = query->default_value;
var_Change (obj, c->name, VLC_VAR_SETDEFAULT, &val, NULL);
val.i_int = query->minimum;
var_Change (obj, c->name, VLC_VAR_SETMIN, &val, NULL);
val.i_int = query->maximum;
var_Change (obj, c->name, VLC_VAR_SETMAX, &val, NULL);
char *value = strchr( p, '=' );
if( value == NULL )
/* Import menu choices */
for (uint_fast32_t idx = query->minimum;
idx <= (uint_fast32_t)query->maximum;
idx++)
{
msg_Err( p_obj, "syntax error in \"%s\": missing '='", name );
p = end;
struct v4l2_querymenu menu = { .id = query->id, .index = idx };
if (v4l2_ioctl (fd, VIDIOC_QUERYMENU, &menu) < 0)
continue;
msg_Dbg (obj, " choice %"PRIu32") %s", menu.index, menu.name);
vlc_value_t text;
val.i_int = menu.index;
text.psz_string = (char *)menu.name;
var_Change (obj, c->name, VLC_VAR_ADDCHOICE, &val, &text);
}
*(value++) = '\0';
return c;
}
for( int i = 0; i < val.p_list->i_count; i++ )
{
vlc_value_t vartext;
const char *var = text.p_list->p_values[i].psz_string;
static vlc_v4l2_ctrl_t *ControlAddButton (vlc_object_t *obj, int fd,
const struct v4l2_queryctrl *query)
{
msg_Dbg (obj, " button %s (%08"PRIX32")", query->name, query->id);
if (query->flags & CTRL_FLAGS_IGNORE)
return NULL;
vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
if (unlikely(c == NULL))
return NULL;
if (var_Create (obj, c->name, VLC_VAR_VOID | VLC_VAR_ISCOMMAND))
return NULL;
(void) fd;
return c;
}
var_Change( p_obj, var, VLC_VAR_GETTEXT, &vartext, NULL );
if( !strcasecmp( vartext.psz_string, name ) )
{
Control( p_obj, i_fd, name,
val.p_list->p_values[i].i_int,
strtol( value, NULL, 0 ) );
free( vartext.psz_string );
goto found;
}
free( vartext.psz_string );
}
msg_Err( p_obj, "control %s not available", name );
found:
p = end;
}
var_FreeList( &val, &text );
free( ctrls );
static vlc_v4l2_ctrl_t *ControlAddClass (vlc_object_t *obj, int fd,
const struct v4l2_queryctrl *query)
{
msg_Dbg (obj, "control class %s:", query->name);
(void) fd;
return NULL;
}
static vlc_v4l2_ctrl_t *ControlAddUnknown (vlc_object_t *obj, int fd,
const struct v4l2_queryctrl *query)
{
msg_Dbg (obj, " unknown %s (%08"PRIX32")", query->name, query->id);
msg_Warn (obj, " unknown control type %u", (unsigned)query->type);
(void) fd;
return NULL;
}
typedef vlc_v4l2_ctrl_t *(*ctrl_type_cb) (vlc_object_t *, int,
const struct v4l2_queryctrl *);
/**
* Lists all user-class v4l2 controls, sets them to the user specified
* value and create the relevant variables to enable run-time changes.
*/
int ControlList( vlc_object_t *p_obj, int i_fd, bool b_demux )
vlc_v4l2_ctrl_t *ControlsInit (vlc_object_t *obj, int fd)
{
struct v4l2_queryctrl queryctrl;
int i_cid;
const bool b_reset = var_InheritBool( p_obj, CFG_PREFIX"controls-reset" );
/* A list of controls that can be modified at run-time is stored in the
* "controls" variable. The V4L2 controls dialog can be built from this. */
var_Create (obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE);
memset( &queryctrl, 0, sizeof( queryctrl ) );
/* A list of available controls (aka the variable name) will be
* stored as choices in the "allcontrols" variable. We'll thus be able
* to use those to create an appropriate interface
* A list of available controls that can be changed mid-stream will
* be stored in the "controls" variable */
var_Create( p_obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
var_Create( p_obj, "allcontrols", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
var_Create( p_obj, "controls-update", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
static const ctrl_type_cb handlers[] =
{
[V4L2_CTRL_TYPE_INTEGER] = ControlAddInteger,
[V4L2_CTRL_TYPE_BOOLEAN] = ControlAddBoolean,
[V4L2_CTRL_TYPE_MENU] = ControlAddMenu,
[V4L2_CTRL_TYPE_BUTTON] = ControlAddButton,
[V4L2_CTRL_TYPE_CTRL_CLASS] = ControlAddClass,
};
/* Add a control to reset all controls to their default values */
vlc_value_t val, val2;
var_Create( p_obj, "controls-reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
val.psz_string = _( "Reset controls to default" );
var_Change( p_obj, "controls-reset", VLC_VAR_SETTEXT, &val, NULL );
val.i_int = -1;
val2.psz_string = (char *)"controls-reset";
var_Change( p_obj, "controls", VLC_VAR_ADDCHOICE, &val, &val2 );
if (b_demux)
var_AddCallback( p_obj, "controls-reset", DemuxControlResetCallback, NULL );
else
var_AddCallback( p_obj, "controls-reset", AccessControlResetCallback, NULL );
vlc_v4l2_ctrl_t *list = NULL;
struct v4l2_queryctrl query;
queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
query.id = V4L2_CTRL_FLAG_NEXT_CTRL;
while (v4l2_ioctl (fd, VIDIOC_QUERYCTRL, &query) >= 0)
{
msg_Dbg( p_obj, "Extended control API supported by v4l2 driver" );
ctrl_type_cb handler = NULL;
if (query.type < (sizeof (handlers) / sizeof (handlers[0])))
handler = handlers[query.type];
if (handler == NULL)
handler = ControlAddUnknown;
/* List extended controls */
queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
{
if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS )
{
msg_Dbg( p_obj, "%s", queryctrl.name );
queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
continue;
}
switch( V4L2_CTRL_ID2CLASS( queryctrl.id ) )
vlc_v4l2_ctrl_t *c = handler (obj, fd, &query);
if (c != NULL)
{
case V4L2_CTRL_CLASS_USER:
msg_Dbg( p_obj, "Available control: %s (%x)",
queryctrl.name, queryctrl.id );
break;
case V4L2_CTRL_CLASS_MPEG:
name2var( queryctrl.name );
msg_Dbg( p_obj, "Available MPEG control: %s (%x)",
queryctrl.name, queryctrl.id );
break;
default:
msg_Dbg( p_obj, "Available private control: %s (%x)",
queryctrl.name, queryctrl.id );
break;
}
ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
vlc_value_t val, text;
var_AddCallback (obj, c->name, ControlSetCallback, c);
text.psz_string = (char *)query.name;
var_Change (obj, c->name, VLC_VAR_SETTEXT, &text, NULL);
val.i_int = query.id;
text.psz_string = (char *)c->name;
var_Change (obj, "controls", VLC_VAR_ADDCHOICE, &val, &text);
c->next = list;
list = c;
}
query.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
}
else
{
msg_Dbg( p_obj, "Extended control API not supported by v4l2 driver" );
/* List public controls */
for( i_cid = V4L2_CID_BASE;
i_cid < V4L2_CID_LASTP1;
i_cid ++ )
/* Set well-known controls from VLC configuration */
for (vlc_v4l2_ctrl_t *ctrl = list; ctrl != NULL; ctrl = ctrl->next)
{
queryctrl.id = i_cid;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
{
if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
if (!CTRL_CID_KNOWN (ctrl->id))
continue;
msg_Dbg( p_obj, "Available control: %s (%x)",
queryctrl.name, queryctrl.id );
ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
}
char varname[sizeof (CFG_PREFIX) + sizeof (ctrl->name) - 1];
sprintf (varname, CFG_PREFIX"%s", ctrl->name);
int64_t val = var_InheritInteger (obj, varname);
if (val == -1)
continue; /* the VLC default value: "do not modify" */
ControlSet (ctrl, val);
}
/* List private controls */
for( i_cid = V4L2_CID_PRIVATE_BASE;
;
i_cid ++ )
{
queryctrl.id = i_cid;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
/* Set any control from the VLC configuration control string */
ControlsSetFromString (obj, list);
/* Add a control to reset all controls to their default values */
{
if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
continue;
msg_Dbg( p_obj, "Available private control: %s (%x)",
queryctrl.name, queryctrl.id );
ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
}
else
break;
vlc_value_t val, text;
var_Create (obj, "reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND);
val.psz_string = _("Reset defaults");
var_Change (obj, "reset", VLC_VAR_SETTEXT, &val, NULL);
val.i_int = -1;
text.psz_string = (char *)"reset";
var_Change (obj, "controls", VLC_VAR_ADDCHOICE, &val, &text);
var_AddCallback (obj, "reset", ControlsResetCallback, list);
}
if (var_InheritBool (obj, CFG_PREFIX"controls-reset"))
ControlsReset (list);
return list;
}
void ControlsDeinit (vlc_object_t *obj, vlc_v4l2_ctrl_t *list)
{
var_DelCallback (obj, "reset", ControlsResetCallback, list);
var_Destroy (obj, "reset");
while (list != NULL)
{
vlc_v4l2_ctrl_t *next = list->next;
var_DelCallback (obj, list->name, ControlSetCallback, list);
var_Destroy (obj, list->name);
free (list);
list = next;
}
SetAvailControlsByString( p_obj, i_fd );
return VLC_SUCCESS;
var_Destroy (obj, "controls");
}
......@@ -116,6 +116,7 @@ void DemuxClose( vlc_object_t *obj )
free( sys->p_buffers );
}
ControlsDeinit( obj, sys->controls );
v4l2_close( fd );
free( sys );
}
......
......@@ -41,8 +41,6 @@
#define CFG_PREFIX "v4l2-"
int ControlList(vlc_object_t *, int fd, bool b_demux);
/* TODO: remove this, use callbacks */
typedef enum {
IO_METHOD_READ=1,
......@@ -50,7 +48,9 @@ typedef enum {
IO_METHOD_USERPTR,
} io_method;
/* TODO: move this to .c */
typedef struct vlc_v4l2_ctrl vlc_v4l2_ctrl_t;
/* TODO: move this to access.c and demux.c (separately) */
struct demux_sys_t
{
int i_fd;
......@@ -67,6 +67,8 @@ struct demux_sys_t
es_out_id_t *p_es;
vlc_v4l2_ctrl_t *controls;
#ifdef HAVE_LIBV4L2
bool b_libv4l2;
#endif
......@@ -93,3 +95,7 @@ void GetMaxDimensions(vlc_object_t *, int fd, uint32_t fmt, float fps_min,
/* access.c */
int AccessOpen(vlc_object_t *);
void AccessClose(vlc_object_t *);
/* controls.c */
vlc_v4l2_ctrl_t *ControlsInit(vlc_object_t *, int fd);
void ControlsDeinit(vlc_object_t *, vlc_v4l2_ctrl_t *);
......@@ -1036,7 +1036,7 @@ static int InitVideo( vlc_object_t *p_obj, int i_fd, demux_sys_t *p_sys,
/* TODO: Move the resolution stuff up here */
/* if MPEG encoder card, no need to do anything else after this */
ControlList( p_obj, i_fd, b_demux );
p_sys->controls = ControlsInit( p_obj, i_fd );
/* Reset Cropping */
memset( &cropcap, 0, sizeof(cropcap) );
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment