diff --git a/src/ffmpeg.c b/src/ffmpeg.c index d55325f..81c424b 100644 --- a/src/ffmpeg.c +++ b/src/ffmpeg.c @@ -1,5 +1,6 @@ /** @file ffmpeg.c - * + * Module interfacing with ffmpeg and v4l2. Cointains mainly boilerplate and + * tool functions to abstract things a bit */ //--includes-------------------------------------------------------------------- @@ -23,10 +24,16 @@ const AVDeviceInfo* find_h264_device(const AVDeviceInfoList* device_list); //--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) { + //initialises avdevice module avdevice_register_all(); + //list all input devices using v4l2 const AVInputFormat* v4l2_format = av_find_input_format("v4l2"); AVDeviceInfoList* device_info_list; int device_nb = avdevice_list_input_sources(v4l2_format, nullptr, nullptr, @@ -41,6 +48,7 @@ int ffmpeg_init(void) return -1; } + //find a v4l2 device that uses H264 const AVDeviceInfo* device = find_h264_device(device_info_list); if (device == nullptr) { fprintf(stderr, "No H264 device found\n"); @@ -62,11 +70,19 @@ void ffmpeg_deinit() //--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) { for (int i=0; inb_devices; ++i) { const AVDeviceInfo* device = device_list->devices[i]; + //opens the descriptor using the v4l2 driver int fd; if ((fd = v4l2_open(device->device_name, O_RDWR)) < 0) { fprintf(stderr, "Failed to open: %s, ignoring...\n", @@ -74,6 +90,8 @@ const AVDeviceInfo* find_h264_device(const AVDeviceInfoList* device_list) continue; } + //reads the device formats until H264 is found or there are no more + //formats to read bool found = false; struct v4l2_fmtdesc fmt; fmt.index = 0; @@ -87,6 +105,7 @@ const AVDeviceInfo* find_h264_device(const AVDeviceInfoList* device_list) ++fmt.index; } + //cleanup after ourselves v4l2_close(fd); if (found == true) { return device;