122 lines
6.2 KiB
Python
122 lines
6.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise Fenrir's Pulse driver against a private PipeWire null sink."""
|
|
import json
|
|
import os
|
|
import shutil
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import threading
|
|
import time
|
|
|
|
|
|
def main():
|
|
repository = Path(__file__).resolve().parents[1]
|
|
for executable in ('pipewire', 'pw-link', 'pw-cli', 'pw-dump', 'go'):
|
|
if not shutil.which(executable):
|
|
raise SystemExit('Missing prerequisite: ' + executable)
|
|
with tempfile.TemporaryDirectory(prefix='fenrir-notification-pulse-') as directory:
|
|
root = Path(directory)
|
|
(root / 'client.conf').write_bytes(Path('/usr/share/pipewire/client.conf').read_bytes())
|
|
config = root / 'pipewire.conf'
|
|
config.write_text('''
|
|
context.properties = { core.daemon = true core.name = fenrir-notification-test support.dbus = false }
|
|
context.spa-libs = { audio.convert.* = audioconvert/libspa-audioconvert support.* = support/libspa-support }
|
|
context.modules = [
|
|
{ name = libpipewire-module-protocol-native }
|
|
{ name = libpipewire-module-client-node }
|
|
{ name = libpipewire-module-adapter }
|
|
{ name = libpipewire-module-metadata }
|
|
{ name = libpipewire-module-spa-node-factory }
|
|
{ name = libpipewire-module-link-factory }
|
|
{ name = libpipewire-module-access args = { access.legacy = true } }
|
|
{ name = libpipewire-module-protocol-pulse args = { server.address = [ "unix:SOCKET" ] } }
|
|
]
|
|
context.objects = [
|
|
{ factory = spa-node-factory args = { factory.name = support.node.driver node.name = Dummy-Driver priority.driver = 200000 } }
|
|
{ factory = adapter args = {
|
|
factory.name = support.null-audio-sink
|
|
node.name = notification-null
|
|
node.description = "Notification test null sink"
|
|
media.class = Audio/Sink
|
|
audio.position = [ FL FR ]
|
|
adapter.auto-port-config = { mode = dsp monitor = true position = preserve }
|
|
} }
|
|
]
|
|
'''.replace('SOCKET', str(root / 'pulse.sock')))
|
|
environment = dict(os.environ, XDG_RUNTIME_DIR=str(root), PIPEWIRE_RUNTIME_DIR=str(root),
|
|
PIPEWIRE_REMOTE='fenrir-notification-test', PIPEWIRE_CONFIG_DIR=str(root),
|
|
FENRIR_TEST_PULSE_SERVER='unix:' + str(root / 'pulse.sock'))
|
|
stop = threading.Event()
|
|
with (root / 'server.log').open('w+') as log:
|
|
server = subprocess.Popen(['pipewire', '-c', str(config)], env=environment, stdout=log, stderr=log)
|
|
def connect_ports():
|
|
linked = set()
|
|
configured = set()
|
|
while not stop.is_set():
|
|
dump = subprocess.run(['pw-dump', '-r', 'fenrir-notification-test'], env=environment, capture_output=True, text=True, timeout=2)
|
|
nodes = [(o['id'], o.get('info',{}).get('props',{})) for o in json.loads(dump.stdout or '[]') if o['type'].endswith(':Node')]
|
|
for nodeId, props in nodes:
|
|
if props.get('media.class') == 'Stream/Output/Audio' and nodeId not in configured:
|
|
portConfig = '{ direction = Output mode = dsp format = { mediaType = audio mediaSubtype = raw format = F32P rate = 48000 channels = 2 position = [ FL FR ] } }'
|
|
result = subprocess.run(['pw-cli', '-r', 'fenrir-notification-test', 'set-param', str(nodeId), 'PortConfig', portConfig], env=environment, capture_output=True, text=True, timeout=2)
|
|
if result.returncode:
|
|
raise RuntimeError(result.stderr)
|
|
configured.add(nodeId)
|
|
result = subprocess.run(['pw-link', '-r', 'fenrir-notification-test', '-o'], env=environment,
|
|
capture_output=True, text=True, timeout=2)
|
|
|
|
for line in result.stdout.splitlines():
|
|
port = line.strip()
|
|
if ':output_' in port:
|
|
channel = port.rsplit('_', 1)[-1]
|
|
target = 'notification-null:playback_' + channel
|
|
pair = (port, target)
|
|
if pair not in linked:
|
|
link = subprocess.run(['pw-link', '-r', 'fenrir-notification-test', port, target],
|
|
env=environment, capture_output=True, timeout=2)
|
|
if link.returncode == 0:
|
|
linked.add(pair)
|
|
stop.wait(.05)
|
|
def link_ports():
|
|
try:
|
|
connect_ports()
|
|
except Exception as error:
|
|
linkErrors.append(str(error))
|
|
stop.set()
|
|
linker = None
|
|
linkErrors = []
|
|
try:
|
|
deadline = time.monotonic() + 5
|
|
while not (root / 'pulse.sock').exists():
|
|
if server.poll() is not None or time.monotonic() >= deadline:
|
|
log.seek(0)
|
|
raise RuntimeError(log.read())
|
|
time.sleep(.02)
|
|
linker = threading.Thread(target=link_ports)
|
|
linker.start()
|
|
result = subprocess.run(['go', 'test', '-race', './internal/sound', '-run', '^TestPrivatePulseNotificationAndShutdown$', '-count=1', '-timeout=20s', '-v'],
|
|
env=environment, cwd=repository, capture_output=True, text=True, timeout=45)
|
|
print(result.stdout)
|
|
print(result.stderr)
|
|
if linkErrors:
|
|
raise RuntimeError("Private port linking failed: " + "; ".join(linkErrors))
|
|
if result.returncode:
|
|
log.seek(0)
|
|
print(log.read())
|
|
raise SystemExit(result.returncode)
|
|
finally:
|
|
stop.set()
|
|
if linker:
|
|
linker.join(timeout=5)
|
|
server.terminate()
|
|
try:
|
|
server.wait(timeout=5)
|
|
except subprocess.TimeoutExpired:
|
|
server.kill()
|
|
server.wait()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|