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 device = config->device; /* just believe the user */
77 if((ossfd = open(device, O_WRONLY, 0)) < 0) {
78 error(errno, "error opening %s", device);
81 /* Set the audio format */
82 stereo = (config->sample_format.channels == 2);
83 if(ioctl(ossfd, SNDCTL_DSP_STEREO, &stereo) < 0) {
84 error(errno, "error calling ioctl SNDCTL_DSP_STEREO %d", stereo);
87 if(config->sample_format.bits == 8)
89 else if(config->sample_format.bits == 16)
90 format = (config->sample_format.endian == ENDIAN_LITTLE
91 ? AFMT_S16_LE : AFMT_S16_BE);
93 error(0, "unsupported sample_format for oss backend");
96 if(ioctl(ossfd, SNDCTL_DSP_SETFMT, &format) < 0) {
97 error(errno, "error calling ioctl SNDCTL_DSP_SETFMT %#x", format);
100 rate = config->sample_format.rate;
101 if(ioctl(ossfd, SNDCTL_DSP_SPEED, &rate) < 0) {
102 error(errno, "error calling ioctl SNDCTL_DSP_SPEED %d", rate);
105 if((unsigned)rate != config->sample_format.rate)
106 error(0, "asked for %luHz, got %dHz",
107 (unsigned long)config->sample_format.rate, rate);
109 device_state = device_open;
113 device_state = device_error;
118 /** @brief Play via OSS */
119 static size_t oss_play(size_t frames) {
120 const size_t bytes_to_play = frames * bpf;
121 ssize_t bytes_written;
123 bytes_written = write(ossfd, playing->buffer + playing->start,
125 if(bytes_written < 0)
127 case EINTR: /* interruped */
128 case EAGAIN: /* overrun */
129 return 0; /* try again later */
131 fatal(errno, "error writing to audio device");
133 return bytes_written / bpf;
138 /** @brief Fill in poll fd array for OSS */
139 static void oss_beforepoll(void) {
140 oss_slot = addfd(ossfd, POLLOUT|POLLERR);
143 /** @brief Process poll() results for OSS */
144 static int oss_ready(void) {
145 return !!(fds[oss_slot].revents & (POLLOUT|POLLERR));
148 const struct speaker_backend oss_backend = {