Make gstreamer work and fix boot reliability issues

The NVIDIA gstreamer modules we need work best in Yocto, not Debian.
So, install the yocto versions of gstreamer and the dependencies needed
to make it work.  This means we will have debian depending on Yocto
depending on Debian, so the install process gets more complicated.

By doing this, I uncovered a bug...  Probing the GPU at startup was
racing with probing the audio module.  This *shouldn't* crash things,
but we don't need to be the one to fix it.  Wait until super late in the
boot process to setup the GPU.  Turns out, the service which sets up CPU
frequency scaling runs super late, and is actually something we should
be after anyways.

[    9.409096] tegra210-adsp tegra210-adsp: Tegra210 ADSP driver successfully registered
[    9.491476] nvgpu: 17000000.ga10b nvgpu_cic_mon_report_err_safety_services:92   [ERR]  Error reporting is not supported in this platform
[    9.491488] nvgpu: 17000000.ga10b           acr_report_error_to_sdl:67   [ERR]  LSPMU pkc signature verification failed
[    9.491493] nvgpu: 17000000.ga10b     nvgpu_acr_wait_for_completion:143  [ERR]  flcn-1: HS ucode boot failed, err b
[    9.491498] nvgpu: 17000000.ga10b     nvgpu_acr_wait_for_completion:145  [ERR]  flcn-1: Mailbox-1 : 0xd
[    9.491503] nvgpu: 17000000.ga10b nvgpu_cic_mon_report_err_safety_services:55   [ERR]  Error reporting is not supported in this platform
[    9.491507] nvgpu: 17000000.ga10b nvgpu_pmu_report_bar0_pri_err_status:41   [ERR]  PMU falcon bar0 timeout. status(0x0), error_type(0xc)
[    9.491511] nvgpu: 17000000.ga10b            ga10b_bootstrap_hs_acr:72   [ERR]  ACR bootstrap failed
[    9.491515] nvgpu: 17000000.ga10b        nvgpu_acr_bootstrap_hs_acr:85   [ERR]  ACR bootstrap failed
[    9.491518] nvgpu: 17000000.ga10b       nvgpu_acr_construct_execute:108  [ERR]  Bootstrap HS ACR failed
[    9.491520] nvgpu: 17000000.ga10b            nvgpu_finalize_poweron:1010 [ERR]  Failed initialization for: g->ops.acr.acr_construct_execute
[    9.513849] mttcan c310000.mttcan cana: renamed from can1
[    9.518987] imx296 10-001a: found IMX296LQ (19.8C)
[    9.519526] tegra-camrtc-capture-vi tegra-capture-vi: subdev imx296 10-001a bound
[    9.601412] peak_pciefd 0001:01:00.0 canb: renamed from can0
[    9.692881] peak_pciefd 0001:01:00.0 canc: renamed from can2
[    9.880190] nvgpu: 17000000.ga10b                 gk20a_power_write:127  [ERR]  power_node_write failed at busy
[    9.896166] eth0: 0xffff800010bcd000, 48:b0:2d:e9:bf:c9, IRQ 302
[    9.948213] mttcan c310000.mttcan cana: Bitrate set
[    9.957417] tegra210-ahub 2900800.ahub: connected() callback only supported for supply widgets (IQC2-1 XBAR-RX -> ADMAIF1 Mux)
[    9.957423] tegra210-ahub 2900800.ahub: ASoC: no dapm match for IQC2-1 XBAR-RX --> IQC2-1 --> ADMAIF1 Mux
[    9.957425] tegra210-ahub 2900800.ahub: ASoC: Failed to add route IQC2-1 XBAR-RX -> IQC2-1 -> ADMAIF1 Mux
[    9.957429] tegra210-ahub 2900800.ahub: connected() callback only supported for supply widgets (IQC2-2 XBAR-RX -> ADMAIF1 Mux)
[    9.957431] tegra210-ahub 2900800.ahub: ASoC: no dapm match for IQC2-2 XBAR-RX --> IQC2-2 --> ADMAIF1 Mux
[    9.957432] tegra210-ahub 2900800.ahub: ASoC: Failed to add route IQC2-2 XBAR-RX -> IQC2-2 -> ADMAIF1 Mux
[    9.957436] tegra210-ahub 2900800.ahub: connected() callback only supported for supply widgets (DMIC1 XBAR-RX -> ADMAIF1 Mux)

