Changed how device format is selected in restart_playback, and other small changes...
[openal-soft/android.git] / Alc / oss.c
blobed96fa17f7526c8a5be505c865b1344f5867a1a4
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 Default";
52 typedef struct {
53 int fd;
54 volatile int killNow;
55 ALvoid *thread;
57 ALubyte *mix_data;
58 int data_size;
60 RingBuffer *ring;
61 int doCapture;
62 } oss_data;
65 static int log2i(ALCuint x)
67 int y = 0;
68 while (x > 1)
70 x >>= 1;
71 y++;
73 return y;
77 static ALuint OSSProc(ALvoid *ptr)
79 ALCdevice *pDevice = (ALCdevice*)ptr;
80 oss_data *data = (oss_data*)pDevice->ExtraData;
81 ALint frameSize;
82 ssize_t wrote;
84 SetRTPriority();
86 frameSize = aluFrameSizeFromFormat(pDevice->Format);
88 while(!data->killNow && pDevice->Connected)
90 ALint len = data->data_size;
91 ALubyte *WritePtr = data->mix_data;
93 aluMixData(pDevice, WritePtr, len/frameSize);
94 while(len > 0 && !data->killNow)
96 wrote = write(data->fd, WritePtr, len);
97 if(wrote < 0)
99 if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
101 AL_PRINT("write failed: %s\n", strerror(errno));
102 aluHandleDisconnect(pDevice);
103 break;
106 Sleep(1);
107 continue;
110 len -= wrote;
111 WritePtr += wrote;
115 return 0;
118 static ALuint OSSCaptureProc(ALvoid *ptr)
120 ALCdevice *pDevice = (ALCdevice*)ptr;
121 oss_data *data = (oss_data*)pDevice->ExtraData;
122 int frameSize;
123 int amt;
125 SetRTPriority();
127 frameSize = aluFrameSizeFromFormat(pDevice->Format);
129 while(!data->killNow)
131 amt = read(data->fd, data->mix_data, data->data_size);
132 if(amt < 0)
134 AL_PRINT("read failed: %s\n", strerror(errno));
135 aluHandleDisconnect(pDevice);
136 break;
138 if(amt == 0)
140 Sleep(1);
141 continue;
143 if(data->doCapture)
144 WriteRingBuffer(data->ring, data->mix_data, amt/frameSize);
147 return 0;
150 static ALCboolean oss_open_playback(ALCdevice *device, const ALCchar *deviceName)
152 char driver[64];
153 oss_data *data;
155 strncpy(driver, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver)-1);
156 driver[sizeof(driver)-1] = 0;
157 if(!deviceName)
158 deviceName = oss_device;
159 else if(strcmp(deviceName, oss_device) != 0)
160 return ALC_FALSE;
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 if(errno != ENOENT)
170 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
171 return ALC_FALSE;
174 device->szDeviceName = strdup(deviceName);
175 device->ExtraData = data;
176 return ALC_TRUE;
179 static void oss_close_playback(ALCdevice *device)
181 oss_data *data = (oss_data*)device->ExtraData;
183 close(data->fd);
184 free(data);
185 device->ExtraData = NULL;
188 static ALCboolean oss_reset_playback(ALCdevice *device)
190 oss_data *data = (oss_data*)device->ExtraData;
191 int numFragmentsLogSize;
192 int log2FragmentSize;
193 unsigned int periods;
194 audio_buf_info info;
195 ALuint frameSize;
196 int numChannels;
197 int ossFormat;
198 int ossSpeed;
199 char *err;
200 int i;
202 switch(aluBytesFromFormat(device->Format))
204 case 1:
205 ossFormat = AFMT_U8;
206 break;
207 case 4:
208 switch(aluChannelsFromFormat(device->Format))
210 case 1: device->Format = AL_FORMAT_MONO16; break;
211 case 2: device->Format = AL_FORMAT_STEREO16; break;
212 case 4: device->Format = AL_FORMAT_QUAD16; break;
213 case 6: device->Format = AL_FORMAT_51CHN16; break;
214 case 7: device->Format = AL_FORMAT_61CHN16; break;
215 case 8: device->Format = AL_FORMAT_71CHN16; break;
217 /* fall-through */
218 case 2:
219 ossFormat = AFMT_S16_NE;
220 break;
221 default:
222 AL_PRINT("Unknown format: 0x%x\n", device->Format);
223 return ALC_FALSE;
226 periods = device->NumUpdates;
227 numChannels = aluChannelsFromFormat(device->Format);
228 frameSize = numChannels * aluBytesFromFormat(device->Format);
230 ossSpeed = device->Frequency;
231 log2FragmentSize = log2i(device->UpdateSize * frameSize);
233 /* according to the OSS spec, 16 bytes are the minimum */
234 if (log2FragmentSize < 4)
235 log2FragmentSize = 4;
236 /* Subtract one period since the temp mixing buffer counts as one. Still
237 * need at least two on the card, though. */
238 if(periods > 2) periods--;
239 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
241 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
242 /* Don't fail if SETFRAGMENT fails. We can handle just about anything
243 * that's reported back via GETOSPACE */
244 ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize);
245 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
246 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
247 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
248 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
250 AL_PRINT("%s failed: %s\n", err, strerror(errno));
251 return ALC_FALSE;
253 #undef ok
255 if((int)aluChannelsFromFormat(device->Format) != numChannels)
257 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
258 return ALC_FALSE;
261 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
262 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
264 AL_PRINT("Could not set %d-bit output, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
265 return ALC_FALSE;
268 device->Frequency = ossSpeed;
269 device->UpdateSize = info.fragsize / frameSize;
270 device->NumUpdates = info.fragments + 1;
272 data->data_size = device->UpdateSize * frameSize;
273 data->mix_data = calloc(1, data->data_size);
275 SetDefaultChannelOrder(device);
277 data->thread = StartThread(OSSProc, device);
278 if(data->thread == NULL)
280 free(data->mix_data);
281 data->mix_data = NULL;
282 return ALC_FALSE;
285 return ALC_TRUE;
288 static void oss_stop_playback(ALCdevice *device)
290 oss_data *data = (oss_data*)device->ExtraData;
292 if(!data->thread)
293 return;
295 data->killNow = 1;
296 StopThread(data->thread);
297 data->thread = NULL;
299 data->killNow = 0;
300 if(ioctl(data->fd, SNDCTL_DSP_RESET) != 0)
301 AL_PRINT("Error resetting device: %s\n", strerror(errno));
303 free(data->mix_data);
304 data->mix_data = NULL;
308 static ALCboolean oss_open_capture(ALCdevice *device, const ALCchar *deviceName)
310 int numFragmentsLogSize;
311 int log2FragmentSize;
312 unsigned int periods;
313 audio_buf_info info;
314 ALuint frameSize;
315 int numChannels;
316 char driver[64];
317 oss_data *data;
318 int ossFormat;
319 int ossSpeed;
320 char *err;
321 int i;
323 strncpy(driver, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver)-1);
324 driver[sizeof(driver)-1] = 0;
325 if(!deviceName)
326 deviceName = oss_device;
327 else if(strcmp(deviceName, oss_device) != 0)
328 return ALC_FALSE;
330 data = (oss_data*)calloc(1, sizeof(oss_data));
331 data->killNow = 0;
333 data->fd = open(driver, O_RDONLY);
334 if(data->fd == -1)
336 free(data);
337 if(errno != ENOENT)
338 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
339 return ALC_FALSE;
342 switch(aluBytesFromFormat(device->Format))
344 case 1:
345 ossFormat = AFMT_U8;
346 break;
347 case 2:
348 ossFormat = AFMT_S16_NE;
349 break;
350 default:
351 AL_PRINT("Unknown format: 0x%x\n", device->Format);
352 close(data->fd);
353 free(data);
354 return ALC_FALSE;
357 periods = 4;
358 numChannels = aluChannelsFromFormat(device->Format);
359 frameSize = numChannels * aluBytesFromFormat(device->Format);
360 ossSpeed = device->Frequency;
361 log2FragmentSize = log2i(device->UpdateSize * device->NumUpdates *
362 frameSize / periods);
364 /* according to the OSS spec, 16 bytes are the minimum */
365 if (log2FragmentSize < 4)
366 log2FragmentSize = 4;
367 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
369 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
370 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
371 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
372 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
373 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
374 ok(ioctl(data->fd, SNDCTL_DSP_GETISPACE, &info), "get space")))
376 AL_PRINT("%s failed: %s\n", err, strerror(errno));
377 close(data->fd);
378 free(data);
379 return ALC_FALSE;
381 #undef ok
383 if((int)aluChannelsFromFormat(device->Format) != numChannels)
385 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
386 close(data->fd);
387 free(data);
388 return ALC_FALSE;
391 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
392 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
394 AL_PRINT("Could not set %d-bit input, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
395 close(data->fd);
396 free(data);
397 return ALC_FALSE;
400 data->ring = CreateRingBuffer(frameSize, device->UpdateSize * device->NumUpdates);
401 if(!data->ring)
403 AL_PRINT("ring buffer create failed\n");
404 close(data->fd);
405 free(data);
406 return ALC_FALSE;
409 data->data_size = info.fragsize;
410 data->mix_data = calloc(1, data->data_size);
412 device->ExtraData = data;
413 data->thread = StartThread(OSSCaptureProc, device);
414 if(data->thread == NULL)
416 device->ExtraData = NULL;
417 free(data->mix_data);
418 free(data);
419 return ALC_FALSE;
422 device->szDeviceName = strdup(deviceName);
423 return ALC_TRUE;
426 static void oss_close_capture(ALCdevice *device)
428 oss_data *data = (oss_data*)device->ExtraData;
429 data->killNow = 1;
430 StopThread(data->thread);
432 close(data->fd);
434 DestroyRingBuffer(data->ring);
436 free(data->mix_data);
437 free(data);
438 device->ExtraData = NULL;
441 static void oss_start_capture(ALCdevice *pDevice)
443 oss_data *data = (oss_data*)pDevice->ExtraData;
444 data->doCapture = 1;
447 static void oss_stop_capture(ALCdevice *pDevice)
449 oss_data *data = (oss_data*)pDevice->ExtraData;
450 data->doCapture = 0;
453 static void oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
455 oss_data *data = (oss_data*)pDevice->ExtraData;
456 if(lSamples <= (ALCuint)RingBufferSize(data->ring))
457 ReadRingBuffer(data->ring, pBuffer, lSamples);
458 else
459 alcSetError(pDevice, ALC_INVALID_VALUE);
462 static ALCuint oss_available_samples(ALCdevice *pDevice)
464 oss_data *data = (oss_data*)pDevice->ExtraData;
465 return RingBufferSize(data->ring);
469 BackendFuncs oss_funcs = {
470 oss_open_playback,
471 oss_close_playback,
472 oss_reset_playback,
473 oss_stop_playback,
474 oss_open_capture,
475 oss_close_capture,
476 oss_start_capture,
477 oss_stop_capture,
478 oss_capture_samples,
479 oss_available_samples
482 void alc_oss_init(BackendFuncs *func_list)
484 *func_list = oss_funcs;
487 void alc_oss_deinit(void)
491 void alc_oss_probe(int type)
493 if(type == DEVICE_PROBE)
495 #ifdef HAVE_STAT
496 struct stat buf;
497 if(stat(GetConfigValue("oss", "device", "/dev/dsp"), &buf) == 0)
498 #endif
499 AppendDeviceList(oss_device);
501 else if(type == ALL_DEVICE_PROBE)
503 #ifdef HAVE_STAT
504 struct stat buf;
505 if(stat(GetConfigValue("oss", "device", "/dev/dsp"), &buf) == 0)
506 #endif
507 AppendAllDeviceList(oss_device);
509 else if(type == CAPTURE_DEVICE_PROBE)
511 #ifdef HAVE_STAT
512 struct stat buf;
513 if(stat(GetConfigValue("oss", "capture", "/dev/dsp"), &buf) == 0)
514 #endif
515 AppendCaptureDeviceList(oss_device);