2 * This file is part of DisOrder
3 * Copyright (C) 2007 Richard Kettlewell
4 * Portions copyright (C) 2007 Ross Younger
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 /** @file server/speaker-oss.c
22 * @brief Support for @ref BACKEND_OSS */
26 #if HAVE_SYS_SOUNDCARD_H
36 #include <sys/ioctl.h>
37 #include <sys/soundcard.h>
39 #include "configuration.h"
42 #include "speaker-protocol.h"
45 /** @brief Current device */
46 static int ossfd
= -1;
48 /** @brief OSS backend initialization */
49 static void oss_init(void) {
50 info("selected OSS backend");
53 /** @brief OSS deactivation */
54 static void oss_deactivate(void) {
58 device_state
= device_closed
;
59 D(("released audio device"));
63 /** @brief OSS backend activation */
64 static void oss_activate(void) {
65 int stereo
, format
, rate
;
69 /* Try to pick a device */
70 if(!strcmp(config
->device
, "default")) {
71 if(access("/dev/dsp", W_OK
) == 0)
73 else if(access("/dev/audio", W_OK
) == 0)
74 device
= "/dev/audio";
76 error(0, "cannot determine default OSS device");
80 device
= config
->device
; /* just believe the user */
82 if((ossfd
= open(device
, O_WRONLY
, 0)) < 0) {
83 error(errno
, "error opening %s", device
);
86 /* Set the audio format */
87 stereo
= (config
->sample_format
.channels
== 2);
88 if(ioctl(ossfd
, SNDCTL_DSP_STEREO
, &stereo
) < 0) {
89 error(errno
, "error calling ioctl SNDCTL_DSP_STEREO %d", stereo
);
92 if(config
->sample_format
.bits
== 8)
94 else if(config
->sample_format
.bits
== 16)
95 format
= (config
->sample_format
.endian
== ENDIAN_LITTLE
96 ? AFMT_S16_LE
: AFMT_S16_BE
);
98 error(0, "unsupported sample_format for oss backend");
101 if(ioctl(ossfd
, SNDCTL_DSP_SETFMT
, &format
) < 0) {
102 error(errno
, "error calling ioctl SNDCTL_DSP_SETFMT %#x", format
);
105 rate
= config
->sample_format
.rate
;
106 if(ioctl(ossfd
, SNDCTL_DSP_SPEED
, &rate
) < 0) {
107 error(errno
, "error calling ioctl SNDCTL_DSP_SPEED %d", rate
);
110 if((unsigned)rate
!= config
->sample_format
.rate
)
111 error(0, "asked for %luHz, got %dHz",
112 (unsigned long)config
->sample_format
.rate
, rate
);
114 device_state
= device_open
;
118 device_state
= device_error
;
125 /** @brief Play via OSS */
126 static size_t oss_play(size_t frames
) {
127 const size_t bytes_to_play
= frames
* bpf
;
128 ssize_t bytes_written
;
130 bytes_written
= write(ossfd
, playing
->buffer
+ playing
->start
,
132 if(bytes_written
< 0)
134 case EINTR
: /* interruped */
135 case EAGAIN
: /* overrun */
136 return 0; /* try again later */
138 fatal(errno
, "error writing to audio device");
140 return bytes_written
/ bpf
;
145 /** @brief Fill in poll fd array for OSS */
146 static void oss_beforepoll(int attribute((unused
)) *timeoutp
) {
147 oss_slot
= addfd(ossfd
, POLLOUT
|POLLERR
);
150 /** @brief Process poll() results for OSS */
151 static int oss_ready(void) {
152 return !!(fds
[oss_slot
].revents
& (POLLOUT
|POLLERR
));
155 const struct speaker_backend oss_backend
= {