1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import glob 6 import logging 7 import ntpath 8 import os 9 import re 10 import stat 11 from autotest_lib.client.bin import test, utils 12 from autotest_lib.client.common_lib import error 13 14 15 class camera_V4L2(test.test): 16 version = 1 17 preserve_srcdir = True 18 v4l2_major_dev_num = 81 19 v4l2_minor_dev_num_min = 0 20 v4l2_minor_dev_num_max = 64 21 test_constant_framerate = False 22 23 def setup(self): 24 # TODO(jiesun): make binary here when cross compile issue is resolved. 25 os.chdir(self.srcdir) 26 utils.make('clean') 27 utils.make() 28 29 def run_once(self, test_constant_framerate=False): 30 31 self.test_constant_framerate = test_constant_framerate 32 self.find_video_capture_devices() 33 34 for device in self.v4l2_devices: 35 self.usb_info = self.get_camera_device_usb_info(device) 36 self.run_v4l2_unittests(device) 37 self.run_v4l2_capture_test(device) 38 39 def get_camera_device_usb_info(self, device): 40 device_name = ntpath.basename(device) 41 vid_path = "/sys/class/video4linux/%s/device/../idVendor" % device_name 42 pid_path = "/sys/class/video4linux/%s/device/../idProduct" % device_name 43 with open(vid_path, 'r') as f_vid, open(pid_path, 'r') as f_pid: 44 vid = f_vid.read() 45 pid = f_pid.read() 46 return vid.strip() + ":" + pid.strip() 47 48 def is_v4l2_capture_device(self, device): 49 executable = os.path.join(self.bindir, "media_v4l2_is_capture_device") 50 cmd = "%s %s" % (executable, device) 51 logging.info("Running %s" % cmd) 52 return utils.system(cmd, ignore_status=True) == 0 53 54 def find_video_capture_devices(self): 55 self.v4l2_devices = [] 56 for device in glob.glob("/dev/video*"): 57 statinfo = os.stat(device) 58 if (stat.S_ISCHR(statinfo.st_mode) and 59 os.major(statinfo.st_rdev) == self.v4l2_major_dev_num and 60 os.minor(statinfo.st_rdev) >= 61 self.v4l2_minor_dev_num_min and 62 os.minor(statinfo.st_rdev) < self.v4l2_minor_dev_num_max and 63 self.is_v4l2_capture_device(device)): 64 self.v4l2_devices.append(device) 65 logging.info("Detected devices: %s\n" % self.v4l2_devices) 66 if not self.v4l2_devices: 67 raise error.TestFail("No V4L2 devices found!") 68 69 def run_v4l2_unittests(self, device): 70 options = ["--device=%s" % device, "--usb-info=%s" % self.usb_info] 71 executable = os.path.join(self.bindir, "media_v4l2_unittest") 72 cmd = "%s %s" % (executable, " ".join(options)) 73 logging.info("Running %s" % cmd) 74 stdout = utils.system_output(cmd, retain_output=True) 75 76 def run_v4l2_capture_test(self, device): 77 options = ["--device=%s" % device, "--usb-info=%s" % self.usb_info] 78 if self.test_constant_framerate: 79 options += ["--constant-framerate"] 80 executable = os.path.join(self.bindir, "media_v4l2_test") 81 cmd = "%s %s" % (executable, " ".join(options)) 82 logging.info("Running %s" % cmd) 83 stdout = utils.system_output(cmd, retain_output=True) 84