Don't needlessly expose a variable for the backends
[openal-soft/android/lowlatency.git] / Alc / oss.c
blob5fee7734c0ba77170ecb9329265bcb5cb0f3c1a0
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 ssize_t wrote;
85 SetRTPriority();
87 frameSize = aluChannelsFromFormat(pDevice->Format) *
88 aluBytesFromFormat(pDevice->Format);
90 while(!data->killNow && pDevice->Connected)
92 ALint len = data->data_size;
93 ALubyte *WritePtr = data->mix_data;
95 aluMixData(pDevice, WritePtr, len/frameSize);
96 while(len > 0 && !data->killNow)
98 wrote = write(data->fd, WritePtr, len);
99 if(wrote < 0)
101 if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
103 AL_PRINT("write failed: %s\n", strerror(errno));
104 aluHandleDisconnect(pDevice);
105 break;
108 Sleep(1);
109 continue;
112 len -= wrote;
113 WritePtr += wrote;
117 return 0;
120 static ALuint OSSCaptureProc(ALvoid *ptr)
122 ALCdevice *pDevice = (ALCdevice*)ptr;
123 oss_data *data = (oss_data*)pDevice->ExtraData;
124 int frameSize;
125 int amt;
127 SetRTPriority();
129 frameSize = aluBytesFromFormat(pDevice->Format);
130 frameSize *= aluChannelsFromFormat(pDevice->Format);
132 while(!data->killNow)
134 amt = read(data->fd, data->mix_data, data->data_size);
135 if(amt < 0)
137 AL_PRINT("read failed: %s\n", strerror(errno));
138 aluHandleDisconnect(pDevice);
139 break;
141 if(amt == 0)
143 Sleep(1);
144 continue;
146 if(data->doCapture)
147 WriteRingBuffer(data->ring, data->mix_data, amt/frameSize);
150 return 0;
153 static ALCboolean oss_open_playback(ALCdevice *device, const ALCchar *deviceName)
155 char driver[64];
156 oss_data *data;
158 strncpy(driver, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver)-1);
159 driver[sizeof(driver)-1] = 0;
160 if(!deviceName)
161 deviceName = oss_device;
162 else if(strcmp(deviceName, oss_device) != 0)
163 return ALC_FALSE;
165 data = (oss_data*)calloc(1, sizeof(oss_data));
166 data->killNow = 0;
168 data->fd = open(driver, O_WRONLY);
169 if(data->fd == -1)
171 free(data);
172 if(errno != ENOENT)
173 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
174 return ALC_FALSE;
177 device->szDeviceName = strdup(deviceName);
178 device->ExtraData = data;
179 return ALC_TRUE;
182 static void oss_close_playback(ALCdevice *device)
184 oss_data *data = (oss_data*)device->ExtraData;
186 close(data->fd);
187 free(data);
188 device->ExtraData = NULL;
191 static ALCboolean oss_reset_playback(ALCdevice *device)
193 oss_data *data = (oss_data*)device->ExtraData;
194 int numFragmentsLogSize;
195 int log2FragmentSize;
196 unsigned int periods;
197 audio_buf_info info;
198 ALuint frameSize;
199 int numChannels;
200 int ossFormat;
201 int ossSpeed;
202 char *err;
203 int i;
205 switch(aluBytesFromFormat(device->Format))
207 case 1:
208 ossFormat = AFMT_U8;
209 break;
210 case 4:
211 switch(aluChannelsFromFormat(device->Format))
213 case 1: device->Format = AL_FORMAT_MONO16; break;
214 case 2: device->Format = AL_FORMAT_STEREO16; break;
215 case 4: device->Format = AL_FORMAT_QUAD16; break;
216 case 6: device->Format = AL_FORMAT_51CHN16; break;
217 case 7: device->Format = AL_FORMAT_61CHN16; break;
218 case 8: device->Format = AL_FORMAT_71CHN16; break;
220 /* fall-through */
221 case 2:
222 ossFormat = AFMT_S16_NE;
223 break;
224 default:
225 AL_PRINT("Unknown format: 0x%x\n", device->Format);
226 return ALC_FALSE;
229 periods = device->NumUpdates;
230 numChannels = aluChannelsFromFormat(device->Format);
231 frameSize = numChannels * aluBytesFromFormat(device->Format);
233 ossSpeed = device->Frequency;
234 log2FragmentSize = log2i(device->UpdateSize * frameSize);
236 /* according to the OSS spec, 16 bytes are the minimum */
237 if (log2FragmentSize < 4)
238 log2FragmentSize = 4;
239 /* Subtract one period since the temp mixing buffer counts as one. Still
240 * need at least two on the card, though. */
241 if(periods > 2) periods--;
242 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
244 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
245 /* Don't fail if SETFRAGMENT fails. We can handle just about anything
246 * that's reported back via GETOSPACE */
247 ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize);
248 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
249 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
250 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
251 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
253 AL_PRINT("%s failed: %s\n", err, strerror(errno));
254 return ALC_FALSE;
256 #undef ok
258 if((int)aluChannelsFromFormat(device->Format) != numChannels)
260 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
261 return ALC_FALSE;
264 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
265 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
267 AL_PRINT("Could not set %d-bit output, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
268 return ALC_FALSE;
271 device->Frequency = ossSpeed;
272 device->UpdateSize = info.fragsize / frameSize;
273 device->NumUpdates = info.fragments + 1;
275 data->data_size = device->UpdateSize * frameSize;
276 data->mix_data = calloc(1, data->data_size);
278 SetDefaultChannelOrder(device);
280 data->thread = StartThread(OSSProc, device);
281 if(data->thread == NULL)
283 free(data->mix_data);
284 data->mix_data = NULL;
285 return ALC_FALSE;
288 return ALC_TRUE;
291 static void oss_stop_playback(ALCdevice *device)
293 oss_data *data = (oss_data*)device->ExtraData;
295 if(!data->thread)
296 return;
298 data->killNow = 1;
299 StopThread(data->thread);
300 data->thread = NULL;
302 data->killNow = 0;
303 if(ioctl(data->fd, SNDCTL_DSP_RESET) != 0)
304 AL_PRINT("Error resetting device: %s\n", strerror(errno));
306 free(data->mix_data);
307 data->mix_data = NULL;
311 static ALCboolean oss_open_capture(ALCdevice *device, const ALCchar *deviceName)
313 int numFragmentsLogSize;
314 int log2FragmentSize;
315 unsigned int periods;
316 audio_buf_info info;
317 ALuint frameSize;
318 int numChannels;
319 char driver[64];
320 oss_data *data;
321 int ossFormat;
322 int ossSpeed;
323 char *err;
324 int i;
326 strncpy(driver, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver)-1);
327 driver[sizeof(driver)-1] = 0;
328 if(!deviceName)
329 deviceName = oss_device_capture;
330 else if(strcmp(deviceName, oss_device_capture) != 0)
331 return ALC_FALSE;
333 data = (oss_data*)calloc(1, sizeof(oss_data));
334 data->killNow = 0;
336 data->fd = open(driver, O_RDONLY);
337 if(data->fd == -1)
339 free(data);
340 if(errno != ENOENT)
341 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
342 return ALC_FALSE;
345 switch(aluBytesFromFormat(device->Format))
347 case 1:
348 ossFormat = AFMT_U8;
349 break;
350 case 2:
351 ossFormat = AFMT_S16_NE;
352 break;
353 default:
354 AL_PRINT("Unknown format: 0x%x\n", device->Format);
355 close(data->fd);
356 free(data);
357 return ALC_FALSE;
360 periods = 4;
361 numChannels = aluChannelsFromFormat(device->Format);
362 frameSize = numChannels * aluBytesFromFormat(device->Format);
363 ossSpeed = device->Frequency;
364 log2FragmentSize = log2i(device->UpdateSize * device->NumUpdates *
365 frameSize / periods);
367 /* according to the OSS spec, 16 bytes are the minimum */
368 if (log2FragmentSize < 4)
369 log2FragmentSize = 4;
370 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
372 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
373 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
374 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
375 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
376 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
377 ok(ioctl(data->fd, SNDCTL_DSP_GETISPACE, &info), "get space")))
379 AL_PRINT("%s failed: %s\n", err, strerror(errno));
380 close(data->fd);
381 free(data);
382 return ALC_FALSE;
384 #undef ok
386 if((int)aluChannelsFromFormat(device->Format) != numChannels)
388 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
389 close(data->fd);
390 free(data);
391 return ALC_FALSE;
394 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
395 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
397 AL_PRINT("Could not set %d-bit input, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
398 close(data->fd);
399 free(data);
400 return ALC_FALSE;
403 data->ring = CreateRingBuffer(frameSize, device->UpdateSize * device->NumUpdates);
404 if(!data->ring)
406 AL_PRINT("ring buffer create failed\n");
407 close(data->fd);
408 free(data);
409 return ALC_FALSE;
412 data->data_size = info.fragsize;
413 data->mix_data = calloc(1, data->data_size);
415 device->ExtraData = data;
416 data->thread = StartThread(OSSCaptureProc, device);
417 if(data->thread == NULL)
419 device->ExtraData = NULL;
420 free(data->mix_data);
421 free(data);
422 return ALC_FALSE;
425 device->szDeviceName = strdup(deviceName);
426 return ALC_TRUE;
429 static void oss_close_capture(ALCdevice *device)
431 oss_data *data = (oss_data*)device->ExtraData;
432 data->killNow = 1;
433 StopThread(data->thread);
435 close(data->fd);
437 DestroyRingBuffer(data->ring);
439 free(data->mix_data);
440 free(data);
441 device->ExtraData = NULL;
444 static void oss_start_capture(ALCdevice *pDevice)
446 oss_data *data = (oss_data*)pDevice->ExtraData;
447 data->doCapture = 1;
450 static void oss_stop_capture(ALCdevice *pDevice)
452 oss_data *data = (oss_data*)pDevice->ExtraData;
453 data->doCapture = 0;
456 static void oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
458 oss_data *data = (oss_data*)pDevice->ExtraData;
459 if(lSamples <= (ALCuint)RingBufferSize(data->ring))
460 ReadRingBuffer(data->ring, pBuffer, lSamples);
461 else
462 alcSetError(pDevice, ALC_INVALID_VALUE);
465 static ALCuint oss_available_samples(ALCdevice *pDevice)
467 oss_data *data = (oss_data*)pDevice->ExtraData;
468 return RingBufferSize(data->ring);
472 BackendFuncs oss_funcs = {
473 oss_open_playback,
474 oss_close_playback,
475 oss_reset_playback,
476 oss_stop_playback,
477 oss_open_capture,
478 oss_close_capture,
479 oss_start_capture,
480 oss_stop_capture,
481 oss_capture_samples,
482 oss_available_samples
485 void alc_oss_init(BackendFuncs *func_list)
487 *func_list = oss_funcs;
490 void alc_oss_deinit(void)
494 void alc_oss_probe(int type)
496 if(type == DEVICE_PROBE)
498 #ifdef HAVE_STAT
499 struct stat buf;
500 if(stat(GetConfigValue("oss", "device", "/dev/dsp"), &buf) == 0)
501 #endif
502 AppendDeviceList(oss_device);
504 else if(type == ALL_DEVICE_PROBE)
506 #ifdef HAVE_STAT
507 struct stat buf;
508 if(stat(GetConfigValue("oss", "device", "/dev/dsp"), &buf) == 0)
509 #endif
510 AppendAllDeviceList(oss_device);
512 else if(type == CAPTURE_DEVICE_PROBE)
514 #ifdef HAVE_STAT
515 struct stat buf;
516 if(stat(GetConfigValue("oss", "capture", "/dev/dsp"), &buf) == 0)
517 #endif
518 AppendCaptureDeviceList(oss_device_capture);