A C library providing basic access to webcams on Windows, macOS and Linux.
- C++ 38.2%
- C 30.3%
- Objective-C 27.5%
- Lua 4%
| examples | ||
| include | ||
| src | ||
| .clang-format | ||
| .gitignore | ||
| LICENSE.md | ||
| premake5.lua | ||
| README.md | ||
sr_webcam
This is a simple wrapper around live camera video capture libraries on Windows, macOS and Linux. It respectively interacts with:
- the MediaFoundation library on Windows
- the AVFoundation framework on macOS
- the video4linux2 system library on Linux
Basic examples in C and C++ (including synchronization details) can be found in the examples directory. The examples connect to the first available device and display the capture in an OpenGL window.
Common files: include/sr_webcam.h, src/sr_webcam_internal.h, src/sr_webcam.c
Dependencies
Each platform implementation can be found in a separate file.
- On Windows, this should link against
mfplat,mf,mfuuid,MfreadwriteandShlwapi. Implementation is insrc/sr_webcam_win.cpp(C++). - On macOS, this should link against
AVFoundation.framework,CoreMedia.frameworkandAccelerate.framework. Implementation is insrc/sr_webcam_mac.m(Objective-C). You additionally need to request the capability for your app to access camera devices by either:- placing
examples/Info.plistin the same directory as your binary. - embedding it in the binary, in a
__TEXTsection named__info_plist(seeexamples/premake5.luafor an example).
- placing
- On Linux, this should link against
pthread. Implementation is insrc/sr_webcam_lin.c(C).
Usage
struct sr_webcam_device;
typedef void (*sr_webcam_callback)(sr_webcam_device * device, void * data);
Selecting a device and configuring it
int sr_webcam_create(sr_webcam_device ** device, int deviceId);
void sr_webcam_set_format(sr_webcam_device * device, int width, int height, int framerate);
void sr_webcam_set_callback(sr_webcam_device * device, sr_webcam_callback callback);
void sr_webcam_set_user(sr_webcam_device * device, void * user);
Starting video capture
int sr_webcam_open(sr_webcam_device * device);
void sr_webcam_start(sr_webcam_device * device);
Ending video capture
void sr_webcam_stop(sr_webcam_device * device);
void sr_webcam_delete(sr_webcam_device * device);
Querying device informations
long sr_webcam_get_format_size(sr_webcam_device * device);
void * sr_webcam_get_user(sr_webcam_device * device);
void sr_webcam_get_dimensions(sr_webcam_device * device, int * width, int * height);
void sr_webcam_get_framerate(sr_webcam_device * device, int * fps);
Limitations
This utility library assumes that the device you are capturing can provide frames in an R8G8B8 or similar, and doesn't support conversion from more complex format such as YUV420. The library only allows you to select a device by ID, and doesn't expose querying for a device name.