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
@@ -35,6 +35,9 @@ fn print_state(state : @model.State) -> Unit {
|
||||
flags.push("-> " + r.choices[r.current_choice].label)
|
||||
}
|
||||
let vol = if r.has_volume { r.volume_text } else { "(no volume)" }
|
||||
if r.members.length() > 1 {
|
||||
flags.push("\{r.members.length()} streams")
|
||||
}
|
||||
let f = if flags.length() > 0 {
|
||||
" [" + flags.join(", ") + "]"
|
||||
} else {
|
||||
@@ -110,6 +113,8 @@ fn main {
|
||||
// default-nodes-api reports defaults shortly after Ready: settle first.
|
||||
Ready => @wp.timeout(300, 2)
|
||||
Timeout(2) => {
|
||||
let names = @model.merge_tab_files(@tabs.files())
|
||||
let _ = state.apply(TabNames(names))
|
||||
print_state(state)
|
||||
if args is ["meter", id] {
|
||||
let id = resolve(state, id)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Headless probe: prints what each tab shows, can apply one action.
|
||||
import {
|
||||
"geordi/moonmix/model",
|
||||
"geordi/moonmix/tabs",
|
||||
"geordi/moonmix/wp",
|
||||
"moonbitlang/core/env",
|
||||
"moonbitlang/core/string",
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
// 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()
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Native-messaging host: matches browser tabs to PipeWire streams.
|
||||
import {
|
||||
"geordi/moonmix/model",
|
||||
"geordi/moonmix/tabs",
|
||||
"geordi/moonmix/wp",
|
||||
}
|
||||
|
||||
supported_targets = "-all+native"
|
||||
|
||||
pkgtype(kind: "executable")
|
||||
|
||||
options(
|
||||
link: { "native": { "cc": "tools/pkg-cc" } },
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
// Generated using `moon info`, DON'T EDIT IT
|
||||
package "geordi/moonmix/cmd/moonmix-tabs-host"
|
||||
|
||||
// Values
|
||||
|
||||
// Errors
|
||||
|
||||
// Types and methods
|
||||
type Host
|
||||
|
||||
// Type aliases
|
||||
|
||||
// Traits
|
||||
+63
-5
@@ -152,7 +152,9 @@ fn new_row_view(app : App, tab : @model.Tab, row : @model.Row) -> RowView {
|
||||
match app.find_view(tab, id) {
|
||||
Some(v) =>
|
||||
if idx >= 0 && idx < v.row.choices.length() {
|
||||
let _ = @wp.move_stream(id, v.row.choices[idx].id)
|
||||
for m in v.row.members {
|
||||
let _ = @wp.move_stream(m, v.row.choices[idx].id)
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
@@ -199,7 +201,11 @@ fn new_row_view(app : App, tab : @model.Tab, row : @model.Row) -> RowView {
|
||||
let (mute, mute_tag) = @gtk.toggle(
|
||||
@gtk.resolve_icon(@model.volume_icon_chain(row.percent, row.mute)),
|
||||
"",
|
||||
fn(on) { let _ = @wp.set_mute(id, on) },
|
||||
fn(on) {
|
||||
for m in app.members(tab, id) {
|
||||
let _ = @wp.set_mute(m, on)
|
||||
}
|
||||
},
|
||||
)
|
||||
tags.push(mute_tag)
|
||||
mute.set_tooltip("Mute")
|
||||
@@ -220,6 +226,14 @@ fn new_row_view(app : App, tab : @model.Tab, row : @model.Row) -> RowView {
|
||||
v.last_local_us = @gtk.now_us()
|
||||
v.volume_label.set_text(@model.format_volume(vols))
|
||||
let _ = @wp.set_volumes(id, vols)
|
||||
for m in v.row.members {
|
||||
match app.state.nodes.get(m) {
|
||||
Some(n) if m != id =>
|
||||
@wp.set_volumes(m, @model.scale_linked(n.volumes, percent))
|
||||
|> ignore
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
@@ -339,6 +353,15 @@ fn App::find_view(self : App, tab : @model.Tab, id : Int32) -> RowView? {
|
||||
None
|
||||
}
|
||||
|
||||
///|
|
||||
/// Nodes a row controls.
|
||||
fn App::members(self : App, tab : @model.Tab, id : Int32) -> Array[Int32] {
|
||||
match self.find_view(tab, id) {
|
||||
Some(v) => v.row.members
|
||||
None => [id]
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn App::sync_default_button(self : App, tab : @model.Tab, id : Int32) -> Unit {
|
||||
match self.find_view(tab, id) {
|
||||
@@ -411,7 +434,9 @@ fn App::sync_meters(self : App) -> Unit {
|
||||
Some(t) =>
|
||||
if t.tab.has_meter() {
|
||||
for row in t.rows {
|
||||
want.set(row.id, t.tab.node_kind())
|
||||
for m in row.members {
|
||||
want.set(m, t.tab.node_kind())
|
||||
}
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
@@ -450,8 +475,16 @@ fn App::tick_meters(self : App) -> Unit {
|
||||
}
|
||||
match self.tabs.get(self.current.val) {
|
||||
Some(t) =>
|
||||
for id, v in t.views {
|
||||
match @wp.meter_take(id) {
|
||||
for _, v in t.views {
|
||||
let mut loudest : Double? = None
|
||||
for m in v.row.members {
|
||||
match (@wp.meter_take(m), loudest) {
|
||||
(Some(p), Some(l)) => if p > l { loudest = Some(p) }
|
||||
(Some(p), None) => loudest = Some(p)
|
||||
(None, _) => ()
|
||||
}
|
||||
}
|
||||
match loudest {
|
||||
Some(peak) => {
|
||||
let next = @model.meter_step(
|
||||
v.shown_level,
|
||||
@@ -513,6 +546,30 @@ fn App::on_wp_event(self : App, ev : @wp.Event) -> Unit {
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Follow browser tab names (moonmix-tabs-host) and MPRIS titles.
|
||||
fn App::follow_tabs(self : App) -> Unit {
|
||||
fn apply(ev : @model.Event) {
|
||||
if self.state.apply(ev) {
|
||||
self.render()
|
||||
}
|
||||
}
|
||||
|
||||
fn names() {
|
||||
apply(TabNames(@model.merge_tab_files(@tabs.files())))
|
||||
}
|
||||
|
||||
@tabs.on_event(fn(ev) {
|
||||
match ev {
|
||||
Files => names()
|
||||
Media => apply(MediaTitles(@tabs.media_titles()))
|
||||
_ => ()
|
||||
}
|
||||
})
|
||||
@tabs.watch()
|
||||
names()
|
||||
}
|
||||
|
||||
///|
|
||||
fn build_ui() -> App {
|
||||
@gtk.load_css(CSS)
|
||||
@@ -670,6 +727,7 @@ fn main {
|
||||
if !@wp.start() {
|
||||
app.set_status("Cannot connect to PipeWire. Is pipewire running?")
|
||||
}
|
||||
app.follow_tabs()
|
||||
app.window.present()
|
||||
app.start_meter_timer()
|
||||
if screenshot is Some(prefix) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// The GUI.
|
||||
import {
|
||||
"geordi/moonmix/model",
|
||||
"geordi/moonmix/tabs",
|
||||
"geordi/moonmix/wp",
|
||||
"geordi/moonmix/gtk",
|
||||
"moonbitlang/core/env",
|
||||
|
||||
Reference in new issue
Block a user