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
@@ -1,5 +1,7 @@
|
||||
// Pure logic: volume curves, the audio-graph state reducer and the view
|
||||
// model the GUI renders. No FFI, so `moon test` runs it on any target.
|
||||
import {
|
||||
"moonbitlang/core/json",
|
||||
"moonbitlang/core/math",
|
||||
"moonbitlang/core/string",
|
||||
}
|
||||
@@ -24,6 +24,12 @@ pub const MIN_UI_CONTRAST : Double = 3.0
|
||||
|
||||
pub const NORM_PERCENT : Double = 100.0
|
||||
|
||||
pub const TAB_SETTLE_MS : Int64 = 3000
|
||||
|
||||
pub const TAB_SLOP_MS : Int64 = 400
|
||||
|
||||
pub const TAB_WINDOW_MS : Int64 = 1500
|
||||
|
||||
pub const WHEEL_STEP_PERCENT : Double = 2.0
|
||||
|
||||
pub fn accent_css(Rgb, Bool) -> String
|
||||
@@ -70,10 +76,16 @@ pub fn linear_to_percent(Double) -> Double
|
||||
|
||||
pub fn linked_percent(Array[Double]) -> Double
|
||||
|
||||
pub fn merge_tab_files(Array[TabFile]) -> Map[String, TabName]
|
||||
|
||||
pub fn meter_level(Double) -> Double
|
||||
|
||||
pub fn meter_step(Double, Double) -> Double
|
||||
|
||||
pub fn parse_media_titles(String) -> Map[Int32, String]
|
||||
|
||||
pub fn parse_tabs(String) -> Array[BrowserTab]?
|
||||
|
||||
pub fn percent_to_linear(Double) -> Double
|
||||
|
||||
pub fn readable_fg(Rgb) -> Rgb
|
||||
@@ -110,6 +122,13 @@ pub(all) struct AccentColors {
|
||||
}
|
||||
pub fn AccentColors::new(Rgb, Bool) -> Self
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) struct BrowserTab {
|
||||
id : Int32
|
||||
title : String
|
||||
audible : Bool
|
||||
}
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) enum ColorScheme {
|
||||
NoPreference
|
||||
@@ -138,6 +157,8 @@ pub(all) enum Event {
|
||||
Remove(Int32)
|
||||
Links(Array[Link])
|
||||
Defaults(sink~ : Int32, source~ : Int32)
|
||||
TabNames(Map[String, TabName])
|
||||
MediaTitles(Map[Int32, String])
|
||||
Reset
|
||||
}
|
||||
|
||||
@@ -160,11 +181,14 @@ pub(all) struct Node {
|
||||
bus : String
|
||||
app_binary : String
|
||||
app_id : String
|
||||
serial : String
|
||||
parent_pid : Int32
|
||||
running : Bool
|
||||
volumes : Array[Double]
|
||||
mute : Bool
|
||||
}
|
||||
pub fn Node::kind(Self) -> NodeKind
|
||||
pub fn Node::new(Int32, String, name? : String, description? : String, app_name? : String, media_name? : String, icon_hints? : Array[String], form_factor? : String, bus? : String, app_binary? : String, app_id? : String, volumes? : Array[Double], mute? : Bool) -> Self
|
||||
pub fn Node::new(Int32, String, name? : String, description? : String, app_name? : String, media_name? : String, icon_hints? : Array[String], form_factor? : String, bus? : String, app_binary? : String, app_id? : String, serial? : String, parent_pid? : Int32, running? : Bool, volumes? : Array[Double], mute? : Bool) -> Self
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) enum NodeKind {
|
||||
@@ -191,6 +215,7 @@ pub fn Rgb::to_hex(Self) -> String
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) struct Row {
|
||||
id : Int32
|
||||
members : Array[Int32]
|
||||
title : String
|
||||
subtitle : String
|
||||
icons : Array[String]
|
||||
@@ -224,11 +249,15 @@ pub(all) struct State {
|
||||
mut links : Array[Link]
|
||||
mut default_sink : Int32
|
||||
mut default_source : Int32
|
||||
mut tab_names : Map[String, TabName]
|
||||
mut media_titles : Map[Int32, String]
|
||||
}
|
||||
pub fn State::apply(Self, Event) -> Bool
|
||||
pub fn State::media_title(Self, Node) -> String?
|
||||
pub fn State::new() -> Self
|
||||
pub fn State::nodes_of(Self, NodeKind) -> Array[Node]
|
||||
pub fn State::stream_device(Self, Int32) -> Int32?
|
||||
pub fn State::tab_of(Self, Node) -> TabName?
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) enum Tab {
|
||||
@@ -244,6 +273,35 @@ pub fn Tab::icon_chain(Self) -> Array[String]
|
||||
pub fn Tab::label(Self) -> String
|
||||
pub fn Tab::node_kind(Self) -> NodeKind
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) struct TabFile {
|
||||
browser : Int32
|
||||
streams : Array[(String, Int32, String)]
|
||||
}
|
||||
pub fn TabFile::parse(String) -> Self?
|
||||
pub fn TabFile::to_text(Self) -> String
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) struct TabName {
|
||||
key : String
|
||||
title : String
|
||||
}
|
||||
|
||||
pub struct TabTracker {
|
||||
streams : Map[String, TrackedStream]
|
||||
tabs : Map[Int32, TrackedTab]
|
||||
mut got_tabs : Bool
|
||||
}
|
||||
pub fn TabTracker::matches(Self) -> Array[(String, Int32, String)]
|
||||
pub fn TabTracker::new() -> Self
|
||||
pub fn TabTracker::set_streams(Self, Array[String], Int64, initial? : Bool) -> Unit
|
||||
pub fn TabTracker::set_tabs(Self, Array[BrowserTab], Int64) -> Unit
|
||||
pub fn TabTracker::settle(Self, Int64, eliminate? : Bool) -> Unit
|
||||
|
||||
type TrackedStream
|
||||
|
||||
type TrackedTab
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) enum WheelRoute {
|
||||
Slider
|
||||
|
||||
+61
-1
@@ -40,6 +40,9 @@ pub(all) struct Node {
|
||||
bus : String // device.bus: "usb", "bluetooth", "pci", ...
|
||||
app_binary : String // application.process.binary
|
||||
app_id : String // pipewire.access.portal.app_id / application.id
|
||||
serial : String // object.serial, never reused (ids are)
|
||||
parent_pid : Int32 // parent of application.process.id; 0 if unknown
|
||||
running : Bool // streams: node state is "running"
|
||||
volumes : Array[Double] // linear, one per channel; empty = no volume
|
||||
mute : Bool
|
||||
}
|
||||
@@ -62,6 +65,9 @@ pub fn Node::new(
|
||||
bus~ : String = "",
|
||||
app_binary~ : String = "",
|
||||
app_id~ : String = "",
|
||||
serial~ : String = "",
|
||||
parent_pid~ : Int32 = 0,
|
||||
running~ : Bool = true,
|
||||
volumes~ : Array[Double] = [],
|
||||
mute~ : Bool = false,
|
||||
) -> Node {
|
||||
@@ -77,6 +83,9 @@ pub fn Node::new(
|
||||
bus,
|
||||
app_binary,
|
||||
app_id,
|
||||
serial,
|
||||
parent_pid,
|
||||
running,
|
||||
volumes,
|
||||
mute,
|
||||
}
|
||||
@@ -101,6 +110,10 @@ pub(all) enum Event {
|
||||
Links(Array[Link])
|
||||
/// Current default sink / source node ids (-1 = none).
|
||||
Defaults(sink~ : Int32, source~ : Int32)
|
||||
/// Streams matched to browser tabs, by object.serial.
|
||||
TabNames(Map[String, TabName])
|
||||
/// Title a browser is playing (MPRIS), by browser pid.
|
||||
MediaTitles(Map[Int32, String])
|
||||
/// Connection to PipeWire lost: forget everything.
|
||||
Reset
|
||||
}
|
||||
@@ -111,11 +124,20 @@ pub(all) struct State {
|
||||
mut links : Array[Link]
|
||||
mut default_sink : Int32
|
||||
mut default_source : Int32
|
||||
mut tab_names : Map[String, TabName]
|
||||
mut media_titles : Map[Int32, String]
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn State::new() -> State {
|
||||
{ nodes: {}, links: [], default_sink: -1, default_source: -1, }
|
||||
{
|
||||
nodes: {},
|
||||
links: [],
|
||||
default_sink: -1,
|
||||
default_source: -1,
|
||||
tab_names: Map([]),
|
||||
media_titles: Map([]),
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -152,6 +174,16 @@ pub fn State::apply(self : State, ev : Event) -> Bool {
|
||||
self.default_source = source
|
||||
changed
|
||||
}
|
||||
TabNames(names) => {
|
||||
let changed = names != self.tab_names
|
||||
self.tab_names = names
|
||||
changed
|
||||
}
|
||||
MediaTitles(titles) => {
|
||||
let changed = titles != self.media_titles
|
||||
self.media_titles = titles
|
||||
changed
|
||||
}
|
||||
Reset => {
|
||||
let changed = self.nodes.length() > 0 || self.links.length() > 0
|
||||
self.nodes.clear()
|
||||
@@ -226,6 +258,34 @@ pub fn display_title(n : Node) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// The browser tab a playback stream was matched to.
|
||||
pub fn State::tab_of(self : State, n : Node) -> TabName? {
|
||||
if n.kind() is PlaybackStream && n.serial != "" {
|
||||
self.tab_names.get(n.serial)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// What the stream's browser says it is playing (MPRIS): only when that can
|
||||
/// only mean this stream, i.e. it is the browser's one running stream.
|
||||
pub fn State::media_title(self : State, n : Node) -> String? {
|
||||
if !(n.kind() is PlaybackStream) || n.parent_pid == 0 || !n.running {
|
||||
return None
|
||||
}
|
||||
for _, other in self.nodes {
|
||||
if other.id != n.id &&
|
||||
other.kind() is PlaybackStream &&
|
||||
other.running &&
|
||||
other.parent_pid == n.parent_pid {
|
||||
return None
|
||||
}
|
||||
}
|
||||
self.media_titles.get(n.parent_pid)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Case-insensitive lexicographic comparison for display ordering.
|
||||
/// (MoonBit's built-in String compare orders by length first.)
|
||||
|
||||
+394
@@ -0,0 +1,394 @@
|
||||
// Which browser tab does a playback stream belong to?
|
||||
//
|
||||
// Chrome opens its audio streams per tab but gives them all the same
|
||||
// properties, and its extension API knows which tabs are audible but not
|
||||
// which stream they use. TabTracker links the two by time: a tab becomes
|
||||
// audible just after its stream starts running. Whatever stays ambiguous is
|
||||
// left unnamed; a wrong name is worse than none.
|
||||
|
||||
///|
|
||||
/// A tab reported by the browser extension.
|
||||
#derive(Eq, Debug)
|
||||
pub(all) struct BrowserTab {
|
||||
id : Int32
|
||||
title : String
|
||||
audible : Bool
|
||||
}
|
||||
|
||||
///|
|
||||
/// A tab a stream was matched to, as the mixer shows it. `key` is unique
|
||||
/// across browsers; streams with the same key share one row.
|
||||
#derive(Eq, Debug)
|
||||
pub(all) struct TabName {
|
||||
key : String
|
||||
title : String
|
||||
}
|
||||
|
||||
///|
|
||||
/// A tab turns audible at most this long after its stream starts.
|
||||
pub const TAB_WINDOW_MS : Int64 = 1500L
|
||||
|
||||
///|
|
||||
/// ... and, since the two reports take different paths, at most this long
|
||||
/// before we see the stream.
|
||||
pub const TAB_SLOP_MS : Int64 = 400L
|
||||
|
||||
///|
|
||||
/// Streams older than this may be matched by elimination.
|
||||
pub const TAB_SETTLE_MS : Int64 = 3000L
|
||||
|
||||
///|
|
||||
struct TrackedStream {
|
||||
since : Int64
|
||||
/// False when it was already running when tracking began.
|
||||
seen_start : Bool
|
||||
mut tab : Int32?
|
||||
/// Matched by elimination, not by time.
|
||||
mut weak : Bool
|
||||
/// Elimination was contradicted: do not try it again.
|
||||
mut doubt : Bool
|
||||
}
|
||||
|
||||
///|
|
||||
struct TrackedTab {
|
||||
mut title : String
|
||||
mut audible : Bool
|
||||
mut since : Int64
|
||||
mut seen_start : Bool
|
||||
}
|
||||
|
||||
///|
|
||||
pub struct TabTracker {
|
||||
streams : Map[String, TrackedStream] // by object.serial
|
||||
tabs : Map[Int32, TrackedTab]
|
||||
mut got_tabs : Bool
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn TabTracker::new() -> TabTracker {
|
||||
{ streams: Map([]), tabs: Map([]), got_tabs: false, }
|
||||
}
|
||||
|
||||
///|
|
||||
/// The set of running streams. `initial` for the first, already running set.
|
||||
/// A stream that stops loses its tab: Chrome reuses idle streams.
|
||||
pub fn TabTracker::set_streams(
|
||||
self : TabTracker,
|
||||
serials : Array[String],
|
||||
now : Int64,
|
||||
initial~ : Bool = false,
|
||||
) -> Unit {
|
||||
for s in self.streams.keys().to_array() {
|
||||
if !serials.contains(s) {
|
||||
self.streams.remove(s)
|
||||
}
|
||||
}
|
||||
for s in serials {
|
||||
if !self.streams.contains(s) {
|
||||
self.streams.set(s, {
|
||||
since: now,
|
||||
seen_start: !initial,
|
||||
tab: None,
|
||||
weak: false,
|
||||
doubt: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// All tabs that are or have been audible and are still open.
|
||||
pub fn TabTracker::set_tabs(
|
||||
self : TabTracker,
|
||||
tabs : Array[BrowserTab],
|
||||
now : Int64,
|
||||
) -> Unit {
|
||||
let initial = !self.got_tabs
|
||||
self.got_tabs = true
|
||||
for id in self.tabs.keys().to_array() {
|
||||
if !tabs.any(fn(t) { t.id == id }) {
|
||||
self.tabs.remove(id)
|
||||
for _, s in self.streams {
|
||||
if s.tab == Some(id) {
|
||||
s.tab = None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for t in tabs {
|
||||
match self.tabs.get(t.id) {
|
||||
Some(old) => {
|
||||
old.title = t.title
|
||||
if t.audible && !old.audible {
|
||||
old.since = now
|
||||
old.seen_start = true
|
||||
}
|
||||
old.audible = t.audible
|
||||
}
|
||||
None =>
|
||||
self.tabs.set(t.id, {
|
||||
title: t.title,
|
||||
audible: t.audible,
|
||||
since: now,
|
||||
seen_start: !initial,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn TabTracker::has_stream(self : TabTracker, tab : Int32) -> Bool {
|
||||
for _, s in self.streams {
|
||||
if s.tab == Some(tab) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
///|
|
||||
fn in_window(s : TrackedStream, t : TrackedTab) -> Bool {
|
||||
let d = t.since - s.since
|
||||
s.seen_start && t.seen_start && d >= -TAB_SLOP_MS && d <= TAB_WINDOW_MS
|
||||
}
|
||||
|
||||
///|
|
||||
/// Match what can be matched at `now`. `eliminate` must be false when tabs
|
||||
/// of the same browser may be missing (another profile has its own list).
|
||||
pub fn TabTracker::settle(
|
||||
self : TabTracker,
|
||||
now : Int64,
|
||||
eliminate~ : Bool = true,
|
||||
) -> Unit {
|
||||
if !self.got_tabs {
|
||||
return
|
||||
}
|
||||
if !eliminate {
|
||||
for _, s in self.streams {
|
||||
if s.weak {
|
||||
s.tab = None
|
||||
s.weak = false
|
||||
}
|
||||
}
|
||||
}
|
||||
let audible : Array[Int32] = []
|
||||
let free_tabs : Array[Int32] = []
|
||||
for id, t in self.tabs {
|
||||
if t.audible {
|
||||
audible.push(id)
|
||||
if !self.has_stream(id) {
|
||||
free_tabs.push(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
// By time: the stream's only candidate is this tab. Several streams may
|
||||
// start with one tab (media plus Web Audio), never the reverse.
|
||||
let found : Array[(TrackedStream, Int32)] = []
|
||||
for _, s in self.streams {
|
||||
if s.tab is None && now - s.since >= TAB_WINDOW_MS {
|
||||
let near = audible.filter(fn(id) {
|
||||
match self.tabs.get(id) {
|
||||
Some(t) => in_window(s, t)
|
||||
None => false
|
||||
}
|
||||
})
|
||||
// A tab that already has a stream comes second.
|
||||
let free = near.filter(fn(id) { free_tabs.contains(id) })
|
||||
let cands = if free.length() > 0 { free } else { near }
|
||||
if cands is [id] {
|
||||
found.push((s, id))
|
||||
}
|
||||
}
|
||||
}
|
||||
for f in found {
|
||||
f.0.tab = Some(f.1)
|
||||
f.0.weak = false
|
||||
}
|
||||
// A tab has been audible for a while with no stream left for it: one of
|
||||
// the eliminated matches must be wrong, so drop them all.
|
||||
let left = free_tabs.filter(fn(id) { !self.has_stream(id) })
|
||||
let free_streams : Array[TrackedStream] = []
|
||||
let mut pending = false
|
||||
for _, s in self.streams {
|
||||
if s.tab is None {
|
||||
free_streams.push(s)
|
||||
if now - s.since < TAB_SETTLE_MS {
|
||||
pending = true
|
||||
}
|
||||
}
|
||||
}
|
||||
let overdue = left.any(fn(id) {
|
||||
match self.tabs.get(id) {
|
||||
Some(t) => now - t.since >= TAB_SETTLE_MS
|
||||
None => false
|
||||
}
|
||||
})
|
||||
if overdue && free_streams.length() == 0 {
|
||||
for _, s in self.streams {
|
||||
if s.weak {
|
||||
s.tab = None
|
||||
s.weak = false
|
||||
s.doubt = true
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
if !eliminate || pending {
|
||||
return
|
||||
}
|
||||
let settled = left.all(fn(id) {
|
||||
match self.tabs.get(id) {
|
||||
Some(t) => now - t.since >= TAB_SETTLE_MS
|
||||
None => false
|
||||
}
|
||||
})
|
||||
if !settled {
|
||||
return
|
||||
}
|
||||
let target = if audible is [only] {
|
||||
// A single tab makes sound: every stream left is its own.
|
||||
Some(only)
|
||||
} else if left is [only] && free_streams.length() == 1 {
|
||||
Some(only)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
match target {
|
||||
Some(id) =>
|
||||
for s in free_streams {
|
||||
if !s.doubt {
|
||||
s.tab = Some(id)
|
||||
s.weak = true
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Matched streams: (object.serial, tab id, tab title).
|
||||
pub fn TabTracker::matches(self : TabTracker) -> Array[(String, Int32, String)] {
|
||||
let out = []
|
||||
for serial, s in self.streams {
|
||||
match s.tab {
|
||||
Some(id) =>
|
||||
match self.tabs.get(id) {
|
||||
Some(t) => out.push((serial, id, t.title))
|
||||
None => ()
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
///|
|
||||
/// Parse a message from the extension:
|
||||
/// `{"tabs":[{"id":1,"title":"...","audible":true}]}`. None if malformed.
|
||||
pub fn parse_tabs(text : String) -> Array[BrowserTab]? {
|
||||
let json = @json.parse(text) catch { _ => return None }
|
||||
guard json is Object({ "tabs": Array(items), .. }) else { return None }
|
||||
let out = []
|
||||
for item in items {
|
||||
guard item
|
||||
is Object(
|
||||
{
|
||||
"id": Number(id, ..),
|
||||
"title": String(title),
|
||||
"audible": True
|
||||
| False as audible,
|
||||
..
|
||||
}
|
||||
) else {
|
||||
return None
|
||||
}
|
||||
out.push({ id: id.to_int(), title, audible: audible is True, })
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
///|
|
||||
/// What one tab host publishes: its browser's pid and its matches.
|
||||
#derive(Eq, Debug)
|
||||
pub(all) struct TabFile {
|
||||
browser : Int32
|
||||
streams : Array[(String, Int32, String)] // serial, tab id, title
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn TabFile::to_text(self : TabFile) -> String {
|
||||
let streams : Array[Json] = self.streams.map(fn(s) {
|
||||
let o : Json = {
|
||||
"serial": Json::string(s.0),
|
||||
"tab": Json::number(s.1.to_double()),
|
||||
"title": Json::string(s.2),
|
||||
}
|
||||
o
|
||||
})
|
||||
let o : Json = {
|
||||
"browser": Json::number(self.browser.to_double()),
|
||||
"streams": Json::array(streams),
|
||||
}
|
||||
o.stringify()
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn TabFile::parse(text : String) -> TabFile? {
|
||||
let json = @json.parse(text) catch { _ => return None }
|
||||
guard json
|
||||
is Object({ "browser": Number(pid, ..), "streams": Array(items), .. }) else {
|
||||
return None
|
||||
}
|
||||
let streams = []
|
||||
for item in items {
|
||||
guard item
|
||||
is Object(
|
||||
{
|
||||
"serial": String(serial),
|
||||
"tab": Number(tab, ..),
|
||||
"title": String(title),
|
||||
..
|
||||
}
|
||||
) else {
|
||||
return None
|
||||
}
|
||||
streams.push((serial, tab.to_int(), title))
|
||||
}
|
||||
Some({ browser: pid.to_int(), streams, })
|
||||
}
|
||||
|
||||
///|
|
||||
/// Names for the mixer from every host's file. A stream that two hosts
|
||||
/// claim (two profiles of one browser) is ambiguous and stays unnamed.
|
||||
pub fn merge_tab_files(files : Array[TabFile]) -> Map[String, TabName] {
|
||||
let out : Map[String, TabName] = Map([])
|
||||
let clash : Map[String, Bool] = Map([])
|
||||
for f in files {
|
||||
for s in f.streams {
|
||||
if out.contains(s.0) {
|
||||
clash.set(s.0, true)
|
||||
}
|
||||
out.set(s.0, { key: "\{f.browser}:\{s.1}", title: s.2, })
|
||||
}
|
||||
}
|
||||
for s, _ in clash {
|
||||
out.remove(s)
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
///|
|
||||
/// Parse "pid\ttitle" lines.
|
||||
pub fn parse_media_titles(text : String) -> Map[Int32, String] {
|
||||
let out : Map[Int32, String] = Map([])
|
||||
for line in text.split("\n") {
|
||||
match line.find("\t") {
|
||||
Some(i) => {
|
||||
let pid = @string.parse_int(line[:i]) catch { _ => continue }
|
||||
out.set(pid, line[i + 1:].to_owned())
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
///|
|
||||
fn tab(id : Int32, title : String, audible~ : Bool = true) -> @model.BrowserTab {
|
||||
{ id, title, audible, }
|
||||
}
|
||||
|
||||
///|
|
||||
/// Names by serial, sorted for comparison.
|
||||
fn names(t : @model.TabTracker) -> String {
|
||||
let out = t.matches().map(fn(m) { "\{m.0}=\{m.2}" })
|
||||
out.sort_by(@model.compare_text)
|
||||
out.join(" ")
|
||||
}
|
||||
|
||||
///|
|
||||
fn fresh() -> @model.TabTracker {
|
||||
let t = @model.TabTracker::new()
|
||||
t.set_streams([], 0L, initial=true)
|
||||
t.set_tabs([], 0L)
|
||||
t
|
||||
}
|
||||
|
||||
///|
|
||||
test "tabs: matched by when they start, also long afterwards" {
|
||||
let t = fresh()
|
||||
t.set_streams(["1"], 1000L)
|
||||
t.set_tabs([tab(10, "A")], 1300L)
|
||||
t.settle(1500L)
|
||||
inspect(names(t), content="")
|
||||
t.set_streams(["1", "2"], 5000L)
|
||||
t.set_tabs([tab(10, "A"), tab(11, "B")], 5400L)
|
||||
t.settle(9000L)
|
||||
inspect(names(t), content="1=A 2=B")
|
||||
// A quiet passage and a new title do not lose the match.
|
||||
t.set_tabs([tab(10, "A2", audible=false), tab(11, "B")], 10000L)
|
||||
t.settle(20000L)
|
||||
inspect(names(t), content="1=A2 2=B")
|
||||
}
|
||||
|
||||
///|
|
||||
test "tabs: starting together is ambiguous" {
|
||||
let t = fresh()
|
||||
t.set_streams(["1", "2"], 1000L)
|
||||
t.set_tabs([tab(10, "A"), tab(11, "B")], 1300L)
|
||||
t.settle(20000L)
|
||||
inspect(names(t), content="")
|
||||
}
|
||||
|
||||
///|
|
||||
test "tabs: already playing at start" {
|
||||
let t = @model.TabTracker::new()
|
||||
t.set_streams(["1", "2"], 0L, initial=true)
|
||||
t.set_tabs([tab(10, "A"), tab(11, "B")], 0L)
|
||||
t.settle(20000L)
|
||||
inspect(names(t), content="")
|
||||
// With a single tab there is no doubt.
|
||||
let t = @model.TabTracker::new()
|
||||
t.set_streams(["1", "2"], 0L, initial=true)
|
||||
t.set_tabs([tab(10, "A")], 0L)
|
||||
t.settle(20000L)
|
||||
inspect(names(t), content="1=A 2=A")
|
||||
}
|
||||
|
||||
///|
|
||||
test "tabs: a second stream of the only tab joins it" {
|
||||
let t = fresh()
|
||||
t.set_streams(["1"], 1000L)
|
||||
t.set_tabs([tab(10, "A")], 1300L)
|
||||
t.settle(4000L)
|
||||
t.set_streams(["1", "2"], 5000L)
|
||||
t.settle(6000L)
|
||||
inspect(names(t), content="1=A")
|
||||
t.settle(9000L)
|
||||
inspect(names(t), content="1=A 2=A")
|
||||
}
|
||||
|
||||
///|
|
||||
test "tabs: media and Web Audio of one tab, next to another tab" {
|
||||
let t = fresh()
|
||||
t.set_streams(["1"], 1000L)
|
||||
t.set_tabs([tab(10, "A")], 1300L)
|
||||
t.settle(4000L)
|
||||
t.set_streams(["1", "2"], 9000L)
|
||||
t.set_streams(["1", "2", "3"], 9100L)
|
||||
t.set_tabs([tab(10, "A"), tab(11, "B")], 9300L)
|
||||
t.settle(10550L)
|
||||
t.settle(11000L)
|
||||
inspect(names(t), content="1=A 2=B 3=B")
|
||||
}
|
||||
|
||||
///|
|
||||
test "tabs: a contradicted elimination is dropped for good" {
|
||||
let t = fresh()
|
||||
t.set_streams(["1"], 1000L)
|
||||
t.set_tabs([tab(10, "A")], 1300L)
|
||||
t.settle(4000L)
|
||||
// Stream 2 is a silent tab's; B is heard only much later.
|
||||
t.set_streams(["1", "2"], 5000L)
|
||||
t.settle(9000L)
|
||||
inspect(names(t), content="1=A 2=A")
|
||||
t.set_tabs([tab(10, "A"), tab(11, "B")], 20000L)
|
||||
t.settle(24000L)
|
||||
inspect(names(t), content="1=A")
|
||||
t.settle(30000L)
|
||||
inspect(names(t), content="1=A")
|
||||
}
|
||||
|
||||
///|
|
||||
test "tabs: a stopped stream is forgotten, a closed tab too" {
|
||||
let t = fresh()
|
||||
t.set_streams(["1"], 1000L)
|
||||
t.set_tabs([tab(10, "A")], 1300L)
|
||||
t.settle(4000L)
|
||||
t.set_streams([], 5000L)
|
||||
inspect(names(t), content="")
|
||||
t.set_streams(["1"], 6000L)
|
||||
t.set_tabs([tab(11, "B")], 6300L)
|
||||
t.settle(9000L)
|
||||
inspect(names(t), content="1=B")
|
||||
t.set_tabs([], 10000L)
|
||||
inspect(names(t), content="")
|
||||
}
|
||||
|
||||
///|
|
||||
test "tabs: no elimination next to another profile" {
|
||||
let t = @model.TabTracker::new()
|
||||
t.set_streams(["1"], 0L, initial=true)
|
||||
t.set_tabs([tab(10, "A")], 0L)
|
||||
t.settle(9000L)
|
||||
inspect(names(t), content="1=A")
|
||||
t.settle(9500L, eliminate=false)
|
||||
inspect(names(t), content="")
|
||||
}
|
||||
|
||||
///|
|
||||
test "tabs: messages and files" {
|
||||
let parsed = @model.parse_tabs(
|
||||
"{\"tabs\":[{\"id\":7,\"title\":\"a \\\"b\\\" é\",\"audible\":true}]}",
|
||||
)
|
||||
assert_true(parsed == Some([tab(7, "a \"b\" é")]))
|
||||
assert_true(@model.parse_tabs("nope") is None)
|
||||
assert_true(@model.parse_tabs("{\"tabs\":[{\"id\":\"7\"}]}") is None)
|
||||
let file : @model.TabFile = {
|
||||
browser: 5,
|
||||
streams: [("84", 7, "x\n\"y\""), ("85", 8, "z")],
|
||||
}
|
||||
assert_true(@model.TabFile::parse(file.to_text()) == Some(file))
|
||||
assert_false(file.to_text().contains("\n"))
|
||||
assert_true(@model.TabFile::parse("{}") is None)
|
||||
let other : @model.TabFile = { browser: 5, streams: [("85", 9, "w")], }
|
||||
let merged = @model.merge_tab_files([file, other])
|
||||
inspect(merged.length(), content="1")
|
||||
inspect(merged.get("84").unwrap().key, content="5:7")
|
||||
let titles = @model.parse_media_titles("12\tSong\nbad\n")
|
||||
assert_true(titles.get(12) == Some("Song"))
|
||||
inspect(titles.length(), content="1")
|
||||
}
|
||||
|
||||
///|
|
||||
test "tabs: rows are named and grouped" {
|
||||
let s = @model.State::new()
|
||||
for id in [60, 61, 62] {
|
||||
let _ = s.apply(
|
||||
Upsert(
|
||||
@model.Node::new(
|
||||
id,
|
||||
"Stream/Output/Audio",
|
||||
app_name="Google Chrome",
|
||||
media_name="Playback",
|
||||
serial="\{id + 100}",
|
||||
parent_pid=5,
|
||||
volumes=[1.0],
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
let name : @model.TabName = { key: "5:1", title: "Music", }
|
||||
assert_true(s.apply(TabNames({ "160": name, "162": name })))
|
||||
assert_false(s.apply(TabNames({ "160": name, "162": name })))
|
||||
// Three running streams: a browser-wide title says nothing about one.
|
||||
let _ = s.apply(MediaTitles({ 5: "Song" }))
|
||||
let rows = @model.rows(s, Playback)
|
||||
inspect(
|
||||
rows
|
||||
.map(fn(r) { "\{r.title}/\{r.subtitle}/\{r.members.length()}" })
|
||||
.join(", "),
|
||||
content="Music/Google Chrome/2, Google Chrome/Playback/1",
|
||||
)
|
||||
let _ = s.apply(Remove(60))
|
||||
let _ = s.apply(Remove(62))
|
||||
inspect(@model.rows(s, Playback)[0].subtitle, content="Song")
|
||||
}
|
||||
+37
-6
@@ -58,6 +58,9 @@ pub(all) struct DeviceChoice {
|
||||
#derive(Eq, Debug)
|
||||
pub(all) struct Row {
|
||||
id : Int32
|
||||
/// Nodes this row controls: its own id first, then, for a browser tab, the
|
||||
/// tab's other streams.
|
||||
members : Array[Int32]
|
||||
title : String
|
||||
subtitle : String
|
||||
/// Icon-theme names to try in order (see icons.mbt).
|
||||
@@ -91,12 +94,33 @@ pub fn rows(state : State, tab : Tab) -> Array[Row] {
|
||||
.map(fn(d) { { id: d.id, name: d.name, label: display_title(d), } })
|
||||
None => []
|
||||
}
|
||||
state
|
||||
.nodes_of(kind)
|
||||
.map(fn(n) {
|
||||
// Streams of one browser tab share a row.
|
||||
let groups : Map[String, Array[Int32]] = Map([])
|
||||
let nodes = state
|
||||
.nodes_of(kind)
|
||||
.filter(fn(n) {
|
||||
match state.tab_of(n) {
|
||||
Some(t) =>
|
||||
match groups.get(t.key) {
|
||||
Some(g) => {
|
||||
g.push(n.id)
|
||||
false
|
||||
}
|
||||
None => {
|
||||
groups.set(t.key, [n.id])
|
||||
true
|
||||
}
|
||||
}
|
||||
None => true
|
||||
}
|
||||
})
|
||||
nodes.map(fn(n) {
|
||||
let is_stream = device_kind is Some(_)
|
||||
let subtitle = if is_stream {
|
||||
n.media_name
|
||||
let tab = state.tab_of(n)
|
||||
let subtitle = if tab is Some(_) {
|
||||
display_title(n)
|
||||
} else if is_stream {
|
||||
state.media_title(n).unwrap_or(n.media_name)
|
||||
} else if n.name != display_title(n) {
|
||||
n.name
|
||||
} else {
|
||||
@@ -125,7 +149,14 @@ pub fn rows(state : State, tab : Tab) -> Array[Row] {
|
||||
}
|
||||
{
|
||||
id: n.id,
|
||||
title: display_title(n),
|
||||
members: match tab {
|
||||
Some(t) => groups.get(t.key).unwrap_or([n.id])
|
||||
None => [n.id]
|
||||
},
|
||||
title: match tab {
|
||||
Some(t) if t.title != "" => t.title
|
||||
_ => display_title(n)
|
||||
},
|
||||
subtitle,
|
||||
icons: if is_stream {
|
||||
stream_icon_chain(n)
|
||||
|
||||
Reference in new issue
Block a user