container-guest-tools: Use pipewire for bullseye

Changes included:
 * cros-apt-config: Prioritize bullseye-backports for pipewire packages.
 * cros-guest-tools: Bullseye version recommends cros-pipe-config.
   Buster version recommends cros-pulse-config.
 * cros-pipe-config: New package to install PipeWire and its
   configuration.
 * cros-pulse-config: Create different version for bullseye and buster.
   Bullseye version requires cros-pipe-config.
 * cros-debs: Remove cros-guest-tools and cros-pulse-config from
   cros-debs-common as they have different version for bullseye and
   buster.
 * lxd_setup: Add bullseye-backports to cros-staging.list. Install
   cros-apt-config before installing cros-guest-tools.

BUG=b:262672243
TEST=Build and install the deb in crostini, check if audio is ok.
TEST=Simulate automatic update by garcon, check if audio is ok.
TEST=See the list of installed packages in kokoro build.

Cq-Depend: chromium:4113777, chromium:4123924
Change-Id: Ic1763c0ceba7d27a461564616d17d3b1de824aa3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/containers/cros-container-guest-tools/+/4125215
Tested-by: Norman Bintang <normanbt@chromium.org>
Reviewed-by: James Ye <jamesye@google.com>
Commit-Queue: Norman Bintang <normanbt@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/cros-apt-config/BUILD b/cros-apt-config/BUILD
index 1963a99..133d507 100644
--- a/cros-apt-config/BUILD
+++ b/cros-apt-config/BUILD
@@ -84,7 +84,7 @@
     maintainer = "The ChromiumOS Authors <chromium-os-dev@chromium.org>",
     package = "cros-apt-config",
     section = "misc",
-    version = "0.24",
+    version = "0.25",
     visibility = ["//cros-debs:__pkg__"],
 )
 
@@ -97,6 +97,6 @@
     maintainer = "The ChromiumOS Authors <chromium-os-dev@chromium.org>",
     package = "cros-apt-config",
     section = "misc",
-    version = "0.24~cros10",  # This version should sort before the :deb version.
+    version = "0.25~cros10",  # This version should sort before the :deb version.
     visibility = ["//cros-debs:__pkg__"],
 )
diff --git a/cros-apt-config/cros.pref b/cros-apt-config/cros.pref
index 3ded4a9..17cc7ce 100644
--- a/cros-apt-config/cros.pref
+++ b/cros-apt-config/cros.pref
@@ -2,3 +2,8 @@
 Pin: release l=cros-containers
 Pin-Priority: 995
 Explanation: Prioritize cros-containers packages
+
+Package: src:pipewire src:wireplumber
+Pin: release a=bullseye-backports
+Pin-Priority: 900
+Explanation: Use bullseye-backports for pipewire packages
diff --git a/cros-debs/BUILD b/cros-debs/BUILD
index 332c5f7..62712d0 100644
--- a/cros-debs/BUILD
+++ b/cros-debs/BUILD
@@ -13,7 +13,6 @@
         "//cros-host-fonts:deb",
         "//cros-logging:deb",
         "//cros-notificationd:deb",
-        "//cros-pulse-config:deb",
         "//cros-sftp:deb",
         "//cros-sommelier:deb",
         "//cros-sommelier-config:deb",
@@ -32,6 +31,8 @@
         ":cros-debs-common",
         "//cros-guest-tools:deb",
         "//cros-apt-config:deb",
+        "//cros-pipe-config:deb",
+        "//cros-pulse-config:deb",
         "//cros-xdg-desktop-portal:deb",
     ],
 )
@@ -43,6 +44,7 @@
         "//cros-guest-tools:deb-buster",
         "//cros-apt-config:deb-buster",
         "//cros-gpu-buster:deb",
+        "//cros-pulse-config:deb-buster",
     ],
 )
 
diff --git a/cros-guest-tools/BUILD b/cros-guest-tools/BUILD
index 2323d55..d4c81f1 100644
--- a/cros-guest-tools/BUILD
+++ b/cros-guest-tools/BUILD
@@ -24,7 +24,6 @@
     "cros-apt-config",
     "cros-gpu",
     "cros-logging",
-    "cros-pulse-config",
     "cros-sommelier-config",
     "cros-sudo-config",
     "cros-systemd-overrides",
@@ -41,7 +40,6 @@
     "less",
     "libxss1",
     "man-db",
-    "pulseaudio",
     "udev",
     "unzip",
     "usbutils",
