Skip to content

Low level UVC (USB) Camera control using v4l2 ioctl (Linux), AVFoundation (MacOS) and MediaCapture (Windows) apis

License

Notifications You must be signed in to change notification settings

danlargo/v4l2Camera

Repository files navigation


Overview

V4l2Camera is an attempt to abstract the video4linux2 api into something more easily usable in a generic C++ application

  • I set out, naively, to create cross-platorm support (Linux, MacOS, Windows) but soon realized the extent of that effort
  • The linux version is "done", although I know there are lots of refinements possible/needed/required
  • I think I see a path forward on the MacOS and Windows side of things after starting down a few interesting pathways (libuvc, libusb)

Details

  • Low level camera control using (or emulating) the video4linux2 interface

  • Currently supporting Linux based systems (tested on Debian AMD64 and Raspbian ARM64 platforms)

    • support planned for MacOS (via AVFoundation api), and
    • Windows (via Media.Capture api)
  • Designed to support both command line and GUI based applications

  • Example code provided for command line application, sample application (binary-only) provided for GUI based application

  • Distributed as open-source under the MIT License




Index

  • -here- V4l2Camera Overview
  • -here- API/Class Details
  • -here- Command Line Example Code
  • -here- camControl Cross Platform GUI app
  • -here- Image Conversion Functions




Distribution

  • The v4l2camera class source code is distributed as open source under the MIT License
    • If you use it and find it useful, drop me a line and let me know
    • Provide attribution and a link back to the github repo so more people can find the code and use it (please)
    • I have gained so much information from the open source community that I figured it was time to give back
  • The camControl app is distributed free of charge on Linux platforms, all rights for this app remain with the developer
    • will decide how to distribute the MacOS and Windows versions once they exist




Build Dependencies

General

  • Makefile is absolutely generic, I spent time on the source code and know I could clean up the Makeifle, will get to it :)
    • Currenly uses the gcc/g++ compiler and has been tested on Linux and MacOS (sort of)
    • MacOS version has not attempted to include AVFoundation api so may require changes to use the LLVM compiler and different include paths
    • Windows version has not been started
  • Makefile will be updated to use native compiler on each platform (as required, would prefer to use gcc/g+ if possible)

Linux

  • Dependent only on the std C++ library

MacOS

  • In progress, will (hopefully) be designed to work with the AVFoundation api

Windows

  • In progress, will be designed to work with the Media.Capture api




Building V4l2Camera into your own App

Download

  • Download or clone the git repo (this repo)
$ git clone https://github.com/danlargo/v4l2Camera.git

Make (MAC and Linux)

  • Run make in the source folder, this will create the example binary (v4l2cam) and create a distribution folder for inclusion in other apps (v4l2cam-dist)
$ cd v4l2camera
$ make

  • Copy the distribution folder into the top level of your project
$ cp -r ./v4l2cam-dist ../myProject

Make (Windows)

  • Check out this link for help if command line tools are not installed - here

  • Verify the C++ command line tools are installed

    • Open a Developer Power Shell from the Visual Studio app
      • Tools -> Command Line -> Developer Power Shell
    • change folder to v4l2camera
    • run nmake
C:\...> cd v4l2camera
C:\...> dir
...should show the file structure for v4l2camera

c:\...> nmake
  • should build the v4l2camera library and command line files

    • might say "nmake : The term 'nmake' is not recognized as the name of a cmdlet...", which means the command line tools are not installed or not in the command path
  • Copy the distribution folder into the top level of your project

$ cp -r ./v4l2cam-dist ../myProject

Add to your project source - Linux

#include "../linux/linuxcamera.h"
.
.
.
// generate a list of UVC cameras available in the system

std::vector<LinuxCamera*> camlist = LinuxCamera::DiscoverCameras();

if( camlist.size() == 0 ) std::cerr << "No Cameras in System??" << std::endl;
else
{
    LinuxCamera * cam = camList[0];
    if( !cam->open() ) std::cerr << "Camera is not accessible - Not Open" << std::endl;
    else
    {
        // do stuff here

    }
}

Add to your Makefile

g++ -g -std=c++20 -o main.o main.c
g++ -g -o main main.o v4l2cam-dist/libv4l2cam-linux-arm64.a





Example Code

  • See command line example folder




Sample App - camControl

  • camControl is built to run in Linux AMD64 and ARM64 GUI environments
    • built on Debian Linux (AMD64), and
    • Raspbian, ARM64
  • It is distributed on Linux platforms free of charge.




Class method documentation




Image Conversion Functions

  • Information I have collected around the internet regarding conversion from YUV imager formats to generic RGB32 bitmaps
  • Here




ToDo

  • Add better support for fourCC codes in Video modes

    • currently lookup and display is managed via string version of fourCC code, which is not guarranteed to be unique
    • fourCC convertion functions are provided in v4l2camera.cpp but not used in video mode queries and selection
  • Add User Control interface into example command line app (v4l2cam)

    • User Controls are fully supported in the V4l2Camera class
  • MacOS support for V4l2Camera

    • went down a few blind alleys...
      • libucv here
        • not unhappy with the api, but found it frustrating in that they still haven't dealt with the app permission issue on MacOS (which may not be solveable)
      • libusb here
        • for obvious reasons it felt like I was simply duplicating the libuvc effort
      • QCamera (on Qt 6) here
        • this effort proved the most frustrsating as I am using Qt as a cross platform GUI development environment (maybe that is my issue)
        • Qt 6 is not supported on Raspbian, which is where the majority of my development is these days, Qt 5 does not support QCamera
        • QCamera is able to fetch images from USB cameras on MacOS but has zero support for any User Controls
          • I have a question ticket here into Qt Forum but have received no response
          • This seems to be an issue that has existed for a while.
    • Everything I am reading is pointing towards having to deal with the AVFoundation api from Apple.
      • All documentation points to XCode and Swift/Objective-C
      • I want to provide command line and GUI support for C++, so may be banging my head on a wall here
      • I want to make sure to properly support the request and maintenance of proper user/device permissionsas well
  • MacOS build of camControl

    • will use this an indication that the class library actually works on MacOS
  • Windows support

    • looking at the Media.Capture api
    • very preliminary
  • Windows build of camControl

    • will use this an indication that the class library actually works on Windows

About

Low level UVC (USB) Camera control using v4l2 ioctl (Linux), AVFoundation (MacOS) and MediaCapture (Windows) apis

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published