Add missing config.h includes
[openal-soft.git] / Alc / oss.c
blob2fc7c36d3cf166683b3814004b33e1be9d76a88e
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 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 remaining = 0;
83 int wrote;
85 while(!data->killNow)
87 int len = data->data_size - remaining;
89 if(len > 0)
91 SuspendContext(NULL);
92 aluMixData(pDevice->Context, data->mix_data+remaining, len, pDevice->Format);
93 ProcessContext(NULL);
96 remaining += len;
97 wrote = write(data->fd, data->mix_data, remaining);
98 if(wrote < 0)
100 AL_PRINT("write failed: %s\n", strerror(errno));
101 remaining = 0;
103 else if(wrote > 0)
105 remaining -= wrote;
106 if(remaining > 0)
107 memmove(data->mix_data, data->mix_data+wrote, remaining);
109 else
110 Sleep(1);
113 return 0;
116 static ALuint OSSCaptureProc(ALvoid *ptr)
118 ALCdevice *pDevice = (ALCdevice*)ptr;
119 oss_data *data = (oss_data*)pDevice->ExtraData;
120 int frameSize;
121 int amt;
123 frameSize = aluBytesFromFormat(pDevice->Format);
124 frameSize *= aluChannelsFromFormat(pDevice->Format);
126 while(!data->killNow)
128 amt = read(data->fd, data->mix_data, data->data_size);
129 if(amt < 0)
131 AL_PRINT("read failed: %s\n", strerror(errno));
132 break;
134 if(amt == 0)
136 Sleep(1);
137 continue;
139 if(data->doCapture)
140 WriteRingBuffer(data->ring, data->mix_data, amt/frameSize);
143 return 0;
146 static ALCboolean oss_open_playback(ALCdevice *device, const ALCchar *deviceName)
148 int numFragmentsLogSize;
149 int log2FragmentSize;
150 unsigned int periods;
151 audio_buf_info info;
152 char driver[64];
153 int numChannels;
154 oss_data *data;
155 int ossFormat;
156 int ossSpeed;
157 char *err;
158 int i;
160 strncpy(driver, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver)-1);
161 driver[sizeof(driver)-1] = 0;
162 if(deviceName)
164 if(strcmp(deviceName, oss_device))
165 return ALC_FALSE;
166 device->szDeviceName = oss_device;
168 else
169 device->szDeviceName = oss_device;
171 data = (oss_data*)calloc(1, sizeof(oss_data));
172 data->killNow = 0;
174 data->fd = open(driver, O_WRONLY);
175 if(data->fd == -1)
177 free(data);
178 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
179 return ALC_FALSE;
182 switch(aluBytesFromFormat(device->Format))
184 case 1:
185 ossFormat = AFMT_U8;
186 break;
187 case 2:
188 ossFormat = AFMT_S16_NE;
189 break;
190 default:
191 ossFormat = -1;
192 AL_PRINT("Unknown format?! %x\n", device->Format);
195 periods = GetConfigValueInt("oss", "periods", 4);
196 if((int)periods <= 0)
197 periods = 4;
198 numChannels = device->Channels;
199 ossSpeed = device->Frequency;
200 log2FragmentSize = log2i(device->UpdateFreq * device->FrameSize / periods);
202 /* according to the OSS spec, 16 bytes are the minimum */
203 if (log2FragmentSize < 4)
204 log2FragmentSize = 4;
205 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
207 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
208 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
209 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
210 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
211 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
212 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
214 AL_PRINT("%s failed: %s\n", err, strerror(errno));
215 close(data->fd);
216 free(data);
217 return ALC_FALSE;
219 #undef ok
221 device->Frequency = ossSpeed;
223 if((int)device->Channels != numChannels)
225 AL_PRINT("Could not set %d channels, got %d instead\n", device->Channels, numChannels);
226 close(data->fd);
227 free(data);
228 return ALC_FALSE;
231 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
232 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
234 AL_PRINT("Could not set %d-bit output, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
235 close(data->fd);
236 free(data);
237 return ALC_FALSE;
240 device->UpdateFreq = info.fragsize / device->FrameSize;
242 data->data_size = device->UpdateFreq * device->FrameSize;
243 data->mix_data = calloc(1, data->data_size);
245 device->MaxNoOfSources = 256;
247 device->ExtraData = data;
248 data->thread = StartThread(OSSProc, device);
249 if(data->thread == NULL)
251 device->ExtraData = NULL;
252 free(data->mix_data);
253 free(data);
254 return ALC_FALSE;
257 return ALC_TRUE;
260 static void oss_close_playback(ALCdevice *device)
262 oss_data *data = (oss_data*)device->ExtraData;
263 data->killNow = 1;
264 StopThread(data->thread);
266 close(data->fd);
268 free(data->mix_data);
269 free(data);
270 device->ExtraData = NULL;
274 static ALCboolean oss_open_capture(ALCdevice *device, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
276 int numFragmentsLogSize;
277 int log2FragmentSize;
278 unsigned int periods;
279 audio_buf_info info;
280 int numChannels;
281 char driver[64];
282 oss_data *data;
283 int ossFormat;
284 int ossSpeed;
285 char *err;
286 int i;
288 strncpy(driver, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver)-1);
289 driver[sizeof(driver)-1] = 0;
290 if(deviceName)
292 if(strcmp(deviceName, oss_device_capture))
293 return ALC_FALSE;
294 device->szDeviceName = oss_device_capture;
296 else
297 device->szDeviceName = oss_device_capture;
299 data = (oss_data*)calloc(1, sizeof(oss_data));
300 data->killNow = 0;
302 data->fd = open(driver, O_RDONLY);
303 if(data->fd == -1)
305 free(data);
306 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
307 return ALC_FALSE;
310 switch(aluBytesFromFormat(format))
312 case 1:
313 ossFormat = AFMT_U8;
314 break;
315 case 2:
316 ossFormat = AFMT_S16_NE;
317 break;
318 default:
319 ossFormat = -1;
320 AL_PRINT("Unknown format?! %x\n", device->Format);
323 periods = 4;
324 numChannels = device->Channels;
325 ossSpeed = frequency;
326 log2FragmentSize = log2i(device->UpdateFreq * device->FrameSize / periods);
328 /* according to the OSS spec, 16 bytes are the minimum */
329 if (log2FragmentSize < 4)
330 log2FragmentSize = 4;
331 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
333 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
334 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
335 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
336 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
337 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
338 ok(ioctl(data->fd, SNDCTL_DSP_GETISPACE, &info), "get space")))
340 AL_PRINT("%s failed: %s\n", err, strerror(errno));
341 close(data->fd);
342 free(data);
343 return ALC_FALSE;
345 #undef ok
347 device->Channels = numChannels;
348 device->Frequency = ossSpeed;
349 device->Format = 0;
350 if(ossFormat == AFMT_U8)
352 if(device->Channels == 1)
354 device->Format = AL_FORMAT_MONO8;
355 device->FrameSize = 1;
357 else if(device->Channels == 2)
359 device->Format = AL_FORMAT_STEREO8;
360 device->FrameSize = 2;
362 else if(device->Channels == 4)
364 device->Format = AL_FORMAT_QUAD8;
365 device->FrameSize = 4;
368 else if(ossFormat == AFMT_S16_NE)
370 if(device->Channels == 1)
372 device->Format = AL_FORMAT_MONO16;
373 device->FrameSize = 2;
375 else if(device->Channels == 2)
377 device->Format = AL_FORMAT_STEREO16;
378 device->FrameSize = 4;
380 else if(device->Channels == 4)
382 device->Format = AL_FORMAT_QUAD16;
383 device->FrameSize = 8;
387 if(!device->Format)
389 AL_PRINT("returned unknown format: %#x %d!\n", ossFormat, numChannels);
390 close(data->fd);
391 free(data);
392 return ALC_FALSE;
395 data->ring = CreateRingBuffer(device->FrameSize, SampleSize);
396 if(!data->ring)
398 AL_PRINT("ring buffer create failed\n");
399 close(data->fd);
400 free(data);
401 return ALC_FALSE;
404 device->UpdateFreq = info.fragsize / device->FrameSize;
406 data->data_size = device->UpdateFreq * device->FrameSize * info.fragments;
407 data->mix_data = calloc(1, data->data_size);
409 device->ExtraData = data;
410 data->thread = StartThread(OSSCaptureProc, device);
411 if(data->thread == NULL)
413 device->ExtraData = NULL;
414 free(data->mix_data);
415 free(data);
416 return ALC_FALSE;
419 return ALC_TRUE;
422 static void oss_close_capture(ALCdevice *device)
424 oss_data *data = (oss_data*)device->ExtraData;
425 data->killNow = 1;
426 StopThread(data->thread);
428 close(data->fd);
430 DestroyRingBuffer(data->ring);
432 free(data->mix_data);
433 free(data);
434 device->ExtraData = NULL;
437 static void oss_start_capture(ALCdevice *pDevice)
439 oss_data *data = (oss_data*)pDevice->ExtraData;
440 data->doCapture = 1;
443 static void oss_stop_capture(ALCdevice *pDevice)
445 oss_data *data = (oss_data*)pDevice->ExtraData;
446 data->doCapture = 0;
449 static void oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
451 oss_data *data = (oss_data*)pDevice->ExtraData;
452 if(lSamples <= (ALCuint)RingBufferSize(data->ring))
453 ReadRingBuffer(data->ring, pBuffer, lSamples);
454 else
455 SetALCError(ALC_INVALID_VALUE);
458 static ALCuint oss_available_samples(ALCdevice *pDevice)
460 oss_data *data = (oss_data*)pDevice->ExtraData;
461 return RingBufferSize(data->ring);
465 BackendFuncs oss_funcs = {
466 oss_open_playback,
467 oss_close_playback,
468 oss_open_capture,
469 oss_close_capture,
470 oss_start_capture,
471 oss_stop_capture,
472 oss_capture_samples,
473 oss_available_samples
476 void alc_oss_init(BackendFuncs *func_list)
478 *func_list = oss_funcs;
480 oss_device = AppendDeviceList("OSS Software");
481 AppendAllDeviceList(oss_device);
483 oss_device_capture = AppendCaptureDeviceList("OSS Capture");