Dynamically load PulseAudio
[openal-soft.git] / Alc / oss.c
blobb1ec7887936ce3a00adf93d48b5f311d19e0a958
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 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 ALuint frameSize;
153 char driver[64];
154 int numChannels;
155 oss_data *data;
156 int ossFormat;
157 int ossSpeed;
158 char *err;
159 int i;
161 strncpy(driver, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver)-1);
162 driver[sizeof(driver)-1] = 0;
163 if(deviceName)
165 if(strcmp(deviceName, oss_device))
166 return ALC_FALSE;
167 device->szDeviceName = oss_device;
169 else
170 device->szDeviceName = oss_device;
172 data = (oss_data*)calloc(1, sizeof(oss_data));
173 data->killNow = 0;
175 data->fd = open(driver, O_WRONLY);
176 if(data->fd == -1)
178 free(data);
179 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
180 return ALC_FALSE;
183 switch(aluBytesFromFormat(device->Format))
185 case 1:
186 ossFormat = AFMT_U8;
187 break;
188 case 2:
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 = aluChannelsFromFormat(device->Format);
200 frameSize = numChannels * aluBytesFromFormat(device->Format);
202 ossSpeed = device->Frequency;
203 log2FragmentSize = log2i(device->UpdateSize * frameSize / periods);
205 /* according to the OSS spec, 16 bytes are the minimum */
206 if (log2FragmentSize < 4)
207 log2FragmentSize = 4;
208 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
210 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
211 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
212 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
213 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
214 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
215 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
217 AL_PRINT("%s failed: %s\n", err, strerror(errno));
218 close(data->fd);
219 free(data);
220 return ALC_FALSE;
222 #undef ok
224 if((int)aluChannelsFromFormat(device->Format) != numChannels)
226 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), 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 data->data_size = device->UpdateSize * frameSize;
242 data->mix_data = calloc(1, data->data_size);
244 device->ExtraData = data;
245 data->thread = StartThread(OSSProc, device);
246 if(data->thread == NULL)
248 device->ExtraData = NULL;
249 free(data->mix_data);
250 free(data);
251 return ALC_FALSE;
254 device->Frequency = ossSpeed;
255 device->UpdateSize = info.fragsize / frameSize;
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 ALuint frameSize;
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 ossFormat = AFMT_U8;
315 break;
316 case 2:
317 ossFormat = AFMT_S16_NE;
318 break;
319 default:
320 ossFormat = -1;
321 AL_PRINT("Unknown format?! %x\n", device->Format);
324 periods = 4;
325 numChannels = aluChannelsFromFormat(device->Format);
326 frameSize = numChannels * aluBytesFromFormat(device->Format);
327 ossSpeed = frequency;
328 log2FragmentSize = log2i(SampleSize * frameSize / periods);
330 /* according to the OSS spec, 16 bytes are the minimum */
331 if (log2FragmentSize < 4)
332 log2FragmentSize = 4;
333 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
335 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
336 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
337 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
338 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
339 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
340 ok(ioctl(data->fd, SNDCTL_DSP_GETISPACE, &info), "get space")))
342 AL_PRINT("%s failed: %s\n", err, strerror(errno));
343 close(data->fd);
344 free(data);
345 return ALC_FALSE;
347 #undef ok
349 if((int)aluChannelsFromFormat(device->Format) != numChannels)
351 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
352 close(data->fd);
353 free(data);
354 return ALC_FALSE;
357 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
358 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
360 AL_PRINT("Could not set %d-bit input, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
361 close(data->fd);
362 free(data);
363 return ALC_FALSE;
366 data->ring = CreateRingBuffer(frameSize, SampleSize);
367 if(!data->ring)
369 AL_PRINT("ring buffer create failed\n");
370 close(data->fd);
371 free(data);
372 return ALC_FALSE;
375 data->data_size = info.fragsize;
376 data->mix_data = calloc(1, data->data_size);
378 device->ExtraData = data;
379 data->thread = StartThread(OSSCaptureProc, device);
380 if(data->thread == NULL)
382 device->ExtraData = NULL;
383 free(data->mix_data);
384 free(data);
385 return ALC_FALSE;
388 return ALC_TRUE;
391 static void oss_close_capture(ALCdevice *device)
393 oss_data *data = (oss_data*)device->ExtraData;
394 data->killNow = 1;
395 StopThread(data->thread);
397 close(data->fd);
399 DestroyRingBuffer(data->ring);
401 free(data->mix_data);
402 free(data);
403 device->ExtraData = NULL;
406 static void oss_start_capture(ALCdevice *pDevice)
408 oss_data *data = (oss_data*)pDevice->ExtraData;
409 data->doCapture = 1;
412 static void oss_stop_capture(ALCdevice *pDevice)
414 oss_data *data = (oss_data*)pDevice->ExtraData;
415 data->doCapture = 0;
418 static void oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
420 oss_data *data = (oss_data*)pDevice->ExtraData;
421 if(lSamples <= (ALCuint)RingBufferSize(data->ring))
422 ReadRingBuffer(data->ring, pBuffer, lSamples);
423 else
424 SetALCError(ALC_INVALID_VALUE);
427 static ALCuint oss_available_samples(ALCdevice *pDevice)
429 oss_data *data = (oss_data*)pDevice->ExtraData;
430 return RingBufferSize(data->ring);
434 BackendFuncs oss_funcs = {
435 oss_open_playback,
436 oss_close_playback,
437 oss_open_capture,
438 oss_close_capture,
439 oss_start_capture,
440 oss_stop_capture,
441 oss_capture_samples,
442 oss_available_samples
445 void alc_oss_init(BackendFuncs *func_list)
447 *func_list = oss_funcs;
449 oss_device = AppendDeviceList("OSS Software");
450 AppendAllDeviceList(oss_device);
452 oss_device_capture = AppendCaptureDeviceList("OSS Capture");