webcam

package module
v0.0.0-...-10ed912 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2020 License: MIT Imports: 8 Imported by: 0

README

go-webcam

Build Status GoDoc

This is a go library for working with webcams and other video capturing devices. It depends entirely on V4L2 framework, thus will compile and work only on Linux machine.

Installation

$ go get github.com/blackjack/webcam

Usage

import "github.com/blackjack/webcam"
// ...
cam, err := webcam.Open("/dev/video0") // Open webcam
if err != nil { panic(err.Error()) }
defer cam.Close()
// ...
// Setup webcam image format and frame size here (see examples or documentation)
// ...
err = cam.StartStreaming()
if err != nil { panic(err.Error()) }
for {
  err = cam.WaitForFrame(timeout)

  switch err.(type) {
  case nil:
  case *webcam.Timeout:
    fmt.Fprint(os.Stderr, err.Error())
    continue
  default:
    panic(err.Error())
  }

  frame, err := cam.ReadFrame()
  if len(frame) != 0 {
   // Process frame
  } else if err != nil {
    panic(err.Error())
  }
}

For more detailed example see examples folder The number of frame buffers used may be set as:

// If already streaming, stop streaming.
if streaming_on {
  cam.StopStreaming()
}
err = cam.SetBufferCount(64)

Roadmap

The library is still under development so API changes can happen. Currently library supports streaming using only MMAP method, which should be sufficient for most of devices available on the market. Other streaming methods can be added in future (please create issue if you need this).

Also currently image format is defined by 4-byte code received from V4L2, which is good in terms of compatibility with different versions of Linux kernel, but not very handy if you want to do some image manipulations. Plans are to aligh V4L2 image format codes with Image package from Go library.

License

See LICENSE file

Documentation

Overview

Library for working with webcams and other video capturing devices. It depends entirely on v4l2 framework, thus will compile and work only on Linux machine

Index

Constants

View Source
const (
	V4L2_CAP_VIDEO_CAPTURE      uint32 = 0x00000001
	V4L2_CAP_STREAMING          uint32 = 0x04000000
	V4L2_BUF_TYPE_VIDEO_CAPTURE uint32 = 1
	V4L2_MEMORY_MMAP            uint32 = 1
	V4L2_FIELD_ANY              uint32 = 0
)
View Source
const (
	V4L2_FRMSIZE_TYPE_DISCRETE   uint32 = 1
	V4L2_FRMSIZE_TYPE_CONTINUOUS uint32 = 2
	V4L2_FRMSIZE_TYPE_STEPWISE   uint32 = 3
)
View Source
const (
	V4L2_CID_BASE               uint32 = 0x00980900
	V4L2_CID_AUTO_WHITE_BALANCE uint32 = V4L2_CID_BASE + 12
	V4L2_CID_PRIVATE_BASE       uint32 = 0x08000000
)
View Source
const (
	V4L2_CTRL_TYPE_INTEGER      uint32 = 1
	V4L2_CTRL_TYPE_BOOLEAN      uint32 = 2
	V4L2_CTRL_TYPE_MENU         uint32 = 3
	V4L2_CTRL_TYPE_BUTTON       uint32 = 4
	V4L2_CTRL_TYPE_INTEGER64    uint32 = 5
	V4L2_CTRL_TYPE_CTRL_CLASS   uint32 = 6
	V4L2_CTRL_TYPE_STRING       uint32 = 7
	V4L2_CTRL_TYPE_BITMASK      uint32 = 8
	V4L2_CTRL_TYPE_INTEGER_MENU uint32 = 9

	V4L2_CTRL_COMPOUND_TYPES uint32 = 0x0100
	V4L2_CTRL_TYPE_U8        uint32 = 0x0100
	V4L2_CTRL_TYPE_U16       uint32 = 0x0101
	V4L2_CTRL_TYPE_U32       uint32 = 0x0102
)
View Source
const (
	V4L2_CTRL_FLAG_DISABLED  uint32 = 0x00000001
	V4L2_CTRL_FLAG_NEXT_CTRL uint32 = 0x80000000
)

Variables

View Source
var (
	VIDIOC_QUERYCAP  = ioctl.IoR(uintptr('V'), 0, unsafe.Sizeof(v4l2_capability{}))
	VIDIOC_ENUM_FMT  = ioctl.IoRW(uintptr('V'), 2, unsafe.Sizeof(v4l2_fmtdesc{}))
	VIDIOC_S_FMT     = ioctl.IoRW(uintptr('V'), 5, unsafe.Sizeof(v4l2_format{}))
	VIDIOC_REQBUFS   = ioctl.IoRW(uintptr('V'), 8, unsafe.Sizeof(v4l2_requestbuffers{}))
	VIDIOC_QUERYBUF  = ioctl.IoRW(uintptr('V'), 9, unsafe.Sizeof(v4l2_buffer{}))
	VIDIOC_QBUF      = ioctl.IoRW(uintptr('V'), 15, unsafe.Sizeof(v4l2_buffer{}))
	VIDIOC_DQBUF     = ioctl.IoRW(uintptr('V'), 17, unsafe.Sizeof(v4l2_buffer{}))
	VIDIOC_G_CTRL    = ioctl.IoRW(uintptr('V'), 27, unsafe.Sizeof(v4l2_control{}))
	VIDIOC_S_CTRL    = ioctl.IoRW(uintptr('V'), 28, unsafe.Sizeof(v4l2_control{}))
	VIDIOC_QUERYCTRL = ioctl.IoRW(uintptr('V'), 36, unsafe.Sizeof(v4l2_queryctrl{}))
	//sizeof int32
	VIDIOC_STREAMON        = ioctl.IoW(uintptr('V'), 18, 4)
	VIDIOC_STREAMOFF       = ioctl.IoW(uintptr('V'), 19, 4)
	VIDIOC_ENUM_FRAMESIZES = ioctl.IoRW(uintptr('V'), 74, unsafe.Sizeof(v4l2_frmsizeenum{}))

	NativeByteOrder = getNativeByteOrder()
)

