Use a macro for the record thread name
[openal-soft.git] / Alc / backends / sndio.c
blob52bff13a5b6b6ab8487d22e5eb9ae532f71d9670
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "alMain.h"
28 #include "alu.h"
29 #include "threads.h"
31 #include <sndio.h>
34 static const ALCchar sndio_device[] = "SndIO Default";
37 static ALCboolean sndio_load(void)
39 return ALC_TRUE;
43 typedef struct {
44 struct sio_hdl *sndHandle;
46 ALvoid *mix_data;
47 ALsizei data_size;
49 volatile int killNow;
50 althrd_t thread;
51 } sndio_data;
54 static int sndio_proc(void *ptr)
56 ALCdevice *device = ptr;
57 sndio_data *data = device->ExtraData;
58 ALsizei frameSize;
59 size_t wrote;
61 SetRTPriority();
62 althrd_setname(althrd_current(), MIXER_THREAD_NAME);
64 frameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
66 while(!data->killNow && device->Connected)
68 ALsizei len = data->data_size;
69 ALubyte *WritePtr = data->mix_data;
71 aluMixData(device, WritePtr, len/frameSize);
72 while(len > 0 && !data->killNow)
74 wrote = sio_write(data->sndHandle, WritePtr, len);
75 if(wrote == 0)
77 ERR("sio_write failed\n");
78 ALCdevice_Lock(device);
79 aluHandleDisconnect(device);
80 ALCdevice_Unlock(device);
81 break;
84 len -= wrote;
85 WritePtr += wrote;
89 return 0;
94 static ALCenum sndio_open_playback(ALCdevice *device, const ALCchar *deviceName)
96 sndio_data *data;
98 if(!deviceName)
99 deviceName = sndio_device;
100 else if(strcmp(deviceName, sndio_device) != 0)
101 return ALC_INVALID_VALUE;
103 data = calloc(1, sizeof(*data));
104 data->killNow = 0;
106 data->sndHandle = sio_open(NULL, SIO_PLAY, 0);
107 if(data->sndHandle == NULL)
109 free(data);
110 ERR("Could not open device\n");
111 return ALC_INVALID_VALUE;
114 al_string_copy_cstr(&device->DeviceName, deviceName);
115 device->ExtraData = data;
117 return ALC_NO_ERROR;
120 static void sndio_close_playback(ALCdevice *device)
122 sndio_data *data = device->ExtraData;
124 sio_close(data->sndHandle);
125 free(data);
126 device->ExtraData = NULL;
129 static ALCboolean sndio_reset_playback(ALCdevice *device)
131 sndio_data *data = device->ExtraData;
132 struct sio_par par;
134 sio_initpar(&par);
136 par.rate = device->Frequency;
137 par.pchan = ((device->FmtChans != DevFmtMono) ? 2 : 1);
139 switch(device->FmtType)
141 case DevFmtByte:
142 par.bits = 8;
143 par.sig = 1;
144 break;
145 case DevFmtUByte:
146 par.bits = 8;
147 par.sig = 0;
148 break;
149 case DevFmtFloat:
150 case DevFmtShort:
151 par.bits = 16;
152 par.sig = 1;
153 break;
154 case DevFmtUShort:
155 par.bits = 16;
156 par.sig = 0;
157 break;
158 case DevFmtInt:
159 par.bits = 32;
160 par.sig = 1;
161 break;
162 case DevFmtUInt:
163 par.bits = 32;
164 par.sig = 0;
165 break;
167 par.le = SIO_LE_NATIVE;
169 par.round = device->UpdateSize;
170 par.appbufsz = device->UpdateSize * (device->NumUpdates-1);
171 if(!par.appbufsz) par.appbufsz = device->UpdateSize;
173 if(!sio_setpar(data->sndHandle, &par) || !sio_getpar(data->sndHandle, &par))
175 ERR("Failed to set device parameters\n");
176 return ALC_FALSE;
179 if(par.bits != par.bps*8)
181 ERR("Padded samples not supported (%u of %u bits)\n", par.bits, par.bps*8);
182 return ALC_FALSE;
185 device->Frequency = par.rate;
186 device->FmtChans = ((par.pchan==1) ? DevFmtMono : DevFmtStereo);
188 if(par.bits == 8 && par.sig == 1)
189 device->FmtType = DevFmtByte;
190 else if(par.bits == 8 && par.sig == 0)
191 device->FmtType = DevFmtUByte;
192 else if(par.bits == 16 && par.sig == 1)
193 device->FmtType = DevFmtShort;
194 else if(par.bits == 16 && par.sig == 0)
195 device->FmtType = DevFmtUShort;
196 else if(par.bits == 32 && par.sig == 1)
197 device->FmtType = DevFmtInt;
198 else if(par.bits == 32 && par.sig == 0)
199 device->FmtType = DevFmtUInt;
200 else
202 ERR("Unhandled sample format: %s %u-bit\n", (par.sig?"signed":"unsigned"), par.bits);
203 return ALC_FALSE;
206 device->UpdateSize = par.round;
207 device->NumUpdates = (par.bufsz/par.round) + 1;
209 SetDefaultChannelOrder(device);
211 return ALC_TRUE;
214 static ALCboolean sndio_start_playback(ALCdevice *device)
216 sndio_data *data = device->ExtraData;
218 if(!sio_start(data->sndHandle))
220 ERR("Error starting playback\n");
221 return ALC_FALSE;
224 data->data_size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
225 data->mix_data = calloc(1, data->data_size);
227 data->killNow = 0;
228 if(althrd_create(&data->thread, sndio_proc, device) != althrd_success)
230 sio_stop(data->sndHandle);
231 free(data->mix_data);
232 data->mix_data = NULL;
233 return ALC_FALSE;
236 return ALC_TRUE;
239 static void sndio_stop_playback(ALCdevice *device)
241 sndio_data *data = device->ExtraData;
242 int res;
244 if(data->killNow)
245 return;
247 data->killNow = 1;
248 althrd_join(data->thread, &res);
250 if(!sio_stop(data->sndHandle))
251 ERR("Error stopping device\n");
253 free(data->mix_data);
254 data->mix_data = NULL;
258 static const BackendFuncs sndio_funcs = {
259 sndio_open_playback,
260 sndio_close_playback,
261 sndio_reset_playback,
262 sndio_start_playback,
263 sndio_stop_playback,
264 NULL,
265 NULL,
266 NULL,
267 NULL,
268 NULL,
269 NULL
272 ALCboolean alc_sndio_init(BackendFuncs *func_list)
274 if(!sndio_load())
275 return ALC_FALSE;
276 *func_list = sndio_funcs;
277 return ALC_TRUE;
280 void alc_sndio_deinit(void)
284 void alc_sndio_probe(enum DevProbe type)
286 switch(type)
288 case ALL_DEVICE_PROBE:
289 AppendAllDevicesList(sndio_device);
290 break;
291 case CAPTURE_DEVICE_PROBE:
292 break;