Files
moonmix/cmd/moonmix-tabs-host/main.mbt
T
nandiandClaude Opus 5.5 e16cc1b2ad 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>
2026-11-29 19:26:29 -08:00

119 lines
2.8 KiB
MoonBit

// moonmix-tabs-host: started by the browser for the moonmix extension
// (native messaging). It sees tabs become audible and the browser's PipeWire
// streams start, matches the two (@model.TabTracker) and publishes the
// result for the mixer. It runs as long as the browser, so streams that
// started before the mixer was opened have names too.
//
// stdout belongs to the native-messaging protocol: never print.
///|
extern "C" fn c_exit(code : Int32) = "exit"
///|
const TICK_MS : Int32 = 500
///|
struct Host {
browser : Int32
state : @model.State
tracker : @model.TabTracker
mut ready : Bool
mut published : @model.TabFile?
}
///|
fn Host::stop(_ : Host) -> Unit {
@tabs.unpublish()
c_exit(0)
}
///|
/// Serials of the browser's running playback streams. Its audio service,
/// which owns them, is a child of the browser process.
fn Host::running(self : Host) -> Array[String] {
let out = []
for _, n in self.state.nodes {
if n.kind() is PlaybackStream &&
n.running &&
n.parent_pid == self.browser &&
n.serial != "" {
out.push(n.serial)
}
}
out
}
///|
fn Host::update(self : Host) -> Unit {
if !self.ready {
return
}
let now = @tabs.now_ms()
// Another profile of this browser runs its own host with its own tabs.
let others = @tabs.files().filter(fn(f) { f.browser == self.browser })
self.tracker.settle(now, eliminate=others.length() <= 1)
let file : @model.TabFile = {
browser: self.browser,
streams: self.tracker.matches(),
}
if self.published != Some(file) && @tabs.publish(file) {
self.published = Some(file)
}
}
///|
fn main {
let host : Host = {
browser: @tabs.browser_pid(),
state: @model.State::new(),
tracker: @model.TabTracker::new(),
ready: false,
published: None,
}
@tabs.prune()
@tabs.on_event(fn(ev) {
match ev {
Message =>
match @model.parse_tabs(@tabs.message()) {
Some(tabs) => {
host.tracker.set_tabs(tabs, @tabs.now_ms())
host.update()
}
None => ()
}
Eof => host.stop()
_ => ()
}
})
@wp.on_event(fn(ev) {
for e in @wp.to_model_events(ev) {
let _ = host.state.apply(e)
}
match ev {
Ready => {
host.ready = true
host.tracker.set_streams(host.running(), @tabs.now_ms(), initial=true)
host.update()
@wp.timeout(TICK_MS, 1)
}
Timeout(1) => {
host.update()
@wp.timeout(TICK_MS, 1)
}
// The extension starts a new host, which reconnects.
Disconnected | PluginError => host.stop()
NodeAdded(_) | NodeChanged(_) | NodeRemoved(_) =>
if host.ready {
host.tracker.set_streams(host.running(), @tabs.now_ms())
host.update()
}
_ => ()
}
})
if !@wp.start() {
c_exit(1)
}
@tabs.listen()
@wp.run()
}