/usr/lib/2013.com.canonical.certification:checkbox/bin/camera_test is in plainbox-provider-checkbox 0.4-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 | #!/usr/bin/env python3
#
# This file is part of Checkbox.
#
# Copyright 2008-2012 Canonical Ltd.
#
# The v4l2 ioctl code comes from the Python bindings for the v4l2
# userspace api (http://pypi.python.org/pypi/v4l2):
# Copyright (C) 1999-2009 the contributors
#
# The JPEG metadata parser is a part of bfg-pages:
# http://code.google.com/p/bfg-pages/source/browse/trunk/pages/getimageinfo.py
# Copyright (C) Tim Hoffman
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
#
import os
import re
import sys
import time
import errno
import fcntl
import ctypes
import struct
import imghdr
from tempfile import NamedTemporaryFile
from subprocess import check_call, CalledProcessError, STDOUT
import argparse
from glob import glob
from gi.repository import GObject
_IOC_NRBITS = 8
_IOC_TYPEBITS = 8
_IOC_SIZEBITS = 14
_IOC_NRSHIFT = 0
_IOC_TYPESHIFT = _IOC_NRSHIFT + _IOC_NRBITS
_IOC_SIZESHIFT = _IOC_TYPESHIFT + _IOC_TYPEBITS
_IOC_DIRSHIFT = _IOC_SIZESHIFT + _IOC_SIZEBITS
_IOC_WRITE = 1
_IOC_READ = 2
def _IOC(dir_, type_, nr, size):
return (
ctypes.c_int32(dir_ << _IOC_DIRSHIFT).value |
ctypes.c_int32(ord(type_) << _IOC_TYPESHIFT).value |
ctypes.c_int32(nr << _IOC_NRSHIFT).value |
ctypes.c_int32(size << _IOC_SIZESHIFT).value)
def _IOC_TYPECHECK(t):
return ctypes.sizeof(t)
def _IOR(type_, nr, size):
return _IOC(_IOC_READ, type_, nr, ctypes.sizeof(size))
def _IOWR(type_, nr, size):
return _IOC(_IOC_READ | _IOC_WRITE, type_, nr, _IOC_TYPECHECK(size))
class v4l2_capability(ctypes.Structure):
"""
Driver capabilities
"""
_fields_ = [
('driver', ctypes.c_char * 16),
('card', ctypes.c_char * 32),
('bus_info', ctypes.c_char * 32),
('version', ctypes.c_uint32),
('capabilities', ctypes.c_uint32),
('reserved', ctypes.c_uint32 * 4),
]
# Values for 'capabilities' field
V4L2_CAP_VIDEO_CAPTURE = 0x00000001
V4L2_CAP_VIDEO_OVERLAY = 0x00000004
V4L2_CAP_READWRITE = 0x01000000
V4L2_CAP_STREAMING = 0x04000000
v4l2_frmsizetypes = ctypes.c_uint
(
V4L2_FRMSIZE_TYPE_DISCRETE,
V4L2_FRMSIZE_TYPE_CONTINUOUS,
V4L2_FRMSIZE_TYPE_STEPWISE,
) = range(1, 4)
class v4l2_frmsize_discrete(ctypes.Structure):
_fields_ = [
('width', ctypes.c_uint32),
('height', ctypes.c_uint32),
]
class v4l2_frmsize_stepwise(ctypes.Structure):
_fields_ = [
('min_width', ctypes.c_uint32),
('min_height', ctypes.c_uint32),
('step_width', ctypes.c_uint32),
('min_height', ctypes.c_uint32),
('max_height', ctypes.c_uint32),
('step_height', ctypes.c_uint32),
]
class v4l2_frmsizeenum(ctypes.Structure):
class _u(ctypes.Union):
_fields_ = [
('discrete', v4l2_frmsize_discrete),
('stepwise', v4l2_frmsize_stepwise),
]
_fields_ = [
('index', ctypes.c_uint32),
('pixel_format', ctypes.c_uint32),
('type', ctypes.c_uint32),
('_u', _u),
('reserved', ctypes.c_uint32 * 2)
]
_anonymous_ = ('_u',)
class v4l2_fmtdesc(ctypes.Structure):
_fields_ = [
('index', ctypes.c_uint32),
('type', ctypes.c_int),
('flags', ctypes.c_uint32),
('description', ctypes.c_char * 32),
('pixelformat', ctypes.c_uint32),
('reserved', ctypes.c_uint32 * 4),
]
V4L2_FMT_FLAG_COMPRESSED = 0x0001
V4L2_FMT_FLAG_EMULATED = 0x0002
# ioctl code for video devices
VIDIOC_QUERYCAP = _IOR('V', 0, v4l2_capability)
VIDIOC_ENUM_FRAMESIZES = _IOWR('V', 74, v4l2_frmsizeenum)
VIDIOC_ENUM_FMT = _IOWR('V', 2, v4l2_fmtdesc)
class CameraTest:
"""
A simple class that displays a test image via GStreamer.
"""
def __init__(self, args, gst_plugin=None, gst_video_type=None):
self.args = args
self._mainloop = GObject.MainLoop()
self._width = 640
self._height = 480
self._gst_plugin = gst_plugin
self._gst_video_type = gst_video_type
def detect(self):
"""
Display information regarding webcam hardware
"""
cap_status = dev_status = 1
for i in range(10):
cp = v4l2_capability()
device = '/dev/video%d' % i
try:
with open(device, 'r') as vd:
fcntl.ioctl(vd, VIDIOC_QUERYCAP, cp)
except IOError:
continue
dev_status = 0
print("%s: OK" % device)
print(" name : %s" % cp.card.decode('UTF-8'))
print(" driver : %s" % cp.driver.decode('UTF-8'))
print(" version: %s.%s.%s"
% (cp.version >> 16,
(cp.version >> 8) & 0xff,
cp.version & 0xff))
print(" flags : 0x%x [" % cp.capabilities,
' CAPTURE' if cp.capabilities & V4L2_CAP_VIDEO_CAPTURE
else '',
' OVERLAY' if cp.capabilities & V4L2_CAP_VIDEO_OVERLAY
else '',
' READWRITE' if cp.capabilities & V4L2_CAP_READWRITE
else '',
' STREAMING' if cp.capabilities & V4L2_CAP_STREAMING
else '',
' ]', sep="")
resolutions = self._get_supported_resolutions(device)
print(' ',
self._supported_resolutions_to_string(resolutions).replace(
"\n", " "),
sep="")
if cp.capabilities & V4L2_CAP_VIDEO_CAPTURE:
cap_status = 0
return dev_status | cap_status
def led(self):
"""
Activate camera (switch on led), but don't display any output
"""
pipespec = ("v4l2src device=%(device)s "
"! %(type)s "
"! %(plugin)s "
"! testsink"
% {'device': self.args.device,
'type': self._gst_video_type,
'plugin': self._gst_plugin})
self._pipeline = Gst.parse_launch(pipespec)
self._pipeline.set_state(Gst.State.PLAYING)
time.sleep(10)
self._pipeline.set_state(Gst.State.NULL)
def display(self):
"""
Displays the preview window
"""
pipespec = ("v4l2src device=%(device)s "
"! %(type)s,width=%(width)d,height=%(height)d "
"! %(plugin)s "
"! autovideosink"
% {'device': self.args.device,
'type': self._gst_video_type,
'width': self._width,
'height': self._height,
'plugin': self._gst_plugin})
self._pipeline = Gst.parse_launch(pipespec)
self._pipeline.set_state(Gst.State.PLAYING)
time.sleep(10)
self._pipeline.set_state(Gst.State.NULL)
def still(self):
"""
Captures an image to a file
"""
if self.args.filename:
self._still_helper(self.args.filename, self._width, self._height,
self.args.quiet)
else:
with NamedTemporaryFile(prefix='camera_test_', suffix='.jpg') as f:
self._still_helper(f.name, self._width, self._height,
self.args.quiet)
def _still_helper(self, filename, width, height, quiet, pixelformat=None):
"""
Captures an image to a given filename. width and height specify the
image size and quiet controls whether the image is displayed to the
user (quiet = True means do not display image).
"""
command = ["fswebcam", "-D 1", "-S 50", "--no-banner",
"-d", self.args.device,
"-r", "%dx%d"
% (width, height), filename]
use_gstreamer = False
if pixelformat:
command.extend(["-p", pixelformat])
try:
check_call(command, stdout=open(os.devnull, 'w'), stderr=STDOUT)
except (CalledProcessError, OSError):
use_gstreamer = True
if use_gstreamer:
pipespec = ("v4l2src device=%(device)s "
"! %(type)s,width=%(width)d,height=%(height)d "
"! %(plugin)s "
"! jpegenc "
"! filesink location=%(filename)s"
% {'device': self.args.device,
'type': self._gst_video_type,
'width': width,
'height': height,
'plugin': self._gst_plugin,
'filename': filename})
self._pipeline = Gst.parse_launch(pipespec)
self._pipeline.set_state(Gst.State.PLAYING)
time.sleep(3)
self._pipeline.set_state(Gst.State.NULL)
if not quiet:
try:
check_call(["timeout", "-k", "11", "10", "eog", filename])
except CalledProcessError:
pass
def _supported_resolutions_to_string(self, supported_resolutions):
"""
Return a printable string representing a list of supported resolutions
"""
ret = ""
for resolution in supported_resolutions:
ret += "Format: %s (%s)\n" % (resolution['pixelformat'],
resolution['description'])
ret += "Resolutions: "
for res in resolution['resolutions']:
ret += "%sx%s," % (res[0], res[1])
# truncate the extra comma with :-1
ret = ret[:-1] + "\n"
return ret
def resolutions(self):
"""
After querying the webcam for supported formats and resolutions,
take multiple images using the first format returned by the driver,
and see if they are valid
"""
resolutions = self._get_supported_resolutions(self.args.device)
# print supported formats and resolutions for the logs
print(self._supported_resolutions_to_string(resolutions))
# pick the first format, which seems to be what the driver wants for a
# default. This also matches the logic that fswebcam uses to select
# a default format.
resolution = resolutions[0]
if resolution:
print("Taking multiple images using the %s format"
% resolution['pixelformat'])
for res in resolution['resolutions']:
w = res[0]
h = res[1]
f = NamedTemporaryFile(prefix='camera_test_%s%sx%s' %
(resolution['pixelformat'], w, h),
suffix='.jpg', delete=False)
print("Taking a picture at %sx%s" % (w, h))
self._still_helper(f.name, w, h, True,
pixelformat=resolution['pixelformat'])
if self._validate_image(f.name, w, h):
print("Validated image %s" % f.name)
os.remove(f.name)
else:
print("Failed to validate image %s" % f.name,
file=sys.stderr)
os.remove(f.name)
return 1
return 0
def _get_pixel_formats(self, device, maxformats=5):
"""
Query the camera to see what pixel formats it supports. A list of
dicts is returned consisting of format and description. The caller
should check whether this camera supports VIDEO_CAPTURE before
calling this function.
"""
supported_formats = []
fmt = v4l2_fmtdesc()
fmt.index = 0
fmt.type = V4L2_CAP_VIDEO_CAPTURE
try:
while fmt.index < maxformats:
with open(device, 'r') as vd:
if fcntl.ioctl(vd, VIDIOC_ENUM_FMT, fmt) == 0:
pixelformat = {}
# save the int type for re-use later
pixelformat['pixelformat_int'] = fmt.pixelformat
pixelformat['pixelformat'] = "%s%s%s%s" % \
(chr(fmt.pixelformat & 0xFF),
chr((fmt.pixelformat >> 8) & 0xFF),
chr((fmt.pixelformat >> 16) & 0xFF),
chr((fmt.pixelformat >> 24) & 0xFF))
pixelformat['description'] = fmt.description.decode()
supported_formats.append(pixelformat)
fmt.index = fmt.index + 1
except IOError as e:
# EINVAL is the ioctl's way of telling us that there are no
# more formats, so we ignore it
if e.errno != errno.EINVAL:
print("Unable to determine Pixel Formats, this may be a "
"driver issue.")
return supported_formats
return supported_formats
def _get_supported_resolutions(self, device):
"""
Query the camera for supported resolutions for a given pixel_format.
Data is returned in a list of dictionaries with supported pixel
formats as the following example shows:
resolution['pixelformat'] = "YUYV"
resolution['description'] = "(YUV 4:2:2 (YUYV))"
resolution['resolutions'] = [[width, height], [640, 480], [1280, 720] ]
If we are unable to gather any information from the driver, then we
return YUYV and 640x480 which seems to be a safe default.
Per the v4l2 spec the ioctl used here is experimental
but seems to be well supported.
"""
supported_formats = self._get_pixel_formats(device)
if not supported_formats:
resolution = {}
resolution['description'] = "YUYV"
resolution['pixelformat'] = "YUYV"
resolution['resolutions'] = [[640, 480]]
supported_formats.append(resolution)
return supported_formats
for supported_format in supported_formats:
resolutions = []
framesize = v4l2_frmsizeenum()
framesize.index = 0
framesize.pixel_format = supported_format['pixelformat_int']
with open(device, 'r') as vd:
try:
while fcntl.ioctl(vd,
VIDIOC_ENUM_FRAMESIZES,
framesize) == 0:
if framesize.type == V4L2_FRMSIZE_TYPE_DISCRETE:
resolutions.append([framesize.discrete.width,
framesize.discrete.height])
# for continuous and stepwise, let's just use min and
# max they use the same structure and only return
# one result
elif framesize.type == V4L2_FRMSIZE_TYPE_CONTINUOUS or\
framesize.type == V4L2_FRMSIZE_TYPE_STEPWISE:
resolutions.append([framesize.stepwise.min_width,
framesize.stepwise.min_height]
)
resolutions.append([framesize.stepwise.max_width,
framesize.stepwise.max_height]
)
break
framesize.index = framesize.index + 1
except IOError as e:
# EINVAL is the ioctl's way of telling us that there are no
# more formats, so we ignore it
if e.errno != errno.EINVAL:
print("Unable to determine supported framesizes "
"(resolutions), this may be a driver issue.")
return supported_formats
supported_format['resolutions'] = resolutions
return supported_formats
def _validate_image(self, filename, width, height):
"""
Given a filename, ensure that the image is the width and height
specified and is a valid image file.
"""
if imghdr.what(filename) != 'jpeg':
return False
outw = outh = 0
with open(filename, mode='rb') as jpeg:
jpeg.seek(2)
b = jpeg.read(1)
try:
while (b and ord(b) != 0xDA):
while (ord(b) != 0xFF):
b = jpeg.read(1)
while (ord(b) == 0xFF):
b = jpeg.read(1)
if (ord(b) >= 0xC0 and ord(b) <= 0xC3):
jpeg.seek(3, 1)
h, w = struct.unpack(">HH", jpeg.read(4))
break
b = jpeg.read(1)
outw, outh = int(w), int(h)
except (struct.error, ValueError):
pass
if outw != width:
print("Image width does not match, was %s should be %s" %
(outw, width), file=sys.stderr)
return False
if outh != height:
print("Image width does not match, was %s should be %s" %
(outh, height), file=sys.stderr)
return False
return True
return True
def parse_arguments(argv):
"""
Parse command line arguments
"""
parser = argparse.ArgumentParser(description="Run a camera-related test")
subparsers = parser.add_subparsers(dest='test',
title='test',
description='Available camera tests')
def add_device_parameter(parser):
group = parser.add_mutually_exclusive_group()
group.add_argument("-d", "--device", default="/dev/video0",
help="Device for the webcam to use")
group.add_argument("--highest-device", action="store_true",
help=("Use the /dev/videoN "
"where N is the highest value available"))
group.add_argument("--lowest-device", action="store_true",
help=("Use the /dev/videoN "
"where N is the lowest value available"))
subparsers.add_parser('detect')
led_parser = subparsers.add_parser('led')
add_device_parameter(led_parser)
display_parser = subparsers.add_parser('display')
add_device_parameter(display_parser)
still_parser = subparsers.add_parser('still')
add_device_parameter(still_parser)
still_parser.add_argument("-f", "--filename",
help="Filename to store the picture")
still_parser.add_argument("-q", "--quiet", action="store_true",
help=("Don't display picture, "
"just write the picture to a file"))
resolutions_parser = subparsers.add_parser('resolutions')
add_device_parameter(resolutions_parser)
args = parser.parse_args(argv)
def get_video_devices():
devices = sorted(glob('/dev/video[0-9]'),
key=lambda d: re.search(r'\d', d).group(0))
assert len(devices) > 0, "No video devices found"
return devices
if hasattr(args, 'highest_device') and args.highest_device:
args.device = get_video_devices()[-1]
elif hasattr(args, 'lowest_device') and args.lowest_device:
args.device = get_video_devices()[0]
return args
if __name__ == "__main__":
args = parse_arguments(sys.argv[1:])
if not args.test:
args.test = 'detect'
# Import Gst only for the test cases that will need it
if args.test in ['display', 'still', 'led', 'resolutions']:
from gi.repository import Gst
if Gst.version()[0] > 0:
gst_plugin = 'videoconvert'
gst_video_type = 'video/x-raw'
else:
gst_plugin = 'ffmpegcolorspace'
gst_video_type = 'video/x-raw-yuv'
Gst.init(None)
camera = CameraTest(args, gst_plugin, gst_video_type)
else:
camera = CameraTest(args)
sys.exit(getattr(camera, args.test)())
|