# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Maintainer: team/alpine-desktop <bribbers@disroot.org>
pkgname=pipewire
pkgver=1.2.3
pkgrel=1
_so_ver=0.3
pkgdesc="Multimedia processing graphs"
url="https://pipewire.org/"
arch="all"
license="LGPL-2.1-or-later"
pkggroups="pipewire"
makedepends="
	alsa-lib-dev
	avahi-dev
	bash
	bluez-dev
	dbus-dev
	doxygen
	eudev-dev
	fdk-aac-dev
	glib-dev
	graphviz
	gst-plugins-base-dev
	gstreamer-dev
	jack-dev
	libcamera-dev
	libfreeaptx-dev
	liblc3-dev
	libusb-dev
	libx11-dev
	lilv-dev
	meson
	modemmanager-dev
	ncurses-dev
	pulseaudio-dev
	py3-docutils
	readline-dev
	roc-toolkit-dev
	sbc-dev
	vulkan-loader-dev
	xmltoman
	"
# move regular -dev after -jackdev, to steal files
subpackages="
	$pkgname-dbg
	$pkgname-doc
	$pkgname-alsa
	$pkgname-pulse
	$pkgname-jack
	$pkgname-jack-dev:jackdev
	$pkgname-dev
	gst-plugin-pipewire:gst_plugin
	$pkgname-zeroconf
	$pkgname-spa-bluez
	$pkgname-spa-vulkan
	$pkgname-tools
	$pkgname-spa-tools:spa_tools
	$pkgname-lang
	"
install="$pkgname.post-upgrade $pkgname.post-install"
source="https://gitlab.freedesktop.org/PipeWire/pipewire/-/archive/$pkgver/pipewire-$pkgver.tar.gz
	0001-libcamera-reset-ringbuffer-when-clearing-buffers.patch
	pipewire.desktop
	pipewire-launcher.sh
	"

case "$CARCH" in
	s390x)
		# libldac not available for big endian
		;;
	*)
		makedepends="$makedepends libldac-dev"
		;;
esac

case "$CARCH" in
	ppc64le|s390x|riscv64)
		# no webrtc-audio-processing
		;;
	*)
		makedepends="$makedepends webrtc-audio-processing-1-dev"
		subpackages="$subpackages $pkgname-echo-cancel:echo_cancel"
		;;
esac

# put libs last to steal residual files taken by some prior extras
subpackages="$subpackages $pkgname-libs"

build() {
	CFLAGS="$CFLAGS -O2" \
	CXXFLAGS="$CXXFLAGS -O2" \
	CPPFLAGS="$CPPFLAGS -O2" \
	abuild-meson \
		-Db_lto=true \
		-Dlibjack-path=/usr/lib \
		-Dlibv4l2-path=/usr/lib \
		-Ddocs=disabled \
		-Dman=enabled \
		-Dgstreamer=enabled \
		-Dexamples=enabled \
		-Dffmpeg=disabled \
		-Djack-devel=true \
		-Dsystemd=disabled \
		-Dvulkan=enabled \
		-Dsdl2=disabled \
		-Dlv2=enabled \
		-Dlibcamera=enabled \
		-Droc=enabled \
		-Dbluez5-codec-lc3=enabled \
		-Dbluez5-backend-native-mm=enabled \
		-Dsession-managers=[] \
		. output
	meson compile -C output
}

check() {
	meson test --no-rebuild --print-errorlogs -C output
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C output

	install -Dm644 "$srcdir"/pipewire.desktop -t "$pkgdir"/etc/xdg/autostart/
	install -Dm755 "$srcdir"/pipewire-launcher.sh "$pkgdir"/usr/libexec/pipewire-launcher
}

