pkgname=pipewire
pkgver=1.4.9
pkgdesc="Low-latency audio/video router and processor"
homepage="https://pipewire.org"
arch=(x86_64)
license=(MIT)
makedepends=(
  alsa-lib
  dbus
  libpulse
  libsndfile
  meson
  ncurses
  python-docutils
  readline
  rtkit
  sdl2
  systemd
  ffmpeg
  fftw
)
checkdepends=(
  desktop-file-utils
)
_patches=("pipewire-0.3.25-enable-failed-mlock-warning.patch"
  "pipewire-1.4.6-no-automagic-ebur128.patch"
  "pipewire-1.4.6-no-automagic-fftw.patch"
)
_patch_sums=("7456a24276b7b766ff5799acf5b80ef5f85ee7a78fc5b52adff3206c0b0aa1985a0bf72634aacf7818c90136c09bcc11cb0fecec291d961c8b3585c897869f65"
  "c8aca1e2a8c4ddb7dd9a357ffefc77070cb6f123228678015865a505210e851948d5e7acbf8c17076ce2d56042f0a68b7f65439f958f938637ca475886eb81d2"
  "726b28015cc378fd2c5e268655f4162ca274ffd25976f3ba816d470fc90c33e86ac1930c76816f20b3ab812f7034a88dddb36b40e5cde51d79443689cdbb5e34"
)
sources=("${pkgname}-${pkgver}.tar.bz2"
  "${pkgname}-1.4.7-0001-don-t-include-standard-C-headers-inside-of-extern-C.patch.xz"
  "99-pipewire-default-hook.conf"
  ${_patches[@]}
)
urls=("https://gitlab.freedesktop.org/${pkgname}/${pkgname}/-/archive/${pkgver}/${sources[0]}"
  "https://dev.gentoo.org/~sam/distfiles/media-video/${pkgname}/${sources[1]}"
  "99-pipewire-default-hook.conf"
  "${_patches[@]}"
)
sha512sums=("369054d920133904a8df709b1be05f9ef0e7e99e1ae10e1d200315ce1200e20d5e20a91f40dd17dd3177eef59de7d47dfdd6161ec6f057b78140690fcfcc1c2e"
  "fbd9644a5df81ee4bc9687f6a8b69a0bf12deef43d749c9c5cbb976f461771deeae4bd13340a6acaffae4b0187221cf734be2b3b62408a4ce79d70575a7e664b"
  "4e512c62b7627fbe9e48afbbf171bd2b8d4fb8b231492cb1ea2d90ac474212c25e0f8ba3389b1df1108a46f243f2e1f3a3eaf2d147a7968cec82a8a49f5fd1b7"
  "${_patch_sums[@]}"
)

src_prepare() {
  tar -xjf ${distdir}/${sources[0]} --strip-components=1
  cp ${distdir}/${sources[1]} ./
  xz --decompress ${sources[1]}
  for _patch in ${_patches[@]}; do
    patch -p1 -i ${filedir}/${_patch}
  done
  patch -p1 -i ${sources[1]%.xz}
}

src_build() {
  local meson_args=(
    --prefix=/usr
    --buildtype=release
    --wrap-mode=nodownload
    -D python.bytecompile=1
    -D docdir="/usr/share/doc/${pkgname}-${pkgver}"
    -D dbus=enabled
    -D avahi=disabled
    -D docs=disabled
    -D man=disabled
    -D tests=enabled
    -D installed_tests=disabled
    -D libffado=disabled
    -D gstreamer=disabled
    -D gstreamer-device-provider=disabled
    -D gsettings=disabled
    -D systemd=enabled
    -D logind=enabled
    -D logind-provider=libsystemd
    -D systemd-system-service=disabled
    -D systemd-user-service=enabled
    -D pipewire-alsa=enabled
    -D selinux=disabled
    -D spa-plugins=enabled
    -D alsa=enabled
    -D compress-offload=disabled
    -D audiomixer=enabled # Matches upstream
    -D audioconvert=enabled # Matches upstream
    -D bluez5=disabled
    -D bluez5-backend-hsp-native=disabled
    -D bluez5-backend-hfp-native=disabled
    -D bluez5-backend-native-mm=disabled
    -D bluez5-backend-ofono=disabled
    -D bluez5-backend-hsphfpd=disabled
    -D bluez5-codec-aac=disabled
    -D bluez5-codec-aptx=disabled
    -D bluez5-codec-ldac=disabled
    -D bluez5-codec-g722=disabled
    -D opus=disabled
    -D bluez5-codec-opus=disabled
    -D libusb=disabled
    -D echo-cancel-webrtc=disabled
    -D control=enabled # Matches upstream
    -D audiotestsrc=enabled # Matches upstream
    -D ffmpeg=disabled
    -D pw-cat-ffmpeg=enabled
    -D flatpak=disabled
    -D pipewire-jack=enabled
    -D jack=disabled
    -D jack-devel=false
    -D support=enabled
    -D evl=disabled # Matches upstream
    -D test=disabled # fakesink and fakesource plugins
    -D bluez5-codec-lc3plus=disabled # unpackaged
    -D bluez5-codec-lc3=disabled
    -D ebur128=disabled
    -D fftw=enabled
    -D lv2=disabled
    -D v4l2=disabled
    -D libcamera=disabled
    -D roc=disabled
    -D readline=enabled
    -D raop=enabled
    -D videoconvert=enabled # Matches upstream
    -D videotestsrc=enabled # Matches upstream
    -D volume=enabled # Matches upstream
    -D vulkan=disabled # Uses pre-compiled Vulkan compute shader to provide a CGI video source (dev thing; disabled by upstream)
    -D pw-cat=enabled
    -D udev=enabled
    -D sdl2=disabled
    -D libmysofa=disabled
    -D sndfile=enabled
    -D session-managers="[]"
    -D pam-defaults-install=false #new kernel
    -D x11=disabled
    -D x11-xfixes=disabled
    -D libcanberra=disabled
    -D snap=disabled
    -D gsettings-pulse-schema=disabled
  )
  meson setup build "${meson_args[@]}"
  meson configure build --no-pager
  meson compile -C build
}

src_check() {
  meson test -C build --print-errorlogs --timeout-multiplier 10
}

src_install() {
  meson install -C build --destdir "$pkgdir"

  install -vDm644 "${filedir}"/99-pipewire-default-hook.conf -t "${pkgdir}/usr/share/alsa/alsa.conf.d"
  mkdir -pv "${pkgdir}/etc/alsa/conf.d"
  ln -sv ../../../usr/share/alsa/alsa.conf.d/50-pipewire.conf "${pkgdir}"/etc/alsa/conf.d/50-pipewire.conf
  ln -sv ../../../usr/share/alsa/alsa.conf.d/99-pipewire-default-hook.conf "${pkgdir}"/etc/alsa/conf.d/99-pipewire-default-hook.conf
}

src_postinstall() {
  systemctl --global enable pipewire.socket
  systemctl --global enable pipewire-pulse.socket
}

src_preremove() {
  systemctl --global disable pipewire.socket
  systemctl --global disable pipewire-pulse.socket
}

# vim:ft=sh syn=sh et sw=2:
