Check capabilities directly in the function, further simplify the code.
---
 libavdevice/v4l2.c |   71 ++++++++++++++++++++++++---------------------------
 1 files changed, 33 insertions(+), 38 deletions(-)

diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index ee43ed7..c1ba756 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -101,7 +101,7 @@ static struct fmt_map fmt_conversion_table[] = {
     { PIX_FMT_NONE,    CODEC_ID_MJPEG,    V4L2_PIX_FMT_JPEG    },
 };
 
-static int device_open(AVFormatContext *ctx, uint32_t *capabilities)
+static int device_open(AVFormatContext *ctx)
 {
     struct v4l2_capability cap;
     int fd;
@@ -114,41 +114,43 @@ static int device_open(AVFormatContext *ctx, uint32_t 
*capabilities)
 
     fd = open(ctx->filename, flags, 0);
     if (fd < 0) {
+        err = errno;
+
         av_log(ctx, AV_LOG_ERROR, "Cannot open video device %s : %s\n",
-               ctx->filename, strerror(errno));
+               ctx->filename, strerror(err));
 
-        return AVERROR(errno);
+        return AVERROR(err);
     }
 
     res = ioctl(fd, VIDIOC_QUERYCAP, &cap);
-    // ENOIOCTLCMD definition only availble on __KERNEL__
-    if (res < 0 && ((err = errno) == 515)) {
-        av_log(ctx, AV_LOG_ERROR,
-               "QUERYCAP not implemented, probably V4L device but "
-               "not supporting V4L2\n");
-        close(fd);
-
-        return AVERROR(515);
-    }
-
     if (res < 0) {
+        err = errno;
         av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYCAP): %s\n",
-                 strerror(errno));
-        close(fd);
+           strerror(err));
 
-        return AVERROR(err);
+        goto fail;
     }
 
-    if ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0) {
-        av_log(ctx, AV_LOG_ERROR, "Not a video capture device\n");
-        close(fd);
+    av_log(ctx, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n",
+           fd, cap.capabilities);
+
+    if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
+        av_log(ctx, AV_LOG_ERROR, "Not a video capture device.\n");
+        err = ENODEV;
 
-        return AVERROR(ENODEV);
+        goto fail;
     }
 
-    *capabilities = cap.capabilities;
+    if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
+        av_log(ctx, AV_LOG_ERROR, "Streaming I/O not supported.\n");
+        err = AVERROR(EIO);
+    }
 
     return fd;
+
+fail:
+    close(fd);
+    return AVERROR(err);
 }
 
 static int device_init(AVFormatContext *ctx, int *width, int *height,
@@ -600,7 +602,7 @@ static int v4l2_read_header(AVFormatContext *s1, 
AVFormatParameters *ap)
     struct video_data *s = s1->priv_data;
     AVStream *st;
     int res = 0;
-    uint32_t desired_format, capabilities = 0;
+    uint32_t desired_format;
     enum CodecID codec_id;
     enum PixelFormat pix_fmt = PIX_FMT_NONE;
 
@@ -610,6 +612,12 @@ static int v4l2_read_header(AVFormatContext *s1, 
AVFormatParameters *ap)
         goto out;
     }
 
+    s->fd = device_open(s1);
+    if (s->fd < 0) {
+        res = s->fd;
+        goto out;
+    }
+
     avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
 
     if (s->video_size &&
@@ -626,13 +634,6 @@ static int v4l2_read_header(AVFormatContext *s1, 
AVFormatParameters *ap)
         goto out;
     }
 
-    s->fd = device_open(s1, &capabilities);
-    if (s->fd < 0) {
-        res = AVERROR(EIO);
-        goto out;
-    }
-    av_log(s1, AV_LOG_VERBOSE, "[%d]Capabilities: %x\n", s->fd, capabilities);
-
     if (!s->width && !s->height) {
         struct v4l2_format fmt;
 
@@ -675,18 +676,12 @@ static int v4l2_read_header(AVFormatContext *s1, 
AVFormatParameters *ap)
     s->frame_size =
         avpicture_get_size(st->codec->pix_fmt, s->width, s->height);
 
-    if (capabilities & V4L2_CAP_STREAMING) {
-        if (!(res = mmap_init(s1)))
-            res = mmap_start(s1);
-    } else {
-        res = -1;
-    }
-    if (res < 0) {
+    if ((res = mmap_init(s1)) ||
+        (res = mmap_start(s1)) < 0) {
         close(s->fd);
-
-        res = AVERROR(EIO);
         goto out;
     }
+
     s->top_field_first = first_field(s->fd);
 
     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
-- 
1.7.8.rc1

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to