2 * This file is part of DisOrder
3 * Copyright (C) 2007 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file server/speaker-oss.c
21 * @brief Support for @ref BACKEND_OSS */
25 #if HAVE_SYS_SOUNDCARD_H
35 #include <sys/ioctl.h>
36 #include <sys/soundcard.h>
38 #include "configuration.h"
41 #include "speaker-protocol.h"
44 /** @brief Current device */
45 static int ossfd = -1;
47 /** @brief OSS backend initialization */
48 static void oss_init(void) {
49 info("selected OSS backend");
52 /** @brief OSS deactivation */
53 static void oss_deactivate(void) {
57 device_state = device_closed;
58 D(("released audio device"));
62 /** @brief OSS backend activation */
63 static void oss_activate(void) {
64 int stereo, format, rate;
68 /* Try to pick a device */
69 if(!strcmp(config->device, "default")) {
70 if(access("/dev/dsp", W_OK) == 0)
72 else if(access("/dev/audio", W_OK) == 0)
73 device = "/dev/audio";
75 error(0, "cannot determine default OSS device");
79 device = config->device; /* just believe the user */
81 if((ossfd = open(device, O_WRONLY, 0)) < 0) {
82 error(errno, "error opening %s", device);
85 /* Set the audio format */
86 stereo = (config->sample_format.channels == 2);
87 if(ioctl(ossfd, SNDCTL_DSP_STEREO, &stereo) < 0) {
88 error(errno, "error calling ioctl SNDCTL_DSP_STEREO %d", stereo);
91 if(config->sample_format.bits == 8)
93 else if(config->sample_format.bits == 16)
94 format = (config->sample_format.endian == ENDIAN_LITTLE
95 ? AFMT_S16_LE : AFMT_S16_BE);
97 error(0, "unsupported sample_format for oss backend");
100 if(ioctl(ossfd, SNDCTL_DSP_SETFMT, &format) < 0) {
101 error(errno, "error calling ioctl SNDCTL_DSP_SETFMT %#x", format);
104 rate = config->sample_format.rate;
105 if(ioctl(ossfd, SNDCTL_DSP_SPEED, &rate) < 0) {
106 error(errno, "error calling ioctl SNDCTL_DSP_SPEED %d", rate);
109 if((unsigned)rate != config->sample_format.rate)
110 error(0, "asked for %luHz, got %dHz",
111 (unsigned long)config->sample_format.rate, rate);
113 device_state = device_open;
117 device_state = device_error;
124 /** @brief Play via OSS */
125 static size_t oss_play(size_t frames) {
126 const size_t bytes_to_play = frames * bpf;
127 ssize_t bytes_written;
129 bytes_written = write(ossfd, playing->buffer + playing->start,
131 if(bytes_written < 0)
133 case EINTR: /* interruped */
134 case EAGAIN: /* overrun */
135 return 0; /* try again later */
137 fatal(errno, "error writing to audio device");
139 return bytes_written / bpf;
144 /** @brief Fill in poll fd array for OSS */
145 static void oss_beforepoll(int attribute((unused)) *timeoutp) {
146 oss_slot = addfd(ossfd, POLLOUT|POLLERR);
149 /** @brief Process poll() results for OSS */
150 static int oss_ready(void) {
151 return !!(fds[oss_slot].revents & (POLLOUT|POLLERR));
154 const struct speaker_backend oss_backend = {