[FFmpeg-devel] [PATCH 1/2] lavd/v4l2: implement list device callback

Lukasz Marek lukasz.m.luki2 at gmail.com
Sun Dec 21 22:43:38 CET 2014


Signed-off-by: Lukasz Marek <lukasz.m.luki2 at gmail.com>
---
 libavdevice/v4l2.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index 2969980..9d4d7ae 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -1006,6 +1006,63 @@ static int v4l2_read_close(AVFormatContext *ctx)
     return 0;
 }
 
+static int v4l2_get_device_list(AVFormatContext *ctx, AVDeviceInfoList *device_list)
+{
+    struct video_data *s = ctx->priv_data;
+    AVDeviceInfo *device = NULL;
+    struct v4l2_capability cap;
+    int i, ret = 0;
+
+    if (!device_list)
+        return AVERROR(EINVAL);
+
+    for (i = 0; i <= 31; i++) {
+        snprintf(ctx->filename, sizeof(ctx->filename), "/dev/video%d", i);
+
+        if (avio_check(ctx->filename, AVIO_FLAG_READ) != AVIO_FLAG_READ ||
+            (s->fd = device_open(ctx)) < 0)
+            continue;
+
+        if (v4l2_ioctl(s->fd, VIDIOC_QUERYCAP, &cap) < 0) {
+            ret = AVERROR(errno);
+            av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n", av_err2str(ret));
+            goto fail_device;
+        }
+
+        device = av_mallocz(sizeof(AVDeviceInfo));
+        if (!device) {
+            ret = AVERROR(ENOMEM);
+            goto fail_device;
+        }
+        device->device_name = av_strdup(ctx->filename);
+        device->device_description = av_strdup(cap.card);
+        if (!device->device_name || !device->device_description) {
+            ret = AVERROR(ENOMEM);
+            goto fail_device;
+        }
+
+        if ((ret = av_dynarray_add_nofree(&device_list->devices,
+                                          &device_list->nb_devices, device)) < 0)
+            goto fail_device;
+
+        v4l2_close(s->fd);
+        s->fd = -1;
+        continue;
+
+      fail_device:
+        if (device) {
+            av_freep(&device->device_name);
+            av_freep(&device->device_description);
+            av_freep(&device);
+        }
+        if (s->fd >= 0)
+            v4l2_close(s->fd);
+        s->fd = -1;
+        return ret;
+    }
+    return 0;
+}
+
 #define OFFSET(x) offsetof(struct video_data, x)
 #define DEC AV_OPT_FLAG_DECODING_PARAM
 
@@ -1050,6 +1107,7 @@ AVInputFormat ff_v4l2_demuxer = {
     .read_header    = v4l2_read_header,
     .read_packet    = v4l2_read_packet,
     .read_close     = v4l2_read_close,
+    .get_device_list = v4l2_get_device_list,
     .flags          = AVFMT_NOFILE,
     .priv_class     = &v4l2_class,
 };
-- 
1.9.1



More information about the ffmpeg-devel mailing list