camera_V4L2: Get camera list by --list_usbcam

We cannot remove the Autotest version now because running a single Tast
test from moblab is not supported yet.

To minimized the duplicated logic in Tast and Autotest, CL:1995092 adds
the USB camera listing logic into the test binary itself. This CL
utilizes the --list_usbcam option in Autotest.

BUG=b:147196144
TEST=test_that --fast <dut> camera_V4L2

Cq-Depend: chromium:1995092
Change-Id: I9062db1e1f7dc390cfee6ce69535192ab6418070
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/1999824
Tested-by: Shik Chen <shik@chromium.org>
Reviewed-by: Heng-ruey Hsu <henryhsu@chromium.org>
Commit-Queue: Shik Chen <shik@chromium.org>
diff --git a/client/site_tests/camera_V4L2/camera_V4L2.py b/client/site_tests/camera_V4L2/camera_V4L2.py
index fc89be4..632570a 100644
--- a/client/site_tests/camera_V4L2/camera_V4L2.py
+++ b/client/site_tests/camera_V4L2/camera_V4L2.py
@@ -2,11 +2,8 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import glob
 import logging
-import ntpath
 import os
-import stat
 from autotest_lib.client.bin import test, utils
 from autotest_lib.client.common_lib import error
 from autotest_lib.client.cros.video import device_capability
@@ -15,9 +12,6 @@
 class camera_V4L2(test.test):
     version = 1
     preserve_srcdir = True
-    v4l2_major_dev_num = 81
-    v4l2_minor_dev_num_min = 0
-    v4l2_minor_dev_num_max = 64
 
     def run_once(self, capability=None, test_list=None):
         if capability is not None:
@@ -31,13 +25,9 @@
             test_list = "halv3" if self.should_test_halv3() else "default"
         self.test_list = test_list
 
-        self.dut_board = utils.get_current_board()
         self.find_video_capture_devices()
 
         for device in self.v4l2_devices:
-            self.usb_info = self.get_camera_device_usb_info(device)
-            if not self.usb_info:
-                continue
             self.run_v4l2_test(device)
 
     def should_test_halv3(self):
@@ -45,36 +35,10 @@
         has_v1 = os.path.exists('/usr/bin/arc_camera_service')
         return has_v3 and not has_v1
 
-    def get_camera_device_usb_info(self, device):
-        device_name = ntpath.basename(device)
-        vid_path = "/sys/class/video4linux/%s/device/../idVendor" % device_name
-        pid_path = "/sys/class/video4linux/%s/device/../idProduct" % device_name
-        if not os.path.isfile(vid_path) or not os.path.isfile(pid_path):
-            logging.info("Device %s is not a USB camera", device)
-            return None
-
-        with open(vid_path, 'r') as f_vid, open(pid_path, 'r') as f_pid:
-            vid = f_vid.read()
-            pid = f_pid.read()
-        return vid.strip() + ":" + pid.strip()
-
-    def is_v4l2_capture_device(self, device):
-        cmd = ["media_v4l2_is_capture_device", device]
-        logging.info("Running %s", cmd)
-        return utils.system(cmd, ignore_status=True) == 0
-
     def find_video_capture_devices(self):
-        self.v4l2_devices = []
-        for device in glob.glob("/dev/video*"):
-            statinfo = os.stat(device)
-            if (stat.S_ISCHR(statinfo.st_mode) and
-                    os.major(statinfo.st_rdev) == self.v4l2_major_dev_num and
-                    os.minor(statinfo.st_rdev) >=
-                    self.v4l2_minor_dev_num_min and
-                    os.minor(statinfo.st_rdev) < self.v4l2_minor_dev_num_max and
-                    self.is_v4l2_capture_device(device)):
-                self.v4l2_devices.append(device)
-        logging.info("Detected devices: %s\n", self.v4l2_devices)
+        cmd = ["media_v4l2_test", "--list_usbcam"]
+        stdout = utils.system_output(cmd, retain_output=True)
+        self.v4l2_devices = stdout.splitlines()
         if not self.v4l2_devices:
             raise error.TestFail("No V4L2 devices found!")
 
@@ -82,7 +46,6 @@
         cmd = [
                 "media_v4l2_test",
                 "--device_path=%s" % device,
-                "--usb_info=%s" % self.usb_info
         ]
         if self.test_list:
             cmd.append("--test_list=%s" % self.test_list)