]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/sndio.c
transcode: fix audio format given to filters (fixes #8385)
[vlc] / modules / audio_output / sndio.c
index 445297da6f3506d97e420b9fd4fd924f6ed47a28..a60c309c02c4834215ef05bc978753777f5a269f 100644 (file)
@@ -3,14 +3,14 @@
  *****************************************************************************
  * Copyright (C) 2012 RĂ©mi Denis-Courmont
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
  * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  * GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public License
@@ -40,88 +40,112 @@ vlc_module_begin ()
     set_category (CAT_AUDIO)
     set_subcategory (SUBCAT_AUDIO_AOUT)
     set_capability ("audio output", 120)
-    set_callbacks (Open, Close )
+    set_callbacks (Open, Close)
 vlc_module_end ()
 
-static void Play (audio_output_t *, block_t *, mtime_t *);
-static void Pause (audio_output_t *, bool, mtime_t);
+static int TimeGet (audio_output_t *, mtime_t *);
+static void Play (audio_output_t *, block_t *);
+static void Flush (audio_output_t *, bool);
 static int VolumeSet (audio_output_t *, float);
 static int MuteSet (audio_output_t *, bool);
 static void VolumeChanged (void *, unsigned);
+static void PositionChanged (void *, int);
 
 struct aout_sys_t
 {
     struct sio_hdl *hdl;
+    int started;
+    int delay;
+    unsigned rate;
     unsigned volume;
     bool mute;
 };
 
 /** Initializes an sndio playback stream */
-static int Open (vlc_object_t *obj)
+static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
 {
-    audio_output_t *aout = (audio_output_t *)obj;
-    aout_sys_t *sys = malloc (sizeof (*sys));
-    if (unlikely(sys == NULL))
-        return VLC_EGENERIC;
+    aout_sys_t *sys = aout->sys;
 
     sys->hdl = sio_open (NULL, SIO_PLAY, 0 /* blocking */);
     if (sys->hdl == NULL)
     {
-        msg_Err (obj, "cannot create audio playback stream");
-        free (sys);
+        msg_Err (aout, "cannot create audio playback stream");
         return VLC_EGENERIC;
     }
-    aout->sys = sys;
 
     struct sio_par par;
     sio_initpar (&par);
-    par.bits = 16;
-    par.bps = par.bits >> 3;
-    par.sig = 1;
-    par.le = SIO_LE_NATIVE;
-    par.pchan = aout_FormatNbChannels (&aout->format);
-    par.rate = aout->format.i_rate;
-    par.xrun = SIO_SYNC;
+    switch (fmt->i_format) {
+    case VLC_CODEC_S8:
+       par.bits = 8;
+       par.sig = 0;
+       break;
+    case VLC_CODEC_S16N:
+       par.bits = 16;
+       par.sig = 1;
+       par.le = SIO_LE_NATIVE;
+       break;
+    case VLC_CODEC_S32N:
+    case VLC_CODEC_FL32:
+    case VLC_CODEC_FL64:
+       par.bits = 32;
+       par.sig = 1;
+       par.le = SIO_LE_NATIVE;
+       break;
+    default:
+       /* use a common audio format */
+       par.bits = 16;
+       par.sig = 1;
+       par.le = SIO_LE_NATIVE;
+    }
+    par.pchan = aout_FormatNbChannels (fmt);
+    par.rate = fmt->i_rate;
+    par.round = par.rate / 50;
+    par.appbufsz = par.rate / 4;
 
     if (!sio_setpar (sys->hdl, &par) || !sio_getpar (sys->hdl, &par))
     {
-        msg_Err (obj, "cannot negotiate audio playback parameters");
+        msg_Err (aout, "cannot negotiate audio playback parameters");
         goto error;
     }
 
-    if (par.bps != par.bits >> 3)
+    if (par.bps != par.bits >> 3 && !par.msb)
     {
-        msg_Err (obj, "unsupported audio sample format (%u bits in %u bytes)",
+        msg_Err (aout, "unsupported audio sample format (%u bits in %u bytes)",
                  par.bits, par.bps);
         goto error;
     }
-
-    audio_format_t f;
-
+    if (par.sig != (par.bits != 8))
+    {
+        msg_Err (aout, "unsupported audio sample format (%ssigned)",
+                 par.sig ? "" : "un");
+        goto error;
+    }
+    if (par.bps > 1 && par.le != SIO_LE_NATIVE)
+    {
+        msg_Err (aout, "unsupported audio sample format (%s endian)",
+                par.le ? "little" : "big");
+        goto error;
+    }
     switch (par.bits)
     {
         case 8:
-            f.i_format = par.sig ? VLC_CODEC_S8 : VLC_CODEC_U8;
+            fmt->i_format = VLC_CODEC_U8;
             break;
         case 16:
-            f.i_format = par.sig ? (par.le ? VLC_CODEC_S16L : VLC_CODEC_S16B)
-                                 : (par.le ? VLC_CODEC_U16L : VLC_CODEC_U16B);
-            break;
-        case 24:
-            f.i_format = par.sig ? (par.le ? VLC_CODEC_S24L : VLC_CODEC_S24B)
-                                 : (par.le ? VLC_CODEC_U24L : VLC_CODEC_U24B);
+            fmt->i_format = VLC_CODEC_S16N;
             break;
         case 32:
-            f.i_format = par.sig ? (par.le ? VLC_CODEC_S32L : VLC_CODEC_S32B)
-                                 : (par.le ? VLC_CODEC_U32L : VLC_CODEC_U32B);
+            fmt->i_format = VLC_CODEC_S32N;
             break;
         default:
-            msg_Err (obj, "unsupported audio sample format (%u bits)",
+            msg_Err (aout, "unsupported audio sample format (%u bits)",
                      par.bits);
             goto error;
     }
 
-    f.i_rate = par.rate;
+    fmt->i_rate = par.rate;
+    sys->rate = par.rate;
 
     /* Channel map */
     unsigned chans;
@@ -147,14 +171,13 @@ static int Open (vlc_object_t *obj)
             goto error;
     }
 
-    f.i_original_channels = f.i_physical_channels = chans;
-    aout_FormatPrepare (&f);
+    fmt->i_original_channels = fmt->i_physical_channels = chans;
+    aout_FormatPrepare (fmt);
 
-    aout->format = f;
-    aout->sys = sys;
-    aout->pf_play = Play;
-    aout->pf_pause = Pause;
-    aout->pf_flush  = NULL; /* sndio sucks! */
+    aout->time_get = TimeGet;
+    aout->play = Play;
+    aout->pause = NULL;
+    aout->flush = Flush;
     if (sio_onvol(sys->hdl, VolumeChanged, aout))
     {
         aout->volume_set = VolumeSet;
@@ -166,56 +189,61 @@ static int Open (vlc_object_t *obj)
         aout->mute_set = NULL;
     }
 
+    sys->started = 0;
+    sys->delay = 0;
+    sio_onmove (sys->hdl, PositionChanged, aout);
     sio_start (sys->hdl);
     return VLC_SUCCESS;
 
 error:
-    Close (obj);
+    sio_close (sys->hdl);
     return VLC_EGENERIC;
 }
 
-static void Close (vlc_object_t *obj)
+static void Stop (audio_output_t *aout)
 {
-    audio_output_t *aout = (audio_output_t *)obj;
     aout_sys_t *sys = aout->sys;
 
     sio_close (sys->hdl);
-    free (sys);
 }
 
-static void Play (audio_output_t *aout, block_t *block,
-                  mtime_t *restrict drift)
+static void PositionChanged (void *arg, int delta)
 {
+    audio_output_t *aout = arg;
     aout_sys_t *sys = aout->sys;
-    struct sio_par par;
 
-    if (sio_getpar (sys->hdl, &par) == 0)
-    {
-        mtime_t delay = par.bufsz * CLOCK_FREQ / aout->format.i_rate;
+    sys->delay -= delta;
+    sys->started = 1;
+}
 
-        *drift = mdate () + delay - block->i_pts;
-    }
+static int TimeGet (audio_output_t *aout, mtime_t *restrict delay)
+{
+    aout_sys_t *sys = aout->sys;
 
-    while (block->i_buffer > 0 && !sio_eof (sys->hdl))
-    {
-        size_t bytes = sio_write (sys->hdl, block->p_buffer, block->i_buffer);
+    if (!sys->started)
+       return -1;
+    *delay = (mtime_t)sys->delay * CLOCK_FREQ / sys->rate;
+    return 0;
+}
 
-        block->p_buffer += bytes;
-        block->i_buffer -= bytes;
-        /* Note that i_nb_samples and i_pts are corrupted here. */
-    }
+static void Play (audio_output_t *aout, block_t *block)
+{
+    aout_sys_t *sys = aout->sys;
+
+    sio_write (sys->hdl, block->p_buffer, block->i_buffer);
+    sys->delay += block->i_nb_samples;
     block_Release (block);
 }
 
-static void Pause (audio_output_t *aout, bool pause, mtime_t date)
+static void Flush (audio_output_t *aout, bool wait)
 {
     aout_sys_t *sys = aout->sys;
 
-    if (pause)
-        sio_stop (sys->hdl);
-    else
-        sio_start (sys->hdl);
-    (void) date;
+    sio_stop (sys->hdl);
+    sys->started = 0;
+    sys->delay = 0;
+    sio_start (sys->hdl);
+    (void)wait;
 }
 
 static void VolumeChanged (void *arg, unsigned volume)
@@ -232,8 +260,13 @@ static void VolumeChanged (void *arg, unsigned volume)
 static int VolumeSet (audio_output_t *aout, float fvol)
 {
     aout_sys_t *sys = aout->sys;
-    unsigned volume = lroundf (fvol * SIO_MAXVOL);
+    unsigned volume;
 
+    if (fvol < 0)
+       fvol = 0;
+    if (fvol > 1)
+       fvol = 1;
+    volume = lroundf (fvol * SIO_MAXVOL);
     if (!sys->mute && !sio_setvol (sys->hdl, volume))
         return -1;
     sys->volume = volume;
@@ -250,3 +283,26 @@ static int MuteSet (audio_output_t *aout, bool mute)
     sys->mute = mute;
     return 0;
 }
+
+static int Open (vlc_object_t *obj)
+{
+    audio_output_t *aout = (audio_output_t *)obj;
+    aout_sys_t *sys = malloc (sizeof (*sys));
+    if (unlikely(sys == NULL))
+        return VLC_ENOMEM;
+
+    aout->sys = sys;
+    aout->start = Start;
+    aout->stop = Stop;
+    /* FIXME: set volume/mute here */
+    return VLC_SUCCESS;
+}
+
+static void Close (vlc_object_t *obj)
+{
+    audio_output_t *aout = (audio_output_t *)obj;
+    aout_sys_t *sys = aout->sys;
+
+    free (sys);
+}
+