Print an error when OSS fails to open the audio device
[openal-soft.git] / Alc / oss.c
blobe1aba0b3d52e50b20c1d89e04713506fda9f2a38
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 <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <memory.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <math.h>
31 #include "alMain.h"
32 #include "AL/al.h"
33 #include "AL/alc.h"
35 #include <sys/soundcard.h>
38 * The OSS documentation talks about SOUND_MIXER_READ, but the header
39 * only contains MIXER_READ. Play safe. Same for WRITE.
41 #ifndef SOUND_MIXER_READ
42 #define SOUND_MIXER_READ MIXER_READ
43 #endif
44 #ifndef SOUND_MIXER_WRITE
45 #define SOUND_MIXER_WRITE MIXER_WRITE
46 #endif
48 static char *oss_device;
50 typedef struct {
51 int fd;
52 int killNow;
53 ALvoid *thread;
55 ALubyte *mix_data;
56 int data_size;
57 int silence;
58 } oss_data;
61 static int log2i(ALCuint x)
63 int y = 0;
64 while (x > 1)
66 x >>= 1;
67 y++;
69 return y;
73 static ALuint OSSProc(ALvoid *ptr)
75 ALCdevice *pDevice = (ALCdevice*)ptr;
76 oss_data *data = (oss_data*)pDevice->ExtraData;
77 int remaining;
78 int wrote;
80 while(!data->killNow)
82 SuspendContext(NULL);
83 aluMixData(pDevice->Context,data->mix_data,data->data_size,pDevice->Format);
84 ProcessContext(NULL);
86 remaining = data->data_size;
87 while(remaining > 0)
89 wrote = write(data->fd, data->mix_data+data->data_size-remaining, remaining);
90 if(wrote < 0)
92 AL_PRINT("write failed: %s\n", strerror(errno));
93 break;
95 if(wrote == 0)
97 usleep(1000);
98 continue;
100 remaining -= wrote;
104 return 0;
107 static ALCboolean oss_open_playback(ALCdevice *device, const ALCchar *deviceName)
109 int numFragmentsLogSize;
110 int log2FragmentSize;
111 unsigned int periods;
112 audio_buf_info info;
113 char driver[64];
114 int numChannels;
115 oss_data *data;
116 int ossFormat;
117 int ossSpeed;
118 char *err;
119 int i;
121 strncpy(driver, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver)-1);
122 driver[sizeof(driver)-1] = 0;
123 if(deviceName)
125 if(strcmp(deviceName, oss_device))
126 return ALC_FALSE;
129 if(deviceName)
130 strcpy(device->szDeviceName, deviceName);
131 else
132 strcpy(device->szDeviceName, oss_device);
134 data = (oss_data*)calloc(1, sizeof(oss_data));
135 data->killNow = 0;
137 data->fd = open(driver, O_WRONLY);
138 if(data->fd == -1)
140 free(data);
141 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
142 return ALC_FALSE;
145 switch(device->Format)
147 case AL_FORMAT_MONO8:
148 case AL_FORMAT_STEREO8:
149 case AL_FORMAT_QUAD8:
150 data->silence = 0x80;
151 ossFormat = AFMT_U8;
152 break;
153 case AL_FORMAT_MONO16:
154 case AL_FORMAT_STEREO16:
155 case AL_FORMAT_QUAD16:
156 data->silence = 0;
157 ossFormat = AFMT_S16_NE;
158 break;
159 default:
160 ossFormat = -1;
161 AL_PRINT("Unknown format?! %x\n", device->Format);
164 periods = GetConfigValueInt("oss", "periods", 4);
165 if((int)periods < 0)
166 periods = 4;
167 numChannels = device->Channels;
168 ossSpeed = device->Frequency;
169 log2FragmentSize = log2i(device->UpdateFreq * device->FrameSize / periods);
171 /* according to the OSS spec, 16 bytes are the minimum */
172 if (log2FragmentSize < 4)
173 log2FragmentSize = 4;
174 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
176 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
177 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
178 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
179 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
180 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
181 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
183 AL_PRINT("%s failed: %s\n", err, strerror(i));
184 close(data->fd);
185 free(data);
186 return ALC_FALSE;
188 #undef ok
190 device->Channels = numChannels;
191 device->Frequency = ossSpeed;
192 device->Format = 0;
193 if(ossFormat == AFMT_U8)
195 if(device->Channels == 1)
197 device->Format = AL_FORMAT_MONO8;
198 device->FrameSize = 1;
200 else if(device->Channels == 2)
202 device->Format = AL_FORMAT_STEREO8;
203 device->FrameSize = 2;
205 else if(device->Channels == 4)
207 device->Format = AL_FORMAT_QUAD8;
208 device->FrameSize = 4;
211 else if(ossFormat == AFMT_S16_NE)
213 if(device->Channels == 1)
215 device->Format = AL_FORMAT_MONO16;
216 device->FrameSize = 2;
218 else if(device->Channels == 2)
220 device->Format = AL_FORMAT_STEREO16;
221 device->FrameSize = 4;
223 else if(device->Channels == 4)
225 device->Format = AL_FORMAT_QUAD16;
226 device->FrameSize = 8;
230 if(!device->Format)
232 AL_PRINT("returned unknown format: %#x %d!\n", ossFormat, numChannels);
233 close(data->fd);
234 free(data);
235 return ALC_FALSE;
238 device->UpdateFreq = info.fragsize / device->FrameSize;
240 data->data_size = device->UpdateFreq * device->FrameSize;
241 data->mix_data = calloc(1, data->data_size);
243 device->MaxNoOfSources = 256;
245 device->ExtraData = data;
246 data->thread = StartThread(OSSProc, device);
247 if(data->thread == NULL)
249 device->ExtraData = NULL;
250 free(data->mix_data);
251 free(data);
252 return ALC_FALSE;
255 return ALC_TRUE;
258 static void oss_close_playback(ALCdevice *device)
260 oss_data *data = (oss_data*)device->ExtraData;
261 data->killNow = 1;
262 StopThread(data->thread);
264 close(data->fd);
266 free(data->mix_data);
267 free(data);
268 device->ExtraData = NULL;
272 static ALCboolean oss_open_capture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
274 (void)pDevice;
275 (void)deviceName;
276 (void)frequency;
277 (void)format;
278 (void)SampleSize;
279 return ALC_FALSE;
282 static void oss_close_capture(ALCdevice *pDevice)
284 (void)pDevice;
287 static void oss_start_capture(ALCdevice *pDevice)
289 (void)pDevice;
292 static void oss_stop_capture(ALCdevice *pDevice)
294 (void)pDevice;
297 static void oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
299 (void)pDevice;
300 (void)pBuffer;
301 (void)lSamples;
304 static ALCuint oss_available_samples(ALCdevice *pDevice)
306 (void)pDevice;
307 return 0;
311 BackendFuncs oss_funcs = {
312 oss_open_playback,
313 oss_close_playback,
314 oss_open_capture,
315 oss_close_capture,
316 oss_start_capture,
317 oss_stop_capture,
318 oss_capture_samples,
319 oss_available_samples
322 void alc_oss_init(BackendFuncs *func_list)
324 *func_list = oss_funcs;
326 oss_device = AppendDeviceList("OSS Software");
327 AppendAllDeviceList(oss_device);