2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 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 driver/disorder.c
21 * @brief libao driver used by DisOrder
23 * The output from this driver is expected to be fed to @c
24 * disorder-normalize to convert to the confnigured target format.
36 #include <ao/plugin.h>
38 #include "speaker-protocol.h"
40 /* extra declarations to help out lazy <ao/plugin.h> */
41 int ao_plugin_test(void);
42 ao_info *ao_plugin_driver_info(void);
43 char *ao_plugin_file_extension(void);
45 /** @brief Private data structure for this driver */
47 int fd; /* output file descriptor */
48 int exit_on_error; /* exit on write error */
50 /** @brief Record of sample format */
51 struct stream_header header;
55 /* like write() but never returns EINTR/EAGAIN or short */
56 static int do_write(int fd, const void *ptr, size_t n) {
61 memset(&ufd, 0, sizeof ufd);
65 ret = write(fd, (const char *)ptr + written, n - written);
70 /* Someone sneakily gave us a nonblocking file descriptor, wait until
71 * we can write again */
72 ret = poll(&ufd, 1, -1);
73 if(ret < 0 && errno != EINTR) return -1;
84 /* return 1 if this driver can be opened */
85 int ao_plugin_test(void) {
89 /* return info about this driver */
90 ao_info *ao_plugin_driver_info(void) {
91 static const char *options[] = { "fd" };
92 static const ao_info info = {
93 AO_TYPE_LIVE, /* type */
94 (char *)"DisOrder format driver", /* name */
95 (char *)"disorder", /* short_name */
96 (char *)"http://www.greenend.org.uk/rjk/disorder/", /* comment */
97 (char *)"Richard Kettlewell", /* author */
98 AO_FMT_NATIVE, /* preferred_byte_format */
100 (char **)options, /* options */
101 1, /* option_count */
103 return (ao_info *)&info;
106 /* initialize the private data structure */
107 int ao_plugin_device_init(ao_device *device) {
108 struct internal *i = malloc(sizeof (struct internal));
112 memset(i, 0, sizeof *i);
113 if((e = getenv("DISORDER_RAW_FD")))
117 device->internal = i;
122 int ao_plugin_set_option(ao_device *device,
125 struct internal *i = device->internal;
127 if(!strcmp(key, "fd"))
129 else if(!strcmp(key, "fragile"))
130 i->exit_on_error = atoi(value);
131 /* unknown options are required to be ignored */
135 /* open the device */
136 int ao_plugin_open(ao_device *device, ao_sample_format *format) {
137 struct internal *i = device->internal;
139 /* we would like native-order samples */
140 device->driver_byte_format = AO_FMT_NATIVE;
141 i->header.rate = format->rate;
142 i->header.channels = format->channels;
143 i->header.bits = format->bits;
144 i->header.endian = ENDIAN_NATIVE;
148 /* play some samples */
149 int ao_plugin_play(ao_device *device, const char *output_samples,
151 struct internal *i = device->internal;
153 /* Fill in and write the header */
154 i->header.nbytes = num_bytes;
155 if(do_write(i->fd, &i->header, sizeof i->header) < 0) {
156 if(i->exit_on_error) _exit(-1);
160 /* Write the sample data */
161 if(do_write(i->fd, output_samples, num_bytes) < 0) {
162 if(i->exit_on_error) _exit(-1);
168 /* close the device */
169 int ao_plugin_close(ao_device attribute((unused)) *device) {
173 /* delete private data structures */
174 void ao_plugin_device_clear(ao_device *device) {
175 free(device->internal);
176 device->internal = 0;
179 /* report preferred filename extension */
180 char *ao_plugin_file_extension(void) {