On Tue, Dec 07, 2021 at 07:07:08PM -0500, Brad Smith wrote:
> Here is another attempt at a sndio backend for mpv.
>
> Looking for some testers.
OK kn if anyone wants to commit.
> Index: Makefile
> ===================================================================
> RCS file: /home/cvs/ports/multimedia/mpv/Makefile,v
> retrieving revision 1.77
> diff -u -p -u -p -r1.77 Makefile
> --- Makefile 16 Nov 2021 21:30:22 -0000 1.77
> +++ Makefile 7 Dec 2021 23:58:30 -0000
> @@ -5,7 +5,7 @@ COMMENT = movie player based on MPlayer
> GH_ACCOUNT = mpv-player
> GH_PROJECT = mpv
> GH_TAGNAME = v0.34.0
> -REVISION = 0
> +REVISION = 1
>
> SHARED_LIBS += mpv 0.2 # 1.109
>
> @@ -26,8 +26,8 @@ WANTLIB += Xrandr Xrender Xss Xv Xxf86vm
> WANTLIB += avfilter avformat avresample avutil bluray c cdio cdio_cdda
> WANTLIB += cdio_paranoia drm dvdnav dvdread epoxy expat fontconfig
> WANTLIB += freetype fribidi gbm glib-2.0 graphite2 harfbuzz iconv
> -WANTLIB += intl jpeg lcms2 ${MODLUA_WANTLIB} m pcre placebo postproc pthread
> -WANTLIB += swresample swscale xcb xcb-dri2 xcb-glx xml2 z
> +WANTLIB += intl jpeg lcms2 ${MODLUA_WANTLIB} m pcre placebo postproc
> +WANTLIB += pthread sndio swresample swscale xcb xcb-dri2 xcb-glx xml2 z
>
> MODULES = lang/lua \
> lang/python
> @@ -66,6 +66,7 @@ CONFIGURE_ARGS = --confloaddir=${SYSCONF
> --enable-dvdnav \
> --enable-libmpv-shared \
> --enable-sdl2 \
> + --enable-sndio \
> --disable-alsa \
> --disable-caca \
> --disable-cuda-hwaccel \
> @@ -77,6 +78,7 @@ CONFIGURE_ARGS = --confloaddir=${SYSCONF
> --disable-optimize \
> --disable-pulse \
> --disable-sixel \
> + --disable-sdl2-audio \
> --disable-uchardet \
> --disable-vaapi \
> --disable-vaapi-drm \
> Index: patches/patch-audio_out_ao_c
> ===================================================================
> RCS file: /home/cvs/ports/multimedia/mpv/patches/patch-audio_out_ao_c,v
> retrieving revision 1.2
> diff -u -p -u -p -r1.2 patch-audio_out_ao_c
> --- patches/patch-audio_out_ao_c 7 Nov 2021 18:47:37 -0000 1.2
> +++ patches/patch-audio_out_ao_c 7 Dec 2021 23:13:33 -0000
> @@ -1,8 +1,8 @@
> $OpenBSD: patch-audio_out_ao_c,v 1.2 2021/11/07 18:47:37 kn Exp $
>
> -ao_sndio: add this audio output again
> +Another attempt to return ao_sndio to mpv
>
> -https://github.com/mpv-player/mpv/pull/8314
> +https://github.com/mpv-player/mpv/pull/9298
>
> Index: audio/out/ao.c
> --- audio/out/ao.c.orig
> Index: patches/patch-audio_out_ao_sndio_c
> ===================================================================
> RCS file: /home/cvs/ports/multimedia/mpv/patches/patch-audio_out_ao_sndio_c,v
> retrieving revision 1.3
> diff -u -p -u -p -r1.3 patch-audio_out_ao_sndio_c
> --- patches/patch-audio_out_ao_sndio_c 22 Aug 2021 06:59:18 -0000
> 1.3
> +++ patches/patch-audio_out_ao_sndio_c 7 Dec 2021 23:12:44 -0000
> @@ -1,13 +1,13 @@
> $OpenBSD: patch-audio_out_ao_sndio_c,v 1.3 2021/08/22 06:59:18 rsadowski Exp
> $
>
> -ao_sndio: add this audio output again
> +Another attempt to return ao_sndio to mpv
>
> -https://github.com/mpv-player/mpv/pull/8314
> +https://github.com/mpv-player/mpv/pull/9298
>
> Index: audio/out/ao_sndio.c
> --- audio/out/ao_sndio.c.orig
> +++ audio/out/ao_sndio.c
> -@@ -0,0 +1,317 @@
> +@@ -0,0 +1,322 @@
> +/*
> + * Copyright (c) 2008 Alexandre Ratchov <[email protected]>
> + * Copyright (c) 2013 Christian Neukirchen <[email protected]>
> @@ -51,7 +51,7 @@ Index: audio/out/ao_sndio.c
> +};
> +
> +
> -+static const struct mp_chmap sndio_layouts[MP_NUM_CHANNELS + 1] = {
> ++static const struct mp_chmap sndio_layouts[] = {
> + {0}, /* empty */
> + {1, {MP_SPEAKER_ID_FL}}, /* mono */
> + MP_CHMAP2(FL, FR), /* stereo */
> @@ -74,11 +74,9 @@ Index: audio/out/ao_sndio.c
> +static void process_events(struct ao *ao)
> +{
> + struct priv *p = ao->priv;
> -+
> -+ if (!p->playing)
> -+ return;
> ++
> + int n = sio_pollfd(p->hdl, p->pfd, POLLOUT);
> -+ while (poll(p->pfd, n, 0) < 0 && errno == EINTR) {}
> ++ while (poll(p->pfd, n, 0) < 0 && errno == EINTR);
> +
> + sio_revents(p->hdl, p->pfd);
> +}
> @@ -129,21 +127,15 @@ Index: audio/out/ao_sndio.c
> +
> + /* Selecting sound format. */
> + ao->format = af_fmt_from_planar(ao->format);
> -+ for (i = 0, ap = af_to_par;; i++, ap++) {
> -+ if (i == MP_ARRAY_SIZE(af_to_par)) {
> -+ MP_VERBOSE(ao, "unsupported format\n");
> -+ p->par.bits = 16;
> -+ p->par.sig = 1;
> -+ p->par.le = SIO_LE_NATIVE;
> -+ break;
> -+ }
> ++
> ++ p->par.bits = 16;
> ++ p->par.sig = 1;
> ++ p->par.le = SIO_LE_NATIVE;
> ++ for (i = 0; i < MP_ARRAY_SIZE(af_to_par); i++) {
> ++ ap = &af_to_par[i];
> + if (ap->format == ao->format) {
> + p->par.bits = ap->bits;
> + p->par.sig = ap->sig;
> -+ if (ap->bits > 8)
> -+ p->par.le = SIO_LE_NATIVE;
> -+ if (ap->bits != SIO_BPS(ap->bits))
> -+ p->par.bps = ap->bits / 8;
> + break;
> + }
> + }
> @@ -161,7 +153,11 @@ Index: audio/out/ao_sndio.c
> +#ifdef __FreeBSD__
> + /* OSS wrapper have bad defaults, overwrite it. */
> + p->par.appbufsz = ((p->par.rate * 25) / 1000); /* 25 ms. */
> ++#else
> ++ p->par.appbufsz = p->par.rate * 250 / 1000; /* 250ms buffer */
> +#endif
> ++ p->par.round = p->par.rate * 10 / 1000; /* 10ms block size */
> ++
> + if (!sio_setpar(p->hdl, &p->par)) {
> + MP_ERR(ao, "couldn't set params\n");
> + goto err_out;
> @@ -192,7 +188,7 @@ Index: audio/out/ao_sndio.c
> + p->havevol = sio_onvol(p->hdl, volcb, ao);
> + sio_onmove(p->hdl, movecb, ao);
> +
> -+ p->pfd = calloc(sio_nfds(p->hdl), sizeof(struct pollfd));
> ++ p->pfd = talloc_array_ptrtype(p, p->pfd, sio_nfds(p->hdl));
> + if (!p->pfd)
> + goto err_out;
> +
> @@ -222,7 +218,6 @@ Index: audio/out/ao_sndio.c
> + sio_close(p->hdl);
> + p->hdl = NULL;
> + }
> -+ free(p->pfd);
> + p->pfd = NULL;
> + p->playing = false;
> +}
> @@ -253,23 +248,16 @@ Index: audio/out/ao_sndio.c
> +{
> + struct priv *p = ao->priv;
> +
> -+ process_events(ao);
> -+ p->delay = 0;
> -+ p->playing = false;
> ++ if (p->playing) {
> ++ p->playing = false;
> +
> -+ /* XXX: some times may block here then sndiod used. */
> -+ if (!sio_stop(p->hdl)) {
> -+ MP_ERR(ao, "reset: couldn't sio_stop()\n");
> -+reinit:
> -+ /* Without this device will never work again. */
> -+ MP_WARN(ao, "Force reinitialize audio device.\n");
> -+ uninit(ao);
> -+ init(ao);
> -+ return;
> -+ }
> -+ if (!sio_start(p->hdl)) {
> -+ MP_ERR(ao, "reset: sio_start() fail.\n");
> -+ goto reinit;
> ++ if (!sio_stop(p->hdl)) {
> ++ MP_ERR(ao, "reset: couldn't sio_stop()\n");
> ++ }
> ++ p->delay = 0;
> ++ if (!sio_start(p->hdl)) {
> ++ MP_ERR(ao, "reset: sio_start() fail.\n");
> ++ }
> + }
> +}
> +
> @@ -296,7 +284,6 @@ Index: audio/out/ao_sndio.c
> + return false;
> + }
> + p->delay += samples;
> -+ process_events(ao);
> +
> + return true;
> +}
> @@ -307,10 +294,28 @@ Index: audio/out/ao_sndio.c
> +
> + process_events(ao);
> +
> -+ state->free_samples = (ao->device_buffer - p->delay);
> ++ /* how many samples we can play without blocking */
> ++ state->free_samples = ao->device_buffer - p->delay;
> ++ state->free_samples = state->free_samples / p->par.round * p->par.round;
> ++ /* how many samples are already in the buffer to be played */
> + state->queued_samples = p->delay;
> -+ state->delay = (p->delay / (double)p->par.rate);
> -+ state->playing = p->playing;
> ++ /* delay in seconds between first and last sample in buffer */
> ++ state->delay = p->delay / (double)p->par.rate;
> ++
> ++ /* report unexpected EOF / underrun */
> ++ if (state->queued_samples && state->queued_samples &&
> ++ state->queued_samples < state->free_samples &&
> ++ p->playing || sio_eof(p->hdl))
> ++ {
> ++ MP_VERBOSE(ao, "get_state: EOF/underrun detected.\n");
> ++ MP_VERBOSE(ao, "get_state: free: %d, queued: %d, delay: %lf\n", \
> ++ state->free_samples, state->queued_samples, state->delay);
> ++ p->playing = false;
> ++ state->playing = p->playing;
> ++ ao_wakeup_playthread(ao);
> ++ } else {
> ++ state->playing = p->playing;
> ++ }
> +}
> +
> +const struct ao_driver audio_out_sndio = {
> Index: patches/patch-wscript
> ===================================================================
> RCS file: /home/cvs/ports/multimedia/mpv/patches/patch-wscript,v
> retrieving revision 1.4
> diff -u -p -u -p -r1.4 patch-wscript
> --- patches/patch-wscript 7 Nov 2021 18:47:37 -0000 1.4
> +++ patches/patch-wscript 7 Dec 2021 23:13:01 -0000
> @@ -1,20 +1,19 @@
> $OpenBSD: patch-wscript,v 1.4 2021/11/07 18:47:37 kn Exp $
>
> -ao_sndio: add this audio output again
> +Another attempt to return ao_sndio to mpv
>
> -https://github.com/mpv-player/mpv/pull/8314
> +https://github.com/mpv-player/mpv/pull/9298
>
> Index: wscript
> --- wscript.orig
> +++ wscript
> -@@ -435,6 +435,12 @@ audio_output_features = [
> +@@ -435,6 +435,11 @@ audio_output_features = [
> 'func': check_statement(['sys/soundcard.h'], 'int x =
> SNDCTL_DSP_SETPLAYVOL'),
> 'deps': 'posix && gpl',
> }, {
> + 'name': '--sndio',
> + 'desc': 'sndio audio input/output',
> -+ 'func': check_statement('sndio.h',
> -+ 'struct sio_par par; sio_initpar(&par); const char *s =
> SIO_DEVANY', lib='sndio'),
> ++ 'func': check_pkg_config('sndio'),
> + 'default': 'disable'
> + }, {
> 'name': '--pulse',
> Index: patches/patch-wscript_build_py
> ===================================================================
> RCS file: /home/cvs/ports/multimedia/mpv/patches/patch-wscript_build_py,v
> retrieving revision 1.2
> diff -u -p -u -p -r1.2 patch-wscript_build_py
> --- patches/patch-wscript_build_py 7 Nov 2021 18:47:37 -0000 1.2
> +++ patches/patch-wscript_build_py 7 Dec 2021 23:13:14 -0000
> @@ -1,8 +1,8 @@
> $OpenBSD: patch-wscript_build_py,v 1.2 2021/11/07 18:47:37 kn Exp $
>
> -ao_sndio: add this audio output again
> +Another attempt to return ao_sndio to mpv
>
> -https://github.com/mpv-player/mpv/pull/8314
> +https://github.com/mpv-player/mpv/pull/9298
>
> Index: wscript_build.py
> --- wscript_build.py.orig
>