AlexxIT/go2rtc · error

[alsa] prepare

Error message

[alsa] prepare: %w

What it means

The SNDRV_PCM_IOCTL_PREPARE ioctl failed, so the ALSA PCM device could not be moved into the PREPARED state after hardware and software parameters were set. Wraps the errno; without preparation the device cannot start reading/writing. Called by Start and AddTrack.

Solutions

  1. Close and reopen the ALSA device, then re-run SetHWParams
  2. Ensure no other process holds the PCM device
  3. Check dmesg for ALSA driver errors
Defensive patterns

Strategy: retry

When it happens

Trigger: Thrown at pkg/alsa/device/device_linux.go:163 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of AlexxIT/go2rtc@c245815e75 (2026-09-07). Data as JSON: /api/errors/8cb685093125571b. Report an issue: GitHub.

Appendix: source

Thrown at pkg/alsa/device/device_linux.go:163

	swparams := snd_pcm_sw_params{
		//tstamp_mode: SNDRV_PCM_TSTAMP_ENABLE,
		period_step:    1,
		avail_min:      1, // start as soon as possible
		stop_threshold: threshold,
	}

	if d.IsCapture() {
		swparams.start_threshold = 1
	} else {
		swparams.start_threshold = threshold
	}

	if err := ioctl(d.fd, SNDRV_PCM_IOCTL_SW_PARAMS, &swparams); err != nil {
		return fmt.Errorf("[alsa] set sw_params: %w", err)
	}

	if err := ioctl(d.fd, SNDRV_PCM_IOCTL_PREPARE, nil); err != nil {
		return fmt.Errorf("[alsa] prepare: %w", err)
	}

	return nil
}

func (d *Device) Write(b []byte) (n int, err error) {
	xfer := &snd_xferi{
		buf:    uintptr(unsafe.Pointer(&b[0])),
		frames: snd_pcm_uframes_t(len(b) / d.frameBytes),
	}
	err = ioctl(d.fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, xfer)
	if err == syscall.EPIPE {
		// auto handle underrun state
		// https://stackoverflow.com/questions/59396728/how-to-properly-handle-xrun-in-alsa-programming-when-playing-audio-with-snd-pcm
		err = ioctl(d.fd, SNDRV_PCM_IOCTL_PREPARE, nil)
	}
	n = int(xfer.result) * d.frameBytes
	return

View on GitHub (pinned to c245815e75)