]> git.sesse.net Git - vlc/blob - modules/audio_output/sndio.c
sndio: add OpenBSD (or RoarAudio emulation) audio output
[vlc] / modules / audio_output / sndio.c
1 /*****************************************************************************
2  * sndio.c : sndio plugin for VLC
3  *****************************************************************************
4  * Copyright (C) 2012 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <assert.h>
26
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
29 #include <vlc_aout.h>
30
31 #include <sndio.h>
32
33 static int Open (vlc_object_t *);
34 static void Close (vlc_object_t *);
35
36 vlc_module_begin ()
37     set_shortname ("sndio")
38     set_description (N_("OpenBSD sndio audio output"))
39     set_category (CAT_AUDIO)
40     set_subcategory (SUBCAT_AUDIO_AOUT)
41     set_capability ("audio output", 120)
42     set_callbacks (Open, Close )
43 vlc_module_end ()
44
45 static void Play  (audio_output_t *, block_t *);
46 static void Pause (audio_output_t *, bool, mtime_t);
47
48 /** Initializes an sndio playback stream */
49 static int Open (vlc_object_t *obj)
50 {
51     audio_output_t *aout = (audio_output_t *)obj;
52
53     struct sio_hdl *sio = sio_open (NULL, SIO_PLAY, 0 /* blocking */);
54     if (sio == NULL)
55     {
56         msg_Err (obj, "cannot create audio playback stream");
57         return VLC_EGENERIC;
58     }
59
60     struct sio_par par;
61     sio_initpar (&par);
62     par.bits = 16;
63     par.bps = par.bits >> 3;
64     par.sig = 1;
65     par.le = SIO_LE_NATIVE;
66     par.pchan = aout_FormatNbChannels (&aout->format);
67     par.rate = aout->format.i_rate;
68     par.xrun = SIO_SYNC;
69
70     if (!sio_setpar (sio, &par) || !sio_getpar (sio, &par))
71     {
72         msg_Err (obj, "cannot negotiate audio playback parameters");
73         goto error;
74     }
75
76     if (par.bps != par.bits >> 3)
77     {
78         msg_Err (obj, "unsupported audio sample format (%u bits in %u bytes)",
79                  par.bits, par.bps);
80         goto error;
81     }
82
83     audio_format_t f;
84
85     switch (par.bps)
86     {
87         case 8:
88             f.i_format = par.sig ? VLC_CODEC_S8 : VLC_CODEC_U8;
89             break;
90         case 16:
91             f.i_format = par.sig ? (par.le ? VLC_CODEC_S16L : VLC_CODEC_S16B)
92                                  : (par.le ? VLC_CODEC_U16L : VLC_CODEC_U16B);
93             break;
94         case 24:
95             f.i_format = par.sig ? (par.le ? VLC_CODEC_S24L : VLC_CODEC_S24B)
96                                  : (par.le ? VLC_CODEC_U24L : VLC_CODEC_U24B);
97             break;
98         case 32:
99             f.i_format = par.sig ? (par.le ? VLC_CODEC_S32L : VLC_CODEC_S32B)
100                                  : (par.le ? VLC_CODEC_U32L : VLC_CODEC_U32B);
101             break;
102         default:
103             msg_Err (obj, "unsupported audio sample format (%u bits)",
104                      par.bits);
105             goto error;
106     }
107
108     f.i_rate = par.rate;
109
110     /* Channel map */
111     unsigned chans;
112     switch (par.pchan)
113     {
114         case 1:
115             chans = AOUT_CHAN_CENTER;
116             break;
117         case 2:
118             chans = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
119             break;
120         case 4:
121             chans = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
122                   | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
123             break;
124         case 6:
125             chans = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
126                   | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
127                   | AOUT_CHAN_CENTER | AOUT_CHAN_LFE;
128             break;
129         default:
130             msg_Err (aout, "unknown %u channels map", par.pchan);
131             goto error;
132     }
133
134     f.i_original_channels = f.i_physical_channels = chans;
135     aout_FormatPrepare (&f);
136
137     aout->format = f;
138     aout->sys = (void *)sio;
139     aout->pf_play = Play;
140     aout->pf_pause = Pause;
141     aout->pf_flush  = NULL; /* sndio sucks! */
142     aout_VolumeSoftInit (aout); /* TODO: sio_onvol() */
143
144     sio_start (sio);
145     return VLC_SUCCESS;
146
147 error:
148     sio_close (sio);
149     return VLC_EGENERIC;
150 }
151
152 static void Close (vlc_object_t *obj)
153 {
154     audio_output_t *aout = (audio_output_t *)obj;
155     struct sio_hdl *sio = (void *)aout->sys;
156
157     sio_close (sio);
158 }
159
160 static void Play (audio_output_t *aout, block_t *block)
161 {
162     struct sio_hdl *sio = (void *)aout->sys;
163     struct sio_par par;
164
165     if (sio_getpar (sio, &par) == 0)
166     {
167         mtime_t delay = par.bufsz * CLOCK_FREQ / aout->format.i_rate;
168
169         delay = block->i_pts - (mdate () - delay);
170         if (delay > 0)
171         {
172             size_t frames = (delay * aout->format.i_rate) / CLOCK_FREQ;
173             msg_Dbg (aout, "prepending %zu zeroes", frames);
174
175             void *pad = calloc (frames, aout->format.i_bytes_per_frame);
176             if (likely(pad != NULL))
177             {
178                 sio_write (sio, pad, frames * aout->format.i_bytes_per_frame);
179                 free (pad);
180             }
181         }
182         else
183             aout_TimeReport (aout, block->i_pts - delay);
184     }
185
186     while (block->i_buffer > 0 && !sio_eof (sio))
187     {
188         size_t bytes = sio_write (sio, block->p_buffer, block->i_buffer);
189
190         block->p_buffer += bytes;
191         block->i_buffer -= bytes;
192         /* Note that i_nb_samples and i_pts are corrupted here. */
193     }
194     block_Release (block);
195 }
196
197 static void Pause (audio_output_t *aout, bool pause, mtime_t date)
198 {
199     struct sio_hdl *sio = (void *)aout->sys;
200
201     if (pause)
202         sio_stop (sio);
203     else
204         sio_start (sio);
205     (void) date;
206 }