Change-Id: Ia6e06e61289b36f4cf2eb458ce9b2fe82615e203
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/frc971/orin/build_rootfs.py b/frc971/orin/build_rootfs.py
index c378226..59a3d7f 100755
--- a/frc971/orin/build_rootfs.py
+++ b/frc971/orin/build_rootfs.py
@@ -972,26 +972,13 @@
 
         target([
             "apt-get", "-y", "install", "bpfcc-tools", "sudo",
-            "openssh-server", "python3", "bash-completion", "git", "v4l-utils",
+            "openssh-server", "python3", "bash-completion", "git",
             "cpufrequtils", "pmount", "rsync", "vim-nox", "chrony",
-            "libopencv-calib3d406", "libopencv-contrib406",
-            "libopencv-core406", "libopencv-features2d406",
-            "libopencv-flann406", "libopencv-highgui406",
-            "libopencv-imgcodecs406", "libopencv-imgproc406",
-            "libopencv-ml406", "libopencv-objdetect406", "libopencv-photo406",
-            "libopencv-shape406", "libopencv-stitching406",
-            "libopencv-superres406", "libopencv-video406",
-            "libopencv-videoio406", "libopencv-videostab406",
-            "libopencv-viz406", "libopencv-dev", "libnice10", "pmount",
-            "libnice-dev", "feh", "libgstreamer1.0-0",
-            "libgstreamer-plugins-base1.0-0", "libgstreamer-plugins-bad1.0-0",
-            "gstreamer1.0-plugins-base", "gstreamer1.0-plugins-good",
-            "gstreamer1.0-plugins-bad", "gstreamer1.0-plugins-ugly",
-            "gstreamer1.0-nice", "usbutils", "locales", "trace-cmd", "clinfo",
-            "jq", "strace", "sysstat", "lm-sensors", "can-utils", "xfsprogs",
-            "gstreamer1.0-tools", "bridge-utils", "net-tools", "apt-file",
-            "parted", "xxd", "libv4l-dev", "file", "pkexec", "libxkbfile1",
-            "gdb"
+            "libnice10", "pmount", "libnice-dev", "feh", "usbutils", "locales",
+            "trace-cmd", "clinfo", "jq", "strace", "sysstat", "lm-sensors",
+            "can-utils", "xfsprogs", "bridge-utils", "net-tools", "apt-file",
+            "parted", "xxd", "file", "pkexec", "libxkbfile1", "gdb", "autossh",
+            "smartmontools", "nvme-cli", "libgtk-3.0"
         ])
         target(["apt-get", "clean"])
 
@@ -1001,11 +988,51 @@
         target(["usermod", "-a", "-G", "dialout", "pi"])
 
         virtual_packages = [
-            'libglib-2.0-0', 'libglvnd', 'libgtk-3-0', 'libxcb-glx', 'wayland'
+            'libglib-2.0-0',
+            'libglvnd',
+            'libgtk-3-0',
+            'libxcb-glx',
+            'wayland',
+            'libz1',
         ]
 
         install_virtual_packages(virtual_packages)
 
