2 * This file is part of DisOrder
3 * Copyright (C) 2007, 2008 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
33 #include <sys/ioctl.h>
34 #include <sys/soundcard.h>
36 #include "configuration.h"
39 #include "speaker-protocol.h"
42 /** @brief Current device */
43 static int ossfd
= -1;
45 /** @brief OSS backend initialization */
46 static void oss_init(void) {
47 info("selected OSS backend");
50 /** @brief OSS deactivation */
51 static void oss_deactivate(void) {
55 device_state
= device_closed
;
56 D(("released audio device"));
60 /** @brief OSS backend activation */
61 static void oss_activate(void) {
62 int stereo
, format
, rate
;
66 /* Try to pick a device */
67 if(!strcmp(config
->device
, "default")) {
68 if(access("/dev/dsp", W_OK
) == 0)
70 else if(access("/dev/audio", W_OK
) == 0)
71 device
= "/dev/audio";
76 error(0, "cannot determine default OSS device");
82 device
= config
->device
; /* just believe the user */
84 if((ossfd
= open(device
, O_WRONLY
, 0)) < 0) {
85 error(errno
, "error opening %s", device
);
88 /* Set the audio format */
89 stereo
= (config
->sample_format
.channels
== 2);
90 if(ioctl(ossfd
, SNDCTL_DSP_STEREO
, &stereo
) < 0) {
91 error(errno
, "error calling ioctl SNDCTL_DSP_STEREO %d", stereo
);
94 if(config
->sample_format
.bits
== 8)
96 else if(config
->sample_format
.bits
== 16)
97 format
= (config
->sample_format
.endian
== ENDIAN_LITTLE
98 ? AFMT_S16_LE
: AFMT_S16_BE
);
100 error(0, "unsupported sample_format for oss backend");
103 if(ioctl(ossfd
, SNDCTL_DSP_SETFMT
, &format
) < 0) {
104 error(errno
, "error calling ioctl SNDCTL_DSP_SETFMT %#x", format
);
107 rate
= config
->sample_format
.rate
;
108 if(ioctl(ossfd
, SNDCTL_DSP_SPEED
, &rate
) < 0) {
109 error(errno
, "error calling ioctl SNDCTL_DSP_SPEED %d", rate
);
112 if((unsigned)rate
!= config
->sample_format
.rate
)
113 error(0, "asked for %luHz, got %dHz",
114 (unsigned long)config
->sample_format
.rate
, rate
);
116 device_state
= device_open
;
120 device_state
= device_error
;
127 /** @brief Play via OSS */
128 static size_t oss_play(size_t frames
) {
129 const size_t bytes_to_play
= frames
* bpf
;
130 ssize_t bytes_written
;
132 bytes_written
= write(ossfd
, playing
->buffer
+ playing
->start
,
134 if(bytes_written
< 0)
136 case EINTR
: /* interruped */
137 case EAGAIN
: /* overrun */
138 return 0; /* try again later */
140 fatal(errno
, "error writing to audio device");
142 return bytes_written
/ bpf
;
147 /** @brief Fill in poll fd array for OSS */
148 static void oss_beforepoll(int attribute((unused
)) *timeoutp
) {
149 oss_slot
= addfd(ossfd
, POLLOUT
|POLLERR
);
152 /** @brief Process poll() results for OSS */
153 static int oss_ready(void) {
154 return !!(fds
[oss_slot
].revents
& (POLLOUT
|POLLERR
));
157 const struct speaker_backend oss_backend
= {