Repository navigation
fix(audio): volume keys move the default sink instead of the active call stream #14806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -465,16 +465,56 @@ Item { | |
| // sink it feeds on every press, from the live routing. An ALSA sink is its | ||
| // own physical sink, so only then do the keys act here; any other default | ||
| // sink falls back to the script. | ||
| // | ||
| // During a call the headset is a separate, non-default sink, so Linux routes | ||
| // the volume keys to the default sink and they move the speakers while the | ||
| // call plays into the headset. To make the keys follow the stream that is | ||
| // actually playing, the active sink is resolved through | ||
| // omarchy-audio-output-sink --active (an uncorked stream on a non-default | ||
| // output wins, else the default). The shell cannot see which sink each | ||
| // stream is linked to, so the resolution runs in a process, refreshed when | ||
| // the default sink changes, on a timer, and on each press. When the active | ||
| // sink is not the default, handleVolumeKey defers to the script, which | ||
| // repeats the same resolution and steps that sink. | ||
| readonly property var defaultSink: Pipewire.defaultAudioSink | ||
| readonly property var volumeSink: defaultSink && String(defaultSink.name).indexOf("alsa_output.") === 0 ? defaultSink : null | ||
| property double lastMuteToggle: 0 | ||
| property string activeSinkName: "" | ||
|
|
||
| function refreshActiveSink() { | ||
| if (!activeSinkProc.running) activeSinkProc.running = true | ||
| } | ||
|
|
||
| onDefaultSinkChanged: refreshActiveSink() | ||
|
|
||
| Process { | ||
| id: activeSinkProc | ||
| command: ["omarchy-audio-output-sink", "--active"] | ||
| stdout: StdioCollector { | ||
| waitForEnd: true | ||
| onStreamFinished: root.activeSinkName = String(text).trim() | ||
| } | ||
| } | ||
|
|
||
| Timer { | ||
| interval: 5000 | ||
| running: true | ||
| repeat: true | ||
| triggeredOnStart: true | ||
| onTriggered: root.refreshActiveSink() | ||
| } | ||
|
|
||
| // Returns false when the default sink is not one to control here, so the | ||
| // caller falls back to the script. | ||
| // Returns false when the default sink is not one to control here, or when an | ||
| // active stream plays on another output, so the caller falls back to the | ||
| // script. | ||
| function handleVolumeKey(action) { | ||
| var audio = volumeSink && volumeSink.audio | ||
| if (!audio) return false | ||
|
|
||
| if (activeSinkName && activeSinkName !== String(defaultSink.name)) return false | ||
|
|
||
| refreshActiveSink() | ||
|
Comment on lines
+514
to
+516
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a headset call starts between polls, |
||
|
|
||
| var step = MediaModel.volumeKeyStep(action, Math.round(audio.volume * 100), audio.muted) | ||
| if (!step) return false | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" | ||
|
|
||
| require_command jq | ||
|
|
||
| test_home=$(mktemp -d) | ||
| test_bin=$(mktemp -d) | ||
|
|
||
| cleanup() { | ||
| rm -rf "$test_home" "$test_bin" | ||
| } | ||
| trap cleanup EXIT | ||
|
|
||
| # Stub pactl for the JSON the resolver reads. The --active path only needs the | ||
| # default sink and the `-f json` listings; the results below are ALSA sinks, so | ||
| # the resolver short-circuits before its text-format DSP/EasyEffects walk. | ||
| cat >"$test_bin/pactl" <<'STUB' | ||
| #!/bin/bash | ||
| format="" | ||
| if [[ ${1:-} == "-f" ]]; then | ||
| format="${2:-}" | ||
| shift 2 | ||
| fi | ||
| case "$format|$1|$2" in | ||
| "|get-default-sink|") cat "$TEST_DATA/default-sink" ;; | ||
| "json|list|sinks") cat "$TEST_DATA/sinks.json" ;; | ||
| "json|list|sink-inputs") cat "$TEST_DATA/sink-inputs.json" ;; | ||
| *) exit 1 ;; | ||
| esac | ||
| STUB | ||
| chmod +x "$test_bin/pactl" | ||
|
|
||
| default=alsa_output.pci-0000_00_1f.3.analog-stereo | ||
| jabra=alsa_output.usb-Jabra_Link_380-00.analog-stereo | ||
| monitor=alsa_output.usb-Jabra_Link_380-00.analog-stereo.monitor | ||
| hdmi=alsa_output.pci-0000_00_1f.3.hdmi-stereo | ||
|
|
||
| resolve() { | ||
| HOME="$test_home" XDG_CONFIG_HOME="$test_home/.config" TEST_DATA="$test_home/data" \ | ||
| PATH="$test_bin:$PATH" bash "$ROOT/bin/omarchy-audio-output-sink" "$@" | ||
| } | ||
|
|
||
| reset_scenario() { | ||
| rm -rf "$test_home/data" | ||
| mkdir -p "$test_home/data" | ||
| printf '%s\n' "$default" >"$test_home/data/default-sink" | ||
| printf '[{"index":70,"name":"%s"},{"index":105,"name":"%s"},{"index":106,"name":"%s"},{"index":107,"name":"%s"}]\n' \ | ||
| "$default" "$jabra" "$monitor" "$hdmi" >"$test_home/data/sinks.json" | ||
| } | ||
|
|
||
| # With nothing playing, --active answers the default sink. | ||
| reset_scenario | ||
| printf '[]\n' >"$test_home/data/sink-inputs.json" | ||
| [[ $(resolve --active) == "$default" ]] || fail "an idle graph answers the default sink" | ||
| pass "--active answers the default sink when nothing is playing" | ||
|
|
||
| # An uncorked stream on another output wins over the default. | ||
| reset_scenario | ||
| printf '[{"index":12,"sink":105,"corked":false,"properties":{}}]\n' >"$test_home/data/sink-inputs.json" | ||
| [[ $(resolve --active) == "$jabra" ]] || fail "an active stream on another output answers that output" | ||
| pass "--active follows an uncorked stream onto its sink" | ||
|
|
||
| # A stream on the default sink keeps the default. | ||
| reset_scenario | ||
| printf '[{"index":12,"sink":70,"corked":false,"properties":{}}]\n' >"$test_home/data/sink-inputs.json" | ||
| [[ $(resolve --active) == "$default" ]] || fail "a stream on the default sink answers the default" | ||
| pass "--active keeps the default sink when only it is playing" | ||
|
|
||
| # A monitor sink is not an output. | ||
| reset_scenario | ||
| printf '[{"index":12,"sink":106,"corked":false,"properties":{}}]\n' >"$test_home/data/sink-inputs.json" | ||
| [[ $(resolve --active) == "$default" ]] || fail "a monitor sink is not followed" | ||
| pass "--active skips a monitor sink" | ||
|
|
||
| # A corked stream is not playing. | ||
| reset_scenario | ||
| printf '[{"index":12,"sink":105,"corked":true,"properties":{}}]\n' >"$test_home/data/sink-inputs.json" | ||
| [[ $(resolve --active) == "$default" ]] || fail "a corked stream is not followed" | ||
| pass "--active ignores a corked stream" | ||
|
|
||
| # A call (media.role phone) outranks background playback on another output. | ||
| reset_scenario | ||
| printf '[{"index":12,"sink":105,"corked":false,"properties":{}},{"index":13,"sink":107,"corked":false,"properties":{"media.role":"phone"}}]\n' >"$test_home/data/sink-inputs.json" | ||
| [[ $(resolve --active) == "$hdmi" ]] || fail "a communication stream outranks background playback" | ||
| pass "--active prefers a call over background playback" | ||
|
|
||
| # With no call, an ordinary playback stream is still followed. | ||
| reset_scenario | ||
| printf '[{"index":12,"sink":105,"corked":false,"properties":{"media.role":"music"}}]\n' >"$test_home/data/sink-inputs.json" | ||
| [[ $(resolve --active) == "$jabra" ]] || fail "an uncorked non-call stream is followed when alone" | ||
| pass "--active follows ordinary playback when no call is playing" | ||
|
|
||
| # An explicit sink name still wins over --active. | ||
| reset_scenario | ||
| printf '[{"index":12,"sink":105,"corked":false,"properties":{}}]\n' >"$test_home/data/sink-inputs.json" | ||
| [[ $(resolve --active "$hdmi") == "$hdmi" ]] || fail "an explicit sink name wins over --active" | ||
| pass "--active with an explicit sink resolves that sink" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A call on the default output loses to background music on another output. The resolver puts the call first, but this unconditional
continuediscards it because it uses the default output. The loop then returns the music output, so volume and mute keys control music rather than the call. Keep the preferred call output even when it is the default, and add this mixed-output case to the tests.