+        yocto_packages = list_yocto_packages()
+        packages = list_packages()
+
+        # Install the kernel and modules after all the normal packages are in.
+        yocto_packages_to_install = [
+            package for package in yocto_packages
+            if (package.name.startswith('kernel-module-') or package.name.
+                startswith('kernel-5.10') or package.name == 'kernel-modules')
+        ]
+
+        packages_to_remove = []
+
+        # Remove kernel-module-* packages + kernel- package.
+        for key in packages:
+            if key.startswith('kernel-module') or key.startswith(
+                    'kernel-5.10'):
+                already_installed = False
+                for index, yocto_package in enumerate(
+                        yocto_packages_to_install):
+                    if key == yocto_package.name and packages[
+                            key] == yocto_package.version:
+                        already_installed = True
+                        del yocto_packages_to_install[index]
+                        break
+                if not already_installed:
+                    packages_to_remove.append(key)
+
+        print("Removing", packages_to_remove)
+        if len(packages_to_remove) > 0:
+            target(['dpkg', '--purge'] + packages_to_remove)
+        print("Installing",
+              [package.name for package in yocto_packages_to_install])
+
+        install_packages(yocto_packages_to_install, packages)
+
         yocto_package_names = [
             'tegra-argus-daemon',
             'tegra-firmware',
@@ -1047,46 +1074,89 @@
             'nsight-systems-cli',
             'nsight-systems-cli-qdstrmimporter',
             'tegra-tools-jetson-clocks',
+            'gstreamer1.0',
+            'gstreamer1.0-plugins-base',
+            'libgstallocators-1.0-0',
+            'liborc',
+            'libgstvideo-1.0-0',
+            'libnvdsbufferpool1.0.0',
+            'gstreamer1.0-plugins-nvarguscamerasrc',
+            'cuda-cudart',
+            'gstreamer1.0-plugins-nvvidconv',
+            'gstreamer1.0-plugins-tegra',
+            'gstreamer1.0-plugins-nvcompositor',
+            'gstreamer1.0-plugins-nvdrmvideosink',
+            'gstreamer1.0-plugins-nveglgles',
+            'gstreamer1.0-plugins-nvjpeg',
+            'gstreamer1.0-plugins-nvtee',
+            'gstreamer1.0-plugins-nvv4l2camerasrc',
+            'gstreamer1.0-plugins-nvvideo4linux2',
+            'gstreamer1.0-plugins-nvvideosinks',
+            'gstreamer1.0-plugins-tegra-binaryonly',
+            'libgstnvcustomhelper',
+            'gstreamer1.0-plugins-bad-videoparsersbad',
+            'libgstcodecparsers-1.0-0',
+            'libgstriff-1.0-0',
+            'liborc-0.4-0',
+            'libgstaudio-1.0-0',
+            'libgsttag-1.0-0',
+            'gstreamer1.0-plugins-good-rtp',
+            'libgstrtp-1.0-0',
+            'gstreamer1.0-plugins-good-udp',
+            # Yocto's doesn't work with gstreamer, and we don't actually care
+            # hugely.  opencv seems to work.
+            'libv4l',
+            'libv4l-dev',
+            'libgstpbutils-1.0-0',
+            'libgstnvdsseimeta1.0.0',
+            'media-ctl',
+            'libgstapp-1.0-0',
         ]
-        yocto_packages = list_yocto_packages()
-        packages = list_packages()
 
         install_packages([
             package for package in yocto_packages
             if package.name in yocto_package_names
         ], packages)
 
-        # Now, install the kernel and modules after all the normal packages are in.
-        yocto_packages_to_install = [
-            package for package in yocto_packages
-            if (package.name.startswith('kernel-module-') or package.name.
-                startswith('kernel-5.10') or package.name == 'kernel-modules')
-        ]
+        install_virtual_packages([
+            'libgstreamer1.0-0',
+            "libgstreamer-plugins-base1.0-0",
+        ])
 
-        packages_to_remove = []
+        target([
+            "apt-mark",
+            "hold",
+            "gstreamer1.0-plugins-base",
+            "libgstreamer1.0-0",
+            "liborc-0.4-0",
+        ])
 
-        # Remove kernel-module-* packages + kernel- package.
-        for key in packages:
-            if key.startswith('kernel-module') or key.startswith(
-                    'kernel-5.10'):
-                already_installed = False
-                for index, yocto_package in enumerate(
-                        yocto_packages_to_install):
-                    if key == yocto_package.name and packages[
-                            key] == yocto_package.version:
-                        already_installed = True
-                        del yocto_packages_to_install[index]
-                        break
-                if not already_installed:
-                    packages_to_remove.append(key)
-
-        print("Removing", packages_to_remove)
-        if len(packages_to_remove) > 0:
-            target(['dpkg', '--purge'] + packages_to_remove)
-        print("Installing",
-              [package.name for package in yocto_packages_to_install])
-
-        install_packages(yocto_packages_to_install, packages)
+        # Opencv depends on gstreamer, but we want our gstreamer...  So install
+        # ours first, install the adapter packages, then install theirs.
+        target([
+            "apt-get",
+            "-y",
+            "install",
+            "libopencv-calib3d406",
+            "libopencv-contrib406",
+            "libopencv-core406",
+            "libopencv-features2d406",
+            "libopencv-flann406",
+            "libopencv-highgui406",
+            "libopencv-imgcodecs406",
+            "libopencv-imgproc406",
+            "libopencv-ml406",
+            "libopencv-objdetect406",
+            "libopencv-photo406",
+            "libopencv-shape406",
+            "libopencv-stitching406",
+            "libopencv-superres406",
+            "libopencv-video406",
+            "libopencv-videoio406",
+            "libopencv-videostab406",
+            "libopencv-viz406",
+            "libopencv-dev",
+        ])
 
         target(["systemctl", "enable", "nvargus-daemon.service"])