Pass the frame count to aluMixData
[openal-soft.git] / Alc / oss.c
blobc7f5421eaa88a629f734d1e49adb59e86d1bf785
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 const ALCchar oss_device[] = "OSS Software";
51 static const ALCchar oss_device_capture[] = "OSS 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 ALint frameSize;
83 int wrote;
85 frameSize = aluChannelsFromFormat(pDevice->Format) *
86 aluBytesFromFormat(pDevice->Format);
88 while(!data->killNow && !pDevice->Connected)
90 ALint len = data->data_size;
91 ALubyte *WritePtr = data->mix_data;
93 SuspendContext(NULL);
94 aluMixData(pDevice->Context, WritePtr, len/frameSize, pDevice->Format);
95 ProcessContext(NULL);
97 while(len > 0 && !data->killNow)
99 wrote = write(data->fd, WritePtr, len);
100 if(wrote < 0)
102 if(errno != EAGAIN && errno != EWOULDBLOCK)
104 AL_PRINT("write failed: %s\n", strerror(errno));
105 aluHandleDisconnect(pDevice);
106 len = 0;
108 else
109 Sleep(1);
110 continue;
113 len -= wrote;
114 WritePtr += wrote;
118 return 0;
121 static ALuint OSSCaptureProc(ALvoid *ptr)
123 ALCdevice *pDevice = (ALCdevice*)ptr;
124 oss_data *data = (oss_data*)pDevice->ExtraData;
125 int frameSize;
126 int amt;
128 frameSize = aluBytesFromFormat(pDevice->Format);
129 frameSize *= aluChannelsFromFormat(pDevice->Format);
131 while(!data->killNow)
133 amt = read(data->fd, data->mix_data, data->data_size);
134 if(amt < 0)
136 AL_PRINT("read failed: %s\n", strerror(errno));
137 aluHandleDisconnect(pDevice);
138 break;
140 if(amt == 0)
142 Sleep(1);
143 continue;
145 if(data->doCapture)
146 WriteRingBuffer(data->ring, data->mix_data, amt/frameSize);
149 return 0;
152 static ALCboolean oss_open_playback(ALCdevice *device, const ALCchar *deviceName)
154 char driver[64];
155 oss_data *data;
157 strncpy(driver, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver)-1);
158 driver[sizeof(driver)-1] = 0;
159 if(!deviceName)
160 deviceName = oss_device;
161 else if(strcmp(deviceName, oss_device) != 0)
162 return ALC_FALSE;
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->szDeviceName = strdup(deviceName);
176 device->ExtraData = data;
177 return ALC_TRUE;
180 static void oss_close_playback(ALCdevice *device)
182 oss_data *data = (oss_data*)device->ExtraData;
184 close(data->fd);
185 free(data);
186 device->ExtraData = NULL;
189 static ALCboolean oss_start_context(ALCdevice *device, ALCcontext *context)
191 oss_data *data = (oss_data*)device->ExtraData;
192 int numFragmentsLogSize;
193 int log2FragmentSize;
194 unsigned int periods;
195 audio_buf_info info;
196 ALuint frameSize;
197 int numChannels;
198 int ossFormat;
199 int ossSpeed;
200 char *err;
201 int i;
202 (void)context;
204 switch(aluBytesFromFormat(device->Format))
206 case 1:
207 ossFormat = AFMT_U8;
208 break;
209 case 4:
210 switch(aluChannelsFromFormat(device->Format))
212 case 1: device->Format = AL_FORMAT_MONO16; break;
213 case 2: device->Format = AL_FORMAT_STEREO16; break;
214 case 4: device->Format = AL_FORMAT_QUAD16; break;
215 case 6: device->Format = AL_FORMAT_51CHN16; break;
216 case 7: device->Format = AL_FORMAT_61CHN16; break;
217 case 8: device->Format = AL_FORMAT_71CHN16; break;
219 /* fall-through */
220 case 2:
221 ossFormat = AFMT_S16_NE;
222 break;
223 default:
224 ossFormat = -1;
225 AL_PRINT("Unknown format?! %x\n", device->Format);
228 periods = GetConfigValueInt("oss", "periods", 4);
229 if((int)periods <= 0)
230 periods = 4;
231 numChannels = aluChannelsFromFormat(device->Format);
232 frameSize = numChannels * aluBytesFromFormat(device->Format);
234 ossSpeed = device->Frequency;
235 log2FragmentSize = log2i(device->BufferSize * frameSize / periods);
237 /* according to the OSS spec, 16 bytes are the minimum */
238 if (log2FragmentSize < 4)
239 log2FragmentSize = 4;
240 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
242 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
243 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
244 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
245 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
246 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
247 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
249 AL_PRINT("%s failed: %s\n", err, strerror(errno));
250 return ALC_FALSE;
252 #undef ok
254 if((int)aluChannelsFromFormat(device->Format) != numChannels)
256 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
257 return ALC_FALSE;
260 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
261 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
263 AL_PRINT("Could not set %d-bit output, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
264 return ALC_FALSE;
267 device->Frequency = ossSpeed;
268 device->UpdateSize = info.fragsize / frameSize;
270 data->data_size = device->UpdateSize * frameSize;
271 data->mix_data = calloc(1, data->data_size);
273 data->thread = StartThread(OSSProc, device);
274 if(data->thread == NULL)
276 free(data->mix_data);
277 data->mix_data = NULL;
278 return ALC_FALSE;
281 return ALC_TRUE;
284 static void oss_stop_context(ALCdevice *device, ALCcontext *context)
286 oss_data *data = (oss_data*)device->ExtraData;
287 (void)context;
289 if(!data->thread)
290 return;
292 data->killNow = 1;
293 StopThread(data->thread);
294 data->thread = NULL;
296 free(data->mix_data);
297 data->mix_data = NULL;
301 static ALCboolean oss_open_capture(ALCdevice *device, const ALCchar *deviceName)
303 int numFragmentsLogSize;
304 int log2FragmentSize;
305 unsigned int periods;
306 audio_buf_info info;
307 ALuint frameSize;
308 int numChannels;
309 char driver[64];
310 oss_data *data;
311 int ossFormat;
312 int ossSpeed;
313 char *err;
314 int i;
316 strncpy(driver, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver)-1);
317 driver[sizeof(driver)-1] = 0;
318 if(!deviceName)
319 deviceName = oss_device_capture;
320 else if(strcmp(deviceName, oss_device_capture) != 0)
321 return ALC_FALSE;
323 data = (oss_data*)calloc(1, sizeof(oss_data));
324 data->killNow = 0;
326 data->fd = open(driver, O_RDONLY);
327 if(data->fd == -1)
329 free(data);
330 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
331 return ALC_FALSE;
334 switch(aluBytesFromFormat(device->Format))
336 case 1:
337 ossFormat = AFMT_U8;
338 break;
339 case 2:
340 ossFormat = AFMT_S16_NE;
341 break;
342 default:
343 ossFormat = -1;
344 AL_PRINT("Unknown format?! %x\n", device->Format);
347 periods = 4;
348 numChannels = aluChannelsFromFormat(device->Format);
349 frameSize = numChannels * aluBytesFromFormat(device->Format);
350 ossSpeed = device->Frequency;
351 log2FragmentSize = log2i(device->BufferSize * frameSize / periods);
353 /* according to the OSS spec, 16 bytes are the minimum */
354 if (log2FragmentSize < 4)
355 log2FragmentSize = 4;
356 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
358 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
359 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
360 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
361 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
362 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
363 ok(ioctl(data->fd, SNDCTL_DSP_GETISPACE, &info), "get space")))
365 AL_PRINT("%s failed: %s\n", err, strerror(errno));
366 close(data->fd);
367 free(data);
368 return ALC_FALSE;
370 #undef ok
372 if((int)aluChannelsFromFormat(device->Format) != numChannels)
374 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
375 close(data->fd);
376 free(data);
377 return ALC_FALSE;
380 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
381 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
383 AL_PRINT("Could not set %d-bit input, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
384 close(data->fd);
385 free(data);
386 return ALC_FALSE;
389 data->ring = CreateRingBuffer(frameSize, device->BufferSize);
390 if(!data->ring)
392 AL_PRINT("ring buffer create failed\n");
393 close(data->fd);
394 free(data);
395 return ALC_FALSE;
398 data->data_size = info.fragsize;
399 data->mix_data = calloc(1, data->data_size);
401 device->ExtraData = data;
402 data->thread = StartThread(OSSCaptureProc, device);
403 if(data->thread == NULL)
405 device->ExtraData = NULL;
406 free(data->mix_data);
407 free(data);
408 return ALC_FALSE;
411 device->szDeviceName = strdup(deviceName);
412 return ALC_TRUE;
415 static void oss_close_capture(ALCdevice *device)
417 oss_data *data = (oss_data*)device->ExtraData;
418 data->killNow = 1;
419 StopThread(data->thread);
421 close(data->fd);
423 DestroyRingBuffer(data->ring);
425 free(data->mix_data);
426 free(data);
427 device->ExtraData = NULL;
430 static void oss_start_capture(ALCdevice *pDevice)
432 oss_data *data = (oss_data*)pDevice->ExtraData;
433 data->doCapture = 1;
436 static void oss_stop_capture(ALCdevice *pDevice)
438 oss_data *data = (oss_data*)pDevice->ExtraData;
439 data->doCapture = 0;
442 static void oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
444 oss_data *data = (oss_data*)pDevice->ExtraData;
445 if(lSamples <= (ALCuint)RingBufferSize(data->ring))
446 ReadRingBuffer(data->ring, pBuffer, lSamples);
447 else
448 SetALCError(ALC_INVALID_VALUE);
451 static ALCuint oss_available_samples(ALCdevice *pDevice)
453 oss_data *data = (oss_data*)pDevice->ExtraData;
454 return RingBufferSize(data->ring);
458 BackendFuncs oss_funcs = {
459 oss_open_playback,
460 oss_close_playback,
461 oss_start_context,
462 oss_stop_context,
463 oss_open_capture,
464 oss_close_capture,
465 oss_start_capture,
466 oss_stop_capture,
467 oss_capture_samples,
468 oss_available_samples
471 void alc_oss_init(BackendFuncs *func_list)
473 *func_list = oss_funcs;
476 void alc_oss_deinit(void)
480 void alc_oss_probe(int type)
482 if(type == DEVICE_PROBE)
483 AppendDeviceList(oss_device);
484 else if(type == ALL_DEVICE_PROBE)
485 AppendAllDeviceList(oss_device);
486 else if(type == CAPTURE_DEVICE_PROBE)
487 AppendCaptureDeviceList(oss_device_capture);