On Thu, Dec 09, 2021 at 10:21:56AM +0100, Alexandre Ratchov wrote: > On Wed, Dec 08, 2021 at 10:30:08PM +0000, Klemens Nanni wrote: > > Following https://www.openbsd.org/faq/faq13.html#usbaudio and reading > > sndiod(8)'s > > > > -F device > > Specify an alternate device to use. If it doesn't work, the one > > given with the last -f or -F options will be used. For > > instance, > > specifying a USB device following a PCI device allows sndiod to > > use the USB one preferably when it's connected and to fall back > > to the PCI one when it's disconnected. Alternate devices may be > > switched with the server.device control of the sndioctl(1) > > utility. > > > > I configured things as follows in order to play audio via USB and > > fall back to internal sound if USB is not available: > > > > $ rcctl get sndiod flags > > -f rsnd/0 -F rsnd/1 > > > > Plugging in an USB headset and restarting sndiod or forcing the device > > with `sndioctl server.device=1' then plays sound via USB. > > > > Unplugging the device makes playback fall back to internal sound. > > > > But plugging USB back in does not prefer USB to internal as I'd expect > > now. I am currently resorting to the following hotplugd(8) script to > > always select the USB sound device whenever I plug it in: > > > > #!/bin/ksh > > set -Cefu -o pipefail > > > > readonly DEVCLASS=$1 DEVNAME=$2 > > typeset -i devid > > > > case "${DEVCLASS}-${DEVNAME}" in > > 0-audio*) # switch sndio(4) to USB headset when plugging it in > > devid=${DEVNAME#audio} > > sndioctl server.device=${devid} > > ;; > > esac > > > > IFAIU, audio devices always match the audio[0-9] pattern, so testing > the device class is not necessary, is it? FWIW, I use a similar > script. > > > I'd expect sndiod to *always* use USB whenever possible. > > > > Is this uni-directional behaviour of sndiod intentional/by-design? > > sndiod doesn't use directly hotplug(4), so it can't obtain by itself a > notification when a new device is plugged in order to switch to > it. Using hotplugd(8) is the right way to make audio hotplug work, > both daemons complete each other. > > When sndiod needs the audio device (i.e. when the first client > connects), it will try all alternate devices (reverse -F options > order, the USB headset is first in your case). This gives a false > impression of hotplug support (indeed, you plug the USB headset, start > an audio player and it just works!). But that's just a side effect of > device priority/fail-over: if the internal device is already open when > you plug the USB headset, sndiod won't switch to it until it needs to > reopen the device. > > Certain programs (including browsers), tend to keep the device open > even when they remain silent. So when actual playback starts sndiod > doesn't need to open a device and doesn't "see" the new USB headset. I > guess that's what happens on your system, but hotplugd(8) handles > this. > > When devices are unplugged, we don't need hotplug because the device > stops working (input/output error) and sndiod switch to the next one > of the fail-over list. > > > If so, can we clarify the manual? > > Sure. While -F option description seems exact, maybe we need an extra > paragraph or FAQ entry to explain how to use it with other tools like > hotplugd and sndioctl. What about this wording? > > HOT-PLUG SUPPORT > If a device is unplugged while in use, sndiod will attempt to switch to > one of the alternate devices (-F), if any. This is seamless to programs > connected to sndiod. > > Later, when the device is connected again, the server.device control of > the sndioctl(1) utility could be used to switch back to it, without the > need to restart all audio programs. This last step could be automated > using hotplugd(8). For instance, if sndiod is started with: > > $ sndiod -f rsnd/0 -F rsnd/1 -F rsnd/2 -F rsnd/3 > > then, the following hotplugd(8) attach script could be used to > automatically switch to the last connected device: > > #!/bin/sh > > DEVNAME=$2 > > case $DEVNAME in > audio[0-3]) > sndioctl server.device=${DEVNAME#audio} > ;; > esac > > Any opinions where to put such information? >
hi. i guess the info is more relevant to sndiod. you could just add your text to the existing EXAMPLES section i think. jmc