More modifications and fixes for context checks
[openal-soft.git] / Alc / oss.c
blob83f8fe554618d1fd70b3d95538e1a4fc68c2af9c
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)
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 len = 0;
103 else
104 Sleep(1);
105 continue;
108 len -= wrote;
109 WritePtr += wrote;
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 char driver[64];
149 oss_data *data;
151 strncpy(driver, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver)-1);
152 driver[sizeof(driver)-1] = 0;
153 if(deviceName)
155 if(strcmp(deviceName, oss_device))
156 return ALC_FALSE;
157 device->szDeviceName = oss_device;
159 else
160 device->szDeviceName = oss_device;
162 data = (oss_data*)calloc(1, sizeof(oss_data));
163 data->killNow = 0;
165 data->fd = open(driver, O_WRONLY);
166 if(data->fd == -1)
168 free(data);
169 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
170 return ALC_FALSE;
173 device->ExtraData = data;
174 return ALC_TRUE;
177 static void oss_close_playback(ALCdevice *device)
179 oss_data *data = (oss_data*)device->ExtraData;
181 close(data->fd);
182 free(data);
183 device->ExtraData = NULL;
186 static ALCboolean oss_start_context(ALCdevice *device, ALCcontext *context)
188 oss_data *data = (oss_data*)device->ExtraData;
189 int numFragmentsLogSize;
190 int log2FragmentSize;
191 unsigned int periods;
192 audio_buf_info info;
193 ALuint frameSize;
194 int numChannels;
195 int ossFormat;
196 int ossSpeed;
197 char *err;
198 int i;
199 (void)context;
201 switch(aluBytesFromFormat(device->Format))
203 case 1:
204 ossFormat = AFMT_U8;
205 break;
206 case 4:
207 switch(aluChannelsFromFormat(device->Format))
209 case 1: device->Format = AL_FORMAT_MONO16; break;
210 case 2: device->Format = AL_FORMAT_STEREO16; break;
211 case 4: device->Format = AL_FORMAT_QUAD16; break;
212 case 6: device->Format = AL_FORMAT_51CHN16; break;
213 case 7: device->Format = AL_FORMAT_61CHN16; break;
214 case 8: device->Format = AL_FORMAT_71CHN16; break;
216 /* fall-through */
217 case 2:
218 ossFormat = AFMT_S16_NE;
219 break;
220 default:
221 ossFormat = -1;
222 AL_PRINT("Unknown format?! %x\n", device->Format);
225 periods = GetConfigValueInt("oss", "periods", 4);
226 if((int)periods <= 0)
227 periods = 4;
228 numChannels = aluChannelsFromFormat(device->Format);
229 frameSize = numChannels * aluBytesFromFormat(device->Format);
231 ossSpeed = device->Frequency;
232 log2FragmentSize = log2i(device->BufferSize * frameSize / periods);
234 /* according to the OSS spec, 16 bytes are the minimum */
235 if (log2FragmentSize < 4)
236 log2FragmentSize = 4;
237 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
239 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
240 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
241 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
242 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
243 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
244 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
246 AL_PRINT("%s failed: %s\n", err, strerror(errno));
247 return ALC_FALSE;
249 #undef ok
251 if((int)aluChannelsFromFormat(device->Format) != numChannels)
253 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
254 return ALC_FALSE;
257 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
258 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
260 AL_PRINT("Could not set %d-bit output, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
261 return ALC_FALSE;
264 device->Frequency = ossSpeed;
265 device->UpdateSize = info.fragsize / frameSize;
267 data->data_size = device->UpdateSize * frameSize;
268 data->mix_data = calloc(1, data->data_size);
270 data->thread = StartThread(OSSProc, device);
271 if(data->thread == NULL)
273 free(data->mix_data);
274 data->mix_data = NULL;
275 return ALC_FALSE;
278 return ALC_TRUE;
281 static void oss_stop_context(ALCdevice *device, ALCcontext *context)
283 oss_data *data = (oss_data*)device->ExtraData;
284 (void)context;
286 if(!data->thread)
287 return;
289 data->killNow = 1;
290 StopThread(data->thread);
291 data->thread = NULL;
293 free(data->mix_data);
294 data->mix_data = NULL;
298 static ALCboolean oss_open_capture(ALCdevice *device, const ALCchar *deviceName)
300 int numFragmentsLogSize;
301 int log2FragmentSize;
302 unsigned int periods;
303 audio_buf_info info;
304 ALuint frameSize;
305 int numChannels;
306 char driver[64];
307 oss_data *data;
308 int ossFormat;
309 int ossSpeed;
310 char *err;
311 int i;
313 strncpy(driver, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver)-1);
314 driver[sizeof(driver)-1] = 0;
315 if(deviceName)
317 if(strcmp(deviceName, oss_device_capture))
318 return ALC_FALSE;
319 device->szDeviceName = oss_device_capture;
321 else
322 device->szDeviceName = oss_device_capture;
324 data = (oss_data*)calloc(1, sizeof(oss_data));
325 data->killNow = 0;
327 data->fd = open(driver, O_RDONLY);
328 if(data->fd == -1)
330 free(data);
331 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
332 return ALC_FALSE;
335 switch(aluBytesFromFormat(device->Format))
337 case 1:
338 ossFormat = AFMT_U8;
339 break;
340 case 2:
341 ossFormat = AFMT_S16_NE;
342 break;
343 default:
344 ossFormat = -1;
345 AL_PRINT("Unknown format?! %x\n", device->Format);
348 periods = 4;
349 numChannels = aluChannelsFromFormat(device->Format);
350 frameSize = numChannels * aluBytesFromFormat(device->Format);
351 ossSpeed = device->Frequency;
352 log2FragmentSize = log2i(device->BufferSize * frameSize / periods);
354 /* according to the OSS spec, 16 bytes are the minimum */
355 if (log2FragmentSize < 4)
356 log2FragmentSize = 4;
357 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
359 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
360 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
361 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
362 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
363 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
364 ok(ioctl(data->fd, SNDCTL_DSP_GETISPACE, &info), "get space")))
366 AL_PRINT("%s failed: %s\n", err, strerror(errno));
367 close(data->fd);
368 free(data);
369 return ALC_FALSE;
371 #undef ok
373 if((int)aluChannelsFromFormat(device->Format) != numChannels)
375 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
376 close(data->fd);
377 free(data);
378 return ALC_FALSE;
381 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
382 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
384 AL_PRINT("Could not set %d-bit input, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
385 close(data->fd);
386 free(data);
387 return ALC_FALSE;
390 data->ring = CreateRingBuffer(frameSize, device->BufferSize);
391 if(!data->ring)
393 AL_PRINT("ring buffer create failed\n");
394 close(data->fd);
395 free(data);
396 return ALC_FALSE;
399 data->data_size = info.fragsize;
400 data->mix_data = calloc(1, data->data_size);
402 device->ExtraData = data;
403 data->thread = StartThread(OSSCaptureProc, device);
404 if(data->thread == NULL)
406 device->ExtraData = NULL;
407 free(data->mix_data);
408 free(data);
409 return ALC_FALSE;
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;
475 oss_device = AppendDeviceList("OSS Software");
476 AppendAllDeviceList(oss_device);
478 oss_device_capture = AppendCaptureDeviceList("OSS Capture");