@@ -59,9 +57,9 @@
     homepage = "https://chromium.googlesource.com/chromiumos/containers/cros-container-guest-tools/",
     maintainer = "The ChromiumOS Authors <chromium-os-dev@chromium.org>",
     package = "cros-guest-tools",
-    recommends = RECOMMENDS,
+    recommends = RECOMMENDS + ["cros-pipe-config"],
     section = "misc",
-    version = "0.33",
+    version = "0.34",
     visibility = ["//cros-debs:__pkg__"],
 )
 
@@ -74,10 +72,10 @@
     homepage = "https://chromium.googlesource.com/chromiumos/containers/cros-container-guest-tools/",
     maintainer = "The ChromiumOS Authors <chromium-os-dev@chromium.org>",
     package = "cros-guest-tools",
-    recommends = RECOMMENDS,
+    recommends = RECOMMENDS + ["cros-pulse-config"],
     section = "misc",
     # This version should sort before the :deb version. This ensures that if
     # the container is upgraded, the :deb version is installed as an update.
-    version = "0.33~cros10",
+    version = "0.34~cros10",
     visibility = ["//cros-debs:__pkg__"],
 )
\ No newline at end of file
diff --git a/cros-pipe-config/BUILD b/cros-pipe-config/BUILD
new file mode 100644
index 0000000..f90477e
--- /dev/null
+++ b/cros-pipe-config/BUILD
@@ -0,0 +1,54 @@
+# Copyright 2023 The ChromiumOS Authors
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+load("@rules_pkg//:pkg.bzl", "pkg_deb", "pkg_tar")
+
+pkg_tar(
+    name = "pipewire-pulse-config",
+    srcs = [
+        "pipewire-pulse/crostini.conf",
+    ],
+    mode = "0644",
+    package_dir = "/etc/pipewire/pipewire-pulse.conf.d",
+    strip_prefix = "/cros-pipe-config/pipewire-pulse",
+)
+
+pkg_tar(
+    name = "pipewire-config",
+    srcs = [
+        "pipewire/crostini.conf",
+    ],
+    mode = "0644",
+    package_dir = "/etc/pipewire/pipewire.conf.d",
+    strip_prefix = "/cros-pipe-config/pipewire",
+)
+
+pkg_tar(
+    name = "debian-data",
+    extension = "tar.gz",
+    deps = [
+        ":pipewire-config",
+        ":pipewire-pulse-config"
+    ],
+)
+
+pkg_deb(
+    name = "deb",
+    architecture = "all",
+    data = ":debian-data",
+    depends = [
+        "pipewire (>= 0.3.61)",
+        "pipewire-alsa",
+        "pipewire-pulse",
+        "wireplumber",
+        "cros-apt-config (>= 0.25)"
+    ],
+    description_file = "deb-description",
+    homepage = "https://chromium.googlesource.com/chromiumos/docs/+/HEAD/containers_and_vms.md",
+    maintainer = "The ChromiumOS Authors <chromium-os-dev@chromium.org>",
+    package = "cros-pipe-config",
+    section = "misc",
+    version = "0.1.0",
+    visibility = ["//cros-debs:__pkg__"],
+)
diff --git a/cros-pipe-config/deb-description b/cros-pipe-config/deb-description
new file mode 100644
index 0000000..bac72f0
--- /dev/null
+++ b/cros-pipe-config/deb-description
@@ -0,0 +1,6 @@
+PipeWire helper for ChromiumOS integration
+ This package installs customized pipewire and pipewire-pulse configurations
+ to /etc/pipewire/pipewire.conf.d and /etc/pipewire/pipewire-pulse.conf.d.
+ The custom configurations are:
+  * Set a higher minimum buffer size to avoid audio glitches.
+  * Load audio sink and source manually because of the lack of udev.
diff --git a/cros-pipe-config/pipewire-pulse/crostini.conf b/cros-pipe-config/pipewire-pulse/crostini.conf
new file mode 100644
index 0000000..1abf79c
--- /dev/null
+++ b/cros-pipe-config/pipewire-pulse/crostini.conf
@@ -0,0 +1,12 @@
+# Copyright 2023 The ChromiumOS Authors
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+stream.properties = {
+    node.latency          = 1024/48000
+}
+
+pulse.properties = {
+    pulse.min.req         = 2048/48000     # 40ms
+    pulse.min.frag        = 2048/48000     # 40ms
+}
diff --git a/cros-pipe-config/pipewire/crostini.conf b/cros-pipe-config/pipewire/crostini.conf
new file mode 100644
index 0000000..fef611d
--- /dev/null
+++ b/cros-pipe-config/pipewire/crostini.conf
@@ -0,0 +1,46 @@
+# Copyright 2023 The ChromiumOS Authors
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+context.properties = {
+    link.max-buffers          = 64
+    default.clock.min-quantum = 1024
+}
+
+context.objects = [
+    { factory = adapter
+       args = {
+           factory.name           = api.alsa.pcm.source
+           node.name              = "alsa-source"
+           node.description       = "PCM Source"
+           media.class            = "Audio/Source"
+           api.alsa.path          = "hw:0"
+           api.alsa.period-size   = 1024
+           api.alsa.headroom      = 0
+           api.alsa.disable-mmap  = false
+           api.alsa.disable-batch = false
+           audio.format           = "S16LE"
+           audio.rate             = 48000
+           audio.channels         = 2
+           audio.position         = "FL,FR"
+       }
+    }
+
+    { factory = adapter
+       args = {
+           factory.name           = api.alsa.pcm.sink
+           node.name              = "alsa-sink"
+           node.description       = "PCM Sink"
+           media.class            = "Audio/Sink"
+           api.alsa.path          = "hw:0"
+           api.alsa.period-size   = 1024
+           api.alsa.headroom      = 1024
+           api.alsa.disable-mmap  = false
+           api.alsa.disable-batch = false
+           audio.format           = "S16LE"
+           audio.rate             = 48000
+           audio.channels         = 2
+           audio.position         = "FL,FR"
+       }
+    }
+]
diff --git a/cros-pulse-config/BUILD b/cros-pulse-config/BUILD
index 40de715..5ce18a1 100644
--- a/cros-pulse-config/BUILD
+++ b/cros-pulse-config/BUILD
@@ -26,6 +26,22 @@
     name = "deb",
     architecture = "all",
     data = ":debian-data",
