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 ) if (ControlSet (ctrl, cur.i_int))
return VLC_SUCCESS;
memset( &queryctrl, 0, sizeof( queryctrl ) );
queryctrl.id = i_cid;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) < 0
|| 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; if (list->type != V4L2_CTRL_TYPE_BUTTON)
msg_Dbg( p_obj, "video %s: %d", psz_name, control.value ); ControlSet (list, list->default_value);
switch( var_Type( p_obj, psz_name ) & VLC_VAR_TYPE ) list = list->next;
{
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;
}
} }
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 */
queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL;
while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
{ {
if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS /* TODO? support button controls that way? */
|| V4L2_CTRL_ID2CLASS( queryctrl.id ) == V4L2_CTRL_CLASS_MPEG ) msg_Err (obj, "syntax error in \"%s\": missing '='", name);
{ continue;
queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
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;
} }
} *(end++) = '\0';
else value = end;
{
/* 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 ) continue;
{
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 )
{
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; return (int32_t)(*id - name->cid);
if( i_fd < 0 )
return VLC_EGENERIC;
ControlReset( p_this, i_fd );
return VLC_EGENERIC;
} }
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;
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';
}
int i_fd = p_sys->i_fd; ctrl->default_value = query->default_value;
return ctrl;
}
if( i_fd < 0 )
return VLC_EGENERIC;
ControlReset( p_this, i_fd ); #ifndef V4L2_CTRL_FLAG_VOLATILE
# define V4L2_CTRL_FLAG_VOLATILE 0x0080
# warning Please update V4L2 kernel headers!
#endif
return VLC_EGENERIC; #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 */)
/***************************************************************************** static vlc_v4l2_ctrl_t *ControlAddInteger (vlc_object_t *obj, int fd,
* Print a user-class v4l2 control's details, create the relevant variable, const struct v4l2_queryctrl *query)
* 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; msg_Dbg (obj, " integer %s (%08"PRIX32")", query->name, query->id);
unsigned int i_mid; if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
return NULL;
int i; vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
int i_val; if (unlikely(c == NULL))
return NULL;
char *psz_name; if (var_Create (obj, c->name, VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND))
vlc_value_t val, val2; {
free (c);
if( queryctrl.flags & V4L2_CTRL_FLAG_GRABBED ) return NULL;
msg_Dbg( p_obj, " control is busy" ); }
if( queryctrl.flags & V4L2_CTRL_FLAG_READ_ONLY )
msg_Dbg( p_obj, " control is read-only" );
for( i = 0; controls[i].psz_name != NULL; i++ ) vlc_value_t val;
if( controls[i].i_cid == queryctrl.id ) break; struct v4l2_control ctrl = { .id = query->id };
if( controls[i].psz_name ) if (v4l2_ioctl (fd, VIDIOC_G_CTRL, &ctrl) >= 0)
{ {
psz_name = strdup( controls[i].psz_name ); msg_Dbg (obj, " current: %3"PRId32", default: %3"PRId32,
char psz_cfg_name[40]; ctrl.value, query->default_value);
sprintf( psz_cfg_name, CFG_PREFIX "%s", psz_name ); val.i_int = ctrl.value;
i_val = var_CreateGetInteger( p_obj, psz_cfg_name ); var_Change (obj, c->name, VLC_VAR_SETVALUE, &val, NULL);
var_Destroy( p_obj, psz_cfg_name );
} }
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)
{ {
psz_name = strdup( (const char *)queryctrl.name ); val.i_int = query->step;
name2var( (unsigned char *)psz_name ); var_Change (obj, c->name, VLC_VAR_SETSTEP, &val, NULL);
i_val = -1;
} }
val.i_int = query->default_value;
var_Change (obj, c->name, VLC_VAR_SETDEFAULT, &val, NULL);
return c;
}
switch( queryctrl.type ) static vlc_v4l2_ctrl_t *ControlAddBoolean (vlc_object_t *obj, int fd,
{ const struct v4l2_queryctrl *query)
case V4L2_CTRL_TYPE_INTEGER: {
msg_Dbg( p_obj, " integer control" ); msg_Dbg (obj, " boolean %s (%08"PRIX32")", query->name, query->id);
msg_Dbg( p_obj, if (query->flags & (CTRL_FLAGS_IGNORE | V4L2_CTRL_FLAG_WRITE_ONLY))
" valid values: %d to %d by steps of %d", return NULL;
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 ) vlc_v4l2_ctrl_t *c = ControlCreate (fd, query);
{ if (unlikely(c == NULL))
case V4L2_CTRL_TYPE_INTEGER: return NULL;
case V4L2_CTRL_TYPE_BOOLEAN:
case V4L2_CTRL_TYPE_MENU:
{
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 );
}
}
else
{
Control( p_obj, i_fd, psz_name, queryctrl.id, i_val );
}
}
break;
default:
break;
}
val.psz_string = (char *)queryctrl.name; if (var_Create (obj, c->name, VLC_VAR_BOOL | VLC_VAR_ISCOMMAND))
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 )
{ {
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);
/* Import menu choices */
for (uint_fast32_t idx = query->minimum;
idx <= (uint_fast32_t)query->maximum;
idx++)
{
struct v4l2_querymenu menu = { .id = query->id, .index = idx };
char *value = strchr( p, '=' ); if (v4l2_ioctl (fd, VIDIOC_QUERYMENU, &menu) < 0)
if( value == NULL )
{
msg_Err( p_obj, "syntax error in \"%s\": missing '='", name );
p = end;
continue; continue;
} msg_Dbg (obj, " choice %"PRIu32") %s", menu.index, menu.name);
*(value++) = '\0';
for( int i = 0; i < val.p_list->i_count; i++ ) vlc_value_t text;
{ val.i_int = menu.index;
vlc_value_t vartext; text.psz_string = (char *)menu.name;
const char *var = text.p_list->p_values[i].psz_string; var_Change (obj, c->name, VLC_VAR_ADDCHOICE, &val, &text);
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 ); return c;
free( ctrls ); }
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;
}
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 * 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 ) );
/* A list of available controls (aka the variable name) will be static const ctrl_type_cb handlers[] =
* stored as choices in the "allcontrols" variable. We'll thus be able {
* to use those to create an appropriate interface [V4L2_CTRL_TYPE_INTEGER] = ControlAddInteger,
* A list of available controls that can be changed mid-stream will [V4L2_CTRL_TYPE_BOOLEAN] = ControlAddBoolean,
* be stored in the "controls" variable */ [V4L2_CTRL_TYPE_MENU] = ControlAddMenu,
var_Create( p_obj, "controls", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE ); [V4L2_CTRL_TYPE_BUTTON] = ControlAddButton,
var_Create( p_obj, "allcontrols", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE ); [V4L2_CTRL_TYPE_CTRL_CLASS] = ControlAddClass,
};
vlc_v4l2_ctrl_t *list = NULL;
struct v4l2_queryctrl query;
query.id = V4L2_CTRL_FLAG_NEXT_CTRL;
while (v4l2_ioctl (fd, VIDIOC_QUERYCTRL, &query) >= 0)
{
ctrl_type_cb handler = NULL;
if (query.type < (sizeof (handlers) / sizeof (handlers[0])))
handler = handlers[query.type];
if (handler == NULL)
handler = ControlAddUnknown;
vlc_v4l2_ctrl_t *c = handler (obj, fd, &query);
if (c != NULL)
{
vlc_value_t val, text;
var_Create( p_obj, "controls-update", VLC_VAR_VOID | VLC_VAR_ISCOMMAND ); 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);
/* Add a control to reset all controls to their default values */ c->next = list;
vlc_value_t val, val2; list = c;
var_Create( p_obj, "controls-reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND ); }
val.psz_string = _( "Reset controls to default" ); query.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
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; /* Set well-known controls from VLC configuration */
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 ) for (vlc_v4l2_ctrl_t *ctrl = list; ctrl != NULL; ctrl = ctrl->next)
{ {
msg_Dbg( p_obj, "Extended control API supported by v4l2 driver" ); if (!CTRL_CID_KNOWN (ctrl->id))
continue;
/* List extended controls */ char varname[sizeof (CFG_PREFIX) + sizeof (ctrl->name) - 1];
queryctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL; sprintf (varname, CFG_PREFIX"%s", ctrl->name);
while( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
{ int64_t val = var_InheritInteger (obj, varname);
if( queryctrl.type == V4L2_CTRL_TYPE_CTRL_CLASS ) if (val == -1)
{ continue; /* the VLC default value: "do not modify" */
msg_Dbg( p_obj, "%s", queryctrl.name ); ControlSet (ctrl, val);
queryctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL;
continue;
}
switch( V4L2_CTRL_ID2CLASS( queryctrl.id ) )
{
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;
}
} }
else
/* Set any control from the VLC configuration control string */
ControlsSetFromString (obj, list);
/* Add a control to reset all controls to their default values */
{ {
msg_Dbg( p_obj, "Extended control API not supported by v4l2 driver" ); vlc_value_t val, text;
/* List public controls */ var_Create (obj, "reset", VLC_VAR_VOID | VLC_VAR_ISCOMMAND);
for( i_cid = V4L2_CID_BASE; val.psz_string = _("Reset defaults");
i_cid < V4L2_CID_LASTP1; var_Change (obj, "reset", VLC_VAR_SETTEXT, &val, NULL);
i_cid ++ ) val.i_int = -1;
{
queryctrl.id = i_cid;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
{
if( queryctrl.flags & V4L2_CTRL_FLAG_DISABLED )
continue;
msg_Dbg( p_obj, "Available control: %s (%x)",
queryctrl.name, queryctrl.id );
ControlListPrint( p_obj, i_fd, queryctrl, b_reset, b_demux );
}
}
/* List private controls */ text.psz_string = (char *)"reset";
for( i_cid = V4L2_CID_PRIVATE_BASE; var_Change (obj, "controls", VLC_VAR_ADDCHOICE, &val, &text);
; var_AddCallback (obj, "reset", ControlsResetCallback, list);
i_cid ++ )
{
queryctrl.id = i_cid;
if( v4l2_ioctl( i_fd, VIDIOC_QUERYCTRL, &queryctrl ) >= 0 )
{
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;
}
} }
if (var_InheritBool (obj, CFG_PREFIX"controls-reset"))
ControlsReset (list);
SetAvailControlsByString( p_obj, i_fd ); return list;
return VLC_SUCCESS; }
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;
}
var_Destroy (obj, "controls");
} }
...@@ -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