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
23 #include <sys/ioctl.h>
24 #include <sys/types.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
46 #ifndef SOUND_MIXER_WRITE
47 #define SOUND_MIXER_WRITE MIXER_WRITE
50 static const ALCchar oss_device
[] = "OSS Default";
65 static int log2i(ALCuint x
)
77 static ALuint
OSSProc(ALvoid
*ptr
)
79 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
80 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
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
);
99 if(errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&& errno
!= EINTR
)
101 AL_PRINT("write failed: %s\n", strerror(errno
));
102 aluHandleDisconnect(pDevice
);
118 static ALuint
OSSCaptureProc(ALvoid
*ptr
)
120 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
121 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
127 frameSize
= aluFrameSizeFromFormat(pDevice
->Format
);
129 while(!data
->killNow
)
131 amt
= read(data
->fd
, data
->mix_data
, data
->data_size
);
134 AL_PRINT("read failed: %s\n", strerror(errno
));
135 aluHandleDisconnect(pDevice
);
144 WriteRingBuffer(data
->ring
, data
->mix_data
, amt
/frameSize
);
150 static ALCboolean
oss_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
155 strncpy(driver
, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver
)-1);
156 driver
[sizeof(driver
)-1] = 0;
158 deviceName
= oss_device
;
159 else if(strcmp(deviceName
, oss_device
) != 0)
162 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
165 data
->fd
= open(driver
, O_WRONLY
);
170 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
174 device
->szDeviceName
= strdup(deviceName
);
175 device
->ExtraData
= data
;
179 static void oss_close_playback(ALCdevice
*device
)
181 oss_data
*data
= (oss_data
*)device
->ExtraData
;
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
;
202 switch(aluBytesFromFormat(device
->Format
))
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;
219 ossFormat
= AFMT_S16_NE
;
222 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
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
));
255 if((int)aluChannelsFromFormat(device
->Format
) != numChannels
)
257 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device
->Format
), numChannels
);
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
);
268 device
->Frequency
= ossSpeed
;
269 device
->UpdateSize
= info
.fragsize
/ frameSize
;
270 device
->NumUpdates
= info
.fragments
+ 1;
271 device
->TimeRes
= (ALuint64
)device
->UpdateSize
* 1000000000 /
274 data
->data_size
= device
->UpdateSize
* frameSize
;
275 data
->mix_data
= calloc(1, data
->data_size
);
277 SetDefaultChannelOrder(device
);
279 data
->thread
= StartThread(OSSProc
, device
);
280 if(data
->thread
== NULL
)
282 free(data
->mix_data
);
283 data
->mix_data
= NULL
;
290 static void oss_stop_playback(ALCdevice
*device
)
292 oss_data
*data
= (oss_data
*)device
->ExtraData
;
298 StopThread(data
->thread
);
302 if(ioctl(data
->fd
, SNDCTL_DSP_RESET
) != 0)
303 AL_PRINT("Error resetting device: %s\n", strerror(errno
));
305 free(data
->mix_data
);
306 data
->mix_data
= NULL
;
310 static ALCboolean
oss_open_capture(ALCdevice
*device
, const ALCchar
*deviceName
)
312 int numFragmentsLogSize
;
313 int log2FragmentSize
;
314 unsigned int periods
;
325 strncpy(driver
, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver
)-1);
326 driver
[sizeof(driver
)-1] = 0;
328 deviceName
= oss_device
;
329 else if(strcmp(deviceName
, oss_device
) != 0)
332 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
335 data
->fd
= open(driver
, O_RDONLY
);
340 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
344 switch(aluBytesFromFormat(device
->Format
))
350 ossFormat
= AFMT_S16_NE
;
353 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
360 numChannels
= aluChannelsFromFormat(device
->Format
);
361 frameSize
= numChannels
* aluBytesFromFormat(device
->Format
);
362 ossSpeed
= device
->Frequency
;
363 log2FragmentSize
= log2i(device
->UpdateSize
* device
->NumUpdates
*
364 frameSize
/ periods
);
366 /* according to the OSS spec, 16 bytes are the minimum */
367 if (log2FragmentSize
< 4)
368 log2FragmentSize
= 4;
369 numFragmentsLogSize
= (periods
<< 16) | log2FragmentSize
;
371 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
372 if (!(ok(ioctl(data
->fd
, SNDCTL_DSP_SETFRAGMENT
, &numFragmentsLogSize
), "set fragment") &&
373 ok(ioctl(data
->fd
, SNDCTL_DSP_SETFMT
, &ossFormat
), "set format") &&
374 ok(ioctl(data
->fd
, SNDCTL_DSP_CHANNELS
, &numChannels
), "set channels") &&
375 ok(ioctl(data
->fd
, SNDCTL_DSP_SPEED
, &ossSpeed
), "set speed") &&
376 ok(ioctl(data
->fd
, SNDCTL_DSP_GETISPACE
, &info
), "get space")))
378 AL_PRINT("%s failed: %s\n", err
, strerror(errno
));
385 if((int)aluChannelsFromFormat(device
->Format
) != numChannels
)
387 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device
->Format
), numChannels
);
393 if(!((ossFormat
== AFMT_U8
&& aluBytesFromFormat(device
->Format
) == 1) ||
394 (ossFormat
== AFMT_S16_NE
&& aluBytesFromFormat(device
->Format
) == 2)))
396 AL_PRINT("Could not set %d-bit input, got format %#x\n", aluBytesFromFormat(device
->Format
)*8, ossFormat
);
402 data
->ring
= CreateRingBuffer(frameSize
, device
->UpdateSize
* device
->NumUpdates
);
405 AL_PRINT("ring buffer create failed\n");
411 data
->data_size
= info
.fragsize
;
412 data
->mix_data
= calloc(1, data
->data_size
);
414 device
->ExtraData
= data
;
415 data
->thread
= StartThread(OSSCaptureProc
, device
);
416 if(data
->thread
== NULL
)
418 device
->ExtraData
= NULL
;
419 free(data
->mix_data
);
424 device
->szDeviceName
= strdup(deviceName
);
428 static void oss_close_capture(ALCdevice
*device
)
430 oss_data
*data
= (oss_data
*)device
->ExtraData
;
432 StopThread(data
->thread
);
436 DestroyRingBuffer(data
->ring
);
438 free(data
->mix_data
);
440 device
->ExtraData
= NULL
;
443 static void oss_start_capture(ALCdevice
*pDevice
)
445 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
449 static void oss_stop_capture(ALCdevice
*pDevice
)
451 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
455 static void oss_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
457 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
458 if(lSamples
<= (ALCuint
)RingBufferSize(data
->ring
))
459 ReadRingBuffer(data
->ring
, pBuffer
, lSamples
);
461 alcSetError(pDevice
, ALC_INVALID_VALUE
);
464 static ALCuint
oss_available_samples(ALCdevice
*pDevice
)
466 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
467 return RingBufferSize(data
->ring
);
470 static ALuint64
oss_get_time(ALCdevice
*Device
)
472 return Device
->SamplesPlayed
* 1000000000 / Device
->Frequency
;
476 BackendFuncs oss_funcs
= {
486 oss_available_samples
,
490 void alc_oss_init(BackendFuncs
*func_list
)
492 *func_list
= oss_funcs
;
495 void alc_oss_deinit(void)
499 void alc_oss_probe(int type
)
501 if(type
== DEVICE_PROBE
)
505 if(stat(GetConfigValue("oss", "device", "/dev/dsp"), &buf
) == 0)
507 AppendDeviceList(oss_device
);
509 else if(type
== ALL_DEVICE_PROBE
)
513 if(stat(GetConfigValue("oss", "device", "/dev/dsp"), &buf
) == 0)
515 AppendAllDeviceList(oss_device
);
517 else if(type
== CAPTURE_DEVICE_PROBE
)
521 if(stat(GetConfigValue("oss", "capture", "/dev/dsp"), &buf
) == 0)
523 AppendCaptureDeviceList(oss_device
);