Document ffmpeg module

This commit is contained in:
Steins7 2024-09-22 23:25:52 +02:00
parent 12fbb0ce77
commit ae33580474

View File

@ -1,5 +1,6 @@
/** @file ffmpeg.c /** @file ffmpeg.c
* * Module interfacing with ffmpeg and v4l2. Cointains mainly boilerplate and
* tool functions to abstract things a bit
*/ */
//--includes-------------------------------------------------------------------- //--includes--------------------------------------------------------------------
@ -23,10 +24,16 @@ const AVDeviceInfo* find_h264_device(const AVDeviceInfoList* device_list);
//--public functions------------------------------------------------------------ //--public functions------------------------------------------------------------
/**
* Initialises the ffmpeg module, scanning through the available devices to find
* a v4l2 USB device that uses H264 since that would correspond to our webcam
*/
int ffmpeg_init(void) int ffmpeg_init(void)
{ {
//initialises avdevice module
avdevice_register_all(); avdevice_register_all();
//list all input devices using v4l2
const AVInputFormat* v4l2_format = av_find_input_format("v4l2"); const AVInputFormat* v4l2_format = av_find_input_format("v4l2");
AVDeviceInfoList* device_info_list; AVDeviceInfoList* device_info_list;
int device_nb = avdevice_list_input_sources(v4l2_format, nullptr, nullptr, int device_nb = avdevice_list_input_sources(v4l2_format, nullptr, nullptr,
@ -41,6 +48,7 @@ int ffmpeg_init(void)
return -1; return -1;
} }
//find a v4l2 device that uses H264
const AVDeviceInfo* device = find_h264_device(device_info_list); const AVDeviceInfo* device = find_h264_device(device_info_list);
if (device == nullptr) { if (device == nullptr) {
fprintf(stderr, "No H264 device found\n"); fprintf(stderr, "No H264 device found\n");
@ -62,11 +70,19 @@ void ffmpeg_deinit()
//--local functions------------------------------------------------------------- //--local functions-------------------------------------------------------------
/**
* Goes through the provided list of input devices and finds the first one to
* use H264 encoding and returns it. If no device is found, return a nullptr.
*
* This function uses v4l2 directly since ffmpeg's interface seems bugged and I
* couldn't for the life of me get a result out of it
*/
const AVDeviceInfo* find_h264_device(const AVDeviceInfoList* device_list) const AVDeviceInfo* find_h264_device(const AVDeviceInfoList* device_list)
{ {
for (int i=0; i<device_list->nb_devices; ++i) { for (int i=0; i<device_list->nb_devices; ++i) {
const AVDeviceInfo* device = device_list->devices[i]; const AVDeviceInfo* device = device_list->devices[i];
//opens the descriptor using the v4l2 driver
int fd; int fd;
if ((fd = v4l2_open(device->device_name, O_RDWR)) < 0) { if ((fd = v4l2_open(device->device_name, O_RDWR)) < 0) {
fprintf(stderr, "Failed to open: %s, ignoring...\n", fprintf(stderr, "Failed to open: %s, ignoring...\n",
@ -74,6 +90,8 @@ const AVDeviceInfo* find_h264_device(const AVDeviceInfoList* device_list)
continue; continue;
} }
//reads the device formats until H264 is found or there are no more
//formats to read
bool found = false; bool found = false;
struct v4l2_fmtdesc fmt; struct v4l2_fmtdesc fmt;
fmt.index = 0; fmt.index = 0;
@ -87,6 +105,7 @@ const AVDeviceInfo* find_h264_device(const AVDeviceInfoList* device_list)
++fmt.index; ++fmt.index;
} }
//cleanup after ourselves
v4l2_close(fd); v4l2_close(fd);
if (found == true) { if (found == true) {
return device; return device;