Name Chrome streams after their browser tabs
A browser extension reports audible tabs to moonmix-tabs-host, which matches them to PipeWire streams by time and publishes the names for the mixer; streams of one tab share a row. Ambiguous streams keep the browser's name. Without the extension, a browser's only running stream shows its MPRIS title. The smoke test no longer claims real audio hardware. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b61d504234
commit
e16cc1b2ad
31 files changed
+1830
-15
No files matched your search
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Smoke-test stand-in for a browser: plays a stream and reports it as a tab.
|
||||
|
||||
fake-browser.py <moonmix-tabs-host> <wav>: starts the host as its child, like a
|
||||
browser does, then pw-play (its "audio service") and a moment later one audible
|
||||
tab. Exits when stdin closes.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
def message(tabs):
|
||||
body = json.dumps({"tabs": tabs}).encode()
|
||||
return struct.pack("=I", len(body)) + body
|
||||
|
||||
|
||||
def play():
|
||||
# exec keeps the pid the stream announces.
|
||||
os.execvp("pw-play", ["pw-play", "-P", json.dumps({
|
||||
"application.name": "Fake Browser",
|
||||
"application.process.id": str(os.getpid()),
|
||||
"media.name": "Playback",
|
||||
}), sys.argv[2]])
|
||||
|
||||
|
||||
host = subprocess.Popen([sys.argv[1]], stdin=subprocess.PIPE)
|
||||
host.stdin.write(message([]))
|
||||
host.stdin.flush()
|
||||
time.sleep(3) # let it connect to PipeWire
|
||||
audio = os.fork()
|
||||
if audio == 0:
|
||||
play()
|
||||
time.sleep(0.5)
|
||||
host.stdin.write(message([{"id": 7, "title": "Smoke test tab", "audible": True}]))
|
||||
host.stdin.flush()
|
||||
sys.stdin.read()
|
||||
os.kill(audio, 15)
|
||||
host.stdin.close()
|
||||
host.wait(5)
|
||||
Executable
+79
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Install the browser tab host and extension for the current user.
|
||||
|
||||
Copies moonmix-tabs-host and the extension, and registers the host with every
|
||||
Chromium-family browser that has a profile. Loading the extension is up to the
|
||||
user. --uninstall removes everything again.
|
||||
"""
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
NAME = "org.latha.moonmix.tabs"
|
||||
BROWSERS = (
|
||||
"google-chrome", "google-chrome-beta", "google-chrome-unstable", "chromium",
|
||||
"BraveSoftware/Brave-Browser", "microsoft-edge", "vivaldi",
|
||||
)
|
||||
|
||||
|
||||
def extension_id(manifest):
|
||||
digest = hashlib.sha256(base64.b64decode(manifest["key"])).hexdigest()[:32]
|
||||
return "".join(chr(ord("a") + int(c, 16)) for c in digest)
|
||||
|
||||
|
||||
def main():
|
||||
root = Path(__file__).resolve().parent.parent
|
||||
config = Path(os.environ.get("XDG_CONFIG_HOME") or Path.home() / ".config")
|
||||
data = Path(os.environ.get("XDG_DATA_HOME") or Path.home() / ".local/share")
|
||||
host = Path.home() / ".local/bin/moonmix-tabs-host"
|
||||
extension = data / "moonmix/extension"
|
||||
registered = [config / b / "NativeMessagingHosts" / f"{NAME}.json" for b in BROWSERS]
|
||||
|
||||
if sys.argv[1:] == ["--uninstall"]:
|
||||
for path in [*registered, host]:
|
||||
path.unlink(missing_ok=True)
|
||||
shutil.rmtree(extension, ignore_errors=True)
|
||||
print("Removed. Also remove the extension in the browser.")
|
||||
return
|
||||
|
||||
built = root / "_build/native/release/build/cmd/moonmix-tabs-host/moonmix-tabs-host.exe"
|
||||
if not built.exists():
|
||||
sys.exit("Build first: moon build --target native --release")
|
||||
host.parent.mkdir(parents=True, exist_ok=True)
|
||||
# Replace, not overwrite: the old one may be running.
|
||||
temporary = host.with_name(host.name + ".new")
|
||||
shutil.copyfile(built, temporary)
|
||||
temporary.chmod(0o755)
|
||||
temporary.replace(host)
|
||||
shutil.copytree(root / "extension", extension, dirs_exist_ok=True)
|
||||
|
||||
origin = extension_id(json.loads((extension / "manifest.json").read_text()))
|
||||
manifest = json.dumps({
|
||||
"name": NAME,
|
||||
"description": "moonmix browser tab names",
|
||||
"path": str(host),
|
||||
"type": "stdio",
|
||||
"allowed_origins": [f"chrome-extension://{origin}/"],
|
||||
}, indent=2)
|
||||
found = False
|
||||
for path in registered:
|
||||
# Only browsers that are in use here.
|
||||
if path.parent.parent.is_dir():
|
||||
path.parent.mkdir(exist_ok=True)
|
||||
path.write_text(manifest + "\n")
|
||||
print(f"Registered: {path}")
|
||||
found = True
|
||||
if not found:
|
||||
print("No supported browser profile found; nothing registered.")
|
||||
print(f"Installed host: {host}")
|
||||
print("Now open the browser's extensions page (chrome://extensions), turn on")
|
||||
print(f"Developer mode, choose 'Load unpacked' and select:\n {extension}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -35,6 +35,14 @@ set -- podman run --rm --pull=never \
|
||||
--mount "type=bind,src=$wayland_socket,dst=/run/moonmix/wayland-0,ro" \
|
||||
--mount "type=bind,src=$pipewire_socket,dst=/run/moonmix/pipewire-0,ro"
|
||||
|
||||
# Browser tab names published by the host's moonmix-tabs-host. Created here so
|
||||
# that names also appear when the browser starts after the mixer.
|
||||
tabs_dir=$XDG_RUNTIME_DIR/moonmix
|
||||
mkdir -p -m 700 "$tabs_dir"
|
||||
set -- "$@" \
|
||||
--mount "type=bind,src=$tabs_dir,dst=/run/moonmix/tabs,ro" \
|
||||
--env MOONMIX_TABS_DIR=/run/moonmix/tabs
|
||||
|
||||
# The portal supplies desktop appearance settings. Do not expose the rest of
|
||||
# XDG_RUNTIME_DIR (which can contain credentials and unrelated service sockets).
|
||||
if [ -S "$XDG_RUNTIME_DIR/bus" ]; then
|
||||
|
||||
@@ -13,6 +13,7 @@ set -eu
|
||||
ROOT=$(cd "$(dirname "$0")/.." && pwd)
|
||||
BUILD="$ROOT/_build/native/debug/build/cmd"
|
||||
PROBE="$BUILD/moonmix-probe/moonmix-probe.exe"
|
||||
TABS_HOST="$BUILD/moonmix-tabs-host/moonmix-tabs-host.exe"
|
||||
GUI="$BUILD/moonmix/moonmix.exe"
|
||||
SHOTS="${1:-}"
|
||||
|
||||
@@ -49,12 +50,19 @@ CONF
|
||||
|
||||
# Ubuntu's WirePlumber asks snapd before granting clients access; there is
|
||||
# no snapd in a sandbox/CI container, so turn that off for the test session.
|
||||
# No hardware either: the test noise must not reach the real speakers.
|
||||
mkdir -p "$XDG_CONFIG_HOME/wireplumber/wireplumber.conf.d"
|
||||
cat > "$XDG_CONFIG_HOME/wireplumber/wireplumber.conf.d/moonmix-test.conf" <<'CONF'
|
||||
wireplumber.profiles = {
|
||||
main = {
|
||||
support.snapd-permission-prompting = disabled
|
||||
script.client.access-snap = disabled
|
||||
monitor.alsa = disabled
|
||||
monitor.alsa-midi = disabled
|
||||
monitor.bluez = disabled
|
||||
monitor.bluez-midi = disabled
|
||||
monitor.libcamera = disabled
|
||||
monitor.v4l2 = disabled
|
||||
}
|
||||
}
|
||||
CONF
|
||||
@@ -160,6 +168,21 @@ meter() {
|
||||
meter "${STREAM:-0}"
|
||||
meter test_speakers
|
||||
|
||||
# Browser tab names: a fake browser with a real tab host.
|
||||
if command -v python3 >/dev/null; then
|
||||
echo "### browser tab names"
|
||||
mkfifo "$LOGS/browser.in"
|
||||
python3 "$ROOT/tools/fake-browser.py" "$TABS_HOST" "$LOGS/noise.wav" \
|
||||
<"$LOGS/browser.in" >"$LOGS/browser.log" 2>&1 & BROWSER=$!; PIDS="$PIDS $BROWSER"
|
||||
exec 5>"$LOGS/browser.in"
|
||||
wait_for 'Smoke test tab | ' || { fail=1; cat "$LOGS/browser.log" "$XDG_RUNTIME_DIR"/moonmix/* || true; "$PROBE" || true; }
|
||||
exec 5>&-
|
||||
wait "$BROWSER" || { echo "FAIL: fake browser"; cat "$LOGS/browser.log"; fail=1; }
|
||||
if [ -n "$(ls "$XDG_RUNTIME_DIR/moonmix")" ]; then echo "FAIL: tab host left its file"; fail=1; fi
|
||||
else
|
||||
echo "python3 not found; skipping browser tab names"
|
||||
fi
|
||||
|
||||
if [ "$fail" != 0 ]; then
|
||||
echo "### diagnostics"
|
||||
echo "--- pw-play playback:"; cat "$LOGS/play.log"
|
||||
|
||||
@@ -92,9 +92,15 @@ class ContainerTests(unittest.TestCase):
|
||||
self.assertEqual(self.mounts(), [
|
||||
f"type=bind,src={self.runtime}/wayland-test,dst=/run/moonmix/wayland-0,ro",
|
||||
f"type=bind,src={self.runtime}/pipewire-0,dst=/run/moonmix/pipewire-0,ro",
|
||||
f"type=bind,src={self.runtime}/moonmix,dst=/run/moonmix/tabs,ro",
|
||||
])
|
||||
self.assertIn("DBUS_SESSION_BUS_ADDRESS=unix:path=/run/moonmix/no-bus", arguments)
|
||||
|
||||
def test_tab_names_directory_is_private_and_read_only(self):
|
||||
self.assertEqual(self.launch().returncode, 0)
|
||||
self.assertEqual((self.runtime / "moonmix").stat().st_mode & 0o777, 0o700)
|
||||
self.assertIn("MOONMIX_TABS_DIR=/run/moonmix/tabs", self.podman_arguments())
|
||||
|
||||
def test_session_bus_is_optional_and_mounted_individually(self):
|
||||
self.bind_socket(self.runtime / "bus")
|
||||
self.assertEqual(self.launch().returncode, 0)
|
||||
@@ -117,6 +123,7 @@ class ContainerTests(unittest.TestCase):
|
||||
self.assertEqual(self.mounts(), [
|
||||
f"type=bind,src={wayland},dst=/run/moonmix/wayland-0,ro",
|
||||
f"type=bind,src={pipewire},dst=/run/moonmix/pipewire-0,ro",
|
||||
f"type=bind,src={self.runtime}/moonmix,dst=/run/moonmix/tabs,ro",
|
||||
])
|
||||
|
||||
def test_pipewire_runtime_remote_and_image_overrides(self):
|
||||
@@ -270,6 +277,7 @@ class ContainerTests(unittest.TestCase):
|
||||
self.assertEqual(mounts, [
|
||||
f"type=bind,src={self.runtime}/wayland-test,dst=/run/moonmix/wayland-0,ro",
|
||||
f"type=bind,src={self.runtime}/pipewire-0,dst=/run/moonmix/pipewire-0,ro",
|
||||
f"type=bind,src={self.runtime}/moonmix,dst=/run/moonmix/tabs,ro",
|
||||
f"type=bind,src={user_icons},dst=/run/moonmix/host/user/icons,ro",
|
||||
f"type=bind,src={sys_pixmaps},dst=/run/moonmix/host/usr/pixmaps,ro",
|
||||
f"type=bind,src={sys_apps},dst=/run/moonmix/host/usr/applications,ro",
|
||||
|
||||
Reference in new issue
Block a user