From d56c3f9adf81cf2c82ab74ae67c86681a412dad8 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 15 Oct 2016 17:30:28 -0700 Subject: [PATCH] USBCamera: Default to MJPEG and lowest resolution. --- src/USBCameraImpl.cpp | 60 +++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/src/USBCameraImpl.cpp b/src/USBCameraImpl.cpp index c8a044d2a3..ee2a886013 100644 --- a/src/USBCameraImpl.cpp +++ b/src/USBCameraImpl.cpp @@ -888,6 +888,9 @@ void USBCameraImpl::DeviceSetMode() { WARNING("USB " << m_path << ": could not set format " << m_mode.pixelFormat << " res " << m_mode.width << "x" << m_mode.height); + } else { + INFO("USB " << m_path << ": set format " << m_mode.pixelFormat << " res " + << m_mode.width << "x" << m_mode.height); } } @@ -903,7 +906,10 @@ void USBCameraImpl::DeviceSetFPS() { std::memset(&parm, 0, sizeof(parm)); parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; parm.parm.capture.timeperframe = FPSToFract(m_mode.fps); - if (DoIoctl(fd, VIDIOC_S_PARM, &parm) != 0) return; + if (DoIoctl(fd, VIDIOC_S_PARM, &parm) != 0) + WARNING("USB " << m_path << ": could not set FPS to " << m_mode.fps); + else + INFO("USB " << m_path << ": set FPS to " << m_mode.fps); } void USBCameraImpl::DeviceCacheMode() { @@ -943,19 +949,6 @@ void USBCameraImpl::DeviceCacheMode() { int width = vfmt.fmt.pix.width; int height = vfmt.fmt.pix.height; - // Update format with user changes. - bool formatChanged = false; - if (m_modeSetPixelFormat && pixelFormat != m_mode.pixelFormat) { - formatChanged = true; - pixelFormat = static_cast(m_mode.pixelFormat); - } - if (m_modeSetResolution && - (width != m_mode.width || height != m_mode.height)) { - formatChanged = true; - width = m_mode.width; - height = m_mode.height; - } - // Get FPS int fps = 0; struct v4l2_streamparm parm; @@ -966,6 +959,45 @@ void USBCameraImpl::DeviceCacheMode() { fps = FractToFPS(parm.parm.capture.timeperframe); } + // Update format with user changes. + bool formatChanged = false; + + if (m_modeSetPixelFormat) { + // User set pixel format + if (pixelFormat != m_mode.pixelFormat) { + formatChanged = true; + pixelFormat = static_cast(m_mode.pixelFormat); + } + } else { + // Default to MJPEG + if (pixelFormat != VideoMode::kMJPEG) { + formatChanged = true; + pixelFormat = VideoMode::kMJPEG; + } + } + + if (m_modeSetResolution) { + // User set resolution + if (width != m_mode.width || height != m_mode.height) { + formatChanged = true; + width = m_mode.width; + height = m_mode.height; + } + } else { + // Default to lowest known resolution (based on number of total pixels) + int numPixels = width * height; + for (const auto& mode : m_videoModes) { + if (mode.pixelFormat != pixelFormat) continue; + int numPixelsHere = mode.width * mode.height; + if (numPixelsHere < numPixels) { + formatChanged = true; + numPixels = numPixelsHere; + width = mode.width; + height = mode.height; + } + } + } + // Update FPS with user changes bool fpsChanged = false; if (m_modeSetFPS && fps != m_mode.fps) {