alsa() {
	pkgdesc="ALSA support for pipewire"
	replaces="$pkgname"  # for backward compatibility

	amove usr/lib/alsa-lib
	amove usr/share/alsa/alsa.conf.d

	install -d "$subpkgdir"/etc/alsa/conf.d
	for i in "$subpkgdir"/usr/share/alsa/alsa.conf.d/*.conf; do
		ln -s -- "${i#"$subpkgdir"}" "$subpkgdir"/etc/alsa/conf.d/
	done
}

pulse() {
	pkgdesc="Pulseaudio support for pipewire"
	depends="
		pipewire-session-manager
		pulseaudio-utils
		"
	provides="pulseaudio=$pkgver-r$pkgrel pulseaudio-bluez=$pkgver-r$pkgrel"
	provider_priority=1

	amove usr/bin/pipewire-pulse
	amove usr/lib/pipewire-$_so_ver/libpipewire-module-protocol-pulse.so
	amove usr/lib/pipewire-$_so_ver/libpipewire-module-pulse-tunnel.so
	amove usr/lib/pipewire-$_so_ver/libpipewire-module-filter-chain-lv2.so
	amove usr/share/pipewire/pipewire-pulse.conf
	amove usr/share/pipewire/pipewire-pulse.conf.avail

}

jack() {
	pkgdesc="JACK support for pipewire"
	depends="pipewire-session-manager"
	provides="jack=$pkgver-r$pkgrel"
	replaces="jack"

	amove usr/lib/libjack*.so.*
	amove usr/bin/pw-jack
	amove usr/lib/spa-*/jack/libspa-jack.so
	amove usr/share/pipewire/jack.conf
}

gst_plugin() {
	pkgdesc="Multimedia graph framework - PipeWire plugin"
	depends="pipewire-session-manager gst-plugins-base"
	install_if="$pkgname=$pkgver-r$pkgrel gstreamer"

	amove usr/lib/gstreamer-1.0
}

echo_cancel() {
	pkgdesc="WebRTC-based echo canceller module for PipeWire"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/lib/pipewire-$_so_ver/libpipewire-module-echo-cancel.so
}

zeroconf() {
	pkgdesc="$pkgdesc - Zeroconf support"
	depends=""
	provides="pulseaudio-zeroconf=$pkgver-r$pkgrel"
	provider_priority=1

	amove usr/lib/pipewire-$_so_ver/libpipewire-module-zeroconf-discover.so
	amove usr/lib/pipewire-$_so_ver/libpipewire-module-raop-*.so
}

bluez() {
	pkgdesc="PipeWire BlueZ5 SPA plugin (Bluetooth)"
	depends=""
	replaces="$pkgname"  # for backward compatibility

	amove usr/lib/spa-*/bluez5
}

vulkan() {
	pkgdesc="PipeWire Vulkan SPA plugin"
	depends=""

	amove usr/lib/spa-*/vulkan
}

tools() {
	pkgdesc="PipeWire tools"
	depends="$pkgname=$pkgver-r$pkgrel"
	replaces="$pkgname"  # for backward compatibility

	amove usr/bin/pw-*
}

spa_tools() {
	pkgdesc="PipeWire SPA tools"
	depends=""
	replaces="$pkgname"  # for backward compatibility

	amove usr/bin/spa-*
}

jackdev() {
	pkgdesc="JACK development files (from pipewire)"
	provides="jack-dev=$pkgver-r$pkgrel"

	amove usr/lib/libjack*.so
	amove usr/include/jack
	amove usr/lib/pkgconfig/jack.pc
}

libs() {
	default_libs

	# the libraries must be paired with the client config and the plugins,
	# see:
	# https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/3296
	# https://bugs.archlinux.org/task/78813
	amove \
		usr/share/pipewire/client*.conf* \
		usr/lib/pipewire-*/ \
		usr/lib/spa-*/
}

sha512sums="
025dbda4bdbea7848a6b3892170bd81eb284385a2da202a9f294030cd710d460ca4e604025b8a475e54eb37839fe91b92adb550c3ad7c7ce9aa960d73d22821b  pipewire-1.2.3.tar.gz
8161b89b0ab0951d539872aceb2f059b84fffb1d41dca94fc525682109cdd80006ad4515a5176a1010ab02ddec1883e7ae9577f8d6f1b807ebe330efe34a97ca  0001-libcamera-reset-ringbuffer-when-clearing-buffers.patch
b70b05151bcf4b7f13d68b2731d789b9c062fadfcb91f4ed6bdb0d495390b0010f57034fb115a053e1459c7d9ea27523ca1c0c69639aeeb96c17ebac8dfc5b57  pipewire.desktop
6f4322d306bef346187778fc0975d608fdd37d4c8e08815a0aed28ef7e07c3f21d6a80b70743d0f867bdc237f5e1dc28131c778e7a45e363e5677668ce7acdb1  pipewire-launcher.sh
"
