/go-uvc

USB Video Camera (UVC) devices with Go

Primary LanguageGoApache License 2.0Apache-2.0

github.com/kevmo314/go-uvc

An almost-pure-Go library for accessing UVC devices. The library currently depends on libusb via cgo but not libuvc. One day this may change but libusb is much more complex.

The non-Go equivalent of this library is libuvc.

image

Features

  • UVC 1.5 support
    • Input terminals (recording from cameras)
    • Output terminals (displaying images on a device)
  • Isochronous and bulk transfer support
  • Android support
  • Video decoding API
  • Camera controls
  • UAC Audio support

Demo

go-uvc includes a debugging tool (the screenshot above) to connect to and debug cameras. To use it,

go run github.com/kevmo314/go-uvc/cmd/inspect -path /dev/bus/usb/001/002

Usage

A minimal example of how you might use go-uvc.

package main

import (
	"image/jpeg"
	"fmt"
	"log"
	"syscall"

	"github.com/kevmo314/go-uvc"
	"github.com/kevmo314/go-uvc/pkg/descriptors"
)

func main() {
	fd, err := syscall.Open("/dev/bus/usb/001/002", syscall.O_RDWR, 0)
	if err != nil {
		panic(err)
	}

	ctx, err := uvc.NewUVCDevice(uintptr(fd))
	if err != nil {
		panic(err)
	}

	info, err := ctx.DeviceInfo()
	if err != nil {
		panic(err)
	}

	for _, iface := range info.StreamingInterfaces {
		for i, desc := range iface.Descriptors {
			fd, ok := desc.(*descriptors.MJPEGFormatDescriptor)
			if !ok {
				continue
			}
			frd := iface.Descriptors[i+1].(*descriptors.MJPEGFrameDescriptor)

			resp, err := iface.ClaimFrameReader(fd.Index(), frd.Index())
			if err != nil {
   				panic(err)
			}

			for i := 0; ; i++ {
				fr, err := resp.ReadFrame()
				if err != nil {
   					panic(err)
				}
				img, err := jpeg.Decode(fr)
				if err != nil {
					continue
				}
    				// do something with img
			}
		}
	}
}