# OpenBSD How to use a webcam

## Enabling Video Recording (OpenBSD 6.9)

For privacy reasons, video recording is disabled by default in OpenBSD.
The sysctl "kern.video.record" may be used to enable it. While recording
is disabled, a blank video will be shown.

```
# sysctl kern.video.record=1
# echo kern.video.record=1 >> /etc/sysctl.conf
```

## Supported Hardware

Most webcams today implement the USB Video Class (UVC) specification,
which is supported by the uvideo(4) device driver and attaches to the
video(4) device. The manpage lists some supported devices, but there is
a good chance that other devices work as well. For example, webcams in
Lenovo Thinkpad laptops do usually work.

A supported webcam (or other video device) shows up in dmesg like this:

```
uvideo0 at uhub0 port 8 configuration 1 interface 0 "Azurewave Integrated Camera"rev 2.01/69.05 addr 10
video0 at uvideo0
uvideo1 at uhub0 port 8 configuration 1 interface 2 "Azurewave Integrated Camera" rev 2.01/69.05 addr 10
video1 at uvideo1
```

You see that an uvideo device was detected and has been attached to
video0. This device will be accessible through /dev/video0.

Modern laptops sometimes attach a second video device, which is the
infrared camera for face recognition. The second camera does not produce
a usable video stream.

You can find the usable camera with the video(1) command:

```
$ video -q -f /dev/video0
video device /dev/video0:
  encodings: yuy2
  frame sizes (width x height, in pixels) and rates (in frames per 
  second):
        320x180: 30
        320x240: 30
        352x288: 30
        424x240: 30
        640x360: 30
        640x480: 30
        848x480: 20
        960x540: 15
        1280x720: 10
  controls: brightness, contrast, saturation, hue, gamma, sharpness, 
  white_balance_temperature

$ video -q -f /dev/video1
video: /dev/video1 has no usable YUV encodings
```

The usable camera device shows supported resolutions and frameratei.
Note that the framerates only apply to the uncompressed YUY2 stream.
More on that in "Recording a webcam stream"

## Using a webcam as user

To use the webcam as regular user, you need to change the device
permissions. Only root is allowed to access video devices by default.

One way of allowing your user to access the video devices is to change
the permissions from ~/.xsession. You can configure doas(1) to perform
chmod(1) as superuser without asking for a password for your user.

```
$ doas chown $USER /dev/video0
```

## Recoding a webcam stream

This section uses ffplay and ffmpeg from graphics/ffmpeg. To find out
what your camera is capable of, run the following command:

```
$ ffplay -f v4l2 -list_formats all -i /dev/video0
[...]
[video4linux2,v4l2 @ 0x921f8420800] Raw : yuyv422 : YUYV : 640x480 320x180 320x240 352x288 424x240 640x360 848x480 960x540 1280x720
[video4linux2,v4l2 @ 0x921f8420800] Compressed: mjpeg : MJPEG : 1280x720 320x180 320x240 352x288 424x240 640x360 640x480 848x480
```

At the end of the output, you'll find two lines similiar to the two
above.

The first line shows resolutions supported in the uncompressed YUYV
format. The frame rates in this format can be very low. The second line
shows the supported MJPEG compressed video resolutions, which deliver
much higher framerates (usually 30fps or 60fps).

Now try to play the webcam stream. Choose one of the MJPEG resolutions
and run:

```
$ ffplay -f v4l2 -input_format mjpeg -video_size 1280x720 -i /dev/video0
[...]
Input #0, video4linux2,v4l2, from '/dev/video0':B sq=    0B f=0/0
  Duration: N/A, start: 1599377893.546533, bitrate: N/A
    Stream #0:0: Video: mjpeg (Baseline), yuvj422p(pc, bt470bg/unknown/unknown), 1280x720, 30 fps, 30 tbr, 1000k tbn, 1000k tbc
```

The webcam stream should be displayed. Ffplay also shows the used
resolution and the framerate (fps) used.

If this works, you can go ahead and record the video with ffmpeg:

```
$ ffmpeg -f v4l2 -input_format mjpeg -video_size 1280x720 \
    -i /dev/video0 ~/video.mkv
```

## Controlling webcam settings

Webcams usually have brightness, contrast and other controls. Video(1)
allows you to alter these settings.

First find out which supported controls your camera has:

```
$ video -c
brightness=128
contrast=32
saturation=64
hue=0
gamma=120
sharpness=3
white_balance_temperature=auto
```

You can change for example the brightness setting to 200:

```
$ video brightness=200
brightness: 128 -> 200
```

If you would like to reset all settings to their defaults, you can do so
with:

```
$ video -d
```

```
$ video -c
brightness=128
contrast=32
saturation=64
hue=0
gamma=120
sharpness=3
white_balance_temperature=auto
```

Some settings, like the white_balance_temperature support automatic
adjustments. You can set them to a fixed value or set them to "auto",
which lets the camera decide and use whatever value it thinks is best.

## Access webcam in chromium

Chromium has access to /dev/video and /dev/video0 by default. To allow
Chromium to access other video devices, you need to add the device paths
tto /etc/chromium/unveil.main and /etc/chromium/unveil.utility_video

## Access webcam in firefox

Firefox has access to /dev/video and /dev/video0 by default. To allow
Firefox to access other video devices, you need to add the device paths
to /etc/firefox/unveil.main.
