Add a method to deinitialize backends
[openal-soft.git] / Alc / oss.c
blobaaa2e47aad371d0053516eef7b5ff4fcf14b8312
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <memory.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <math.h>
33 #include "alMain.h"
34 #include "AL/al.h"
35 #include "AL/alc.h"
37 #include <sys/soundcard.h>
40 * The OSS documentation talks about SOUND_MIXER_READ, but the header
41 * only contains MIXER_READ. Play safe. Same for WRITE.
43 #ifndef SOUND_MIXER_READ
44 #define SOUND_MIXER_READ MIXER_READ
45 #endif
46 #ifndef SOUND_MIXER_WRITE
47 #define SOUND_MIXER_WRITE MIXER_WRITE
48 #endif
50 static char *oss_device;
51 static char *oss_device_capture;
53 typedef struct {
54 int fd;
55 volatile int killNow;
56 ALvoid *thread;
58 ALubyte *mix_data;
59 int data_size;
61 RingBuffer *ring;
62 int doCapture;
63 } oss_data;
66 static int log2i(ALCuint x)
68 int y = 0;
69 while (x > 1)
71 x >>= 1;
72 y++;
74 return y;
78 static ALuint OSSProc(ALvoid *ptr)
80 ALCdevice *pDevice = (ALCdevice*)ptr;
81 oss_data *data = (oss_data*)pDevice->ExtraData;
82 int wrote;
84 while(!data->killNow && !pDevice->Connected)
86 ALint len = data->data_size;
87 ALubyte *WritePtr = data->mix_data;
89 SuspendContext(NULL);
90 aluMixData(pDevice->Context, WritePtr, len, pDevice->Format);
91 ProcessContext(NULL);
93 while(len > 0 && !data->killNow)
95 wrote = write(data->fd, WritePtr, len);
96 if(wrote < 0)
98 if(errno != EAGAIN && errno != EWOULDBLOCK)
100 AL_PRINT("write failed: %s\n", strerror(errno));
101 aluHandleDisconnect(pDevice);
102 len = 0;
104 else
105 Sleep(1);
106 continue;
109 len -= wrote;
110 WritePtr += wrote;
114 return 0;
117 static ALuint OSSCaptureProc(ALvoid *ptr)
119 ALCdevice *pDevice = (ALCdevice*)ptr;
120 oss_data *data = (oss_data*)pDevice->ExtraData;
121 int frameSize;
122 int amt;
124 frameSize = aluBytesFromFormat(pDevice->Format);
125 frameSize *= aluChannelsFromFormat(pDevice->Format);
127 while(!data->killNow)
129 amt = read(data->fd, data->mix_data, data->data_size);
130 if(amt < 0)
132 AL_PRINT("read failed: %s\n", strerror(errno));
133 aluHandleDisconnect(pDevice);
134 break;
136 if(amt == 0)
138 Sleep(1);
139 continue;
141 if(data->doCapture)
142 WriteRingBuffer(data->ring, data->mix_data, amt/frameSize);
145 return 0;
148 static ALCboolean oss_open_playback(ALCdevice *device, const ALCchar *deviceName)
150 char driver[64];
151 oss_data *data;
153 strncpy(driver, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver)-1);
154 driver[sizeof(driver)-1] = 0;
155 if(deviceName)
157 if(strcmp(deviceName, oss_device))
158 return ALC_FALSE;
159 device->szDeviceName = oss_device;
161 else
162 device->szDeviceName = oss_device;
164 data = (oss_data*)calloc(1, sizeof(oss_data));
165 data->killNow = 0;
167 data->fd = open(driver, O_WRONLY);
168 if(data->fd == -1)
170 free(data);
171 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
172 return ALC_FALSE;
175 device->ExtraData = data;
176 return ALC_TRUE;
179 static void oss_close_playback(ALCdevice *device)
181 oss_data *data = (oss_data*)device->ExtraData;
183 close(data->fd);
184 free(data);
185 device->ExtraData = NULL;
188 static ALCboolean oss_start_context(ALCdevice *device, ALCcontext *context)
190 oss_data *data = (oss_data*)device->ExtraData;
191 int numFragmentsLogSize;
192 int log2FragmentSize;
193 unsigned int periods;
194 audio_buf_info info;
195 ALuint frameSize;
196 int numChannels;
197 int ossFormat;
198 int ossSpeed;
199 char *err;
200 int i;
201 (void)context;
203 switch(aluBytesFromFormat(device->Format))
205 case 1:
206 ossFormat = AFMT_U8;
207 break;
208 case 4:
209 switch(aluChannelsFromFormat(device->Format))
211 case 1: device->Format = AL_FORMAT_MONO16; break;
212 case 2: device->Format = AL_FORMAT_STEREO16; break;
213 case 4: device->Format = AL_FORMAT_QUAD16; break;
214 case 6: device->Format = AL_FORMAT_51CHN16; break;
215 case 7: device->Format = AL_FORMAT_61CHN16; break;
216 case 8: device->Format = AL_FORMAT_71CHN16; break;
218 /* fall-through */
219 case 2:
220 ossFormat = AFMT_S16_NE;
221 break;
222 default:
223 ossFormat = -1;
224 AL_PRINT("Unknown format?! %x\n", device->Format);
227 periods = GetConfigValueInt("oss", "periods", 4);
228 if((int)periods <= 0)
229 periods = 4;
230 numChannels = aluChannelsFromFormat(device->Format);
231 frameSize = numChannels * aluBytesFromFormat(device->Format);
233 ossSpeed = device->Frequency;
234 log2FragmentSize = log2i(device->BufferSize * frameSize / periods);
236 /* according to the OSS spec, 16 bytes are the minimum */
237 if (log2FragmentSize < 4)
238 log2FragmentSize = 4;
239 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
241 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
242 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
243 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
244 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
245 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
246 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
248 AL_PRINT("%s failed: %s\n", err, strerror(errno));
249 return ALC_FALSE;
251 #undef ok
253 if((int)aluChannelsFromFormat(device->Format) != numChannels)
255 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
256 return ALC_FALSE;
259 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
260 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
262 AL_PRINT("Could not set %d-bit output, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
263 return ALC_FALSE;
266 device->Frequency = ossSpeed;
267 device->UpdateSize = info.fragsize / frameSize;
269 data->data_size = device->UpdateSize * frameSize;
270 data->mix_data = calloc(1, data->data_size);
272 data->thread = StartThread(OSSProc, device);
273 if(data->thread == NULL)
275 free(data->mix_data);
276 data->mix_data = NULL;
277 return ALC_FALSE;
280 return ALC_TRUE;
283 static void oss_stop_context(ALCdevice *device, ALCcontext *context)
285 oss_data *data = (oss_data*)device->ExtraData;
286 (void)context;
288 if(!data->thread)
289 return;
291 data->killNow = 1;
292 StopThread(data->thread);
293 data->thread = NULL;
295 free(data->mix_data);
296 data->mix_data = NULL;
300 static ALCboolean oss_open_capture(ALCdevice *device, const ALCchar *deviceName)
302 int numFragmentsLogSize;
303 int log2FragmentSize;
304 unsigned int periods;
305 audio_buf_info info;
306 ALuint frameSize;
307 int numChannels;
308 char driver[64];
309 oss_data *data;
310 int ossFormat;
311 int ossSpeed;
312 char *err;
313 int i;
315 strncpy(driver, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver)-1);
316 driver[sizeof(driver)-1] = 0;
317 if(deviceName)
319 if(strcmp(deviceName, oss_device_capture))
320 return ALC_FALSE;
321 device->szDeviceName = oss_device_capture;
323 else
324 device->szDeviceName = oss_device_capture;
326 data = (oss_data*)calloc(1, sizeof(oss_data));
327 data->killNow = 0;
329 data->fd = open(driver, O_RDONLY);
330 if(data->fd == -1)
332 free(data);
333 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
334 return ALC_FALSE;
337 switch(aluBytesFromFormat(device->Format))
339 case 1:
340 ossFormat = AFMT_U8;
341 break;
342 case 2:
343 ossFormat = AFMT_S16_NE;
344 break;
345 default:
346 ossFormat = -1;
347 AL_PRINT("Unknown format?! %x\n", device->Format);
350 periods = 4;
351 numChannels = aluChannelsFromFormat(device->Format);
352 frameSize = numChannels * aluBytesFromFormat(device->Format);
353 ossSpeed = device->Frequency;
354 log2FragmentSize = log2i(device->BufferSize * frameSize / periods);
356 /* according to the OSS spec, 16 bytes are the minimum */
357 if (log2FragmentSize < 4)
358 log2FragmentSize = 4;
359 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
361 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
362 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
363 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
364 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
365 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
366 ok(ioctl(data->fd, SNDCTL_DSP_GETISPACE, &info), "get space")))
368 AL_PRINT("%s failed: %s\n", err, strerror(errno));
369 close(data->fd);
370 free(data);
371 return ALC_FALSE;
373 #undef ok
375 if((int)aluChannelsFromFormat(device->Format) != numChannels)
377 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
378 close(data->fd);
379 free(data);
380 return ALC_FALSE;
383 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
384 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
386 AL_PRINT("Could not set %d-bit input, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
387 close(data->fd);
388 free(data);
389 return ALC_FALSE;
392 data->ring = CreateRingBuffer(frameSize, device->BufferSize);
393 if(!data->ring)
395 AL_PRINT("ring buffer create failed\n");
396 close(data->fd);
397 free(data);
398 return ALC_FALSE;
401 data->data_size = info.fragsize;
402 data->mix_data = calloc(1, data->data_size);
404 device->ExtraData = data;
405 data->thread = StartThread(OSSCaptureProc, device);
406 if(data->thread == NULL)
408 device->ExtraData = NULL;
409 free(data->mix_data);
410 free(data);
411 return ALC_FALSE;
414 return ALC_TRUE;
417 static void oss_close_capture(ALCdevice *device)
419 oss_data *data = (oss_data*)device->ExtraData;
420 data->killNow = 1;
421 StopThread(data->thread);
423 close(data->fd);
425 DestroyRingBuffer(data->ring);
427 free(data->mix_data);
428 free(data);
429 device->ExtraData = NULL;
432 static void oss_start_capture(ALCdevice *pDevice)
434 oss_data *data = (oss_data*)pDevice->ExtraData;
435 data->doCapture = 1;
438 static void oss_stop_capture(ALCdevice *pDevice)
440 oss_data *data = (oss_data*)pDevice->ExtraData;
441 data->doCapture = 0;
444 static void oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
446 oss_data *data = (oss_data*)pDevice->ExtraData;
447 if(lSamples <= (ALCuint)RingBufferSize(data->ring))
448 ReadRingBuffer(data->ring, pBuffer, lSamples);
449 else
450 SetALCError(ALC_INVALID_VALUE);
453 static ALCuint oss_available_samples(ALCdevice *pDevice)
455 oss_data *data = (oss_data*)pDevice->ExtraData;
456 return RingBufferSize(data->ring);
460 BackendFuncs oss_funcs = {
461 oss_open_playback,
462 oss_close_playback,
463 oss_start_context,
464 oss_stop_context,
465 oss_open_capture,
466 oss_close_capture,
467 oss_start_capture,
468 oss_stop_capture,
469 oss_capture_samples,
470 oss_available_samples
473 void alc_oss_init(BackendFuncs *func_list)
475 *func_list = oss_funcs;
477 oss_device = AppendDeviceList("OSS Software");
478 AppendAllDeviceList(oss_device);
480 oss_device_capture = AppendCaptureDeviceList("OSS Capture");
483 void alc_oss_deinit(void)