Bug 1302059 - use VIDIOC_ENUM_FMT & VIDIOC_ENUM_FRAMESIZES to query supported format & resolution; r=jesup draft
authorMunro Chiang <mchiang@mozilla.com>
Wed, 21 Sep 2016 17:25:00 +0800
changeset 415904 c3b6b71b167a897cdd14e54f24b3c495d5151dd5
parent 411433 77940cbf0c2a9f52c209fbbde5b2e7d4c74a1501
child 531737 606bbe02576415cd51904661b4a04a480e06ca8c
push id30011
push usermchiang@mozilla.com
push dateWed, 21 Sep 2016 09:25:45 +0000
reviewersjesup
bugs1302059
milestone51.0a1
Bug 1302059 - use VIDIOC_ENUM_FMT & VIDIOC_ENUM_FRAMESIZES to query supported format & resolution; r=jesup MozReview-Commit-ID: AFEeb9yrIzb
media/webrtc/trunk/webrtc/modules/video_capture/linux/device_info_linux.cc
--- a/media/webrtc/trunk/webrtc/modules/video_capture/linux/device_info_linux.cc
+++ b/media/webrtc/trunk/webrtc/modules/video_capture/linux/device_info_linux.cc
@@ -262,87 +262,62 @@ bool DeviceInfoLinux::IsDeviceNameMatche
 {
     if (strncmp(deviceUniqueIdUTF8, name, strlen(name)) == 0)
             return true;
     return false;
 }
 
 int32_t DeviceInfoLinux::FillCapabilities(int fd)
 {
-
-    // set image format
-    struct v4l2_format video_fmt;
-    memset(&video_fmt, 0, sizeof(struct v4l2_format));
-
-    video_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-    video_fmt.fmt.pix.sizeimage = 0;
-
-    int totalFmts = 3;
-    unsigned int videoFormats[] = {
-        V4L2_PIX_FMT_MJPEG,
-        V4L2_PIX_FMT_YUV420,
-        V4L2_PIX_FMT_YUYV };
+    struct v4l2_fmtdesc fmt;
+    struct v4l2_frmsizeenum frmsize;
+    struct v4l2_frmivalenum frmival;
 
-    int sizes = 13;
-    unsigned int size[][2] = { { 128, 96 }, { 160, 120 }, { 176, 144 },
-                               { 320, 240 }, { 352, 288 }, { 640, 480 },
-                               { 704, 576 }, { 800, 600 }, { 960, 720 },
-                               { 1280, 720 }, { 1024, 768 }, { 1440, 1080 },
-                               { 1920, 1080 } };
-
-    int index = 0;
-    for (int fmts = 0; fmts < totalFmts; fmts++)
-    {
-        for (int i = 0; i < sizes; i++)
-        {
-            video_fmt.fmt.pix.pixelformat = videoFormats[fmts];
-            video_fmt.fmt.pix.width = size[i][0];
-            video_fmt.fmt.pix.height = size[i][1];
+    fmt.index = 0;
+    fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+    while (ioctl(fd, VIDIOC_ENUM_FMT, &fmt) >= 0) {
+        frmsize.pixel_format = fmt.pixelformat;
+        frmsize.index = 0;
+        while (ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frmsize) >= 0) {
+            if (frmsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
+                frmival.index = 0;
+                frmival.pixel_format = fmt.pixelformat;
+                frmival.width = frmsize.discrete.width;
+                frmival.height = frmsize.discrete.height;
+                if (ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &frmival) >= 0) {
+                    if (fmt.pixelformat == V4L2_PIX_FMT_YUYV ||
+                        fmt.pixelformat == V4L2_PIX_FMT_YUV420 ||
+                        fmt.pixelformat == V4L2_PIX_FMT_MJPEG) {
 
-            if (ioctl(fd, VIDIOC_TRY_FMT, &video_fmt) >= 0)
-            {
-                if ((video_fmt.fmt.pix.width == size[i][0])
-                    && (video_fmt.fmt.pix.height == size[i][1]))
-                {
-                    VideoCaptureCapability cap;
-                    cap.width = video_fmt.fmt.pix.width;
-                    cap.height = video_fmt.fmt.pix.height;
-                    cap.expectedCaptureDelay = 120;
-                    if (videoFormats[fmts] == V4L2_PIX_FMT_YUYV)
-                    {
-                        cap.rawType = kVideoYUY2;
-                    }
-                    else if (videoFormats[fmts] == V4L2_PIX_FMT_YUV420)
-                    {
-                        cap.rawType = kVideoI420;
+                        VideoCaptureCapability cap;
+                        cap.width = frmsize.discrete.width;
+                        cap.height = frmsize.discrete.height;
+                        cap.expectedCaptureDelay = 120;
+
+                        if (fmt.pixelformat == V4L2_PIX_FMT_YUYV)
+                        {
+                            cap.rawType = kVideoYUY2;
+                        }
+                        else if (fmt.pixelformat == V4L2_PIX_FMT_YUV420)
+                        {
+                            cap.rawType = kVideoI420;
+                        }
+                        else if (fmt.pixelformat == V4L2_PIX_FMT_MJPEG)
+                        {
+                            cap.rawType = kVideoMJPEG;
+                        }
+
+                        cap.maxFPS = frmival.discrete.denominator / frmival.discrete.numerator;
+                        _captureCapabilities.push_back(cap);
                     }
-                    else if (videoFormats[fmts] == V4L2_PIX_FMT_MJPEG)
-                    {
-                        cap.rawType = kVideoMJPEG;
-                    }
-
-                    // get fps of current camera mode
-                    // V4l2 does not have a stable method of knowing so we just guess.
-                    if(cap.width >= 800 && cap.rawType != kVideoMJPEG)
-                    {
-                        cap.maxFPS = 15;
-                    }
-                    else
-                    {
-                        cap.maxFPS = 30;
-                    }
-
-                    _captureCapabilities.push_back(cap);
-                    index++;
-                    WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideoCapture, _id,
-                               "Camera capability, width:%d height:%d type:%d fps:%d",
-                               cap.width, cap.height, cap.rawType, cap.maxFPS);
                 }
             }
+            frmsize.index++;
         }
+        fmt.index++;
     }
 
     WEBRTC_TRACE(webrtc::kTraceInfo,
                  webrtc::kTraceVideoCapture,
                  _id,
                  "CreateCapabilityMap %u",
                  static_cast<unsigned int>(_captureCapabilities.size()));
     return _captureCapabilities.size();