Functions

func CToGoString

func CToGoString(c []byte) string

Types

type Control

type Control struct {
	Name string
	Min  int32
	Max  int32
}

type ControlID

type ControlID uint32

type FrameSize

type FrameSize struct {
	MinWidth  uint32
	MaxWidth  uint32
	StepWidth uint32

	MinHeight  uint32
	MaxHeight  uint32
	StepHeight uint32
}

Struct that describes frame size supported by a webcam For fixed sizes min and max values will be the same and step value will be equal to '0'

func (FrameSize) GetString

func (s FrameSize) GetString() string

Returns string representation of frame size, e.g. 1280x720 for fixed-size frames and [320-640;160]x[240-480;160] for stepwise-sized frames

type PixelFormat

type PixelFormat uint32

Represents image format code used by V4L2 subsystem. Number of formats can be different in various Linux kernel versions See /usr/include/linux/videodev2.h for full list of supported image formats

type Timeout

type Timeout struct{}

Timeout error

func (*Timeout) Error

func (e *Timeout) Error() string

type Webcam

type Webcam struct {
	// contains filtered or unexported fields
}

Webcam object

func Open

func Open(path string) (*Webcam, error)

Open a webcam with a given path Checks if device is a v4l2 device and if it is capable to stream video

func (*Webcam) Close

func (w *Webcam) Close() error

Close the device

func (*Webcam) GetControl

func (w *Webcam) GetControl(id ControlID) (int32, error)

Get the value of a control.

func (*Webcam) GetControls

func (w *Webcam) GetControls() map[ControlID]Control

Get a map of available controls.

func (*Webcam) GetFrame

func (w *Webcam) GetFrame() ([]byte, uint32, error)

Get a single frame from the webcam and return the frame and the buffer index. To return the buffer, ReleaseFrame must be called. If frame cannot be read at the moment function will return empty slice

func (*Webcam) GetSupportedFormats

func (w *Webcam) GetSupportedFormats() map[PixelFormat]string

Returns image formats supported by the device alongside with their text description Not that this function is somewhat experimental. Frames are not ordered in any meaning, also duplicates can occur so it's up to developer to clean it up. See http://linuxtv.org/downloads/v4l-dvb-apis/vidioc-enum-framesizes.html for more information

func (*Webcam) GetSupportedFrameSizes

func (w *Webcam) GetSupportedFrameSizes(f PixelFormat) []FrameSize

Returns supported frame sizes for a given image format

func (*Webcam) ReadFrame

func (w *Webcam) ReadFrame() ([]byte, error)

Read a single frame from the webcam If frame cannot be read at the moment function will return empty slice

func (*Webcam) ReleaseFrame

func (w *Webcam) ReleaseFrame(index uint32) error

Release the frame buffer that was obtained via GetFrame

func (*Webcam) SetAutoWhiteBalance

func (w *Webcam) SetAutoWhiteBalance(val bool) error

Sets automatic white balance correction

func (*Webcam) SetBufferCount

func (w *Webcam) SetBufferCount(count uint32) error

Set the number of frames to be buffered. Not allowed if streaming is already on.

func (*Webcam) SetControl

func (w *Webcam) SetControl(id ControlID, value int32) error

Set a control.

func (*Webcam) SetImageFormat

func (w *Webcam) SetImageFormat(f PixelFormat, width, height uint32) (PixelFormat, uint32, uint32, error)

Sets desired image format and frame size Note, that device driver can change that values. Resulting values are returned by a function alongside with an error if any

func (*Webcam) StartStreaming

func (w *Webcam) StartStreaming() error

Start streaming process

func (*Webcam) StopStreaming

func (w *Webcam) StopStreaming() error

func (*Webcam) WaitForFrame

func (w *Webcam) WaitForFrame(timeout uint32) error

Wait until frame could be read

Directories

Path Synopsis
examples
getcontrols
Example program that reads the list of available controls and prints them.
Example program that reads the list of available controls and prints them.
http_mjpeg_streamer
Example program that uses blakjack/webcam library for working with V4L2 devices.
Example program that uses blakjack/webcam library for working with V4L2 devices.
stdout_streamer
Example program that uses blakjack/webcam library for working with V4L2 devices.
Example program that uses blakjack/webcam library for working with V4L2 devices.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL