1import os
2
3import infra.basetest
4
5
6class TestLibCamera(infra.basetest.BRTest):
7    # A specific configuration is needed for testing libcamera:
8    # a kernel config fragment enables v4l2 vimc driver.
9    # The libevent package is also enabled to have the libcamera "cam"
10    # test application.
11    kernel_fragment = \
12        infra.filepath("tests/package/test_libcamera/linux-vimc.fragment")
13    config = \
14        f"""
15        BR2_aarch64=y
16        BR2_TOOLCHAIN_EXTERNAL=y
17        BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
18        BR2_LINUX_KERNEL=y
19        BR2_LINUX_KERNEL_CUSTOM_VERSION=y
20        BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.1.76"
21        BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
22        BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
23        BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="{kernel_fragment}"
24        BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
25        BR2_PACKAGE_LIBCAMERA=y
26        BR2_PACKAGE_LIBCAMERA_PIPELINE_VIMC=y
27        BR2_PACKAGE_LIBEVENT=y
28        BR2_TARGET_ROOTFS_CPIO=y
29        BR2_TARGET_ROOTFS_CPIO_GZIP=y
30        # BR2_TARGET_ROOTFS_TAR is not set
31        """
32
33    def test_run(self):
34        img = os.path.join(self.builddir, "images", "rootfs.cpio.gz")
35        kern = os.path.join(self.builddir, "images", "Image")
36        self.emulator.boot(arch="aarch64",
37                           kernel=kern,
38                           kernel_cmdline=["console=ttyAMA0"],
39                           options=["-M", "virt", "-cpu", "cortex-a57", "-m", "256M",
40                                    "-initrd", img])
41        self.emulator.login()
42
43        # The Kernel config of this test has only one v4l2 vimc
44        # driver. The camera index is expected to be #1.
45        cam_idx = 1
46
47        # We test libcamera with its simple "cam" application, by
48        # requesting a list of available cameras.
49        cmd = "cam --list"
50        out, ret = self.emulator.run(cmd)
51        self.assertEqual(ret, 0)
52        # libcamera generates info messages. We filter only the
53        # line(s) starting with our camera index.
54        cam_line = [ln for ln in out if ln.startswith(f"{cam_idx}:")]
55        # We should have the vimc camera in this line.
56        self.assertIn("platform/vimc.0", cam_line[0])
57
58        # List the camera information.
59        cmd = f"cam --camera {cam_idx} --info"
60        self.assertRunOk(cmd)
61
62        # List the camera controls and check we have a brightness
63        # control.
64        cmd = f"cam --camera {cam_idx} --list-controls"
65        out, ret = self.emulator.run(cmd)
66        self.assertEqual(ret, 0)
67        self.assertIn("Control: Brightness:", "\n".join(out))
68
69        # List the camera properties and check we have a camera
70        # "Model" property.
71        cmd = f"cam --camera {cam_idx} --list-properties"
72        out, ret = self.emulator.run(cmd)
73        self.assertEqual(ret, 0)
74        self.assertIn("Property: Model = ", "\n".join(out))
75
76        # Capture few frames.
77        cmd = f"cam --camera {cam_idx} --capture=5"
78        cmd += " --stream width=160,height=120,role=video,pixelformat=RGB888"
79        self.assertRunOk(cmd)
80