Exchange TRY by enumerating image formats in Linux video capture

ISSUE = issue 529
TEST  = unittest on Linux
Review URL: https://webrtc-codereview.appspot.com/1066011

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3463 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
braveyao@webrtc.org 2013-02-04 07:53:53 +00:00
parent 6ed8ebcef9
commit 254d85af54

View File

@ -165,33 +165,48 @@ WebRtc_Word32 VideoCaptureModuleV4L2::StartCapture(
fmts[2] = V4L2_PIX_FMT_MJPEG;
}
// Enumerate image formats.
struct v4l2_fmtdesc fmt;
int fmtsIdx = nFormats;
memset(&fmt, 0, sizeof(fmt));
fmt.index = 0;
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideoCapture, _id,
"Video Capture enumerats supported image formats:");
while (ioctl(_deviceFd, VIDIOC_ENUM_FMT, &fmt) == 0) {
WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideoCapture, _id,
" { pixelformat = %c%c%c%c, description = '%s' }",
fmt.pixelformat & 0xFF, (fmt.pixelformat>>8) & 0xFF,
(fmt.pixelformat>>16) & 0xFF, (fmt.pixelformat>>24) & 0xFF,
fmt.description);
// Match the preferred order.
for (int i = 0; i < nFormats; i++) {
if (fmt.pixelformat == fmts[i] && i < fmtsIdx)
fmtsIdx = i;
}
// Keep enumerating.
fmt.index++;
}
if (fmtsIdx == nFormats)
{
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id,
"no supporting video formats found");
return -1;
} else {
WEBRTC_TRACE(webrtc::kTraceInfo, webrtc::kTraceVideoCapture, _id,
"We prefer format %c%c%c%c",
fmts[fmtsIdx] & 0xFF, (fmts[fmtsIdx]>>8) & 0xFF,
(fmts[fmtsIdx]>>16) & 0xFF, (fmts[fmtsIdx]>>24) & 0xFF);
}
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;
video_fmt.fmt.pix.width = capability.width;
video_fmt.fmt.pix.height = capability.height;
bool formatMatch = false;
for (int i = 0; i < nFormats; i++)
{
video_fmt.fmt.pix.pixelformat = fmts[i];
if (ioctl(_deviceFd, VIDIOC_TRY_FMT, &video_fmt) < 0)
{
continue;
}
else
{
formatMatch = true;
break;
}
}
if (!formatMatch)
{
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id,
"no supporting video formats found");
return -1;
}
video_fmt.fmt.pix.pixelformat = fmts[fmtsIdx];
if (video_fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
_captureVideoType = kVideoYUY2;