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 ) ...@@ -73,6 +73,7 @@ void AccessClose( vlc_object_t *obj )
access_t *access = (access_t *)obj; access_t *access = (access_t *)obj;
demux_sys_t *sys = (demux_sys_t *)access->p_sys; demux_sys_t *sys = (demux_sys_t *)access->p_sys;
ControlsDeinit( obj, sys->controls );
v4l2_close( sys->i_fd ); v4l2_close( sys->i_fd );
free( sys ); free( sys );
} }
......
...@@ -28,19 +28,17 @@ ...@@ -28,19 +28,17 @@
#endif #endif
#include "v4l2.h" #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 <ctype.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
static const struct typedef struct vlc_v4l2_ctrl_name
{ {
const char *psz_name; const char name[20];
unsigned int i_cid; uint32_t cid;
} controls[] = } vlc_v4l2_ctrl_name_t;
/* NOTE: must be sorted by ID */
static const vlc_v4l2_ctrl_name_t controls[] =
{ {
{ "brightness", V4L2_CID_BRIGHTNESS }, { "brightness", V4L2_CID_BRIGHTNESS },
{ "contrast", V4L2_CID_CONTRAST }, { "contrast", V4L2_CID_CONTRAST },
...@@ -65,609 +63,447 @@ static const struct ...@@ -65,609 +63,447 @@ static const struct
{ "vflip", V4L2_CID_VFLIP }, { "vflip", V4L2_CID_VFLIP },
{ "hcenter", V4L2_CID_HCENTER }, { "hcenter", V4L2_CID_HCENTER },
{ "vcenter", V4L2_CID_VCENTER }, { "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++ ) struct v4l2_control ctrl = {
*name = (*name == ' ') ? '_' : tolower( *name ); .id = c->id,
.value = value,
};
if (v4l2_ioctl (c->fd, VIDIOC_S_CTRL, &ctrl) < 0)
return -1;
return 0;
} }
/***************************************************************************** static int ControlSetCallback (vlc_object_t *obj, const char *var,
* Issue user-class v4l2 controls vlc_value_t old, vlc_value_t cur, void *data)
*****************************************************************************/
static int Control( vlc_object_t *p_obj, int i_fd,
const char *psz_name, int i_cid, int i_value )
{ {
struct v4l2_queryctrl queryctrl; const vlc_v4l2_ctrl_t *ctrl = data;
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;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0 if (ControlSet (ctrl, cur.i_int))
|| queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
{ {
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; return VLC_EGENERIC;
} }
(void) old;
return VLC_SUCCESS;
}
memset( &control, 0, sizeof( control ) ); static void ControlsReset (vlc_v4l2_ctrl_t *list)
memset( &ext_control, 0, sizeof( ext_control ) ); {
memset( &ext_controls, 0, sizeof( ext_controls ) ); while (list != NULL)
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 )
{ {
case VLC_VAR_BOOL: if (list->type != V4L2_CTRL_TYPE_BUTTON)
val.b_bool = control.value; ControlSet (list, list->default_value);
var_Change( p_obj, psz_name, VLC_VAR_SETVALUE, &val, NULL ); list = list->next;
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;
}
} }
return VLC_SUCCESS;
} }
static int DemuxControlCallback( vlc_object_t *p_this, static int ControlsResetCallback (vlc_object_t *obj, const char *var,
const char *psz_var, vlc_value_t oldval, vlc_value_t newval, vlc_value_t old, vlc_value_t cur, void *data)
void *p_data )
{ {
(void)oldval; ControlsReset (data);
demux_t *p_demux = (demux_t*)p_this; (void) obj; (void) var; (void) old; (void) cur;
demux_sys_t *p_sys = p_demux->p_sys; return VLC_SUCCESS;
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;
} }
static int AccessControlCallback( vlc_object_t *p_this, static void ControlsSetFromString (vlc_object_t *obj,
const char *psz_var, vlc_value_t oldval, vlc_value_t newval, const vlc_v4l2_ctrl_t *list)
void *p_data )
{ {
(void)oldval; char *buf = var_InheritString (obj, CFG_PREFIX"set-ctrls");
access_t *p_access = (access_t *)p_this; if (buf == NULL)
demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys; return;
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 ); 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;
/** p += strspn (p, ", ");
* Resets all user-class V4L2 controls to their default value name = p;
*/ end = strchr (p, ',');
static int ControlReset( vlc_object_t *p_obj, int i_fd ) if (end != NULL)
{ *(end++) = '\0';
struct v4l2_queryctrl queryctrl; p = end; /* next name/value pair */
int i_cid;
memset( &queryctrl, 0, sizeof( queryctrl ) );
queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL; end = strchr (name, '=');
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 ) if (end == NULL)
{ {
/* Extended control API supported */ /* TODO? support button controls that way? */
queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL; msg_Err (obj, "syntax error in \"%s\": missing '='", name);
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;
continue; continue;
} }
struct v4l2_control control; *(end++) = '\0';
memset( &control, 0, sizeof( control ) ); value = end;
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
{
/* public controls */ long val = strtol (value, &end, 0);
for( i_cid = V4L2_CID_BASE; if (*end)
i_cid < V4L2_CID_LASTP1;
i_cid ++ )
{ {
queryctrl.id = i_cid; msg_Err (obj, "syntax error in \"%s\": not an integer", value);
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
{
struct v4l2_control control;
if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
continue; 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 (const vlc_v4l2_ctrl_t *c = list; c != NULL; c = c->next)
for( i_cid = V4L2_CID_PRIVATE_BASE; if (!strcasecmp (name, c->name))
;
i_cid ++ )
{
queryctrl.id = i_cid;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
{ {
struct v4l2_control control; ControlSet (c, val);
if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED ) goto next;
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;
} }
msg_Err (obj, "control \"%s\" not available", name);
} }
return VLC_SUCCESS; free (buf);
} }
static int DemuxControlResetCallback( vlc_object_t *p_this, static int cidcmp (const void *a, const void *b)
const char *psz_var, vlc_value_t oldval, vlc_value_t newval, void *p_data )
{ {
(void)psz_var; (void)oldval; (void)newval; (void)p_data; const uint32_t *id = a;
demux_t *p_demux = (demux_t*)p_this; const vlc_v4l2_ctrl_name_t *name = b;
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 );
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; vlc_v4l2_ctrl_t *ctrl = malloc (sizeof (*ctrl));
access_t *p_access = (access_t *)p_this; if (unlikely(ctrl == NULL))
demux_sys_t *p_sys = (demux_sys_t *) p_access->p_sys; return NULL;
int i_fd = p_sys->i_fd; ctrl->fd = fd;
ctrl->id = query->id;
if( i_fd < 0 ) ctrl->type = query->type;
return VLC_EGENERIC;
/* Search for a well-known control */
ControlReset( p_this, i_fd ); 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; #ifndef V4L2_CTRL_FLAG_VOLATILE
int i_val; # define V4L2_CTRL_FLAG_VOLATILE 0x0080
# warning Please update V4L2 kernel headers!
#endif
char *psz_name; #define CTRL_FLAGS_IGNORE \
vlc_value_t val, val2; (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 ) static vlc_v4l2_ctrl_t *ControlAddInteger (vlc_object_t *obj, int fd,
msg_Dbg( p_obj, " control is busy" ); const struct v4l2_queryctrl *query)
if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY ) {
msg_Dbg( p_obj, " control is read-only" ); 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++ ) vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
if( controls[i].i_cid == queryctrl.id ) break; if (unlikely(c == NULL))
return NULL;
if( controls[i].psz_name ) if (var_Create (obj, c->name, VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND))
{
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
{ {
psz_name = strdup( (const char *)queryctrl.name ); free (c);
name2var( (unsigned char *)psz_name ); return NULL;
i_val = -1;
} }
switch( queryctrl.type ) vlc_value_t val;
{ struct v4l2_control ctrl = { .id = query->id };
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;
}
switch( queryctrl.type ) if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
{
case V4L2_CTRL_TYPE_INTEGER:
case V4L2_CTRL_TYPE_BOOLEAN:
case V4L2_CTRL_TYPE_MENU:
{ {
struct v4l2_control control; msg_Dbg (obj, " current: %3"PRId32", default: %3"PRId32,
msg_Dbg( p_obj, " default value: %d", ctrl.value, query->default_value);
queryctrl.default_value ); val.i_int = ctrl.value;
memset( &control, 0, sizeof( control ) ); var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
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 );
}
} }
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 ); val.i_int = query->step;
} var_Change (obj, c->name, VLC_VAR_SETSTEP, &val, NULL);
}
break;
default:
break;
} }
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; vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
var_Change( p_obj, psz_name, VLC_VAR_SETTEXT, &val, NULL ); if (unlikely(c == NULL))
val.i_int = queryctrl.id; return NULL;
val2.psz_string = (char *)psz_name;
var_Change( p_obj, "allcontrols", VLC_VAR_ADDCHOICE, &val, &val2 ); if (var_Create (obj, c->name, VLC_VAR_BOOL | VLC_VAR_ISCOMMAND))
/* 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 )
{ {
case VLC_VAR_BOOL: free (c);
var_SetBool( p_obj, psz_name, i_val ); return NULL;
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;
} }
if( b_demux ) vlc_value_t val;
var_AddCallback( p_obj, psz_name, struct v4l2_control ctrl = { .id = query->id };
DemuxControlCallback, (void*)(intptr_t)queryctrl.id );
else
var_AddCallback( p_obj, psz_name,
AccessControlCallback, (void*)(intptr_t)queryctrl.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" ); msg_Dbg (obj, " menu %s (%08"PRIX32")", query->name, query->id);
if( ctrls == NULL ) if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
return; 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 (c);
free( ctrls ); return NULL;
return;
} }
char *p = ctrls; vlc_value_t val;
if( *p == '{' ) struct v4l2_control ctrl = { .id = query->id };
p++;
while( p != NULL && *p && *p != '}' ) if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
{ {
p += strspn( p, ", " ); msg_Dbg (obj, " current: %"PRId32", default: %"PRId32,
ctrl.value, query->default_value);
const char *name = p; val.i_int = ctrl.value;
char *end = strchr( p, ',' ); var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
if( end == NULL ) }
end = strchr( p, '}' ); val.b_bool = query->default_value;
if( end != NULL ) var_Change (obj, c->name, VLC_VAR_SETDEFAULT, &val, NULL);
*(end++) = '\0'; 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, '=' ); /* Import menu choices */
if( value == NULL ) for (uint_fast32_t idx = query->minimum;
idx <= (uint_fast32_t)query->maximum;
idx++)
{ {
msg_Err( p_obj, "syntax error in \"%s\": missing '='", name ); struct v4l2_querymenu menu = { .id = query->id, .index = idx };
p = end;
if (v4l2_ioctl (fd, VIDIOC_QUERYMENU, &menu) < 0)
continue; 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++ ) static vlc_v4l2_ctrl_t *ControlAddButton (vlc_object_t *obj, int fd,
{ const struct v4l2_queryctrl *query)
vlc_value_t vartext; {
const char *var = text.p_list->p_values[i].psz_string; 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 ); static vlc_v4l2_ctrl_t *ControlAddClass (vlc_object_t *obj, int fd,
if( !strcasecmp( vartext.psz_string, name ) ) const struct v4l2_queryctrl *query)
{ {
Control( p_obj, i_fd, name, msg_Dbg (obj, "control class %s:", query->name);
val.p_list->p_values[i].i_int, (void) fd;
strtol( value, NULL, 0 ) ); return NULL;
free( vartext.psz_string ); }
goto found;
} static vlc_v4l2_ctrl_t *ControlAddUnknown (vlc_object_t *obj, int fd,
free( vartext.psz_string ); const struct v4l2_queryctrl *query)
} {
msg_Err( p_obj, "control %s not available", name ); msg_Dbg (obj, " unknown %s (%08"PRIX32")", query->name, query->id);
found: msg_Warn (obj, " unknown control type %u", (unsigned)query->type);
p = end; (void) fd;
} return NULL;
var_FreeList( &val, &text );
free( ctrls );
} }
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 * Lists all user-class v4l2 controls, sets them to the user specified
* value and create the relevant variables to enable run-time changes. * 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; /* A list of controls that can be modified at run-time is stored in the
int i_cid; * "controls" variable. The V4L2 controls dialog can be built from this. */
const bool b_reset = var_InheritBool( p_obj, CFG_PREFIX"controls-reset" ); var_Create (obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE);
memset( &queryctrl, 0, sizeof( queryctrl ) ); static const ctrl_type_cb handlers[] =
{
/* A list of available controls (aka the variable name) will be [V4L2_CTRL_TYPE_INTEGER] = ControlAddInteger,
* stored as choices in the "allcontrols" variable. We'll thus be able [V4L2_CTRL_TYPE_BOOLEAN] = ControlAddBoolean,
* to use those to create an appropriate interface [V4L2_CTRL_TYPE_MENU] = ControlAddMenu,
* A list of available controls that can be changed mid-stream will [V4L2_CTRL_TYPE_BUTTON] = ControlAddButton,
* be stored in the "controls" variable */ [V4L2_CTRL_TYPE_CTRL_CLASS] = ControlAddClass,
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 );
/* Add a control to reset all controls to their default values */ vlc_v4l2_ctrl_t *list = NULL;
vlc_value_t val, val2; struct v4l2_queryctrl query;
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 );
queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL; query.id = V4L2_CTRL_FLAG_NEXT_CTRL;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 ) 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 */ vlc_v4l2_ctrl_t *c = handler (obj, fd, &query);
queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL; if (c != NULL)
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 ) )
{ {
case V4L2_CTRL_CLASS_USER: vlc_value_t val, text;
msg_Dbg( p_obj, "Available control: %s (%x)",
queryctrl.name, queryctrl.id ); var_AddCallback (obj, c->name, ControlSetCallback, c);
break; text.psz_string = (char *)query.name;
case V4L2_CTRL_CLASS_MPEG: var_Change (obj, c->name, VLC_VAR_SETTEXT, &text, NULL);
name2var( queryctrl.name ); val.i_int = query.id;
msg_Dbg( p_obj, "Available MPEG control: %s (%x)", text.psz_string = (char *)c->name;
queryctrl.name, queryctrl.id ); var_Change (obj, "controls", VLC_VAR_ADDCHOICE, &val, &text);
break;
default: c->next = list;
msg_Dbg( p_obj, "Available private control: %s (%x)", list = c;
queryctrl.name, queryctrl.id );
break;
}
ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
} }
query.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
} }
else
{
msg_Dbg( p_obj, "Extended control API not supported by v4l2 driver" );
/* List public controls */ /* Set well-known controls from VLC configuration */
for( i_cid = V4L2_CID_BASE; for (vlc_v4l2_ctrl_t *ctrl = list; ctrl != NULL; ctrl = ctrl->next)
i_cid < V4L2_CID_LASTP1;
i_cid ++ )
{ {
queryctrl.id = i_cid; if (!CTRL_CID_KNOWN (ctrl->id))
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
{
if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
continue; continue;
msg_Dbg( p_obj, "Available control: %s (%x)",
queryctrl.name, queryctrl.id ); char varname[sizeof (CFG_PREFIX) + sizeof (ctrl->name) - 1];
ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux ); 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 */ /* Set any control from the VLC configuration control string */
for( i_cid = V4L2_CID_PRIVATE_BASE; ControlsSetFromString (obj, list);
;
i_cid ++ ) /* Add a control to reset all controls to their default values */
{
queryctrl.id = i_cid;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
{ {
if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED ) vlc_value_t val, text;
continue;
msg_Dbg( p_obj, "Available private control: %s (%x)", var_Create (obj, "reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND);
queryctrl.name, queryctrl.id ); val.psz_string = _("Reset defaults");
ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux ); var_Change (obj, "reset", VLC_VAR_SETTEXT, &val, NULL);
} val.i_int = -1;
else
break; 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 ); var_Destroy (obj, "controls");
return VLC_SUCCESS;
} }
...@@ -116,6 +116,7 @@ void DemuxClose( vlc_object_t *obj ) ...@@ -116,6 +116,7 @@ void DemuxClose( vlc_object_t *obj )
free( sys->p_buffers ); free( sys->p_buffers );
} }
ControlsDeinit( obj, sys->controls );
v4l2_close( fd ); v4l2_close( fd );
free( sys ); free( sys );
} }
......
...@@ -41,8 +41,6 @@ ...@@ -41,8 +41,6 @@
#define CFG_PREFIX "v4l2-" #define CFG_PREFIX "v4l2-"
int ControlList(vlc_object_t *, int fd, bool b_demux);
/* TODO: remove this, use callbacks */ /* TODO: remove this, use callbacks */
typedef enum { typedef enum {
IO_METHOD_READ=1, IO_METHOD_READ=1,
...@@ -50,7 +48,9 @@ typedef enum { ...@@ -50,7 +48,9 @@ typedef enum {
IO_METHOD_USERPTR, IO_METHOD_USERPTR,
} io_method; } 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 struct demux_sys_t
{ {
int i_fd; int i_fd;
...@@ -67,6 +67,8 @@ struct demux_sys_t ...@@ -67,6 +67,8 @@ struct demux_sys_t
es_out_id_t *p_es; es_out_id_t *p_es;
vlc_v4l2_ctrl_t *controls;
#ifdef HAVE_LIBV4L2 #ifdef HAVE_LIBV4L2
bool b_libv4l2; bool b_libv4l2;
#endif #endif
...@@ -93,3 +95,7 @@ void GetMaxDimensions(vlc_object_t *, int fd, uint32_t fmt, float fps_min, ...@@ -93,3 +95,7 @@ void GetMaxDimensions(vlc_object_t *, int fd, uint32_t fmt, float fps_min,
/* access.c */ /* access.c */
int AccessOpen(vlc_object_t *); int AccessOpen(vlc_object_t *);
void AccessClose(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, ...@@ -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 */ /* TODO: Move the resolution stuff up here */
/* if MPEG encoder card, no need to do anything else after this */ /* 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 */ /* Reset Cropping */
memset( &cropcap, 0, sizeof(cropcap) ); 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