+    depends = ["cros-pipe-config"],
+    description_file = "deb-description",
+    homepage = "https://chromium.googlesource.com/chromiumos/docs/+/HEAD/containers_and_vms.md",
+    maintainer = "The ChromiumOS Authors <chromium-os-dev@chromium.org>",
+    package = "cros-pulse-config",
+    postinst = "postinst",
+    prerm = "prerm",
+    section = "misc",
+    version = "0.3.0",
+    visibility = ["//cros-debs:__pkg__"],
+)
+
+pkg_deb(
+    name = "deb-buster",
+    architecture = "all",
+    data = ":debian-data",
     depends = ["pulseaudio"],
     description_file = "deb-description",
     homepage = "https://chromium.googlesource.com/chromiumos/docs/+/HEAD/containers_and_vms.md",
@@ -34,6 +50,6 @@
     postinst = "postinst",
     prerm = "prerm",
     section = "misc",
-    version = "0.2.1",
+    version = "0.3.0~cros10",
     visibility = ["//cros-debs:__pkg__"],
 )
diff --git a/cros-pulse-config/pipe-deb-description b/cros-pulse-config/pipe-deb-description
new file mode 100644
index 0000000..4e0adcd
--- /dev/null
+++ b/cros-pulse-config/pipe-deb-description
@@ -0,0 +1 @@
+Transitional package for cros-pipe-config
diff --git a/lxd/lxd_setup.sh b/lxd/lxd_setup.sh
index b9fa865..e5525e7 100755
--- a/lxd/lxd_setup.sh
+++ b/lxd/lxd_setup.sh
@@ -15,13 +15,16 @@
     echo "deb [trusted=yes] file:///run/apt ${release} main" > "${cros_staging_list}"
     if [[ "${release}" = "buster" ]]; then
         echo "deb https://deb.debian.org/debian buster-backports main" >> "${cros_staging_list}"
+    elif [[ "${release}" = "bullseye" ]]; then
+        echo "deb https://deb.debian.org/debian bullseye-backports main" >> "${cros_staging_list}"
     fi
 
     apt-get -o Acquire::Retries=3 update
 
+    apt-get -o Acquire::Retries=3 -q -y --allow-unauthenticated install cros-apt-config
     apt-get -o Acquire::Retries=3 -q -y --allow-unauthenticated install cros-guest-tools
     # Upgrade packages again to ensure cros-apt-config changes are picked up.
-    apt-get -o Acquire::Retries=3 -q -y upgrade
+    apt-get -o Acquire::Retries=3 -q -y dist-upgrade
 
     apt-get clean
     rm "${cros_staging_list}"