Don't allow 0 periods
[openal-soft/openbsd.git] / Alc / oss.c
blob26be11d7636e724953abc9c3b856daa26fa269a1
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 <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <memory.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <math.h>
31 #include "alMain.h"
32 #include "AL/al.h"
33 #include "AL/alc.h"
35 #include <sys/soundcard.h>
38 * The OSS documentation talks about SOUND_MIXER_READ, but the header
39 * only contains MIXER_READ. Play safe. Same for WRITE.
41 #ifndef SOUND_MIXER_READ
42 #define SOUND_MIXER_READ MIXER_READ
43 #endif
44 #ifndef SOUND_MIXER_WRITE
45 #define SOUND_MIXER_WRITE MIXER_WRITE
46 #endif
48 static char *oss_device;
49 static char *oss_device_capture;
51 typedef struct {
52 int fd;
53 int killNow;
54 ALvoid *thread;
56 ALubyte *mix_data;
57 int data_size;
58 int silence;
60 RingBuffer *ring;
61 int doCapture;
62 } oss_data;
65 static int log2i(ALCuint x)
67 int y = 0;
68 while (x > 1)
70 x >>= 1;
71 y++;
73 return y;
77 static ALuint OSSProc(ALvoid *ptr)
79 ALCdevice *pDevice = (ALCdevice*)ptr;
80 oss_data *data = (oss_data*)pDevice->ExtraData;
81 int remaining = 0;
82 int wrote;
84 while(!data->killNow)
86 int len = data->data_size - remaining;
88 if(len > 0)
90 SuspendContext(NULL);
91 aluMixData(pDevice->Context, data->mix_data+remaining, len, pDevice->Format);
92 ProcessContext(NULL);
95 remaining += len;
96 wrote = write(data->fd, data->mix_data, remaining);
97 if(wrote < 0)
99 AL_PRINT("write failed: %s\n", strerror(errno));
100 remaining = 0;
102 else if(wrote > 0)
104 remaining -= wrote;
105 if(remaining > 0)
106 memmove(data->mix_data, data->mix_data+wrote, remaining);
108 else
109 usleep(1000);
112 return 0;
115 static ALuint OSSCaptureProc(ALvoid *ptr)
117 ALCdevice *pDevice = (ALCdevice*)ptr;
118 oss_data *data = (oss_data*)pDevice->ExtraData;
119 int frameSize;
120 int amt;
122 frameSize = aluBytesFromFormat(pDevice->Format);
123 frameSize *= aluChannelsFromFormat(pDevice->Format);
125 while(!data->killNow)
127 amt = read(data->fd, data->mix_data, data->data_size);
128 if(amt < 0)
130 AL_PRINT("read failed: %s\n", strerror(errno));
131 break;
133 if(amt == 0)
135 usleep(1000);
136 continue;
138 if(data->doCapture)
139 WriteRingBuffer(data->ring, data->mix_data, amt/frameSize);
142 return 0;
145 static ALCboolean oss_open_playback(ALCdevice *device, const ALCchar *deviceName)
147 int numFragmentsLogSize;
148 int log2FragmentSize;
149 unsigned int periods;
150 audio_buf_info info;
151 char driver[64];
152 int numChannels;
153 oss_data *data;
154 int ossFormat;
155 int ossSpeed;
156 char *err;
157 int i;
159 strncpy(driver, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver)-1);
160 driver[sizeof(driver)-1] = 0;
161 if(deviceName)
163 if(strcmp(deviceName, oss_device))
164 return ALC_FALSE;
165 device->szDeviceName = oss_device;
167 else
168 device->szDeviceName = oss_device;
170 data = (oss_data*)calloc(1, sizeof(oss_data));
171 data->killNow = 0;
173 data->fd = open(driver, O_WRONLY);
174 if(data->fd == -1)
176 free(data);
177 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
178 return ALC_FALSE;
181 switch(aluBytesFromFormat(device->Format))
183 case 1:
184 data->silence = 0x80;
185 ossFormat = AFMT_U8;
186 break;
187 case 2:
188 data->silence = 0;
189 ossFormat = AFMT_S16_NE;
190 break;
191 default:
192 ossFormat = -1;
193 AL_PRINT("Unknown format?! %x\n", device->Format);
196 periods = GetConfigValueInt("oss", "periods", 4);
197 if((int)periods <= 0)
198 periods = 4;
199 numChannels = device->Channels;
200 ossSpeed = device->Frequency;
201 log2FragmentSize = log2i(device->UpdateFreq * device->FrameSize / periods);
203 /* according to the OSS spec, 16 bytes are the minimum */
204 if (log2FragmentSize < 4)
205 log2FragmentSize = 4;
206 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
208 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
209 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
210 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
211 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
212 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
213 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
215 AL_PRINT("%s failed: %s\n", err, strerror(errno));
216 close(data->fd);
217 free(data);
218 return ALC_FALSE;
220 #undef ok
222 device->Frequency = ossSpeed;
224 if((int)device->Channels != numChannels)
226 AL_PRINT("Could not set %d channels, got %d instead\n", device->Channels, numChannels);
227 close(data->fd);
228 free(data);
229 return ALC_FALSE;
232 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
233 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
235 AL_PRINT("Could not set %d-bit output, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
236 close(data->fd);
237 free(data);
238 return ALC_FALSE;
241 device->UpdateFreq = info.fragsize / device->FrameSize;
243 data->data_size = device->UpdateFreq * device->FrameSize;
244 data->mix_data = calloc(1, data->data_size);
246 device->MaxNoOfSources = 256;
248 device->ExtraData = data;
249 data->thread = StartThread(OSSProc, device);
250 if(data->thread == NULL)
252 device->ExtraData = NULL;
253 free(data->mix_data);
254 free(data);
255 return ALC_FALSE;
258 return ALC_TRUE;
261 static void oss_close_playback(ALCdevice *device)
263 oss_data *data = (oss_data*)device->ExtraData;
264 data->killNow = 1;
265 StopThread(data->thread);
267 close(data->fd);
269 free(data->mix_data);
270 free(data);
271 device->ExtraData = NULL;
275 static ALCboolean oss_open_capture(ALCdevice *device, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
277 int numFragmentsLogSize;
278 int log2FragmentSize;
279 unsigned int periods;
280 audio_buf_info info;
281 int numChannels;
282 char driver[64];
283 oss_data *data;
284 int ossFormat;
285 int ossSpeed;
286 char *err;
287 int i;
289 strncpy(driver, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver)-1);
290 driver[sizeof(driver)-1] = 0;
291 if(deviceName)
293 if(strcmp(deviceName, oss_device_capture))
294 return ALC_FALSE;
295 device->szDeviceName = oss_device_capture;
297 else
298 device->szDeviceName = oss_device_capture;
300 data = (oss_data*)calloc(1, sizeof(oss_data));
301 data->killNow = 0;
303 data->fd = open(driver, O_RDONLY);
304 if(data->fd == -1)
306 free(data);
307 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
308 return ALC_FALSE;
311 switch(aluBytesFromFormat(format))
313 case 1:
314 data->silence = 0x80;
315 ossFormat = AFMT_U8;
316 break;
317 case 2:
318 data->silence = 0;
319 ossFormat = AFMT_S16_NE;
320 break;
321 default:
322 ossFormat = -1;
323 AL_PRINT("Unknown format?! %x\n", device->Format);
326 periods = 4;
327 numChannels = device->Channels;
328 ossSpeed = frequency;
329 log2FragmentSize = log2i(device->UpdateFreq * device->FrameSize / periods);
331 /* according to the OSS spec, 16 bytes are the minimum */
332 if (log2FragmentSize < 4)
333 log2FragmentSize = 4;
334 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
336 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
337 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
338 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
339 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
340 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
341 ok(ioctl(data->fd, SNDCTL_DSP_GETISPACE, &info), "get space")))
343 AL_PRINT("%s failed: %s\n", err, strerror(errno));
344 close(data->fd);
345 free(data);
346 return ALC_FALSE;
348 #undef ok
350 device->Channels = numChannels;
351 device->Frequency = ossSpeed;
352 device->Format = 0;
353 if(ossFormat == AFMT_U8)
355 if(device->Channels == 1)
357 device->Format = AL_FORMAT_MONO8;
358 device->FrameSize = 1;
360 else if(device->Channels == 2)
362 device->Format = AL_FORMAT_STEREO8;
363 device->FrameSize = 2;
365 else if(device->Channels == 4)
367 device->Format = AL_FORMAT_QUAD8;
368 device->FrameSize = 4;
371 else if(ossFormat == AFMT_S16_NE)
373 if(device->Channels == 1)
375 device->Format = AL_FORMAT_MONO16;
376 device->FrameSize = 2;
378 else if(device->Channels == 2)
380 device->Format = AL_FORMAT_STEREO16;
381 device->FrameSize = 4;
383 else if(device->Channels == 4)
385 device->Format = AL_FORMAT_QUAD16;
386 device->FrameSize = 8;
390 if(!device->Format)
392 AL_PRINT("returned unknown format: %#x %d!\n", ossFormat, numChannels);
393 close(data->fd);
394 free(data);
395 return ALC_FALSE;
398 data->ring = CreateRingBuffer(device->FrameSize, SampleSize);
399 if(!data->ring)
401 AL_PRINT("ring buffer create failed\n");
402 close(data->fd);
403 free(data);
404 return ALC_FALSE;
407 device->UpdateFreq = info.fragsize / device->FrameSize;
409 data->data_size = device->UpdateFreq * device->FrameSize * info.fragments;
410 data->mix_data = calloc(1, data->data_size);
412 device->ExtraData = data;
413 data->thread = StartThread(OSSCaptureProc, device);
414 if(data->thread == NULL)
416 device->ExtraData = NULL;
417 free(data->mix_data);
418 free(data);
419 return ALC_FALSE;
422 return ALC_TRUE;
425 static void oss_close_capture(ALCdevice *device)
427 oss_data *data = (oss_data*)device->ExtraData;
428 data->killNow = 1;
429 StopThread(data->thread);
431 close(data->fd);
433 DestroyRingBuffer(data->ring);
435 free(data->mix_data);
436 free(data);
437 device->ExtraData = NULL;
440 static void oss_start_capture(ALCdevice *pDevice)
442 oss_data *data = (oss_data*)pDevice->ExtraData;
443 data->doCapture = 1;
446 static void oss_stop_capture(ALCdevice *pDevice)
448 oss_data *data = (oss_data*)pDevice->ExtraData;
449 data->doCapture = 0;
452 static void oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
454 oss_data *data = (oss_data*)pDevice->ExtraData;
455 if(lSamples <= (ALCuint)RingBufferSize(data->ring))
456 ReadRingBuffer(data->ring, pBuffer, lSamples);
457 else
458 SetALCError(ALC_INVALID_VALUE);
461 static ALCuint oss_available_samples(ALCdevice *pDevice)
463 oss_data *data = (oss_data*)pDevice->ExtraData;
464 return RingBufferSize(data->ring);
468 BackendFuncs oss_funcs = {
469 oss_open_playback,
470 oss_close_playback,
471 oss_open_capture,
472 oss_close_capture,
473 oss_start_capture,
474 oss_stop_capture,
475 oss_capture_samples,
476 oss_available_samples
479 void alc_oss_init(BackendFuncs *func_list)
481 *func_list = oss_funcs;
483 oss_device = AppendDeviceList("OSS Software");
484 AppendAllDeviceList(oss_device);
486 oss_device_capture = AppendCaptureDeviceList("OSS Capture");