snapserver has problems with sample rate on alsa:///
Describe the bug snapserver has problems with sample rate on alsa:///
Steps to Reproduce
- Add
source = alsa:///?name=aloop&device=hw:0,1,0&idle_threshold=10000&sampleformat=48000:16:2to your/etc/snapserver.conf. - Start snapserver fails (see log below)
- Same problem for
sampleformat=44100:16:2
Environment details
- OS: Fedora 42
- Snapcast version: v0.31.100
- self compiled
Attach logfile if applicable
Generate logs with snapclient --logfilter debug or snapserver --logging.filter debug if possible and paste them in the following codeblock
$ snapserver --logging.filter debug
2025-04-18 11-02-00.054 [Info] (Snapserver) Version 0.31.100
2025-04-18 11-02-00.054 [Info] (Snapserver) Stream plugin directory: "/usr/share/snapserver/plug-ins"
2025-04-18 11-02-00.054 [Info] (Snapserver) Adding source: alsa:///?name=aloop&device=hw:0,1,0&idle_threshold=10000&sampleformat=48000:16:2
2025-04-18 11-02-00.054 [Notice] (init) Settings file: "/home/media/.config/snapserver/server.json"
2025-04-18 11-02-00.057 [Info] (Avahi) Adding service 'Snapcast'
2025-04-18 11-02-00.059 [Info] (Snapserver) Using HTTP host name: unke
2025-04-18 11-02-00.063 [Debug] (StreamUri) StreamUri: alsa:///?name=aloop&device=hw:0,1,0&idle_threshold=10000&sampleformat=48000:16:2
2025-04-18 11-02-00.063 [Debug] (StreamUri) StreamUri.toString: alsa:///?device=hw:0,1,0&idle_threshold=10000&name=aloop&sampleformat=48000:16:2
2025-04-18 11-02-00.064 [Debug] (PcmStream) Chunk duration: 20 ms, frames: 960, size: 3840
2025-04-18 11-02-00.064 [Info] (PcmStream) PcmStream: aloop, sampleFormat: 48000:16:2
2025-04-18 11-02-00.064 [Debug] (PcmStream) Silence threshold percent: 0, silence threshold amplitude: 0
2025-04-18 11-02-00.064 [Info] (Server) Stream: {"fragment":"","host":"","path":"/","query":{"chunk_ms":"50","codec":"flac","device":"hw:0,1,0","idle_threshold":"10000","name":"aloop","sampleformat":"48000:16:2"},"raw":"alsa:///?chunk_ms=50&codec=flac&device=hw:0,1,0&idle_threshold=10000&name=aloop&sampleformat=48000:16:2","scheme":"alsa"}
2025-04-18 11-02-00.064 [Debug] (StreamUri) StreamUri: alsa:///?name=aloop&device=hw:0,1,0&idle_threshold=10000&sampleformat=48000:16:2
2025-04-18 11-02-00.064 [Debug] (StreamUri) StreamUri.toString: alsa:///?device=hw:0,1,0&idle_threshold=10000&name=aloop&sampleformat=48000:16:2
2025-04-18 11-02-00.064 [Debug] (AlsaStream) Start, sampleformat: 48000:16:2
2025-04-18 11-02-00.065 [Notice] (Server) Server::start: Can't set sample format: Invalid argument
2025-04-18 11-02-00.066 [Error] (Snapserver) Exception: Can't set sample format: Invalid argument
2025-04-18 11-02-00.067 [Notice] (Snapserver) Snapserver terminated.
This is delicate, as it is not the normal thing to happen. However, it happens to me regularly under certain conditions.
Perhaps it is related to using
pw-record -P stream.capture.sink=true --target alsa_output.platform-snd_aloop.0.analog-stereo - >/tmp/snapfifo &
before trying alsa (and after killing pw-record; see https://github.com/badaix/snapcast/issues/1371 for details).
Before getting this error, I sometimes observe the following:
...
2025-04-25 09-39-08.845 [Info] (ControlServer) Creating HTTP acceptor for address: ::, port: 1780
2025-04-25 09-39-08.845 [Info] (StreamServer) Creating stream acceptor for address: ::, port: 1704
2025-04-25 09-39-08.845 [Info] (Snapserver) Number of threads: 4, hw threads: 4
2025-04-25 09-39-08.845 [Info] (AlsaStream) Not enough data available: 1 ms, missing: 19 ms, needed: 20 ms
2025-04-25 09-39-08.845 [Info] (Server) onResync (aloop): 29 ms
2025-04-25 09-39-08.942 [Info] (PcmStream) State changed: default, state: idle => playing
2025-04-25 09-39-08.943 [Info] (Server) onStateChanged (default): playing
2025-04-25 09-39-08.955 [Error] (AlsaStream) Error reading PCM data: File descriptor in bad state (code: -77)
2025-04-25 09-39-08.958 [Error] (AlsaStream) Exception: Failed to start PCM: Input/output error
terminate called after throwing an instance of 'SnapException'
what(): Failed to start PCM: Input/output error
The button line might be that more information is needed under which conditions snd_pcm_hw_params_set_format might fail (in snapcast/server/streamreader/alsa_stream.cpp).
Same here
just add ALSA source with whatever
source = alsa://?name=ALSA&device=jack_capture&send_silence=true
and whala
2025-05-11 21:07:50 2025-05-11 21-07-50.094 [Info] (Snapserver) Adding source: alsa://?name=ALSA&device=jack_capture&send_silence=true
2025-05-11 21:07:50 2025-05-11 21-07-50.095 [Notice] (init) Settings file: "/root/.config/snapserver/server.json"
2025-05-11 21:07:50 2025-05-11 21-07-50.095 [Error] (Avahi) Failed to create client: Daemon not running
2025-05-11 21:07:50 2025-05-11 21-07-50.095 [Info] (Snapserver) Using HTTP host name: e69737a697be
2025-05-11 21:07:50 2025-05-11 21-07-50.098 [Info] (PcmStream) PcmStream: ALSA, sampleFormat: 48000:16:2
2025-05-11 21:07:50 2025-05-11 21-07-50.098 [Info] (Server) Stream: {"fragment":"","host":"","path":"","query":{"chunk_ms":"20","codec":"flac","device":"jack_capture","name":"ALSA","sampleformat":"48000:16:2","send_silence":"true"},"raw":"alsa://?chunk_ms=20&codec=flac&device=jack_capture&name=ALSA&sampleformat=48000:16:2&send_silence=true","scheme":"alsa"}
2025-05-11 21:07:50 2025-05-11 21-07-50.121 [Notice] (Server) Server::start: Can't set sample format: Invalid argument
2025-05-11 21:07:50 2025-05-11 21-07-50.128 [Error] (Snapserver) Exception: Can't set sample format: Invalid argument
2025-05-11 21:07:50 2025-05-11 21-07-50.129 [Notice] (Snapserver) Snapserver terminated.
OS debian: bookworm
Adding this here for reference, this is how you can check your sample rate of what is playing in on your device.
cat /proc/asound/card10/pcm0p/sub0/hw_params
output
access: MMAP_INTERLEAVED
format: FLOAT_LE
subformat: STD
channels: 2
rate: 44100 (44100/1)
period_size: 1323
buffer_size: 2646